blob: e54a8ba6eadaca4ba062f5abb8394a47a81a0dc8 [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]
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200187module binascii
188[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300189/*[clinic end generated code: output=da39a3ee5e6b4b0d input=de89fb46bcaf3fec]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200190
191/*[python input]
192
193class ascii_buffer_converter(CConverter):
194 type = 'Py_buffer'
195 converter = 'ascii_buffer_converter'
196 impl_by_reference = True
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500197 c_default = "{NULL, NULL}"
198
199 def cleanup(self):
200 name = self.name
201 return "".join(["if (", name, ".obj)\n PyBuffer_Release(&", name, ");\n"])
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200202
203[python start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800204/*[python end generated code: output=da39a3ee5e6b4b0d input=3eb7b63610da92cd]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200205
Antoine Pitrou08316762011-12-20 13:58:41 +0100206static int
207ascii_buffer_converter(PyObject *arg, Py_buffer *buf)
208{
209 if (arg == NULL) {
210 PyBuffer_Release(buf);
211 return 1;
212 }
213 if (PyUnicode_Check(arg)) {
214 if (PyUnicode_READY(arg) < 0)
215 return 0;
216 if (!PyUnicode_IS_ASCII(arg)) {
217 PyErr_SetString(PyExc_ValueError,
218 "string argument should contain only ASCII characters");
219 return 0;
220 }
221 assert(PyUnicode_KIND(arg) == PyUnicode_1BYTE_KIND);
222 buf->buf = (void *) PyUnicode_1BYTE_DATA(arg);
223 buf->len = PyUnicode_GET_LENGTH(arg);
224 buf->obj = NULL;
225 return 1;
226 }
227 if (PyObject_GetBuffer(arg, buf, PyBUF_SIMPLE) != 0) {
228 PyErr_Format(PyExc_TypeError,
229 "argument should be bytes, buffer or ASCII string, "
Berker Peksag3cd30c22015-02-15 00:31:00 +0200230 "not '%.100s'", Py_TYPE(arg)->tp_name);
Antoine Pitrou08316762011-12-20 13:58:41 +0100231 return 0;
232 }
233 if (!PyBuffer_IsContiguous(buf, 'C')) {
234 PyErr_Format(PyExc_TypeError,
235 "argument should be a contiguous buffer, "
Berker Peksag3cd30c22015-02-15 00:31:00 +0200236 "not '%.100s'", Py_TYPE(arg)->tp_name);
Antoine Pitrou08316762011-12-20 13:58:41 +0100237 PyBuffer_Release(buf);
238 return 0;
239 }
240 return Py_CLEANUP_SUPPORTED;
241}
242
Larry Hastingsf256c222014-01-25 21:30:37 -0800243#include "clinic/binascii.c.h"
Antoine Pitrou08316762011-12-20 13:58:41 +0100244
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200245/*[clinic input]
246binascii.a2b_uu
247
Serhiy Storchaka12785612014-01-25 11:49:49 +0200248 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200249 /
250
251Decode a line of uuencoded data.
252[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000253
254static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200255binascii_a2b_uu_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800256/*[clinic end generated code: output=5779f39b0b48459f input=7cafeaf73df63d1c]*/
Jack Jansen72781191995-08-07 14:34:15 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 unsigned char *ascii_data, *bin_data;
259 int leftbits = 0;
260 unsigned char this_ch;
261 unsigned int leftchar = 0;
262 PyObject *rv;
263 Py_ssize_t ascii_len, bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000264
Serhiy Storchaka12785612014-01-25 11:49:49 +0200265 ascii_data = data->buf;
266 ascii_len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 assert(ascii_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 /* First byte: binary data length (in bytes) */
271 bin_len = (*ascii_data++ - ' ') & 077;
272 ascii_len--;
Jack Jansen72781191995-08-07 14:34:15 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* Allocate the buffer */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200275 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
280 /* XXX is it really best to add NULs if there's no more data */
281 this_ch = (ascii_len > 0) ? *ascii_data : 0;
282 if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
283 /*
284 ** Whitespace. Assume some spaces got eaten at
285 ** end-of-line. (We check this later)
286 */
287 this_ch = 0;
288 } else {
289 /* Check the character for legality
290 ** The 64 in stead of the expected 63 is because
291 ** there are a few uuencodes out there that use
292 ** '`' as zero instead of space.
293 */
294 if ( this_ch < ' ' || this_ch > (' ' + 64)) {
295 PyErr_SetString(Error, "Illegal char");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 Py_DECREF(rv);
297 return NULL;
298 }
299 this_ch = (this_ch - ' ') & 077;
300 }
301 /*
302 ** Shift it in on the low end, and see if there's
303 ** a byte ready for output.
304 */
305 leftchar = (leftchar << 6) | (this_ch);
306 leftbits += 6;
307 if ( leftbits >= 8 ) {
308 leftbits -= 8;
309 *bin_data++ = (leftchar >> leftbits) & 0xff;
310 leftchar &= ((1 << leftbits) - 1);
311 bin_len--;
312 }
313 }
314 /*
315 ** Finally, check that if there's anything left on the line
316 ** that it's whitespace only.
317 */
318 while( ascii_len-- > 0 ) {
319 this_ch = *ascii_data++;
320 /* Extra '`' may be written as padding in some cases */
321 if ( this_ch != ' ' && this_ch != ' '+64 &&
322 this_ch != '\n' && this_ch != '\r' ) {
323 PyErr_SetString(Error, "Trailing garbage");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 Py_DECREF(rv);
325 return NULL;
326 }
327 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000329}
330
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200331/*[clinic input]
332binascii.b2a_uu
333
334 data: Py_buffer
335 /
336
337Uuencode line of data.
338[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000339
Jack Jansen72781191995-08-07 14:34:15 +0000340static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200341binascii_b2a_uu_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800342/*[clinic end generated code: output=181021b69bb9a414 input=00fdf458ce8b465b]*/
Jack Jansen72781191995-08-07 14:34:15 +0000343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 unsigned char *ascii_data, *bin_data;
345 int leftbits = 0;
346 unsigned char this_ch;
347 unsigned int leftchar = 0;
348 PyObject *rv;
349 Py_ssize_t bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000350
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200351 bin_data = data->buf;
352 bin_len = data->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if ( bin_len > 45 ) {
354 /* The 45 is a limit that appears in all uuencode's */
355 PyErr_SetString(Error, "At most 45 bytes at once");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 return NULL;
357 }
Jack Jansen72781191995-08-07 14:34:15 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 /* We're lazy and allocate to much (fixed up later) */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200360 if ( (rv=PyBytes_FromStringAndSize(NULL, 2 + (bin_len+2)/3*4)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 /* Store the length */
365 *ascii_data++ = ' ' + (bin_len & 077);
Tim Peters934c1a12002-07-02 22:24:50 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
368 /* Shift the data (or padding) into our buffer */
369 if ( bin_len > 0 ) /* Data */
370 leftchar = (leftchar << 8) | *bin_data;
371 else /* Padding */
372 leftchar <<= 8;
373 leftbits += 8;
Jack Jansen72781191995-08-07 14:34:15 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 /* See if there are 6-bit groups ready */
376 while ( leftbits >= 6 ) {
377 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
378 leftbits -= 6;
379 *ascii_data++ = this_ch + ' ';
380 }
381 }
382 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (_PyBytes_Resize(&rv,
385 (ascii_data -
386 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200387 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000390}
391
Guido van Rossum2db4f471999-10-19 19:05:14 +0000392
393static int
Thomas Woutersf98db652006-03-01 21:37:32 +0000394binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num)
Guido van Rossum2db4f471999-10-19 19:05:14 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 /* Finds & returns the (num+1)th
397 ** valid character for base64, or -1 if none.
398 */
Guido van Rossum2db4f471999-10-19 19:05:14 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 int ret = -1;
401 unsigned char c, b64val;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 while ((slen > 0) && (ret == -1)) {
404 c = *s;
405 b64val = table_a2b_base64[c & 0x7f];
406 if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
407 if (num == 0)
408 ret = *s;
409 num--;
410 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 s++;
413 slen--;
414 }
415 return ret;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000416}
417
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200418/*[clinic input]
419binascii.a2b_base64
420
Serhiy Storchaka12785612014-01-25 11:49:49 +0200421 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200422 /
423
424Decode a line of base64 data.
425[clinic start generated code]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000426
427static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200428binascii_a2b_base64_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800429/*[clinic end generated code: output=3e351b702bed56d2 input=5872acf6e1cac243]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 unsigned char *ascii_data, *bin_data;
432 int leftbits = 0;
433 unsigned char this_ch;
434 unsigned int leftchar = 0;
435 PyObject *rv;
436 Py_ssize_t ascii_len, bin_len;
437 int quad_pos = 0;
Tim Peters934c1a12002-07-02 22:24:50 +0000438
Serhiy Storchaka12785612014-01-25 11:49:49 +0200439 ascii_data = data->buf;
440 ascii_len = data->len;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 assert(ascii_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000443
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200444 if (ascii_len > PY_SSIZE_T_MAX - 3)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* Allocate the buffer */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200450 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
453 bin_len = 0;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 for( ; ascii_len > 0; ascii_len--, ascii_data++) {
456 this_ch = *ascii_data;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (this_ch > 0x7f ||
459 this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
460 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 /* Check for pad sequences and ignore
463 ** the invalid ones.
464 */
465 if (this_ch == BASE64_PAD) {
466 if ( (quad_pos < 2) ||
467 ((quad_pos == 2) &&
468 (binascii_find_valid(ascii_data, ascii_len, 1)
469 != BASE64_PAD)) )
470 {
471 continue;
472 }
473 else {
474 /* A pad sequence means no more input.
475 ** We've already interpreted the data
476 ** from the quad at this point.
477 */
478 leftbits = 0;
479 break;
480 }
481 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 this_ch = table_a2b_base64[*ascii_data];
484 if ( this_ch == (unsigned char) -1 )
485 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 /*
488 ** Shift it in on the low end, and see if there's
489 ** a byte ready for output.
490 */
491 quad_pos = (quad_pos + 1) & 0x03;
492 leftchar = (leftchar << 6) | (this_ch);
493 leftbits += 6;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if ( leftbits >= 8 ) {
496 leftbits -= 8;
497 *bin_data++ = (leftchar >> leftbits) & 0xff;
498 bin_len++;
499 leftchar &= ((1 << leftbits) - 1);
500 }
501 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (leftbits != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 PyErr_SetString(Error, "Incorrect padding");
505 Py_DECREF(rv);
506 return NULL;
507 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* And set string size correctly. If the result string is empty
510 ** (because the input was all invalid) return the shared empty
511 ** string instead; _PyBytes_Resize() won't do this for us.
512 */
513 if (bin_len > 0) {
514 if (_PyBytes_Resize(&rv, bin_len) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200515 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 }
517 }
518 else {
519 Py_DECREF(rv);
520 rv = PyBytes_FromStringAndSize("", 0);
521 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 return rv;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000523}
524
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200525
526/*[clinic input]
527binascii.b2a_base64
528
529 data: Py_buffer
530 /
531
532Base64-code line of data.
533[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000534
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000535static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200536binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800537/*[clinic end generated code: output=3cd61fbee2913285 input=14ec4e47371174a9]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 unsigned char *ascii_data, *bin_data;
540 int leftbits = 0;
541 unsigned char this_ch;
542 unsigned int leftchar = 0;
543 PyObject *rv;
544 Py_ssize_t bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000545
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200546 bin_data = data->buf;
547 bin_len = data->len;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 assert(bin_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if ( bin_len > BASE64_MAXBIN ) {
552 PyErr_SetString(Error, "Too much data for base64 line");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return NULL;
554 }
Tim Peters934c1a12002-07-02 22:24:50 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* We're lazy and allocate too much (fixed up later).
557 "+3" leaves room for up to two pad characters and a trailing
558 newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200559 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
564 /* Shift the data into our buffer */
565 leftchar = (leftchar << 8) | *bin_data;
566 leftbits += 8;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 /* See if there are 6-bit groups ready */
569 while ( leftbits >= 6 ) {
570 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
571 leftbits -= 6;
572 *ascii_data++ = table_b2a_base64[this_ch];
573 }
574 }
575 if ( leftbits == 2 ) {
576 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
577 *ascii_data++ = BASE64_PAD;
578 *ascii_data++ = BASE64_PAD;
579 } else if ( leftbits == 4 ) {
580 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
581 *ascii_data++ = BASE64_PAD;
582 }
583 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if (_PyBytes_Resize(&rv,
586 (ascii_data -
587 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200588 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 return rv;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000591}
592
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200593/*[clinic input]
594binascii.a2b_hqx
595
Serhiy Storchaka12785612014-01-25 11:49:49 +0200596 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200597 /
598
599Decode .hqx coding.
600[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000601
602static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200603binascii_a2b_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800604/*[clinic end generated code: output=60bcdbbd28b105cd input=0d914c680e0eed55]*/
Jack Jansen72781191995-08-07 14:34:15 +0000605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 unsigned char *ascii_data, *bin_data;
607 int leftbits = 0;
608 unsigned char this_ch;
609 unsigned int leftchar = 0;
610 PyObject *rv;
611 Py_ssize_t len;
612 int done = 0;
Tim Peters934c1a12002-07-02 22:24:50 +0000613
Serhiy Storchaka12785612014-01-25 11:49:49 +0200614 ascii_data = data->buf;
615 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000618
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200619 if (len > PY_SSIZE_T_MAX - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* Allocate a string that is too big (fixed later)
623 Add two to the initial length to prevent interning which
624 would preclude subsequent resizing. */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200625 if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 for( ; len > 0 ; len--, ascii_data++ ) {
630 /* Get the byte and look it up */
631 this_ch = table_a2b_hqx[*ascii_data];
632 if ( this_ch == SKIP )
633 continue;
634 if ( this_ch == FAIL ) {
635 PyErr_SetString(Error, "Illegal char");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 Py_DECREF(rv);
637 return NULL;
638 }
639 if ( this_ch == DONE ) {
640 /* The terminating colon */
641 done = 1;
642 break;
643 }
Jack Jansen72781191995-08-07 14:34:15 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 /* Shift it into the buffer and see if any bytes are ready */
646 leftchar = (leftchar << 6) | (this_ch);
647 leftbits += 6;
648 if ( leftbits >= 8 ) {
649 leftbits -= 8;
650 *bin_data++ = (leftchar >> leftbits) & 0xff;
651 leftchar &= ((1 << leftbits) - 1);
652 }
653 }
Tim Peters934c1a12002-07-02 22:24:50 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if ( leftbits && !done ) {
656 PyErr_SetString(Incomplete,
657 "String has incomplete number of bytes");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 Py_DECREF(rv);
659 return NULL;
660 }
661 if (_PyBytes_Resize(&rv,
662 (bin_data -
663 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200664 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 }
666 if (rv) {
667 PyObject *rrv = Py_BuildValue("Oi", rv, done);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 Py_DECREF(rv);
669 return rrv;
670 }
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000673}
674
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200675
676/*[clinic input]
677binascii.rlecode_hqx
678
679 data: Py_buffer
680 /
681
682Binhex RLE-code binary data.
683[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000684
685static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200686binascii_rlecode_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800687/*[clinic end generated code: output=0905da344dbf0648 input=e1f1712447a82b09]*/
Jack Jansen72781191995-08-07 14:34:15 +0000688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 unsigned char *in_data, *out_data;
690 PyObject *rv;
691 unsigned char ch;
692 Py_ssize_t in, inend, len;
Tim Peters934c1a12002-07-02 22:24:50 +0000693
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200694 in_data = data->buf;
695 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000698
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200699 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 /* Worst case: output is twice as big as input (fixed later) */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200703 if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 out_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 for( in=0; in<len; in++) {
708 ch = in_data[in];
709 if ( ch == RUNCHAR ) {
710 /* RUNCHAR. Escape it. */
711 *out_data++ = RUNCHAR;
712 *out_data++ = 0;
713 } else {
714 /* Check how many following are the same */
715 for(inend=in+1;
716 inend<len && in_data[inend] == ch &&
717 inend < in+255;
718 inend++) ;
719 if ( inend - in > 3 ) {
720 /* More than 3 in a row. Output RLE. */
721 *out_data++ = ch;
722 *out_data++ = RUNCHAR;
Antoine Pitrou40455752010-08-15 18:51:10 +0000723 *out_data++ = (unsigned char) (inend-in);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 in = inend-1;
725 } else {
726 /* Less than 3. Output the byte itself */
727 *out_data++ = ch;
728 }
729 }
730 }
731 if (_PyBytes_Resize(&rv,
732 (out_data -
733 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200734 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000737}
738
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200739
740/*[clinic input]
741binascii.b2a_hqx
742
743 data: Py_buffer
744 /
745
746Encode .hqx data.
747[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000748
Jack Jansen72781191995-08-07 14:34:15 +0000749static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200750binascii_b2a_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800751/*[clinic end generated code: output=5a987810d5e3cdbb input=9596ebe019fe12ba]*/
Jack Jansen72781191995-08-07 14:34:15 +0000752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 unsigned char *ascii_data, *bin_data;
754 int leftbits = 0;
755 unsigned char this_ch;
756 unsigned int leftchar = 0;
757 PyObject *rv;
758 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000759
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200760 bin_data = data->buf;
761 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000764
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200765 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 /* Allocate a buffer that is at least large enough */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200769 if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 for( ; len > 0 ; len--, bin_data++ ) {
774 /* Shift into our buffer, and output any 6bits ready */
775 leftchar = (leftchar << 8) | *bin_data;
776 leftbits += 8;
777 while ( leftbits >= 6 ) {
778 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
779 leftbits -= 6;
780 *ascii_data++ = table_b2a_hqx[this_ch];
781 }
782 }
783 /* Output a possible runt byte */
784 if ( leftbits ) {
785 leftchar <<= (6-leftbits);
786 *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
787 }
788 if (_PyBytes_Resize(&rv,
789 (ascii_data -
790 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200791 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000794}
795
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200796
797/*[clinic input]
798binascii.rledecode_hqx
799
800 data: Py_buffer
801 /
802
803Decode hexbin RLE-coded string.
804[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000805
Jack Jansen72781191995-08-07 14:34:15 +0000806static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200807binascii_rledecode_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800808/*[clinic end generated code: output=f7afd89b789946ab input=54cdd49fc014402c]*/
Jack Jansen72781191995-08-07 14:34:15 +0000809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 unsigned char *in_data, *out_data;
811 unsigned char in_byte, in_repeat;
812 PyObject *rv;
813 Py_ssize_t in_len, out_len, out_len_left;
Jack Jansen72781191995-08-07 14:34:15 +0000814
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200815 in_data = data->buf;
816 in_len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 assert(in_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* Empty string is a special case */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200821 if ( in_len == 0 )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 return PyBytes_FromStringAndSize("", 0);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200823 else if (in_len > PY_SSIZE_T_MAX / 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 return PyErr_NoMemory();
Jack Jansen72781191995-08-07 14:34:15 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 /* Allocate a buffer of reasonable size. Resized when needed */
827 out_len = in_len*2;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200828 if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 out_len_left = out_len;
831 out_data = (unsigned char *)PyBytes_AS_STRING(rv);
Jack Jansen72781191995-08-07 14:34:15 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 /*
834 ** We need two macros here to get/put bytes and handle
835 ** end-of-buffer for input and output strings.
836 */
Jack Jansen72781191995-08-07 14:34:15 +0000837#define INBYTE(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 do { \
839 if ( --in_len < 0 ) { \
840 PyErr_SetString(Incomplete, ""); \
841 Py_DECREF(rv); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 return NULL; \
843 } \
844 b = *in_data++; \
845 } while(0)
Tim Peters934c1a12002-07-02 22:24:50 +0000846
Jack Jansen72781191995-08-07 14:34:15 +0000847#define OUTBYTE(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 do { \
849 if ( --out_len_left < 0 ) { \
850 if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
851 if (_PyBytes_Resize(&rv, 2*out_len) < 0) \
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200852 { Py_XDECREF(rv); return NULL; } \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
854 + out_len; \
855 out_len_left = out_len-1; \
856 out_len = out_len * 2; \
857 } \
858 *out_data++ = b; \
859 } while(0)
Jack Jansen72781191995-08-07 14:34:15 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 /*
862 ** Handle first byte separately (since we have to get angry
863 ** in case of an orphaned RLE code).
864 */
865 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (in_byte == RUNCHAR) {
868 INBYTE(in_repeat);
869 if (in_repeat != 0) {
870 /* Note Error, not Incomplete (which is at the end
871 ** of the string only). This is a programmer error.
872 */
873 PyErr_SetString(Error, "Orphaned RLE code at start");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 Py_DECREF(rv);
875 return NULL;
876 }
877 OUTBYTE(RUNCHAR);
878 } else {
879 OUTBYTE(in_byte);
880 }
Tim Peters934c1a12002-07-02 22:24:50 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 while( in_len > 0 ) {
883 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 if (in_byte == RUNCHAR) {
886 INBYTE(in_repeat);
887 if ( in_repeat == 0 ) {
888 /* Just an escaped RUNCHAR value */
889 OUTBYTE(RUNCHAR);
890 } else {
891 /* Pick up value and output a sequence of it */
892 in_byte = out_data[-1];
893 while ( --in_repeat > 0 )
894 OUTBYTE(in_byte);
895 }
896 } else {
897 /* Normal byte */
898 OUTBYTE(in_byte);
899 }
900 }
901 if (_PyBytes_Resize(&rv,
902 (out_data -
903 (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200904 Py_CLEAR(rv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000907}
908
Jack Jansen72781191995-08-07 14:34:15 +0000909
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200910/*[clinic input]
911binascii.crc_hqx -> int
912
913 data: Py_buffer
914 crc: int
915 /
916
917Compute hqx CRC incrementally.
918[clinic start generated code]*/
919
920static int
921binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, int crc)
Larry Hastings581ee362014-01-28 05:00:08 -0800922/*[clinic end generated code: output=634dac18dfa863d7 input=68060931b2f51c8a]*/
Jack Jansen72781191995-08-07 14:34:15 +0000923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 unsigned char *bin_data;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200925 unsigned int ucrc = (unsigned int)crc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000927
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200928 bin_data = data->buf;
929 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 while(len-- > 0) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200932 ucrc=((ucrc<<8)&0xff00)^crctab_hqx[((ucrc>>8)&0xff)^*bin_data++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 }
Jack Jansen72781191995-08-07 14:34:15 +0000934
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200935 return (int)ucrc;
Jack Jansen72781191995-08-07 14:34:15 +0000936}
937
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200938#ifndef USE_ZLIB_CRC32
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000939/* Crc - 32 BIT ANSI X3.66 CRC checksum files
940 Also known as: ISO 3307
941**********************************************************************|
942* *|
943* Demonstration program to compute the 32-bit CRC used as the frame *|
944* check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *|
945* and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *|
946* protocol). The 32-bit FCS was added via the Federal Register, *|
947* 1 June 1982, p.23798. I presume but don't know for certain that *|
948* this polynomial is or will be included in CCITT V.41, which *|
949* defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *|
950* PUB 78 says that the 32-bit FCS reduces otherwise undetected *|
951* errors by a factor of 10^-5 over 16-bit FCS. *|
952* *|
953**********************************************************************|
954
955 Copyright (C) 1986 Gary S. Brown. You may use this program, or
956 code or tables extracted from it, as desired without restriction.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000957
Tim Peters934c1a12002-07-02 22:24:50 +0000958 First, the polynomial itself and its table of feedback terms. The
959 polynomial is
960 X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
961 Note that we take it "backwards" and put the highest-order term in
962 the lowest-order bit. The X^32 term is "implied"; the LSB is the
963 X^31 term, etc. The X^0 term (usually shown as "+1") results in
964 the MSB being 1.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000965
Tim Peters934c1a12002-07-02 22:24:50 +0000966 Note that the usual hardware shift register implementation, which
967 is what we're using (we're merely optimizing it by doing eight-bit
968 chunks at a time) shifts bits into the lowest-order term. In our
969 implementation, that means shifting towards the right. Why do we
970 do it this way? Because the calculated CRC must be transmitted in
971 order from highest-order term to lowest-order term. UARTs transmit
972 characters in order from LSB to MSB. By storing the CRC this way,
973 we hand it to the UART in the order low-byte to high-byte; the UART
974 sends each low-bit to hight-bit; and the result is transmission bit
975 by bit from highest- to lowest-order term without requiring any bit
976 shuffling on our part. Reception works similarly.
977
978 The feedback terms table consists of 256, 32-bit entries. Notes:
979
980 1. The table can be generated at runtime if desired; code to do so
981 is shown later. It might not be obvious, but the feedback
982 terms simply represent the results of eight shift/xor opera-
983 tions for all combinations of data and CRC register values.
984
985 2. The CRC accumulation logic is the same for all CRC polynomials,
986 be they sixteen or thirty-two bits wide. You simply choose the
987 appropriate table. Alternatively, because the table can be
988 generated at runtime, you can start by generating the table for
989 the polynomial in question and use exactly the same "updcrc",
990 if your application needn't simultaneously handle two CRC
991 polynomials. (Note, however, that XMODEM is strange.)
992
993 3. For 16-bit CRCs, the table entries need be only 16 bits wide;
994 of course, 32-bit entries work OK if the high 16 bits are zero.
995
996 4. The values must be right-shifted by eight bits by the "updcrc"
997 logic; the shift must be unsigned (bring in zeroes). On some
998 hardware you could probably optimize the shift in assembler by
999 using byte-swap instructions.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001000********************************************************************/
1001
Gregory P. Smith3c0e4d22008-03-25 07:51:12 +00001002static unsigned int crc_32_tab[256] = {
10030x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
10040x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
10050xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
10060x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
10070x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
10080x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
10090xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
10100xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
10110x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
10120x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
10130xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
10140xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
10150x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
10160x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
10170x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
10180xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
10190x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
10200x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
10210x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
10220xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
10230x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
10240x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
10250xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
10260xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
10270x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
10280x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
10290x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
10300x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
10310xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
10320x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
10330x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
10340x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
10350xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
10360xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
10370x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
10380x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
10390xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
10400xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
10410x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
10420x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
10430x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
10440xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
10450x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
10460x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
10470x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
10480xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
10490x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
10500x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
10510xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
10520xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
10530x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
10540x2d02ef8dU
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001055};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001056#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001057
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001058/*[clinic input]
1059binascii.crc32 -> unsigned_int
1060
1061 data: Py_buffer
1062 crc: unsigned_int(bitwise=True) = 0
1063 /
1064
1065Compute CRC-32 incrementally.
1066[clinic start generated code]*/
1067
1068static unsigned int
1069binascii_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc)
Larry Hastings581ee362014-01-28 05:00:08 -08001070/*[clinic end generated code: output=620a961643393c4f input=bbe340bc99d25aa8]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001071
1072#ifdef USE_ZLIB_CRC32
1073/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
1074{
1075 Byte *buf;
1076 Py_ssize_t len;
1077 int signed_val;
1078
1079 buf = (Byte*)data->buf;
1080 len = data->len;
1081 signed_val = crc32(crc, buf, len);
1082 return (unsigned int)signed_val & 0xffffffffU;
1083}
1084#else /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001085{ /* By Jim Ahlstrom; All rights transferred to CNRI */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 Py_ssize_t len;
1088 unsigned int result;
Tim Peters934c1a12002-07-02 22:24:50 +00001089
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001090 bin_data = data->buf;
1091 len = data->len;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 crc = ~ crc;
1094 while (len-- > 0) {
1095 crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
1096 /* Note: (crc >> 8) MUST zero fill on left */
1097 }
Tim Petersa98011c2002-07-02 20:20:08 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 result = (crc ^ 0xFFFFFFFF);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001100 return result & 0xffffffff;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001101}
Christian Heimes1dc54002008-03-24 02:19:29 +00001102#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001103
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001104/*[clinic input]
1105binascii.b2a_hex
1106
1107 data: Py_buffer
1108 /
1109
1110Hexadecimal representation of binary data.
1111
1112The return value is a bytes object. This function is also
1113available as "hexlify()".
1114[clinic start generated code]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001115
1116static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001117binascii_b2a_hex_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -08001118/*[clinic end generated code: output=179318922c2f8fda input=96423cfa299ff3b1]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 char* argbuf;
1121 Py_ssize_t arglen;
1122 PyObject *retval;
1123 char* retbuf;
1124 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001125
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001126 argbuf = data->buf;
1127 arglen = data->len;
Barry Warsawe977c212000-08-15 06:07:13 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 assert(arglen >= 0);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001130 if (arglen > PY_SSIZE_T_MAX / 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 retval = PyBytes_FromStringAndSize(NULL, arglen*2);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001134 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 /* make hex version of string, taken from shamodule.c */
1139 for (i=j=0; i < arglen; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +02001140 unsigned char c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 c = (argbuf[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +02001142 retbuf[j++] = Py_hexdigits[c];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 c = argbuf[i] & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +02001144 retbuf[j++] = Py_hexdigits[c];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 return retval;
Barry Warsawe977c212000-08-15 06:07:13 +00001147}
1148
Zachary Wareb176d402015-01-20 13:59:46 -06001149/*[clinic input]
1150binascii.hexlify = binascii.b2a_hex
1151
1152Hexadecimal representation of binary data.
1153
1154The return value is a bytes object.
1155[clinic start generated code]*/
1156
1157static PyObject *
1158binascii_hexlify_impl(PyModuleDef *module, Py_buffer *data)
1159/*[clinic end generated code: output=6098440091fb61dc input=2e3afae7f083f061]*/
1160{
1161 return binascii_b2a_hex_impl(module, data);
1162}
Barry Warsawe977c212000-08-15 06:07:13 +00001163
1164static int
Tim Peters934c1a12002-07-02 22:24:50 +00001165to_int(int c)
Barry Warsawe977c212000-08-15 06:07:13 +00001166{
Antoine Pitrou4de74572013-02-09 23:11:27 +01001167 if (Py_ISDIGIT(c))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 return c - '0';
1169 else {
Antoine Pitroued8ba142011-10-04 13:50:21 +02001170 if (Py_ISUPPER(c))
1171 c = Py_TOLOWER(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (c >= 'a' && c <= 'f')
1173 return c - 'a' + 10;
1174 }
1175 return -1;
Barry Warsawe977c212000-08-15 06:07:13 +00001176}
1177
1178
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001179/*[clinic input]
1180binascii.a2b_hex
1181
1182 hexstr: ascii_buffer
1183 /
1184
1185Binary data of hexadecimal representation.
1186
1187hexstr must contain an even number of hex digits (upper or lower case).
1188This function is also available as "unhexlify()".
1189[clinic start generated code]*/
1190
Barry Warsawe977c212000-08-15 06:07:13 +00001191static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001192binascii_a2b_hex_impl(PyModuleDef *module, Py_buffer *hexstr)
Larry Hastings581ee362014-01-28 05:00:08 -08001193/*[clinic end generated code: output=d61da452b5c6d290 input=9e1e7f2f94db24fd]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 char* argbuf;
1196 Py_ssize_t arglen;
1197 PyObject *retval;
1198 char* retbuf;
1199 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001200
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001201 argbuf = hexstr->buf;
1202 arglen = hexstr->len;
Barry Warsawe977c212000-08-15 06:07:13 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 assert(arglen >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 /* XXX What should we do about strings with an odd length? Should
1207 * we add an implicit leading zero, or a trailing zero? For now,
1208 * raise an exception.
1209 */
1210 if (arglen % 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 PyErr_SetString(Error, "Odd-length string");
1212 return NULL;
1213 }
Barry Warsawe977c212000-08-15 06:07:13 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001216 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 for (i=j=0; i < arglen; i += 2) {
1221 int top = to_int(Py_CHARMASK(argbuf[i]));
1222 int bot = to_int(Py_CHARMASK(argbuf[i+1]));
1223 if (top == -1 || bot == -1) {
1224 PyErr_SetString(Error,
1225 "Non-hexadecimal digit found");
1226 goto finally;
1227 }
1228 retbuf[j++] = (top << 4) + bot;
1229 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 return retval;
Barry Warsawe977c212000-08-15 06:07:13 +00001231
1232 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Py_DECREF(retval);
1234 return NULL;
Barry Warsawe977c212000-08-15 06:07:13 +00001235}
1236
Zachary Wareb176d402015-01-20 13:59:46 -06001237/*[clinic input]
1238binascii.unhexlify = binascii.a2b_hex
1239
1240Binary data of hexadecimal representation.
1241
1242hexstr must contain an even number of hex digits (upper or lower case).
1243[clinic start generated code]*/
1244
1245static PyObject *
1246binascii_unhexlify_impl(PyModuleDef *module, Py_buffer *hexstr)
1247/*[clinic end generated code: output=17cec7544499803e input=dd8c012725f462da]*/
1248{
1249 return binascii_a2b_hex_impl(module, hexstr);
1250}
1251
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001252static int table_hex[128] = {
1253 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1254 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1255 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1256 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1257 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1258 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1259 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1260 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1261};
1262
1263#define hexval(c) table_hex[(unsigned int)(c)]
1264
1265#define MAXLINESIZE 76
1266
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001267
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001268/*[clinic input]
1269binascii.a2b_qp
1270
Serhiy Storchaka12785612014-01-25 11:49:49 +02001271 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001272 header: int(c_default="0") = False
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001273
1274Decode a string of qp-encoded data.
1275[clinic start generated code]*/
1276
1277static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +02001278binascii_a2b_qp_impl(PyModuleDef *module, Py_buffer *data, int header)
Larry Hastings581ee362014-01-28 05:00:08 -08001279/*[clinic end generated code: output=a44ef88270352114 input=5187a0d3d8e54f3b]*/
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 Py_ssize_t in, out;
1282 char ch;
Serhiy Storchaka12785612014-01-25 11:49:49 +02001283 unsigned char *ascii_data, *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 Py_ssize_t datalen = 0;
1285 PyObject *rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001286
Serhiy Storchaka12785612014-01-25 11:49:49 +02001287 ascii_data = data->buf;
1288 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* We allocate the output same size as input, this is overkill.
1291 * The previous implementation used calloc() so we'll zero out the
1292 * memory here too, since PyMem_Malloc() does not guarantee that.
1293 */
1294 odata = (unsigned char *) PyMem_Malloc(datalen);
1295 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PyErr_NoMemory();
1297 return NULL;
1298 }
1299 memset(odata, 0, datalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 in = out = 0;
1302 while (in < datalen) {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001303 if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 in++;
1305 if (in >= datalen) break;
1306 /* Soft line breaks */
Serhiy Storchaka12785612014-01-25 11:49:49 +02001307 if ((ascii_data[in] == '\n') || (ascii_data[in] == '\r')) {
1308 if (ascii_data[in] != '\n') {
1309 while (in < datalen && ascii_data[in] != '\n') in++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 }
1311 if (in < datalen) in++;
1312 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001313 else if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* broken case from broken python qp */
1315 odata[out++] = '=';
1316 in++;
1317 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001318 else if (((ascii_data[in] >= 'A' && ascii_data[in] <= 'F') ||
1319 (ascii_data[in] >= 'a' && ascii_data[in] <= 'f') ||
1320 (ascii_data[in] >= '0' && ascii_data[in] <= '9')) &&
1321 ((ascii_data[in+1] >= 'A' && ascii_data[in+1] <= 'F') ||
1322 (ascii_data[in+1] >= 'a' && ascii_data[in+1] <= 'f') ||
1323 (ascii_data[in+1] >= '0' && ascii_data[in+1] <= '9'))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 /* hexval */
Serhiy Storchaka12785612014-01-25 11:49:49 +02001325 ch = hexval(ascii_data[in]) << 4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 in++;
Serhiy Storchaka12785612014-01-25 11:49:49 +02001327 ch |= hexval(ascii_data[in]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 in++;
1329 odata[out++] = ch;
1330 }
1331 else {
1332 odata[out++] = '=';
1333 }
1334 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001335 else if (header && ascii_data[in] == '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 odata[out++] = ' ';
1337 in++;
1338 }
1339 else {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001340 odata[out] = ascii_data[in];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 in++;
1342 out++;
1343 }
1344 }
1345 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 PyMem_Free(odata);
1347 return NULL;
1348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 PyMem_Free(odata);
1350 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001351}
1352
Tim Peters934c1a12002-07-02 22:24:50 +00001353static int
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001354to_hex (unsigned char ch, unsigned char *s)
1355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 unsigned int uvalue = ch;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 s[1] = "0123456789ABCDEF"[uvalue % 16];
1359 uvalue = (uvalue / 16);
1360 s[0] = "0123456789ABCDEF"[uvalue % 16];
1361 return 0;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001362}
1363
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001364/* XXX: This is ridiculously complicated to be backward compatible
1365 * (mostly) with the quopri module. It doesn't re-create the quopri
1366 * module bug where text ending in CRLF has the CR encoded */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001367
1368/*[clinic input]
1369binascii.b2a_qp
1370
1371 data: Py_buffer
1372 quotetabs: int(c_default="0") = False
1373 istext: int(c_default="1") = True
1374 header: int(c_default="0") = False
1375
1376Encode a string using quoted-printable encoding.
1377
1378On encoding, when istext is set, newlines are not encoded, and white
1379space at end of lines is. When istext is not set, \r and \n (CR/LF)
1380are both encoded. When quotetabs is set, space and tabs are encoded.
1381[clinic start generated code]*/
1382
1383static PyObject *
1384binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs, int istext, int header)
Larry Hastings581ee362014-01-28 05:00:08 -08001385/*[clinic end generated code: output=ff2991ba640fff3e input=7f2a9aaa008e92b2]*/
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 Py_ssize_t in, out;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001388 unsigned char *databuf, *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 Py_ssize_t datalen = 0, odatalen = 0;
1390 PyObject *rv;
1391 unsigned int linelen = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 unsigned char ch;
1393 int crlf = 0;
1394 unsigned char *p;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001395
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001396 databuf = data->buf;
1397 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 /* See if this string is using CRLF line ends */
1400 /* XXX: this function has the side effect of converting all of
1401 * the end of lines to be the same depending on this detection
1402 * here */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001403 p = (unsigned char *) memchr(databuf, '\n', datalen);
1404 if ((p != NULL) && (p > databuf) && (*(p-1) == '\r'))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 crlf = 1;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 /* First, scan to see how many characters need to be encoded */
1408 in = 0;
1409 while (in < datalen) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001410 if ((databuf[in] > 126) ||
1411 (databuf[in] == '=') ||
1412 (header && databuf[in] == '_') ||
1413 ((databuf[in] == '.') && (linelen == 0) &&
1414 (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1415 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1416 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1417 ((databuf[in] < 33) &&
1418 (databuf[in] != '\r') && (databuf[in] != '\n') &&
1419 (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' ')))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 {
1421 if ((linelen + 3) >= MAXLINESIZE) {
1422 linelen = 0;
1423 if (crlf)
1424 odatalen += 3;
1425 else
1426 odatalen += 2;
1427 }
1428 linelen += 3;
1429 odatalen += 3;
1430 in++;
1431 }
1432 else {
1433 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001434 ((databuf[in] == '\n') ||
1435 ((in+1 < datalen) && (databuf[in] == '\r') &&
1436 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 {
1438 linelen = 0;
1439 /* Protect against whitespace on end of line */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001440 if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 odatalen += 2;
1442 if (crlf)
1443 odatalen += 2;
1444 else
1445 odatalen += 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001446 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 in += 2;
1448 else
1449 in++;
1450 }
1451 else {
1452 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001453 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 (linelen + 1) >= MAXLINESIZE) {
1455 linelen = 0;
1456 if (crlf)
1457 odatalen += 3;
1458 else
1459 odatalen += 2;
1460 }
1461 linelen++;
1462 odatalen++;
1463 in++;
1464 }
1465 }
1466 }
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 /* We allocate the output same size as input, this is overkill.
1469 * The previous implementation used calloc() so we'll zero out the
1470 * memory here too, since PyMem_Malloc() does not guarantee that.
1471 */
1472 odata = (unsigned char *) PyMem_Malloc(odatalen);
1473 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 PyErr_NoMemory();
1475 return NULL;
1476 }
1477 memset(odata, 0, odatalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 in = out = linelen = 0;
1480 while (in < datalen) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001481 if ((databuf[in] > 126) ||
1482 (databuf[in] == '=') ||
1483 (header && databuf[in] == '_') ||
1484 ((databuf[in] == '.') && (linelen == 0) &&
1485 (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1486 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1487 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1488 ((databuf[in] < 33) &&
1489 (databuf[in] != '\r') && (databuf[in] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 (quotetabs ||
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001491 (!quotetabs && ((databuf[in] != '\t') && (databuf[in] != ' '))))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 {
1493 if ((linelen + 3 )>= MAXLINESIZE) {
1494 odata[out++] = '=';
1495 if (crlf) odata[out++] = '\r';
1496 odata[out++] = '\n';
1497 linelen = 0;
1498 }
1499 odata[out++] = '=';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001500 to_hex(databuf[in], &odata[out]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 out += 2;
1502 in++;
1503 linelen += 3;
1504 }
1505 else {
1506 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001507 ((databuf[in] == '\n') ||
1508 ((in+1 < datalen) && (databuf[in] == '\r') &&
1509 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 {
1511 linelen = 0;
1512 /* Protect against whitespace on end of line */
1513 if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
1514 ch = odata[out-1];
1515 odata[out-1] = '=';
1516 to_hex(ch, &odata[out]);
1517 out += 2;
1518 }
Tim Peters934c1a12002-07-02 22:24:50 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (crlf) odata[out++] = '\r';
1521 odata[out++] = '\n';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001522 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 in += 2;
1524 else
1525 in++;
1526 }
1527 else {
1528 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001529 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 (linelen + 1) >= MAXLINESIZE) {
1531 odata[out++] = '=';
1532 if (crlf) odata[out++] = '\r';
1533 odata[out++] = '\n';
1534 linelen = 0;
1535 }
1536 linelen++;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001537 if (header && databuf[in] == ' ') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 odata[out++] = '_';
1539 in++;
1540 }
1541 else {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001542 odata[out++] = databuf[in++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 }
1544 }
1545 }
1546 }
1547 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 PyMem_Free(odata);
1549 return NULL;
1550 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 PyMem_Free(odata);
1552 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001553}
Barry Warsawe977c212000-08-15 06:07:13 +00001554
Jack Jansen72781191995-08-07 14:34:15 +00001555/* List of functions defined in the module */
1556
1557static struct PyMethodDef binascii_module_methods[] = {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001558 BINASCII_A2B_UU_METHODDEF
1559 BINASCII_B2A_UU_METHODDEF
1560 BINASCII_A2B_BASE64_METHODDEF
1561 BINASCII_B2A_BASE64_METHODDEF
1562 BINASCII_A2B_HQX_METHODDEF
1563 BINASCII_B2A_HQX_METHODDEF
1564 BINASCII_A2B_HEX_METHODDEF
1565 BINASCII_B2A_HEX_METHODDEF
Zachary Wareb176d402015-01-20 13:59:46 -06001566 BINASCII_HEXLIFY_METHODDEF
1567 BINASCII_UNHEXLIFY_METHODDEF
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001568 BINASCII_RLECODE_HQX_METHODDEF
1569 BINASCII_RLEDECODE_HQX_METHODDEF
1570 BINASCII_CRC_HQX_METHODDEF
1571 BINASCII_CRC32_METHODDEF
1572 BINASCII_A2B_QP_METHODDEF
1573 BINASCII_B2A_QP_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 {NULL, NULL} /* sentinel */
Jack Jansen72781191995-08-07 14:34:15 +00001575};
1576
1577
Martin v. Löwis1a214512008-06-11 05:26:20 +00001578/* Initialization function for the module (*must* be called PyInit_binascii) */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001579PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
Jack Jansen72781191995-08-07 14:34:15 +00001580
Martin v. Löwis1a214512008-06-11 05:26:20 +00001581
1582static struct PyModuleDef binasciimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 PyModuleDef_HEAD_INIT,
1584 "binascii",
1585 doc_binascii,
1586 -1,
1587 binascii_module_methods,
1588 NULL,
1589 NULL,
1590 NULL,
1591 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001592};
1593
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001594PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001595PyInit_binascii(void)
Jack Jansen72781191995-08-07 14:34:15 +00001596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 PyObject *m, *d;
Jack Jansen72781191995-08-07 14:34:15 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 /* Create the module and add the functions */
1600 m = PyModule_Create(&binasciimodule);
1601 if (m == NULL)
1602 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 d = PyModule_GetDict(m);
Jack Jansen72781191995-08-07 14:34:15 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
1607 PyDict_SetItemString(d, "Error", Error);
1608 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
1609 PyDict_SetItemString(d, "Incomplete", Incomplete);
1610 if (PyErr_Occurred()) {
1611 Py_DECREF(m);
1612 m = NULL;
1613 }
1614 return m;
Jack Jansen72781191995-08-07 14:34:15 +00001615}