pybase16384 0.3.9__cp312-cp312-musllinux_1_2_aarch64.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.
- pybase16384/__init__.py +205 -0
- pybase16384/backends/__init__.py +1 -0
- pybase16384/backends/cffi/__init__.py +463 -0
- pybase16384/backends/cffi/_core.abi3.so +0 -0
- pybase16384/backends/cffi/build.py +183 -0
- pybase16384/backends/cython/__init__.py +36 -0
- pybase16384/backends/cython/_core.abi3.so +0 -0
- pybase16384/backends/cython/_core.c +39928 -0
- pybase16384/backends/cython/_core.pxi +12 -0
- pybase16384/backends/cython/_core.pyx +619 -0
- pybase16384/backends/cython/base16384.pxd +113 -0
- pybase16384-0.3.9.dist-info/METADATA +216 -0
- pybase16384-0.3.9.dist-info/RECORD +16 -0
- pybase16384-0.3.9.dist-info/WHEEL +5 -0
- pybase16384-0.3.9.dist-info/licenses/LICENSE +674 -0
- pybase16384-0.3.9.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from typing import BinaryIO
|
|
2
|
+
|
|
3
|
+
def decode_len(dlen: int, offset: int) -> int: ...
|
|
4
|
+
def _decode(data: bytes) -> bytes: ...
|
|
5
|
+
def _decode_into(data: memoryview, dest: memoryview) -> int: ...
|
|
6
|
+
def decode_file(input: BinaryIO, output: BinaryIO, buf_rate: int = 10) -> None: ...
|
|
7
|
+
def encode_len(dlen: int) -> int: ...
|
|
8
|
+
def _encode(data: bytes) -> bytes: ...
|
|
9
|
+
def _encode_into(data: memoryview, dest: memoryview) -> int: ...
|
|
10
|
+
def encode_file(
|
|
11
|
+
input: BinaryIO, output: BinaryIO, boolwrite_head: bool = False, buf_rate: int = 10
|
|
12
|
+
) -> None: ...
|
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
# cython: language_level=3
|
|
2
|
+
# cython: cdivision=True
|
|
3
|
+
from cpython.bytes cimport (PyBytes_AS_STRING, PyBytes_Check,
|
|
4
|
+
PyBytes_FromStringAndSize, PyBytes_GET_SIZE)
|
|
5
|
+
from cpython.mem cimport PyMem_Free, PyMem_Malloc
|
|
6
|
+
from cpython.object cimport PyObject, PyObject_HasAttrString
|
|
7
|
+
from libc.stdint cimport int32_t, uint8_t
|
|
8
|
+
from libc.string cimport memcpy
|
|
9
|
+
|
|
10
|
+
from pybase16384.backends.cython.base16384 cimport (
|
|
11
|
+
BASE16384_DECBUFSZ, BASE16384_ENCBUFSZ,
|
|
12
|
+
BASE16384_FLAG_DO_SUM_CHECK_FORCELY, BASE16384_FLAG_NOHEADER,
|
|
13
|
+
BASE16384_FLAG_SUM_CHECK_ON_REMAIN, b14_decode, b14_decode_fd,
|
|
14
|
+
b14_decode_fd_detailed, b14_decode_file, b14_decode_file_detailed,
|
|
15
|
+
b14_decode_len, b14_decode_safe, b14_decode_stream,
|
|
16
|
+
b14_decode_stream_detailed, b14_encode, b14_encode_fd,
|
|
17
|
+
b14_encode_fd_detailed, b14_encode_file, b14_encode_file_detailed,
|
|
18
|
+
b14_encode_len, b14_encode_safe, b14_encode_stream,
|
|
19
|
+
b14_encode_stream_detailed, base16384_err_fopen_input_file,
|
|
20
|
+
base16384_err_fopen_output_file, base16384_err_get_file_size,
|
|
21
|
+
base16384_err_invalid_commandline_parameter,
|
|
22
|
+
base16384_err_invalid_decoding_checksum, base16384_err_invalid_file_name,
|
|
23
|
+
base16384_err_map_input_file, base16384_err_ok,
|
|
24
|
+
base16384_err_open_input_file, base16384_err_read_file, base16384_err_t,
|
|
25
|
+
base16384_err_write_file, base16384_io_function_t, base16384_stream_t,
|
|
26
|
+
pybase16384_64bits)
|
|
27
|
+
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
|
|
30
|
+
ENCBUFSZ = BASE16384_ENCBUFSZ
|
|
31
|
+
DECBUFSZ = BASE16384_DECBUFSZ
|
|
32
|
+
FLAG_NOHEADER = BASE16384_FLAG_NOHEADER
|
|
33
|
+
FLAG_SUM_CHECK_ON_REMAIN = BASE16384_FLAG_SUM_CHECK_ON_REMAIN
|
|
34
|
+
FLAG_DO_SUM_CHECK_FORCELY = BASE16384_FLAG_DO_SUM_CHECK_FORCELY
|
|
35
|
+
|
|
36
|
+
cdef inline bytes ensure_bytes(object inp):
|
|
37
|
+
if isinstance(inp, unicode):
|
|
38
|
+
return inp.encode()
|
|
39
|
+
elif isinstance(inp, bytes):
|
|
40
|
+
return inp
|
|
41
|
+
elif isinstance(inp, Path):
|
|
42
|
+
return str(inp).encode()
|
|
43
|
+
else:
|
|
44
|
+
return bytes(inp)
|
|
45
|
+
|
|
46
|
+
cdef inline uint8_t PyFile_Check(object file):
|
|
47
|
+
if PyObject_HasAttrString(file, "read") and PyObject_HasAttrString(file, "write") and PyObject_HasAttrString(file,
|
|
48
|
+
"seek"):
|
|
49
|
+
return 1
|
|
50
|
+
return 0
|
|
51
|
+
|
|
52
|
+
cpdef inline int encode_len(int dlen) nogil:
|
|
53
|
+
return b14_encode_len(dlen)
|
|
54
|
+
|
|
55
|
+
cpdef inline int decode_len(int dlen, int offset) nogil:
|
|
56
|
+
return b14_decode_len(dlen, offset)
|
|
57
|
+
|
|
58
|
+
cpdef inline bytes _encode(const uint8_t[::1] data):
|
|
59
|
+
cdef size_t length = data.shape[0]
|
|
60
|
+
cdef size_t output_size = <size_t> b14_encode_len(<int>length) + 16
|
|
61
|
+
cdef char *output_buf = <char*>PyMem_Malloc(output_size)
|
|
62
|
+
if output_buf == NULL:
|
|
63
|
+
raise MemoryError
|
|
64
|
+
cdef int count
|
|
65
|
+
with nogil:
|
|
66
|
+
count = b14_encode(<const char*> &data[0],
|
|
67
|
+
<int>length,
|
|
68
|
+
output_buf) # encode 整数倍的那个
|
|
69
|
+
try:
|
|
70
|
+
return <bytes>output_buf[:count]
|
|
71
|
+
finally:
|
|
72
|
+
PyMem_Free(output_buf)
|
|
73
|
+
|
|
74
|
+
cpdef inline bytes _encode_safe(const uint8_t[::1] data):
|
|
75
|
+
cdef size_t length = data.shape[0]
|
|
76
|
+
cdef size_t output_size = <size_t> b14_encode_len(<int>length)
|
|
77
|
+
cdef char *output_buf = <char*>PyMem_Malloc(output_size)
|
|
78
|
+
if output_buf == NULL:
|
|
79
|
+
raise MemoryError
|
|
80
|
+
cdef int count
|
|
81
|
+
with nogil:
|
|
82
|
+
count = b14_encode_safe(<const char*> &data[0],
|
|
83
|
+
<int>length,
|
|
84
|
+
output_buf) # encode 整数倍的那个
|
|
85
|
+
try:
|
|
86
|
+
return <bytes>output_buf[:count]
|
|
87
|
+
finally:
|
|
88
|
+
PyMem_Free(output_buf)
|
|
89
|
+
|
|
90
|
+
cpdef inline bytes _decode(const uint8_t[::1] data):
|
|
91
|
+
cdef size_t length = data.shape[0]
|
|
92
|
+
cdef size_t output_size = <size_t> b14_decode_len(<int>length, 0) + 16
|
|
93
|
+
cdef char *output_buf = <char *> PyMem_Malloc(output_size)
|
|
94
|
+
if output_buf == NULL:
|
|
95
|
+
raise MemoryError
|
|
96
|
+
cdef int count
|
|
97
|
+
with nogil:
|
|
98
|
+
count = b14_decode(<const char *> &data[0],
|
|
99
|
+
<int> length,
|
|
100
|
+
output_buf) # decode
|
|
101
|
+
try:
|
|
102
|
+
return <bytes> output_buf[:count]
|
|
103
|
+
finally:
|
|
104
|
+
PyMem_Free(output_buf)
|
|
105
|
+
|
|
106
|
+
cpdef inline bytes _decode_safe(const uint8_t[::1] data):
|
|
107
|
+
cdef size_t length = data.shape[0]
|
|
108
|
+
cdef size_t output_size = <size_t> b14_decode_len(<int>length, 0)
|
|
109
|
+
cdef char *output_buf = <char *> PyMem_Malloc(output_size)
|
|
110
|
+
if output_buf == NULL:
|
|
111
|
+
raise MemoryError
|
|
112
|
+
cdef int count
|
|
113
|
+
with nogil:
|
|
114
|
+
count = b14_decode_safe(<const char *> &data[0],
|
|
115
|
+
<int> length,
|
|
116
|
+
output_buf) # decode
|
|
117
|
+
try:
|
|
118
|
+
return <bytes> output_buf[:count]
|
|
119
|
+
finally:
|
|
120
|
+
PyMem_Free(output_buf)
|
|
121
|
+
|
|
122
|
+
cpdef inline int _encode_into(const uint8_t[::1] data, uint8_t[::1] dest) except -1:
|
|
123
|
+
cdef size_t input_size = data.shape[0]
|
|
124
|
+
cdef size_t output_size = <size_t> b14_encode_len(<int> input_size)
|
|
125
|
+
cdef size_t output_buf_size = dest.shape[0]
|
|
126
|
+
if output_buf_size < output_size:
|
|
127
|
+
raise ValueError("Buffer is too small to hold result")
|
|
128
|
+
with nogil:
|
|
129
|
+
return b14_encode(<const char *> &data[0],
|
|
130
|
+
<int> input_size,
|
|
131
|
+
<char *> &dest[0])
|
|
132
|
+
|
|
133
|
+
cpdef inline int _encode_into_safe(const uint8_t[::1] data, uint8_t[::1] dest) except -1:
|
|
134
|
+
cdef size_t input_size = data.shape[0]
|
|
135
|
+
cdef size_t output_size = <size_t> b14_encode_len(<int> input_size)
|
|
136
|
+
cdef size_t output_buf_size = dest.shape[0]
|
|
137
|
+
if output_buf_size < output_size:
|
|
138
|
+
raise ValueError("Buffer is too small to hold result")
|
|
139
|
+
with nogil:
|
|
140
|
+
return b14_encode_safe(<const char *> &data[0],
|
|
141
|
+
<int> input_size,
|
|
142
|
+
<char *> &dest[0])
|
|
143
|
+
|
|
144
|
+
cpdef inline int _decode_into(const uint8_t[::1] data, uint8_t[::1] dest) except -1:
|
|
145
|
+
cdef size_t input_size = data.shape[0]
|
|
146
|
+
cdef size_t output_size = <size_t> b14_decode_len(<int> input_size, 0)
|
|
147
|
+
cdef size_t output_buf_size = dest.shape[0]
|
|
148
|
+
if output_buf_size < output_size:
|
|
149
|
+
raise ValueError("Buffer is too small to hold result")
|
|
150
|
+
with nogil:
|
|
151
|
+
return b14_decode(<const char *> &data[0],
|
|
152
|
+
<int> input_size,
|
|
153
|
+
<char *> &dest[0])
|
|
154
|
+
|
|
155
|
+
cpdef inline int _decode_into_safe(const uint8_t[::1] data, uint8_t[::1] dest) except -1:
|
|
156
|
+
cdef size_t input_size = data.shape[0]
|
|
157
|
+
cdef size_t output_size = <size_t> b14_decode_len(<int> input_size, 0)
|
|
158
|
+
cdef size_t output_buf_size = dest.shape[0]
|
|
159
|
+
if output_buf_size < output_size:
|
|
160
|
+
raise ValueError("Buffer is too small to hold result")
|
|
161
|
+
with nogil:
|
|
162
|
+
return b14_decode_safe(<const char *> &data[0],
|
|
163
|
+
<int> input_size,
|
|
164
|
+
<char *> &dest[0])
|
|
165
|
+
|
|
166
|
+
def encode_file(object input,
|
|
167
|
+
object output,
|
|
168
|
+
bint write_head = False,
|
|
169
|
+
int32_t buf_rate = 10):
|
|
170
|
+
if not PyFile_Check(input):
|
|
171
|
+
raise TypeError("input except a file-like object, got %s" % type(input).__name__)
|
|
172
|
+
if not PyFile_Check(output):
|
|
173
|
+
raise TypeError("output except a file-like object, got %s" % type(output).__name__)
|
|
174
|
+
if buf_rate <= 0:
|
|
175
|
+
buf_rate = 1
|
|
176
|
+
|
|
177
|
+
if write_head:
|
|
178
|
+
output.write(b'\xfe\xff')
|
|
179
|
+
|
|
180
|
+
cdef int32_t current_buf_len = buf_rate * 7 # 一次读取这么多字节
|
|
181
|
+
cdef size_t output_size = <size_t> b14_encode_len(<int> current_buf_len) + 16 # 因为encode_len不是单调的 这16备用
|
|
182
|
+
cdef char *output_buf = <char *> PyMem_Malloc(output_size)
|
|
183
|
+
if output_buf == NULL:
|
|
184
|
+
raise MemoryError
|
|
185
|
+
|
|
186
|
+
cdef Py_ssize_t size
|
|
187
|
+
cdef uint8_t first_check = 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
|
|
188
|
+
cdef int count = 0
|
|
189
|
+
cdef const char *chunk_ptr
|
|
190
|
+
try:
|
|
191
|
+
while True:
|
|
192
|
+
chunk = input.read(current_buf_len)
|
|
193
|
+
if first_check:
|
|
194
|
+
first_check = 0
|
|
195
|
+
if not PyBytes_Check(chunk):
|
|
196
|
+
raise TypeError(f"input must be a file-like rb object, got {type(input).__name__}")
|
|
197
|
+
size = PyBytes_GET_SIZE(chunk)
|
|
198
|
+
if <int32_t> size < current_buf_len: # 数据不够了 要减小一次读取的量
|
|
199
|
+
if buf_rate > 1: # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
|
|
200
|
+
buf_rate = buf_rate / 2
|
|
201
|
+
current_buf_len = buf_rate * 7
|
|
202
|
+
input.seek(-size, 1)
|
|
203
|
+
continue
|
|
204
|
+
chunk_ptr = <const char*>PyBytes_AS_STRING(chunk)
|
|
205
|
+
with nogil:
|
|
206
|
+
count = b14_encode(chunk_ptr, <int>size, output_buf)
|
|
207
|
+
output.write(<bytes>output_buf[:count])
|
|
208
|
+
if size < 7:
|
|
209
|
+
break
|
|
210
|
+
finally:
|
|
211
|
+
PyMem_Free(output_buf)
|
|
212
|
+
|
|
213
|
+
def encode_file_safe(object input,
|
|
214
|
+
object output,
|
|
215
|
+
bint write_head = False,
|
|
216
|
+
int32_t buf_rate = 10):
|
|
217
|
+
if not PyFile_Check(input):
|
|
218
|
+
raise TypeError("input except a file-like object, got %s" % type(input).__name__)
|
|
219
|
+
if not PyFile_Check(output):
|
|
220
|
+
raise TypeError("output except a file-like object, got %s" % type(output).__name__)
|
|
221
|
+
if buf_rate <= 0:
|
|
222
|
+
buf_rate = 1
|
|
223
|
+
|
|
224
|
+
if write_head:
|
|
225
|
+
output.write(b'\xfe\xff')
|
|
226
|
+
|
|
227
|
+
cdef int32_t current_buf_len = buf_rate * 7 # 一次读取这么多字节
|
|
228
|
+
cdef size_t output_size = <size_t> b14_encode_len(<int> current_buf_len) # 因为encode_len不是单调的 这16备用
|
|
229
|
+
cdef char *output_buf = <char *> PyMem_Malloc(output_size)
|
|
230
|
+
if output_buf == NULL:
|
|
231
|
+
raise MemoryError
|
|
232
|
+
|
|
233
|
+
cdef Py_ssize_t size
|
|
234
|
+
cdef uint8_t first_check = 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
|
|
235
|
+
cdef int count = 0
|
|
236
|
+
cdef const char *chunk_ptr
|
|
237
|
+
try:
|
|
238
|
+
while True:
|
|
239
|
+
chunk = input.read(current_buf_len)
|
|
240
|
+
if first_check:
|
|
241
|
+
first_check = 0
|
|
242
|
+
if not PyBytes_Check(chunk):
|
|
243
|
+
raise TypeError(f"input must be a file-like rb object, got {type(input).__name__}")
|
|
244
|
+
size = PyBytes_GET_SIZE(chunk)
|
|
245
|
+
if <int32_t> size < current_buf_len: # 数据不够了 要减小一次读取的量
|
|
246
|
+
if buf_rate > 1: # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
|
|
247
|
+
buf_rate = buf_rate / 2
|
|
248
|
+
current_buf_len = buf_rate * 7
|
|
249
|
+
input.seek(-size, 1)
|
|
250
|
+
continue
|
|
251
|
+
chunk_ptr = <const char*>PyBytes_AS_STRING(chunk)
|
|
252
|
+
with nogil:
|
|
253
|
+
count = b14_encode_safe(chunk_ptr, <int>size, output_buf)
|
|
254
|
+
output.write(<bytes>output_buf[:count])
|
|
255
|
+
if size < 7:
|
|
256
|
+
break
|
|
257
|
+
finally:
|
|
258
|
+
PyMem_Free(output_buf)
|
|
259
|
+
|
|
260
|
+
def decode_file(object input,
|
|
261
|
+
object output,
|
|
262
|
+
int32_t buf_rate = 10):
|
|
263
|
+
if not PyFile_Check(input):
|
|
264
|
+
raise TypeError("input except a file-like object, got %s" % type(input).__name__)
|
|
265
|
+
if not PyFile_Check(output):
|
|
266
|
+
raise TypeError("output except a file-like object, got %s" % type(output).__name__)
|
|
267
|
+
if buf_rate <= 0:
|
|
268
|
+
buf_rate = 1
|
|
269
|
+
|
|
270
|
+
chunk = input.read(1) # type: bytes
|
|
271
|
+
if not PyBytes_Check(chunk):
|
|
272
|
+
raise TypeError(f"input must be a file-like rb object, got {type(input).__name__}")
|
|
273
|
+
if chunk == b"\xfe": # 去头
|
|
274
|
+
input.read(1)
|
|
275
|
+
else:
|
|
276
|
+
input.seek(0, 0) # 没有头 回到开头
|
|
277
|
+
|
|
278
|
+
cdef int32_t current_buf_len = buf_rate * 8
|
|
279
|
+
cdef size_t output_size = <size_t> b14_decode_len(<int> current_buf_len, 0) + 16
|
|
280
|
+
cdef char *output_buf = <char *> PyMem_Malloc(output_size)
|
|
281
|
+
if output_buf == NULL:
|
|
282
|
+
raise MemoryError
|
|
283
|
+
cdef Py_ssize_t size
|
|
284
|
+
cdef int count = 0
|
|
285
|
+
cdef const char *chunk_ptr
|
|
286
|
+
try:
|
|
287
|
+
while True:
|
|
288
|
+
chunk = input.read(current_buf_len) # 8的倍数
|
|
289
|
+
size = PyBytes_GET_SIZE(chunk)
|
|
290
|
+
if size == 0:
|
|
291
|
+
break
|
|
292
|
+
if <int32_t> size < current_buf_len: # 长度不够了
|
|
293
|
+
if buf_rate > 1: # 还能继续变小
|
|
294
|
+
buf_rate = buf_rate / 2 # 重新设置一次读取的大小
|
|
295
|
+
current_buf_len = buf_rate * 8
|
|
296
|
+
input.seek(-size, 1)
|
|
297
|
+
continue
|
|
298
|
+
tmp = input.read(2) # type: bytes
|
|
299
|
+
if PyBytes_GET_SIZE(tmp) == 2:
|
|
300
|
+
if tmp[0] == 61: # = stream完了 一次解码8n+2个字节
|
|
301
|
+
chunk += tmp
|
|
302
|
+
size += 2
|
|
303
|
+
else:
|
|
304
|
+
input.seek(-2, 1)
|
|
305
|
+
chunk_ptr = <const char *> PyBytes_AS_STRING(chunk)
|
|
306
|
+
with nogil:
|
|
307
|
+
count = b14_decode(chunk_ptr, <int> size, output_buf)
|
|
308
|
+
output.write(<bytes>output_buf[:count])
|
|
309
|
+
finally:
|
|
310
|
+
PyMem_Free(output_buf)
|
|
311
|
+
|
|
312
|
+
def decode_file_safe(object input,
|
|
313
|
+
object output,
|
|
314
|
+
int32_t buf_rate = 10):
|
|
315
|
+
if not PyFile_Check(input):
|
|
316
|
+
raise TypeError("input except a file-like object, got %s" % type(input).__name__)
|
|
317
|
+
if not PyFile_Check(output):
|
|
318
|
+
raise TypeError("output except a file-like object, got %s" % type(output).__name__)
|
|
319
|
+
if buf_rate <= 0:
|
|
320
|
+
buf_rate = 1
|
|
321
|
+
|
|
322
|
+
chunk = input.read(1) # type: bytes
|
|
323
|
+
if not PyBytes_Check(chunk):
|
|
324
|
+
raise TypeError(f"input must be a file-like rb object, got {type(input).__name__}")
|
|
325
|
+
if chunk == b"\xfe": # 去头
|
|
326
|
+
input.read(1)
|
|
327
|
+
else:
|
|
328
|
+
input.seek(0, 0) # 没有头 回到开头
|
|
329
|
+
|
|
330
|
+
cdef int32_t current_buf_len = buf_rate * 8
|
|
331
|
+
cdef size_t output_size = <size_t> b14_decode_len(<int> current_buf_len, 0)
|
|
332
|
+
cdef char *output_buf = <char *> PyMem_Malloc(output_size)
|
|
333
|
+
if output_buf == NULL:
|
|
334
|
+
raise MemoryError
|
|
335
|
+
cdef Py_ssize_t size
|
|
336
|
+
cdef int count = 0
|
|
337
|
+
cdef const char *chunk_ptr
|
|
338
|
+
try:
|
|
339
|
+
while True:
|
|
340
|
+
chunk = input.read(current_buf_len) # 8的倍数
|
|
341
|
+
size = PyBytes_GET_SIZE(chunk)
|
|
342
|
+
if size == 0:
|
|
343
|
+
break
|
|
344
|
+
if <int32_t> size < current_buf_len: # 长度不够了
|
|
345
|
+
if buf_rate > 1: # 还能继续变小
|
|
346
|
+
buf_rate = buf_rate / 2 # 重新设置一次读取的大小
|
|
347
|
+
current_buf_len = buf_rate * 8
|
|
348
|
+
input.seek(-size, 1)
|
|
349
|
+
continue
|
|
350
|
+
tmp = input.read(2) # type: bytes
|
|
351
|
+
if PyBytes_GET_SIZE(tmp) == 2:
|
|
352
|
+
if tmp[0] == 61: # = stream完了 一次解码8n+2个字节
|
|
353
|
+
chunk += tmp
|
|
354
|
+
size += 2
|
|
355
|
+
else:
|
|
356
|
+
input.seek(-2, 1)
|
|
357
|
+
chunk_ptr = <const char *> PyBytes_AS_STRING(chunk)
|
|
358
|
+
with nogil:
|
|
359
|
+
count = b14_decode_safe(chunk_ptr, <int> size, output_buf)
|
|
360
|
+
output.write(<bytes>output_buf[:count])
|
|
361
|
+
finally:
|
|
362
|
+
PyMem_Free(output_buf)
|
|
363
|
+
|
|
364
|
+
cpdef inline bint is_64bits() nogil:
|
|
365
|
+
return pybase16384_64bits()
|
|
366
|
+
|
|
367
|
+
cdef inline str err_to_str(base16384_err_t ret):
|
|
368
|
+
if ret == base16384_err_get_file_size:
|
|
369
|
+
return "base16384_err_get_file_size"
|
|
370
|
+
elif ret == base16384_err_fopen_output_file:
|
|
371
|
+
return "base16384_err_fopen_output_file"
|
|
372
|
+
elif ret == base16384_err_fopen_input_file:
|
|
373
|
+
return "base16384_err_fopen_input_file"
|
|
374
|
+
elif ret == base16384_err_write_file:
|
|
375
|
+
return "base16384_err_write_file"
|
|
376
|
+
elif ret == base16384_err_open_input_file:
|
|
377
|
+
return "base16384_err_open_input_file"
|
|
378
|
+
elif ret == base16384_err_map_input_file:
|
|
379
|
+
return "base16384_err_map_input_file"
|
|
380
|
+
elif ret == base16384_err_read_file:
|
|
381
|
+
return "base16384_err_read_file"
|
|
382
|
+
elif ret == base16384_err_invalid_file_name:
|
|
383
|
+
return "base16384_err_invalid_file_name"
|
|
384
|
+
elif ret == base16384_err_invalid_commandline_parameter:
|
|
385
|
+
return "base16384_err_invalid_commandline_parameter"
|
|
386
|
+
elif ret == base16384_err_invalid_decoding_checksum:
|
|
387
|
+
return "base16384_err_invalid_decoding_checksum"
|
|
388
|
+
|
|
389
|
+
cpdef inline encode_local_file(object inp, object out):
|
|
390
|
+
cdef bytes inp_name = ensure_bytes(inp)
|
|
391
|
+
cdef bytes out_name = ensure_bytes(out)
|
|
392
|
+
cdef const char * inp_name_ptr = <const char *> inp_name
|
|
393
|
+
cdef const char * out_name_ptr = <const char *> out_name
|
|
394
|
+
cdef char * encbuf = <char*>PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
395
|
+
if encbuf == NULL:
|
|
396
|
+
raise MemoryError
|
|
397
|
+
cdef char * decbuf = <char*>PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
398
|
+
if decbuf == NULL:
|
|
399
|
+
PyMem_Free(encbuf)
|
|
400
|
+
raise MemoryError
|
|
401
|
+
cdef base16384_err_t ret
|
|
402
|
+
try:
|
|
403
|
+
with nogil:
|
|
404
|
+
ret = b14_encode_file(inp_name_ptr, out_name_ptr, encbuf, decbuf)
|
|
405
|
+
if ret != base16384_err_ok:
|
|
406
|
+
raise ValueError(err_to_str(ret))
|
|
407
|
+
finally:
|
|
408
|
+
PyMem_Free(encbuf)
|
|
409
|
+
PyMem_Free(decbuf)
|
|
410
|
+
|
|
411
|
+
cpdef inline decode_local_file(object inp, object out):
|
|
412
|
+
cdef bytes inp_name = ensure_bytes(inp)
|
|
413
|
+
cdef bytes out_name = ensure_bytes(out)
|
|
414
|
+
cdef const char * inp_name_ptr = <const char *> inp_name
|
|
415
|
+
cdef const char * out_name_ptr = <const char *> out_name
|
|
416
|
+
cdef char * encbuf = <char*>PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
417
|
+
if encbuf == NULL:
|
|
418
|
+
raise MemoryError
|
|
419
|
+
cdef char * decbuf = <char*>PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
420
|
+
if decbuf == NULL:
|
|
421
|
+
PyMem_Free(encbuf)
|
|
422
|
+
raise MemoryError
|
|
423
|
+
cdef base16384_err_t ret
|
|
424
|
+
try:
|
|
425
|
+
with nogil:
|
|
426
|
+
ret = b14_decode_file(inp_name_ptr, out_name_ptr, encbuf, decbuf)
|
|
427
|
+
if ret != base16384_err_ok:
|
|
428
|
+
raise ValueError(err_to_str(ret))
|
|
429
|
+
finally:
|
|
430
|
+
PyMem_Free(encbuf)
|
|
431
|
+
PyMem_Free(decbuf)
|
|
432
|
+
|
|
433
|
+
cpdef inline encode_fd(int inp, int out):
|
|
434
|
+
cdef char * encbuf = <char *> PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
435
|
+
if encbuf == NULL:
|
|
436
|
+
raise MemoryError
|
|
437
|
+
cdef char * decbuf = <char *> PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
438
|
+
if decbuf == NULL:
|
|
439
|
+
PyMem_Free(encbuf)
|
|
440
|
+
raise MemoryError
|
|
441
|
+
cdef base16384_err_t ret
|
|
442
|
+
try:
|
|
443
|
+
with nogil:
|
|
444
|
+
ret = b14_encode_fd(inp, out, encbuf, decbuf)
|
|
445
|
+
if ret != base16384_err_ok:
|
|
446
|
+
raise ValueError(err_to_str(ret))
|
|
447
|
+
finally:
|
|
448
|
+
PyMem_Free(encbuf)
|
|
449
|
+
PyMem_Free(decbuf)
|
|
450
|
+
|
|
451
|
+
cpdef inline decode_fd(int inp, int out):
|
|
452
|
+
cdef char * encbuf = <char *> PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
453
|
+
if encbuf == NULL:
|
|
454
|
+
raise MemoryError
|
|
455
|
+
cdef char * decbuf = <char *> PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
456
|
+
if decbuf == NULL:
|
|
457
|
+
PyMem_Free(encbuf)
|
|
458
|
+
raise MemoryError
|
|
459
|
+
cdef base16384_err_t ret
|
|
460
|
+
try:
|
|
461
|
+
with nogil:
|
|
462
|
+
ret = b14_decode_fd(inp, out, encbuf, decbuf)
|
|
463
|
+
if ret != base16384_err_ok:
|
|
464
|
+
raise ValueError(err_to_str(ret))
|
|
465
|
+
finally:
|
|
466
|
+
PyMem_Free(encbuf)
|
|
467
|
+
PyMem_Free(decbuf)
|
|
468
|
+
|
|
469
|
+
# detailed
|
|
470
|
+
cpdef inline encode_local_file_detailed(object inp, object out, int flag):
|
|
471
|
+
cdef bytes inp_name = ensure_bytes(inp)
|
|
472
|
+
cdef bytes out_name = ensure_bytes(out)
|
|
473
|
+
cdef const char * inp_name_ptr = <const char *> inp_name
|
|
474
|
+
cdef const char * out_name_ptr = <const char *> out_name
|
|
475
|
+
cdef char * encbuf = <char*>PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
476
|
+
if encbuf == NULL:
|
|
477
|
+
raise MemoryError
|
|
478
|
+
cdef char * decbuf = <char*>PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
479
|
+
if decbuf == NULL:
|
|
480
|
+
PyMem_Free(encbuf)
|
|
481
|
+
raise MemoryError
|
|
482
|
+
cdef base16384_err_t ret
|
|
483
|
+
try:
|
|
484
|
+
with nogil:
|
|
485
|
+
ret = b14_encode_file_detailed(inp_name_ptr, out_name_ptr, encbuf, decbuf, flag)
|
|
486
|
+
if ret != base16384_err_ok:
|
|
487
|
+
raise ValueError(err_to_str(ret))
|
|
488
|
+
finally:
|
|
489
|
+
PyMem_Free(encbuf)
|
|
490
|
+
PyMem_Free(decbuf)
|
|
491
|
+
|
|
492
|
+
cpdef inline decode_local_file_detailed(object inp, object out, int flag):
|
|
493
|
+
cdef bytes inp_name = ensure_bytes(inp)
|
|
494
|
+
cdef bytes out_name = ensure_bytes(out)
|
|
495
|
+
cdef const char * inp_name_ptr = <const char *> inp_name
|
|
496
|
+
cdef const char * out_name_ptr = <const char *> out_name
|
|
497
|
+
cdef char * encbuf = <char*>PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
498
|
+
if encbuf == NULL:
|
|
499
|
+
raise MemoryError
|
|
500
|
+
cdef char * decbuf = <char*>PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
501
|
+
if decbuf == NULL:
|
|
502
|
+
PyMem_Free(encbuf)
|
|
503
|
+
raise MemoryError
|
|
504
|
+
cdef base16384_err_t ret
|
|
505
|
+
try:
|
|
506
|
+
with nogil:
|
|
507
|
+
ret = b14_decode_file_detailed(inp_name_ptr, out_name_ptr, encbuf, decbuf, flag)
|
|
508
|
+
if ret != base16384_err_ok:
|
|
509
|
+
raise ValueError(err_to_str(ret))
|
|
510
|
+
finally:
|
|
511
|
+
PyMem_Free(encbuf)
|
|
512
|
+
PyMem_Free(decbuf)
|
|
513
|
+
|
|
514
|
+
cpdef inline encode_fd_detailed(int inp, int out, int flag):
|
|
515
|
+
cdef char * encbuf = <char *> PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
516
|
+
if encbuf == NULL:
|
|
517
|
+
raise MemoryError
|
|
518
|
+
cdef char * decbuf = <char *> PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
519
|
+
if decbuf == NULL:
|
|
520
|
+
PyMem_Free(encbuf)
|
|
521
|
+
raise MemoryError
|
|
522
|
+
cdef base16384_err_t ret
|
|
523
|
+
try:
|
|
524
|
+
with nogil:
|
|
525
|
+
ret = b14_encode_fd_detailed(inp, out, encbuf, decbuf, flag)
|
|
526
|
+
if ret != base16384_err_ok:
|
|
527
|
+
raise ValueError(err_to_str(ret))
|
|
528
|
+
finally:
|
|
529
|
+
PyMem_Free(encbuf)
|
|
530
|
+
PyMem_Free(decbuf)
|
|
531
|
+
|
|
532
|
+
cpdef inline decode_fd_detailed(int inp, int out, int flag):
|
|
533
|
+
cdef char * encbuf = <char *> PyMem_Malloc(<size_t>BASE16384_ENCBUFSZ)
|
|
534
|
+
if encbuf == NULL:
|
|
535
|
+
raise MemoryError
|
|
536
|
+
cdef char * decbuf = <char *> PyMem_Malloc(<size_t>BASE16384_DECBUFSZ)
|
|
537
|
+
if decbuf == NULL:
|
|
538
|
+
PyMem_Free(encbuf)
|
|
539
|
+
raise MemoryError
|
|
540
|
+
cdef base16384_err_t ret
|
|
541
|
+
try:
|
|
542
|
+
with nogil:
|
|
543
|
+
ret = b14_decode_fd_detailed(inp, out, encbuf, decbuf, flag)
|
|
544
|
+
if ret != base16384_err_ok:
|
|
545
|
+
raise ValueError(err_to_str(ret))
|
|
546
|
+
finally:
|
|
547
|
+
PyMem_Free(encbuf)
|
|
548
|
+
PyMem_Free(decbuf)
|
|
549
|
+
|
|
550
|
+
# stream
|
|
551
|
+
cdef ssize_t b14_readcallback(const void *client_data, void *buffer, size_t count) except -100 with gil:
|
|
552
|
+
cdef object file = <object>client_data
|
|
553
|
+
cdef bytes data = file.read(count)
|
|
554
|
+
cdef char* data_ptr = PyBytes_AS_STRING(data)
|
|
555
|
+
cdef ssize_t data_size = <ssize_t>PyBytes_GET_SIZE(data)
|
|
556
|
+
memcpy(buffer, data_ptr, <size_t>data_size)
|
|
557
|
+
return data_size
|
|
558
|
+
|
|
559
|
+
cdef ssize_t b14_writecallback(const void *client_data, const void *buffer, size_t count) except -100 with gil:
|
|
560
|
+
cdef object file = <object>client_data
|
|
561
|
+
cdef bytes data = PyBytes_FromStringAndSize(<char*>buffer, <Py_ssize_t>count)
|
|
562
|
+
cdef ssize_t ret = <ssize_t>file.write(data)
|
|
563
|
+
return ret
|
|
564
|
+
|
|
565
|
+
cpdef inline encode_stream_detailed(object inp, object out, int flag):
|
|
566
|
+
cdef char * encbuf = <char *> PyMem_Malloc(<size_t> BASE16384_ENCBUFSZ)
|
|
567
|
+
if encbuf == NULL:
|
|
568
|
+
raise MemoryError
|
|
569
|
+
cdef char * decbuf = <char *> PyMem_Malloc(<size_t> BASE16384_DECBUFSZ)
|
|
570
|
+
if decbuf == NULL:
|
|
571
|
+
PyMem_Free(encbuf)
|
|
572
|
+
raise MemoryError
|
|
573
|
+
|
|
574
|
+
cdef base16384_err_t ret
|
|
575
|
+
|
|
576
|
+
cdef base16384_stream_t inpstream = base16384_stream_t(f=base16384_io_function_t(reader=b14_readcallback),
|
|
577
|
+
client_data=<void *> inp)
|
|
578
|
+
# inpstream.f.reader = b14_readcallback
|
|
579
|
+
# inpstream.client_data = <const void*>inp
|
|
580
|
+
|
|
581
|
+
cdef base16384_stream_t outstream = base16384_stream_t(f=base16384_io_function_t(writer=b14_writecallback),
|
|
582
|
+
client_data=<void *> out)
|
|
583
|
+
# outstream.f.writer = b14_writecallback
|
|
584
|
+
# outstream.client_data = <const void*>out
|
|
585
|
+
try:
|
|
586
|
+
with nogil:
|
|
587
|
+
ret = b14_encode_stream_detailed(&inpstream, &outstream, encbuf, decbuf, flag)
|
|
588
|
+
if ret != base16384_err_ok:
|
|
589
|
+
raise ValueError(err_to_str(ret))
|
|
590
|
+
finally:
|
|
591
|
+
PyMem_Free(encbuf)
|
|
592
|
+
PyMem_Free(decbuf)
|
|
593
|
+
|
|
594
|
+
cpdef inline decode_stream_detailed(object inp, object out, int flag):
|
|
595
|
+
cdef char * encbuf = <char *> PyMem_Malloc(<size_t> BASE16384_ENCBUFSZ)
|
|
596
|
+
if encbuf == NULL:
|
|
597
|
+
raise MemoryError
|
|
598
|
+
cdef char * decbuf = <char *> PyMem_Malloc(<size_t> BASE16384_DECBUFSZ)
|
|
599
|
+
if decbuf == NULL:
|
|
600
|
+
PyMem_Free(encbuf)
|
|
601
|
+
raise MemoryError
|
|
602
|
+
|
|
603
|
+
cdef base16384_err_t ret
|
|
604
|
+
|
|
605
|
+
cdef base16384_stream_t inpstream = base16384_stream_t(f=base16384_io_function_t(reader=b14_readcallback),client_data= <void*>inp)
|
|
606
|
+
# inpstream.f.reader = b14_readcallback
|
|
607
|
+
# inpstream.client_data = <const void*>inp
|
|
608
|
+
|
|
609
|
+
cdef base16384_stream_t outstream = base16384_stream_t(f=base16384_io_function_t(writer=b14_writecallback),client_data= <void*>out)
|
|
610
|
+
# outstream.f.writer = b14_writecallback
|
|
611
|
+
# outstream.client_data = <const void*>out
|
|
612
|
+
try:
|
|
613
|
+
with nogil:
|
|
614
|
+
ret = b14_decode_stream_detailed(&inpstream, &outstream, encbuf, decbuf, flag)
|
|
615
|
+
if ret != base16384_err_ok:
|
|
616
|
+
raise ValueError(err_to_str(ret))
|
|
617
|
+
finally:
|
|
618
|
+
PyMem_Free(encbuf)
|
|
619
|
+
PyMem_Free(decbuf)
|