mirror of
https://github.com/jwetzell/psn-go.git
synced 2026-08-21 06:59:13 +00:00
add most basic error handling on the decode side
This commit is contained in:
+21
-4
@@ -1,6 +1,10 @@
|
|||||||
package psn
|
package psn
|
||||||
|
|
||||||
import "github.com/jwetzell/psn-go/internal/decoders"
|
import (
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/jwetzell/psn-go/internal/decoders"
|
||||||
|
)
|
||||||
|
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
lastInfoPacketHeader *decoders.PacketHeaderChunk
|
lastInfoPacketHeader *decoders.PacketHeaderChunk
|
||||||
@@ -49,10 +53,19 @@ func (d *Decoder) updateData(framePackets []decoders.DataPacketChunk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode(bytes []byte) {
|
func (d *Decoder) Decode(bytes []byte) {
|
||||||
chunk := decoders.DecodeChunk(bytes)
|
chunk, err := decoders.DecodeChunk(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error decoding", "err", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if chunk.Header.Id == 0x6756 {
|
if chunk.Header.Id == 0x6756 {
|
||||||
infoPacket := decoders.DecodeInfoPacketChunk(bytes)
|
infoPacket, err := decoders.DecodeInfoPacketChunk(bytes)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error decoding", "err", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
currentInfoPacketHeader := infoPacket.Data.PacketHeader
|
currentInfoPacketHeader := infoPacket.Data.PacketHeader
|
||||||
|
|
||||||
_, ok := d.infoPacketFrames[currentInfoPacketHeader.Data.FrameId]
|
_, ok := d.infoPacketFrames[currentInfoPacketHeader.Data.FrameId]
|
||||||
@@ -67,7 +80,11 @@ func (d *Decoder) Decode(bytes []byte) {
|
|||||||
delete(d.infoPacketFrames, currentInfoPacketHeader.Data.FrameId)
|
delete(d.infoPacketFrames, currentInfoPacketHeader.Data.FrameId)
|
||||||
}
|
}
|
||||||
} else if chunk.Header.Id == 0x6755 {
|
} else if chunk.Header.Id == 0x6755 {
|
||||||
dataPacket := decoders.DecodeDataPacketChunk(bytes)
|
dataPacket, err := decoders.DecodeDataPacketChunk(bytes)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error decoding", "err", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
currentInfoPacketHeader := dataPacket.Data.PacketHeader
|
currentInfoPacketHeader := dataPacket.Data.PacketHeader
|
||||||
|
|
||||||
_, ok := d.dataPacketFrames[currentInfoPacketHeader.Data.FrameId]
|
_, ok := d.dataPacketFrames[currentInfoPacketHeader.Data.FrameId]
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package decoders
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChunkHeader struct {
|
type ChunkHeader struct {
|
||||||
@@ -15,7 +16,12 @@ type Chunk struct {
|
|||||||
ChunkData []byte
|
ChunkData []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeChunk(bytes []byte) Chunk {
|
func DecodeChunk(bytes []byte) (Chunk, error) {
|
||||||
|
|
||||||
|
if len(bytes) < 4 {
|
||||||
|
return Chunk{}, errors.New("chunk must be at least 4 bytes")
|
||||||
|
}
|
||||||
|
|
||||||
id := binary.LittleEndian.Uint16(bytes[0:2])
|
id := binary.LittleEndian.Uint16(bytes[0:2])
|
||||||
lengthAndFlag := binary.LittleEndian.Uint16(bytes[2:4])
|
lengthAndFlag := binary.LittleEndian.Uint16(bytes[2:4])
|
||||||
|
|
||||||
@@ -38,5 +44,5 @@ func DecodeChunk(bytes []byte) Chunk {
|
|||||||
return Chunk{
|
return Chunk{
|
||||||
Header: header,
|
Header: header,
|
||||||
ChunkData: chunk_data,
|
ChunkData: chunk_data,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,12 @@ func TestChunkDecoding(t *testing.T) {
|
|||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
|
|
||||||
actual := DecodeChunk(testCase.bytes)
|
actual, err := DecodeChunk(testCase.bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description)
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(actual, testCase.expected) {
|
if !reflect.DeepEqual(actual, testCase.expected) {
|
||||||
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description)
|
t.Errorf("Test '%s' failed to decode chunk properly", testCase.description)
|
||||||
|
|||||||
@@ -15,8 +15,13 @@ type DataPacketChunk struct {
|
|||||||
Data DataPacketChunkData
|
Data DataPacketChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeDataPacketChunk(bytes []byte) DataPacketChunk {
|
func DecodeDataPacketChunk(bytes []byte) (DataPacketChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return DataPacketChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
data := DataPacketChunkData{}
|
data := DataPacketChunkData{}
|
||||||
|
|
||||||
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
||||||
@@ -25,14 +30,20 @@ func DecodeDataPacketChunk(bytes []byte) DataPacketChunk {
|
|||||||
for offset < int(chunk.Header.DataLen) {
|
for offset < int(chunk.Header.DataLen) {
|
||||||
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
||||||
case 0x0000:
|
case 0x0000:
|
||||||
packet_header := DecodePacketHeaderChunk(chunk.ChunkData[offset:])
|
packet_header, err := DecodePacketHeaderChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataPacketChunk{}, err
|
||||||
|
}
|
||||||
data.PacketHeader = &packet_header
|
data.PacketHeader = &packet_header
|
||||||
offset += 4
|
offset += 4
|
||||||
if packet_header.Chunk.Header.DataLen > 0 {
|
if packet_header.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(packet_header.Chunk.Header.DataLen)
|
offset = offset + int(packet_header.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0001:
|
case 0x0001:
|
||||||
tracker_list := DecodeDataTrackerListChunk(chunk.ChunkData[offset:])
|
tracker_list, err := DecodeDataTrackerListChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataPacketChunk{}, err
|
||||||
|
}
|
||||||
data.TrackerList = &tracker_list
|
data.TrackerList = &tracker_list
|
||||||
offset += 4
|
offset += 4
|
||||||
if tracker_list.Chunk.Header.DataLen > 0 {
|
if tracker_list.Chunk.Header.DataLen > 0 {
|
||||||
@@ -46,7 +57,8 @@ func DecodeDataPacketChunk(bytes []byte) DataPacketChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DataPacketChunk{
|
return DataPacketChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,13 @@ type DataTrackerChunk struct {
|
|||||||
Data DataTrackerChunkData
|
Data DataTrackerChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeDataTrackerChunk(bytes []byte) DataTrackerChunk {
|
func DecodeDataTrackerChunk(bytes []byte) (DataTrackerChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
data := DataTrackerChunkData{}
|
data := DataTrackerChunkData{}
|
||||||
|
|
||||||
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
||||||
@@ -30,49 +35,70 @@ func DecodeDataTrackerChunk(bytes []byte) DataTrackerChunk {
|
|||||||
for offset < int(chunk.Header.DataLen) {
|
for offset < int(chunk.Header.DataLen) {
|
||||||
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
||||||
case 0x0000:
|
case 0x0000:
|
||||||
pos := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
pos, err := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.Pos = &pos
|
data.Pos = &pos
|
||||||
offset += 4
|
offset += 4
|
||||||
if data.Pos.Chunk.Header.DataLen > 0 {
|
if data.Pos.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(data.Pos.Chunk.Header.DataLen)
|
offset = offset + int(data.Pos.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0001:
|
case 0x0001:
|
||||||
speed := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
speed, err := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.Speed = &speed
|
data.Speed = &speed
|
||||||
offset += 4
|
offset += 4
|
||||||
if data.Speed.Chunk.Header.DataLen > 0 {
|
if data.Speed.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(data.Speed.Chunk.Header.DataLen)
|
offset = offset + int(data.Speed.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0002:
|
case 0x0002:
|
||||||
ori := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
ori, err := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.Ori = &ori
|
data.Ori = &ori
|
||||||
offset += 4
|
offset += 4
|
||||||
if data.Ori.Chunk.Header.DataLen > 0 {
|
if data.Ori.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(data.Ori.Chunk.Header.DataLen)
|
offset = offset + int(data.Ori.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0003:
|
case 0x0003:
|
||||||
status := DecodeDataTrackerStatusChunk(chunk.ChunkData[offset:])
|
status, err := DecodeDataTrackerStatusChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.Status = &status
|
data.Status = &status
|
||||||
offset += 4
|
offset += 4
|
||||||
if data.Status.Chunk.Header.DataLen > 0 {
|
if data.Status.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(data.Status.Chunk.Header.DataLen)
|
offset = offset + int(data.Status.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0004:
|
case 0x0004:
|
||||||
accel := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
accel, err := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.Accel = &accel
|
data.Accel = &accel
|
||||||
offset += 4
|
offset += 4
|
||||||
if data.Accel.Chunk.Header.DataLen > 0 {
|
if data.Accel.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(data.Accel.Chunk.Header.DataLen)
|
offset = offset + int(data.Accel.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0005:
|
case 0x0005:
|
||||||
trgtpos := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
trgtpos, err := DecodeDataTrackerXYZChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.TrgtPos = &trgtpos
|
data.TrgtPos = &trgtpos
|
||||||
offset += 4
|
offset += 4
|
||||||
if data.TrgtPos.Chunk.Header.DataLen > 0 {
|
if data.TrgtPos.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(data.TrgtPos.Chunk.Header.DataLen)
|
offset = offset + int(data.TrgtPos.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0006:
|
case 0x0006:
|
||||||
timestamp := DecodeDataTrackerTimestampChunk(chunk.ChunkData[offset:])
|
timestamp, err := DecodeDataTrackerTimestampChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.Timestamp = ×tamp
|
data.Timestamp = ×tamp
|
||||||
offset += 4
|
offset += 4
|
||||||
if data.Timestamp.Chunk.Header.DataLen > 0 {
|
if data.Timestamp.Chunk.Header.DataLen > 0 {
|
||||||
@@ -86,7 +112,8 @@ func DecodeDataTrackerChunk(bytes []byte) DataTrackerChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DataTrackerChunk{
|
return DataTrackerChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,21 @@ type DataTrackerListChunk struct {
|
|||||||
Data DataTrackerListChunkData
|
Data DataTrackerListChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeDataTrackerListChunk(bytes []byte) DataTrackerListChunk {
|
func DecodeDataTrackerListChunk(bytes []byte) (DataTrackerListChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerListChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
trackers := []DataTrackerChunk{}
|
trackers := []DataTrackerChunk{}
|
||||||
if chunk.Header.HasSubchunks && chunk.Header.DataLen > 0 {
|
if chunk.Header.HasSubchunks && chunk.Header.DataLen > 0 {
|
||||||
offset := 0
|
offset := 0
|
||||||
for offset < int(chunk.Header.DataLen) {
|
for offset < int(chunk.Header.DataLen) {
|
||||||
trackerChunk := DecodeDataTrackerChunk(chunk.ChunkData[offset:])
|
trackerChunk, err := DecodeDataTrackerChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerListChunk{}, err
|
||||||
|
}
|
||||||
offset += 4
|
offset += 4
|
||||||
if trackerChunk.Chunk.Header.DataLen > 0 {
|
if trackerChunk.Chunk.Header.DataLen > 0 {
|
||||||
offset += int(trackerChunk.Chunk.Header.DataLen)
|
offset += int(trackerChunk.Chunk.Header.DataLen)
|
||||||
@@ -29,7 +36,8 @@ func DecodeDataTrackerListChunk(bytes []byte) DataTrackerListChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DataTrackerListChunk{
|
return DataTrackerListChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package decoders
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,8 +15,16 @@ type DataTrackerStatusChunk struct {
|
|||||||
Data DataTrackerStatusChunkData
|
Data DataTrackerStatusChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeDataTrackerStatusChunk(bytes []byte) DataTrackerStatusChunk {
|
func DecodeDataTrackerStatusChunk(bytes []byte) (DataTrackerStatusChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerStatusChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(chunk.ChunkData) < 4 {
|
||||||
|
return DataTrackerStatusChunk{}, errors.New("DATA_TRACKER_STATUS chunk must be at least 4 bytes")
|
||||||
|
}
|
||||||
|
|
||||||
statusBits := binary.LittleEndian.Uint32(chunk.ChunkData[0:4])
|
statusBits := binary.LittleEndian.Uint32(chunk.ChunkData[0:4])
|
||||||
|
|
||||||
@@ -24,7 +33,8 @@ func DecodeDataTrackerStatusChunk(bytes []byte) DataTrackerStatusChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DataTrackerStatusChunk{
|
return DataTrackerStatusChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package decoders
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DataTrackerTimestampChunkData struct {
|
type DataTrackerTimestampChunkData struct {
|
||||||
@@ -13,8 +14,16 @@ type DataTrackerTimestampChunk struct {
|
|||||||
Data DataTrackerTimestampChunkData
|
Data DataTrackerTimestampChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeDataTrackerTimestampChunk(bytes []byte) DataTrackerTimestampChunk {
|
func DecodeDataTrackerTimestampChunk(bytes []byte) (DataTrackerTimestampChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerTimestampChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(chunk.ChunkData) < 8 {
|
||||||
|
return DataTrackerTimestampChunk{}, errors.New("DATA_TRACKER_TIMESTAMP chunk must be at least 8 bytes")
|
||||||
|
}
|
||||||
|
|
||||||
timestamp := binary.LittleEndian.Uint64(chunk.ChunkData[0:8])
|
timestamp := binary.LittleEndian.Uint64(chunk.ChunkData[0:8])
|
||||||
|
|
||||||
@@ -23,7 +32,8 @@ func DecodeDataTrackerTimestampChunk(bytes []byte) DataTrackerTimestampChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return DataTrackerTimestampChunk{
|
return DataTrackerTimestampChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package decoders
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,8 +17,16 @@ type DataTrackerXYZChunk struct {
|
|||||||
Data DataTrackerXYZChunkData
|
Data DataTrackerXYZChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeDataTrackerXYZChunk(bytes []byte) DataTrackerXYZChunk {
|
func DecodeDataTrackerXYZChunk(bytes []byte) (DataTrackerXYZChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return DataTrackerXYZChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(chunk.ChunkData) < 12 {
|
||||||
|
return DataTrackerXYZChunk{}, errors.New("DATA_TRACKER_XYZ chunk must be at least 12 bytes")
|
||||||
|
}
|
||||||
|
|
||||||
xBits := binary.LittleEndian.Uint32(chunk.ChunkData[0:4])
|
xBits := binary.LittleEndian.Uint32(chunk.ChunkData[0:4])
|
||||||
yBits := binary.LittleEndian.Uint32(chunk.ChunkData[4:8])
|
yBits := binary.LittleEndian.Uint32(chunk.ChunkData[4:8])
|
||||||
@@ -32,5 +41,5 @@ func DecodeDataTrackerXYZChunk(bytes []byte) DataTrackerXYZChunk {
|
|||||||
return DataTrackerXYZChunk{
|
return DataTrackerXYZChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,11 @@ type InfoPacketChunk struct {
|
|||||||
Data InfoPacketChunkData
|
Data InfoPacketChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeInfoPacketChunk(bytes []byte) InfoPacketChunk {
|
func DecodeInfoPacketChunk(bytes []byte) (InfoPacketChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
if err != nil {
|
||||||
|
return InfoPacketChunk{}, err
|
||||||
|
}
|
||||||
data := InfoPacketChunkData{}
|
data := InfoPacketChunkData{}
|
||||||
|
|
||||||
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
||||||
@@ -26,21 +29,30 @@ func DecodeInfoPacketChunk(bytes []byte) InfoPacketChunk {
|
|||||||
for offset < int(chunk.Header.DataLen) {
|
for offset < int(chunk.Header.DataLen) {
|
||||||
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
||||||
case 0x0000:
|
case 0x0000:
|
||||||
packet_header := DecodePacketHeaderChunk(chunk.ChunkData[offset:])
|
packet_header, err := DecodePacketHeaderChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return InfoPacketChunk{}, err
|
||||||
|
}
|
||||||
data.PacketHeader = &packet_header
|
data.PacketHeader = &packet_header
|
||||||
offset += 4
|
offset += 4
|
||||||
if packet_header.Chunk.Header.DataLen > 0 {
|
if packet_header.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(packet_header.Chunk.Header.DataLen)
|
offset = offset + int(packet_header.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0001:
|
case 0x0001:
|
||||||
system_name := DecodeInfoSystemNameChunk(chunk.ChunkData[offset:])
|
system_name, err := DecodeInfoSystemNameChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return InfoPacketChunk{}, err
|
||||||
|
}
|
||||||
data.SystemName = &system_name
|
data.SystemName = &system_name
|
||||||
offset += 4
|
offset += 4
|
||||||
if system_name.Chunk.Header.DataLen > 0 {
|
if system_name.Chunk.Header.DataLen > 0 {
|
||||||
offset = offset + int(system_name.Chunk.Header.DataLen)
|
offset = offset + int(system_name.Chunk.Header.DataLen)
|
||||||
}
|
}
|
||||||
case 0x0002:
|
case 0x0002:
|
||||||
tracker_list := DecodeInfoTrackerListChunk(chunk.ChunkData[offset:])
|
tracker_list, err := DecodeInfoTrackerListChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return InfoPacketChunk{}, err
|
||||||
|
}
|
||||||
data.TrackerList = &tracker_list
|
data.TrackerList = &tracker_list
|
||||||
offset += 4
|
offset += 4
|
||||||
if tracker_list.Chunk.Header.DataLen > 0 {
|
if tracker_list.Chunk.Header.DataLen > 0 {
|
||||||
@@ -54,7 +66,8 @@ func DecodeInfoPacketChunk(bytes []byte) InfoPacketChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return InfoPacketChunk{
|
return InfoPacketChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,16 +9,19 @@ type InfoSystemNameChunk struct {
|
|||||||
Data InfoSystemNameChunkData
|
Data InfoSystemNameChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeInfoSystemNameChunk(bytes []byte) InfoSystemNameChunk {
|
func DecodeInfoSystemNameChunk(bytes []byte) (InfoSystemNameChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
system_name := string(chunk.ChunkData[0:chunk.Header.DataLen])
|
if err != nil {
|
||||||
|
return InfoSystemNameChunk{}, err
|
||||||
|
}
|
||||||
|
data := InfoSystemNameChunkData{}
|
||||||
|
|
||||||
data := InfoSystemNameChunkData{
|
if chunk.Header.DataLen > 0 {
|
||||||
SystemName: system_name,
|
data.SystemName = string(chunk.ChunkData[0:chunk.Header.DataLen])
|
||||||
}
|
}
|
||||||
|
|
||||||
return InfoSystemNameChunk{
|
return InfoSystemNameChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,11 @@ type InfoTrackerChunk struct {
|
|||||||
Data InfoTrackerChunkData
|
Data InfoTrackerChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeInfoTrackerChunk(bytes []byte) InfoTrackerChunk {
|
func DecodeInfoTrackerChunk(bytes []byte) (InfoTrackerChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
if err != nil {
|
||||||
|
return InfoTrackerChunk{}, err
|
||||||
|
}
|
||||||
data := InfoTrackerChunkData{}
|
data := InfoTrackerChunkData{}
|
||||||
|
|
||||||
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
if chunk.Header.HasSubchunks && chunk.ChunkData != nil && chunk.Header.DataLen > 0 {
|
||||||
@@ -24,7 +27,10 @@ func DecodeInfoTrackerChunk(bytes []byte) InfoTrackerChunk {
|
|||||||
for offset < int(chunk.Header.DataLen) {
|
for offset < int(chunk.Header.DataLen) {
|
||||||
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
||||||
case 0x0000:
|
case 0x0000:
|
||||||
tracker_name := DecodeInfoTrackerNameChunk(chunk.ChunkData[offset:])
|
tracker_name, err := DecodeInfoTrackerNameChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return InfoTrackerChunk{}, err
|
||||||
|
}
|
||||||
data.TrackerName = &tracker_name
|
data.TrackerName = &tracker_name
|
||||||
offset += 4
|
offset += 4
|
||||||
if tracker_name.Chunk.Header.DataLen > 0 {
|
if tracker_name.Chunk.Header.DataLen > 0 {
|
||||||
@@ -37,7 +43,8 @@ func DecodeInfoTrackerChunk(bytes []byte) InfoTrackerChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return InfoTrackerChunk{
|
return InfoTrackerChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,14 +9,20 @@ type InfoTrackerListChunk struct {
|
|||||||
Data InfoTrackerListChunkData
|
Data InfoTrackerListChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeInfoTrackerListChunk(bytes []byte) InfoTrackerListChunk {
|
func DecodeInfoTrackerListChunk(bytes []byte) (InfoTrackerListChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
if err != nil {
|
||||||
|
return InfoTrackerListChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
trackers := []InfoTrackerChunk{}
|
trackers := []InfoTrackerChunk{}
|
||||||
if chunk.Header.HasSubchunks && chunk.Header.DataLen > 0 {
|
if chunk.Header.HasSubchunks && chunk.Header.DataLen > 0 {
|
||||||
offset := 0
|
offset := 0
|
||||||
for offset < int(chunk.Header.DataLen) {
|
for offset < int(chunk.Header.DataLen) {
|
||||||
trackerChunk := DecodeInfoTrackerChunk(chunk.ChunkData[offset:])
|
trackerChunk, err := DecodeInfoTrackerChunk(chunk.ChunkData[offset:])
|
||||||
|
if err != nil {
|
||||||
|
return InfoTrackerListChunk{}, err
|
||||||
|
}
|
||||||
offset += 4
|
offset += 4
|
||||||
if trackerChunk.Chunk.Header.DataLen > 0 {
|
if trackerChunk.Chunk.Header.DataLen > 0 {
|
||||||
offset += int(trackerChunk.Chunk.Header.DataLen)
|
offset += int(trackerChunk.Chunk.Header.DataLen)
|
||||||
@@ -30,7 +36,8 @@ func DecodeInfoTrackerListChunk(bytes []byte) InfoTrackerListChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return InfoTrackerListChunk{
|
return InfoTrackerListChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,17 +9,22 @@ type InfoTrackerNameChunk struct {
|
|||||||
Data InfoTrackerNameChunkData
|
Data InfoTrackerNameChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeInfoTrackerNameChunk(bytes []byte) InfoTrackerNameChunk {
|
func DecodeInfoTrackerNameChunk(bytes []byte) (InfoTrackerNameChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
|
||||||
tracker_name := string(chunk.ChunkData[0:chunk.Header.DataLen])
|
if err != nil {
|
||||||
|
return InfoTrackerNameChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
data := InfoTrackerNameChunkData{
|
data := InfoTrackerNameChunkData{}
|
||||||
TrackerName: tracker_name,
|
|
||||||
|
if chunk.Header.DataLen > 0 {
|
||||||
|
data.TrackerName = string(chunk.ChunkData[0:chunk.Header.DataLen])
|
||||||
}
|
}
|
||||||
|
|
||||||
return InfoTrackerNameChunk{
|
return InfoTrackerNameChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,11 @@ type PacketHeaderChunk struct {
|
|||||||
Data PacketHeaderChunkData
|
Data PacketHeaderChunkData
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodePacketHeaderChunk(bytes []byte) PacketHeaderChunk {
|
func DecodePacketHeaderChunk(bytes []byte) (PacketHeaderChunk, error) {
|
||||||
chunk := DecodeChunk(bytes)
|
chunk, err := DecodeChunk(bytes)
|
||||||
|
if err != nil {
|
||||||
|
return PacketHeaderChunk{}, err
|
||||||
|
}
|
||||||
|
|
||||||
packet_timestamp := binary.LittleEndian.Uint64(chunk.ChunkData[0:8])
|
packet_timestamp := binary.LittleEndian.Uint64(chunk.ChunkData[0:8])
|
||||||
version_high := chunk.ChunkData[8]
|
version_high := chunk.ChunkData[8]
|
||||||
@@ -33,7 +36,8 @@ func DecodePacketHeaderChunk(bytes []byte) PacketHeaderChunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return PacketHeaderChunk{
|
return PacketHeaderChunk{
|
||||||
Chunk: chunk,
|
Chunk: chunk,
|
||||||
Data: data,
|
Data: data,
|
||||||
}
|
},
|
||||||
|
nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user