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