-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparser.go
617 lines (542 loc) · 12.9 KB
/
parser.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package mail
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"strings"
"github.com/paulrosania/go-charset/charset"
_ "github.com/paulrosania/go-charset/data"
)
type parserState struct {
at int
err error
next *parserState
mark int
}
func newParserState(other *parserState) *parserState {
ps := &parserState{mark: 1}
if other != nil {
ps.at = other.at
ps.next = other
ps.mark = other.mark + 1
}
return ps
}
type parser struct {
*parserState
str string
mime bool
lc string
}
func newParser(s string) *parser {
st := newParserState(nil)
return &parser{str: s, parserState: st}
}
// Returns true if \a c belongs to the RFC 2822 'atext' production, and false
// in all other circumstances.
func isAtext(c byte) bool {
if c < 32 || c > 127 {
return false
}
if (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') {
return true
}
switch c {
case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^',
'_', '`', '{', '|', '}', '~':
return true
}
return false
}
// Moves Pos() to the first nonwhitespace character after the current point.
// If Pos() points to nonwhitespace already, it is not moved.
func (p *parser) Whitespace() string {
var out bytes.Buffer
c := p.NextChar()
for c == ' ' || c == 9 || c == 10 || c == 13 || c == 160 {
out.WriteByte(c)
p.Step(1)
c = p.NextChar()
}
return out.String()
}
// Moves Pos() past all comments and surrounding white space, and returns the
// contents of the last comment.
//
// Returns a null string if there was no comment.
func (p *parser) Comment() string {
var buf bytes.Buffer
p.Whitespace()
for p.Present("(") {
buf.Truncate(0)
commentLevel := 1
for commentLevel > 0 && !p.AtEnd() {
c := p.NextChar()
switch c {
case '(':
if commentLevel > 0 {
buf.WriteByte(c)
}
commentLevel++
case ')':
commentLevel--
if commentLevel > 0 {
buf.WriteByte(c)
}
case '\\':
p.Step(1)
buf.WriteByte(p.NextChar())
default:
buf.WriteByte(c)
}
p.Step(1)
}
p.Whitespace()
p.lc = buf.String()
}
return buf.String()
}
// Steps past an atom or a quoted-text, and returns that text.
func (p *parser) String() string {
p.Comment()
// now, treat it either as a quoted string or an unquoted atom
if p.NextChar() != '"' {
return p.Atom()
}
var buf bytes.Buffer
p.Step(1)
done := false
for !done && !p.AtEnd() {
c := p.NextChar()
p.Step(1)
if c == '"' {
done = true
} else if c == '\\' {
buf.WriteByte(p.NextChar())
p.Step(1)
} else if c == 9 || c == '\r' || c == '\n' || c == ' ' {
wsp := p.Pos() - 1
p.Whitespace()
t := p.str[wsp:p.Pos()]
if strings.ContainsAny(t, "\r\n") {
buf.WriteByte(' ')
} else {
buf.WriteString(t)
}
} else {
buf.WriteByte(c)
}
}
return buf.String()
}
// Returns a dot-atom, stepping past all relevant whitespace and comments.
func (p *parser) DotAtom() string {
result := p.Atom()
if result == "" {
return ""
}
done := false
for !done {
m := p.mark()
p.Comment()
p.require(".")
p.Comment()
a := p.Atom()
if a == "" {
p.err = errors.New("Trailing dot in dot-atom")
}
if p.Valid() {
result += "." + a
} else {
p.restore(m)
done = true
}
}
return result
}
// Returns a single atom, stepping past white space and comments before and
// after it.
func (p *parser) Atom() string {
p.Comment()
var buf bytes.Buffer
for !p.AtEnd() && isAtext(p.NextChar()) {
buf.WriteByte(p.NextChar())
p.Step(1)
}
return buf.String()
}
// Returns a single MIME token (as defined in RFC 2045 section 5), which is an
// atom minus [/?=] plus [.].
func (p *parser) MIMEToken() string {
p.Comment()
var buf bytes.Buffer
c := p.NextChar()
for c > 32 && c < 128 &&
c != '(' && c != ')' && c != '<' && c != '>' &&
c != '@' && c != ',' && c != ';' && c != ':' &&
c != '[' && c != ']' && c != '?' && c != '=' &&
c != '\\' && c != '"' && c != '/' {
buf.WriteByte(c)
p.Step(1)
c = p.NextChar()
}
return buf.String()
}
// Returns a single MIME value (as defined in RFC 2045 section 5), which is an
// atom minus [/?=] plus [.] (i.e., a MIME token) or a quoted string.
func (p *parser) MIMEValue() string {
p.Comment()
if p.NextChar() == '"' {
return p.String()
}
return p.MIMEToken()
}
type EncodedTextType int
const (
EncodedText EncodedTextType = iota
EncodedComment
EncodedPhrase
)
type EncodingType int
const (
QPEncoding EncodingType = iota
Base64Encoding
UuencodeEncoding
BinaryEncoding
)
// Steps past a MIME encoded-word (as defined in RFC 2047) and returns its
// decoded unicode representation, or an empty string if the cursor does not
// point to a valid encoded-word. The caller is responsible for checking that
// the encoded-word is separated from neighbouring tokens by whitespace.
//
// The characters permitted in the encoded-text are adjusted based on \a type,
// which may be Text (by default), Comment, or Phrase.
func (p *parser) encodedWord(t EncodedTextType) string {
// encoded-word = "=?" charset '?' encoding '?' encoded-text "?="
m := p.mark()
p.require("=?")
if !p.Valid() {
p.restore(m)
return ""
}
var csBuf bytes.Buffer
c := p.NextChar()
for c > 32 && c < 128 &&
c != '(' && c != ')' && c != '<' && c != '>' &&
c != '@' && c != ',' && c != ';' && c != ':' &&
c != '[' && c != ']' && c != '?' && c != '=' &&
c != '\\' && c != '"' && c != '/' && c != '.' {
csBuf.WriteByte(c)
p.Step(1)
c = p.NextChar()
}
cs := csBuf.String()
if strings.ContainsRune(cs, '*') {
// XXX: What should we do with the language information?
cs = section(cs, "*", 1)
}
p.require("?")
encoding := QPEncoding
if p.Present("q") {
encoding = QPEncoding
} else if p.Present("b") {
encoding = Base64Encoding
} else {
p.err = fmt.Errorf("Unknown encoding: %c", p.NextChar())
}
p.require("?")
var buf bytes.Buffer
c = p.NextChar()
if encoding == Base64Encoding {
for (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '+' || c == '/' || c == '=' {
buf.WriteByte(c)
p.Step(1)
c = p.NextChar()
}
} else {
for c > 32 && c < 128 && c != '?' &&
(t != EncodedComment ||
(c != '(' && c != ')' && c != '\\')) &&
(t != EncodedPhrase ||
(c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c == '!' || c == '*' || c == '-' || c == '/' ||
c == '=' || c == '_' || c == '\'' ||
// this is non-standard, but some encoders don't encode dots :(
c == '.')) {
buf.WriteByte(c)
p.Step(1)
c = p.NextChar()
}
}
p.require("?=")
if !p.Valid() {
p.restore(m)
return ""
}
text := buf.String()
if encoding == QPEncoding {
text = deQP(text, false)
} else {
text = de64(text)
}
r := strings.NewReader(text)
cr, err := charset.NewReader(cs, r)
if err != nil {
// XXX: Should we treat unknown charsets as us-ascii?
p.err = fmt.Errorf("Unknown character set: %s", cs)
p.restore(m)
return ""
}
bs, err := ioutil.ReadAll(cr)
if err != nil {
p.err = err
p.restore(m)
return ""
}
result := string(bs)
if strings.ContainsAny(result, "\r\n") {
result = simplify(result) // defend against =?ascii?q?x=0aEvil:_nasty?=
}
if strings.IndexByte(result, 8) >= 0 { // we interpret literal DEL. fsck.
i := 0
for i >= 0 {
i = strings.IndexByte(result[i:], 8)
if i >= 0 {
s := result[i+1:]
if i > 1 {
result = result[:i-1] + s
} else {
result = s
}
}
}
}
return result
}
// Steps past a sequence of adjacent encoded-words with whitespace in between
// and returns the decoded representation. \a t passed through to
// encodedWord().
//
// Leading and trailing whitespace is trimmed, internal whitespace is kept as
// is.
func (p *parser) encodedWords(t EncodedTextType) string {
var out bytes.Buffer
end := false
var m int
for !end {
m = p.mark()
p.Whitespace()
n := p.Pos()
s := p.encodedWord(t)
if n == p.Pos() {
end = true
} else {
out.WriteString(s)
}
}
p.restore(m)
return trim(out.String())
}
func (p *parser) Text() string {
var out bytes.Buffer
space := p.Whitespace()
var word string
progress := true
for progress {
m := p.mark()
start := p.Pos()
encodedWord := false
if p.Present("=?") {
p.restore(m)
encodedWord = true
word = p.encodedWords(EncodedText)
if p.Pos() == start {
encodedWord = false
}
}
if !encodedWord {
var buf bytes.Buffer
c := p.NextChar()
for !p.AtEnd() && c < 128 && c != ' ' && c != 9 && c != 10 && c != 13 {
buf.WriteByte(c)
p.Step(1)
c = p.NextChar()
}
word = buf.String()
}
if p.Pos() == start {
progress = false
} else {
out.WriteString(space)
out.WriteString(word)
space = p.Whitespace()
if strings.ContainsAny(space, "\r\n") {
space = " "
}
}
}
if len(space) != 0 {
out.WriteString(space)
}
return out.String()
}
// Steps past an RFC 822 phrase (a series of word/encoded-words) at the cursor
// and returns its unicode representation, which may be an empty string.
func (p *parser) Phrase() string {
var buf bytes.Buffer
p.Comment()
wasEncoded := false
spaces := ""
progress := true
for !p.AtEnd() && progress {
t := ""
encoded := false
h := false
start := p.Pos()
m := p.mark()
if p.Present("=?") {
p.restore(m)
t = p.encodedWords(EncodedPhrase)
if start < p.Pos() {
h = true
encoded = true
}
}
if !h && p.Present("\"") {
p.restore(m)
t, _ = decode(p.String(), "us-ascii")
if start < p.Pos() {
h = true
}
}
if !h {
t, _ = decode(p.Atom(), "us-ascii")
if start < p.Pos() {
h = true
}
}
if h || t != "" {
// we did read something, so we need to add it to the
// previous word(s).
// first, append the spaces before the word we added. RFC
// 2047 says that spaces between encoded-words should be
// disregarded, so we do.
if !encoded || !wasEncoded {
buf.WriteString(spaces)
}
// next append the word we read
buf.WriteString(t)
// then read new spaces which we'll use if there is
// another word.
spaces = p.Whitespace()
start := p.Pos()
p.Comment()
// if there weren't any spaces, but there is a comment,
// then we need to treat the comment as a single space.
if spaces == "" && start < p.Pos() {
spaces += " "
}
// RFC violation: if the spaces included a CR/LF, we
// properly should just get rid of the CRLF and one
// trailing SP, but changing it all to a single space
// matches the expectations of most senders better.
if strings.ContainsAny(spaces, "\r\n") {
spaces = " "
}
wasEncoded = encoded
} else {
progress = false
}
}
return buf.String()
}
// Returns the current (0-indexed) position of the cursor in the input() string
// without changing anything.
func (p *parser) Pos() int {
return p.at
}
// Returns the next character at the cursor without changing the cursor
// position. Returns 0 if there isn't a character available (e.g. when the
// cursor is past the end of the input string).
func (p *parser) NextChar() uint8 {
if p.at >= len(p.str) {
return 0
} else {
return p.str[p.at]
}
}
// Advances the cursor past n characters of the input.
func (p *parser) Step(n int) {
p.at += n
}
// Checks whether the next characters in the input match s. If so, Present()
// steps past the matching characters and returns true. If not, it returns
// false without advancing the cursor. The match is case insensitive.
func (p *parser) Present(s string) bool {
if s == "" {
return true
}
if p.at+len(s) > len(p.str) {
return false
}
l := strings.ToLower(p.str[p.at : p.at+len(s)])
s = strings.ToLower(s)
if l != s {
return false
}
p.Step(len(s))
return true
}
// Requires that the next characters in the input match \a s (case
// insensitively), and steps past the matching characters. If \a s is not
// present(), it is considered an error().
func (p *parser) require(s string) {
if !p.Present(s) {
p.err = fmt.Errorf("Expected: %q, got: %s", s, p.following())
}
}
// Returns a string of no more than 15 characters containing the first unparsed
// bits of input. Meant for use in error messages.
func (p *parser) following() string {
if p.at >= len(p.str) {
return ""
}
f := p.str[p.at:]
if len(f) > 15 {
f = f[:15]
}
return simplify(f)
}
// Returns true if we have parsed the entire input string, and false otherwise.
func (p *parser) AtEnd() bool {
return p.at >= len(p.str)
}
// Saves the current cursor position and error state of the parser and returns
// an identifier of the current mark. The companion function restore() restores
// the last or a specified mark. The returned mark is never 0.
func (p *parser) mark() int {
p.parserState = newParserState(p.parserState)
return p.next.mark
}
// Restores the last mark()ed cursor position and error state of this parser
// object.
func (p *parser) restore(m int) {
c := p.parserState
for c != nil && c.mark != m && c.next != nil {
c = c.next
}
if c != nil && c.mark == m {
p.parserState = c
}
}
func (p *parser) Valid() bool {
return p.err == nil
}