All files / src binary.js

96.87% Statements 93/96
83.33% Branches 30/36
100% Functions 26/26
98.7% Lines 76/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176  314964x       17x 3072013x 17x       92x 11x 11x 205569x 11x       30x       37x     65x 65x 65x     2x 2x 2x 2x 2x         60x 60x 60x 60x     60x       225x       57x       162x 161x 161x       2x 2x       27x 27x       35x 35x       1284x 1284x       22x 22x 22x   22x 1163x   22x 21x 21x         28x 27x 27x   1x 1x   28x       25x 24x                 1083x         267x 267x 267x 267x         62x         65x         48x       387x 387x 387x         65x 64x 64x 64x         48x 48x 48x 48x 48x         51x 51x 51x      
export function signExtend(val) {
    return val >= 128 ? val - 256 : val;
}
 
export function uint8ArrayToString(array) {
    let str = "";
    for (let i = 0; i < array.length; ++i) str += String.fromCharCode(array[i]);
    return str;
}
 
export function stringToUint8Array(str) {
    if (str instanceof Uint8Array) return str;
    const len = str.length;
    const array = new Uint8Array(len);
    for (let i = 0; i < len; ++i) array[i] = str.charCodeAt(i) & 0xff;
    return array;
}
 
export function readInt32(data, offset) {
    return (data[offset + 3] << 24) | (data[offset + 2] << 16) | (data[offset + 1] << 8) | data[offset + 0];
}
 
export function readInt16(data, offset) {
    return (data[offset + 1] << 8) | data[offset + 0];
}
 
const tempBuf = new ArrayBuffer(4);
const tempBuf8 = new Uint8Array(tempBuf);
const tempBufF32 = new Float32Array(tempBuf);
 
export function readFloat32(data, offset) {
    tempBuf8[0] = data[offset];
    tempBuf8[1] = data[offset + 1];
    tempBuf8[2] = data[offset + 2];
    tempBuf8[3] = data[offset + 3];
    return tempBufF32[0];
}
 
export class DataStream {
    constructor(name, data) {
        this.name = name;
        this.pos = 0;
        this.data = stringToUint8Array(data);
        Iif (!this.data) {
            throw new Error("No data in " + name);
        }
        this.end = this.data.length;
    }
 
    bytesLeft() {
        return this.end - this.pos;
    }
 
    eof() {
        return this.bytesLeft() === 0;
    }
 
    advance(distance) {
        if (this.bytesLeft() < distance) throw new RangeError("EOF in " + this.name);
        this.pos += distance;
        return this.pos - distance;
    }
 
    readFloat32(pos) {
        Eif (pos === undefined) pos = this.advance(4);
        return readFloat32(this.data, pos);
    }
 
    readInt32(pos) {
        Eif (pos === undefined) pos = this.advance(4);
        return readInt32(this.data, pos);
    }
 
    readInt16(pos) {
        if (pos === undefined) pos = this.advance(2);
        return readInt16(this.data, pos);
    }
 
    readByte(pos) {
        if (pos === undefined) pos = this.advance(1);
        return this.data[pos];
    }
 
    readNulString(pos, maxLength) {
        Eif (!maxLength) maxLength = 1024;
        let posToUse = pos === undefined ? this.pos : pos;
        let result = "";
        let c;
        while ((c = this.readByte(posToUse++)) !== 0 && --maxLength) {
            result += String.fromCharCode(c);
        }
        if (maxLength === 0) return "";
        if (pos === undefined) this.pos = posToUse;
        return result;
    }
 
    substream(posOrLength, length) {
        let pos;
        if (length === undefined) {
            length = posOrLength;
            pos = this.advance(length);
        } else {
            pos = posOrLength;
            Iif (pos + length >= this.end) throw new RangeError("EOF in " + this.name);
        }
        return new DataStream(this.name + ".sub", this.data.subarray(pos, pos + length));
    }
 
    seek(to) {
        if (to >= this.end) throw new RangeError("Seek out of range in " + this.name);
        this.pos = to;
    }
}
 
export function makeFast32(u32) {
    // Firefox is ~5% faster with signed 32-bit arrays. Chrome is the same speed
    // either way, so here we unconditionally wrap all u32 buffers as i32.
    // Having a function do this makes it easy to test u32 vs i32, and means we
    // keep the rest of the code using u32 (which makes more sense to me).
    return new Int32Array(u32.buffer);
}
 
export class Fifo {
    constructor(capacity) {
        this._buffer = new Uint8Array(capacity);
        this._size = 0;
        this._wPtr = 0;
        this._rPtr = 0;
    }
 
    /** @returns {number} */
    get size() {
        return this._size;
    }
 
    /** @returns {boolean} */
    get full() {
        return this._size === this._buffer.length;
    }
 
    /** @returns {boolean} */
    get empty() {
        return this._size === 0;
    }
 
    clear() {
        this._size = 0;
        this._wPtr = 0;
        this._rPtr = 0;
    }
 
    /** @type {Number} b */
    put(b) {
        if (this.full) return;
        this._buffer[this._wPtr % this._buffer.length] = b;
        this._wPtr++;
        this._size++;
    }
 
    /** @returns {Number} */
    get() {
        Iif (this.empty) return;
        const res = this._buffer[this._rPtr % this._buffer.length];
        this._rPtr++;
        this._size--;
        return res;
    }
 
    /** @returns {number[]} pending bytes, oldest first */
    toArray() {
        const result = [];
        for (let i = 0; i < this._size; ++i) result.push(this._buffer[(this._rPtr + i) % this._buffer.length]);
        return result;
    }
}