Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compute_tables(b, huffman_groups, symbols_in_use):
groups_lengths = []
for j in range(huffman_groups):
length = b.readbits(5)
lengths = []
for i in range(symbols_in_use):
if not 0 <= length <= 20:
raise "Bzip2 Huffman length code outside range 0..20"
while b.readbits(1):
length -= (b.readbits(1) * 2) - 1
lengths += [length]
groups_lengths += [lengths]
tables = []
for g in groups_lengths:
codes = OrderedHuffmanTable(g)
codes.populate_huffman_symbols()
codes.min_max_bits()
tables.append(codes)
return tables
static_huffman_bootstrap = [
(0, 8), (144, 9), (256, 7), (280, 8), (288, -1)]
static_huffman_lengths_bootstrap = [(0, 5), (32, -1)]
main_literals = HuffmanTable(static_huffman_bootstrap)
main_distances = HuffmanTable(static_huffman_lengths_bootstrap)
elif blocktype == 2: # Dynamic Huffman
literals = b.readbits(5) + 257
distances = b.readbits(5) + 1
code_lengths_length = b.readbits(4) + 4
l = [0] * 19
for i in range(code_lengths_length):
l[code_length_orders(i)] = b.readbits(3)
dynamic_codes = OrderedHuffmanTable(l)
dynamic_codes.populate_huffman_symbols()
dynamic_codes.min_max_bits()
# Decode the code_lengths for both tables at once,
# then split the list later
code_lengths = []
n = 0
while n < (literals + distances):
r = dynamic_codes.find_next_symbol(b)
if 0 <= r <= 15: # literal bitlength for this code
count = 1
what = r
elif r == 16: # repeat last code
count = 3 + b.readbits(2)
# Is this supposed to default to '0' if in the zeroth
# position?
what = code_lengths[-1]
elif r == 17: # repeat zero
count = 3 + b.readbits(3)
what = 0
elif r == 18: # repeat zero lots
count = 11 + b.readbits(7)
what = 0
else:
raise Exception(
"next code length is outside of the range 0 <= r <= 18")
code_lengths += [what] * count
n += count
main_literals = OrderedHuffmanTable(code_lengths[:literals])
main_distances = OrderedHuffmanTable(code_lengths[literals:])
# Common path for both Static and Dynamic Huffman decode now
main_literals.populate_huffman_symbols()
main_distances.populate_huffman_symbols()
main_literals.min_max_bits()
main_distances.min_max_bits()
literal_count = 0
while True:
r = main_literals.find_next_symbol(b)
if 0 <= r <= 255:
literal_count += 1
out.append(int2byte(r))
elif r == 256:
# Is this supposed to default to '0' if in the zeroth
# position?
what = code_lengths[-1]
elif r == 17: # repeat zero
count = 3 + b.readbits(3)
what = 0
elif r == 18: # repeat zero lots
count = 11 + b.readbits(7)
what = 0
else:
raise Exception(
"next code length is outside of the range 0 <= r <= 18")
code_lengths += [what] * count
n += count
main_literals = OrderedHuffmanTable(code_lengths[:literals])
main_distances = OrderedHuffmanTable(code_lengths[literals:])
# Common path for both Static and Dynamic Huffman decode now
main_literals.populate_huffman_symbols()
main_distances.populate_huffman_symbols()
main_literals.min_max_bits()
main_distances.min_max_bits()
literal_count = 0
while True:
r = main_literals.find_next_symbol(b)
if 0 <= r <= 255:
literal_count += 1
out.append(int2byte(r))