示例#1
0
文件: encoder.go 项目: Jxck/hpack
func (frame *StringLiteral) EncodeHuffman() (buf *swrap.SWrap) {
	buf = new(swrap.SWrap)
	switch frame.Indexing {
	case WITH:
		buf.Add(0x40) // 0100 0000
	case WITHOUT:
		buf.Add(0) // 0000 0000
	case NEVER:
		buf.Add(0x10) // 0001 0000
	}

	var encoded, length []byte

	// Name With Huffman
	encoded = huffman.Encode([]byte(frame.NameString))
	length = integer.Encode(uint32(len(encoded)), 7)
	length[0] += 0x80 // 1000 0000 (huffman flag)
	buf.Merge(length)
	buf.Merge(encoded)

	// Value With Huffman
	encoded = huffman.Encode([]byte(frame.ValueString))
	length = integer.Encode(uint32(len(encoded)), 7)
	length[0] += 0x80 // 1000 0000 (huffman flag)
	buf.Merge(length)
	buf.Merge(encoded)

	Trace("huffman encoded %s %v", frame, *buf)
	return buf
}
示例#2
0
文件: encoder.go 项目: Jxck/hpack
func (frame *IndexedLiteral) EncodeHuffman() (buf *swrap.SWrap) {
	switch frame.Indexing {
	case WITH:
		buf = swrap.Make(integer.Encode(frame.Index, 6))
		(*buf)[0] += 0x20
	case WITHOUT:
		buf = swrap.Make(integer.Encode(frame.Index, 4))
	case NEVER:
		buf = swrap.Make(integer.Encode(frame.Index, 4))
		(*buf)[0] += 0x8
	}

	var encoded, length []byte

	// Value With Huffman
	encoded = huffman.Encode([]byte(frame.ValueString))
	length = integer.Encode(uint32(len(encoded)), 7)
	length[0] += 0x80 // 1000 0000 (huffman flag)
	buf.Merge(length)
	buf.Merge(encoded)

	return buf
}