-
Notifications
You must be signed in to change notification settings - Fork 1
/
extra.go
303 lines (250 loc) · 7.03 KB
/
extra.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
// Copyright 2014 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package valid
import (
"encoding/base32"
"encoding/base64"
"errors"
"fmt"
"hash/adler32"
"net"
"net/mail"
"net/url"
"regexp"
"strconv"
"strings"
"code.google.com/p/go.net/publicsuffix"
"github.com/kless/datautil"
)
// == Algorithms
var ErrLuhnChecksum = errors.New("invalid checksum")
// LuhnChecksum validates a variety of identification numbers, such as
// credit card numbers and IMEI numbers.
//
// https://en.wikipedia.org/wiki/Luhn_algorithm
func LuhnChecksum(s *Schema, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
odd := len(str) & 1
sum := 0
// From the rightmost digit, moving left
for i := len(str) - 1; i >= 0; i-- {
num64b, err := strconv.ParseInt(string(str[i]), 10, 0)
if err != nil {
return "", passError("LuhnChecksum", str, err)
}
num := int(num64b)
if i&1 == odd {
num *= 2
if num > 9 {
num -= 9
}
}
sum += num
}
if (sum % 10) != 0 {
return "", passError("LuhnChecksum", str, ErrLuhnChecksum)
}
return str, nil
}
// == Encodings
// Base32 checks if the string is encoded in base32 and returns the value decoded.
func Base32(s *Schema, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
data, err := base32.StdEncoding.DecodeString(str)
if err == nil {
return string(data), nil
}
if data, err = base32.HexEncoding.DecodeString(str); err == nil {
return string(data), nil
}
err = passError("Base32", str, err)
return
}
// Base64 checks if the string is encoded in base64 and returns the value decoded.
func Base64(s *Schema, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
data, err := base64.StdEncoding.DecodeString(str)
if err == nil {
return string(data), nil
}
if data, err = base64.URLEncoding.DecodeString(str); err == nil {
return string(data), nil
}
err = passError("Base64", str, err)
return
}
// Hexadecimal checks if the string is an hexadecimal value and returns the
// corresponding value.
func Hexadecimal(s *Schema, str string) (value int64, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
// Remove "0x"
if len(str) > 2 && str[0] == '0' && str[1] == 'x' {
str = str[2:]
}
if value, err = strconv.ParseInt(str, 16, 64); err != nil {
return 0, passError("Hexadecimal", "", err)
}
return
}
// == Net
var (
ErrIPDomain = errors.New("IP address in domain")
ErrNoIP = errors.New("no IP address")
ErrNoScheme = errors.New(`no protocol scheme (like "http://")`)
ErrHTTP_FTP = errors.New("no protocol schemes for http,https,ftp")
)
type ICANNError string
func (e ICANNError) Error() string {
return `no ICANN domain: "` + string(e) + `"`
}
type URLSchemeError []string
func (e URLSchemeError) Error() string {
return "no protocol scheme for " + strings.Join([]string(e), ",")
}
// CheckDomain uses the flag C_TLD to checking if the domain is ICANN's
// and the flag C_DNS to checking if it is mapped to an address.
func CheckDomain(flag Checker, domain string, forEmail bool) (err error) {
if flag&C_TLD != 0 {
if ip := net.ParseIP(domain); ip != nil {
return ErrIPDomain
}
if suffix, isIcann := publicsuffix.PublicSuffix(domain); !isIcann {
return ICANNError(suffix)
}
}
if flag&C_DNS != 0 {
if domain, err = publicsuffix.EffectiveTLDPlusOne(domain); err != nil {
return err
}
if forEmail {
_, err = net.LookupMX(domain)
} else {
_, err = net.LookupHost(domain)
}
if err != nil {
return err
}
}
return nil
}
// Email checks if the string is an email address correctly parsed and returns
// the address part.
// Uses the flag C_TLD to checking the domain and C_DNS for the DNS MX records.
func Email(s *Schema, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
addr, err := mail.ParseAddress(str)
if err != nil {
return "", passError("Email", str, err)
}
str = addr.Address
if s.flagCheck != 0 {
err = CheckDomain(s.flagCheck, strings.SplitAfter(str, "@")[1], true)
if err != nil {
return "", passError("Email", str, err)
}
}
return str, nil
}
// IP checks if the string is an IP address.
// Uses the flag C_TLD to checking the domain by a reverse lookup.
func IP(s *Schema, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
if ip := net.ParseIP(str); ip == nil {
return "", passError("IP", str, ErrNoIP)
}
if s.flagCheck&C_DNS != 0 {
if _, err = net.LookupAddr(str); err != nil {
return "", passError("IP", str, err)
}
}
return str, nil
}
// MAC checks if the string is a hardware address.
func MAC(s *Schema, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
if _, err = net.ParseMAC(str); err != nil {
return "", passError("MAC", "", err)
}
return str, nil
}
// URL checks if the string is an URL and returns a valid URL string.
// Uses the flags C_HTTP_FTP to checking the protocol schemes for HTTP and FTP,
// C_TLD for the domain, and C_DNS for the DNS records.
func URL(s *Schema, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
u, err := url.Parse(str)
if err != nil {
return "", passError("URL", str, err)
}
if u.Scheme == "" {
return "", passError("URL", str, ErrNoScheme)
}
if s.flagCheck != 0 {
if s.flagCheck&C_HTTP_FTP != 0 {
switch u.Scheme {
case "http", "https", "ftp": // found
default:
return "", passError("URL", str, ErrHTTP_FTP)
}
}
if err = CheckDomain(s.flagCheck, u.Host, false); err != nil {
return "", passError("URL", str, err)
}
}
return u.String(), nil
}
// == Regular expressions
type PatternError struct {
expr string
format string
}
func (e *PatternError) Error() string {
if e.format != "" {
return fmt.Sprintf("does not matches with format: %s", e.format)
} else {
return fmt.Sprintf("does not matches with expression: `%s`", e.expr)
}
}
// regexpCache represents a cache of compiled regular expression.
type regexpCache map[uint32]*regexp.Regexp
var reCache regexpCache = make(map[uint32]*regexp.Regexp, 5)
// Pattern checks if the string matches a regular expression.
//
// Internally, uses a cache to store every new compiled regular expression.
func Pattern(s *Schema, expr, str string) (value string, err error) {
if str, err = s.PreCheck(str, datautil.Custom); err != nil {
return
}
crc := adler32.Checksum([]byte(expr))
re, found := reCache[crc]
if !found {
re, err = regexp.Compile(expr)
if err != nil {
return "", passError("Pattern", str, err)
}
reCache[crc] = re
}
if ok := re.MatchString(str); !ok {
return "", passError("Pattern", str, &PatternError{expr, s.patternFmt})
}
return str, nil
}