-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
221 lines (181 loc) · 4.88 KB
/
decoder.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
212
213
214
215
216
217
218
219
220
221
package splunker
import (
"encoding/binary"
"io"
"log"
"github.com/fionera/splunker/varint"
)
type Header struct {
Version byte
AlignBits byte
BaseIndexTime int32
}
func (jd *JournalDecoder) headerDecoder(r *CountedReader, o byte) error {
var h Header
if err := binary.Read(r, binary.LittleEndian, &h); err != nil {
return err
}
log.Printf("Journal %s - Version: %d", jd.n, h.Version)
alignMask := (1 << h.AlignBits) - 1
_ = alignMask //TODO
return nil
}
// splunkPrivateDecoder is the decoder for OpcodeSplunkPrivate
func (jd *JournalDecoder) splunkPrivateDecoder(r *CountedReader, o byte) error {
l, err := binary.ReadUvarint(r)
if err != nil {
return err
}
if _, err := io.CopyN(io.Discard, r, int64(l)); err != nil {
return err
}
return nil
}
func (jd *JournalDecoder) stringFieldDecoder(r *CountedReader) (string, error) {
l, err := binary.ReadUvarint(r)
if err != nil {
return "", err
}
return readString(r, l)
}
// hostDecoder is the decoder for OpcodeNewHost
func (jd *JournalDecoder) hostDecoder(r *CountedReader, o byte) error {
s, err := jd.stringFieldDecoder(r)
if err != nil {
return err
}
jd.s.fields[o] = append(jd.s.fields[o], s)
return nil
}
// sourceDecoder is the decoder for OpcodeNewSource
func (jd *JournalDecoder) sourceDecoder(r *CountedReader, o byte) error {
s, err := jd.stringFieldDecoder(r)
if err != nil {
return err
}
jd.s.fields[o] = append(jd.s.fields[o], s)
return nil
}
// sourceTypeDecoder is the decoder for OpcodeNewSourceType
func (jd *JournalDecoder) sourceTypeDecoder(r *CountedReader, o byte) error {
s, err := jd.stringFieldDecoder(r)
if err != nil {
return err
}
jd.s.fields[o] = append(jd.s.fields[o], s)
return nil
}
// stringDecoder is the decoder for OpcodeNewString
func (jd *JournalDecoder) stringDecoder(r *CountedReader, o byte) error {
s, err := jd.stringFieldDecoder(r)
if err != nil {
return err
}
jd.s.fields[o] = append(jd.s.fields[o], s)
return nil
}
// read the data for the following values
// messageLength, streamID, eStorageLen, streamID, streamOffset, streamSubOffset, indexTime, subSeconds, metadataCount
const eventInfoSize = 8*binary.MaxVarintLen64 + decBufSize + hashSize
// eventDecoder is the decoder for OpcodeOldstyleEventWithHash, OpcodeOldstyleEvent
func (jd *JournalDecoder) eventDecoder(r *CountedReader, o byte) (err error) {
var peekOffset, n int
peek, err := jd.cr.Peek(eventInfoSize)
if err != nil {
return err
}
jd.e.messageLength, n = varint.Uvarint(peek[peekOffset:])
peekOffset += n
if err != nil {
return err
}
// add our current position to the message length
// after decoding metadata of the event, the new position will
// be subtracted
jd.e.messageLength += uint64(r.pos) + uint64(peekOffset)
var eStorageLen uint64
if jd.e.hasExtendedStorage = o&0x4 != 0; jd.e.hasExtendedStorage {
jd.e.extendedStorageLen, n = varint.Uvarint(peek[peekOffset:])
peekOffset += n
if err != nil {
return err
}
}
if jd.e.hasHash = o&0x01 == 0; jd.e.hasHash {
copy(jd.e.hash[:], peek[peekOffset:])
peekOffset += hashSize
}
jd.e.streamID = binary.LittleEndian.Uint64(peek[peekOffset:])
peekOffset += decBufSize
jd.e.streamOffset, n = varint.Uvarint(peek[peekOffset:])
peekOffset += n
if err != nil {
return err
}
jd.e.streamSubOffset, n = varint.Uvarint(peek[peekOffset:])
peekOffset += n
if err != nil {
return err
}
jd.e.indexTime, n = varint.Varint(peek[peekOffset:]) // TODO: +basetime
peekOffset += n
if err != nil {
return err
}
// Add the current baseTime
jd.e.indexTime += int64(jd.s.baseTime)
jd.e.subSeconds, n = varint.Uvarint(peek[peekOffset:])
peekOffset += n
if err != nil {
return err
}
jd.e.metadataCount, n = varint.Uvarint(peek[peekOffset:])
peekOffset += n
if err != nil {
return err
}
_, err = jd.cr.Discard(peekOffset)
if err != nil {
return err
}
// read all into a buffer to prevent tons of ReadByte calls
// per entry: 1
// max highest int needed: 3
// max var int size: MaxVarintLen64
peek, err = r.Peek(4 * binary.MaxVarintLen64 * int(jd.e.metadataCount))
if err != nil {
return err
}
peekOffset = 0
for i := 0; i < int(jd.e.metadataCount); i++ {
n, err := readMetadata(peek[peekOffset:], o)
if err != nil {
return err
}
peekOffset += n
}
if _, err := r.Discard(peekOffset); err != nil {
return err
}
if jd.e.hasExtendedStorage {
eStorage, err := readString(r, eStorageLen)
if err != nil {
return err
}
log.Fatal(eStorage)
}
jd.e.messageLength = jd.e.messageLength - uint64(r.pos)
if cap(jd.e.message) < int(jd.e.messageLength) {
// create a new byte slice double the size
// that way we reduce re allocating the slice
jd.e.message = make([]byte, jd.e.messageLength*2)
}
jd.e.message = jd.e.message[:jd.e.messageLength]
// read the actual message
_, err = r.Read(jd.e.message)
if err != nil {
return err
}
jd.e.includePunctuation = (o & 0x22) == 34
return nil
}