diff --git a/App/lib/Projectrans/Utils.vp b/App/lib/Projectrans/Utils.vp index 6563957..78633cf 100644 --- a/App/lib/Projectrans/Utils.vp +++ b/App/lib/Projectrans/Utils.vp @@ -19,6 +19,50 @@ import w32.win32base _mbuddy: memhub.MemBuddy | t.CPtr +# ============================================================ +# nibble_to_hex_char - 半字节转十六进制字符 +# ============================================================ +def nibble_to_hex_char(n: int, uppercase: int) -> t.CChar: + """0-15 → '0'-'9'/'a'-'f'(uppercase=1 时用 'A'-'F')""" + if n < 10: + return '0' + n + if uppercase != 0: + return 'A' + (n - 10) + return 'a' + (n - 10) + + +# ============================================================ +# bytes_to_hex - 字节序列转十六进制字符串 +# ============================================================ +def bytes_to_hex(pool: memhub.MemBuddy | t.CPtr, + digest: bytes, + nbytes: t.CSizeT, + uppercase: int, + use_malloc: int) -> str: + """将 digest 前 nbytes 字节转为 2*nbytes 个十六进制字符(含 null 终止) + + use_malloc=1 时用 stdlib.malloc(调用方负责 free),否则 pool.alloc。 + 合并自 Utils.compute_sha1 / IncludesScanner.compute_file_sha1 的重复 hex 转换。 + """ + if digest is None or nbytes <= 0: + return None + hex_len: t.CSizeT = nbytes * 2 + 1 + hex_buf: str = None + if use_malloc != 0: + hex_buf = stdlib.malloc(hex_len) + else: + hex_buf = pool.alloc(hex_len) + if hex_buf is None: + return None + for i in range(nbytes): + hi: int = (digest[i] >> 4) & 0xF + lo: int = digest[i] & 0xF + hex_buf[i * 2] = nibble_to_hex_char(hi, uppercase) + hex_buf[i * 2 + 1] = nibble_to_hex_char(lo, uppercase) + hex_buf[nbytes * 2] = '\0' + return hex_buf + + # ============================================================ # compute_sha1 - 计算字符串的 SHA1,返回前 16 个十六进制字符 # @@ -44,22 +88,7 @@ def compute_sha1(pool: memhub.MemBuddy | t.CPtr, content: str) -> str: ctx.final(digest) # 转为十六进制字符串(取前 8 字节 = 16 个十六进制字符) - hex_buf: str = pool.alloc(17) - if hex_buf is None: - return None - for i in range(8): - hi: int = (digest[i] >> 4) & 0xF - lo: int = digest[i] & 0xF - if hi < 10: - hex_buf[i * 2] = '0' + hi - else: - hex_buf[i * 2] = 'a' + (hi - 10) - if lo < 10: - hex_buf[i * 2 + 1] = '0' + lo - else: - hex_buf[i * 2 + 1] = 'a' + (lo - 10) - hex_buf[16] = '\0' - return hex_buf + return bytes_to_hex(pool, digest, 8, 0, 0) # ============================================================ diff --git a/App/lib/core/Handles/HandlesExpr.vp b/App/lib/core/Handles/HandlesExpr.vp index b62e870..31ca8cc 100644 --- a/App/lib/core/Handles/HandlesExpr.vp +++ b/App/lib/core/Handles/HandlesExpr.vp @@ -22,6 +22,7 @@ import lib.core.Handles.HandlesImports as HandlesImports import lib.core.StubMerger as StubMerger import lib.core.VLogger as VLogger import lib.Projectrans.Config as Config +import lib.Projectrans.Utils as Utils # ============================================================ @@ -75,15 +76,9 @@ def escape_llvm_string(pool: memhub.MemBuddy | t.CPtr, pos += 1 hi: int = (ch >> 4) & 0xF lo: int = ch & 0xF - if hi < 10: - buf[pos] = '0' + hi - else: - buf[pos] = 'A' + (hi - 10) + buf[pos] = Utils.nibble_to_hex_char(hi, 1) pos += 1 - if lo < 10: - buf[pos] = '0' + lo - else: - buf[pos] = 'A' + (lo - 10) + buf[pos] = Utils.nibble_to_hex_char(lo, 1) pos += 1 else: buf[pos] = ch diff --git a/App/lib/core/IncludesScanner.vp b/App/lib/core/IncludesScanner.vp index 6b9ebab..42e02b5 100644 --- a/App/lib/core/IncludesScanner.vp +++ b/App/lib/core/IncludesScanner.vp @@ -9,6 +9,7 @@ import w32.win32base as win32base import w32.fileio as fileio import hashlib import viperlib +import lib.Projectrans.Utils as Utils import lib.core.VLogger as VLogger @@ -201,24 +202,8 @@ def compute_file_sha1(pool: memhub.MemBuddy | t.CPtr, return None ctx.final(digest) - # 转为十六进制字符串(取前 8 字节 = 16 个十六进制字符) - hex_buf: str = stdlib.malloc(17) - if hex_buf is None: - stdlib.free(buf) - stdlib.free(digest) - return None - for i in range(8): - hi: int = (digest[i] >> 4) & 0xF - lo: int = digest[i] & 0xF - if hi < 10: - hex_buf[i * 2] = '0' + hi - else: - hex_buf[i * 2] = 'a' + (hi - 10) - if lo < 10: - hex_buf[i * 2 + 1] = '0' + lo - else: - hex_buf[i * 2 + 1] = 'a' + (lo - 10) - hex_buf[16] = '\0' + # 转为十六进制字符串(取前 8 字节 = 16 个十六进制字符,stdlib 分配保持原行为) + hex_buf: str = Utils.bytes_to_hex(pool, digest, 8, 0, 1) stdlib.free(buf) stdlib.free(digest) return hex_buf