Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Feb 13, 2025
1 parent dbbb3dc commit e6883cc
Showing 1 changed file with 65 additions and 49 deletions.
114 changes: 65 additions & 49 deletions src/seek-bzip/bitreader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
/*
node-bzip - a pure-javascript Node.JS module for decoding bzip2 data
Expand Down Expand Up @@ -37,61 +36,78 @@ import { toHex } from './toHex'

const BITMASK = [0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff]

// offset in bytes
const BitReader = function (stream) {
this.stream = stream
this.bitOffset = 0
this.curByte = 0
this.hasByte = false
interface Stream {
readByte(): number
seek(pos: number): void
}

BitReader.prototype._ensureByte = function () {
if (!this.hasByte) {
this.curByte = this.stream.readByte()
this.hasByte = true
export default class BitReader {
private stream: Stream
private bitOffset: number
private curByte: number
private hasByte: boolean

constructor(stream: Stream) {
this.stream = stream
this.bitOffset = 0
this.curByte = 0
this.hasByte = false
}
}

// reads bits from the buffer
BitReader.prototype.read = function (bits) {
let result = 0
while (bits > 0) {
this._ensureByte()
const remaining = 8 - this.bitOffset
// if we're in a byte
if (bits >= remaining) {
result <<= remaining
result |= BITMASK[remaining] & this.curByte
this.hasByte = false
this.bitOffset = 0
bits -= remaining
} else {
result <<= bits
const shift = remaining - bits
result |= (this.curByte & (BITMASK[bits] << shift)) >> shift
this.bitOffset += bits
bits = 0
private _ensureByte(): void {
if (!this.hasByte) {
this.curByte = this.stream.readByte()
this.hasByte = true
}
}
return result
}

// seek to an arbitrary point in the buffer (expressed in bits)
BitReader.prototype.seek = function (pos) {
const n_bit = pos % 8
const n_byte = (pos - n_bit) / 8
this.bitOffset = n_bit
this.stream.seek(n_byte)
this.hasByte = false
}
/**
* Reads bits from the buffer
* @param bits Number of bits to read
*/
read(bits: number): number {
let result = 0
while (bits > 0) {
this._ensureByte()
const remaining = 8 - this.bitOffset
// if we're in a byte
if (bits >= remaining) {
result <<= remaining
result |= BITMASK[remaining]! & this.curByte
this.hasByte = false
this.bitOffset = 0
bits -= remaining
} else {
result <<= bits
const shift = remaining - bits
result |= (this.curByte & (BITMASK[bits]! << shift)) >> shift
this.bitOffset += bits
bits = 0
}
}
return result
}

// reads 6 bytes worth of data using the read method
BitReader.prototype.pi = function () {
const buf = new Uint8Array(6)
for (let i = 0; i < buf.length; i++) {
buf[i] = this.read(8)
/**
* Seek to an arbitrary point in the buffer (expressed in bits)
* @param pos Position in bits
*/
seek(pos: number): void {
const n_bit = pos % 8
const n_byte = (pos - n_bit) / 8
this.bitOffset = n_bit
this.stream.seek(n_byte)
this.hasByte = false
}
return toHex(buf)
}

module.exports = BitReader
/**
* Reads 6 bytes worth of data using the read method
*/
pi(): string {
const buf = new Uint8Array(6)
for (let i = 0; i < buf.length; i++) {
buf[i] = this.read(8)
}
return toHex(buf)
}
}

0 comments on commit e6883cc

Please sign in to comment.