pybase16384 0.3.6__cp311-cp311-macosx_10_9_x86_64.whl → 0.3.8__cp311-cp311-macosx_10_9_x86_64.whl
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.
Potentially problematic release.
This version of pybase16384 might be problematic. Click here for more details.
- pybase16384/__init__.py +79 -3
- pybase16384/backends/cffi/__init__.py +200 -4
- pybase16384/backends/cffi/_core.abi3.so +0 -0
- pybase16384/backends/cffi/build.py +46 -6
- pybase16384/backends/cython/__init__.py +10 -1
- pybase16384/backends/cython/_core.abi3.so +0 -0
- pybase16384/backends/cython/_core.c +15978 -12446
- pybase16384/backends/cython/_core.pyx +241 -12
- pybase16384/backends/cython/base16384.pxd +21 -3
- {pybase16384-0.3.6.dist-info → pybase16384-0.3.8.dist-info}/METADATA +74 -11
- pybase16384-0.3.8.dist-info/RECORD +16 -0
- {pybase16384-0.3.6.dist-info → pybase16384-0.3.8.dist-info}/WHEEL +1 -1
- pybase16384-0.3.6.dist-info/RECORD +0 -16
- {pybase16384-0.3.6.dist-info → pybase16384-0.3.8.dist-info}/LICENSE +0 -0
- {pybase16384-0.3.6.dist-info → pybase16384-0.3.8.dist-info}/top_level.txt +0 -0
pybase16384/__init__.py
CHANGED
|
@@ -19,54 +19,70 @@ if not _should_use_cffi():
|
|
|
19
19
|
from pybase16384.backends.cython import (
|
|
20
20
|
DECBUFSZ,
|
|
21
21
|
ENCBUFSZ,
|
|
22
|
+
FLAG_DO_SUM_CHECK_FORCELY,
|
|
22
23
|
FLAG_NOHEADER,
|
|
23
24
|
FLAG_SUM_CHECK_ON_REMAIN,
|
|
24
|
-
SIMPLE_SUM_INIT_VALUE,
|
|
25
25
|
_decode,
|
|
26
26
|
_decode_into,
|
|
27
|
+
_decode_into_safe,
|
|
28
|
+
_decode_safe,
|
|
27
29
|
_encode,
|
|
28
30
|
_encode_into,
|
|
31
|
+
_encode_into_safe,
|
|
32
|
+
_encode_safe,
|
|
29
33
|
decode_fd,
|
|
30
34
|
decode_fd_detailed,
|
|
31
35
|
decode_file,
|
|
36
|
+
decode_file_safe,
|
|
32
37
|
decode_len,
|
|
33
38
|
decode_local_file,
|
|
34
39
|
decode_local_file_detailed,
|
|
40
|
+
decode_stream_detailed,
|
|
35
41
|
encode_fd,
|
|
36
42
|
encode_fd_detailed,
|
|
37
43
|
encode_file,
|
|
44
|
+
encode_file_safe,
|
|
38
45
|
encode_len,
|
|
39
46
|
encode_local_file,
|
|
40
47
|
encode_local_file_detailed,
|
|
48
|
+
encode_stream_detailed,
|
|
41
49
|
is_64bits,
|
|
42
50
|
)
|
|
43
51
|
else:
|
|
44
52
|
from pybase16384.backends.cffi import (
|
|
45
53
|
DECBUFSZ,
|
|
46
54
|
ENCBUFSZ,
|
|
55
|
+
FLAG_DO_SUM_CHECK_FORCELY,
|
|
47
56
|
FLAG_NOHEADER,
|
|
48
57
|
FLAG_SUM_CHECK_ON_REMAIN,
|
|
49
|
-
SIMPLE_SUM_INIT_VALUE,
|
|
50
58
|
_decode,
|
|
51
59
|
_decode_into,
|
|
60
|
+
_decode_into_safe,
|
|
61
|
+
_decode_safe,
|
|
52
62
|
_encode,
|
|
53
63
|
_encode_into,
|
|
64
|
+
_encode_into_safe,
|
|
65
|
+
_encode_safe,
|
|
54
66
|
decode_fd,
|
|
55
67
|
decode_fd_detailed,
|
|
56
68
|
decode_file,
|
|
69
|
+
decode_file_safe,
|
|
57
70
|
decode_len,
|
|
58
71
|
decode_local_file,
|
|
59
72
|
decode_local_file_detailed,
|
|
73
|
+
decode_stream_detailed,
|
|
60
74
|
encode_fd,
|
|
61
75
|
encode_fd_detailed,
|
|
62
76
|
encode_file,
|
|
77
|
+
encode_file_safe,
|
|
63
78
|
encode_len,
|
|
64
79
|
encode_local_file,
|
|
65
80
|
encode_local_file_detailed,
|
|
81
|
+
encode_stream_detailed,
|
|
66
82
|
is_64bits,
|
|
67
83
|
)
|
|
68
84
|
|
|
69
|
-
__version__ = "0.3.
|
|
85
|
+
__version__ = "0.3.8"
|
|
70
86
|
|
|
71
87
|
|
|
72
88
|
def encode(data: bytes) -> bytes:
|
|
@@ -76,6 +92,13 @@ def encode(data: bytes) -> bytes:
|
|
|
76
92
|
return out.getvalue()
|
|
77
93
|
|
|
78
94
|
|
|
95
|
+
def encode_safe(data: bytes) -> bytes:
|
|
96
|
+
inp = BytesIO(data)
|
|
97
|
+
out = BytesIO()
|
|
98
|
+
encode_file_safe(inp, out, False, len(data) // 7)
|
|
99
|
+
return out.getvalue()
|
|
100
|
+
|
|
101
|
+
|
|
79
102
|
def decode(data: bytes) -> bytes:
|
|
80
103
|
inp = BytesIO(data)
|
|
81
104
|
out = BytesIO()
|
|
@@ -83,6 +106,13 @@ def decode(data: bytes) -> bytes:
|
|
|
83
106
|
return out.getvalue()
|
|
84
107
|
|
|
85
108
|
|
|
109
|
+
def decode_safe(data: bytes) -> bytes:
|
|
110
|
+
inp = BytesIO(data)
|
|
111
|
+
out = BytesIO()
|
|
112
|
+
decode_file_safe(inp, out, len(data) // 8)
|
|
113
|
+
return out.getvalue()
|
|
114
|
+
|
|
115
|
+
|
|
86
116
|
def encode_from_string(data: str, write_head: bool = False) -> bytes:
|
|
87
117
|
bt = data.encode()
|
|
88
118
|
inp = BytesIO(bt)
|
|
@@ -91,6 +121,14 @@ def encode_from_string(data: str, write_head: bool = False) -> bytes:
|
|
|
91
121
|
return out.getvalue()
|
|
92
122
|
|
|
93
123
|
|
|
124
|
+
def encode_from_string_safe(data: str, write_head: bool = False) -> bytes:
|
|
125
|
+
bt = data.encode()
|
|
126
|
+
inp = BytesIO(bt)
|
|
127
|
+
out = BytesIO()
|
|
128
|
+
encode_file_safe(inp, out, write_head, len(bt) // 7)
|
|
129
|
+
return out.getvalue()
|
|
130
|
+
|
|
131
|
+
|
|
94
132
|
def encode_to_string(data: bytes) -> str:
|
|
95
133
|
inp = BytesIO(data)
|
|
96
134
|
out = BytesIO()
|
|
@@ -98,6 +136,13 @@ def encode_to_string(data: bytes) -> str:
|
|
|
98
136
|
return out.getvalue().decode("utf-16-be")
|
|
99
137
|
|
|
100
138
|
|
|
139
|
+
def encode_to_string_safe(data: bytes) -> str:
|
|
140
|
+
inp = BytesIO(data)
|
|
141
|
+
out = BytesIO()
|
|
142
|
+
encode_file_safe(inp, out, False, len(data) // 7)
|
|
143
|
+
return out.getvalue().decode("utf-16-be")
|
|
144
|
+
|
|
145
|
+
|
|
101
146
|
def encode_string(data: str) -> str:
|
|
102
147
|
data = data.encode()
|
|
103
148
|
inp = BytesIO(data)
|
|
@@ -106,6 +151,14 @@ def encode_string(data: str) -> str:
|
|
|
106
151
|
return out.getvalue().decode("utf-16-be")
|
|
107
152
|
|
|
108
153
|
|
|
154
|
+
def encode_string_safe(data: str) -> str:
|
|
155
|
+
data = data.encode()
|
|
156
|
+
inp = BytesIO(data)
|
|
157
|
+
out = BytesIO()
|
|
158
|
+
encode_file_safe(inp, out, False, len(data) // 7)
|
|
159
|
+
return out.getvalue().decode("utf-16-be")
|
|
160
|
+
|
|
161
|
+
|
|
109
162
|
def decode_from_bytes(data: bytes) -> str:
|
|
110
163
|
inp = BytesIO(data)
|
|
111
164
|
out = BytesIO()
|
|
@@ -113,6 +166,13 @@ def decode_from_bytes(data: bytes) -> str:
|
|
|
113
166
|
return out.getvalue().decode()
|
|
114
167
|
|
|
115
168
|
|
|
169
|
+
def decode_from_bytes_safe(data: bytes) -> str:
|
|
170
|
+
inp = BytesIO(data)
|
|
171
|
+
out = BytesIO()
|
|
172
|
+
decode_file_safe(inp, out, len(data) // 8)
|
|
173
|
+
return out.getvalue().decode()
|
|
174
|
+
|
|
175
|
+
|
|
116
176
|
def decode_from_string(data: str) -> bytes:
|
|
117
177
|
bt = data.encode("utf-16-be")
|
|
118
178
|
inp = BytesIO(bt)
|
|
@@ -121,9 +181,25 @@ def decode_from_string(data: str) -> bytes:
|
|
|
121
181
|
return out.getvalue()
|
|
122
182
|
|
|
123
183
|
|
|
184
|
+
def decode_from_string_safe(data: str) -> bytes:
|
|
185
|
+
bt = data.encode("utf-16-be")
|
|
186
|
+
inp = BytesIO(bt)
|
|
187
|
+
out = BytesIO()
|
|
188
|
+
decode_file_safe(inp, out, len(bt) // 8)
|
|
189
|
+
return out.getvalue()
|
|
190
|
+
|
|
191
|
+
|
|
124
192
|
def decode_string(data: str) -> str:
|
|
125
193
|
bt = data.encode("utf-16-be")
|
|
126
194
|
inp = BytesIO(bt)
|
|
127
195
|
out = BytesIO()
|
|
128
196
|
decode_file(inp, out, len(bt) // 8)
|
|
129
197
|
return out.getvalue().decode()
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def decode_string_safe(data: str) -> str:
|
|
201
|
+
bt = data.encode("utf-16-be")
|
|
202
|
+
inp = BytesIO(bt)
|
|
203
|
+
out = BytesIO()
|
|
204
|
+
decode_file_safe(inp, out, len(bt) // 8)
|
|
205
|
+
return out.getvalue().decode()
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Copyright (c) 2008-2021 synodriver <synodriver@gmail.com>
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from pathlib import Path
|
|
5
6
|
from typing import IO
|
|
6
7
|
|
|
@@ -15,7 +16,7 @@ ENCBUFSZ = lib.get_encsize()
|
|
|
15
16
|
DECBUFSZ = lib.get_decsize()
|
|
16
17
|
FLAG_NOHEADER = lib.BASE16384_FLAG_NOHEADER_()
|
|
17
18
|
FLAG_SUM_CHECK_ON_REMAIN = lib.BASE16384_FLAG_SUM_CHECK_ON_REMAIN_()
|
|
18
|
-
|
|
19
|
+
FLAG_DO_SUM_CHECK_FORCELY = lib.BASE16384_FLAG_DO_SUM_CHECK_FORCELY_()
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
# -----------------low level api------------------------------
|
|
@@ -29,10 +30,26 @@ def _encode(data: bytes) -> bytes:
|
|
|
29
30
|
return ffi.unpack(output_buf, count)
|
|
30
31
|
|
|
31
32
|
|
|
33
|
+
def _encode_safe(data: bytes) -> bytes:
|
|
34
|
+
length = len(data)
|
|
35
|
+
output_size = encode_len(length)
|
|
36
|
+
output_buf = ffi.new(f"char[{output_size}]")
|
|
37
|
+
if output_buf == ffi.NULL:
|
|
38
|
+
raise MemoryError
|
|
39
|
+
count = lib.base16384_encode_safe(ffi.from_buffer(data), length, output_buf)
|
|
40
|
+
return ffi.unpack(output_buf, count)
|
|
41
|
+
|
|
42
|
+
|
|
32
43
|
def _encode_into(data: bytes, out: bytearray) -> int:
|
|
33
44
|
return lib.base16384_encode(ffi.from_buffer(data), len(data), ffi.from_buffer(out))
|
|
34
45
|
|
|
35
46
|
|
|
47
|
+
def _encode_into_safe(data: bytes, out: bytearray) -> int:
|
|
48
|
+
return lib.base16384_encode_safe(
|
|
49
|
+
ffi.from_buffer(data), len(data), ffi.from_buffer(out)
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
36
53
|
def _decode(data: bytes) -> bytes:
|
|
37
54
|
length = len(data)
|
|
38
55
|
output_size = decode_len(length, 0) + 16
|
|
@@ -43,10 +60,26 @@ def _decode(data: bytes) -> bytes:
|
|
|
43
60
|
return ffi.unpack(output_buf, count)
|
|
44
61
|
|
|
45
62
|
|
|
63
|
+
def _decode_safe(data: bytes) -> bytes:
|
|
64
|
+
length = len(data)
|
|
65
|
+
output_size = decode_len(length, 0)
|
|
66
|
+
output_buf = ffi.new(f"char[{output_size}]")
|
|
67
|
+
if output_buf == ffi.NULL:
|
|
68
|
+
raise MemoryError
|
|
69
|
+
count = lib.base16384_decode_safe(ffi.from_buffer(data), length, output_buf)
|
|
70
|
+
return ffi.unpack(output_buf, count)
|
|
71
|
+
|
|
72
|
+
|
|
46
73
|
def _decode_into(data: bytes, out: bytearray) -> int:
|
|
47
74
|
return lib.base16384_decode(ffi.from_buffer(data), len(data), ffi.from_buffer(out))
|
|
48
75
|
|
|
49
76
|
|
|
77
|
+
def _decode_into_safe(data: bytes, out: bytearray) -> int:
|
|
78
|
+
return lib.base16384_decode_safe(
|
|
79
|
+
ffi.from_buffer(data), len(data), ffi.from_buffer(out)
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
50
83
|
def is_64bits() -> bool:
|
|
51
84
|
return bool(lib.pybase16384_64bits())
|
|
52
85
|
|
|
@@ -73,11 +106,15 @@ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int =
|
|
|
73
106
|
output.write(b"\xfe\xff")
|
|
74
107
|
|
|
75
108
|
current_buf_len: int = buf_rate * 7 # 一次读取这么多字节
|
|
76
|
-
output_size: int =
|
|
109
|
+
output_size: int = (
|
|
110
|
+
encode_len(current_buf_len) + 16
|
|
111
|
+
) # 因为encode_len不是单调的 这16备用
|
|
77
112
|
output_buf = ffi.new(f"char[{output_size}]")
|
|
78
113
|
if output_buf == ffi.NULL:
|
|
79
114
|
raise MemoryError
|
|
80
|
-
first_check: int =
|
|
115
|
+
first_check: int = (
|
|
116
|
+
1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
|
|
117
|
+
)
|
|
81
118
|
while True:
|
|
82
119
|
chunk = input.read(current_buf_len)
|
|
83
120
|
if first_check:
|
|
@@ -88,7 +125,9 @@ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int =
|
|
|
88
125
|
)
|
|
89
126
|
size = len(chunk)
|
|
90
127
|
if size < current_buf_len: # 数据不够了 要减小一次读取的量
|
|
91
|
-
if
|
|
128
|
+
if (
|
|
129
|
+
buf_rate > 1
|
|
130
|
+
): # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
|
|
92
131
|
buf_rate = buf_rate // 2
|
|
93
132
|
current_buf_len = buf_rate * 7
|
|
94
133
|
input.seek(-size, 1)
|
|
@@ -100,6 +139,56 @@ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int =
|
|
|
100
139
|
break
|
|
101
140
|
|
|
102
141
|
|
|
142
|
+
def encode_file_safe(
|
|
143
|
+
input: IO, output: IO, write_head: bool = False, buf_rate: int = 10
|
|
144
|
+
):
|
|
145
|
+
if not _check_file(input):
|
|
146
|
+
raise TypeError(
|
|
147
|
+
"input except a file-like object, got %s" % type(input).__name__
|
|
148
|
+
)
|
|
149
|
+
if not _check_file(output):
|
|
150
|
+
raise TypeError(
|
|
151
|
+
"output except a file-like object, got %s" % type(input).__name__
|
|
152
|
+
)
|
|
153
|
+
if buf_rate <= 0:
|
|
154
|
+
buf_rate = 1
|
|
155
|
+
if write_head:
|
|
156
|
+
output.write(b"\xfe\xff")
|
|
157
|
+
|
|
158
|
+
current_buf_len: int = buf_rate * 7 # 一次读取这么多字节
|
|
159
|
+
output_size: int = encode_len(
|
|
160
|
+
current_buf_len
|
|
161
|
+
) # 因为encode_len不是单调的 safe不用加16
|
|
162
|
+
output_buf = ffi.new(f"char[{output_size}]")
|
|
163
|
+
if output_buf == ffi.NULL:
|
|
164
|
+
raise MemoryError
|
|
165
|
+
first_check: int = (
|
|
166
|
+
1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
|
|
167
|
+
)
|
|
168
|
+
while True:
|
|
169
|
+
chunk = input.read(current_buf_len)
|
|
170
|
+
if first_check:
|
|
171
|
+
first_check = 0
|
|
172
|
+
if not isinstance(chunk, bytes):
|
|
173
|
+
raise TypeError(
|
|
174
|
+
f"input must be a file-like rb object, got {type(input).__name__}"
|
|
175
|
+
)
|
|
176
|
+
size = len(chunk)
|
|
177
|
+
if size < current_buf_len: # 数据不够了 要减小一次读取的量
|
|
178
|
+
if (
|
|
179
|
+
buf_rate > 1
|
|
180
|
+
): # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
|
|
181
|
+
buf_rate = buf_rate // 2
|
|
182
|
+
current_buf_len = buf_rate * 7
|
|
183
|
+
input.seek(-size, 1)
|
|
184
|
+
continue
|
|
185
|
+
|
|
186
|
+
count = lib.base16384_encode_safe(ffi.from_buffer(chunk), size, output_buf)
|
|
187
|
+
output.write(ffi.unpack(output_buf, count))
|
|
188
|
+
if size < 7:
|
|
189
|
+
break
|
|
190
|
+
|
|
191
|
+
|
|
103
192
|
def decode_file(input: IO, output: IO, buf_rate: int = 10):
|
|
104
193
|
if not _check_file(input):
|
|
105
194
|
raise TypeError(
|
|
@@ -150,6 +239,56 @@ def decode_file(input: IO, output: IO, buf_rate: int = 10):
|
|
|
150
239
|
output.write(ffi.unpack(output_buf, count))
|
|
151
240
|
|
|
152
241
|
|
|
242
|
+
def decode_file_safe(input: IO, output: IO, buf_rate: int = 10):
|
|
243
|
+
if not _check_file(input):
|
|
244
|
+
raise TypeError(
|
|
245
|
+
"input except a file-like object, got %s" % type(input).__name__
|
|
246
|
+
)
|
|
247
|
+
if not _check_file(output):
|
|
248
|
+
raise TypeError(
|
|
249
|
+
"output except a file-like object, got %s" % type(output).__name__
|
|
250
|
+
)
|
|
251
|
+
if buf_rate <= 0:
|
|
252
|
+
buf_rate = 1
|
|
253
|
+
|
|
254
|
+
chunk = input.read(1) # type: bytes
|
|
255
|
+
if not isinstance(chunk, bytes):
|
|
256
|
+
raise TypeError(
|
|
257
|
+
f"input must be a file-like rb object, got {type(input).__name__}"
|
|
258
|
+
)
|
|
259
|
+
if chunk == b"\xfe": # 去头
|
|
260
|
+
input.read(1)
|
|
261
|
+
else:
|
|
262
|
+
input.seek(0, 0) # 没有头 回到开头
|
|
263
|
+
|
|
264
|
+
current_buf_len: int = buf_rate * 8
|
|
265
|
+
output_size: int = decode_len(current_buf_len, 0)
|
|
266
|
+
output_buf = ffi.new(f"char[{output_size}]")
|
|
267
|
+
if output_buf == ffi.NULL:
|
|
268
|
+
raise MemoryError
|
|
269
|
+
while True:
|
|
270
|
+
chunk = input.read(current_buf_len) # 8的倍数
|
|
271
|
+
size = len(chunk)
|
|
272
|
+
if size == 0:
|
|
273
|
+
break
|
|
274
|
+
if size < current_buf_len: # 长度不够了
|
|
275
|
+
if buf_rate > 1: # 还能继续变小
|
|
276
|
+
buf_rate = buf_rate // 2 # 重新设置一次读取的大小
|
|
277
|
+
current_buf_len = buf_rate * 8
|
|
278
|
+
input.seek(-size, 1)
|
|
279
|
+
continue
|
|
280
|
+
tmp = input.read(2) # type: bytes
|
|
281
|
+
if len(tmp) == 2:
|
|
282
|
+
if tmp[0] == 61: # = stream完了 一次解码8n+2个字节
|
|
283
|
+
chunk += tmp
|
|
284
|
+
size += 2
|
|
285
|
+
else:
|
|
286
|
+
input.seek(-2, 1)
|
|
287
|
+
|
|
288
|
+
count = lib.base16384_decode_safe(ffi.from_buffer(chunk), size, output_buf)
|
|
289
|
+
output.write(ffi.unpack(output_buf, count))
|
|
290
|
+
|
|
291
|
+
|
|
153
292
|
def ensure_bytes(inp) -> bytes:
|
|
154
293
|
if isinstance(inp, str):
|
|
155
294
|
return inp.encode()
|
|
@@ -265,3 +404,60 @@ def decode_fd_detailed(inp: int, out: int, flag: int) -> None:
|
|
|
265
404
|
ret = lib.base16384_decode_fd_detailed(inp, out, encbuf, decbuf, flag)
|
|
266
405
|
if ret != lib.base16384_err_ok:
|
|
267
406
|
raise ValueError(err_to_str(ret))
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
# stream
|
|
410
|
+
@ffi.def_extern()
|
|
411
|
+
def b14_readcallback(client_data, buffer, count: int):
|
|
412
|
+
file = ffi.from_handle(client_data)
|
|
413
|
+
data: bytes = file.read(count)
|
|
414
|
+
data_size: int = len(data)
|
|
415
|
+
ffi.memmove(buffer, data, data_size)
|
|
416
|
+
return data_size
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
@ffi.def_extern()
|
|
420
|
+
def b14_writecallback(client_data, buffer, count: int):
|
|
421
|
+
file = ffi.from_handle(client_data)
|
|
422
|
+
data: bytes = ffi.unpack(ffi.cast("char*", buffer), count)
|
|
423
|
+
return file.write(data)
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
def encode_stream_detailed(inp, out, flag: int):
|
|
427
|
+
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
|
|
428
|
+
decbuf = ffi.new(f"char[{DECBUFSZ}]")
|
|
429
|
+
|
|
430
|
+
inpstream = ffi.new(f"base16384_stream_t *")
|
|
431
|
+
inpstream.f.reader = lib.b14_readcallback
|
|
432
|
+
handle_inp = ffi.new_handle(inp)
|
|
433
|
+
inpstream.client_data = handle_inp
|
|
434
|
+
|
|
435
|
+
outstream = ffi.new(f"base16384_stream_t *")
|
|
436
|
+
outstream.f.writer = lib.b14_writecallback
|
|
437
|
+
handle_out = ffi.new_handle(out)
|
|
438
|
+
outstream.client_data = handle_out
|
|
439
|
+
ret = lib.base16384_encode_stream_detailed(
|
|
440
|
+
inpstream, outstream, encbuf, decbuf, flag
|
|
441
|
+
)
|
|
442
|
+
if ret != lib.base16384_err_ok:
|
|
443
|
+
raise ValueError(err_to_str(ret))
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def decode_stream_detailed(inp, out, flag: int):
|
|
447
|
+
encbuf = ffi.new(f"char[{ENCBUFSZ}]")
|
|
448
|
+
decbuf = ffi.new(f"char[{DECBUFSZ}]")
|
|
449
|
+
|
|
450
|
+
inpstream = ffi.new(f"base16384_stream_t *")
|
|
451
|
+
inpstream.f.reader = lib.b14_readcallback
|
|
452
|
+
handle_inp = ffi.new_handle(inp)
|
|
453
|
+
inpstream.client_data = handle_inp
|
|
454
|
+
|
|
455
|
+
outstream = ffi.new(f"base16384_stream_t *")
|
|
456
|
+
outstream.f.writer = lib.b14_writecallback
|
|
457
|
+
handle_out = ffi.new_handle(out)
|
|
458
|
+
outstream.client_data = handle_out
|
|
459
|
+
ret = lib.base16384_decode_stream_detailed(
|
|
460
|
+
inpstream, outstream, encbuf, decbuf, flag
|
|
461
|
+
)
|
|
462
|
+
if ret != lib.base16384_err_ok:
|
|
463
|
+
raise ValueError(err_to_str(ret))
|
|
Binary file
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Copyright (c) 2008-2021 synodriver <synodriver@gmail.com>
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
import platform
|
|
5
6
|
import sys
|
|
6
7
|
|
|
@@ -74,6 +75,8 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d
|
|
|
74
75
|
|
|
75
76
|
int base16384_encode_unsafe(const char * data, int dlen, char * buf);
|
|
76
77
|
int base16384_decode_unsafe(const char * data, int dlen, char * buf);
|
|
78
|
+
int base16384_encode_safe(const char * data, int dlen, char * buf);
|
|
79
|
+
int base16384_decode_safe(const char * data, int dlen, char * buf);
|
|
77
80
|
|
|
78
81
|
base16384_err_t base16384_encode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag);
|
|
79
82
|
base16384_err_t base16384_decode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag);
|
|
@@ -92,7 +95,40 @@ int BASE16384_FLAG_NOHEADER_();
|
|
|
92
95
|
|
|
93
96
|
int BASE16384_FLAG_SUM_CHECK_ON_REMAIN_();
|
|
94
97
|
|
|
95
|
-
int
|
|
98
|
+
int BASE16384_FLAG_DO_SUM_CHECK_FORCELY_();
|
|
99
|
+
|
|
100
|
+
typedef ssize_t (*base16384_reader_t)(const void *client_data, void *buffer, size_t count);
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @brief custom writer function interface
|
|
104
|
+
* @param client_data the data pointer defined by the client
|
|
105
|
+
* @param buffer from where read data
|
|
106
|
+
* @param count write bytes count
|
|
107
|
+
* @return the size written
|
|
108
|
+
*/
|
|
109
|
+
typedef ssize_t (*base16384_writer_t)(const void *client_data, const void *buffer, size_t count);
|
|
110
|
+
|
|
111
|
+
union base16384_io_function_t {
|
|
112
|
+
base16384_reader_t reader;
|
|
113
|
+
base16384_writer_t writer;
|
|
114
|
+
};
|
|
115
|
+
typedef union base16384_io_function_t base16384_io_function_t;
|
|
116
|
+
|
|
117
|
+
struct base16384_stream_t {
|
|
118
|
+
base16384_io_function_t f;
|
|
119
|
+
void *client_data;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* @brief for stream encode/decode
|
|
123
|
+
*/
|
|
124
|
+
typedef struct base16384_stream_t base16384_stream_t;
|
|
125
|
+
|
|
126
|
+
base16384_err_t base16384_encode_stream_detailed(base16384_stream_t* input, base16384_stream_t* output, char* encbuf, char* decbuf, int flag);
|
|
127
|
+
base16384_err_t base16384_decode_stream_detailed(base16384_stream_t* input, base16384_stream_t* output, char* encbuf, char* decbuf, int flag);
|
|
128
|
+
|
|
129
|
+
extern "Python" ssize_t b14_readcallback(const void *client_data, void *buffer, size_t count);
|
|
130
|
+
|
|
131
|
+
extern "Python" ssize_t b14_writecallback(const void *client_data, const void *buffer, size_t count);
|
|
96
132
|
"""
|
|
97
133
|
)
|
|
98
134
|
|
|
@@ -100,9 +136,9 @@ source = """
|
|
|
100
136
|
#include "base16384.h"
|
|
101
137
|
|
|
102
138
|
#ifdef CPUBIT32
|
|
103
|
-
#define pybase16384_64bits() 0
|
|
139
|
+
#define pybase16384_64bits() (0)
|
|
104
140
|
#else
|
|
105
|
-
#define pybase16384_64bits() 1
|
|
141
|
+
#define pybase16384_64bits() (1)
|
|
106
142
|
#endif
|
|
107
143
|
|
|
108
144
|
int get_encsize()
|
|
@@ -125,16 +161,20 @@ int BASE16384_FLAG_SUM_CHECK_ON_REMAIN_()
|
|
|
125
161
|
return BASE16384_FLAG_SUM_CHECK_ON_REMAIN;
|
|
126
162
|
}
|
|
127
163
|
|
|
128
|
-
int
|
|
164
|
+
int BASE16384_FLAG_DO_SUM_CHECK_FORCELY_()
|
|
129
165
|
{
|
|
130
|
-
return
|
|
166
|
+
return BASE16384_FLAG_DO_SUM_CHECK_FORCELY;
|
|
131
167
|
}
|
|
132
168
|
"""
|
|
133
169
|
|
|
134
170
|
ffibuilder.set_source(
|
|
135
171
|
"pybase16384.backends.cffi._core",
|
|
136
172
|
source,
|
|
137
|
-
sources=[
|
|
173
|
+
sources=[
|
|
174
|
+
f"./base16384/base14{CPUBIT}.c",
|
|
175
|
+
"./base16384/file.c",
|
|
176
|
+
"./base16384/wrap.c",
|
|
177
|
+
],
|
|
138
178
|
include_dirs=["./base16384"],
|
|
139
179
|
define_macros=macro_base,
|
|
140
180
|
)
|
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Copyright (c) 2008-2021 synodriver <synodriver@gmail.com>
|
|
3
3
|
"""
|
|
4
|
+
|
|
4
5
|
from pybase16384.backends.cython._core import (
|
|
5
6
|
DECBUFSZ,
|
|
6
7
|
ENCBUFSZ,
|
|
8
|
+
FLAG_DO_SUM_CHECK_FORCELY,
|
|
7
9
|
FLAG_NOHEADER,
|
|
8
10
|
FLAG_SUM_CHECK_ON_REMAIN,
|
|
9
|
-
SIMPLE_SUM_INIT_VALUE,
|
|
10
11
|
_decode,
|
|
11
12
|
_decode_into,
|
|
13
|
+
_decode_into_safe,
|
|
14
|
+
_decode_safe,
|
|
12
15
|
_encode,
|
|
13
16
|
_encode_into,
|
|
17
|
+
_encode_into_safe,
|
|
18
|
+
_encode_safe,
|
|
14
19
|
decode_fd,
|
|
15
20
|
decode_fd_detailed,
|
|
16
21
|
decode_file,
|
|
22
|
+
decode_file_safe,
|
|
17
23
|
decode_len,
|
|
18
24
|
decode_local_file,
|
|
19
25
|
decode_local_file_detailed,
|
|
26
|
+
decode_stream_detailed,
|
|
20
27
|
encode_fd,
|
|
21
28
|
encode_fd_detailed,
|
|
22
29
|
encode_file,
|
|
30
|
+
encode_file_safe,
|
|
23
31
|
encode_len,
|
|
24
32
|
encode_local_file,
|
|
25
33
|
encode_local_file_detailed,
|
|
34
|
+
encode_stream_detailed,
|
|
26
35
|
is_64bits,
|
|
27
36
|
)
|
|
Binary file
|