在 Python 中使用 zlib crc32_combine

Gfy*_*Gfy 2 python crc32 ctypes zlib

我正在尝试在 Python 中使用zlib中的crc32_combine函数。尽管可以使用各种其他 zlib 函数,但该函数不是“包含电池”标准库的一部分。我尝试了两种方法:从 C 代码到 Python 的端口和从 Python 调用 zlib 与 ctypes。两者都给了我不同的结果,尽管不是我期望的结果。我正在展示 ctypes 代码,因为我认为它执行得更快,并且出现其他程序员错误的可能性更小。

当提供第二个散列的数据长度时,该算法可以组合两个CRC32散列。crc32_combine定义如下:

crc32(crc32(0, seq1, len1), seq2, len2) == crc32_combine(
    crc32(0, seq1, len1), crc32(0, seq2, len2), len2)
Run Code Online (Sandbox Code Playgroud)

这是输出:

Expected CRC: 45E57586
Combined CRC: 567EE4E4
Run Code Online (Sandbox Code Playgroud)

在 win32 上使用 Python 3.5.1 运行时,第二行总是不同的。不是 Python 2,但结果也不是我所期望的。将zlib1.dll放在与脚本相同的目录中进行尝试。

import zlib

def crc32_combine_ctypes(crc1, crc2, len2):
    import ctypes
    from ctypes import util

    lib = util.find_library('zlib1')
    _zlib = ctypes.CDLL(lib)
    assert _zlib._name, "Can't find zlib"

    _zlib.crc32_combine.argtypes = [
        ctypes.c_ulong, ctypes.c_ulong, ctypes.c_ulong]
    _zlib.crc32_combine.restype = ctypes.c_ulong

    return _zlib.crc32_combine(crc1, crc2, len2)

testfile = "zlib1.dll"

with open(testfile, "rb") as tf:
    data = tf.read()

print("Expected CRC: %0.8X" % (zlib.crc32(data) & 0xFFFFFFFF))

cut = len(data) // 2 - 100
p1 = data[0:cut]
p2 = data[cut:]

crc1 = zlib.crc32(p1)
crc2 = zlib.crc32(p2)
len1 = len(p1)
len2 = len(p2)

combined = crc32_combine_ctypes(crc1, crc2, len2)
print("Combined CRC: %0.8X" % (combined & 0xFFFFFFFF))
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Gfy*_*Gfy 7

eryksun有正确的想法:我使用了一个坏的旧 DLL。最后一个带有 32 位 dll 的 zlib 版本包括:https : //sourceforge.net/projects/libpng/files/zlib/1.2.8/

我对纯 Python 代码的移植比使用 ctypes 调用库慢几百倍。(使用带有 1k 次迭代和 50m 作为长度参数的 timeit 的数字)

31.729 (function provided below)
 0.120 (just the _zlib.crc32_combine() call: no library loading included)
Run Code Online (Sandbox Code Playgroud)

纯Python代码:

def crc32_combine(crc1, crc2, len2):
    """Explanation algorithm: /sf/answers/1618873791/
    crc32(crc32(0, seq1, len1), seq2, len2) == crc32_combine(
        crc32(0, seq1, len1), crc32(0, seq2, len2), len2)"""
    # degenerate case (also disallow negative lengths)
    if len2 <= 0:
        return crc1

    # put operator for one zero bit in odd
    # CRC-32 polynomial, 1, 2, 4, 8, ..., 1073741824
    odd = [0xedb88320] + [1 << i for i in range(0, 31)]
    even = [0] * 32

    def matrix_times(matrix, vector):
        number_sum = 0
        matrix_index = 0
        while vector != 0:
            if vector & 1:
                number_sum ^= matrix[matrix_index]
            vector = vector >> 1 & 0x7FFFFFFF
            matrix_index += 1
        return number_sum

    # put operator for two zero bits in even - gf2_matrix_square(even, odd)
    even[:] = [matrix_times(odd, odd[n]) for n in range(0, 32)]

    # put operator for four zero bits in odd
    odd[:] = [matrix_times(even, even[n]) for n in range(0, 32)]

    # apply len2 zeros to crc1 (first square will put the operator for one
    # zero byte, eight zero bits, in even)
    while len2 != 0:
        # apply zeros operator for this bit of len2
        even[:] = [matrix_times(odd, odd[n]) for n in range(0, 32)]
        if len2 & 1:
            crc1 = matrix_times(even, crc1)
        len2 >>= 1

        # if no more bits set, then done
        if len2 == 0:
            break

        # another iteration of the loop with odd and even swapped
        odd[:] = [matrix_times(even, even[n]) for n in range(0, 32)]
        if len2 & 1:
            crc1 = matrix_times(odd, crc1)
        len2 >>= 1

        # if no more bits set, then done
    # return combined crc
    crc1 ^= crc2
    return crc1
Run Code Online (Sandbox Code Playgroud)