Added comments for explanation
This commit is contained in:
17
main.py
17
main.py
@@ -59,15 +59,15 @@ class BZZCompressor:
|
|||||||
print(f"Len Table: {len_table}")
|
print(f"Len Table: {len_table}")
|
||||||
print(f"Num Flags: {num_flags.tobytes()}")
|
print(f"Num Flags: {num_flags.tobytes()}")
|
||||||
|
|
||||||
|
# Adding 0x100 here means the bitarray is a length of 9, and the first item is always 1
|
||||||
|
# This means that later, when we need to gather more flag bits, we aren't losing any data, or
|
||||||
|
# hitting an index out of bounds error
|
||||||
flag_bits = bitarray(bin(int(data[0:8].to01(), 2) + 0x100)[2:])
|
flag_bits = bitarray(bin(int(data[0:8].to01(), 2) + 0x100)[2:])
|
||||||
del data[0:8]
|
del data[0:8]
|
||||||
|
|
||||||
print(f"Starting flag_bits: {flag_bits.tobytes()}")
|
print(f"Starting flag_bits: {flag_bits}")
|
||||||
hold_val = b""
|
|
||||||
|
|
||||||
while int(num_flags.to01(), 2) > 0:
|
while int(num_flags.to01(), 2) > 0:
|
||||||
# Any time we reset the flag_bits, it's length is 9 digits. So we shift right and trim.
|
|
||||||
# Otherwise, we just shift right
|
|
||||||
carry = flag_bits[-1]
|
carry = flag_bits[-1]
|
||||||
flag_bits = flag_bits >> 1
|
flag_bits = flag_bits >> 1
|
||||||
|
|
||||||
@@ -77,11 +77,14 @@ class BZZCompressor:
|
|||||||
print(f"Carry: {carry}")
|
print(f"Carry: {carry}")
|
||||||
print(f"Flag Bits: {flag_bits}")
|
print(f"Flag Bits: {flag_bits}")
|
||||||
|
|
||||||
if int(flag_bits.to01(), 2) == 0:
|
# if we are down to only 0 bits, we are out of file-driven data
|
||||||
|
# Here we collect more flag bits and re-iterate the loop
|
||||||
|
if len(flag_bits.to01()) == 0:
|
||||||
flag_bits = bitarray(bin(int(data[0:8].to01(), 2) + 0x100)[2:])
|
flag_bits = bitarray(bin(int(data[0:8].to01(), 2) + 0x100)[2:])
|
||||||
del data[0:8]
|
del data[0:8]
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Carry means the next byte is raw data, no weird placement or indexing
|
||||||
if carry:
|
if carry:
|
||||||
if len(data) == 0:
|
if len(data) == 0:
|
||||||
print("Error processing file. Reached of data stream early.")
|
print("Error processing file. Reached of data stream early.")
|
||||||
@@ -89,11 +92,15 @@ class BZZCompressor:
|
|||||||
|
|
||||||
output_buffer.append(data[0:8])
|
output_buffer.append(data[0:8])
|
||||||
del data[0:8]
|
del data[0:8]
|
||||||
|
|
||||||
|
# If Carry is 0, then we are doing actual decompression. This is the tricky part
|
||||||
else:
|
else:
|
||||||
|
# This shouldn't happen
|
||||||
if len(data) <= 8:
|
if len(data) <= 8:
|
||||||
print("Error processing file. Reached of data stream early.")
|
print("Error processing file. Reached of data stream early.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# This is "temp" in our documentation
|
||||||
distance_data = data[0:16]
|
distance_data = data[0:16]
|
||||||
del data[0:16]
|
del data[0:16]
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user