def decode(self, in_stream, out_stream): bs = BitStream() dq = deque() at_least_three = False for word in self.words_from_file(in_stream): if not word or word not in self.word_dict: continue #print >> sys.stderr, 'word:"', word, '"' dq.append(self.word_dict[word]) if at_least_three or len(dq) == 3: bs.append(pack(self.int_type, dq.popleft())) at_least_three = True if bs.len > self.bit_buffer: cut = 0 for byte in bs.cut(self.bit_buffer): cut += 1 byte.tofile(out_stream) del bs[:cut * self.bit_buffer] # dq has to have exactly 2 elements here, the last is the bit length of the first, unless it's 0 #print >> sys.stderr, 'dq:', dq extra_bits = dq.pop() bs.append(pack('uint:' + str(extra_bits), dq.popleft())) bs.tofile(out_stream)
def find_mapping(infile, outfile): stream = BitStream() with open(infile) as f: byte = f.read(1) while byte: if byte == '\x01': stream.append('0b1') else: stream.append('0b0') byte = f.read(1) iteration = 0 for mapping in bitmappings: stream.pos = 0 tmpstream = BitStream() for pie in stream.cut(2): index = pie.read('uint:2') tmpstream.append( BitArray(uint=mapping[index], length=2) ) # look for the start of the flag in the stream of bits pos = tmpstream.find(ZULU) if len(pos) > 0: # print("Found the bytes we are looking for on iteration {}".format(iteration)) # print("pos = {}".format(pos)) tmpstream <<= pos[0] % 8 data = tmpstream.tobytes() # print(data) with open(outfile, 'wb') as fh: fh.write(data) break else: # print("Did not find the header") iteration += 1