CoolFace
Apppublic

TrinetraLabs/Placebo_AI

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
base64.js107 linesDownload Raw Back to lib
1"use strict";2var utils = require("./utils");3var support = require("./support");4// private property5var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";6 7 8// public method for encoding9exports.encode = function(input) {10    var output = [];11    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;12    var i = 0, len = input.length, remainingBytes = len;13 14    var isArray = utils.getTypeOf(input) !== "string";15    while (i < input.length) {16        remainingBytes = len - i;17 18        if (!isArray) {19            chr1 = input.charCodeAt(i++);20            chr2 = i < len ? input.charCodeAt(i++) : 0;21            chr3 = i < len ? input.charCodeAt(i++) : 0;22        } else {23            chr1 = input[i++];24            chr2 = i < len ? input[i++] : 0;25            chr3 = i < len ? input[i++] : 0;26        }27 28        enc1 = chr1 >> 2;29        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);30        enc3 = remainingBytes > 1 ? (((chr2 & 15) << 2) | (chr3 >> 6)) : 64;31        enc4 = remainingBytes > 2 ? (chr3 & 63) : 64;32 33        output.push(_keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4));34 35    }36 37    return output.join("");38};39 40// public method for decoding41exports.decode = function(input) {42    var chr1, chr2, chr3;43    var enc1, enc2, enc3, enc4;44    var i = 0, resultIndex = 0;45 46    var dataUrlPrefix = "data:";47 48    if (input.substr(0, dataUrlPrefix.length) === dataUrlPrefix) {49        // This is a common error: people give a data url50        // (data:image/png;base64,iVBOR...) with a {base64: true} and51        // wonders why things don't work.52        // We can detect that the string input looks like a data url but we53        // *can't* be sure it is one: removing everything up to the comma would54        // be too dangerous.55        throw new Error("Invalid base64 input, it looks like a data url.");56    }57 58    input = input.replace(/[^A-Za-z0-9+/=]/g, "");59 60    var totalLength = input.length * 3 / 4;61    if(input.charAt(input.length - 1) === _keyStr.charAt(64)) {62        totalLength--;63    }64    if(input.charAt(input.length - 2) === _keyStr.charAt(64)) {65        totalLength--;66    }67    if (totalLength % 1 !== 0) {68        // totalLength is not an integer, the length does not match a valid69        // base64 content. That can happen if:70        // - the input is not a base64 content71        // - the input is *almost* a base64 content, with a extra chars at the72        //   beginning or at the end73        // - the input uses a base64 variant (base64url for example)74        throw new Error("Invalid base64 input, bad content length.");75    }76    var output;77    if (support.uint8array) {78        output = new Uint8Array(totalLength|0);79    } else {80        output = new Array(totalLength|0);81    }82 83    while (i < input.length) {84 85        enc1 = _keyStr.indexOf(input.charAt(i++));86        enc2 = _keyStr.indexOf(input.charAt(i++));87        enc3 = _keyStr.indexOf(input.charAt(i++));88        enc4 = _keyStr.indexOf(input.charAt(i++));89 90        chr1 = (enc1 << 2) | (enc2 >> 4);91        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);92        chr3 = ((enc3 & 3) << 6) | enc4;93 94        output[resultIndex++] = chr1;95 96        if (enc3 !== 64) {97            output[resultIndex++] = chr2;98        }99        if (enc4 !== 64) {100            output[resultIndex++] = chr3;101        }102 103    }104 105    return output;106};107