blob: 42d08de373f507cd4992c8323769e4e62c7fe587 [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:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006** 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:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020** File starts with introductory text, real data starts and ends
21** with colons.
22** Data consists of three similar parts: info, datafork, resourcefork.
23** Each part is protected (at the end) with a 16-bit crc
24** The binary data is run-length encoded, and then ascii-fied:
25** binary: 01234567 abcdefgh ijklmnop
26** ascii: 012345 67abcd efghij klmnop
27** ASCII encoding is table-driven, see the code.
28** Short binary data results in the runt ascii-byte being output with
29** the bits in the right place.
Jack Jansen72781191995-08-07 14:34:15 +000030**
31** While I was reading dozens of programs that encode or decode the formats
32** here (documentation? hihi:-) I have formulated Jansen's Observation:
33**
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034** Programs that encode binary data in ASCII are written in
35** such a style that they are as unreadable as possible. Devices used
36** include unnecessary global variables, burying important tables
37** in unrelated sourcefiles, putting functions in include files,
38** using seemingly-descriptive variable names for different purposes,
39** calls to empty subroutines and a host of others.
Jack Jansen72781191995-08-07 14:34:15 +000040**
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 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000079/* \b \t \n ^K ^L \r ^N ^O */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080/* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000081/* ^P ^Q ^R ^S ^T ^U ^V ^W */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082/* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000083/* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084/* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000085/* ! " # $ % & ' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086/* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
Jack Jansen72781191995-08-07 14:34:15 +000087/* ( ) * + , - . / */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088/* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000089/* 0 1 2 3 4 5 6 7 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090/* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000091/* 8 9 : ; < = > ? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092/* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000093/* @ A B C D E F G */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094/* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
Jack Jansen72781191995-08-07 14:34:15 +000095/* H I J K L M N O */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096/* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000097/* P Q R S T U V W */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098/*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000099/* X Y Z [ \ ] ^ _ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100/*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000101/* ` a b c d e f g */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102/*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000103/* h i j k l m n o */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104/*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000105/* p q r s t u v w */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106/*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000107/* x y z { | } ~ ^? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108/*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
109/*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
110 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
111 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
112 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
113 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
114 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
115 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
116 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
117 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
118 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
119 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
120 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
121 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
122 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
123 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
124 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000125};
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[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
132 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
133 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
134 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
135 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
136 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
137 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
138 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000139};
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] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200186/*[clinic input]
187output preset file
188module binascii
189[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800190/*[clinic end generated code: output=da39a3ee5e6b4b0d input=44c6f840ce708f0c]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200191
192/*[python input]
193
194class ascii_buffer_converter(CConverter):
195 type = 'Py_buffer'
196 converter = 'ascii_buffer_converter'
197 impl_by_reference = True
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500198 c_default = "{NULL, NULL}"
199
200 def cleanup(self):
201 name = self.name
202 return "".join(["if (", name, ".obj)\n PyBuffer_Release(&", name, ");\n"])
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200203
204[python start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800205/*[python end generated code: output=da39a3ee5e6b4b0d input=3eb7b63610da92cd]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200206
Antoine Pitrou08316762011-12-20 13:58:41 +0100207static int
208ascii_buffer_converter(PyObject *arg, Py_buffer *buf)
209{
210 if (arg == NULL) {
211 PyBuffer_Release(buf);
212 return 1;
213 }
214 if (PyUnicode_Check(arg)) {
215 if (PyUnicode_READY(arg) < 0)
216 return 0;
217 if (!PyUnicode_IS_ASCII(arg)) {
218 PyErr_SetString(PyExc_ValueError,
219 "string argument should contain only ASCII characters");
220 return 0;
221 }
222 assert(PyUnicode_KIND(arg) == PyUnicode_1BYTE_KIND);
223 buf->buf = (void *) PyUnicode_1BYTE_DATA(arg);
224 buf->len = PyUnicode_GET_LENGTH(arg);
225 buf->obj = NULL;
226 return 1;
227 }
228 if (PyObject_GetBuffer(arg, buf, PyBUF_SIMPLE) != 0) {
229 PyErr_Format(PyExc_TypeError,
230 "argument should be bytes, buffer or ASCII string, "
231 "not %R", Py_TYPE(arg));
232 return 0;
233 }
234 if (!PyBuffer_IsContiguous(buf, 'C')) {
235 PyErr_Format(PyExc_TypeError,
236 "argument should be a contiguous buffer, "
237 "not %R", Py_TYPE(arg));
238 PyBuffer_Release(buf);
239 return 0;
240 }
241 return Py_CLEANUP_SUPPORTED;
242}
243
Larry Hastingsf256c222014-01-25 21:30:37 -0800244#include "clinic/binascii.c.h"
Antoine Pitrou08316762011-12-20 13:58:41 +0100245
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200246/*[clinic input]
247binascii.a2b_uu
248
Serhiy Storchaka12785612014-01-25 11:49:49 +0200249 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200250 /
251
252Decode a line of uuencoded data.
253[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000254
255static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200256binascii_a2b_uu_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800257/*[clinic end generated code: output=5779f39b0b48459f input=7cafeaf73df63d1c]*/
Jack Jansen72781191995-08-07 14:34:15 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 unsigned char *ascii_data, *bin_data;
260 int leftbits = 0;
261 unsigned char this_ch;
262 unsigned int leftchar = 0;
263 PyObject *rv;
264 Py_ssize_t ascii_len, bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000265
Serhiy Storchaka12785612014-01-25 11:49:49 +0200266 ascii_data = data->buf;
267 ascii_len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 assert(ascii_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* First byte: binary data length (in bytes) */
272 bin_len = (*ascii_data++ - ' ') & 077;
273 ascii_len--;
Jack Jansen72781191995-08-07 14:34:15 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 /* Allocate the buffer */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200276 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
281 /* XXX is it really best to add NULs if there's no more data */
282 this_ch = (ascii_len > 0) ? *ascii_data : 0;
283 if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
284 /*
285 ** Whitespace. Assume some spaces got eaten at
286 ** end-of-line. (We check this later)
287 */
288 this_ch = 0;
289 } else {
290 /* Check the character for legality
291 ** The 64 in stead of the expected 63 is because
292 ** there are a few uuencodes out there that use
293 ** '`' as zero instead of space.
294 */
295 if ( this_ch < ' ' || this_ch > (' ' + 64)) {
296 PyErr_SetString(Error, "Illegal char");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_DECREF(rv);
298 return NULL;
299 }
300 this_ch = (this_ch - ' ') & 077;
301 }
302 /*
303 ** Shift it in on the low end, and see if there's
304 ** a byte ready for output.
305 */
306 leftchar = (leftchar << 6) | (this_ch);
307 leftbits += 6;
308 if ( leftbits >= 8 ) {
309 leftbits -= 8;
310 *bin_data++ = (leftchar >> leftbits) & 0xff;
311 leftchar &= ((1 << leftbits) - 1);
312 bin_len--;
313 }
314 }
315 /*
316 ** Finally, check that if there's anything left on the line
317 ** that it's whitespace only.
318 */
319 while( ascii_len-- > 0 ) {
320 this_ch = *ascii_data++;
321 /* Extra '`' may be written as padding in some cases */
322 if ( this_ch != ' ' && this_ch != ' '+64 &&
323 this_ch != '\n' && this_ch != '\r' ) {
324 PyErr_SetString(Error, "Trailing garbage");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 Py_DECREF(rv);
326 return NULL;
327 }
328 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000330}
331
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200332/*[clinic input]
333binascii.b2a_uu
334
335 data: Py_buffer
336 /
337
338Uuencode line of data.
339[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000340
Jack Jansen72781191995-08-07 14:34:15 +0000341static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200342binascii_b2a_uu_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800343/*[clinic end generated code: output=181021b69bb9a414 input=00fdf458ce8b465b]*/
Jack Jansen72781191995-08-07 14:34:15 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 unsigned char *ascii_data, *bin_data;
346 int leftbits = 0;
347 unsigned char this_ch;
348 unsigned int leftchar = 0;
349 PyObject *rv;
350 Py_ssize_t bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000351
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200352 bin_data = data->buf;
353 bin_len = data->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if ( bin_len > 45 ) {
355 /* The 45 is a limit that appears in all uuencode's */
356 PyErr_SetString(Error, "At most 45 bytes at once");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return NULL;
358 }
Jack Jansen72781191995-08-07 14:34:15 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 /* We're lazy and allocate to much (fixed up later) */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200361 if ( (rv=PyBytes_FromStringAndSize(NULL, 2 + (bin_len+2)/3*4)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 /* Store the length */
366 *ascii_data++ = ' ' + (bin_len & 077);
Tim Peters934c1a12002-07-02 22:24:50 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
369 /* Shift the data (or padding) into our buffer */
370 if ( bin_len > 0 ) /* Data */
371 leftchar = (leftchar << 8) | *bin_data;
372 else /* Padding */
373 leftchar <<= 8;
374 leftbits += 8;
Jack Jansen72781191995-08-07 14:34:15 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 /* See if there are 6-bit groups ready */
377 while ( leftbits >= 6 ) {
378 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
379 leftbits -= 6;
380 *ascii_data++ = this_ch + ' ';
381 }
382 }
383 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (_PyBytes_Resize(&rv,
386 (ascii_data -
387 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200388 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000391}
392
Guido van Rossum2db4f471999-10-19 19:05:14 +0000393
394static int
Thomas Woutersf98db652006-03-01 21:37:32 +0000395binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num)
Guido van Rossum2db4f471999-10-19 19:05:14 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* Finds & returns the (num+1)th
398 ** valid character for base64, or -1 if none.
399 */
Guido van Rossum2db4f471999-10-19 19:05:14 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 int ret = -1;
402 unsigned char c, b64val;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 while ((slen > 0) && (ret == -1)) {
405 c = *s;
406 b64val = table_a2b_base64[c & 0x7f];
407 if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
408 if (num == 0)
409 ret = *s;
410 num--;
411 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 s++;
414 slen--;
415 }
416 return ret;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000417}
418
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200419/*[clinic input]
420binascii.a2b_base64
421
Serhiy Storchaka12785612014-01-25 11:49:49 +0200422 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200423 /
424
425Decode a line of base64 data.
426[clinic start generated code]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000427
428static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200429binascii_a2b_base64_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800430/*[clinic end generated code: output=3e351b702bed56d2 input=5872acf6e1cac243]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 unsigned char *ascii_data, *bin_data;
433 int leftbits = 0;
434 unsigned char this_ch;
435 unsigned int leftchar = 0;
436 PyObject *rv;
437 Py_ssize_t ascii_len, bin_len;
438 int quad_pos = 0;
Tim Peters934c1a12002-07-02 22:24:50 +0000439
Serhiy Storchaka12785612014-01-25 11:49:49 +0200440 ascii_data = data->buf;
441 ascii_len = data->len;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 assert(ascii_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000444
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200445 if (ascii_len > PY_SSIZE_T_MAX - 3)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 /* Allocate the buffer */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200451 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
454 bin_len = 0;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 for( ; ascii_len > 0; ascii_len--, ascii_data++) {
457 this_ch = *ascii_data;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (this_ch > 0x7f ||
460 this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
461 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 /* Check for pad sequences and ignore
464 ** the invalid ones.
465 */
466 if (this_ch == BASE64_PAD) {
467 if ( (quad_pos < 2) ||
468 ((quad_pos == 2) &&
469 (binascii_find_valid(ascii_data, ascii_len, 1)
470 != BASE64_PAD)) )
471 {
472 continue;
473 }
474 else {
475 /* A pad sequence means no more input.
476 ** We've already interpreted the data
477 ** from the quad at this point.
478 */
479 leftbits = 0;
480 break;
481 }
482 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 this_ch = table_a2b_base64[*ascii_data];
485 if ( this_ch == (unsigned char) -1 )
486 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 /*
489 ** Shift it in on the low end, and see if there's
490 ** a byte ready for output.
491 */
492 quad_pos = (quad_pos + 1) & 0x03;
493 leftchar = (leftchar << 6) | (this_ch);
494 leftbits += 6;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if ( leftbits >= 8 ) {
497 leftbits -= 8;
498 *bin_data++ = (leftchar >> leftbits) & 0xff;
499 bin_len++;
500 leftchar &= ((1 << leftbits) - 1);
501 }
502 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (leftbits != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 PyErr_SetString(Error, "Incorrect padding");
506 Py_DECREF(rv);
507 return NULL;
508 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* And set string size correctly. If the result string is empty
511 ** (because the input was all invalid) return the shared empty
512 ** string instead; _PyBytes_Resize() won't do this for us.
513 */
514 if (bin_len > 0) {
515 if (_PyBytes_Resize(&rv, bin_len) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200516 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 }
519 else {
520 Py_DECREF(rv);
521 rv = PyBytes_FromStringAndSize("", 0);
522 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 return rv;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000524}
525
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200526
527/*[clinic input]
528binascii.b2a_base64
529
530 data: Py_buffer
531 /
532
533Base64-code line of data.
534[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000535
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000536static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200537binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800538/*[clinic end generated code: output=3cd61fbee2913285 input=14ec4e47371174a9]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 unsigned char *ascii_data, *bin_data;
541 int leftbits = 0;
542 unsigned char this_ch;
543 unsigned int leftchar = 0;
544 PyObject *rv;
545 Py_ssize_t bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000546
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200547 bin_data = data->buf;
548 bin_len = data->len;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 assert(bin_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if ( bin_len > BASE64_MAXBIN ) {
553 PyErr_SetString(Error, "Too much data for base64 line");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return NULL;
555 }
Tim Peters934c1a12002-07-02 22:24:50 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* We're lazy and allocate too much (fixed up later).
558 "+3" leaves room for up to two pad characters and a trailing
559 newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200560 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
565 /* Shift the data into our buffer */
566 leftchar = (leftchar << 8) | *bin_data;
567 leftbits += 8;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 /* See if there are 6-bit groups ready */
570 while ( leftbits >= 6 ) {
571 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
572 leftbits -= 6;
573 *ascii_data++ = table_b2a_base64[this_ch];
574 }
575 }
576 if ( leftbits == 2 ) {
577 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
578 *ascii_data++ = BASE64_PAD;
579 *ascii_data++ = BASE64_PAD;
580 } else if ( leftbits == 4 ) {
581 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
582 *ascii_data++ = BASE64_PAD;
583 }
584 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (_PyBytes_Resize(&rv,
587 (ascii_data -
588 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200589 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return rv;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000592}
593
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200594/*[clinic input]
595binascii.a2b_hqx
596
Serhiy Storchaka12785612014-01-25 11:49:49 +0200597 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200598 /
599
600Decode .hqx coding.
601[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000602
603static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200604binascii_a2b_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800605/*[clinic end generated code: output=60bcdbbd28b105cd input=0d914c680e0eed55]*/
Jack Jansen72781191995-08-07 14:34:15 +0000606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 unsigned char *ascii_data, *bin_data;
608 int leftbits = 0;
609 unsigned char this_ch;
610 unsigned int leftchar = 0;
611 PyObject *rv;
612 Py_ssize_t len;
613 int done = 0;
Tim Peters934c1a12002-07-02 22:24:50 +0000614
Serhiy Storchaka12785612014-01-25 11:49:49 +0200615 ascii_data = data->buf;
616 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000619
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200620 if (len > PY_SSIZE_T_MAX - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Allocate a string that is too big (fixed later)
624 Add two to the initial length to prevent interning which
625 would preclude subsequent resizing. */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200626 if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 for( ; len > 0 ; len--, ascii_data++ ) {
631 /* Get the byte and look it up */
632 this_ch = table_a2b_hqx[*ascii_data];
633 if ( this_ch == SKIP )
634 continue;
635 if ( this_ch == FAIL ) {
636 PyErr_SetString(Error, "Illegal char");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 Py_DECREF(rv);
638 return NULL;
639 }
640 if ( this_ch == DONE ) {
641 /* The terminating colon */
642 done = 1;
643 break;
644 }
Jack Jansen72781191995-08-07 14:34:15 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 /* Shift it into the buffer and see if any bytes are ready */
647 leftchar = (leftchar << 6) | (this_ch);
648 leftbits += 6;
649 if ( leftbits >= 8 ) {
650 leftbits -= 8;
651 *bin_data++ = (leftchar >> leftbits) & 0xff;
652 leftchar &= ((1 << leftbits) - 1);
653 }
654 }
Tim Peters934c1a12002-07-02 22:24:50 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 if ( leftbits && !done ) {
657 PyErr_SetString(Incomplete,
658 "String has incomplete number of bytes");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 Py_DECREF(rv);
660 return NULL;
661 }
662 if (_PyBytes_Resize(&rv,
663 (bin_data -
664 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200665 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 }
667 if (rv) {
668 PyObject *rrv = Py_BuildValue("Oi", rv, done);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 Py_DECREF(rv);
670 return rrv;
671 }
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000674}
675
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200676
677/*[clinic input]
678binascii.rlecode_hqx
679
680 data: Py_buffer
681 /
682
683Binhex RLE-code binary data.
684[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000685
686static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200687binascii_rlecode_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800688/*[clinic end generated code: output=0905da344dbf0648 input=e1f1712447a82b09]*/
Jack Jansen72781191995-08-07 14:34:15 +0000689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 unsigned char *in_data, *out_data;
691 PyObject *rv;
692 unsigned char ch;
693 Py_ssize_t in, inend, len;
Tim Peters934c1a12002-07-02 22:24:50 +0000694
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200695 in_data = data->buf;
696 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000699
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200700 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 /* Worst case: output is twice as big as input (fixed later) */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200704 if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 out_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 for( in=0; in<len; in++) {
709 ch = in_data[in];
710 if ( ch == RUNCHAR ) {
711 /* RUNCHAR. Escape it. */
712 *out_data++ = RUNCHAR;
713 *out_data++ = 0;
714 } else {
715 /* Check how many following are the same */
716 for(inend=in+1;
717 inend<len && in_data[inend] == ch &&
718 inend < in+255;
719 inend++) ;
720 if ( inend - in > 3 ) {
721 /* More than 3 in a row. Output RLE. */
722 *out_data++ = ch;
723 *out_data++ = RUNCHAR;
Antoine Pitrou40455752010-08-15 18:51:10 +0000724 *out_data++ = (unsigned char) (inend-in);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 in = inend-1;
726 } else {
727 /* Less than 3. Output the byte itself */
728 *out_data++ = ch;
729 }
730 }
731 }
732 if (_PyBytes_Resize(&rv,
733 (out_data -
734 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200735 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000738}
739
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200740
741/*[clinic input]
742binascii.b2a_hqx
743
744 data: Py_buffer
745 /
746
747Encode .hqx data.
748[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000749
Jack Jansen72781191995-08-07 14:34:15 +0000750static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200751binascii_b2a_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800752/*[clinic end generated code: output=5a987810d5e3cdbb input=9596ebe019fe12ba]*/
Jack Jansen72781191995-08-07 14:34:15 +0000753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 unsigned char *ascii_data, *bin_data;
755 int leftbits = 0;
756 unsigned char this_ch;
757 unsigned int leftchar = 0;
758 PyObject *rv;
759 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000760
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200761 bin_data = data->buf;
762 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000765
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200766 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 /* Allocate a buffer that is at least large enough */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200770 if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 for( ; len > 0 ; len--, bin_data++ ) {
775 /* Shift into our buffer, and output any 6bits ready */
776 leftchar = (leftchar << 8) | *bin_data;
777 leftbits += 8;
778 while ( leftbits >= 6 ) {
779 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
780 leftbits -= 6;
781 *ascii_data++ = table_b2a_hqx[this_ch];
782 }
783 }
784 /* Output a possible runt byte */
785 if ( leftbits ) {
786 leftchar <<= (6-leftbits);
787 *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
788 }
789 if (_PyBytes_Resize(&rv,
790 (ascii_data -
791 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200792 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000795}
796
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200797
798/*[clinic input]
799binascii.rledecode_hqx
800
801 data: Py_buffer
802 /
803
804Decode hexbin RLE-coded string.
805[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000806
Jack Jansen72781191995-08-07 14:34:15 +0000807static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200808binascii_rledecode_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800809/*[clinic end generated code: output=f7afd89b789946ab input=54cdd49fc014402c]*/
Jack Jansen72781191995-08-07 14:34:15 +0000810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 unsigned char *in_data, *out_data;
812 unsigned char in_byte, in_repeat;
813 PyObject *rv;
814 Py_ssize_t in_len, out_len, out_len_left;
Jack Jansen72781191995-08-07 14:34:15 +0000815
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200816 in_data = data->buf;
817 in_len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 assert(in_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Empty string is a special case */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200822 if ( in_len == 0 )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 return PyBytes_FromStringAndSize("", 0);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200824 else if (in_len > PY_SSIZE_T_MAX / 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 return PyErr_NoMemory();
Jack Jansen72781191995-08-07 14:34:15 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 /* Allocate a buffer of reasonable size. Resized when needed */
828 out_len = in_len*2;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200829 if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 out_len_left = out_len;
832 out_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /*
835 ** We need two macros here to get/put bytes and handle
836 ** end-of-buffer for input and output strings.
837 */
Jack Jansen72781191995-08-07 14:34:15 +0000838#define INBYTE(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 do { \
840 if ( --in_len < 0 ) { \
841 PyErr_SetString(Incomplete, ""); \
842 Py_DECREF(rv); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 return NULL; \
844 } \
845 b = *in_data++; \
846 } while(0)
Tim Peters934c1a12002-07-02 22:24:50 +0000847
Jack Jansen72781191995-08-07 14:34:15 +0000848#define OUTBYTE(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 do { \
850 if ( --out_len_left < 0 ) { \
851 if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
852 if (_PyBytes_Resize(&rv, 2*out_len) < 0) \
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200853 { Py_XDECREF(rv); return NULL; } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
855 + out_len; \
856 out_len_left = out_len-1; \
857 out_len = out_len * 2; \
858 } \
859 *out_data++ = b; \
860 } while(0)
Jack Jansen72781191995-08-07 14:34:15 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 /*
863 ** Handle first byte separately (since we have to get angry
864 ** in case of an orphaned RLE code).
865 */
866 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (in_byte == RUNCHAR) {
869 INBYTE(in_repeat);
870 if (in_repeat != 0) {
871 /* Note Error, not Incomplete (which is at the end
872 ** of the string only). This is a programmer error.
873 */
874 PyErr_SetString(Error, "Orphaned RLE code at start");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 Py_DECREF(rv);
876 return NULL;
877 }
878 OUTBYTE(RUNCHAR);
879 } else {
880 OUTBYTE(in_byte);
881 }
Tim Peters934c1a12002-07-02 22:24:50 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 while( in_len > 0 ) {
884 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 if (in_byte == RUNCHAR) {
887 INBYTE(in_repeat);
888 if ( in_repeat == 0 ) {
889 /* Just an escaped RUNCHAR value */
890 OUTBYTE(RUNCHAR);
891 } else {
892 /* Pick up value and output a sequence of it */
893 in_byte = out_data[-1];
894 while ( --in_repeat > 0 )
895 OUTBYTE(in_byte);
896 }
897 } else {
898 /* Normal byte */
899 OUTBYTE(in_byte);
900 }
901 }
902 if (_PyBytes_Resize(&rv,
903 (out_data -
904 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200905 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000908}
909
Jack Jansen72781191995-08-07 14:34:15 +0000910
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200911/*[clinic input]
912binascii.crc_hqx -> int
913
914 data: Py_buffer
915 crc: int
916 /
917
918Compute hqx CRC incrementally.
919[clinic start generated code]*/
920
921static int
922binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, int crc)
Larry Hastings581ee362014-01-28 05:00:08 -0800923/*[clinic end generated code: output=634dac18dfa863d7 input=68060931b2f51c8a]*/
Jack Jansen72781191995-08-07 14:34:15 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 unsigned char *bin_data;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200926 unsigned int ucrc = (unsigned int)crc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000928
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200929 bin_data = data->buf;
930 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 while(len-- > 0) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200933 ucrc=((ucrc<<8)&0xff00)^crctab_hqx[((ucrc>>8)&0xff)^*bin_data++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
Jack Jansen72781191995-08-07 14:34:15 +0000935
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200936 return (int)ucrc;
Jack Jansen72781191995-08-07 14:34:15 +0000937}
938
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200939#ifndef USE_ZLIB_CRC32
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000940/* Crc - 32 BIT ANSI X3.66 CRC checksum files
941 Also known as: ISO 3307
942**********************************************************************|
943* *|
944* Demonstration program to compute the 32-bit CRC used as the frame *|
945* check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *|
946* and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *|
947* protocol). The 32-bit FCS was added via the Federal Register, *|
948* 1 June 1982, p.23798. I presume but don't know for certain that *|
949* this polynomial is or will be included in CCITT V.41, which *|
950* defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *|
951* PUB 78 says that the 32-bit FCS reduces otherwise undetected *|
952* errors by a factor of 10^-5 over 16-bit FCS. *|
953* *|
954**********************************************************************|
955
956 Copyright (C) 1986 Gary S. Brown. You may use this program, or
957 code or tables extracted from it, as desired without restriction.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000958
Tim Peters934c1a12002-07-02 22:24:50 +0000959 First, the polynomial itself and its table of feedback terms. The
960 polynomial is
961 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
962 Note that we take it "backwards" and put the highest-order term in
963 the lowest-order bit. The X^32 term is "implied"; the LSB is the
964 X^31 term, etc. The X^0 term (usually shown as "+1") results in
965 the MSB being 1.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000966
Tim Peters934c1a12002-07-02 22:24:50 +0000967 Note that the usual hardware shift register implementation, which
968 is what we're using (we're merely optimizing it by doing eight-bit
969 chunks at a time) shifts bits into the lowest-order term. In our
970 implementation, that means shifting towards the right. Why do we
971 do it this way? Because the calculated CRC must be transmitted in
972 order from highest-order term to lowest-order term. UARTs transmit
973 characters in order from LSB to MSB. By storing the CRC this way,
974 we hand it to the UART in the order low-byte to high-byte; the UART
975 sends each low-bit to hight-bit; and the result is transmission bit
976 by bit from highest- to lowest-order term without requiring any bit
977 shuffling on our part. Reception works similarly.
978
979 The feedback terms table consists of 256, 32-bit entries. Notes:
980
981 1. The table can be generated at runtime if desired; code to do so
982 is shown later. It might not be obvious, but the feedback
983 terms simply represent the results of eight shift/xor opera-
984 tions for all combinations of data and CRC register values.
985
986 2. The CRC accumulation logic is the same for all CRC polynomials,
987 be they sixteen or thirty-two bits wide. You simply choose the
988 appropriate table. Alternatively, because the table can be
989 generated at runtime, you can start by generating the table for
990 the polynomial in question and use exactly the same "updcrc",
991 if your application needn't simultaneously handle two CRC
992 polynomials. (Note, however, that XMODEM is strange.)
993
994 3. For 16-bit CRCs, the table entries need be only 16 bits wide;
995 of course, 32-bit entries work OK if the high 16 bits are zero.
996
997 4. The values must be right-shifted by eight bits by the "updcrc"
998 logic; the shift must be unsigned (bring in zeroes). On some
999 hardware you could probably optimize the shift in assembler by
1000 using byte-swap instructions.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001001********************************************************************/
1002
Gregory P. Smith3c0e4d22008-03-25 07:51:12 +00001003static unsigned int crc_32_tab[256] = {
10040x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
10050x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
10060xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
10070x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
10080x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
10090x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
10100xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
10110xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
10120x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
10130x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
10140xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
10150xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
10160x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
10170x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
10180x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
10190xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
10200x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
10210x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
10220x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
10230xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
10240x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
10250x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
10260xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
10270xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
10280x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
10290x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
10300x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
10310x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
10320xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
10330x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
10340x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
10350x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
10360xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
10370xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
10380x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
10390x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
10400xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
10410xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
10420x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
10430x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
10440x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
10450xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
10460x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
10470x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
10480x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
10490xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
10500x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
10510x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
10520xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
10530xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
10540x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
10550x2d02ef8dU
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001056};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001057#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001058
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001059/*[clinic input]
1060binascii.crc32 -> unsigned_int
1061
1062 data: Py_buffer
1063 crc: unsigned_int(bitwise=True) = 0
1064 /
1065
1066Compute CRC-32 incrementally.
1067[clinic start generated code]*/
1068
1069static unsigned int
1070binascii_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc)
Larry Hastings581ee362014-01-28 05:00:08 -08001071/*[clinic end generated code: output=620a961643393c4f input=bbe340bc99d25aa8]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001072
1073#ifdef USE_ZLIB_CRC32
1074/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
1075{
1076 Byte *buf;
1077 Py_ssize_t len;
1078 int signed_val;
1079
1080 buf = (Byte*)data->buf;
1081 len = data->len;
1082 signed_val = crc32(crc, buf, len);
1083 return (unsigned int)signed_val & 0xffffffffU;
1084}
1085#else /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001086{ /* By Jim Ahlstrom; All rights transferred to CNRI */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 Py_ssize_t len;
1089 unsigned int result;
Tim Peters934c1a12002-07-02 22:24:50 +00001090
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001091 bin_data = data->buf;
1092 len = data->len;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 crc = ~ crc;
1095 while (len-- > 0) {
1096 crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
1097 /* Note: (crc >> 8) MUST zero fill on left */
1098 }
Tim Petersa98011c2002-07-02 20:20:08 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 result = (crc ^ 0xFFFFFFFF);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001101 return result & 0xffffffff;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001102}
Christian Heimes1dc54002008-03-24 02:19:29 +00001103#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001104
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001105/*[clinic input]
1106binascii.b2a_hex
1107
1108 data: Py_buffer
1109 /
1110
1111Hexadecimal representation of binary data.
1112
1113The return value is a bytes object. This function is also
1114available as "hexlify()".
1115[clinic start generated code]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001116
1117static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001118binascii_b2a_hex_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -08001119/*[clinic end generated code: output=179318922c2f8fda input=96423cfa299ff3b1]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 char* argbuf;
1122 Py_ssize_t arglen;
1123 PyObject *retval;
1124 char* retbuf;
1125 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001126
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001127 argbuf = data->buf;
1128 arglen = data->len;
Barry Warsawe977c212000-08-15 06:07:13 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 assert(arglen >= 0);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001131 if (arglen > PY_SSIZE_T_MAX / 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 retval = PyBytes_FromStringAndSize(NULL, arglen*2);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001135 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 /* make hex version of string, taken from shamodule.c */
1140 for (i=j=0; i < arglen; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +02001141 unsigned char c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 c = (argbuf[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +02001143 retbuf[j++] = Py_hexdigits[c];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 c = argbuf[i] & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +02001145 retbuf[j++] = Py_hexdigits[c];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 return retval;
Barry Warsawe977c212000-08-15 06:07:13 +00001148}
1149
Barry Warsawe977c212000-08-15 06:07:13 +00001150
1151static int
Tim Peters934c1a12002-07-02 22:24:50 +00001152to_int(int c)
Barry Warsawe977c212000-08-15 06:07:13 +00001153{
Antoine Pitrou4de74572013-02-09 23:11:27 +01001154 if (Py_ISDIGIT(c))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 return c - '0';
1156 else {
Antoine Pitroued8ba142011-10-04 13:50:21 +02001157 if (Py_ISUPPER(c))
1158 c = Py_TOLOWER(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (c >= 'a' && c <= 'f')
1160 return c - 'a' + 10;
1161 }
1162 return -1;
Barry Warsawe977c212000-08-15 06:07:13 +00001163}
1164
1165
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001166/*[clinic input]
1167binascii.a2b_hex
1168
1169 hexstr: ascii_buffer
1170 /
1171
1172Binary data of hexadecimal representation.
1173
1174hexstr must contain an even number of hex digits (upper or lower case).
1175This function is also available as "unhexlify()".
1176[clinic start generated code]*/
1177
Barry Warsawe977c212000-08-15 06:07:13 +00001178static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001179binascii_a2b_hex_impl(PyModuleDef *module, Py_buffer *hexstr)
Larry Hastings581ee362014-01-28 05:00:08 -08001180/*[clinic end generated code: output=d61da452b5c6d290 input=9e1e7f2f94db24fd]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 char* argbuf;
1183 Py_ssize_t arglen;
1184 PyObject *retval;
1185 char* retbuf;
1186 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001187
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001188 argbuf = hexstr->buf;
1189 arglen = hexstr->len;
Barry Warsawe977c212000-08-15 06:07:13 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 assert(arglen >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* XXX What should we do about strings with an odd length? Should
1194 * we add an implicit leading zero, or a trailing zero? For now,
1195 * raise an exception.
1196 */
1197 if (arglen % 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 PyErr_SetString(Error, "Odd-length string");
1199 return NULL;
1200 }
Barry Warsawe977c212000-08-15 06:07:13 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001203 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 for (i=j=0; i < arglen; i += 2) {
1208 int top = to_int(Py_CHARMASK(argbuf[i]));
1209 int bot = to_int(Py_CHARMASK(argbuf[i+1]));
1210 if (top == -1 || bot == -1) {
1211 PyErr_SetString(Error,
1212 "Non-hexadecimal digit found");
1213 goto finally;
1214 }
1215 retbuf[j++] = (top << 4) + bot;
1216 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 return retval;
Barry Warsawe977c212000-08-15 06:07:13 +00001218
1219 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 Py_DECREF(retval);
1221 return NULL;
Barry Warsawe977c212000-08-15 06:07:13 +00001222}
1223
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001224static int table_hex[128] = {
1225 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1226 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1227 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1228 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1229 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1230 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1231 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1232 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1233};
1234
1235#define hexval(c) table_hex[(unsigned int)(c)]
1236
1237#define MAXLINESIZE 76
1238
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001239
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001240/*[clinic input]
1241binascii.a2b_qp
1242
Serhiy Storchaka12785612014-01-25 11:49:49 +02001243 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001244 header: int(c_default="0") = False
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001245
1246Decode a string of qp-encoded data.
1247[clinic start generated code]*/
1248
1249static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +02001250binascii_a2b_qp_impl(PyModuleDef *module, Py_buffer *data, int header)
Larry Hastings581ee362014-01-28 05:00:08 -08001251/*[clinic end generated code: output=a44ef88270352114 input=5187a0d3d8e54f3b]*/
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Py_ssize_t in, out;
1254 char ch;
Serhiy Storchaka12785612014-01-25 11:49:49 +02001255 unsigned char *ascii_data, *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 Py_ssize_t datalen = 0;
1257 PyObject *rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001258
Serhiy Storchaka12785612014-01-25 11:49:49 +02001259 ascii_data = data->buf;
1260 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 /* We allocate the output same size as input, this is overkill.
1263 * The previous implementation used calloc() so we'll zero out the
1264 * memory here too, since PyMem_Malloc() does not guarantee that.
1265 */
1266 odata = (unsigned char *) PyMem_Malloc(datalen);
1267 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 PyErr_NoMemory();
1269 return NULL;
1270 }
1271 memset(odata, 0, datalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 in = out = 0;
1274 while (in < datalen) {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001275 if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 in++;
1277 if (in >= datalen) break;
1278 /* Soft line breaks */
Serhiy Storchaka12785612014-01-25 11:49:49 +02001279 if ((ascii_data[in] == '\n') || (ascii_data[in] == '\r')) {
1280 if (ascii_data[in] != '\n') {
1281 while (in < datalen && ascii_data[in] != '\n') in++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 }
1283 if (in < datalen) in++;
1284 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001285 else if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 /* broken case from broken python qp */
1287 odata[out++] = '=';
1288 in++;
1289 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001290 else if (((ascii_data[in] >= 'A' && ascii_data[in] <= 'F') ||
1291 (ascii_data[in] >= 'a' && ascii_data[in] <= 'f') ||
1292 (ascii_data[in] >= '0' && ascii_data[in] <= '9')) &&
1293 ((ascii_data[in+1] >= 'A' && ascii_data[in+1] <= 'F') ||
1294 (ascii_data[in+1] >= 'a' && ascii_data[in+1] <= 'f') ||
1295 (ascii_data[in+1] >= '0' && ascii_data[in+1] <= '9'))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 /* hexval */
Serhiy Storchaka12785612014-01-25 11:49:49 +02001297 ch = hexval(ascii_data[in]) << 4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 in++;
Serhiy Storchaka12785612014-01-25 11:49:49 +02001299 ch |= hexval(ascii_data[in]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 in++;
1301 odata[out++] = ch;
1302 }
1303 else {
1304 odata[out++] = '=';
1305 }
1306 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001307 else if (header && ascii_data[in] == '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 odata[out++] = ' ';
1309 in++;
1310 }
1311 else {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001312 odata[out] = ascii_data[in];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 in++;
1314 out++;
1315 }
1316 }
1317 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 PyMem_Free(odata);
1319 return NULL;
1320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 PyMem_Free(odata);
1322 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001323}
1324
Tim Peters934c1a12002-07-02 22:24:50 +00001325static int
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001326to_hex (unsigned char ch, unsigned char *s)
1327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 unsigned int uvalue = ch;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 s[1] = "0123456789ABCDEF"[uvalue % 16];
1331 uvalue = (uvalue / 16);
1332 s[0] = "0123456789ABCDEF"[uvalue % 16];
1333 return 0;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001334}
1335
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001336/* XXX: This is ridiculously complicated to be backward compatible
1337 * (mostly) with the quopri module. It doesn't re-create the quopri
1338 * module bug where text ending in CRLF has the CR encoded */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001339
1340/*[clinic input]
1341binascii.b2a_qp
1342
1343 data: Py_buffer
1344 quotetabs: int(c_default="0") = False
1345 istext: int(c_default="1") = True
1346 header: int(c_default="0") = False
1347
1348Encode a string using quoted-printable encoding.
1349
1350On encoding, when istext is set, newlines are not encoded, and white
1351space at end of lines is. When istext is not set, \r and \n (CR/LF)
1352are both encoded. When quotetabs is set, space and tabs are encoded.
1353[clinic start generated code]*/
1354
1355static PyObject *
1356binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int istext, int header)
Larry Hastings581ee362014-01-28 05:00:08 -08001357/*[clinic end generated code: output=ff2991ba640fff3e input=7f2a9aaa008e92b2]*/
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 Py_ssize_t in, out;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001360 unsigned char *databuf, *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 Py_ssize_t datalen = 0, odatalen = 0;
1362 PyObject *rv;
1363 unsigned int linelen = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 unsigned char ch;
1365 int crlf = 0;
1366 unsigned char *p;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001367
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001368 databuf = data->buf;
1369 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 /* See if this string is using CRLF line ends */
1372 /* XXX: this function has the side effect of converting all of
1373 * the end of lines to be the same depending on this detection
1374 * here */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001375 p = (unsigned char *) memchr(databuf, '\n', datalen);
1376 if ((p != NULL) && (p > databuf) && (*(p-1) == '\r'))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 crlf = 1;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 /* First, scan to see how many characters need to be encoded */
1380 in = 0;
1381 while (in < datalen) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001382 if ((databuf[in] > 126) ||
1383 (databuf[in] == '=') ||
1384 (header && databuf[in] == '_') ||
1385 ((databuf[in] == '.') && (linelen == 0) &&
1386 (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1387 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1388 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1389 ((databuf[in] < 33) &&
1390 (databuf[in] != '\r') && (databuf[in] != '\n') &&
1391 (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' ')))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 {
1393 if ((linelen + 3) >= MAXLINESIZE) {
1394 linelen = 0;
1395 if (crlf)
1396 odatalen += 3;
1397 else
1398 odatalen += 2;
1399 }
1400 linelen += 3;
1401 odatalen += 3;
1402 in++;
1403 }
1404 else {
1405 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001406 ((databuf[in] == '\n') ||
1407 ((in+1 < datalen) && (databuf[in] == '\r') &&
1408 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 {
1410 linelen = 0;
1411 /* Protect against whitespace on end of line */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001412 if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 odatalen += 2;
1414 if (crlf)
1415 odatalen += 2;
1416 else
1417 odatalen += 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001418 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 in += 2;
1420 else
1421 in++;
1422 }
1423 else {
1424 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001425 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 (linelen + 1) >= MAXLINESIZE) {
1427 linelen = 0;
1428 if (crlf)
1429 odatalen += 3;
1430 else
1431 odatalen += 2;
1432 }
1433 linelen++;
1434 odatalen++;
1435 in++;
1436 }
1437 }
1438 }
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 /* We allocate the output same size as input, this is overkill.
1441 * The previous implementation used calloc() so we'll zero out the
1442 * memory here too, since PyMem_Malloc() does not guarantee that.
1443 */
1444 odata = (unsigned char *) PyMem_Malloc(odatalen);
1445 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 PyErr_NoMemory();
1447 return NULL;
1448 }
1449 memset(odata, 0, odatalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 in = out = linelen = 0;
1452 while (in < datalen) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001453 if ((databuf[in] > 126) ||
1454 (databuf[in] == '=') ||
1455 (header && databuf[in] == '_') ||
1456 ((databuf[in] == '.') && (linelen == 0) &&
1457 (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1458 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1459 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1460 ((databuf[in] < 33) &&
1461 (databuf[in] != '\r') && (databuf[in] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 (quotetabs ||
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001463 (!quotetabs && ((databuf[in] != '\t') && (databuf[in] != ' '))))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 {
1465 if ((linelen + 3 )>= MAXLINESIZE) {
1466 odata[out++] = '=';
1467 if (crlf) odata[out++] = '\r';
1468 odata[out++] = '\n';
1469 linelen = 0;
1470 }
1471 odata[out++] = '=';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001472 to_hex(databuf[in], &odata[out]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 out += 2;
1474 in++;
1475 linelen += 3;
1476 }
1477 else {
1478 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001479 ((databuf[in] == '\n') ||
1480 ((in+1 < datalen) && (databuf[in] == '\r') &&
1481 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 {
1483 linelen = 0;
1484 /* Protect against whitespace on end of line */
1485 if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
1486 ch = odata[out-1];
1487 odata[out-1] = '=';
1488 to_hex(ch, &odata[out]);
1489 out += 2;
1490 }
Tim Peters934c1a12002-07-02 22:24:50 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (crlf) odata[out++] = '\r';
1493 odata[out++] = '\n';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001494 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 in += 2;
1496 else
1497 in++;
1498 }
1499 else {
1500 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001501 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 (linelen + 1) >= MAXLINESIZE) {
1503 odata[out++] = '=';
1504 if (crlf) odata[out++] = '\r';
1505 odata[out++] = '\n';
1506 linelen = 0;
1507 }
1508 linelen++;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001509 if (header && databuf[in] == ' ') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 odata[out++] = '_';
1511 in++;
1512 }
1513 else {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001514 odata[out++] = databuf[in++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 }
1516 }
1517 }
1518 }
1519 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 PyMem_Free(odata);
1521 return NULL;
1522 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 PyMem_Free(odata);
1524 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001525}
Barry Warsawe977c212000-08-15 06:07:13 +00001526
Jack Jansen72781191995-08-07 14:34:15 +00001527/* List of functions defined in the module */
1528
1529static struct PyMethodDef binascii_module_methods[] = {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001530 BINASCII_A2B_UU_METHODDEF
1531 BINASCII_B2A_UU_METHODDEF
1532 BINASCII_A2B_BASE64_METHODDEF
1533 BINASCII_B2A_BASE64_METHODDEF
1534 BINASCII_A2B_HQX_METHODDEF
1535 BINASCII_B2A_HQX_METHODDEF
1536 BINASCII_A2B_HEX_METHODDEF
1537 BINASCII_B2A_HEX_METHODDEF
1538 {"unhexlify", (PyCFunction)binascii_a2b_hex, METH_VARARGS,
1539 binascii_a2b_hex__doc__},
1540 {"hexlify", (PyCFunction)binascii_b2a_hex, METH_VARARGS,
1541 binascii_b2a_hex__doc__},
1542 BINASCII_RLECODE_HQX_METHODDEF
1543 BINASCII_RLEDECODE_HQX_METHODDEF
1544 BINASCII_CRC_HQX_METHODDEF
1545 BINASCII_CRC32_METHODDEF
1546 BINASCII_A2B_QP_METHODDEF
1547 BINASCII_B2A_QP_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 {NULL, NULL} /* sentinel */
Jack Jansen72781191995-08-07 14:34:15 +00001549};
1550
1551
Martin v. Löwis1a214512008-06-11 05:26:20 +00001552/* Initialization function for the module (*must* be called PyInit_binascii) */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001553PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
Jack Jansen72781191995-08-07 14:34:15 +00001554
Martin v. Löwis1a214512008-06-11 05:26:20 +00001555
1556static struct PyModuleDef binasciimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 PyModuleDef_HEAD_INIT,
1558 "binascii",
1559 doc_binascii,
1560 -1,
1561 binascii_module_methods,
1562 NULL,
1563 NULL,
1564 NULL,
1565 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001566};
1567
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001568PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001569PyInit_binascii(void)
Jack Jansen72781191995-08-07 14:34:15 +00001570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 PyObject *m, *d;
Jack Jansen72781191995-08-07 14:34:15 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 /* Create the module and add the functions */
1574 m = PyModule_Create(&binasciimodule);
1575 if (m == NULL)
1576 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 d = PyModule_GetDict(m);
Jack Jansen72781191995-08-07 14:34:15 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
1581 PyDict_SetItemString(d, "Error", Error);
1582 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
1583 PyDict_SetItemString(d, "Incomplete", Incomplete);
1584 if (PyErr_Occurred()) {
1585 Py_DECREF(m);
1586 m = NULL;
1587 }
1588 return m;
Jack Jansen72781191995-08-07 14:34:15 +00001589}