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