@@ -15,17 +15,31 @@ import (
1515 variable pe pe;
1616}%%
1717
18+ type cefState struct {
19+ key string // Extension key.
20+ valueStart int // Start index of extension value.
21+ valueEnd int // End index of extension value.
22+ escapes []escapePosition // Array of escapes indices within the current value.
23+ }
24+
25+ func (s *cefState) reset() {
26+ s.key = ""
27+ s.valueStart = 0
28+ s.valueEnd = 0
29+ s.escapes = s.escapes[:0]
30+ }
31+
32+ func (s *cefState) pushEscape(start, end int) {
33+ s.escapes = append(s.escapes, escapePosition{start, end})
34+ }
35+
1836// unpack unpacks a CEF message.
1937func (e *Event) unpack(data string) error {
2038 cs, p, pe, eof := 0, 0, len(data), len(data)
2139 mark, mark_slash := 0, 0
22- var escapes []int
23-
24- // Extension key.
25- var extKey string
2640
27- // Extension value start and end indices .
28- extValueStart, extValueEnd := 0, 0
41+ // state related to CEF values .
42+ var state cefState
2943
3044 // recoveredErrs are problems with the message that the parser was able to
3145 // recover from (though the parsing might not be "correct").
@@ -42,62 +56,62 @@ func (e *Event) unpack(data string) error {
4256 mark_slash = p
4357 }
4458 action mark_escape {
45- escapes = append(escapes, mark_slash, p)
59+ state.pushEscape( mark_slash, p)
4660 }
4761 action version {
4862 e.Version, _ = strconv.Atoi(data[mark:p])
4963 }
5064 action device_vendor {
51- e.DeviceVendor = replaceEscapes(data[mark:p], mark, escapes)
52- escapes = escapes[:0]
65+ e.DeviceVendor = replaceEscapes(data[mark:p], mark, state. escapes)
66+ state.reset()
5367 }
5468 action device_product {
55- e.DeviceProduct = replaceEscapes(data[mark:p], mark, escapes)
56- escapes = escapes[:0]
69+ e.DeviceProduct = replaceEscapes(data[mark:p], mark, state. escapes)
70+ state.reset()
5771 }
5872 action device_version {
59- e.DeviceVersion = replaceEscapes(data[mark:p], mark, escapes)
60- escapes = escapes[:0]
73+ e.DeviceVersion = replaceEscapes(data[mark:p], mark, state. escapes)
74+ state.reset()
6175 }
6276 action device_event_class_id {
63- e.DeviceEventClassID = replaceEscapes(data[mark:p], mark, escapes)
64- escapes = escapes[:0]
77+ e.DeviceEventClassID = replaceEscapes(data[mark:p], mark, state. escapes)
78+ state.reset()
6579 }
6680 action name {
67- e.Name = replaceEscapes(data[mark:p], mark, escapes)
68- escapes = escapes[:0]
81+ e.Name = replaceEscapes(data[mark:p], mark, state. escapes)
82+ state.reset()
6983 }
7084 action severity {
7185 e.Severity = data[mark:p]
7286 }
7387 action extension_key {
7488 // A new extension key marks the end of the last extension value.
75- if len(extKey ) > 0 && extValueStart <= mark - 1 {
76- e.pushExtension(extKey , replaceEscapes(data[extValueStart :mark-1], extValueStart, escapes))
77- extKey, extValueStart, extValueEnd, escapes = "", 0, 0, escapes[:0]
89+ if len(state.key ) > 0 && state.valueStart <= mark - 1 {
90+ e.pushExtension(state.key , replaceEscapes(data[state.valueStart :mark-1], state.valueStart, state. escapes))
91+ state.reset()
7892 }
79- extKey = data[mark:p]
93+ state.key = data[mark:p]
8094 }
8195 action extension_value_start {
82- extValueStart = p;
83- extValueEnd = p
96+ state.valueStart = p;
97+ state.valueEnd = p
8498 }
8599 action extension_value_mark {
86- extValueEnd = p+1
100+ state.valueEnd = p+1
87101 }
88102 action extension_eof {
89103 // Reaching the EOF marks the end of the final extension value.
90- if len(extKey ) > 0 && extValueStart <= extValueEnd {
91- e.pushExtension(extKey , replaceEscapes(data[extValueStart:extValueEnd ], extValueStart, escapes))
92- extKey, extValueStart, extValueEnd, escapes = "", 0, 0, escapes[:0]
104+ if len(state.key ) > 0 && state.valueStart <= state.valueEnd {
105+ e.pushExtension(state.key , replaceEscapes(data[state.valueStart:state.valueEnd ], state.valueStart, state. escapes))
106+ state.reset()
93107 }
94108 }
95109 action extension_err {
96- recoveredErrs = append(recoveredErrs, fmt.Errorf("malformed value for %s at pos %d", extKey , p+1))
110+ recoveredErrs = append(recoveredErrs, fmt.Errorf("malformed value for %s at pos %d", state.key , p+1))
97111 fhold; fnext gobble_extension;
98112 }
99113 action recover_next_extension {
100- extKey, extValueStart, extValueEnd, escapes = "", 0, 0, escapes[:0]
114+ state.reset()
101115 // Resume processing at p, the start of the next extension key.
102116 p = mark;
103117 fnext extensions;
0 commit comments