Skip to content

Switch from Base64 to Z85 #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
110 changes: 0 additions & 110 deletions src/base64.js

This file was deleted.

47 changes: 21 additions & 26 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decode64 } from './base64.js';
import { decode85 } from './z85.js';
import {
HOLE,
NAN,
Expand Down Expand Up @@ -102,31 +102,26 @@ export function unflatten(parsed, revivers) {
}
break;

case "Int8Array":
case "Uint8Array":
case "Uint8ClampedArray":
case "Int16Array":
case "Uint16Array":
case "Int32Array":
case "Uint32Array":
case "Float32Array":
case "Float64Array":
case "BigInt64Array":
case "BigUint64Array": {
const TypedArrayConstructor = globalThis[type];
const base64 = value[1];
const arraybuffer = decode64(base64);
const typedArray = new TypedArrayConstructor(arraybuffer);
hydrated[index] = typedArray;
break;
}

case "ArrayBuffer": {
const base64 = value[1];
const arraybuffer = decode64(base64);
hydrated[index] = arraybuffer;
break;
}
case "Int8Array":
case "Uint8Array":
case "Uint8ClampedArray":
case "Int16Array":
case "Uint16Array":
case "Int32Array":
case "Uint32Array":
case "Float32Array":
case "Float64Array":
case "BigInt64Array":
case "BigUint64Array": {
const TypedArrayConstructor = globalThis[type];
hydrated[index] = new TypedArrayConstructor(decode85(value[1]));
break;
}

case "ArrayBuffer": {
hydrated[index] = decode85(value[1]);
break;
}

default:
throw new Error(`Unknown type ${type}`);
Expand Down
14 changes: 6 additions & 8 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
POSITIVE_INFINITY,
UNDEFINED
} from './constants.js';
import { encode64 } from './base64.js';
import { encode85 } from './z85.js';

/**
* Turn a value into a JSON string that can be parsed with `devalue.parse`
Expand Down Expand Up @@ -153,20 +153,18 @@ export function stringify(value, reducers) {
case "BigUint64Array": {
/** @type {import("./types.js").TypedArray} */
const typedArray = thing;
const base64 = encode64(typedArray.buffer);
str = '["' + type + '","' + base64 + '"]';
str = '["' + type + '","' + encode85(typedArray.buffer) + '"]';
break;
}

case "ArrayBuffer": {
/** @type {ArrayBuffer} */
const arraybuffer = thing;
const base64 = encode64(arraybuffer);

str = `["ArrayBuffer","${base64}"]`;

str = `["ArrayBuffer","${encode85(arraybuffer)}"]`;
break;
}

default:
if (!is_plain_object(thing)) {
throw new DevalueError(
Expand Down
62 changes: 62 additions & 0 deletions src/z85.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const ENCODE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
const DECODE = [-1, 68, -1, 84, 83, 82, 72, -1, 75, 76, 70, 65, -1, 63, 62, 69, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 64, -1, 73, 66, 74, 71, 81, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, -1, 78, 67, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 79, -1, 80, -1, -1];
const POW_85 = [1, 85, 7225, 614125, 52200625];
const POW_256 = [1, 256, 65536, 16777216];

/**
* Encodes binary data into a Z85 string.
* @param {ArrayBuffer} data - The binary data to encode
* @returns {string} The Z85 encoded string
*/
export function encode85(data) {
const u8a = new Uint8Array(data);
const length = u8a.length;
const padding = (4 - (length % 4)) % 4;

let result = '', value = 0;
for (let i = 0; i < length + padding; ++i) {
const isPadding = i >= length;
value = value * 256 + (isPadding ? 0 : u8a[i]);
if ((i + 1) % 4 !== 0) continue;

if (!isPadding) {
result += ENCODE[Math.floor(value / POW_85[4]) % 85];
result += ENCODE[Math.floor(value / POW_85[3]) % 85];
result += ENCODE[Math.floor(value / POW_85[2]) % 85];
result += ENCODE[Math.floor(value / POW_85[1]) % 85];
result += ENCODE[value % 85];
} else {
for (let j = 5; j > padding; --j) {
result += ENCODE[Math.floor(value / POW_85[j - 1]) % 85];
}
}
value = 0;
}

return result;
};

/**
* Decodes a Z85 string into binary data.
* @param {string} string - The Z85 encoded string
* @returns {ArrayBuffer} The decoded binary data
*/
export function decode85(string) {
const remainder = string.length % 5;
const padding = 5 - (remainder === 0 ? 5 : remainder);
string = string.padEnd(string.length + padding, ENCODE[ENCODE.length - 1]);
const length = string.length;

let buffer = new Uint8Array((length * 4 / 5) - padding);
let value = 0, char = 0, byte = 0;
for (let i = 0; i < length; ++i) {
value = value * 85 + DECODE[string.charCodeAt(char++) - 32];
if (char % 5 !== 0) continue;

for (let j = 3; j >= 0; --j)
buffer[byte++] = Math.floor(value / POW_256[j]) % 256;
value = 0;
}

return buffer.buffer;
}
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ const fixtures = {
name: 'Uint8Array',
value: new Uint8Array([1, 2, 3]),
js: 'new Uint8Array([1,2,3])',
json: '[["Uint8Array","AQID"]]'
json: '[["Uint8Array","0rJu"]]'
},
{
name: 'ArrayBuffer',
value: new Uint8Array([1, 2, 3]).buffer,
js: 'new Uint8Array([1,2,3]).buffer',
json: '[["ArrayBuffer","AQID"]]'
json: '[["ArrayBuffer","0rJu"]]'
}
],

Expand Down