1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-06 02:39:55 +02:00

Improve parameter validation and test coverage. Also add errorws when trying to seek past the end of the stream

This commit is contained in:
codedread 2018-01-19 17:40:43 -08:00
parent 981ac156ac
commit ec27877ffa
3 changed files with 150 additions and 59 deletions

View file

@ -13,6 +13,8 @@ var bitjs = bitjs || {};
bitjs.io = bitjs.io || {};
// TODO: Add method for pushing bits (multiple arrays) and have tests.
// TODO: Add method for tee-ing off the stream with tests.
/**
* This object allows you to peek and consume bytes as numbers and strings
* out of an ArrayBuffer. In this buffer, everything must be byte-aligned.
@ -24,6 +26,10 @@ bitjs.io.ByteStream = class {
* @param {number=} opt_length The length of this BitStream
*/
constructor(ab, opt_offset, opt_length) {
if (!(ab instanceof ArrayBuffer)) {
throw 'Error! BitArray constructed with an invalid ArrayBuffer object';
}
const offset = opt_offset || 0;
const length = opt_length || ab.byteLength;
this.bytes = new Uint8Array(ab, offset, length);
@ -35,19 +41,24 @@ bitjs.io.ByteStream = class {
* Peeks at the next n bytes as an unsigned number but does not advance the
* pointer
* TODO: This apparently cannot read more than 4 bytes as a number?
* @param {number} n The number of bytes to peek at.
* @param {number} n The number of bytes to peek at. Must be a positive integer.
* @return {number} The n bytes interpreted as an unsigned number.
*/
peekNumber(n) {
// TODO: return error if n would go past the end of the stream?
if (n <= 0 || typeof n != typeof 1) {
return -1;
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
throw 'Error! Called peekNumber() with a non-positive integer';
}
let result = 0;
// read from last byte to first byte and roll them in
let curByte = this.ptr + n - 1;
let curByte = this.ptr + num - 1;
while (curByte >= this.ptr) {
if (curByte >= this.bytes.byteLength) {
throw 'Error! Overflowed the byte stream while peekNumber()! n=' + num +
', ptr=' + this.ptr + ', bytes.length=' + this.bytes.length;
}
result <<= 8;
result |= this.bytes[curByte];
--curByte;
@ -59,11 +70,11 @@ bitjs.io.ByteStream = class {
/**
* Returns the next n bytes as an unsigned number (or -1 on error)
* and advances the stream pointer n bytes.
* @param {number} n The number of bytes to read.
* @param {number} n The number of bytes to read. Must be a positive integer.
* @return {number} The n bytes interpreted as an unsigned number.
*/
readNumber(n) {
const num = this.peekNumber( n );
const num = this.peekNumber(n);
this.ptr += n;
return num;
}
@ -72,7 +83,7 @@ bitjs.io.ByteStream = class {
/**
* Returns the next n bytes as a signed number but does not advance the
* pointer.
* @param {number} n The number of bytes to read.
* @param {number} n The number of bytes to read. Must be a positive integer.
* @return {number} The bytes interpreted as a signed number.
*/
peekSignedNumber(n) {
@ -88,7 +99,7 @@ bitjs.io.ByteStream = class {
/**
* Returns the next n bytes as a signed number and advances the stream pointer.
* @param {number} n The number of bytes to read.
* @param {number} n The number of bytes to read. Must be a positive integer.
* @return {number} The bytes interpreted as a signed number.
*/
readSignedNumber(n) {
@ -101,19 +112,25 @@ bitjs.io.ByteStream = class {
/**
* This returns n bytes as a sub-array, advancing the pointer if movePointers
* is true.
* @param {number} n The number of bytes to read.
* @param {number} n The number of bytes to read. Must be a positive integer.
* @param {boolean} movePointers Whether to move the pointers.
* @return {Uint8Array} The subarray.
*/
peekBytes(n, movePointers) {
if (n <= 0 || typeof n != typeof 1) {
return null;
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
throw 'Error! Called peekBytes() with a non-positive integer';
}
const result = this.bytes.subarray(this.ptr, this.ptr + n);
if (this.ptr + num > this.bytes.byteLength) {
throw 'Error! Overflowed the byte stream! n=' + num + ', ptr=' + this.ptr +
', bytes.length=' + this.bytes.length;
}
const result = this.bytes.subarray(this.ptr, this.ptr + num);
if (movePointers) {
this.ptr += n;
this.ptr += num;
}
return result;
@ -121,7 +138,7 @@ bitjs.io.ByteStream = class {
/**
* Reads the next n bytes as a sub-array.
* @param {number} n The number of bytes to read.
* @param {number} n The number of bytes to read. Must be a positive integer.
* @return {Uint8Array} The subarray.
*/
readBytes(n) {
@ -129,17 +146,25 @@ bitjs.io.ByteStream = class {
}
/**
* Peeks at the next n bytes as a string but does not advance the pointer.
* @param {number} n The number of bytes to peek at.
* Peeks at the next n bytes as an ASCII string but does not advance the pointer.
* @param {number} n The number of bytes to peek at. Must be a positive integer.
* @return {string} The next n bytes as a string.
*/
peekString(n) {
if (n <= 0 || typeof n != typeof 1) {
return "";
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
throw 'Error! Called peekString() with a non-positive integer';
}
// TODO: return error if n would go past the end of the stream.
let result = "";
for (let p = this.ptr, end = this.ptr + n; p < end; ++p) {
if (p >= this.bytes.byteLength) {
throw 'Error! Overflowed the byte stream while peekString()! n=' + num +
', ptr=' + this.ptr + ', bytes.length=' + this.bytes.length;
}
result += String.fromCharCode(this.bytes[p]);
}
return result;
@ -148,7 +173,7 @@ bitjs.io.ByteStream = class {
/**
* Returns the next n bytes as an ASCII string and advances the stream pointer
* n bytes.
* @param {number} n The number of bytes to read.
* @param {number} n The number of bytes to read. Must be a positive integer.
* @return {string} The next n bytes as a string.
*/
readString(n) {