window.ENTITIES={'/api/snippets/javascript/javascript%20-%20json-safe%20base94%20encoding%20and%20decoding':[{"result":true,"message":null,"batch":{"type":"javascript","name":"javascript - json-safe base94 encoding and decoding","items":[{"id":"103oLj","type":"javascript","name":"JavaScript - JSON-safe Base94 encoding and decoding","content":"// ONLINE-RUNNER:browser;\n\n// ⚠️ Warning: this source code contains proposition to Base94 encoding and decoding algorithm that may be used in JSONs as Base64 replacement.\n\n\n// License: you can use it for free for private and commercial, but do not remove information about author and source.\n//\n// Author: Grzegorz G.\n// Source: https://dirask.com/snippets/103oLj\n\n\n// indexes: 0 10 20 30 40 50 60 70 80 90 93\n// | | | | | | | | | | |\n// v __ v v v v v __v v v v v\nconst ALPHABET = ' !#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';\n\nconst CODES = {};\nconst VALUES = {};\n\nfor (let value = 0; value < ALPHABET.length; ++value) {\n const code = ALPHABET[value];\n CODES[value] = code;\n VALUES[code] = value;\n}\n\n// Encodes bytes (from 0 to 255) to indexes of base94 codes (from 0 to 93).\n//\nconst encodeBase94$1 = (bytes) => {\n const length1 = bytes.length;\n const length2 = Math.ceil(1.22 * length1);\n const base94 = new Array(length2);\n for (let k1 = 0, k2 = 0; k1 < length1; k1 += 9, k2 += 11) {\n const limit1 = Math.min(9, length1 - k1);\n const limit2 = Math.ceil(1.22 * limit1);\n let buffer = 0n;\n for (let i = 0; i < limit1; ++i) {\n const byte = bytes[k1 + i];\n if (byte < 0 || byte > 255) {\n throw new Error('Incorrect value in input bytes array.');\n }\n buffer *= 256n;\n buffer += BigInt(byte);\n }\n for (let i = limit2 - 1; i > -1; --i) {\n const rest = buffer % 94n;\n base94[k2 + i] = Number(rest);\n buffer -= rest;\n buffer /= 94n;\n }\n }\n return base94;\n};\n\n// Decodes indexes of base94 codes (from 0 to 93) to bytes (from 0 to 255).\n//\nconst decodeBase94$1 = (base94) => {\n const rest = base94.length % 11;\n if (rest === 1 || rest === 6) {\n throw new Error('Input data length is incorrect.');\n }\n const length1 = base94.length;\n const length2 = Math.floor(0.82 * length1);\n const bytes = new Array(length2);\n for (let k1 = 0, k2 = 0; k1 < length1; k1 += 11, k2 += 9) {\n const limit1 = Math.min(11, length1 - k1);\n const limit2 = Math.floor(0.82 * limit1);\n let buffer = 0n;\n for (let i = 0; i < limit1; ++i) {\n const code = base94[k1 + i];\n if (code < 0 || code > 94) {\n throw new Error('Incorrect value in input base94 array.');\n }\n buffer *= 94n;\n buffer += BigInt(code);\n }\n for (let i = limit2 - 1; i > -1; --i) {\n const rest = buffer % 256n;\n bytes[k2 + i] = Number(rest);\n buffer -= rest;\n buffer /= 256n;\n }\n }\n return bytes;\n};\n\n// Encodes bytes (from 0 to 255) to JSON-safe base94 codes.\n//\nconst encodeBase94$2 = (bytes) => {\n const values = encodeBase94$1(bytes);\n let base94 = '';\n for (let i = 0; i < values.length; ++i) {\n const value = values[i];\n base94 += CODES[value];\n }\n return base94;\n};\n\n// Decodes JSON-safe base94 codes to bytes (from 0 to 255).\n//\nconst decodeBase94$2 = (base94) => {\n const values = new Array(base94.length);\n for (let i = 0; i < base94.length; ++i) {\n const code = base94[i];\n const value = VALUES[code];\n if (value == null) { // null or undefined\n throw new Error('Incorrect value in input base94 array.');\n }\n values[i] = value;\n }\n return decodeBase94$1(values);\n};\n\n\n\n// Usage example:\n\nconst inputBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];\n\nconst base94 = encodeBase94$2(inputBytes);\nconst outputBytes = decodeBase94$2(base94);\n\nconsole.log('Base94: ', base94); // AJ|iQ0U{Nl#/r;.\nconsole.log('Bytes: ', outputBytes); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\n\n\n\n// Note: to find more optimal solution goto https://dirask.com/snippets/JavaScript-JSON-Base94-encoding-and-decoding-DNO7rp","source":"","author":{"id":"Ro42dD","name":"Creg","avatar":"1667335677213__Ro42dD__w40px_h40px.jpg","points":9600,"role":"ADMIN"},"creationTime":1728337807000,"updateTime":1731487640000,"removalTime":null},{"id":"DNO7rp","type":"javascript","name":"JavaScript - JSON-safe Base94 encoding and decoding","content":"// ONLINE-RUNNER:browser;\n\n// ⚠️ Warning: this source code contains proposition to Base94 encoding and decoding algorithm that may be used in JSONs as Base64 replacement.\n\n\n// License: you can use it for free for private and commercial, but do not remove information about author and source.\n//\n// Author: Grzegorz G.\n// Source: https://dirask.com/snippets/DNO7rp\n\n\n// indexes: 0 10 20 30 40 50 60 70 80 90 93\n// | | | | | | | | | | |\n// v __ v v v v v __v v v v v\nconst ALPHABET = ' !#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';\n\nconst CODES = {};\nconst VALUES = {};\n\nfor (let value = 0; value < ALPHABET.length; ++value) {\n const code = ALPHABET[value];\n CODES[value] = code;\n VALUES[code] = value;\n}\n\n// Encodes bytes to Base94 string.\n//\nconst encodeBase94 = (bytes) => {\n let base94 = '';\n for (let k1 = 0, k2 = 0; k1 < bytes.length; k1 += 9, k2 += 11) {\n const limit1 = Math.min(9, bytes.length - k1);\n const limit2 = Math.ceil(1.22 * limit1);\n let buffer = 0n;\n for (let i = 0; i < limit1; ++i) {\n const byte = bytes[k1 + i];\n if (byte < 0 || byte > 255) {\n throw new Error('Incorrect value in input bytes array.');\n }\n buffer *= 256n;\n buffer += BigInt(byte);\n }\n let part = '';\n for (let i = 0; i < limit2; ++i) {\n const rest = buffer % 94n;\n const value = Number(rest);\n const code = CODES[value];\n part = code + part;\n buffer -= rest;\n buffer /= 94n;\n }\n base94 += part;\n }\n return base94;\n};\n\n// Decodes Base94 string to bytes.\n//\nconst decodeBase94 = (base94) => {\n const rest = base94.length % 11;\n if (rest === 1 || rest === 6) {\n throw new Error('Input data length is incorrect.');\n }\n const length1 = base94.length;\n const length2 = Math.floor(0.82 * length1);\n const bytes = new Array(length2);\n for (let k1 = 0, k2 = 0; k1 < length1; k1 += 11, k2 += 9) {\n const limit1 = Math.min(11, length1 - k1);\n const limit2 = Math.floor(0.82 * limit1);\n let buffer = 0n;\n for (let i = 0; i < limit1; ++i) {\n const code = base94[k1 + i];\n const value = VALUES[code];\n if (value == null) { // null or undefined\n throw new Error('Incorrect base64 input character.');\n }\n buffer *= 94n;\n buffer += BigInt(value);\n }\n for (let i = limit2 - 1; i > -1; --i) {\n const rest = buffer % 256n;\n const byte = Number(rest);\n bytes[k2 + i] = byte;\n buffer -= rest;\n buffer /= 256n;\n }\n }\n return bytes;\n};\n\n\n\n// Usage example:\n\nconst inputBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];\n\nconst base94 = encodeBase94(inputBytes);\nconst outputBytes = decodeBase94(base94);\n\nconsole.log('Base94: ', base94); // AJ|iQ0U{Nl#/r;.\nconsole.log('Bytes: ', outputBytes); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\n\n\n\n// Note: the solution bases on https://dirask.com/snippets/JavaScript-Base94-proposal-jPlJPj","source":"","author":{"id":"Ro42dD","name":"Creg","avatar":"1667335677213__Ro42dD__w40px_h40px.jpg","points":9600,"role":"ADMIN"},"creationTime":1728345600000,"updateTime":1731487579000,"removalTime":null}]}}]};