2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-08-11 11:51:20 +00:00
|
|
|
|
|
|
|
/**
|
2024-01-25 14:08:29 +00:00
|
|
|
* Utilities for working with {@link https://en.wikipedia.org/wiki/Ascii85 | ascii85} encoding.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* ## Specifying a standard and delimiter
|
|
|
|
*
|
|
|
|
* By default, all functions are using the most popular Adobe version of ascii85
|
|
|
|
* and not adding any delimiter. However, there are three more standards
|
|
|
|
* supported - btoa (different delimiter and additional compression of 4 bytes
|
2024-01-25 14:08:29 +00:00
|
|
|
* equal to 32), {@link https://rfc.zeromq.org/spec/32/ | Z85} and
|
2024-05-20 07:14:09 +00:00
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc1924.html | RFC 1924}. It's possible to use a
|
2022-11-25 11:40:23 +00:00
|
|
|
* different encoding by specifying it in `options` object as a second parameter.
|
|
|
|
*
|
|
|
|
* Similarly, it's possible to make `encode` add a delimiter (`<~` and `~>` for
|
|
|
|
* Adobe, `xbtoa Begin` and `xbtoa End` with newlines between the delimiters and
|
|
|
|
* encoded data for btoa. Checksums for btoa are not supported. Delimiters are not
|
|
|
|
* supported by other encodings.)
|
|
|
|
*
|
2022-08-11 11:51:20 +00:00
|
|
|
* @module
|
|
|
|
*/
|
2020-07-14 18:26:49 +00:00
|
|
|
|
2024-05-28 03:12:24 +00:00
|
|
|
import { validateBinaryLike } from "./_validate_binary_like.ts";
|
2024-01-25 14:08:29 +00:00
|
|
|
|
2024-05-30 09:43:30 +00:00
|
|
|
/**
|
|
|
|
* Supported ascii85 standards for {@linkcode EncodeAscii85Options} and
|
|
|
|
* {@linkcode DecodeAscii85Options}.
|
|
|
|
*/
|
2020-07-14 18:26:49 +00:00
|
|
|
export type Ascii85Standard = "Adobe" | "btoa" | "RFC 1924" | "Z85";
|
2022-08-11 11:51:20 +00:00
|
|
|
|
2024-05-30 09:43:30 +00:00
|
|
|
/** Options for {@linkcode encodeAscii85}. */
|
|
|
|
export interface EncodeAscii85Options {
|
2023-12-13 02:57:59 +00:00
|
|
|
/**
|
|
|
|
* Character set and delimiter (if supported and used).
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @default {"Adobe"}
|
|
|
|
*/
|
2020-07-14 18:26:49 +00:00
|
|
|
standard?: Ascii85Standard;
|
2023-12-13 02:57:59 +00:00
|
|
|
/**
|
|
|
|
* Whether to use a delimiter (if supported).
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
2020-07-14 18:26:49 +00:00
|
|
|
delimiter?: boolean;
|
|
|
|
}
|
2024-05-30 09:43:30 +00:00
|
|
|
|
2020-07-14 18:26:49 +00:00
|
|
|
const rfc1924 =
|
2024-02-24 20:24:08 +00:00
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~" as const;
|
2020-07-14 18:26:49 +00:00
|
|
|
const Z85 =
|
2024-02-24 20:24:08 +00:00
|
|
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#" as const;
|
2023-09-21 09:29:13 +00:00
|
|
|
|
2020-07-14 18:26:49 +00:00
|
|
|
/**
|
2024-05-27 04:17:51 +00:00
|
|
|
* Converts data into an ascii85-encoded string.
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @param data The data to encode.
|
|
|
|
* @param options Options for encoding.
|
|
|
|
*
|
|
|
|
* @returns The ascii85-encoded string.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { encodeAscii85 } from "@std/encoding/ascii85";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(encodeAscii85("Hello world!"), "87cURD]j7BEbo80");
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2020-07-14 18:26:49 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function encodeAscii85(
|
|
|
|
data: ArrayBuffer | Uint8Array | string,
|
2024-05-30 09:43:30 +00:00
|
|
|
options: EncodeAscii85Options = {},
|
2023-09-21 09:29:13 +00:00
|
|
|
): string {
|
2023-09-22 09:47:24 +00:00
|
|
|
let uint8 = validateBinaryLike(data);
|
2023-09-21 09:29:13 +00:00
|
|
|
|
2024-05-30 09:43:30 +00:00
|
|
|
const { standard = "Adobe" } = options;
|
|
|
|
|
2024-03-14 11:18:00 +00:00
|
|
|
let output: string[] = [];
|
|
|
|
let v: number;
|
|
|
|
let n = 0;
|
|
|
|
let difference = 0;
|
2020-07-14 18:26:49 +00:00
|
|
|
if (uint8.length % 4 !== 0) {
|
|
|
|
const tmp = uint8;
|
|
|
|
difference = 4 - (tmp.length % 4);
|
|
|
|
uint8 = new Uint8Array(tmp.length + difference);
|
|
|
|
uint8.set(tmp);
|
|
|
|
}
|
2023-04-11 08:18:53 +00:00
|
|
|
const view = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
|
2024-03-14 11:18:00 +00:00
|
|
|
for (let i = 0; i < uint8.length; i += 4) {
|
2020-07-14 18:26:49 +00:00
|
|
|
v = view.getUint32(i);
|
|
|
|
// Adobe and btoa standards compress 4 zeroes to single "z" character
|
|
|
|
if (
|
|
|
|
(standard === "Adobe" || standard === "btoa") &&
|
|
|
|
v === 0 &&
|
2024-03-14 11:18:00 +00:00
|
|
|
i < uint8.length - difference - 3
|
2020-07-14 18:26:49 +00:00
|
|
|
) {
|
|
|
|
output[n++] = "z";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// btoa compresses 4 spaces - that is, bytes equal to 32 - into single "y" character
|
|
|
|
if (standard === "btoa" && v === 538976288) {
|
|
|
|
output[n++] = "y";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (let j = 4; j >= 0; j--) {
|
|
|
|
output[n + j] = String.fromCharCode((v % 85) + 33);
|
|
|
|
v = Math.trunc(v / 85);
|
|
|
|
}
|
|
|
|
n += 5;
|
|
|
|
}
|
|
|
|
switch (standard) {
|
|
|
|
case "Adobe":
|
|
|
|
if (options?.delimiter) {
|
|
|
|
return `<~${output.slice(0, output.length - difference).join("")}~>`;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "btoa":
|
|
|
|
if (options?.delimiter) {
|
2020-07-14 19:24:17 +00:00
|
|
|
return `xbtoa Begin\n${
|
|
|
|
output
|
|
|
|
.slice(0, output.length - difference)
|
|
|
|
.join("")
|
|
|
|
}\nxbtoa End`;
|
2020-07-14 18:26:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "RFC 1924":
|
2024-02-24 20:24:08 +00:00
|
|
|
output = output.map((val) => rfc1924[val.charCodeAt(0) - 33]!);
|
2020-07-14 18:26:49 +00:00
|
|
|
break;
|
|
|
|
case "Z85":
|
2024-02-24 20:24:08 +00:00
|
|
|
output = output.map((val) => Z85[val.charCodeAt(0) - 33]!);
|
2020-07-14 18:26:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return output.slice(0, output.length - difference).join("");
|
|
|
|
}
|
2023-09-21 09:29:13 +00:00
|
|
|
|
2024-05-30 09:43:30 +00:00
|
|
|
/** Options for {@linkcode decodeAscii85}. */
|
|
|
|
export type DecodeAscii85Options = Omit<EncodeAscii85Options, "delimiter">;
|
|
|
|
|
2020-07-14 18:26:49 +00:00
|
|
|
/**
|
2024-04-02 10:42:55 +00:00
|
|
|
* Decodes a ascii85-encoded string.
|
|
|
|
*
|
|
|
|
* @param ascii85 The ascii85-encoded string to decode.
|
|
|
|
* @param options Options for decoding.
|
|
|
|
* @returns The decoded data.
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { decodeAscii85 } from "@std/encoding/ascii85";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(
|
|
|
|
* decodeAscii85("87cURD]j7BEbo80"),
|
|
|
|
* new TextEncoder().encode("Hello world!"),
|
|
|
|
* );
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2020-07-14 18:26:49 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function decodeAscii85(
|
|
|
|
ascii85: string,
|
2024-05-30 09:43:30 +00:00
|
|
|
options: DecodeAscii85Options = {},
|
2023-09-21 09:29:13 +00:00
|
|
|
): Uint8Array {
|
2024-05-30 09:43:30 +00:00
|
|
|
const { standard = "Adobe" } = options;
|
|
|
|
|
2020-07-14 18:26:49 +00:00
|
|
|
// translate all encodings to most basic adobe/btoa one and decompress some special characters ("z" and "y")
|
2024-05-30 09:43:30 +00:00
|
|
|
switch (standard) {
|
2020-07-14 18:26:49 +00:00
|
|
|
case "Adobe":
|
|
|
|
ascii85 = ascii85.replaceAll(/(<~|~>)/g, "").replaceAll("z", "!!!!!");
|
|
|
|
break;
|
|
|
|
case "btoa":
|
|
|
|
ascii85 = ascii85
|
|
|
|
.replaceAll(/(xbtoa Begin|xbtoa End|\n)/g, "")
|
|
|
|
.replaceAll("z", "!!!!!")
|
|
|
|
.replaceAll("y", "+<VdL");
|
|
|
|
break;
|
|
|
|
case "RFC 1924":
|
2020-07-14 19:24:17 +00:00
|
|
|
ascii85 = ascii85.replaceAll(
|
|
|
|
/./g,
|
|
|
|
(match) => String.fromCharCode(rfc1924.indexOf(match) + 33),
|
2020-07-14 18:26:49 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "Z85":
|
2020-07-14 19:24:17 +00:00
|
|
|
ascii85 = ascii85.replaceAll(
|
|
|
|
/./g,
|
|
|
|
(match) => String.fromCharCode(Z85.indexOf(match) + 33),
|
2020-07-14 18:26:49 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2024-04-02 10:42:55 +00:00
|
|
|
// remove all invalid characters
|
2020-07-14 18:26:49 +00:00
|
|
|
ascii85 = ascii85.replaceAll(/[^!-u]/g, "");
|
2024-03-14 11:18:00 +00:00
|
|
|
const len = ascii85.length;
|
|
|
|
const output = new Uint8Array(len + 4 - (len % 4));
|
2020-07-14 18:26:49 +00:00
|
|
|
const view = new DataView(output.buffer);
|
2024-03-14 11:18:00 +00:00
|
|
|
let v = 0;
|
|
|
|
let n = 0;
|
|
|
|
let max = 0;
|
2020-07-14 19:24:17 +00:00
|
|
|
for (let i = 0; i < len;) {
|
2020-07-14 18:26:49 +00:00
|
|
|
for (max += 5; i < max; i++) {
|
|
|
|
v = v * 85 + (i < len ? ascii85.charCodeAt(i) : 117) - 33;
|
|
|
|
}
|
|
|
|
view.setUint32(n, v);
|
|
|
|
v = 0;
|
|
|
|
n += 4;
|
|
|
|
}
|
|
|
|
return output.slice(0, Math.trunc(len * 0.8));
|
|
|
|
}
|