diff --git a/gapstring/gapstring.go b/gapstring/gapstring.go index 722eea1..d8548b1 100644 --- a/gapstring/gapstring.go +++ b/gapstring/gapstring.go @@ -280,7 +280,14 @@ func (g GapString) Hexdump() string { return out.String() } -// XXX: These should probably move to Packet +func (g GapString) Uint32LE() (uint32, GapString) { + return binary.LittleEndian.Uint32(g.Slice(0, 4).Bytes()), g.Slice(4, g.Length()) +} + +func (g GapString) Uint16LE() (uint16, GapString) { + return binary.LittleEndian.Uint16(g.Slice(0, 2).Bytes()), g.Slice(2, g.Length()) +} + func (g GapString) Utf16(order binary.ByteOrder, fill string) string { in := g.Bytes([]byte(fill)...) ints := make([]uint16, len(in)/2) diff --git a/stream.go b/stream.go index 12c418e..12b063b 100644 --- a/stream.go +++ b/stream.go @@ -85,16 +85,21 @@ func (stream *Stream) Read(length int) (Utterance, error) { stream.pending.When = u.When } + pendingLen := stream.pending.Data.Length() // If we got nothing, it's the end of the stream - if stream.pending.Data.Length() == 0 { + if pendingLen == 0 { return Utterance{}, io.EOF } + sliceLen := length + if sliceLen > pendingLen { + sliceLen = pendingLen + } ret := Utterance{ - Data: stream.pending.Data.Slice(0, length), + Data: stream.pending.Data.Slice(0, sliceLen), When: stream.pending.When, } - stream.pending.Data = stream.pending.Data.Slice(length, stream.pending.Data.Length()) + stream.pending.Data = stream.pending.Data.Slice(sliceLen, pendingLen) return ret, nil }