add decode functions for info and data packets

This commit is contained in:
2024-12-18 22:48:08 -06:00
parent 90c734a023
commit 0a74b6a956
18 changed files with 724 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package decoders
import (
"encoding/binary"
)
type ChunkHeader struct {
Id uint16
DataLen uint16
HasSubchunks bool
}
type Chunk struct {
Header ChunkHeader
ChunkData []byte
}
func DecodeChunk(bytes []byte) Chunk {
id := binary.LittleEndian.Uint16(bytes[0:2])
lengthAndFlag := binary.LittleEndian.Uint16(bytes[2:4])
data_len := lengthAndFlag
has_subchunks := lengthAndFlag > 32768
if has_subchunks {
data_len = data_len - 32768
}
header := ChunkHeader{
Id: id,
DataLen: data_len,
HasSubchunks: has_subchunks,
}
chunk_data := bytes[4 : 4+header.DataLen]
return Chunk{
Header: header,
ChunkData: chunk_data,
}
}