mirror of
https://github.com/jwetzell/psn-go.git
synced 2026-09-08 15:59:20 +00:00
add most basic error handling on the decode side
This commit is contained in:
@@ -16,8 +16,11 @@ type InfoPacketChunk struct {
|
||||
Data InfoPacketChunkData
|
||||
}
|
||||
|
||||
func DecodeInfoPacketChunk(bytes []byte) InfoPacketChunk {
|
||||
chunk := DecodeChunk(bytes)
|
||||
func DecodeInfoPacketChunk(bytes []byte) (InfoPacketChunk, error) {
|
||||
chunk, err := DecodeChunk(bytes)
|
||||
if err != nil {
|
||||
return InfoPacketChunk{}, err
|
||||
}
|
||||
data := InfoPacketChunkData{}
|
||||
|
||||
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) {
|
||||
switch id := binary.LittleEndian.Uint16(chunk.ChunkData[offset : offset+2]); id {
|
||||
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
|
||||
offset += 4
|
||||
if packet_header.Chunk.Header.DataLen > 0 {
|
||||
offset = offset + int(packet_header.Chunk.Header.DataLen)
|
||||
}
|
||||
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
|
||||
offset += 4
|
||||
if system_name.Chunk.Header.DataLen > 0 {
|
||||
offset = offset + int(system_name.Chunk.Header.DataLen)
|
||||
}
|
||||
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
|
||||
offset += 4
|
||||
if tracker_list.Chunk.Header.DataLen > 0 {
|
||||
@@ -54,7 +66,8 @@ func DecodeInfoPacketChunk(bytes []byte) InfoPacketChunk {
|
||||
}
|
||||
|
||||
return InfoPacketChunk{
|
||||
Chunk: chunk,
|
||||
Data: data,
|
||||
}
|
||||
Chunk: chunk,
|
||||
Data: data,
|
||||
},
|
||||
nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user