pybase16384 0.3.6__cp311-cp311-win_amd64.whl → 0.3.7__cp311-cp311-win_amd64.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 CHANGED
@@ -19,22 +19,28 @@ 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,
35
40
  encode_fd,
36
41
  encode_fd_detailed,
37
42
  encode_file,
43
+ encode_file_safe,
38
44
  encode_len,
39
45
  encode_local_file,
40
46
  encode_local_file_detailed,
@@ -44,29 +50,35 @@ else:
44
50
  from pybase16384.backends.cffi import (
45
51
  DECBUFSZ,
46
52
  ENCBUFSZ,
53
+ FLAG_DO_SUM_CHECK_FORCELY,
47
54
  FLAG_NOHEADER,
48
55
  FLAG_SUM_CHECK_ON_REMAIN,
49
- SIMPLE_SUM_INIT_VALUE,
50
56
  _decode,
51
57
  _decode_into,
58
+ _decode_into_safe,
59
+ _decode_safe,
52
60
  _encode,
53
61
  _encode_into,
62
+ _encode_into_safe,
63
+ _encode_safe,
54
64
  decode_fd,
55
65
  decode_fd_detailed,
56
66
  decode_file,
67
+ decode_file_safe,
57
68
  decode_len,
58
69
  decode_local_file,
59
70
  decode_local_file_detailed,
60
71
  encode_fd,
61
72
  encode_fd_detailed,
62
73
  encode_file,
74
+ encode_file_safe,
63
75
  encode_len,
64
76
  encode_local_file,
65
77
  encode_local_file_detailed,
66
78
  is_64bits,
67
79
  )
68
80
 
69
- __version__ = "0.3.6"
81
+ __version__ = "0.3.7"
70
82
 
71
83
 
72
84
  def encode(data: bytes) -> bytes:
@@ -76,6 +88,13 @@ def encode(data: bytes) -> bytes:
76
88
  return out.getvalue()
77
89
 
78
90
 
