blitzb64 0.1.0__tar.gz
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.
- blitzb64-0.1.0/LICENSE +21 -0
- blitzb64-0.1.0/MANIFEST.in +4 -0
- blitzb64-0.1.0/PKG-INFO +151 -0
- blitzb64-0.1.0/README.md +127 -0
- blitzb64-0.1.0/blitzb64/__init__.py +65 -0
- blitzb64-0.1.0/blitzb64.egg-info/PKG-INFO +151 -0
- blitzb64-0.1.0/blitzb64.egg-info/SOURCES.txt +11 -0
- blitzb64-0.1.0/blitzb64.egg-info/dependency_links.txt +1 -0
- blitzb64-0.1.0/blitzb64.egg-info/top_level.txt +1 -0
- blitzb64-0.1.0/pyproject.toml +28 -0
- blitzb64-0.1.0/setup.cfg +4 -0
- blitzb64-0.1.0/setup.py +73 -0
- blitzb64-0.1.0/src/_blitzb64.cpp +2248 -0
blitzb64-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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.
|
blitzb64-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: blitzb64
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: High-speed multithreaded AVX2 C++ Base64 and file-to-Base64 / Base64-to-file library
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Keywords: base64,simd,avx2,file,mmap,fast
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
10
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
11
|
+
Classifier: Operating System :: MacOS
|
|
12
|
+
Classifier: Programming Language :: C++
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Utilities
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# blitzb64
|
|
26
|
+
|
|
27
|
+
**High-speed, multithreaded SIMD C++ Python library for converting files to Base64, Base64 to files, and in-memory Base64 encoding/decoding.**
|
|
28
|
+
|
|
29
|
+
## Why `blitzb64`?
|
|
30
|
+
|
|
31
|
+
Existing SIMD Base64 Python packages (`pybase64`, `fastpybase64`) only operate on in-memory buffers (`bytes`), forcing you to read an entire file into Python memory, encode/decode on a single thread, and write it back out.
|
|
32
|
+
|
|
33
|
+
`blitzb64` eliminates those bottlenecks with a native C++17 extension:
|
|
34
|
+
- **Zero-copy OS memory-mapped file I/O** (`CreateFileMappingW`/`MapViewOfFile` on Windows, `mmap` on Linux/macOS) — no Python `open().read()` / `open().write()` heap copies.
|
|
35
|
+
- **Multithreaded execution** (Win32 Threadpool / POSIX `std::thread`, GIL released) across all CPU cores.
|
|
36
|
+
- **Runtime SIMD dispatch** (`AVX-512 VBMI` → `AVX2` → `SSSE3` on `x86_64`, `NEON` on `ARM64`, plus portable scalar fallback).
|
|
37
|
+
- **Non-temporal streaming stores** (`_mm256_stream_si256` / `_mm512_stream_si512`) for both encode and decode to avoid cache-line Read-For-Ownership (RFO) DRAM traffic on large files.
|
|
38
|
+
- **Parallel SIMD MIME / whitespace-wrapped decoding** when `validate=False` or `ignore_whitespace=True`.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install blitzb64
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
> **Requirements:** Python `>=3.10`. Prebuilt binary wheels are provided where available; building from source requires a C++17 compiler (`MSVC`, `clang++`, or `g++`).
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
### 1. Convert a File to Base64 (`file_to_base64`)
|
|
55
|
+
|
|
56
|
+
Encodes any binary file (images, videos, archives, etc.) directly into a `.b64` file on disk and returns the number of Base64 bytes written.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import blitzb64
|
|
60
|
+
|
|
61
|
+
# Standard Base64 file conversion
|
|
62
|
+
bytes_written = blitzb64.file_to_base64("photo.jpg", "photo.b64")
|
|
63
|
+
print(f"Encoded {bytes_written:,} Base64 bytes")
|
|
64
|
+
|
|
65
|
+
# URL-safe Base64 ('-' and '_' instead of '+' and '/')
|
|
66
|
+
blitzb64.file_to_base64("photo.jpg", "photo.urlsafe.b64", urlsafe=True)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2. Convert a Base64 File Back to a File (`base64_to_file`)
|
|
70
|
+
|
|
71
|
+
Decodes a Base64 file directly back into a binary file and returns the number of decoded bytes written.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
import blitzb64
|
|
75
|
+
|
|
76
|
+
# Strict Base64 decode (fast path, raises ValueError on invalid characters)
|
|
77
|
+
decoded_bytes = blitzb64.base64_to_file("photo.b64", "photo_restored.jpg")
|
|
78
|
+
print(f"Decoded {decoded_bytes:,} bytes")
|
|
79
|
+
|
|
80
|
+
# URL-safe Base64 file
|
|
81
|
+
blitzb64.base64_to_file("photo.urlsafe.b64", "photo_restored.jpg", urlsafe=True)
|
|
82
|
+
|
|
83
|
+
# MIME / line-wrapped Base64 file (ignores '\r', '\n', spaces)
|
|
84
|
+
blitzb64.base64_to_file("mime_wrapped.b64", "output.bin", ignore_whitespace=True)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. Read a File Directly into Base64 `bytes` (`file_to_b64_bytes`)
|
|
88
|
+
|
|
89
|
+
Skips `open(path, "rb").read()` and encodes straight from mapped file pages into a single Python `bytes` object:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import blitzb64
|
|
93
|
+
|
|
94
|
+
b64_bytes = blitzb64.file_to_b64_bytes("photo.jpg")
|
|
95
|
+
# Or as a UTF-8/ASCII string:
|
|
96
|
+
b64_text = blitzb64.file_to_b64_bytes("photo.jpg").decode("ascii")
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 4. Write Base64 `bytes` Directly to a File (`b64_bytes_to_file`)
|
|
100
|
+
|
|
101
|
+
Decodes a Python `bytes`, `bytearray`, or `memoryview` directly into a file without creating an intermediate decoded `bytes` object in Python:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import blitzb64
|
|
105
|
+
|
|
106
|
+
n = blitzb64.b64_bytes_to_file(b64_bytes, "restored.jpg")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 5. In-Memory `base64` Drop-In Functions
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
import blitzb64
|
|
113
|
+
|
|
114
|
+
encoded = blitzb64.b64encode(b"Hello, World!")
|
|
115
|
+
decoded = blitzb64.b64decode(encoded)
|
|
116
|
+
|
|
117
|
+
# Standard & URL-safe helpers
|
|
118
|
+
std_enc = blitzb64.standard_b64encode(b"data")
|
|
119
|
+
std_dec = blitzb64.standard_b64decode(std_enc)
|
|
120
|
+
|
|
121
|
+
url_enc = blitzb64.urlsafe_b64encode(b"\xfb\xff\xbf")
|
|
122
|
+
url_dec = blitzb64.urlsafe_b64decode(url_enc)
|
|
123
|
+
|
|
124
|
+
# Non-strict / line-wrapped in-memory decode (accepts bytes or ASCII str)
|
|
125
|
+
raw = blitzb64.b64decode("SGVs\nbG8h\n", validate=False)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## API Reference
|
|
131
|
+
|
|
132
|
+
| Function | Description |
|
|
133
|
+
|---|---|
|
|
134
|
+
| `file_to_base64(src_path, dst_path, *, urlsafe=False, threads=0) -> int` | Encode `src_path` directly to `dst_path`; returns bytes written. |
|
|
135
|
+
| `base64_to_file(src_path, dst_path, *, urlsafe=False, validate=True, ignore_whitespace=False, threads=0) -> int` | Decode `src_path` directly to `dst_path`; returns bytes written. |
|
|
136
|
+
| `file_to_b64_bytes(src_path, *, urlsafe=False, threads=0) -> bytes` | Read & encode `src_path` directly into `bytes`. |
|
|
137
|
+
| `b64_bytes_to_file(data, dst_path, *, urlsafe=False, validate=True, ignore_whitespace=False, threads=0) -> int` | Decode `data` directly to `dst_path`; returns bytes written. |
|
|
138
|
+
| `b64encode(data, *, urlsafe=False, threads=0) -> bytes` | Encode `bytes`/`bytearray`/`memoryview` to Base64 `bytes`. |
|
|
139
|
+
| `b64decode(data, *, urlsafe=False, validate=True, threads=0) -> bytes` | Decode Base64 `bytes`-like or ASCII `str` to `bytes`. |
|
|
140
|
+
| `standard_b64encode(data, *, threads=0) -> bytes` | Standard Base64 encode. |
|
|
141
|
+
| `standard_b64decode(data, *, threads=0) -> bytes` | Standard Base64 decode (`validate=False`). |
|
|
142
|
+
| `urlsafe_b64encode(data, *, threads=0) -> bytes` | URL-safe Base64 encode (`-` / `_`). |
|
|
143
|
+
| `urlsafe_b64decode(data, *, threads=0) -> bytes` | URL-safe Base64 decode (`validate=False`). |
|
|
144
|
+
| `simd_info() -> dict` | Inspect active SIMD ISA (`avx512_vbmi`, `avx2`, `ssse3`, `neon`, `scalar`) and hardware thread count. |
|
|
145
|
+
|
|
146
|
+
- **`src_path` / `dst_path`**: accepts `str`, `bytes`, or `pathlib.Path` (`os.PathLike`).
|
|
147
|
+
- **`threads`**: `0` (default) uses all available logical CPU cores for payloads `>= 256 KiB`. Pass `threads=1` to force single-threaded execution.
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT License — see [LICENSE](LICENSE).
|
blitzb64-0.1.0/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# blitzb64
|
|
2
|
+
|
|
3
|
+
**High-speed, multithreaded SIMD C++ Python library for converting files to Base64, Base64 to files, and in-memory Base64 encoding/decoding.**
|
|
4
|
+
|
|
5
|
+
## Why `blitzb64`?
|
|
6
|
+
|
|
7
|
+
Existing SIMD Base64 Python packages (`pybase64`, `fastpybase64`) only operate on in-memory buffers (`bytes`), forcing you to read an entire file into Python memory, encode/decode on a single thread, and write it back out.
|
|
8
|
+
|
|
9
|
+
`blitzb64` eliminates those bottlenecks with a native C++17 extension:
|
|
10
|
+
- **Zero-copy OS memory-mapped file I/O** (`CreateFileMappingW`/`MapViewOfFile` on Windows, `mmap` on Linux/macOS) — no Python `open().read()` / `open().write()` heap copies.
|
|
11
|
+
- **Multithreaded execution** (Win32 Threadpool / POSIX `std::thread`, GIL released) across all CPU cores.
|
|
12
|
+
- **Runtime SIMD dispatch** (`AVX-512 VBMI` → `AVX2` → `SSSE3` on `x86_64`, `NEON` on `ARM64`, plus portable scalar fallback).
|
|
13
|
+
- **Non-temporal streaming stores** (`_mm256_stream_si256` / `_mm512_stream_si512`) for both encode and decode to avoid cache-line Read-For-Ownership (RFO) DRAM traffic on large files.
|
|
14
|
+
- **Parallel SIMD MIME / whitespace-wrapped decoding** when `validate=False` or `ignore_whitespace=True`.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install blitzb64
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
> **Requirements:** Python `>=3.10`. Prebuilt binary wheels are provided where available; building from source requires a C++17 compiler (`MSVC`, `clang++`, or `g++`).
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
### 1. Convert a File to Base64 (`file_to_base64`)
|
|
31
|
+
|
|
32
|
+
Encodes any binary file (images, videos, archives, etc.) directly into a `.b64` file on disk and returns the number of Base64 bytes written.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import blitzb64
|
|
36
|
+
|
|
37
|
+
# Standard Base64 file conversion
|
|
38
|
+
bytes_written = blitzb64.file_to_base64("photo.jpg", "photo.b64")
|
|
39
|
+
print(f"Encoded {bytes_written:,} Base64 bytes")
|
|
40
|
+
|
|
41
|
+
# URL-safe Base64 ('-' and '_' instead of '+' and '/')
|
|
42
|
+
blitzb64.file_to_base64("photo.jpg", "photo.urlsafe.b64", urlsafe=True)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 2. Convert a Base64 File Back to a File (`base64_to_file`)
|
|
46
|
+
|
|
47
|
+
Decodes a Base64 file directly back into a binary file and returns the number of decoded bytes written.
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
import blitzb64
|
|
51
|
+
|
|
52
|
+
# Strict Base64 decode (fast path, raises ValueError on invalid characters)
|
|
53
|
+
decoded_bytes = blitzb64.base64_to_file("photo.b64", "photo_restored.jpg")
|
|
54
|
+
print(f"Decoded {decoded_bytes:,} bytes")
|
|
55
|
+
|
|
56
|
+
# URL-safe Base64 file
|
|
57
|
+
blitzb64.base64_to_file("photo.urlsafe.b64", "photo_restored.jpg", urlsafe=True)
|
|
58
|
+
|
|
59
|
+
# MIME / line-wrapped Base64 file (ignores '\r', '\n', spaces)
|
|
60
|
+
blitzb64.base64_to_file("mime_wrapped.b64", "output.bin", ignore_whitespace=True)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 3. Read a File Directly into Base64 `bytes` (`file_to_b64_bytes`)
|
|
64
|
+
|
|
65
|
+
Skips `open(path, "rb").read()` and encodes straight from mapped file pages into a single Python `bytes` object:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import blitzb64
|
|
69
|
+
|
|
70
|
+
b64_bytes = blitzb64.file_to_b64_bytes("photo.jpg")
|
|
71
|
+
# Or as a UTF-8/ASCII string:
|
|
72
|
+
b64_text = blitzb64.file_to_b64_bytes("photo.jpg").decode("ascii")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 4. Write Base64 `bytes` Directly to a File (`b64_bytes_to_file`)
|
|
76
|
+
|
|
77
|
+
Decodes a Python `bytes`, `bytearray`, or `memoryview` directly into a file without creating an intermediate decoded `bytes` object in Python:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import blitzb64
|
|
81
|
+
|
|
82
|
+
n = blitzb64.b64_bytes_to_file(b64_bytes, "restored.jpg")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 5. In-Memory `base64` Drop-In Functions
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
import blitzb64
|
|
89
|
+
|
|
90
|
+
encoded = blitzb64.b64encode(b"Hello, World!")
|
|
91
|
+
decoded = blitzb64.b64decode(encoded)
|
|
92
|
+
|
|
93
|
+
# Standard & URL-safe helpers
|
|
94
|
+
std_enc = blitzb64.standard_b64encode(b"data")
|
|
95
|
+
std_dec = blitzb64.standard_b64decode(std_enc)
|
|
96
|
+
|
|
97
|
+
url_enc = blitzb64.urlsafe_b64encode(b"\xfb\xff\xbf")
|
|
98
|
+
url_dec = blitzb64.urlsafe_b64decode(url_enc)
|
|
99
|
+
|
|
100
|
+
# Non-strict / line-wrapped in-memory decode (accepts bytes or ASCII str)
|
|
101
|
+
raw = blitzb64.b64decode("SGVs\nbG8h\n", validate=False)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## API Reference
|
|
107
|
+
|
|
108
|
+
| Function | Description |
|
|
109
|
+
|---|---|
|
|
110
|
+
| `file_to_base64(src_path, dst_path, *, urlsafe=False, threads=0) -> int` | Encode `src_path` directly to `dst_path`; returns bytes written. |
|
|
111
|
+
| `base64_to_file(src_path, dst_path, *, urlsafe=False, validate=True, ignore_whitespace=False, threads=0) -> int` | Decode `src_path` directly to `dst_path`; returns bytes written. |
|
|
112
|
+
| `file_to_b64_bytes(src_path, *, urlsafe=False, threads=0) -> bytes` | Read & encode `src_path` directly into `bytes`. |
|
|
113
|
+
| `b64_bytes_to_file(data, dst_path, *, urlsafe=False, validate=True, ignore_whitespace=False, threads=0) -> int` | Decode `data` directly to `dst_path`; returns bytes written. |
|
|
114
|
+
| `b64encode(data, *, urlsafe=False, threads=0) -> bytes` | Encode `bytes`/`bytearray`/`memoryview` to Base64 `bytes`. |
|
|
115
|
+
| `b64decode(data, *, urlsafe=False, validate=True, threads=0) -> bytes` | Decode Base64 `bytes`-like or ASCII `str` to `bytes`. |
|
|
116
|
+
| `standard_b64encode(data, *, threads=0) -> bytes` | Standard Base64 encode. |
|
|
117
|
+
| `standard_b64decode(data, *, threads=0) -> bytes` | Standard Base64 decode (`validate=False`). |
|
|
118
|
+
| `urlsafe_b64encode(data, *, threads=0) -> bytes` | URL-safe Base64 encode (`-` / `_`). |
|
|
119
|
+
| `urlsafe_b64decode(data, *, threads=0) -> bytes` | URL-safe Base64 decode (`validate=False`). |
|
|
120
|
+
| `simd_info() -> dict` | Inspect active SIMD ISA (`avx512_vbmi`, `avx2`, `ssse3`, `neon`, `scalar`) and hardware thread count. |
|
|
121
|
+
|
|
122
|
+
- **`src_path` / `dst_path`**: accepts `str`, `bytes`, or `pathlib.Path` (`os.PathLike`).
|
|
123
|
+
- **`threads`**: `0` (default) uses all available logical CPU cores for payloads `>= 256 KiB`. Pass `threads=1` to force single-threaded execution.
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
MIT License — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""blitzb64: High-speed multithreaded AVX2 C++ Base64 & File Conversion Library."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from ._blitzb64 import (
|
|
6
|
+
b64_bytes_to_file,
|
|
7
|
+
b64decode as _c_b64decode,
|
|
8
|
+
b64encode,
|
|
9
|
+
base64_to_file,
|
|
10
|
+
file_to_b64_bytes,
|
|
11
|
+
file_to_base64,
|
|
12
|
+
simd_info,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__version__ = "0.1.0"
|
|
16
|
+
__all__ = [
|
|
17
|
+
"b64encode",
|
|
18
|
+
"b64decode",
|
|
19
|
+
"standard_b64encode",
|
|
20
|
+
"standard_b64decode",
|
|
21
|
+
"urlsafe_b64encode",
|
|
22
|
+
"urlsafe_b64decode",
|
|
23
|
+
"file_to_base64",
|
|
24
|
+
"base64_to_file",
|
|
25
|
+
"file_to_b64_bytes",
|
|
26
|
+
"b64_bytes_to_file",
|
|
27
|
+
"simd_info",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _coerce_b64_input(data: bytes | bytearray | memoryview | str) -> bytes | bytearray | memoryview:
|
|
32
|
+
if isinstance(data, str):
|
|
33
|
+
return data.encode("ascii")
|
|
34
|
+
return data
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def b64decode(
|
|
38
|
+
data: bytes | bytearray | memoryview | str,
|
|
39
|
+
*,
|
|
40
|
+
urlsafe: bool = False,
|
|
41
|
+
validate: bool = True,
|
|
42
|
+
threads: int = 0,
|
|
43
|
+
) -> bytes:
|
|
44
|
+
"""Decode Base64 bytes-like object or ASCII str to bytes."""
|
|
45
|
+
return _c_b64decode(_coerce_b64_input(data), urlsafe=urlsafe, validate=validate, threads=threads)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def standard_b64encode(data: bytes | bytearray | memoryview, *, threads: int = 0) -> bytes:
|
|
49
|
+
"""Encode bytes-like object using standard Base64 alphabet."""
|
|
50
|
+
return b64encode(data, urlsafe=False, threads=threads)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def standard_b64decode(data: bytes | bytearray | memoryview | str, *, threads: int = 0) -> bytes:
|
|
54
|
+
"""Decode bytes-like object or ASCII str using standard Base64 alphabet."""
|
|
55
|
+
return b64decode(data, urlsafe=False, validate=False, threads=threads)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def urlsafe_b64encode(data: bytes | bytearray | memoryview, *, threads: int = 0) -> bytes:
|
|
59
|
+
"""Encode bytes-like object using URL-safe Base64 alphabet."""
|
|
60
|
+
return b64encode(data, urlsafe=True, threads=threads)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def urlsafe_b64decode(data: bytes | bytearray | memoryview | str, *, threads: int = 0) -> bytes:
|
|
64
|
+
"""Decode bytes-like object or ASCII str using URL-safe Base64 alphabet."""
|
|
65
|
+
return b64decode(data, urlsafe=True, validate=False, threads=threads)
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: blitzb64
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: High-speed multithreaded AVX2 C++ Base64 and file-to-Base64 / Base64-to-file library
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Keywords: base64,simd,avx2,file,mmap,fast
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
10
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
11
|
+
Classifier: Operating System :: MacOS
|
|
12
|
+
Classifier: Programming Language :: C++
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Utilities
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# blitzb64
|
|
26
|
+
|
|
27
|
+
**High-speed, multithreaded SIMD C++ Python library for converting files to Base64, Base64 to files, and in-memory Base64 encoding/decoding.**
|
|
28
|
+
|
|
29
|
+
## Why `blitzb64`?
|
|
30
|
+
|
|
31
|
+
Existing SIMD Base64 Python packages (`pybase64`, `fastpybase64`) only operate on in-memory buffers (`bytes`), forcing you to read an entire file into Python memory, encode/decode on a single thread, and write it back out.
|
|
32
|
+
|
|
33
|
+
`blitzb64` eliminates those bottlenecks with a native C++17 extension:
|
|
34
|
+
- **Zero-copy OS memory-mapped file I/O** (`CreateFileMappingW`/`MapViewOfFile` on Windows, `mmap` on Linux/macOS) — no Python `open().read()` / `open().write()` heap copies.
|
|
35
|
+
- **Multithreaded execution** (Win32 Threadpool / POSIX `std::thread`, GIL released) across all CPU cores.
|
|
36
|
+
- **Runtime SIMD dispatch** (`AVX-512 VBMI` → `AVX2` → `SSSE3` on `x86_64`, `NEON` on `ARM64`, plus portable scalar fallback).
|
|
37
|
+
- **Non-temporal streaming stores** (`_mm256_stream_si256` / `_mm512_stream_si512`) for both encode and decode to avoid cache-line Read-For-Ownership (RFO) DRAM traffic on large files.
|
|
38
|
+
- **Parallel SIMD MIME / whitespace-wrapped decoding** when `validate=False` or `ignore_whitespace=True`.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install blitzb64
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
> **Requirements:** Python `>=3.10`. Prebuilt binary wheels are provided where available; building from source requires a C++17 compiler (`MSVC`, `clang++`, or `g++`).
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
### 1. Convert a File to Base64 (`file_to_base64`)
|
|
55
|
+
|
|
56
|
+
Encodes any binary file (images, videos, archives, etc.) directly into a `.b64` file on disk and returns the number of Base64 bytes written.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import blitzb64
|
|
60
|
+
|
|
61
|
+
# Standard Base64 file conversion
|
|
62
|
+
bytes_written = blitzb64.file_to_base64("photo.jpg", "photo.b64")
|
|
63
|
+
print(f"Encoded {bytes_written:,} Base64 bytes")
|
|
64
|
+
|
|
65
|
+
# URL-safe Base64 ('-' and '_' instead of '+' and '/')
|
|
66
|
+
blitzb64.file_to_base64("photo.jpg", "photo.urlsafe.b64", urlsafe=True)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2. Convert a Base64 File Back to a File (`base64_to_file`)
|
|
70
|
+
|
|
71
|
+
Decodes a Base64 file directly back into a binary file and returns the number of decoded bytes written.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
import blitzb64
|
|
75
|
+
|
|
76
|
+
# Strict Base64 decode (fast path, raises ValueError on invalid characters)
|
|
77
|
+
decoded_bytes = blitzb64.base64_to_file("photo.b64", "photo_restored.jpg")
|
|
78
|
+
print(f"Decoded {decoded_bytes:,} bytes")
|
|
79
|
+
|
|
80
|
+
# URL-safe Base64 file
|
|
81
|
+
blitzb64.base64_to_file("photo.urlsafe.b64", "photo_restored.jpg", urlsafe=True)
|
|
82
|
+
|
|
83
|
+
# MIME / line-wrapped Base64 file (ignores '\r', '\n', spaces)
|
|
84
|
+
blitzb64.base64_to_file("mime_wrapped.b64", "output.bin", ignore_whitespace=True)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. Read a File Directly into Base64 `bytes` (`file_to_b64_bytes`)
|
|
88
|
+
|
|
89
|
+
Skips `open(path, "rb").read()` and encodes straight from mapped file pages into a single Python `bytes` object:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import blitzb64
|
|
93
|
+
|
|
94
|
+
b64_bytes = blitzb64.file_to_b64_bytes("photo.jpg")
|
|
95
|
+
# Or as a UTF-8/ASCII string:
|
|
96
|
+
b64_text = blitzb64.file_to_b64_bytes("photo.jpg").decode("ascii")
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 4. Write Base64 `bytes` Directly to a File (`b64_bytes_to_file`)
|
|
100
|
+
|
|
101
|
+
Decodes a Python `bytes`, `bytearray`, or `memoryview` directly into a file without creating an intermediate decoded `bytes` object in Python:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import blitzb64
|
|
105
|
+
|
|
106
|
+
n = blitzb64.b64_bytes_to_file(b64_bytes, "restored.jpg")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 5. In-Memory `base64` Drop-In Functions
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
import blitzb64
|
|
113
|
+
|
|
114
|
+
encoded = blitzb64.b64encode(b"Hello, World!")
|
|
115
|
+
decoded = blitzb64.b64decode(encoded)
|
|
116
|
+
|
|
117
|
+
# Standard & URL-safe helpers
|
|
118
|
+
std_enc = blitzb64.standard_b64encode(b"data")
|
|
119
|
+
std_dec = blitzb64.standard_b64decode(std_enc)
|
|
120
|
+
|
|
121
|
+
url_enc = blitzb64.urlsafe_b64encode(b"\xfb\xff\xbf")
|
|
122
|
+
url_dec = blitzb64.urlsafe_b64decode(url_enc)
|
|
123
|
+
|
|
124
|
+
# Non-strict / line-wrapped in-memory decode (accepts bytes or ASCII str)
|
|
125
|
+
raw = blitzb64.b64decode("SGVs\nbG8h\n", validate=False)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## API Reference
|
|
131
|
+
|
|
132
|
+
| Function | Description |
|
|
133
|
+
|---|---|
|
|
134
|
+
| `file_to_base64(src_path, dst_path, *, urlsafe=False, threads=0) -> int` | Encode `src_path` directly to `dst_path`; returns bytes written. |
|
|
135
|
+
| `base64_to_file(src_path, dst_path, *, urlsafe=False, validate=True, ignore_whitespace=False, threads=0) -> int` | Decode `src_path` directly to `dst_path`; returns bytes written. |
|
|
136
|
+
| `file_to_b64_bytes(src_path, *, urlsafe=False, threads=0) -> bytes` | Read & encode `src_path` directly into `bytes`. |
|
|
137
|
+
| `b64_bytes_to_file(data, dst_path, *, urlsafe=False, validate=True, ignore_whitespace=False, threads=0) -> int` | Decode `data` directly to `dst_path`; returns bytes written. |
|
|
138
|
+
| `b64encode(data, *, urlsafe=False, threads=0) -> bytes` | Encode `bytes`/`bytearray`/`memoryview` to Base64 `bytes`. |
|
|
139
|
+
| `b64decode(data, *, urlsafe=False, validate=True, threads=0) -> bytes` | Decode Base64 `bytes`-like or ASCII `str` to `bytes`. |
|
|
140
|
+
| `standard_b64encode(data, *, threads=0) -> bytes` | Standard Base64 encode. |
|
|
141
|
+
| `standard_b64decode(data, *, threads=0) -> bytes` | Standard Base64 decode (`validate=False`). |
|
|
142
|
+
| `urlsafe_b64encode(data, *, threads=0) -> bytes` | URL-safe Base64 encode (`-` / `_`). |
|
|
143
|
+
| `urlsafe_b64decode(data, *, threads=0) -> bytes` | URL-safe Base64 decode (`validate=False`). |
|
|
144
|
+
| `simd_info() -> dict` | Inspect active SIMD ISA (`avx512_vbmi`, `avx2`, `ssse3`, `neon`, `scalar`) and hardware thread count. |
|
|
145
|
+
|
|
146
|
+
- **`src_path` / `dst_path`**: accepts `str`, `bytes`, or `pathlib.Path` (`os.PathLike`).
|
|
147
|
+
- **`threads`**: `0` (default) uses all available logical CPU cores for payloads `>= 256 KiB`. Pass `threads=1` to force single-threaded execution.
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT License — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
blitzb64
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "blitzb64"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "High-speed multithreaded AVX2 C++ Base64 and file-to-Base64 / Base64-to-file library"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
keywords = ["base64", "simd", "avx2", "file", "mmap", "fast"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Operating System :: Microsoft :: Windows",
|
|
18
|
+
"Operating System :: POSIX :: Linux",
|
|
19
|
+
"Operating System :: MacOS",
|
|
20
|
+
"Programming Language :: C++",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
"Topic :: Utilities",
|
|
28
|
+
]
|
blitzb64-0.1.0/setup.cfg
ADDED
blitzb64-0.1.0/setup.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import shutil
|
|
2
|
+
import subprocess
|
|
3
|
+
import sys
|
|
4
|
+
import sysconfig
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from setuptools import Extension, find_packages, setup
|
|
8
|
+
from setuptools.command.build_ext import build_ext
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PortableBuildExt(build_ext):
|
|
12
|
+
def build_extensions(self):
|
|
13
|
+
if sys.platform == "win32" and self.compiler.compiler_type == "msvc":
|
|
14
|
+
try:
|
|
15
|
+
self.compiler.initialize()
|
|
16
|
+
use_msvc = True
|
|
17
|
+
except Exception:
|
|
18
|
+
use_msvc = False
|
|
19
|
+
|
|
20
|
+
if use_msvc:
|
|
21
|
+
for ext in self.extensions:
|
|
22
|
+
ext.extra_compile_args = ["/O2", "/std:c++17", "/EHsc"]
|
|
23
|
+
super().build_extensions()
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
cxx = shutil.which("clang++") or shutil.which("g++")
|
|
27
|
+
if not cxx:
|
|
28
|
+
raise RuntimeError("Neither MSVC (cl.exe) nor clang++/g++ was found to build C++ extension.")
|
|
29
|
+
|
|
30
|
+
include_dir = sysconfig.get_path("include")
|
|
31
|
+
ver = f"{sys.version_info.major}{sys.version_info.minor}"
|
|
32
|
+
lib_dir = Path(sysconfig.get_config_var("LIBDIR") or (Path(sys.base_prefix) / "libs"))
|
|
33
|
+
py_lib = lib_dir / f"python{ver}.lib"
|
|
34
|
+
|
|
35
|
+
for ext in self.extensions:
|
|
36
|
+
out_path = Path(self.get_ext_fullpath(ext.name))
|
|
37
|
+
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
38
|
+
cmd = [
|
|
39
|
+
cxx,
|
|
40
|
+
"-O3",
|
|
41
|
+
"-std=c++17",
|
|
42
|
+
"-shared",
|
|
43
|
+
"-static-libgcc",
|
|
44
|
+
"-static-libstdc++",
|
|
45
|
+
f"-I{include_dir}",
|
|
46
|
+
*ext.sources,
|
|
47
|
+
str(py_lib),
|
|
48
|
+
"-o",
|
|
49
|
+
str(out_path),
|
|
50
|
+
]
|
|
51
|
+
print("Building extension with:", " ".join(cmd))
|
|
52
|
+
subprocess.check_call(cmd)
|
|
53
|
+
return
|
|
54
|
+
|
|
55
|
+
for ext in self.extensions:
|
|
56
|
+
ext.extra_compile_args = ["-O3", "-std=c++17", "-pthread"]
|
|
57
|
+
ext.extra_link_args = ["-pthread"]
|
|
58
|
+
super().build_extensions()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
ext = Extension(
|
|
62
|
+
"blitzb64._blitzb64",
|
|
63
|
+
sources=["src/_blitzb64.cpp"],
|
|
64
|
+
language="c++",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
setup(
|
|
68
|
+
name="blitzb64",
|
|
69
|
+
version="0.1.0",
|
|
70
|
+
packages=find_packages(),
|
|
71
|
+
ext_modules=[ext],
|
|
72
|
+
cmdclass={"build_ext": PortableBuildExt},
|
|
73
|
+
)
|