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