Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
b.align()
length = b.readbits(16)
if length & b.readbits(16):
raise Exception("stored block lengths do not match each other")
for i in range(length):
out.append(int2byte(b.readbits(8)))
elif blocktype == 1 or blocktype == 2: # Huffman
main_literals, main_distances = None, None
if blocktype == 1: # Static Huffman
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
if blocktype == 0:
b.align()
length = b.readbits(16)
if length & b.readbits(16):
raise Exception("stored block lengths do not match each other")
for i in range(length):
out.append(int2byte(b.readbits(8)))
elif blocktype == 1 or blocktype == 2: # Huffman
main_literals, main_distances = None, None
if blocktype == 1: # Static Huffman
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,
def __init__(self, lengths):
l = len(lengths)
z = list(zip(range(l), lengths)) + [(l, -1)]
HuffmanTable.__init__(self, z)
if (reversed and x.reverse_symbol == cached) or (not reversed and x.symbol == cached):
field.readbits(x.bits)
return x.code
raise Exception("unfound symbol, even after end of table @%r"
% field.tell())
for bits in range(self.min_bits, self.max_bits + 1):
r = self._find_symbol(bits, field.snoopbits(bits), self.table)
if 0 <= r:
field.readbits(bits)
return r
elif bits == self.max_bits:
raise "unfound symbol, even after max_bits"
class OrderedHuffmanTable(HuffmanTable):
def __init__(self, lengths):
l = len(lengths)
z = list(zip(range(l), lengths)) + [(l, -1)]
HuffmanTable.__init__(self, z)
def code_length_orders(i):
return (16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3,
13, 2, 14, 1, 15)[i]
def distance_base(i):
return (1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193,
12289, 16385, 24577)[i]