Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Routines to represent binary data in ASCII and vice-versa |
| 3 | ** |
| 4 | ** This module currently supports the following encodings: |
| 5 | ** uuencode: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6 | ** each line encodes 45 bytes (except possibly the last) |
| 7 | ** First char encodes (binary) length, rest data |
| 8 | ** each char encodes 6 bits, as follows: |
| 9 | ** binary: 01234567 abcdefgh ijklmnop |
| 10 | ** ascii: 012345 67abcd efghij klmnop |
| 11 | ** ASCII encoding method is "excess-space": 000000 is encoded as ' ', etc. |
| 12 | ** short binary data is zero-extended (so the bits are always in the |
| 13 | ** right place), this does *not* reflect in the length. |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 14 | ** base64: |
| 15 | ** Line breaks are insignificant, but lines are at most 76 chars |
| 16 | ** each char encodes 6 bits, in similar order as uucode/hqx. Encoding |
| 17 | ** is done via a table. |
| 18 | ** Short binary data is filled (in ASCII) with '='. |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 19 | ** hqx: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | ** File starts with introductory text, real data starts and ends |
| 21 | ** with colons. |
| 22 | ** Data consists of three similar parts: info, datafork, resourcefork. |
| 23 | ** Each part is protected (at the end) with a 16-bit crc |
| 24 | ** The binary data is run-length encoded, and then ascii-fied: |
| 25 | ** binary: 01234567 abcdefgh ijklmnop |
| 26 | ** ascii: 012345 67abcd efghij klmnop |
| 27 | ** ASCII encoding is table-driven, see the code. |
| 28 | ** Short binary data results in the runt ascii-byte being output with |
| 29 | ** the bits in the right place. |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 30 | ** |
| 31 | ** While I was reading dozens of programs that encode or decode the formats |
| 32 | ** here (documentation? hihi:-) I have formulated Jansen's Observation: |
| 33 | ** |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | ** Programs that encode binary data in ASCII are written in |
| 35 | ** such a style that they are as unreadable as possible. Devices used |
| 36 | ** include unnecessary global variables, burying important tables |
| 37 | ** in unrelated sourcefiles, putting functions in include files, |
| 38 | ** using seemingly-descriptive variable names for different purposes, |
| 39 | ** calls to empty subroutines and a host of others. |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 40 | ** |
| 41 | ** I have attempted to break with this tradition, but I guess that that |
| 42 | ** does make the performance sub-optimal. Oh well, too bad... |
| 43 | ** |
| 44 | ** Jack Jansen, CWI, July 1995. |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 45 | ** |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 46 | ** Added support for quoted-printable encoding, based on rfc 1521 et al |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 47 | ** quoted-printable encoding specifies that non printable characters (anything |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 48 | ** below 32 and above 126) be encoded as =XX where XX is the hexadecimal value |
| 49 | ** of the character. It also specifies some other behavior to enable 8bit data |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 50 | ** in a mail message with little difficulty (maximum line sizes, protecting |
| 51 | ** some cases of whitespace, etc). |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 52 | ** |
| 53 | ** Brandon Long, September 2001. |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 54 | */ |
| 55 | |
Thomas Wouters | 9c54448 | 2006-03-01 21:59:44 +0000 | [diff] [blame] | 56 | #define PY_SSIZE_T_CLEAN |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 57 | |
| 58 | #include "Python.h" |
Christian Heimes | 1dc5400 | 2008-03-24 02:19:29 +0000 | [diff] [blame] | 59 | #ifdef USE_ZLIB_CRC32 |
| 60 | #include "zlib.h" |
| 61 | #endif |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 62 | |
| 63 | static PyObject *Error; |
| 64 | static PyObject *Incomplete; |
| 65 | |
| 66 | /* |
| 67 | ** hqx lookup table, ascii->binary. |
| 68 | */ |
| 69 | |
| 70 | #define RUNCHAR 0x90 |
| 71 | |
| 72 | #define DONE 0x7F |
| 73 | #define SKIP 0x7E |
| 74 | #define FAIL 0x7D |
| 75 | |
| 76 | static unsigned char table_a2b_hqx[256] = { |
| 77 | /* ^@ ^A ^B ^C ^D ^E ^F ^G */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | /* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 79 | /* \b \t \n ^K ^L \r ^N ^O */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | /* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 81 | /* ^P ^Q ^R ^S ^T ^U ^V ^W */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | /* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 83 | /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | /* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 85 | /* ! " # $ % & ' */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | /* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 87 | /* ( ) * + , - . / */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | /* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 89 | /* 0 1 2 3 4 5 6 7 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | /* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 91 | /* 8 9 : ; < = > ? */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | /* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 93 | /* @ A B C D E F G */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | /* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 95 | /* H I J K L M N O */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | /* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 97 | /* P Q R S T U V W */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | /*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 99 | /* X Y Z [ \ ] ^ _ */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | /*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 101 | /* ` a b c d e f g */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | /*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 103 | /* h i j k l m n o */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | /*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 105 | /* p q r s t u v w */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | /*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 107 | /* x y z { | } ~ ^? */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | /*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 109 | /*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 110 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 111 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 112 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 113 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 114 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 115 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 116 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 117 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 118 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 119 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 120 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 121 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 122 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 123 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
| 124 | FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static unsigned char table_b2a_hqx[] = |
Roger E. Masse | 5f4ce18 | 1997-01-16 17:10:22 +0000 | [diff] [blame] | 128 | "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr"; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 129 | |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 130 | static char table_a2b_base64[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 132 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 133 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, |
| 134 | 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */ |
| 135 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, |
| 136 | 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, |
| 137 | -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, |
| 138 | 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #define BASE64_PAD '=' |
Guido van Rossum | 355bc0c | 2001-10-30 03:00:52 +0000 | [diff] [blame] | 142 | |
| 143 | /* Max binary chunk size; limited only by available memory */ |
Guido van Rossum | 0e225aa | 2007-05-22 20:24:57 +0000 | [diff] [blame] | 144 | #define BASE64_MAXBIN ((PY_SSIZE_T_MAX - 3) / 2) |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 145 | |
| 146 | static unsigned char table_b2a_base64[] = |
Roger E. Masse | 5f4ce18 | 1997-01-16 17:10:22 +0000 | [diff] [blame] | 147 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 151 | static unsigned short crctab_hqx[256] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, |
| 153 | 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, |
| 154 | 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, |
| 155 | 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, |
| 156 | 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, |
| 157 | 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, |
| 158 | 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, |
| 159 | 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, |
| 160 | 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, |
| 161 | 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, |
| 162 | 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, |
| 163 | 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, |
| 164 | 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, |
| 165 | 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, |
| 166 | 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, |
| 167 | 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, |
| 168 | 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, |
| 169 | 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, |
| 170 | 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, |
| 171 | 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, |
| 172 | 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, |
| 173 | 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
| 174 | 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, |
| 175 | 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, |
| 176 | 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, |
| 177 | 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, |
| 178 | 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, |
| 179 | 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, |
| 180 | 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, |
| 181 | 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, |
| 182 | 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, |
| 183 | 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0, |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 186 | /*[clinic input] |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 187 | module binascii |
| 188 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 189 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=de89fb46bcaf3fec]*/ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 190 | |
| 191 | /*[python input] |
| 192 | |
| 193 | class ascii_buffer_converter(CConverter): |
| 194 | type = 'Py_buffer' |
| 195 | converter = 'ascii_buffer_converter' |
| 196 | impl_by_reference = True |
Benjamin Peterson | b62deac | 2014-01-26 10:41:58 -0500 | [diff] [blame] | 197 | c_default = "{NULL, NULL}" |
| 198 | |
| 199 | def cleanup(self): |
| 200 | name = self.name |
| 201 | return "".join(["if (", name, ".obj)\n PyBuffer_Release(&", name, ");\n"]) |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 202 | |
| 203 | [python start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 204 | /*[python end generated code: output=da39a3ee5e6b4b0d input=3eb7b63610da92cd]*/ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 205 | |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 206 | static int |
| 207 | ascii_buffer_converter(PyObject *arg, Py_buffer *buf) |
| 208 | { |
| 209 | if (arg == NULL) { |
| 210 | PyBuffer_Release(buf); |
| 211 | return 1; |
| 212 | } |
| 213 | if (PyUnicode_Check(arg)) { |
| 214 | if (PyUnicode_READY(arg) < 0) |
| 215 | return 0; |
| 216 | if (!PyUnicode_IS_ASCII(arg)) { |
| 217 | PyErr_SetString(PyExc_ValueError, |
| 218 | "string argument should contain only ASCII characters"); |
| 219 | return 0; |
| 220 | } |
| 221 | assert(PyUnicode_KIND(arg) == PyUnicode_1BYTE_KIND); |
| 222 | buf->buf = (void *) PyUnicode_1BYTE_DATA(arg); |
| 223 | buf->len = PyUnicode_GET_LENGTH(arg); |
| 224 | buf->obj = NULL; |
| 225 | return 1; |
| 226 | } |
| 227 | if (PyObject_GetBuffer(arg, buf, PyBUF_SIMPLE) != 0) { |
| 228 | PyErr_Format(PyExc_TypeError, |
| 229 | "argument should be bytes, buffer or ASCII string, " |
Berker Peksag | 3cd30c2 | 2015-02-15 00:31:00 +0200 | [diff] [blame] | 230 | "not '%.100s'", Py_TYPE(arg)->tp_name); |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | if (!PyBuffer_IsContiguous(buf, 'C')) { |
| 234 | PyErr_Format(PyExc_TypeError, |
| 235 | "argument should be a contiguous buffer, " |
Berker Peksag | 3cd30c2 | 2015-02-15 00:31:00 +0200 | [diff] [blame] | 236 | "not '%.100s'", Py_TYPE(arg)->tp_name); |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 237 | PyBuffer_Release(buf); |
| 238 | return 0; |
| 239 | } |
| 240 | return Py_CLEANUP_SUPPORTED; |
| 241 | } |
| 242 | |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 243 | #include "clinic/binascii.c.h" |
Antoine Pitrou | 0831676 | 2011-12-20 13:58:41 +0100 | [diff] [blame] | 244 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 245 | /*[clinic input] |
| 246 | binascii.a2b_uu |
| 247 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 248 | data: ascii_buffer |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 249 | / |
| 250 | |
| 251 | Decode a line of uuencoded data. |
| 252 | [clinic start generated code]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 253 | |
| 254 | static PyObject * |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 255 | binascii_a2b_uu_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 256 | /*[clinic end generated code: output=5779f39b0b48459f input=7cafeaf73df63d1c]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | unsigned char *ascii_data, *bin_data; |
| 259 | int leftbits = 0; |
| 260 | unsigned char this_ch; |
| 261 | unsigned int leftchar = 0; |
| 262 | PyObject *rv; |
| 263 | Py_ssize_t ascii_len, bin_len; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 264 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 265 | ascii_data = data->buf; |
| 266 | ascii_len = data->len; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | assert(ascii_len >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | /* First byte: binary data length (in bytes) */ |
| 271 | bin_len = (*ascii_data++ - ' ') & 077; |
| 272 | ascii_len--; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | /* Allocate the buffer */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 275 | if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | bin_data = (unsigned char *)PyBytes_AS_STRING(rv); |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) { |
| 280 | /* XXX is it really best to add NULs if there's no more data */ |
| 281 | this_ch = (ascii_len > 0) ? *ascii_data : 0; |
| 282 | if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) { |
| 283 | /* |
| 284 | ** Whitespace. Assume some spaces got eaten at |
| 285 | ** end-of-line. (We check this later) |
| 286 | */ |
| 287 | this_ch = 0; |
| 288 | } else { |
| 289 | /* Check the character for legality |
| 290 | ** The 64 in stead of the expected 63 is because |
| 291 | ** there are a few uuencodes out there that use |
| 292 | ** '`' as zero instead of space. |
| 293 | */ |
| 294 | if ( this_ch < ' ' || this_ch > (' ' + 64)) { |
| 295 | PyErr_SetString(Error, "Illegal char"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | Py_DECREF(rv); |
| 297 | return NULL; |
| 298 | } |
| 299 | this_ch = (this_ch - ' ') & 077; |
| 300 | } |
| 301 | /* |
| 302 | ** Shift it in on the low end, and see if there's |
| 303 | ** a byte ready for output. |
| 304 | */ |
| 305 | leftchar = (leftchar << 6) | (this_ch); |
| 306 | leftbits += 6; |
| 307 | if ( leftbits >= 8 ) { |
| 308 | leftbits -= 8; |
| 309 | *bin_data++ = (leftchar >> leftbits) & 0xff; |
| 310 | leftchar &= ((1 << leftbits) - 1); |
| 311 | bin_len--; |
| 312 | } |
| 313 | } |
| 314 | /* |
| 315 | ** Finally, check that if there's anything left on the line |
| 316 | ** that it's whitespace only. |
| 317 | */ |
| 318 | while( ascii_len-- > 0 ) { |
| 319 | this_ch = *ascii_data++; |
| 320 | /* Extra '`' may be written as padding in some cases */ |
| 321 | if ( this_ch != ' ' && this_ch != ' '+64 && |
| 322 | this_ch != '\n' && this_ch != '\r' ) { |
| 323 | PyErr_SetString(Error, "Trailing garbage"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | Py_DECREF(rv); |
| 325 | return NULL; |
| 326 | } |
| 327 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | return rv; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 331 | /*[clinic input] |
| 332 | binascii.b2a_uu |
| 333 | |
| 334 | data: Py_buffer |
| 335 | / |
| 336 | |
| 337 | Uuencode line of data. |
| 338 | [clinic start generated code]*/ |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 339 | |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 340 | static PyObject * |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 341 | binascii_b2a_uu_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 342 | /*[clinic end generated code: output=181021b69bb9a414 input=00fdf458ce8b465b]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | unsigned char *ascii_data, *bin_data; |
| 345 | int leftbits = 0; |
| 346 | unsigned char this_ch; |
| 347 | unsigned int leftchar = 0; |
| 348 | PyObject *rv; |
| 349 | Py_ssize_t bin_len; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 350 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 351 | bin_data = data->buf; |
| 352 | bin_len = data->len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | if ( bin_len > 45 ) { |
| 354 | /* The 45 is a limit that appears in all uuencode's */ |
| 355 | PyErr_SetString(Error, "At most 45 bytes at once"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | /* We're lazy and allocate to much (fixed up later) */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 360 | if ( (rv=PyBytes_FromStringAndSize(NULL, 2 + (bin_len+2)/3*4)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | /* Store the length */ |
| 365 | *ascii_data++ = ' ' + (bin_len & 077); |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) { |
| 368 | /* Shift the data (or padding) into our buffer */ |
| 369 | if ( bin_len > 0 ) /* Data */ |
| 370 | leftchar = (leftchar << 8) | *bin_data; |
| 371 | else /* Padding */ |
| 372 | leftchar <<= 8; |
| 373 | leftbits += 8; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | /* See if there are 6-bit groups ready */ |
| 376 | while ( leftbits >= 6 ) { |
| 377 | this_ch = (leftchar >> (leftbits-6)) & 0x3f; |
| 378 | leftbits -= 6; |
| 379 | *ascii_data++ = this_ch + ' '; |
| 380 | } |
| 381 | } |
| 382 | *ascii_data++ = '\n'; /* Append a courtesy newline */ |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | if (_PyBytes_Resize(&rv, |
| 385 | (ascii_data - |
| 386 | (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 387 | Py_CLEAR(rv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | return rv; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 392 | |
| 393 | static int |
Thomas Wouters | f98db65 | 2006-03-01 21:37:32 +0000 | [diff] [blame] | 394 | binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num) |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | /* Finds & returns the (num+1)th |
| 397 | ** valid character for base64, or -1 if none. |
| 398 | */ |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | int ret = -1; |
| 401 | unsigned char c, b64val; |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | while ((slen > 0) && (ret == -1)) { |
| 404 | c = *s; |
| 405 | b64val = table_a2b_base64[c & 0x7f]; |
| 406 | if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) { |
| 407 | if (num == 0) |
| 408 | ret = *s; |
| 409 | num--; |
| 410 | } |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | s++; |
| 413 | slen--; |
| 414 | } |
| 415 | return ret; |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 418 | /*[clinic input] |
| 419 | binascii.a2b_base64 |
| 420 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 421 | data: ascii_buffer |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 422 | / |
| 423 | |
| 424 | Decode a line of base64 data. |
| 425 | [clinic start generated code]*/ |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 426 | |
| 427 | static PyObject * |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 428 | binascii_a2b_base64_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 429 | /*[clinic end generated code: output=3e351b702bed56d2 input=5872acf6e1cac243]*/ |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | unsigned char *ascii_data, *bin_data; |
| 432 | int leftbits = 0; |
| 433 | unsigned char this_ch; |
| 434 | unsigned int leftchar = 0; |
| 435 | PyObject *rv; |
| 436 | Py_ssize_t ascii_len, bin_len; |
| 437 | int quad_pos = 0; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 438 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 439 | ascii_data = data->buf; |
| 440 | ascii_len = data->len; |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | assert(ascii_len >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 443 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 444 | if (ascii_len > PY_SSIZE_T_MAX - 3) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | return PyErr_NoMemory(); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */ |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | /* Allocate the buffer */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 450 | if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | bin_data = (unsigned char *)PyBytes_AS_STRING(rv); |
| 453 | bin_len = 0; |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | for( ; ascii_len > 0; ascii_len--, ascii_data++) { |
| 456 | this_ch = *ascii_data; |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | if (this_ch > 0x7f || |
| 459 | this_ch == '\r' || this_ch == '\n' || this_ch == ' ') |
| 460 | continue; |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | /* Check for pad sequences and ignore |
| 463 | ** the invalid ones. |
| 464 | */ |
| 465 | if (this_ch == BASE64_PAD) { |
| 466 | if ( (quad_pos < 2) || |
| 467 | ((quad_pos == 2) && |
| 468 | (binascii_find_valid(ascii_data, ascii_len, 1) |
| 469 | != BASE64_PAD)) ) |
| 470 | { |
| 471 | continue; |
| 472 | } |
| 473 | else { |
| 474 | /* A pad sequence means no more input. |
| 475 | ** We've already interpreted the data |
| 476 | ** from the quad at this point. |
| 477 | */ |
| 478 | leftbits = 0; |
| 479 | break; |
| 480 | } |
| 481 | } |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | this_ch = table_a2b_base64[*ascii_data]; |
| 484 | if ( this_ch == (unsigned char) -1 ) |
| 485 | continue; |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | /* |
| 488 | ** Shift it in on the low end, and see if there's |
| 489 | ** a byte ready for output. |
| 490 | */ |
| 491 | quad_pos = (quad_pos + 1) & 0x03; |
| 492 | leftchar = (leftchar << 6) | (this_ch); |
| 493 | leftbits += 6; |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | if ( leftbits >= 8 ) { |
| 496 | leftbits -= 8; |
| 497 | *bin_data++ = (leftchar >> leftbits) & 0xff; |
| 498 | bin_len++; |
| 499 | leftchar &= ((1 << leftbits) - 1); |
| 500 | } |
| 501 | } |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | if (leftbits != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | PyErr_SetString(Error, "Incorrect padding"); |
| 505 | Py_DECREF(rv); |
| 506 | return NULL; |
| 507 | } |
Guido van Rossum | 2db4f47 | 1999-10-19 19:05:14 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | /* And set string size correctly. If the result string is empty |
| 510 | ** (because the input was all invalid) return the shared empty |
| 511 | ** string instead; _PyBytes_Resize() won't do this for us. |
| 512 | */ |
| 513 | if (bin_len > 0) { |
| 514 | if (_PyBytes_Resize(&rv, bin_len) < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 515 | Py_CLEAR(rv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | else { |
| 519 | Py_DECREF(rv); |
| 520 | rv = PyBytes_FromStringAndSize("", 0); |
| 521 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | return rv; |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 525 | |
| 526 | /*[clinic input] |
| 527 | binascii.b2a_base64 |
| 528 | |
| 529 | data: Py_buffer |
| 530 | / |
| 531 | |
| 532 | Base64-code line of data. |
| 533 | [clinic start generated code]*/ |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 534 | |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 535 | static PyObject * |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 536 | binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 537 | /*[clinic end generated code: output=3cd61fbee2913285 input=14ec4e47371174a9]*/ |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | unsigned char *ascii_data, *bin_data; |
| 540 | int leftbits = 0; |
| 541 | unsigned char this_ch; |
| 542 | unsigned int leftchar = 0; |
| 543 | PyObject *rv; |
| 544 | Py_ssize_t bin_len; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 545 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 546 | bin_data = data->buf; |
| 547 | bin_len = data->len; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | assert(bin_len >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | if ( bin_len > BASE64_MAXBIN ) { |
| 552 | PyErr_SetString(Error, "Too much data for base64 line"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | return NULL; |
| 554 | } |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | /* We're lazy and allocate too much (fixed up later). |
| 557 | "+3" leaves room for up to two pad characters and a trailing |
| 558 | newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 559 | if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | for( ; bin_len > 0 ; bin_len--, bin_data++ ) { |
| 564 | /* Shift the data into our buffer */ |
| 565 | leftchar = (leftchar << 8) | *bin_data; |
| 566 | leftbits += 8; |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | /* See if there are 6-bit groups ready */ |
| 569 | while ( leftbits >= 6 ) { |
| 570 | this_ch = (leftchar >> (leftbits-6)) & 0x3f; |
| 571 | leftbits -= 6; |
| 572 | *ascii_data++ = table_b2a_base64[this_ch]; |
| 573 | } |
| 574 | } |
| 575 | if ( leftbits == 2 ) { |
| 576 | *ascii_data++ = table_b2a_base64[(leftchar&3) << 4]; |
| 577 | *ascii_data++ = BASE64_PAD; |
| 578 | *ascii_data++ = BASE64_PAD; |
| 579 | } else if ( leftbits == 4 ) { |
| 580 | *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2]; |
| 581 | *ascii_data++ = BASE64_PAD; |
| 582 | } |
| 583 | *ascii_data++ = '\n'; /* Append a courtesy newline */ |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | if (_PyBytes_Resize(&rv, |
| 586 | (ascii_data - |
| 587 | (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 588 | Py_CLEAR(rv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | return rv; |
Jack Jansen | 84bbc2e | 1995-10-04 16:38:44 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 593 | /*[clinic input] |
| 594 | binascii.a2b_hqx |
| 595 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 596 | data: ascii_buffer |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 597 | / |
| 598 | |
| 599 | Decode .hqx coding. |
| 600 | [clinic start generated code]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 601 | |
| 602 | static PyObject * |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 603 | binascii_a2b_hqx_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 604 | /*[clinic end generated code: output=60bcdbbd28b105cd input=0d914c680e0eed55]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 605 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | unsigned char *ascii_data, *bin_data; |
| 607 | int leftbits = 0; |
| 608 | unsigned char this_ch; |
| 609 | unsigned int leftchar = 0; |
| 610 | PyObject *rv; |
| 611 | Py_ssize_t len; |
| 612 | int done = 0; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 613 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 614 | ascii_data = data->buf; |
| 615 | len = data->len; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | assert(len >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 618 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 619 | if (len > PY_SSIZE_T_MAX - 2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | return PyErr_NoMemory(); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | /* Allocate a string that is too big (fixed later) |
| 623 | Add two to the initial length to prevent interning which |
| 624 | would preclude subsequent resizing. */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 625 | if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | bin_data = (unsigned char *)PyBytes_AS_STRING(rv); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | for( ; len > 0 ; len--, ascii_data++ ) { |
| 630 | /* Get the byte and look it up */ |
| 631 | this_ch = table_a2b_hqx[*ascii_data]; |
| 632 | if ( this_ch == SKIP ) |
| 633 | continue; |
| 634 | if ( this_ch == FAIL ) { |
| 635 | PyErr_SetString(Error, "Illegal char"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | Py_DECREF(rv); |
| 637 | return NULL; |
| 638 | } |
| 639 | if ( this_ch == DONE ) { |
| 640 | /* The terminating colon */ |
| 641 | done = 1; |
| 642 | break; |
| 643 | } |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | /* Shift it into the buffer and see if any bytes are ready */ |
| 646 | leftchar = (leftchar << 6) | (this_ch); |
| 647 | leftbits += 6; |
| 648 | if ( leftbits >= 8 ) { |
| 649 | leftbits -= 8; |
| 650 | *bin_data++ = (leftchar >> leftbits) & 0xff; |
| 651 | leftchar &= ((1 << leftbits) - 1); |
| 652 | } |
| 653 | } |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | if ( leftbits && !done ) { |
| 656 | PyErr_SetString(Incomplete, |
| 657 | "String has incomplete number of bytes"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | Py_DECREF(rv); |
| 659 | return NULL; |
| 660 | } |
| 661 | if (_PyBytes_Resize(&rv, |
| 662 | (bin_data - |
| 663 | (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 664 | Py_CLEAR(rv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | } |
| 666 | if (rv) { |
| 667 | PyObject *rrv = Py_BuildValue("Oi", rv, done); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | Py_DECREF(rv); |
| 669 | return rrv; |
| 670 | } |
Roger E. Masse | 5f4ce18 | 1997-01-16 17:10:22 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | return NULL; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 675 | |
| 676 | /*[clinic input] |
| 677 | binascii.rlecode_hqx |
| 678 | |
| 679 | data: Py_buffer |
| 680 | / |
| 681 | |
| 682 | Binhex RLE-code binary data. |
| 683 | [clinic start generated code]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 684 | |
| 685 | static PyObject * |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 686 | binascii_rlecode_hqx_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 687 | /*[clinic end generated code: output=0905da344dbf0648 input=e1f1712447a82b09]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | unsigned char *in_data, *out_data; |
| 690 | PyObject *rv; |
| 691 | unsigned char ch; |
| 692 | Py_ssize_t in, inend, len; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 693 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 694 | in_data = data->buf; |
| 695 | len = data->len; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | assert(len >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 698 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 699 | if (len > PY_SSIZE_T_MAX / 2 - 2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | return PyErr_NoMemory(); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | /* Worst case: output is twice as big as input (fixed later) */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 703 | if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | out_data = (unsigned char *)PyBytes_AS_STRING(rv); |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | for( in=0; in<len; in++) { |
| 708 | ch = in_data[in]; |
| 709 | if ( ch == RUNCHAR ) { |
| 710 | /* RUNCHAR. Escape it. */ |
| 711 | *out_data++ = RUNCHAR; |
| 712 | *out_data++ = 0; |
| 713 | } else { |
| 714 | /* Check how many following are the same */ |
| 715 | for(inend=in+1; |
| 716 | inend<len && in_data[inend] == ch && |
| 717 | inend < in+255; |
| 718 | inend++) ; |
| 719 | if ( inend - in > 3 ) { |
| 720 | /* More than 3 in a row. Output RLE. */ |
| 721 | *out_data++ = ch; |
| 722 | *out_data++ = RUNCHAR; |
Antoine Pitrou | 4045575 | 2010-08-15 18:51:10 +0000 | [diff] [blame] | 723 | *out_data++ = (unsigned char) (inend-in); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | in = inend-1; |
| 725 | } else { |
| 726 | /* Less than 3. Output the byte itself */ |
| 727 | *out_data++ = ch; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | if (_PyBytes_Resize(&rv, |
| 732 | (out_data - |
| 733 | (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 734 | Py_CLEAR(rv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | return rv; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 739 | |
| 740 | /*[clinic input] |
| 741 | binascii.b2a_hqx |
| 742 | |
| 743 | data: Py_buffer |
| 744 | / |
| 745 | |
| 746 | Encode .hqx data. |
| 747 | [clinic start generated code]*/ |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 748 | |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 749 | static PyObject * |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 750 | binascii_b2a_hqx_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 751 | /*[clinic end generated code: output=5a987810d5e3cdbb input=9596ebe019fe12ba]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | unsigned char *ascii_data, *bin_data; |
| 754 | int leftbits = 0; |
| 755 | unsigned char this_ch; |
| 756 | unsigned int leftchar = 0; |
| 757 | PyObject *rv; |
| 758 | Py_ssize_t len; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 759 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 760 | bin_data = data->buf; |
| 761 | len = data->len; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | assert(len >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 764 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 765 | if (len > PY_SSIZE_T_MAX / 2 - 2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | return PyErr_NoMemory(); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | /* Allocate a buffer that is at least large enough */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 769 | if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | ascii_data = (unsigned char *)PyBytes_AS_STRING(rv); |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | for( ; len > 0 ; len--, bin_data++ ) { |
| 774 | /* Shift into our buffer, and output any 6bits ready */ |
| 775 | leftchar = (leftchar << 8) | *bin_data; |
| 776 | leftbits += 8; |
| 777 | while ( leftbits >= 6 ) { |
| 778 | this_ch = (leftchar >> (leftbits-6)) & 0x3f; |
| 779 | leftbits -= 6; |
| 780 | *ascii_data++ = table_b2a_hqx[this_ch]; |
| 781 | } |
| 782 | } |
| 783 | /* Output a possible runt byte */ |
| 784 | if ( leftbits ) { |
| 785 | leftchar <<= (6-leftbits); |
| 786 | *ascii_data++ = table_b2a_hqx[leftchar & 0x3f]; |
| 787 | } |
| 788 | if (_PyBytes_Resize(&rv, |
| 789 | (ascii_data - |
| 790 | (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 791 | Py_CLEAR(rv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | return rv; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 796 | |
| 797 | /*[clinic input] |
| 798 | binascii.rledecode_hqx |
| 799 | |
| 800 | data: Py_buffer |
| 801 | / |
| 802 | |
| 803 | Decode hexbin RLE-coded string. |
| 804 | [clinic start generated code]*/ |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 805 | |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 806 | static PyObject * |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 807 | binascii_rledecode_hqx_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 808 | /*[clinic end generated code: output=f7afd89b789946ab input=54cdd49fc014402c]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | unsigned char *in_data, *out_data; |
| 811 | unsigned char in_byte, in_repeat; |
| 812 | PyObject *rv; |
| 813 | Py_ssize_t in_len, out_len, out_len_left; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 814 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 815 | in_data = data->buf; |
| 816 | in_len = data->len; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | assert(in_len >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | /* Empty string is a special case */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 821 | if ( in_len == 0 ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | return PyBytes_FromStringAndSize("", 0); |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 823 | else if (in_len > PY_SSIZE_T_MAX / 2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | return PyErr_NoMemory(); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | /* Allocate a buffer of reasonable size. Resized when needed */ |
| 827 | out_len = in_len*2; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 828 | if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | out_len_left = out_len; |
| 831 | out_data = (unsigned char *)PyBytes_AS_STRING(rv); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | /* |
| 834 | ** We need two macros here to get/put bytes and handle |
| 835 | ** end-of-buffer for input and output strings. |
| 836 | */ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 837 | #define INBYTE(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | do { \ |
| 839 | if ( --in_len < 0 ) { \ |
| 840 | PyErr_SetString(Incomplete, ""); \ |
| 841 | Py_DECREF(rv); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | return NULL; \ |
| 843 | } \ |
| 844 | b = *in_data++; \ |
| 845 | } while(0) |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 846 | |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 847 | #define OUTBYTE(b) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | do { \ |
| 849 | if ( --out_len_left < 0 ) { \ |
| 850 | if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \ |
| 851 | if (_PyBytes_Resize(&rv, 2*out_len) < 0) \ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 852 | { Py_XDECREF(rv); return NULL; } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | out_data = (unsigned char *)PyBytes_AS_STRING(rv) \ |
| 854 | + out_len; \ |
| 855 | out_len_left = out_len-1; \ |
| 856 | out_len = out_len * 2; \ |
| 857 | } \ |
| 858 | *out_data++ = b; \ |
| 859 | } while(0) |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | /* |
| 862 | ** Handle first byte separately (since we have to get angry |
| 863 | ** in case of an orphaned RLE code). |
| 864 | */ |
| 865 | INBYTE(in_byte); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | if (in_byte == RUNCHAR) { |
| 868 | INBYTE(in_repeat); |
| 869 | if (in_repeat != 0) { |
| 870 | /* Note Error, not Incomplete (which is at the end |
| 871 | ** of the string only). This is a programmer error. |
| 872 | */ |
| 873 | PyErr_SetString(Error, "Orphaned RLE code at start"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | Py_DECREF(rv); |
| 875 | return NULL; |
| 876 | } |
| 877 | OUTBYTE(RUNCHAR); |
| 878 | } else { |
| 879 | OUTBYTE(in_byte); |
| 880 | } |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | while( in_len > 0 ) { |
| 883 | INBYTE(in_byte); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | if (in_byte == RUNCHAR) { |
| 886 | INBYTE(in_repeat); |
| 887 | if ( in_repeat == 0 ) { |
| 888 | /* Just an escaped RUNCHAR value */ |
| 889 | OUTBYTE(RUNCHAR); |
| 890 | } else { |
| 891 | /* Pick up value and output a sequence of it */ |
| 892 | in_byte = out_data[-1]; |
| 893 | while ( --in_repeat > 0 ) |
| 894 | OUTBYTE(in_byte); |
| 895 | } |
| 896 | } else { |
| 897 | /* Normal byte */ |
| 898 | OUTBYTE(in_byte); |
| 899 | } |
| 900 | } |
| 901 | if (_PyBytes_Resize(&rv, |
| 902 | (out_data - |
| 903 | (unsigned char *)PyBytes_AS_STRING(rv))) < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 904 | Py_CLEAR(rv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | return rv; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 909 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 910 | /*[clinic input] |
| 911 | binascii.crc_hqx -> int |
| 912 | |
| 913 | data: Py_buffer |
| 914 | crc: int |
| 915 | / |
| 916 | |
| 917 | Compute hqx CRC incrementally. |
| 918 | [clinic start generated code]*/ |
| 919 | |
| 920 | static int |
| 921 | binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, int crc) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 922 | /*[clinic end generated code: output=634dac18dfa863d7 input=68060931b2f51c8a]*/ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | unsigned char *bin_data; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 925 | unsigned int ucrc = (unsigned int)crc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | Py_ssize_t len; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 927 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 928 | bin_data = data->buf; |
| 929 | len = data->len; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | while(len-- > 0) { |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 932 | ucrc=((ucrc<<8)&0xff00)^crctab_hqx[((ucrc>>8)&0xff)^*bin_data++]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | } |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 934 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 935 | return (int)ucrc; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 938 | #ifndef USE_ZLIB_CRC32 |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 939 | /* Crc - 32 BIT ANSI X3.66 CRC checksum files |
| 940 | Also known as: ISO 3307 |
| 941 | **********************************************************************| |
| 942 | * *| |
| 943 | * Demonstration program to compute the 32-bit CRC used as the frame *| |
| 944 | * check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *| |
| 945 | * and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *| |
| 946 | * protocol). The 32-bit FCS was added via the Federal Register, *| |
| 947 | * 1 June 1982, p.23798. I presume but don't know for certain that *| |
| 948 | * this polynomial is or will be included in CCITT V.41, which *| |
| 949 | * defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *| |
| 950 | * PUB 78 says that the 32-bit FCS reduces otherwise undetected *| |
| 951 | * errors by a factor of 10^-5 over 16-bit FCS. *| |
| 952 | * *| |
| 953 | **********************************************************************| |
| 954 | |
| 955 | Copyright (C) 1986 Gary S. Brown. You may use this program, or |
| 956 | code or tables extracted from it, as desired without restriction. |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 957 | |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 958 | First, the polynomial itself and its table of feedback terms. The |
| 959 | polynomial is |
| 960 | X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 |
| 961 | Note that we take it "backwards" and put the highest-order term in |
| 962 | the lowest-order bit. The X^32 term is "implied"; the LSB is the |
| 963 | X^31 term, etc. The X^0 term (usually shown as "+1") results in |
| 964 | the MSB being 1. |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 965 | |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 966 | Note that the usual hardware shift register implementation, which |
| 967 | is what we're using (we're merely optimizing it by doing eight-bit |
| 968 | chunks at a time) shifts bits into the lowest-order term. In our |
| 969 | implementation, that means shifting towards the right. Why do we |
| 970 | do it this way? Because the calculated CRC must be transmitted in |
| 971 | order from highest-order term to lowest-order term. UARTs transmit |
| 972 | characters in order from LSB to MSB. By storing the CRC this way, |
| 973 | we hand it to the UART in the order low-byte to high-byte; the UART |
| 974 | sends each low-bit to hight-bit; and the result is transmission bit |
| 975 | by bit from highest- to lowest-order term without requiring any bit |
| 976 | shuffling on our part. Reception works similarly. |
| 977 | |
| 978 | The feedback terms table consists of 256, 32-bit entries. Notes: |
| 979 | |
| 980 | 1. The table can be generated at runtime if desired; code to do so |
| 981 | is shown later. It might not be obvious, but the feedback |
| 982 | terms simply represent the results of eight shift/xor opera- |
| 983 | tions for all combinations of data and CRC register values. |
| 984 | |
| 985 | 2. The CRC accumulation logic is the same for all CRC polynomials, |
| 986 | be they sixteen or thirty-two bits wide. You simply choose the |
| 987 | appropriate table. Alternatively, because the table can be |
| 988 | generated at runtime, you can start by generating the table for |
| 989 | the polynomial in question and use exactly the same "updcrc", |
| 990 | if your application needn't simultaneously handle two CRC |
| 991 | polynomials. (Note, however, that XMODEM is strange.) |
| 992 | |
| 993 | 3. For 16-bit CRCs, the table entries need be only 16 bits wide; |
| 994 | of course, 32-bit entries work OK if the high 16 bits are zero. |
| 995 | |
| 996 | 4. The values must be right-shifted by eight bits by the "updcrc" |
| 997 | logic; the shift must be unsigned (bring in zeroes). On some |
| 998 | hardware you could probably optimize the shift in assembler by |
| 999 | using byte-swap instructions. |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 1000 | ********************************************************************/ |
| 1001 | |
Gregory P. Smith | 3c0e4d2 | 2008-03-25 07:51:12 +0000 | [diff] [blame] | 1002 | static unsigned int crc_32_tab[256] = { |
| 1003 | 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, |
| 1004 | 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, |
| 1005 | 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, |
| 1006 | 0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU, |
| 1007 | 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U, |
| 1008 | 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U, |
| 1009 | 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U, |
| 1010 | 0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU, |
| 1011 | 0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U, |
| 1012 | 0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU, |
| 1013 | 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U, |
| 1014 | 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U, |
| 1015 | 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U, |
| 1016 | 0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU, |
| 1017 | 0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU, |
| 1018 | 0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U, |
| 1019 | 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU, |
| 1020 | 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U, |
| 1021 | 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U, |
| 1022 | 0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U, |
| 1023 | 0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU, |
| 1024 | 0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U, |
| 1025 | 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U, |
| 1026 | 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU, |
| 1027 | 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U, |
| 1028 | 0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U, |
| 1029 | 0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U, |
| 1030 | 0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U, |
| 1031 | 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U, |
| 1032 | 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU, |
| 1033 | 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU, |
| 1034 | 0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U, |
| 1035 | 0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U, |
| 1036 | 0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU, |
| 1037 | 0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU, |
| 1038 | 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U, |
| 1039 | 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU, |
| 1040 | 0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U, |
| 1041 | 0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU, |
| 1042 | 0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U, |
| 1043 | 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU, |
| 1044 | 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U, |
| 1045 | 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U, |
| 1046 | 0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU, |
| 1047 | 0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U, |
| 1048 | 0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U, |
| 1049 | 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U, |
| 1050 | 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U, |
| 1051 | 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U, |
| 1052 | 0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U, |
| 1053 | 0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU, |
| 1054 | 0x2d02ef8dU |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 1055 | }; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1056 | #endif /* USE_ZLIB_CRC32 */ |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 1057 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1058 | /*[clinic input] |
| 1059 | binascii.crc32 -> unsigned_int |
| 1060 | |
| 1061 | data: Py_buffer |
| 1062 | crc: unsigned_int(bitwise=True) = 0 |
| 1063 | / |
| 1064 | |
| 1065 | Compute CRC-32 incrementally. |
| 1066 | [clinic start generated code]*/ |
| 1067 | |
| 1068 | static unsigned int |
| 1069 | binascii_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1070 | /*[clinic end generated code: output=620a961643393c4f input=bbe340bc99d25aa8]*/ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1071 | |
| 1072 | #ifdef USE_ZLIB_CRC32 |
| 1073 | /* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */ |
| 1074 | { |
| 1075 | Byte *buf; |
| 1076 | Py_ssize_t len; |
| 1077 | int signed_val; |
| 1078 | |
| 1079 | buf = (Byte*)data->buf; |
| 1080 | len = data->len; |
| 1081 | signed_val = crc32(crc, buf, len); |
| 1082 | return (unsigned int)signed_val & 0xffffffffU; |
| 1083 | } |
| 1084 | #else /* USE_ZLIB_CRC32 */ |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 1085 | { /* By Jim Ahlstrom; All rights transferred to CNRI */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | unsigned char *bin_data; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | Py_ssize_t len; |
| 1088 | unsigned int result; |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 1089 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1090 | bin_data = data->buf; |
| 1091 | len = data->len; |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 1092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | crc = ~ crc; |
| 1094 | while (len-- > 0) { |
| 1095 | crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8); |
| 1096 | /* Note: (crc >> 8) MUST zero fill on left */ |
| 1097 | } |
Tim Peters | a98011c | 2002-07-02 20:20:08 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | result = (crc ^ 0xFFFFFFFF); |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1100 | return result & 0xffffffff; |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 1101 | } |
Christian Heimes | 1dc5400 | 2008-03-24 02:19:29 +0000 | [diff] [blame] | 1102 | #endif /* USE_ZLIB_CRC32 */ |
Guido van Rossum | 7d47c9e | 2000-02-16 21:11:52 +0000 | [diff] [blame] | 1103 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1104 | /*[clinic input] |
| 1105 | binascii.b2a_hex |
| 1106 | |
| 1107 | data: Py_buffer |
| 1108 | / |
| 1109 | |
| 1110 | Hexadecimal representation of binary data. |
| 1111 | |
| 1112 | The return value is a bytes object. This function is also |
| 1113 | available as "hexlify()". |
| 1114 | [clinic start generated code]*/ |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1115 | |
| 1116 | static PyObject * |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1117 | binascii_b2a_hex_impl(PyModuleDef *module, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1118 | /*[clinic end generated code: output=179318922c2f8fda input=96423cfa299ff3b1]*/ |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | char* argbuf; |
| 1121 | Py_ssize_t arglen; |
| 1122 | PyObject *retval; |
| 1123 | char* retbuf; |
| 1124 | Py_ssize_t i, j; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1125 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1126 | argbuf = data->buf; |
| 1127 | arglen = data->len; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | assert(arglen >= 0); |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1130 | if (arglen > PY_SSIZE_T_MAX / 2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | return PyErr_NoMemory(); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | retval = PyBytes_FromStringAndSize(NULL, arglen*2); |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1134 | if (!retval) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | retbuf = PyBytes_AS_STRING(retval); |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | /* make hex version of string, taken from shamodule.c */ |
| 1139 | for (i=j=0; i < arglen; i++) { |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1140 | unsigned char c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | c = (argbuf[i] >> 4) & 0xf; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1142 | retbuf[j++] = Py_hexdigits[c]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | c = argbuf[i] & 0xf; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1144 | retbuf[j++] = Py_hexdigits[c]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | return retval; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Zachary Ware | b176d40 | 2015-01-20 13:59:46 -0600 | [diff] [blame] | 1149 | /*[clinic input] |
| 1150 | binascii.hexlify = binascii.b2a_hex |
| 1151 | |
| 1152 | Hexadecimal representation of binary data. |
| 1153 | |
| 1154 | The return value is a bytes object. |
| 1155 | [clinic start generated code]*/ |
| 1156 | |
| 1157 | static PyObject * |
| 1158 | binascii_hexlify_impl(PyModuleDef *module, Py_buffer *data) |
| 1159 | /*[clinic end generated code: output=6098440091fb61dc input=2e3afae7f083f061]*/ |
| 1160 | { |
| 1161 | return binascii_b2a_hex_impl(module, data); |
| 1162 | } |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1163 | |
| 1164 | static int |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 1165 | to_int(int c) |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1166 | { |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 1167 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | return c - '0'; |
| 1169 | else { |
Antoine Pitrou | ed8ba14 | 2011-10-04 13:50:21 +0200 | [diff] [blame] | 1170 | if (Py_ISUPPER(c)) |
| 1171 | c = Py_TOLOWER(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | if (c >= 'a' && c <= 'f') |
| 1173 | return c - 'a' + 10; |
| 1174 | } |
| 1175 | return -1; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1179 | /*[clinic input] |
| 1180 | binascii.a2b_hex |
| 1181 | |
| 1182 | hexstr: ascii_buffer |
| 1183 | / |
| 1184 | |
| 1185 | Binary data of hexadecimal representation. |
| 1186 | |
| 1187 | hexstr must contain an even number of hex digits (upper or lower case). |
| 1188 | This function is also available as "unhexlify()". |
| 1189 | [clinic start generated code]*/ |
| 1190 | |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1191 | static PyObject * |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1192 | binascii_a2b_hex_impl(PyModuleDef *module, Py_buffer *hexstr) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1193 | /*[clinic end generated code: output=d61da452b5c6d290 input=9e1e7f2f94db24fd]*/ |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | char* argbuf; |
| 1196 | Py_ssize_t arglen; |
| 1197 | PyObject *retval; |
| 1198 | char* retbuf; |
| 1199 | Py_ssize_t i, j; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1200 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1201 | argbuf = hexstr->buf; |
| 1202 | arglen = hexstr->len; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | assert(arglen >= 0); |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | /* XXX What should we do about strings with an odd length? Should |
| 1207 | * we add an implicit leading zero, or a trailing zero? For now, |
| 1208 | * raise an exception. |
| 1209 | */ |
| 1210 | if (arglen % 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | PyErr_SetString(Error, "Odd-length string"); |
| 1212 | return NULL; |
| 1213 | } |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | retval = PyBytes_FromStringAndSize(NULL, (arglen/2)); |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1216 | if (!retval) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | retbuf = PyBytes_AS_STRING(retval); |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | for (i=j=0; i < arglen; i += 2) { |
| 1221 | int top = to_int(Py_CHARMASK(argbuf[i])); |
| 1222 | int bot = to_int(Py_CHARMASK(argbuf[i+1])); |
| 1223 | if (top == -1 || bot == -1) { |
| 1224 | PyErr_SetString(Error, |
| 1225 | "Non-hexadecimal digit found"); |
| 1226 | goto finally; |
| 1227 | } |
| 1228 | retbuf[j++] = (top << 4) + bot; |
| 1229 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | return retval; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1231 | |
| 1232 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | Py_DECREF(retval); |
| 1234 | return NULL; |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Zachary Ware | b176d40 | 2015-01-20 13:59:46 -0600 | [diff] [blame] | 1237 | /*[clinic input] |
| 1238 | binascii.unhexlify = binascii.a2b_hex |
| 1239 | |
| 1240 | Binary data of hexadecimal representation. |
| 1241 | |
| 1242 | hexstr must contain an even number of hex digits (upper or lower case). |
| 1243 | [clinic start generated code]*/ |
| 1244 | |
| 1245 | static PyObject * |
| 1246 | binascii_unhexlify_impl(PyModuleDef *module, Py_buffer *hexstr) |
| 1247 | /*[clinic end generated code: output=17cec7544499803e input=dd8c012725f462da]*/ |
| 1248 | { |
| 1249 | return binascii_a2b_hex_impl(module, hexstr); |
| 1250 | } |
| 1251 | |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1252 | static int table_hex[128] = { |
| 1253 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 1254 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 1255 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 1256 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1, |
| 1257 | -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 1258 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 1259 | -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1, |
| 1260 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1 |
| 1261 | }; |
| 1262 | |
| 1263 | #define hexval(c) table_hex[(unsigned int)(c)] |
| 1264 | |
| 1265 | #define MAXLINESIZE 76 |
| 1266 | |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1267 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1268 | /*[clinic input] |
| 1269 | binascii.a2b_qp |
| 1270 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1271 | data: ascii_buffer |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1272 | header: int(c_default="0") = False |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1273 | |
| 1274 | Decode a string of qp-encoded data. |
| 1275 | [clinic start generated code]*/ |
| 1276 | |
| 1277 | static PyObject * |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1278 | binascii_a2b_qp_impl(PyModuleDef *module, Py_buffer *data, int header) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1279 | /*[clinic end generated code: output=a44ef88270352114 input=5187a0d3d8e54f3b]*/ |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | Py_ssize_t in, out; |
| 1282 | char ch; |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1283 | unsigned char *ascii_data, *odata; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | Py_ssize_t datalen = 0; |
| 1285 | PyObject *rv; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1286 | |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1287 | ascii_data = data->buf; |
| 1288 | datalen = data->len; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | /* We allocate the output same size as input, this is overkill. |
| 1291 | * The previous implementation used calloc() so we'll zero out the |
| 1292 | * memory here too, since PyMem_Malloc() does not guarantee that. |
| 1293 | */ |
| 1294 | odata = (unsigned char *) PyMem_Malloc(datalen); |
| 1295 | if (odata == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | PyErr_NoMemory(); |
| 1297 | return NULL; |
| 1298 | } |
| 1299 | memset(odata, 0, datalen); |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | in = out = 0; |
| 1302 | while (in < datalen) { |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1303 | if (ascii_data[in] == '=') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1304 | in++; |
| 1305 | if (in >= datalen) break; |
| 1306 | /* Soft line breaks */ |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1307 | if ((ascii_data[in] == '\n') || (ascii_data[in] == '\r')) { |
| 1308 | if (ascii_data[in] != '\n') { |
| 1309 | while (in < datalen && ascii_data[in] != '\n') in++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | } |
| 1311 | if (in < datalen) in++; |
| 1312 | } |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1313 | else if (ascii_data[in] == '=') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | /* broken case from broken python qp */ |
| 1315 | odata[out++] = '='; |
| 1316 | in++; |
| 1317 | } |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1318 | else if (((ascii_data[in] >= 'A' && ascii_data[in] <= 'F') || |
| 1319 | (ascii_data[in] >= 'a' && ascii_data[in] <= 'f') || |
| 1320 | (ascii_data[in] >= '0' && ascii_data[in] <= '9')) && |
| 1321 | ((ascii_data[in+1] >= 'A' && ascii_data[in+1] <= 'F') || |
| 1322 | (ascii_data[in+1] >= 'a' && ascii_data[in+1] <= 'f') || |
| 1323 | (ascii_data[in+1] >= '0' && ascii_data[in+1] <= '9'))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | /* hexval */ |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1325 | ch = hexval(ascii_data[in]) << 4; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | in++; |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1327 | ch |= hexval(ascii_data[in]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | in++; |
| 1329 | odata[out++] = ch; |
| 1330 | } |
| 1331 | else { |
| 1332 | odata[out++] = '='; |
| 1333 | } |
| 1334 | } |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1335 | else if (header && ascii_data[in] == '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | odata[out++] = ' '; |
| 1337 | in++; |
| 1338 | } |
| 1339 | else { |
Serhiy Storchaka | 1278561 | 2014-01-25 11:49:49 +0200 | [diff] [blame] | 1340 | odata[out] = ascii_data[in]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | in++; |
| 1342 | out++; |
| 1343 | } |
| 1344 | } |
| 1345 | if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | PyMem_Free(odata); |
| 1347 | return NULL; |
| 1348 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | PyMem_Free(odata); |
| 1350 | return rv; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 1353 | static int |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1354 | to_hex (unsigned char ch, unsigned char *s) |
| 1355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | unsigned int uvalue = ch; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | s[1] = "0123456789ABCDEF"[uvalue % 16]; |
| 1359 | uvalue = (uvalue / 16); |
| 1360 | s[0] = "0123456789ABCDEF"[uvalue % 16]; |
| 1361 | return 0; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1364 | /* XXX: This is ridiculously complicated to be backward compatible |
| 1365 | * (mostly) with the quopri module. It doesn't re-create the quopri |
| 1366 | * module bug where text ending in CRLF has the CR encoded */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1367 | |
| 1368 | /*[clinic input] |
| 1369 | binascii.b2a_qp |
| 1370 | |
| 1371 | data: Py_buffer |
| 1372 | quotetabs: int(c_default="0") = False |
| 1373 | istext: int(c_default="1") = True |
| 1374 | header: int(c_default="0") = False |
| 1375 | |
| 1376 | Encode a string using quoted-printable encoding. |
| 1377 | |
| 1378 | On encoding, when istext is set, newlines are not encoded, and white |
| 1379 | space at end of lines is. When istext is not set, \r and \n (CR/LF) |
| 1380 | are both encoded. When quotetabs is set, space and tabs are encoded. |
| 1381 | [clinic start generated code]*/ |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int istext, int header) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1385 | /*[clinic end generated code: output=ff2991ba640fff3e input=7f2a9aaa008e92b2]*/ |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | Py_ssize_t in, out; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1388 | unsigned char *databuf, *odata; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | Py_ssize_t datalen = 0, odatalen = 0; |
| 1390 | PyObject *rv; |
| 1391 | unsigned int linelen = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | unsigned char ch; |
| 1393 | int crlf = 0; |
| 1394 | unsigned char *p; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1395 | |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1396 | databuf = data->buf; |
| 1397 | datalen = data->len; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | /* See if this string is using CRLF line ends */ |
| 1400 | /* XXX: this function has the side effect of converting all of |
| 1401 | * the end of lines to be the same depending on this detection |
| 1402 | * here */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1403 | p = (unsigned char *) memchr(databuf, '\n', datalen); |
| 1404 | if ((p != NULL) && (p > databuf) && (*(p-1) == '\r')) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | crlf = 1; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | /* First, scan to see how many characters need to be encoded */ |
| 1408 | in = 0; |
| 1409 | while (in < datalen) { |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1410 | if ((databuf[in] > 126) || |
| 1411 | (databuf[in] == '=') || |
| 1412 | (header && databuf[in] == '_') || |
| 1413 | ((databuf[in] == '.') && (linelen == 0) && |
| 1414 | (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) || |
| 1415 | (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) || |
| 1416 | ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) || |
| 1417 | ((databuf[in] < 33) && |
| 1418 | (databuf[in] != '\r') && (databuf[in] != '\n') && |
| 1419 | (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' '))))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | { |
| 1421 | if ((linelen + 3) >= MAXLINESIZE) { |
| 1422 | linelen = 0; |
| 1423 | if (crlf) |
| 1424 | odatalen += 3; |
| 1425 | else |
| 1426 | odatalen += 2; |
| 1427 | } |
| 1428 | linelen += 3; |
| 1429 | odatalen += 3; |
| 1430 | in++; |
| 1431 | } |
| 1432 | else { |
| 1433 | if (istext && |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1434 | ((databuf[in] == '\n') || |
| 1435 | ((in+1 < datalen) && (databuf[in] == '\r') && |
| 1436 | (databuf[in+1] == '\n')))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | { |
| 1438 | linelen = 0; |
| 1439 | /* Protect against whitespace on end of line */ |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1440 | if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t'))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | odatalen += 2; |
| 1442 | if (crlf) |
| 1443 | odatalen += 2; |
| 1444 | else |
| 1445 | odatalen += 1; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1446 | if (databuf[in] == '\r') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | in += 2; |
| 1448 | else |
| 1449 | in++; |
| 1450 | } |
| 1451 | else { |
| 1452 | if ((in + 1 != datalen) && |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1453 | (databuf[in+1] != '\n') && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | (linelen + 1) >= MAXLINESIZE) { |
| 1455 | linelen = 0; |
| 1456 | if (crlf) |
| 1457 | odatalen += 3; |
| 1458 | else |
| 1459 | odatalen += 2; |
| 1460 | } |
| 1461 | linelen++; |
| 1462 | odatalen++; |
| 1463 | in++; |
| 1464 | } |
| 1465 | } |
| 1466 | } |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | /* We allocate the output same size as input, this is overkill. |
| 1469 | * The previous implementation used calloc() so we'll zero out the |
| 1470 | * memory here too, since PyMem_Malloc() does not guarantee that. |
| 1471 | */ |
| 1472 | odata = (unsigned char *) PyMem_Malloc(odatalen); |
| 1473 | if (odata == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | PyErr_NoMemory(); |
| 1475 | return NULL; |
| 1476 | } |
| 1477 | memset(odata, 0, odatalen); |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | in = out = linelen = 0; |
| 1480 | while (in < datalen) { |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1481 | if ((databuf[in] > 126) || |
| 1482 | (databuf[in] == '=') || |
| 1483 | (header && databuf[in] == '_') || |
| 1484 | ((databuf[in] == '.') && (linelen == 0) && |
| 1485 | (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) || |
| 1486 | (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) || |
| 1487 | ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) || |
| 1488 | ((databuf[in] < 33) && |
| 1489 | (databuf[in] != '\r') && (databuf[in] != '\n') && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | (quotetabs || |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1491 | (!quotetabs && ((databuf[in] != '\t') && (databuf[in] != ' ')))))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | { |
| 1493 | if ((linelen + 3 )>= MAXLINESIZE) { |
| 1494 | odata[out++] = '='; |
| 1495 | if (crlf) odata[out++] = '\r'; |
| 1496 | odata[out++] = '\n'; |
| 1497 | linelen = 0; |
| 1498 | } |
| 1499 | odata[out++] = '='; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1500 | to_hex(databuf[in], &odata[out]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | out += 2; |
| 1502 | in++; |
| 1503 | linelen += 3; |
| 1504 | } |
| 1505 | else { |
| 1506 | if (istext && |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1507 | ((databuf[in] == '\n') || |
| 1508 | ((in+1 < datalen) && (databuf[in] == '\r') && |
| 1509 | (databuf[in+1] == '\n')))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | { |
| 1511 | linelen = 0; |
| 1512 | /* Protect against whitespace on end of line */ |
| 1513 | if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) { |
| 1514 | ch = odata[out-1]; |
| 1515 | odata[out-1] = '='; |
| 1516 | to_hex(ch, &odata[out]); |
| 1517 | out += 2; |
| 1518 | } |
Tim Peters | 934c1a1 | 2002-07-02 22:24:50 +0000 | [diff] [blame] | 1519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | if (crlf) odata[out++] = '\r'; |
| 1521 | odata[out++] = '\n'; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1522 | if (databuf[in] == '\r') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | in += 2; |
| 1524 | else |
| 1525 | in++; |
| 1526 | } |
| 1527 | else { |
| 1528 | if ((in + 1 != datalen) && |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1529 | (databuf[in+1] != '\n') && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | (linelen + 1) >= MAXLINESIZE) { |
| 1531 | odata[out++] = '='; |
| 1532 | if (crlf) odata[out++] = '\r'; |
| 1533 | odata[out++] = '\n'; |
| 1534 | linelen = 0; |
| 1535 | } |
| 1536 | linelen++; |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1537 | if (header && databuf[in] == ' ') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | odata[out++] = '_'; |
| 1539 | in++; |
| 1540 | } |
| 1541 | else { |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1542 | odata[out++] = databuf[in++]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | PyMem_Free(odata); |
| 1549 | return NULL; |
| 1550 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | PyMem_Free(odata); |
| 1552 | return rv; |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 1553 | } |
Barry Warsaw | e977c21 | 2000-08-15 06:07:13 +0000 | [diff] [blame] | 1554 | |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1555 | /* List of functions defined in the module */ |
| 1556 | |
| 1557 | static struct PyMethodDef binascii_module_methods[] = { |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1558 | BINASCII_A2B_UU_METHODDEF |
| 1559 | BINASCII_B2A_UU_METHODDEF |
| 1560 | BINASCII_A2B_BASE64_METHODDEF |
| 1561 | BINASCII_B2A_BASE64_METHODDEF |
| 1562 | BINASCII_A2B_HQX_METHODDEF |
| 1563 | BINASCII_B2A_HQX_METHODDEF |
| 1564 | BINASCII_A2B_HEX_METHODDEF |
| 1565 | BINASCII_B2A_HEX_METHODDEF |
Zachary Ware | b176d40 | 2015-01-20 13:59:46 -0600 | [diff] [blame] | 1566 | BINASCII_HEXLIFY_METHODDEF |
| 1567 | BINASCII_UNHEXLIFY_METHODDEF |
Serhiy Storchaka | 3ffd913 | 2014-01-25 11:21:23 +0200 | [diff] [blame] | 1568 | BINASCII_RLECODE_HQX_METHODDEF |
| 1569 | BINASCII_RLEDECODE_HQX_METHODDEF |
| 1570 | BINASCII_CRC_HQX_METHODDEF |
| 1571 | BINASCII_CRC32_METHODDEF |
| 1572 | BINASCII_A2B_QP_METHODDEF |
| 1573 | BINASCII_B2A_QP_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | {NULL, NULL} /* sentinel */ |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1575 | }; |
| 1576 | |
| 1577 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1578 | /* Initialization function for the module (*must* be called PyInit_binascii) */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1579 | PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII"); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1580 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1581 | |
| 1582 | static struct PyModuleDef binasciimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | PyModuleDef_HEAD_INIT, |
| 1584 | "binascii", |
| 1585 | doc_binascii, |
| 1586 | -1, |
| 1587 | binascii_module_methods, |
| 1588 | NULL, |
| 1589 | NULL, |
| 1590 | NULL, |
| 1591 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1592 | }; |
| 1593 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1594 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1595 | PyInit_binascii(void) |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | PyObject *m, *d; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | /* Create the module and add the functions */ |
| 1600 | m = PyModule_Create(&binasciimodule); |
| 1601 | if (m == NULL) |
| 1602 | return NULL; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | d = PyModule_GetDict(m); |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL); |
| 1607 | PyDict_SetItemString(d, "Error", Error); |
| 1608 | Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); |
| 1609 | PyDict_SetItemString(d, "Incomplete", Incomplete); |
| 1610 | if (PyErr_Occurred()) { |
| 1611 | Py_DECREF(m); |
| 1612 | m = NULL; |
| 1613 | } |
| 1614 | return m; |
Jack Jansen | 7278119 | 1995-08-07 14:34:15 +0000 | [diff] [blame] | 1615 | } |