JavaScript generic ASN.1 parser (mirror)
at github-64 3.9 kB view raw
1// Base64 JavaScript decoder 2// Copyright (c) 2008-2023 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(typeof define != 'undefined' ? define : function (factory) { 'use strict'; 17 if (typeof module == 'object') module.exports = factory(); 18 else window.base64 = factory(); 19})(function () { 20'use strict'; 21 22const 23 haveU8 = (typeof Uint8Array == 'function'); 24 25let decoder; // populated on first usage 26 27class Base64 { 28 29 static decode(a) { 30 let isString = (typeof a == 'string'); 31 let i; 32 if (decoder === undefined) { 33 let b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', 34 ignore = '= \f\n\r\t\u00A0\u2028\u2029'; 35 decoder = []; 36 for (i = 0; i < 64; ++i) 37 decoder[b64.charCodeAt(i)] = i; 38 for (i = 0; i < ignore.length; ++i) 39 decoder[ignore.charCodeAt(i)] = -1; 40 // RFC 3548 URL & file safe encoding 41 decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)]; 42 decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)]; 43 } 44 let out = haveU8 ? new Uint8Array(a.length * 3 >> 2) : []; 45 let bits = 0, char_count = 0, len = 0; 46 for (i = 0; i < a.length; ++i) { 47 let c = isString ? a.charCodeAt(i) : a[i]; 48 if (c == 61) // '='.charCodeAt(0) 49 break; 50 c = decoder[c]; 51 if (c == -1) 52 continue; 53 if (c === undefined) 54 throw 'Illegal character at offset ' + i; 55 bits |= c; 56 if (++char_count >= 4) { 57 out[len++] = (bits >> 16); 58 out[len++] = (bits >> 8) & 0xFF; 59 out[len++] = bits & 0xFF; 60 bits = 0; 61 char_count = 0; 62 } else { 63 bits <<= 6; 64 } 65 } 66 switch (char_count) { 67 case 1: 68 throw 'Base64 encoding incomplete: at least 2 bits missing'; 69 case 2: 70 out[len++] = (bits >> 10); 71 break; 72 case 3: 73 out[len++] = (bits >> 16); 74 out[len++] = (bits >> 8) & 0xFF; 75 break; 76 } 77 if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters 78 out = out.subarray(0, len); 79 return out; 80 } 81 82 static pretty(str) { 83 // fix padding 84 if (str.length % 4 > 0) 85 str = (str + '===').slice(0, str.length + str.length % 4); 86 // convert RFC 3548 to standard Base64 87 str = str.replace(/-/g, '+').replace(/_/g, '/'); 88 // 80 column width 89 return str.replace(/(.{80})/g, '$1\n'); 90 } 91 92 static unarmor(a) { 93 let m = Base64.re.exec(a); 94 if (m) { 95 if (m[1]) 96 a = m[1]; 97 else if (m[2]) 98 a = m[2]; 99 else if (m[3]) 100 a = m[3]; 101 else 102 throw 'RegExp out of sync'; 103 } 104 return Base64.decode(a); 105 } 106 107} 108 109Base64.re = /-----BEGIN [^-]+-----([A-Za-z0-9+/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+/=\s]+)====|^([A-Za-z0-9+/=\s]+)$/; 110 111return Base64; 112 113});