// Decode Wire byte seq to Slice of Frames func Decode(wire []byte) (frames []Frame) { buf := swrap.Make(wire) for buf.Len() > 0 { frames = append(frames, DecodeHeader(buf)) } return frames }
func (frame *IndexedHeader) Encode() (buf *swrap.SWrap) { buf = swrap.Make(integer.Encode(frame.Index, 7)) (*buf)[0] += 0x80 if frame.Index == 0 { // TODO: Encoding Error } return buf }
func (frame *IndexedLiteral) Encode() (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 } // No Huffman buf.Merge(integer.Encode(frame.ValueLength, 7)) buf.Merge([]byte(frame.ValueString)) return buf }
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 }