piomar123

Mission003

May 12th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import itertools
  2.  
  3. enc = "10101011101000110100011101111111011111110111010010110010011100101111001011001001\
  4. 10111001101000111011111110100101111010011101111011001011001001110101011110110011\
  5. 11101011111011110111101111011101111111001011001001011001001110110011100101100100\
  6. 11010001110110011110110100101100100110111001110010111"
  7. trees = [ ( 0, (1, (2, 3)) ), ( 0, ((1,2), 3) )]
  8.  
  9. def huff_decode(tree, msg, flip_bit=lambda x:x ,perm=lambda x:x):
  10.     decoded = []
  11.     i = 0
  12.     while i < len(msg):
  13.         node = tree
  14.         while True:
  15.             bit_str = msg[i]
  16.             b = int(bit_str)
  17.             node = node[flip_bit(b)]
  18.             i += 1
  19.             if type(node) is tuple:
  20.                 if i >= len(msg):
  21.                     return (None, decoded)
  22.                 continue
  23.             else:
  24.                 decoded.append(perm(node))
  25.                 break
  26.     return decoded
  27.  
  28. def make_string(dec):
  29.     bit = 0
  30.     out = []
  31.     byte = 0
  32.     for q in reversed(dec):
  33.         if bit == 8:
  34.             bit = 0
  35.             out.append(byte)
  36.             byte = 0
  37.         byte |= q << bit
  38.         bit += 2
  39.     if byte != 0:
  40.         out.append(byte)
  41.     chars = []
  42.     legible = True
  43.     for i in out:
  44.         if 0x20 <= i <= 0x7F:
  45.             chars.append(chr(i))
  46.         else:
  47.             legible = False
  48.             chars.append('_')
  49.     return ("".join(chars), legible)
  50.  
  51.  
  52. fout = open("strs.txt", "w")
  53.  
  54. for tree_id, tree in enumerate(trees):
  55.     for flip_id, flip in enumerate([lambda x:x, lambda x:1-x]):
  56.         for perm_tuple, perm in ( (p, lambda x: p[x]) for p in itertools.permutations( range(4) ) ):
  57.             dec = huff_decode(tree, enc, flip, perm)
  58.             s, legible = make_string(dec)
  59.             fout.write("{} t{} f{} {}: {}\n".format(legible, tree_id, flip_id, perm_tuple, s))
  60.  
  61. fout.close()
Advertisement
Add Comment
Please, Sign In to add comment