UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

75 lines (73 loc) 2.82 kB
/** * Base64ArrayBuffer class to encode/decode. */ export class Base64ArrayBuffer { static chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // Use a lookup table to find the index. static lookupInternal = new Uint8Array(256); static scanned = false; static get lookup() { if (Base64ArrayBuffer.scanned) { return Base64ArrayBuffer.lookupInternal; } for (let i = 0; i < Base64ArrayBuffer.chars.length; i++) { Base64ArrayBuffer.lookupInternal[Base64ArrayBuffer.chars.charCodeAt(i)] = i; } Base64ArrayBuffer.scanned = true; return Base64ArrayBuffer.lookupInternal; } /** * Encode the array buffer to base64. * * @param arraybuffer The array buffer. * @returns the base64 string. */ static Encode(arraybuffer) { const bytes = new Uint8Array(arraybuffer); const len = bytes.length; let base64 = ""; for (let i = 0; i < len; i += 3) { base64 += Base64ArrayBuffer.chars[bytes[i] >> 2]; base64 += Base64ArrayBuffer.chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; base64 += Base64ArrayBuffer.chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; base64 += Base64ArrayBuffer.chars[bytes[i + 2] & 63]; } if ((len % 3) === 2) { base64 = base64.substring(0, base64.length - 1) + "="; } else if (len % 3 === 1) { base64 = base64.substring(0, base64.length - 2) + "=="; } return base64; } /** * Decode the base64 string to array buffer. * * @param base64 the base64 string. * @returns The array buffer. */ static Decode(base64) { let bufferLength = base64.length * 0.75; const len = base64.length; if (base64[base64.length - 1] === '=') { bufferLength--; if (base64[base64.length - 2] === '=') { bufferLength--; } } const arraybuffer = new ArrayBuffer(bufferLength); let bytes = new Uint8Array(arraybuffer); let p = 0; for (let i = 0; i < len; i += 4) { const encoded1 = Base64ArrayBuffer.lookup[base64.charCodeAt(i)]; const encoded2 = Base64ArrayBuffer.lookup[base64.charCodeAt(i + 1)]; const encoded3 = Base64ArrayBuffer.lookup[base64.charCodeAt(i + 2)]; const encoded4 = Base64ArrayBuffer.lookup[base64.charCodeAt(i + 3)]; bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); } return arraybuffer; } } //# sourceMappingURL=base64-array-buffer.js.map