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

Side by Side Diff: Modules/binascii.c

Issue 25357: Add an optional newline parameter to binascii.b2a_base64() to not add a newline
Patch Set: Created 4 years, 1 month 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:
View unified diff | Download patch
OLDNEW
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 510 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 rv = PyBytes_FromStringAndSize("", 0); 521 rv = PyBytes_FromStringAndSize("", 0);
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(c_default="1") = True
532 533
533 Base64-code line of data. 534 Base64-code line of data.
534 [clinic start generated code]*/ 535 [clinic start generated code]*/
535 536
536 static PyObject * 537 static PyObject *
537 binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data) 538 binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data, int newline)
538 /*[clinic end generated code: output=3cd61fbee2913285 input=14ec4e47371174a9]*/ 539 /*[clinic end generated code: output=19e1dd719a890b50 input=7b2ea6fa38d8924c]*/
539 { 540 {
540 unsigned char *ascii_data, *bin_data; 541 unsigned char *ascii_data, *bin_data;
541 int leftbits = 0; 542 int leftbits = 0;
542 unsigned char this_ch; 543 unsigned char this_ch;
543 unsigned int leftchar = 0; 544 unsigned int leftchar = 0;
544 PyObject *rv; 545 PyObject *rv;
545 Py_ssize_t bin_len; 546 Py_ssize_t bin_len, out_len;
546 547
547 bin_data = data->buf; 548 bin_data = data->buf;
548 bin_len = data->len; 549 bin_len = data->len;
549 550
550 assert(bin_len >= 0); 551 assert(bin_len >= 0);
551 552
552 if ( bin_len > BASE64_MAXBIN ) { 553 if ( bin_len > BASE64_MAXBIN ) {
553 PyErr_SetString(Error, "Too much data for base64 line"); 554 PyErr_SetString(Error, "Too much data for base64 line");
554 return NULL; 555 return NULL;
555 } 556 }
556 557
557 /* We're lazy and allocate too much (fixed up later). 558 /* We're lazy and allocate too much (fixed up later).
558 "+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
559 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). */
560 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) 561 out_len = bin_len*2 + 2;
562 if (newline)
563 out_len++;
564 if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL )
561 return NULL; 565 return NULL;
562 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); 566 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
563 567
564 for( ; bin_len > 0 ; bin_len--, bin_data++ ) { 568 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
565 /* Shift the data into our buffer */ 569 /* Shift the data into our buffer */
566 leftchar = (leftchar << 8) | *bin_data; 570 leftchar = (leftchar << 8) | *bin_data;
567 leftbits += 8; 571 leftbits += 8;
568 572
569 /* See if there are 6-bit groups ready */ 573 /* See if there are 6-bit groups ready */
570 while ( leftbits >= 6 ) { 574 while ( leftbits >= 6 ) {
571 this_ch = (leftchar >> (leftbits-6)) & 0x3f; 575 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
572 leftbits -= 6; 576 leftbits -= 6;
573 *ascii_data++ = table_b2a_base64[this_ch]; 577 *ascii_data++ = table_b2a_base64[this_ch];
574 } 578 }
575 } 579 }
576 if ( leftbits == 2 ) { 580 if ( leftbits == 2 ) {
577 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4]; 581 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
578 *ascii_data++ = BASE64_PAD; 582 *ascii_data++ = BASE64_PAD;
579 *ascii_data++ = BASE64_PAD; 583 *ascii_data++ = BASE64_PAD;
580 } else if ( leftbits == 4 ) { 584 } else if ( leftbits == 4 ) {
581 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2]; 585 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
582 *ascii_data++ = BASE64_PAD; 586 *ascii_data++ = BASE64_PAD;
583 } 587 }
584 *ascii_data++ = '\n'; /* Append a courtesy newline */ 588 if (newline)
589 *ascii_data++ = '\n'; /* Append a courtesy newline */
585 590
586 if (_PyBytes_Resize(&rv, 591 if (_PyBytes_Resize(&rv,
587 (ascii_data - 592 (ascii_data -
588 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { 593 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
589 Py_CLEAR(rv); 594 Py_CLEAR(rv);
590 } 595 }
591 return rv; 596 return rv;
592 } 597 }
593 598
594 /*[clinic input] 599 /*[clinic input]
(...skipping 987 matching lines...) Expand 10 before | Expand all | Expand 10 after
1582 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL); 1587 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
1583 PyDict_SetItemString(d, "Error", Error); 1588 PyDict_SetItemString(d, "Error", Error);
1584 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); 1589 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
1585 PyDict_SetItemString(d, "Incomplete", Incomplete); 1590 PyDict_SetItemString(d, "Incomplete", Incomplete);
1586 if (PyErr_Occurred()) { 1591 if (PyErr_Occurred()) {
1587 Py_DECREF(m); 1592 Py_DECREF(m);
1588 m = NULL; 1593 m = NULL;
1589 } 1594 }
1590 return m; 1595 return m;
1591 } 1596 }
OLDNEW

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