rtzip 0.3.1__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rtzip-0.5.0/PKG-INFO +6 -0
- rtzip-0.5.0/pyproject.toml +19 -0
- rtzip-0.5.0/pyproject.toml.orig +21 -0
- {rtzip-0.3.1 → rtzip-0.5.0}/src/rtzip/__init__.py +2 -0
- rtzip-0.5.0/src/rtzip/algorithm_register.py +75 -0
- {rtzip-0.3.1 → rtzip-0.5.0}/src/rtzip/codec_wrappers.py +5 -5
- {rtzip-0.3.1 → rtzip-0.5.0}/src/rtzip/errors.py +4 -0
- {rtzip-0.3.1 → rtzip-0.5.0}/src/rtzip/models.py +4 -0
- rtzip-0.5.0/src/rtzip/path_methods.py +17 -0
- {rtzip-0.3.1 → rtzip-0.5.0}/src/rtzip/remotezip.py +183 -40
- rtzip-0.5.0/src/rtzip/wzaes_backends/__init__.py +0 -0
- rtzip-0.5.0/src/rtzip/wzaes_backends/cryptography_impl.py +39 -0
- rtzip-0.5.0/src/rtzip/wzaes_backends/pycryptodome_impl.py +47 -0
- rtzip-0.5.0/src/rtzip/zip_aes.py +293 -0
- rtzip-0.5.0/src/rtzip/zip_crypto.py +117 -0
- rtzip-0.3.1/LICENSE +0 -28
- rtzip-0.3.1/PKG-INFO +0 -7
- rtzip-0.3.1/README.md +0 -119
- rtzip-0.3.1/pyproject.toml +0 -12
- rtzip-0.3.1/setup.cfg +0 -4
- rtzip-0.3.1/src/rtzip/algorithm_register.py +0 -45
- rtzip-0.3.1/src/rtzip.egg-info/PKG-INFO +0 -7
- rtzip-0.3.1/src/rtzip.egg-info/SOURCES.txt +0 -15
- rtzip-0.3.1/src/rtzip.egg-info/dependency_links.txt +0 -1
- rtzip-0.3.1/src/rtzip.egg-info/requires.txt +0 -1
- rtzip-0.3.1/src/rtzip.egg-info/top_level.txt +0 -1
- {rtzip-0.3.1 → rtzip-0.5.0}/src/rtzip/const.py +0 -0
rtzip-0.5.0/PKG-INFO
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "uv_build"
|
|
3
|
+
requires = ["uv_build < 0.13"]
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rtzip"
|
|
7
|
+
version = "0.5.0"
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"obstruct==0.5.2",
|
|
11
|
+
"pyaes>=1.6.1",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[dependency-groups]
|
|
15
|
+
dev = [
|
|
16
|
+
"cryptography>=50.0.1",
|
|
17
|
+
"niquests>=3.21.0",
|
|
18
|
+
"pycryptodome>=3.23.0",
|
|
19
|
+
]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = 'uv_build'
|
|
3
|
+
requires = [
|
|
4
|
+
'uv_build < 0.13'
|
|
5
|
+
]
|
|
6
|
+
|
|
7
|
+
[project]
|
|
8
|
+
name = "rtzip"
|
|
9
|
+
version = "0.5.0"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"obstruct==0.5.2",
|
|
13
|
+
"pyaes>=1.6.1",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[dependency-groups]
|
|
17
|
+
dev = [
|
|
18
|
+
"cryptography>=50.0.1",
|
|
19
|
+
"niquests>=3.21.0",
|
|
20
|
+
"pycryptodome>=3.23.0",
|
|
21
|
+
]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from typing import AsyncGenerator, Any, Protocol, runtime_checkable
|
|
2
|
+
|
|
3
|
+
from .errors import UnsupportedAlgorithmError
|
|
4
|
+
from .models import CDEntry, LocalFileHeader
|
|
5
|
+
|
|
6
|
+
type IndexAcceptedTypes = bytes | bytearray | memoryview
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@runtime_checkable
|
|
10
|
+
class AlgorithmHandler(Protocol):
|
|
11
|
+
def __call__(
|
|
12
|
+
self,
|
|
13
|
+
raw_data: AsyncGenerator[IndexAcceptedTypes, None],
|
|
14
|
+
entry: CDEntry,
|
|
15
|
+
header: LocalFileHeader,
|
|
16
|
+
context: dict[str, Any],
|
|
17
|
+
/
|
|
18
|
+
) -> AsyncGenerator[IndexAcceptedTypes, None]:
|
|
19
|
+
"""
|
|
20
|
+
压缩算法处理器
|
|
21
|
+
|
|
22
|
+
:param raw_data: 原始数据流(异步生成器)
|
|
23
|
+
:param entry: 当前文件的 CDEntry 对象
|
|
24
|
+
:param header: 当前文件的 LocalFileHeader 对象(已经过文件描述符修补)
|
|
25
|
+
:param context: 上下文信息
|
|
26
|
+
:return:
|
|
27
|
+
"""
|
|
28
|
+
...
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
algorithm_mapping: dict[int, AlgorithmHandler] = {}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def algorithm_handler(algorithm: int, override=False):
|
|
35
|
+
"""
|
|
36
|
+
压缩流包装器注册装饰器
|
|
37
|
+
|
|
38
|
+
:param algorithm: 压缩算法 ID
|
|
39
|
+
:param override: 如果你希望覆盖一个已注册的包装器,请设置为 True,否则将会阻止可能的覆盖行为
|
|
40
|
+
:return:
|
|
41
|
+
"""
|
|
42
|
+
if not override and algorithm in algorithm_mapping:
|
|
43
|
+
raise ValueError(f"Algorithm {algorithm} is already registered")
|
|
44
|
+
|
|
45
|
+
def decorator(func: AlgorithmHandler):
|
|
46
|
+
algorithm_mapping[algorithm] = func
|
|
47
|
+
return func
|
|
48
|
+
|
|
49
|
+
return decorator
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def get_algorithm_handler(algorithm: int) -> AlgorithmHandler:
|
|
53
|
+
"""
|
|
54
|
+
获取包装器函数
|
|
55
|
+
|
|
56
|
+
:param algorithm: 压缩算法 ID
|
|
57
|
+
:return: AlgorithmHandler 可调用对象,接受一个异步生成器,返回一个异步生成器
|
|
58
|
+
"""
|
|
59
|
+
try:
|
|
60
|
+
return algorithm_mapping[algorithm]
|
|
61
|
+
except KeyError:
|
|
62
|
+
raise UnsupportedAlgorithmError(algorithm)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def get_algorithm_handler_safe(algorithm: int) -> AlgorithmHandler | None:
|
|
66
|
+
return algorithm_mapping.get(algorithm)
|
|
67
|
+
|
|
68
|
+
def is_algorithm_handlier_available(algorithm: int) -> bool:
|
|
69
|
+
"""
|
|
70
|
+
判断某个压缩算法是否已有注册的包装器
|
|
71
|
+
|
|
72
|
+
:param algorithm: 压缩算法 ID
|
|
73
|
+
:return:
|
|
74
|
+
"""
|
|
75
|
+
return algorithm in algorithm_mapping
|
|
@@ -4,7 +4,7 @@ from .algorithm_register import algorithm_handler
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
@algorithm_handler(0x0008)
|
|
7
|
-
async def deflate_wrapper(gen):
|
|
7
|
+
async def deflate_wrapper(gen, entry, header, ext):
|
|
8
8
|
import zlib
|
|
9
9
|
decompresser = zlib.decompressobj(-15) # 禁用 zlib 头部
|
|
10
10
|
async for chunk in gen:
|
|
@@ -15,13 +15,13 @@ async def deflate_wrapper(gen):
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
@algorithm_handler(0x0000)
|
|
18
|
-
async def raw_wrapper(gen):
|
|
18
|
+
async def raw_wrapper(gen, entry, header, ext):
|
|
19
19
|
async for chunk in gen:
|
|
20
20
|
yield chunk
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
@algorithm_handler(0x000E)
|
|
24
|
-
async def lzma_wrapper(gen):
|
|
24
|
+
async def lzma_wrapper(gen, entry, header, ext):
|
|
25
25
|
import lzma
|
|
26
26
|
decompressor = lzma.LZMADecompressor()
|
|
27
27
|
|
|
@@ -41,7 +41,7 @@ async def lzma_wrapper(gen):
|
|
|
41
41
|
)
|
|
42
42
|
|
|
43
43
|
@algorithm_handler(0x000C)
|
|
44
|
-
async def bz2_wrapper(gen):
|
|
44
|
+
async def bz2_wrapper(gen, entry, header, ext):
|
|
45
45
|
import bz2
|
|
46
46
|
decompressor = bz2.BZ2Decompressor()
|
|
47
47
|
|
|
@@ -66,7 +66,7 @@ async def bz2_wrapper(gen):
|
|
|
66
66
|
|
|
67
67
|
if sys.version_info >= (3, 14):
|
|
68
68
|
@algorithm_handler(0x000D)
|
|
69
|
-
async def zstd_wrapper(gen): # Not Tested
|
|
69
|
+
async def zstd_wrapper(gen, entry, header, ext): # Not Tested
|
|
70
70
|
import warnings
|
|
71
71
|
warnings.warn("ZStandard support is experimental and has not been tested")
|
|
72
72
|
|
|
@@ -196,6 +196,10 @@ class CDEntry(obs.Struct):
|
|
|
196
196
|
"""
|
|
197
197
|
return bool(self.flags & 0b100)
|
|
198
198
|
|
|
199
|
+
@property
|
|
200
|
+
def is_symlink(self):
|
|
201
|
+
return (self.outer_file_attrs >> 16) & 0xF000 == 0xA000
|
|
202
|
+
|
|
199
203
|
@property
|
|
200
204
|
def extra_fields(self) -> dict[int, bytes]:
|
|
201
205
|
"""
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
def remove_consecutive_duplicates(data: bytes, byte: int = b'/'[0]) -> bytes:
|
|
2
|
+
"""双指针法,最快实现"""
|
|
3
|
+
if not data:
|
|
4
|
+
return data
|
|
5
|
+
|
|
6
|
+
result = bytearray()
|
|
7
|
+
prev = None
|
|
8
|
+
for b in data:
|
|
9
|
+
if b == byte and prev == byte:
|
|
10
|
+
continue # 跳过重复的
|
|
11
|
+
result.append(b)
|
|
12
|
+
prev = b
|
|
13
|
+
return bytes(result)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def normpath(path: bytes) -> bytes:
|
|
17
|
+
return remove_consecutive_duplicates(path).removeprefix(b'/')
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import struct
|
|
2
2
|
from collections.abc import Buffer
|
|
3
|
-
from
|
|
3
|
+
from fnmatch import fnmatch
|
|
4
|
+
from typing import AsyncGenerator, Any
|
|
4
5
|
|
|
5
|
-
from .algorithm_register import
|
|
6
|
+
from .algorithm_register import is_algorithm_handlier_available, get_algorithm_handler
|
|
6
7
|
from .const import *
|
|
7
|
-
from .errors import UnsupportedAlgorithmError
|
|
8
|
+
from .errors import UnsupportedAlgorithmError
|
|
8
9
|
from .models import EOCD, Zip64EOCD, CDEntry, LocalFileHeader
|
|
10
|
+
from .zip_crypto import zipcrypto_wrapper
|
|
11
|
+
from .path_methods import normpath
|
|
9
12
|
|
|
10
13
|
|
|
11
14
|
class DataSource:
|
|
@@ -47,7 +50,7 @@ class RemoteZip:
|
|
|
47
50
|
(RemoteZip 不会缓存已读取的文件,你需要自己保证数据复用)
|
|
48
51
|
|
|
49
52
|
:ivar source: 数据源回调对象
|
|
50
|
-
:ivar
|
|
53
|
+
:ivar zip_info: 中央仓库元数据(EOCD),可能为 EOCD、Zip64EOCD 或 None 值
|
|
51
54
|
:ivar files: 中央目录文件列表
|
|
52
55
|
:ivar file_mapping: 构建的文件映射表,可能为 None
|
|
53
56
|
:ivar is_zip64: 此 zip 文件是否启动了 zip64 扩展,默认为 False
|
|
@@ -62,24 +65,60 @@ class RemoteZip:
|
|
|
62
65
|
async def _get_total_size(self):
|
|
63
66
|
return await self.source.get_total_size()
|
|
64
67
|
|
|
65
|
-
|
|
68
|
+
__slots__ = [
|
|
69
|
+
'source', 'zip_info', 'files', 'file_mapping', 'cached_headers', 'is_zip64', 'context',
|
|
70
|
+
'_namelist'
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
def __init__(self, source: DataSource, **kwargs):
|
|
66
74
|
self.source = source
|
|
67
75
|
|
|
68
|
-
self.
|
|
69
|
-
self.files: list[CDEntry] =
|
|
76
|
+
self.zip_info: EOCD | Zip64EOCD | None = None
|
|
77
|
+
self.files: list[CDEntry] | None = None
|
|
70
78
|
self.file_mapping: dict[bytes, CDEntry] | None = None
|
|
71
79
|
|
|
72
|
-
self.
|
|
80
|
+
self.cached_headers: dict[bytes, LocalFileHeader] = {}
|
|
73
81
|
|
|
74
|
-
|
|
82
|
+
self.is_zip64 = False
|
|
83
|
+
self.context = kwargs
|
|
84
|
+
self._namelist = None
|
|
85
|
+
|
|
86
|
+
def _merge_context(self, overrides: dict[str, Any]):
|
|
87
|
+
return {
|
|
88
|
+
**self.context,
|
|
89
|
+
**overrides
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
def namelist(self):
|
|
93
|
+
if self._namelist is None:
|
|
94
|
+
if self.files is None:
|
|
95
|
+
raise ValueError("No files. Please call `fetch_file_list()` first")
|
|
96
|
+
self._namelist = [
|
|
97
|
+
f.filename for f in self.files
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
return self._namelist
|
|
101
|
+
|
|
102
|
+
async def fetch_zip_info(
|
|
103
|
+
self,
|
|
104
|
+
initial_chunk=4096,
|
|
105
|
+
max_cnt=-1,
|
|
106
|
+
loss_factor: int | float = 2,
|
|
107
|
+
reuse=True
|
|
108
|
+
) -> EOCD | Zip64EOCD:
|
|
75
109
|
"""
|
|
76
110
|
获取 zip 文件的元信息(EOCD)
|
|
77
111
|
|
|
112
|
+
:param reuse: 如果启用,函数会缓存上一次的获取结果(并保存在 ``.zip_info`` 中),否则将会重新扫描 EOCD 并覆盖旧缓存
|
|
78
113
|
:param initial_chunk: 初始扫描的大小
|
|
79
114
|
:param max_cnt: 最大重试次数
|
|
80
115
|
:param loss_factor: 重试后更新扫描大小的系数
|
|
81
|
-
:return: 一个 EOCD 或者 Zip64EOCD 对象(如果这个 zip 文件启用了 zip64 扩展)
|
|
116
|
+
:return: 一个 ``EOCD`` 或者 ``Zip64EOCD`` 对象(如果这个 zip 文件启用了 zip64 扩展)
|
|
117
|
+
|
|
82
118
|
"""
|
|
119
|
+
if reuse and self.zip_info is not None:
|
|
120
|
+
return self.zip_info
|
|
121
|
+
|
|
83
122
|
chunk_size = initial_chunk
|
|
84
123
|
|
|
85
124
|
last_start = await self._get_total_size()
|
|
@@ -95,9 +134,11 @@ class RemoteZip:
|
|
|
95
134
|
|
|
96
135
|
signature_pos = buffer.rfind(EOCD_SIGNATURE)
|
|
97
136
|
if signature_pos != -1:
|
|
98
|
-
self.
|
|
137
|
+
self.zip_info = eocd = EOCD.from_buffer(buffer[signature_pos:])
|
|
99
138
|
# return cnt
|
|
100
139
|
if eocd.total_entries == 0xFFFFFFFF or eocd.cd_offset == 0xFFFFFFFF or eocd.cd_size == 0xFFFFFFFF:
|
|
140
|
+
# 处理 zip64 扩展
|
|
141
|
+
# 我们需要解析 Locator 并定位到 Zip64 EOCD 获取信息
|
|
101
142
|
self.is_zip64 = True
|
|
102
143
|
|
|
103
144
|
# 继续获取 zip64 的 eocd
|
|
@@ -151,10 +192,10 @@ class RemoteZip:
|
|
|
151
192
|
|
|
152
193
|
# print(zip64_eocd)
|
|
153
194
|
|
|
154
|
-
self.
|
|
155
|
-
return self.
|
|
195
|
+
self.zip_info = zip64_eocd
|
|
196
|
+
return self.zip_info
|
|
156
197
|
else:
|
|
157
|
-
return self.
|
|
198
|
+
return self.zip_info
|
|
158
199
|
|
|
159
200
|
if max_cnt != -1 and cnt >= max_cnt:
|
|
160
201
|
raise RuntimeError
|
|
@@ -164,15 +205,24 @@ class RemoteZip:
|
|
|
164
205
|
else:
|
|
165
206
|
raise RuntimeError
|
|
166
207
|
|
|
167
|
-
async def fetch_file_list(self, ignore_extra: bool = False) -> list[CDEntry]:
|
|
208
|
+
async def fetch_file_list(self, ignore_extra: bool = False, reuse=True) -> list[CDEntry]:
|
|
168
209
|
"""
|
|
169
210
|
从远程 zip 的中央目录获取文件列表
|
|
170
211
|
|
|
212
|
+
自动在 `zip_info` 缺失的情况下拉取
|
|
213
|
+
|
|
171
214
|
:param ignore_extra: 是否忽略无法解析的片段并返回已有的内容
|
|
215
|
+
:param reuse: 如果启用,将自动缓存已拉取的数据(位于 ``.files`` 属性)并复用它,否则将重新拉取文件列表并覆盖缓存
|
|
172
216
|
:return: 一个列表,包含所有获取到的文件
|
|
173
217
|
"""
|
|
174
|
-
|
|
175
|
-
|
|
218
|
+
|
|
219
|
+
if reuse and self.files is not None:
|
|
220
|
+
return self.files
|
|
221
|
+
|
|
222
|
+
if self.zip_info is None:
|
|
223
|
+
self.zip_info = await self.fetch_zip_info()
|
|
224
|
+
|
|
225
|
+
eocd = self.zip_info
|
|
176
226
|
buffer = bytearray()
|
|
177
227
|
files = []
|
|
178
228
|
|
|
@@ -200,24 +250,29 @@ class RemoteZip:
|
|
|
200
250
|
break
|
|
201
251
|
|
|
202
252
|
if buffer and not ignore_extra:
|
|
203
|
-
# print("Read Entries: ", len(files))
|
|
204
|
-
# print("Buffer:", buffer)
|
|
205
253
|
raise ValueError()
|
|
206
254
|
|
|
207
255
|
self.files = files
|
|
208
256
|
|
|
257
|
+
self._namelist = None
|
|
258
|
+
|
|
209
259
|
return files
|
|
210
260
|
|
|
211
|
-
async def fetch_file_header(self, filename=None, entry: CDEntry | None = None):
|
|
261
|
+
async def fetch_file_header(self, filename=None, entry: CDEntry | None = None, reuse=True):
|
|
212
262
|
"""
|
|
213
263
|
获取 filename 对应的本地文件头
|
|
214
264
|
|
|
265
|
+
:param reuse: 如果启用,将自动缓存已获取的本地文件头,否则将重新拉取并覆盖缓存
|
|
215
266
|
:param filename: 文件名称,可选
|
|
216
267
|
:param entry: CDEntry 对象,可选
|
|
217
268
|
:return: 一个 LocalFileHeader 对象,以及在解析时获取到的多余数据(在文件本身很小的时候,这一段数据可以直接解析出文件内容)
|
|
218
269
|
"""
|
|
219
|
-
|
|
220
|
-
entry = entry or self.find_file_entry(filename)
|
|
270
|
+
|
|
271
|
+
entry = entry or self.find_file_entry(normpath(bytes(filename)))
|
|
272
|
+
|
|
273
|
+
if reuse and entry.filename in self.cached_headers:
|
|
274
|
+
return self.cached_headers[entry.filename]
|
|
275
|
+
|
|
221
276
|
buffer = await self._read_range(entry.local_header_offset, entry.__cstruct__.size + 64) # 预留64字节的变长字段
|
|
222
277
|
|
|
223
278
|
# 仅头部模式解析 LocalFileHeader
|
|
@@ -237,6 +292,14 @@ class RemoteZip:
|
|
|
237
292
|
header.raw_extra_fields = bytes(extra_fields)
|
|
238
293
|
|
|
239
294
|
header.fix_by_zip64()
|
|
295
|
+
|
|
296
|
+
if entry.has_data_descriptor:
|
|
297
|
+
header.compressed_size = entry.compressed_size
|
|
298
|
+
header.uncompressed_size = entry.original_size
|
|
299
|
+
header.crc32 = entry.crc32_raw
|
|
300
|
+
|
|
301
|
+
self.cached_headers[entry.filename] = header
|
|
302
|
+
|
|
240
303
|
return header, buffer[pos:] # 返回剩余的数据部分
|
|
241
304
|
|
|
242
305
|
def build_mapping(self):
|
|
@@ -247,6 +310,9 @@ class RemoteZip:
|
|
|
247
310
|
|
|
248
311
|
:return: 构建完成的映射表
|
|
249
312
|
"""
|
|
313
|
+
if self.files is None:
|
|
314
|
+
raise ValueError("No files. Please call `fetch_file_list` first.")
|
|
315
|
+
|
|
250
316
|
m = self.file_mapping = {}
|
|
251
317
|
for entry in self.files:
|
|
252
318
|
m[entry.filename] = entry
|
|
@@ -262,9 +328,17 @@ class RemoteZip:
|
|
|
262
328
|
|
|
263
329
|
若是发现文件不存在,将会抛出 FileNotFoundError
|
|
264
330
|
|
|
265
|
-
:param filename:
|
|
266
|
-
:
|
|
331
|
+
:param filename: 文件名
|
|
332
|
+
:raise FileNotFoundError: 当找不到所需要的文件时抛出
|
|
333
|
+
:raise ValueError: 当本地没有文件列表时抛出
|
|
334
|
+
:return: 文件的 CDEntry 对象
|
|
267
335
|
"""
|
|
336
|
+
|
|
337
|
+
filename = normpath(filename)
|
|
338
|
+
|
|
339
|
+
if self.files is None:
|
|
340
|
+
raise ValueError("No files. Please call `fetch_file_list` first.")
|
|
341
|
+
|
|
268
342
|
if self.file_mapping:
|
|
269
343
|
return self.file_mapping[filename]
|
|
270
344
|
else:
|
|
@@ -274,51 +348,120 @@ class RemoteZip:
|
|
|
274
348
|
else:
|
|
275
349
|
raise FileNotFoundError(filename)
|
|
276
350
|
|
|
277
|
-
async def _stream_single_file(self, filename: bytes):
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if entry.is_encrypted:
|
|
281
|
-
raise UnsupportedCryptoError()
|
|
351
|
+
async def _stream_single_file(self, filename: bytes, kwargs):
|
|
352
|
+
kwargs = self._merge_context(kwargs)
|
|
282
353
|
|
|
283
|
-
|
|
284
|
-
raise UnsupportedDataDescriptorError()
|
|
354
|
+
entry = self.find_file_entry(filename)
|
|
285
355
|
|
|
286
|
-
if not
|
|
356
|
+
if not is_algorithm_handlier_available(entry.algorithm):
|
|
287
357
|
raise UnsupportedAlgorithmError(entry.algorithm)
|
|
288
358
|
|
|
289
359
|
header, buffer = await self.fetch_file_header(entry=entry)
|
|
290
|
-
# print(header)
|
|
291
360
|
real_data_offset = entry.local_header_offset + header.data_offset
|
|
292
361
|
|
|
362
|
+
if entry.has_data_descriptor:
|
|
363
|
+
header.compressed_size = entry.compressed_size
|
|
364
|
+
header.uncompressed_size = entry.original_size
|
|
365
|
+
header.crc32 = entry.crc32_raw
|
|
366
|
+
|
|
293
367
|
if entry.compressed_size < len(buffer):
|
|
294
368
|
raw_generator = single_chunk_wrapper(buffer[:entry.compressed_size])
|
|
295
369
|
else:
|
|
296
370
|
raw_generator = self._read_range_stream(real_data_offset, entry.compressed_size)
|
|
297
371
|
|
|
298
|
-
|
|
372
|
+
if entry.is_encrypted and entry.algorithm != 0x63: # 暂时只支持 Zip-Crypto (Legacy)
|
|
373
|
+
raw_generator = zipcrypto_wrapper(raw_generator, entry, header, kwargs)
|
|
374
|
+
|
|
375
|
+
# 对于 zip-aes 的支持可以作为 algorithm-handler
|
|
376
|
+
# 它会把 algorithm 设成 0x63,可以直接让它路由到我们的 AES 解密包装器上
|
|
299
377
|
|
|
300
|
-
|
|
378
|
+
return get_algorithm_handler(entry.algorithm)(raw_generator, entry, header, kwargs)
|
|
379
|
+
|
|
380
|
+
async def stream(self, filename: bytes, **kwargs):
|
|
301
381
|
"""
|
|
302
382
|
流式读取一个压缩包内的文件
|
|
303
383
|
|
|
304
384
|
:param filename: 压缩包内的文件名
|
|
305
385
|
:return:
|
|
306
386
|
"""
|
|
307
|
-
gen = await self._stream_single_file(filename)
|
|
387
|
+
gen = await self._stream_single_file(normpath(filename), kwargs)
|
|
308
388
|
async for i in gen:
|
|
309
389
|
yield i
|
|
310
390
|
|
|
311
|
-
async def
|
|
391
|
+
async def read(self, filename: bytes, **kwargs) -> bytearray:
|
|
312
392
|
"""
|
|
313
|
-
|
|
393
|
+
直接全量加载文件的内容
|
|
314
394
|
|
|
315
395
|
建议在小文件上使用此方法
|
|
316
396
|
|
|
317
397
|
:param filename: 压缩包内的文件名
|
|
318
|
-
:return:
|
|
398
|
+
:return: 一个 bytearray 表示文件内容
|
|
319
399
|
"""
|
|
320
400
|
buffer = bytearray()
|
|
321
|
-
async for i in self.
|
|
401
|
+
async for i in self.stream(filename, **kwargs):
|
|
322
402
|
buffer.extend(i)
|
|
323
403
|
|
|
324
404
|
return buffer
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
async def resolve(self, filename: bytes, max_resolve=24, **kwargs) -> bytes:
|
|
408
|
+
kwargs = self._merge_context(kwargs)
|
|
409
|
+
entry = self.find_file_entry(filename)
|
|
410
|
+
target = entry.filename
|
|
411
|
+
|
|
412
|
+
cnt = 0
|
|
413
|
+
while entry.is_symlink:
|
|
414
|
+
target = await self.read(filename, **kwargs)
|
|
415
|
+
try:
|
|
416
|
+
entry = self.find_file_entry(target)
|
|
417
|
+
except FileNotFoundError:
|
|
418
|
+
break
|
|
419
|
+
|
|
420
|
+
cnt += 1
|
|
421
|
+
|
|
422
|
+
return target
|
|
423
|
+
|
|
424
|
+
def listdir(self, path: bytes) -> list[bytes]:
|
|
425
|
+
path = normpath(path).removesuffix(b'/') + b'/'
|
|
426
|
+
return [
|
|
427
|
+
i
|
|
428
|
+
for i in self.namelist()
|
|
429
|
+
if i.startswith(path) and i != path # 确保不会包含目录本身
|
|
430
|
+
]
|
|
431
|
+
|
|
432
|
+
def exists(self, path: bytes) -> bool:
|
|
433
|
+
return normpath(path) in self.namelist()
|
|
434
|
+
|
|
435
|
+
def entry(self, path: bytes) -> CDEntry:
|
|
436
|
+
i = self.namelist().index(normpath(path))
|
|
437
|
+
if i == -1:
|
|
438
|
+
raise FileNotFoundError()
|
|
439
|
+
|
|
440
|
+
return self.files[i]
|
|
441
|
+
|
|
442
|
+
def original_size(self, path: bytes) -> int:
|
|
443
|
+
return self.entry(path).original_size
|
|
444
|
+
|
|
445
|
+
def compressed_size(self, path: bytes) -> int:
|
|
446
|
+
return self.entry(path).compressed_size
|
|
447
|
+
|
|
448
|
+
async def header(self, path: bytes):
|
|
449
|
+
return await self.fetch_file_header(
|
|
450
|
+
filename=normpath(path)
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
async def read_text(self, path: bytes, encoding='utf-8', errors='strict', **kwargs) -> str:
|
|
454
|
+
return (await self.read(path, **kwargs)).decode(encoding, errors=errors)
|
|
455
|
+
|
|
456
|
+
def rglob(self, pattern):
|
|
457
|
+
for file in self._namelist:
|
|
458
|
+
if fnmatch(file, pattern):
|
|
459
|
+
yield file
|
|
460
|
+
|
|
461
|
+
def rglob_entries(self, pattern):
|
|
462
|
+
if self.files is None:
|
|
463
|
+
raise ValueError("No files. Please call `fetch_file_list` first.")
|
|
464
|
+
|
|
465
|
+
for file in self.files:
|
|
466
|
+
if fnmatch(file.filename, pattern):
|
|
467
|
+
yield file
|
|
File without changes
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Written by DeepSeek
|
|
2
|
+
|
|
3
|
+
from cryptography.hazmat.primitives import hashes
|
|
4
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
5
|
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def pbkdf2_hmac_impl(pwd: bytes, length: int, salt: bytes, iterations: int) -> bytes:
|
|
9
|
+
kdf = PBKDF2HMAC(
|
|
10
|
+
algorithm=hashes.SHA1(),
|
|
11
|
+
length=length,
|
|
12
|
+
salt=salt,
|
|
13
|
+
iterations=iterations,
|
|
14
|
+
)
|
|
15
|
+
return kdf.derive(pwd) # 注意:密码需要单独传递
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class CryptographyAESCipher:
|
|
19
|
+
__slots__ = ('_d', )
|
|
20
|
+
|
|
21
|
+
def __init__(self, decryptor):
|
|
22
|
+
self._d = decryptor
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def get_cipher(cls, enc_key: bytes):
|
|
26
|
+
cipher = Cipher(algorithms.AES(enc_key), modes.CTR((1).to_bytes(16, 'little')))
|
|
27
|
+
decryptor = cipher.decryptor()
|
|
28
|
+
return cls(decryptor)
|
|
29
|
+
|
|
30
|
+
def update(self, encrypted_data: bytes) -> bytes:
|
|
31
|
+
return self._d.update(encrypted_data)
|
|
32
|
+
|
|
33
|
+
def finalize(self) -> bytes:
|
|
34
|
+
return self._d.finalize()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def install():
|
|
38
|
+
from rtzip.zip_aes import install
|
|
39
|
+
install(pbkdf2_hmac_impl, CryptographyAESCipher)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Written by DeepSeek
|
|
2
|
+
|
|
3
|
+
from Crypto.Cipher import AES
|
|
4
|
+
from Crypto.Protocol.KDF import PBKDF2
|
|
5
|
+
from Crypto.Hash import SHA1
|
|
6
|
+
from Crypto.Util import Counter
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def pbkdf2_hmac_pycryptodome(pwd: bytes, length: int, salt: bytes, iterations: int) -> bytes:
|
|
10
|
+
"""
|
|
11
|
+
使用 PyCryptodome 的 PBKDF2 实现
|
|
12
|
+
"""
|
|
13
|
+
return PBKDF2(
|
|
14
|
+
password=pwd,
|
|
15
|
+
salt=salt,
|
|
16
|
+
dkLen=length,
|
|
17
|
+
count=iterations,
|
|
18
|
+
hmac_hash_module=SHA1
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class PyCryptodomeAESCipher:
|
|
23
|
+
__slots__ = ('_cipher',)
|
|
24
|
+
|
|
25
|
+
def __init__(self, cipher):
|
|
26
|
+
self._cipher = cipher
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
def get_cipher(cls, enc_key: bytes):
|
|
30
|
+
"""
|
|
31
|
+
创建 AES-CTR 解密器
|
|
32
|
+
"""
|
|
33
|
+
ctr = Counter.new(128, little_endian=True, initial_value=1)
|
|
34
|
+
cipher = AES.new(enc_key, AES.MODE_CTR, counter=ctr)
|
|
35
|
+
return cls(cipher)
|
|
36
|
+
|
|
37
|
+
def update(self, encrypted_data: bytes) -> bytes:
|
|
38
|
+
return self._cipher.decrypt(encrypted_data)
|
|
39
|
+
|
|
40
|
+
def finalize(self) -> bytes:
|
|
41
|
+
# pycryptodome 的 CTR 模式没有 finalize,直接返回空
|
|
42
|
+
return b''
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def install():
|
|
46
|
+
from rtzip.zip_aes import install
|
|
47
|
+
install(pbkdf2_hmac_pycryptodome, PyCryptodomeAESCipher)
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import hashlib
|
|
3
|
+
import hmac
|
|
4
|
+
from typing import Protocol
|
|
5
|
+
|
|
6
|
+
from obstruct import Struct, u16, u8
|
|
7
|
+
|
|
8
|
+
from .errors import UnsupportedAlgorithmError, WrongPasswordError
|
|
9
|
+
from .algorithm_register import algorithm_handler, is_algorithm_handlier_available, get_algorithm_handler
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclasses.dataclass(slots=True)
|
|
13
|
+
class AESExtraData(Struct):
|
|
14
|
+
vendor_version: u16
|
|
15
|
+
vendor_id: u16 # 2 = AE-1, 3 = AE-2
|
|
16
|
+
strength: u8 # 1=128bits, 2=192bits, 3=256bits
|
|
17
|
+
compression: u16
|
|
18
|
+
# mac_length: u16 # only available on AE-2
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# 这些方法要求用户在初始化的时候设置,否则无法解密
|
|
22
|
+
|
|
23
|
+
_decrypt_helpers = {}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class PBKDF2_HMAC_FunctionType(Protocol):
|
|
27
|
+
def __call__(self, pwd: bytes, length: int, salt: bytes, iterations: int) -> bytes:
|
|
28
|
+
"""
|
|
29
|
+
PBKDF2-HMAC-SHA1 实现函数
|
|
30
|
+
|
|
31
|
+
:param pwd: 解密使用的密码
|
|
32
|
+
:param length: 派生密钥的总长度
|
|
33
|
+
:param salt: 密钥盐
|
|
34
|
+
:param iterations: 迭代次数
|
|
35
|
+
:return: 一个字节串,表示派生的密钥
|
|
36
|
+
"""
|
|
37
|
+
...
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
from typing import Protocol, runtime_checkable
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@runtime_checkable
|
|
44
|
+
class AESDecryptor(Protocol):
|
|
45
|
+
"""
|
|
46
|
+
AES 计数器模式 (CTR) 解密器协议。
|
|
47
|
+
|
|
48
|
+
该协议定义了 WinZip AES 加密解密所需的核心接口,
|
|
49
|
+
任何实现了该协议的类型都可以用于 AES-CTR 模式解密。
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
@classmethod
|
|
53
|
+
def get_cipher(cls, enc_key: bytes) -> 'AESDecryptor':
|
|
54
|
+
"""
|
|
55
|
+
创建并返回一个 AES-CTR 模式解密器实例。
|
|
56
|
+
|
|
57
|
+
该方法是工厂方法,用于初始化解密器。实现类应确保
|
|
58
|
+
返回的实例处于可解密状态,并准备好处理数据。
|
|
59
|
+
|
|
60
|
+
:param enc_key: AES 加密密钥,长度必须为 16 (AES-128)、
|
|
61
|
+
24 (AES-192) 或 32 (AES-256) 字节
|
|
62
|
+
:param iv: 初始化向量 (Initialization Vector),固定 16 字节,
|
|
63
|
+
作为 AES-CTR 模式的计数器初始值
|
|
64
|
+
:return: 初始化完成的 AESCipher 实例
|
|
65
|
+
:raises ValueError: 当 enc_key 或 iv 长度不符合要求时抛出
|
|
66
|
+
|
|
67
|
+
:example:
|
|
68
|
+
>>> cipher = AESDecryptor.get_cipher(
|
|
69
|
+
... enc_key=b'\\x00' * 32,
|
|
70
|
+
... iv=b'\\x00' * 16
|
|
71
|
+
... )
|
|
72
|
+
"""
|
|
73
|
+
...
|
|
74
|
+
|
|
75
|
+
def update(self, encrypted_data: bytes) -> bytes:
|
|
76
|
+
"""
|
|
77
|
+
解密一部分数据,返回解密后的明文数据。
|
|
78
|
+
|
|
79
|
+
该方法可以多次调用,每次处理一个数据块。解密状态会
|
|
80
|
+
在调用之间保持,以支持流式解密。如果 encrypted_data
|
|
81
|
+
为空,返回空字节串。
|
|
82
|
+
|
|
83
|
+
:param encrypted_data: 需要解密的密文数据块,长度任意
|
|
84
|
+
:return: 解密后的明文数据块,长度与输入相同
|
|
85
|
+
:raises ValueError: 当解密器未正确初始化或处于无效状态时抛出
|
|
86
|
+
|
|
87
|
+
:example:
|
|
88
|
+
>>> cipher = AESDecryptor.get_cipher(enc_key, iv)
|
|
89
|
+
>>> plaintext1 = cipher.update(ciphertext[:1024])
|
|
90
|
+
>>> plaintext2 = cipher.update(ciphertext[1024:])
|
|
91
|
+
>>> plaintext = plaintext1 + plaintext2
|
|
92
|
+
"""
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
def finalize(self) -> bytes:
|
|
96
|
+
"""
|
|
97
|
+
完成解密过程,返回剩余的明文数据并清理资源。
|
|
98
|
+
|
|
99
|
+
该方法应在所有数据通过 update() 处理完毕后调用。它会
|
|
100
|
+
处理可能存在的剩余数据(对于 CTR 模式通常为空),
|
|
101
|
+
并执行必要的清理工作(如清除敏感数据)。调用后,解密器
|
|
102
|
+
实例应被视为不可用状态,不应再调用 update()。
|
|
103
|
+
|
|
104
|
+
:return: 剩余的明文数据(对于 AES-CTR 通常为空字节串)
|
|
105
|
+
:raises ValueError: 当解密器处于无效状态或已被 finalize 时抛出
|
|
106
|
+
|
|
107
|
+
:example:
|
|
108
|
+
>>> cipher = AESDecryptor.get_cipher(enc_key, iv)
|
|
109
|
+
>>> plaintext = cipher.update(ciphertext)
|
|
110
|
+
>>> plaintext += cipher.finalize()
|
|
111
|
+
"""
|
|
112
|
+
...
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def pbkdf2_hmac_implement[T: PBKDF2_HMAC_FunctionType](func: T) -> T:
|
|
116
|
+
"""
|
|
117
|
+
注入 pbkdf2-hmac 的实现,库为了轻量,并没有选择依赖任何已有的加密库,而是将选择留给开发者
|
|
118
|
+
函数接受 2 个参数:
|
|
119
|
+
1. length: int 这个参数指定了派生密钥的总长度
|
|
120
|
+
2. salt: 密钥的 salt
|
|
121
|
+
|
|
122
|
+
返回一个字节串,表示派生密钥的值
|
|
123
|
+
|
|
124
|
+
:param func:
|
|
125
|
+
:return:
|
|
126
|
+
"""
|
|
127
|
+
_decrypt_helpers['pbkdf2-hmac'] = func
|
|
128
|
+
return func
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def aes_cipher_implement[T: type[AESDecryptor]](cls: T) -> T:
|
|
132
|
+
_decrypt_helpers['aes-cipher'] = cls
|
|
133
|
+
return cls
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def call_pbkdf2_hmac(pwd: bytes, length: int, salt: bytes, iterations: int):
|
|
137
|
+
return _decrypt_helpers['pbkdf2-hmac'](pwd, length, salt, iterations)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def get_cipher(enc_key: bytes) -> AESDecryptor:
|
|
141
|
+
return _decrypt_helpers['aes-cipher'].get_cipher(enc_key)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# @algorithm_handler(0x0063)
|
|
145
|
+
async def zip_aes_stream_decryptor(
|
|
146
|
+
raw_data,
|
|
147
|
+
compressed_size,
|
|
148
|
+
aes_extra,
|
|
149
|
+
raw_aes_extra, pwd
|
|
150
|
+
):
|
|
151
|
+
mac_length = 10
|
|
152
|
+
|
|
153
|
+
# print(aes_extra)
|
|
154
|
+
# print(raw_aes_extra, len(raw_aes_extra), aes_extra.__cstruct__.size)
|
|
155
|
+
|
|
156
|
+
if aes_extra.vendor_version == 3:
|
|
157
|
+
offset = aes_extra.__cstruct__.size
|
|
158
|
+
mac_length = int.from_bytes(
|
|
159
|
+
raw_aes_extra[offset:offset + 2], 'little'
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
key_len = (0, 16, 24, 32)[aes_extra.strength]
|
|
163
|
+
salt_len = (0, 8, 12, 16)[aes_extra.strength]
|
|
164
|
+
|
|
165
|
+
buf = bytearray()
|
|
166
|
+
|
|
167
|
+
enc_header_len = salt_len + 2
|
|
168
|
+
|
|
169
|
+
while len(buf) < enc_header_len:
|
|
170
|
+
buf.extend(await anext(raw_data))
|
|
171
|
+
|
|
172
|
+
salt = buf[:salt_len]
|
|
173
|
+
quick_check = buf[salt_len: salt_len + 2]
|
|
174
|
+
|
|
175
|
+
encrypted_data_len = compressed_size - enc_header_len - mac_length
|
|
176
|
+
|
|
177
|
+
# 获取派生密钥
|
|
178
|
+
key_material = call_pbkdf2_hmac(pwd, 2 * key_len + 2, salt, 1000)
|
|
179
|
+
enc_key = key_material[:key_len]
|
|
180
|
+
hmac_key = key_material[key_len:key_len * 2]
|
|
181
|
+
checksum = key_material[-2:]
|
|
182
|
+
|
|
183
|
+
# print(f"Vendor Version: {aes_extra.vendor_version}")
|
|
184
|
+
# print(f"Strenth: {aes_extra.strength}")
|
|
185
|
+
# print(f"User Password: {pwd}")
|
|
186
|
+
# print(f"Zip Salt: {salt} ({len(salt)})")
|
|
187
|
+
# print(F"HMAC Length: {mac_length}")
|
|
188
|
+
# print(F"ENC Key: {enc_key}")
|
|
189
|
+
# print(F"HMAC Key: {hmac_key}")
|
|
190
|
+
# print(F"Check: {checksum} vs. {quick_check}")
|
|
191
|
+
|
|
192
|
+
if checksum != quick_check:
|
|
193
|
+
raise WrongPasswordError()
|
|
194
|
+
|
|
195
|
+
# 流式解密
|
|
196
|
+
# 同时需要校验
|
|
197
|
+
|
|
198
|
+
cipher = get_cipher(enc_key)
|
|
199
|
+
|
|
200
|
+
h = hmac.new(
|
|
201
|
+
key=hmac_key,
|
|
202
|
+
digestmod=hashlib.sha1
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
size = len(buf) - enc_header_len
|
|
206
|
+
|
|
207
|
+
_ = buf[enc_header_len:]
|
|
208
|
+
yield (x := cipher.update(_)) # 先把读取AES头产生的余量数据消耗掉
|
|
209
|
+
h.update(_) # 更新校验值
|
|
210
|
+
|
|
211
|
+
# print("Data in Buffer", x)
|
|
212
|
+
|
|
213
|
+
buf = bytearray()
|
|
214
|
+
|
|
215
|
+
async for chunk in raw_data: # 开始遍历真正的数据
|
|
216
|
+
size += len(chunk) # 累计大小
|
|
217
|
+
|
|
218
|
+
if size > encrypted_data_len: # 如果:发现已接收大小超了
|
|
219
|
+
# print("Overflow")
|
|
220
|
+
# 多出来的部分的大小就是 size - encrypted_data_len
|
|
221
|
+
# 相对的索引就是这个值取反
|
|
222
|
+
# 此后就是真实的 MAC 校验值了
|
|
223
|
+
pos = -(size - encrypted_data_len)
|
|
224
|
+
|
|
225
|
+
yield cipher.update(chunk[:pos])
|
|
226
|
+
h.update(chunk[:pos])
|
|
227
|
+
|
|
228
|
+
buf.extend(chunk[pos:]) # 把剩余数据放进 buffer
|
|
229
|
+
# 某些极端情况下可能会产生数据差几个字节的问题
|
|
230
|
+
# 我们需要在后面把数据补上
|
|
231
|
+
break
|
|
232
|
+
|
|
233
|
+
# 否则:正常迭代、解密和更新校验值
|
|
234
|
+
yield cipher.update(chunk)
|
|
235
|
+
h.update(chunk)
|
|
236
|
+
|
|
237
|
+
# print("Get chunk {}/{}".format(size, encrypted_data_len))
|
|
238
|
+
|
|
239
|
+
# 该读取 MAC 了
|
|
240
|
+
|
|
241
|
+
while len(buf) < mac_length:
|
|
242
|
+
buf.extend(await anext(raw_data))
|
|
243
|
+
|
|
244
|
+
# 现在肯定够了
|
|
245
|
+
|
|
246
|
+
if len(buf) != mac_length:
|
|
247
|
+
raise ValueError("Unexpected extra data")
|
|
248
|
+
|
|
249
|
+
# print(f"Elpasted Size: {len(buf)}")
|
|
250
|
+
mac = buf[:mac_length]
|
|
251
|
+
|
|
252
|
+
digest = h.digest()[:10]
|
|
253
|
+
if not hmac.compare_digest(digest, mac):
|
|
254
|
+
# print(digest, mac, h.digest())
|
|
255
|
+
raise ValueError("Wrong HMAC")
|
|
256
|
+
|
|
257
|
+
try:
|
|
258
|
+
await anext(raw_data)
|
|
259
|
+
raise ValueError("Unexpect extra data")
|
|
260
|
+
|
|
261
|
+
except StopAsyncIteration:
|
|
262
|
+
# print("Stop")
|
|
263
|
+
return
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def zip_aes_wrapper(raw_data, entry, header, ctx):
|
|
267
|
+
assert ctx.get('pwd')
|
|
268
|
+
assert _decrypt_helpers.get(
|
|
269
|
+
'aes-cipher'
|
|
270
|
+
), "You didn't register a AESCipher type, please register it by `aes_cipher_implement` decorator first"
|
|
271
|
+
assert _decrypt_helpers.get(
|
|
272
|
+
'pbkdf2-hmac'
|
|
273
|
+
), "You didn't register a PBKDF2-HMAC function, please register it by `pbkdf2_hmac_implement` decorator first"
|
|
274
|
+
|
|
275
|
+
raw_aes_extra = header.extra_fields[0x9901]
|
|
276
|
+
aes_extra = AESExtraData.unpack(raw_aes_extra)
|
|
277
|
+
|
|
278
|
+
# 获取解密后的压缩格式
|
|
279
|
+
algorithm = aes_extra.compression
|
|
280
|
+
|
|
281
|
+
if not is_algorithm_handlier_available(algorithm):
|
|
282
|
+
raise UnsupportedAlgorithmError(algorithm)
|
|
283
|
+
|
|
284
|
+
return get_algorithm_handler(algorithm)(
|
|
285
|
+
zip_aes_stream_decryptor(raw_data, entry.compressed_size, aes_extra, raw_aes_extra, ctx.get('pwd')),
|
|
286
|
+
entry, header, ctx
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def install(pbkdf2_hmac: PBKDF2_HMAC_FunctionType, aes_cipher: type[AESDecryptor]):
|
|
291
|
+
pbkdf2_hmac_implement(pbkdf2_hmac)
|
|
292
|
+
aes_cipher_implement(aes_cipher)
|
|
293
|
+
algorithm_handler(0x0063)(zip_aes_wrapper) if not is_algorithm_handlier_available(0x0063) else None
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
from typing import AsyncGenerator, Any
|
|
2
|
+
|
|
3
|
+
from .models import CDEntry, LocalFileHeader
|
|
4
|
+
|
|
5
|
+
_crc_table = None
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_crc_table():
|
|
9
|
+
global _crc_table
|
|
10
|
+
if _crc_table is None:
|
|
11
|
+
table = [0] * 256
|
|
12
|
+
for i in range(256):
|
|
13
|
+
crc = i
|
|
14
|
+
for j in range(8):
|
|
15
|
+
if crc & 1:
|
|
16
|
+
crc = 0xEDB88320 ^ (crc >> 1)
|
|
17
|
+
else:
|
|
18
|
+
crc >>= 1
|
|
19
|
+
|
|
20
|
+
table[i] = crc & 0xFFFFFFFF
|
|
21
|
+
|
|
22
|
+
_crc_table = table
|
|
23
|
+
|
|
24
|
+
return _crc_table
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def crc_update_keys(pwd: bytes, k0, k1, k2):
|
|
28
|
+
for b in pwd:
|
|
29
|
+
k0, k1, k2 = crc_update_keys_byte(b, k0, k1, k2)
|
|
30
|
+
|
|
31
|
+
return k0, k1, k2
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def crc_update_keys_byte(b: int, k0, k1, k2):
|
|
35
|
+
t = get_crc_table()
|
|
36
|
+
|
|
37
|
+
k0 = t[(k0 ^ b) & 0xFF] ^ (k0 >> 8)
|
|
38
|
+
k0 &= 0xFFFFFFFF
|
|
39
|
+
|
|
40
|
+
k1 = (k1 + (k0 & 0xFF)) & 0xFFFFFFFF
|
|
41
|
+
k1 = (k1 * 0x08088405 + 1) & 0xFFFFFFFF
|
|
42
|
+
|
|
43
|
+
k2 = t[(k2 ^ ((k1 >> 24) & 0xFF)) & 0xFF] ^ (k2 >> 8)
|
|
44
|
+
k2 &= 0xFFFFFFFF
|
|
45
|
+
|
|
46
|
+
return k0, k1, k2
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def crc_keys_from_bytes(pwd: bytes):
|
|
50
|
+
k0, k1, k2 = 0x12345678, 0x23456789, 0x34567890
|
|
51
|
+
return crc_update_keys(pwd, k0, k1, k2)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def decrypt_byte(k2: int) -> int:
|
|
55
|
+
temp = k2 | 3
|
|
56
|
+
return ((temp * (temp ^ 1)) >> 8) & 0xFF
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
async def zipcrypto_wrapper(
|
|
60
|
+
raw_data: AsyncGenerator[bytes, None],
|
|
61
|
+
entry: CDEntry,
|
|
62
|
+
header: LocalFileHeader,
|
|
63
|
+
ctx: dict[str , Any]
|
|
64
|
+
) -> AsyncGenerator[bytes, None]:
|
|
65
|
+
pwd = bytes(ctx['pwd'])
|
|
66
|
+
|
|
67
|
+
buffer = bytearray()
|
|
68
|
+
|
|
69
|
+
keys = crc_keys_from_bytes(pwd)
|
|
70
|
+
|
|
71
|
+
crc = header.crc32
|
|
72
|
+
|
|
73
|
+
# Get encrypted header
|
|
74
|
+
while 12 > len(buffer):
|
|
75
|
+
buffer.extend(await anext(raw_data))
|
|
76
|
+
|
|
77
|
+
encrypted_header = buffer[:12]
|
|
78
|
+
buffer = buffer[12:]
|
|
79
|
+
|
|
80
|
+
for b in range(12):
|
|
81
|
+
k = decrypt_byte(keys[2])
|
|
82
|
+
dec = encrypted_header[b] ^ k
|
|
83
|
+
|
|
84
|
+
keys = crc_update_keys_byte(dec, *keys)
|
|
85
|
+
|
|
86
|
+
if b == 11 and dec != ((crc >> 24) & 0xFF):
|
|
87
|
+
raise ValueError("Wrong Password")
|
|
88
|
+
|
|
89
|
+
# 开始解密文件的数据
|
|
90
|
+
data = bytearray()
|
|
91
|
+
|
|
92
|
+
for b in buffer:
|
|
93
|
+
k = decrypt_byte(keys[2])
|
|
94
|
+
dec = b ^ k
|
|
95
|
+
|
|
96
|
+
data.append(dec)
|
|
97
|
+
|
|
98
|
+
keys = crc_update_keys_byte(dec, *keys)
|
|
99
|
+
|
|
100
|
+
buffer.clear()
|
|
101
|
+
del buffer
|
|
102
|
+
|
|
103
|
+
yield bytes(data)
|
|
104
|
+
|
|
105
|
+
async for chunk in raw_data:
|
|
106
|
+
data.clear()
|
|
107
|
+
|
|
108
|
+
for b in chunk:
|
|
109
|
+
k = decrypt_byte(keys[2])
|
|
110
|
+
dec = b ^ k
|
|
111
|
+
|
|
112
|
+
data.append(dec)
|
|
113
|
+
|
|
114
|
+
keys = crc_update_keys_byte(dec, *keys)
|
|
115
|
+
|
|
116
|
+
yield bytes(data)
|
|
117
|
+
|
rtzip-0.3.1/LICENSE
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
BSD 3-Clause License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026, LittleNightSong
|
|
4
|
-
|
|
5
|
-
Redistribution and use in source and binary forms, with or without
|
|
6
|
-
modification, are permitted provided that the following conditions are met:
|
|
7
|
-
|
|
8
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
-
list of conditions and the following disclaimer.
|
|
10
|
-
|
|
11
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
-
this list of conditions and the following disclaimer in the documentation
|
|
13
|
-
and/or other materials provided with the distribution.
|
|
14
|
-
|
|
15
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
-
contributors may be used to endorse or promote products derived from
|
|
17
|
-
this software without specific prior written permission.
|
|
18
|
-
|
|
19
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
rtzip-0.3.1/PKG-INFO
DELETED
rtzip-0.3.1/README.md
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
# Rtzip
|
|
2
|
-
|
|
3
|
-
_一个以最小的数据量提取远程 zip 文件内容的库_
|
|
4
|
-
|
|
5
|
-
## 特点
|
|
6
|
-
|
|
7
|
-
- 不与任何 HTTP 库耦合
|
|
8
|
-
- 依赖简单(只有用于解析二进制结构的 `obstruct`)
|
|
9
|
-
- 兼容标准 zip 格式、ZIP64 扩展
|
|
10
|
-
|
|
11
|
-
## 安装
|
|
12
|
-
|
|
13
|
-
```commandline
|
|
14
|
-
pip install rtzip
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## 用法
|
|
18
|
-
|
|
19
|
-
### 直接使用
|
|
20
|
-
|
|
21
|
-
```python
|
|
22
|
-
from rtzip import RemoteZip, DataSource
|
|
23
|
-
|
|
24
|
-
import niquests # 如果你使用 niquests 作为 http 依赖的话
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class HTTPSource(DataSource):
|
|
28
|
-
def __init__(self, url, session=None):
|
|
29
|
-
self.url = url
|
|
30
|
-
self.session = session or niquests.AsyncSession
|
|
31
|
-
|
|
32
|
-
async def _request_range(self, offset, length):
|
|
33
|
-
resp = await self.session.get(self.url, headers={'Range': f"bytes={offset}-{offset + length - 1}"}, stream=True)
|
|
34
|
-
resp.raise_for_status()
|
|
35
|
-
if resp.status_code != 206:
|
|
36
|
-
await resp.close()
|
|
37
|
-
raise niquests.HTTPError(response=resp)
|
|
38
|
-
return resp
|
|
39
|
-
|
|
40
|
-
async def read_range(self, offset, length):
|
|
41
|
-
return await (await self._request_range(offset, length)).content
|
|
42
|
-
|
|
43
|
-
async def read_range_stream(self, offset, length):
|
|
44
|
-
async for i in await (await self._request_range(offset, length)).iter_content():
|
|
45
|
-
yield i
|
|
46
|
-
|
|
47
|
-
async def get_total_size(self):
|
|
48
|
-
resp = await self.session.head(self.url)
|
|
49
|
-
resp.raise_for_status()
|
|
50
|
-
return int(resp['content-length'])
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
async def main():
|
|
54
|
-
url = ...
|
|
55
|
-
rz = RemoteZip(HTTPSource(url))
|
|
56
|
-
rz.fetch_zip_info() # 加载文件的 EOCD,返回一个 EOCD 获取 Zip64EOCD 对象
|
|
57
|
-
rz.fetch_file_list() # 加载文件的中央目录,返回一个包含 CDEntry 对象的列表
|
|
58
|
-
# 这些数据如果没有即时接收,也会被缓存在 rz.eocd 和 rz.files 中
|
|
59
|
-
|
|
60
|
-
rz.build_mapping() # 构建文件名 -> Entry 对象的映射表,方便查表
|
|
61
|
-
# 返回映射表本身,同时缓存在 rz.file_mapping 中
|
|
62
|
-
|
|
63
|
-
# 一次性读取一个文件的全量数据
|
|
64
|
-
data = await rz.load_single_file(b'filename')
|
|
65
|
-
|
|
66
|
-
# 流式读取一个文件的数据
|
|
67
|
-
buffer = bytearray()
|
|
68
|
-
async for i in rz.stream_single_file(b'filename'):
|
|
69
|
-
buffer.extend(i)
|
|
70
|
-
|
|
71
|
-
# 获取一个文件的元数据信息
|
|
72
|
-
entry = rz.find_file_entry(b'filename')
|
|
73
|
-
# 可能抛出 FileNotFoundError
|
|
74
|
-
|
|
75
|
-
# 从文件名拉取一个文件对应的本地文件头
|
|
76
|
-
header = await rz.fetch_file_header(filename=b'filename')
|
|
77
|
-
|
|
78
|
-
# 也可以从已有的 entry 对象拉取
|
|
79
|
-
# 但这时你要自己保证这个 entry 对象是有效的
|
|
80
|
-
# 将返回一个 LocalFileHeader 对象
|
|
81
|
-
header = await rz.fetch_file_header(entry=entry)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if __name__ == '__main__':
|
|
85
|
-
import asyncio
|
|
86
|
-
|
|
87
|
-
asyncio.run(main())
|
|
88
|
-
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### 自定义压缩算法处理
|
|
92
|
-
|
|
93
|
-
```python
|
|
94
|
-
# 示例:扩展 PPMd 算法支持(需要 pip install ppmd)
|
|
95
|
-
import ppmd
|
|
96
|
-
from rtzip import algorithm_handler
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
@algorithm_handler(0x000A)
|
|
100
|
-
async def ppmd_wrapper(raw_generator):
|
|
101
|
-
# PPMd 流式解压(API 取决于具体库)
|
|
102
|
-
decomp = ppmd.Ppmd7Decompressor()
|
|
103
|
-
async for chunk in raw_generator:
|
|
104
|
-
yield decomp.decompress(chunk)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
# 这之后使用 RemoteZip 时遇到 ppmd 算法会自动应用你的包装器
|
|
108
|
-
|
|
109
|
-
# 如果想要重写某个内置的包装器
|
|
110
|
-
# 你需要指定 `override=True`,否则程序将会阻止覆盖行为
|
|
111
|
-
@algorithm_handler(0x0008, override=True)
|
|
112
|
-
async def your_deflate_wrapper(raw_generator):
|
|
113
|
-
...
|
|
114
|
-
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
> [!TIP]
|
|
118
|
-
> 本库无计划新功能,若是让自行添加功能,可克隆仓库并重写 `rtzip/remotezip.py` 下的代码
|
|
119
|
-
> 大多数库的功能已完成并可以直接投入使用
|
rtzip-0.3.1/pyproject.toml
DELETED
rtzip-0.3.1/setup.cfg
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
from collections.abc import Buffer
|
|
2
|
-
from typing import Callable, AsyncGenerator
|
|
3
|
-
|
|
4
|
-
AlgorithmHandler = Callable[[AsyncGenerator[Buffer, None]], AsyncGenerator[bytes | bytearray | memoryview, None]]
|
|
5
|
-
|
|
6
|
-
algorithm_mapping: dict[int, AlgorithmHandler] = {}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def algorithm_handler(algorithm: int, override=False):
|
|
10
|
-
"""
|
|
11
|
-
压缩流包装器注册装饰器
|
|
12
|
-
|
|
13
|
-
:param algorithm: 压缩算法 ID
|
|
14
|
-
:param override: 如果你希望覆盖一个已注册的包装器,请设置为 True,否则将会阻止可能的覆盖行为
|
|
15
|
-
:param decorator.func: 被包装的函数,接受一个异步生成器,返回一个异步生成器
|
|
16
|
-
:return:
|
|
17
|
-
"""
|
|
18
|
-
if not override and algorithm in algorithm_mapping:
|
|
19
|
-
raise ValueError(f"Algorithm {algorithm} is already registered")
|
|
20
|
-
|
|
21
|
-
def decorator(func: AlgorithmHandler):
|
|
22
|
-
algorithm_mapping[algorithm] = func
|
|
23
|
-
return func
|
|
24
|
-
|
|
25
|
-
return decorator
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def get_algorithm_handler(algorithm: int) -> AlgorithmHandler:
|
|
29
|
-
"""
|
|
30
|
-
获取包装器函数
|
|
31
|
-
|
|
32
|
-
:param algorithm: 压缩算法 ID
|
|
33
|
-
:return: AlgorithmHandler 可调用对象,接受一个异步生成器,返回一个异步生成器
|
|
34
|
-
"""
|
|
35
|
-
return algorithm_mapping[algorithm]
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def is_algorithm_available(algorithm: int) -> bool:
|
|
39
|
-
"""
|
|
40
|
-
判断某个压缩算法是否已有注册的包装器
|
|
41
|
-
|
|
42
|
-
:param algorithm: 压缩算法 ID
|
|
43
|
-
:return:
|
|
44
|
-
"""
|
|
45
|
-
return algorithm in algorithm_mapping
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
LICENSE
|
|
2
|
-
README.md
|
|
3
|
-
pyproject.toml
|
|
4
|
-
src/rtzip/__init__.py
|
|
5
|
-
src/rtzip/algorithm_register.py
|
|
6
|
-
src/rtzip/codec_wrappers.py
|
|
7
|
-
src/rtzip/const.py
|
|
8
|
-
src/rtzip/errors.py
|
|
9
|
-
src/rtzip/models.py
|
|
10
|
-
src/rtzip/remotezip.py
|
|
11
|
-
src/rtzip.egg-info/PKG-INFO
|
|
12
|
-
src/rtzip.egg-info/SOURCES.txt
|
|
13
|
-
src/rtzip.egg-info/dependency_links.txt
|
|
14
|
-
src/rtzip.egg-info/requires.txt
|
|
15
|
-
src/rtzip.egg-info/top_level.txt
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
obstruct==0.5.2
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
rtzip
|
|
File without changes
|