minbit 3.1.0__py3-none-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.
- minbit-3.1.0.data/purelib/minbit/__init__.py +671 -0
- minbit-3.1.0.data/purelib/minbit/__main__.py +89 -0
- minbit-3.1.0.data/purelib/minbit/_lib/minbit.dll +0 -0
- minbit-3.1.0.data/purelib/minbit/py.typed +0 -0
- minbit-3.1.0.dist-info/METADATA +227 -0
- minbit-3.1.0.dist-info/RECORD +9 -0
- minbit-3.1.0.dist-info/WHEEL +5 -0
- minbit-3.1.0.dist-info/licenses/LICENSE +21 -0
- minbit-3.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
"""MinBit — Cross-Platform Minimum-Bit Lossless Compression Library (Format v3).
|
|
2
|
+
|
|
3
|
+
Zero-dependency Python wrapper around the native C11 ``libminbit`` shared library.
|
|
4
|
+
Archives compressed via Python are 100% bit-for-bit identical to archives produced
|
|
5
|
+
by the ``minbit`` CLI and C library across macOS (ARM64/x86-64), Linux, and Windows.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import ctypes
|
|
11
|
+
import ctypes.util
|
|
12
|
+
import os
|
|
13
|
+
import platform
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import BinaryIO, Optional, Union
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"__version__",
|
|
20
|
+
"MINBIT_FORMAT_VERSION",
|
|
21
|
+
"MINBIT_MAX_LEVEL",
|
|
22
|
+
"MINBIT_MIN_BLOCK_SIZE",
|
|
23
|
+
"MINBIT_MAX_BLOCK_SIZE",
|
|
24
|
+
"MinBitError",
|
|
25
|
+
"MinBitInvalidArgError",
|
|
26
|
+
"MinBitCorruptError",
|
|
27
|
+
"MinBitLimitError",
|
|
28
|
+
"MinBitIOError",
|
|
29
|
+
"Stats",
|
|
30
|
+
"HeaderInfo",
|
|
31
|
+
"version",
|
|
32
|
+
"format_version",
|
|
33
|
+
"simd_backend",
|
|
34
|
+
"library_path",
|
|
35
|
+
"compress_bound",
|
|
36
|
+
"inspect_header",
|
|
37
|
+
"compress",
|
|
38
|
+
"decompress",
|
|
39
|
+
"compress_file",
|
|
40
|
+
"decompress_file",
|
|
41
|
+
"verify_file",
|
|
42
|
+
"compress_stream",
|
|
43
|
+
"decompress_stream",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
__version__ = "3.1.0"
|
|
47
|
+
MINBIT_FORMAT_VERSION = 3
|
|
48
|
+
MINBIT_MAX_LEVEL = 4
|
|
49
|
+
MINBIT_MIN_BLOCK_SIZE = 32 * 1024
|
|
50
|
+
MINBIT_MAX_BLOCK_SIZE = 64 * 1024 * 1024
|
|
51
|
+
|
|
52
|
+
BufferLike = Union[bytes, bytearray, memoryview]
|
|
53
|
+
PathLike = Union[str, os.PathLike]
|
|
54
|
+
|
|
55
|
+
# C status codes (must match include/minbit.h)
|
|
56
|
+
_MINBIT_OK = 0
|
|
57
|
+
_MINBIT_ERR_INVALID_ARG = -1
|
|
58
|
+
_MINBIT_ERR_BUFFER_TOO_SMALL = -2
|
|
59
|
+
_MINBIT_ERR_CORRUPT_DATA = -3
|
|
60
|
+
_MINBIT_ERR_OUTPUT_LIMIT = -4
|
|
61
|
+
_MINBIT_ERR_IO = -5
|
|
62
|
+
_MINBIT_ERR_OUT_OF_MEMORY = -6
|
|
63
|
+
_MINBIT_ERR_INTERNAL = -7
|
|
64
|
+
|
|
65
|
+
_TRANSFORM_NAMES = {
|
|
66
|
+
0: "none",
|
|
67
|
+
1: "bcj-arm64",
|
|
68
|
+
2: "bcj-x86",
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class MinBitError(RuntimeError):
|
|
73
|
+
"""Base exception for MinBit compression/decompression errors."""
|
|
74
|
+
|
|
75
|
+
def __init__(self, status: int, message: str) -> None:
|
|
76
|
+
super().__init__(message)
|
|
77
|
+
self.status = status
|
|
78
|
+
self.detail = message
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class MinBitInvalidArgError(MinBitError, ValueError):
|
|
82
|
+
"""Invalid argument supplied to MinBit."""
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class MinBitCorruptError(MinBitError):
|
|
86
|
+
"""Archive failed magic, CRC-16, CRC-32, XXH64, or framing integrity checks."""
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class MinBitLimitError(MinBitError):
|
|
90
|
+
"""Decompressed output exceeded the configured max_output_bytes anti-zip-bomb limit."""
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class MinBitIOError(MinBitError, OSError):
|
|
94
|
+
"""File or stream callback I/O error."""
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass(frozen=True)
|
|
98
|
+
class Stats:
|
|
99
|
+
"""Statistics returned by streaming file and callback operations."""
|
|
100
|
+
|
|
101
|
+
original_size: int
|
|
102
|
+
compressed_size: int
|
|
103
|
+
elapsed_ms: float
|
|
104
|
+
bits_per_byte: float
|
|
105
|
+
stream_xxh64: int
|
|
106
|
+
num_blocks: int
|
|
107
|
+
threads_used: int
|
|
108
|
+
block_size_used: int
|
|
109
|
+
transform: str
|
|
110
|
+
raw_fallback: bool
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def ratio(self) -> float:
|
|
114
|
+
"""Compressed-to-original ratio (e.g. 0.18 means 18% of original size)."""
|
|
115
|
+
return (self.compressed_size / self.original_size) if self.original_size > 0 else 0.0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass(frozen=True)
|
|
119
|
+
class HeaderInfo:
|
|
120
|
+
"""Validated header metadata parsed without decompressing any payload."""
|
|
121
|
+
|
|
122
|
+
format_version: int
|
|
123
|
+
level: int
|
|
124
|
+
max_block_size: int
|
|
125
|
+
original_size_hint: int
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# ---------------------------------------------------------------------------
|
|
129
|
+
# C ABI Structs & Callback Signatures (exact match to include/minbit.h)
|
|
130
|
+
# ---------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class _CCompressOptions(ctypes.Structure):
|
|
134
|
+
_fields_ = [
|
|
135
|
+
("level", ctypes.c_int),
|
|
136
|
+
("threads", ctypes.c_int),
|
|
137
|
+
("block_size", ctypes.c_uint32),
|
|
138
|
+
("auto_transform", ctypes.c_int),
|
|
139
|
+
("allow_raw_fallback", ctypes.c_int),
|
|
140
|
+
]
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class _CDecompressOptions(ctypes.Structure):
|
|
144
|
+
_fields_ = [
|
|
145
|
+
("threads", ctypes.c_int),
|
|
146
|
+
("max_output_bytes", ctypes.c_uint64),
|
|
147
|
+
("reject_trailing_bytes", ctypes.c_int),
|
|
148
|
+
]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class _CStats(ctypes.Structure):
|
|
152
|
+
_fields_ = [
|
|
153
|
+
("original_size", ctypes.c_uint64),
|
|
154
|
+
("compressed_size", ctypes.c_uint64),
|
|
155
|
+
("elapsed_ms", ctypes.c_double),
|
|
156
|
+
("bits_per_byte", ctypes.c_double),
|
|
157
|
+
("stream_xxh64", ctypes.c_uint64),
|
|
158
|
+
("num_blocks", ctypes.c_uint32),
|
|
159
|
+
("threads_used", ctypes.c_uint32),
|
|
160
|
+
("block_size_used", ctypes.c_uint32),
|
|
161
|
+
("transform", ctypes.c_int),
|
|
162
|
+
("raw_fallback", ctypes.c_int),
|
|
163
|
+
]
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class _CHeaderInfo(ctypes.Structure):
|
|
167
|
+
_fields_ = [
|
|
168
|
+
("format_version", ctypes.c_uint32),
|
|
169
|
+
("level", ctypes.c_int),
|
|
170
|
+
("max_block_size", ctypes.c_uint32),
|
|
171
|
+
("original_size_hint", ctypes.c_uint64),
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
_READ_CB = ctypes.CFUNCTYPE(ctypes.c_int64, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t)
|
|
176
|
+
_WRITE_CB = ctypes.CFUNCTYPE(ctypes.c_int64, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _candidate_lib_names() -> list[str]:
|
|
180
|
+
sysname = platform.system()
|
|
181
|
+
if sysname == "Darwin":
|
|
182
|
+
return ["libminbit.dylib", "libminbit.3.dylib"]
|
|
183
|
+
if sysname == "Windows":
|
|
184
|
+
return ["minbit.dll", "libminbit.dll"]
|
|
185
|
+
return ["libminbit.so", "libminbit.so.3"]
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _locate_library() -> str:
|
|
189
|
+
env_path = os.environ.get("MINBIT_LIB_PATH")
|
|
190
|
+
if env_path:
|
|
191
|
+
if os.path.isfile(env_path):
|
|
192
|
+
return env_path
|
|
193
|
+
raise OSError(f"MINBIT_LIB_PATH points to non-existent file: {env_path}")
|
|
194
|
+
|
|
195
|
+
names = _candidate_lib_names()
|
|
196
|
+
pkg_dir = Path(__file__).resolve().parent
|
|
197
|
+
search_dirs = [
|
|
198
|
+
pkg_dir / "_lib",
|
|
199
|
+
pkg_dir,
|
|
200
|
+
pkg_dir.parent.parent / "build",
|
|
201
|
+
pkg_dir.parent.parent / "build" / "Release",
|
|
202
|
+
pkg_dir.parent.parent / "build" / "Debug",
|
|
203
|
+
Path.cwd() / "build",
|
|
204
|
+
Path.cwd() / "build" / "Release",
|
|
205
|
+
]
|
|
206
|
+
for d in search_dirs:
|
|
207
|
+
for name in names:
|
|
208
|
+
cand = d / name
|
|
209
|
+
if cand.is_file():
|
|
210
|
+
return str(cand)
|
|
211
|
+
|
|
212
|
+
found = ctypes.util.find_library("minbit")
|
|
213
|
+
if found:
|
|
214
|
+
return found
|
|
215
|
+
|
|
216
|
+
raise OSError(
|
|
217
|
+
"Could not locate native libminbit shared library. "
|
|
218
|
+
"Build it with `make lib` or `cmake --build build`, or set MINBIT_LIB_PATH."
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
_LIB_PATH: Optional[str] = None
|
|
223
|
+
_LIB: Optional[ctypes.CDLL] = None
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _get_lib() -> ctypes.CDLL:
|
|
227
|
+
global _LIB, _LIB_PATH
|
|
228
|
+
if _LIB is not None:
|
|
229
|
+
return _LIB
|
|
230
|
+
|
|
231
|
+
path = _locate_library()
|
|
232
|
+
lib = ctypes.CDLL(path)
|
|
233
|
+
|
|
234
|
+
lib.minbit_version_string.argtypes = []
|
|
235
|
+
lib.minbit_version_string.restype = ctypes.c_char_p
|
|
236
|
+
|
|
237
|
+
lib.minbit_format_version.argtypes = []
|
|
238
|
+
lib.minbit_format_version.restype = ctypes.c_uint32
|
|
239
|
+
|
|
240
|
+
lib.minbit_simd_backend.argtypes = []
|
|
241
|
+
lib.minbit_simd_backend.restype = ctypes.c_char_p
|
|
242
|
+
|
|
243
|
+
lib.minbit_status_string.argtypes = [ctypes.c_int]
|
|
244
|
+
lib.minbit_status_string.restype = ctypes.c_char_p
|
|
245
|
+
|
|
246
|
+
lib.minbit_last_error.argtypes = []
|
|
247
|
+
lib.minbit_last_error.restype = ctypes.c_char_p
|
|
248
|
+
|
|
249
|
+
lib.minbit_compress_bound.argtypes = [ctypes.c_size_t, ctypes.c_uint32]
|
|
250
|
+
lib.minbit_compress_bound.restype = ctypes.c_size_t
|
|
251
|
+
|
|
252
|
+
lib.minbit_inspect_header.argtypes = [
|
|
253
|
+
ctypes.c_void_p,
|
|
254
|
+
ctypes.c_size_t,
|
|
255
|
+
ctypes.POINTER(_CHeaderInfo),
|
|
256
|
+
]
|
|
257
|
+
lib.minbit_inspect_header.restype = ctypes.c_int
|
|
258
|
+
|
|
259
|
+
lib.minbit_compress_alloc.argtypes = [
|
|
260
|
+
ctypes.c_void_p,
|
|
261
|
+
ctypes.c_size_t,
|
|
262
|
+
ctypes.POINTER(ctypes.c_void_p),
|
|
263
|
+
ctypes.POINTER(ctypes.c_size_t),
|
|
264
|
+
ctypes.POINTER(_CCompressOptions),
|
|
265
|
+
]
|
|
266
|
+
lib.minbit_compress_alloc.restype = ctypes.c_int
|
|
267
|
+
|
|
268
|
+
lib.minbit_decompress_alloc.argtypes = [
|
|
269
|
+
ctypes.c_void_p,
|
|
270
|
+
ctypes.c_size_t,
|
|
271
|
+
ctypes.POINTER(ctypes.c_void_p),
|
|
272
|
+
ctypes.POINTER(ctypes.c_size_t),
|
|
273
|
+
ctypes.POINTER(_CDecompressOptions),
|
|
274
|
+
]
|
|
275
|
+
lib.minbit_decompress_alloc.restype = ctypes.c_int
|
|
276
|
+
|
|
277
|
+
lib.minbit_free.argtypes = [ctypes.c_void_p]
|
|
278
|
+
lib.minbit_free.restype = None
|
|
279
|
+
|
|
280
|
+
lib.minbit_compress_file.argtypes = [
|
|
281
|
+
ctypes.c_char_p,
|
|
282
|
+
ctypes.c_char_p,
|
|
283
|
+
ctypes.POINTER(_CCompressOptions),
|
|
284
|
+
ctypes.POINTER(_CStats),
|
|
285
|
+
]
|
|
286
|
+
lib.minbit_compress_file.restype = ctypes.c_int
|
|
287
|
+
|
|
288
|
+
lib.minbit_decompress_file.argtypes = [
|
|
289
|
+
ctypes.c_char_p,
|
|
290
|
+
ctypes.c_char_p,
|
|
291
|
+
ctypes.POINTER(_CDecompressOptions),
|
|
292
|
+
ctypes.POINTER(_CStats),
|
|
293
|
+
]
|
|
294
|
+
lib.minbit_decompress_file.restype = ctypes.c_int
|
|
295
|
+
|
|
296
|
+
lib.minbit_verify_file.argtypes = [
|
|
297
|
+
ctypes.c_char_p,
|
|
298
|
+
ctypes.POINTER(_CDecompressOptions),
|
|
299
|
+
ctypes.POINTER(_CStats),
|
|
300
|
+
]
|
|
301
|
+
lib.minbit_verify_file.restype = ctypes.c_int
|
|
302
|
+
|
|
303
|
+
lib.minbit_compress_stream_cb.argtypes = [
|
|
304
|
+
_READ_CB,
|
|
305
|
+
ctypes.c_void_p,
|
|
306
|
+
_WRITE_CB,
|
|
307
|
+
ctypes.c_void_p,
|
|
308
|
+
ctypes.POINTER(_CCompressOptions),
|
|
309
|
+
ctypes.POINTER(_CStats),
|
|
310
|
+
]
|
|
311
|
+
lib.minbit_compress_stream_cb.restype = ctypes.c_int
|
|
312
|
+
|
|
313
|
+
lib.minbit_decompress_stream_cb.argtypes = [
|
|
314
|
+
_READ_CB,
|
|
315
|
+
ctypes.c_void_p,
|
|
316
|
+
_WRITE_CB,
|
|
317
|
+
ctypes.c_void_p,
|
|
318
|
+
ctypes.POINTER(_CDecompressOptions),
|
|
319
|
+
ctypes.POINTER(_CStats),
|
|
320
|
+
]
|
|
321
|
+
lib.minbit_decompress_stream_cb.restype = ctypes.c_int
|
|
322
|
+
|
|
323
|
+
_LIB = lib
|
|
324
|
+
_LIB_PATH = path
|
|
325
|
+
return lib
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _raise_for_status(lib: ctypes.CDLL, status: int) -> None:
|
|
329
|
+
if status == _MINBIT_OK:
|
|
330
|
+
return
|
|
331
|
+
detail_b = lib.minbit_last_error()
|
|
332
|
+
detail = detail_b.decode("utf-8", errors="replace") if detail_b else ""
|
|
333
|
+
if not detail:
|
|
334
|
+
st_b = lib.minbit_status_string(status)
|
|
335
|
+
detail = st_b.decode("utf-8", errors="replace") if st_b else f"MinBit error {status}"
|
|
336
|
+
if status == _MINBIT_ERR_INVALID_ARG:
|
|
337
|
+
raise MinBitInvalidArgError(status, detail)
|
|
338
|
+
if status == _MINBIT_ERR_CORRUPT_DATA:
|
|
339
|
+
raise MinBitCorruptError(status, detail)
|
|
340
|
+
if status == _MINBIT_ERR_OUTPUT_LIMIT:
|
|
341
|
+
raise MinBitLimitError(status, detail)
|
|
342
|
+
if status == _MINBIT_ERR_IO:
|
|
343
|
+
raise MinBitIOError(status, detail)
|
|
344
|
+
if status == _MINBIT_ERR_OUT_OF_MEMORY:
|
|
345
|
+
raise MemoryError(detail)
|
|
346
|
+
raise MinBitError(status, detail)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def _as_c_buffer(data: BufferLike) -> tuple[ctypes.c_void_p, int, object]:
|
|
350
|
+
"""Return (void_ptr, byte_len, keepalive_owner) for any bytes-like buffer."""
|
|
351
|
+
if isinstance(data, bytes):
|
|
352
|
+
if len(data) == 0:
|
|
353
|
+
return ctypes.c_void_p(0), 0, data
|
|
354
|
+
c_ptr = ctypes.cast(ctypes.c_char_p(data), ctypes.c_void_p)
|
|
355
|
+
return c_ptr, len(data), data
|
|
356
|
+
mv = memoryview(data)
|
|
357
|
+
if mv.nbytes == 0:
|
|
358
|
+
return ctypes.c_void_p(0), 0, mv
|
|
359
|
+
if not mv.c_contiguous or mv.readonly:
|
|
360
|
+
raw = mv.tobytes()
|
|
361
|
+
c_ptr = ctypes.cast(ctypes.c_char_p(raw), ctypes.c_void_p)
|
|
362
|
+
return c_ptr, len(raw), raw
|
|
363
|
+
arr = (ctypes.c_uint8 * mv.nbytes).from_buffer(mv)
|
|
364
|
+
return ctypes.c_void_p(ctypes.addressof(arr)), mv.nbytes, (mv, arr)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _make_compress_opt(
|
|
368
|
+
level: int,
|
|
369
|
+
threads: int,
|
|
370
|
+
block_size: int,
|
|
371
|
+
auto_transform: bool,
|
|
372
|
+
allow_raw_fallback: bool,
|
|
373
|
+
) -> _CCompressOptions:
|
|
374
|
+
if not (0 <= level <= MINBIT_MAX_LEVEL):
|
|
375
|
+
raise MinBitInvalidArgError(_MINBIT_ERR_INVALID_ARG, f"level must be 0..{MINBIT_MAX_LEVEL}, got {level}")
|
|
376
|
+
if threads < 0:
|
|
377
|
+
raise MinBitInvalidArgError(_MINBIT_ERR_INVALID_ARG, f"threads must be >= 0, got {threads}")
|
|
378
|
+
if block_size < 0 or block_size > MINBIT_MAX_BLOCK_SIZE:
|
|
379
|
+
raise MinBitInvalidArgError(
|
|
380
|
+
_MINBIT_ERR_INVALID_ARG,
|
|
381
|
+
f"block_size must be 0 or <= {MINBIT_MAX_BLOCK_SIZE}, got {block_size}",
|
|
382
|
+
)
|
|
383
|
+
return _CCompressOptions(
|
|
384
|
+
level=level,
|
|
385
|
+
threads=threads,
|
|
386
|
+
block_size=block_size,
|
|
387
|
+
auto_transform=1 if auto_transform else 0,
|
|
388
|
+
allow_raw_fallback=1 if allow_raw_fallback else 0,
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def _make_decompress_opt(
|
|
393
|
+
threads: int,
|
|
394
|
+
max_output_bytes: int,
|
|
395
|
+
reject_trailing_bytes: bool,
|
|
396
|
+
) -> _CDecompressOptions:
|
|
397
|
+
if threads < 0:
|
|
398
|
+
raise MinBitInvalidArgError(_MINBIT_ERR_INVALID_ARG, f"threads must be >= 0, got {threads}")
|
|
399
|
+
if max_output_bytes < 0:
|
|
400
|
+
raise MinBitInvalidArgError(
|
|
401
|
+
_MINBIT_ERR_INVALID_ARG, f"max_output_bytes must be >= 0, got {max_output_bytes}"
|
|
402
|
+
)
|
|
403
|
+
return _CDecompressOptions(
|
|
404
|
+
threads=threads,
|
|
405
|
+
max_output_bytes=max_output_bytes,
|
|
406
|
+
reject_trailing_bytes=1 if reject_trailing_bytes else 0,
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def _to_stats(st: _CStats) -> Stats:
|
|
411
|
+
return Stats(
|
|
412
|
+
original_size=int(st.original_size),
|
|
413
|
+
compressed_size=int(st.compressed_size),
|
|
414
|
+
elapsed_ms=float(st.elapsed_ms),
|
|
415
|
+
bits_per_byte=float(st.bits_per_byte),
|
|
416
|
+
stream_xxh64=int(st.stream_xxh64),
|
|
417
|
+
num_blocks=int(st.num_blocks),
|
|
418
|
+
threads_used=int(st.threads_used),
|
|
419
|
+
block_size_used=int(st.block_size_used),
|
|
420
|
+
transform=_TRANSFORM_NAMES.get(int(st.transform), "none"),
|
|
421
|
+
raw_fallback=bool(st.raw_fallback),
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
# ---------------------------------------------------------------------------
|
|
426
|
+
# Public API
|
|
427
|
+
# ---------------------------------------------------------------------------
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def version() -> str:
|
|
431
|
+
"""Return the native libminbit version string (e.g. '3.0.0')."""
|
|
432
|
+
return _get_lib().minbit_version_string().decode("ascii")
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def format_version() -> int:
|
|
436
|
+
"""Return the native .mbit format version (3)."""
|
|
437
|
+
return int(_get_lib().minbit_format_version())
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def simd_backend() -> str:
|
|
441
|
+
"""Return the active SIMD backend name compiled into libminbit."""
|
|
442
|
+
return _get_lib().minbit_simd_backend().decode("ascii")
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def library_path() -> str:
|
|
446
|
+
"""Return the resolved path to the loaded native shared library."""
|
|
447
|
+
_get_lib()
|
|
448
|
+
assert _LIB_PATH is not None
|
|
449
|
+
return _LIB_PATH
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def compress_bound(src_size: int, block_size: int = 0) -> int:
|
|
453
|
+
"""Return the conservative upper bound on compressed size for src_size bytes."""
|
|
454
|
+
if src_size < 0:
|
|
455
|
+
raise MinBitInvalidArgError(_MINBIT_ERR_INVALID_ARG, "src_size must be >= 0")
|
|
456
|
+
return int(_get_lib().minbit_compress_bound(src_size, block_size))
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
def inspect_header(data: BufferLike) -> HeaderInfo:
|
|
460
|
+
"""Validate and parse the leading .mbit header without decompressing payload."""
|
|
461
|
+
lib = _get_lib()
|
|
462
|
+
ptr, size, _keep = _as_c_buffer(data)
|
|
463
|
+
info = _CHeaderInfo()
|
|
464
|
+
st = lib.minbit_inspect_header(ptr, size, ctypes.byref(info))
|
|
465
|
+
_raise_for_status(lib, st)
|
|
466
|
+
return HeaderInfo(
|
|
467
|
+
format_version=int(info.format_version),
|
|
468
|
+
level=int(info.level),
|
|
469
|
+
max_block_size=int(info.max_block_size),
|
|
470
|
+
original_size_hint=int(info.original_size_hint),
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def compress(
|
|
475
|
+
data: BufferLike,
|
|
476
|
+
*,
|
|
477
|
+
level: int = 2,
|
|
478
|
+
threads: int = 1,
|
|
479
|
+
block_size: int = 0,
|
|
480
|
+
auto_transform: bool = True,
|
|
481
|
+
allow_raw_fallback: bool = True,
|
|
482
|
+
) -> bytes:
|
|
483
|
+
"""Compress a bytes-like object into a .mbit v3 byte string."""
|
|
484
|
+
lib = _get_lib()
|
|
485
|
+
copt = _make_compress_opt(level, threads, block_size, auto_transform, allow_raw_fallback)
|
|
486
|
+
ptr, size, _keep = _as_c_buffer(data)
|
|
487
|
+
out_ptr = ctypes.c_void_p(0)
|
|
488
|
+
out_sz = ctypes.c_size_t(0)
|
|
489
|
+
st = lib.minbit_compress_alloc(
|
|
490
|
+
ptr, size, ctypes.byref(out_ptr), ctypes.byref(out_sz), ctypes.byref(copt)
|
|
491
|
+
)
|
|
492
|
+
if st != _MINBIT_OK:
|
|
493
|
+
_raise_for_status(lib, st)
|
|
494
|
+
try:
|
|
495
|
+
return ctypes.string_at(out_ptr.value, out_sz.value) if out_sz.value > 0 else b""
|
|
496
|
+
finally:
|
|
497
|
+
lib.minbit_free(out_ptr)
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
def decompress(
|
|
501
|
+
data: BufferLike,
|
|
502
|
+
*,
|
|
503
|
+
threads: int = 0,
|
|
504
|
+
max_output_bytes: int = 0,
|
|
505
|
+
reject_trailing_bytes: bool = True,
|
|
506
|
+
) -> bytes:
|
|
507
|
+
"""Decompress a .mbit archive (v1, v2, or v3) back into original bytes."""
|
|
508
|
+
lib = _get_lib()
|
|
509
|
+
dopt = _make_decompress_opt(threads, max_output_bytes, reject_trailing_bytes)
|
|
510
|
+
ptr, size, _keep = _as_c_buffer(data)
|
|
511
|
+
out_ptr = ctypes.c_void_p(0)
|
|
512
|
+
out_sz = ctypes.c_size_t(0)
|
|
513
|
+
st = lib.minbit_decompress_alloc(
|
|
514
|
+
ptr, size, ctypes.byref(out_ptr), ctypes.byref(out_sz), ctypes.byref(dopt)
|
|
515
|
+
)
|
|
516
|
+
if st != _MINBIT_OK:
|
|
517
|
+
_raise_for_status(lib, st)
|
|
518
|
+
try:
|
|
519
|
+
return ctypes.string_at(out_ptr.value, out_sz.value) if out_sz.value > 0 else b""
|
|
520
|
+
finally:
|
|
521
|
+
lib.minbit_free(out_ptr)
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
def compress_file(
|
|
525
|
+
src_path: PathLike,
|
|
526
|
+
dst_path: PathLike,
|
|
527
|
+
*,
|
|
528
|
+
level: int = 2,
|
|
529
|
+
threads: int = 0,
|
|
530
|
+
block_size: int = 0,
|
|
531
|
+
auto_transform: bool = True,
|
|
532
|
+
allow_raw_fallback: bool = True,
|
|
533
|
+
) -> Stats:
|
|
534
|
+
"""Stream-compress a file on disk using bounded RAM."""
|
|
535
|
+
lib = _get_lib()
|
|
536
|
+
copt = _make_compress_opt(level, threads, block_size, auto_transform, allow_raw_fallback)
|
|
537
|
+
st_out = _CStats()
|
|
538
|
+
rc = lib.minbit_compress_file(
|
|
539
|
+
os.fsencode(os.fspath(src_path)),
|
|
540
|
+
os.fsencode(os.fspath(dst_path)),
|
|
541
|
+
ctypes.byref(copt),
|
|
542
|
+
ctypes.byref(st_out),
|
|
543
|
+
)
|
|
544
|
+
_raise_for_status(lib, rc)
|
|
545
|
+
return _to_stats(st_out)
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
def decompress_file(
|
|
549
|
+
src_path: PathLike,
|
|
550
|
+
dst_path: PathLike,
|
|
551
|
+
*,
|
|
552
|
+
threads: int = 0,
|
|
553
|
+
max_output_bytes: int = 0,
|
|
554
|
+
reject_trailing_bytes: bool = True,
|
|
555
|
+
) -> Stats:
|
|
556
|
+
"""Stream-decompress a .mbit file on disk using bounded RAM."""
|
|
557
|
+
lib = _get_lib()
|
|
558
|
+
dopt = _make_decompress_opt(threads, max_output_bytes, reject_trailing_bytes)
|
|
559
|
+
st_out = _CStats()
|
|
560
|
+
rc = lib.minbit_decompress_file(
|
|
561
|
+
os.fsencode(os.fspath(src_path)),
|
|
562
|
+
os.fsencode(os.fspath(dst_path)),
|
|
563
|
+
ctypes.byref(dopt),
|
|
564
|
+
ctypes.byref(st_out),
|
|
565
|
+
)
|
|
566
|
+
_raise_for_status(lib, rc)
|
|
567
|
+
return _to_stats(st_out)
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def verify_file(
|
|
571
|
+
src_path: PathLike,
|
|
572
|
+
*,
|
|
573
|
+
threads: int = 0,
|
|
574
|
+
max_output_bytes: int = 0,
|
|
575
|
+
reject_trailing_bytes: bool = True,
|
|
576
|
+
) -> Stats:
|
|
577
|
+
"""Verify full integrity (CRC-16, per-block CRC-32, XXH64) of a .mbit archive."""
|
|
578
|
+
lib = _get_lib()
|
|
579
|
+
dopt = _make_decompress_opt(threads, max_output_bytes, reject_trailing_bytes)
|
|
580
|
+
st_out = _CStats()
|
|
581
|
+
rc = lib.minbit_verify_file(
|
|
582
|
+
os.fsencode(os.fspath(src_path)),
|
|
583
|
+
ctypes.byref(dopt),
|
|
584
|
+
ctypes.byref(st_out),
|
|
585
|
+
)
|
|
586
|
+
_raise_for_status(lib, rc)
|
|
587
|
+
return _to_stats(st_out)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def _make_stream_callbacks(src: BinaryIO, dst: BinaryIO):
|
|
591
|
+
cb_exc: list[Optional[BaseException]] = [None]
|
|
592
|
+
|
|
593
|
+
@_READ_CB
|
|
594
|
+
def _read(_ctx: int, buf_ptr: int, max_bytes: int) -> int:
|
|
595
|
+
try:
|
|
596
|
+
chunk = src.read(max_bytes)
|
|
597
|
+
if not chunk:
|
|
598
|
+
return 0
|
|
599
|
+
if not isinstance(chunk, (bytes, bytearray, memoryview)):
|
|
600
|
+
raise TypeError(f"stream read() must return bytes-like object, got {type(chunk).__name__}")
|
|
601
|
+
n = len(chunk)
|
|
602
|
+
if n > max_bytes:
|
|
603
|
+
raise ValueError(f"stream read({max_bytes}) returned {n} bytes (> max_bytes)")
|
|
604
|
+
if isinstance(chunk, bytes):
|
|
605
|
+
ctypes.memmove(buf_ptr, chunk, n)
|
|
606
|
+
else:
|
|
607
|
+
ctypes.memmove(buf_ptr, bytes(chunk), n)
|
|
608
|
+
return n
|
|
609
|
+
except BaseException as exc: # noqa: BLE001
|
|
610
|
+
cb_exc[0] = exc
|
|
611
|
+
return -1
|
|
612
|
+
|
|
613
|
+
@_WRITE_CB
|
|
614
|
+
def _write(_ctx: int, buf_ptr: int, num_bytes: int) -> int:
|
|
615
|
+
try:
|
|
616
|
+
data = ctypes.string_at(buf_ptr, num_bytes)
|
|
617
|
+
written = dst.write(data)
|
|
618
|
+
if written is None:
|
|
619
|
+
return num_bytes
|
|
620
|
+
return int(written)
|
|
621
|
+
except BaseException as exc: # noqa: BLE001
|
|
622
|
+
cb_exc[0] = exc
|
|
623
|
+
return -1
|
|
624
|
+
|
|
625
|
+
return _read, _write, cb_exc
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
def compress_stream(
|
|
629
|
+
src: BinaryIO,
|
|
630
|
+
dst: BinaryIO,
|
|
631
|
+
*,
|
|
632
|
+
level: int = 2,
|
|
633
|
+
threads: int = 0,
|
|
634
|
+
block_size: int = 0,
|
|
635
|
+
auto_transform: bool = True,
|
|
636
|
+
allow_raw_fallback: bool = True,
|
|
637
|
+
) -> Stats:
|
|
638
|
+
"""Stream-compress from any readable binary file-like object to a writable one."""
|
|
639
|
+
lib = _get_lib()
|
|
640
|
+
copt = _make_compress_opt(level, threads, block_size, auto_transform, allow_raw_fallback)
|
|
641
|
+
read_cb, write_cb, cb_exc = _make_stream_callbacks(src, dst)
|
|
642
|
+
st_out = _CStats()
|
|
643
|
+
rc = lib.minbit_compress_stream_cb(
|
|
644
|
+
read_cb, None, write_cb, None, ctypes.byref(copt), ctypes.byref(st_out)
|
|
645
|
+
)
|
|
646
|
+
if cb_exc[0] is not None:
|
|
647
|
+
raise cb_exc[0]
|
|
648
|
+
_raise_for_status(lib, rc)
|
|
649
|
+
return _to_stats(st_out)
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
def decompress_stream(
|
|
653
|
+
src: BinaryIO,
|
|
654
|
+
dst: BinaryIO,
|
|
655
|
+
*,
|
|
656
|
+
threads: int = 0,
|
|
657
|
+
max_output_bytes: int = 0,
|
|
658
|
+
reject_trailing_bytes: bool = True,
|
|
659
|
+
) -> Stats:
|
|
660
|
+
"""Stream-decompress from any readable binary file-like object to a writable one."""
|
|
661
|
+
lib = _get_lib()
|
|
662
|
+
dopt = _make_decompress_opt(threads, max_output_bytes, reject_trailing_bytes)
|
|
663
|
+
read_cb, write_cb, cb_exc = _make_stream_callbacks(src, dst)
|
|
664
|
+
st_out = _CStats()
|
|
665
|
+
rc = lib.minbit_decompress_stream_cb(
|
|
666
|
+
read_cb, None, write_cb, None, ctypes.byref(dopt), ctypes.byref(st_out)
|
|
667
|
+
)
|
|
668
|
+
if cb_exc[0] is not None:
|
|
669
|
+
raise cb_exc[0]
|
|
670
|
+
_raise_for_status(lib, rc)
|
|
671
|
+
return _to_stats(st_out)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Command-line interface via `python -m minbit`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
import minbit
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def main(argv: list[str] | None = None) -> int:
|
|
12
|
+
ap = argparse.ArgumentParser(
|
|
13
|
+
prog="python -m minbit",
|
|
14
|
+
description="MinBit v3 lossless compressor (Python native-library CLI)",
|
|
15
|
+
)
|
|
16
|
+
ap.add_argument("-V", "--version", action="store_true", help="Print version and SIMD backend")
|
|
17
|
+
sub = ap.add_subparsers(dest="cmd")
|
|
18
|
+
|
|
19
|
+
pc = sub.add_parser("c", help="Compress file")
|
|
20
|
+
pc.add_argument("src", help="Input file path")
|
|
21
|
+
pc.add_argument("dst", help="Output .mbit path")
|
|
22
|
+
pc.add_argument("-l", "--level", type=int, default=2, choices=range(0, 5))
|
|
23
|
+
pc.add_argument("-T", "--threads", type=int, default=0)
|
|
24
|
+
pc.add_argument("--block-kb", type=int, default=0)
|
|
25
|
+
|
|
26
|
+
pd = sub.add_parser("d", help="Decompress .mbit file")
|
|
27
|
+
pd.add_argument("src", help="Input .mbit path")
|
|
28
|
+
pd.add_argument("dst", help="Output restored file path")
|
|
29
|
+
pd.add_argument("-T", "--threads", type=int, default=0)
|
|
30
|
+
pd.add_argument("--max-out-mb", type=int, default=0)
|
|
31
|
+
|
|
32
|
+
pt = sub.add_parser("t", help="Verify .mbit archive integrity")
|
|
33
|
+
pt.add_argument("src", help="Input .mbit path")
|
|
34
|
+
pt.add_argument("-T", "--threads", type=int, default=0)
|
|
35
|
+
pt.add_argument("--max-out-mb", type=int, default=0)
|
|
36
|
+
|
|
37
|
+
args = ap.parse_args(argv)
|
|
38
|
+
if args.version:
|
|
39
|
+
print(f"minbit {minbit.version()} (format v{minbit.format_version()}, simd={minbit.simd_backend()})")
|
|
40
|
+
return 0
|
|
41
|
+
|
|
42
|
+
try:
|
|
43
|
+
if args.cmd == "c":
|
|
44
|
+
st = minbit.compress_file(
|
|
45
|
+
args.src,
|
|
46
|
+
args.dst,
|
|
47
|
+
level=args.level,
|
|
48
|
+
threads=args.threads,
|
|
49
|
+
block_size=args.block_kb * 1024 if args.block_kb else 0,
|
|
50
|
+
)
|
|
51
|
+
print(
|
|
52
|
+
f"Compressed {st.original_size:,d} -> {st.compressed_size:,d} B "
|
|
53
|
+
f"({100.0 * st.ratio:.2f}%, {st.bits_per_byte:.4f} bpb, L{args.level}, "
|
|
54
|
+
f"blocks={st.num_blocks}, T={st.threads_used})"
|
|
55
|
+
)
|
|
56
|
+
return 0
|
|
57
|
+
if args.cmd == "d":
|
|
58
|
+
st = minbit.decompress_file(
|
|
59
|
+
args.src,
|
|
60
|
+
args.dst,
|
|
61
|
+
threads=args.threads,
|
|
62
|
+
max_output_bytes=args.max_out_mb * 1024 * 1024 if args.max_out_mb else 0,
|
|
63
|
+
)
|
|
64
|
+
print(
|
|
65
|
+
f"Decompressed {st.compressed_size:,d} -> {st.original_size:,d} B "
|
|
66
|
+
f"(XXH64=0x{st.stream_xxh64:016X} OK)"
|
|
67
|
+
)
|
|
68
|
+
return 0
|
|
69
|
+
if args.cmd == "t":
|
|
70
|
+
st = minbit.verify_file(
|
|
71
|
+
args.src,
|
|
72
|
+
threads=args.threads,
|
|
73
|
+
max_output_bytes=args.max_out_mb * 1024 * 1024 if args.max_out_mb else 0,
|
|
74
|
+
)
|
|
75
|
+
print(
|
|
76
|
+
f"OK: {args.src} ({st.compressed_size:,d} -> {st.original_size:,d} B, "
|
|
77
|
+
f"{st.num_blocks} blocks, XXH64=0x{st.stream_xxh64:016X})"
|
|
78
|
+
)
|
|
79
|
+
return 0
|
|
80
|
+
except minbit.MinBitError as exc:
|
|
81
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
82
|
+
return 2
|
|
83
|
+
|
|
84
|
+
ap.print_help()
|
|
85
|
+
return 1
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if __name__ == "__main__":
|
|
89
|
+
raise SystemExit(main())
|
|
Binary file
|
|
File without changes
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: minbit
|
|
3
|
+
Version: 3.1.0
|
|
4
|
+
Summary: Cross-platform minimum-bit lossless compression library (Bitwise Context Mixing, format v3)
|
|
5
|
+
Author: NeiH4207
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 NeiH4207
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/NeiH4207/minbit
|
|
29
|
+
Project-URL: Repository, https://github.com/NeiH4207/minbit
|
|
30
|
+
Project-URL: Issues, https://github.com/NeiH4207/minbit/issues
|
|
31
|
+
Project-URL: Changelog, https://github.com/NeiH4207/minbit/blob/main/CHANGELOG.md
|
|
32
|
+
Keywords: compression,lossless,context-mixing,paq,archiving,simd
|
|
33
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
34
|
+
Classifier: Intended Audience :: Developers
|
|
35
|
+
Classifier: Intended Audience :: Science/Research
|
|
36
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
37
|
+
Classifier: Operating System :: MacOS
|
|
38
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
39
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
40
|
+
Classifier: Programming Language :: C
|
|
41
|
+
Classifier: Programming Language :: C++
|
|
42
|
+
Classifier: Programming Language :: Python :: 3
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
46
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
47
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
48
|
+
Classifier: Topic :: System :: Archiving :: Compression
|
|
49
|
+
Classifier: Typing :: Typed
|
|
50
|
+
Requires-Python: >=3.9
|
|
51
|
+
Description-Content-Type: text/markdown
|
|
52
|
+
License-File: LICENSE
|
|
53
|
+
Dynamic: license-file
|
|
54
|
+
|
|
55
|
+
# MinBit — Cross-Platform Minimum-Bit Lossless Compressor
|
|
56
|
+
|
|
57
|
+
[](https://github.com/NeiH4207/minbit/actions/workflows/ci.yml)
|
|
58
|
+
[](LICENSE)
|
|
59
|
+
[](include/minbit.h)
|
|
60
|
+
[](FORMAT.md)
|
|
61
|
+
|
|
62
|
+
**MinBit** is a zero-dependency, cross-platform (**macOS**, **Linux**, **Windows**) C++20/C11/Python lossless compression engine based on **Bitwise Context Mixing** with adaptive 16-bit/2D spatial modeling, structural markup/lexical modeling, and bit-exact cross-architecture SIMD acceleration.
|
|
63
|
+
|
|
64
|
+
Across **all 5 international standard benchmarks** — **Silesia (211.94 MB)**, **Canterbury (2.81 MB)**, **Calgary (3.25 MB)**, **`enwik8_10m` (10 MB)**, and **full `enwik8` (100 MB)** — MinBit beats every standard industry compressor (`gzip -9`, `bzip2 -9`, `zstd --ultra -22`, `brotli -11`, `xz -9e`, `7z-PPMd`) on **100% of files**, and achieves **#1 overall** ahead of **`zpaq -m5`** on all 5 benchmarks!
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Installation
|
|
69
|
+
|
|
70
|
+
### Homebrew (macOS & Linux)
|
|
71
|
+
```bash
|
|
72
|
+
brew tap NeiH4207/minbit https://github.com/NeiH4207/minbit.git
|
|
73
|
+
brew install NeiH4207/minbit/minbit
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Python (`pip`)
|
|
77
|
+
```bash
|
|
78
|
+
# Install from source / GitHub (or PyPI once published)
|
|
79
|
+
pip install git+https://github.com/NeiH4207/minbit.git
|
|
80
|
+
# Verify bundled native library & SIMD backend:
|
|
81
|
+
python -m minbit --version
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Prebuilt Binaries (macOS ARM64, Linux x86-64, Windows x64)
|
|
85
|
+
Download CLI + `libminbit` + `include/minbit.h` directly from [GitHub Releases](https://github.com/NeiH4207/minbit/releases).
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## Key Features
|
|
90
|
+
|
|
91
|
+
- **100% Bit-Exact Cross-Platform Archives**: An archive compressed on **Windows (x86-64 AVX2/SSE2)**, **Linux (GCC/Clang x86-64/ARM64)**, or **macOS (Apple Silicon ARM64 NEON)** is guaranteed **bit-for-bit identical** and cross-decodable everywhere:
|
|
92
|
+
- Unified SIMD [`LogisticMixer`](src/core/mixer.hpp) (`AVX2 _mm256_mulhrs_epi16` == `ARM64 NEON vqrdmulhq_s16` == `SSSE3 _mm_mulhrs_epi16` == scalar `(x * err2 + 16384) >> 15`).
|
|
93
|
+
- Self-contained IEEE-754 deterministic `det_exp` / `det_log` / `det_round` in [`src/core/simd.hpp`](src/core/simd.hpp) (no OS `libm` ULP divergence).
|
|
94
|
+
- **Streaming Chunked Format (`.mbit` v3) & Security Hardening** ([`src/codec/minbit_codec.hpp`](src/codec/minbit_codec.hpp)):
|
|
95
|
+
- Bounded-RAM pipelined multi-threaded compression & decompression for arbitrarily large files and Unix/Windows `stdin`/`stdout` pipes (`-` path).
|
|
96
|
+
- Header **CRC-16/CCITT**, per-block **IEEE CRC-32**, whole-stream **XXH64** + exact length/block-count footer, hard 64 MiB anti-OOM ceiling, and configurable `--max-out-mb` anti-zip-bomb guard.
|
|
97
|
+
- Strict bijective per-block ARM64 (`BL`+`ADRP`) and x86 (`E8`/`E9`) BCJ filters ([`src/transforms/bcj_transform.hpp`](src/transforms/bcj_transform.hpp)) + automatic incompressible `RAW` fallback.
|
|
98
|
+
- **Multi-Modal Adaptive Modeling**:
|
|
99
|
+
- 24-context 64-byte cache-aligned `ContextMap` + zero-collision L2 `DirectSparseModel` + `IndirectModel` + 4-way multi-hash-chain `MatchModel` (3B/5B/8B/12B + shadow resync).
|
|
100
|
+
- Lexical UTF-8 `WordModel` with unfragmented word/bigram/trigram/skip-bigram, `PrevLine` column alignment, and `<tag>`/bracket nesting ([`src/models/word_model.hpp`](src/models/word_model.hpp)).
|
|
101
|
+
- Out-of-sample cross-validated modular AMDF row/pixel stride discovery (`sx ∈ {1,2,3,4}`), 6-tap fixed-point 2D NLMS Wiener filter, and **16-bit carry-aware MSB/LSB prediction** for 16-bit medical imagery & PCM audio ([`src/models/record_model.hpp`](src/models/record_model.hpp), [`src/models/linear_model.hpp`](src/models/linear_model.hpp)).
|
|
102
|
+
- **Pareto Preset Spectrum (`-0` .. `-4`)**:
|
|
103
|
+
- `-0` (`FastPredictor`, ~12 MB RAM/thread): matches `zstd -22` / `xz -9e` multi-core compression speed while producing **12%–30% smaller** files.
|
|
104
|
+
- `-2` (~58 MB RAM/thread) & `-3` (~210 MB RAM/thread): high-ratio general archival surpassing `zpaq -m5` on Silesia, Canterbury, Calgary, and `enwik8_10m`.
|
|
105
|
+
- `-4` (Ultra 64 MiB, ~710 MB RAM/thread): 512 MiB `ContextMap` + 64 MiB sliding window surpassing `zpaq -m5` on **full 100 MB `enwik8`** (`1.5662 bpb`) and `silesia/webster` (`1.0923 bpb`).
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## International Standard Corpus Benchmarks
|
|
110
|
+
|
|
111
|
+
> Measured on **Apple M4 Pro** (12 threads, ARM64 NEON, Apple Clang 21.0.0 `-O3 -flto`). Every archive was decompressed and verified bit-for-bit identical against the original input.
|
|
112
|
+
|
|
113
|
+
### 1. Summary Across 5 International Standard Benchmarks
|
|
114
|
+
|
|
115
|
+
| Corpus | Original | `gzip -9` | `bzip2 -9` | `zstd -22` | `brotli -11` | `xz -9e` | `7z-PPMd` | `zpaq -m5` | `minbit -0` | `minbit -3` | **`minbit -4`** |
|
|
116
|
+
|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|
|
|
117
|
+
| **Canterbury** (11 files, `-T1`) | 2,810,784 | 727,952 | 542,710 | 516,140 | 490,662 | 493,168 | 573,140 | 361,685 | 356,001 | **340,898*** (`0.970 bpb`) | — |
|
|
118
|
+
| **Calgary** (18 files, `-T1`) | 3,251,493 | 1,059,877 | 866,501 | 920,849 | 856,672 | 883,896 | 794,683 | 718,141 | 706,403 | **675,768*** (`1.663 bpb`) | — |
|
|
119
|
+
| **enwik8_10m** (10 MB, `-T1`) | 10,000,000 | 3,688,458 | 2,916,026 | 2,793,362 | 2,704,839 | 2,720,264 | 2,370,078 | 2,091,350 | 2,352,061 | 2,077,685 (`1.662 bpb`) | **2,045,519*** (`1.636 bpb`) |
|
|
120
|
+
| **Silesia** (12 files, `-T0`) | 211,938,580 | 67,643,417 | 54,506,769 | 52,363,658 | 49,564,563 | 48,456,100 | 48,035,603 | 39,112,522 | 42,625,116 | **38,673,757*** (`1.460 bpb`) | *webster*: **5,660,825*** |
|
|
121
|
+
| **enwik8** (100 MB, `-T0`) | 100,000,000 | 36,475,804 | 29,008,758 | 25,272,468 | 25,742,001 | 24,831,656 | 23,626,709 | 19,625,007 | 23,486,750 | 20,291,985 (`1.623 bpb`) | **19,576,877*** (`1.566 bpb`) |
|
|
122
|
+
|
|
123
|
+
> On full **100 MB `enwik8`**, `minbit -4 -T0` splits into 2 balanced ~50 MB blocks (`0.51 MB/s enc / 0.49 MB/s dec`, ~710 MB RAM/thread) and beats `zpaq -m5` (`19,625,007 B` / `1.5700 bpb`) by **-48,130 B (-0.25%)** and `xz -9e` by **-5.25 MB (-21.16%)**. On **Silesia**, `minbit -4` also beats `zpaq -m5` on `webster` (`5,660,825` vs `5,666,828 B`), giving **11 / 12** head-to-head Silesia file wins over `zpaq -m5`.
|
|
124
|
+
|
|
125
|
+
### 2. Full Silesia Corpus Breakdown (12 Files, 211.94 MB)
|
|
126
|
+
|
|
127
|
+
| File | Original | `zstd -22` | `brotli -11` | `xz -9e` | `7z-PPMd` | `zpaq -m5` | `minbit -0` | `minbit -2` | **`minbit -3`** |
|
|
128
|
+
|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|
|
|
129
|
+
| `dickens` | 10,192,446 | 2,849,064 | 2,827,777 | 2,831,220 | 2,334,295 | **2,094,739*** | 2,425,631 | 2,282,800 | 2,191,736 |
|
|
130
|
+
| `mozilla` | 51,220,480 | 14,912,709 | 13,872,265 | 13,376,248 | 15,929,217 | 12,041,051 | 12,936,448 | 11,944,441 | **11,698,763*** |
|
|
131
|
+
| `mr` | 9,970,564 | 3,102,044 | 2,823,136 | 2,751,900 | 2,319,043 | 2,181,306 | 2,142,920 | 2,105,418 | **2,094,950*** |
|
|
132
|
+
| `nci` | 33,553,445 | 1,578,382 | 1,519,768 | 1,449,280 | 1,839,074 | 1,251,105 | 1,343,170 | 1,144,453 | **1,120,622*** |
|
|
133
|
+
| `ooffice` | 6,152,192 | 2,595,234 | 2,478,855 | 2,427,232 | 2,516,423 | 1,766,548 | 1,937,780 | 1,765,172 | **1,655,893*** |
|
|
134
|
+
| `osdb` | 10,085,684 | 3,099,936 | 2,816,278 | 2,844,564 | 2,374,462 | 2,204,737 | 2,363,622 | 2,246,130 | **2,202,031*** |
|
|
135
|
+
| `reymont` | 6,627,202 | 1,348,572 | 1,332,158 | 1,315,600 | 1,020,200 | 956,497 | 1,038,495 | 957,661 | **915,617*** |
|
|
136
|
+
| `samba` | 21,606,400 | 3,865,315 | 3,766,340 | 3,739,532 | 3,784,047 | 3,053,816 | 3,373,314 | 3,107,649 | **2,988,481*** |
|
|
137
|
+
| `sao` | 7,251,944 | 5,000,573 | 4,586,092 | 4,425,672 | 4,783,825 | 3,899,253 | 3,963,598 | 3,826,663 | **3,801,162*** |
|
|
138
|
+
| `webster` | 41,458,703 | 8,430,464 | 8,428,575 | 8,368,680 | 6,904,476 | **5,666,828*** | 7,123,062 | 6,455,064 | 6,083,394 |
|
|
139
|
+
| `x-ray` | 8,474,240 | 5,129,825 | 4,682,753 | 4,491,272 | 3,850,892 | 3,669,697 | 3,655,513 | 3,628,834 | **3,625,896*** |
|
|
140
|
+
| `xml` | 5,345,280 | 451,540 | 430,566 | 434,900 | 379,649 | 326,945 | 321,563 | 300,645 | **295,212*** |
|
|
141
|
+
| **TOTAL** | **211,938,580** | 52,363,658 | 49,564,563 | 48,456,100 | 48,035,603 | 39,112,522 | 42,625,116 | 39,764,930 | **38,673,757*** |
|
|
142
|
+
| **Ratio / bpb** | `100% / 8.00` | `24.71% / 1.98` | `23.39% / 1.87` | `22.86% / 1.83` | `22.66% / 1.81` | `18.45% / 1.48` | `20.11% / 1.61` | `18.76% / 1.50` | **`18.25% / 1.46`** |
|
|
143
|
+
| **Enc / Dec MB/s** | — | `2.51 / 573.1` | `0.73 / 268.2` | `2.61 / 112.7` | `9.70 / 8.67` | `0.46 / 0.45` | **`2.40 / 2.11`** | `0.90 / 0.87` | `0.56 / 0.54` |
|
|
144
|
+
|
|
145
|
+
> **Honest Trade-Off Note**: Context mixing decoders are symmetric with their encoders (`~2.1–4.3 MB/s` multi-core on `-0`, `~0.5–1.6 MB/s` on `-2/-3`), unlike LZ77/LZMA decoders (`110–660 MB/s`). Use MinBit when **minimizing storage/bandwidth bytes** matters most (archival, medical/scientific datasets, release payloads, cold backups).
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Cross-Platform Build (macOS / Linux / Windows)
|
|
150
|
+
|
|
151
|
+
### CMake (macOS, Linux, Windows MSVC / Clang / MinGW)
|
|
152
|
+
```bash
|
|
153
|
+
cmake -B build -DCMAKE_BUILD_TYPE=Release
|
|
154
|
+
cmake --build build --config Release
|
|
155
|
+
ctest --test-dir build --output-on-failure -C Release
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Make (macOS / Linux)
|
|
159
|
+
```bash
|
|
160
|
+
make all test test-py asan asan-c fuzz
|
|
161
|
+
bash tests/test_cross_simd.sh # Verify ARM64-NEON, x86-64 AVX2/SSSE3/SSE2, Scalar bit-exactness
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### C11 Library API (`include/minbit.h`)
|
|
165
|
+
Link against `libminbit.a` or `libminbit.{dylib,so,dll}` (or CMake `minbit::minbit`):
|
|
166
|
+
```c
|
|
167
|
+
#include "minbit.h"
|
|
168
|
+
|
|
169
|
+
minbit_compress_options_t copt;
|
|
170
|
+
minbit_compress_options_init(&copt);
|
|
171
|
+
copt.level = 2; /* 0 (fast-CM) .. 3 (max ratio) */
|
|
172
|
+
copt.threads = 0; /* auto CPU cores */
|
|
173
|
+
|
|
174
|
+
uint8_t* comp = NULL; size_t comp_sz = 0;
|
|
175
|
+
if (minbit_compress_alloc(src, src_sz, &comp, &comp_sz, &copt) == MINBIT_OK) {
|
|
176
|
+
minbit_decompress_options_t dopt;
|
|
177
|
+
minbit_decompress_options_init(&dopt);
|
|
178
|
+
dopt.max_output_bytes = 64u * 1024u * 1024u; /* anti-zip-bomb guard */
|
|
179
|
+
uint8_t* out = NULL; size_t out_sz = 0;
|
|
180
|
+
minbit_decompress_alloc(comp, comp_sz, &out, &out_sz, &dopt);
|
|
181
|
+
minbit_free(out);
|
|
182
|
+
minbit_free(comp);
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
Also provides caller-buffer (`minbit_compress_buffer` / `minbit_decompress_buffer`), streaming file (`minbit_compress_file` / `minbit_decompress_file` / `minbit_verify_file`), and custom I/O callback streaming (`minbit_compress_stream_cb` / `minbit_decompress_stream_cb`).
|
|
186
|
+
|
|
187
|
+
### Python Package (`python/minbit`)
|
|
188
|
+
```python
|
|
189
|
+
import minbit
|
|
190
|
+
|
|
191
|
+
comp = minbit.compress(data, level=2, threads=0)
|
|
192
|
+
raw = minbit.decompress(comp, max_output_bytes=64 * 1024 * 1024)
|
|
193
|
+
|
|
194
|
+
# Bounded-RAM streaming files & file-like objects
|
|
195
|
+
stats = minbit.compress_file("scan.dcm", "scan.dcm.mbit", level=0, threads=0)
|
|
196
|
+
minbit.verify_file("scan.dcm.mbit")
|
|
197
|
+
minbit.decompress_file("scan.dcm.mbit", "scan_restored.dcm")
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### CLI Examples
|
|
201
|
+
```bash
|
|
202
|
+
# Fast multi-core compression (matches zstd -22 / xz -9e encode speed, 12–19% smaller files)
|
|
203
|
+
./build/minbit c -0 -T0 input.dat output.mbit
|
|
204
|
+
|
|
205
|
+
# High-ratio multi-core compression
|
|
206
|
+
./build/minbit c -2 -T0 input.dat output.mbit
|
|
207
|
+
|
|
208
|
+
# Maximum compression ratio (-3) or Ultra 64 MiB Archival (-4 for enwik8/enwik9)
|
|
209
|
+
./build/minbit c -3 -T0 --block-kb 8192 input.dat output.mbit
|
|
210
|
+
./build/minbit c -4 -T0 enwik8 enwik8.mbit
|
|
211
|
+
|
|
212
|
+
# Unix/Windows streaming pipe (bounded RAM for arbitrarily large archives)
|
|
213
|
+
tar cf - my_folder/ | ./build/minbit c -0 -T0 - my_folder.tar.mbit
|
|
214
|
+
|
|
215
|
+
# Decompress with anti-zip-bomb cap (automatically parallelizes across blocks)
|
|
216
|
+
./build/minbit d --max-out-mb 1024 output.mbit restored.dat
|
|
217
|
+
|
|
218
|
+
# Verify archive integrity without writing output
|
|
219
|
+
./build/minbit t output.mbit
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Documentation & License
|
|
223
|
+
- **Bitstream Format Specification**: [`FORMAT.md`](FORMAT.md) (frozen `.mbit` v3 spec & cross-platform determinism contract)
|
|
224
|
+
- **Release Notes**: [`CHANGELOG.md`](CHANGELOG.md)
|
|
225
|
+
- **Contributing & Invariants**: [`CONTRIBUTING.md`](CONTRIBUTING.md)
|
|
226
|
+
- **License**: [MIT License](LICENSE)
|
|
227
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
minbit-3.1.0.data/purelib/minbit/__init__.py,sha256=uXTl5rJRTdGD3j6DWNP43mZ4jFnzzWlYikrOV3C-5ks,21207
|
|
2
|
+
minbit-3.1.0.data/purelib/minbit/__main__.py,sha256=8faNfXx9luD184nJ5_O7R8aOPMHK9fF7hosYr2IncEA,3300
|
|
3
|
+
minbit-3.1.0.data/purelib/minbit/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
minbit-3.1.0.data/purelib/minbit/_lib/minbit.dll,sha256=4BKxsREmGC6zekm_kDyx35hDXQRqzIU9Ve2h-7SH38U,411136
|
|
5
|
+
minbit-3.1.0.dist-info/licenses/LICENSE,sha256=pCy0BZpxaAJrZmpN7DRWGMu1Rq9MD9ZQyXyH_yDxID8,1086
|
|
6
|
+
minbit-3.1.0.dist-info/METADATA,sha256=-SKbr-5UcMiRYsvvtuInaN6OScrzzbViCnjEWZX7WZ4,14681
|
|
7
|
+
minbit-3.1.0.dist-info/WHEEL,sha256=79BCzrXBJOa2YV6ugKrK59dqWifMGjAkgVd_6AQrqbU,98
|
|
8
|
+
minbit-3.1.0.dist-info/top_level.txt,sha256=S7xnbxOgrPBGlstztbHZVWb5Rv4TEJ4McV_cDvCFyR0,7
|
|
9
|
+
minbit-3.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NeiH4207
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
minbit
|