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