go-teeworlds-protocol/chunk7/splitter.go

29 lines
656 B
Go
Raw Normal View History

2024-06-20 00:26:04 +00:00
package chunk7
2024-06-07 07:38:35 +00:00
// data has to be the uncompressed payload of a teeworlds packet
// without the packet header
//
// It will return all the chunks (messages) in that packet
func UnpackChunks(data []byte) []Chunk {
chunks := []Chunk{}
payloadSize := len(data)
i := 0
for i < payloadSize {
chunk := Chunk{}
// TODO: can we use ChunkHeader.Unpack(u) here to simplify the code?
chunk.Header.UnpackRaw(data[i:])
2024-06-07 07:38:35 +00:00
i += 2 // header
if chunk.Header.Flags.Vital {
i++
}
end := i + chunk.Header.Size
2024-06-17 04:24:08 +00:00
chunk.Data = make([]byte, end-i)
copy(chunk.Data, data[i:end])
2024-06-07 07:38:35 +00:00
i += chunk.Header.Size
chunks = append(chunks, chunk)
}
return chunks
}