pybase16384 0.3.7__cp310-cp310-macosx_10_9_x86_64.whl → 0.3.8__cp310-cp310-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 CHANGED
@@ -37,6 +37,7 @@ if not _should_use_cffi():
37
37
  decode_len,
38
38
  decode_local_file,
39
39
  decode_local_file_detailed,
40
+ decode_stream_detailed,
40
41
  encode_fd,
41
42
  encode_fd_detailed,
42
43
  encode_file,
@@ -44,6 +45,7 @@ if not _should_use_cffi():
44
45
  encode_len,
45
46
  encode_local_file,
46
47
  encode_local_file_detailed,
48
+ encode_stream_detailed,
47
49
  is_64bits,
48
50
  )
49
51
  else:
@@ -68,6 +70,7 @@ else:
68
70
  decode_len,
69
71
  decode_local_file,
70
72
  decode_local_file_detailed,
73
+ decode_stream_detailed,
71
74
  encode_fd,
72
75
  encode_fd_detailed,
73
76
  encode_file,
@@ -75,10 +78,11 @@ else:
75
78
  encode_len,
76
79
  encode_local_file,
77
80
  encode_local_file_detailed,
81
+ encode_stream_detailed,
78
82
  is_64bits,
79
83
  )
80
84
 
81
- __version__ = "0.3.7"
85
+ __version__ = "0.3.8"
82
86
 
83
87
 
84
88
  def encode(data: bytes) -> bytes:
@@ -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
 
@@ -105,11 +106,15 @@ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int =
105
106
  output.write(b"\xfe\xff")
106
107
 
107
108
  current_buf_len: int = buf_rate * 7 # 一次读取这么多字节
108
- output_size: int = encode_len(current_buf_len) + 16 # 因为encode_len不是单调的 这16备用
109
+ output_size: int = (
110
+ encode_len(current_buf_len) + 16
111
+ ) # 因为encode_len不是单调的 这16备用
109
112
  output_buf = ffi.new(f"char[{output_size}]")
110
113
  if output_buf == ffi.NULL:
111
114
  raise MemoryError
112
- first_check: int = 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
115
+ first_check: int = (
116
+ 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
117
+ )
113
118
  while True:
114
119
  chunk = input.read(current_buf_len)
115
120
  if first_check:
@@ -120,7 +125,9 @@ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int =
120
125
  )
121
126
  size = len(chunk)
122
127
  if size < current_buf_len: # 数据不够了 要减小一次读取的量
123
- if buf_rate > 1: # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
128
+ if (
129
+ buf_rate > 1
130
+ ): # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
124
131
  buf_rate = buf_rate // 2
125
132
  current_buf_len = buf_rate * 7
126
133
  input.seek(-size, 1)
@@ -149,11 +156,15 @@ def encode_file_safe(
149
156
  output.write(b"\xfe\xff")
150
157
 
151
158
  current_buf_len: int = buf_rate * 7 # 一次读取这么多字节
152
- output_size: int = encode_len(current_buf_len) # 因为encode_len不是单调的 safe不用加16
159
+ output_size: int = encode_len(
160
+ current_buf_len
161
+ ) # 因为encode_len不是单调的 safe不用加16
153
162
  output_buf = ffi.new(f"char[{output_size}]")
154
163
  if output_buf == ffi.NULL:
155
164
  raise MemoryError
156
- first_check: int = 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
165
+ first_check: int = (
166
+ 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
167
+ )
157
168
  while True:
158
169
  chunk = input.read(current_buf_len)
159
170
  if first_check:
@@ -164,7 +175,9 @@ def encode_file_safe(
164
175
  )
165
176
  size = len(chunk)
166
177
  if size < current_buf_len: # 数据不够了 要减小一次读取的量
167
- if buf_rate > 1: # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
178
+ if (
179
+ buf_rate > 1
180
+ ): # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
168
181
  buf_rate = buf_rate // 2
169
182
  current_buf_len = buf_rate * 7
170
183
  input.seek(-size, 1)
@@ -391,3 +404,60 @@ def decode_fd_detailed(inp: int, out: int, flag: int) -> None:
391
404
  ret = lib.base16384_decode_fd_detailed(inp, out, encbuf, decbuf, flag)
392
405
  if ret != lib.base16384_err_ok:
393
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
 
@@ -95,6 +96,39 @@ int BASE16384_FLAG_NOHEADER_();
95
96
  int BASE16384_FLAG_SUM_CHECK_ON_REMAIN_();
96
97
 
97
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);
98
132
  """
99
133
  )
100
134
 
@@ -136,7 +170,11 @@ int BASE16384_FLAG_DO_SUM_CHECK_FORCELY_()
136
170
  ffibuilder.set_source(
137
171
  "pybase16384.backends.cffi._core",
138
172
  source,
139
- sources=[f"./base16384/base14{CPUBIT}.c", "./base16384/file.c"],
173
+ sources=[
174
+ f"./base16384/base14{CPUBIT}.c",
175
+ "./base16384/file.c",
176
+ "./base16384/wrap.c",
177
+ ],
140
178
  include_dirs=["./base16384"],
141
179
  define_macros=macro_base,
142
180
  )
@@ -1,6 +1,7 @@
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,
@@ -22,6 +23,7 @@ from pybase16384.backends.cython._core import (
22
23
  decode_len,
23
24
  decode_local_file,
24
25
  decode_local_file_detailed,
26
+ decode_stream_detailed,
25
27
  encode_fd,
26
28
  encode_fd_detailed,
27
29
  encode_file,
@@ -29,5 +31,6 @@ from pybase16384.backends.cython._core import (
29
31
  encode_len,
30
32
  encode_local_file,
31
33
  encode_local_file_detailed,
34
+ encode_stream_detailed,
32
35
  is_64bits,
33
36
  )
Binary file