Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(261476)

Delta Between Two Patch Sets: Modules/binascii.c

Issue 25357: Add an optional newline parameter to binascii.b2a_base64() to not add a newline
Left Patch Set: Created 3 years, 10 months ago
Right Patch Set: Created 3 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 ** Routines to represent binary data in ASCII and vice-versa 2 ** Routines to represent binary data in ASCII and vice-versa
3 ** 3 **
4 ** This module currently supports the following encodings: 4 ** This module currently supports the following encodings:
5 ** uuencode: 5 ** uuencode:
6 ** each line encodes 45 bytes (except possibly the last) 6 ** each line encodes 45 bytes (except possibly the last)
7 ** First char encodes (binary) length, rest data 7 ** First char encodes (binary) length, rest data
8 ** each char encodes 6 bits, as follows: 8 ** each char encodes 6 bits, as follows:
9 ** binary: 01234567 abcdefgh ijklmnop 9 ** binary: 01234567 abcdefgh ijklmnop
10 ** ascii: 012345 67abcd efghij klmnop 10 ** ascii: 012345 67abcd efghij klmnop
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 } 522 }
523 return rv; 523 return rv;
524 } 524 }
525 525
526 526
527 /*[clinic input] 527 /*[clinic input]
528 binascii.b2a_base64 528 binascii.b2a_base64
529 529
530 data: Py_buffer 530 data: Py_buffer
531 * 531 *
532 newline: int = 1 532 newline: int(c_default="1") = True
storchaka 2015/10/09 18:35:27 newline: int(c_default="1") = True
haypo 2015/10/11 11:01:59 Done.
533 533
534 Base64-code line of data. 534 Base64-code line of data.
535 [clinic start generated code]*/ 535 [clinic start generated code]*/
536 536
537 static PyObject * 537 static PyObject *
538 binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data, int newline) 538 binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data, int newline)
539 /*[clinic end generated code: output=19e1dd719a890b50 input=cfca8726e4351336]*/ 539 /*[clinic end generated code: output=19e1dd719a890b50 input=7b2ea6fa38d8924c]*/
540 { 540 {
541 unsigned char *ascii_data, *bin_data; 541 unsigned char *ascii_data, *bin_data;
542 int leftbits = 0; 542 int leftbits = 0;
543 unsigned char this_ch; 543 unsigned char this_ch;
544 unsigned int leftchar = 0; 544 unsigned int leftchar = 0;
545 PyObject *rv; 545 PyObject *rv;
546 Py_ssize_t bin_len, out_len; 546 Py_ssize_t bin_len, out_len;
547 547
548 bin_data = data->buf; 548 bin_data = data->buf;
549 bin_len = data->len; 549 bin_len = data->len;
550 550
551 assert(bin_len >= 0); 551 assert(bin_len >= 0);
552 552
553 if ( bin_len > BASE64_MAXBIN ) { 553 if ( bin_len > BASE64_MAXBIN ) {
554 PyErr_SetString(Error, "Too much data for base64 line"); 554 PyErr_SetString(Error, "Too much data for base64 line");
555 return NULL; 555 return NULL;
556 } 556 }
557 557
558 /* We're lazy and allocate too much (fixed up later). 558 /* We're lazy and allocate too much (fixed up later).
559 "+3" leaves room for up to two pad characters and a trailing 559 "+3" leaves room for up to two pad characters and a trailing
Martin Panter 2015/10/10 06:00:10 With newline handled separately, this would make m
560 newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */ 560 newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
561 out_len = bin_len*2 + 2; 561 out_len = bin_len*2 + 2;
562 if (newline) 562 if (newline)
563 out_len++; 563 out_len++;
564 if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL ) 564 if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL )
565 return NULL; 565 return NULL;
566 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); 566 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
567 567
568 for( ; bin_len > 0 ; bin_len--, bin_data++ ) { 568 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
569 /* Shift the data into our buffer */ 569 /* Shift the data into our buffer */
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after
1587 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL); 1587 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
1588 PyDict_SetItemString(d, "Error", Error); 1588 PyDict_SetItemString(d, "Error", Error);
1589 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); 1589 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
1590 PyDict_SetItemString(d, "Incomplete", Incomplete); 1590 PyDict_SetItemString(d, "Incomplete", Incomplete);
1591 if (PyErr_Occurred()) { 1591 if (PyErr_Occurred()) {
1592 Py_DECREF(m); 1592 Py_DECREF(m);
1593 m = NULL; 1593 m = NULL;
1594 } 1594 }
1595 return m; 1595 return m;
1596 } 1596 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+