forked from tormoder/fit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
211 lines (181 loc) · 4.53 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package fit
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/JesprUniverse/fit/dyncrc16"
)
// Header represents a FIT file header.
type Header struct {
Size byte
ProtocolVersion byte
ProfileVersion uint16
DataSize uint32
DataType [4]byte
CRC uint16
}
var (
errNotFit = FormatError("header data type was not '.FIT'")
errHeaderSize = FormatError("illegal header size")
errHdrCRC = IntegrityError("header checksum failed")
errReadSize = ioError{op: "read size", err: io.ErrUnexpectedEOF}
errReadData = ioError{op: "read data", err: io.ErrUnexpectedEOF}
)
// NewHeader creates a new Header with required info.
func NewHeader(v ProtocolVersion, crc bool) Header {
h := Header{
ProtocolVersion: v.Version(),
ProfileVersion: ProfileVersion,
Size: headerSizeNoCRC,
}
if crc {
h.Size = headerSizeCRC
}
copy(h.DataType[:], fitDataTypeString[:])
return h
}
func (d *decoder) decodeHeader() error {
err := binary.Read(d.r, le, &d.h.Size)
if err != nil {
if errors.Is(err, io.EOF) {
return errReadSize
}
return ioError{"reading size", err}
}
if d.h.Size != headerSizeCRC && d.h.Size != headerSizeNoCRC {
return errHeaderSize
}
_, err = io.ReadFull(d.r, d.tmp[:d.h.Size-1])
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return errReadData
}
return ioError{"reading data", err}
}
if err = checkProtocolVersion(d.tmp[0]); err != nil {
return err
}
d.h.ProtocolVersion = d.tmp[0]
d.h.ProfileVersion = le.Uint16(d.tmp[1:3])
d.h.DataSize = le.Uint32(d.tmp[3:7])
if string(d.tmp[7:11]) != fitDataTypeString {
return errNotFit
}
copy(d.h.DataType[:], d.tmp[7:11])
d.crc.Write([]byte{d.h.Size})
d.crc.Write(d.tmp[:d.h.Size-1])
if d.h.Size == headerSizeNoCRC {
return nil
}
d.h.CRC = le.Uint16(d.tmp[11 : d.h.Size-1])
if d.h.CRC == 0x0000 {
return nil
}
if d.crc.Sum16() != 0x0000 {
return errHdrCRC
}
return nil
}
func checkProtocolVersion(b byte) error {
fileProtoVer := ProtocolVersion(b)
if fileProtoVer.Major() > CurrentProtocolVersion().Major() {
err := fmt.Sprintf(
"protocol version %v not supported by sdk protocol version %v",
fileProtoVer,
CurrentProtocolVersion(),
)
return NotSupportedError(err)
}
return nil
}
// CheckIntegrity verifies the FIT header CRC.
func (h Header) CheckIntegrity() error {
if err := checkProtocolVersion(h.ProtocolVersion); err != nil {
return err
}
if string(h.DataType[:len(h.DataType)]) != fitDataTypeString {
return errNotFit
}
if h.Size == headerSizeNoCRC {
return nil
}
if h.CRC == 0 {
return nil
}
crc := dyncrc16.New()
bh := make([]byte, h.Size)
bh[0] = h.Size
bh[1] = h.ProtocolVersion
le.PutUint16(bh[2:4], h.ProfileVersion)
le.PutUint32(bh[4:8], h.DataSize)
copy(bh[8:12], h.DataType[:])
le.PutUint16(bh[12:14], h.CRC)
if crc.Sum16() != 0x0000 {
return errHdrCRC
}
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (h Header) MarshalJSON() ([]byte, error) {
e := newJSONEncodeState()
e.open()
e.writeFieldName("Size")
e.writeUint(uint64(h.Size))
e.c()
e.writeFieldName("ProtocolVersion")
e.writeUint(uint64(h.ProtocolVersion))
e.c()
e.writeFieldName("ProfileVersion")
e.writeUint(uint64(h.ProfileVersion))
e.c()
e.writeFieldName("DataSize")
e.writeUint(uint64(h.DataSize))
e.c()
e.writeFieldName("DataType")
e.writeStringBytes(h.DataType[:4])
e.c()
e.writeFieldName("CRC")
e.writeUint(uint64(h.CRC))
e.close()
return e.Bytes(), nil
}
func (h Header) String() string {
return fmt.Sprintf(
"size: %d | protover: %d | profver: %d | dsize: %d | dtype: %s | crc: 0x%x",
h.Size, h.ProtocolVersion, h.ProfileVersion, h.DataSize, string(h.DataType[:]), h.CRC,
)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (h Header) MarshalBinary() ([]byte, error) {
buf := &bytes.Buffer{}
err := binary.Write(buf, binary.LittleEndian, h.Size)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, h.ProtocolVersion)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, h.ProfileVersion)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, h.DataSize)
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.LittleEndian, h.DataType)
if err != nil {
return nil, err
}
h.CRC = dyncrc16.Checksum(buf.Bytes())
if h.Size == headerSizeCRC {
err = binary.Write(buf, binary.LittleEndian, h.CRC)
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}