Skip to content

Commit

Permalink
fix(tbinaryreader): fix skipBitOffsetOf and skipBitOffsetTo in case t…
Browse files Browse the repository at this point in the history
…hey are first statement
  • Loading branch information
Itee committed Mar 3, 2022
1 parent 9134c0c commit 74542e4
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions sources/loaders/TBinaryReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class TBinaryReader {
// For bit reading use same approche than byte
this._bits = {
buffer: null,
offset: null,
length: null
offset: 0,
length: 0
}

this._updateDataView()
Expand Down Expand Up @@ -283,6 +283,21 @@ class TBinaryReader {
_isOutOfRangeBitOffset( offset ) {
return offset > this._bits.length
}
_readBit8() {
this._bits.buffer = this.getUint8()
this._bits.length = 8
this._bits.offset = 0
}
_readBit16() {
this._bits.buffer = this.getUint16()
this._bits.length = 16
this._bits.offset = 0
}
_readBit32() {
this._bits.buffer = this.getUint32()
this._bits.length = 32
this._bits.offset = 0
}
_getBitAt ( bitOffset ) {

return ( this._bits.buffer & ( 1 << bitOffset ) ) === 0 ? 0 : 1
Expand All @@ -295,8 +310,31 @@ class TBinaryReader {
}

skipBitOffsetTo ( bitOffset ) {
//todo is positive bitoffset

// In case we start directly by a skip offset try to determine which kind of data is expected
if ( this._isNullBitBuffer() ) {

if (bitOffset <= 8) {

this._readBit8()

} else if (8 < bitOffset && bitOffset <= 16 ){

if ( this._isOutOfRangeBitOffset(bitOffset) ) { throw new RangeError( 'Bit offset is out of range of the current bits field.' ) }
this._readBit16()

} else if (16 < bitOffset && bitOffset <= 32 ){

this._readBit32()

} else {

throw new RangeError( 'You cannot skip more than 32 bits. Please use skipOffsetOf instead !' )

}

}
else if ( this._isOutOfRangeBitOffset(bitOffset) ) { throw new RangeError( 'Bit offset is out of range of the current bits field.' ) }

this._bits.offset = bitOffset
if(this._isEndOfBitBuffer()) {
Expand All @@ -314,9 +352,7 @@ class TBinaryReader {
getBit8 ( moveNext = true ) {

if ( this._isNullBitBuffer() ) {
this._bits.buffer = this.getUint8()
this._bits.length = 8
this._bits.offset = 0
this._readBit8()
}

const bitValue = this._getBitAt( this._bits.offset )
Expand Down Expand Up @@ -358,10 +394,8 @@ class TBinaryReader {

getBit16 ( moveNext = true ) {

if ( this._isNullBitBuffer() || this._isEndOfBitBuffer() ) {
this._bits.buffer = this.getUint16()
this._bits.length = 16
this._bits.offset = 0
if ( this._isNullBitBuffer() ) {
this._readBit16()
}

const bitValue = this._getBitAt( this._bits.offset )
Expand Down Expand Up @@ -403,10 +437,8 @@ class TBinaryReader {

getBit32 ( moveNext = true ) {

if ( this._isNullBitBuffer() || this._isEndOfBitBuffer() ) {
this._bits.buffer = this.getUint32()
this._bits.length = 32
this._bits.offset = 0
if ( this._isNullBitBuffer() ) {
this._readBit32()
}

const bitValue = this._getBitAt( this._bits.offset )
Expand Down

0 comments on commit 74542e4

Please sign in to comment.