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:
parent
ec27877ffa
commit
b593fc6d8e
2 changed files with 26 additions and 6 deletions
|
@ -51,8 +51,10 @@ bitjs.io.BitStream = class {
|
||||||
*/
|
*/
|
||||||
peekBits_ltr(n, opt_movePointers) {
|
peekBits_ltr(n, opt_movePointers) {
|
||||||
let num = parseInt(n, 10);
|
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';
|
throw 'Error! Called peekBits_ltr() with a non-positive integer';
|
||||||
|
} else if (num === 0) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const movePointers = opt_movePointers || false;
|
const movePointers = opt_movePointers || false;
|
||||||
|
@ -112,8 +114,10 @@ bitjs.io.BitStream = class {
|
||||||
*/
|
*/
|
||||||
peekBits_rtl(n, opt_movePointers) {
|
peekBits_rtl(n, opt_movePointers) {
|
||||||
let num = parseInt(n, 10);
|
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';
|
throw 'Error! Called peekBits_rtl() with a non-positive integer';
|
||||||
|
} else if (num === 0) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const movePointers = opt_movePointers || false;
|
const movePointers = opt_movePointers || false;
|
||||||
|
@ -189,8 +193,10 @@ bitjs.io.BitStream = class {
|
||||||
*/
|
*/
|
||||||
peekBytes(n, opt_movePointers) {
|
peekBytes(n, opt_movePointers) {
|
||||||
const num = parseInt(n, 10);
|
const num = parseInt(n, 10);
|
||||||
if (n !== num || num <= 0) {
|
if (n !== num || num < 0) {
|
||||||
throw 'Error! Called peekBytes() with a non-positive integer';
|
throw 'Error! Called peekBytes() with a non-positive integer';
|
||||||
|
} else if (num === 0) {
|
||||||
|
return new Uint8Array();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush bits until we are byte-aligned.
|
// Flush bits until we are byte-aligned.
|
||||||
|
|
|
@ -36,6 +36,13 @@ bitjs.io.ByteStream = class {
|
||||||
this.ptr = 0;
|
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
|
* 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) {
|
peekNumber(n) {
|
||||||
let num = parseInt(n, 10);
|
let num = parseInt(n, 10);
|
||||||
if (n !== num || num <= 0) {
|
if (n !== num || num < 0) {
|
||||||
throw 'Error! Called peekNumber() with a non-positive integer';
|
throw 'Error! Called peekNumber() with a non-positive integer';
|
||||||
|
} else if (num === 0) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = 0;
|
let result = 0;
|
||||||
|
@ -120,9 +129,12 @@ bitjs.io.ByteStream = class {
|
||||||
let num = parseInt(n, 10);
|
let num = parseInt(n, 10);
|
||||||
if (n !== num || num <= 0) {
|
if (n !== num || num <= 0) {
|
||||||
throw 'Error! Called peekBytes() with a non-positive integer';
|
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 +
|
throw 'Error! Overflowed the byte stream! n=' + num + ', ptr=' + this.ptr +
|
||||||
', bytes.length=' + this.bytes.length;
|
', bytes.length=' + this.bytes.length;
|
||||||
}
|
}
|
||||||
|
@ -152,8 +164,10 @@ bitjs.io.ByteStream = class {
|
||||||
*/
|
*/
|
||||||
peekString(n) {
|
peekString(n) {
|
||||||
let num = parseInt(n, 10);
|
let num = parseInt(n, 10);
|
||||||
if (n !== num || num <= 0) {
|
if (n !== num || num < 0) {
|
||||||
throw 'Error! Called peekString() with a non-positive integer';
|
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.
|
// TODO: return error if n would go past the end of the stream.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue