Compare commits

...

2 Commits

Author SHA1 Message Date
6ffa3b6dd4 remove println 2021-12-20 18:43:14 -05:00
562fc231a4 remove unnecessary slice boxing 2021-12-20 18:42:36 -05:00

View File

@ -95,9 +95,9 @@ func parsePayload(stream []uint8) (int64, int) {
return parseNumber(num, chars), pos return parseNumber(num, chars), pos
} }
func parsePackets(stream []uint8) (int, []packet) { func parsePackets(stream []uint8) (int, packet) {
if len(stream) <= 7 { if len(stream) <= 7 {
return 0, []packet{} return 0, packet{}
} }
parsedPacket := packet{} parsedPacket := packet{}
parsedPacket.version = getPacketVersion(stream[0:3]) parsedPacket.version = getPacketVersion(stream[0:3])
@ -105,29 +105,28 @@ func parsePackets(stream []uint8) (int, []packet) {
if parsedPacket.typeID == 4 { if parsedPacket.typeID == 4 {
encoded, consumed := parsePayload(stream[6:]) encoded, consumed := parsePayload(stream[6:])
parsedPacket.encodedNumber = encoded parsedPacket.encodedNumber = encoded
return consumed + 6, []packet{parsedPacket} // 6 is packet header length return consumed + 6, parsedPacket // 6 is packet header length
} else { } else {
parsed := 0 parsed := 0
size, subpacketlength := getSubpacketLength(stream[6], stream[7:]) size, subpacketlength := getSubpacketLength(stream[6], stream[7:])
for { for {
consumed, subs := parsePackets(stream[7+size+parsed:]) consumed, subs := parsePackets(stream[7+size+parsed:])
parsedPacket.subPackets = append(parsedPacket.subPackets, subs...) parsedPacket.subPackets = append(parsedPacket.subPackets, subs)
parsed += consumed parsed += consumed
if size == 11 && int64(len(parsedPacket.subPackets)) == subpacketlength { if size == 11 && int64(len(parsedPacket.subPackets)) == subpacketlength {
return parsed + 7 + size, []packet{parsedPacket} return parsed + 7 + size, parsedPacket
} else if size == 15 && int64(parsed) > subpacketlength || consumed == 0 { } else if size == 15 && int64(parsed) > subpacketlength || consumed == 0 {
return parsed + 7 + size, []packet{parsedPacket} return parsed + 7 + size, parsedPacket
} }
} }
} }
} }
func countVersions(packets []packet) int { func countVersions(pac packet) int {
i := 0 i := int(pac.version)
for _, p := range packets { for _, p := range pac.subPackets {
i += int(p.version) i += countVersions(p)
i += countVersions(p.subPackets)
} }
return i return i
} }