Exemplo n.º 1
0
    def SerializeInstructions(self, ops, packing_instructions, huff, stream_id,
                              group_id, end_of_frame, verbose):
        """ Serializes a set of instructions possibly containing many different
    type of opcodes into SPDY4 wire format, discovers the resultant length,
    computes the appropriate SPDY4 boilerplate, and then returns this
    in a new BitBucket
    """
        #print 'SerializeInstructions\n', ops
        if verbose >= 5:
            print
            print "stream_id: %s group_id: %s" % (stream_id, group_id)
            print "Ops to be serialied:"
            print FormatOps(ops)
            print
            descr = deque()

        payload_bb = BitBucket()
        for opcode, oplist in ops.iteritems():
            self.OutputOps(packing_instructions, huff, payload_bb, oplist,
                           opcode)
            #if verbose >= 5:
            #  tmp_bb = BitBucket()
            #  self.OutputOps(packing_instructions, huff, tmp_bb, oplist, opcode)
            #  descr.append(FormatHex(tmp_bb.GetAllBits()[0]))
            #  descr.append("\n")

        (payload, payload_len) = payload_bb.GetAllBits()
        payload_len = (payload_len +
                       7) / 8  # partial bytes are counted as full
        frame_bb = BitBucket()
        self.WriteControlFrameBoilerplate(frame_bb, 0, 0, 0, group_id, 0)
        boilerplate_length = frame_bb.BytesOfStorage()
        frame_bb = BitBucket()
        overall_bb = BitBucket()
        bytes_allowed = 2**16 - boilerplate_length
        while True:
            #print 'payload_len: ', payload_len
            bytes_to_consume = min(payload_len, bytes_allowed)
            #print 'bytes_to_consume: ', bytes_to_consume
            end_of_frame = (bytes_to_consume <= payload_len)
            #print 'end_of_Frame: ', end_of_frame
            self.WriteControlFrameBoilerplate(overall_bb, bytes_to_consume,
                                              end_of_frame, stream_id,
                                              group_id, 0x8)
            if verbose >= 5:
                descr.appendleft(FormatHex(overall_bb.GetAllBits()[0]))
            overall_bb.StoreBits((payload, bytes_to_consume * 8))
            payload = payload[bytes_to_consume:]
            payload_len -= bytes_allowed
            if payload_len <= 0:
                break
        if verbose >= 5:
            PrintHex(overall_bb.GetAllBits()[0])
            print ''.join(descr)

        return overall_bb.GetAllBits()
Exemplo n.º 2
0
 def OpsToRealOps(self, in_ops):
     data = [BitBucket(), BitBucket(), BitBucket()]
     PackOps(data, alt_packing_instructions, in_ops, self.huffman_table)
     overall = BitBucket()
     overall.StoreBits(data[0].GetAllBits())
     overall.StoreBits(data[1].GetAllBits())
     overall.StoreBits(data[2].GetAllBits())
     return ListToStr(overall.GetAllBits()[0])
Exemplo n.º 3
0
    def SerializeInstructions(self, ops, packing_instructions, huff, stream_id,
                              group_id, end_of_frame):
        """ Serializes a set of instructions possibly containing many different
    type of opcodes into SPDY4 wire format, discovers the resultant length,
    computes the appropriate SPDY4 boilerplate, and then returns this
    in a new BitBucket
    """
        #print 'SerializeInstructions\n', ops
        (ot, otr) = self.MutateTogglesToToggleRanges(ops)
        #print FormatOps({'toggl': ot, 'trang': otr, 'clone': ops['clone'],
        #                 'kvsto': ops['kvsto'], 'eref': ops['eref']})

        payload_bb = BitBucket()
        self.OutputOps(packing_instructions, huff, payload_bb, ot, 'toggl')
        self.OutputOps(packing_instructions, huff, payload_bb, otr, 'trang')
        self.OutputOps(packing_instructions, huff, payload_bb, ops['clone'],
                       'clone')
        self.OutputOps(packing_instructions, huff, payload_bb, ops['kvsto'],
                       'kvsto')
        self.OutputOps(packing_instructions, huff, payload_bb, ops['eref'],
                       'eref')

        (payload, payload_len) = payload_bb.GetAllBits()
        payload_len = (payload_len +
                       7) / 8  # partial bytes are counted as full
        frame_bb = BitBucket()
        self.WriteControlFrameBoilerplate(frame_bb, 0, 0, 0, group_id, 0)
        boilerplate_length = frame_bb.BytesOfStorage()
        frame_bb = BitBucket()
        overall_bb = BitBucket()
        bytes_allowed = 2**16 - boilerplate_length
        while True:
            #print 'payload_len: ', payload_len
            bytes_to_consume = min(payload_len, bytes_allowed)
            #print 'bytes_to_consume: ', bytes_to_consume
            end_of_frame = (bytes_to_consume <= payload_len)
            #print 'end_of_Frame: ', end_of_frame
            self.WriteControlFrameBoilerplate(overall_bb, bytes_to_consume,
                                              end_of_frame, stream_id,
                                              group_id, 0x8)
            overall_bb.StoreBits((payload, bytes_to_consume * 8))
            payload = payload[bytes_to_consume:]
            payload_len -= bytes_allowed
            if payload_len <= 0:
                break
        return overall_bb.GetAllBits()
Exemplo n.º 4
0
 def Encode(self, text, include_eof):
     """
 Encodes 'text' using the pre-computed huffman coding, and returns it as
 a tuple of (list-of-bytes, number-of-bits-as-int). If 'include_eof' is true,
 then an EOF will be encoded at the end.
 """
     bb = BitBucket()
     self.EncodeToBB(bb, text, include_eof)
     return bb.GetAllBits()
Exemplo n.º 5
0
    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])
Exemplo n.º 6
0
def main():
  h = Huffman(request_freq_table)
  for s in test_data:
    print " encoding: ", s
    sp = [ord(c) for c in s]
    e_result = BitBucket()
    h.EncodeToBB(e_result, sp, True)
    print "      e_result: ", FormatAsBits(e_result.GetAllBits())

    d_result = ListToStr(h.DecodeFromBB(e_result, True, -1))
    if d_result != s:
      print "difference found: d_result(%s) vs orig(%s)" % (repr(d_result),
                                                            repr(s))
    else:
      print "It worked: ", s
    print
Exemplo n.º 7
0
 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]))
Exemplo n.º 8
0
 def OpsToRealOps(self, in_ops):
   """ Packs in-memory format operations into wire format"""
   data = BitBucket()
   PackOps(data, packing_instructions, in_ops, self.huffman_table)
   return ListToStr(data.GetAllBits()[0])
Exemplo n.º 9
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])
Exemplo n.º 10
0
 def Encode(self, text, include_eof):
     bb = BitBucket()
     self.EncodeToBB(bb, text, include_eof)
     return bb.GetAllBits()