blob: 52bd6dcc11eb3e2242f42d37cbdb2ea381270f66 [file] [log] [blame]
Jack Jansen72781191995-08-07 14:34:15 +00001/*
2** Routines to represent binary data in ASCII and vice-versa
3**
4** This module currently supports the following encodings:
5** uuencode:
6** each line encodes 45 bytes (except possibly the last)
7** First char encodes (binary) length, rest data
8** each char encodes 6 bits, as follows:
9** binary: 01234567 abcdefgh ijklmnop
10** ascii: 012345 67abcd efghij klmnop
11** ASCII encoding method is "excess-space": 000000 is encoded as ' ', etc.
12** short binary data is zero-extended (so the bits are always in the
13** right place), this does *not* reflect in the length.
Jack Jansen84bbc2e1995-10-04 16:38:44 +000014** base64:
15** Line breaks are insignificant, but lines are at most 76 chars
16** each char encodes 6 bits, in similar order as uucode/hqx. Encoding
17** is done via a table.
18** Short binary data is filled (in ASCII) with '='.
Jack Jansen72781191995-08-07 14:34:15 +000019** hqx:
20** File starts with introductory text, real data starts and ends
21** with colons.
22** Data consists of three similar parts: info, datafork, resourcefork.
23** Each part is protected (at the end) with a 16-bit crc
24** The binary data is run-length encoded, and then ascii-fied:
25** binary: 01234567 abcdefgh ijklmnop
26** ascii: 012345 67abcd efghij klmnop
27** ASCII encoding is table-driven, see the code.
28** Short binary data results in the runt ascii-byte being output with
29** the bits in the right place.
30**
31** While I was reading dozens of programs that encode or decode the formats
32** here (documentation? hihi:-) I have formulated Jansen's Observation:
33**
34** Programs that encode binary data in ASCII are written in
35** such a style that they are as unreadable as possible. Devices used
36** include unnecessary global variables, burying important tables
37** in unrelated sourcefiles, putting functions in include files,
38** using seemingly-descriptive variable names for different purposes,
39** calls to empty subroutines and a host of others.
40**
41** I have attempted to break with this tradition, but I guess that that
42** does make the performance sub-optimal. Oh well, too bad...
43**
44** Jack Jansen, CWI, July 1995.
Tim Peters934c1a12002-07-02 22:24:50 +000045**
Martin v. Löwis16dc7f42001-09-30 20:32:11 +000046** Added support for quoted-printable encoding, based on rfc 1521 et al
Tim Peters934c1a12002-07-02 22:24:50 +000047** quoted-printable encoding specifies that non printable characters (anything
Martin v. Löwis16dc7f42001-09-30 20:32:11 +000048** below 32 and above 126) be encoded as =XX where XX is the hexadecimal value
49** of the character. It also specifies some other behavior to enable 8bit data
Tim Peters934c1a12002-07-02 22:24:50 +000050** in a mail message with little difficulty (maximum line sizes, protecting
51** some cases of whitespace, etc).
Martin v. Löwis16dc7f42001-09-30 20:32:11 +000052**
53** Brandon Long, September 2001.
Jack Jansen72781191995-08-07 14:34:15 +000054*/
55
Thomas Wouters9c544482006-03-01 21:59:44 +000056#define PY_SSIZE_T_CLEAN
Jack Jansen72781191995-08-07 14:34:15 +000057
58#include "Python.h"
Christian Heimes1dc54002008-03-24 02:19:29 +000059#ifdef USE_ZLIB_CRC32
60#include "zlib.h"
61#endif
Jack Jansen72781191995-08-07 14:34:15 +000062
63static PyObject *Error;
64static PyObject *Incomplete;
65
66/*
67** hqx lookup table, ascii->binary.
68*/
69
70#define RUNCHAR 0x90
71
72#define DONE 0x7F
73#define SKIP 0x7E
74#define FAIL 0x7D
75
76static unsigned char table_a2b_hqx[256] = {
77/* ^@ ^A ^B ^C ^D ^E ^F ^G */
78/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
79/* \b \t \n ^K ^L \r ^N ^O */
80/* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
81/* ^P ^Q ^R ^S ^T ^U ^V ^W */
82/* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
83/* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
84/* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
85/* ! " # $ % & ' */
86/* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
87/* ( ) * + , - . / */
88/* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
89/* 0 1 2 3 4 5 6 7 */
90/* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
91/* 8 9 : ; < = > ? */
92/* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
93/* @ A B C D E F G */
94/* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
95/* H I J K L M N O */
96/* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
97/* P Q R S T U V W */
98/*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
99/* X Y Z [ \ ] ^ _ */
100/*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
101/* ` a b c d e f g */
102/*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
103/* h i j k l m n o */
104/*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
105/* p q r s t u v w */
106/*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
107/* x y z { | } ~ ^? */
108/*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
109/*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
110 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
111 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
112 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
113 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
114 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
115 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
116 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
117 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
118 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
119 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
120 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
121 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
122 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
123 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
124 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
125};
126
127static unsigned char table_b2a_hqx[] =
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000128"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
Jack Jansen72781191995-08-07 14:34:15 +0000129
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000130static char table_a2b_base64[] = {
131 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
132 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
133 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
134 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
135 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
136 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
137 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
138 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
139};
140
141#define BASE64_PAD '='
Guido van Rossum355bc0c2001-10-30 03:00:52 +0000142
143/* Max binary chunk size; limited only by available memory */
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000144#define BASE64_MAXBIN ((PY_SSIZE_T_MAX - 3) / 2)
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000145
146static unsigned char table_b2a_base64[] =
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000147"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000148
149
150
Jack Jansen72781191995-08-07 14:34:15 +0000151static unsigned short crctab_hqx[256] = {
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000152 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
153 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
154 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
155 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
156 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
157 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
158 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
159 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
160 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
161 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
162 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
163 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
164 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
165 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
166 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
167 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
168 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
169 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
170 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
171 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
172 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
173 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
174 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
175 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
176 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
177 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
178 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
179 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
180 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
181 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
182 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
183 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
Jack Jansen72781191995-08-07 14:34:15 +0000184};
185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000186PyDoc_STRVAR(doc_a2b_uu, "(ascii) -> bin. Decode a line of uuencoded data");
Jack Jansen72781191995-08-07 14:34:15 +0000187
188static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000189binascii_a2b_uu(PyObject *self, PyObject *args)
Jack Jansen72781191995-08-07 14:34:15 +0000190{
Martin v. Löwis423be952008-08-13 15:53:07 +0000191 Py_buffer pascii;
Jack Jansen72781191995-08-07 14:34:15 +0000192 unsigned char *ascii_data, *bin_data;
193 int leftbits = 0;
194 unsigned char this_ch;
195 unsigned int leftchar = 0;
196 PyObject *rv;
Thomas Woutersf98db652006-03-01 21:37:32 +0000197 Py_ssize_t ascii_len, bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000198
Martin v. Löwis423be952008-08-13 15:53:07 +0000199 if ( !PyArg_ParseTuple(args, "y*:a2b_uu", &pascii) )
Jack Jansen72781191995-08-07 14:34:15 +0000200 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000201 ascii_data = pascii.buf;
202 ascii_len = pascii.len;
Jack Jansen72781191995-08-07 14:34:15 +0000203
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000204 assert(ascii_len >= 0);
205
Jack Jansen72781191995-08-07 14:34:15 +0000206 /* First byte: binary data length (in bytes) */
207 bin_len = (*ascii_data++ - ' ') & 077;
208 ascii_len--;
209
210 /* Allocate the buffer */
Martin v. Löwis423be952008-08-13 15:53:07 +0000211 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) {
212 PyBuffer_Release(&pascii);
Jack Jansen72781191995-08-07 14:34:15 +0000213 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000214 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000215 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000216
Jack Jansen72781191995-08-07 14:34:15 +0000217 for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
Neal Norwitzfe92eef2004-06-06 20:13:10 +0000218 /* XXX is it really best to add NULs if there's no more data */
219 this_ch = (ascii_len > 0) ? *ascii_data : 0;
Jack Jansen72781191995-08-07 14:34:15 +0000220 if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
221 /*
222 ** Whitespace. Assume some spaces got eaten at
223 ** end-of-line. (We check this later)
224 */
225 this_ch = 0;
226 } else {
Jack Jansen5d957971995-11-14 10:35:19 +0000227 /* Check the character for legality
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000228 ** The 64 in stead of the expected 63 is because
229 ** there are a few uuencodes out there that use
Guido van Rossum92d89172001-01-09 02:11:57 +0000230 ** '`' as zero instead of space.
Jack Jansen5d957971995-11-14 10:35:19 +0000231 */
232 if ( this_ch < ' ' || this_ch > (' ' + 64)) {
Jack Jansen72781191995-08-07 14:34:15 +0000233 PyErr_SetString(Error, "Illegal char");
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +0000234 PyBuffer_Release(&pascii);
Jack Jansen72781191995-08-07 14:34:15 +0000235 Py_DECREF(rv);
236 return NULL;
237 }
238 this_ch = (this_ch - ' ') & 077;
239 }
240 /*
241 ** Shift it in on the low end, and see if there's
242 ** a byte ready for output.
243 */
244 leftchar = (leftchar << 6) | (this_ch);
245 leftbits += 6;
246 if ( leftbits >= 8 ) {
247 leftbits -= 8;
248 *bin_data++ = (leftchar >> leftbits) & 0xff;
249 leftchar &= ((1 << leftbits) - 1);
250 bin_len--;
251 }
252 }
253 /*
254 ** Finally, check that if there's anything left on the line
255 ** that it's whitespace only.
256 */
257 while( ascii_len-- > 0 ) {
258 this_ch = *ascii_data++;
Guido van Rossum92d89172001-01-09 02:11:57 +0000259 /* Extra '`' may be written as padding in some cases */
260 if ( this_ch != ' ' && this_ch != ' '+64 &&
Guido van Rossum1243ae71997-07-11 18:36:28 +0000261 this_ch != '\n' && this_ch != '\r' ) {
Jack Jansen72781191995-08-07 14:34:15 +0000262 PyErr_SetString(Error, "Trailing garbage");
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +0000263 PyBuffer_Release(&pascii);
Jack Jansen72781191995-08-07 14:34:15 +0000264 Py_DECREF(rv);
265 return NULL;
266 }
267 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000268 PyBuffer_Release(&pascii);
Jack Jansen72781191995-08-07 14:34:15 +0000269 return rv;
270}
271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000272PyDoc_STRVAR(doc_b2a_uu, "(bin) -> ascii. Uuencode line of data");
Tim Peters934c1a12002-07-02 22:24:50 +0000273
Jack Jansen72781191995-08-07 14:34:15 +0000274static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000275binascii_b2a_uu(PyObject *self, PyObject *args)
Jack Jansen72781191995-08-07 14:34:15 +0000276{
Martin v. Löwis423be952008-08-13 15:53:07 +0000277 Py_buffer pbin;
Jack Jansen72781191995-08-07 14:34:15 +0000278 unsigned char *ascii_data, *bin_data;
279 int leftbits = 0;
280 unsigned char this_ch;
281 unsigned int leftchar = 0;
282 PyObject *rv;
Thomas Woutersf98db652006-03-01 21:37:32 +0000283 Py_ssize_t bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000284
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000285 if ( !PyArg_ParseTuple(args, "y*:b2a_uu", &pbin) )
Jack Jansen72781191995-08-07 14:34:15 +0000286 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000287 bin_data = pbin.buf;
288 bin_len = pbin.len;
Jack Jansen72781191995-08-07 14:34:15 +0000289 if ( bin_len > 45 ) {
290 /* The 45 is a limit that appears in all uuencode's */
291 PyErr_SetString(Error, "At most 45 bytes at once");
Martin v. Löwis423be952008-08-13 15:53:07 +0000292 PyBuffer_Release(&pbin);
Jack Jansen72781191995-08-07 14:34:15 +0000293 return NULL;
294 }
295
296 /* We're lazy and allocate to much (fixed up later) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000297 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2+2)) == NULL ) {
298 PyBuffer_Release(&pbin);
Jack Jansen72781191995-08-07 14:34:15 +0000299 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000300 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000301 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000302
303 /* Store the length */
304 *ascii_data++ = ' ' + (bin_len & 077);
Tim Peters934c1a12002-07-02 22:24:50 +0000305
Jack Jansen72781191995-08-07 14:34:15 +0000306 for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
307 /* Shift the data (or padding) into our buffer */
308 if ( bin_len > 0 ) /* Data */
309 leftchar = (leftchar << 8) | *bin_data;
310 else /* Padding */
311 leftchar <<= 8;
312 leftbits += 8;
313
314 /* See if there are 6-bit groups ready */
315 while ( leftbits >= 6 ) {
316 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
317 leftbits -= 6;
318 *ascii_data++ = this_ch + ' ';
319 }
320 }
321 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000322
Christian Heimes72b710a2008-05-26 13:28:38 +0000323 if (_PyBytes_Resize(&rv,
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000324 (ascii_data -
Christian Heimes72b710a2008-05-26 13:28:38 +0000325 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000326 Py_DECREF(rv);
327 rv = NULL;
328 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000329 PyBuffer_Release(&pbin);
Jack Jansen72781191995-08-07 14:34:15 +0000330 return rv;
331}
332
Guido van Rossum2db4f471999-10-19 19:05:14 +0000333
334static int
Thomas Woutersf98db652006-03-01 21:37:32 +0000335binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num)
Guido van Rossum2db4f471999-10-19 19:05:14 +0000336{
Tim Peters934c1a12002-07-02 22:24:50 +0000337 /* Finds & returns the (num+1)th
Guido van Rossum2db4f471999-10-19 19:05:14 +0000338 ** valid character for base64, or -1 if none.
339 */
340
341 int ret = -1;
342 unsigned char c, b64val;
343
344 while ((slen > 0) && (ret == -1)) {
345 c = *s;
346 b64val = table_a2b_base64[c & 0x7f];
347 if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
348 if (num == 0)
349 ret = *s;
350 num--;
351 }
352
353 s++;
354 slen--;
355 }
356 return ret;
357}
358
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359PyDoc_STRVAR(doc_a2b_base64, "(ascii) -> bin. Decode a line of base64 data");
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000360
361static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000362binascii_a2b_base64(PyObject *self, PyObject *args)
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000363{
Martin v. Löwis423be952008-08-13 15:53:07 +0000364 Py_buffer pascii;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000365 unsigned char *ascii_data, *bin_data;
366 int leftbits = 0;
367 unsigned char this_ch;
368 unsigned int leftchar = 0;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000369 PyObject *rv;
Thomas Woutersf98db652006-03-01 21:37:32 +0000370 Py_ssize_t ascii_len, bin_len;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000371 int quad_pos = 0;
Tim Peters934c1a12002-07-02 22:24:50 +0000372
Martin v. Löwis423be952008-08-13 15:53:07 +0000373 if ( !PyArg_ParseTuple(args, "y*:a2b_base64", &pascii) )
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000374 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000375 ascii_data = pascii.buf;
376 ascii_len = pascii.len;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000377
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000378 assert(ascii_len >= 0);
379
Martin v. Löwis423be952008-08-13 15:53:07 +0000380 if (ascii_len > PY_SSIZE_T_MAX - 3) {
381 PyBuffer_Release(&pascii);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000382 return PyErr_NoMemory();
Martin v. Löwis423be952008-08-13 15:53:07 +0000383 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000384
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000385 bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
386
387 /* Allocate the buffer */
Martin v. Löwis423be952008-08-13 15:53:07 +0000388 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL ) {
389 PyBuffer_Release(&pascii);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000390 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000391 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000392 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000393 bin_len = 0;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000394
395 for( ; ascii_len > 0; ascii_len--, ascii_data++) {
396 this_ch = *ascii_data;
397
398 if (this_ch > 0x7f ||
399 this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
Jack Jansenba1de3b1996-01-22 10:47:15 +0000400 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000401
402 /* Check for pad sequences and ignore
403 ** the invalid ones.
404 */
405 if (this_ch == BASE64_PAD) {
406 if ( (quad_pos < 2) ||
407 ((quad_pos == 2) &&
408 (binascii_find_valid(ascii_data, ascii_len, 1)
409 != BASE64_PAD)) )
410 {
411 continue;
412 }
413 else {
414 /* A pad sequence means no more input.
415 ** We've already interpreted the data
416 ** from the quad at this point.
417 */
418 leftbits = 0;
419 break;
420 }
421 }
422
423 this_ch = table_a2b_base64[*ascii_data];
424 if ( this_ch == (unsigned char) -1 )
425 continue;
426
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000427 /*
428 ** Shift it in on the low end, and see if there's
429 ** a byte ready for output.
430 */
Guido van Rossum2db4f471999-10-19 19:05:14 +0000431 quad_pos = (quad_pos + 1) & 0x03;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000432 leftchar = (leftchar << 6) | (this_ch);
433 leftbits += 6;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000434
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000435 if ( leftbits >= 8 ) {
436 leftbits -= 8;
437 *bin_data++ = (leftchar >> leftbits) & 0xff;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000438 bin_len++;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000439 leftchar &= ((1 << leftbits) - 1);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000440 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000441 }
442
443 if (leftbits != 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000444 PyBuffer_Release(&pascii);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000445 PyErr_SetString(Error, "Incorrect padding");
446 Py_DECREF(rv);
447 return NULL;
448 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000449
Thomas Wouters9e1c1922003-03-17 11:24:29 +0000450 /* And set string size correctly. If the result string is empty
451 ** (because the input was all invalid) return the shared empty
Christian Heimes72b710a2008-05-26 13:28:38 +0000452 ** string instead; _PyBytes_Resize() won't do this for us.
Thomas Wouters9e1c1922003-03-17 11:24:29 +0000453 */
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000454 if (bin_len > 0) {
Christian Heimes72b710a2008-05-26 13:28:38 +0000455 if (_PyBytes_Resize(&rv, bin_len) < 0) {
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000456 Py_DECREF(rv);
457 rv = NULL;
458 }
459 }
Thomas Wouters9e1c1922003-03-17 11:24:29 +0000460 else {
461 Py_DECREF(rv);
Christian Heimes72b710a2008-05-26 13:28:38 +0000462 rv = PyBytes_FromStringAndSize("", 0);
Thomas Wouters9e1c1922003-03-17 11:24:29 +0000463 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000464 PyBuffer_Release(&pascii);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000465 return rv;
466}
467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000468PyDoc_STRVAR(doc_b2a_base64, "(bin) -> ascii. Base64-code line of data");
Tim Peters934c1a12002-07-02 22:24:50 +0000469
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000470static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000471binascii_b2a_base64(PyObject *self, PyObject *args)
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000472{
Martin v. Löwis423be952008-08-13 15:53:07 +0000473 Py_buffer pbuf;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000474 unsigned char *ascii_data, *bin_data;
475 int leftbits = 0;
476 unsigned char this_ch;
477 unsigned int leftchar = 0;
478 PyObject *rv;
Thomas Woutersf98db652006-03-01 21:37:32 +0000479 Py_ssize_t bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000480
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000481 if ( !PyArg_ParseTuple(args, "y*:b2a_base64", &pbuf) )
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000482 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000483 bin_data = pbuf.buf;
484 bin_len = pbuf.len;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000485
486 assert(bin_len >= 0);
487
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000488 if ( bin_len > BASE64_MAXBIN ) {
489 PyErr_SetString(Error, "Too much data for base64 line");
Martin v. Löwis423be952008-08-13 15:53:07 +0000490 PyBuffer_Release(&pbuf);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000491 return NULL;
492 }
Tim Peters934c1a12002-07-02 22:24:50 +0000493
Tim Peters1fbb5772001-12-19 04:41:35 +0000494 /* We're lazy and allocate too much (fixed up later).
495 "+3" leaves room for up to two pad characters and a trailing
496 newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
Martin v. Löwis423be952008-08-13 15:53:07 +0000497 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL ) {
498 PyBuffer_Release(&pbuf);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000499 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000500 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000501 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000502
503 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
504 /* Shift the data into our buffer */
505 leftchar = (leftchar << 8) | *bin_data;
506 leftbits += 8;
507
508 /* See if there are 6-bit groups ready */
509 while ( leftbits >= 6 ) {
510 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
511 leftbits -= 6;
512 *ascii_data++ = table_b2a_base64[this_ch];
513 }
514 }
515 if ( leftbits == 2 ) {
516 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
517 *ascii_data++ = BASE64_PAD;
518 *ascii_data++ = BASE64_PAD;
519 } else if ( leftbits == 4 ) {
520 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
521 *ascii_data++ = BASE64_PAD;
Tim Peters934c1a12002-07-02 22:24:50 +0000522 }
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000523 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000524
Christian Heimes72b710a2008-05-26 13:28:38 +0000525 if (_PyBytes_Resize(&rv,
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000526 (ascii_data -
Christian Heimes72b710a2008-05-26 13:28:38 +0000527 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000528 Py_DECREF(rv);
529 rv = NULL;
530 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000531 PyBuffer_Release(&pbuf);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000532 return rv;
533}
534
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000535PyDoc_STRVAR(doc_a2b_hqx, "ascii -> bin, done. Decode .hqx coding");
Jack Jansen72781191995-08-07 14:34:15 +0000536
537static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000538binascii_a2b_hqx(PyObject *self, PyObject *args)
Jack Jansen72781191995-08-07 14:34:15 +0000539{
540 unsigned char *ascii_data, *bin_data;
541 int leftbits = 0;
542 unsigned char this_ch;
Jack Janseneaeb1c81995-08-14 12:17:57 +0000543 unsigned int leftchar = 0;
Jack Jansen72781191995-08-07 14:34:15 +0000544 PyObject *rv;
Thomas Woutersf98db652006-03-01 21:37:32 +0000545 Py_ssize_t len;
Jack Jansen72781191995-08-07 14:34:15 +0000546 int done = 0;
Tim Peters934c1a12002-07-02 22:24:50 +0000547
Guido van Rossum43713e52000-02-29 13:59:29 +0000548 if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) )
Jack Jansen72781191995-08-07 14:34:15 +0000549 return NULL;
550
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000551 assert(len >= 0);
552
553 if (len > PY_SSIZE_T_MAX - 2)
554 return PyErr_NoMemory();
555
Raymond Hettinger658717e2004-09-06 22:58:37 +0000556 /* Allocate a string that is too big (fixed later)
557 Add two to the initial length to prevent interning which
558 would preclude subsequent resizing. */
Christian Heimes72b710a2008-05-26 13:28:38 +0000559 if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
Jack Jansen72781191995-08-07 14:34:15 +0000560 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +0000561 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000562
563 for( ; len > 0 ; len--, ascii_data++ ) {
564 /* Get the byte and look it up */
565 this_ch = table_a2b_hqx[*ascii_data];
566 if ( this_ch == SKIP )
567 continue;
568 if ( this_ch == FAIL ) {
569 PyErr_SetString(Error, "Illegal char");
570 Py_DECREF(rv);
571 return NULL;
572 }
573 if ( this_ch == DONE ) {
574 /* The terminating colon */
575 done = 1;
576 break;
577 }
578
579 /* Shift it into the buffer and see if any bytes are ready */
580 leftchar = (leftchar << 6) | (this_ch);
581 leftbits += 6;
582 if ( leftbits >= 8 ) {
583 leftbits -= 8;
584 *bin_data++ = (leftchar >> leftbits) & 0xff;
585 leftchar &= ((1 << leftbits) - 1);
586 }
587 }
Tim Peters934c1a12002-07-02 22:24:50 +0000588
Jack Jansen72781191995-08-07 14:34:15 +0000589 if ( leftbits && !done ) {
590 PyErr_SetString(Incomplete,
591 "String has incomplete number of bytes");
592 Py_DECREF(rv);
593 return NULL;
594 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000595 if (_PyBytes_Resize(&rv,
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000596 (bin_data -
Christian Heimes72b710a2008-05-26 13:28:38 +0000597 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000598 Py_DECREF(rv);
599 rv = NULL;
600 }
Guido van Rossum9c6ba5e1997-01-12 20:02:04 +0000601 if (rv) {
602 PyObject *rrv = Py_BuildValue("Oi", rv, done);
603 Py_DECREF(rv);
604 return rrv;
605 }
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000606
Jack Jansen72781191995-08-07 14:34:15 +0000607 return NULL;
608}
609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000610PyDoc_STRVAR(doc_rlecode_hqx, "Binhex RLE-code binary data");
Jack Jansen72781191995-08-07 14:34:15 +0000611
612static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000613binascii_rlecode_hqx(PyObject *self, PyObject *args)
Jack Jansen72781191995-08-07 14:34:15 +0000614{
Martin v. Löwis423be952008-08-13 15:53:07 +0000615 Py_buffer pbuf;
Jack Jansen72781191995-08-07 14:34:15 +0000616 unsigned char *in_data, *out_data;
617 PyObject *rv;
618 unsigned char ch;
Thomas Woutersf98db652006-03-01 21:37:32 +0000619 Py_ssize_t in, inend, len;
Tim Peters934c1a12002-07-02 22:24:50 +0000620
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000621 if ( !PyArg_ParseTuple(args, "y*:rlecode_hqx", &pbuf) )
Jack Jansen72781191995-08-07 14:34:15 +0000622 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000623 in_data = pbuf.buf;
624 len = pbuf.len;
Jack Jansen72781191995-08-07 14:34:15 +0000625
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000626 assert(len >= 0);
627
Martin v. Löwis423be952008-08-13 15:53:07 +0000628 if (len > PY_SSIZE_T_MAX / 2 - 2) {
629 PyBuffer_Release(&pbuf);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000630 return PyErr_NoMemory();
Martin v. Löwis423be952008-08-13 15:53:07 +0000631 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000632
Jack Jansen72781191995-08-07 14:34:15 +0000633 /* Worst case: output is twice as big as input (fixed later) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000634 if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) {
635 PyBuffer_Release(&pbuf);
Jack Jansen72781191995-08-07 14:34:15 +0000636 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000637 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000638 out_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000639
Jack Jansen72781191995-08-07 14:34:15 +0000640 for( in=0; in<len; in++) {
641 ch = in_data[in];
642 if ( ch == RUNCHAR ) {
643 /* RUNCHAR. Escape it. */
644 *out_data++ = RUNCHAR;
645 *out_data++ = 0;
646 } else {
647 /* Check how many following are the same */
648 for(inend=in+1;
649 inend<len && in_data[inend] == ch &&
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000650 inend < in+255;
Jack Jansen72781191995-08-07 14:34:15 +0000651 inend++) ;
Jack Jansen0223aa11995-08-31 13:44:23 +0000652 if ( inend - in > 3 ) {
653 /* More than 3 in a row. Output RLE. */
Jack Jansen72781191995-08-07 14:34:15 +0000654 *out_data++ = ch;
655 *out_data++ = RUNCHAR;
656 *out_data++ = inend-in;
657 in = inend-1;
658 } else {
659 /* Less than 3. Output the byte itself */
660 *out_data++ = ch;
661 }
662 }
663 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000664 if (_PyBytes_Resize(&rv,
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000665 (out_data -
Christian Heimes72b710a2008-05-26 13:28:38 +0000666 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000667 Py_DECREF(rv);
668 rv = NULL;
669 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000670 PyBuffer_Release(&pbuf);
Jack Jansen72781191995-08-07 14:34:15 +0000671 return rv;
672}
673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000674PyDoc_STRVAR(doc_b2a_hqx, "Encode .hqx data");
Tim Peters934c1a12002-07-02 22:24:50 +0000675
Jack Jansen72781191995-08-07 14:34:15 +0000676static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000677binascii_b2a_hqx(PyObject *self, PyObject *args)
Jack Jansen72781191995-08-07 14:34:15 +0000678{
Martin v. Löwis423be952008-08-13 15:53:07 +0000679 Py_buffer pbin;
Jack Jansen72781191995-08-07 14:34:15 +0000680 unsigned char *ascii_data, *bin_data;
681 int leftbits = 0;
682 unsigned char this_ch;
Jack Janseneaeb1c81995-08-14 12:17:57 +0000683 unsigned int leftchar = 0;
Jack Jansen72781191995-08-07 14:34:15 +0000684 PyObject *rv;
Thomas Woutersf98db652006-03-01 21:37:32 +0000685 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000686
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000687 if ( !PyArg_ParseTuple(args, "y*:b2a_hqx", &pbin) )
Jack Jansen72781191995-08-07 14:34:15 +0000688 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000689 bin_data = pbin.buf;
690 len = pbin.len;
Jack Jansen72781191995-08-07 14:34:15 +0000691
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000692 assert(len >= 0);
693
Martin v. Löwis423be952008-08-13 15:53:07 +0000694 if (len > PY_SSIZE_T_MAX / 2 - 2) {
695 PyBuffer_Release(&pbin);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000696 return PyErr_NoMemory();
Martin v. Löwis423be952008-08-13 15:53:07 +0000697 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000698
Jack Jansen72781191995-08-07 14:34:15 +0000699 /* Allocate a buffer that is at least large enough */
Martin v. Löwis423be952008-08-13 15:53:07 +0000700 if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL ) {
701 PyBuffer_Release(&pbin);
Jack Jansen72781191995-08-07 14:34:15 +0000702 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000703 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000704 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000705
Jack Jansen72781191995-08-07 14:34:15 +0000706 for( ; len > 0 ; len--, bin_data++ ) {
707 /* Shift into our buffer, and output any 6bits ready */
708 leftchar = (leftchar << 8) | *bin_data;
709 leftbits += 8;
710 while ( leftbits >= 6 ) {
711 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
712 leftbits -= 6;
713 *ascii_data++ = table_b2a_hqx[this_ch];
714 }
715 }
716 /* Output a possible runt byte */
717 if ( leftbits ) {
718 leftchar <<= (6-leftbits);
719 *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
720 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000721 if (_PyBytes_Resize(&rv,
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000722 (ascii_data -
Christian Heimes72b710a2008-05-26 13:28:38 +0000723 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000724 Py_DECREF(rv);
725 rv = NULL;
726 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000727 PyBuffer_Release(&pbin);
Jack Jansen72781191995-08-07 14:34:15 +0000728 return rv;
729}
730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000731PyDoc_STRVAR(doc_rledecode_hqx, "Decode hexbin RLE-coded string");
Tim Peters934c1a12002-07-02 22:24:50 +0000732
Jack Jansen72781191995-08-07 14:34:15 +0000733static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000734binascii_rledecode_hqx(PyObject *self, PyObject *args)
Jack Jansen72781191995-08-07 14:34:15 +0000735{
Martin v. Löwis423be952008-08-13 15:53:07 +0000736 Py_buffer pin;
Jack Jansen72781191995-08-07 14:34:15 +0000737 unsigned char *in_data, *out_data;
738 unsigned char in_byte, in_repeat;
739 PyObject *rv;
Thomas Woutersf98db652006-03-01 21:37:32 +0000740 Py_ssize_t in_len, out_len, out_len_left;
Jack Jansen72781191995-08-07 14:34:15 +0000741
Martin v. Löwis423be952008-08-13 15:53:07 +0000742 if ( !PyArg_ParseTuple(args, "s*:rledecode_hqx", &pin) )
Jack Jansen72781191995-08-07 14:34:15 +0000743 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000744 in_data = pin.buf;
745 in_len = pin.len;
Jack Jansen72781191995-08-07 14:34:15 +0000746
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000747 assert(in_len >= 0);
748
Jack Jansen72781191995-08-07 14:34:15 +0000749 /* Empty string is a special case */
Martin v. Löwis423be952008-08-13 15:53:07 +0000750 if ( in_len == 0 ) {
751 PyBuffer_Release(&pin);
Christian Heimes72b710a2008-05-26 13:28:38 +0000752 return PyBytes_FromStringAndSize("", 0);
Martin v. Löwis423be952008-08-13 15:53:07 +0000753 }
754 else if (in_len > PY_SSIZE_T_MAX / 2) {
755 PyBuffer_Release(&pin);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000756 return PyErr_NoMemory();
Martin v. Löwis423be952008-08-13 15:53:07 +0000757 }
Jack Jansen72781191995-08-07 14:34:15 +0000758
759 /* Allocate a buffer of reasonable size. Resized when needed */
760 out_len = in_len*2;
Martin v. Löwis423be952008-08-13 15:53:07 +0000761 if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL ) {
762 PyBuffer_Release(&pin);
Jack Jansen72781191995-08-07 14:34:15 +0000763 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000764 }
Jack Jansen72781191995-08-07 14:34:15 +0000765 out_len_left = out_len;
Christian Heimes72b710a2008-05-26 13:28:38 +0000766 out_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000767
768 /*
769 ** We need two macros here to get/put bytes and handle
770 ** end-of-buffer for input and output strings.
771 */
772#define INBYTE(b) \
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000773 do { \
774 if ( --in_len < 0 ) { \
775 PyErr_SetString(Incomplete, ""); \
776 Py_DECREF(rv); \
Martin v. Löwis423be952008-08-13 15:53:07 +0000777 PyBuffer_Release(&pin); \
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000778 return NULL; \
779 } \
780 b = *in_data++; \
781 } while(0)
Tim Peters934c1a12002-07-02 22:24:50 +0000782
Jack Jansen72781191995-08-07 14:34:15 +0000783#define OUTBYTE(b) \
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000784 do { \
785 if ( --out_len_left < 0 ) { \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000786 if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
Christian Heimes72b710a2008-05-26 13:28:38 +0000787 if (_PyBytes_Resize(&rv, 2*out_len) < 0) \
Martin v. Löwis423be952008-08-13 15:53:07 +0000788 { Py_DECREF(rv); PyBuffer_Release(&pin); return NULL; } \
Christian Heimes72b710a2008-05-26 13:28:38 +0000789 out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000790 + out_len; \
791 out_len_left = out_len-1; \
792 out_len = out_len * 2; \
793 } \
794 *out_data++ = b; \
795 } while(0)
Jack Jansen72781191995-08-07 14:34:15 +0000796
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000797 /*
798 ** Handle first byte separately (since we have to get angry
799 ** in case of an orphaned RLE code).
800 */
801 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000802
803 if (in_byte == RUNCHAR) {
804 INBYTE(in_repeat);
805 if (in_repeat != 0) {
806 /* Note Error, not Incomplete (which is at the end
807 ** of the string only). This is a programmer error.
808 */
809 PyErr_SetString(Error, "Orphaned RLE code at start");
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +0000810 PyBuffer_Release(&pin);
Jack Jansen72781191995-08-07 14:34:15 +0000811 Py_DECREF(rv);
812 return NULL;
813 }
814 OUTBYTE(RUNCHAR);
815 } else {
816 OUTBYTE(in_byte);
817 }
Tim Peters934c1a12002-07-02 22:24:50 +0000818
Jack Jansen72781191995-08-07 14:34:15 +0000819 while( in_len > 0 ) {
820 INBYTE(in_byte);
821
822 if (in_byte == RUNCHAR) {
823 INBYTE(in_repeat);
824 if ( in_repeat == 0 ) {
825 /* Just an escaped RUNCHAR value */
826 OUTBYTE(RUNCHAR);
827 } else {
828 /* Pick up value and output a sequence of it */
829 in_byte = out_data[-1];
830 while ( --in_repeat > 0 )
831 OUTBYTE(in_byte);
832 }
833 } else {
834 /* Normal byte */
835 OUTBYTE(in_byte);
836 }
837 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000838 if (_PyBytes_Resize(&rv,
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000839 (out_data -
Christian Heimes72b710a2008-05-26 13:28:38 +0000840 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000841 Py_DECREF(rv);
842 rv = NULL;
843 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000844 PyBuffer_Release(&pin);
Jack Jansen72781191995-08-07 14:34:15 +0000845 return rv;
846}
847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000848PyDoc_STRVAR(doc_crc_hqx,
849"(data, oldcrc) -> newcrc. Compute hqx CRC incrementally");
Jack Jansen72781191995-08-07 14:34:15 +0000850
851static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000852binascii_crc_hqx(PyObject *self, PyObject *args)
Jack Jansen72781191995-08-07 14:34:15 +0000853{
Martin v. Löwis423be952008-08-13 15:53:07 +0000854 Py_buffer pin;
Jack Jansen72781191995-08-07 14:34:15 +0000855 unsigned char *bin_data;
856 unsigned int crc;
Thomas Woutersf98db652006-03-01 21:37:32 +0000857 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000858
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000859 if ( !PyArg_ParseTuple(args, "y*i:crc_hqx", &pin, &crc) )
Jack Jansen72781191995-08-07 14:34:15 +0000860 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000861 bin_data = pin.buf;
862 len = pin.len;
Jack Jansen72781191995-08-07 14:34:15 +0000863
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000864 while(len-- > 0) {
Jack Jansen72781191995-08-07 14:34:15 +0000865 crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++];
866 }
867
Martin v. Löwis423be952008-08-13 15:53:07 +0000868 PyBuffer_Release(&pin);
Jack Jansen72781191995-08-07 14:34:15 +0000869 return Py_BuildValue("i", crc);
870}
871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000872PyDoc_STRVAR(doc_crc32,
873"(data, oldcrc = 0) -> newcrc. Compute CRC-32 incrementally");
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000874
Christian Heimes1dc54002008-03-24 02:19:29 +0000875#ifdef USE_ZLIB_CRC32
876/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
877static PyObject *
878binascii_crc32(PyObject *self, PyObject *args)
879{
Christian Heimescc47b052008-03-25 14:56:36 +0000880 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000881 Py_buffer pbuf;
Christian Heimes1dc54002008-03-24 02:19:29 +0000882 Byte *buf;
Neal Norwitz4027bf82008-03-24 04:59:05 +0000883 Py_ssize_t len;
Christian Heimescc47b052008-03-25 14:56:36 +0000884 int signed_val;
885
Martin v. Löwis15b16a32008-12-02 06:00:15 +0000886 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Christian Heimes1dc54002008-03-24 02:19:29 +0000887 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000888 buf = (Byte*)pbuf.buf;
889 len = pbuf.len;
Christian Heimescc47b052008-03-25 14:56:36 +0000890 signed_val = crc32(crc32val, buf, len);
Martin v. Löwis423be952008-08-13 15:53:07 +0000891 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +0000892 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Christian Heimes1dc54002008-03-24 02:19:29 +0000893}
894#else /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000895/* Crc - 32 BIT ANSI X3.66 CRC checksum files
896 Also known as: ISO 3307
897**********************************************************************|
898* *|
899* Demonstration program to compute the 32-bit CRC used as the frame *|
900* check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *|
901* and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *|
902* protocol). The 32-bit FCS was added via the Federal Register, *|
903* 1 June 1982, p.23798. I presume but don't know for certain that *|
904* this polynomial is or will be included in CCITT V.41, which *|
905* defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *|
906* PUB 78 says that the 32-bit FCS reduces otherwise undetected *|
907* errors by a factor of 10^-5 over 16-bit FCS. *|
908* *|
909**********************************************************************|
910
911 Copyright (C) 1986 Gary S. Brown. You may use this program, or
912 code or tables extracted from it, as desired without restriction.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000913
Tim Peters934c1a12002-07-02 22:24:50 +0000914 First, the polynomial itself and its table of feedback terms. The
915 polynomial is
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
917 Note that we take it "backwards" and put the highest-order term in
918 the lowest-order bit. The X^32 term is "implied"; the LSB is the
919 X^31 term, etc. The X^0 term (usually shown as "+1") results in
920 the MSB being 1.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000921
Tim Peters934c1a12002-07-02 22:24:50 +0000922 Note that the usual hardware shift register implementation, which
923 is what we're using (we're merely optimizing it by doing eight-bit
924 chunks at a time) shifts bits into the lowest-order term. In our
925 implementation, that means shifting towards the right. Why do we
926 do it this way? Because the calculated CRC must be transmitted in
927 order from highest-order term to lowest-order term. UARTs transmit
928 characters in order from LSB to MSB. By storing the CRC this way,
929 we hand it to the UART in the order low-byte to high-byte; the UART
930 sends each low-bit to hight-bit; and the result is transmission bit
931 by bit from highest- to lowest-order term without requiring any bit
932 shuffling on our part. Reception works similarly.
933
934 The feedback terms table consists of 256, 32-bit entries. Notes:
935
936 1. The table can be generated at runtime if desired; code to do so
937 is shown later. It might not be obvious, but the feedback
938 terms simply represent the results of eight shift/xor opera-
939 tions for all combinations of data and CRC register values.
940
941 2. The CRC accumulation logic is the same for all CRC polynomials,
942 be they sixteen or thirty-two bits wide. You simply choose the
943 appropriate table. Alternatively, because the table can be
944 generated at runtime, you can start by generating the table for
945 the polynomial in question and use exactly the same "updcrc",
946 if your application needn't simultaneously handle two CRC
947 polynomials. (Note, however, that XMODEM is strange.)
948
949 3. For 16-bit CRCs, the table entries need be only 16 bits wide;
950 of course, 32-bit entries work OK if the high 16 bits are zero.
951
952 4. The values must be right-shifted by eight bits by the "updcrc"
953 logic; the shift must be unsigned (bring in zeroes). On some
954 hardware you could probably optimize the shift in assembler by
955 using byte-swap instructions.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000956********************************************************************/
957
Gregory P. Smith3c0e4d22008-03-25 07:51:12 +0000958static unsigned int crc_32_tab[256] = {
9590x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
9600x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
9610xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
9620x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
9630x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
9640x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
9650xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
9660xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
9670x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
9680x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
9690xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
9700xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
9710x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
9720x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
9730x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
9740xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
9750x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
9760x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
9770x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
9780xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
9790x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
9800x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
9810xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
9820xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
9830x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
9840x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
9850x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
9860x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
9870xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
9880x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
9890x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
9900x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
9910xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
9920xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
9930x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
9940x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
9950xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
9960xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
9970x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
9980x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
9990x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
10000xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
10010x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
10020x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
10030x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
10040xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
10050x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
10060x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
10070xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
10080xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
10090x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
10100x2d02ef8dU
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001011};
1012
1013static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001014binascii_crc32(PyObject *self, PyObject *args)
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001015{ /* By Jim Ahlstrom; All rights transferred to CNRI */
Martin v. Löwis423be952008-08-13 15:53:07 +00001016 Py_buffer pbin;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001017 unsigned char *bin_data;
Gregory P. Smith27275032008-03-20 06:20:09 +00001018 unsigned int crc = 0; /* initial value of CRC */
Thomas Woutersf98db652006-03-01 21:37:32 +00001019 Py_ssize_t len;
Gregory P. Smith27275032008-03-20 06:20:09 +00001020 unsigned int result;
Tim Peters934c1a12002-07-02 22:24:50 +00001021
Martin v. Löwis423be952008-08-13 15:53:07 +00001022 if ( !PyArg_ParseTuple(args, "s*|I:crc32", &pbin, &crc) )
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001023 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00001024 bin_data = pbin.buf;
1025 len = pbin.len;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001026
Tim Peters934c1a12002-07-02 22:24:50 +00001027 crc = ~ crc;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001028 while (len-- > 0) {
Gregory P. Smith27275032008-03-20 06:20:09 +00001029 crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001030 /* Note: (crc >> 8) MUST zero fill on left */
Gregory P. Smith27275032008-03-20 06:20:09 +00001031 }
Tim Petersa98011c2002-07-02 20:20:08 +00001032
Gregory P. Smith27275032008-03-20 06:20:09 +00001033 result = (crc ^ 0xFFFFFFFF);
Antoine Pitrou34ed9f72008-08-15 14:51:48 +00001034 PyBuffer_Release(&pbin);
Gregory P. Smith27275032008-03-20 06:20:09 +00001035 return PyLong_FromUnsignedLong(result & 0xffffffff);
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001036}
Christian Heimes1dc54002008-03-24 02:19:29 +00001037#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001038
Barry Warsawe977c212000-08-15 06:07:13 +00001039
1040static PyObject *
1041binascii_hexlify(PyObject *self, PyObject *args)
1042{
Martin v. Löwis423be952008-08-13 15:53:07 +00001043 Py_buffer parg;
Barry Warsawe977c212000-08-15 06:07:13 +00001044 char* argbuf;
Thomas Woutersf98db652006-03-01 21:37:32 +00001045 Py_ssize_t arglen;
Barry Warsawe977c212000-08-15 06:07:13 +00001046 PyObject *retval;
1047 char* retbuf;
Thomas Woutersf98db652006-03-01 21:37:32 +00001048 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001049
Martin v. Löwis15b16a32008-12-02 06:00:15 +00001050 if (!PyArg_ParseTuple(args, "y*:b2a_hex", &parg))
Barry Warsawe977c212000-08-15 06:07:13 +00001051 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00001052 argbuf = parg.buf;
1053 arglen = parg.len;
Barry Warsawe977c212000-08-15 06:07:13 +00001054
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001055 assert(arglen >= 0);
Martin v. Löwis423be952008-08-13 15:53:07 +00001056 if (arglen > PY_SSIZE_T_MAX / 2) {
1057 PyBuffer_Release(&parg);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001058 return PyErr_NoMemory();
Martin v. Löwis423be952008-08-13 15:53:07 +00001059 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001060
Christian Heimes72b710a2008-05-26 13:28:38 +00001061 retval = PyBytes_FromStringAndSize(NULL, arglen*2);
Martin v. Löwis423be952008-08-13 15:53:07 +00001062 if (!retval) {
1063 PyBuffer_Release(&parg);
Barry Warsawe977c212000-08-15 06:07:13 +00001064 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00001065 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001066 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001067
1068 /* make hex version of string, taken from shamodule.c */
1069 for (i=j=0; i < arglen; i++) {
1070 char c;
1071 c = (argbuf[i] >> 4) & 0xf;
1072 c = (c>9) ? c+'a'-10 : c + '0';
1073 retbuf[j++] = c;
1074 c = argbuf[i] & 0xf;
1075 c = (c>9) ? c+'a'-10 : c + '0';
1076 retbuf[j++] = c;
1077 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001078 PyBuffer_Release(&parg);
Barry Warsawe977c212000-08-15 06:07:13 +00001079 return retval;
Barry Warsawe977c212000-08-15 06:07:13 +00001080}
1081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001082PyDoc_STRVAR(doc_hexlify,
Barry Warsawe977c212000-08-15 06:07:13 +00001083"b2a_hex(data) -> s; Hexadecimal representation of binary data.\n\
1084\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001085This function is also available as \"hexlify()\".");
Barry Warsawe977c212000-08-15 06:07:13 +00001086
1087
1088static int
Tim Peters934c1a12002-07-02 22:24:50 +00001089to_int(int c)
Barry Warsawe977c212000-08-15 06:07:13 +00001090{
1091 if (isdigit(c))
1092 return c - '0';
1093 else {
1094 if (isupper(c))
1095 c = tolower(c);
1096 if (c >= 'a' && c <= 'f')
1097 return c - 'a' + 10;
1098 }
1099 return -1;
1100}
1101
1102
1103static PyObject *
1104binascii_unhexlify(PyObject *self, PyObject *args)
1105{
Martin v. Löwis423be952008-08-13 15:53:07 +00001106 Py_buffer parg;
Barry Warsawe977c212000-08-15 06:07:13 +00001107 char* argbuf;
Thomas Woutersf98db652006-03-01 21:37:32 +00001108 Py_ssize_t arglen;
Barry Warsawe977c212000-08-15 06:07:13 +00001109 PyObject *retval;
1110 char* retbuf;
Thomas Woutersf98db652006-03-01 21:37:32 +00001111 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001112
Martin v. Löwis423be952008-08-13 15:53:07 +00001113 if (!PyArg_ParseTuple(args, "s*:a2b_hex", &parg))
Barry Warsawe977c212000-08-15 06:07:13 +00001114 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00001115 argbuf = parg.buf;
1116 arglen = parg.len;
Barry Warsawe977c212000-08-15 06:07:13 +00001117
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001118 assert(arglen >= 0);
1119
Barry Warsaw16168472000-08-15 06:59:58 +00001120 /* XXX What should we do about strings with an odd length? Should
1121 * we add an implicit leading zero, or a trailing zero? For now,
1122 * raise an exception.
Barry Warsawe977c212000-08-15 06:07:13 +00001123 */
1124 if (arglen % 2) {
Martin v. Löwis423be952008-08-13 15:53:07 +00001125 PyBuffer_Release(&parg);
Guido van Rossum4581ae52007-05-22 21:56:47 +00001126 PyErr_SetString(Error, "Odd-length string");
Barry Warsawe977c212000-08-15 06:07:13 +00001127 return NULL;
1128 }
1129
Christian Heimes72b710a2008-05-26 13:28:38 +00001130 retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
Martin v. Löwis423be952008-08-13 15:53:07 +00001131 if (!retval) {
1132 PyBuffer_Release(&parg);
Barry Warsawe977c212000-08-15 06:07:13 +00001133 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00001134 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001135 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001136
1137 for (i=j=0; i < arglen; i += 2) {
1138 int top = to_int(Py_CHARMASK(argbuf[i]));
1139 int bot = to_int(Py_CHARMASK(argbuf[i+1]));
1140 if (top == -1 || bot == -1) {
Guido van Rossum4581ae52007-05-22 21:56:47 +00001141 PyErr_SetString(Error,
Barry Warsaw16168472000-08-15 06:59:58 +00001142 "Non-hexadecimal digit found");
Barry Warsawe977c212000-08-15 06:07:13 +00001143 goto finally;
1144 }
1145 retbuf[j++] = (top << 4) + bot;
1146 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001147 PyBuffer_Release(&parg);
Barry Warsawe977c212000-08-15 06:07:13 +00001148 return retval;
1149
1150 finally:
Martin v. Löwis423be952008-08-13 15:53:07 +00001151 PyBuffer_Release(&parg);
Barry Warsawe977c212000-08-15 06:07:13 +00001152 Py_DECREF(retval);
1153 return NULL;
1154}
1155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001156PyDoc_STRVAR(doc_unhexlify,
Barry Warsawe977c212000-08-15 06:07:13 +00001157"a2b_hex(hexstr) -> s; Binary data of hexadecimal representation.\n\
1158\n\
1159hexstr must contain an even number of hex digits (upper or lower case).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001160This function is also available as \"unhexlify()\"");
Barry Warsawe977c212000-08-15 06:07:13 +00001161
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001162static int table_hex[128] = {
1163 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1164 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1165 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1166 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1167 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1168 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1169 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1170 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1171};
1172
1173#define hexval(c) table_hex[(unsigned int)(c)]
1174
1175#define MAXLINESIZE 76
1176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001177PyDoc_STRVAR(doc_a2b_qp, "Decode a string of qp-encoded data");
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001178
Tim Peters934c1a12002-07-02 22:24:50 +00001179static PyObject*
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001180binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
1181{
Thomas Wouters7087f782006-03-01 23:10:05 +00001182 Py_ssize_t in, out;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001183 char ch;
Martin v. Löwis423be952008-08-13 15:53:07 +00001184 Py_buffer pdata;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001185 unsigned char *data, *odata;
Thomas Woutersf98db652006-03-01 21:37:32 +00001186 Py_ssize_t datalen = 0;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001187 PyObject *rv;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001188 static char *kwlist[] = {"data", "header", NULL};
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001189 int header = 0;
1190
Martin v. Löwis423be952008-08-13 15:53:07 +00001191 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s*|i", kwlist, &pdata,
1192 &header))
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001193 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00001194 data = pdata.buf;
1195 datalen = pdata.len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001196
Barry Warsaw23164a52004-05-11 02:05:11 +00001197 /* We allocate the output same size as input, this is overkill.
1198 * The previous implementation used calloc() so we'll zero out the
1199 * memory here too, since PyMem_Malloc() does not guarantee that.
1200 */
1201 odata = (unsigned char *) PyMem_Malloc(datalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001202 if (odata == NULL) {
Martin v. Löwis423be952008-08-13 15:53:07 +00001203 PyBuffer_Release(&pdata);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001204 PyErr_NoMemory();
1205 return NULL;
1206 }
Martin v. Löwisb600fe92004-07-27 15:03:53 +00001207 memset(odata, 0, datalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001208
1209 in = out = 0;
1210 while (in < datalen) {
1211 if (data[in] == '=') {
1212 in++;
1213 if (in >= datalen) break;
1214 /* Soft line breaks */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001215 if ((data[in] == '\n') || (data[in] == '\r')) {
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001216 if (data[in] != '\n') {
1217 while (in < datalen && data[in] != '\n') in++;
1218 }
1219 if (in < datalen) in++;
1220 }
1221 else if (data[in] == '=') {
1222 /* broken case from broken python qp */
1223 odata[out++] = '=';
1224 in++;
1225 }
Tim Peters934c1a12002-07-02 22:24:50 +00001226 else if (((data[in] >= 'A' && data[in] <= 'F') ||
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001227 (data[in] >= 'a' && data[in] <= 'f') ||
1228 (data[in] >= '0' && data[in] <= '9')) &&
1229 ((data[in+1] >= 'A' && data[in+1] <= 'F') ||
1230 (data[in+1] >= 'a' && data[in+1] <= 'f') ||
1231 (data[in+1] >= '0' && data[in+1] <= '9'))) {
1232 /* hexval */
1233 ch = hexval(data[in]) << 4;
1234 in++;
1235 ch |= hexval(data[in]);
1236 in++;
1237 odata[out++] = ch;
1238 }
1239 else {
1240 odata[out++] = '=';
1241 }
1242 }
1243 else if (header && data[in] == '_') {
1244 odata[out++] = ' ';
1245 in++;
1246 }
1247 else {
1248 odata[out] = data[in];
1249 in++;
1250 out++;
1251 }
1252 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001253 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Martin v. Löwis423be952008-08-13 15:53:07 +00001254 PyBuffer_Release(&pdata);
Barry Warsaw23164a52004-05-11 02:05:11 +00001255 PyMem_Free(odata);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001256 return NULL;
1257 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001258 PyBuffer_Release(&pdata);
Barry Warsaw23164a52004-05-11 02:05:11 +00001259 PyMem_Free(odata);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001260 return rv;
1261}
1262
Tim Peters934c1a12002-07-02 22:24:50 +00001263static int
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001264to_hex (unsigned char ch, unsigned char *s)
1265{
1266 unsigned int uvalue = ch;
1267
1268 s[1] = "0123456789ABCDEF"[uvalue % 16];
1269 uvalue = (uvalue / 16);
1270 s[0] = "0123456789ABCDEF"[uvalue % 16];
1271 return 0;
1272}
1273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001274PyDoc_STRVAR(doc_b2a_qp,
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001275"b2a_qp(data, quotetabs=0, istext=1, header=0) -> s; \n\
1276 Encode a string using quoted-printable encoding. \n\
1277\n\
1278On encoding, when istext is set, newlines are not encoded, and white \n\
1279space at end of lines is. When istext is not set, \\r and \\n (CR/LF) are \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001280both encoded. When quotetabs is set, space and tabs are encoded.");
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001281
1282/* XXX: This is ridiculously complicated to be backward compatible
1283 * (mostly) with the quopri module. It doesn't re-create the quopri
1284 * module bug where text ending in CRLF has the CR encoded */
Tim Peters934c1a12002-07-02 22:24:50 +00001285static PyObject*
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001286binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
1287{
Thomas Wouters7087f782006-03-01 23:10:05 +00001288 Py_ssize_t in, out;
Martin v. Löwis423be952008-08-13 15:53:07 +00001289 Py_buffer pdata;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001290 unsigned char *data, *odata;
Thomas Woutersf98db652006-03-01 21:37:32 +00001291 Py_ssize_t datalen = 0, odatalen = 0;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001292 PyObject *rv;
1293 unsigned int linelen = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001294 static char *kwlist[] = {"data", "quotetabs", "istext",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001295 "header", NULL};
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001296 int istext = 1;
1297 int quotetabs = 0;
1298 int header = 0;
1299 unsigned char ch;
1300 int crlf = 0;
1301 unsigned char *p;
1302
Martin v. Löwis15b16a32008-12-02 06:00:15 +00001303 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|iii", kwlist, &pdata,
Martin v. Löwis423be952008-08-13 15:53:07 +00001304 &quotetabs, &istext, &header))
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001305 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +00001306 data = pdata.buf;
1307 datalen = pdata.len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001308
1309 /* See if this string is using CRLF line ends */
1310 /* XXX: this function has the side effect of converting all of
1311 * the end of lines to be the same depending on this detection
1312 * here */
Walter Dörwald0925e412007-05-09 18:23:50 +00001313 p = (unsigned char *) memchr(data, '\n', datalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001314 if ((p != NULL) && (p > data) && (*(p-1) == '\r'))
1315 crlf = 1;
1316
1317 /* First, scan to see how many characters need to be encoded */
1318 in = 0;
1319 while (in < datalen) {
Tim Peters934c1a12002-07-02 22:24:50 +00001320 if ((data[in] > 126) ||
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001321 (data[in] == '=') ||
1322 (header && data[in] == '_') ||
Guido van Rossumd8faa362007-04-27 19:54:29 +00001323 ((data[in] == '.') && (linelen == 0) &&
1324 (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001325 (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
1326 ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
Tim Peters934c1a12002-07-02 22:24:50 +00001327 ((data[in] < 33) &&
1328 (data[in] != '\r') && (data[in] != '\n') &&
Guido van Rossumd8faa362007-04-27 19:54:29 +00001329 (quotetabs ||
1330 (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001331 {
1332 if ((linelen + 3) >= MAXLINESIZE) {
1333 linelen = 0;
1334 if (crlf)
1335 odatalen += 3;
1336 else
1337 odatalen += 2;
1338 }
1339 linelen += 3;
1340 odatalen += 3;
1341 in++;
1342 }
1343 else {
Tim Peters934c1a12002-07-02 22:24:50 +00001344 if (istext &&
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001345 ((data[in] == '\n') ||
1346 ((in+1 < datalen) && (data[in] == '\r') &&
1347 (data[in+1] == '\n'))))
1348 {
1349 linelen = 0;
1350 /* Protect against whitespace on end of line */
1351 if (in && ((data[in-1] == ' ') || (data[in-1] == '\t')))
1352 odatalen += 2;
1353 if (crlf)
1354 odatalen += 2;
1355 else
1356 odatalen += 1;
1357 if (data[in] == '\r')
1358 in += 2;
1359 else
1360 in++;
1361 }
1362 else {
Tim Peters934c1a12002-07-02 22:24:50 +00001363 if ((in + 1 != datalen) &&
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001364 (data[in+1] != '\n') &&
1365 (linelen + 1) >= MAXLINESIZE) {
1366 linelen = 0;
1367 if (crlf)
1368 odatalen += 3;
1369 else
1370 odatalen += 2;
1371 }
1372 linelen++;
1373 odatalen++;
1374 in++;
1375 }
1376 }
1377 }
1378
Barry Warsaw23164a52004-05-11 02:05:11 +00001379 /* We allocate the output same size as input, this is overkill.
1380 * The previous implementation used calloc() so we'll zero out the
1381 * memory here too, since PyMem_Malloc() does not guarantee that.
1382 */
1383 odata = (unsigned char *) PyMem_Malloc(odatalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001384 if (odata == NULL) {
Martin v. Löwis423be952008-08-13 15:53:07 +00001385 PyBuffer_Release(&pdata);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001386 PyErr_NoMemory();
1387 return NULL;
1388 }
Martin v. Löwisb600fe92004-07-27 15:03:53 +00001389 memset(odata, 0, odatalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001390
1391 in = out = linelen = 0;
1392 while (in < datalen) {
Tim Peters934c1a12002-07-02 22:24:50 +00001393 if ((data[in] > 126) ||
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001394 (data[in] == '=') ||
1395 (header && data[in] == '_') ||
Guido van Rossumd8faa362007-04-27 19:54:29 +00001396 ((data[in] == '.') && (linelen == 0) &&
1397 (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001398 (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
1399 ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
Tim Peters934c1a12002-07-02 22:24:50 +00001400 ((data[in] < 33) &&
1401 (data[in] != '\r') && (data[in] != '\n') &&
Guido van Rossumd8faa362007-04-27 19:54:29 +00001402 (quotetabs ||
1403 (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001404 {
1405 if ((linelen + 3 )>= MAXLINESIZE) {
1406 odata[out++] = '=';
1407 if (crlf) odata[out++] = '\r';
1408 odata[out++] = '\n';
1409 linelen = 0;
1410 }
1411 odata[out++] = '=';
1412 to_hex(data[in], &odata[out]);
1413 out += 2;
1414 in++;
1415 linelen += 3;
1416 }
1417 else {
Tim Peters934c1a12002-07-02 22:24:50 +00001418 if (istext &&
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001419 ((data[in] == '\n') ||
1420 ((in+1 < datalen) && (data[in] == '\r') &&
1421 (data[in+1] == '\n'))))
1422 {
1423 linelen = 0;
1424 /* Protect against whitespace on end of line */
1425 if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
1426 ch = odata[out-1];
1427 odata[out-1] = '=';
1428 to_hex(ch, &odata[out]);
1429 out += 2;
1430 }
Tim Peters934c1a12002-07-02 22:24:50 +00001431
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001432 if (crlf) odata[out++] = '\r';
1433 odata[out++] = '\n';
1434 if (data[in] == '\r')
1435 in += 2;
1436 else
1437 in++;
1438 }
1439 else {
Tim Peters934c1a12002-07-02 22:24:50 +00001440 if ((in + 1 != datalen) &&
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001441 (data[in+1] != '\n') &&
1442 (linelen + 1) >= MAXLINESIZE) {
1443 odata[out++] = '=';
1444 if (crlf) odata[out++] = '\r';
1445 odata[out++] = '\n';
1446 linelen = 0;
1447 }
1448 linelen++;
1449 if (header && data[in] == ' ') {
1450 odata[out++] = '_';
1451 in++;
1452 }
1453 else {
1454 odata[out++] = data[in++];
1455 }
1456 }
1457 }
1458 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001459 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Martin v. Löwis423be952008-08-13 15:53:07 +00001460 PyBuffer_Release(&pdata);
Barry Warsaw23164a52004-05-11 02:05:11 +00001461 PyMem_Free(odata);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001462 return NULL;
1463 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001464 PyBuffer_Release(&pdata);
Barry Warsaw23164a52004-05-11 02:05:11 +00001465 PyMem_Free(odata);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001466 return rv;
1467}
Barry Warsawe977c212000-08-15 06:07:13 +00001468
Jack Jansen72781191995-08-07 14:34:15 +00001469/* List of functions defined in the module */
1470
1471static struct PyMethodDef binascii_module_methods[] = {
Barry Warsawe977c212000-08-15 06:07:13 +00001472 {"a2b_uu", binascii_a2b_uu, METH_VARARGS, doc_a2b_uu},
1473 {"b2a_uu", binascii_b2a_uu, METH_VARARGS, doc_b2a_uu},
1474 {"a2b_base64", binascii_a2b_base64, METH_VARARGS, doc_a2b_base64},
1475 {"b2a_base64", binascii_b2a_base64, METH_VARARGS, doc_b2a_base64},
1476 {"a2b_hqx", binascii_a2b_hqx, METH_VARARGS, doc_a2b_hqx},
1477 {"b2a_hqx", binascii_b2a_hqx, METH_VARARGS, doc_b2a_hqx},
1478 {"b2a_hex", binascii_hexlify, METH_VARARGS, doc_hexlify},
1479 {"a2b_hex", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
1480 {"hexlify", binascii_hexlify, METH_VARARGS, doc_hexlify},
1481 {"unhexlify", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
1482 {"rlecode_hqx", binascii_rlecode_hqx, METH_VARARGS, doc_rlecode_hqx},
1483 {"rledecode_hqx", binascii_rledecode_hqx, METH_VARARGS,
1484 doc_rledecode_hqx},
1485 {"crc_hqx", binascii_crc_hqx, METH_VARARGS, doc_crc_hqx},
1486 {"crc32", binascii_crc32, METH_VARARGS, doc_crc32},
Tim Peters934c1a12002-07-02 22:24:50 +00001487 {"a2b_qp", (PyCFunction)binascii_a2b_qp, METH_VARARGS | METH_KEYWORDS,
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001488 doc_a2b_qp},
Tim Peters934c1a12002-07-02 22:24:50 +00001489 {"b2a_qp", (PyCFunction)binascii_b2a_qp, METH_VARARGS | METH_KEYWORDS,
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001490 doc_b2a_qp},
Barry Warsawe977c212000-08-15 06:07:13 +00001491 {NULL, NULL} /* sentinel */
Jack Jansen72781191995-08-07 14:34:15 +00001492};
1493
1494
Martin v. Löwis1a214512008-06-11 05:26:20 +00001495/* Initialization function for the module (*must* be called PyInit_binascii) */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001496PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
Jack Jansen72781191995-08-07 14:34:15 +00001497
Martin v. Löwis1a214512008-06-11 05:26:20 +00001498
1499static struct PyModuleDef binasciimodule = {
1500 PyModuleDef_HEAD_INIT,
1501 "binascii",
1502 doc_binascii,
1503 -1,
1504 binascii_module_methods,
1505 NULL,
1506 NULL,
1507 NULL,
1508 NULL
1509};
1510
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001511PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001512PyInit_binascii(void)
Jack Jansen72781191995-08-07 14:34:15 +00001513{
Guido van Rossumfe096d22007-08-23 18:31:24 +00001514 PyObject *m, *d;
Jack Jansen72781191995-08-07 14:34:15 +00001515
1516 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001517 m = PyModule_Create(&binasciimodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001518 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001519 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +00001520
1521 d = PyModule_GetDict(m);
Jack Jansen72781191995-08-07 14:34:15 +00001522
Guido van Rossum4581ae52007-05-22 21:56:47 +00001523 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
Jack Jansen72781191995-08-07 14:34:15 +00001524 PyDict_SetItemString(d, "Error", Error);
Guido van Rossum7dbb48a1997-10-08 15:26:07 +00001525 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
Jack Jansen72781191995-08-07 14:34:15 +00001526 PyDict_SetItemString(d, "Incomplete", Incomplete);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001527 if (PyErr_Occurred()) {
1528 Py_DECREF(m);
1529 m = NULL;
1530 }
1531 return m;
Jack Jansen72781191995-08-07 14:34:15 +00001532}