ยปCore Development>Code coverage>Modules/binascii.c

Python code coverage for Modules/binascii.c

#countcontent
1n/a/*
2n/a** Routines to represent binary data in ASCII and vice-versa
3n/a**
4n/a** This module currently supports the following encodings:
5n/a** uuencode:
6n/a** each line encodes 45 bytes (except possibly the last)
7n/a** First char encodes (binary) length, rest data
8n/a** each char encodes 6 bits, as follows:
9n/a** binary: 01234567 abcdefgh ijklmnop
10n/a** ascii: 012345 67abcd efghij klmnop
11n/a** ASCII encoding method is "excess-space": 000000 is encoded as ' ', etc.
12n/a** short binary data is zero-extended (so the bits are always in the
13n/a** right place), this does *not* reflect in the length.
14n/a** base64:
15n/a** Line breaks are insignificant, but lines are at most 76 chars
16n/a** each char encodes 6 bits, in similar order as uucode/hqx. Encoding
17n/a** is done via a table.
18n/a** Short binary data is filled (in ASCII) with '='.
19n/a** hqx:
20n/a** File starts with introductory text, real data starts and ends
21n/a** with colons.
22n/a** Data consists of three similar parts: info, datafork, resourcefork.
23n/a** Each part is protected (at the end) with a 16-bit crc
24n/a** The binary data is run-length encoded, and then ascii-fied:
25n/a** binary: 01234567 abcdefgh ijklmnop
26n/a** ascii: 012345 67abcd efghij klmnop
27n/a** ASCII encoding is table-driven, see the code.
28n/a** Short binary data results in the runt ascii-byte being output with
29n/a** the bits in the right place.
30n/a**
31n/a** While I was reading dozens of programs that encode or decode the formats
32n/a** here (documentation? hihi:-) I have formulated Jansen's Observation:
33n/a**
34n/a** Programs that encode binary data in ASCII are written in
35n/a** such a style that they are as unreadable as possible. Devices used
36n/a** include unnecessary global variables, burying important tables
37n/a** in unrelated sourcefiles, putting functions in include files,
38n/a** using seemingly-descriptive variable names for different purposes,
39n/a** calls to empty subroutines and a host of others.
40n/a**
41n/a** I have attempted to break with this tradition, but I guess that that
42n/a** does make the performance sub-optimal. Oh well, too bad...
43n/a**
44n/a** Jack Jansen, CWI, July 1995.
45n/a**
46n/a** Added support for quoted-printable encoding, based on rfc 1521 et al
47n/a** quoted-printable encoding specifies that non printable characters (anything
48n/a** below 32 and above 126) be encoded as =XX where XX is the hexadecimal value
49n/a** of the character. It also specifies some other behavior to enable 8bit data
50n/a** in a mail message with little difficulty (maximum line sizes, protecting
51n/a** some cases of whitespace, etc).
52n/a**
53n/a** Brandon Long, September 2001.
54n/a*/
55n/a
56n/a#define PY_SSIZE_T_CLEAN
57n/a
58n/a#include "Python.h"
59n/a#include "pystrhex.h"
60n/a#ifdef USE_ZLIB_CRC32
61n/a#include "zlib.h"
62n/a#endif
63n/a
64n/astatic PyObject *Error;
65n/astatic PyObject *Incomplete;
66n/a
67n/a/*
68n/a** hqx lookup table, ascii->binary.
69n/a*/
70n/a
71n/a#define RUNCHAR 0x90
72n/a
73n/a#define DONE 0x7F
74n/a#define SKIP 0x7E
75n/a#define FAIL 0x7D
76n/a
77n/astatic const unsigned char table_a2b_hqx[256] = {
78n/a/* ^@ ^A ^B ^C ^D ^E ^F ^G */
79n/a/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
80n/a/* \b \t \n ^K ^L \r ^N ^O */
81n/a/* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
82n/a/* ^P ^Q ^R ^S ^T ^U ^V ^W */
83n/a/* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
84n/a/* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
85n/a/* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
86n/a/* ! " # $ % & ' */
87n/a/* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
88n/a/* ( ) * + , - . / */
89n/a/* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
90n/a/* 0 1 2 3 4 5 6 7 */
91n/a/* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
92n/a/* 8 9 : ; < = > ? */
93n/a/* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
94n/a/* @ A B C D E F G */
95n/a/* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
96n/a/* H I J K L M N O */
97n/a/* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
98n/a/* P Q R S T U V W */
99n/a/*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
100n/a/* X Y Z [ \ ] ^ _ */
101n/a/*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
102n/a/* ` a b c d e f g */
103n/a/*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
104n/a/* h i j k l m n o */
105n/a/*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
106n/a/* p q r s t u v w */
107n/a/*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
108n/a/* x y z { | } ~ ^? */
109n/a/*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
110n/a/*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
111n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
112n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
113n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
114n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
115n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
116n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
117n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
118n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
119n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
120n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
121n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
122n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
123n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
124n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
125n/a FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
126n/a};
127n/a
128n/astatic const unsigned char table_b2a_hqx[] =
129n/a"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
130n/a
131n/astatic const char table_a2b_base64[] = {
132n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
133n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
134n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
135n/a 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
136n/a -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
137n/a 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
138n/a -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
139n/a 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
140n/a};
141n/a
142n/a#define BASE64_PAD '='
143n/a
144n/a/* Max binary chunk size; limited only by available memory */
145n/a#define BASE64_MAXBIN ((PY_SSIZE_T_MAX - 3) / 2)
146n/a
147n/astatic const unsigned char table_b2a_base64[] =
148n/a"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
149n/a
150n/a
151n/a
152n/astatic const unsigned short crctab_hqx[256] = {
153n/a 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
154n/a 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
155n/a 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
156n/a 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
157n/a 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
158n/a 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
159n/a 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
160n/a 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
161n/a 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
162n/a 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
163n/a 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
164n/a 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
165n/a 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
166n/a 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
167n/a 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
168n/a 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
169n/a 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
170n/a 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
171n/a 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
172n/a 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
173n/a 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
174n/a 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
175n/a 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
176n/a 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
177n/a 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
178n/a 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
179n/a 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
180n/a 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
181n/a 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
182n/a 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
183n/a 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
184n/a 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
185n/a};
186n/a
187n/a/*[clinic input]
188n/amodule binascii
189n/a[clinic start generated code]*/
190n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=de89fb46bcaf3fec]*/
191n/a
192n/a/*[python input]
193n/a
194n/aclass ascii_buffer_converter(CConverter):
195n/a type = 'Py_buffer'
196n/a converter = 'ascii_buffer_converter'
197n/a impl_by_reference = True
198n/a c_default = "{NULL, NULL}"
199n/a
200n/a def cleanup(self):
201n/a name = self.name
202n/a return "".join(["if (", name, ".obj)\n PyBuffer_Release(&", name, ");\n"])
203n/a
204n/a[python start generated code]*/
205n/a/*[python end generated code: output=da39a3ee5e6b4b0d input=3eb7b63610da92cd]*/
206n/a
207n/astatic int
208n/aascii_buffer_converter(PyObject *arg, Py_buffer *buf)
209n/a{
210n/a if (arg == NULL) {
211n/a PyBuffer_Release(buf);
212n/a return 1;
213n/a }
214n/a if (PyUnicode_Check(arg)) {
215n/a if (PyUnicode_READY(arg) < 0)
216n/a return 0;
217n/a if (!PyUnicode_IS_ASCII(arg)) {
218n/a PyErr_SetString(PyExc_ValueError,
219n/a "string argument should contain only ASCII characters");
220n/a return 0;
221n/a }
222n/a assert(PyUnicode_KIND(arg) == PyUnicode_1BYTE_KIND);
223n/a buf->buf = (void *) PyUnicode_1BYTE_DATA(arg);
224n/a buf->len = PyUnicode_GET_LENGTH(arg);
225n/a buf->obj = NULL;
226n/a return 1;
227n/a }
228n/a if (PyObject_GetBuffer(arg, buf, PyBUF_SIMPLE) != 0) {
229n/a PyErr_Format(PyExc_TypeError,
230n/a "argument should be bytes, buffer or ASCII string, "
231n/a "not '%.100s'", Py_TYPE(arg)->tp_name);
232n/a return 0;
233n/a }
234n/a if (!PyBuffer_IsContiguous(buf, 'C')) {
235n/a PyErr_Format(PyExc_TypeError,
236n/a "argument should be a contiguous buffer, "
237n/a "not '%.100s'", Py_TYPE(arg)->tp_name);
238n/a PyBuffer_Release(buf);
239n/a return 0;
240n/a }
241n/a return Py_CLEANUP_SUPPORTED;
242n/a}
243n/a
244n/a#include "clinic/binascii.c.h"
245n/a
246n/a/*[clinic input]
247n/abinascii.a2b_uu
248n/a
249n/a data: ascii_buffer
250n/a /
251n/a
252n/aDecode a line of uuencoded data.
253n/a[clinic start generated code]*/
254n/a
255n/astatic PyObject *
256n/abinascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
257n/a/*[clinic end generated code: output=e027f8e0b0598742 input=7cafeaf73df63d1c]*/
258n/a{
259n/a const unsigned char *ascii_data;
260n/a unsigned char *bin_data;
261n/a int leftbits = 0;
262n/a unsigned char this_ch;
263n/a unsigned int leftchar = 0;
264n/a PyObject *rv;
265n/a Py_ssize_t ascii_len, bin_len;
266n/a
267n/a ascii_data = data->buf;
268n/a ascii_len = data->len;
269n/a
270n/a assert(ascii_len >= 0);
271n/a
272n/a /* First byte: binary data length (in bytes) */
273n/a bin_len = (*ascii_data++ - ' ') & 077;
274n/a ascii_len--;
275n/a
276n/a /* Allocate the buffer */
277n/a if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
278n/a return NULL;
279n/a bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
280n/a
281n/a for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
282n/a /* XXX is it really best to add NULs if there's no more data */
283n/a this_ch = (ascii_len > 0) ? *ascii_data : 0;
284n/a if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
285n/a /*
286n/a ** Whitespace. Assume some spaces got eaten at
287n/a ** end-of-line. (We check this later)
288n/a */
289n/a this_ch = 0;
290n/a } else {
291n/a /* Check the character for legality
292n/a ** The 64 in stead of the expected 63 is because
293n/a ** there are a few uuencodes out there that use
294n/a ** '`' as zero instead of space.
295n/a */
296n/a if ( this_ch < ' ' || this_ch > (' ' + 64)) {
297n/a PyErr_SetString(Error, "Illegal char");
298n/a Py_DECREF(rv);
299n/a return NULL;
300n/a }
301n/a this_ch = (this_ch - ' ') & 077;
302n/a }
303n/a /*
304n/a ** Shift it in on the low end, and see if there's
305n/a ** a byte ready for output.
306n/a */
307n/a leftchar = (leftchar << 6) | (this_ch);
308n/a leftbits += 6;
309n/a if ( leftbits >= 8 ) {
310n/a leftbits -= 8;
311n/a *bin_data++ = (leftchar >> leftbits) & 0xff;
312n/a leftchar &= ((1 << leftbits) - 1);
313n/a bin_len--;
314n/a }
315n/a }
316n/a /*
317n/a ** Finally, check that if there's anything left on the line
318n/a ** that it's whitespace only.
319n/a */
320n/a while( ascii_len-- > 0 ) {
321n/a this_ch = *ascii_data++;
322n/a /* Extra '`' may be written as padding in some cases */
323n/a if ( this_ch != ' ' && this_ch != ' '+64 &&
324n/a this_ch != '\n' && this_ch != '\r' ) {
325n/a PyErr_SetString(Error, "Trailing garbage");
326n/a Py_DECREF(rv);
327n/a return NULL;
328n/a }
329n/a }
330n/a return rv;
331n/a}
332n/a
333n/a/*[clinic input]
334n/abinascii.b2a_uu
335n/a
336n/a data: Py_buffer
337n/a /
338n/a
339n/aUuencode line of data.
340n/a[clinic start generated code]*/
341n/a
342n/astatic PyObject *
343n/abinascii_b2a_uu_impl(PyObject *module, Py_buffer *data)
344n/a/*[clinic end generated code: output=0070670e52e4aa6b input=00fdf458ce8b465b]*/
345n/a{
346n/a unsigned char *ascii_data;
347n/a const unsigned char *bin_data;
348n/a int leftbits = 0;
349n/a unsigned char this_ch;
350n/a unsigned int leftchar = 0;
351n/a Py_ssize_t bin_len, out_len;
352n/a _PyBytesWriter writer;
353n/a
354n/a _PyBytesWriter_Init(&writer);
355n/a bin_data = data->buf;
356n/a bin_len = data->len;
357n/a if ( bin_len > 45 ) {
358n/a /* The 45 is a limit that appears in all uuencode's */
359n/a PyErr_SetString(Error, "At most 45 bytes at once");
360n/a return NULL;
361n/a }
362n/a
363n/a /* We're lazy and allocate to much (fixed up later) */
364n/a out_len = 2 + (bin_len + 2) / 3 * 4;
365n/a ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
366n/a if (ascii_data == NULL)
367n/a return NULL;
368n/a
369n/a /* Store the length */
370n/a *ascii_data++ = ' ' + (bin_len & 077);
371n/a
372n/a for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
373n/a /* Shift the data (or padding) into our buffer */
374n/a if ( bin_len > 0 ) /* Data */
375n/a leftchar = (leftchar << 8) | *bin_data;
376n/a else /* Padding */
377n/a leftchar <<= 8;
378n/a leftbits += 8;
379n/a
380n/a /* See if there are 6-bit groups ready */
381n/a while ( leftbits >= 6 ) {
382n/a this_ch = (leftchar >> (leftbits-6)) & 0x3f;
383n/a leftbits -= 6;
384n/a *ascii_data++ = this_ch + ' ';
385n/a }
386n/a }
387n/a *ascii_data++ = '\n'; /* Append a courtesy newline */
388n/a
389n/a return _PyBytesWriter_Finish(&writer, ascii_data);
390n/a}
391n/a
392n/a
393n/astatic int
394n/abinascii_find_valid(const unsigned char *s, Py_ssize_t slen, int num)
395n/a{
396n/a /* Finds & returns the (num+1)th
397n/a ** valid character for base64, or -1 if none.
398n/a */
399n/a
400n/a int ret = -1;
401n/a unsigned char c, b64val;
402n/a
403n/a while ((slen > 0) && (ret == -1)) {
404n/a c = *s;
405n/a b64val = table_a2b_base64[c & 0x7f];
406n/a if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
407n/a if (num == 0)
408n/a ret = *s;
409n/a num--;
410n/a }
411n/a
412n/a s++;
413n/a slen--;
414n/a }
415n/a return ret;
416n/a}
417n/a
418n/a/*[clinic input]
419n/abinascii.a2b_base64
420n/a
421n/a data: ascii_buffer
422n/a /
423n/a
424n/aDecode a line of base64 data.
425n/a[clinic start generated code]*/
426n/a
427n/astatic PyObject *
428n/abinascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
429n/a/*[clinic end generated code: output=0628223f19fd3f9b input=5872acf6e1cac243]*/
430n/a{
431n/a const unsigned char *ascii_data;
432n/a unsigned char *bin_data;
433n/a int leftbits = 0;
434n/a unsigned char this_ch;
435n/a unsigned int leftchar = 0;
436n/a Py_ssize_t ascii_len, bin_len;
437n/a int quad_pos = 0;
438n/a _PyBytesWriter writer;
439n/a
440n/a ascii_data = data->buf;
441n/a ascii_len = data->len;
442n/a
443n/a assert(ascii_len >= 0);
444n/a
445n/a if (ascii_len > PY_SSIZE_T_MAX - 3)
446n/a return PyErr_NoMemory();
447n/a
448n/a bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
449n/a
450n/a _PyBytesWriter_Init(&writer);
451n/a
452n/a /* Allocate the buffer */
453n/a bin_data = _PyBytesWriter_Alloc(&writer, bin_len);
454n/a if (bin_data == NULL)
455n/a return NULL;
456n/a
457n/a for( ; ascii_len > 0; ascii_len--, ascii_data++) {
458n/a this_ch = *ascii_data;
459n/a
460n/a if (this_ch > 0x7f ||
461n/a this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
462n/a continue;
463n/a
464n/a /* Check for pad sequences and ignore
465n/a ** the invalid ones.
466n/a */
467n/a if (this_ch == BASE64_PAD) {
468n/a if ( (quad_pos < 2) ||
469n/a ((quad_pos == 2) &&
470n/a (binascii_find_valid(ascii_data, ascii_len, 1)
471n/a != BASE64_PAD)) )
472n/a {
473n/a continue;
474n/a }
475n/a else {
476n/a /* A pad sequence means no more input.
477n/a ** We've already interpreted the data
478n/a ** from the quad at this point.
479n/a */
480n/a leftbits = 0;
481n/a break;
482n/a }
483n/a }
484n/a
485n/a this_ch = table_a2b_base64[*ascii_data];
486n/a if ( this_ch == (unsigned char) -1 )
487n/a continue;
488n/a
489n/a /*
490n/a ** Shift it in on the low end, and see if there's
491n/a ** a byte ready for output.
492n/a */
493n/a quad_pos = (quad_pos + 1) & 0x03;
494n/a leftchar = (leftchar << 6) | (this_ch);
495n/a leftbits += 6;
496n/a
497n/a if ( leftbits >= 8 ) {
498n/a leftbits -= 8;
499n/a *bin_data++ = (leftchar >> leftbits) & 0xff;
500n/a leftchar &= ((1 << leftbits) - 1);
501n/a }
502n/a }
503n/a
504n/a if (leftbits != 0) {
505n/a PyErr_SetString(Error, "Incorrect padding");
506n/a _PyBytesWriter_Dealloc(&writer);
507n/a return NULL;
508n/a }
509n/a
510n/a return _PyBytesWriter_Finish(&writer, bin_data);
511n/a}
512n/a
513n/a
514n/a/*[clinic input]
515n/abinascii.b2a_base64
516n/a
517n/a data: Py_buffer
518n/a *
519n/a newline: int(c_default="1") = True
520n/a
521n/aBase64-code line of data.
522n/a[clinic start generated code]*/
523n/a
524n/astatic PyObject *
525n/abinascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
526n/a/*[clinic end generated code: output=4ad62c8e8485d3b3 input=7b2ea6fa38d8924c]*/
527n/a{
528n/a unsigned char *ascii_data;
529n/a const unsigned char *bin_data;
530n/a int leftbits = 0;
531n/a unsigned char this_ch;
532n/a unsigned int leftchar = 0;
533n/a Py_ssize_t bin_len, out_len;
534n/a _PyBytesWriter writer;
535n/a
536n/a bin_data = data->buf;
537n/a bin_len = data->len;
538n/a _PyBytesWriter_Init(&writer);
539n/a
540n/a assert(bin_len >= 0);
541n/a
542n/a if ( bin_len > BASE64_MAXBIN ) {
543n/a PyErr_SetString(Error, "Too much data for base64 line");
544n/a return NULL;
545n/a }
546n/a
547n/a /* We're lazy and allocate too much (fixed up later).
548n/a "+2" leaves room for up to two pad characters.
549n/a Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
550n/a out_len = bin_len*2 + 2;
551n/a if (newline)
552n/a out_len++;
553n/a ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
554n/a if (ascii_data == NULL)
555n/a return NULL;
556n/a
557n/a for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
558n/a /* Shift the data into our buffer */
559n/a leftchar = (leftchar << 8) | *bin_data;
560n/a leftbits += 8;
561n/a
562n/a /* See if there are 6-bit groups ready */
563n/a while ( leftbits >= 6 ) {
564n/a this_ch = (leftchar >> (leftbits-6)) & 0x3f;
565n/a leftbits -= 6;
566n/a *ascii_data++ = table_b2a_base64[this_ch];
567n/a }
568n/a }
569n/a if ( leftbits == 2 ) {
570n/a *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
571n/a *ascii_data++ = BASE64_PAD;
572n/a *ascii_data++ = BASE64_PAD;
573n/a } else if ( leftbits == 4 ) {
574n/a *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
575n/a *ascii_data++ = BASE64_PAD;
576n/a }
577n/a if (newline)
578n/a *ascii_data++ = '\n'; /* Append a courtesy newline */
579n/a
580n/a return _PyBytesWriter_Finish(&writer, ascii_data);
581n/a}
582n/a
583n/a/*[clinic input]
584n/abinascii.a2b_hqx
585n/a
586n/a data: ascii_buffer
587n/a /
588n/a
589n/aDecode .hqx coding.
590n/a[clinic start generated code]*/
591n/a
592n/astatic PyObject *
593n/abinascii_a2b_hqx_impl(PyObject *module, Py_buffer *data)
594n/a/*[clinic end generated code: output=4d6d8c54d54ea1c1 input=0d914c680e0eed55]*/
595n/a{
596n/a const unsigned char *ascii_data;
597n/a unsigned char *bin_data;
598n/a int leftbits = 0;
599n/a unsigned char this_ch;
600n/a unsigned int leftchar = 0;
601n/a PyObject *res;
602n/a Py_ssize_t len;
603n/a int done = 0;
604n/a _PyBytesWriter writer;
605n/a
606n/a ascii_data = data->buf;
607n/a len = data->len;
608n/a _PyBytesWriter_Init(&writer);
609n/a
610n/a assert(len >= 0);
611n/a
612n/a if (len > PY_SSIZE_T_MAX - 2)
613n/a return PyErr_NoMemory();
614n/a
615n/a /* Allocate a string that is too big (fixed later)
616n/a Add two to the initial length to prevent interning which
617n/a would preclude subsequent resizing. */
618n/a bin_data = _PyBytesWriter_Alloc(&writer, len + 2);
619n/a if (bin_data == NULL)
620n/a return NULL;
621n/a
622n/a for( ; len > 0 ; len--, ascii_data++ ) {
623n/a /* Get the byte and look it up */
624n/a this_ch = table_a2b_hqx[*ascii_data];
625n/a if ( this_ch == SKIP )
626n/a continue;
627n/a if ( this_ch == FAIL ) {
628n/a PyErr_SetString(Error, "Illegal char");
629n/a _PyBytesWriter_Dealloc(&writer);
630n/a return NULL;
631n/a }
632n/a if ( this_ch == DONE ) {
633n/a /* The terminating colon */
634n/a done = 1;
635n/a break;
636n/a }
637n/a
638n/a /* Shift it into the buffer and see if any bytes are ready */
639n/a leftchar = (leftchar << 6) | (this_ch);
640n/a leftbits += 6;
641n/a if ( leftbits >= 8 ) {
642n/a leftbits -= 8;
643n/a *bin_data++ = (leftchar >> leftbits) & 0xff;
644n/a leftchar &= ((1 << leftbits) - 1);
645n/a }
646n/a }
647n/a
648n/a if ( leftbits && !done ) {
649n/a PyErr_SetString(Incomplete,
650n/a "String has incomplete number of bytes");
651n/a _PyBytesWriter_Dealloc(&writer);
652n/a return NULL;
653n/a }
654n/a
655n/a res = _PyBytesWriter_Finish(&writer, bin_data);
656n/a if (res == NULL)
657n/a return NULL;
658n/a return Py_BuildValue("Ni", res, done);
659n/a}
660n/a
661n/a
662n/a/*[clinic input]
663n/abinascii.rlecode_hqx
664n/a
665n/a data: Py_buffer
666n/a /
667n/a
668n/aBinhex RLE-code binary data.
669n/a[clinic start generated code]*/
670n/a
671n/astatic PyObject *
672n/abinascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data)
673n/a/*[clinic end generated code: output=393d79338f5f5629 input=e1f1712447a82b09]*/
674n/a{
675n/a const unsigned char *in_data;
676n/a unsigned char *out_data;
677n/a unsigned char ch;
678n/a Py_ssize_t in, inend, len;
679n/a _PyBytesWriter writer;
680n/a
681n/a _PyBytesWriter_Init(&writer);
682n/a in_data = data->buf;
683n/a len = data->len;
684n/a
685n/a assert(len >= 0);
686n/a
687n/a if (len > PY_SSIZE_T_MAX / 2 - 2)
688n/a return PyErr_NoMemory();
689n/a
690n/a /* Worst case: output is twice as big as input (fixed later) */
691n/a out_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
692n/a if (out_data == NULL)
693n/a return NULL;
694n/a
695n/a for( in=0; in<len; in++) {
696n/a ch = in_data[in];
697n/a if ( ch == RUNCHAR ) {
698n/a /* RUNCHAR. Escape it. */
699n/a *out_data++ = RUNCHAR;
700n/a *out_data++ = 0;
701n/a } else {
702n/a /* Check how many following are the same */
703n/a for(inend=in+1;
704n/a inend<len && in_data[inend] == ch &&
705n/a inend < in+255;
706n/a inend++) ;
707n/a if ( inend - in > 3 ) {
708n/a /* More than 3 in a row. Output RLE. */
709n/a *out_data++ = ch;
710n/a *out_data++ = RUNCHAR;
711n/a *out_data++ = (unsigned char) (inend-in);
712n/a in = inend-1;
713n/a } else {
714n/a /* Less than 3. Output the byte itself */
715n/a *out_data++ = ch;
716n/a }
717n/a }
718n/a }
719n/a
720n/a return _PyBytesWriter_Finish(&writer, out_data);
721n/a}
722n/a
723n/a
724n/a/*[clinic input]
725n/abinascii.b2a_hqx
726n/a
727n/a data: Py_buffer
728n/a /
729n/a
730n/aEncode .hqx data.
731n/a[clinic start generated code]*/
732n/a
733n/astatic PyObject *
734n/abinascii_b2a_hqx_impl(PyObject *module, Py_buffer *data)
735n/a/*[clinic end generated code: output=d0aa5a704bc9f7de input=9596ebe019fe12ba]*/
736n/a{
737n/a unsigned char *ascii_data;
738n/a const unsigned char *bin_data;
739n/a int leftbits = 0;
740n/a unsigned char this_ch;
741n/a unsigned int leftchar = 0;
742n/a Py_ssize_t len;
743n/a _PyBytesWriter writer;
744n/a
745n/a bin_data = data->buf;
746n/a len = data->len;
747n/a _PyBytesWriter_Init(&writer);
748n/a
749n/a assert(len >= 0);
750n/a
751n/a if (len > PY_SSIZE_T_MAX / 2 - 2)
752n/a return PyErr_NoMemory();
753n/a
754n/a /* Allocate a buffer that is at least large enough */
755n/a ascii_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
756n/a if (ascii_data == NULL)
757n/a return NULL;
758n/a
759n/a for( ; len > 0 ; len--, bin_data++ ) {
760n/a /* Shift into our buffer, and output any 6bits ready */
761n/a leftchar = (leftchar << 8) | *bin_data;
762n/a leftbits += 8;
763n/a while ( leftbits >= 6 ) {
764n/a this_ch = (leftchar >> (leftbits-6)) & 0x3f;
765n/a leftbits -= 6;
766n/a *ascii_data++ = table_b2a_hqx[this_ch];
767n/a }
768n/a }
769n/a /* Output a possible runt byte */
770n/a if ( leftbits ) {
771n/a leftchar <<= (6-leftbits);
772n/a *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
773n/a }
774n/a
775n/a return _PyBytesWriter_Finish(&writer, ascii_data);
776n/a}
777n/a
778n/a
779n/a/*[clinic input]
780n/abinascii.rledecode_hqx
781n/a
782n/a data: Py_buffer
783n/a /
784n/a
785n/aDecode hexbin RLE-coded string.
786n/a[clinic start generated code]*/
787n/a
788n/astatic PyObject *
789n/abinascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data)
790n/a/*[clinic end generated code: output=9826619565de1c6c input=54cdd49fc014402c]*/
791n/a{
792n/a const unsigned char *in_data;
793n/a unsigned char *out_data;
794n/a unsigned char in_byte, in_repeat;
795n/a Py_ssize_t in_len;
796n/a _PyBytesWriter writer;
797n/a
798n/a in_data = data->buf;
799n/a in_len = data->len;
800n/a _PyBytesWriter_Init(&writer);
801n/a
802n/a assert(in_len >= 0);
803n/a
804n/a /* Empty string is a special case */
805n/a if ( in_len == 0 )
806n/a return PyBytes_FromStringAndSize("", 0);
807n/a else if (in_len > PY_SSIZE_T_MAX / 2)
808n/a return PyErr_NoMemory();
809n/a
810n/a /* Allocate a buffer of reasonable size. Resized when needed */
811n/a out_data = _PyBytesWriter_Alloc(&writer, in_len);
812n/a if (out_data == NULL)
813n/a return NULL;
814n/a
815n/a /* Use overallocation */
816n/a writer.overallocate = 1;
817n/a
818n/a /*
819n/a ** We need two macros here to get/put bytes and handle
820n/a ** end-of-buffer for input and output strings.
821n/a */
822n/a#define INBYTE(b) \
823n/a do { \
824n/a if ( --in_len < 0 ) { \
825n/a PyErr_SetString(Incomplete, ""); \
826n/a goto error; \
827n/a } \
828n/a b = *in_data++; \
829n/a } while(0)
830n/a
831n/a /*
832n/a ** Handle first byte separately (since we have to get angry
833n/a ** in case of an orphaned RLE code).
834n/a */
835n/a INBYTE(in_byte);
836n/a
837n/a if (in_byte == RUNCHAR) {
838n/a INBYTE(in_repeat);
839n/a /* only 1 byte will be written, but 2 bytes were preallocated:
840n/a subtract 1 byte to prevent overallocation */
841n/a writer.min_size--;
842n/a
843n/a if (in_repeat != 0) {
844n/a /* Note Error, not Incomplete (which is at the end
845n/a ** of the string only). This is a programmer error.
846n/a */
847n/a PyErr_SetString(Error, "Orphaned RLE code at start");
848n/a goto error;
849n/a }
850n/a *out_data++ = RUNCHAR;
851n/a } else {
852n/a *out_data++ = in_byte;
853n/a }
854n/a
855n/a while( in_len > 0 ) {
856n/a INBYTE(in_byte);
857n/a
858n/a if (in_byte == RUNCHAR) {
859n/a INBYTE(in_repeat);
860n/a /* only 1 byte will be written, but 2 bytes were preallocated:
861n/a subtract 1 byte to prevent overallocation */
862n/a writer.min_size--;
863n/a
864n/a if ( in_repeat == 0 ) {
865n/a /* Just an escaped RUNCHAR value */
866n/a *out_data++ = RUNCHAR;
867n/a } else {
868n/a /* Pick up value and output a sequence of it */
869n/a in_byte = out_data[-1];
870n/a
871n/a /* enlarge the buffer if needed */
872n/a if (in_repeat > 1) {
873n/a /* -1 because we already preallocated 1 byte */
874n/a out_data = _PyBytesWriter_Prepare(&writer, out_data,
875n/a in_repeat - 1);
876n/a if (out_data == NULL)
877n/a goto error;
878n/a }
879n/a
880n/a while ( --in_repeat > 0 )
881n/a *out_data++ = in_byte;
882n/a }
883n/a } else {
884n/a /* Normal byte */
885n/a *out_data++ = in_byte;
886n/a }
887n/a }
888n/a return _PyBytesWriter_Finish(&writer, out_data);
889n/a
890n/aerror:
891n/a _PyBytesWriter_Dealloc(&writer);
892n/a return NULL;
893n/a}
894n/a
895n/a
896n/a/*[clinic input]
897n/abinascii.crc_hqx -> unsigned_int
898n/a
899n/a data: Py_buffer
900n/a crc: unsigned_int(bitwise=True)
901n/a /
902n/a
903n/aCompute CRC-CCITT incrementally.
904n/a[clinic start generated code]*/
905n/a
906n/astatic unsigned int
907n/abinascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc)
908n/a/*[clinic end generated code: output=8ec2a78590d19170 input=f18240ff8c705b79]*/
909n/a{
910n/a const unsigned char *bin_data;
911n/a Py_ssize_t len;
912n/a
913n/a crc &= 0xffff;
914n/a bin_data = data->buf;
915n/a len = data->len;
916n/a
917n/a while(len-- > 0) {
918n/a crc = ((crc<<8)&0xff00) ^ crctab_hqx[(crc>>8)^*bin_data++];
919n/a }
920n/a
921n/a return crc;
922n/a}
923n/a
924n/a#ifndef USE_ZLIB_CRC32
925n/a/* Crc - 32 BIT ANSI X3.66 CRC checksum files
926n/a Also known as: ISO 3307
927n/a**********************************************************************|
928n/a* *|
929n/a* Demonstration program to compute the 32-bit CRC used as the frame *|
930n/a* check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *|
931n/a* and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *|
932n/a* protocol). The 32-bit FCS was added via the Federal Register, *|
933n/a* 1 June 1982, p.23798. I presume but don't know for certain that *|
934n/a* this polynomial is or will be included in CCITT V.41, which *|
935n/a* defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *|
936n/a* PUB 78 says that the 32-bit FCS reduces otherwise undetected *|
937n/a* errors by a factor of 10^-5 over 16-bit FCS. *|
938n/a* *|
939n/a**********************************************************************|
940n/a
941n/a Copyright (C) 1986 Gary S. Brown. You may use this program, or
942n/a code or tables extracted from it, as desired without restriction.
943n/a
944n/a First, the polynomial itself and its table of feedback terms. The
945n/a polynomial is
946n/a 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
947n/a Note that we take it "backwards" and put the highest-order term in
948n/a the lowest-order bit. The X^32 term is "implied"; the LSB is the
949n/a X^31 term, etc. The X^0 term (usually shown as "+1") results in
950n/a the MSB being 1.
951n/a
952n/a Note that the usual hardware shift register implementation, which
953n/a is what we're using (we're merely optimizing it by doing eight-bit
954n/a chunks at a time) shifts bits into the lowest-order term. In our
955n/a implementation, that means shifting towards the right. Why do we
956n/a do it this way? Because the calculated CRC must be transmitted in
957n/a order from highest-order term to lowest-order term. UARTs transmit
958n/a characters in order from LSB to MSB. By storing the CRC this way,
959n/a we hand it to the UART in the order low-byte to high-byte; the UART
960n/a sends each low-bit to hight-bit; and the result is transmission bit
961n/a by bit from highest- to lowest-order term without requiring any bit
962n/a shuffling on our part. Reception works similarly.
963n/a
964n/a The feedback terms table consists of 256, 32-bit entries. Notes:
965n/a
966n/a 1. The table can be generated at runtime if desired; code to do so
967n/a is shown later. It might not be obvious, but the feedback
968n/a terms simply represent the results of eight shift/xor opera-
969n/a tions for all combinations of data and CRC register values.
970n/a
971n/a 2. The CRC accumulation logic is the same for all CRC polynomials,
972n/a be they sixteen or thirty-two bits wide. You simply choose the
973n/a appropriate table. Alternatively, because the table can be
974n/a generated at runtime, you can start by generating the table for
975n/a the polynomial in question and use exactly the same "updcrc",
976n/a if your application needn't simultaneously handle two CRC
977n/a polynomials. (Note, however, that XMODEM is strange.)
978n/a
979n/a 3. For 16-bit CRCs, the table entries need be only 16 bits wide;
980n/a of course, 32-bit entries work OK if the high 16 bits are zero.
981n/a
982n/a 4. The values must be right-shifted by eight bits by the "updcrc"
983n/a logic; the shift must be unsigned (bring in zeroes). On some
984n/a hardware you could probably optimize the shift in assembler by
985n/a using byte-swap instructions.
986n/a********************************************************************/
987n/a
988n/astatic const unsigned int crc_32_tab[256] = {
989n/a0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
990n/a0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
991n/a0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
992n/a0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
993n/a0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
994n/a0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
995n/a0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
996n/a0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
997n/a0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
998n/a0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
999n/a0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
1000n/a0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
1001n/a0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
1002n/a0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
1003n/a0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
1004n/a0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
1005n/a0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
1006n/a0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
1007n/a0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
1008n/a0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
1009n/a0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
1010n/a0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
1011n/a0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
1012n/a0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
1013n/a0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
1014n/a0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
1015n/a0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
1016n/a0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
1017n/a0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
1018n/a0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
1019n/a0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
1020n/a0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
1021n/a0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
1022n/a0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
1023n/a0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
1024n/a0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
1025n/a0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
1026n/a0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
1027n/a0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
1028n/a0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
1029n/a0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
1030n/a0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
1031n/a0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
1032n/a0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
1033n/a0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
1034n/a0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
1035n/a0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
1036n/a0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
1037n/a0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
1038n/a0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
1039n/a0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
1040n/a0x2d02ef8dU
1041n/a};
1042n/a#endif /* USE_ZLIB_CRC32 */
1043n/a
1044n/a/*[clinic input]
1045n/abinascii.crc32 -> unsigned_int
1046n/a
1047n/a data: Py_buffer
1048n/a crc: unsigned_int(bitwise=True) = 0
1049n/a /
1050n/a
1051n/aCompute CRC-32 incrementally.
1052n/a[clinic start generated code]*/
1053n/a
1054n/astatic unsigned int
1055n/abinascii_crc32_impl(PyObject *module, Py_buffer *data, unsigned int crc)
1056n/a/*[clinic end generated code: output=52cf59056a78593b input=bbe340bc99d25aa8]*/
1057n/a
1058n/a#ifdef USE_ZLIB_CRC32
1059n/a/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
1060n/a{
1061n/a const Byte *buf;
1062n/a Py_ssize_t len;
1063n/a int signed_val;
1064n/a
1065n/a buf = (Byte*)data->buf;
1066n/a len = data->len;
1067n/a signed_val = crc32(crc, buf, len);
1068n/a return (unsigned int)signed_val & 0xffffffffU;
1069n/a}
1070n/a#else /* USE_ZLIB_CRC32 */
1071n/a{ /* By Jim Ahlstrom; All rights transferred to CNRI */
1072n/a const unsigned char *bin_data;
1073n/a Py_ssize_t len;
1074n/a unsigned int result;
1075n/a
1076n/a bin_data = data->buf;
1077n/a len = data->len;
1078n/a
1079n/a crc = ~ crc;
1080n/a while (len-- > 0) {
1081n/a crc = crc_32_tab[(crc ^ *bin_data++) & 0xff] ^ (crc >> 8);
1082n/a /* Note: (crc >> 8) MUST zero fill on left */
1083n/a }
1084n/a
1085n/a result = (crc ^ 0xFFFFFFFF);
1086n/a return result & 0xffffffff;
1087n/a}
1088n/a#endif /* USE_ZLIB_CRC32 */
1089n/a
1090n/a/*[clinic input]
1091n/abinascii.b2a_hex
1092n/a
1093n/a data: Py_buffer
1094n/a /
1095n/a
1096n/aHexadecimal representation of binary data.
1097n/a
1098n/aThe return value is a bytes object. This function is also
1099n/aavailable as "hexlify()".
1100n/a[clinic start generated code]*/
1101n/a
1102n/astatic PyObject *
1103n/abinascii_b2a_hex_impl(PyObject *module, Py_buffer *data)
1104n/a/*[clinic end generated code: output=92fec1a95c9897a0 input=96423cfa299ff3b1]*/
1105n/a{
1106n/a return _Py_strhex_bytes((const char *)data->buf, data->len);
1107n/a}
1108n/a
1109n/a/*[clinic input]
1110n/abinascii.hexlify = binascii.b2a_hex
1111n/a
1112n/aHexadecimal representation of binary data.
1113n/a
1114n/aThe return value is a bytes object.
1115n/a[clinic start generated code]*/
1116n/a
1117n/astatic PyObject *
1118n/abinascii_hexlify_impl(PyObject *module, Py_buffer *data)
1119n/a/*[clinic end generated code: output=749e95e53c14880c input=2e3afae7f083f061]*/
1120n/a{
1121n/a return _Py_strhex_bytes((const char *)data->buf, data->len);
1122n/a}
1123n/a
1124n/astatic int
1125n/ato_int(int c)
1126n/a{
1127n/a if (Py_ISDIGIT(c))
1128n/a return c - '0';
1129n/a else {
1130n/a if (Py_ISUPPER(c))
1131n/a c = Py_TOLOWER(c);
1132n/a if (c >= 'a' && c <= 'f')
1133n/a return c - 'a' + 10;
1134n/a }
1135n/a return -1;
1136n/a}
1137n/a
1138n/a
1139n/a/*[clinic input]
1140n/abinascii.a2b_hex
1141n/a
1142n/a hexstr: ascii_buffer
1143n/a /
1144n/a
1145n/aBinary data of hexadecimal representation.
1146n/a
1147n/ahexstr must contain an even number of hex digits (upper or lower case).
1148n/aThis function is also available as "unhexlify()".
1149n/a[clinic start generated code]*/
1150n/a
1151n/astatic PyObject *
1152n/abinascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
1153n/a/*[clinic end generated code: output=0cc1a139af0eeecb input=9e1e7f2f94db24fd]*/
1154n/a{
1155n/a const char* argbuf;
1156n/a Py_ssize_t arglen;
1157n/a PyObject *retval;
1158n/a char* retbuf;
1159n/a Py_ssize_t i, j;
1160n/a
1161n/a argbuf = hexstr->buf;
1162n/a arglen = hexstr->len;
1163n/a
1164n/a assert(arglen >= 0);
1165n/a
1166n/a /* XXX What should we do about strings with an odd length? Should
1167n/a * we add an implicit leading zero, or a trailing zero? For now,
1168n/a * raise an exception.
1169n/a */
1170n/a if (arglen % 2) {
1171n/a PyErr_SetString(Error, "Odd-length string");
1172n/a return NULL;
1173n/a }
1174n/a
1175n/a retval = PyBytes_FromStringAndSize(NULL, (arglen/2));
1176n/a if (!retval)
1177n/a return NULL;
1178n/a retbuf = PyBytes_AS_STRING(retval);
1179n/a
1180n/a for (i=j=0; i < arglen; i += 2) {
1181n/a int top = to_int(Py_CHARMASK(argbuf[i]));
1182n/a int bot = to_int(Py_CHARMASK(argbuf[i+1]));
1183n/a if (top == -1 || bot == -1) {
1184n/a PyErr_SetString(Error,
1185n/a "Non-hexadecimal digit found");
1186n/a goto finally;
1187n/a }
1188n/a retbuf[j++] = (top << 4) + bot;
1189n/a }
1190n/a return retval;
1191n/a
1192n/a finally:
1193n/a Py_DECREF(retval);
1194n/a return NULL;
1195n/a}
1196n/a
1197n/a/*[clinic input]
1198n/abinascii.unhexlify = binascii.a2b_hex
1199n/a
1200n/aBinary data of hexadecimal representation.
1201n/a
1202n/ahexstr must contain an even number of hex digits (upper or lower case).
1203n/a[clinic start generated code]*/
1204n/a
1205n/astatic PyObject *
1206n/abinascii_unhexlify_impl(PyObject *module, Py_buffer *hexstr)
1207n/a/*[clinic end generated code: output=51a64c06c79629e3 input=dd8c012725f462da]*/
1208n/a{
1209n/a return binascii_a2b_hex_impl(module, hexstr);
1210n/a}
1211n/a
1212n/astatic const int table_hex[128] = {
1213n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1214n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1215n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1216n/a 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1217n/a -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1218n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1219n/a -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1220n/a -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1221n/a};
1222n/a
1223n/a#define hexval(c) table_hex[(unsigned int)(c)]
1224n/a
1225n/a#define MAXLINESIZE 76
1226n/a
1227n/a
1228n/a/*[clinic input]
1229n/abinascii.a2b_qp
1230n/a
1231n/a data: ascii_buffer
1232n/a header: int(c_default="0") = False
1233n/a
1234n/aDecode a string of qp-encoded data.
1235n/a[clinic start generated code]*/
1236n/a
1237n/astatic PyObject *
1238n/abinascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header)
1239n/a/*[clinic end generated code: output=e99f7846cfb9bc53 input=5187a0d3d8e54f3b]*/
1240n/a{
1241n/a Py_ssize_t in, out;
1242n/a char ch;
1243n/a const unsigned char *ascii_data;
1244n/a unsigned char *odata;
1245n/a Py_ssize_t datalen = 0;
1246n/a PyObject *rv;
1247n/a
1248n/a ascii_data = data->buf;
1249n/a datalen = data->len;
1250n/a
1251n/a /* We allocate the output same size as input, this is overkill.
1252n/a * The previous implementation used calloc() so we'll zero out the
1253n/a * memory here too, since PyMem_Malloc() does not guarantee that.
1254n/a */
1255n/a odata = (unsigned char *) PyMem_Malloc(datalen);
1256n/a if (odata == NULL) {
1257n/a PyErr_NoMemory();
1258n/a return NULL;
1259n/a }
1260n/a memset(odata, 0, datalen);
1261n/a
1262n/a in = out = 0;
1263n/a while (in < datalen) {
1264n/a if (ascii_data[in] == '=') {
1265n/a in++;
1266n/a if (in >= datalen) break;
1267n/a /* Soft line breaks */
1268n/a if ((ascii_data[in] == '\n') || (ascii_data[in] == '\r')) {
1269n/a if (ascii_data[in] != '\n') {
1270n/a while (in < datalen && ascii_data[in] != '\n') in++;
1271n/a }
1272n/a if (in < datalen) in++;
1273n/a }
1274n/a else if (ascii_data[in] == '=') {
1275n/a /* broken case from broken python qp */
1276n/a odata[out++] = '=';
1277n/a in++;
1278n/a }
1279n/a else if ((in + 1 < datalen) &&
1280n/a ((ascii_data[in] >= 'A' && ascii_data[in] <= 'F') ||
1281n/a (ascii_data[in] >= 'a' && ascii_data[in] <= 'f') ||
1282n/a (ascii_data[in] >= '0' && ascii_data[in] <= '9')) &&
1283n/a ((ascii_data[in+1] >= 'A' && ascii_data[in+1] <= 'F') ||
1284n/a (ascii_data[in+1] >= 'a' && ascii_data[in+1] <= 'f') ||
1285n/a (ascii_data[in+1] >= '0' && ascii_data[in+1] <= '9'))) {
1286n/a /* hexval */
1287n/a ch = hexval(ascii_data[in]) << 4;
1288n/a in++;
1289n/a ch |= hexval(ascii_data[in]);
1290n/a in++;
1291n/a odata[out++] = ch;
1292n/a }
1293n/a else {
1294n/a odata[out++] = '=';
1295n/a }
1296n/a }
1297n/a else if (header && ascii_data[in] == '_') {
1298n/a odata[out++] = ' ';
1299n/a in++;
1300n/a }
1301n/a else {
1302n/a odata[out] = ascii_data[in];
1303n/a in++;
1304n/a out++;
1305n/a }
1306n/a }
1307n/a if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
1308n/a PyMem_Free(odata);
1309n/a return NULL;
1310n/a }
1311n/a PyMem_Free(odata);
1312n/a return rv;
1313n/a}
1314n/a
1315n/astatic int
1316n/ato_hex (unsigned char ch, unsigned char *s)
1317n/a{
1318n/a unsigned int uvalue = ch;
1319n/a
1320n/a s[1] = "0123456789ABCDEF"[uvalue % 16];
1321n/a uvalue = (uvalue / 16);
1322n/a s[0] = "0123456789ABCDEF"[uvalue % 16];
1323n/a return 0;
1324n/a}
1325n/a
1326n/a/* XXX: This is ridiculously complicated to be backward compatible
1327n/a * (mostly) with the quopri module. It doesn't re-create the quopri
1328n/a * module bug where text ending in CRLF has the CR encoded */
1329n/a
1330n/a/*[clinic input]
1331n/abinascii.b2a_qp
1332n/a
1333n/a data: Py_buffer
1334n/a quotetabs: int(c_default="0") = False
1335n/a istext: int(c_default="1") = True
1336n/a header: int(c_default="0") = False
1337n/a
1338n/aEncode a string using quoted-printable encoding.
1339n/a
1340n/aOn encoding, when istext is set, newlines are not encoded, and white
1341n/aspace at end of lines is. When istext is not set, \r and \n (CR/LF)
1342n/aare both encoded. When quotetabs is set, space and tabs are encoded.
1343n/a[clinic start generated code]*/
1344n/a
1345n/astatic PyObject *
1346n/abinascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
1347n/a int istext, int header)
1348n/a/*[clinic end generated code: output=e9884472ebb1a94c input=7f2a9aaa008e92b2]*/
1349n/a{
1350n/a Py_ssize_t in, out;
1351n/a const unsigned char *databuf;
1352n/a unsigned char *odata;
1353n/a Py_ssize_t datalen = 0, odatalen = 0;
1354n/a PyObject *rv;
1355n/a unsigned int linelen = 0;
1356n/a unsigned char ch;
1357n/a int crlf = 0;
1358n/a const unsigned char *p;
1359n/a
1360n/a databuf = data->buf;
1361n/a datalen = data->len;
1362n/a
1363n/a /* See if this string is using CRLF line ends */
1364n/a /* XXX: this function has the side effect of converting all of
1365n/a * the end of lines to be the same depending on this detection
1366n/a * here */
1367n/a p = (const unsigned char *) memchr(databuf, '\n', datalen);
1368n/a if ((p != NULL) && (p > databuf) && (*(p-1) == '\r'))
1369n/a crlf = 1;
1370n/a
1371n/a /* First, scan to see how many characters need to be encoded */
1372n/a in = 0;
1373n/a while (in < datalen) {
1374n/a Py_ssize_t delta = 0;
1375n/a if ((databuf[in] > 126) ||
1376n/a (databuf[in] == '=') ||
1377n/a (header && databuf[in] == '_') ||
1378n/a ((databuf[in] == '.') && (linelen == 0) &&
1379n/a (in + 1 == datalen || databuf[in+1] == '\n' ||
1380n/a databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1381n/a (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1382n/a ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1383n/a ((databuf[in] < 33) &&
1384n/a (databuf[in] != '\r') && (databuf[in] != '\n') &&
1385n/a (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' ')))))
1386n/a {
1387n/a if ((linelen + 3) >= MAXLINESIZE) {
1388n/a linelen = 0;
1389n/a if (crlf)
1390n/a delta += 3;
1391n/a else
1392n/a delta += 2;
1393n/a }
1394n/a linelen += 3;
1395n/a delta += 3;
1396n/a in++;
1397n/a }
1398n/a else {
1399n/a if (istext &&
1400n/a ((databuf[in] == '\n') ||
1401n/a ((in+1 < datalen) && (databuf[in] == '\r') &&
1402n/a (databuf[in+1] == '\n'))))
1403n/a {
1404n/a linelen = 0;
1405n/a /* Protect against whitespace on end of line */
1406n/a if (in && ((databuf[in-1] == ' ') || (databuf[in-1] == '\t')))
1407n/a delta += 2;
1408n/a if (crlf)
1409n/a delta += 2;
1410n/a else
1411n/a delta += 1;
1412n/a if (databuf[in] == '\r')
1413n/a in += 2;
1414n/a else
1415n/a in++;
1416n/a }
1417n/a else {
1418n/a if ((in + 1 != datalen) &&
1419n/a (databuf[in+1] != '\n') &&
1420n/a (linelen + 1) >= MAXLINESIZE) {
1421n/a linelen = 0;
1422n/a if (crlf)
1423n/a delta += 3;
1424n/a else
1425n/a delta += 2;
1426n/a }
1427n/a linelen++;
1428n/a delta++;
1429n/a in++;
1430n/a }
1431n/a }
1432n/a if (PY_SSIZE_T_MAX - delta < odatalen) {
1433n/a PyErr_NoMemory();
1434n/a return NULL;
1435n/a }
1436n/a odatalen += delta;
1437n/a }
1438n/a
1439n/a /* We allocate the output same size as input, this is overkill.
1440n/a * The previous implementation used calloc() so we'll zero out the
1441n/a * memory here too, since PyMem_Malloc() does not guarantee that.
1442n/a */
1443n/a odata = (unsigned char *) PyMem_Malloc(odatalen);
1444n/a if (odata == NULL) {
1445n/a PyErr_NoMemory();
1446n/a return NULL;
1447n/a }
1448n/a memset(odata, 0, odatalen);
1449n/a
1450n/a in = out = linelen = 0;
1451n/a while (in < datalen) {
1452n/a if ((databuf[in] > 126) ||
1453n/a (databuf[in] == '=') ||
1454n/a (header && databuf[in] == '_') ||
1455n/a ((databuf[in] == '.') && (linelen == 0) &&
1456n/a (in + 1 == datalen || databuf[in+1] == '\n' ||
1457n/a databuf[in+1] == '\r' || databuf[in+1] == 0)) ||
1458n/a (!istext && ((databuf[in] == '\r') || (databuf[in] == '\n'))) ||
1459n/a ((databuf[in] == '\t' || databuf[in] == ' ') && (in + 1 == datalen)) ||
1460n/a ((databuf[in] < 33) &&
1461n/a (databuf[in] != '\r') && (databuf[in] != '\n') &&
1462n/a (quotetabs || ((databuf[in] != '\t') && (databuf[in] != ' ')))))
1463n/a {
1464n/a if ((linelen + 3 )>= MAXLINESIZE) {
1465n/a odata[out++] = '=';
1466n/a if (crlf) odata[out++] = '\r';
1467n/a odata[out++] = '\n';
1468n/a linelen = 0;
1469n/a }
1470n/a odata[out++] = '=';
1471n/a to_hex(databuf[in], &odata[out]);
1472n/a out += 2;
1473n/a in++;
1474n/a linelen += 3;
1475n/a }
1476n/a else {
1477n/a if (istext &&
1478n/a ((databuf[in] == '\n') ||
1479n/a ((in+1 < datalen) && (databuf[in] == '\r') &&
1480n/a (databuf[in+1] == '\n'))))
1481n/a {
1482n/a linelen = 0;
1483n/a /* Protect against whitespace on end of line */
1484n/a if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
1485n/a ch = odata[out-1];
1486n/a odata[out-1] = '=';
1487n/a to_hex(ch, &odata[out]);
1488n/a out += 2;
1489n/a }
1490n/a
1491n/a if (crlf) odata[out++] = '\r';
1492n/a odata[out++] = '\n';
1493n/a if (databuf[in] == '\r')
1494n/a in += 2;
1495n/a else
1496n/a in++;
1497n/a }
1498n/a else {
1499n/a if ((in + 1 != datalen) &&
1500n/a (databuf[in+1] != '\n') &&
1501n/a (linelen + 1) >= MAXLINESIZE) {
1502n/a odata[out++] = '=';
1503n/a if (crlf) odata[out++] = '\r';
1504n/a odata[out++] = '\n';
1505n/a linelen = 0;
1506n/a }
1507n/a linelen++;
1508n/a if (header && databuf[in] == ' ') {
1509n/a odata[out++] = '_';
1510n/a in++;
1511n/a }
1512n/a else {
1513n/a odata[out++] = databuf[in++];
1514n/a }
1515n/a }
1516n/a }
1517n/a }
1518n/a if ((rv = PyBytes_FromStringAndSize((char *)odata, out)) == NULL) {
1519n/a PyMem_Free(odata);
1520n/a return NULL;
1521n/a }
1522n/a PyMem_Free(odata);
1523n/a return rv;
1524n/a}
1525n/a
1526n/a/* List of functions defined in the module */
1527n/a
1528n/astatic struct PyMethodDef binascii_module_methods[] = {
1529n/a BINASCII_A2B_UU_METHODDEF
1530n/a BINASCII_B2A_UU_METHODDEF
1531n/a BINASCII_A2B_BASE64_METHODDEF
1532n/a BINASCII_B2A_BASE64_METHODDEF
1533n/a BINASCII_A2B_HQX_METHODDEF
1534n/a BINASCII_B2A_HQX_METHODDEF
1535n/a BINASCII_A2B_HEX_METHODDEF
1536n/a BINASCII_B2A_HEX_METHODDEF
1537n/a BINASCII_HEXLIFY_METHODDEF
1538n/a BINASCII_UNHEXLIFY_METHODDEF
1539n/a BINASCII_RLECODE_HQX_METHODDEF
1540n/a BINASCII_RLEDECODE_HQX_METHODDEF
1541n/a BINASCII_CRC_HQX_METHODDEF
1542n/a BINASCII_CRC32_METHODDEF
1543n/a BINASCII_A2B_QP_METHODDEF
1544n/a BINASCII_B2A_QP_METHODDEF
1545n/a {NULL, NULL} /* sentinel */
1546n/a};
1547n/a
1548n/a
1549n/a/* Initialization function for the module (*must* be called PyInit_binascii) */
1550n/aPyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
1551n/a
1552n/a
1553n/astatic struct PyModuleDef binasciimodule = {
1554n/a PyModuleDef_HEAD_INIT,
1555n/a "binascii",
1556n/a doc_binascii,
1557n/a -1,
1558n/a binascii_module_methods,
1559n/a NULL,
1560n/a NULL,
1561n/a NULL,
1562n/a NULL
1563n/a};
1564n/a
1565n/aPyMODINIT_FUNC
1566n/aPyInit_binascii(void)
1567n/a{
1568n/a PyObject *m, *d;
1569n/a
1570n/a /* Create the module and add the functions */
1571n/a m = PyModule_Create(&binasciimodule);
1572n/a if (m == NULL)
1573n/a return NULL;
1574n/a
1575n/a d = PyModule_GetDict(m);
1576n/a
1577n/a Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL);
1578n/a PyDict_SetItemString(d, "Error", Error);
1579n/a Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
1580n/a PyDict_SetItemString(d, "Incomplete", Incomplete);
1581n/a if (PyErr_Occurred()) {
1582n/a Py_DECREF(m);
1583n/a m = NULL;
1584n/a }
1585n/a return m;
1586n/a}