pybase16384 0.3.5__cp39-cp39-macosx_11_0_arm64.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.

@@ -0,0 +1,111 @@
1
+ import os
2
+ import platform
3
+ from io import BytesIO
4
+
5
+ impl = platform.python_implementation()
6
+
7
+
8
+ def _should_use_cffi() -> bool:
9
+ ev = os.getenv("B14_USE_CFFI")
10
+ if ev is not None:
11
+ return True
12
+ if impl == "CPython":
13
+ return False
14
+ else:
15
+ return True
16
+
17
+
18
+ if not _should_use_cffi():
19
+ from pybase16384.backends.cython import (
20
+ _decode,
21
+ _decode_into,
22
+ _encode,
23
+ _encode_into,
24
+ decode_fd,
25
+ decode_file,
26
+ decode_len,
27
+ decode_local_file,
28
+ encode_fd,
29
+ encode_file,
30
+ encode_len,
31
+ encode_local_file,
32
+ is_64bits,
33
+ )
34
+ else:
35
+ from pybase16384.backends.cffi import (
36
+ _decode,
37
+ _decode_into,
38
+ _encode,
39
+ _encode_into,
40
+ decode_fd,
41
+ decode_file,
42
+ decode_len,
43
+ decode_local_file,
44
+ encode_fd,
45
+ encode_file,
46
+ encode_len,
47
+ encode_local_file,
48
+ is_64bits,
49
+ )
50
+
51
+ __version__ = "0.3.5"
52
+
53
+
54
+ def encode(data: bytes) -> bytes:
55
+ inp = BytesIO(data)
56
+ out = BytesIO()
57
+ encode_file(inp, out, False, len(data) // 7)
58
+ return out.getvalue()
59
+
60
+
61
+ def decode(data: bytes) -> bytes:
62
+ inp = BytesIO(data)
63
+ out = BytesIO()
64
+ decode_file(inp, out, len(data) // 8)
65
+ return out.getvalue()
66
+
67
+
68
+ def encode_from_string(data: str, write_head: bool = False) -> bytes:
69
+ bt = data.encode()
70
+ inp = BytesIO(bt)
71
+ out = BytesIO()
72
+ encode_file(inp, out, write_head, len(bt) // 7)
73
+ return out.getvalue()
74
+
75
+
76
+ def encode_to_string(data: bytes) -> str:
77
+ inp = BytesIO(data)
78
+ out = BytesIO()
79
+ encode_file(inp, out, False, len(data) // 7)
80
+ return out.getvalue().decode("utf-16-be")
81
+
82
+
83
+ def encode_string(data: str) -> str:
84
+ data = data.encode()
85
+ inp = BytesIO(data)
86
+ out = BytesIO()
87
+ encode_file(inp, out, False, len(data) // 7)
88
+ return out.getvalue().decode("utf-16-be")
89
+
90
+
91
+ def decode_from_bytes(data: bytes) -> str:
92
+ inp = BytesIO(data)
93
+ out = BytesIO()
94
+ decode_file(inp, out, len(data) // 8)
95
+ return out.getvalue().decode()
96
+
97
+
98
+ def decode_from_string(data: str) -> bytes:
99
+ bt = data.encode("utf-16-be")
100
+ inp = BytesIO(bt)
101
+ out = BytesIO()
102
+ decode_file(inp, out, len(bt) // 8)
103
+ return out.getvalue()
104
+
105
+
106
+ def decode_string(data: str) -> str:
107
+ bt = data.encode("utf-16-be")
108
+ inp = BytesIO(bt)
109
+ out = BytesIO()
110
+ decode_file(inp, out, len(bt) // 8)
111
+ return out.getvalue().decode()
@@ -0,0 +1 @@
1
+ # Make cython happy, otherwise couldn't find pxd files
@@ -0,0 +1,218 @@
1
+ """
2
+ Copyright (c) 2008-2021 synodriver <synodriver@gmail.com>
3
+ """
4
+ from pathlib import Path
5
+ from typing import IO
6
+
7
+ from pybase16384.backends.cffi._core import ffi, lib
8
+
9
+ __version__ = "0.1.0"
10
+
11
+ encode_len = lib.base16384_encode_len
12
+ decode_len = lib.base16384_decode_len
13
+
14
+
15
+ # -----------------low level api------------------------------
16
+ def _encode(data: bytes) -> bytes:
17
+ length = len(data)
18
+ output_size = encode_len(length) + 16
19
+ output_buf = ffi.new(f"char[{output_size}]")
20
+ if output_buf == ffi.NULL:
21
+ raise MemoryError
22
+ count = lib.base16384_encode(ffi.from_buffer(data), length, output_buf)
23
+ return ffi.unpack(output_buf, count)
24
+
25
+
26
+ def _encode_into(data: bytes, out: bytearray) -> int:
27
+ return lib.base16384_encode(
28
+ ffi.from_buffer(data), len(data), ffi.from_buffer(out)
29
+ )
30
+
31
+
32
+ def _decode(data: bytes) -> bytes:
33
+ length = len(data)
34
+ output_size = decode_len(length, 0) + 16
35
+ output_buf = ffi.new(f"char[{output_size}]")
36
+ if output_buf == ffi.NULL:
37
+ raise MemoryError
38
+ count = lib.base16384_decode(ffi.from_buffer(data), length, output_buf)
39
+ return ffi.unpack(output_buf, count)
40
+
41
+
42
+ def _decode_into(data: bytes, out: bytearray) -> int:
43
+ return lib.base16384_decode(
44
+ ffi.from_buffer(data), len(data), ffi.from_buffer(out)
45
+ )
46
+
47
+
48
+ def is_64bits() -> bool:
49
+ return bool(lib.pybase16384_64bits())
50
+
51
+
52
+ # ----------------------------
53
+ def _check_file(file) -> bool:
54
+ if hasattr(file, "read") and hasattr(file, "write") and hasattr(file, "seek"):
55
+ return True
56
+ return False
57
+
58
+
59
+ def encode_file(input: IO, output: IO, write_head: bool = False, buf_rate: int = 10):
60
+ if not _check_file(input):
61
+ raise TypeError(
62
+ "input except a file-like object, got %s" % type(input).__name__
63
+ )
64
+ if not _check_file(output):
65
+ raise TypeError(
66
+ "output except a file-like object, got %s" % type(input).__name__
67
+ )
68
+ if buf_rate <= 0:
69
+ buf_rate = 1
70
+ if write_head:
71
+ output.write(b"\xfe\xff")
72
+
73
+ current_buf_len: int = buf_rate * 7 # 一次读取这么多字节
74
+ output_size: int = encode_len(current_buf_len) + 16 # 因为encode_len不是单调的 这16备用
75
+ output_buf = ffi.new(f"char[{output_size}]")
76
+ if output_buf == ffi.NULL:
77
+ raise MemoryError
78
+ first_check: int = 1 # 检查一次就行了 怎么可能出现第一次读出来是bytes 以后又变卦了的对象呢 不会吧不会吧
79
+ while True:
80
+ chunk = input.read(current_buf_len)
81
+ if first_check:
82
+ first_check = 0
83
+ if not isinstance(chunk, bytes):
84
+ raise TypeError(
85
+ f"input must be a file-like rb object, got {type(input).__name__}"
86
+ )
87
+ size = len(chunk)
88
+ if size < current_buf_len: # 数据不够了 要减小一次读取的量
89
+ if buf_rate > 1: # 重新设置一次读取的大小 重新设置流的位置 当然要是已经是一次读取7字节了 那就不能再变小了 直接encode吧
90
+ buf_rate = buf_rate // 2
91
+ current_buf_len = buf_rate * 7
92
+ input.seek(-size, 1)
93
+ continue
94
+
95
+ count = lib.base16384_encode(
96
+ ffi.from_buffer(chunk), size, output_buf
97
+ )
98
+ output.write(ffi.unpack(output_buf, count))
99
+ if size < 7:
100
+ break
101
+
102
+
103
+ def decode_file(input: IO, output: IO, buf_rate: int = 10):
104
+ if not _check_file(input):
105
+ raise TypeError(
106
+ "input except a file-like object, got %s" % type(input).__name__
107
+ )
108
+ if not _check_file(output):
109
+ raise TypeError(
110
+ "output except a file-like object, got %s" % type(output).__name__
111
+ )
112
+ if buf_rate <= 0:
113
+ buf_rate = 1
114
+
115
+ chunk = input.read(1) # type: bytes
116
+ if not isinstance(chunk, bytes):
117
+ raise TypeError(
118
+ f"input must be a file-like rb object, got {type(input).__name__}"
119
+ )
120
+ if chunk == b"\xfe": # 去头
121
+ input.read(1)
122
+ else:
123
+ input.seek(0, 0) # 没有头 回到开头
124
+
125
+ current_buf_len: int = buf_rate * 8
126
+ output_size: int = decode_len(current_buf_len, 0) + 16
127
+ output_buf = ffi.new(f"char[{output_size}]")
128
+ if output_buf == ffi.NULL:
129
+ raise MemoryError
130
+ while True:
131
+ chunk = input.read(current_buf_len) # 8的倍数
132
+ size = len(chunk)
133
+ if size == 0:
134
+ break
135
+ if size < current_buf_len: # 长度不够了
136
+ if buf_rate > 1: # 还能继续变小
137
+ buf_rate = buf_rate // 2 # 重新设置一次读取的大小
138
+ current_buf_len = buf_rate * 8
139
+ input.seek(-size, 1)
140
+ continue
141
+ tmp = input.read(2) # type: bytes
142
+ if len(tmp) == 2:
143
+ if tmp[0] == 61: # = stream完了 一次解码8n+2个字节
144
+ chunk += tmp
145
+ size += 2
146
+ else:
147
+ input.seek(-2, 1)
148
+
149
+ count = lib.base16384_decode(
150
+ ffi.from_buffer(chunk), size, output_buf
151
+ )
152
+ output.write(ffi.unpack(output_buf, count))
153
+
154
+
155
+ def ensure_bytes(inp) -> bytes:
156
+ if isinstance(inp, str):
157
+ return inp.encode()
158
+ elif isinstance(inp, bytes):
159
+ return inp
160
+ elif isinstance(inp, Path):
161
+ return str(inp).encode()
162
+ else:
163
+ return bytes(inp)
164
+
165
+
166
+ def err_to_str(ret) -> str:
167
+ if ret == lib.base16384_err_get_file_size:
168
+ return "base16384_err_get_file_size"
169
+ elif ret == lib.base16384_err_fopen_output_file:
170
+ return "base16384_err_fopen_output_file"
171
+ elif ret == lib.base16384_err_fopen_input_file:
172
+ return "base16384_err_fopen_input_file"
173
+ elif ret == lib.base16384_err_write_file:
174
+ return "base16384_err_write_file"
175
+ elif ret == lib.base16384_err_open_input_file:
176
+ return "base16384_err_open_input_file"
177
+ elif ret == lib.base16384_err_map_input_file:
178
+ return "base16384_err_map_input_file"
179
+
180
+
181
+ def encode_local_file(inp, out) -> None:
182
+ inp_name: bytes = ensure_bytes(inp)
183
+ out_name: bytes = ensure_bytes(out)
184
+ encbuf = ffi.new(f"char[{lib.get_encsize()}]")
185
+ decbuf = ffi.new(f"char[{lib.get_decsize()}]")
186
+ ret = lib.base16384_encode_file(
187
+ ffi.from_buffer(inp_name), ffi.from_buffer(out_name), encbuf, decbuf
188
+ )
189
+ if ret != lib.base16384_err_ok:
190
+ raise ValueError(err_to_str(ret))
191
+
192
+
193
+ def decode_local_file(inp, out) -> None:
194
+ inp_name: bytes = ensure_bytes(inp)
195
+ out_name: bytes = ensure_bytes(out)
196
+ encbuf = ffi.new(f"char[{lib.get_encsize()}]")
197
+ decbuf = ffi.new(f"char[{lib.get_decsize()}]")
198
+ ret = lib.base16384_decode_file(
199
+ ffi.from_buffer(inp_name), ffi.from_buffer(out_name), encbuf, decbuf
200
+ )
201
+ if ret != lib.base16384_err_ok:
202
+ raise ValueError(err_to_str(ret))
203
+
204
+
205
+ def encode_fd(inp: int, out: int) -> None:
206
+ encbuf = ffi.new(f"char[{lib.get_encsize()}]")
207
+ decbuf = ffi.new(f"char[{lib.get_decsize()}]")
208
+ ret = lib.base16384_encode_fd(inp, out, encbuf, decbuf)
209
+ if ret != lib.base16384_err_ok:
210
+ raise ValueError(err_to_str(ret))
211
+
212
+
213
+ def decode_fd(inp: int, out: int) -> None:
214
+ encbuf = ffi.new(f"char[{lib.get_encsize()}]")
215
+ decbuf = ffi.new(f"char[{lib.get_decsize()}]")
216
+ ret = lib.base16384_decode_fd(inp, out, encbuf, decbuf)
217
+ if ret != lib.base16384_err_ok:
218
+ raise ValueError(err_to_str(ret))
Binary file
@@ -0,0 +1,107 @@
1
+ """
2
+ Copyright (c) 2008-2021 synodriver <synodriver@gmail.com>
3
+ """
4
+ import platform
5
+ import sys
6
+
7
+ from cffi import FFI
8
+
9
+ if sys.maxsize > 2**32:
10
+ CPUBIT = 64
11
+ else:
12
+ CPUBIT = 32
13
+
14
+ system = platform.system()
15
+ if system == "Windows":
16
+ macro_base = [("_WIN64", None)]
17
+ elif system == "Linux":
18
+ macro_base = [("__linux__", None)]
19
+ elif system == "Darwin":
20
+ macro_base = [("__MAC_10_0", None)]
21
+ else:
22
+ macro_base = []
23
+
24
+ if sys.byteorder != "little":
25
+ macro_base.append(("WORDS_BIGENDIAN", None))
26
+
27
+ if CPUBIT == 64:
28
+ macro_base.append(("CPUBIT64", None))
29
+ else:
30
+ macro_base.append(("CPUBIT32", None))
31
+
32
+ ffibuilder = FFI()
33
+ ffibuilder.cdef(
34
+ """
35
+ // base16384_err_t is the return value of base16384_en/decode_file
36
+ enum base16384_err_t {
37
+ base16384_err_ok,
38
+ base16384_err_get_file_size,
39
+ base16384_err_fopen_output_file,
40
+ base16384_err_fopen_input_file,
41
+ base16384_err_write_file,
42
+ base16384_err_open_input_file,
43
+ base16384_err_map_input_file,
44
+ };
45
+ // base16384_err_t is the return value of base16384_en/decode_file
46
+ typedef enum base16384_err_t base16384_err_t;
47
+ int base16384_encode_len(int dlen);
48
+ int base16384_decode_len(int dlen, int offset);
49
+ int base16384_encode(const char* data, int dlen, char* buf);
50
+ int base16384_decode(const char* data, int dlen, char* buf);
51
+ base16384_err_t base16384_encode_file(const char* input, const char* output, char* encbuf, char* decbuf);
52
+ base16384_err_t base16384_decode_file(const char* input, const char* output, char* encbuf, char* decbuf);
53
+
54
+ // base16384_encode_fp encodes input file to output file.
55
+ // encbuf & decbuf must be no less than BASE16384_ENCBUFSZ & BASE16384_DECBUFSZ
56
+ base16384_err_t base16384_encode_fp(FILE* input, FILE* output, char* encbuf, char* decbuf);
57
+
58
+ // base16384_encode_fd encodes input fd to output fd.
59
+ // encbuf & decbuf must be no less than BASE16384_ENCBUFSZ & BASE16384_DECBUFSZ
60
+ base16384_err_t base16384_encode_fd(int input, int output, char* encbuf, char* decbuf);
61
+
62
+ // base16384_decode_fp decodes input file to output file.
63
+ // encbuf & decbuf must be no less than BASE16384_ENCBUFSZ & BASE16384_DECBUFSZ
64
+ base16384_err_t base16384_decode_fp(FILE* input, FILE* output, char* encbuf, char* decbuf);
65
+
66
+ // base16384_decode_fd decodes input fd to output fd.
67
+ // encbuf & decbuf must be no less than BASE16384_ENCBUFSZ & BASE16384_DECBUFSZ
68
+ base16384_err_t base16384_decode_fd(int input, int output, char* encbuf, char* decbuf);
69
+
70
+ int32_t pybase16384_64bits();
71
+
72
+ int get_encsize();
73
+
74
+ int get_decsize();
75
+ """
76
+ )
77
+
78
+ source = """
79
+ #include "base16384.h"
80
+
81
+ #ifdef CPUBIT32
82
+ #define pybase16384_64bits() 0
83
+ #else
84
+ #define pybase16384_64bits() 1
85
+ #endif
86
+
87
+ int get_encsize()
88
+ {
89
+ return BASE16384_ENCBUFSZ;
90
+ }
91
+
92
+ int get_decsize()
93
+ {
94
+ return BASE16384_DECBUFSZ;
95
+ }
96
+ """
97
+
98
+ ffibuilder.set_source(
99
+ "pybase16384.backends.cffi._core",
100
+ source,
101
+ sources=[f"./base16384/base14{CPUBIT}.c", "./base16384/file.c"],
102
+ include_dirs=["./base16384"],
103
+ define_macros=macro_base,
104
+ )
105
+
106
+ if __name__ == "__main__":
107
+ ffibuilder.compile()
@@ -0,0 +1,18 @@
1
+ """
2
+ Copyright (c) 2008-2021 synodriver <synodriver@gmail.com>
3
+ """
4
+ from pybase16384.backends.cython._core import (
5
+ _decode,
6
+ _decode_into,
7
+ _encode,
8
+ _encode_into,
9
+ decode_fd,
10
+ decode_file,
11
+ decode_len,
12
+ decode_local_file,
13
+ encode_fd,
14
+ encode_file,
15
+ encode_len,
16
+ encode_local_file,
17
+ is_64bits,
18
+ )