refactor(zlib): 裸 stdlib/string 函数调用改为 namespace 限定形式

free/malloc/calloc/realloc→stdlib.*, memcpy/memset/strncpy→string.*;
修复 pyzlib.vp L177 free 未定义且未导入; zhuff.vp 补 import string;
方法形态(self.pool.free/self.free/def free)不受影响
This commit is contained in:
TermiNexus committed 2026-08-16 07:32:07 +08:00
1 parent 995faf8a9c
commit 5e6a80c9f6
5 files changed
+116 -115

No files matched your search

+69 -69
View File
@@ -100,38 +100,38 @@ class Compress:
if data_len == 0:
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p, 0, 1)
string.memset(p, 0, 1)
return p
while self.input_buf_len + data_len > self.input_buf_cap:
self.input_buf_cap *= 2
if self.pool == None:
new_buf: BYTEPTR = BYTEPTR(realloc(self.input_buf, self.input_buf_cap))
new_buf: BYTEPTR = BYTEPTR(stdlib.realloc(self.input_buf, self.input_buf_cap))
else:
new_buf: BYTEPTR = BYTEPTR(self.pool.alloc(self.input_buf_cap))
if new_buf:
memcpy(new_buf, self.input_buf, self.input_buf_len)
string.memcpy(new_buf, self.input_buf, self.input_buf_len)
self.pool.free(self.input_buf)
if not new_buf:
set_error(Z_MEM_ERROR, "out of memory")
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p2: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p2, 0, 1)
string.memset(p2, 0, 1)
return p2
self.input_buf = new_buf
memcpy(self.input_buf + self.input_buf_len, data, data_len)
string.memcpy(self.input_buf + self.input_buf_len, data, data_len)
self.input_buf_len += data_len
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p3: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p3, 0, 1)
string.memset(p3, 0, 1)
return p3
def flush(self, mode: t.CInt, out_len: t.CSizeT | t.CPtr) -> BYTEPTR:
@@ -168,13 +168,13 @@ class Compress:
self.is_finished = 1
total: t.CSizeT = s.writer.total()
if self.pool == None:
result: BYTEPTR = BYTEPTR(malloc(total))
result: BYTEPTR = BYTEPTR(stdlib.malloc(total))
else:
result: BYTEPTR = BYTEPTR(self.pool.alloc(total))
if result: memcpy(result, s.writer.buf, total)
if result: string.memcpy(result, s.writer.buf, total)
c.Set(c.Deref(out_len), total)
if self.pool == None:
free(self.input_buf)
stdlib.free(self.input_buf)
else:
self.pool.free(self.input_buf)
self.input_buf = None
@@ -198,25 +198,25 @@ class Compress:
if delta == 0:
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p4: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p4, 0, 1)
string.memset(p4, 0, 1)
return p4
if self.pool == None:
result: BYTEPTR = BYTEPTR(malloc(delta))
result: BYTEPTR = BYTEPTR(stdlib.malloc(delta))
else:
result: BYTEPTR = BYTEPTR(self.pool.alloc(delta))
if result: memcpy(result, s.writer.buf + prev_pos, delta)
if result: string.memcpy(result, s.writer.buf + prev_pos, delta)
c.Set(c.Deref(out_len), delta)
return result
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p5: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p5, 0, 1)
string.memset(p5, 0, 1)
return p5
def copy(self) -> Compress | t.CPtr:
@@ -224,10 +224,10 @@ class Compress:
set_error(Z_STREAM_ERROR, "compress object not initialized")
return None
if self.pool == None:
copy_obj: Compress | t.CPtr = calloc(1, Compress.__sizeof__())
copy_obj: Compress | t.CPtr = stdlib.calloc(1, Compress.__sizeof__())
else:
copy_obj: Compress | t.CPtr = self.pool.alloc(Compress.__sizeof__())
memset(copy_obj, 0, Compress.__sizeof__())
string.memset(copy_obj, 0, Compress.__sizeof__())
if not copy_obj:
set_error(Z_MEM_ERROR, "out of memory")
return None
@@ -236,7 +236,7 @@ class Compress:
copy_obj.stream = s.copy()
if not copy_obj.stream:
if self.pool == None:
free(copy_obj)
stdlib.free(copy_obj)
else:
self.pool.free(copy_obj)
set_error(Z_MEM_ERROR, "out of memory")
@@ -252,20 +252,20 @@ class Compress:
copy_obj.header_written = self.header_written
if self.input_buf and self.input_buf_len > 0:
if self.pool == None:
copy_obj.input_buf = BYTEPTR(calloc(1, self.input_buf_cap))
copy_obj.input_buf = BYTEPTR(stdlib.calloc(1, self.input_buf_cap))
else:
copy_obj.input_buf = BYTEPTR(self.pool.alloc(self.input_buf_cap))
memset(copy_obj.input_buf, 0, self.input_buf_cap)
string.memset(copy_obj.input_buf, 0, self.input_buf_cap)
if copy_obj.input_buf:
memcpy(copy_obj.input_buf, self.input_buf, self.input_buf_len)
string.memcpy(copy_obj.input_buf, self.input_buf, self.input_buf_len)
copy_obj.input_buf_cap = self.input_buf_cap
copy_obj.input_buf_len = self.input_buf_len
else:
if self.pool == None:
copy_obj.input_buf = BYTEPTR(calloc(1, 256))
copy_obj.input_buf = BYTEPTR(stdlib.calloc(1, 256))
else:
copy_obj.input_buf = BYTEPTR(self.pool.alloc(256))
memset(copy_obj.input_buf, 0, 256)
string.memset(copy_obj.input_buf, 0, 256)
copy_obj.input_buf_cap = 256
copy_obj.input_buf_len = 0
if self.zdict and self.zdict_len > 0:
@@ -278,9 +278,9 @@ class Compress:
s: zdeflate.zdeflate_stream | t.CPtr = self.stream # 隐式类型转换
s.destroy()
if self.pool == None:
free(self.zdict)
free(self.input_buf)
free(self)
stdlib.free(self.zdict)
stdlib.free(self.input_buf)
stdlib.free(self)
else:
self.pool.free(self.zdict)
self.pool.free(self.input_buf)
@@ -323,10 +323,10 @@ class Decompress:
c.Addr(self._unused_data_cap), data, data_len)
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p6: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p6, 0, 1)
string.memset(p6, 0, 1)
return p6
self._unconsumed_tail_len = 0
if data and data_len > 0:
@@ -335,10 +335,10 @@ class Decompress:
if self.input_buf_len == 0:
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p7: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p7, 0, 1)
string.memset(p7, 0, 1)
return p7
s: zinflate.zinflate_stream | t.CPtr = zinflate.zinflate_create(self.pool, self.wbits)
if not s:
@@ -365,7 +365,7 @@ class Decompress:
consumed: t.CSizeT = s.input_pos
if consumed < self.input_buf_len:
remaining: t.CSizeT = self.input_buf_len - consumed
memmove(self.input_buf, self.input_buf + consumed, remaining)
string.memmove(self.input_buf, self.input_buf + consumed, remaining)
self.input_buf_len = remaining
else:
self.input_buf_len = 0
@@ -381,10 +381,10 @@ class Decompress:
return None
c.Set(c.Deref(out_len), 0)
if self.pool == None:
return BYTEPTR(calloc(1, 1))
return BYTEPTR(stdlib.calloc(1, 1))
else:
p8: BYTEPTR = BYTEPTR(self.pool.alloc(1))
memset(p8, 0, 1)
string.memset(p8, 0, 1)
return p8
def copy(self) -> Decompress | t.CPtr:
@@ -392,10 +392,10 @@ class Decompress:
set_error(Z_STREAM_ERROR, "decompress object not initialized")
return None
if self.pool == None:
copy_obj: Decompress | t.CPtr = calloc(1, Decompress.__sizeof__())
copy_obj: Decompress | t.CPtr = stdlib.calloc(1, Decompress.__sizeof__())
else:
copy_obj: Decompress | t.CPtr = self.pool.alloc(Decompress.__sizeof__())
memset(copy_obj, 0, Decompress.__sizeof__())
string.memset(copy_obj, 0, Decompress.__sizeof__())
if not copy_obj:
set_error(Z_MEM_ERROR, "out of memory")
return None
@@ -420,21 +420,21 @@ class Decompress:
copy_obj.input_buf_cap = self.input_buf_cap
else:
if self.pool == None:
copy_obj.input_buf = BYTEPTR(calloc(1, 256))
copy_obj.input_buf = BYTEPTR(stdlib.calloc(1, 256))
else:
copy_obj.input_buf = BYTEPTR(self.pool.alloc(256))
memset(copy_obj.input_buf, 0, 256)
string.memset(copy_obj.input_buf, 0, 256)
copy_obj.input_buf_cap = 256
copy_obj.input_buf_len = 0
return copy_obj
def delete(self):
if self.pool == None:
free(self.zdict)
free(self._unused_data)
free(self._unconsumed_tail)
free(self.input_buf)
free(self)
stdlib.free(self.zdict)
stdlib.free(self._unused_data)
stdlib.free(self._unconsumed_tail)
stdlib.free(self.input_buf)
stdlib.free(self)
else:
self.pool.free(self.zdict)
self.pool.free(self._unused_data)
@@ -470,7 +470,7 @@ def set_error(code: int, msg: str):
global pyzlib_error_code_val, pyzlib_error_msg
pyzlib_error_code_val = code
if msg:
strncpy(pyzlib_error_msg, msg, pyzlib_error_msg.__sizeof__() - 1)
string.strncpy(pyzlib_error_msg, msg, pyzlib_error_msg.__sizeof__() - 1)
pyzlib_error_msg[pyzlib_error_msg.__sizeof__() - 1] = '\0'
else:
pyzlib_error_msg[0] = '\0'
@@ -478,10 +478,10 @@ def set_error(code: int, msg: str):
def clone_bytes(pool: memhub.MemManager | t.CPtr, src: BYTEPTR, length: t.CSizeT) -> BYTEPTR:
if not src or length == 0: return None
if pool == None:
dst: BYTEPTR = BYTEPTR(malloc(length))
dst: BYTEPTR = BYTEPTR(stdlib.malloc(length))
else:
dst: BYTEPTR = BYTEPTR(pool.alloc(length))
if dst: memcpy(dst, src, length)
if dst: string.memcpy(dst, src, length)
return dst
def append_bytes(pool: memhub.MemManager | t.CPtr, buf: BYTE | t.CPtr[t.CPtr], length: t.CSizeT | t.CPtr, cap: t.CSizeT | t.CPtr,
@@ -492,16 +492,16 @@ def append_bytes(pool: memhub.MemManager | t.CPtr, buf: BYTE | t.CPtr[t.CPtr], l
new_cap: t.CSizeT = c.Deref(cap) * 2
if new_cap < needed: new_cap = needed
if pool == None:
new_buf: BYTEPTR = BYTEPTR(realloc(c.Deref(buf), new_cap))
new_buf: BYTEPTR = BYTEPTR(stdlib.realloc(c.Deref(buf), new_cap))
else:
new_buf: BYTEPTR = BYTEPTR(pool.alloc(new_cap))
if new_buf:
memcpy(new_buf, c.Deref(buf), c.Deref(length))
string.memcpy(new_buf, c.Deref(buf), c.Deref(length))
pool.free(c.Deref(buf))
if not new_buf: return -1
c.Set(c.Deref(buf), new_buf)
c.Set(c.Deref(cap), new_cap)
memcpy(c.Deref(buf) + c.Deref(length), data, data_len)
string.memcpy(c.Deref(buf) + c.Deref(length), data, data_len)
c.Set(c.Deref(length), c.Deref(length) + data_len)
return 0
@@ -509,20 +509,20 @@ def append_bytes(pool: memhub.MemManager | t.CPtr, buf: BYTE | t.CPtr[t.CPtr], l
def shrink_to_fit(pool: memhub.MemManager | t.CPtr, buf: BYTEPTR, length: t.CSizeT) -> BYTEPTR:
if length == 0:
if pool == None:
free(buf)
return BYTEPTR(calloc(1, 1))
stdlib.free(buf)
return BYTEPTR(stdlib.calloc(1, 1))
else:
pool.free(buf)
p: BYTEPTR = BYTEPTR(pool.alloc(1))
memset(p, 0, 1)
string.memset(p, 0, 1)
return p
if pool == None:
result: BYTEPTR = BYTEPTR(realloc(buf, length))
result: BYTEPTR = BYTEPTR(stdlib.realloc(buf, length))
return result if result else buf
else:
result: BYTEPTR = BYTEPTR(pool.alloc(length))
if result:
memcpy(result, buf, length)
string.memcpy(result, buf, length)
pool.free(buf)
return result
return buf
@@ -560,12 +560,12 @@ def compress(pool: memhub.MemManager | t.CPtr, data: BYTEPTR, data_len: t.CSizeT
if wbits > MAX_WBITS:
gzip_len: t.CSizeT = 10 + c.Deref(out_len) + 8
if pool == None:
gzip_buf: BYTEPTR = BYTEPTR(malloc(gzip_len))
gzip_buf: BYTEPTR = BYTEPTR(stdlib.malloc(gzip_len))
else:
gzip_buf: BYTEPTR = BYTEPTR(pool.alloc(gzip_len))
if not gzip_buf:
if pool == None:
free(raw_result)
stdlib.free(raw_result)
else:
pool.free(raw_result)
set_error(Z_MEM_ERROR, "out of memory")
@@ -583,10 +583,10 @@ def compress(pool: memhub.MemManager | t.CPtr, data: BYTEPTR, data_len: t.CSizeT
gzip_buf[pos + 9] = 0xFF
pos += 10
memcpy(gzip_buf + pos, raw_result, c.Deref(out_len))
string.memcpy(gzip_buf + pos, raw_result, c.Deref(out_len))
pos += c.Deref(out_len)
if pool == None:
free(raw_result)
stdlib.free(raw_result)
else:
pool.free(raw_result)
@@ -623,10 +623,10 @@ def compressobj(pool: memhub.MemManager | t.CPtr, level: t.CInt, method: t.CInt,
memLevel: t.CInt, strategy: t.CInt,
zdict: BYTEPTR, zdict_len: t.CSizeT) -> Compress | t.CPtr:
if pool == None:
obj: Compress | t.CPtr = calloc(1, Compress.__sizeof__())
obj: Compress | t.CPtr = stdlib.calloc(1, Compress.__sizeof__())
else:
obj: Compress | t.CPtr = pool.alloc(Compress.__sizeof__())
memset(obj, 0, Compress.__sizeof__())
string.memset(obj, 0, Compress.__sizeof__())
if not obj:
set_error(Z_MEM_ERROR, "out of memory")
return None
@@ -636,7 +636,7 @@ def compressobj(pool: memhub.MemManager | t.CPtr, level: t.CInt, method: t.CInt,
s: zdeflate.zdeflate_stream | t.CPtr = zdeflate.zdeflate_create(pool, level, actual_wbits, memLevel, strategy)
if not s:
if pool == None:
free(obj)
stdlib.free(obj)
else:
pool.free(obj)
set_error(Z_MEM_ERROR, "out of memory")
@@ -649,10 +649,10 @@ def compressobj(pool: memhub.MemManager | t.CPtr, level: t.CInt, method: t.CInt,
obj.memLevel = memLevel
obj.strategy = strategy
if pool == None:
obj.input_buf = BYTEPTR(calloc(1, 256))
obj.input_buf = BYTEPTR(stdlib.calloc(1, 256))
else:
obj.input_buf = BYTEPTR(pool.alloc(256))
memset(obj.input_buf, 0, 256)
string.memset(obj.input_buf, 0, 256)
obj.input_buf_cap = 256
obj.input_buf_len = 0
obj.header_written = 0
@@ -665,10 +665,10 @@ def compressobj(pool: memhub.MemManager | t.CPtr, level: t.CInt, method: t.CInt,
def decompressobj(pool: memhub.MemManager | t.CPtr, wbits: t.CInt, zdict: BYTEPTR, zdict_len: t.CSizeT) -> Decompress | t.CPtr:
if pool == None:
obj: Decompress | t.CPtr = calloc(1, Decompress.__sizeof__())
obj: Decompress | t.CPtr = stdlib.calloc(1, Decompress.__sizeof__())
else:
obj: Decompress | t.CPtr = pool.alloc(Decompress.__sizeof__())
memset(obj, 0, Decompress.__sizeof__())
string.memset(obj, 0, Decompress.__sizeof__())
if not obj:
set_error(Z_MEM_ERROR, "out of memory")
return None
@@ -682,10 +682,10 @@ def decompressobj(pool: memhub.MemManager | t.CPtr, wbits: t.CInt, zdict: BYTEPT
obj.zdict = clone_bytes(pool, zdict, zdict_len)
obj.zdict_len = zdict_len
if pool == None:
obj.input_buf = BYTEPTR(calloc(1, 256))
obj.input_buf = BYTEPTR(stdlib.calloc(1, 256))
else:
obj.input_buf = BYTEPTR(pool.alloc(256))
memset(obj.input_buf, 0, 256)
string.memset(obj.input_buf, 0, 256)
obj.input_buf_cap = 256
obj.input_buf_len = 0
return obj
+8 -8
View File
@@ -106,10 +106,10 @@ class zbit_writer:
self.pool = pool
self.cap = 4096
if pool == None:
self.buf = BYTEPTR(malloc(self.cap))
self.buf = BYTEPTR(stdlib.malloc(self.cap))
else:
self.buf = BYTEPTR(pool.alloc(self.cap))
memset(self.buf, 0, self.cap)
string.memset(self.buf, 0, self.cap)
self.byte_pos = 0
self.bit_pos = 0
@@ -117,14 +117,14 @@ class zbit_writer:
while self.byte_pos + need + 4 >= self.cap:
new_cap: t.CSizeT = self.cap * 2
if self.pool == None:
new_buf: BYTE | t.CPtr = BYTEPTR(realloc(self.buf, new_cap))
new_buf: BYTE | t.CPtr = BYTEPTR(stdlib.realloc(self.buf, new_cap))
else:
new_buf: BYTE | t.CPtr = BYTEPTR(self.pool.alloc(new_cap))
if new_buf:
memcpy(new_buf, self.buf, self.byte_pos + (self.bit_pos + 7) / 8)
string.memcpy(new_buf, self.buf, self.byte_pos + (self.bit_pos + 7) / 8)
self.pool.free(self.buf)
if not new_buf: return
memset(new_buf + self.cap, 0, new_cap - self.cap)
string.memset(new_buf + self.cap, 0, new_cap - self.cap)
self.buf = new_buf
self.cap = new_cap
@@ -215,7 +215,7 @@ class zbit_writer:
def free(self):
if self.pool == None:
free(self.buf)
stdlib.free(self.buf)
else:
self.pool.free(self.buf)
self.buf = None
@@ -278,10 +278,10 @@ class zbit_reader:
# Can be replaced with custom allocator
# ============================================================
def zdef_alloc(pool: memhub.MemManager | t.CPtr, n: t.CSizeT) -> VOIDPTR:
if pool == None: return malloc(n)
if pool == None: return stdlib.malloc(n)
return pool.alloc(n)
def zdef_free(pool: memhub.MemManager | t.CPtr, p: VOIDPTR):
if pool == None:
free(p)
stdlib.free(p)
return
pool.free(p)
+19 -19
View File
@@ -171,13 +171,13 @@ class zdeflate_stream:
c.Set(c.Deref(out_len), 0)
return 0
self.adler = zchecksum.zchecksum_adler32(data, length, self.adler)
memset(self.hash_head, -1, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
memset(self.hash_prev, -1, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
string.memset(self.hash_head, -1, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
string.memset(self.hash_prev, -1, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
self.compress_block(data, length, 0)
c.Set(c.Deref(out_len), self.writer.total())
c.Set(c.Deref(out), UINT8PTR(zdef.zdef_alloc(self.pool, c.Deref(out_len))))
if not c.Deref(out): return -4
memcpy(c.Deref(out), self.writer.buf, c.Deref(out_len))
string.memcpy(c.Deref(out), self.writer.buf, c.Deref(out_len))
return 0
def flush(self, mode: t.CInt, out: t.CUInt8T | t.CPtr[t.CPtr], out_len: t.CSizeT | t.CPtr) -> t.CInt:
@@ -209,7 +209,7 @@ class zdeflate_stream:
c.Set(c.Deref(out_len), self.writer.total())
c.Set(c.Deref(out), UINT8PTR(zdef.zdef_alloc(self.pool, c.Deref(out_len))))
if not c.Deref(out): return -4
memcpy(c.Deref(out), self.writer.buf, c.Deref(out_len))
string.memcpy(c.Deref(out), self.writer.buf, c.Deref(out_len))
return 0
def set_dictionary(self, dict: UINT8PTR, length: t.CSizeT) -> t.CInt:
@@ -218,7 +218,7 @@ class zdeflate_stream:
if use > zdef.ZDEFLATE_WINDOW_SIZE:
dict += length - zdef.ZDEFLATE_WINDOW_SIZE
use = zdef.ZDEFLATE_WINDOW_SIZE
memcpy(self.window, dict, use)
string.memcpy(self.window, dict, use)
self.window_size = t.CInt(use)
self.window_pos = t.CInt(use)
self.adler = zchecksum.zchecksum_adler32(dict, length, self.adler)
@@ -227,12 +227,12 @@ class zdeflate_stream:
def copy(self) -> zdeflate_stream | t.CPtr:
z: zdeflate_stream | t.CPtr = zdef.zdef_alloc(self.pool, zdeflate_stream.__sizeof__())
if not z: return None
memcpy(z, self, zdeflate_stream.__sizeof__())
string.memcpy(z, self, zdeflate_stream.__sizeof__())
z.writer.buf = BYTEPTR(zdef.zdef_alloc(self.pool, self.writer.cap))
if not z.writer.buf:
zdef.zdef_free(self.pool, z)
return None
memcpy(z.writer.buf, self.writer.buf, self.writer.cap)
string.memcpy(z.writer.buf, self.writer.buf, self.writer.cap)
z.window = BYTEPTR(zdef.zdef_alloc(self.pool, zdef.ZDEFLATE_WINDOW_SIZE))
z.hash_head = INTPTR(zdef.zdef_alloc(self.pool, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__()))
z.hash_prev = INTPTR(zdef.zdef_alloc(self.pool, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__()))
@@ -240,9 +240,9 @@ class zdeflate_stream:
if z.writer.buf: zdef.zdef_free(self.pool, z.writer.buf)
zdef.zdef_free(self.pool, z)
return None
memcpy(z.window, self.window, zdef.ZDEFLATE_WINDOW_SIZE)
memcpy(z.hash_head, self.hash_head, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
memcpy(z.hash_prev, self.hash_prev, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
string.memcpy(z.window, self.window, zdef.ZDEFLATE_WINDOW_SIZE)
string.memcpy(z.hash_head, self.hash_head, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
string.memcpy(z.hash_prev, self.hash_prev, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
return z
def destroy(self):
@@ -257,8 +257,8 @@ class zdeflate_stream:
def zdeflate_count_freqs(lit_freqs: INTPTR, dist_freqs: INTPTR,
s: zdeflate_stream | t.CPtr, data: UINT8PTR, length: t.CSizeT):
memset(lit_freqs, 0, 288 * int.__sizeof__())
memset(dist_freqs, 0, 32 * int.__sizeof__())
string.memset(lit_freqs, 0, 288 * int.__sizeof__())
string.memset(dist_freqs, 0, 32 * int.__sizeof__())
pos: t.CSizeT = 0
while pos < length:
best_dist: t.CInt = 0
@@ -297,7 +297,7 @@ def zdeflate_dist_to_symbol(dist: t.CInt) -> t.CInt:
return 29
def zdeflate_count_cl_freqs(all_lengths: INTPTR, total: t.CInt, cl_freqs: INTPTR):
memset(cl_freqs, 0, 19 * int.__sizeof__())
string.memset(cl_freqs, 0, 19 * int.__sizeof__())
i: t.CInt = 0
while i < total:
if all_lengths[i] == 0:
@@ -375,7 +375,7 @@ def zdeflate_create(pool: memhub.MemManager | t.CPtr, level: t.CInt, wbits: t.CI
n: t.CSizeT = zdeflate_stream.__sizeof__()
s: zdeflate_stream | t.CPtr = zdef.zdef_alloc(pool, n)
if not s: return None
memset(s, 0, zdeflate_stream.__sizeof__())
string.memset(s, 0, zdeflate_stream.__sizeof__())
s.pool = pool
s.writer = zdef.zbit_writer(pool)
s.window = BYTEPTR(zdef.zdef_alloc(pool, zdef.ZDEFLATE_WINDOW_SIZE))
@@ -384,8 +384,8 @@ def zdeflate_create(pool: memhub.MemManager | t.CPtr, level: t.CInt, wbits: t.CI
if not s.window or not s.hash_head or not s.hash_prev:
s.destroy()
return None
memset(s.hash_head, -1, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
memset(s.hash_prev, -1, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
string.memset(s.hash_head, -1, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
string.memset(s.hash_prev, -1, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
s.level = level
s.wbits = wbits
s.strategy = strategy
@@ -403,8 +403,8 @@ def zdeflate_one_shot(pool: memhub.MemManager | t.CPtr, data: UINT8PTR, length:
s: zdeflate_stream | t.CPtr = zdeflate_create(pool, level, wbits, 8, 0)
if not s: return None
memset(s.hash_head, -1, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
memset(s.hash_prev, -1, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
string.memset(s.hash_head, -1, zdef.ZDEFLATE_HASH_SIZE * int.__sizeof__())
string.memset(s.hash_prev, -1, zdef.ZDEFLATE_WINDOW_SIZE * int.__sizeof__())
s.adler = zchecksum.zchecksum_adler32(data, length, 1)
@@ -429,6 +429,6 @@ def zdeflate_one_shot(pool: memhub.MemManager | t.CPtr, data: UINT8PTR, length:
s.writer.byte_pos += 4
c.Set(c.Deref(out_len), s.writer.total())
result: UINT8PTR = UINT8PTR(zdef.zdef_alloc(pool, c.Deref(out_len)))
if result: memcpy(result, s.writer.buf, c.Deref(out_len))
if result: string.memcpy(result, s.writer.buf, c.Deref(out_len))
s.destroy()
return result
+8 -7
View File
@@ -1,6 +1,7 @@
#include <string.h>
from stdint import *
import zlib.zdef as zdef
import string
import memhub
import t, c
@@ -29,7 +30,7 @@ class zhuff_tree:
self.count = count
self.max_bits = max_bits
self.build_code_lengths(lengths, freqs, count, max_bits)
memset(bl_count, 0, bl_count.__sizeof__())
string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count):
if lengths[i] > 0:
bl_count[lengths[i]] += 1
@@ -51,7 +52,7 @@ class zhuff_tree:
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__())
string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(288):
if lengths[i] > 0:
bl_count[lengths[i]] += 1
@@ -75,7 +76,7 @@ class zhuff_tree:
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__())
string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(32):
if lengths[i] > 0:
bl_count[lengths[i]] += 1
@@ -105,8 +106,8 @@ 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: 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)
string.memset(bl_count, 0, bl_count.__sizeof__())
string.memset(lengths, 0, int.__sizeof__() * count)
for i in range(count):
if freqs[i] > 0:
sort_count += 1
@@ -215,7 +216,7 @@ class zhuff_tree:
length += 1
node = parent[node]
lengths[items[i]] = length
memset(bl_count, 0, bl_count.__sizeof__())
string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count):
if lengths[i] > 0:
bl_count[lengths[i]] += 1
@@ -275,7 +276,7 @@ class zhuff_tree:
bl_count: t.CArray[t.CInt, 16]
next_code: t.CArray[t.CUnsignedInt, 16]
memset(bl_count, 0, bl_count.__sizeof__())
string.memset(bl_count, 0, bl_count.__sizeof__())
for i in range(count):
if lengths[i] > 0:
bl_count[lengths[i]] += 1
+12 -12
View File
@@ -47,7 +47,7 @@ class zinflate_stream:
if new_cap < 256: new_cap = 256
new_buf: BYTEPTR = BYTEPTR(zdef.zdef_alloc(self.pool, new_cap))
if not new_buf: return
memcpy(new_buf, self.output, self.output_len)
string.memcpy(new_buf, self.output, self.output_len)
zdef.zdef_free(self.pool, self.output)
self.output = new_buf
self.output_cap = new_cap
@@ -147,8 +147,8 @@ class zinflate_stream:
if lit_dt: zdef.zdef_free(self.pool, lit_dt)
if dist_dt: zdef.zdef_free(self.pool, dist_dt)
return -4
memset(lit_dt, 0, zhuff.zhuff_decode_tree.__sizeof__())
memset(dist_dt, 0, zhuff.zhuff_decode_tree.__sizeof__())
string.memset(lit_dt, 0, zhuff.zhuff_decode_tree.__sizeof__())
string.memset(dist_dt, 0, zhuff.zhuff_decode_tree.__sizeof__())
lit_dt.build_decode_tree(c.Addr(lit_tree))
dist_dt.build_decode_tree(c.Addr(dist_tree))
@@ -214,7 +214,7 @@ class zinflate_stream:
hclen: t.CInt = t.CInt(hclen_val) + 4
cl_lengths: t.CArray[t.CInt, 19]
memset(cl_lengths, 0, cl_lengths.__sizeof__())
string.memset(cl_lengths, 0, cl_lengths.__sizeof__())
for i in range(hclen):
len_val: UINT
if self.read_bits_from_input(3, c.Addr(len_val)) != 0: return -3
@@ -284,13 +284,13 @@ class zinflate_stream:
dist_tree = zhuff.zhuff_tree()
lit_lengths: t.CArray[t.CInt, 288]
memset(lit_lengths, 0, lit_lengths.__sizeof__())
string.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: t.CArray[t.CInt, 32]
memset(dist_lengths, 0, dist_lengths.__sizeof__())
string.memset(dist_lengths, 0, dist_lengths.__sizeof__())
for i in range(hdist):
dist_lengths[i] = all_lengths[hlit + i]
dist_tree.build_tree_from_lengths(dist_lengths, hdist, zdef.ZDEFLATE_MAX_BITS)
@@ -393,7 +393,7 @@ class zinflate_stream:
dst: UINT8PTR = UINT8PTR(zdef.zdef_alloc(self.pool, self.output_len))
c.Set(c.Deref(out), dst)
if dst:
memcpy(dst, self.output, self.output_len)
string.memcpy(dst, self.output, self.output_len)
return 0
def set_dictionary(self, dict: UINT8PTR, length: t.CSizeT) -> t.CInt:
@@ -402,21 +402,21 @@ class zinflate_stream:
if use > zdef.ZDEFLATE_WINDOW_SIZE:
dict += length - zdef.ZDEFLATE_WINDOW_SIZE
use = zdef.ZDEFLATE_WINDOW_SIZE
memcpy(self.window, dict, use)
string.memcpy(self.window, dict, use)
self.window_pos = t.CInt(use)
return 0
def copy(self) -> zinflate_stream | t.CPtr:
z: zinflate_stream | t.CPtr = zdef.zdef_alloc(self.pool, zinflate_stream.__sizeof__())
if not z: return None
memcpy(z, self, zinflate_stream.__sizeof__())
string.memcpy(z, self, zinflate_stream.__sizeof__())
z.window = BYTEPTR(zdef.zdef_alloc(self.pool, zdef.ZDEFLATE_WINDOW_SIZE))
z.output = BYTEPTR(zdef.zdef_alloc(self.pool, self.output_cap))
if not z.window or not z.output:
zinflate_stream.destroy(z)
return None
memcpy(z.window, self.window, zdef.ZDEFLATE_WINDOW_SIZE)
memcpy(z.output, self.output, self.output_cap)
string.memcpy(z.window, self.window, zdef.ZDEFLATE_WINDOW_SIZE)
string.memcpy(z.output, self.output, self.output_cap)
return z
def destroy(self):
@@ -440,7 +440,7 @@ def zinflate_one_shot(pool: memhub.MemManager | t.CPtr, data: UINT8PTR, length:
def zinflate_create(pool: memhub.MemManager | t.CPtr, wbits: t.CInt) -> zinflate_stream | t.CPtr:
s: zinflate_stream | t.CPtr = zdef.zdef_alloc(pool, zinflate_stream.__sizeof__())
if not s: return None
memset(s, 0, zinflate_stream.__sizeof__())
string.memset(s, 0, zinflate_stream.__sizeof__())
s.pool = pool
s.wbits = wbits
s.is_initialized = 1