This commit is contained in:
2025-01-04 00:34:03 +01:00
parent 41829408dc
commit 0ca14bbc19
18111 changed files with 1871397 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
const alphabetByEncoding = {};
const alphabetByValue = new Array(64);
for (let i = 0, start = "A".charCodeAt(0), limit = "Z".charCodeAt(0); i + start <= limit; i++) {
const char = String.fromCharCode(i + start);
alphabetByEncoding[char] = i;
alphabetByValue[i] = char;
}
for (let i = 0, start = "a".charCodeAt(0), limit = "z".charCodeAt(0); i + start <= limit; i++) {
const char = String.fromCharCode(i + start);
const index = i + 26;
alphabetByEncoding[char] = index;
alphabetByValue[index] = char;
}
for (let i = 0; i < 10; i++) {
alphabetByEncoding[i.toString(10)] = i + 52;
const char = i.toString(10);
const index = i + 52;
alphabetByEncoding[char] = index;
alphabetByValue[index] = char;
}
alphabetByEncoding["+"] = 62;
alphabetByValue[62] = "+";
alphabetByEncoding["/"] = 63;
alphabetByValue[63] = "/";
const bitsPerLetter = 6;
const bitsPerByte = 8;
const maxLetterValue = 0b111111;
export { alphabetByEncoding, alphabetByValue, bitsPerLetter, bitsPerByte, maxLetterValue };

View File

@@ -0,0 +1,36 @@
import { alphabetByEncoding, bitsPerByte, bitsPerLetter } from "./constants.browser";
export const fromBase64 = (input) => {
let totalByteLength = (input.length / 4) * 3;
if (input.slice(-2) === "==") {
totalByteLength -= 2;
}
else if (input.slice(-1) === "=") {
totalByteLength--;
}
const out = new ArrayBuffer(totalByteLength);
const dataView = new DataView(out);
for (let i = 0; i < input.length; i += 4) {
let bits = 0;
let bitLength = 0;
for (let j = i, limit = i + 3; j <= limit; j++) {
if (input[j] !== "=") {
if (!(input[j] in alphabetByEncoding)) {
throw new TypeError(`Invalid character ${input[j]} in base64 string.`);
}
bits |= alphabetByEncoding[input[j]] << ((limit - j) * bitsPerLetter);
bitLength += bitsPerLetter;
}
else {
bits >>= bitsPerLetter;
}
}
const chunkOffset = (i / 4) * 3;
bits >>= bitLength % bitsPerByte;
const byteLength = Math.floor(bitLength / bitsPerByte);
for (let k = 0; k < byteLength; k++) {
const offset = (byteLength - k - 1) * bitsPerByte;
dataView.setUint8(chunkOffset + k, (bits & (255 << offset)) >> offset);
}
}
return new Uint8Array(out);
};

View File

@@ -0,0 +1,12 @@
import { fromString } from "@smithy/util-buffer-from";
const BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;
export const fromBase64 = (input) => {
if ((input.length * 3) % 4 !== 0) {
throw new TypeError(`Incorrect padding on base64 string.`);
}
if (!BASE64_REGEX.exec(input)) {
throw new TypeError(`Invalid base64 string.`);
}
const buffer = fromString(input, "base64");
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
};

View File

@@ -0,0 +1,2 @@
export * from "./fromBase64";
export * from "./toBase64";

View File

@@ -0,0 +1,35 @@
import { fromUtf8 } from "@smithy/util-utf8";
import { alphabetByValue, bitsPerByte, bitsPerLetter, maxLetterValue } from "./constants.browser";
export function toBase64(_input) {
let input;
if (typeof _input === "string") {
input = fromUtf8(_input);
}
else {
input = _input;
}
const isArrayLike = typeof input === "object" && typeof input.length === "number";
const isUint8Array = typeof input === "object" &&
typeof input.byteOffset === "number" &&
typeof input.byteLength === "number";
if (!isArrayLike && !isUint8Array) {
throw new Error("@smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array.");
}
let str = "";
for (let i = 0; i < input.length; i += 3) {
let bits = 0;
let bitLength = 0;
for (let j = i, limit = Math.min(i + 3, input.length); j < limit; j++) {
bits |= input[j] << ((limit - j - 1) * bitsPerByte);
bitLength += bitsPerByte;
}
const bitClusterCount = Math.ceil(bitLength / bitsPerLetter);
bits <<= bitClusterCount * bitsPerLetter - bitLength;
for (let k = 1; k <= bitClusterCount; k++) {
const offset = (bitClusterCount - k) * bitsPerLetter;
str += alphabetByValue[(bits & (maxLetterValue << offset)) >> offset];
}
str += "==".slice(0, 4 - bitClusterCount);
}
return str;
}

View File

@@ -0,0 +1,15 @@
import { fromArrayBuffer } from "@smithy/util-buffer-from";
import { fromUtf8 } from "@smithy/util-utf8";
export const toBase64 = (_input) => {
let input;
if (typeof _input === "string") {
input = fromUtf8(_input);
}
else {
input = _input;
}
if (typeof input !== "object" || typeof input.byteOffset !== "number" || typeof input.byteLength !== "number") {
throw new Error("@smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array.");
}
return fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("base64");
};