-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse.go
202 lines (191 loc) · 5.47 KB
/
parse.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
package rosbag
import (
"bytes"
)
// ParseBagHeader parses a bag header record.
func ParseBagHeader(record []byte) (*BagHeader, error) {
if len(record) < 4 {
return nil, ErrShortBuffer
}
headerLength := int(u32(record))
header := record[4 : 4+headerLength]
indexPos, err := GetHeaderValue(header, "index_pos")
if err != nil {
return nil, err
}
connCount, err := GetHeaderValue(header, "conn_count")
if err != nil {
return nil, err
}
chunkCount, err := GetHeaderValue(header, "chunk_count")
if err != nil {
return nil, err
}
return &BagHeader{
IndexPos: u64(indexPos),
ConnCount: u32(connCount),
ChunkCount: u32(chunkCount),
}, nil
}
// ParseConnection parses a connection record.
func ParseConnection(record []byte) (*Connection, error) {
var headerLength, dataLength int
offset := readInt(&headerLength, record)
header := readHeader(record[offset : offset+headerLength])
offset += headerLength
offset += readInt(&dataLength, record[offset:])
data := readHeader(record[offset : offset+dataLength])
var callerID *string
var latching *bool
if v, ok := data["callerid"]; ok {
s := string(v)
callerID = &s
}
if v, ok := data["latching"]; ok {
var value bool
if string(v) == "1" {
value = true
} else {
value = false
}
latching = &value
}
return &Connection{
Conn: u32(header["conn"]),
Topic: string(header["topic"]),
Data: ConnectionHeader{
Topic: string(data["topic"]),
Type: string(data["type"]),
MD5Sum: string(data["md5sum"]),
MessageDefinition: bytes.Clone(data["message_definition"]),
CallerID: callerID,
Latching: latching,
},
}, nil
}
// ParseMessage parses a message data. The returned message data is sliced from
// the record provided. The caller should take care not to modify that record
// subsequently.
func ParseMessage(record []byte) (*Message, error) {
var headerLength, dataLength int
offset := readInt(&headerLength, record)
header := record[offset : offset+headerLength]
offset += headerLength
offset += readInt(&dataLength, record[offset:])
conn, err := GetHeaderValue(header, "conn")
if err != nil {
return nil, err
}
time, err := GetHeaderValue(header, "time")
if err != nil {
return nil, err
}
return &Message{
Conn: u32(conn),
Time: parseROSTime(time),
Data: record[offset:], // without the length prefix
}, nil
}
// ParseChunkInfo parses a chunk info record.
func ParseChunkInfo(record []byte) (*ChunkInfo, error) {
var headerLength, dataLength int
offset := readInt(&headerLength, record)
header := readHeader(record[offset : offset+headerLength])
offset += headerLength
offset += readInt(&dataLength, record[offset:])
dataEnd := offset + dataLength
data := make(map[uint32]uint32)
for offset < dataEnd {
connID := u32(record[offset:])
offset += 4
count := u32(record[offset:])
offset += 4
data[connID] = count
}
return &ChunkInfo{
ChunkPos: u64(header["chunk_pos"]),
StartTime: parseROSTime(header["start_time"]),
EndTime: parseROSTime(header["end_time"]),
Count: u32(header["count"]),
Data: data,
}, nil
}
// ParseIndexData parses an index data record.
func ParseIndexData(record []byte) (*IndexData, error) {
var headerLength int
readInt(&headerLength, record)
header := record[4 : 4+headerLength]
conn, err := GetHeaderValue(header, "conn")
if err != nil {
return nil, err
}
connID := u32(conn)
countHeader, err := GetHeaderValue(header, "count")
if err != nil {
return nil, err
}
count := u32(countHeader)
inset := 4 + headerLength + 4 // 4 skips the data length
data := []MessageIndexEntry{}
for i := 0; i < int(count); i++ {
time := parseROSTime(record[inset:])
inset += 8
offset := u32(record[inset:])
inset += 4
data = append(data, MessageIndexEntry{
Time: time,
Offset: offset,
})
}
return &IndexData{
Conn: connID,
Count: count,
Data: data,
}, nil
}
// parseROSTime converts a ROS time bytes slice to epoch nanoseconds. The ROS
// time bytes consist of little endian uint32s repreenting seconds from the
// epoch, and nanos from the second.
func parseROSTime(data []byte) uint64 {
return fromRostime(rostime(u64(data)))
}
// readHeader reads a ROS header into a map. This is mostly a convenience method
// or for usage in contexts where calls are few: in most situations it is
// cheaper to do linear lookups with readHeaderValue several times, than it is
// to build a map.
func readHeader(buf []byte) map[string][]byte {
result := make(map[string][]byte)
offset := 0
for offset < len(buf) {
fieldLength := u32(buf[offset:])
offset += 4
separatorIdx := bytes.Index(buf[offset:], []byte{'='})
key := string(buf[offset : offset+separatorIdx])
value := buf[offset+separatorIdx+1 : offset+int(fieldLength)]
result[key] = value
offset += int(fieldLength)
}
return result
}
// GetHeaderValue gets the value of a header in a ROS record header. If there is
// no header with the given key, it returns ErrKeyNotFound.
func GetHeaderValue(header []byte, key string) ([]byte, error) {
offset := 0
for offset < len(header) {
fieldLen := u32(header[offset:])
offset += 4
fieldEnd := offset + int(fieldLen)
if fieldEnd > len(header) {
return nil, ErrMalformedHeader
}
separatorIdx := bytes.Index(header[offset:], []byte("="))
fieldKey := string(header[offset : offset+separatorIdx])
offset += separatorIdx + 1
fieldValue := header[offset:fieldEnd]
if fieldKey == key {
return fieldValue, nil
}
offset = fieldEnd
}
return nil, ErrHeaderKeyNotFound{key}
}