summaryrefslogtreecommitdiff
path: root/std/encoding
diff options
context:
space:
mode:
authortimonson <54777088+timonson@users.noreply.github.com>2020-10-13 03:12:10 +0200
committerGitHub <noreply@github.com>2020-10-13 12:12:10 +1100
commit1956cb81372b96bc476e74ab43a62a6e60861277 (patch)
tree014dd7c132239eab61cceb0d31b6d27304a8ead5 /std/encoding
parent26639b3bac463768c65f7fc40a1c53317549e1eb (diff)
fix(std/encoding): base64 properly encodes mbc and handles Uint8Arrays (#7807)
Fixes #6094 Fixes #4794
Diffstat (limited to 'std/encoding')
-rw-r--r--std/encoding/base64.ts78
-rw-r--r--std/encoding/base64_test.ts28
-rw-r--r--std/encoding/base64url.ts31
-rw-r--r--std/encoding/base64url_test.ts25
4 files changed, 84 insertions, 78 deletions
diff --git a/std/encoding/base64.ts b/std/encoding/base64.ts
index 4c9aa4059..e631e5b88 100644
--- a/std/encoding/base64.ts
+++ b/std/encoding/base64.ts
@@ -1,41 +1,59 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-/**
- * Converts given data with base64 encoding
- * @param data input to encode
- */
-export function encode(data: string | ArrayBuffer): string {
- if (typeof data === "string") {
- return btoa(data);
- } else {
- const d = new Uint8Array(data);
- let dataString = "";
- for (let i = 0; i < d.length; ++i) {
- dataString += String.fromCharCode(d[i]);
- }
-
- return btoa(dataString);
- }
-}
+// deno-fmt-ignore
+const base64abc = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
+ "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a",
+ "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
+ "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4",
+ "5", "6", "7", "8", "9", "+", "/"];
/**
- * Converts given base64 encoded data back to original
- * @param data input to decode
+ * CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
+ * Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation
+ * @param data
*/
-export function decode(data: string): ArrayBuffer {
- const binaryString = decodeString(data);
- const binary = new Uint8Array(binaryString.length);
- for (let i = 0; i < binary.length; ++i) {
- binary[i] = binaryString.charCodeAt(i);
+export function encode(data: ArrayBuffer | string): string {
+ const uint8 =
+ typeof data === "string"
+ ? new TextEncoder().encode(data)
+ : data instanceof Uint8Array
+ ? data
+ : new Uint8Array(data);
+ let result = "",
+ i;
+ const l = uint8.length;
+ for (i = 2; i < l; i += 3) {
+ result += base64abc[uint8[i - 2] >> 2];
+ result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
+ result += base64abc[((uint8[i - 1] & 0x0f) << 2) | (uint8[i] >> 6)];
+ result += base64abc[uint8[i] & 0x3f];
}
-
- return binary.buffer;
+ if (i === l + 1) {
+ // 1 octet yet to write
+ result += base64abc[uint8[i - 2] >> 2];
+ result += base64abc[(uint8[i - 2] & 0x03) << 4];
+ result += "==";
+ }
+ if (i === l) {
+ // 2 octets yet to write
+ result += base64abc[uint8[i - 2] >> 2];
+ result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
+ result += base64abc[(uint8[i - 1] & 0x0f) << 2];
+ result += "=";
+ }
+ return result;
}
/**
- * Decodes data assuming the output is in string type
- * @param data input to decode
+ * Decodes a given RFC4648 base64 encoded string
+ * @param b64
*/
-export function decodeString(data: string): string {
- return atob(data);
+export function decode(b64: string): Uint8Array {
+ const binString = atob(b64);
+ const size = binString.length;
+ const bytes = new Uint8Array(size);
+ for (let i = 0; i < size; i++) {
+ bytes[i] = binString.charCodeAt(i);
+ }
+ return bytes;
}
diff --git a/std/encoding/base64_test.ts b/std/encoding/base64_test.ts
index 5fe89410b..65c62f4cd 100644
--- a/std/encoding/base64_test.ts
+++ b/std/encoding/base64_test.ts
@@ -1,9 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
import { assertEquals } from "../testing/asserts.ts";
-import { decode, decodeString, encode } from "./base64.ts";
+import { decode, encode } from "./base64.ts";
const testsetString = [
["", ""],
+ ["ß", "w58="],
["f", "Zg=="],
["fo", "Zm8="],
["foo", "Zm9v"],
@@ -12,12 +14,10 @@ const testsetString = [
["foobar", "Zm9vYmFy"],
];
-const testsetBinary = [
- [new TextEncoder().encode("\x00"), "AA=="],
- [new TextEncoder().encode("\x00\x00"), "AAA="],
- [new TextEncoder().encode("\x00\x00\x00"), "AAAA"],
- [new TextEncoder().encode("\x00\x00\x00\x00"), "AAAAAA=="],
-];
+const testsetBinary = testsetString.map(([str, b64]) => [
+ new TextEncoder().encode(str),
+ b64,
+]) as Array<[Uint8Array, string]>;
Deno.test("[encoding/base64] testBase64EncodeString", () => {
for (const [input, output] of testsetString) {
@@ -25,21 +25,21 @@ Deno.test("[encoding/base64] testBase64EncodeString", () => {
}
});
-Deno.test("[encoding/base64] testBase64DecodeString", () => {
- for (const [input, output] of testsetString) {
- assertEquals(decodeString(output), input);
+Deno.test("[encoding/base64] testBase64EncodeBinary", () => {
+ for (const [input, output] of testsetBinary) {
+ assertEquals(encode(input), output);
}
});
-Deno.test("[encoding/base64] testBase64EncodeBinary", () => {
+Deno.test("[encoding/base64] testBase64EncodeBinaryBuffer", () => {
for (const [input, output] of testsetBinary) {
- assertEquals(encode(input), output);
+ assertEquals(encode(input.buffer), output);
}
});
Deno.test("[encoding/base64] testBase64DecodeBinary", () => {
for (const [input, output] of testsetBinary) {
- const outputBinary = new Uint8Array(decode(output as string));
- assertEquals(outputBinary, input as Uint8Array);
+ const outputBinary = decode(output);
+ assertEquals(outputBinary, input);
}
});
diff --git a/std/encoding/base64url.ts b/std/encoding/base64url.ts
index b20878554..fe8019a29 100644
--- a/std/encoding/base64url.ts
+++ b/std/encoding/base64url.ts
@@ -1,13 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-import {
- decode as convertBase64ToArrayBuffer,
- encode as convertArrayBufferToBase64,
-} from "./base64.ts";
+import * as base64 from "./base64.ts";
/*
* Some variants allow or require omitting the padding '=' signs:
* https://en.wikipedia.org/wiki/Base64#URL_applications
+ * @param base64url
*/
export function addPaddingToBase64url(base64url: string): string {
if (base64url.length % 4 === 2) return base64url + "==";
@@ -18,29 +16,26 @@ export function addPaddingToBase64url(base64url: string): string {
return base64url;
}
-function convertBase64urlToBase64(base64url: string): string {
- return addPaddingToBase64url(base64url)
- .replace(/\-/g, "+")
- .replace(/_/g, "/");
+function convertBase64urlToBase64(b64url: string): string {
+ return addPaddingToBase64url(b64url).replace(/\-/g, "+").replace(/_/g, "/");
}
-function convertBase64ToBase64url(base64: string): string {
- return base64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
+function convertBase64ToBase64url(b64: string): string {
+ return b64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
}
/**
- * Converts given data with base64url encoding.
- * Removes paddings '='.
- * @param data input to encode
+ * Encodes a given Uint8Array into a base64url representation
+ * @param uint8
*/
-export function encode(data: string | ArrayBuffer): string {
- return convertBase64ToBase64url(convertArrayBufferToBase64(data));
+export function encode(uint8: Uint8Array): string {
+ return convertBase64ToBase64url(base64.encode(uint8));
}
/**
* Converts given base64url encoded data back to original
- * @param data input to decode
+ * @param b64url
*/
-export function decode(data: string): ArrayBuffer {
- return convertBase64ToArrayBuffer(convertBase64urlToBase64(data));
+export function decode(b64url: string): Uint8Array {
+ return base64.decode(convertBase64urlToBase64(b64url));
}
diff --git a/std/encoding/base64url_test.ts b/std/encoding/base64url_test.ts
index 59d67240f..99dcf260c 100644
--- a/std/encoding/base64url_test.ts
+++ b/std/encoding/base64url_test.ts
@@ -1,30 +1,24 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
import { assertEquals } from "../testing/asserts.ts";
import { decode, encode } from "./base64url.ts";
const testsetString = [
["", ""],
+ ["ß", "w58"],
["f", "Zg"],
["fo", "Zm8"],
["foo", "Zm9v"],
["foob", "Zm9vYg"],
["fooba", "Zm9vYmE"],
["foobar", "Zm9vYmFy"],
- [">?>d?ß", "Pj8-ZD_f"],
-];
-
-const testsetBinary = [
- [new TextEncoder().encode("\x00"), "AA"],
- [new TextEncoder().encode("\x00\x00"), "AAA"],
- [new TextEncoder().encode("\x00\x00\x00"), "AAAA"],
- [new TextEncoder().encode("\x00\x00\x00\x00"), "AAAAAA"],
+ [">?>d?ß", "Pj8-ZD_Dnw"],
];
-Deno.test("[encoding/base64url] testBase64urlEncodeString", () => {
- for (const [input, output] of testsetString) {
- assertEquals(encode(input), output);
- }
-});
+const testsetBinary = testsetString.map(([str, b64]) => [
+ new TextEncoder().encode(str),
+ b64,
+]) as Array<[Uint8Array, string]>;
Deno.test("[encoding/base64url] testBase64urlEncodeBinary", () => {
for (const [input, output] of testsetBinary) {
@@ -32,9 +26,8 @@ Deno.test("[encoding/base64url] testBase64urlEncodeBinary", () => {
}
});
-Deno.test("[encoding/base64ur] testBase64urDecodeBinary", () => {
+Deno.test("[decoding/base64url] testBase64urlDecodeBinary", () => {
for (const [input, output] of testsetBinary) {
- const outputBinary = new Uint8Array(decode(output as string));
- assertEquals(outputBinary, input as Uint8Array);
+ assertEquals(decode(output), input);
}
});