Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: decoder peek file_id #467

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down
41 changes: 0 additions & 41 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down