|
1 | 1 | // Copyright (c) 2014 Paul Sokolovsky
|
2 | 2 | // SPDX-FileCopyrightText: 2014 MicroPython & CircuitPython contributors (https://github.com/adafruit/circuitpython/graphs/contributors)
|
| 3 | +// SPDX-FileCopyrightText: 2022 Beat Ludin for Adafruit Industries |
3 | 4 | //
|
4 | 5 | // SPDX-License-Identifier: MIT
|
5 | 6 |
|
@@ -204,29 +205,86 @@ STATIC mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) {
|
204 | 205 | }
|
205 | 206 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64);
|
206 | 207 |
|
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 | +} |
209 | 269 |
|
210 | 270 | STATIC mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) {
|
211 | 271 | mp_buffer_info_t bufinfo;
|
212 | 272 | check_not_unicode(args[0]);
|
213 | 273 | mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
|
214 | 274 | 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); |
216 | 276 | return mp_obj_new_int_from_uint(crc ^ 0xffffffff);
|
217 | 277 | }
|
218 | 278 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_binascii_crc32);
|
219 |
| -#endif |
| 279 | + |
220 | 280 |
|
221 | 281 | STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = {
|
222 | 282 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_binascii) },
|
223 | 283 | { MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) },
|
224 | 284 | { MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) },
|
225 | 285 | { MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) },
|
226 | 286 | { MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) },
|
227 |
| - #if MICROPY_PY_UBINASCII_CRC32 |
228 | 287 | { MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&mod_binascii_crc32_obj) },
|
229 |
| - #endif |
230 | 288 | };
|
231 | 289 |
|
232 | 290 | STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);
|
|
0 commit comments