JavaScript generic ASN.1 parser (mirror)
at github-37 3.4 kB view raw
1// Base64 JavaScript decoder 2// Copyright (c) 2008-2019 Lapo Luchini <lapo@lapo.it> 3 4// Permission to use, copy, modify, and/or distribute this software for any 5// purpose with or without fee is hereby granted, provided that the above 6// copyright notice and this permission notice appear in all copies. 7// 8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 16(function (undefined) { 17"use strict"; 18 19var Base64 = {}, 20 decoder, // populated on first usage 21 haveU8 = ('Uint8Array' in (typeof window == 'object' ? window : global)); 22 23Base64.decode = function (a) { 24 var i; 25 if (decoder === undefined) { 26 var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 27 ignore = "= \f\n\r\t\u00A0\u2028\u2029"; 28 decoder = []; 29 for (i = 0; i < 64; ++i) 30 decoder[b64.charAt(i)] = i; 31 for (i = 0; i < ignore.length; ++i) 32 decoder[ignore.charAt(i)] = -1; 33 // RFC 3548 URL & file safe encoding 34 decoder['-'] = decoder['+']; 35 decoder['_'] = decoder['/']; 36 } 37 var out = haveU8 ? new Uint8Array(a.length * 3 >> 2) : []; 38 var bits = 0, char_count = 0, len = 0; 39 for (i = 0; i < a.length; ++i) { 40 var c = a.charAt(i); 41 if (c == '=') 42 break; 43 c = decoder[c]; 44 if (c == -1) 45 continue; 46 if (c === undefined) 47 throw 'Illegal character at offset ' + i; 48 bits |= c; 49 if (++char_count >= 4) { 50 out[len++] = (bits >> 16); 51 out[len++] = (bits >> 8) & 0xFF; 52 out[len++] = bits & 0xFF; 53 bits = 0; 54 char_count = 0; 55 } else { 56 bits <<= 6; 57 } 58 } 59 switch (char_count) { 60 case 1: 61 throw "Base64 encoding incomplete: at least 2 bits missing"; 62 case 2: 63 out[len++] = (bits >> 10); 64 break; 65 case 3: 66 out[len++] = (bits >> 16); 67 out[len++] = (bits >> 8) & 0xFF; 68 break; 69 } 70 if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters 71 out = out.subarray(0, len); 72 return out; 73}; 74 75Base64.pretty = function (str) { 76 // fix padding 77 if (str.length % 4 > 0) 78 str = (str + '===').slice(0, str.length + str.length % 4); 79 // convert RFC 3548 to standard Base64 80 str = str.replace(/-/g, '+').replace(/_/g, '/'); 81 // 80 column width 82 return str.replace(/(.{80})/g, '$1\n'); 83}; 84 85Base64.re = /-----BEGIN [^-]+-----([A-Za-z0-9+\/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+\/=\s]+)====/; 86Base64.unarmor = function (a) { 87 var m = Base64.re.exec(a); 88 if (m) { 89 if (m[1]) 90 a = m[1]; 91 else if (m[2]) 92 a = m[2]; 93 else 94 throw "RegExp out of sync"; 95 } 96 return Base64.decode(a); 97}; 98 99// export globals 100if (typeof module !== 'undefined') { module.exports = Base64; } else { window.Base64 = Base64; } 101})();