Nuitka
The Python compiler
Loading...
Searching...
No Matches
HelpersChecksumTools.c
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3// This file is included from another C file, help IDEs to still parse it on
4// its own.
5#ifdef __IDE_ONLY__
6#include "nuitka/prelude.h"
7#endif
8
9// Comment in to disable outside zlib usage for code size, very slow though,
10// since it doesn't use assembly to use CPU crc32 instructions.
11// #define _NUITKA_USE_OWN_CRC32
12
13#ifdef _NUITKA_USE_OWN_CRC32
14uint32_t _initCRC32(void) { return 0xFFFFFFFF; }
15
16uint32_t _updateCRC32(uint32_t crc, unsigned char const *message, uint32_t size) {
17 for (uint32_t i = 0; i < size; i++) {
18 unsigned int c = message[i];
19 crc = crc ^ c;
20
21 for (int j = 7; j >= 0; j--) {
22 uint32_t mask = ((crc & 1) != 0) ? 0xFFFFFFFF : 0;
23 crc = (crc >> 1) ^ (0xEDB88320 & mask);
24 }
25 }
26
27 return crc;
28}
29
30uint32_t _finalizeCRC32(uint32_t crc) { return ~crc; }
31
32// No Python runtime is available yet, need to do this in C.
33uint32_t calcCRC32(unsigned char const *message, uint32_t size) {
34 return _finalizeCRC32(_updateCRC32(_initCRC32(), message, size));
35}
36#else
37
38#ifdef _NUITKA_USE_SYSTEM_CRC32
39#include "zlib.h"
40#else
41
42// Avoid collisions with system libz containing it and being linked against.
43#define ZEXTERN NUITKA_MAY_BE_UNUSED static
44#include "crc32.c"
45#endif
46
47uint32_t calcCRC32(unsigned char const *message, uint32_t size) { return crc32(0, message, size) & 0xFFFFFFFF; }
48#endif
49// Part of "Nuitka", an optimizing Python compiler that is compatible and
50// integrates with CPython, but also works on its own.
51//
52// Licensed under the Apache License, Version 2.0 (the "License");
53// you may not use this file except in compliance with the License.
54// You may obtain a copy of the License at
55//
56// http://www.apache.org/licenses/LICENSE-2.0
57//
58// Unless required by applicable law or agreed to in writing, software
59// distributed under the License is distributed on an "AS IS" BASIS,
60// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61// See the License for the specific language governing permissions and
62// limitations under the License.