Skip to content

Commit 0129735

Browse files
authored
Merge pull request #5969 from Life-Imaging-Services/crc32
crc32() code moved into binascii
2 parents 4dc9b00 + 0d43e3e commit 0129735

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

extmod/modubinascii.c

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2014 Paul Sokolovsky
22
// SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
3+
// SPDX-FileCopyrightText: 2022 Beat Ludin for Adafruit Industries
34
//
45
// SPDX-License-Identifier: MIT
56

@@ -204,29 +205,86 @@ STATIC mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) {
204205
}
205206
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64);
206207

207-
#if MICROPY_PY_UBINASCII_CRC32
208-
#include "lib/uzlib/tinf.h"
208+
/*
209+
* CRC32 checksum
210+
*
211+
* Copyright (c) 1998-2003 by Joergen Ibsen / Jibz
212+
* All Rights Reserved
213+
*
214+
* http://www.ibsensoftware.com/
215+
*
216+
* This software is provided 'as-is', without any express
217+
* or implied warranty. In no event will the authors be
218+
* held liable for any damages arising from the use of
219+
* this software.
220+
*
221+
* Permission is granted to anyone to use this software
222+
* for any purpose, including commercial applications,
223+
* and to alter it and redistribute it freely, subject to
224+
* the following restrictions:
225+
*
226+
* 1. The origin of this software must not be
227+
* misrepresented; you must not claim that you
228+
* wrote the original software. If you use this
229+
* software in a product, an acknowledgment in
230+
* the product documentation would be appreciated
231+
* but is not required.
232+
*
233+
* 2. Altered source versions must be plainly marked
234+
* as such, and must not be misrepresented as
235+
* being the original software.
236+
*
237+
* 3. This notice may not be removed or altered from
238+
* any source distribution.
239+
*/
240+
241+
/*
242+
* CRC32 algorithm taken from the zlib source, which is
243+
* Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
244+
*/
245+
246+
247+
static const unsigned int tinf_crc32tab[16] = {
248+
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190,
249+
0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344,
250+
0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278,
251+
0xbdbdf21c
252+
};
253+
254+
/* crc is previous value for incremental computation, 0xffffffff initially */
255+
static uint32_t from_uzlib_crc32(const void *data, unsigned int length, uint32_t crc) {
256+
const unsigned char *buf = (const unsigned char *)data;
257+
unsigned int i;
258+
259+
for (i = 0; i < length; ++i)
260+
{
261+
crc ^= buf[i];
262+
crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
263+
crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
264+
}
265+
266+
// return value suitable for passing in next time, for final value invert it
267+
return crc /* ^ 0xffffffff*/;
268+
}
209269

210270
STATIC mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) {
211271
mp_buffer_info_t bufinfo;
212272
check_not_unicode(args[0]);
213273
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
214274
uint32_t crc = (n_args > 1) ? mp_obj_get_int_truncated(args[1]) : 0;
215-
crc = uzlib_crc32(bufinfo.buf, bufinfo.len, crc ^ 0xffffffff);
275+
crc = from_uzlib_crc32(bufinfo.buf, bufinfo.len, crc ^ 0xffffffff);
216276
return mp_obj_new_int_from_uint(crc ^ 0xffffffff);
217277
}
218278
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_binascii_crc32);
219-
#endif
279+
220280

221281
STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = {
222282
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_binascii) },
223283
{ MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) },
224284
{ MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) },
225285
{ MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) },
226286
{ MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) },
227-
#if MICROPY_PY_UBINASCII_CRC32
228287
{ MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&mod_binascii_crc32_obj) },
229-
#endif
230288
};
231289

232290
STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);

ports/atmel-samd/boards/datum_imu/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
99
INTERNAL_FLASH_FILESYSTEM = 1
1010
LONGINT_IMPL = NONE
1111
CIRCUITPY_FULL_BUILD = 0
12+
13+
CIRCUITPY_ONEWIREIO = 0

0 commit comments

Comments
 (0)