-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdecoder.go
227 lines (195 loc) · 4.04 KB
/
decoder.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
package vcard
import (
"bufio"
"errors"
"io"
"strconv"
"strings"
)
// A Decoder parses cards.
type Decoder struct {
r *bufio.Reader
}
// NewDecoder creates a new Decoder reading cards from an io.Reader.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: bufio.NewReader(r)}
}
func (dec *Decoder) readLine() (string, error) {
l, err := dec.r.ReadString('\n')
l = strings.TrimRight(l, "\r\n")
if len(l) > 0 && err == io.EOF {
return l, nil
} else if err != nil {
return l, err
}
for {
next, err := dec.r.Peek(1)
if err == io.EOF {
break
} else if err != nil {
return l, err
}
if ch := next[0]; ch != ' ' && ch != '\t' {
break
}
if _, err := dec.r.Discard(1); err != nil {
return l, err
}
folded, err := dec.r.ReadString('\n')
if err != nil {
return l, err
}
l += strings.TrimRight(folded, "\r\n")
}
return l, nil
}
// Decode parses a single card.
func (dec *Decoder) Decode() (Card, error) {
card := make(Card)
var hasBegin, hasEnd bool
for {
l, err := dec.readLine()
if err == io.EOF {
break
} else if err != nil {
return card, err
}
k, f, err := parseLine(l)
if err != nil {
continue
}
if !hasBegin {
if k == "BEGIN" {
if strings.ToUpper(f.Value) != "VCARD" {
return card, errors.New("vcard: invalid BEGIN value")
}
hasBegin = true
continue
} else {
return card, errors.New("vcard: no BEGIN field found")
}
} else if k == "END" {
if strings.ToUpper(f.Value) != "VCARD" {
return card, errors.New("vcard: invalid END value")
}
hasEnd = true
break
}
card[k] = append(card[k], f)
}
if !hasEnd {
if !hasBegin {
return nil, io.EOF
}
return card, errors.New("vcard: no END field found")
}
return card, nil
}
func parseLine(l string) (key string, field *Field, err error) {
field = new(Field)
field.Group, l = parseGroup(l)
key, hasParams, l, err := parseKey(l)
if err != nil {
return
}
if hasParams {
field.Params, l, err = parseParams(l)
if err != nil {
return
}
}
field.Value = parseValue(l)
return
}
func parseGroup(s string) (group, tail string) {
i := strings.IndexAny(s, ".;:")
if i < 0 || s[i] != '.' {
return "", s
}
return s[:i], s[i+1:]
}
func parseKey(s string) (key string, params bool, tail string, err error) {
i := strings.IndexAny(s, ";:")
if i < 0 {
err = errors.New("vcard: invalid property key")
return
}
return strings.ToUpper(s[:i]), s[i] == ';', s[i+1:], nil
}
func parseParams(s string) (params Params, tail string, err error) {
tail = s
params = make(Params)
for tail != "" {
i := strings.IndexAny(tail, "=;:")
if i < 0 {
err = errors.New("vcard: malformed parameters")
return
}
if tail[i] == ';' {
tail = tail[i+1:]
continue
}
k := strings.ToUpper(tail[:i])
var values []string
var more bool
values, more, tail, err = parseParamValues(tail[i+1:])
if err != nil {
return
}
params[k] = append(params[k], values...)
if !more {
break
}
}
return
}
func parseParamValues(s string) (values []string, more bool, tail string, err error) {
if s == "" {
return
}
quote := s[0]
var vs string
if quote == '"' {
vs, tail, err = parseQuoted(s[1:], quote)
if tail == "" || (tail[0] != ';' && tail[0] != ':') {
err = errors.New("vcard: malformed quoted parameter value")
return
}
more = tail[0] != ':'
tail = tail[1:]
} else {
i := strings.IndexAny(s, ";:")
if i < 0 {
vs = s
} else {
vs, more, tail = s[:i], s[i] != ':', s[i+1:]
}
}
values = strings.Split(vs, ",")
for i, value := range values {
values[i] = parseValue(value)
}
return
}
func parseQuoted(s string, quote byte) (value, tail string, err error) {
tail = s
var buf []rune
for tail != "" {
if tail[0] == quote {
tail = tail[1:]
break
}
var r rune
r, _, tail, err = strconv.UnquoteChar(tail, quote)
if err != nil {
return
}
buf = append(buf, r)
}
value = string(buf)
return
}
var valueParser = strings.NewReplacer("\\\\", "\\", "\\n", "\n", "\\,", ",")
func parseValue(s string) string {
return valueParser.Replace(s)
}