Fast Ascii85 Encode/Decode for JavaScript – base85.js

Category: Javascript | December 24, 2024
AuthorAlttiRi
Last UpdateDecember 24, 2024
LicenseMIT
Tags
Views77 views
Fast Ascii85 Encode/Decode for JavaScript – base85.js

base85.js is a tiny JavaScript utility library that provides a fast way to encode Uint8Array data into a base85 (Ascii85) string and decode base85 strings back into Uint8Array.

The library processes binary data using two established Base85 variants:

  • ASCII85 encoding with a 94-character ASCII set
  • Z85 encoding using an 85-character subset optimized for source code

Base85 achieves 4:5 compression efficiency – encoding 4 bytes of binary data into 5 ASCII characters. This produces more compact output compared to Base64, which uses a 6:8 ratio.

Use Cases:

  • Data Embedding: Embed binary data such as images, PDFs, or multimedia files within a text-based format like JSON or XML.
  • Efficient Data Transfer: Use Base85 encoding to send large binary data over protocols that support only text, like email or API requests.
  • Compact Data Storage: Convert binary files into a text format for storage in databases or files that expect only text.

How to use it:

1. Install base85 package with NPM.

# NPM
$ npm install @alttiri/base85

2. Import the encodeBase85 and decodeBase85 modules into your JavaScript:

import { encodeBase85, decodeBase85 } from "@alttiri/base85";

3. Encode Uint8Array to Base85 String. The encodeBase85 function takes a Uint8Array as input and returns its base85 encoded string representation. You can optionally specify a character set: “ascii85” or “z85”.

encodeBase85(ui8a: Uint8Array, charset?: "ascii85" | "z85" | string): string
const myData = new Uint8Array(/* Uint8Array Data */);
const base85 = encodeBase85(myData);
// => base85 string
console.log(base85);
// => base85 string length
console.log(base85.length);

4. Decode a Base85 string back into a Uint8Array.

//decode85(base85: string, charset?: "ascii85" | "z85" | string): Uint8Array
const myData = "Base 85Data Here"
const result = decodeBase85(myData);

How it works:

This library implements the base85 encoding by treating every four bytes of the input Uint8Array as a 32-bit unsigned integer. This integer is then divided repeatedly by 85, and the remainders are mapped to characters from the chosen character set (either the default ‘z85’, ‘ascii85’, or a custom 85-character string). The encoding process iterates through the input data in chunks of four bytes. For any remaining bytes less than four, the library pads them with zeros to form a complete four-byte sequence before encoding.

Decoding reverses this process. It takes every five characters from the base85 string and uses their positions in the character set to reconstruct a 32-bit integer. This is done by multiplying the character’s index by powers of 85 and summing the results. The resulting integer is then split back into four bytes. Padding added during encoding is handled to ensure the correct length of the decoded Uint8Array. The library uses efficient bitwise operations and data views for fast processing of the encoding and decoding tasks.

You Might Be Interested In:


Leave a Reply