JavaScript generic ASN.1 parser (mirror)
at github-19 119 lines 3.3 kB view raw
1/*global Hex, Base64, ASN1 */ 2"use strict"; 3 4var maxLength = 10240, 5 reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/, 6 tree = id('tree'), 7 dump = id('dump'), 8 wantHex = id('wantHex'), 9 area = id('area'), 10 file = id('file'), 11 hash = null; 12function id(elem) { 13 return document.getElementById(elem); 14} 15function text(el, string) { 16 if ('textContent' in el) 17 el.textContent = string; 18 else 19 el.innerText = string; 20} 21function decode(der) { 22 tree.innerHTML = ''; 23 dump.innerHTML = ''; 24 try { 25 var asn1 = ASN1.decode(der); 26 tree.appendChild(asn1.toDOM()); 27 if (wantHex.checked) 28 dump.appendChild(asn1.toHexDOM()); 29 var hex = (der.length < maxLength) ? asn1.toHexString() : ''; 30 if (area.value === '') 31 area.value = hex; 32 try { 33 window.location.hash = hash = '#' + hex; 34 } catch (e) { // fails with "Access Denied" on IE with URLs longer than ~2048 chars 35 window.location.hash = hash = '#'; 36 } 37 } catch (e) { 38 text(tree, e); 39 } 40} 41function decodeArea() { 42 try { 43 var val = area.value, 44 der = reHex.test(val) ? Hex.decode(val) : Base64.unarmor(val); 45 decode(der); 46 } catch (e) { 47 text(tree, e); 48 dump.innerHTML = ''; 49 } 50} 51function decodeBinaryString(str) { 52 var der; 53 try { 54 if (reHex.test(str)) 55 der = Hex.decode(str); 56 else if (Base64.re.test(str)) 57 der = Base64.unarmor(str); 58 else 59 der = str; 60 decode(der); 61 } catch (e) { 62 text(tree, 'Cannot decode file.'); 63 dump.innerHTML = ''; 64 } 65} 66function clearAll() { 67 area.value = ''; 68 tree.innerHTML = ''; 69 dump.innerHTML = ''; 70 hash = window.location.hash = ''; 71} 72// this is only used if window.FileReader 73function read(f) { 74 area.value = ''; // clear text area, will get hex content 75 var r = new FileReader(); 76 r.onloadend = function () { 77 if (r.error) 78 alert("Your browser couldn't read the specified file (error code " + r.error.code + ")."); 79 else 80 decodeBinaryString(r.result); 81 }; 82 r.readAsBinaryString(f); 83} 84function load() { 85 if (file.files.length === 0) 86 alert("Select a file to load first."); 87 else 88 read(file.files[0]); 89} 90function loadFromHash() { 91 if (window.location.hash && window.location.hash != hash) { 92 hash = window.location.hash; 93 // Firefox is not consistent with other browsers and return an 94 // already-decoded hash string so we risk double-decoding here, 95 // but since % is not allowed in base64 nor hexadecimal, it's ok 96 area.value = decodeURIComponent(hash.substr(1)); 97 decodeArea(); 98 } 99} 100function stop(e) { 101 e.stopPropagation(); 102 e.preventDefault(); 103} 104function dragAccept(e) { 105 stop(e); 106 if (e.dataTransfer.files.length > 0) 107 read(e.dataTransfer.files[0]); 108} 109// main 110if ('onhashchange' in window) 111 window.onhashchange = loadFromHash; 112loadFromHash(); 113document.ondragover = stop; 114document.ondragleave = stop; 115if ('FileReader' in window) { 116 file.style.display = 'block'; 117 file.onchange = load; 118 document.ondrop = dragAccept; 119}