donmccurdy / KTX-Parse

KTX 2.0 (.ktx2) parser and serializer.
MIT License
48 stars 7 forks source link

An error occurs if the Value is a Uint8Array and starts with 0, when reading Key/Value data #78

Closed gz65555 closed 1 year ago

gz65555 commented 1 year ago

image

Reproduction code:

...
container.keyValue["info"] = new Uint8Array([0, 0, 0, 16]);
const buffer = write(container);
const newContainer = read(buffer);

The function _scan rely on NUL termination which is not correct:

_scan(maxByteLength: number, term = 0x00): Uint8Array {
    const byteOffset = this._offset;
    let byteLength = 0;
    while (this._dataView.getUint8(this._offset) !== term && byteLength < maxByteLength) {
        byteLength++;
        this._offset++;
    }

    if (byteLength < maxByteLength) this._offset++;

    return new Uint8Array(this._dataView.buffer, this._dataView.byteOffset + byteOffset, byteLength);
}
donmccurdy commented 1 year ago

Thanks for reporting the bug! Would you be open to making a PR with a fix to support these values? And/or a failing unit test would also be helpful.