1
0
mirror of synced 2024-11-17 11:37:13 +01:00

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

View File

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