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