91
+ def encode_safe(data: bytes) -> bytes:
92
+ inp = BytesIO(data)
93
+ out = BytesIO()
94
+ encode_file_safe(inp, out, False, len(data) // 7)
95
+ return out.getvalue()
96
+
97
+
79
98
  def decode(data: bytes) -> bytes:
80
99
  inp = BytesIO(data)
81
100
  out = BytesIO()
@@ -83,6 +102,13 @@ def decode(data: bytes) -> bytes:
83
102
  return out.getvalue()
84
103
 
85
104
 
105
+ def decode_safe(data: bytes) -> bytes:
106
+ inp = BytesIO(data)
107
+ out = BytesIO()
108
+ decode_file_safe(inp, out, len(data) // 8)
109
+ return out.getvalue()
110
+
111
+
86
112
  def encode_from_string(data: str, write_head: bool = False) -> bytes:
87
113
  bt = data.encode()
88
114
  inp = BytesIO(bt)
@@ -91,6 +117,14 @@ def encode_from_string(data: str, write_head: bool = False) -> bytes:
91
117
  return out.getvalue()
92
118
 
93
119
 
120
+ def encode_from_string_safe(data: str, write_head: bool = False) -> bytes:
121
+ bt = data.encode()
122
+ inp = BytesIO(bt)
123
+ out = BytesIO()
124
+ encode_file_safe(inp, out, write_head, len(bt) // 7)
125
+ return out.getvalue()
126
+
127
+
94
128
  def encode_to_string(data: bytes) -> str:
95
129
  inp = BytesIO(data)
96
130
  out = BytesIO()
@@ -98,6 +132,13 @@ def encode_to_string(data: bytes) -> str:
98
132
  return out.getvalue().decode("utf-16-be")
99
133
 
100
134
 
135
+ def encode_to_string_safe(data: bytes) -> str:
136
+ inp = BytesIO(data)
137
+ out = BytesIO()
138
+ encode_file_safe(inp, out, False, len(data) // 7)
139
+ return out.getvalue().decode("utf-16-be")
140
+
141
+
101
142
  def encode_string(data: str) -> str:
102
143
  data = data.encode()
103
144
  inp = BytesIO(data)
@@ -106,6 +147,14 @@ def encode_string(data: str) -> str:
106
147
  return out.getvalue().decode("utf-16-be")
107
148
 
108
149
 
150
+ def encode_string_safe(data: str) -> str:
151
+ data = data.encode()
152
+ inp = BytesIO(data)
153
+ out = BytesIO()
154
+ encode_file_safe(inp, out, False, len(data) // 7)
155
+ return out.getvalue().decode("utf-16-be")
156
+
157
+
109
158
  def decode_from_bytes(data: bytes) -> str:
110
159
  inp = BytesIO(data)
111
160
  out = BytesIO()
@@ -113,6 +162,13 @@ def decode_from_bytes(data: bytes) -> str:
113
162
  return out.getvalue().decode()
114
163
 
115
164
 
165
+ def decode_from_bytes_safe(data: bytes) -> str:
166
+ inp = BytesIO(data)
167
+ out = BytesIO()
168
+ decode_file_safe(inp, out, len(data) // 8)
169
+ return out.getvalue().decode()
170
+
171
+
116
172
  def decode_from_string(data: str) -> bytes:
117
173
  bt = data.encode("utf-16-be")
118
174
  inp = BytesIO(bt)
@@ -121,9 +177,25 @@ def decode_from_string(data: str) -> bytes:
121
177
  return out.getvalue()
122
178
 
123
179
 
180
+ def decode_from_string_safe(data: str) -> bytes:
181
+ bt = data.encode("utf-16-be")
182
+ inp = BytesIO(bt)
183
+ out = BytesIO()
184
+ decode_file_safe(inp, out, len(bt) // 8)
185
+ return out.getvalue()
186
+
187
+
124
188
  def decode_string(data: str) -> str:
125
189
  bt = data.encode("utf-16-be")
126
190
  inp = BytesIO(bt)
127
191
  out = BytesIO()
128
192
  decode_file(inp, out, len(bt) // 8)
129
193
  return out.getvalue().decode()
194
+
195
+
196
+ def decode_string_safe(data: str) -> str:
197
+ bt = data.encode("utf-16-be")
198
+ inp = BytesIO(bt)
199
+ out = BytesIO()
200
+ decode_file_safe(inp, out, len(bt) // 8)
201
+ return out.getvalue().decode()
@@ -15,7 +15,7 @@ ENCBUFSZ = lib.get_encsize()
15
15
  DECBUFSZ = lib.get_decsize()
16
16
  FLAG_NOHEADER = lib.BASE16384_FLAG_NOHEADER_()
17
17
  FLAG_SUM_CHECK_ON_REMAIN = lib.BASE16384_FLAG_SUM_CHECK_ON_REMAIN_()
18
- SIMPLE_SUM_INIT_VALUE = lib.BASE16384_SIMPLE_SUM_INIT_VALUE_()
18
+ FLAG_DO_SUM_CHECK_FORCELY = lib.BASE16384_FLAG_DO_SUM_CHECK_FORCELY_()
19
19
 
20
20
 
21
21
  # -----------------low level api------------------------------
@@ -29,10 +29,26 @@ def _encode(data: bytes) -> bytes:
29
29
  return ffi.unpack(output_buf, count)
30
30
 
31
31
 
32
+ def _encode_safe(data: bytes) -> bytes:
33
+ length = len(data)
34
+ output_size = encode_len(length)
35
+ output_buf = ffi.new(f"char[{output_size}]")
36
+ if output_buf == ffi.NULL:
37
+ raise MemoryError
38
+ count = lib.base16384_encode_safe(ffi.from_buffer(data), length, output_buf)
39
+ return ffi.unpack(output_buf, count)
40
+
41
+
32
42
  def _encode_into(data: bytes, out: bytearray) -> int:
33
43
  return lib.base16384_encode(ffi.from_buffer(data), len(data), ffi.from_buffer(out))
34
44
 
35
45
 
46
+ def _encode_into_safe(data: bytes, out: bytearray) -> int:
47
+ return lib.base16384_encode_safe(
48
+ ffi.from_buffer(data), len(data), ffi.from_buffer(out)
49
+ )
50
+
51
+
36
52
  def _decode(data: bytes) -> bytes:
37
53
  length = len(data)
38
54
  output_size = decode_len(length, 0) + 16
@@ -43,10 +59,26 @@ def _decode(data: bytes) -> bytes:
43
59
  return ffi.unpack(output_buf, count)
44
60
 
45
61
 
62
+ def _decode_safe(data: bytes) -> bytes:
63
+ length = len(data)
64
+ output_size = decode_len(length, 0)
65
+ output_buf = ffi.new(f"char[{output_size}]")
66
+ if output_buf == ffi.NULL:
67
+ raise MemoryError
68
+ count = lib.base16384_decode_safe(ffi.from_buffer(data), length, output_buf)
69
+ return ffi.unpack(output_buf, count)
70
+
71
+
46
72
  def _decode_into(data: bytes, out: bytearray) -> int:
47
73
  return lib.base16384_decode(ffi.from_buffer(data), len(data), ffi.from_buffer(out))
48
74
 
49
75
 
76
+ def _decode_into_safe(data: bytes, out: bytearray) -> int:
77
+ return lib.base16384_decode_safe(
78
+ ffi.from_buffer(data), len(data), ffi.from_buffer(out)
79
+ )
80
+
81
+
50
82
  def is_64bits() -> bool:
51
83
  return bool(lib.pybase16384_64bits())
52
84
 
@@ -100,6 +132,50 @@ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int =
100
132
  break
101
133
 
102
134
 
135
+ def encode_file_safe(
136
+ input: IO, output: IO, write_head: bool = False, buf_rate: int = 10
137
+ ):
138
+ if not _check_file(input):
139
+ raise TypeError(
140
+ "input except a file-like object, got %s" % type(input).__name__
141
+ )
142
+ if not _check_file(output):
143
+ raise TypeError(
144
+ "output except a file-like object, got %s" % type(input).__name__
145
+ )
146
+ if buf_rate <= 0:
147
+ buf_rate = 1
148
+ if write_head:
149
+ output.write(b"\xfe\xff")
150
+
151
+ current_buf_len: int = buf_rate * 7 # 一次读取这么多字节
152
+ output_size: int = encode_len(current_buf_len) # 因为encode_len不是单调的 safe不用加16
153
+ output_buf = ffi.new(f"char[{output_size}]")
154
+ if output_buf == ffi.NULL:
155
+ raise MemoryError
156
+ first_check: int = 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
157
+ while True:
158
+ chunk = input.read(current_buf_len)
159
+ if first_check:
160
+ first_check = 0
161
+ if not isinstance(chunk, bytes):
162
+ raise TypeError(
163
+ f"input must be a file-like rb object, got {type(input).__name__}"
164
+ )
165
+ size = len(chunk)
166
+ if size < current_buf_len: # 数据不够了 要减小一次读取的量
167
+ if buf_rate > 1: # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
168
+ buf_rate = buf_rate // 2
169
+ current_buf_len = buf_rate * 7
170
+ input.seek(-size, 1)
171
+ continue
172
+
173
+ count = lib.base16384_encode_safe(ffi.from_buffer(chunk), size, output_buf)
174
+ output.write(ffi.unpack(output_buf, count))
175
+ if size < 7:
176
+ break
177
+
178
+
103
179
  def decode_file(input: IO, output: IO, buf_rate: int = 10):
104
180
  if not _check_file(input):
105
181
  raise TypeError(
@@ -150,6 +226,56 @@ def decode_file(input: IO, output: IO, buf_rate: int = 10):
150
226
  output.write(ffi.unpack(output_buf, count))
151
227
 
152
228
 
229
+ def decode_file_safe(input: IO, output: IO, buf_rate: int = 10):
230
+ if not _check_file(input):
231
+ raise TypeError(
232
+ "input except a file-like object, got %s" % type(input).__name__
233
+ )
234
+ if not _check_file(output):
235
+ raise TypeError(
236
+ "output except a file-like object, got %s" % type(output).__name__
237
+ )
238
+ if buf_rate <= 0:
239
+ buf_rate = 1
240
+
241
+ chunk = input.read(1) # type: bytes
242
+ if not isinstance(chunk, bytes):
243
+ raise TypeError(
244
+ f"input must be a file-like rb object, got {type(input).__name__}"
245
+ )
246
+ if chunk == b"\xfe": # 去头
247
+ input.read(1)
248
+ else:
249
+ input.seek(0, 0) # 没有头 回到开头
250
+
251
+ current_buf_len: int = buf_rate * 8
252
+ output_size: int = decode_len(current_buf_len, 0)
253
+ output_buf = ffi.new(f"char[{output_size}]")
254
+ if output_buf == ffi.NULL:
255
+ raise MemoryError
256
+ while True:
257
+ chunk = input.read(current_buf_len) # 8的倍数
258
+ size = len(chunk)
259
+ if size == 0:
260
+ break
261
+ if size < current_buf_len: # 长度不够了
262
+ if buf_rate > 1: # 还能继续变小
263
+ buf_rate = buf_rate // 2 # 重新设置一次读取的大小
264
+ current_buf_len = buf_rate * 8
265
+ input.seek(-size, 1)
266
+ continue
267
+ tmp = input.read(2) # type: bytes
268
+ if len(tmp) == 2:
269
+ if tmp[0] == 61: # = stream完了 一次解码8n+2个字节
270
+ chunk += tmp
271
+ size += 2
272
+ else:
273
+ input.seek(-2, 1)
274
+
275
+ count = lib.base16384_decode_safe(ffi.from_buffer(chunk), size, output_buf)
276
+ output.write(ffi.unpack(output_buf, count))
277
+
278
+
153
279
  def ensure_bytes(inp) -> bytes:
154
280
  if isinstance(inp, str):
155
281
  return inp.encode()
Binary file
@@ -74,6 +74,8 @@ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* d
74
74
 
75
75
  int base16384_encode_unsafe(const char * data, int dlen, char * buf);
76
76
  int base16384_decode_unsafe(const char * data, int dlen, char * buf);
77
+ int base16384_encode_safe(const char * data, int dlen, char * buf);
78
+ int base16384_decode_safe(const char * data, int dlen, char * buf);
77
79
 
78
80
  base16384_err_t base16384_encode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag);
79
81
  base16384_err_t base16384_decode_file_detailed(const char* input, const char* output, char* encbuf, char* decbuf, int flag);
@@ -92,7 +94,7 @@ int BASE16384_FLAG_NOHEADER_();
92
94
 
93
95
  int BASE16384_FLAG_SUM_CHECK_ON_REMAIN_();
94
96
 
95
- int BASE16384_SIMPLE_SUM_INIT_VALUE_();
97
+ int BASE16384_FLAG_DO_SUM_CHECK_FORCELY_();
96
98
  """
97
99
  )
98
100
 
@@ -100,9 +102,9 @@ source = """
100
102
  #include "base16384.h"
101
103
 
102
104
  #ifdef CPUBIT32
103
- #define pybase16384_64bits() 0
105
+ #define pybase16384_64bits() (0)
104
106
  #else
105
- #define pybase16384_64bits() 1
107
+ #define pybase16384_64bits() (1)
106
108
  #endif
107
109
 
108
110
  int get_encsize()
@@ -125,9 +127,9 @@ int BASE16384_FLAG_SUM_CHECK_ON_REMAIN_()
125
127
  return BASE16384_FLAG_SUM_CHECK_ON_REMAIN;
126
128
  }
127
129
 
128
- int BASE16384_SIMPLE_SUM_INIT_VALUE_()
130
+ int BASE16384_FLAG_DO_SUM_CHECK_FORCELY_()
129
131
  {
130
- return BASE16384_SIMPLE_SUM_INIT_VALUE;
132
+ return BASE16384_FLAG_DO_SUM_CHECK_FORCELY;
131
133
  }
132
134
  """
133
135
 
@@ -4,22 +4,28 @@ Copyright (c) 2008-2021 synodriver <synodriver@gmail.com>
4
4
  from pybase16384.backends.cython._core import (
5
5
  DECBUFSZ,
6
6
  ENCBUFSZ,
7
+ FLAG_DO_SUM_CHECK_FORCELY,
7
8
  FLAG_NOHEADER,
8
9
  FLAG_SUM_CHECK_ON_REMAIN,
9
- SIMPLE_SUM_INIT_VALUE,
10
10
  _decode,
11
11
  _decode_into,
12
+ _decode_into_safe,
13
+ _decode_safe,
12
14
  _encode,
13
15
  _encode_into,
16
+ _encode_into_safe,
17
+ _encode_safe,
14
18
  decode_fd,
15
19
  decode_fd_detailed,
16
20
  decode_file,
21
+ decode_file_safe,
17
22
  decode_len,
18
23
  decode_local_file,
19
24
  decode_local_file_detailed,
20
25
  encode_fd,
21
26
  encode_fd_detailed,
22
27
  encode_file,
28
+ encode_file_safe,
23
29
  encode_len,
24
30
  encode_local_file,
25
31
  encode_local_file_detailed,