1/*jshint browser: true, strict: true, globalstrict: true, indent: 4, immed: true, latedef: true, undef: true, regexdash: false */
2/*global Hex, Base64, ASN1 */
3"use strict";
4
5var reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/,
6 hash = null;
7function id(elem) {
8 return document.getElementById(elem);
9}
10function toHTML(obj) {
11 return String(obj).replace(/</g, "<");
12}
13function decode(der) {
14 var tree = id('tree');
15 var dump = id('dump');
16 tree.innerHTML = '';
17 dump.innerHTML = '';
18 try {
19 var asn1 = ASN1.decode(der),
20 hex = asn1.toHexString();
21 tree.appendChild(asn1.toDOM());
22 if (id('wantHex').checked)
23 dump.appendChild(asn1.toHexDOM());
24 if (id('pem').value === '')
25 id('pem').value = hex;
26 // update URL hash (does this have length limits we should avoid?)
27 hash = '#' + hex;
28 window.location.hash = hash;
29 } catch (e) {
30 tree.innerHTML = toHTML(e);
31 }
32 return false;
33}
34function decodeArea() {
35 try {
36 var pem = id('pem').value;
37 var der = reHex.test(pem) ? Hex.decode(pem) : Base64.unarmor(pem);
38 decode(der);
39 } catch (e) {
40 id('tree').innerHTML = toHTML(e);
41 id('dump').innerHTML = '';
42 }
43 return false;
44}
45function decodeBinaryString(str) {
46 var der;
47 try {
48 if (reHex.test(str))
49 der = Hex.decode(str);
50 else if (Base64.re.test(str))
51 der = Base64.unarmor(str);
52 else {
53 der = [];
54 for (var i = 0; i < str.length; ++i)
55 der[der.length] = str.charCodeAt(i);
56 }
57 decode(der);
58 } catch (e) {
59 id('tree').innerHTML = 'Cannot decode file.';
60 id('dump').innerHTML = '';
61 }
62 return false;
63}
64function clearAll() {
65 id('pem').value = '';
66 id('tree').innerHTML = '';
67 id('dump').innerHTML = '';
68 hash = window.location.hash = '';
69 return false;
70}
71// this is only used if window.FileReader
72function read(f) {
73 id('pem').value = ''; // clear text area, will get hex content
74 var r = new FileReader();
75 r.onloadend = function () {
76 if (r.error) {
77 alert("Your browser couldn't read the specified file (error code " + r.error.code + ").");
78 } else
79 decodeBinaryString(r.result);
80 };
81 r.readAsBinaryString(f);
82}
83function load() {
84 var file = id('file');
85 if (file.files.length === 0) {
86 alert("Select a file to load first.");
87 return false;
88 }
89 read(file.files[0]);
90 return false;
91}
92function loadFromHash() {
93 if (window.location.hash && window.location.hash != hash) {
94 hash = window.location.hash;
95 id('pem').value = hash.substring(1);
96 decodeArea();
97 }
98}
99function stop(e) {
100 e.stopPropagation();
101 e.preventDefault();
102}
103function dragAccept(e) {
104 stop(e);
105 if (e.dataTransfer.files.length > 0)
106 read(e.dataTransfer.files[0]);
107}
108window.onload = function () {
109 if ('onhashchange' in window)
110 window.onhashchange = loadFromHash;
111 loadFromHash();
112 document.ondragover = stop;
113 document.ondragleave = stop;
114 if ('FileReader' in window) {
115 id('file').style.display = 'block';
116 id('file').onchange = load;
117 document.ondrop = dragAccept;
118 }
119};