def OpsToRealOps(self, in_ops, header_group): """ Packs in-memory format operations into wire format""" data = BitBucket() seder = Spdy4SeDer() data.StoreBits( seder.SerializeInstructions(in_ops, self.packing_instructions, self.huffman, 0xffff, header_group, True, self.options.verbose)) return common_utils.ListToStr(data.GetAllBits()[0])
def UnpackStr(input_data, params, huff): """ Reads a string from an input BitBucket and returns it. 'input_data' is a BitBucket containing the data to be interpreted as a string. 'params' is (bitlen_size, use_eof, pad_to_byte_boundary, use_huffman) 'bitlen_size' indicates the size of the length field. A size of 0 is valid IFF 'use_eof' is true. 'use_eof' indicates that an EOF character will be used (for ascii strings, this will be a null. For huffman-encoded strings, this will be the specific to that huffman encoding). If 'pad_to_byte_boundary' is true, then the 'bitlen_size' parameter represents bits of size, else 'bitlen_size' represents bytes. if 'use_huffman' is false, then the string is not huffman-encoded. If 'huff' is None, then the string is not huffman-encoded. If 'huff' is not None, then it must be a Huffman compatible object which is used to do huffman decoding. """ (bitlen_size, use_eof, pad_to_byte_boundary, use_huffman) = params if not use_huffman: huff = None if not use_eof and not bitlen_size: # without either a bitlen size or an EOF, we can't know when the string ends # having both is certainly fine, however. raise StandardError() if bitlen_size: bitlen = UnpackInt(input_data, bitlen_size, huff) if huff: retval = huff.DecodeFromBB(input_data, use_eof, bitlen) else: retval = input_data.GetBits(bitlen)[0] else: # bitlen_size == 0 if huff: retval = huff.DecodeFromBB(input_data, use_eof, 0) else: retval = [] while True: c = input_data.GetBits8() retval.append(c) if c == 0: break if pad_to_byte_boundary: input_data.AdvanceToByteBoundary() retval = common_utils.ListToStr(retval) return retval
def UnpackStr(input_data, params, huff): """ Reads a string from an input BitBucket and returns it. 'input_data' is a BitBucket containing the data to be interpreted as a string. 'params' is (bitlen_size, use_eof, pad_to_byte_boundary, use_huffman) 'bitlen_size' indicates the size of the length field. A size of 0 is valid IFF 'use_eof' is true. 'use_eof' indicates that an EOF character will be used (for ascii strings, this will be a null. For huffman-encoded strings, this will be the specific to that huffman encoding). If 'pad_to_byte_boundary' is true, then the 'bitlen_size' parameter represents bits of size, else 'bitlen_size' represents bytes. if 'use_huffman' is false, then the string is not huffman-encoded. If 'huff' is None, then the string is not huffman-encoded. If 'huff' is not None, then it must be a Huffman compatible object which is used to do huffman decoding. """ (pad_to_byte_boundary, use_huffman) = params if not use_huffman: huff = None if huff: retval = huff.DecodeFromBB(input_data, True, 0) else: retval = [] while True: c = input_data.GetBits8() if c == 0: break retval.append(c) if pad_to_byte_boundary: input_data.AdvanceReadPtrToByteBoundary() return common_utils.ListToStr(retval)
def compress(self, inp_headers, host): data = BitBucket() res = '' for k, v in inp_headers.items(): if k in bohe.ID_TABLE: zz = data.NumBits() # encode as registered header data.StoreBits8(bohe.ID_TABLE.index(k) + 1) l = 0 dohuff = True # Set the binary flag if k in bohe.ENCODERS: data.StoreBit(1) dohuff = False # Set the multiple values flag... if '\u00' in v: data.StoreBit(1) else: data.StoreBit(0) val = bohe.encode(k, v) if dohuff: val_as_list, len_in_bits = self.do_huff(self.huff, val) else: val_as_list = common_utils.StrToList(val) len_in_bits = len(val_as_list) * 8 data.StoreBits22(len(val_as_list)) data.StoreBits((val_as_list, len_in_bits)) else: data.StoreBits8(128 | len(k)) data.StoreBits((common_utils.StrToList(k), len(k) * 8)) data.StoreBit(0) # assume not binary value for now if '\u00' in v: data.StoreBit(1) else: data.StoreBit(0) val_as_list, len_in_bits = self.do_huff(self.huff, v) data.StoreBits22(len(val_as_list)) data.StoreBits((val_as_list, len_in_bits)) return ''.join(common_utils.ListToStr(data.GetAllBits()[0]))
def OpsToRealOps(self, in_ops, header_group): """ Packs in-memory format operations into wire format""" data = BitBucket() PackOps(data, self.packing_instructions, in_ops, self.huffman, header_group, self.options.verbose) return common_utils.ListToStr(data.GetAllBits()[0])