JavaScript generic ASN.1 parser (mirror)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Improve input checking. Avoid checking type on each single access.

+13 -2
+13 -2
asn1.js
··· 60 60 * It uses an existing array or binary string and advances a position index. */ 61 61 class Stream { 62 62 63 + /** 64 + * @param {Stream|array|string} enc data (will not be copied) 65 + * @param {?number} pos starting position (mandatory when `end` is not a Stream) 66 + */ 63 67 constructor(enc, pos) { 64 68 if (enc instanceof Stream) { 65 69 this.enc = enc.enc; 66 70 this.pos = enc.pos; 67 71 } else { 68 - // enc should be an array or a binary string 69 72 this.enc = enc; 70 73 this.pos = pos; 71 74 } 75 + if (typeof this.pos != 'number') 76 + throw new Error('"pos" must be a numeric value'); 77 + if (typeof this.enc == 'string') 78 + this.getRaw = pos => this.enc.charCodeAt(pos); 79 + else if (typeof this.enc[0] == 'number') 80 + this.getRaw = pos => this.enc[pos]; 81 + else 82 + throw new Error('"enc" must be a numeric array or a string'); 72 83 } 73 84 /** Get the byte at current position (and increment it) or at a specified position (and avoid moving current position). 74 85 * @param {?number} pos read position if specified, else current position (and increment it) */ ··· 77 88 pos = this.pos++; 78 89 if (pos >= this.enc.length) 79 90 throw new Error('Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length); 80 - return (typeof this.enc == 'string') ? this.enc.charCodeAt(pos) : this.enc[pos]; 91 + return this.getRaw(pos); 81 92 } 82 93 /** Convert a single byte to an hexadcimal string (of length 2). 83 94 * @param {number} b */