annotate Modules/binascii.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 1c964ed35ef9
children be0bf0bbd94a b3f50d6cb640
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1 /*
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
2 ** Routines to represent binary data in ASCII and vice-versa
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
3 **
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
4 ** This module currently supports the following encodings:
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
5 ** uuencode:
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
6 ** each line encodes 45 bytes (except possibly the last)
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
7 ** First char encodes (binary) length, rest data
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8 ** each char encodes 6 bits, as follows:
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
9 ** binary: 01234567 abcdefgh ijklmnop
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
10 ** ascii: 012345 67abcd efghij klmnop
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
11 ** ASCII encoding method is "excess-space": 000000 is encoded as ' ', etc.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
12 ** short binary data is zero-extended (so the bits are always in the
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
13 ** right place), this does *not* reflect in the length.
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
14 ** base64:
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
15 ** Line breaks are insignificant, but lines are at most 76 chars
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
16 ** each char encodes 6 bits, in similar order as uucode/hqx. Encoding
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
17 ** is done via a table.
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
18 ** Short binary data is filled (in ASCII) with '='.
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
19 ** hqx:
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
20 ** File starts with introductory text, real data starts and ends
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
21 ** with colons.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
22 ** Data consists of three similar parts: info, datafork, resourcefork.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
23 ** Each part is protected (at the end) with a 16-bit crc
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
24 ** The binary data is run-length encoded, and then ascii-fied:
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
25 ** binary: 01234567 abcdefgh ijklmnop
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
26 ** ascii: 012345 67abcd efghij klmnop
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27 ** ASCII encoding is table-driven, see the code.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
28 ** Short binary data results in the runt ascii-byte being output with
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
29 ** the bits in the right place.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
30 **
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
31 ** While I was reading dozens of programs that encode or decode the formats
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
32 ** here (documentation? hihi:-) I have formulated Jansen's Observation:
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
33 **
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
34 ** Programs that encode binary data in ASCII are written in
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35 ** such a style that they are as unreadable as possible. Devices used
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36 ** include unnecessary global variables, burying important tables
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37 ** in unrelated sourcefiles, putting functions in include files,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
38 ** using seemingly-descriptive variable names for different purposes,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
39 ** calls to empty subroutines and a host of others.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
40 **
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
41 ** I have attempted to break with this tradition, but I guess that that
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
42 ** does make the performance sub-optimal. Oh well, too bad...
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
43 **
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
44 ** Jack Jansen, CWI, July 1995.
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
45 **
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
46 ** Added support for quoted-printable encoding, based on rfc 1521 et al
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
47 ** quoted-printable encoding specifies that non printable characters (anything
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
48 ** below 32 and above 126) be encoded as =XX where XX is the hexadecimal value
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
49 ** of the character. It also specifies some other behavior to enable 8bit data
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
50 ** in a mail message with little difficulty (maximum line sizes, protecting
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
51 ** some cases of whitespace, etc).
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
52 **
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
53 ** Brandon Long, September 2001.
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
54 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
55
36779
a7b3632d0e05 Fix brainfart.
Thomas Wouters <thomas@python.org>
parents: 36776
diff changeset
56 #define PY_SSIZE_T_CLEAN
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
57
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
58 #include "Python.h"
46038
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
59 #ifdef USE_ZLIB_CRC32
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
60 #include "zlib.h"
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
61 #endif
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
62
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
63 static PyObject *Error;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
64 static PyObject *Incomplete;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
65
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
66 /*
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
67 ** hqx lookup table, ascii->binary.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
68 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
69
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
70 #define RUNCHAR 0x90
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
71
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
72 #define DONE 0x7F
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
73 #define SKIP 0x7E
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
74 #define FAIL 0x7D
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
75
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
76 static unsigned char table_a2b_hqx[256] = {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
77 /* ^@ ^A ^B ^C ^D ^E ^F ^G */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
78 /* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
79 /* \b \t \n ^K ^L \r ^N ^O */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
80 /* 1*/ FAIL, FAIL, SKIP, FAIL, FAIL, SKIP, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
81 /* ^P ^Q ^R ^S ^T ^U ^V ^W */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
82 /* 2*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
83 /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
84 /* 3*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
85 /* ! " # $ % & ' */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
86 /* 4*/ FAIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
87 /* ( ) * + , - . / */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
88 /* 5*/ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
89 /* 0 1 2 3 4 5 6 7 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
90 /* 6*/ 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
91 /* 8 9 : ; < = > ? */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
92 /* 7*/ 0x14, 0x15, DONE, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
93 /* @ A B C D E F G */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
94 /* 8*/ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
95 /* H I J K L M N O */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
96 /* 9*/ 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
97 /* P Q R S T U V W */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
98 /*10*/ 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
99 /* X Y Z [ \ ] ^ _ */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
100 /*11*/ 0x2C, 0x2D, 0x2E, 0x2F, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
101 /* ` a b c d e f g */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
102 /*12*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
103 /* h i j k l m n o */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
104 /*13*/ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
105 /* p q r s t u v w */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
106 /*14*/ 0x3D, 0x3E, 0x3F, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
107 /* x y z { | } ~ ^? */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
108 /*15*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
109 /*16*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
110 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
111 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
112 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
113 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
114 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
115 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
116 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
117 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
118 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
119 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
120 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
121 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
122 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
123 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
124 FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
125 };
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
126
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
127 static unsigned char table_b2a_hqx[] =
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
128 "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
129
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
130 static char table_a2b_base64[] = {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
131 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
132 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
133 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
134 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
135 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
136 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
137 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
138 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
139 };
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
140
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
141 #define BASE64_PAD '='
20759
5b236f377a25 Change the limit on the input size for b2a_base64 to what will fit in
Guido van Rossum <guido@python.org>
parents: 20250
diff changeset
142
5b236f377a25 Change the limit on the input size for b2a_base64 to what will fit in
Guido van Rossum <guido@python.org>
parents: 20250
diff changeset
143 /* Max binary chunk size; limited only by available memory */
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
144 #define BASE64_MAXBIN (PY_SSIZE_T_MAX/2 - sizeof(PyStringObject) - 3)
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
145
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
146 static unsigned char table_b2a_base64[] =
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
147 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
148
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
149
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
150
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
151 static unsigned short crctab_hqx[256] = {
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
152 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
153 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
154 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
155 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
156 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
157 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
158 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
159 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
160 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
161 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
162 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
163 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
164 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
165 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
166 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
167 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
168 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
169 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
170 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
171 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
172 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
173 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
174 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
175 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
176 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
177 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
178 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
179 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
180 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
181 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
182 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
183 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
184 };
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
185
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
186 PyDoc_STRVAR(doc_a2b_uu, "(ascii) -> bin. Decode a line of uuencoded data");
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
187
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
188 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
189 binascii_a2b_uu(PyObject *self, PyObject *args)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
190 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
191 unsigned char *ascii_data, *bin_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
192 int leftbits = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
193 unsigned char this_ch;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
194 unsigned int leftchar = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
195 PyObject *rv;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
196 Py_ssize_t ascii_len, bin_len;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
197
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
198 if ( !PyArg_ParseTuple(args, "t#:a2b_uu", &ascii_data, &ascii_len) )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
199 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
200
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
201 assert(ascii_len >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
202
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
203 /* First byte: binary data length (in bytes) */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
204 bin_len = (*ascii_data++ - ' ') & 077;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
205 ascii_len--;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
206
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
207 /* Allocate the buffer */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
208 if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
209 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
210 bin_data = (unsigned char *)PyString_AsString(rv);
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
211
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
212 for( ; bin_len > 0 ; ascii_len--, ascii_data++ ) {
32282
fd52b6807924 Valgrind was reporting an uninitialized read for bad input.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32046
diff changeset
213 /* XXX is it really best to add NULs if there's no more data */
fd52b6807924 Valgrind was reporting an uninitialized read for bad input.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32046
diff changeset
214 this_ch = (ascii_len > 0) ? *ascii_data : 0;
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
215 if ( this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
216 /*
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
217 ** Whitespace. Assume some spaces got eaten at
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
218 ** end-of-line. (We check this later)
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
219 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
220 this_ch = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
221 } else {
3263
8c58ab7aa71e Allow '@' for ' ' in uuencoded files.
Jack Jansen <jack.jansen@cwi.nl>
parents: 3144
diff changeset
222 /* Check the character for legality
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
223 ** The 64 in stead of the expected 63 is because
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
224 ** there are a few uuencodes out there that use
15768
e791f4905d25 Address a bug in the uuencode decoder, reported bu "donut" in SF bug
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
225 ** '`' as zero instead of space.
3263
8c58ab7aa71e Allow '@' for ' ' in uuencoded files.
Jack Jansen <jack.jansen@cwi.nl>
parents: 3144
diff changeset
226 */
8c58ab7aa71e Allow '@' for ' ' in uuencoded files.
Jack Jansen <jack.jansen@cwi.nl>
parents: 3144
diff changeset
227 if ( this_ch < ' ' || this_ch > (' ' + 64)) {
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
228 PyErr_SetString(Error, "Illegal char");
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
229 Py_DECREF(rv);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
230 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
231 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
232 this_ch = (this_ch - ' ') & 077;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
233 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
234 /*
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
235 ** Shift it in on the low end, and see if there's
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
236 ** a byte ready for output.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
237 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
238 leftchar = (leftchar << 6) | (this_ch);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
239 leftbits += 6;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
240 if ( leftbits >= 8 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
241 leftbits -= 8;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
242 *bin_data++ = (leftchar >> leftbits) & 0xff;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
243 leftchar &= ((1 << leftbits) - 1);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
244 bin_len--;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
245 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
246 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
247 /*
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
248 ** Finally, check that if there's anything left on the line
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
249 ** that it's whitespace only.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
250 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
251 while( ascii_len-- > 0 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
252 this_ch = *ascii_data++;
15768
e791f4905d25 Address a bug in the uuencode decoder, reported bu "donut" in SF bug
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
253 /* Extra '`' may be written as padding in some cases */
e791f4905d25 Address a bug in the uuencode decoder, reported bu "donut" in SF bug
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
254 if ( this_ch != ' ' && this_ch != ' '+64 &&
5771
fb8f079e4eff Allow '@' character as end of line padding in uuencode format.
Guido van Rossum <guido@python.org>
parents: 4973
diff changeset
255 this_ch != '\n' && this_ch != '\r' ) {
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
256 PyErr_SetString(Error, "Trailing garbage");
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
257 Py_DECREF(rv);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
258 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
259 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
260 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
261 return rv;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
262 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
263
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
264 PyDoc_STRVAR(doc_b2a_uu, "(bin) -> ascii. Uuencode line of data");
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
265
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
266 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
267 binascii_b2a_uu(PyObject *self, PyObject *args)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
268 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
269 unsigned char *ascii_data, *bin_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
270 int leftbits = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
271 unsigned char this_ch;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
272 unsigned int leftchar = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
273 PyObject *rv;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
274 Py_ssize_t bin_len;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
275
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
276 if ( !PyArg_ParseTuple(args, "s#:b2a_uu", &bin_data, &bin_len) )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
277 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
278 if ( bin_len > 45 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
279 /* The 45 is a limit that appears in all uuencode's */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
280 PyErr_SetString(Error, "At most 45 bytes at once");
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
281 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
282 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
283
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
284 /* We're lazy and allocate to much (fixed up later) */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
285 if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2+2)) == NULL )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
286 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
287 ascii_data = (unsigned char *)PyString_AsString(rv);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
288
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
289 /* Store the length */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
290 *ascii_data++ = ' ' + (bin_len & 077);
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
291
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
292 for( ; bin_len > 0 || leftbits != 0 ; bin_len--, bin_data++ ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
293 /* Shift the data (or padding) into our buffer */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
294 if ( bin_len > 0 ) /* Data */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
295 leftchar = (leftchar << 8) | *bin_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
296 else /* Padding */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
297 leftchar <<= 8;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
298 leftbits += 8;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
299
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
300 /* See if there are 6-bit groups ready */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
301 while ( leftbits >= 6 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
302 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
303 leftbits -= 6;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
304 *ascii_data++ = this_ch + ' ';
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
305 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
306 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
307 *ascii_data++ = '\n'; /* Append a courtesy newline */
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
308
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
309 _PyString_Resize(&rv, (ascii_data -
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
310 (unsigned char *)PyString_AsString(rv)));
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
311 return rv;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
312 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
313
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
314
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
315 static int
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
316 binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num)
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
317 {
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
318 /* Finds & returns the (num+1)th
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
319 ** valid character for base64, or -1 if none.
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
320 */
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
321
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
322 int ret = -1;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
323 unsigned char c, b64val;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
324
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
325 while ((slen > 0) && (ret == -1)) {
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
326 c = *s;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
327 b64val = table_a2b_base64[c & 0x7f];
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
328 if ( ((c <= 0x7f) && (b64val != (unsigned char)-1)) ) {
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
329 if (num == 0)
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
330 ret = *s;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
331 num--;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
332 }
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
333
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
334 s++;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
335 slen--;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
336 }
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
337 return ret;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
338 }
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
339
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
340 PyDoc_STRVAR(doc_a2b_base64, "(ascii) -> bin. Decode a line of base64 data");
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
341
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
342 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
343 binascii_a2b_base64(PyObject *self, PyObject *args)
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
344 {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
345 unsigned char *ascii_data, *bin_data;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
346 int leftbits = 0;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
347 unsigned char this_ch;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
348 unsigned int leftchar = 0;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
349 PyObject *rv;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
350 Py_ssize_t ascii_len, bin_len;
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
351 int quad_pos = 0;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
352
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
353 if ( !PyArg_ParseTuple(args, "t#:a2b_base64", &ascii_data, &ascii_len) )
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
354 return NULL;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
355
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
356 assert(ascii_len >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
357
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
358 if (ascii_len > PY_SSIZE_T_MAX - 3)
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
359 return PyErr_NoMemory();
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
360
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
361 bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
362
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
363 /* Allocate the buffer */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
364 if ( (rv=PyString_FromStringAndSize(NULL, bin_len)) == NULL )
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
365 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
366 bin_data = (unsigned char *)PyString_AsString(rv);
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
367 bin_len = 0;
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
368
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
369 for( ; ascii_len > 0; ascii_len--, ascii_data++) {
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
370 this_ch = *ascii_data;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
371
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
372 if (this_ch > 0x7f ||
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
373 this_ch == '\r' || this_ch == '\n' || this_ch == ' ')
3343
012376d3aa14 Fixed off-by-one error in rle-decode, and allow whitespace in base64
Jack Jansen <jack.jansen@cwi.nl>
parents: 3263
diff changeset
374 continue;
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
375
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
376 /* Check for pad sequences and ignore
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
377 ** the invalid ones.
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
378 */
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
379 if (this_ch == BASE64_PAD) {
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
380 if ( (quad_pos < 2) ||
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
381 ((quad_pos == 2) &&
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
382 (binascii_find_valid(ascii_data, ascii_len, 1)
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
383 != BASE64_PAD)) )
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
384 {
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
385 continue;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
386 }
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
387 else {
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
388 /* A pad sequence means no more input.
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
389 ** We've already interpreted the data
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
390 ** from the quad at this point.
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
391 */
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
392 leftbits = 0;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
393 break;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
394 }
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
395 }
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
396
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
397 this_ch = table_a2b_base64[*ascii_data];
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
398 if ( this_ch == (unsigned char) -1 )
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
399 continue;
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
400
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
401 /*
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
402 ** Shift it in on the low end, and see if there's
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
403 ** a byte ready for output.
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
404 */
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
405 quad_pos = (quad_pos + 1) & 0x03;
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
406 leftchar = (leftchar << 6) | (this_ch);
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
407 leftbits += 6;
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
408
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
409 if ( leftbits >= 8 ) {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
410 leftbits -= 8;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
411 *bin_data++ = (leftchar >> leftbits) & 0xff;
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
412 bin_len++;
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
413 leftchar &= ((1 << leftbits) - 1);
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
414 }
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
415 }
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
416
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
417 if (leftbits != 0) {
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
418 PyErr_SetString(Error, "Incorrect padding");
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
419 Py_DECREF(rv);
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
420 return NULL;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
421 }
11393
0b2e994ef175 Patch by Jason Trowbridge. (Followup to his PR#110.) (Slightly
Guido van Rossum <guido@python.org>
parents: 11390
diff changeset
422
28311
2489d22228db binascii_a2b_base64: Properly return an empty string if the input was all
Thomas Wouters <thomas@python.org>
parents: 24851
diff changeset
423 /* And set string size correctly. If the result string is empty
2489d22228db binascii_a2b_base64: Properly return an empty string if the input was all
Thomas Wouters <thomas@python.org>
parents: 24851
diff changeset
424 ** (because the input was all invalid) return the shared empty
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
425 ** string instead; _PyString_Resize() won't do this for us.
28311
2489d22228db binascii_a2b_base64: Properly return an empty string if the input was all
Thomas Wouters <thomas@python.org>
parents: 24851
diff changeset
426 */
24851
34cc98892e89 base64.decodestring('') should return '' instead of raising an
Barry Warsaw <barry@python.org>
parents: 24471
diff changeset
427 if (bin_len > 0)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
428 _PyString_Resize(&rv, bin_len);
28311
2489d22228db binascii_a2b_base64: Properly return an empty string if the input was all
Thomas Wouters <thomas@python.org>
parents: 24851
diff changeset
429 else {
2489d22228db binascii_a2b_base64: Properly return an empty string if the input was all
Thomas Wouters <thomas@python.org>
parents: 24851
diff changeset
430 Py_DECREF(rv);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
431 rv = PyString_FromString("");
28311
2489d22228db binascii_a2b_base64: Properly return an empty string if the input was all
Thomas Wouters <thomas@python.org>
parents: 24851
diff changeset
432 }
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
433 return rv;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
434 }
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
435
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
436 PyDoc_STRVAR(doc_b2a_base64, "(bin) -> ascii. Base64-code line of data");
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
437
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
438 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
439 binascii_b2a_base64(PyObject *self, PyObject *args)
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
440 {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
441 unsigned char *ascii_data, *bin_data;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
442 int leftbits = 0;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
443 unsigned char this_ch;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
444 unsigned int leftchar = 0;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
445 PyObject *rv;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
446 Py_ssize_t bin_len;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
447
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
448 if ( !PyArg_ParseTuple(args, "s#:b2a_base64", &bin_data, &bin_len) )
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
449 return NULL;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
450
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
451 assert(bin_len >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
452
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
453 if ( bin_len > BASE64_MAXBIN ) {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
454 PyErr_SetString(Error, "Too much data for base64 line");
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
455 return NULL;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
456 }
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
457
21478
b48910a95797 SF bug #494738: binascii_b2a_base64 overwrites memory.
Tim Peters <tim.peters@gmail.com>
parents: 20759
diff changeset
458 /* We're lazy and allocate too much (fixed up later).
b48910a95797 SF bug #494738: binascii_b2a_base64 overwrites memory.
Tim Peters <tim.peters@gmail.com>
parents: 20759
diff changeset
459 "+3" leaves room for up to two pad characters and a trailing
b48910a95797 SF bug #494738: binascii_b2a_base64 overwrites memory.
Tim Peters <tim.peters@gmail.com>
parents: 20759
diff changeset
460 newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
461 if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
462 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
463 ascii_data = (unsigned char *)PyString_AsString(rv);
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
464
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
465 for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
466 /* Shift the data into our buffer */
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
467 leftchar = (leftchar << 8) | *bin_data;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
468 leftbits += 8;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
469
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
470 /* See if there are 6-bit groups ready */
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
471 while ( leftbits >= 6 ) {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
472 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
473 leftbits -= 6;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
474 *ascii_data++ = table_b2a_base64[this_ch];
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
475 }
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
476 }
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
477 if ( leftbits == 2 ) {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
478 *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
479 *ascii_data++ = BASE64_PAD;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
480 *ascii_data++ = BASE64_PAD;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
481 } else if ( leftbits == 4 ) {
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
482 *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
483 *ascii_data++ = BASE64_PAD;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
484 }
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
485 *ascii_data++ = '\n'; /* Append a courtesy newline */
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
486
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
487 _PyString_Resize(&rv, (ascii_data -
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
488 (unsigned char *)PyString_AsString(rv)));
3144
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
489 return rv;
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
490 }
fbf58d278d64 Added base64 support
Jack Jansen <jack.jansen@cwi.nl>
parents: 3042
diff changeset
491
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
492 PyDoc_STRVAR(doc_a2b_hqx, "ascii -> bin, done. Decode .hqx coding");
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
493
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
494 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
495 binascii_a2b_hqx(PyObject *self, PyObject *args)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
496 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
497 unsigned char *ascii_data, *bin_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
498 int leftbits = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
499 unsigned char this_ch;
2976
4de779232aa4 Added missing 'leftchar' initializer.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2911
diff changeset
500 unsigned int leftchar = 0;
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
501 PyObject *rv;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
502 Py_ssize_t len;
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
503 int done = 0;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
504
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
505 if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
506 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
507
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
508 assert(len >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
509
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
510 if (len > PY_SSIZE_T_MAX - 2)
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
511 return PyErr_NoMemory();
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
512
33418
767a38dcb300 SF #1022953: binascii.a2b_hqx("") raises SystemError
Raymond Hettinger <python@rcn.com>
parents: 32832
diff changeset
513 /* Allocate a string that is too big (fixed later)
767a38dcb300 SF #1022953: binascii.a2b_hqx("") raises SystemError
Raymond Hettinger <python@rcn.com>
parents: 32832
diff changeset
514 Add two to the initial length to prevent interning which
767a38dcb300 SF #1022953: binascii.a2b_hqx("") raises SystemError
Raymond Hettinger <python@rcn.com>
parents: 32832
diff changeset
515 would preclude subsequent resizing. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
516 if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
517 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
518 bin_data = (unsigned char *)PyString_AsString(rv);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
519
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
520 for( ; len > 0 ; len--, ascii_data++ ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
521 /* Get the byte and look it up */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
522 this_ch = table_a2b_hqx[*ascii_data];
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
523 if ( this_ch == SKIP )
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
524 continue;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
525 if ( this_ch == FAIL ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
526 PyErr_SetString(Error, "Illegal char");
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
527 Py_DECREF(rv);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
528 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
529 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
530 if ( this_ch == DONE ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
531 /* The terminating colon */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
532 done = 1;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
533 break;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
534 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
535
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
536 /* Shift it into the buffer and see if any bytes are ready */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
537 leftchar = (leftchar << 6) | (this_ch);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
538 leftbits += 6;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
539 if ( leftbits >= 8 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
540 leftbits -= 8;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
541 *bin_data++ = (leftchar >> leftbits) & 0xff;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
542 leftchar &= ((1 << leftbits) - 1);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
543 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
544 }
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
545
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
546 if ( leftbits && !done ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
547 PyErr_SetString(Incomplete,
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
548 "String has incomplete number of bytes");
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
549 Py_DECREF(rv);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
550 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
551 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
552 _PyString_Resize(
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
553 &rv, (bin_data - (unsigned char *)PyString_AsString(rv)));
4938
70db4be1dd4b Fix leak involving BuildValue("...O...").
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
554 if (rv) {
70db4be1dd4b Fix leak involving BuildValue("...O...").
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
555 PyObject *rrv = Py_BuildValue("Oi", rv, done);
70db4be1dd4b Fix leak involving BuildValue("...O...").
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
556 Py_DECREF(rv);
70db4be1dd4b Fix leak involving BuildValue("...O...").
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
557 return rrv;
70db4be1dd4b Fix leak involving BuildValue("...O...").
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
558 }
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
559
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
560 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
561 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
562
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
563 PyDoc_STRVAR(doc_rlecode_hqx, "Binhex RLE-code binary data");
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
564
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
565 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
566 binascii_rlecode_hqx(PyObject *self, PyObject *args)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
567 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
568 unsigned char *in_data, *out_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
569 PyObject *rv;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
570 unsigned char ch;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
571 Py_ssize_t in, inend, len;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
572
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
573 if ( !PyArg_ParseTuple(args, "s#:rlecode_hqx", &in_data, &len) )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
574 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
575
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
576 assert(len >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
577
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
578 if (len > PY_SSIZE_T_MAX / 2 - 2)
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
579 return PyErr_NoMemory();
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
580
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
581 /* Worst case: output is twice as big as input (fixed later) */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
582 if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
583 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
584 out_data = (unsigned char *)PyString_AsString(rv);
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
585
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
586 for( in=0; in<len; in++) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
587 ch = in_data[in];
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
588 if ( ch == RUNCHAR ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
589 /* RUNCHAR. Escape it. */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
590 *out_data++ = RUNCHAR;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
591 *out_data++ = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
592 } else {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
593 /* Check how many following are the same */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
594 for(inend=in+1;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
595 inend<len && in_data[inend] == ch &&
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
596 inend < in+255;
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
597 inend++) ;
3042
6af0fbc8553a Obscure bugfix
Jack Jansen <jack.jansen@cwi.nl>
parents: 2976
diff changeset
598 if ( inend - in > 3 ) {
6af0fbc8553a Obscure bugfix
Jack Jansen <jack.jansen@cwi.nl>
parents: 2976
diff changeset
599 /* More than 3 in a row. Output RLE. */
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
600 *out_data++ = ch;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
601 *out_data++ = RUNCHAR;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
602 *out_data++ = inend-in;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
603 in = inend-1;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
604 } else {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
605 /* Less than 3. Output the byte itself */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
606 *out_data++ = ch;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
607 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
608 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
609 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
610 _PyString_Resize(&rv, (out_data -
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
611 (unsigned char *)PyString_AsString(rv)));
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
612 return rv;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
613 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
614
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
615 PyDoc_STRVAR(doc_b2a_hqx, "Encode .hqx data");
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
616
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
617 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
618 binascii_b2a_hqx(PyObject *self, PyObject *args)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
619 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
620 unsigned char *ascii_data, *bin_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
621 int leftbits = 0;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
622 unsigned char this_ch;
2976
4de779232aa4 Added missing 'leftchar' initializer.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2911
diff changeset
623 unsigned int leftchar = 0;
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
624 PyObject *rv;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
625 Py_ssize_t len;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
626
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
627 if ( !PyArg_ParseTuple(args, "s#:b2a_hqx", &bin_data, &len) )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
628 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
629
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
630 assert(len >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
631
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
632 if (len > PY_SSIZE_T_MAX / 2 - 2)
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
633 return PyErr_NoMemory();
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
634
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
635 /* Allocate a buffer that is at least large enough */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
636 if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
637 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
638 ascii_data = (unsigned char *)PyString_AsString(rv);
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
639
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
640 for( ; len > 0 ; len--, bin_data++ ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
641 /* Shift into our buffer, and output any 6bits ready */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
642 leftchar = (leftchar << 8) | *bin_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
643 leftbits += 8;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
644 while ( leftbits >= 6 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
645 this_ch = (leftchar >> (leftbits-6)) & 0x3f;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
646 leftbits -= 6;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
647 *ascii_data++ = table_b2a_hqx[this_ch];
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
648 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
649 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
650 /* Output a possible runt byte */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
651 if ( leftbits ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
652 leftchar <<= (6-leftbits);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
653 *ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
654 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
655 _PyString_Resize(&rv, (ascii_data -
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
656 (unsigned char *)PyString_AsString(rv)));
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
657 return rv;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
658 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
659
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
660 PyDoc_STRVAR(doc_rledecode_hqx, "Decode hexbin RLE-coded string");
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
661
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
662 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
663 binascii_rledecode_hqx(PyObject *self, PyObject *args)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
664 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
665 unsigned char *in_data, *out_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
666 unsigned char in_byte, in_repeat;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
667 PyObject *rv;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
668 Py_ssize_t in_len, out_len, out_len_left;
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
669
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
670 if ( !PyArg_ParseTuple(args, "s#:rledecode_hqx", &in_data, &in_len) )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
671 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
672
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
673 assert(in_len >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
674
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
675 /* Empty string is a special case */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
676 if ( in_len == 0 )
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
677 return PyString_FromString("");
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
678 else if (in_len > PY_SSIZE_T_MAX / 2)
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
679 return PyErr_NoMemory();
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
680
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
681 /* Allocate a buffer of reasonable size. Resized when needed */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
682 out_len = in_len*2;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
683 if ( (rv=PyString_FromStringAndSize(NULL, out_len)) == NULL )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
684 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
685 out_len_left = out_len;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
686 out_data = (unsigned char *)PyString_AsString(rv);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
687
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
688 /*
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
689 ** We need two macros here to get/put bytes and handle
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
690 ** end-of-buffer for input and output strings.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
691 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
692 #define INBYTE(b) \
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
693 do { \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
694 if ( --in_len < 0 ) { \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
695 PyErr_SetString(Incomplete, ""); \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
696 Py_DECREF(rv); \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
697 return NULL; \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
698 } \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
699 b = *in_data++; \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
700 } while(0)
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
701
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
702 #define OUTBYTE(b) \
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
703 do { \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
704 if ( --out_len_left < 0 ) { \
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
705 if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
706 _PyString_Resize(&rv, 2*out_len); \
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
707 if ( rv == NULL ) return NULL; \
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
708 out_data = (unsigned char *)PyString_AsString(rv) \
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
709 + out_len; \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
710 out_len_left = out_len-1; \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
711 out_len = out_len * 2; \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
712 } \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
713 *out_data++ = b; \
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
714 } while(0)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
715
4973
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
716 /*
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
717 ** Handle first byte separately (since we have to get angry
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
718 ** in case of an orphaned RLE code).
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
719 */
7d73cdc01198 Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4938
diff changeset
720 INBYTE(in_byte);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
721
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
722 if (in_byte == RUNCHAR) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
723 INBYTE(in_repeat);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
724 if (in_repeat != 0) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
725 /* Note Error, not Incomplete (which is at the end
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
726 ** of the string only). This is a programmer error.
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
727 */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
728 PyErr_SetString(Error, "Orphaned RLE code at start");
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
729 Py_DECREF(rv);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
730 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
731 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
732 OUTBYTE(RUNCHAR);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
733 } else {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
734 OUTBYTE(in_byte);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
735 }
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
736
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
737 while( in_len > 0 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
738 INBYTE(in_byte);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
739
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
740 if (in_byte == RUNCHAR) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
741 INBYTE(in_repeat);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
742 if ( in_repeat == 0 ) {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
743 /* Just an escaped RUNCHAR value */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
744 OUTBYTE(RUNCHAR);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
745 } else {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
746 /* Pick up value and output a sequence of it */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
747 in_byte = out_data[-1];
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
748 while ( --in_repeat > 0 )
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
749 OUTBYTE(in_byte);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
750 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
751 } else {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
752 /* Normal byte */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
753 OUTBYTE(in_byte);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
754 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
755 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
756 _PyString_Resize(&rv, (out_data -
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
757 (unsigned char *)PyString_AsString(rv)));
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
758 return rv;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
759 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
760
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
761 PyDoc_STRVAR(doc_crc_hqx,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
762 "(data, oldcrc) -> newcrc. Compute hqx CRC incrementally");
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
763
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
764 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
765 binascii_crc_hqx(PyObject *self, PyObject *args)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
766 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
767 unsigned char *bin_data;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
768 unsigned int crc;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
769 Py_ssize_t len;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
770
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11795
diff changeset
771 if ( !PyArg_ParseTuple(args, "s#i:crc_hqx", &bin_data, &len, &crc) )
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
772 return NULL;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
773
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
774 while(len-- > 0) {
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
775 crc=((crc<<8)&0xff00)^crctab_hqx[((crc>>8)&0xff)^*bin_data++];
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
776 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
777
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
778 return Py_BuildValue("i", crc);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
779 }
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
780
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
781 PyDoc_STRVAR(doc_crc32,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
782 "(data, oldcrc = 0) -> newcrc. Compute CRC-32 incrementally");
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
783
46038
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
784 #ifdef USE_ZLIB_CRC32
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
785 /* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
786 static PyObject *
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
787 binascii_crc32(PyObject *self, PyObject *args)
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
788 {
46070
4f9ca827a7d8 Use a 32-bit unsigned int here, a long is not needed.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46038
diff changeset
789 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
46038
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
790 Byte *buf;
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
791 Py_ssize_t len;
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
792 int signed_val;
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
793
46070
4f9ca827a7d8 Use a 32-bit unsigned int here, a long is not needed.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46038
diff changeset
794 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
46038
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
795 return NULL;
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
796 /* In Python 2.x we return a signed integer regardless of native platform
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
797 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
798 * extended into a 64-bit long inside the integer object). 3.0 does the
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
799 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
800 signed_val = crc32(crc32val, buf, len);
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
801 return PyInt_FromLong(signed_val);
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
802 }
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
803 #else /* USE_ZLIB_CRC32 */
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
804 /* Crc - 32 BIT ANSI X3.66 CRC checksum files
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
805 Also known as: ISO 3307
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
806 **********************************************************************|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
807 * *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
808 * Demonstration program to compute the 32-bit CRC used as the frame *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
809 * check sequence in ADCCP (ANSI X3.66, also known as FIPS PUB 71 *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
810 * and FED-STD-1003, the U.S. versions of CCITT's X.25 link-level *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
811 * protocol). The 32-bit FCS was added via the Federal Register, *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
812 * 1 June 1982, p.23798. I presume but don't know for certain that *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
813 * this polynomial is or will be included in CCITT V.41, which *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
814 * defines the 16-bit CRC (often called CRC-CCITT) polynomial. FIPS *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
815 * PUB 78 says that the 32-bit FCS reduces otherwise undetected *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
816 * errors by a factor of 10^-5 over 16-bit FCS. *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
817 * *|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
818 **********************************************************************|
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
819
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
820 Copyright (C) 1986 Gary S. Brown. You may use this program, or
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
821 code or tables extracted from it, as desired without restriction.
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
822
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
823 First, the polynomial itself and its table of feedback terms. The
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
824 polynomial is
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
825 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
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
826 Note that we take it "backwards" and put the highest-order term in
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
827 the lowest-order bit. The X^32 term is "implied"; the LSB is the
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
828 X^31 term, etc. The X^0 term (usually shown as "+1") results in
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
829 the MSB being 1.
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
830
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
831 Note that the usual hardware shift register implementation, which
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
832 is what we're using (we're merely optimizing it by doing eight-bit
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
833 chunks at a time) shifts bits into the lowest-order term. In our
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
834 implementation, that means shifting towards the right. Why do we
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
835 do it this way? Because the calculated CRC must be transmitted in
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
836 order from highest-order term to lowest-order term. UARTs transmit
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
837 characters in order from LSB to MSB. By storing the CRC this way,
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
838 we hand it to the UART in the order low-byte to high-byte; the UART
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
839 sends each low-bit to hight-bit; and the result is transmission bit
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
840 by bit from highest- to lowest-order term without requiring any bit
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
841 shuffling on our part. Reception works similarly.
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
842
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
843 The feedback terms table consists of 256, 32-bit entries. Notes:
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
844
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
845 1. The table can be generated at runtime if desired; code to do so
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
846 is shown later. It might not be obvious, but the feedback
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
847 terms simply represent the results of eight shift/xor opera-
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
848 tions for all combinations of data and CRC register values.
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
849
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
850 2. The CRC accumulation logic is the same for all CRC polynomials,
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
851 be they sixteen or thirty-two bits wide. You simply choose the
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
852 appropriate table. Alternatively, because the table can be
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
853 generated at runtime, you can start by generating the table for
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
854 the polynomial in question and use exactly the same "updcrc",
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
855 if your application needn't simultaneously handle two CRC
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
856 polynomials. (Note, however, that XMODEM is strange.)
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
857
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
858 3. For 16-bit CRCs, the table entries need be only 16 bits wide;
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
859 of course, 32-bit entries work OK if the high 16 bits are zero.
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
860
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
861 4. The values must be right-shifted by eight bits by the "updcrc"
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
862 logic; the shift must be unsigned (bring in zeroes). On some
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
863 hardware you could probably optimize the shift in assembler by
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
864 using byte-swap instructions.
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
865 ********************************************************************/
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
866
46071
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
867 static unsigned int crc_32_tab[256] = {
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
868 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
869 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
870 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
871 0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
872 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
873 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
874 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
875 0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
876 0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
877 0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
878 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
879 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
880 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
881 0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
882 0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
883 0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
884 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
885 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
886 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
887 0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
888 0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
889 0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
890 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
891 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
892 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
893 0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
894 0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
895 0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
896 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
897 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
898 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
899 0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
900 0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
901 0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
902 0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
903 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
904 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
905 0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
906 0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
907 0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
908 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
909 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
910 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
911 0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
912 0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
913 0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
914 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
915 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
916 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
917 0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
918 0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
919 0x2d02ef8dU
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
920 };
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
921
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
922 static PyObject *
13496
03bc51e43f7a ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13255
diff changeset
923 binascii_crc32(PyObject *self, PyObject *args)
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
924 { /* By Jim Ahlstrom; All rights transferred to CNRI */
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
925 unsigned char *bin_data;
46071
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
926 unsigned int crc = 0U; /* initial value of CRC */
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
927 Py_ssize_t len;
46071
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
928 int result;
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
929
46071
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
930 if ( !PyArg_ParseTuple(args, "s#|I:crc32", &bin_data, &len, &crc) )
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
931 return NULL;
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
932
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
933 crc = ~ crc;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
934 while (len-- > 0)
46071
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
935 crc = crc_32_tab[(crc ^ *bin_data++) & 0xffU] ^ (crc >> 8);
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
936 /* Note: (crc >> 8) MUST zero fill on left */
24089
b185c85952fe Fix for SF bug #576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 23832
diff changeset
937
46071
0dd441ca8eb0 On platforms without zlib, make this do the right thing and return the python
Gregory P. Smith <greg@mad-scientist.com>
parents: 46070
diff changeset
938 result = (int)(crc ^ 0xFFFFFFFFU);
24089
b185c85952fe Fix for SF bug #576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 23832
diff changeset
939 return PyInt_FromLong(result);
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
940 }
46038
5ca928402e54 Have the binascii module use zlib's optimized crc32() function when available
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
941 #endif /* USE_ZLIB_CRC32 */
11795
63ce09df607d Patch by Jim Ahlstrom to add crc32, a useful checksum function
Guido van Rossum <guido@python.org>
parents: 11393
diff changeset
942
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
943
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
944 static PyObject *
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
945 binascii_hexlify(PyObject *self, PyObject *args)
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
946 {
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
947 char* argbuf;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
948 Py_ssize_t arglen;
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
949 PyObject *retval;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
950 char* retbuf;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
951 Py_ssize_t i, j;
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
952
38674
6ba7d4667986 Make binascii.hexlify() use s# for its arguments instead of t# to actually
Brett Cannon <bcannon@gmail.com>
parents: 38485
diff changeset
953 if (!PyArg_ParseTuple(args, "s#:b2a_hex", &argbuf, &arglen))
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
954 return NULL;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
955
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
956 assert(arglen >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
957 if (arglen > PY_SSIZE_T_MAX / 2)
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
958 return PyErr_NoMemory();
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
959
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
960 retval = PyString_FromStringAndSize(NULL, arglen*2);
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
961 if (!retval)
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
962 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
963 retbuf = PyString_AsString(retval);
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
964 if (!retbuf)
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
965 goto finally;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
966
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
967 /* make hex version of string, taken from shamodule.c */
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
968 for (i=j=0; i < arglen; i++) {
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
969 char c;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
970 c = (argbuf[i] >> 4) & 0xf;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
971 c = (c>9) ? c+'a'-10 : c + '0';
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
972 retbuf[j++] = c;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
973 c = argbuf[i] & 0xf;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
974 c = (c>9) ? c+'a'-10 : c + '0';
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
975 retbuf[j++] = c;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
976 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
977 return retval;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
978
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
979 finally:
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
980 Py_DECREF(retval);
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
981 return NULL;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
982 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
983
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
984 PyDoc_STRVAR(doc_hexlify,
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
985 "b2a_hex(data) -> s; Hexadecimal representation of binary data.\n\
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
986 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
987 This function is also available as \"hexlify()\".");
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
988
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
989
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
990 static int
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
991 to_int(int c)
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
992 {
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
993 if (isdigit(c))
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
994 return c - '0';
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
995 else {
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
996 if (isupper(c))
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
997 c = tolower(c);
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
998 if (c >= 'a' && c <= 'f')
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
999 return c - 'a' + 10;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1000 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1001 return -1;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1002 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1003
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1004
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1005 static PyObject *
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1006 binascii_unhexlify(PyObject *self, PyObject *args)
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1007 {
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1008 char* argbuf;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1009 Py_ssize_t arglen;
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1010 PyObject *retval;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1011 char* retbuf;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1012 Py_ssize_t i, j;
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1013
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1014 if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen))
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1015 return NULL;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1016
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1017 assert(arglen >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1018
13986
400a5e3ac76c binascii_unhexlify(): Better error message, courtesy effbot.
Barry Warsaw <barry@python.org>
parents: 13983
diff changeset
1019 /* XXX What should we do about strings with an odd length? Should
400a5e3ac76c binascii_unhexlify(): Better error message, courtesy effbot.
Barry Warsaw <barry@python.org>
parents: 13983
diff changeset
1020 * we add an implicit leading zero, or a trailing zero? For now,
400a5e3ac76c binascii_unhexlify(): Better error message, courtesy effbot.
Barry Warsaw <barry@python.org>
parents: 13983
diff changeset
1021 * raise an exception.
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1022 */
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1023 if (arglen % 2) {
13986
400a5e3ac76c binascii_unhexlify(): Better error message, courtesy effbot.
Barry Warsaw <barry@python.org>
parents: 13983
diff changeset
1024 PyErr_SetString(PyExc_TypeError, "Odd-length string");
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1025 return NULL;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1026 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1027
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1028 retval = PyString_FromStringAndSize(NULL, (arglen/2));
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1029 if (!retval)
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1030 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1031 retbuf = PyString_AsString(retval);
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1032 if (!retbuf)
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1033 goto finally;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1034
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1035 for (i=j=0; i < arglen; i += 2) {
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1036 int top = to_int(Py_CHARMASK(argbuf[i]));
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1037 int bot = to_int(Py_CHARMASK(argbuf[i+1]));
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1038 if (top == -1 || bot == -1) {
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1039 PyErr_SetString(PyExc_TypeError,
13986
400a5e3ac76c binascii_unhexlify(): Better error message, courtesy effbot.
Barry Warsaw <barry@python.org>
parents: 13983
diff changeset
1040 "Non-hexadecimal digit found");
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1041 goto finally;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1042 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1043 retbuf[j++] = (top << 4) + bot;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1044 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1045 return retval;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1046
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1047 finally:
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1048 Py_DECREF(retval);
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1049 return NULL;
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1050 }
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1051
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
1052 PyDoc_STRVAR(doc_unhexlify,
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1053 "a2b_hex(hexstr) -> s; Binary data of hexadecimal representation.\n\
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1054 \n\
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1055 hexstr must contain an even number of hex digits (upper or lower case).\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
1056 This function is also available as \"unhexlify()\"");
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1057
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1058 static int table_hex[128] = {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1059 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1060 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1061 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1062 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1063 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1064 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1065 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1066 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1067 };
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1068
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1069 #define hexval(c) table_hex[(unsigned int)(c)]
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1070
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1071 #define MAXLINESIZE 76
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1072
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
1073 PyDoc_STRVAR(doc_a2b_qp, "Decode a string of qp-encoded data");
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1074
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1075 static PyObject*
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1076 binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1077 {
36789
0e2cd94c53a9 Use Py_ssize_t for arithmetic on Py_ssize_t's, instead of unsigned ints.
Thomas Wouters <thomas@python.org>
parents: 36779
diff changeset
1078 Py_ssize_t in, out;
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1079 char ch;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1080 unsigned char *data, *odata;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1081 Py_ssize_t datalen = 0;
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1082 PyObject *rv;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1083 static char *kwlist[] = {"data", "header", NULL};
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1084 int header = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1085
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1086 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i", kwlist, &data,
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1087 &datalen, &header))
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1088 return NULL;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1089
32046
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1090 /* We allocate the output same size as input, this is overkill.
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1091 * The previous implementation used calloc() so we'll zero out the
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1092 * memory here too, since PyMem_Malloc() does not guarantee that.
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1093 */
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1094 odata = (unsigned char *) PyMem_Malloc(datalen);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1095 if (odata == NULL) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1096 PyErr_NoMemory();
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1097 return NULL;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1098 }
32832
fa4c7063a736 Switch arguments to memset (kudos to MSVC C4318 for finding that)
Martin v. Löwis <martin@v.loewis.de>
parents: 32282
diff changeset
1099 memset(odata, 0, datalen);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1100
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1101 in = out = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1102 while (in < datalen) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1103 if (data[in] == '=') {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1104 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1105 if (in >= datalen) break;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1106 /* Soft line breaks */
40346
2515ec4b5b33 Bug #1588217: don't parse "= " as a soft line break in binascii's
Georg Brandl <georg@python.org>
parents: 38674
diff changeset
1107 if ((data[in] == '\n') || (data[in] == '\r')) {
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1108 if (data[in] != '\n') {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1109 while (in < datalen && data[in] != '\n') in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1110 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1111 if (in < datalen) in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1112 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1113 else if (data[in] == '=') {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1114 /* broken case from broken python qp */
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1115 odata[out++] = '=';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1116 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1117 }
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1118 else if (((data[in] >= 'A' && data[in] <= 'F') ||
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1119 (data[in] >= 'a' && data[in] <= 'f') ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1120 (data[in] >= '0' && data[in] <= '9')) &&
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1121 ((data[in+1] >= 'A' && data[in+1] <= 'F') ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1122 (data[in+1] >= 'a' && data[in+1] <= 'f') ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1123 (data[in+1] >= '0' && data[in+1] <= '9'))) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1124 /* hexval */
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1125 ch = hexval(data[in]) << 4;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1126 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1127 ch |= hexval(data[in]);
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1128 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1129 odata[out++] = ch;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1130 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1131 else {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1132 odata[out++] = '=';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1133 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1134 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1135 else if (header && data[in] == '_') {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1136 odata[out++] = ' ';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1137 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1138 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1139 else {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1140 odata[out] = data[in];
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1141 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1142 out++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1143 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1144 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1145 if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
32046
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1146 PyMem_Free(odata);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1147 return NULL;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1148 }
32046
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1149 PyMem_Free(odata);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1150 return rv;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1151 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1152
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1153 static int
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1154 to_hex (unsigned char ch, unsigned char *s)
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1155 {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1156 unsigned int uvalue = ch;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1157
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1158 s[1] = "0123456789ABCDEF"[uvalue % 16];
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1159 uvalue = (uvalue / 16);
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1160 s[0] = "0123456789ABCDEF"[uvalue % 16];
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1161 return 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1162 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1163
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
1164 PyDoc_STRVAR(doc_b2a_qp,
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1165 "b2a_qp(data, quotetabs=0, istext=1, header=0) -> s; \n\
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1166 Encode a string using quoted-printable encoding. \n\
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1167 \n\
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1168 On encoding, when istext is set, newlines are not encoded, and white \n\
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1169 space at end of lines is. When istext is not set, \\r and \\n (CR/LF) are \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
1170 both encoded. When quotetabs is set, space and tabs are encoded.");
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1171
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1172 /* XXX: This is ridiculously complicated to be backward compatible
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1173 * (mostly) with the quopri module. It doesn't re-create the quopri
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1174 * module bug where text ending in CRLF has the CR encoded */
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1175 static PyObject*
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1176 binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1177 {
36789
0e2cd94c53a9 Use Py_ssize_t for arithmetic on Py_ssize_t's, instead of unsigned ints.
Thomas Wouters <thomas@python.org>
parents: 36779
diff changeset
1178 Py_ssize_t in, out;
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1179 unsigned char *data, *odata;
36776
1aea3aeca535 Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1180 Py_ssize_t datalen = 0, odatalen = 0;
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1181 PyObject *rv;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1182 unsigned int linelen = 0;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1183 static char *kwlist[] = {"data", "quotetabs", "istext",
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33419
diff changeset
1184 "header", NULL};
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1185 int istext = 1;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1186 int quotetabs = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1187 int header = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1188 unsigned char ch;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1189 int crlf = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1190 unsigned char *p;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1191
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1192 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|iii", kwlist, &data,
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1193 &datalen, &quotetabs, &istext, &header))
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1194 return NULL;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1195
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1196 /* See if this string is using CRLF line ends */
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1197 /* XXX: this function has the side effect of converting all of
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1198 * the end of lines to be the same depending on this detection
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1199 * here */
41663
475c7c5437fc Forwardport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 41177
diff changeset
1200 p = (unsigned char *) memchr(data, '\n', datalen);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1201 if ((p != NULL) && (p > data) && (*(p-1) == '\r'))
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1202 crlf = 1;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1203
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1204 /* First, scan to see how many characters need to be encoded */
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1205 in = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1206 while (in < datalen) {
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1207 if ((data[in] > 126) ||
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1208 (data[in] == '=') ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1209 (header && data[in] == '_') ||
41177
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1210 ((data[in] == '.') && (linelen == 0) &&
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1211 (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1212 (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1213 ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1214 ((data[in] < 33) &&
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1215 (data[in] != '\r') && (data[in] != '\n') &&
41177
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1216 (quotetabs ||
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1217 (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1218 {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1219 if ((linelen + 3) >= MAXLINESIZE) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1220 linelen = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1221 if (crlf)
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1222 odatalen += 3;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1223 else
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1224 odatalen += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1225 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1226 linelen += 3;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1227 odatalen += 3;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1228 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1229 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1230 else {
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1231 if (istext &&
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1232 ((data[in] == '\n') ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1233 ((in+1 < datalen) && (data[in] == '\r') &&
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1234 (data[in+1] == '\n'))))
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1235 {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1236 linelen = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1237 /* Protect against whitespace on end of line */
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1238 if (in && ((data[in-1] == ' ') || (data[in-1] == '\t')))
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1239 odatalen += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1240 if (crlf)
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1241 odatalen += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1242 else
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1243 odatalen += 1;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1244 if (data[in] == '\r')
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1245 in += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1246 else
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1247 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1248 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1249 else {
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1250 if ((in + 1 != datalen) &&
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1251 (data[in+1] != '\n') &&
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1252 (linelen + 1) >= MAXLINESIZE) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1253 linelen = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1254 if (crlf)
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1255 odatalen += 3;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1256 else
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1257 odatalen += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1258 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1259 linelen++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1260 odatalen++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1261 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1262 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1263 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1264 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1265
32046
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1266 /* We allocate the output same size as input, this is overkill.
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1267 * The previous implementation used calloc() so we'll zero out the
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1268 * memory here too, since PyMem_Malloc() does not guarantee that.
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1269 */
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1270 odata = (unsigned char *) PyMem_Malloc(odatalen);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1271 if (odata == NULL) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1272 PyErr_NoMemory();
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1273 return NULL;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1274 }
32832
fa4c7063a736 Switch arguments to memset (kudos to MSVC C4318 for finding that)
Martin v. Löwis <martin@v.loewis.de>
parents: 32282
diff changeset
1275 memset(odata, 0, odatalen);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1276
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1277 in = out = linelen = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1278 while (in < datalen) {
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1279 if ((data[in] > 126) ||
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1280 (data[in] == '=') ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1281 (header && data[in] == '_') ||
41177
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1282 ((data[in] == '.') && (linelen == 0) &&
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1283 (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1284 (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1285 ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1286 ((data[in] < 33) &&
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1287 (data[in] != '\r') && (data[in] != '\n') &&
41177
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1288 (quotetabs ||
5a1a0aa9a4bb Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
Georg Brandl <georg@python.org>
parents: 40346
diff changeset
1289 (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1290 {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1291 if ((linelen + 3 )>= MAXLINESIZE) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1292 odata[out++] = '=';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1293 if (crlf) odata[out++] = '\r';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1294 odata[out++] = '\n';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1295 linelen = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1296 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1297 odata[out++] = '=';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1298 to_hex(data[in], &odata[out]);
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1299 out += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1300 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1301 linelen += 3;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1302 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1303 else {
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1304 if (istext &&
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1305 ((data[in] == '\n') ||
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1306 ((in+1 < datalen) && (data[in] == '\r') &&
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1307 (data[in+1] == '\n'))))
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1308 {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1309 linelen = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1310 /* Protect against whitespace on end of line */
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1311 if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1312 ch = odata[out-1];
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1313 odata[out-1] = '=';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1314 to_hex(ch, &odata[out]);
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1315 out += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1316 }
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1317
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1318 if (crlf) odata[out++] = '\r';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1319 odata[out++] = '\n';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1320 if (data[in] == '\r')
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1321 in += 2;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1322 else
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1323 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1324 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1325 else {
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1326 if ((in + 1 != datalen) &&
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1327 (data[in+1] != '\n') &&
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1328 (linelen + 1) >= MAXLINESIZE) {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1329 odata[out++] = '=';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1330 if (crlf) odata[out++] = '\r';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1331 odata[out++] = '\n';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1332 linelen = 0;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1333 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1334 linelen++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1335 if (header && data[in] == ' ') {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1336 odata[out++] = '_';
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1337 in++;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1338 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1339 else {
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1340 odata[out++] = data[in++];
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1341 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1342 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1343 }
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1344 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1345 if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
32046
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1346 PyMem_Free(odata);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1347 return NULL;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1348 }
32046
5a22465fda24 In order to fix SF bug # 824977, we replace calloc()/free() calls in
Barry Warsaw <barry@python.org>
parents: 28311
diff changeset
1349 PyMem_Free(odata);
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1350 return rv;
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1351 }
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1352
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1353 /* List of functions defined in the module */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1354
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1355 static struct PyMethodDef binascii_module_methods[] = {
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1356 {"a2b_uu", binascii_a2b_uu, METH_VARARGS, doc_a2b_uu},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1357 {"b2a_uu", binascii_b2a_uu, METH_VARARGS, doc_b2a_uu},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1358 {"a2b_base64", binascii_a2b_base64, METH_VARARGS, doc_a2b_base64},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1359 {"b2a_base64", binascii_b2a_base64, METH_VARARGS, doc_b2a_base64},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1360 {"a2b_hqx", binascii_a2b_hqx, METH_VARARGS, doc_a2b_hqx},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1361 {"b2a_hqx", binascii_b2a_hqx, METH_VARARGS, doc_b2a_hqx},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1362 {"b2a_hex", binascii_hexlify, METH_VARARGS, doc_hexlify},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1363 {"a2b_hex", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1364 {"hexlify", binascii_hexlify, METH_VARARGS, doc_hexlify},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1365 {"unhexlify", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1366 {"rlecode_hqx", binascii_rlecode_hqx, METH_VARARGS, doc_rlecode_hqx},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1367 {"rledecode_hqx", binascii_rledecode_hqx, METH_VARARGS,
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1368 doc_rledecode_hqx},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1369 {"crc_hqx", binascii_crc_hqx, METH_VARARGS, doc_crc_hqx},
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1370 {"crc32", binascii_crc32, METH_VARARGS, doc_crc32},
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1371 {"a2b_qp", (PyCFunction)binascii_a2b_qp, METH_VARARGS | METH_KEYWORDS,
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1372 doc_a2b_qp},
24098
cfcfb5df6e61 Another stab at SF 576327: zipfile when sizeof(long) == 8
Tim Peters <tim.peters@gmail.com>
parents: 24089
diff changeset
1373 {"b2a_qp", (PyCFunction)binascii_b2a_qp, METH_VARARGS | METH_KEYWORDS,
20187
8e4e49105595 Patch #462190, patch #464070: Support quoted printable in the binascii module.
Martin v. Löwis <martin@v.loewis.de>
parents: 18049
diff changeset
1374 doc_b2a_qp},
13983
1b107c9ba702 After a brief conversation and code review with TP, adding two very
Barry Warsaw <barry@python.org>
parents: 13817
diff changeset
1375 {NULL, NULL} /* sentinel */
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1376 };
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1377
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1378
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1379 /* Initialization function for the module (*must* be called initbinascii) */
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 21478
diff changeset
1380 PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1381
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24098
diff changeset
1382 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13496
diff changeset
1383 initbinascii(void)
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1384 {
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1385 PyObject *m, *d, *x;
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1386
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1387 /* Create the module and add the functions */
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1388 m = Py_InitModule("binascii", binascii_module_methods);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1389 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1390 return;
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1391
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1392 d = PyModule_GetDict(m);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1393 x = PyString_FromString(doc_binascii);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1394 PyDict_SetItemString(d, "__doc__", x);
5929
b3c913746900 Plug memory leak (DECREF doc string properly after inserting in dict).
Guido van Rossum <guido@python.org>
parents: 5771
diff changeset
1395 Py_XDECREF(x);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1396
6456
2c5c399b6ea4 Converted to new exception style (Jack)
Guido van Rossum <guido@python.org>
parents: 5929
diff changeset
1397 Error = PyErr_NewException("binascii.Error", NULL, NULL);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1398 PyDict_SetItemString(d, "Error", Error);
6456
2c5c399b6ea4 Converted to new exception style (Jack)
Guido van Rossum <guido@python.org>
parents: 5929
diff changeset
1399 Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL);
2911
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1400 PyDict_SetItemString(d, "Incomplete", Incomplete);
429d7e1b1f2b Helper module for uuencode and binhex coders
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1401 }