{"version":3,"file":"Base64Decode.js","sources":["../../src/encode/Base64Decode.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { Constants } from \"@azure/msal-common\";\nimport { BrowserStringUtils } from \"../utils/BrowserStringUtils\";\n\n/**\n * Class which exposes APIs to decode base64 strings to plaintext. See here for implementation details:\n * https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_2_%E2%80%93_JavaScript's_UTF-16_%3E_UTF-8_%3E_base64\n */\nexport class Base64Decode {\n\n /**\n * Returns a URL-safe plaintext decoded string from b64 encoded input.\n * @param input \n */\n decode(input: string): string {\n let encodedString = input.replace(/-/g, \"+\").replace(/_/g, \"/\");\n switch (encodedString.length % 4) {\n case 0:\n break;\n case 2:\n encodedString += \"==\";\n break;\n case 3:\n encodedString += \"=\";\n break;\n default:\n throw new Error(\"Invalid base64 string\");\n }\n\n const inputUtf8Arr = this.base64DecToArr(encodedString);\n return BrowserStringUtils.utf8ArrToString(inputUtf8Arr);\n }\n\n /**\n * Decodes base64 into Uint8Array\n * @param base64String \n * @param nBlockSize \n */\n private base64DecToArr(base64String: string, nBlockSize?: number): Uint8Array {\n const sB64Enc = base64String.replace(/[^A-Za-z0-9\\+\\/]/g, Constants.EMPTY_STRING);\n const nInLen = sB64Enc.length;\n const nOutLen = nBlockSize ? Math.ceil((nInLen * 3 + 1 >>> 2) / nBlockSize) * nBlockSize : nInLen * 3 + 1 >>> 2;\n const aBytes = new Uint8Array(nOutLen);\n\n for (let nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {\n nMod4 = nInIdx & 3;\n nUint24 |= this.b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;\n if (nMod4 === 3 || nInLen - nInIdx === 1) {\n for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {\n aBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;\n }\n nUint24 = 0;\n }\n }\n\n return aBytes;\n }\n\n /**\n * Base64 string to array decoding helper\n * @param charNum \n */\n private b64ToUint6(charNum: number): number {\n return charNum > 64 && charNum < 91 ?\n charNum - 65\n : charNum > 96 && charNum < 123 ? \n charNum - 71\n : charNum > 47 && charNum < 58 ?\n charNum + 4\n : charNum === 43 ?\n 62\n : charNum === 47 ?\n 63\n :\n 0;\n }\n}\n"],"names":[],"mappings":";;;;;AAAA;;;;AAQA;;;;;IAIA;KAoEC;;;;;IA9DG,6BAAM,GAAN,UAAO,KAAa;QAChB,IAAI,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAChE,QAAQ,aAAa,CAAC,MAAM,GAAG,CAAC;YAC5B,KAAK,CAAC;gBACF,MAAM;YACV,KAAK,CAAC;gBACF,aAAa,IAAI,IAAI,CAAC;gBACtB,MAAM;YACV,KAAK,CAAC;gBACF,aAAa,IAAI,GAAG,CAAC;gBACrB,MAAM;YACV;gBACI,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;SAChD;QAED,IAAM,YAAY,GAAG,IAAI,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC;QACxD,OAAO,kBAAkB,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;KAC3D;;;;;;IAOO,qCAAc,GAAtB,UAAuB,YAAoB,EAAE,UAAmB;QAC5D,IAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,mBAAmB,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC;QAClF,IAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC9B,IAAM,OAAO,GAAG,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,UAAU,CAAC,GAAG,UAAU,G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