Skip to content

Commit

Permalink
Refactoring: Updated the DecodePayload interface, removed the second …
Browse files Browse the repository at this point in the history
…parameter `func(*packet.Packet)`, changed it to the previous return value `[]*packet.Packet` (for data statistics), and `error` has been changed added to the return value.

The error checking of DecodePayload calls to DecodePacket is preserved, and an `ERROR_PACKET` packet is not allowed to be returned.
  • Loading branch information
zishang520 committed Jul 19, 2023
1 parent c4f322d commit 544f7d1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func main() {
decoded from their base64 representation.
- **Parameters**
- `types.BufferInterface`: the payload
- `func(*packet.Packet)`: The decoded packet data

## Tests

Expand Down
53 changes: 31 additions & 22 deletions parser/parser-v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,74 +314,77 @@ func (p *parserv3) encodePayloadAsBinary(packets []*packet.Packet) (types.Buffer

// Decodes data when a payload is maybe expected. Possible binary contents are
// decoded from their base64 representation
func (p *parserv3) DecodePayload(data types.BufferInterface, callback func(*packet.Packet)) error {
func (p *parserv3) DecodePayload(data types.BufferInterface) (packets []*packet.Packet, _ error) {
switch v := data.(type) {
case *types.StringBuffer:
PACKETLEN := 0
for v.Len() > 0 {
length, err := v.ReadString(':')
if err != nil {
return err
return packets, err
}
_l := len(length)
if _l < 1 {
return errors.New("invalid data length")
return packets, errors.New("invalid data length")
}
packetLen, err := strconv.ParseInt(length[:_l-1], 10, 0)
if err != nil {
return err
return packets, err
}

PACKETLEN = int(packetLen)
msg := types.NewStringBuffer(nil)
for i := 0; i < PACKETLEN; {
r, _, e := v.ReadRune()
if e != nil {
return e
return packets, e
}
i += utils.Utf16Len(r)
if _, err := msg.WriteRune(r); err != nil {
return err
return packets, err
}
}

if msg.Len() > 0 {
packet, _ := p.DecodePacket(msg, false)
callback(packet)
if packet, err := p.DecodePacket(msg, false); err == nil {
packets = append(packets, packet)
} else {
return packets, nil
}
}
}
return nil
return packets, nil
}
return p.decodePayloadAsBinary(data, callback)
return p.decodePayloadAsBinary(data)
}

// Decodes data when a payload is maybe expected. Strings are decoded by
// interpreting each byte as a key code for entries marked to start with 0. See
// description of encodePayloadAsBinary
func (p *parserv3) decodePayloadAsBinary(bufferTail types.BufferInterface, callback func(*packet.Packet)) error {
func (p *parserv3) decodePayloadAsBinary(bufferTail types.BufferInterface) (packets []*packet.Packet, _ error) {
PACKETLEN := 0
for bufferTail.Len() > 0 {
startByte, err := bufferTail.ReadByte()
if err != nil {
// parser error in individual packet - ignoring payload
return err
return packets, err
}
isString := startByte == 0x00
length, err := bufferTail.ReadBytes(0xFF)
if err != nil {
return err
return packets, err
}
_l := len(length)
if _l < 1 {
return errors.New("invalid data length")
return packets, errors.New("invalid data length")
}
lenByte := length[:_l-1]
for k, l := 0, len(lenByte); k < l; k++ {
lenByte[k] = lenByte[k] + '0'
}
packetLen, err := strconv.ParseInt(string(lenByte), 10, 0)
if err != nil {
return err
return packets, err
}
PACKETLEN = int(packetLen)
if isString {
Expand All @@ -395,7 +398,7 @@ func (p *parserv3) decodePayloadAsBinary(bufferTail types.BufferInterface, callb
if err == io.EOF {
break
}
return err
return packets, err
}
if !utf8.ValidRune(r) {
r = 0xFFFD
Expand All @@ -405,23 +408,29 @@ func (p *parserv3) decodePayloadAsBinary(bufferTail types.BufferInterface, callb
r, l := utf8.DecodeRune(buf)
k += utils.Utf16Len(r)
if _, err := data.Write(utils.Utf8decodeBytes(buf[0:l])); err != nil {
return err
return packets, err
}
buf = buf[l:]
}
if cursor := len(utils.Utf8encodeBytes(buf)); cursor > 0 {
bufferTail.Seek(-int64(cursor), io.SeekCurrent)
}
if data.Len() > 0 {
packet, _ := p.DecodePacket(data, false)
callback(packet)
if packet, err := p.DecodePacket(data, false); err == nil {
packets = append(packets, packet)
} else {
return packets, err
}
}
} else {
if data := bufferTail.Next(PACKETLEN); len(data) > 0 {
packet, _ := p.DecodePacket(types.NewBytesBuffer(data), false)
callback(packet)
if packet, err := p.DecodePacket(types.NewBytesBuffer(data), false); err == nil {
packets = append(packets, packet)
} else {
return packets, err
}
}
}
}
return nil
return packets, nil
}
11 changes: 7 additions & 4 deletions parser/parser-v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (p *parserv4) EncodePayload(packets []*packet.Packet, _ ...bool) (types.Buf
return enPayload, nil
}

func (p *parserv4) DecodePayload(data types.BufferInterface, callback func(*packet.Packet)) error {
func (p *parserv4) DecodePayload(data types.BufferInterface) (packets []*packet.Packet, _ error) {
scanner := bufio.NewScanner(data)
scanner.Split(func(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
Expand All @@ -155,8 +155,11 @@ func (p *parserv4) DecodePayload(data types.BufferInterface, callback func(*pack
return 0, nil, nil
})
for scanner.Scan() {
packet, _ := p.DecodePacket(types.NewStringBuffer(scanner.Bytes()))
callback(packet)
if packet, err := p.DecodePacket(types.NewStringBuffer(scanner.Bytes())); err == nil {
packets = append(packets, packet)
} else {
return packets, err
}
}
return nil
return packets, scanner.Err()
}
20 changes: 4 additions & 16 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,7 @@ func TestParserv3(t *testing.T) {
})

t.Run("DecodePayload/Base64", func(t *testing.T) {
packs := []*packet.Packet{}
err := p.DecodePayload(types.NewStringBufferString("6:b0QUJD26:1test测试中文和表情字符❤️🧡💛🧓🏾💟x"), func(pack *packet.Packet) {
packs = append(packs, pack)
})
packs, err := p.DecodePayload(types.NewStringBufferString("6:b0QUJD26:1test测试中文和表情字符❤️🧡💛🧓🏾💟x"))

if err == nil {
t.Fatal("DecodePayload error must be not nil.")
Expand Down Expand Up @@ -406,10 +403,7 @@ func TestParserv3(t *testing.T) {
})

t.Run("DecodePayload", func(t *testing.T) {
packs := []*packet.Packet{}
p.DecodePayload(types.NewBytesBuffer([]byte{1, 4, 255, 0, 65, 66, 67, 0, 5, 8, 255, 49, 116, 101, 115, 116, 195, 131, 194, 166, 195, 130, 194, 181, 195, 130, 194, 139, 195, 131, 194, 168, 195, 130, 194, 175, 195, 130, 194, 149, 195, 131, 194, 164, 195, 130, 194, 184, 195, 130, 194, 173, 195, 131, 194, 166, 195, 130, 194, 150, 195, 130, 194, 135, 195, 131, 194, 165, 195, 130, 194, 146, 195, 130, 194, 140, 195, 131, 194, 168, 195, 130, 194, 161, 195, 130, 194, 168, 195, 131, 194, 166, 195, 130, 194, 131, 195, 130, 194, 133, 195, 131, 194, 165, 195, 130, 194, 173, 195, 130, 194, 151, 195, 131, 194, 167, 195, 130, 194, 172, 195, 130, 194, 166, 195, 131, 194, 162, 195, 130, 194, 157, 195, 130, 194, 164, 195, 131, 194, 175, 195, 130, 194, 184, 195, 130, 194, 143, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 167, 195, 130, 194, 161, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 146, 195, 130, 194, 155, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 167, 195, 130, 194, 147, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 143, 195, 130, 194, 190, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 146, 195, 130, 194, 159}), func(pack *packet.Packet) {
packs = append(packs, pack)
})
packs, _ := p.DecodePayload(types.NewBytesBuffer([]byte{1, 4, 255, 0, 65, 66, 67, 0, 5, 8, 255, 49, 116, 101, 115, 116, 195, 131, 194, 166, 195, 130, 194, 181, 195, 130, 194, 139, 195, 131, 194, 168, 195, 130, 194, 175, 195, 130, 194, 149, 195, 131, 194, 164, 195, 130, 194, 184, 195, 130, 194, 173, 195, 131, 194, 166, 195, 130, 194, 150, 195, 130, 194, 135, 195, 131, 194, 165, 195, 130, 194, 146, 195, 130, 194, 140, 195, 131, 194, 168, 195, 130, 194, 161, 195, 130, 194, 168, 195, 131, 194, 166, 195, 130, 194, 131, 195, 130, 194, 133, 195, 131, 194, 165, 195, 130, 194, 173, 195, 130, 194, 151, 195, 131, 194, 167, 195, 130, 194, 172, 195, 130, 194, 166, 195, 131, 194, 162, 195, 130, 194, 157, 195, 130, 194, 164, 195, 131, 194, 175, 195, 130, 194, 184, 195, 130, 194, 143, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 167, 195, 130, 194, 161, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 146, 195, 130, 194, 155, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 167, 195, 130, 194, 147, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 143, 195, 130, 194, 190, 195, 131, 194, 176, 195, 130, 194, 159, 195, 130, 194, 146, 195, 130, 194, 159}))

if l := len(packs); l != 2 {
t.Fatalf(`*len(packs) = %d, want match for %d`, l, 2)
Expand Down Expand Up @@ -687,10 +681,7 @@ func TestParserv4(t *testing.T) {
})

t.Run("DecodePayload/Base64", func(t *testing.T) {
packs := []*packet.Packet{}
p.DecodePayload(types.NewStringBufferString("bQUJD\x1e1test测试中文和表情字符❤️🧡💛🧓🏾💟"), func(pack *packet.Packet) {
packs = append(packs, pack)
})
packs, _ := p.DecodePayload(types.NewStringBufferString("bQUJD\x1e1test测试中文和表情字符❤️🧡💛🧓🏾💟"))

if l := len(packs); l != 2 {
t.Fatalf(`*len(packs) = %d, want match for %d`, l, 2)
Expand Down Expand Up @@ -750,10 +741,7 @@ func TestParserv4(t *testing.T) {
})

t.Run("DecodePayload", func(t *testing.T) {
packs := []*packet.Packet{}
p.DecodePayload(types.NewBytesBuffer([]byte{98, 81, 85, 74, 68, 30, 49, 116, 101, 115, 116, 230, 181, 139, 232, 175, 149, 228, 184, 173, 230, 150, 135, 229, 146, 140, 232, 161, 168, 230, 131, 133, 229, 173, 151, 231, 172, 166, 226, 157, 164, 239, 184, 143, 240, 159, 167, 161, 240, 159, 146, 155, 240, 159, 167, 147, 240, 159, 143, 190, 240, 159, 146, 159}), func(pack *packet.Packet) {
packs = append(packs, pack)
})
packs, _ := p.DecodePayload(types.NewBytesBuffer([]byte{98, 81, 85, 74, 68, 30, 49, 116, 101, 115, 116, 230, 181, 139, 232, 175, 149, 228, 184, 173, 230, 150, 135, 229, 146, 140, 232, 161, 168, 230, 131, 133, 229, 173, 151, 231, 172, 166, 226, 157, 164, 239, 184, 143, 240, 159, 167, 161, 240, 159, 146, 155, 240, 159, 167, 147, 240, 159, 143, 190, 240, 159, 146, 159}))

if l := len(packs); l != 2 {
t.Fatalf(`*len(packs) = %d, want match for %d`, l, 2)
Expand Down
2 changes: 1 addition & 1 deletion parser/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Parser interface {
EncodePacket(*packet.Packet, bool, ...bool) (types.BufferInterface, error)
DecodePacket(types.BufferInterface, ...bool) (*packet.Packet, error)
EncodePayload([]*packet.Packet, ...bool) (types.BufferInterface, error)
DecodePayload(types.BufferInterface, func(*packet.Packet)) error
DecodePayload(types.BufferInterface) ([]*packet.Packet, error)
}

// Packet types.
Expand Down

0 comments on commit 544f7d1

Please sign in to comment.