Stream.readBits() method implemented. Unfinished.

This commit is contained in:
n1474335 2019-01-10 17:30:52 +00:00
parent 2a6db47aeb
commit c077b22410
2 changed files with 8 additions and 11 deletions

View File

@ -1438,10 +1438,8 @@ export function extractGZIP(bytes, offset) {
while (!finalBlock) {
// Read header
const blockHeader = stream.readBits(3);
finalBlock = blockHeader & 0x1;
const blockType = blockHeader & 0x6;
finalBlock = stream.readBits(1);
const blockType = stream.readBits(2);
if (blockType === 0) {
// No compression
@ -1451,23 +1449,23 @@ export function extractGZIP(bytes, offset) {
stream.moveForwardsBy(2 + blockLength);
} else if (blockType === 1) {
// Fixed Huffman
console.log("Fixed Huffman");
} else if (blockType === 2) {
// Dynamic Huffman
console.log("Dynamic Huffman");
} else {
throw new Error("Invalid block type");
break;
}
}
/* FOOTER */
// Skip over checksum and size of original uncompressed input
stream.moveForwardsBy(8);
console.log(stream.position);
console.log("Ending at " + stream.position);
return stream.carve();
}

View File

@ -106,7 +106,7 @@ export default class Stream {
bitBufLen = 0;
// Add remaining bits from current byte
bitBuf = this.bytes[this.position++] & bitMask(this.bitPos);
bitBuf = (this.bytes[this.position++] & bitMask(this.bitPos)) >>> this.bitPos;
bitBufLen = 8 - this.bitPos;
this.bitPos = 0;
@ -119,7 +119,7 @@ export default class Stream {
// Reverse back to numBits
if (bitBufLen > numBits) {
const excess = bitBufLen - numBits;
bitBuf >>>= excess;
bitBuf &= (1 << numBits) - 1;
bitBufLen -= excess;
this.position--;
this.bitPos = 8 - excess;
@ -134,11 +134,10 @@ export default class Stream {
* @returns {number} The bit mask
*/
function bitMask(bitPos) {
return (1 << (8 - bitPos)) - 1;
return 256 - (1 << bitPos);
}
}
/**
* Consume the stream until we reach the specified byte or sequence of bytes.
*