1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 17:49:16 +02:00

Allow 0-length reads, the unarchivers expect this

This commit is contained in:
codedread 2018-01-19 20:16:28 -08:00
parent ec27877ffa
commit b593fc6d8e
2 changed files with 26 additions and 6 deletions

View file

@ -51,8 +51,10 @@ bitjs.io.BitStream = class {
*/
peekBits_ltr(n, opt_movePointers) {
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
if (n !== num || num < 0) {
throw 'Error! Called peekBits_ltr() with a non-positive integer';
} else if (num === 0) {
return 0;
}
const movePointers = opt_movePointers || false;
@ -112,8 +114,10 @@ bitjs.io.BitStream = class {
*/
peekBits_rtl(n, opt_movePointers) {
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
if (n !== num || num < 0) {
throw 'Error! Called peekBits_rtl() with a non-positive integer';
} else if (num === 0) {
return 0;
}
const movePointers = opt_movePointers || false;
@ -189,8 +193,10 @@ bitjs.io.BitStream = class {
*/
peekBytes(n, opt_movePointers) {
const num = parseInt(n, 10);
if (n !== num || num <= 0) {
if (n !== num || num < 0) {
throw 'Error! Called peekBytes() with a non-positive integer';
} else if (num === 0) {
return new Uint8Array();
}
// Flush bits until we are byte-aligned.

View file

@ -36,6 +36,13 @@ bitjs.io.ByteStream = class {
this.ptr = 0;
}
/**
* Returns how many bytes are currently in the stream left to be read.
* @private
*/
getBytesLeft_() {
return (this.bytes.byteLength - this.ptr);
}
/**
* Peeks at the next n bytes as an unsigned number but does not advance the
@ -46,8 +53,10 @@ bitjs.io.ByteStream = class {
*/
peekNumber(n) {
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
if (n !== num || num < 0) {
throw 'Error! Called peekNumber() with a non-positive integer';
} else if (num === 0) {
return 0;
}
let result = 0;
@ -120,9 +129,12 @@ bitjs.io.ByteStream = class {
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
throw 'Error! Called peekBytes() with a non-positive integer';
} else if (num === 0) {
return new Uint8Array();
}
if (this.ptr + num > this.bytes.byteLength) {
if (num > this.getBytesLeft_()) {
//if (this.ptr + num > this.bytes.byteLength) {
throw 'Error! Overflowed the byte stream! n=' + num + ', ptr=' + this.ptr +
', bytes.length=' + this.bytes.length;
}
@ -152,8 +164,10 @@ bitjs.io.ByteStream = class {
*/
peekString(n) {
let num = parseInt(n, 10);
if (n !== num || num <= 0) {
if (n !== num || num < 0) {
throw 'Error! Called peekString() with a non-positive integer';
} else if (num === 0) {
return '';
}
// TODO: return error if n would go past the end of the stream.