Debian Codegraph @

parsercommon/parsercommon.go

raw symbols SCIP
1 package parsercommon
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 )
8
9 const (
10 NO_VERSION = -1
11 )
12
13 var (
14 ErrEOL = &ParserError{"End of log line"}
15 ErrNoSpace = &ParserError{"No space found"}
16
17 ErrPriorityNoStart = &ParserError{"No start char found for priority"}
18 ErrPriorityEmpty = &ParserError{"Priority field empty"}
19 ErrPriorityNoEnd = &ParserError{"No end char found for priority"}
20 ErrPriorityTooShort = &ParserError{"Priority field too short"}
21 ErrPriorityTooLong = &ParserError{"Priority field too long"}
22 ErrPriorityNonDigit = &ParserError{"Non digit found in priority"}
23
24 ErrVersionNotFound = &ParserError{"Can not find version"}
25
26 ErrTimestampUnknownFormat = &ParserError{"Timestamp format unknown"}
27
28 ErrHostnameNotFound = &ParserError{"Hostname not found"}
29 )
30
31 type ParserError struct {
32 ErrorString string
33 }
34
35 type Priority struct {
36 P int
37 F Facility
38 S Severity
39 }
40
41 type Facility struct {
42 Value int
43 }
44
45 type Severity struct {
46 Value int
47 }
48
49 // https://tools.ietf.org/html/rfc3164#section-4.1
50 func ParsePriority(buff []byte, cursor *int, l int) (*Priority, error) {
51 if l <= 0 {
52 return nil, ErrPriorityEmpty
53 }
54
55 if buff[*cursor] != '<' {
56 return nil, ErrPriorityNoStart
57 }
58
59 i := 1
60 priDigit := 0
61
62 for i < l {
63 if i >= 5 {
64 return nil, ErrPriorityTooLong
65 }
66
67 c := buff[i]
68
69 if c == '>' {
70 if i == 1 {
71 return nil, ErrPriorityTooShort
72 }
73
74 *cursor = i + 1
75
76 return NewPriority(priDigit), nil
77 }
78
79 if IsDigit(c) {
80 v, e := strconv.Atoi(string(c))
81 if e != nil {
82 return nil, e
83 }
84
85 priDigit = (priDigit * 10) + v
86 } else {
87 return nil, ErrPriorityNonDigit
88 }
89
90 i++
91 }
92
93 return nil, ErrPriorityNoEnd
94 }
95
96 // https://tools.ietf.org/html/rfc5424#section-6.2.2
97 func ParseVersion(buff []byte, cursor *int, l int) (int, error) {
98 if *cursor >= l {
99 return NO_VERSION, ErrVersionNotFound
100 }
101
102 c := buff[*cursor]
103 *cursor++
104
105 // XXX : not a version, not an error though as RFC 3164 does not support it
106 if !IsDigit(c) {
107 return NO_VERSION, nil
108 }
109
110 v, e := strconv.Atoi(string(c))
111 if e != nil {
112 *cursor--
113 return NO_VERSION, e
114 }
115
116 return v, nil
117
118 }
119
120 func IsDigit(c byte) bool {
121 return c >= '0' && c <= '9'
122 }
123
124 func NewPriority(p int) *Priority {
125 // The Priority value is calculated by first multiplying the Facility
126 // number by 8 and then adding the numerical value of the Severity.
127
128 return &Priority{
129 P: p,
130 F: Facility{Value: p / 8},
131 S: Severity{Value: p % 8},
132 }
133 }
134
135 func FindNextSpace(buff []byte, from int, l int) (int, error) {
136 var to int
137
138 for to = from; to < l; to++ {
139 if buff[to] == ' ' {
140 to++
141 return to, nil
142 }
143 }
144
145 return 0, ErrNoSpace
146 }
147
148 func Parse2Digits(buff []byte, cursor *int, l int, min int, max int, e error) (int, error) {
149 digitLen := 2
150
151 if *cursor+digitLen > l {
152 return 0, ErrEOL
153 }
154
155 sub := string(buff[*cursor : *cursor+digitLen])
156
157 *cursor += digitLen
158
159 i, err := strconv.Atoi(sub)
160 if err != nil {
161 return 0, e
162 }
163
164 if i >= min && i <= max {
165 return i, nil
166 }
167
168 return 0, e
169 }
170
171 func ParseHostname(buff []byte, cursor *int, l int) (string, error) {
172 from := *cursor
173 var to int
174
175 for to = from; to < l; to++ {
176 if buff[to] == ' ' {
177 break
178 }
179 }
180
181 hostname := buff[from:to]
182
183 *cursor = to
184
185 return string(hostname), nil
186 }
187
188 func ShowCursorPos(buff []byte, cursor int) {
189 fmt.Println(string(buff))
190 padding := strings.Repeat("-", cursor)
191 fmt.Println(padding + "↑\n")
192 }
193
194 func (err *ParserError) Error() string {
195 return err.ErrorString
196 }