Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- enc = "10101011101000110100011101111111011111110111010010110010011100101111001011001001\
- 10111001101000111011111110100101111010011101111011001011001001110101011110110011\
- 11101011111011110111101111011101111111001011001001011001001110110011100101100100\
- 11010001110110011110110100101100100110111001110010111"
- trees = [ ( 0, (1, (2, 3)) ), ( 0, ((1,2), 3) )]
- def huff_decode(tree, msg, flip_bit=lambda x:x ,perm=lambda x:x):
- decoded = []
- i = 0
- while i < len(msg):
- node = tree
- while True:
- bit_str = msg[i]
- b = int(bit_str)
- node = node[flip_bit(b)]
- i += 1
- if type(node) is tuple:
- if i >= len(msg):
- return (None, decoded)
- continue
- else:
- decoded.append(perm(node))
- break
- return decoded
- def make_string(dec):
- bit = 0
- out = []
- byte = 0
- for q in reversed(dec):
- if bit == 8:
- bit = 0
- out.append(byte)
- byte = 0
- byte |= q << bit
- bit += 2
- if byte != 0:
- out.append(byte)
- chars = []
- legible = True
- for i in out:
- if 0x20 <= i <= 0x7F:
- chars.append(chr(i))
- else:
- legible = False
- chars.append('_')
- return ("".join(chars), legible)
- fout = open("strs.txt", "w")
- for tree_id, tree in enumerate(trees):
- for flip_id, flip in enumerate([lambda x:x, lambda x:1-x]):
- for perm_tuple, perm in ( (p, lambda x: p[x]) for p in itertools.permutations( range(4) ) ):
- dec = huff_decode(tree, enc, flip, perm)
- s, legible = make_string(dec)
- fout.write("{} t{} f{} {}: {}\n".format(legible, tree_id, flip_id, perm_tuple, s))
- fout.close()
Advertisement
Add Comment
Please, Sign In to add comment