Replaced Checking Flag Bits against 0x00, and using a counter to count down us using the flag bits

This commit is contained in:
2025-09-21 12:56:19 -06:00
parent fb7442afd5
commit 707e5ef658

53
main.py
View File

@@ -17,8 +17,7 @@ class BZZCompressor:
data = temp.tobytes()
except IOError:
print("Could not open {input_file_path}/{input_file}...")
raise
raise IOError(f"Could not open {input_file_path}/{input_file}...")
##############################################################################
#
@@ -69,8 +68,6 @@ class BZZCompressor:
# File Loop
for file_num, file in enumerate(files):
# print(hex(starting_index))
index = starting_index
# Prepping for the next loop
@@ -128,18 +125,23 @@ class BZZCompressor:
# 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(data[index] + 0x100)[2:])
flag_bits = data[index] + 0x100
index = index + 1
# used to track how far in flag_bits we are
counter = 8
try:
while num_flags > 0:
carry = flag_bits[-1]
carry = bin(flag_bits)[-1]
flag_bits = flag_bits >> 1
counter = counter - 1
# 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 int(flag_bits.to01(), 2) == 0x00:
flag_bits = bitarray(bin(data[index] + 0x100)[2:])
if counter == 0:
flag_bits = data[index] + 0x100
index = index + 1
counter = 8
continue
# Carry means the next byte is raw data, no weird placement or indexing
@@ -148,20 +150,19 @@ class BZZCompressor:
output_buffer.append(data[index])
index = index + 1
except IndexError:
print(output_buffer)
print(
f"Error processing {input_file_path}/{input_file} on {file_num}/{len(files)}. Reached of data stream early. Index: {index}"
raise IndexError(
f"Error processing {input_file_path}/{input_file} on {file_num}/{len(files)} (Carry Path). Reached of data stream early. Index: {index}\n"
+ f"Index: {hex(index)}. File Start: {hex(starting_index - int(file.get("padding_end")[2:], 16))}. File End: {hex(file_end)}"
)
return
# If Carry is 0, then we are doing actual decompression. This is the tricky part
else:
# This shouldn't happen
if len(data) <= index + 1:
print(
"Error processing {input_file_path}/{input_file} on {file_num}/{len(files)}. Reached of data stream early."
if file_end <= index + 1:
raise IndexError(
f"Error processing {input_file_path}/{input_file} on {file_num}/{len(files)} (Non-Carry Path). Reached of data stream early.\n"
+ f"Index: {hex(copy_index)}. File Start: {hex(starting_index - int(file.get("padding_end")[2:], 16))}. File End: {hex(file_end)}"
)
return
# The "temp" in our documentation
distance_data = int.from_bytes(data[index : index + 2], "big")
@@ -177,11 +178,10 @@ class BZZCompressor:
# This shouldn't happen
if displacement <= 0:
print(
raise ValueError(
f"Error processing {input_file_path}/{input_file} on {file_num}/{len(files)}. Displacement was less than or equal to 0.\n"
+ f"Displacement: {displacement}. Distance Data: {distance_data}. Length Bits: {len_bits}"
)
return
# print(f"Output Buffer Size {len(output_buffer)}")
# print(f"Distance Data: {distance_data}")
@@ -196,17 +196,22 @@ class BZZCompressor:
# If start index is less than 0, we'll be checking something like output_buffer[-2]
# or smth, which will have an IndexOutOfBounds exception
if copy_index < 0:
print(
"Error decompressing {input_file_path}/{input_file} on {file_num}/{len(files)}. Start Index was out of range."
raise IndexError(
f"Error decompressing {input_file_path}/{input_file} on {file_num}/{len(files)}. Displacement Index was out of range.\n"
+ f"Displacement Index: {hex(copy_index)}. File Start: {hex(starting_index - int(file.get("padding_end")[2:], 16))}. File End: {hex(file_end)}"
)
return
for i in range(length):
output_buffer.append(output_buffer[copy_index + i])
num_flags = num_flags - 1
except Exception as e:
print(
f"\nError while processing {input_file_path}/{input_file}_{hex(file_num)[2:].zfill(3)}. \nError: {e}"
)
# This handoff is so I can change buffer logic without breaking write-out logic
# This handoff is so I can change buffer logic without breaking write-out logic.
# I need to
out_data = output_buffer
try:
@@ -219,7 +224,7 @@ class BZZCompressor:
# f"File {output_folder}/{input_file}_{str(file_num).zfill(3)}.file{file['type'][2:]} saved successfully!"
# )
except IOError as e:
print(
raise IOError(
f"Unable to write file for {input_file_path}/{input_file} on {file_num}/{len(files)}. Error: {e}"
)
@@ -256,6 +261,6 @@ if __name__ == "__main__":
compressor.decompress(dirpath, file, str(output_folder_path))
except Exception as e:
print(
f"Error while decompressing {output_folder_path}/{file}. Error: {e}"
f"\nError while decompressing {output_folder_path}/{file}. \nError: {e}"
)
continue