-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
303 lines (262 loc) · 7.36 KB
/
header.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
// This file defines encoding and decoding functions for encoded-words
// as defined in RFC 2047.
package qp
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"regexp"
"strings"
"unicode/utf8"
)
const (
// Q represents the Q-encoding defined in RFC 2047.
Q = "Q"
// B represents the Base64 encoding defined in RFC 2045.
B = "B"
)
// WordEncoder in an encoder for encoded words.
type WordEncoder struct {
charset string
encoding string
splitWords bool
}
const maxEncodedWordLen = 75 // As defined in RFC 2047, section 2
// StdWordEncoder is a RFC 2047 encoder for UTF-8 strings using Q encoding.
var StdWordEncoder = &WordEncoder{"UTF-8", Q, true}
// NewWordEncoder returns a new WordEncoder to encode strings in the
// specified charset using the encoding enc.
func NewWordEncoder(charset string, enc string) (*WordEncoder, error) {
if strings.ToUpper(enc) != Q && strings.ToUpper(enc) != B {
return nil, fmt.Errorf("qp: RFC 2047 encoding not supported: %q", enc)
}
// We automatically split encoded-words only when the charset is UTF-8
// because since multi-octet character must not be split across adjacent
// encoded-words (see RFC 2047, section 5) there is no way to split words
// without knowing how the charset works.
splitWords := strings.ToUpper(charset) == "UTF-8"
return &WordEncoder{charset, enc, splitWords}, nil
}
// EncodeWord encodes a string into an encoded-word.
func (e *WordEncoder) EncodeWord(s string) string {
buf := new(bytes.Buffer)
e.openWord(buf)
if strings.ToUpper(e.encoding) == B {
maxLen := maxEncodedWordLen - buf.Len() - 2
if !e.splitWords || base64.StdEncoding.EncodedLen(len(s)) <= maxLen {
buf.WriteString(base64.StdEncoding.EncodeToString([]byte(s)))
} else {
var n, last, runeSize int
for i := 0; i < len(s); i += runeSize {
runeSize = getRuneSize(s, i)
if base64.StdEncoding.EncodedLen(n+runeSize) <= maxLen {
n += runeSize
} else {
buf.WriteString(base64.StdEncoding.EncodeToString([]byte(s[last:i])))
e.splitWord(buf)
last = i
n = runeSize
}
}
buf.WriteString(base64.StdEncoding.EncodeToString([]byte(s[last:])))
}
} else {
if !e.splitWords {
for i := 0; i < len(s); i++ {
writeQ(buf, s[i])
}
} else {
var runeSize int
n := buf.Len()
for i := 0; i < len(s); i += runeSize {
b := s[i]
var encLen int
if b == ' ' || (isVchar(b) && b != '=' && b != '?' && b != '_') {
encLen, runeSize = 1, 1
} else {
runeSize = getRuneSize(s, i)
encLen = 3 * runeSize
}
// We remove 2 to let spaces for closing chars "?="
if n+encLen > maxEncodedWordLen-2 {
n = e.splitWord(buf)
}
writeQString(buf, s[i:i+runeSize])
n += encLen
}
}
}
e.closeWord(buf)
return buf.String()
}
func (e *WordEncoder) openWord(buf *bytes.Buffer) int {
buf.WriteString("=?")
buf.WriteString(e.charset)
buf.WriteByte('?')
buf.WriteString(e.encoding)
buf.WriteByte('?')
return 4 + len(e.charset) + len(e.encoding)
}
func (e *WordEncoder) closeWord(buf *bytes.Buffer) {
buf.WriteString("?=")
}
func (e *WordEncoder) splitWord(buf *bytes.Buffer) int {
e.closeWord(buf)
buf.WriteString("\r\n ")
return e.openWord(buf)
}
func getRuneSize(s string, i int) int {
runeSize := 1
for i+runeSize < len(s) && !utf8.RuneStart(s[i+runeSize]) {
runeSize++
}
return runeSize
}
func writeQString(buf *bytes.Buffer, s string) {
for i := 0; i < len(s); i++ {
writeQ(buf, s[i])
}
}
func writeQ(buf *bytes.Buffer, b byte) {
switch {
case b == ' ':
buf.WriteByte('_')
case isVchar(b) && b != '=' && b != '?' && b != '_':
buf.WriteByte(b)
default:
enc := make([]byte, 3)
encodeByte(enc[0:3], b)
buf.Write(enc)
}
}
// EncodeHeader encodes a string to be used as a MIME header value. It works
// like EncodeWord but it encodes the input only if it contains non-ASCII
// characters.
func (e *WordEncoder) EncodeHeader(s string) string {
if !needsEncoding(s) {
return s
}
return e.EncodeWord(s)
}
func needsEncoding(s string) bool {
for i := 0; i < len(s); i++ {
if !isVchar(s[i]) && !isWSP(s[i]) {
return true
}
}
return false
}
// DecodeWord decodes an encoded-word returning the decoded text and the
// charset. This function does not do any charset conversion, the returned text
// is encoded in the returned charset. So text is not necessarily encoded in
// UTF-8.
func DecodeWord(s string) (text string, charset string, err error) {
dec, charset, err := decodeWord(s)
return string(dec), charset, err
}
func decodeWord(s string) (text []byte, charset string, err error) {
fields := strings.Split(s, "?")
if len(fields) != 5 || fields[0] != "=" || fields[4] != "=" || len(fields[2]) != 1 {
return []byte(s), "", nil
}
charset, enc, src := fields[1], fields[2], fields[3]
var dec []byte
switch strings.ToUpper(enc) {
case B:
if dec, err = base64.StdEncoding.DecodeString(src); err != nil {
return dec, charset, err
}
case Q:
if dec, err = qDecode(src); err != nil {
return dec, charset, err
}
default:
return []byte(""), charset, fmt.Errorf("qp: RFC 2047 encoding not supported: %q", enc)
}
return dec, charset, nil
}
// qDecode decodes a Q encoded string.
func qDecode(s string) ([]byte, error) {
dec := make([]byte, MaxDecodedLen(len(s)))
n := 0
for i := 0; i < len(s); i++ {
switch c := s[i]; {
case c == '_':
dec[n] = ' '
case c == '=':
if i+2 >= len(s) {
return dec[:n], io.ErrUnexpectedEOF
}
buf, err := readHexByte([]byte(s[i+1:]))
if err != nil {
return dec[:n], err
}
dec[n] = buf
i += 2
case isVchar(c) || c == ' ' || c == '\n' || c == '\r' || c == '\t':
dec[n] = c
default:
return dec[:n], fmt.Errorf("qp: invalid unescaped byte 0x%02x in Q encoded string", c)
}
n++
}
return dec[:n], nil
}
// DecodeHeader decodes a MIME header by decoding all encoded-words of the
// header. This function does not do any charset conversion, the returned text
// is encoded in the returned charset. So text is not necessarily encoded in
// UTF-8. As such, this function does not support decoding headers with multiple
// encoded-words using different charsets.
func DecodeHeader(header string) (text string, charset string, err error) {
var buf bytes.Buffer
for {
i := strings.IndexByte(header, '=')
if i == -1 {
break
}
if i > 0 {
buf.WriteString(header[:i])
header = header[i:]
}
word := rfc2047.FindString(header)
if word == "" {
buf.WriteByte('=')
header = header[1:]
continue
}
for {
dec, wordCharset, err := decodeWord(word)
if err != nil {
buf.WriteString(word)
header = header[len(word):]
break
}
if charset == "" {
charset = wordCharset
} else if charset != wordCharset {
return "", "", fmt.Errorf("qp: multiple charsets in header are not supported: %q and %q used", charset, wordCharset)
}
buf.Write(dec)
header = header[len(word):]
// White-space and newline characters separating two encoded-words
// must be deleted.
var j int
for j = 0; j < len(header) && (isWSP(header[j]) || isNewline(header[j])); j++ {
}
if j == 0 {
// If there are no white-space characters following the current
// encoded-word there is nothing special to do.
break
}
word = rfc2047.FindString(header[j:])
if word == "" {
break
}
header = header[j:]
}
}
buf.WriteString(header)
return buf.String(), charset, nil
}
var rfc2047 = regexp.MustCompile(`^=\?[\w\-]+\?[bBqQ]\?[^?]+\?=`)