1
2
3
4
5
6
7 package quotedprintable
8
9 import (
10 "bufio"
11 "bytes"
12 "fmt"
13 "io"
14 )
15
16
17 type Reader struct {
18 br *bufio.Reader
19 rerr error
20 line []byte
21 }
22
23
24 func NewReader(r io.Reader) *Reader {
25 return &Reader{
26 br: bufio.NewReader(r),
27 }
28 }
29
30 func fromHex(b byte) (byte, error) {
31 switch {
32 case b >= '0' && b <= '9':
33 return b - '0', nil
34 case b >= 'A' && b <= 'F':
35 return b - 'A' + 10, nil
36
37 case b >= 'a' && b <= 'f':
38 return b - 'a' + 10, nil
39 }
40 return 0, fmt.Errorf("quotedprintable: invalid hex byte 0x%02x", b)
41 }
42
43 func readHexByte(v []byte) (b byte, err error) {
44 if len(v) < 2 {
45 return 0, io.ErrUnexpectedEOF
46 }
47 var hb, lb byte
48 if hb, err = fromHex(v[0]); err != nil {
49 return 0, err
50 }
51 if lb, err = fromHex(v[1]); err != nil {
52 return 0, err
53 }
54 return hb<<4 | lb, nil
55 }
56
57 func isQPDiscardWhitespace(r rune) bool {
58 switch r {
59 case '\n', '\r', ' ', '\t':
60 return true
61 }
62 return false
63 }
64
65 var (
66 crlf = []byte("\r\n")
67 lf = []byte("\n")
68 softSuffix = []byte("=")
69 )
70
71
72 func (r *Reader) Read(p []byte) (n int, err error) {
73
74
75
76
77
78
79
80
81
82 for len(p) > 0 {
83 if len(r.line) == 0 {
84 if r.rerr != nil {
85 return n, r.rerr
86 }
87 r.line, r.rerr = r.br.ReadSlice('\n')
88
89
90 hasLF := bytes.HasSuffix(r.line, lf)
91 hasCR := bytes.HasSuffix(r.line, crlf)
92 wholeLine := r.line
93 r.line = bytes.TrimRightFunc(wholeLine, isQPDiscardWhitespace)
94 if bytes.HasSuffix(r.line, softSuffix) {
95 rightStripped := wholeLine[len(r.line):]
96 r.line = r.line[:len(r.line)-1]
97 if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) &&
98 !(len(rightStripped) == 0 && len(r.line) > 0 && r.rerr == io.EOF) {
99 r.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
100 }
101 } else if hasLF {
102 if hasCR {
103 r.line = append(r.line, '\r', '\n')
104 } else {
105 r.line = append(r.line, '\n')
106 }
107 }
108 continue
109 }
110 b := r.line[0]
111
112 switch {
113 case b == '=':
114 b, err = readHexByte(r.line[1:])
115 if err != nil {
116 if len(r.line) >= 2 && r.line[1] != '\r' && r.line[1] != '\n' {
117
118 b = '='
119 break
120 }
121 return n, err
122 }
123 r.line = r.line[2:]
124 case b == '\t' || b == '\r' || b == '\n':
125 break
126 case b >= 0x80:
127
128
129 break
130 case b < ' ' || b > '~':
131 return n, fmt.Errorf("quotedprintable: invalid unescaped byte 0x%02x in body", b)
132 }
133 p[0] = b
134 p = p[1:]
135 r.line = r.line[1:]
136 n++
137 }
138 return n, nil
139 }
140
View as plain text