Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
steps:

- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 2026.01.09
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - 2026.01.09
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
Expand All @@ -47,10 +47,29 @@ jobs:
- name: C++ Performance Tests
run: make perf

# # # # java:

- name: Set up JDK
uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # 5.2.0 - 2026.0
with:
java-version: '21'
distribution: 'temurin'

- name: Java Compilation
run: javac -d out src/main/java/base91x/Base91x.java src/test/java/base91x/Base91xTest.java

- name: Java Test
run: java -cp out base91x.Base91xTest

# # # # js:

- name: JS Test
run: node --test

# # # # python:

- name: Set up latest Python
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 2026.01.22
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 - 2026.01.22
with:
python-version: 3.x

Expand All @@ -64,7 +83,7 @@ jobs:
run: pytest --random-order-bucket=global --cov=src --cov-report xml:xmlcov/coverage.xml

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@1af58845a975a7985b0beb0cbe6fbbb71a41dbad # v5.5.3 2026.03.18
uses: codecov/codecov-action@1af58845a975a7985b0beb0cbe6fbbb71a41dbad # v5.5.3 - 2026.03.18
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: xmlcov/coverage.xml
Expand Down Expand Up @@ -134,7 +153,7 @@ jobs:

- name: Failure data upload
if: ${{ failure() }}
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 2026.02.26
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0 - 2026.02.26
with:
name: crash_data
path: ./*.dat
Expand All @@ -143,7 +162,7 @@ jobs:

- name: Publish
if: ${{ 'release' == github.event_name }}
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0 2026.09.04
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0 - 2026.09.04
with:
user: __token__
password: ${{ secrets.PYPI_TOKEN }}
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ https://pypi.org/project/base91x/
[![License](https://img.shields.io/badge/licence-MIT-green.svg?style=flat)](LICENSE)
[![Python](https://img.shields.io/pypi/pyversions/base91x.svg)](https://badge.fury.io/py/base91x)

This base91x method provides data encoding and decoding
This base91x method provides data encoding and decoding
using numeric system of base 91 with specific alphabet that does not require
escaping any symbols in C, C++ (and many other languages?) string.
'x' - means the alphabet was obtained with XOR function.
Expand All @@ -23,11 +23,11 @@ The alphabet contains visible printable characters of ASCII except:

`\` Backslash

An encoded string might be used for JSON string if JSON does not require to escape / Slash.
An encoded string might be used for JSON string if JSON does not require to escape ``/`` Slash.

Encoded string size ~ 16 * original size / 13.

There is possibility to extend the algorithm to use 89 codes during decode.
There is possibility to extend the algorithm to use 89 codes during decode. (breaks compatibility)

The alphabet transforms from base91 value with operation XOR(0x7F) with the tree exceptions.

Expand All @@ -47,4 +47,14 @@ char string[]=R"%%%( a string )%%%";
Workaround: use space | line feed | tab in encoded text to break wrong sequence due the algorithm skips non alphabet symbols.
e.g. ``Ma^7*/0629`` -> ``Ma^7* /0629``

HTML built-in encoding may hurt with ``</script>`` and so on, but it is almost impossible to appear the symbols from binary data.
Anyway - extra spaces do the workaround to fix parsing issue. ``< / script >`` is not valid closed tag.

The algorithm is not compatible with https://base91.sourceforge.net/

Current realization for:
- [Python](./src/base91x/base91x.py)
- [C](./src/base91x.c)
- [C++](./include/base91x.hpp) (header realization)
- [Java](./src/main/java/base91x/Base91x.java)
- [JS](./src/base91x.js)
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "js-base91x-lib",
"version": "1.0.0",
"type": "module",
"scripts": {
"test": "node --test"
}
}
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
pythonpath = src
testpaths = tests
addopts = -ra -v
53 changes: 30 additions & 23 deletions src/base91x.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,40 @@ static const unsigned b91word_mask = 0x1FFF;

