|
| 1 | +(function (global, factory) { |
| 2 | + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 3 | + typeof define === 'function' && define.amd ? define(factory) : |
| 4 | + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.base64 = factory()); |
| 5 | +})(this, (function () { 'use strict'; |
| 6 | + |
| 7 | + /* ------------------------------------------------------------------------------------ |
| 8 | +
|
| 9 | + base64 - MIT License - Hexagon <[email protected]> |
| 10 | +
|
| 11 | + ------------------------------------------------------------------------------------ |
| 12 | +
|
| 13 | + License: |
| 14 | +
|
| 15 | + Copyright (c) 2021 Hexagon <[email protected]> |
| 16 | +
|
| 17 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 18 | + of this software and associated documentation files (the "Software"), to deal |
| 19 | + in the Software without restriction, including without limitation the rights |
| 20 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 21 | + copies of the Software, and to permit persons to whom the Software is |
| 22 | + furnished to do so, subject to the following conditions: |
| 23 | + The above copyright notice and this permission notice shall be included in |
| 24 | + all copies or substantial portions of the Software. |
| 25 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 31 | + THE SOFTWARE. |
| 32 | +
|
| 33 | + ------------------------------------------------------------------------------------ */ |
| 34 | + |
| 35 | + const |
| 36 | + // Regular base64 characters |
| 37 | + chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", |
| 38 | + |
| 39 | + // Base64url characters |
| 40 | + charsUrl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", |
| 41 | + |
| 42 | + genLookup = (target) => { |
| 43 | + let lookupTemp = typeof Uint8Array === "undefined" ? [] : new Uint8Array(256); |
| 44 | + for (let i = 0; i < chars.length; i++) { |
| 45 | + lookupTemp[target.charCodeAt(i)] = i; |
| 46 | + } |
| 47 | + return lookupTemp; |
| 48 | + }, |
| 49 | + |
| 50 | + // Use a lookup table to find the index. |
| 51 | + lookup = genLookup(chars), |
| 52 | + lookupUrl = genLookup(charsUrl); |
| 53 | + |
| 54 | + /** |
| 55 | + * @namespace base64 |
| 56 | + */ |
| 57 | + let base64 = {}; |
| 58 | + |
| 59 | + /** |
| 60 | + * Convenience function for converting a base64 encoded string to an ArrayBuffer instance |
| 61 | + * @public |
| 62 | + * |
| 63 | + * @param {string} data - Base64 representation of data |
| 64 | + * @param {boolean} [urlMode] - If set to true, URL mode string will be expected |
| 65 | + * @returns {ArrayBuffer} - Decoded data |
| 66 | + */ |
| 67 | + base64.toArrayBuffer = (data, urlMode) => { |
| 68 | + let bufferLength = data.length * 0.75, |
| 69 | + len = data.length, |
| 70 | + i, |
| 71 | + p = 0, |
| 72 | + encoded1, |
| 73 | + encoded2, |
| 74 | + encoded3, |
| 75 | + encoded4; |
| 76 | + |
| 77 | + if (data[data.length - 1] === "=") { |
| 78 | + bufferLength--; |
| 79 | + if (data[data.length - 2] === "=") { |
| 80 | + bufferLength--; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const |
| 85 | + arraybuffer = new ArrayBuffer(bufferLength), |
| 86 | + bytes = new Uint8Array(arraybuffer), |
| 87 | + target = urlMode ? lookupUrl : lookup; |
| 88 | + |
| 89 | + for (i = 0; i < len; i += 4) { |
| 90 | + encoded1 = target[data.charCodeAt(i)]; |
| 91 | + encoded2 = target[data.charCodeAt(i + 1)]; |
| 92 | + encoded3 = target[data.charCodeAt(i + 2)]; |
| 93 | + encoded4 = target[data.charCodeAt(i + 3)]; |
| 94 | + |
| 95 | + bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); |
| 96 | + bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); |
| 97 | + bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); |
| 98 | + } |
| 99 | + |
| 100 | + return arraybuffer; |
| 101 | + |
| 102 | + }; |
| 103 | + |
| 104 | + /** |
| 105 | + * Convenience function for converting base64 encoded string to an ArrayBuffer instance |
| 106 | + * @public |
| 107 | + * |
| 108 | + * @param {ArrayBuffer} arrBuf - ArrayBuffer to be encoded |
| 109 | + * @param {boolean} [urlMode] - If set to true, URL mode string will be returned |
| 110 | + * @returns {string} - Base64 representation of data |
| 111 | + */ |
| 112 | + base64.fromArrayBuffer = (arrBuf, urlMode) => { |
| 113 | + let bytes = new Uint8Array(arrBuf), |
| 114 | + i, |
| 115 | + len = bytes.length, |
| 116 | + result = "", |
| 117 | + target = urlMode ? charsUrl : chars; |
| 118 | + |
| 119 | + for (i = 0; i < len; i += 3) { |
| 120 | + result += target[bytes[i] >> 2]; |
| 121 | + result += target[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; |
| 122 | + result += target[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; |
| 123 | + result += target[bytes[i + 2] & 63]; |
| 124 | + } |
| 125 | + |
| 126 | + if (len % 3 === 2) { |
| 127 | + result = result.substring(0, result.length - 1) + (urlMode ? "" : "="); |
| 128 | + } else if (len % 3 === 1) { |
| 129 | + result = result.substring(0, result.length - 2) + (urlMode ? "" : "=="); |
| 130 | + } |
| 131 | + |
| 132 | + return result; |
| 133 | + }; |
| 134 | + |
| 135 | + /** |
| 136 | + * Convenience function for converting base64 to string |
| 137 | + * @public |
| 138 | + * |
| 139 | + * @param {string} str - Base64 encoded string to be decoded |
| 140 | + * @param {boolean} [urlMode] - If set to true, URL mode string will be expected |
| 141 | + * @returns {string} - Decoded string |
| 142 | + */ |
| 143 | + base64.toString = (str, urlMode) => { |
| 144 | + return new TextDecoder().decode(base64.toArrayBuffer(str, urlMode)); |
| 145 | + }; |
| 146 | + |
| 147 | + /** |
| 148 | + * Convenience function for converting a javascript string to base64 |
| 149 | + * @public |
| 150 | + * |
| 151 | + * @param {string} str - String to be converted to base64 |
| 152 | + * @param {boolean} [urlMode] - If set to true, URL mode string will be returned |
| 153 | + * @returns {string} - Base64 encoded string |
| 154 | + */ |
| 155 | + base64.fromString = (str, urlMode) => { |
| 156 | + return base64.fromArrayBuffer(new TextEncoder().encode(str), urlMode); |
| 157 | + }; |
| 158 | + |
| 159 | + /** |
| 160 | + * Function to validate base64 |
| 161 | + * @public |
| 162 | + * @param {string} encoded - Base64 or Base64url encoded data |
| 163 | + * @param {boolean} [urlMode] - If set to true, base64url will be expected |
| 164 | + * @returns {boolean} - Valid base64/base64url? |
| 165 | + */ |
| 166 | + base64.validate = (encoded, urlMode) => { |
| 167 | + |
| 168 | + // Bail out if not string |
| 169 | + if (!(typeof encoded === "string" || encoded instanceof String)) { |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + // Go on validate |
| 174 | + try { |
| 175 | + if (urlMode) { |
| 176 | + return /^[-A-Za-z0-9\-_]*$/.test(encoded); |
| 177 | + } else { |
| 178 | + return /^[-A-Za-z0-9+/]*={0,3}$/.test(encoded); |
| 179 | + } |
| 180 | + } catch (_e) { |
| 181 | + return false; |
| 182 | + } |
| 183 | + }; |
| 184 | + |
| 185 | + base64.base64 = base64; |
| 186 | + |
| 187 | + return base64; |
| 188 | + |
| 189 | +})); |
0 commit comments