Skip to content

Commit 29cb37d

Browse files
committed
copied crc32 code from uzlib to modubinascii.c
removed dependency on build flag
1 parent 5435a40 commit 29cb37d

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

extmod/modubinascii.c

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,29 +204,86 @@ STATIC mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) {
204204
}
205205
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64);
206206

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+
}
209268

210269
STATIC mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) {
211270
mp_buffer_info_t bufinfo;
212271
check_not_unicode(args[0]);
213272
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
214273
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);
216275
return mp_obj_new_int_from_uint(crc ^ 0xffffffff);
217276
}
218277
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_binascii_crc32);
219-
#endif
278+
220279

221280
STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = {
222281
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_binascii) },
223282
{ MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) },
224283
{ MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) },
225284
{ MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) },
226285
{ MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) },
227-
#if MICROPY_PY_UBINASCII_CRC32
228286
{ MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&mod_binascii_crc32_obj) },
229-
#endif
230287
};
231288

232289
STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);

0 commit comments

Comments
 (0)