/** base91x JSON OPTIMIZED ALPHABET: */
// clang-format off
static const unsigned char BASE91X_ALPHABET[BASE91X_LEN] = {'!', '~', '}', '|', '{', 'z', 'y', 'x', 'w', 'v',
'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l',
'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b',
'a', '`', '_', '^', ']', '#', '[', 'Z', 'Y', 'X',
'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N',
'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D',
'C', 'B', 'A', '@', '?', '>', '=', '<', ';', ':',
'9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
'/', '.', '-', ',', '+', '*', ')', '(', '$', '&',
'%'};
static const unsigned char BASE91X_ALPHABET[BASE91X_LEN] = {
'!', '~', '}', '|', '{', 'z', 'y', 'x', 'w', 'v',
'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l',
'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b',
'a', '`', '_', '^', ']', '#', '[', 'Z', 'Y', 'X',
'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N',
'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D',
'C', 'B', 'A', '@', '?', '>', '=', '<', ';', ':',
'9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
'/', '.', '-', ',', '+', '*', ')', '(', '$', '&',
'%'
};
// clang-format on

/** base91x reverse table for quick decoding from an 8-bit unsigned index */
// clang-format off
static const char BASE91X_ZYX[0x100] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 0, -1, 35, 88, 90, 89, -1, 87, 86, 85, 84, 83, 82, 81, 80,
79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64,
63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48,
47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, -1, 34, 33, 32,
31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
static const char BASE91X_ZYX[0x100] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, +0, -1, 35, 88, 90, 89, -1, 87, 86, 85, 84, 83, 82, 81, 80,
79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64,
63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48,
47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, -1, 34, 33, 32,
31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10, +9, +8, +7, +6, +5, +4, +3, +2, +1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
// clang-format on

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Expand Down
140 changes: 140 additions & 0 deletions src/base91x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
MIT License

Copyright (c) 2026 Roman Babenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

const BASE91X_LEN = 91;
const CHAR_BIT = 8;
const B91WORD_BIT = 13;
const B91WORD_MASK = 0x1FFF;

const BASE91X_ALPHABET = [
'!', '~', '}', '|', '{', 'z', 'y', 'x', 'w', 'v',
'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l',
'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b',
'a', '`', '_', '^', ']', '#', '[', 'Z', 'Y', 'X',
'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N',
'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D',
'C', 'B', 'A', '@', '?', '>', '=', '<', ';', ':',
'9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
'/', '.', '-', ',', '+', '*', ')', '(', '$', '&',
'%'
];

const BASE91X_ZYX = new Int16Array([
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, +0, -1, 35, 88, 90, 89, -1, 87, 86, 85, 84, 83, 82, 81, 80,
79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64,
63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48,
47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, -1, 34, 33, 32,
31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10, +9, +8, +7, +6, +5, +4, +3, +2, +1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
]);


export function encode(input) {
let collector = 0;
let bitCollected = 0;

const out = [];

for (let i = 0; i < input.length; i++) {
collector |= input[i] << bitCollected;
bitCollected += CHAR_BIT;

while (bitCollected >= B91WORD_BIT) {
const value = collector & B91WORD_MASK;

const quot = (value / BASE91X_LEN) | 0;
const rem = value - quot * BASE91X_LEN;

out.push(BASE91X_ALPHABET[rem]);
out.push(BASE91X_ALPHABET[quot]);

collector >>= B91WORD_BIT;
bitCollected -= B91WORD_BIT;
}
}

if (bitCollected !== 0) {
const value = collector & B91WORD_MASK;

const rem = value % BASE91X_LEN;
const quot = Math.floor(value / BASE91X_LEN);

out.push(BASE91X_ALPHABET[rem]);

if (bitCollected >= 7) {
out.push(BASE91X_ALPHABET[quot]);
}
}

return out.join("");
}


export function decode(str) {
let collector = 0;
let bitCollected = 0;
let lower = -1;

const out = [];

for (let i = 0; i < str.length; i++) {
const digit = BASE91X_ZYX[str.charCodeAt(i)];
if (digit === -1) continue;

if (lower === -1) {
lower = digit;
continue;
}

collector |= (BASE91X_LEN * digit + lower) << bitCollected;
bitCollected += B91WORD_BIT;
lower = -1;

while (bitCollected >= CHAR_BIT) {
out.push(collector & 0xFF);
collector >>= CHAR_BIT;
bitCollected -= CHAR_BIT;
}
}
if (lower !== -1) {
collector |= lower << bitCollected;
bitCollected += (CHAR_BIT - 1);
}

if (bitCollected >= CHAR_BIT) {
out.push(collector & 0xFF);
}

return Uint8Array.from(out);
}
Loading
Loading