@@ -204,29 +204,86 @@ STATIC mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) {
204
204
}
205
205
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mod_binascii_b2a_base64_obj , mod_binascii_b2a_base64 );
206
206
207
- #if MICROPY_PY_UBINASCII_CRC32
208
- #include "lib/uzlib/tinf.h"
207
+ /*
208
+ * CRC32 checksum
209
+ *
210
+ * Copyright (c) 1998-2003 by Joergen Ibsen / Jibz
211
+ * All Rights Reserved
212
+ *
213
+ * http://www.ibsensoftware.com/
214
+ *
215
+ * This software is provided 'as-is', without any express
216
+ * or implied warranty. In no event will the authors be
217
+ * held liable for any damages arising from the use of
218
+ * this software.
219
+ *
220
+ * Permission is granted to anyone to use this software
221
+ * for any purpose, including commercial applications,
222
+ * and to alter it and redistribute it freely, subject to
223
+ * the following restrictions:
224
+ *
225
+ * 1. The origin of this software must not be
226
+ * misrepresented; you must not claim that you
227
+ * wrote the original software. If you use this
228
+ * software in a product, an acknowledgment in
229
+ * the product documentation would be appreciated
230
+ * but is not required.
231
+ *
232
+ * 2. Altered source versions must be plainly marked
233
+ * as such, and must not be misrepresented as
234
+ * being the original software.
235
+ *
236
+ * 3. This notice may not be removed or altered from
237
+ * any source distribution.
238
+ */
239
+
240
+ /*
241
+ * CRC32 algorithm taken from the zlib source, which is
242
+ * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
243
+ */
244
+
245
+
246
+ static const unsigned int tinf_crc32tab [16 ] = {
247
+ 0x00000000 , 0x1db71064 , 0x3b6e20c8 , 0x26d930ac , 0x76dc4190 ,
248
+ 0x6b6b51f4 , 0x4db26158 , 0x5005713c , 0xedb88320 , 0xf00f9344 ,
249
+ 0xd6d6a3e8 , 0xcb61b38c , 0x9b64c2b0 , 0x86d3d2d4 , 0xa00ae278 ,
250
+ 0xbdbdf21c
251
+ };
252
+
253
+ /* crc is previous value for incremental computation, 0xffffffff initially */
254
+ static uint32_t from_uzlib_crc32 (const void * data , unsigned int length , uint32_t crc ) {
255
+ const unsigned char * buf = (const unsigned char * )data ;
256
+ unsigned int i ;
257
+
258
+ for (i = 0 ; i < length ; ++ i )
259
+ {
260
+ crc ^= buf [i ];
261
+ crc = tinf_crc32tab [crc & 0x0f ] ^ (crc >> 4 );
262
+ crc = tinf_crc32tab [crc & 0x0f ] ^ (crc >> 4 );
263
+ }
264
+
265
+ // return value suitable for passing in next time, for final value invert it
266
+ return crc /* ^ 0xffffffff*/ ;
267
+ }
209
268
210
269
STATIC mp_obj_t mod_binascii_crc32 (size_t n_args , const mp_obj_t * args ) {
211
270
mp_buffer_info_t bufinfo ;
212
271
check_not_unicode (args [0 ]);
213
272
mp_get_buffer_raise (args [0 ], & bufinfo , MP_BUFFER_READ );
214
273
uint32_t crc = (n_args > 1 ) ? mp_obj_get_int_truncated (args [1 ]) : 0 ;
215
- crc = uzlib_crc32 (bufinfo .buf , bufinfo .len , crc ^ 0xffffffff );
274
+ crc = from_uzlib_crc32 (bufinfo .buf , bufinfo .len , crc ^ 0xffffffff );
216
275
return mp_obj_new_int_from_uint (crc ^ 0xffffffff );
217
276
}
218
277
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_binascii_crc32_obj , 1 , 2 , mod_binascii_crc32 );
219
- #endif
278
+
220
279
221
280
STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table [] = {
222
281
{ MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_binascii ) },
223
282
{ MP_ROM_QSTR (MP_QSTR_hexlify ), MP_ROM_PTR (& mod_binascii_hexlify_obj ) },
224
283
{ MP_ROM_QSTR (MP_QSTR_unhexlify ), MP_ROM_PTR (& mod_binascii_unhexlify_obj ) },
225
284
{ MP_ROM_QSTR (MP_QSTR_a2b_base64 ), MP_ROM_PTR (& mod_binascii_a2b_base64_obj ) },
226
285
{ MP_ROM_QSTR (MP_QSTR_b2a_base64 ), MP_ROM_PTR (& mod_binascii_b2a_base64_obj ) },
227
- #if MICROPY_PY_UBINASCII_CRC32
228
286
{ MP_ROM_QSTR (MP_QSTR_crc32 ), MP_ROM_PTR (& mod_binascii_crc32_obj ) },
229
- #endif
230
287
};
231
288
232
289
STATIC MP_DEFINE_CONST_DICT (mp_module_binascii_globals , mp_module_binascii_globals_table );
0 commit comments