From dfbb2baff9491b086b2a636dca406beac18209d2 Mon Sep 17 00:00:00 2001 From: Ivan Danyliuk Date: Sat, 10 Nov 2018 19:58:20 +0100 Subject: [PATCH] Switch to decimals in protocol code --- protocol/decode.go | 12 +++--------- protocol/encode.go | 4 ++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/protocol/decode.go b/protocol/decode.go index 0cf4e84..5268fd6 100644 --- a/protocol/decode.go +++ b/protocol/decode.go @@ -1,16 +1,10 @@ package protocol import ( - "errors" "fmt" "sort" ) -// default errors -var ( - ErrInvalidFrame = errors.New("invalid frame") -) - // Decoder represents protocol decode. type Decoder struct { buffer []byte @@ -43,16 +37,16 @@ func NewDecoderSize(size int) *Decoder { // DecodeChunk takes a single chunk of data and decodes it. func (d *Decoder) DecodeChunk(data string) error { if data == "" || len(data) < 4 { - return ErrInvalidFrame + return fmt.Errorf("invalid frame: \"%s\"", data) } var ( offset, total int payload []byte ) - _, err := fmt.Sscanf(data, "%x/%x|%s", &offset, &total, &payload) + _, err := fmt.Sscanf(data, "%d/%d|%s", &offset, &total, &payload) if err != nil { - return fmt.Errorf("invalid frame: %v", err) + return fmt.Errorf("invalid frame: %v (%s)", err, data) } // allocate enough memory at first total read diff --git a/protocol/encode.go b/protocol/encode.go index 08412aa..36d5dc6 100644 --- a/protocol/encode.go +++ b/protocol/encode.go @@ -33,7 +33,7 @@ func (e *Encoder) EncodeReader(r io.Reader) ([]string, error) { // futher converted to QR code frames. func (e *Encoder) Encode(str string) ([]string, error) { if len(str) < e.chunkLen { - return []string{str}, nil + return []string{e.frame(0, len(str), str)}, nil } numChunks := len(str)/e.chunkLen + 1 @@ -54,5 +54,5 @@ func (e *Encoder) Encode(str string) ([]string, error) { } func (e *Encoder) frame(count, total int, str string) string { - return fmt.Sprintf("%x/%x|%s", count, total, str) + return fmt.Sprintf("%d/%d|%s", count, total, str) }