Skip to content

Commit

Permalink
Add some JSDoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapo-luchini committed Sep 14, 2024
1 parent aa4d5c5 commit 2003ce7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function checkPrintable(s) {
}
}

/** Class to manage a stream of bytes, with a zero-copy approach.
* It uses an existing array or binary string and advances a position index. */
class Stream {

constructor(enc, pos) {
Expand All @@ -68,18 +70,24 @@ class Stream {
this.pos = pos;
}
}
/** Get the byte at current position (and increment it) or at a specified position (and avoid moving current position).
* @param {?number} pos read position if specified, else current position (and increment it) */
get(pos) {
if (pos === undefined)
pos = this.pos++;
if (pos >= this.enc.length)
throw new Error('Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length);
return (typeof this.enc == 'string') ? this.enc.charCodeAt(pos) : this.enc[pos];
}
hexByte(b) {
/** Convert a single byte to an hexadcimal string (of length 2).
* @param {number} b */
static hexByte(b) {
return hexDigits.charAt((b >> 4) & 0xF) + hexDigits.charAt(b & 0xF);
}
/** Hexadecimal dump.
* @param type 'raw', 'byte' or 'dump' */
/** Hexadecimal dump of a specified region of the stream.
* @param {number} start starting position (included)
* @param {number} end ending position (excluded)
* @param {string} type 'raw', 'byte' or 'dump' */
hexDump(start, end, type = 'dump') {
let s = '';
for (let i = start; i < end; ++i) {
Expand All @@ -95,6 +103,9 @@ class Stream {
}
return s;
}
/** Base-64 dump of a specified region of the stream.
* @param {number} start starting position (included)
* @param {number} end ending position (excluded) */
b64Dump(start, end) {
let extra = (end - start) % 3,
s = '',
Expand Down

0 comments on commit 2003ce7

Please sign in to comment.