Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to the KaitaiStream methods and fix 2 bugs #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Tests

on:
push:
branches:
- '*'
pull_request:
branches: [master]

jobs:
build:

strategy:
matrix:
# The first supported and avaable for testing, LTS and latest
node-version: [12, lts, latest]
os: [ubuntu-latest, windows-latest, macos-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Test
run: npm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
57 changes: 29 additions & 28 deletions KaitaiStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,12 @@ KaitaiStream.prototype.readS8be = function() {
var v2 = this.readU4be();

if ((v1 & 0x80000000) !== 0) {
var p1 = v1 ^ 0xffffffff;
var p2 = v2 ^ 0xffffffff;
var p3 = 0x100000000 * (p1 + 1) + p2;

// negative number
return -(0x100000000 * (v1 ^ 0xffffffff) + (v2 ^ 0xffffffff)) - 1;
return -p3 - 1;
} else {
return 0x100000000 * v1 + v2;
}
Expand Down Expand Up @@ -265,8 +269,12 @@ KaitaiStream.prototype.readS8le = function() {
var v2 = this.readU4le();

if ((v2 & 0x80000000) !== 0) {
var p1 = v1 ^ 0xffffffff;
var p2 = v2 ^ 0xffffffff;
var p3 = 0x100000000 * (p2 + 1) + p1;

// negative number
return -(0x100000000 * (v2 ^ 0xffffffff) + (v1 ^ 0xffffffff)) - 1;
return -p3 - 1;
} else {
return 0x100000000 * v2 + v1;
}
Expand Down Expand Up @@ -537,36 +545,28 @@ KaitaiStream.endianness = new Int8Array(new Int16Array([1]).buffer)[0] > 0;
// ========================================================================

KaitaiStream.prototype.readBytes = function(len) {
return this.mapUint8Array(len);
return this.mapUint8Array(len, len);
};

KaitaiStream.prototype.readBytesFull = function() {
return this.mapUint8Array(this.size - this.pos);
return this.readBytes(this.size - this.pos);
};

KaitaiStream.prototype.readBytesTerm = function(terminator, include, consume, eosError) {
var blen = this.size - this.pos;
var u8 = new Uint8Array(this._buffer, this._byteOffset + this.pos);
for (var i = 0; i < blen && u8[i] !== terminator; i++); // find first zero byte
if (i === blen) {
var len = this.size - this.pos;
var u8 = new Uint8Array(this._buffer, this._byteOffset + this.pos, len);
var i = u8.findIndex(function(value) { return value === terminator; });
if (i < 0) {
// we've read all the buffer and haven't found the terminator
if (eosError) {
throw "End of stream reached, but no terminator " + terminator + " found";
} else {
return this.mapUint8Array(i);
}
} else {
var arr;
if (include) {
arr = this.mapUint8Array(i + 1);
} else {
arr = this.mapUint8Array(i);
}
if (consume) {
this.pos += 1;
throw new Error("End of stream reached, but no terminator " + terminator + " found");
}
return arr;
return this.readBytes(len);
}
return this.mapUint8Array(
include ? i + 1 : i,
consume ? i + 1 : i
);
};

// Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions
Expand Down Expand Up @@ -622,8 +622,7 @@ KaitaiStream.bytesToStr = function(arr, encoding) {
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return new Buffer(arr).toString(encoding);
break;
return Buffer.from(arr).toString(encoding);
default:
// unsupported encoding, we'll have to resort to iconv-lite
if (typeof KaitaiStream.iconvlite === 'undefined')
Expand Down Expand Up @@ -663,7 +662,7 @@ KaitaiStream.processXorMany = function(data, key) {

KaitaiStream.processRotateLeft = function(data, amount, groupSize) {
if (groupSize !== 1)
throw("unable to rotate group of " + groupSize + " bytes yet");
throw new Error("unable to rotate group of " + groupSize + " bytes yet");

var mask = groupSize * 8 - 1;
var antiAmount = -amount & mask;
Expand Down Expand Up @@ -706,7 +705,7 @@ KaitaiStream.processZlib = function(buf) {

KaitaiStream.mod = function(a, b) {
if (b <= 0)
throw "mod divisor <= 0";
throw new Error("mod divisor <= 0");
var r = a % b;
if (r < 0)
r += b;
Expand Down Expand Up @@ -859,15 +858,17 @@ KaitaiStream.prototype.ensureBytesLeft = function(length) {
Nice for quickly reading in data.

@param {number} length Number of elements to map.
@param {number} [consume=length] Number of elements to consume (default to `length`).
@return {Object} Uint8Array to the KaitaiStream backing buffer.
*/
KaitaiStream.prototype.mapUint8Array = function(length) {
KaitaiStream.prototype.mapUint8Array = function(length, consume = length) {
length |= 0;
consume |= 0;

this.ensureBytesLeft(length);

var arr = new Uint8Array(this._buffer, this.byteOffset + this.pos, length);
this.pos += length;
this.pos += consume;
return arr;
};

Expand Down
Loading