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