修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -59,7 +59,7 @@ DEF_MEM_LEVEL: t.CDefine = 8
# Version
ZLIB_VERSION: t.CDefine = "1.3.2"
pyzlib_error_msg: list[t.CChar, 512] = ""
pyzlib_error_msg: t.CArray[t.CChar, 512] = ""
pyzlib_error_code_val: t.CInt = 0
# ============================================================
@@ -552,7 +552,9 @@ def compress(pool: mpool.MPool | t.CPtr, data: BYTEPTR, data_len: t.CSizeT,
actual_wbits: t.CInt = wbits
if wbits > MAX_WBITS:
actual_wbits = -MAX_WBITS
stdio.printf("[DEBUG] compress: calling zdeflate_one_shot, pool=%p, data=%p, len=%zu, level=%d, wbits=%d\n", pool, data, data_len, level, actual_wbits)
raw_result: UINT8PTR = zdeflate.zdeflate_one_shot(pool, data, data_len, level, actual_wbits, out_len)
stdio.printf("[DEBUG] compress: zdeflate_one_shot returned %p\n", raw_result)
if not raw_result:
set_error(Z_DATA_ERROR, "compression failed")
return None

View File

@@ -13,7 +13,7 @@ def zchecksum_adler32(data: UINT8PTR, length: t.CSizeT, init: UINT32) -> t.CUInt
b = (b + a) % 65521
return (b << 16) | a
crc32_table: list[t.CUInt32T, 256] = [
crc32_table: t.CArray[t.CUInt32T, 256] = [
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,

View File

@@ -34,7 +34,7 @@ ZDEFLATE_END_OF_BLOCK: t.CDefine = 256
# ============================================================
# Length / Distance extra bits tables (RFC 1951)
# ============================================================
zdeflate_len_extra_bits: list[t.CInt, 29] = [
zdeflate_len_extra_bits: t.CArray[t.CInt, 29] = [
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1,
2, 2, 2, 2,
@@ -44,7 +44,7 @@ zdeflate_len_extra_bits: list[t.CInt, 29] = [
0
]
zdeflate_len_base: list[t.CInt, 29] = [
zdeflate_len_base: t.CArray[t.CInt, 29] = [
3, 4, 5, 6, 7, 8, 9, 10,
11, 13, 15, 17,
19, 23, 27, 31,
@@ -54,7 +54,7 @@ zdeflate_len_base: list[t.CInt, 29] = [
258
]
zdeflate_dist_extra_bits: list[t.CInt, 30] = [
zdeflate_dist_extra_bits: t.CArray[t.CInt, 30] = [
0, 0, 0, 0,
1, 1,
2, 2,
@@ -71,7 +71,7 @@ zdeflate_dist_extra_bits: list[t.CInt, 30] = [
13, 13
]
zdeflate_dist_base: list[t.CInt, 30] = [
zdeflate_dist_base: t.CArray[t.CInt, 30] = [
1, 2, 3, 4,
5, 7,
9, 13,
@@ -88,7 +88,7 @@ zdeflate_dist_base: list[t.CInt, 30] = [
16385, 24577
]
zdeflate_codelen_order: list[int, 19] = [
zdeflate_codelen_order: t.CArray[int, 19] = [
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
]

View File

@@ -122,8 +122,8 @@ class zdeflate_stream:
def write_dynamic_block(self, data: UINT8PTR, length: t.CSizeT, final: t.CInt):
lit_freqs: list[t.CInt, 288]
dist_freqs: list[t.CInt, 32]
lit_freqs: t.CArray[t.CInt, 288]
dist_freqs: t.CArray[t.CInt, 32]
zdeflate_count_freqs(lit_freqs, dist_freqs, self, data, length)
hlit: t.CInt = 286
while hlit > 257 and lit_freqs[hlit - 1] == 0: hlit -= 1
@@ -133,11 +133,11 @@ class zdeflate_stream:
dist_tree = zhuff.zhuff_tree()
lit_tree.build_codes(lit_freqs, hlit, zdef.ZDEFLATE_MAX_BITS)
dist_tree.build_codes(dist_freqs, hdist, zdef.ZDEFLATE_MAX_BITS)
all_lengths: list[t.CInt, 288 + 32]
all_lengths: t.CArray[t.CInt, 288 + 32]
for i in range(hlit): all_lengths[i] = lit_tree.codes[i].bits
for i in range(hdist): all_lengths[hlit + i] = dist_tree.codes[i].bits
total_lengths: t.CInt = hlit + hdist
cl_freqs: list[t.CInt, 19]
cl_freqs: t.CArray[t.CInt, 19]
zdeflate_count_cl_freqs(all_lengths, total_lengths, cl_freqs)
cl_tree = zhuff.zhuff_tree()
cl_tree.build_codes(cl_freqs, 19, 7)
@@ -373,14 +373,28 @@ def zdeflate_write_cl_encoded(all_lengths: INTPTR, total: t.CInt,
def zdeflate_create(pool: mpool.MPool | t.CPtr, level: t.CInt, wbits: t.CInt, mem_level: t.CInt, strategy: t.CInt) -> zdeflate_stream | t.CPtr:
s: zdeflate_stream | t.CPtr = zdef.zdef_alloc(pool, zdeflate_stream.__sizeof__())
stdio.printf("[DEBUG] zdeflate_create: pool=%p, level=%d, wbits=%d\n", pool, level, wbits)
n: t.CSizeT = zdeflate_stream.__sizeof__()
stdio.printf("[DEBUG] zdeflate_create: sizeof(zdeflate_stream)=%zu\n", n)
s: zdeflate_stream | t.CPtr = zdef.zdef_alloc(pool, n)
stdio.printf("[DEBUG] zdeflate_create: allocated stream=%p\n", s)
if not s: return None
stdio.printf("[DEBUG] zdeflate_create: memset(s, 0)\n")
memset(s, 0, zdeflate_stream.__sizeof__())
stdio.printf("[DEBUG] zdeflate_create: s.pool = pool\n")
s.pool = pool
stdio.printf("[DEBUG] zdeflate_create: creating zbit_writer\n")
s.writer = zdef.zbit_writer(pool)
stdio.printf("[DEBUG] zdeflate_create: zbit_writer created, writer.buf=%p\n", s.writer.buf)
stdio.printf("[DEBUG] zdeflate_create: allocating window\n")
s.window = BYTEPTR(zdef.zdef_alloc(pool, zdef.ZDEFLATE_WINDOW_SIZE))
stdio.printf("[DEBUG] zdeflate_create: window=%p\n", s.window)
stdio.printf("[DEBUG] zdeflate_create: allocating hash_head\n")
s.hash_head = INTPTR(zdef.zdef_alloc(pool, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__()))
stdio.printf("[DEBUG] zdeflate_create: hash_head=%p\n", s.hash_head)
stdio.printf("[DEBUG] zdeflate_create: allocating hash_prev\n")
s.hash_prev = INTPTR(zdef.zdef_alloc(pool, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__()))
stdio.printf("[DEBUG] zdeflate_create: hash_prev=%p\n", s.hash_prev)
if not s.window or not s.hash_head or not s.hash_prev:
s.destroy()
return None
@@ -400,7 +414,9 @@ def zdeflate_create(pool: mpool.MPool | t.CPtr, level: t.CInt, wbits: t.CInt, me
def zdeflate_one_shot(pool: mpool.MPool | t.CPtr, data: UINT8PTR, length: t.CSizeT,
level: t.CInt, wbits: t.CInt, out_len: t.CSizeT | t.CPtr) -> t.CUInt8T | t.CPtr:
stdio.printf("[DEBUG] zdeflate_one_shot: pool=%p, data=%p, length=%zu, level=%d, wbits=%d\n", pool, data, length, level, wbits)
s: zdeflate_stream | t.CPtr = zdeflate_create(pool, level, wbits, 8, 0)
stdio.printf("[DEBUG] zdeflate_one_shot: stream created=%p\n", s)
if not s: return None
memset(s.hash_head, -1, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())

View File

@@ -15,17 +15,17 @@ class zhuff_code:
@t.Object
class zhuff_tree:
pool: mpool.MPool | t.CPtr
codes: list[zhuff_code, ZHUFF_MAX_CODES]
codes: t.CArray[zhuff_code, ZHUFF_MAX_CODES]
count: t.CInt
max_bits: t.CInt
def __init__(self):
pass
self.pool = None
def build_codes(self, freqs: INTPTR, count: t.CInt, max_bits: t.CInt):
lengths: list[t.CInt, ZHUFF_MAX_CODES]
bl_count: list[t.CInt, ZHUFF_MAX_BITS + 1]
next_code: list[UINT, ZHUFF_MAX_BITS + 1]
lengths: t.CArray[t.CInt, ZHUFF_MAX_CODES]
bl_count: t.CArray[t.CInt, ZHUFF_MAX_BITS + 1]
next_code: t.CArray[UINT, ZHUFF_MAX_BITS + 1]
self.count = count
self.max_bits = max_bits
self.build_code_lengths(lengths, freqs, count, max_bits)
@@ -47,9 +47,9 @@ class zhuff_tree:
self.codes[i].code = 0
def build_fixed_lit_tree(self):
lengths: list[t.CInt, 288]
bl_count: list[t.CInt, 16]
next_code: list[t.CUnsignedInt, 16]
lengths: t.CArray[t.CInt, 288]
bl_count: t.CArray[t.CInt, 16]
next_code: t.CArray[t.CUnsignedInt, 16]
self.get_fixed_lit_lengths(lengths)
memset(bl_count, 0, bl_count.__sizeof__())
for i in range(288):
@@ -71,9 +71,9 @@ class zhuff_tree:
self.codes[i].code = 0
def build_fixed_dist_tree(self):
lengths: list[t.CInt, 32]
bl_count: list[t.CInt, 16]
next_code: list[t.CUnsignedInt, 16]
lengths: t.CArray[t.CInt, 32]
bl_count: t.CArray[t.CInt, 16]
next_code: t.CArray[t.CUnsignedInt, 16]
self.get_fixed_dist_lengths(lengths)
memset(bl_count, 0, bl_count.__sizeof__())
for i in range(32):
@@ -103,7 +103,7 @@ class zhuff_tree:
def build_code_lengths(self, lengths: t.CInt | t.CPtr, freqs: t.CInt | t.CPtr, count: t.CInt, max_bits: t.CInt):
bl_count: list[t.CInt, ZHUFF_MAX_BITS + 2]
bl_count: t.CArray[t.CInt, ZHUFF_MAX_BITS + 2]
sort_count: t.CInt = 0
memset(bl_count, 0, bl_count.__sizeof__())
memset(lengths, 0, int.__sizeof__() * count)
@@ -272,8 +272,8 @@ class zhuff_tree:
lengths[i] = 5
def build_tree_from_lengths(self, lengths: INTPTR, count: t.CInt, max_bits: t.CInt):
bl_count: list[t.CInt, 16]
next_code: list[t.CUnsignedInt, 16]
bl_count: t.CArray[t.CInt, 16]
next_code: t.CArray[t.CUnsignedInt, 16]
memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count):
@@ -298,19 +298,19 @@ class zhuff_tree:
class zhuff_decode_node:
children: list[t.CInt, 2]
children: t.CArray[t.CInt, 2]
symbol: t.CInt
@t.Object
class zhuff_decode_tree:
pool: mpool.MPool | t.CPtr
nodes: list[zhuff_decode_node, 2 * ZHUFF_MAX_CODES]
nodes: t.CArray[zhuff_decode_node, 2 * ZHUFF_MAX_CODES]
node_count: t.CInt
root: t.CInt
def __init__(self):
pass
self.pool = None
def build_decode_tree(self, ht: zhuff_tree | t.CPtr):
self.node_count = 1

View File

@@ -213,7 +213,7 @@ class zinflate_stream:
hdist: t.CInt = t.CInt(hdist_val) + 1
hclen: t.CInt = t.CInt(hclen_val) + 4
cl_lengths: list[t.CInt, 19]
cl_lengths: t.CArray[t.CInt, 19]
memset(cl_lengths, 0, cl_lengths.__sizeof__())
for i in range(hclen):
len_val: UINT
@@ -283,13 +283,13 @@ class zinflate_stream:
lit_tree = zhuff.zhuff_tree()
dist_tree = zhuff.zhuff_tree()
lit_lengths: list[t.CInt, 288]
lit_lengths: t.CArray[t.CInt, 288]
memset(lit_lengths, 0, lit_lengths.__sizeof__())
for i in range(hlit):
lit_lengths[i] = all_lengths[i]
lit_tree.build_tree_from_lengths(lit_lengths, hlit, zdef.ZDEFLATE_MAX_BITS)
dist_lengths: list[t.CInt, 32]
dist_lengths: t.CArray[t.CInt, 32]
memset(dist_lengths, 0, dist_lengths.__sizeof__())
for i in range(hdist):
dist_lengths[i] = all_lengths[hlit + i]