blob: 5667018d6e1bfac85d205e078f5472d9614705cd [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
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020077static const unsigned char table_a2b_hqx[256] = {
Jack Jansen72781191995-08-07 14:34:15 +000078/* ^@ ^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
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200128static const 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
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200131static const 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
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200147static const 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
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200152static const 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 Storchaka1a2b24f2016-07-07 17:35:15 +0300256binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
257/*[clinic end generated code: output=e027f8e0b0598742 input=7cafeaf73df63d1c]*/
Jack Jansen72781191995-08-07 14:34:15 +0000258{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200259 const unsigned char *ascii_data;
260 unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 int leftbits = 0;
262 unsigned char this_ch;
263 unsigned int leftchar = 0;
264 PyObject *rv;
265 Py_ssize_t ascii_len, bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000266
Serhiy Storchaka12785612014-01-25 11:49:49 +0200267 ascii_data = data->buf;
268 ascii_len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 assert(ascii_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* First byte: binary data length (in bytes) */
273 bin_len = (*ascii_data++ - ' ') & 077;
274 ascii_len--;
Jack Jansen72781191995-08-07 14:34:15 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 /* Allocate the buffer */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200277 if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
Tim Peters934c1a12002-07-02 22:24:50 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
282 /* XXX is it really best to add NULs if there's no more data */
283 this_ch = (ascii_len > 0) ? *ascii_data : 0;
284 if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
285 /*
286 ** Whitespace. Assume some spaces got eaten at
287 ** end-of-line. (We check this later)
288 */
289 this_ch = 0;
290 } else {
291 /* Check the character for legality
292 ** The 64 in stead of the expected 63 is because
293 ** there are a few uuencodes out there that use
294 ** '`' as zero instead of space.
295 */
296 if ( this_ch < ' ' || this_ch > (' ' + 64)) {
297 PyErr_SetString(Error, "Illegal char");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 Py_DECREF(rv);
299 return NULL;
300 }
301 this_ch = (this_ch - ' ') & 077;
302 }
303 /*
304 ** Shift it in on the low end, and see if there's
305 ** a byte ready for output.
306 */
307 leftchar = (leftchar << 6) | (this_ch);
308 leftbits += 6;
309 if ( leftbits >= 8 ) {
310 leftbits -= 8;
311 *bin_data++ = (leftchar >> leftbits) & 0xff;
312 leftchar &= ((1 << leftbits) - 1);
313 bin_len--;
314 }
315 }
316 /*
317 ** Finally, check that if there's anything left on the line
318 ** that it's whitespace only.
319 */
320 while( ascii_len-- > 0 ) {
321 this_ch = *ascii_data++;
322 /* Extra '`' may be written as padding in some cases */
323 if ( this_ch != ' ' && this_ch != ' '+64 &&
324 this_ch != '\n' && this_ch != '\r' ) {
325 PyErr_SetString(Error, "Trailing garbage");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 Py_DECREF(rv);
327 return NULL;
328 }
329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return rv;
Jack Jansen72781191995-08-07 14:34:15 +0000331}
332
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200333/*[clinic input]
334binascii.b2a_uu
335
336 data: Py_buffer
337 /
Xiang Zhang13f1f422017-05-03 11:16:21 +0800338 *
339 backtick: bool(accept={int}) = False
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200340
341Uuencode line of data.
342[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000343
Jack Jansen72781191995-08-07 14:34:15 +0000344static PyObject *
Xiang Zhang13f1f422017-05-03 11:16:21 +0800345binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick)
346/*[clinic end generated code: output=b1b99de62d9bbeb8 input=b26bc8d32b6ed2f6]*/
Jack Jansen72781191995-08-07 14:34:15 +0000347{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200348 unsigned char *ascii_data;
349 const unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 int leftbits = 0;
351 unsigned char this_ch;
352 unsigned int leftchar = 0;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200353 Py_ssize_t bin_len, out_len;
354 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000355
Victor Stinnereaaaf132015-10-13 10:51:47 +0200356 _PyBytesWriter_Init(&writer);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200357 bin_data = data->buf;
358 bin_len = data->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if ( bin_len > 45 ) {
360 /* The 45 is a limit that appears in all uuencode's */
361 PyErr_SetString(Error, "At most 45 bytes at once");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return NULL;
363 }
Jack Jansen72781191995-08-07 14:34:15 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 /* We're lazy and allocate to much (fixed up later) */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200366 out_len = 2 + (bin_len + 2) / 3 * 4;
367 ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
368 if (ascii_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 /* Store the length */
Xiang Zhang13f1f422017-05-03 11:16:21 +0800372 if (backtick && !bin_len)
373 *ascii_data++ = '`';
374 else
Segev Finer679b5662017-07-27 01:17:57 +0300375 *ascii_data++ = ' ' + (unsigned char)bin_len;
Tim Peters934c1a12002-07-02 22:24:50 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
378 /* Shift the data (or padding) into our buffer */
379 if ( bin_len > 0 ) /* Data */
380 leftchar = (leftchar << 8) | *bin_data;
381 else /* Padding */
382 leftchar <<= 8;
383 leftbits += 8;
Jack Jansen72781191995-08-07 14:34:15 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* See if there are 6-bit groups ready */
386 while ( leftbits >= 6 ) {
387 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
388 leftbits -= 6;
Xiang Zhang13f1f422017-05-03 11:16:21 +0800389 if (backtick && !this_ch)
390 *ascii_data++ = '`';
391 else
392 *ascii_data++ = this_ch + ' ';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 }
394 }
395 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000396
Victor Stinnereaaaf132015-10-13 10:51:47 +0200397 return _PyBytesWriter_Finish(&writer, ascii_data);
Jack Jansen72781191995-08-07 14:34:15 +0000398}
399
Guido van Rossum2db4f471999-10-19 19:05:14 +0000400
401static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200402binascii_find_valid(const unsigned char *s, Py_ssize_t slen, int num)
Guido van Rossum2db4f471999-10-19 19:05:14 +0000403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 /* Finds & returns the (num+1)th
405 ** valid character for base64, or -1 if none.
406 */
Guido van Rossum2db4f471999-10-19 19:05:14 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 int ret = -1;
409 unsigned char c, b64val;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 while ((slen > 0) && (ret == -1)) {
412 c = *s;
413 b64val = table_a2b_base64[c & 0x7f];
414 if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
415 if (num == 0)
416 ret = *s;
417 num--;
418 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 s++;
421 slen--;
422 }
423 return ret;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000424}
425
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200426/*[clinic input]
427binascii.a2b_base64
428
Serhiy Storchaka12785612014-01-25 11:49:49 +0200429 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200430 /
431
432Decode a line of base64 data.
433[clinic start generated code]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000434
435static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300436binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
437/*[clinic end generated code: output=0628223f19fd3f9b input=5872acf6e1cac243]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000438{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200439 const unsigned char *ascii_data;
440 unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 int leftbits = 0;
442 unsigned char this_ch;
443 unsigned int leftchar = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 Py_ssize_t ascii_len, bin_len;
445 int quad_pos = 0;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200446 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000447
Serhiy Storchaka12785612014-01-25 11:49:49 +0200448 ascii_data = data->buf;
449 ascii_len = data->len;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 assert(ascii_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000452
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200453 if (ascii_len > PY_SSIZE_T_MAX - 3)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000457
Victor Stinnereaaaf132015-10-13 10:51:47 +0200458 _PyBytesWriter_Init(&writer);
459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* Allocate the buffer */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200461 bin_data = _PyBytesWriter_Alloc(&writer, bin_len);
462 if (bin_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 return NULL;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 for( ; ascii_len > 0; ascii_len--, ascii_data++) {
466 this_ch = *ascii_data;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 if (this_ch > 0x7f ||
469 this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
470 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* Check for pad sequences and ignore
473 ** the invalid ones.
474 */
475 if (this_ch == BASE64_PAD) {
476 if ( (quad_pos < 2) ||
477 ((quad_pos == 2) &&
478 (binascii_find_valid(ascii_data, ascii_len, 1)
479 != BASE64_PAD)) )
480 {
481 continue;
482 }
483 else {
484 /* A pad sequence means no more input.
485 ** We've already interpreted the data
486 ** from the quad at this point.
487 */
488 leftbits = 0;
489 break;
490 }
491 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 this_ch = table_a2b_base64[*ascii_data];
494 if ( this_ch == (unsigned char) -1 )
495 continue;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 /*
498 ** Shift it in on the low end, and see if there's
499 ** a byte ready for output.
500 */
501 quad_pos = (quad_pos + 1) & 0x03;
502 leftchar = (leftchar << 6) | (this_ch);
503 leftbits += 6;
Guido van Rossum2db4f471999-10-19 19:05:14 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if ( leftbits >= 8 ) {
506 leftbits -= 8;
507 *bin_data++ = (leftchar >> leftbits) & 0xff;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 leftchar &= ((1 << leftbits) - 1);
509 }
510 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (leftbits != 0) {
Tal Einat1b85c712018-06-10 10:01:50 +0300513 if (leftbits == 6) {
514 /*
515 ** There is exactly one extra valid, non-padding, base64 character.
516 ** This is an invalid length, as there is no possible input that
517 ** could encoded into such a base64 string.
518 */
519 PyErr_SetString(Error,
520 "Invalid base64-encoded string: "
521 "length cannot be 1 more than a multiple of 4");
522 } else {
523 PyErr_SetString(Error, "Incorrect padding");
524 }
Victor Stinnereaaaf132015-10-13 10:51:47 +0200525 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 return NULL;
527 }
Guido van Rossum2db4f471999-10-19 19:05:14 +0000528
Victor Stinnereaaaf132015-10-13 10:51:47 +0200529 return _PyBytesWriter_Finish(&writer, bin_data);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000530}
531
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200532
533/*[clinic input]
534binascii.b2a_base64
535
536 data: Py_buffer
Xiang Zhang1374dbb2017-05-01 13:12:07 +0800537 /
Victor Stinnere84c9762015-10-11 11:01:02 +0200538 *
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200539 newline: bool(accept={int}) = True
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200540
541Base64-code line of data.
542[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000543
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000544static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300545binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
Xiang Zhang1374dbb2017-05-01 13:12:07 +0800546/*[clinic end generated code: output=4ad62c8e8485d3b3 input=6083dac5777fa45d]*/
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000547{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200548 unsigned char *ascii_data;
549 const unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 int leftbits = 0;
551 unsigned char this_ch;
552 unsigned int leftchar = 0;
Victor Stinnere84c9762015-10-11 11:01:02 +0200553 Py_ssize_t bin_len, out_len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200554 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000555
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200556 bin_data = data->buf;
557 bin_len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200558 _PyBytesWriter_Init(&writer);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 assert(bin_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if ( bin_len > BASE64_MAXBIN ) {
563 PyErr_SetString(Error, "Too much data for base64 line");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return NULL;
565 }
Tim Peters934c1a12002-07-02 22:24:50 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 /* We're lazy and allocate too much (fixed up later).
Victor Stinnere84c9762015-10-11 11:01:02 +0200568 "+2" leaves room for up to two pad characters.
569 Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
570 out_len = bin_len*2 + 2;
571 if (newline)
572 out_len++;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200573 ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
574 if (ascii_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 return NULL;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
578 /* Shift the data into our buffer */
579 leftchar = (leftchar << 8) | *bin_data;
580 leftbits += 8;
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* See if there are 6-bit groups ready */
583 while ( leftbits >= 6 ) {
584 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
585 leftbits -= 6;
586 *ascii_data++ = table_b2a_base64[this_ch];
587 }
588 }
589 if ( leftbits == 2 ) {
590 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
591 *ascii_data++ = BASE64_PAD;
592 *ascii_data++ = BASE64_PAD;
593 } else if ( leftbits == 4 ) {
594 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
595 *ascii_data++ = BASE64_PAD;
596 }
Victor Stinnere84c9762015-10-11 11:01:02 +0200597 if (newline)
598 *ascii_data++ = '\n'; /* Append a courtesy newline */
Tim Peters934c1a12002-07-02 22:24:50 +0000599
Victor Stinnereaaaf132015-10-13 10:51:47 +0200600 return _PyBytesWriter_Finish(&writer, ascii_data);
Jack Jansen84bbc2e1995-10-04 16:38:44 +0000601}
602
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200603/*[clinic input]
604binascii.a2b_hqx
605
Serhiy Storchaka12785612014-01-25 11:49:49 +0200606 data: ascii_buffer
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200607 /
608
609Decode .hqx coding.
610[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000611
612static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300613binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data)
614/*[clinic end generated code: output=4d6d8c54d54ea1c1 input=0d914c680e0eed55]*/
Jack Jansen72781191995-08-07 14:34:15 +0000615{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200616 const unsigned char *ascii_data;
617 unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 int leftbits = 0;
619 unsigned char this_ch;
620 unsigned int leftchar = 0;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200621 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 Py_ssize_t len;
623 int done = 0;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200624 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000625
Serhiy Storchaka12785612014-01-25 11:49:49 +0200626 ascii_data = data->buf;
627 len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200628 _PyBytesWriter_Init(&writer);
Jack Jansen72781191995-08-07 14:34:15 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000631
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200632 if (len > PY_SSIZE_T_MAX - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 /* Allocate a string that is too big (fixed later)
636 Add two to the initial length to prevent interning which
637 would preclude subsequent resizing. */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200638 bin_data = _PyBytesWriter_Alloc(&writer, len + 2);
639 if (bin_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 for( ; len > 0 ; len--, ascii_data++ ) {
643 /* Get the byte and look it up */
644 this_ch = table_a2b_hqx[*ascii_data];
645 if ( this_ch == SKIP )
646 continue;
647 if ( this_ch == FAIL ) {
648 PyErr_SetString(Error, "Illegal char");
Victor Stinnereaaaf132015-10-13 10:51:47 +0200649 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return NULL;
651 }
652 if ( this_ch == DONE ) {
653 /* The terminating colon */
654 done = 1;
655 break;
656 }
Jack Jansen72781191995-08-07 14:34:15 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 /* Shift it into the buffer and see if any bytes are ready */
659 leftchar = (leftchar << 6) | (this_ch);
660 leftbits += 6;
661 if ( leftbits >= 8 ) {
662 leftbits -= 8;
663 *bin_data++ = (leftchar >> leftbits) & 0xff;
664 leftchar &= ((1 << leftbits) - 1);
665 }
666 }
Tim Peters934c1a12002-07-02 22:24:50 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if ( leftbits && !done ) {
669 PyErr_SetString(Incomplete,
670 "String has incomplete number of bytes");
Victor Stinnereaaaf132015-10-13 10:51:47 +0200671 _PyBytesWriter_Dealloc(&writer);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 return NULL;
673 }
Roger E. Masse5f4ce181997-01-16 17:10:22 +0000674
Victor Stinnereaaaf132015-10-13 10:51:47 +0200675 res = _PyBytesWriter_Finish(&writer, bin_data);
676 if (res == NULL)
677 return NULL;
678 return Py_BuildValue("Ni", res, done);
Jack Jansen72781191995-08-07 14:34:15 +0000679}
680
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200681
682/*[clinic input]
683binascii.rlecode_hqx
684
685 data: Py_buffer
686 /
687
688Binhex RLE-code binary data.
689[clinic start generated code]*/
Jack Jansen72781191995-08-07 14:34:15 +0000690
691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300692binascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data)
693/*[clinic end generated code: output=393d79338f5f5629 input=e1f1712447a82b09]*/
Jack Jansen72781191995-08-07 14:34:15 +0000694{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200695 const unsigned char *in_data;
696 unsigned char *out_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 unsigned char ch;
698 Py_ssize_t in, inend, len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200699 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000700
Victor Stinnereaaaf132015-10-13 10:51:47 +0200701 _PyBytesWriter_Init(&writer);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200702 in_data = data->buf;
703 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000706
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200707 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 /* Worst case: output is twice as big as input (fixed later) */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200711 out_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
712 if (out_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 return NULL;
Tim Peters934c1a12002-07-02 22:24:50 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 for( in=0; in<len; in++) {
716 ch = in_data[in];
717 if ( ch == RUNCHAR ) {
718 /* RUNCHAR. Escape it. */
719 *out_data++ = RUNCHAR;
720 *out_data++ = 0;
721 } else {
722 /* Check how many following are the same */
723 for(inend=in+1;
724 inend<len && in_data[inend] == ch &&
725 inend < in+255;
726 inend++) ;
727 if ( inend - in > 3 ) {
728 /* More than 3 in a row. Output RLE. */
729 *out_data++ = ch;
730 *out_data++ = RUNCHAR;
Antoine Pitrou40455752010-08-15 18:51:10 +0000731 *out_data++ = (unsigned char) (inend-in);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 in = inend-1;
733 } else {
734 /* Less than 3. Output the byte itself */
735 *out_data++ = ch;
736 }
737 }
738 }
Victor Stinnereaaaf132015-10-13 10:51:47 +0200739
740 return _PyBytesWriter_Finish(&writer, out_data);
Jack Jansen72781191995-08-07 14:34:15 +0000741}
742
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200743
744/*[clinic input]
745binascii.b2a_hqx
746
747 data: Py_buffer
748 /
749
750Encode .hqx data.
751[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000752
Jack Jansen72781191995-08-07 14:34:15 +0000753static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300754binascii_b2a_hqx_impl(PyObject *module, Py_buffer *data)
755/*[clinic end generated code: output=d0aa5a704bc9f7de input=9596ebe019fe12ba]*/
Jack Jansen72781191995-08-07 14:34:15 +0000756{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200757 unsigned char *ascii_data;
758 const unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 int leftbits = 0;
760 unsigned char this_ch;
761 unsigned int leftchar = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 Py_ssize_t len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200763 _PyBytesWriter writer;
Tim Peters934c1a12002-07-02 22:24:50 +0000764
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200765 bin_data = data->buf;
766 len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200767 _PyBytesWriter_Init(&writer);
Jack Jansen72781191995-08-07 14:34:15 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 assert(len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000770
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200771 if (len > PY_SSIZE_T_MAX / 2 - 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 return PyErr_NoMemory();
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 /* Allocate a buffer that is at least large enough */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200775 ascii_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
776 if (ascii_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 return NULL;
Tim Peters934c1a12002-07-02 22:24:50 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 for( ; len > 0 ; len--, bin_data++ ) {
780 /* Shift into our buffer, and output any 6bits ready */
781 leftchar = (leftchar << 8) | *bin_data;
782 leftbits += 8;
783 while ( leftbits >= 6 ) {
784 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
785 leftbits -= 6;
786 *ascii_data++ = table_b2a_hqx[this_ch];
787 }
788 }
789 /* Output a possible runt byte */
790 if ( leftbits ) {
791 leftchar <<= (6-leftbits);
792 *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
793 }
Victor Stinnereaaaf132015-10-13 10:51:47 +0200794
795 return _PyBytesWriter_Finish(&writer, ascii_data);
Jack Jansen72781191995-08-07 14:34:15 +0000796}
797
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200798
799/*[clinic input]
800binascii.rledecode_hqx
801
802 data: Py_buffer
803 /
804
805Decode hexbin RLE-coded string.
806[clinic start generated code]*/
Tim Peters934c1a12002-07-02 22:24:50 +0000807
Jack Jansen72781191995-08-07 14:34:15 +0000808static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300809binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data)
810/*[clinic end generated code: output=9826619565de1c6c input=54cdd49fc014402c]*/
Jack Jansen72781191995-08-07 14:34:15 +0000811{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200812 const unsigned char *in_data;
813 unsigned char *out_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 unsigned char in_byte, in_repeat;
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200815 Py_ssize_t in_len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200816 _PyBytesWriter writer;
Jack Jansen72781191995-08-07 14:34:15 +0000817
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200818 in_data = data->buf;
819 in_len = data->len;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200820 _PyBytesWriter_Init(&writer);
Jack Jansen72781191995-08-07 14:34:15 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 assert(in_len >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 /* Empty string is a special case */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200825 if ( in_len == 0 )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 return PyBytes_FromStringAndSize("", 0);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200827 else if (in_len > PY_SSIZE_T_MAX / 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 return PyErr_NoMemory();
Jack Jansen72781191995-08-07 14:34:15 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 /* Allocate a buffer of reasonable size. Resized when needed */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200831 out_data = _PyBytesWriter_Alloc(&writer, in_len);
Victor Stinnereaaaf132015-10-13 10:51:47 +0200832 if (out_data == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 return NULL;
Victor Stinnereaaaf132015-10-13 10:51:47 +0200834
835 /* Use overallocation */
836 writer.overallocate = 1;
Jack Jansen72781191995-08-07 14:34:15 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 /*
839 ** We need two macros here to get/put bytes and handle
840 ** end-of-buffer for input and output strings.
841 */
Victor Stinnereaaaf132015-10-13 10:51:47 +0200842#define INBYTE(b) \
843 do { \
844 if ( --in_len < 0 ) { \
845 PyErr_SetString(Incomplete, ""); \
846 goto error; \
847 } \
848 b = *in_data++; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 } while(0)
Tim Peters934c1a12002-07-02 22:24:50 +0000850
Victor Stinnereaaaf132015-10-13 10:51:47 +0200851 /*
852 ** Handle first byte separately (since we have to get angry
853 ** in case of an orphaned RLE code).
854 */
855 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (in_byte == RUNCHAR) {
858 INBYTE(in_repeat);
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200859 /* only 1 byte will be written, but 2 bytes were preallocated:
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700860 subtract 1 byte to prevent overallocation */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200861 writer.min_size--;
862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 if (in_repeat != 0) {
864 /* Note Error, not Incomplete (which is at the end
865 ** of the string only). This is a programmer error.
866 */
867 PyErr_SetString(Error, "Orphaned RLE code at start");
Victor Stinnereaaaf132015-10-13 10:51:47 +0200868 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 }
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200870 *out_data++ = RUNCHAR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 } else {
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200872 *out_data++ = in_byte;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 }
Tim Peters934c1a12002-07-02 22:24:50 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 while( in_len > 0 ) {
876 INBYTE(in_byte);
Jack Jansen72781191995-08-07 14:34:15 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (in_byte == RUNCHAR) {
879 INBYTE(in_repeat);
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200880 /* only 1 byte will be written, but 2 bytes were preallocated:
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700881 subtract 1 byte to prevent overallocation */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200882 writer.min_size--;
883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 if ( in_repeat == 0 ) {
885 /* Just an escaped RUNCHAR value */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200886 *out_data++ = RUNCHAR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 } else {
888 /* Pick up value and output a sequence of it */
889 in_byte = out_data[-1];
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200890
891 /* enlarge the buffer if needed */
892 if (in_repeat > 1) {
893 /* -1 because we already preallocated 1 byte */
894 out_data = _PyBytesWriter_Prepare(&writer, out_data,
895 in_repeat - 1);
896 if (out_data == NULL)
897 goto error;
898 }
899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 while ( --in_repeat > 0 )
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200901 *out_data++ = in_byte;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 }
903 } else {
904 /* Normal byte */
Victor Stinnerf9c9a3f2015-10-14 15:20:07 +0200905 *out_data++ = in_byte;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 }
907 }
Victor Stinnereaaaf132015-10-13 10:51:47 +0200908 return _PyBytesWriter_Finish(&writer, out_data);
909
910error:
911 _PyBytesWriter_Dealloc(&writer);
912 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +0000913}
914
Jack Jansen72781191995-08-07 14:34:15 +0000915
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200916/*[clinic input]
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300917binascii.crc_hqx -> unsigned_int
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200918
919 data: Py_buffer
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300920 crc: unsigned_int(bitwise=True)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200921 /
922
Martin Panter3310e142016-12-24 07:36:44 +0000923Compute CRC-CCITT incrementally.
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200924[clinic start generated code]*/
925
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300926static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300927binascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc)
Martin Panter3310e142016-12-24 07:36:44 +0000928/*[clinic end generated code: output=8ec2a78590d19170 input=f18240ff8c705b79]*/
Jack Jansen72781191995-08-07 14:34:15 +0000929{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200930 const unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Py_ssize_t len;
Tim Peters934c1a12002-07-02 22:24:50 +0000932
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300933 crc &= 0xffff;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200934 bin_data = data->buf;
935 len = data->len;
Jack Jansen72781191995-08-07 14:34:15 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 while(len-- > 0) {
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300938 crc = ((crc<<8)&0xff00) ^ crctab_hqx[(crc>>8)^*bin_data++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 }
Jack Jansen72781191995-08-07 14:34:15 +0000940
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300941 return crc;
Jack Jansen72781191995-08-07 14:34:15 +0000942}
943
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200944#ifndef USE_ZLIB_CRC32
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000945/* Crc - 32 BIT ANSI X3.66 CRC checksum files
946 Also known as: ISO 3307
947**********************************************************************|
948* *|
949* Demonstration program to compute the 32-bit CRC used as the frame *|
950* check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *|
951* and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *|
952* protocol). The 32-bit FCS was added via the Federal Register, *|
953* 1 June 1982, p.23798. I presume but don't know for certain that *|
954* this polynomial is or will be included in CCITT V.41, which *|
955* defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *|
956* PUB 78 says that the 32-bit FCS reduces otherwise undetected *|
957* errors by a factor of 10^-5 over 16-bit FCS. *|
958* *|
959**********************************************************************|
960
961 Copyright (C) 1986 Gary S. Brown. You may use this program, or
962 code or tables extracted from it, as desired without restriction.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000963
Tim Peters934c1a12002-07-02 22:24:50 +0000964 First, the polynomial itself and its table of feedback terms. The
965 polynomial is
966 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
967 Note that we take it "backwards" and put the highest-order term in
968 the lowest-order bit. The X^32 term is "implied"; the LSB is the
969 X^31 term, etc. The X^0 term (usually shown as "+1") results in
970 the MSB being 1.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +0000971
Tim Peters934c1a12002-07-02 22:24:50 +0000972 Note that the usual hardware shift register implementation, which
973 is what we're using (we're merely optimizing it by doing eight-bit
974 chunks at a time) shifts bits into the lowest-order term. In our
975 implementation, that means shifting towards the right. Why do we
976 do it this way? Because the calculated CRC must be transmitted in
977 order from highest-order term to lowest-order term. UARTs transmit
978 characters in order from LSB to MSB. By storing the CRC this way,
979 we hand it to the UART in the order low-byte to high-byte; the UART
980 sends each low-bit to hight-bit; and the result is transmission bit
981 by bit from highest- to lowest-order term without requiring any bit
982 shuffling on our part. Reception works similarly.
983
984 The feedback terms table consists of 256, 32-bit entries. Notes:
985
986 1. The table can be generated at runtime if desired; code to do so
987 is shown later. It might not be obvious, but the feedback
988 terms simply represent the results of eight shift/xor opera-
989 tions for all combinations of data and CRC register values.
990
991 2. The CRC accumulation logic is the same for all CRC polynomials,
992 be they sixteen or thirty-two bits wide. You simply choose the
993 appropriate table. Alternatively, because the table can be
994 generated at runtime, you can start by generating the table for
995 the polynomial in question and use exactly the same "updcrc",
996 if your application needn't simultaneously handle two CRC
997 polynomials. (Note, however, that XMODEM is strange.)
998
999 3. For 16-bit CRCs, the table entries need be only 16 bits wide;
1000 of course, 32-bit entries work OK if the high 16 bits are zero.
1001
1002 4. The values must be right-shifted by eight bits by the "updcrc"
1003 logic; the shift must be unsigned (bring in zeroes). On some
1004 hardware you could probably optimize the shift in assembler by
1005 using byte-swap instructions.
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001006********************************************************************/
1007
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001008static const unsigned int crc_32_tab[256] = {
Gregory P. Smith3c0e4d22008-03-25 07:51:12 +000010090x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
10100x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
10110xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
10120x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
10130x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
10140x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
10150xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
10160xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
10170x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
10180x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
10190xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
10200xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
10210x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
10220x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
10230x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
10240xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
10250x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
10260x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
10270x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
10280xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
10290x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
10300x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
10310xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
10320xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
10330x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
10340x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
10350x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
10360x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
10370xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
10380x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
10390x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
10400x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
10410xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
10420xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
10430x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
10440x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
10450xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
10460xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
10470x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
10480x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
10490x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
10500xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
10510x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
10520x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
10530x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
10540xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
10550x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
10560x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
10570xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
10580xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
10590x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
10600x2d02ef8dU
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001061};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001062#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001063
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001064/*[clinic input]
1065binascii.crc32 -> unsigned_int
1066
1067 data: Py_buffer
1068 crc: unsigned_int(bitwise=True) = 0
1069 /
1070
1071Compute CRC-32 incrementally.
1072[clinic start generated code]*/
1073
1074static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001075binascii_crc32_impl(PyObject *module, Py_buffer *data, unsigned int crc)
1076/*[clinic end generated code: output=52cf59056a78593b input=bbe340bc99d25aa8]*/
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001077
1078#ifdef USE_ZLIB_CRC32
1079/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
1080{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001081 const Byte *buf;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001082 Py_ssize_t len;
1083 int signed_val;
1084
1085 buf = (Byte*)data->buf;
1086 len = data->len;
1087 signed_val = crc32(crc, buf, len);
1088 return (unsigned int)signed_val & 0xffffffffU;
1089}
1090#else /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001091{ /* By Jim Ahlstrom; All rights transferred to CNRI */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001092 const unsigned char *bin_data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 Py_ssize_t len;
1094 unsigned int result;
Tim Peters934c1a12002-07-02 22:24:50 +00001095
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001096 bin_data = data->buf;
1097 len = data->len;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 crc = ~ crc;
1100 while (len-- > 0) {
1101 crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
1102 /* Note: (crc >> 8) MUST zero fill on left */
1103 }
Tim Petersa98011c2002-07-02 20:20:08 +00001104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 result = (crc ^ 0xFFFFFFFF);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001106 return result & 0xffffffff;
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001107}
Christian Heimes1dc54002008-03-24 02:19:29 +00001108#endif /* USE_ZLIB_CRC32 */
Guido van Rossum7d47c9e2000-02-16 21:11:52 +00001109
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001110/*[clinic input]
1111binascii.b2a_hex
1112
1113 data: Py_buffer
1114 /
1115
1116Hexadecimal representation of binary data.
1117
1118The return value is a bytes object. This function is also
1119available as "hexlify()".
1120[clinic start generated code]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001121
1122static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001123binascii_b2a_hex_impl(PyObject *module, Py_buffer *data)
1124/*[clinic end generated code: output=92fec1a95c9897a0 input=96423cfa299ff3b1]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001125{
Gregory P. Smith9c6b9162015-04-26 00:42:13 +00001126 return _Py_strhex_bytes((const char *)data->buf, data->len);
Barry Warsawe977c212000-08-15 06:07:13 +00001127}
1128
Zachary Wareb176d402015-01-20 13:59:46 -06001129/*[clinic input]
1130binascii.hexlify = binascii.b2a_hex
1131
1132Hexadecimal representation of binary data.
1133
1134The return value is a bytes object.
1135[clinic start generated code]*/
1136
1137static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001138binascii_hexlify_impl(PyObject *module, Py_buffer *data)
1139/*[clinic end generated code: output=749e95e53c14880c input=2e3afae7f083f061]*/
Zachary Wareb176d402015-01-20 13:59:46 -06001140{
Gregory P. Smith9c6b9162015-04-26 00:42:13 +00001141 return _Py_strhex_bytes((const char *)data->buf, data->len);
Zachary Wareb176d402015-01-20 13:59:46 -06001142}
Barry Warsawe977c212000-08-15 06:07:13 +00001143
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001144/*[clinic input]
1145binascii.a2b_hex
1146
1147 hexstr: ascii_buffer
1148 /
1149
1150Binary data of hexadecimal representation.
1151
1152hexstr must contain an even number of hex digits (upper or lower case).
1153This function is also available as "unhexlify()".
1154[clinic start generated code]*/
1155
Barry Warsawe977c212000-08-15 06:07:13 +00001156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001157binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
1158/*[clinic end generated code: output=0cc1a139af0eeecb input=9e1e7f2f94db24fd]*/
Barry Warsawe977c212000-08-15 06:07:13 +00001159{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001160 const char* argbuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 Py_ssize_t arglen;
1162 PyObject *retval;
1163 char* retbuf;
1164 Py_ssize_t i, j;
Barry Warsawe977c212000-08-15 06:07:13 +00001165
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001166 argbuf = hexstr->buf;
1167 arglen = hexstr->len;
Barry Warsawe977c212000-08-15 06:07:13 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 assert(arglen >= 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 /* XXX What should we do about strings with an odd length? Should
1172 * we add an implicit leading zero, or a trailing zero? For now,
1173 * raise an exception.
1174 */
1175 if (arglen % 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 PyErr_SetString(Error, "Odd-length string");
1177 return NULL;
1178 }
Barry Warsawe977c212000-08-15 06:07:13 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001181 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 retbuf = PyBytes_AS_STRING(retval);
Barry Warsawe977c212000-08-15 06:07:13 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 for (i=j=0; i < arglen; i += 2) {
Sergey Fedoseev6b5df902018-02-27 01:35:41 +05001186 unsigned int top = _PyLong_DigitValue[Py_CHARMASK(argbuf[i])];
1187 unsigned int bot = _PyLong_DigitValue[Py_CHARMASK(argbuf[i+1])];
1188 if (top >= 16 || bot >= 16) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 PyErr_SetString(Error,
1190 "Non-hexadecimal digit found");
1191 goto finally;
1192 }
1193 retbuf[j++] = (top << 4) + bot;
1194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 return retval;
Barry Warsawe977c212000-08-15 06:07:13 +00001196
1197 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 Py_DECREF(retval);
1199 return NULL;
Barry Warsawe977c212000-08-15 06:07:13 +00001200}
1201
Zachary Wareb176d402015-01-20 13:59:46 -06001202/*[clinic input]
1203binascii.unhexlify = binascii.a2b_hex
1204
1205Binary data of hexadecimal representation.
1206
1207hexstr must contain an even number of hex digits (upper or lower case).
1208[clinic start generated code]*/
1209
1210static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001211binascii_unhexlify_impl(PyObject *module, Py_buffer *hexstr)
1212/*[clinic end generated code: output=51a64c06c79629e3 input=dd8c012725f462da]*/
Zachary Wareb176d402015-01-20 13:59:46 -06001213{
1214 return binascii_a2b_hex_impl(module, hexstr);
1215}
1216
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001217#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 Storchaka202fda52017-03-12 10:10:47 +02001224 header: bool(accept={int}) = 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 Storchaka1a2b24f2016-07-07 17:35:15 +03001230binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001231/*[clinic end generated code: output=e99f7846cfb9bc53 input=bf6766fea76cce8f]*/
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 Storchakaef1585e2015-12-25 20:01:53 +02001235 const unsigned char *ascii_data;
1236 unsigned char *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 Py_ssize_t datalen = 0;
1238 PyObject *rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001239
Serhiy Storchaka12785612014-01-25 11:49:49 +02001240 ascii_data = data->buf;
1241 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 /* We allocate the output same size as input, this is overkill.
1244 * The previous implementation used calloc() so we'll zero out the
1245 * memory here too, since PyMem_Malloc() does not guarantee that.
1246 */
1247 odata = (unsigned char *) PyMem_Malloc(datalen);
1248 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 PyErr_NoMemory();
1250 return NULL;
1251 }
1252 memset(odata, 0, datalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 in = out = 0;
1255 while (in < datalen) {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001256 if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 in++;
1258 if (in >= datalen) break;
1259 /* Soft line breaks */
Serhiy Storchaka12785612014-01-25 11:49:49 +02001260 if ((ascii_data[in] == '\n') || (ascii_data[in] == '\r')) {
1261 if (ascii_data[in] != '\n') {
1262 while (in < datalen && ascii_data[in] != '\n') in++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 }
1264 if (in < datalen) in++;
1265 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001266 else if (ascii_data[in] == '=') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 /* broken case from broken python qp */
1268 odata[out++] = '=';
1269 in++;
1270 }
Serhiy Storchakae6265e92016-09-14 16:34:37 +03001271 else if ((in + 1 < datalen) &&
1272 ((ascii_data[in] >= 'A' && ascii_data[in] <= 'F') ||
Serhiy Storchaka12785612014-01-25 11:49:49 +02001273 (ascii_data[in] >= 'a' && ascii_data[in] <= 'f') ||
1274 (ascii_data[in] >= '0' && ascii_data[in] <= '9')) &&
1275 ((ascii_data[in+1] >= 'A' && ascii_data[in+1] <= 'F') ||
1276 (ascii_data[in+1] >= 'a' && ascii_data[in+1] <= 'f') ||
1277 (ascii_data[in+1] >= '0' && ascii_data[in+1] <= '9'))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 /* hexval */
Sergey Fedoseev6b5df902018-02-27 01:35:41 +05001279 ch = _PyLong_DigitValue[ascii_data[in]] << 4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 in++;
Sergey Fedoseev6b5df902018-02-27 01:35:41 +05001281 ch |= _PyLong_DigitValue[ascii_data[in]];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 in++;
1283 odata[out++] = ch;
1284 }
1285 else {
1286 odata[out++] = '=';
1287 }
1288 }
Serhiy Storchaka12785612014-01-25 11:49:49 +02001289 else if (header && ascii_data[in] == '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 odata[out++] = ' ';
1291 in++;
1292 }
1293 else {
Serhiy Storchaka12785612014-01-25 11:49:49 +02001294 odata[out] = ascii_data[in];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 in++;
1296 out++;
1297 }
1298 }
1299 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 PyMem_Free(odata);
1301 return NULL;
1302 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 PyMem_Free(odata);
1304 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001305}
1306
Tim Peters934c1a12002-07-02 22:24:50 +00001307static int
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001308to_hex (unsigned char ch, unsigned char *s)
1309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 unsigned int uvalue = ch;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 s[1] = "0123456789ABCDEF"[uvalue % 16];
1313 uvalue = (uvalue / 16);
1314 s[0] = "0123456789ABCDEF"[uvalue % 16];
1315 return 0;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001316}
1317
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001318/* XXX: This is ridiculously complicated to be backward compatible
1319 * (mostly) with the quopri module. It doesn't re-create the quopri
1320 * module bug where text ending in CRLF has the CR encoded */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001321
1322/*[clinic input]
1323binascii.b2a_qp
1324
1325 data: Py_buffer
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001326 quotetabs: bool(accept={int}) = False
1327 istext: bool(accept={int}) = True
1328 header: bool(accept={int}) = False
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001329
1330Encode a string using quoted-printable encoding.
1331
1332On encoding, when istext is set, newlines are not encoded, and white
1333space at end of lines is. When istext is not set, \r and \n (CR/LF)
1334are both encoded. When quotetabs is set, space and tabs are encoded.
1335[clinic start generated code]*/
1336
1337static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001338binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
Larry Hastings89964c42015-04-14 18:07:59 -04001339 int istext, int header)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001340/*[clinic end generated code: output=e9884472ebb1a94c input=21fb7eea4a184ba6]*/
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 Py_ssize_t in, out;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001343 const unsigned char *databuf;
1344 unsigned char *odata;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 Py_ssize_t datalen = 0, odatalen = 0;
1346 PyObject *rv;
1347 unsigned int linelen = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 unsigned char ch;
1349 int crlf = 0;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001350 const unsigned char *p;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001351
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001352 databuf = data->buf;
1353 datalen = data->len;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 /* See if this string is using CRLF line ends */
1356 /* XXX: this function has the side effect of converting all of
1357 * the end of lines to be the same depending on this detection
1358 * here */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001359 p = (const unsigned char *) memchr(databuf, '\n', datalen);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001360 if ((p != NULL) && (p > databuf) && (*(p-1) == '\r'))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 crlf = 1;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* First, scan to see how many characters need to be encoded */
1364 in = 0;
1365 while (in < datalen) {
Benjamin Peterson4f976512016-08-13 18:33:33 -07001366 Py_ssize_t delta = 0;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001367 if ((databuf[in] > 126) ||
1368 (databuf[in] == '=') ||
1369 (header && databuf[in] == '_') ||
1370 ((databuf[in] == '.') && (linelen == 0) &&
Serhiy Storchakae6265e92016-09-14 16:34:37 +03001371 (in + 1 == datalen || databuf[in+1] == '\n' ||
1372 databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001373 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1374 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1375 ((databuf[in] < 33) &&
1376 (databuf[in] != '\r') && (databuf[in] != '\n') &&
1377 (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' ')))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 {
1379 if ((linelen + 3) >= MAXLINESIZE) {
1380 linelen = 0;
1381 if (crlf)
Benjamin Peterson4f976512016-08-13 18:33:33 -07001382 delta += 3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 else
Benjamin Peterson4f976512016-08-13 18:33:33 -07001384 delta += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
1386 linelen += 3;
Benjamin Peterson4f976512016-08-13 18:33:33 -07001387 delta += 3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 in++;
1389 }
1390 else {
1391 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001392 ((databuf[in] == '\n') ||
1393 ((in+1 < datalen) && (databuf[in] == '\r') &&
1394 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 {
1396 linelen = 0;
1397 /* Protect against whitespace on end of line */
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001398 if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
Benjamin Peterson4f976512016-08-13 18:33:33 -07001399 delta += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if (crlf)
Benjamin Peterson4f976512016-08-13 18:33:33 -07001401 delta += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 else
Benjamin Peterson4f976512016-08-13 18:33:33 -07001403 delta += 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001404 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 in += 2;
1406 else
1407 in++;
1408 }
1409 else {
1410 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001411 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 (linelen + 1) >= MAXLINESIZE) {
1413 linelen = 0;
1414 if (crlf)
Benjamin Peterson4f976512016-08-13 18:33:33 -07001415 delta += 3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 else
Benjamin Peterson4f976512016-08-13 18:33:33 -07001417 delta += 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 }
1419 linelen++;
Benjamin Peterson4f976512016-08-13 18:33:33 -07001420 delta++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 in++;
1422 }
1423 }
Benjamin Peterson4f976512016-08-13 18:33:33 -07001424 if (PY_SSIZE_T_MAX - delta < odatalen) {
Benjamin Peterson4f976512016-08-13 18:33:33 -07001425 PyErr_NoMemory();
1426 return NULL;
1427 }
1428 odatalen += delta;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 }
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 /* We allocate the output same size as input, this is overkill.
1432 * The previous implementation used calloc() so we'll zero out the
1433 * memory here too, since PyMem_Malloc() does not guarantee that.
1434 */
1435 odata = (unsigned char *) PyMem_Malloc(odatalen);
1436 if (odata == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 PyErr_NoMemory();
1438 return NULL;
1439 }
1440 memset(odata, 0, odatalen);
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 in = out = linelen = 0;
1443 while (in < datalen) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001444 if ((databuf[in] > 126) ||
1445 (databuf[in] == '=') ||
1446 (header && databuf[in] == '_') ||
1447 ((databuf[in] == '.') && (linelen == 0) &&
Serhiy Storchakae6265e92016-09-14 16:34:37 +03001448 (in + 1 == datalen || databuf[in+1] == '\n' ||
1449 databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001450 (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1451 ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1452 ((databuf[in] < 33) &&
1453 (databuf[in] != '\r') && (databuf[in] != '\n') &&
Serhiy Storchakae6265e92016-09-14 16:34:37 +03001454 (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' ')))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 {
1456 if ((linelen + 3 )>= MAXLINESIZE) {
1457 odata[out++] = '=';
1458 if (crlf) odata[out++] = '\r';
1459 odata[out++] = '\n';
1460 linelen = 0;
1461 }
1462 odata[out++] = '=';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001463 to_hex(databuf[in], &odata[out]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 out += 2;
1465 in++;
1466 linelen += 3;
1467 }
1468 else {
1469 if (istext &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001470 ((databuf[in] == '\n') ||
1471 ((in+1 < datalen) && (databuf[in] == '\r') &&
1472 (databuf[in+1] == '\n'))))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 {
1474 linelen = 0;
1475 /* Protect against whitespace on end of line */
1476 if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
1477 ch = odata[out-1];
1478 odata[out-1] = '=';
1479 to_hex(ch, &odata[out]);
1480 out += 2;
1481 }
Tim Peters934c1a12002-07-02 22:24:50 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (crlf) odata[out++] = '\r';
1484 odata[out++] = '\n';
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001485 if (databuf[in] == '\r')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 in += 2;
1487 else
1488 in++;
1489 }
1490 else {
1491 if ((in + 1 != datalen) &&
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001492 (databuf[in+1] != '\n') &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 (linelen + 1) >= MAXLINESIZE) {
1494 odata[out++] = '=';
1495 if (crlf) odata[out++] = '\r';
1496 odata[out++] = '\n';
1497 linelen = 0;
1498 }
1499 linelen++;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001500 if (header && databuf[in] == ' ') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 odata[out++] = '_';
1502 in++;
1503 }
1504 else {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001505 odata[out++] = databuf[in++];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 }
1507 }
1508 }
1509 }
1510 if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 PyMem_Free(odata);
1512 return NULL;
1513 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 PyMem_Free(odata);
1515 return rv;
Martin v. Löwis16dc7f42001-09-30 20:32:11 +00001516}
Barry Warsawe977c212000-08-15 06:07:13 +00001517
Jack Jansen72781191995-08-07 14:34:15 +00001518/* List of functions defined in the module */
1519
1520static struct PyMethodDef binascii_module_methods[] = {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001521 BINASCII_A2B_UU_METHODDEF
1522 BINASCII_B2A_UU_METHODDEF
1523 BINASCII_A2B_BASE64_METHODDEF
1524 BINASCII_B2A_BASE64_METHODDEF
1525 BINASCII_A2B_HQX_METHODDEF
1526 BINASCII_B2A_HQX_METHODDEF
1527 BINASCII_A2B_HEX_METHODDEF
1528 BINASCII_B2A_HEX_METHODDEF
Zachary Wareb176d402015-01-20 13:59:46 -06001529 BINASCII_HEXLIFY_METHODDEF
1530 BINASCII_UNHEXLIFY_METHODDEF
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001531 BINASCII_RLECODE_HQX_METHODDEF
1532 BINASCII_RLEDECODE_HQX_METHODDEF
1533 BINASCII_CRC_HQX_METHODDEF
1534 BINASCII_CRC32_METHODDEF
1535 BINASCII_A2B_QP_METHODDEF
1536 BINASCII_B2A_QP_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 {NULL, NULL} /* sentinel */
Jack Jansen72781191995-08-07 14:34:15 +00001538};
1539
1540
Martin v. Löwis1a214512008-06-11 05:26:20 +00001541/* Initialization function for the module (*must* be called PyInit_binascii) */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001542PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
Jack Jansen72781191995-08-07 14:34:15 +00001543
Martin v. Löwis1a214512008-06-11 05:26:20 +00001544
1545static struct PyModuleDef binasciimodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 PyModuleDef_HEAD_INIT,
1547 "binascii",
1548 doc_binascii,
1549 -1,
1550 binascii_module_methods,
1551 NULL,
1552 NULL,
1553 NULL,
1554 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001555};
1556
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001557PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001558PyInit_binascii(void)
Jack Jansen72781191995-08-07 14:34:15 +00001559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 PyObject *m, *d;
Jack Jansen72781191995-08-07 14:34:15 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 /* Create the module and add the functions */
1563 m = PyModule_Create(&binasciimodule);
1564 if (m == NULL)
1565 return NULL;
Jack Jansen72781191995-08-07 14:34:15 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 d = PyModule_GetDict(m);
Jack Jansen72781191995-08-07 14:34:15 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
1570 PyDict_SetItemString(d, "Error", Error);
1571 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
1572 PyDict_SetItemString(d, "Incomplete", Incomplete);
1573 if (PyErr_Occurred()) {
1574 Py_DECREF(m);
1575 m = NULL;
1576 }
1577 return m;
Jack Jansen72781191995-08-07 14:34:15 +00001578}