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