blob: ccd81faf94cebf6b7444d43eeb1c2301f5cfe4e4 [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"
Gregory P. Smith9c6b9162015-04-26 00:42:13 +000059#include "pystrhex.h"
Christian Heimes1dc54002008-03-24 02:19:29 +000060#ifdef USE_ZLIB_CRC32
61#include "zlib.h"
62#endif
Jack Jansen72781191995-08-07 14:34:15 +000063
64static PyObject *Error;
65static PyObject *Incomplete;
66
67/*
68** hqx lookup table, ascii->binary.
69*/
70
71#define RUNCHAR 0x90
72
73#define DONE 0x7F
74#define SKIP 0x7E
75#define FAIL 0x7D
76
77static unsigned char table_a2b_hqx[256] = {
78/* ^@ ^A ^B ^C ^D ^E ^F ^G */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000080/* \b \t \n ^K ^L \r ^N ^O */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081/* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000082/* ^P ^Q ^R ^S ^T ^U ^V ^W */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083/* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000084/* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085/* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000086/* ! " # $ % & ' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087/* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
Jack Jansen72781191995-08-07 14:34:15 +000088/* ( ) * + , - . / */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089/* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000090/* 0 1 2 3 4 5 6 7 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091/* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000092/* 8 9 : ; < = > ? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093/* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000094/* @ A B C D E F G */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095/* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
Jack Jansen72781191995-08-07 14:34:15 +000096/* H I J K L M N O */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097/* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +000098/* P Q R S T U V W */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099/*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000100/* X Y Z [ \ ] ^ _ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101/*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000102/* ` a b c d e f g */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103/*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000104/* h i j k l m n o */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105/*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000106/* p q r s t u v w */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107/*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000108/* x y z { | } ~ ^? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109/*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
110/*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
111 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
112 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
113 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
114 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
115 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
116 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
117 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
118 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
119 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
120 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
121 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
122 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
123 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
124 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
125 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
Jack Jansen72781191995-08-07 14:34:15 +0000126};
127
128static unsigned char table_b2a_hqx[] =
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000129"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
Jack Jansen72781191995-08-07 14:34:15 +0000130
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000131static char table_a2b_base64[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 -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,-1, -1,-1,-1,-1,
134 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
135 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
136 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
137 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
138 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
139 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000140};
141
142#define BASE64_PAD '='
Guido van Rossum355bc0c2001-10-30 03:00:52 +0000143
144/* Max binary chunk size; limited only by available memory */
Guido van Rossum0e225aa2007-05-22 20:24:57 +0000145#define BASE64_MAXBIN ((PY_SSIZE_T_MAX - 3) / 2)
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000146
147static unsigned char table_b2a_base64[] =
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000148"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000149
150
151
Jack Jansen72781191995-08-07 14:34:15 +0000152static unsigned short crctab_hqx[256] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
154 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
155 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
156 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
157 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
158 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
159 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
160 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
161 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
162 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
163 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
164 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
165 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
166 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
167 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
168 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
169 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
170 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
171 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
172 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
173 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
174 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
175 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
176 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
177 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
178 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
179 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
180 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
181 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
182 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
183 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
184 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
Jack Jansen72781191995-08-07 14:34:15 +0000185};
186
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200187/*[clinic input]
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200188module binascii
189[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300190/*[clinic end generated code: output=da39a3ee5e6b4b0d input=de89fb46bcaf3fec]*/
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, "
Berker Peksag3cd30c22015-02-15 00:31:00 +0200231 "not '%.100s'", Py_TYPE(arg)->tp_name);
Antoine Pitrou08316762011-12-20 13:58:41 +0100232 return 0;
233 }
234 if (!PyBuffer_IsContiguous(buf, 'C')) {
235 PyErr_Format(PyExc_TypeError,
236 "argument should be a contiguous buffer, "
Berker Peksag3cd30c22015-02-15 00:31:00 +0200237 "not '%.100s'", Py_TYPE(arg)->tp_name);
Antoine Pitrou08316762011-12-20 13:58:41 +0100238 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;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200349 Py_ssize_t bin_len, out_len;
350 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000351
Victor Stinnereaaaf132015-10-13 10:51:47 +0200352 _PyBytesWriter_Init(&writer);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200353 bin_data = data->buf;
354 bin_len = data->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 if ( bin_len > 45 ) {
356 /* The 45 is a limit that appears in all uuencode's */
357 PyErr_SetString(Error, "At most 45 bytes at once");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 return NULL;
359 }
Jack Jansen72781191995-08-07 14:34:15 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* We're lazy and allocate to much (fixed up later) */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200362 out_len = 2 + (bin_len + 2) / 3 * 4;
363 ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
364 if (ascii_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 /* Store the length */
368 *ascii_data++ = ' ' + (bin_len & 077);
Tim Peters934c1a12002-07-02 22:24:50 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
371 /* Shift the data (or padding) into our buffer */
372 if ( bin_len > 0 ) /* Data */
373 leftchar = (leftchar << 8) | *bin_data;
374 else /* Padding */
375 leftchar <<= 8;
376 leftbits += 8;
Jack Jansen72781191995-08-07 14:34:15 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 /* See if there are 6-bit groups ready */
379 while ( leftbits >= 6 ) {
380 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
381 leftbits -= 6;
382 *ascii_data++ = this_ch + ' ';
383 }
384 }
385 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000386
Victor Stinnereaaaf132015-10-13 10:51:47 +0200387 return _PyBytesWriter_Finish(&writer, ascii_data);
Jack Jansen72781191995-08-07 14:34:15 +0000388}
389
Guido van Rossum2db4f471999-10-19 19:05:14 +0000390
391static int
Thomas Woutersf98db652006-03-01 21:37:32 +0000392binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num)
Guido van Rossum2db4f471999-10-19 19:05:14 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 /* Finds & returns the (num+1)th
395 ** valid character for base64, or -1 if none.
396 */
Guido van Rossum2db4f471999-10-19 19:05:14 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 int ret = -1;
399 unsigned char c, b64val;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 while ((slen > 0) && (ret == -1)) {
402 c = *s;
403 b64val = table_a2b_base64[c & 0x7f];
404 if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
405 if (num == 0)
406 ret = *s;
407 num--;
408 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 s++;
411 slen--;
412 }
413 return ret;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000414}
415
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200416/*[clinic input]
417binascii.a2b_base64
418
Serhiy Storchaka12785612014-01-25 11:49:49 +0200419 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200420 /
421
422Decode a line of base64 data.
423[clinic start generated code]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000424
425static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200426binascii_a2b_base64_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800427/*[clinic end generated code: output=3e351b702bed56d2 input=5872acf6e1cac243]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 unsigned char *ascii_data, *bin_data;
430 int leftbits = 0;
431 unsigned char this_ch;
432 unsigned int leftchar = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 Py_ssize_t ascii_len, bin_len;
434 int quad_pos = 0;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200435 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000436
Serhiy Storchaka12785612014-01-25 11:49:49 +0200437 ascii_data = data->buf;
438 ascii_len = data->len;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 assert(ascii_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000441
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200442 if (ascii_len > PY_SSIZE_T_MAX - 3)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000446
Victor Stinnereaaaf132015-10-13 10:51:47 +0200447 _PyBytesWriter_Init(&writer);
448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* Allocate the buffer */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200450 bin_data = _PyBytesWriter_Alloc(&writer, bin_len);
451 if (bin_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 return NULL;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 for( ; ascii_len > 0; ascii_len--, ascii_data++) {
455 this_ch = *ascii_data;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (this_ch > 0x7f ||
458 this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
459 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* Check for pad sequences and ignore
462 ** the invalid ones.
463 */
464 if (this_ch == BASE64_PAD) {
465 if ( (quad_pos < 2) ||
466 ((quad_pos == 2) &&
467 (binascii_find_valid(ascii_data, ascii_len, 1)
468 != BASE64_PAD)) )
469 {
470 continue;
471 }
472 else {
473 /* A pad sequence means no more input.
474 ** We've already interpreted the data
475 ** from the quad at this point.
476 */
477 leftbits = 0;
478 break;
479 }
480 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 this_ch = table_a2b_base64[*ascii_data];
483 if ( this_ch == (unsigned char) -1 )
484 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 /*
487 ** Shift it in on the low end, and see if there's
488 ** a byte ready for output.
489 */
490 quad_pos = (quad_pos + 1) & 0x03;
491 leftchar = (leftchar << 6) | (this_ch);
492 leftbits += 6;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 if ( leftbits >= 8 ) {
495 leftbits -= 8;
496 *bin_data++ = (leftchar >> leftbits) & 0xff;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 leftchar &= ((1 << leftbits) - 1);
498 }
499 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (leftbits != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyErr_SetString(Error, "Incorrect padding");
Victor Stinnereaaaf132015-10-13 10:51:47 +0200503 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return NULL;
505 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000506
Victor Stinnereaaaf132015-10-13 10:51:47 +0200507 return _PyBytesWriter_Finish(&writer, bin_data);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000508}
509
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200510
511/*[clinic input]
512binascii.b2a_base64
513
514 data: Py_buffer
Victor Stinnere84c9762015-10-11 11:01:02 +0200515 *
516 newline: int(c_default="1") = True
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200517
518Base64-code line of data.
519[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000520
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000521static PyObject *
Victor Stinnere84c9762015-10-11 11:01:02 +0200522binascii_b2a_base64_impl(PyModuleDef *module, Py_buffer *data, int newline)
523/*[clinic end generated code: output=19e1dd719a890b50 input=7b2ea6fa38d8924c]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 unsigned char *ascii_data, *bin_data;
526 int leftbits = 0;
527 unsigned char this_ch;
528 unsigned int leftchar = 0;
Victor Stinnere84c9762015-10-11 11:01:02 +0200529 Py_ssize_t bin_len, out_len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200530 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000531
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200532 bin_data = data->buf;
533 bin_len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200534 _PyBytesWriter_Init(&writer);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 assert(bin_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if ( bin_len > BASE64_MAXBIN ) {
539 PyErr_SetString(Error, "Too much data for base64 line");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return NULL;
541 }
Tim Peters934c1a12002-07-02 22:24:50 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* We're lazy and allocate too much (fixed up later).
Victor Stinnere84c9762015-10-11 11:01:02 +0200544 "+2" leaves room for up to two pad characters.
545 Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
546 out_len = bin_len*2 + 2;
547 if (newline)
548 out_len++;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200549 ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
550 if (ascii_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 return NULL;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
554 /* Shift the data into our buffer */
555 leftchar = (leftchar << 8) | *bin_data;
556 leftbits += 8;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 /* See if there are 6-bit groups ready */
559 while ( leftbits >= 6 ) {
560 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
561 leftbits -= 6;
562 *ascii_data++ = table_b2a_base64[this_ch];
563 }
564 }
565 if ( leftbits == 2 ) {
566 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
567 *ascii_data++ = BASE64_PAD;
568 *ascii_data++ = BASE64_PAD;
569 } else if ( leftbits == 4 ) {
570 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
571 *ascii_data++ = BASE64_PAD;
572 }
Victor Stinnere84c9762015-10-11 11:01:02 +0200573 if (newline)
574 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000575
Victor Stinnereaaaf132015-10-13 10:51:47 +0200576 return _PyBytesWriter_Finish(&writer, ascii_data);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000577}
578
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200579/*[clinic input]
580binascii.a2b_hqx
581
Serhiy Storchaka12785612014-01-25 11:49:49 +0200582 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200583 /
584
585Decode .hqx coding.
586[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000587
588static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +0200589binascii_a2b_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800590/*[clinic end generated code: output=60bcdbbd28b105cd input=0d914c680e0eed55]*/
Jack Jansen72781191995-08-07 14:34:15 +0000591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 unsigned char *ascii_data, *bin_data;
593 int leftbits = 0;
594 unsigned char this_ch;
595 unsigned int leftchar = 0;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200596 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_ssize_t len;
598 int done = 0;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200599 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000600
Serhiy Storchaka12785612014-01-25 11:49:49 +0200601 ascii_data = data->buf;
602 len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200603 _PyBytesWriter_Init(&writer);
Jack Jansen72781191995-08-07 14:34:15 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000606
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200607 if (len > PY_SSIZE_T_MAX - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 /* Allocate a string that is too big (fixed later)
611 Add two to the initial length to prevent interning which
612 would preclude subsequent resizing. */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200613 bin_data = _PyBytesWriter_Alloc(&writer, len + 2);
614 if (bin_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 for( ; len > 0 ; len--, ascii_data++ ) {
618 /* Get the byte and look it up */
619 this_ch = table_a2b_hqx[*ascii_data];
620 if ( this_ch == SKIP )
621 continue;
622 if ( this_ch == FAIL ) {
623 PyErr_SetString(Error, "Illegal char");
Victor Stinnereaaaf132015-10-13 10:51:47 +0200624 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 return NULL;
626 }
627 if ( this_ch == DONE ) {
628 /* The terminating colon */
629 done = 1;
630 break;
631 }
Jack Jansen72781191995-08-07 14:34:15 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* Shift it into the buffer and see if any bytes are ready */
634 leftchar = (leftchar << 6) | (this_ch);
635 leftbits += 6;
636 if ( leftbits >= 8 ) {
637 leftbits -= 8;
638 *bin_data++ = (leftchar >> leftbits) & 0xff;
639 leftchar &= ((1 << leftbits) - 1);
640 }
641 }
Tim Peters934c1a12002-07-02 22:24:50 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if ( leftbits && !done ) {
644 PyErr_SetString(Incomplete,
645 "String has incomplete number of bytes");
Victor Stinnereaaaf132015-10-13 10:51:47 +0200646 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 return NULL;
648 }
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000649
Victor Stinnereaaaf132015-10-13 10:51:47 +0200650 res = _PyBytesWriter_Finish(&writer, bin_data);
651 if (res == NULL)
652 return NULL;
653 return Py_BuildValue("Ni", res, done);
Jack Jansen72781191995-08-07 14:34:15 +0000654}
655
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200656
657/*[clinic input]
658binascii.rlecode_hqx
659
660 data: Py_buffer
661 /
662
663Binhex RLE-code binary data.
664[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000665
666static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200667binascii_rlecode_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800668/*[clinic end generated code: output=0905da344dbf0648 input=e1f1712447a82b09]*/
Jack Jansen72781191995-08-07 14:34:15 +0000669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 unsigned char *in_data, *out_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 unsigned char ch;
672 Py_ssize_t in, inend, len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200673 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000674
Victor Stinnereaaaf132015-10-13 10:51:47 +0200675 _PyBytesWriter_Init(&writer);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200676 in_data = data->buf;
677 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000680
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200681 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 /* Worst case: output is twice as big as input (fixed later) */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200685 out_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
686 if (out_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 return NULL;
Tim Peters934c1a12002-07-02 22:24:50 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 for( in=0; in<len; in++) {
690 ch = in_data[in];
691 if ( ch == RUNCHAR ) {
692 /* RUNCHAR. Escape it. */
693 *out_data++ = RUNCHAR;
694 *out_data++ = 0;
695 } else {
696 /* Check how many following are the same */
697 for(inend=in+1;
698 inend<len && in_data[inend] == ch &&
699 inend < in+255;
700 inend++) ;
701 if ( inend - in > 3 ) {
702 /* More than 3 in a row. Output RLE. */
703 *out_data++ = ch;
704 *out_data++ = RUNCHAR;
Antoine Pitrou40455752010-08-15 18:51:10 +0000705 *out_data++ = (unsigned char) (inend-in);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 in = inend-1;
707 } else {
708 /* Less than 3. Output the byte itself */
709 *out_data++ = ch;
710 }
711 }
712 }
Victor Stinnereaaaf132015-10-13 10:51:47 +0200713
714 return _PyBytesWriter_Finish(&writer, out_data);
Jack Jansen72781191995-08-07 14:34:15 +0000715}
716
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200717
718/*[clinic input]
719binascii.b2a_hqx
720
721 data: Py_buffer
722 /
723
724Encode .hqx data.
725[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000726
Jack Jansen72781191995-08-07 14:34:15 +0000727static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200728binascii_b2a_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800729/*[clinic end generated code: output=5a987810d5e3cdbb input=9596ebe019fe12ba]*/
Jack Jansen72781191995-08-07 14:34:15 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 unsigned char *ascii_data, *bin_data;
732 int leftbits = 0;
733 unsigned char this_ch;
734 unsigned int leftchar = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 Py_ssize_t len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200736 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000737
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200738 bin_data = data->buf;
739 len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200740 _PyBytesWriter_Init(&writer);
Jack Jansen72781191995-08-07 14:34:15 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000743
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200744 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 /* Allocate a buffer that is at least large enough */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200748 ascii_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
749 if (ascii_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 return NULL;
Tim Peters934c1a12002-07-02 22:24:50 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 for( ; len > 0 ; len--, bin_data++ ) {
753 /* Shift into our buffer, and output any 6bits ready */
754 leftchar = (leftchar << 8) | *bin_data;
755 leftbits += 8;
756 while ( leftbits >= 6 ) {
757 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
758 leftbits -= 6;
759 *ascii_data++ = table_b2a_hqx[this_ch];
760 }
761 }
762 /* Output a possible runt byte */
763 if ( leftbits ) {
764 leftchar <<= (6-leftbits);
765 *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
766 }
Victor Stinnereaaaf132015-10-13 10:51:47 +0200767
768 return _PyBytesWriter_Finish(&writer, ascii_data);
Jack Jansen72781191995-08-07 14:34:15 +0000769}
770
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200771
772/*[clinic input]
773binascii.rledecode_hqx
774
775 data: Py_buffer
776 /
777
778Decode hexbin RLE-coded string.
779[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000780
Jack Jansen72781191995-08-07 14:34:15 +0000781static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200782binascii_rledecode_hqx_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800783/*[clinic end generated code: output=f7afd89b789946ab input=54cdd49fc014402c]*/
Jack Jansen72781191995-08-07 14:34:15 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 unsigned char *in_data, *out_data;
786 unsigned char in_byte, in_repeat;
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200787 Py_ssize_t in_len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200788 _PyBytesWriter writer;
Jack Jansen72781191995-08-07 14:34:15 +0000789
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200790 in_data = data->buf;
791 in_len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200792 _PyBytesWriter_Init(&writer);
Jack Jansen72781191995-08-07 14:34:15 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 assert(in_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 /* Empty string is a special case */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200797 if ( in_len == 0 )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 return PyBytes_FromStringAndSize("", 0);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200799 else if (in_len > PY_SSIZE_T_MAX / 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return PyErr_NoMemory();
Jack Jansen72781191995-08-07 14:34:15 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 /* Allocate a buffer of reasonable size. Resized when needed */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200803 out_data = _PyBytesWriter_Alloc(&writer, in_len);
Victor Stinnereaaaf132015-10-13 10:51:47 +0200804 if (out_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 return NULL;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200806
807 /* Use overallocation */
808 writer.overallocate = 1;
Jack Jansen72781191995-08-07 14:34:15 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 /*
811 ** We need two macros here to get/put bytes and handle
812 ** end-of-buffer for input and output strings.
813 */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200814#define INBYTE(b) \
815 do { \
816 if ( --in_len < 0 ) { \
817 PyErr_SetString(Incomplete, ""); \
818 goto error; \
819 } \
820 b = *in_data++; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 } while(0)
Tim Peters934c1a12002-07-02 22:24:50 +0000822
Victor Stinnereaaaf132015-10-13 10:51:47 +0200823 /*
824 ** Handle first byte separately (since we have to get angry
825 ** in case of an orphaned RLE code).
826 */
827 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (in_byte == RUNCHAR) {
830 INBYTE(in_repeat);
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200831 /* only 1 byte will be written, but 2 bytes were preallocated:
832 substract 1 byte to prevent overallocation */
833 writer.min_size--;
834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if (in_repeat != 0) {
836 /* Note Error, not Incomplete (which is at the end
837 ** of the string only). This is a programmer error.
838 */
839 PyErr_SetString(Error, "Orphaned RLE code at start");
Victor Stinnereaaaf132015-10-13 10:51:47 +0200840 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 }
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200842 *out_data++ = RUNCHAR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 } else {
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200844 *out_data++ = in_byte;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 }
Tim Peters934c1a12002-07-02 22:24:50 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 while( in_len > 0 ) {
848 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 if (in_byte == RUNCHAR) {
851 INBYTE(in_repeat);
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200852 /* only 1 byte will be written, but 2 bytes were preallocated:
853 substract 1 byte to prevent overallocation */
854 writer.min_size--;
855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if ( in_repeat == 0 ) {
857 /* Just an escaped RUNCHAR value */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200858 *out_data++ = RUNCHAR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 } else {
860 /* Pick up value and output a sequence of it */
861 in_byte = out_data[-1];
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200862
863 /* enlarge the buffer if needed */
864 if (in_repeat > 1) {
865 /* -1 because we already preallocated 1 byte */
866 out_data = _PyBytesWriter_Prepare(&writer, out_data,
867 in_repeat - 1);
868 if (out_data == NULL)
869 goto error;
870 }
871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 while ( --in_repeat > 0 )
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200873 *out_data++ = in_byte;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 }
875 } else {
876 /* Normal byte */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200877 *out_data++ = in_byte;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
879 }
Victor Stinnereaaaf132015-10-13 10:51:47 +0200880 return _PyBytesWriter_Finish(&writer, out_data);
881
882error:
883 _PyBytesWriter_Dealloc(&writer);
884 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000885}
886
Jack Jansen72781191995-08-07 14:34:15 +0000887
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200888/*[clinic input]
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300889binascii.crc_hqx -> unsigned_int
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200890
891 data: Py_buffer
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300892 crc: unsigned_int(bitwise=True)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200893 /
894
895Compute hqx CRC incrementally.
896[clinic start generated code]*/
897
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300898static unsigned int
899binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc)
900/*[clinic end generated code: output=167c2dac62625717 input=add8c53712ccceda]*/
Jack Jansen72781191995-08-07 14:34:15 +0000901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000904
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300905 crc &= 0xffff;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200906 bin_data = data->buf;
907 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 while(len-- > 0) {
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300910 crc = ((crc<<8)&0xff00) ^ crctab_hqx[(crc>>8)^*bin_data++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
Jack Jansen72781191995-08-07 14:34:15 +0000912
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300913 return crc;
Jack Jansen72781191995-08-07 14:34:15 +0000914}
915
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200916#ifndef USE_ZLIB_CRC32
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000917/* Crc - 32 BIT ANSI X3.66 CRC checksum files
918 Also known as: ISO 3307
919**********************************************************************|
920* *|
921* Demonstration program to compute the 32-bit CRC used as the frame *|
922* check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *|
923* and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *|
924* protocol). The 32-bit FCS was added via the Federal Register, *|
925* 1 June 1982, p.23798. I presume but don't know for certain that *|
926* this polynomial is or will be included in CCITT V.41, which *|
927* defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *|
928* PUB 78 says that the 32-bit FCS reduces otherwise undetected *|
929* errors by a factor of 10^-5 over 16-bit FCS. *|
930* *|
931**********************************************************************|
932
933 Copyright (C) 1986 Gary S. Brown. You may use this program, or
934 code or tables extracted from it, as desired without restriction.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000935
Tim Peters934c1a12002-07-02 22:24:50 +0000936 First, the polynomial itself and its table of feedback terms. The
937 polynomial is
938 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
939 Note that we take it "backwards" and put the highest-order term in
940 the lowest-order bit. The X^32 term is "implied"; the LSB is the
941 X^31 term, etc. The X^0 term (usually shown as "+1") results in
942 the MSB being 1.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000943
Tim Peters934c1a12002-07-02 22:24:50 +0000944 Note that the usual hardware shift register implementation, which
945 is what we're using (we're merely optimizing it by doing eight-bit
946 chunks at a time) shifts bits into the lowest-order term. In our
947 implementation, that means shifting towards the right. Why do we
948 do it this way? Because the calculated CRC must be transmitted in
949 order from highest-order term to lowest-order term. UARTs transmit
950 characters in order from LSB to MSB. By storing the CRC this way,
951 we hand it to the UART in the order low-byte to high-byte; the UART
952 sends each low-bit to hight-bit; and the result is transmission bit
953 by bit from highest- to lowest-order term without requiring any bit
954 shuffling on our part. Reception works similarly.
955
956 The feedback terms table consists of 256, 32-bit entries. Notes:
957
958 1. The table can be generated at runtime if desired; code to do so
959 is shown later. It might not be obvious, but the feedback
960 terms simply represent the results of eight shift/xor opera-
961 tions for all combinations of data and CRC register values.
962
963 2. The CRC accumulation logic is the same for all CRC polynomials,
964 be they sixteen or thirty-two bits wide. You simply choose the
965 appropriate table. Alternatively, because the table can be
966 generated at runtime, you can start by generating the table for
967 the polynomial in question and use exactly the same "updcrc",
968 if your application needn't simultaneously handle two CRC
969 polynomials. (Note, however, that XMODEM is strange.)
970
971 3. For 16-bit CRCs, the table entries need be only 16 bits wide;
972 of course, 32-bit entries work OK if the high 16 bits are zero.
973
974 4. The values must be right-shifted by eight bits by the "updcrc"
975 logic; the shift must be unsigned (bring in zeroes). On some
976 hardware you could probably optimize the shift in assembler by
977 using byte-swap instructions.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000978********************************************************************/
979
Gregory P. Smith3c0e4d22008-03-25 07:51:12 +0000980static unsigned int crc_32_tab[256] = {
9810x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
9820x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
9830xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
9840x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
9850x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
9860x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
9870xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
9880xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
9890x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
9900x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
9910xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
9920xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
9930x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
9940x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
9950x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
9960xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
9970x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
9980x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
9990x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
10000xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
10010x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
10020x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
10030xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
10040xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
10050x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
10060x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
10070x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
10080x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
10090xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
10100x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
10110x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
10120x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
10130xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
10140xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
10150x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
10160x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
10170xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
10180xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
10190x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
10200x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
10210x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
10220xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
10230x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
10240x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
10250x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
10260xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
10270x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
10280x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
10290xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
10300xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
10310x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
10320x2d02ef8dU
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001033};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001034#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001035
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001036/*[clinic input]
1037binascii.crc32 -> unsigned_int
1038
1039 data: Py_buffer
1040 crc: unsigned_int(bitwise=True) = 0
1041 /
1042
1043Compute CRC-32 incrementally.
1044[clinic start generated code]*/
1045
1046static unsigned int
1047binascii_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc)
Larry Hastings581ee362014-01-28 05:00:08 -08001048/*[clinic end generated code: output=620a961643393c4f input=bbe340bc99d25aa8]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001049
1050#ifdef USE_ZLIB_CRC32
1051/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
1052{
1053 Byte *buf;
1054 Py_ssize_t len;
1055 int signed_val;
1056
1057 buf = (Byte*)data->buf;
1058 len = data->len;
1059 signed_val = crc32(crc, buf, len);
1060 return (unsigned int)signed_val & 0xffffffffU;
1061}
1062#else /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001063{ /* By Jim Ahlstrom; All rights transferred to CNRI */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 Py_ssize_t len;
1066 unsigned int result;
Tim Peters934c1a12002-07-02 22:24:50 +00001067
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001068 bin_data = data->buf;
1069 len = data->len;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 crc = ~ crc;
1072 while (len-- > 0) {
1073 crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
1074 /* Note: (crc >> 8) MUST zero fill on left */
1075 }
Tim Petersa98011c2002-07-02 20:20:08 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 result = (crc ^ 0xFFFFFFFF);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001078 return result & 0xffffffff;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001079}
Christian Heimes1dc54002008-03-24 02:19:29 +00001080#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001081
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001082/*[clinic input]
1083binascii.b2a_hex
1084
1085 data: Py_buffer
1086 /
1087
1088Hexadecimal representation of binary data.
1089
1090The return value is a bytes object. This function is also
1091available as "hexlify()".
1092[clinic start generated code]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001093
1094static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001095binascii_b2a_hex_impl(PyModuleDef *module, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -08001096/*[clinic end generated code: output=179318922c2f8fda input=96423cfa299ff3b1]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001097{
Gregory P. Smith9c6b9162015-04-26 00:42:13 +00001098 return _Py_strhex_bytes((const char *)data->buf, data->len);
Barry Warsawe977c212000-08-15 06:07:13 +00001099}
1100
Zachary Wareb176d402015-01-20 13:59:46 -06001101/*[clinic input]
1102binascii.hexlify = binascii.b2a_hex
1103
1104Hexadecimal representation of binary data.
1105
1106The return value is a bytes object.
1107[clinic start generated code]*/
1108
1109static PyObject *
1110binascii_hexlify_impl(PyModuleDef *module, Py_buffer *data)
1111/*[clinic end generated code: output=6098440091fb61dc input=2e3afae7f083f061]*/
1112{
Gregory P. Smith9c6b9162015-04-26 00:42:13 +00001113 return _Py_strhex_bytes((const char *)data->buf, data->len);
Zachary Wareb176d402015-01-20 13:59:46 -06001114}
Barry Warsawe977c212000-08-15 06:07:13 +00001115
1116static int
Tim Peters934c1a12002-07-02 22:24:50 +00001117to_int(int c)
Barry Warsawe977c212000-08-15 06:07:13 +00001118{
Antoine Pitrou4de74572013-02-09 23:11:27 +01001119 if (Py_ISDIGIT(c))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 return c - '0';
1121 else {
Antoine Pitroued8ba142011-10-04 13:50:21 +02001122 if (Py_ISUPPER(c))
1123 c = Py_TOLOWER(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (c >= 'a' && c <= 'f')
1125 return c - 'a' + 10;
1126 }
1127 return -1;
Barry Warsawe977c212000-08-15 06:07:13 +00001128}
1129
1130
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001131/*[clinic input]
1132binascii.a2b_hex
1133
1134 hexstr: ascii_buffer
1135 /
1136
1137Binary data of hexadecimal representation.
1138
1139hexstr must contain an even number of hex digits (upper or lower case).
1140This function is also available as "unhexlify()".
1141[clinic start generated code]*/
1142
Barry Warsawe977c212000-08-15 06:07:13 +00001143static PyObject *
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001144binascii_a2b_hex_impl(PyModuleDef *module, Py_buffer *hexstr)
Larry Hastings581ee362014-01-28 05:00:08 -08001145/*[clinic end generated code: output=d61da452b5c6d290 input=9e1e7f2f94db24fd]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 char* argbuf;
1148 Py_ssize_t arglen;
1149 PyObject *retval;
1150 char* retbuf;
1151 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001152
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001153 argbuf = hexstr->buf;
1154 arglen = hexstr->len;
Barry Warsawe977c212000-08-15 06:07:13 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 assert(arglen >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 /* XXX What should we do about strings with an odd length? Should
1159 * we add an implicit leading zero, or a trailing zero? For now,
1160 * raise an exception.
1161 */
1162 if (arglen % 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 PyErr_SetString(Error, "Odd-length string");
1164 return NULL;
1165 }
Barry Warsawe977c212000-08-15 06:07:13 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001168 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 for (i=j=0; i < arglen; i += 2) {
1173 int top = to_int(Py_CHARMASK(argbuf[i]));
1174 int bot = to_int(Py_CHARMASK(argbuf[i+1]));
1175 if (top == -1 || bot == -1) {
1176 PyErr_SetString(Error,
1177 "Non-hexadecimal digit found");
1178 goto finally;
1179 }
1180 retbuf[j++] = (top << 4) + bot;
1181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return retval;
Barry Warsawe977c212000-08-15 06:07:13 +00001183
1184 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 Py_DECREF(retval);
1186 return NULL;
Barry Warsawe977c212000-08-15 06:07:13 +00001187}
1188
Zachary Wareb176d402015-01-20 13:59:46 -06001189/*[clinic input]
1190binascii.unhexlify = binascii.a2b_hex
1191
1192Binary data of hexadecimal representation.
1193
1194hexstr must contain an even number of hex digits (upper or lower case).
1195[clinic start generated code]*/
1196
1197static PyObject *
1198binascii_unhexlify_impl(PyModuleDef *module, Py_buffer *hexstr)
1199/*[clinic end generated code: output=17cec7544499803e input=dd8c012725f462da]*/
1200{
1201 return binascii_a2b_hex_impl(module, hexstr);
1202}
1203
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001204static int table_hex[128] = {
1205 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1206 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1207 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1208 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1209 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1210 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1211 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1212 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1213};
1214
1215#define hexval(c) table_hex[(unsigned int)(c)]
1216
1217#define MAXLINESIZE 76
1218
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001219
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001220/*[clinic input]
1221binascii.a2b_qp
1222
Serhiy Storchaka12785612014-01-25 11:49:49 +02001223 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001224 header: int(c_default="0") = False
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001225
1226Decode a string of qp-encoded data.
1227[clinic start generated code]*/
1228
1229static PyObject *
Serhiy Storchaka12785612014-01-25 11:49:49 +02001230binascii_a2b_qp_impl(PyModuleDef *module, Py_buffer *data, int header)
Larry Hastings581ee362014-01-28 05:00:08 -08001231/*[clinic end generated code: output=a44ef88270352114 input=5187a0d3d8e54f3b]*/
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Py_ssize_t in, out;
1234 char ch;
Serhiy Storchaka12785612014-01-25 11:49:49 +02001235 unsigned char *ascii_data, *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 Py_ssize_t datalen = 0;
1237 PyObject *rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001238
Serhiy Storchaka12785612014-01-25 11:49:49 +02001239 ascii_data = data->buf;
1240 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 /* We allocate the output same size as input, this is overkill.
1243 * The previous implementation used calloc() so we'll zero out the
1244 * memory here too, since PyMem_Malloc() does not guarantee that.
1245 */
1246 odata = (unsigned char *) PyMem_Malloc(datalen);
1247 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 PyErr_NoMemory();
1249 return NULL;
1250 }
1251 memset(odata, 0, datalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 in = out = 0;
1254 while (in < datalen) {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001255 if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 in++;
1257 if (in >= datalen) break;
1258 /* Soft line breaks */
Serhiy Storchaka12785612014-01-25 11:49:49 +02001259 if ((ascii_data[in] == '\n') || (ascii_data[in] == '\r')) {
1260 if (ascii_data[in] != '\n') {
1261 while (in < datalen && ascii_data[in] != '\n') in++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 }
1263 if (in < datalen) in++;
1264 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001265 else if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 /* broken case from broken python qp */
1267 odata[out++] = '=';
1268 in++;
1269 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001270 else if (((ascii_data[in] >= 'A' && ascii_data[in] <= 'F') ||
1271 (ascii_data[in] >= 'a' && ascii_data[in] <= 'f') ||
1272 (ascii_data[in] >= '0' && ascii_data[in] <= '9')) &&
1273 ((ascii_data[in+1] >= 'A' && ascii_data[in+1] <= 'F') ||
1274 (ascii_data[in+1] >= 'a' && ascii_data[in+1] <= 'f') ||
1275 (ascii_data[in+1] >= '0' && ascii_data[in+1] <= '9'))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 /* hexval */
Serhiy Storchaka12785612014-01-25 11:49:49 +02001277 ch = hexval(ascii_data[in]) << 4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 in++;
Serhiy Storchaka12785612014-01-25 11:49:49 +02001279 ch |= hexval(ascii_data[in]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 in++;
1281 odata[out++] = ch;
1282 }
1283 else {
1284 odata[out++] = '=';
1285 }
1286 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001287 else if (header && ascii_data[in] == '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 odata[out++] = ' ';
1289 in++;
1290 }
1291 else {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001292 odata[out] = ascii_data[in];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 in++;
1294 out++;
1295 }
1296 }
1297 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 PyMem_Free(odata);
1299 return NULL;
1300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 PyMem_Free(odata);
1302 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001303}
1304
Tim Peters934c1a12002-07-02 22:24:50 +00001305static int
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001306to_hex (unsigned char ch, unsigned char *s)
1307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 unsigned int uvalue = ch;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 s[1] = "0123456789ABCDEF"[uvalue % 16];
1311 uvalue = (uvalue / 16);
1312 s[0] = "0123456789ABCDEF"[uvalue % 16];
1313 return 0;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001314}
1315
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001316/* XXX: This is ridiculously complicated to be backward compatible
1317 * (mostly) with the quopri module. It doesn't re-create the quopri
1318 * module bug where text ending in CRLF has the CR encoded */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001319
1320/*[clinic input]
1321binascii.b2a_qp
1322
1323 data: Py_buffer
1324 quotetabs: int(c_default="0") = False
1325 istext: int(c_default="1") = True
1326 header: int(c_default="0") = False
1327
1328Encode a string using quoted-printable encoding.
1329
1330On encoding, when istext is set, newlines are not encoded, and white
1331space at end of lines is. When istext is not set, \r and \n (CR/LF)
1332are both encoded. When quotetabs is set, space and tabs are encoded.
1333[clinic start generated code]*/
1334
1335static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001336binascii_b2a_qp_impl(PyModuleDef *module, Py_buffer *data, int quotetabs,
1337 int istext, int header)
1338/*[clinic end generated code: output=a87ca9ccb94e2a9f input=7f2a9aaa008e92b2]*/
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 Py_ssize_t in, out;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001341 unsigned char *databuf, *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 Py_ssize_t datalen = 0, odatalen = 0;
1343 PyObject *rv;
1344 unsigned int linelen = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 unsigned char ch;
1346 int crlf = 0;
1347 unsigned char *p;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001348
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001349 databuf = data->buf;
1350 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 /* See if this string is using CRLF line ends */
1353 /* XXX: this function has the side effect of converting all of
1354 * the end of lines to be the same depending on this detection
1355 * here */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001356 p = (unsigned char *) memchr(databuf, '\n', datalen);
1357 if ((p != NULL) && (p > databuf) && (*(p-1) == '\r'))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 crlf = 1;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 /* First, scan to see how many characters need to be encoded */
1361 in = 0;
1362 while (in < datalen) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001363 if ((databuf[in] > 126) ||
1364 (databuf[in] == '=') ||
1365 (header && databuf[in] == '_') ||
1366 ((databuf[in] == '.') && (linelen == 0) &&
1367 (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1368 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1369 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1370 ((databuf[in] < 33) &&
1371 (databuf[in] != '\r') && (databuf[in] != '\n') &&
1372 (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' ')))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 {
1374 if ((linelen + 3) >= MAXLINESIZE) {
1375 linelen = 0;
1376 if (crlf)
1377 odatalen += 3;
1378 else
1379 odatalen += 2;
1380 }
1381 linelen += 3;
1382 odatalen += 3;
1383 in++;
1384 }
1385 else {
1386 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001387 ((databuf[in] == '\n') ||
1388 ((in+1 < datalen) && (databuf[in] == '\r') &&
1389 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 {
1391 linelen = 0;
1392 /* Protect against whitespace on end of line */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001393 if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 odatalen += 2;
1395 if (crlf)
1396 odatalen += 2;
1397 else
1398 odatalen += 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001399 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 in += 2;
1401 else
1402 in++;
1403 }
1404 else {
1405 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001406 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 (linelen + 1) >= MAXLINESIZE) {
1408 linelen = 0;
1409 if (crlf)
1410 odatalen += 3;
1411 else
1412 odatalen += 2;
1413 }
1414 linelen++;
1415 odatalen++;
1416 in++;
1417 }
1418 }
1419 }
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 /* We allocate the output same size as input, this is overkill.
1422 * The previous implementation used calloc() so we'll zero out the
1423 * memory here too, since PyMem_Malloc() does not guarantee that.
1424 */
1425 odata = (unsigned char *) PyMem_Malloc(odatalen);
1426 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 PyErr_NoMemory();
1428 return NULL;
1429 }
1430 memset(odata, 0, odatalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 in = out = linelen = 0;
1433 while (in < datalen) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001434 if ((databuf[in] > 126) ||
1435 (databuf[in] == '=') ||
1436 (header && databuf[in] == '_') ||
1437 ((databuf[in] == '.') && (linelen == 0) &&
1438 (databuf[in+1] == '\n' || databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1439 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1440 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1441 ((databuf[in] < 33) &&
1442 (databuf[in] != '\r') && (databuf[in] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 (quotetabs ||
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001444 (!quotetabs && ((databuf[in] != '\t') && (databuf[in] != ' '))))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 {
1446 if ((linelen + 3 )>= MAXLINESIZE) {
1447 odata[out++] = '=';
1448 if (crlf) odata[out++] = '\r';
1449 odata[out++] = '\n';
1450 linelen = 0;
1451 }
1452 odata[out++] = '=';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001453 to_hex(databuf[in], &odata[out]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 out += 2;
1455 in++;
1456 linelen += 3;
1457 }
1458 else {
1459 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001460 ((databuf[in] == '\n') ||
1461 ((in+1 < datalen) && (databuf[in] == '\r') &&
1462 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 {
1464 linelen = 0;
1465 /* Protect against whitespace on end of line */
1466 if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
1467 ch = odata[out-1];
1468 odata[out-1] = '=';
1469 to_hex(ch, &odata[out]);
1470 out += 2;
1471 }
Tim Peters934c1a12002-07-02 22:24:50 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (crlf) odata[out++] = '\r';
1474 odata[out++] = '\n';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001475 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 in += 2;
1477 else
1478 in++;
1479 }
1480 else {
1481 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001482 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 (linelen + 1) >= MAXLINESIZE) {
1484 odata[out++] = '=';
1485 if (crlf) odata[out++] = '\r';
1486 odata[out++] = '\n';
1487 linelen = 0;
1488 }
1489 linelen++;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001490 if (header && databuf[in] == ' ') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 odata[out++] = '_';
1492 in++;
1493 }
1494 else {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001495 odata[out++] = databuf[in++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 }
1497 }
1498 }
1499 }
1500 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 PyMem_Free(odata);
1502 return NULL;
1503 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 PyMem_Free(odata);
1505 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001506}
Barry Warsawe977c212000-08-15 06:07:13 +00001507
Jack Jansen72781191995-08-07 14:34:15 +00001508/* List of functions defined in the module */
1509
1510static struct PyMethodDef binascii_module_methods[] = {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001511 BINASCII_A2B_UU_METHODDEF
1512 BINASCII_B2A_UU_METHODDEF
1513 BINASCII_A2B_BASE64_METHODDEF
1514 BINASCII_B2A_BASE64_METHODDEF
1515 BINASCII_A2B_HQX_METHODDEF
1516 BINASCII_B2A_HQX_METHODDEF
1517 BINASCII_A2B_HEX_METHODDEF
1518 BINASCII_B2A_HEX_METHODDEF
Zachary Wareb176d402015-01-20 13:59:46 -06001519 BINASCII_HEXLIFY_METHODDEF
1520 BINASCII_UNHEXLIFY_METHODDEF
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001521 BINASCII_RLECODE_HQX_METHODDEF
1522 BINASCII_RLEDECODE_HQX_METHODDEF
1523 BINASCII_CRC_HQX_METHODDEF
1524 BINASCII_CRC32_METHODDEF
1525 BINASCII_A2B_QP_METHODDEF
1526 BINASCII_B2A_QP_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 {NULL, NULL} /* sentinel */
Jack Jansen72781191995-08-07 14:34:15 +00001528};
1529
1530
Martin v. Löwis1a214512008-06-11 05:26:20 +00001531/* Initialization function for the module (*must* be called PyInit_binascii) */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
Jack Jansen72781191995-08-07 14:34:15 +00001533
Martin v. Löwis1a214512008-06-11 05:26:20 +00001534
1535static struct PyModuleDef binasciimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 PyModuleDef_HEAD_INIT,
1537 "binascii",
1538 doc_binascii,
1539 -1,
1540 binascii_module_methods,
1541 NULL,
1542 NULL,
1543 NULL,
1544 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001545};
1546
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001547PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001548PyInit_binascii(void)
Jack Jansen72781191995-08-07 14:34:15 +00001549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 PyObject *m, *d;
Jack Jansen72781191995-08-07 14:34:15 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 /* Create the module and add the functions */
1553 m = PyModule_Create(&binasciimodule);
1554 if (m == NULL)
1555 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 d = PyModule_GetDict(m);
Jack Jansen72781191995-08-07 14:34:15 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
1560 PyDict_SetItemString(d, "Error", Error);
1561 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
1562 PyDict_SetItemString(d, "Incomplete", Incomplete);
1563 if (PyErr_Occurred()) {
1564 Py_DECREF(m);
1565 m = NULL;
1566 }
1567 return m;
Jack Jansen72781191995-08-07 14:34:15 +00001568}