From 8cfc5ff44b09f964a84da6e33e9e2e73cd7db3af Mon Sep 17 00:00:00 2001 From: Hikmatulloh Hari Mukti Date: Mon, 23 Sep 2024 15:40:33 +0700 Subject: [PATCH] fix: deocder peek file_id --- decoder/decoder.go | 11 +++-------- decoder/decoder_test.go | 41 ----------------------------------------- 2 files changed, 3 insertions(+), 49 deletions(-) diff --git a/decoder/decoder.go b/decoder/decoder.go index 2d6b986f..58c8ec33 100644 --- a/decoder/decoder.go +++ b/decoder/decoder.go @@ -42,7 +42,6 @@ const ( ErrMesgDefMissing = errorString("message definition missing") // NOTE: Kept exported since it's used by RawDecoder errInvalidBaseType = errorString("invalid basetype") - errMissingFileId = errorString("missing file_id") ) // Decoder is FIT file decoder. See New() for details. @@ -343,7 +342,8 @@ func (d *Decoder) PeekFileHeader() (*proto.FileHeader, error) { } // PeekFileId decodes only up to FileId message without decoding the whole reader. -// FileId message should be the first message of any FIT file, otherwise return an error. +// The FileId is expected to be present as the first message; however, we don't validate this, +// as it's an edge case that occurs when a FIT file is poorly encoded. // // If we choose to continue, Decode picks up where this left then continue decoding next messages instead of starting from zero. func (d *Decoder) PeekFileId() (*mesgdef.FileId, error) { @@ -353,16 +353,11 @@ func (d *Decoder) PeekFileId() (*mesgdef.FileId, error) { if d.err = d.decodeFileHeaderOnce(); d.err != nil { return nil, d.err } - for len(d.messages) == 0 { + for d.fileId == nil { if d.err = d.decodeMessage(); d.err != nil { return nil, d.err } } - if d.fileId == nil { - mesg := &d.messages[0] - return nil, fmt.Errorf("expect file_id as first mesg, got: %s(%d): %w", - mesg.Num, mesg.Num, errMissingFileId) - } return d.fileId, nil } diff --git a/decoder/decoder_test.go b/decoder/decoder_test.go index a8a00baa..42c4fca8 100644 --- a/decoder/decoder_test.go +++ b/decoder/decoder_test.go @@ -432,47 +432,6 @@ func TestPeekFileId(t *testing.T) { fileId: mesgdef.NewFileId(&fit.Messages[0]), err: io.EOF, }, - { - name: "peek file id returned non file_id message", - r: func() io.Reader { - fileId := mesgdef.NewRecord(nil).SetDistance(0) - mesg := fileId.ToMesg(nil) - mesgb, _ := mesg.MarshalAppend(nil, proto.LittleEndian) - mesgDef, _ := proto.NewMessageDefinition(&mesg) - mesgDefb, _ := mesgDef.MarshalAppend(nil) - - fileHeaderb, _ := (&proto.FileHeader{ - Size: 12, - ProtocolVersion: proto.V1, - ProfileVersion: profile.Version, - DataSize: uint32(len(mesgDefb) + len(mesgb)), - DataType: proto.DataTypeFIT, - }).MarshalAppend(nil) - - buf := append(mesgDefb, mesgb...) - crc := crc16.New(nil) - crc.Write(buf) - - buf = append(fileHeaderb, buf...) - buf = binary.LittleEndian.AppendUint16(buf, crc.Sum16()) - - buf, cur := buf, 0 - return fnReader(func(b []byte) (n int, err error) { - m := len(buf) - if cur >= m { // only decode header - return 0, io.EOF - } - if cur+len(b) < m { - m = cur + len(b) - } - n = copy(b, buf[cur:m]) - cur += n - return - }) - }(), - fileId: nil, - err: errMissingFileId, - }, } for i, tc := range tt {