···11+# ChangeLog
22+33+## 2.1.1 - 2025-10-24
44+55+### Changed
66+77+- update dev dependencies
88+- fix test suite that was reporting no error with empty responses
99+1010+### Added
1111+1212+- add content length check for BOOLEAN, INTEGER, OID ([GitHub #104](https://github.com/lapo-luchini/asn1js/pull/104))
1313+1414+## 2.1.0 - 2025-08-03
1515+1616+### Changed
1717+1818+- when fields are CHOICEs now both the field name and the choice name are shown (fixes [GitHub #102](https://github.com/lapo-luchini/asn1js/issues/102))
1919+- upgrade minimum NodeJS version supported from 12.20.0 to 14.6.0 due to usage of ?. and ?? operators in defs.js (ECMAScript 2020); older code is still linted against ECMAScript 2015 for now
2020+2121+### Added
2222+2323+- add tests to check expected decoding
2424+2525+## 2.0.6 - 2025-07-29
2626+2727+### Added
2828+2929+- add proper support for standard Base64 (we previously only supported Base64url) (fixes [GitHub #99](https://github.com/lapo-luchini/asn1js/pull/99))
3030+- improve test harness
3131+3232+## 2.0.5 - 2025-04-12
3333+3434+### Added
3535+3636+- add `index-local.html` for local `file://` usage without needing a web server
3737+- add definitions support for `LDAPMessage`
3838+- #TODO continue producing old ChangeLog entries
+1-1
LICENSE
···11ISC License
2233-Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
33+Copyright (c) 2008-2025 Lapo Luchini <lapo@lapo.it>
4455Permission to use, copy, modify, and/or distribute this software for any
66purpose with or without fee is hereby granted, provided that the above
+48-13
README.md
···3344asn1js is a JavaScript generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.
5566-An example page that can decode Base64-encoded (raw base64, PEM armoring and `begin-base64` are recognized) or Hex-encoded (or local files with some browsers) is included and can be used both [online on the official website](https://lapo.it/asn1js/) or [offline (ZIP file)](https://lapo.it/asn1js/asn1js.zip).
66+An example page that can decode Base64-encoded (raw base64, PEM armoring and `begin-base64` are recognized) or Hex-encoded (or local files with some browsers) is included and can be used both [online on the official website](https://asn1js.eu/) or [offline (ZIP file)](https://lapo.it/asn1js/asn1js.zip) by opening `index-local.html`.
7788-Usage with `npm` / `yarn`
99--------------------------
88+Usage with `nodejs`
99+-------------------
10101111This package can be installed with either npm or yarn via the following commands:
1212···1717yarn add @lapo/asn1js
1818```
19192020-Assuming a standard javascript bundler is setup you can import it like so:
2020+You can import the classes like this:
21212222```js
2323-import ASN1 from '@lapo/asn1js';
2323+import { ASN1 } from '@lapo/asn1js';
2424```
25252626A submodule of this package can also be imported:
27272828```js
2929-import Hex from '@lapo/asn1js/hex';
2929+import { Hex } from '@lapo/asn1js/hex.js';
3030+```
3131+3232+If your code is still not using ES6 Modules (and is using CommonJS) you can `require` it normally [since NodeJS 22](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) (with parameter `--experimental-require-module`):
3333+3434+```js
3535+const
3636+ { ASN1 } = require('@lapo/asn1js'),
3737+ { Hex } = require('@lapo/asn1js/hex.js');
3838+console.log(ASN1.decode(Hex.decode('06032B6570')).content());
3039```
31403232-Unfortunately until [`require(esm)` gets released](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) it is necessary to use async `import()` when used from CommonJS (legacy NodeJS) code.
4141+On older NodeJS you instead need to use async `import`:
4242+4343+```js
4444+async function main() {
4545+ const
4646+ { ASN1 } = await import('@lapo/asn1js'),
4747+ { Hex } = await import('@lapo/asn1js/hex.js');
4848+ console.log(ASN1.decode(Hex.decode('06032B6570')).content());
4949+}
5050+main();
5151+```
33523453Usage on the web
3554--------------------
···38573958```html
4059<script>
4141-import { ASN1} from 'https://unpkg.com/@lapo/asn1js@2.0.0/asn1.js';
6060+import { ASN1 } from 'https://unpkg.com/@lapo/asn1js@2.0.0/asn1.js';
4261import { Hex } from 'https://unpkg.com/@lapo/asn1js@2.0.0/hex.js';
43624463document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
4564</script>
4665```
47666767+Local usage
6868+--------------------
6969+7070+Since unfortunately ESM modules are not working on `file:` protocol due to [CORS issues](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts), there is a bundled [single-file version working locally](https://asn1js.eu/index-local.html). It doesn't work online (due to [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) restrictions about inline content) but can be saved locally and opened in a browser.
7171+7272+Usage from CLI
7373+--------------------
7474+7575+You can dump an ASN.1 structure from the command line using the following command (no need to even install it):
7676+7777+```sh
7878+npx @lapo/asn1js ed25519.cer
7979+```
8080+4881ISC license
4982-----------
50835151-ASN.1 JavaScript decoder Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
8484+ASN.1 JavaScript decoder Copyright (c) 2008-2025 Lapo Luchini <lapo@lapo.it>
52855386Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
5487···6295- extended tag support added by [Pรฉter Budai](https://www.peterbudai.eu/)
6396- patches by [Gergely Nagy](https://github.com/ngg)
6497- Relative OID support added by [Mistial Developer](https://github.com/mistial-dev)
6565-- dark mode support added by [Oliver Burgmaier](https://github.com/olibu/)
9898+- dark mode and other UI improvements by [Oliver Burgmaier](https://github.com/olibu/)
6699- patches by [Nicolai Sรธborg](https://github.com/NicolaiSoeborg)
6710068101links
69102-----
701037171-- [official website](https://lapo.it/asn1js/)
7272-- [dedicated domain](https://asn1js.eu/)
7373-- [InDefero tracker](http://idf.lapo.it/p/asn1js/)
104104+- [official website](https://asn1js.eu/)
105105+- [alternate website](https://lapo.it/asn1js/)
106106+- [single-file version working locally](https://asn1js.eu/index-local.html) (just save this link)
107107+- [InDefero tracker](http://idf.lapo.it/p/asn1js/) (currently offline)
74108- [GitHub mirror](https://github.com/lapo-luchini/asn1js)
109109+- [ChangeLog on GitHub](https://github.com/lapo-luchini/asn1js/blob/trunk/CHANGELOG.md)
75110- [Ohloh code stats](https://www.openhub.net/p/asn1js)
+343-89
asn1.js
···11// ASN.1 JavaScript decoder
22-// Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
22+// Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3344// Permission to use, copy, modify, and/or distribute this software for any
55// purpose with or without fee is hereby granted, provided that the above
66// copyright notice and this permission notice appear in all copies.
77-//
77+//
88// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
99// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1010// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
···1313// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1414// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15151616-import { Int10 } from './int10.js';
1716import { oids } from './oids.js';
18171918const
···2120 reTimeS = /^(\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
2221 reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
2322 hexDigits = '0123456789ABCDEF',
2424- b64Safe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
2323+ b64Std = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
2424+ b64URL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
2525 tableT61 = [
2626 ['', ''],
2727 ['AEIOUaeiou', 'รรรรรร รจรฌรฒรน'], // Grave
···4141 ['CDELNRSTZcdelnrstz', 'ฤฤฤฤฝลลล ลคลฝฤฤฤฤพลลลกลฅลพ'], // Caron
4242 ];
43434444+/**
4545+ * Truncates a string to a specified length and adds an ellipsis if needed.
4646+ * @param {string} str - The input string to truncate
4747+ * @param {number} len - The maximum length of the string
4848+ * @returns {string} The truncated string
4949+ */
4450function stringCut(str, len) {
4551 if (str.length > len)
4652 str = str.substring(0, len) + ellipsis;
4753 return str;
4854}
49555656+/**
5757+ * Checks if a string contains only printable characters (ASCII 32-126, plus tab, newline, carriage return)
5858+ * @param {string} s - The string to check
5959+ * @throws {Error} If an unprintable character is found
6060+ */
5061function checkPrintable(s) {
5162 let i, v;
5263 for (i = 0; i < s.length; ++i) {
···5667 }
5768}
58695959-class Stream {
7070+/**
7171+ * Class to manage a stream of bytes, with a zero-copy approach.
7272+ * It uses an existing array or binary string and advances a position index.
7373+ */
7474+export class Stream {
60757676+ /**
7777+ * Creates a new Stream object.
7878+ * @param {Stream|array|string} enc data (will not be copied)
7979+ * @param {?number} pos starting position (mandatory when `end` is not a Stream)
8080+ */
6181 constructor(enc, pos) {
6282 if (enc instanceof Stream) {
6383 this.enc = enc.enc;
6484 this.pos = enc.pos;
6585 } else {
6666- // enc should be an array or a binary string
6786 this.enc = enc;
6887 this.pos = pos;
6988 }
8989+ if (typeof this.pos != 'number')
9090+ throw new Error('"pos" must be a numeric value');
9191+ // Set up the raw byte access function based on the type of data
9292+ if (typeof this.enc == 'string')
9393+ this.getRaw = pos => this.enc.charCodeAt(pos);
9494+ else if (typeof this.enc[0] == 'number')
9595+ this.getRaw = pos => this.enc[pos];
9696+ else
9797+ throw new Error('"enc" must be a numeric array or a string');
7098 }
9999+100100+ /**
101101+ * Get the byte at current position (and increment it) or at a specified position (and avoid moving current position).
102102+ * @param {?number} pos read position if specified, else current position (and increment it)
103103+ * @returns {number} The byte value at the specified position
104104+ */
71105 get(pos) {
72106 if (pos === undefined)
73107 pos = this.pos++;
74108 if (pos >= this.enc.length)
7575- throw 'Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length;
7676- return (typeof this.enc == 'string') ? this.enc.charCodeAt(pos) : this.enc[pos];
109109+ throw new Error('Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length);
110110+ return this.getRaw(pos);
77111 }
7878- hexByte(b) {
112112+113113+ /**
114114+ * Convert a single byte to a hexadecimal string (of length 2).
115115+ * @param {number} b - The byte to convert
116116+ * @returns {string} Hexadecimal representation of the byte
117117+ */
118118+ static hexByte(b) {
79119 return hexDigits.charAt((b >> 4) & 0xF) + hexDigits.charAt(b & 0xF);
80120 }
8181- hexDump(start, end, raw) {
121121+122122+ /**
123123+ * Hexadecimal dump of a specified region of the stream.
124124+ * @param {number} start - starting position (included)
125125+ * @param {number} end - ending position (excluded)
126126+ * @param {string} type - 'raw', 'byte' or 'dump' (default)
127127+ * @returns {string} Hexadecimal representation of the data
128128+ */
129129+ hexDump(start, end, type = 'dump') {
82130 let s = '';
83131 for (let i = start; i < end; ++i) {
8484- s += this.hexByte(this.get(i));
8585- if (raw !== true)
132132+ if (type == 'byte' && i > start)
133133+ s += ' ';
134134+ s += Stream.hexByte(this.get(i));
135135+ if (type == 'dump')
86136 switch (i & 0xF) {
87137 case 0x7: s += ' '; break;
88138 case 0xF: s += '\n'; break;
···91141 }
92142 return s;
93143 }
9494- b64Dump(start, end) {
9595- let extra = (end - start) % 3,
9696- s = '',
144144+145145+ /**
146146+ * Base64url dump of a specified region of the stream (according to RFC 4648 section 5).
147147+ * @param {number} start - starting position (included)
148148+ * @param {number} end - ending position (excluded)
149149+ * @param {string} type - 'url' (default, section 5 without padding) or 'std' (section 4 with padding)
150150+ * @returns {string} Base64 encoded representation of the data
151151+ */
152152+ b64Dump(start, end, type = 'url') {
153153+ const b64 = type === 'url' ? b64URL : b64Std,
154154+ extra = (end - start) % 3;
155155+ let s = '',
97156 i, c;
98157 for (i = start; i + 2 < end; i += 3) {
99158 c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
100100- s += b64Safe.charAt(c >> 18 & 0x3F);
101101- s += b64Safe.charAt(c >> 12 & 0x3F);
102102- s += b64Safe.charAt(c >> 6 & 0x3F);
103103- s += b64Safe.charAt(c & 0x3F);
159159+ s += b64.charAt(c >> 18 & 0x3F);
160160+ s += b64.charAt(c >> 12 & 0x3F);
161161+ s += b64.charAt(c >> 6 & 0x3F);
162162+ s += b64.charAt(c & 0x3F);
104163 }
105164 if (extra > 0) {
106165 c = this.get(i) << 16;
107166 if (extra > 1) c |= this.get(i + 1) << 8;
108108- s += b64Safe.charAt(c >> 18 & 0x3F);
109109- s += b64Safe.charAt(c >> 12 & 0x3F);
110110- if (extra == 2) s += b64Safe.charAt(c >> 6 & 0x3F);
167167+ s += b64.charAt(c >> 18 & 0x3F);
168168+ s += b64.charAt(c >> 12 & 0x3F);
169169+ if (extra == 2) s += b64.charAt(c >> 6 & 0x3F);
170170+ if (b64 === b64Std) s += '==='.slice(0, 3 - extra);
111171 }
112172 return s;
113173 }
174174+175175+ /**
176176+ * Check if a region of the stream contains only ASCII characters (32-176)
177177+ * @param {number} start - starting position (included)
178178+ * @param {number} end - ending position (excluded)
179179+ * @returns {boolean} True if all characters are ASCII, false otherwise
180180+ */
114181 isASCII(start, end) {
115182 for (let i = start; i < end; ++i) {
116183 let c = this.get(i);
···119186 }
120187 return true;
121188 }
189189+190190+ /**
191191+ * Parse a region of the stream as an ISO string
192192+ * @param {number} start - starting position (included)
193193+ * @param {number} end - ending position (excluded)
194194+ * @param {number} maxLength - maximum length of the output string
195195+ * @returns {Object} Object with size and str properties
196196+ */
122197 parseStringISO(start, end, maxLength) {
123198 let s = '';
124199 for (let i = start; i < end; ++i)
125200 s += String.fromCharCode(this.get(i));
126201 return { size: s.length, str: stringCut(s, maxLength) };
127202 }
203203+204204+ /**
205205+ * Parse a region of the stream as a T.61 string
206206+ * @param {number} start - starting position (included)
207207+ * @param {number} end - ending position (excluded)
208208+ * @param {number} maxLength - maximum length of the output string
209209+ * @returns {Object} Object with size and str properties
210210+ */
128211 parseStringT61(start, end, maxLength) {
129212 // warning: this code is not very well tested so far
130213 function merge(c, d) {
131131- let t = tableT61[c - 0xC0];
132132- let i = t[0].indexOf(String.fromCharCode(d));
214214+ const t = tableT61[c - 0xC0];
215215+ const i = t[0].indexOf(String.fromCharCode(d));
133216 return (i < 0) ? '\0' : t[1].charAt(i);
134217 }
135218 let s = '', c;
···146229 }
147230 return { size: s.length, str: stringCut(s, maxLength) };
148231 }
232232+233233+ /**
234234+ * Parse a region of the stream as a UTF-8 string
235235+ * @param {number} start - starting position (included)
236236+ * @param {number} end - ending position (excluded)
237237+ * @param {number} maxLength - maximum length of the output string
238238+ * @returns {Object} Object with size and str properties
239239+ */
149240 parseStringUTF(start, end, maxLength) {
241241+ /**
242242+ * Helper function to process UTF-8 continuation bytes
243243+ * @param {number} c - The continuation byte
244244+ * @returns {number} The extracted data bits
245245+ */
150246 function ex(c) { // must be 10xxxxxx
151247 if ((c < 0x80) || (c >= 0xC0))
152248 throw new Error('Invalid UTF-8 continuation byte: ' + c);
153249 return (c & 0x3F);
154250 }
251251+ /**
252252+ * Helper function to convert a code point to a surrogate pair
253253+ * @param {number} cp - The code point to convert
254254+ * @returns {string} The surrogate pair as a string
255255+ */
155256 function surrogate(cp) {
156257 if (cp < 0x10000)
157258 throw new Error('UTF-8 overlong encoding, codepoint encoded in 4 bytes: ' + cp);
···161262 }
162263 let s = '';
163264 for (let i = start; i < end; ) {
164164- let c = this.get(i++);
265265+ const c = this.get(i++);
165266 if (c < 0x80) // 0xxxxxxx (7 bit)
166267 s += String.fromCharCode(c);
167268 else if (c < 0xC0)
···177278 }
178279 return { size: s.length, str: stringCut(s, maxLength) };
179280 }
281281+282282+ /**
283283+ * Parse a region of the stream as a BMP (Basic Multilingual Plane) string
284284+ * @param {number} start - starting position (included)
285285+ * @param {number} end - ending position (excluded)
286286+ * @param {number} maxLength - maximum length of the output string
287287+ * @returns {Object} Object with size and str properties
288288+ */
180289 parseStringBMP(start, end, maxLength) {
181290 let s = '', hi, lo;
182291 for (let i = start; i < end; ) {
···186295 }
187296 return { size: s.length, str: stringCut(s, maxLength) };
188297 }
298298+299299+ /**
300300+ * Parse a region of the stream as a time string
301301+ * @param {number} start - starting position (included)
302302+ * @param {number} end - ending position (excluded)
303303+ * @param {boolean} shortYear - Whether to parse as short year (2-digit)
304304+ * @returns {string} Formatted time string
305305+ */
189306 parseTime(start, end, shortYear) {
190307 let s = this.parseStringISO(start, end).str,
191308 m = (shortYear ? reTimeS : reTimeL).exec(s);
192309 if (!m)
193193- return 'Unrecognized time: ' + s;
310310+ throw new Error('Unrecognized time: ' + s);
194311 if (shortYear) {
195312 // to avoid querying the timer, use the fixed range [1970, 2069]
196313 // it will conform with ITU X.400 [-10, +40] sliding window until 2030
···213330 }
214331 return s;
215332 }
333333+334334+ /**
335335+ * Parse a region of the stream as an integer
336336+ * @param {number} start - starting position (included)
337337+ * @param {number} end - ending position (excluded)
338338+ * @returns {string} Formatted integer string
339339+ */
216340 parseInteger(start, end) {
217341 let v = this.get(start),
218218- neg = (v > 127),
219219- pad = neg ? 255 : 0,
220220- len,
221342 s = '';
343343+ const neg = (v > 127),
344344+ pad = neg ? 255 : 0;
222345 // skip unuseful bits (not allowed in DER)
223346 while (v == pad && ++start < end)
224347 v = this.get(start);
225225- len = end - start;
348348+ const len = end - start;
226349 if (len === 0)
227350 return neg ? '-1' : '0';
228351 // show bit length of huge integers
229352 if (len > 4) {
230230- s = v;
231231- len <<= 3;
232232- while (((s ^ pad) & 0x80) == 0) {
233233- s <<= 1;
234234- --len;
353353+ let v2 = v,
354354+ lenBit = len << 3;
355355+ while (((v2 ^ pad) & 0x80) == 0) {
356356+ v2 <<= 1;
357357+ --lenBit;
235358 }
236236- s = '(' + len + ' bit)\n';
359359+ s = '(' + lenBit + ' bit)\n';
237360 }
238361 // decode the integer
239362 if (neg) v = v - 256;
240240- let n = new Int10(v);
363363+ let n = BigInt(v);
241364 for (let i = start + 1; i < end; ++i)
242242- n.mulAdd(256, this.get(i));
243243- return s + n.toString();
365365+ n = (n << 8n) | BigInt(this.get(i));
366366+ return s + n;
244367 }
368368+369369+ /**
370370+ * Parse a region of the stream as a bit string.
371371+ * @param {number} start - starting position (included)
372372+ * @param {number} end - ending position (excluded)
373373+ * @param {number} maxLength - maximum length of the output string
374374+ * @returns {Object} Object with size and str properties
375375+ */
245376 parseBitString(start, end, maxLength) {
246246- let unusedBits = this.get(start);
377377+ const unusedBits = this.get(start);
247378 if (unusedBits > 7)
248248- throw 'Invalid BitString with unusedBits=' + unusedBits;
249249- let lenBit = ((end - start - 1) << 3) - unusedBits,
250250- s = '';
379379+ throw new Error('Invalid BitString with unusedBits=' + unusedBits);
380380+ const lenBit = ((end - start - 1) << 3) - unusedBits;
381381+ let s = '';
251382 for (let i = start + 1; i < end; ++i) {
252383 let b = this.get(i),
253384 skip = (i == end - 1) ? unusedBits : 0;
···258389 }
259390 return { size: lenBit, str: s };
260391 }
392392+393393+ /**
394394+ * Parse a region of the stream as an octet string.
395395+ * @param {number} start - starting position (included)
396396+ * @param {number} end - ending position (excluded)
397397+ * @param {number} maxLength - maximum length of the output string
398398+ * @returns {Object} Object with size and str properties
399399+ */
261400 parseOctetString(start, end, maxLength) {
262262- let len = end - start,
263263- s;
264401 try {
265265- s = this.parseStringUTF(start, end, maxLength);
402402+ let s = this.parseStringUTF(start, end, maxLength);
266403 checkPrintable(s.str);
267404 return { size: end - start, str: s.str };
268268- } catch (e) {
269269- // ignore
405405+ } catch (ignore) {
406406+ // If UTF-8 parsing fails, fall back to hexadecimal dump
270407 }
408408+ const len = end - start;
271409 maxLength /= 2; // we work in bytes
272410 if (len > maxLength)
273411 end = start + maxLength;
274274- s = '';
412412+ let s = '';
275413 for (let i = start; i < end; ++i)
276276- s += this.hexByte(this.get(i));
414414+ s += Stream.hexByte(this.get(i));
277415 if (len > maxLength)
278416 s += ellipsis;
279417 return { size: len, str: s };
280418 }
419419+420420+ /**
421421+ * Parse a region of the stream as an OID (Object Identifier).
422422+ * @param {number} start - starting position (included)
423423+ * @param {number} end - ending position (excluded)
424424+ * @param {number} maxLength - maximum length of the output string
425425+ * @param {boolean} isRelative - Whether the OID is relative
426426+ * @returns {string} Formatted OID string
427427+ */
281428 parseOID(start, end, maxLength, isRelative) {
282429 let s = '',
283283- n = new Int10(),
430430+ n = 0n,
284431 bits = 0;
285432 for (let i = start; i < end; ++i) {
286433 let v = this.get(i);
287287- n.mulAdd(128, v & 0x7F);
434434+ // Shift bits and add the lower 7 bits of the byte
435435+ n = (n << 7n) | BigInt(v & 0x7F);
288436 bits += 7;
437437+ // If the most significant bit is 0, this is the last byte of the OID component
289438 if (!(v & 0x80)) { // finished
439439+ // If this is the first component, handle it specially
290440 if (s === '') {
291291- n = n.simplify();
292441 if (isRelative) {
293293- s = (n instanceof Int10) ? n.toString() : '' + n;
294294- } else if (n instanceof Int10) {
295295- n.sub(80);
296296- s = '2.' + n.toString();
442442+ s = n.toString();
297443 } else {
298298- let m = n < 80 ? n < 40 ? 0 : 1 : 2;
299299- s = m + '.' + (n - m * 40);
444444+ let m = n < 80 ? n < 40 ? 0n : 1n : 2n;
445445+ s = m + '.' + (n - m * 40n);
300446 }
301447 } else
302302- s += '.' + n.toString();
448448+ s += '.' + n;
303449 if (s.length > maxLength)
304450 return stringCut(s, maxLength);
305305- n = new Int10();
451451+ n = 0n;
306452 bits = 0;
307453 }
308454 }
309455 if (bits > 0)
310456 s += '.incomplete';
457457+ // If OIDs mapping is available and the OID is absolute, try to resolve it
311458 if (typeof oids === 'object' && !isRelative) {
312459 let oid = oids[s];
313460 if (oid) {
···318465 }
319466 return s;
320467 }
468468+469469+ /**
470470+ * Parse a region of the stream as a relative OID (Object Identifier).
471471+ * @param {number} start - starting position (included)
472472+ * @param {number} end - ending position (excluded)
473473+ * @param {number} maxLength - maximum length of the output string
474474+ * @returns {string} Formatted relative OID string
475475+ */
321476 parseRelativeOID(start, end, maxLength) {
322477 return this.parseOID(start, end, maxLength, true);
323478 }
···350505 this.tagConstructed = ((buf & 0x20) !== 0);
351506 this.tagNumber = buf & 0x1F;
352507 if (this.tagNumber == 0x1F) { // long tag
353353- let n = new Int10();
508508+ let n = 0n;
354509 do {
355510 buf = stream.get();
356356- n.mulAdd(128, buf & 0x7F);
511511+ n = (n << 7n) | BigInt(buf & 0x7F);
357512 } while (buf & 0x80);
358358- this.tagNumber = n.simplify();
513513+ this.tagNumber = n <= Number.MAX_SAFE_INTEGER ? Number(n) : n;
359514 }
360515 }
361516 isUniversal() {
···366521 }
367522}
368523524524+/**
525525+ * ASN1 class for parsing ASN.1 encoded data.
526526+ * Instances of this class represent an ASN.1 element and provides methods to parse and display its content.
527527+ */
369528export class ASN1 {
529529+ /**
530530+ * Creates an ASN1 parser object.
531531+ * @param {Stream} stream - The stream containing the ASN.1 data.
532532+ * @param {number} header - The header length.
533533+ * @param {number} length - The length of the data.
534534+ * @param {ASN1Tag} tag - The ASN.1 tag.
535535+ * @param {number} tagLen - The length of the tag.
536536+ * @param {Array} sub - The sub-elements.
537537+ */
370538 constructor(stream, header, length, tag, tagLen, sub) {
371371- if (!(tag instanceof ASN1Tag)) throw 'Invalid tag value.';
539539+ if (!(tag instanceof ASN1Tag)) throw new Error('Invalid tag value.');
372540 this.stream = stream;
373541 this.header = header;
374542 this.length = length;
···376544 this.tagLen = tagLen;
377545 this.sub = sub;
378546 }
547547+548548+ /**
549549+ * Get the type name of the ASN.1 element.
550550+ * @returns {string} The type name.
551551+ */
379552 typeName() {
380553 switch (this.tag.tagClass) {
381554 case 0: // universal
···415588 case 3: return 'Private_' + this.tag.tagNumber.toString();
416589 }
417590 }
418418- /** A string preview of the content (intended for humans). */
591591+592592+ /**
593593+ * Get a string preview of the content (intended for humans).
594594+ * @param {number} maxLength - The maximum length of the content.
595595+ * @returns {string|null} The content preview or null if not supported.
596596+ */
419597 content(maxLength) {
420598 if (this.tag === undefined)
421599 return null;
422600 if (maxLength === undefined)
423601 maxLength = Infinity;
424424- let content = this.posContent(),
602602+ const content = this.posContent(),
425603 len = Math.abs(this.length);
426604 if (!this.tag.isUniversal()) {
427605 if (this.sub !== null)
···431609 }
432610 switch (this.tag.tagNumber) {
433611 case 0x01: // BOOLEAN
612612+ if (len != 1) return 'invalid length ' + len;
434613 return (this.stream.get(content) === 0) ? 'false' : 'true';
435614 case 0x02: // INTEGER
615615+ if (len < 1) return 'invalid length ' + len;
436616 return this.stream.parseInteger(content, content + len);
437617 case 0x03: { // BIT_STRING
438618 let d = recurse(this, 'parseBitString', maxLength);
···444624 }
445625 //case 0x05: // NULL
446626 case 0x06: // OBJECT_IDENTIFIER
627627+ if (len < 1) return 'invalid length ' + len; // pgut001's dumpasn1.c enforces a minimum lenght of 3
447628 return this.stream.parseOID(content, content + len, maxLength);
448629 //case 0x07: // ObjectDescriptor
449630 //case 0x08: // EXTERNAL
···480661 }
481662 return null;
482663 }
664664+665665+ /**
666666+ * Get a string representation of the ASN.1 element.
667667+ * @returns {string} The string representation.
668668+ */
483669 toString() {
484670 return this.typeName() + '@' + this.stream.pos + '[header:' + this.header + ',length:' + this.length + ',sub:' + ((this.sub === null) ? 'null' : this.sub.length) + ']';
485671 }
672672+673673+ /**
674674+ * Get a pretty string representation of the ASN.1 element.
675675+ * @param {string} indent - The indentation string.
676676+ * @returns {string} The pretty string representation.
677677+ */
486678 toPrettyString(indent) {
487679 if (indent === undefined) indent = '';
488488- let s = indent + this.typeName() + ' @' + this.stream.pos;
680680+ let s = indent;
681681+ if (this.def) {
682682+ if (this.def.id)
683683+ s += this.def.id + ' ';
684684+ if (this.def.name && this.def.name != this.typeName().replace(/_/g, ' '))
685685+ s+= this.def.name + ' ';
686686+ if (this.def.mismatch)
687687+ s += '[?] ';
688688+ }
689689+ s += this.typeName() + ' @' + this.stream.pos;
489690 if (this.length >= 0)
490691 s += '+';
491692 s += this.length;
···504705 }
505706 return s;
506707 }
708708+709709+ /**
710710+ * Get the starting position of the element in the stream.
711711+ * @returns {number} The starting position.
712712+ */
507713 posStart() {
508714 return this.stream.pos;
509715 }
716716+717717+ /**
718718+ * Get the position of the content in the stream.
719719+ * @returns {number} The content position.
720720+ */
510721 posContent() {
511722 return this.stream.pos + this.header;
512723 }
724724+725725+ /**
726726+ * Get the ending position of the element in the stream.
727727+ * @returns {number} The ending position.
728728+ */
513729 posEnd() {
514730 return this.stream.pos + this.header + Math.abs(this.length);
515731 }
516516- /** Position of the length. */
732732+733733+ /**
734734+ * Get the position of the length in the stream.
735735+ * @returns {number} The length position.
736736+ */
517737 posLen() {
518738 return this.stream.pos + this.tagLen;
519739 }
520520- toHexString() {
521521- return this.stream.hexDump(this.posStart(), this.posEnd(), true);
740740+741741+ /**
742742+ * Get a hexadecimal dump of the node.
743743+ * @param {string} [type='raw'] - The dump type: 'raw', 'byte', or 'dump'.
744744+ * @returns {string} The hexadecimal dump.
745745+ */
746746+ toHexString(type = 'raw') {
747747+ return this.stream.hexDump(this.posStart(), this.posEnd(), type);
522748 }
523523- toB64String() {
524524- return this.stream.b64Dump(this.posStart(), this.posEnd());
749749+750750+ /**
751751+ * Get a base64url dump of the node (according to RFC 4648 section 5).
752752+ * @param {string} [type='url'] - The dump type: 'url' (section 5 without padding) or 'std' (section 4 with padding).
753753+ * @returns {string} The base64 encoded representation.
754754+ */
755755+ toB64String(type = 'url') {
756756+ return this.stream.b64Dump(this.posStart(), this.posEnd(), type);
525757 }
758758+759759+ /**
760760+ * Decode the length field of an ASN.1 element.
761761+ * @param {Stream} stream - The stream to read from.
762762+ * @returns {number|null} The decoded length, or null for indefinite length.
763763+ * @throws {Error} If the length is invalid or exceeds 48 bits.
764764+ */
526765 static decodeLength(stream) {
527527- let buf = stream.get(),
766766+ const buf = stream.get(),
528767 len = buf & 0x7F;
529768 if (len == buf) // first bit was 0, short form
530769 return len;
531770 if (len === 0) // long form with length 0 is a special case
532771 return null; // undefined length
533533- if (len > 6) // no reason to use Int10, as it would be a huge buffer anyways
534534- throw 'Length over 48 bits not supported at position ' + (stream.pos - 1);
535535- buf = 0;
772772+ if (len > 6) // no reason to use BigInt, as it would be a huge buffer anyways
773773+ throw new Error('Length over 48 bits not supported at position ' + (stream.pos - 1));
774774+ let value = 0;
536775 for (let i = 0; i < len; ++i)
537537- buf = (buf * 256) + stream.get();
538538- return buf;
776776+ value = (value << 8) | stream.get();
777777+ return value;
539778 }
779779+780780+ /**
781781+ * Decode an ASN.1 element from a stream.
782782+ * @param {Stream|array|string} stream - The input data.
783783+ * @param {number} [offset=0] - The offset to start decoding from.
784784+ * @param {Function} [type=ASN1] - The class to instantiate.
785785+ * @returns {ASN1} The decoded ASN.1 element.
786786+ * @throws {Error} If the decoding fails.
787787+ */
540788 static decode(stream, offset, type = ASN1) {
541789 if (!(type == ASN1 || type.prototype instanceof ASN1))
542542- throw 'Must pass a class that extends ASN1';
790790+ throw new Error('Must pass a class that extends ASN1');
543791 if (!(stream instanceof Stream))
544792 stream = new Stream(stream, offset || 0);
545793 let streamStart = new Stream(stream),
···555803 // definite length
556804 let end = start + len;
557805 if (end > stream.enc.length)
558558- throw 'Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream';
806806+ throw new Error('Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream');
559807 while (stream.pos < end)
560808 sub[sub.length] = type.decode(stream);
561809 if (stream.pos != end)
562562- throw 'Content size is not correct for container at offset ' + start;
810810+ throw new Error('Content size is not correct for container at offset ' + start);
563811 } else {
564812 // undefined length
565813 try {
···571819 }
572820 len = start - stream.pos; // undefined lengths are represented as negative values
573821 } catch (e) {
574574- throw 'Exception while decoding undefined length content at offset ' + start + ': ' + e;
822822+ throw new Error('Exception while decoding undefined length content at offset ' + start + ': ' + e);
575823 }
576824 }
577825 };
···583831 try {
584832 if (tag.tagNumber == 0x03)
585833 if (stream.get() != 0)
586586- throw 'BIT STRINGs with unused bits cannot encapsulate.';
834834+ throw new Error('BIT STRINGs with unused bits cannot encapsulate.');
587835 getSub();
588588- for (let i = 0; i < sub.length; ++i)
589589- if (sub[i].tag.isEOC())
590590- throw 'EOC is not supposed to be actual content.';
591591- } catch (e) {
836836+ for (let s of sub) {
837837+ if (s.tag.isEOC())
838838+ throw new Error('EOC is not supposed to be actual content.');
839839+ try {
840840+ s.content();
841841+ } catch (e) {
842842+ throw new Error('Unable to parse content: ' + e);
843843+ }
844844+ }
845845+ } catch (ignore) {
592846 // but silently ignore when they don't
593847 sub = null;
594848 //DEBUG console.log('Could not decode structure at ' + start + ':', e);
···596850 }
597851 if (sub === null) {
598852 if (len === null)
599599- throw "We can't skip over an invalid tag with undefined length at offset " + start;
853853+ throw new Error("We can't skip over an invalid tag with undefined length at offset " + start);
600854 stream.pos = start + Math.abs(len);
601855 }
602856 return new type(streamStart, header, len, tag, tagLen, sub);
+8-7
base64.js
···11// Base64 JavaScript decoder
22-// Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
22+// Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3344// Permission to use, copy, modify, and/or distribute this software for any
55// purpose with or without fee is hereby granted, provided that the above
66// copyright notice and this permission notice appear in all copies.
77-//
77+//
88// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
99// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1010// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
···3131 decoder[b64.charCodeAt(i)] = i;
3232 for (i = 0; i < ignore.length; ++i)
3333 decoder[ignore.charCodeAt(i)] = -1;
3434- // RFC 3548 URL & file safe encoding
3434+ // also support decoding Base64url (RFC 4648 section 5)
3535 decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)];
3636 decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)];
3737 }
···75757676 static pretty(str) {
7777 // fix padding
7878- if (str.length % 4 > 0)
7979- str = (str + '===').slice(0, str.length + str.length % 4);
8080- // convert RFC 3548 to standard Base64
7878+ let pad = 4 - str.length % 4;
7979+ if (pad < 4)
8080+ str += '==='.slice(0, pad);
8181+ // convert Base64url (RFC 4648 section 5) to standard Base64 (RFC 4648 section 4)
8182 str = str.replace(/-/g, '+').replace(/_/g, '/');
8283 // 80 column width
8383- return str.replace(/(.{80})/g, '$1\n');
8484+ return str.replace(/.{80}/g, '$&\n');
8485 }
85868687 static unarmor(a) {
···11+This is a PKCS#7/CMS encrypted with passwod.
22+$ echo content | openssl cms -encrypt -pwri_password test -aes256 -outform pem -out examples/cms-password.p7m
33+-----BEGIN CMS-----
44+MIHYBgkqhkiG9w0BBwOggcowgccCAQMxgYOjgYACAQCgGwYJKoZIhvcNAQUMMA4E
55+CED/DSxXMtH6AgIIADAsBgsqhkiG9w0BCRADCTAdBglghkgBZQMEASoEEDIQbJMC
66+Sfb3LpwHduj/meQEMKwrwq5M4V0stztm6OUTAsFY2zKDY20SApwSEeEcAh9TM42E
77+1palnHeqHTBpC8pIpjA8BgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBByt+scPrdM
88+giR7WUOJyB3hgBDcD3UDMtZSep8X/3yy1/Yq
99+-----END CMS-----
+12
examples/crl-rfc5280.b64
···11+CRL example from RFC5280 as found here:
22+https://csrc.nist.gov/projects/pki-testing/sample-certificates-and-crls
33+44+begin-base64 644 crl-rfc5280.der
55+MIIBYDCBygIBATANBgkqhkiG9w0BAQUFADBDMRMwEQYKCZImiZPyLGQBGRYDY29tMRcwFQYKCZIm
66+iZPyLGQBGRYHZXhhbXBsZTETMBEGA1UEAxMKRXhhbXBsZSBDQRcNMDUwMjA1MTIwMDAwWhcNMDUw
77+MjA2MTIwMDAwWjAiMCACARIXDTA0MTExOTE1NTcwM1owDDAKBgNVHRUEAwoBAaAvMC0wHwYDVR0j
88+BBgwFoAUCGivhTPIOUp6+IKTjnBqSiCELDIwCgYDVR0UBAMCAQwwDQYJKoZIhvcNAQEFBQADgYEA
99+ItwYffcIzsx10NBqm60Q9HYjtIFutW2+DvsVFGzIF20f7pAXom9g5L2qjFXejoRvkvifEBInr0rU
1010+L4XiNkR9qqNMJTgV/wD9Pn7uPSYS69jnK2LiK8NGgO94gtEVxtCccmrLznrtZ5mLbnCBfUNCdMGm
1111+r8FVF6IzTNYGmCuk/C4=
1212+====
+45
examples/crl-rfc5280.b64.dump
···11+CertificateList SEQUENCE @0+352 (constructed): (3 elem)
22+ tbsCertList TBSCertList SEQUENCE @4+202 (constructed): (7 elem)
33+ version Version INTEGER @7+1: 1
44+ signature AlgorithmIdentifier SEQUENCE @10+13 (constructed): (2 elem)
55+ algorithm OBJECT_IDENTIFIER @12+9: 1.2.840.113549.1.1.5|sha1WithRSAEncryption|PKCS #1
66+ parameters ANY NULL @23+0
77+ issuer rdnSequence Name SEQUENCE @25+67 (constructed): (3 elem)
88+ RelativeDistinguishedName SET @27+19 (constructed): (1 elem)
99+ AttributeTypeAndValue SEQUENCE @29+17 (constructed): (2 elem)
1010+ type AttributeType OBJECT_IDENTIFIER @31+10: 0.9.2342.19200300.100.1.25|domainComponent|Men are from Mars, this OID is from Pluto
1111+ value AttributeValue [?] IA5String @43+3: com
1212+ RelativeDistinguishedName SET @48+23 (constructed): (1 elem)
1313+ AttributeTypeAndValue SEQUENCE @50+21 (constructed): (2 elem)
1414+ type AttributeType OBJECT_IDENTIFIER @52+10: 0.9.2342.19200300.100.1.25|domainComponent|Men are from Mars, this OID is from Pluto
1515+ value AttributeValue [?] IA5String @64+7: example
1616+ RelativeDistinguishedName SET @73+19 (constructed): (1 elem)
1717+ AttributeTypeAndValue SEQUENCE @75+17 (constructed): (2 elem)
1818+ type AttributeType OBJECT_IDENTIFIER @77+3: 2.5.4.3|commonName|X.520 DN component
1919+ value AttributeValue [?] PrintableString @82+10: Example CA
2020+ thisUpdate utcTime Time UTCTime @94+13: 2005-02-05 12:00:00 UTC
2121+ nextUpdate utcTime Time UTCTime @109+13: 2005-02-06 12:00:00 UTC
2222+ revokedCertificates SEQUENCE @124+34 (constructed): (1 elem)
2323+ SEQUENCE @126+32 (constructed): (3 elem)
2424+ userCertificate CertificateSerialNumber INTEGER @128+1: 18
2525+ revocationDate utcTime Time UTCTime @131+13: 2004-11-19 15:57:03 UTC
2626+ crlEntryExtensions Extensions SEQUENCE @146+12 (constructed): (1 elem)
2727+ Extension SEQUENCE @148+10 (constructed): (2 elem)
2828+ extnID OBJECT_IDENTIFIER @150+3: 2.5.29.21|cRLReason|X.509 extension
2929+ extnValue OCTET_STRING @155+3 (encapsulates): (3 byte)|0A0101
3030+ ENUMERATED @157+1: 1
3131+ crlExtensions [0] @160+47 (constructed): (1 elem)
3232+ Extensions SEQUENCE @162+45 (constructed): (2 elem)
3333+ Extension SEQUENCE @164+31 (constructed): (2 elem)
3434+ extnID OBJECT_IDENTIFIER @166+3: 2.5.29.35|authorityKeyIdentifier|X.509 extension
3535+ extnValue OCTET_STRING @171+24 (encapsulates): (24 byte)|301680140868AF8533C8394A7AF882938E706A4A20842C32
3636+ SEQUENCE @173+22 (constructed): (1 elem)
3737+ [0] @175+20: (20 byte)|0868AF8533C8394A7AF882938E706A4A20842C32
3838+ Extension SEQUENCE @197+10 (constructed): (2 elem)
3939+ extnID OBJECT_IDENTIFIER @199+3: 2.5.29.20|cRLNumber|X.509 extension
4040+ extnValue OCTET_STRING @204+3 (encapsulates): (3 byte)|02010C
4141+ INTEGER @206+1: 12
4242+ signatureAlgorithm AlgorithmIdentifier SEQUENCE @209+13 (constructed): (2 elem)
4343+ algorithm OBJECT_IDENTIFIER @211+9: 1.2.840.113549.1.1.5|sha1WithRSAEncryption|PKCS #1
4444+ parameters ANY NULL @222+0
4545+ signature BIT_STRING @224+129: (1024 bit)|0010001011011100000110000111110111110111000010001100111011001100011101011101000011010000011010101001101110101101000100001111010001110110001000111011010010000001011011101011010101101101101111100000111011111011000101010001010001101100110010000001011101101101000111111110111010010000000101111010001001101111011000001110010010111101101010101000110001010101110111101000111010000100011011111001001011111000100111110001000000010010001001111010111101001010110101000010111110000101111000100011011001000100011111011010101010100011010011000010010100111000000101011111111100000000111111010011111001111110111011100011110100100110000100101110101111011000111001110010101101100010111000100010101111000011010001101000000011101111011110001000001011010001000101011100011011010000100111000111001001101010110010111100111001111010111011010110011110011001100010110110111001110000100000010111110101000011010000100111010011000001101001101010111111000001010101010001011110100010001100110100110011010110000001101001100000101011101001001111110000101110
···11+LDAPMessage example as found on ldap.com.
22+33+Original link:
44+https://ldap.com/ldapv3-wire-protocol-reference-ldap-message/
55+66+begin-base64 644 ldapmessage.der
77+MDUCAQVKEWRjPWV4YW1wbGUsZGM9Y29toB0wGwQWMS4yLjg0MC4xMTM1NTYuMS40LjgwNQEB/w==
88+====
···11// Hex JavaScript decoder
22-// Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
22+// Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3344// Permission to use, copy, modify, and/or distribute this software for any
55// purpose with or without fee is hereby granted, provided that the above
66// copyright notice and this permission notice appear in all copies.
77-//
77+//
88// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
99// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1010// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
···11+import './theme.js';
12import { ASN1DOM } from './dom.js';
23import { Base64 } from './base64.js';
34import { Hex } from './hex.js';
···1516 area = id('area'),
1617 file = id('file'),
1718 examples = id('examples'),
1818- selectTheme = id('theme-select'),
1919 selectDefs = id('definitions'),
2020 selectTag = id('tags');
2121···4141function show(asn1) {
4242 tree.innerHTML = '';
4343 dump.innerHTML = '';
4444- tree.appendChild(asn1.toDOM());
4444+ let ul = document.createElement('ul');
4545+ ul.className = 'treecollapse';
4646+ tree.appendChild(ul);
4747+ ul.appendChild(asn1.toDOM());
4548 if (wantHex.checked) dump.appendChild(asn1.toHexDOM(undefined, trimHex.checked));
4649}
4747-function decode(der, offset) {
5050+export function decode(der, offset) {
4851 offset = offset || 0;
4952 try {
5053 const asn1 = ASN1DOM.decode(der, offset);
···8386 if (area.value === '') area.value = Base64.pretty(b64);
8487 try {
8588 window.location.hash = hash = '#' + b64;
8686- } catch (e) {
8989+ } catch (ignore) {
8790 // fails with "Access Denied" on IE with URLs longer than ~2048 chars
8891 window.location.hash = hash = '#';
8992 }
···103106 text(tree, e);
104107 }
105108}
106106-function decodeText(val) {
109109+export function decodeText(val) {
107110 try {
108111 let der = reHex.test(val) ? Hex.decode(val) : Base64.unarmor(val);
109112 decode(der);
···112115 dump.innerHTML = '';
113116 }
114117}
115115-function decodeBinaryString(str) {
118118+export function decodeBinaryString(str) {
116119 let der;
117120 try {
118121 if (reHex.test(str)) der = Hex.decode(str);
119122 else if (Base64.re.test(str)) der = Base64.unarmor(str);
120123 else der = str;
121124 decode(der);
122122- } catch (e) {
125125+ } catch (ignore) {
123126 text(tree, 'Cannot decode file.');
124127 dump.innerHTML = '';
125128 }
126129}
127130// set up buttons
128128-id('butDecode').onclick = function () {
129129- decodeText(area.value);
131131+const butClickHandlers = {
132132+ butDecode: () => {
133133+ decodeText(area.value);
134134+ },
135135+ butClear: () => {
136136+ area.value = '';
137137+ file.value = '';
138138+ tree.innerHTML = '';
139139+ dump.innerHTML = '';
140140+ selectDefs.innerHTML = '';
141141+ hash = window.location.hash = '';
142142+ },
143143+ butExample: () => {
144144+ console.log('Loading example:', examples.value);
145145+ let request = new XMLHttpRequest();
146146+ request.open('GET', 'examples/' + examples.value, true);
147147+ request.onreadystatechange = function () {
148148+ if (this.readyState !== 4) return;
149149+ if (this.status >= 200 && this.status < 400) {
150150+ area.value = this.responseText;
151151+ decodeText(this.responseText);
152152+ } else {
153153+ console.log('Error loading example.');
154154+ }
155155+ };
156156+ request.send();
157157+ },
130158};
131131-id('butClear').onclick = function () {
132132- area.value = '';
133133- file.value = '';
134134- tree.innerHTML = '';
135135- dump.innerHTML = '';
136136- selectDefs.innerHTML = '';
137137- hash = window.location.hash = '';
138138-};
139139-id('butExample').onclick = function () {
140140- console.log('Loading example:', examples.value);
141141- let request = new XMLHttpRequest();
142142- request.open('GET', 'examples/' + examples.value, true);
143143- request.onreadystatechange = function () {
144144- if (this.readyState !== 4) return;
145145- if (this.status >= 200 && this.status < 400) {
146146- area.value = this.responseText;
147147- decodeText(this.responseText);
148148- } else {
149149- console.log('Error loading example.');
150150- }
151151- };
152152- request.send();
153153-};
154154-// set dark theme depending on OS settings
155155-function setTheme() {
156156- let storedTheme = localStorage.getItem('theme');
157157- let theme = 'os';
158158- if (storedTheme)
159159- theme = storedTheme;
160160- selectTheme.value = theme;
161161- if (theme == 'os') {
162162- let prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
163163- theme = prefersDarkScheme.matches ? 'dark': 'light';
164164- }
165165- if (theme == 'dark') {
166166- const css1 = id('theme-base');
167167- const css2 = css1.cloneNode();
168168- css2.id = 'theme-override';
169169- css2.href = 'index-' + theme + '.css';
170170- css1.parentElement.appendChild(css2);
171171- } else {
172172- const css2 = id('theme-override');
173173- if (css2) css2.remove();
174174- }
159159+for (const [name, onClick] of Object.entries(butClickHandlers)) {
160160+ let elem = id(name);
161161+ if (elem)
162162+ elem.onclick = onClick;
175163}
176176-setTheme();
177177-selectTheme.addEventListener('change', function () {
178178- localStorage.setItem('theme', selectTheme.value);
179179- setTheme();
180180-});
181164// this is only used if window.FileReader
182165function read(f) {
183166 area.value = ''; // clear text area, will get b64 content
-106
int10.js
···11-// Big integer base-10 printing library
22-// Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
33-44-// Permission to use, copy, modify, and/or distribute this software for any
55-// purpose with or without fee is hereby granted, provided that the above
66-// copyright notice and this permission notice appear in all copies.
77-//
88-// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
99-// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1010-// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1111-// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1212-// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1313-// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1414-// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515-1616-let max = 10000000000000; // biggest 10^n integer that can still fit 2^53 when multiplied by 256
1717-1818-export class Int10 {
1919- /**
2020- * Arbitrary length base-10 value.
2121- * @param {number} value - Optional initial value (will be 0 otherwise).
2222- */
2323- constructor(value) {
2424- this.buf = [+value || 0];
2525- }
2626-2727- /**
2828- * Multiply value by m and add c.
2929- * @param {number} m - multiplier, must be < =256
3030- * @param {number} c - value to add
3131- */
3232- mulAdd(m, c) {
3333- // assert(m <= 256)
3434- let b = this.buf,
3535- l = b.length,
3636- i, t;
3737- for (i = 0; i < l; ++i) {
3838- t = b[i] * m + c;
3939- if (t < max)
4040- c = 0;
4141- else {
4242- c = 0|(t / max);
4343- t -= c * max;
4444- }
4545- b[i] = t;
4646- }
4747- if (c > 0)
4848- b[i] = c;
4949- }
5050-5151- /**
5252- * Subtract value.
5353- * @param {number} c - value to subtract
5454- */
5555- sub(c) {
5656- let b = this.buf,
5757- l = b.length,
5858- i, t;
5959- for (i = 0; i < l; ++i) {
6060- t = b[i] - c;
6161- if (t < 0) {
6262- t += max;
6363- c = 1;
6464- } else
6565- c = 0;
6666- b[i] = t;
6767- }
6868- while (b[b.length - 1] === 0)
6969- b.pop();
7070- }
7171-7272- /**
7373- * Convert to decimal string representation.
7474- * @param {*} base - optional value, only value accepted is 10
7575- */
7676- toString(base) {
7777- if ((base || 10) != 10)
7878- throw 'only base 10 is supported';
7979- let b = this.buf,
8080- s = b[b.length - 1].toString();
8181- for (let i = b.length - 2; i >= 0; --i)
8282- s += (max + b[i]).toString().substring(1);
8383- return s;
8484- }
8585-8686- /**
8787- * Convert to Number value representation.
8888- * Will probably overflow 2^53 and thus become approximate.
8989- */
9090- valueOf() {
9191- let b = this.buf,
9292- v = 0;
9393- for (let i = b.length - 1; i >= 0; --i)
9494- v = v * max + b[i];
9595- return v;
9696- }
9797-9898- /**
9999- * Return value as a simple Number (if it is <= 10000000000000), or return this.
100100- */
101101- simplify() {
102102- let b = this.buf;
103103- return (b.length == 1) ? b[0] : this;
104104- }
105105-106106-}