Repository navigation
Expand file tree
/
Copy pathutf.go
More file actions
407 lines (339 loc) · 8.74 KB
/
Copy pathutf.go
File metadata and controls
407 lines (339 loc) · 8.74 KB
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
package syntax
/*
unicode.go handles rune analysis and unicode ranging.
*/
import (
"unicode"
"unicode/utf16"
"unicode/utf8"
)
/*
UTF8String implements the UTF8 String syntax and abstraction.
From [§ 1.4 of RFC 4512]:
UTF8 = UTF1 / UTFMB
UTFMB = UTF2 / UTF3 / UTF4
UTF0 = %x80-BF
UTF1 = %x00-7F
UTF2 = %xC2-DF UTF0
UTF3 = %xE0 %xA0-BF UTF0 / %xE1-EC 2(UTF0) /
%xED %x80-9F UTF0 / %xEE-EF 2(UTF0)
UTF4 = %xF0 %x90-BF 2(UTF0) / %xF1-F3 3(UTF0) /
%xF4 %x80-8F 2(UTF0)
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
*/
type UTF8String []byte
/*
NewUTF8String returns an instance of [UTF8String] alongside an error
following an analysis of x in the context of a UTF8-compliant string.
*/
func NewUTF8String(x any) (UTF8String, error) {
return assertUTF8String(x)
}
func assertUTF8String(x any) (u UTF8String, err error) {
var raw []byte
switch tv := x.(type) {
case UTF8String:
raw = []byte(tv)
case []byte:
raw = tv
case string:
raw = []byte(tv)
default:
err = errorBadType("UTF8String")
return
}
u, err = uTF8(raw)
return
}
/*
Encode returns an instance of []byte alongside an error following an
attempt to encode the receiver instance as an ASN.1 UTF8String value.
*/
func (r UTF8String) Encode() ([]byte, error) {
return encodeP(tUTF8, r)
}
/*
Decode returns an error following an attempt to decode and write
the input enc value to the receiver instance. The encoding must
not be truncated, and must bear the UTF8String tag (0x0c).
*/
func (r *UTF8String) Decode(enc []byte) error {
if len(enc) < 3 || enc[0] != tUTF8 {
return errUTF8Decode
}
l, n := readLen(enc[1:])
if n == 0 || len(enc) < 1+n+l {
return errUTF8Decode
}
*r = enc[1+n : 1+n+l]
return nil
}
var errUTF8Decode = asn1Error("invalid OCTET STRING encoding")
/*
String returns the string representation of the receiver instance.
*/
func (r UTF8String) String() string { return string(r) }
func (r UTF8String) IsZero() bool { return len(r) == 0 }
var (
runeLen func(rune) int = utf8.RuneLen
decRune func([]byte) (rune, int) = utf8.DecodeRune
utf8OK func([]byte) bool = utf8.Valid
utf16Enc func([]rune) []uint16 = utf16.Encode
)
var runeSelf rune = utf8.RuneSelf
var maxASCII rune = unicode.MaxASCII
var (
asciiRange,
uTF8SubsetRange,
utf0Range,
utf1Range,
utf2Range,
utf2aSafeRange,
utf2bSafeRange,
utf3aRange,
utf3SafeRange,
utf3bRange,
utf3cRange,
utf3dRange,
utf4aRange,
utf4SafeRange,
utf4bRange,
utf4cRange *unicode.RangeTable
)
/*
IsSafeUTF8 returns a Boolean value alongside an error following an
analysis of input argument x as a "Safe UTF8" value.
UTF8String = StringValue
StringValue = dquote *SafeUTF8Character dquote
dquote = %x22 ; " (double quote)
SafeUTF8Character = %x00-21 / %x23-7F / ; ASCII minus dquote
dquote dquote / ; escaped double quote
%xC0-DF %x80-BF / ; 2 byte UTF-8 character
%xE0-EF 2(%x80-BF) / ; 3 byte UTF-8 character
%xF0-F7 3(%x80-BF) ; 4 byte UTF-8 character
*/
func IsSafeUTF8(x any) (result bool, err error) {
var raw []rune
if raw, err = assertRunes(x); err != nil {
return
}
funcs := map[int]func(string) error{
2: isSafeUTF2,
3: isSafeUTF3,
4: isSafeUTF4,
}
var last rune
for i := 0; i < len(raw) && err == nil; i++ {
r := raw[i]
switch rL := runeLen(r); rL {
case 1:
// ASCII range w/o double-quote
err = isSafeUTF1(string(r))
if '"' == r && last != '\u005C' {
err = syntaxError("Unescaped double-quote; not a UTF8 Safe Character")
}
last = r
case 2, 3, 4:
// UTF2/3/4
err = funcs[rL](string(r))
}
}
result = err == nil
return
}
func isSafeUTF1(x string) (err error) {
z := rune([]byte(x)[0])
if !(unicode.Is(asciiRange, z) && z != '"') {
err = syntaxError("Incompatible char for UTF0 (in ASCII Safe Range):", x)
}
return
}
func isSafeUTF2(x string) (err error) {
z := []byte(string(x))
ch1 := rune(z[0])
ch2 := rune(z[1])
if !(unicode.Is(utf2aSafeRange, ch1) &&
unicode.Is(utf2bSafeRange, ch2)) {
err = syntaxError("Incompatible chars for UTF2 (in UTF2 Safe Range): ", x)
}
return
}
func isSafeUTF3(x string) (err error) {
z := []byte(string(x))
ch1 := rune(z[0])
ch2 := rune(z[1])
ch3 := rune(z[2])
if !(unicode.Is(utf3SafeRange, ch1) &&
unicode.Is(utf2bSafeRange, ch2) &&
unicode.Is(utf2bSafeRange, ch3)) {
err = syntaxError("Incompatible chars for UTF3 (in UTF3 Safe Range): ", x)
}
return
}
func isSafeUTF4(x string) (err error) {
z := []byte(string(x))
ch1 := rune(z[0])
ch2 := rune(z[1])
ch3 := rune(z[2])
ch4 := rune(z[3])
if !(unicode.Is(utf4SafeRange, ch1) &&
unicode.Is(utf2bSafeRange, ch2) &&
unicode.Is(utf2bSafeRange, ch3) &&
unicode.Is(utf2bSafeRange, ch4)) {
err = syntaxError("Incompatible chars for UTF4 (in UTF4 Safe Range): ", x)
}
return
}
/*
uTF8 returns an error following an analysis of x in the context of
one (1) or more UTF8 characters.
From [§ 1.4 of RFC 4512]:
UTF8 = UTF1 / UTFMB
UTFMB = UTF2 / UTF3 / UTF4
UTF0 = %x80-BF
UTF1 = %x00-7F
UTF2 = %xC2-DF UTF0
UTF3 = %xE0 %xA0-BF UTF0 / %xE1-EC 2(UTF0) /
%xED %x80-9F UTF0 / %xEE-EF 2(UTF0)
UTF4 = %xF0 %x90-BF 2(UTF0) / %xF1-F3 3(UTF0) /
%xF4 %x80-8F 2(UTF0)
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
*/
func uTF8(x any, zok ...bool) (u UTF8String, err error) {
var raw []rune
if raw, err = assertRunes(x, zok...); err != nil {
return
}
for i := 0; i < len(raw) && err == nil; i++ {
if !unicode.Is(utf1Range, rune(raw[i])) {
err = uTFMB(rune(raw[i]))
}
}
if err == nil {
var b string
for i := 0; i < len(raw); i++ {
b += string(raw[i])
}
u = UTF8String(b)
}
return
}
/*
IsUTFMB returns a Boolean value indicative of the input value --
which may be a string, rune, rune slice, byte or byte slice --
being a valid UTFMB value.
*/
func IsUTFMB(x any) bool { return uTFMB(x) == nil }
/*
uTFMB returns an error following an analysis of x in the context of
one (1) or more UTFMB characters.
*/
func uTFMB(x any) (err error) {
var raw []rune
if raw, err = assertRunes(x); err == nil {
for i := 0; i < len(raw) && err == nil; i++ {
r := rune(raw[i])
var valid bool
if valid, err = isUTFMBChar(r); !valid {
err = syntaxError("Invalid UTFMB char: ", string(r))
break
}
}
}
return
}
func isUTF0(b byte) bool {
return b >= 0x80 && b <= 0xBF
}
func isUTF2(ub []byte) bool {
return ub[0] >= 0xC2 && ub[0] <= 0xDF && isUTF0(ub[1])
}
func isUTF3(ub []byte) (b bool) {
switch ub[0] {
case 0xE0:
b = unicode.Is(utf3aRange, rune(ub[1])) && isUTF0(ub[2])
case 0xED:
b = unicode.Is(utf3cRange, rune(ub[1])) && isUTF0(ub[2])
case 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xEE, 0xEF:
b = isUTF0(ub[1]) && isUTF0(ub[2])
}
return
}
func isUTF4(ub []byte) (b bool) {
switch ub[0] {
case 0xF0:
b = ub[1] >= 0x90 && ub[1] <= 0xBF && isUTF0(ub[2]) && isUTF0(ub[3])
case 0xF1, 0xF2, 0xF3:
b = isUTF0(ub[1]) && isUTF0(ub[2]) && isUTF0(ub[3])
case 0xF4:
b = unicode.Is(utf4cRange, rune(ub[1])) && isUTF0(ub[2]) && isUTF0(ub[3])
}
return
}
func isUTFMBChar(r rune) (b bool, err error) {
ub := make([]byte, 4)
n := utf8.EncodeRune(ub, r)
switch n {
case 2:
b = isUTF2(ub)
case 3:
b = isUTF3(ub)
case 4:
b = isUTF4(ub)
}
if !b {
err = syntaxError("invalid leading byte for ",
itoa(n), "-byte sequence, or bad length")
}
return
}
func init() {
uTF8SubsetRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0001, 0x0027, 1},
{0x002B, 0x005B, 1},
{0x005D, 0x007F, 1},
}}
utf0Range = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x00BF, 1},
}}
utf1Range = &unicode.RangeTable{R16: []unicode.Range16{
{0x0000, 0x007F, 1},
}}
asciiRange = utf1Range
utf2Range = &unicode.RangeTable{R16: []unicode.Range16{
{0x00C2, 0x00DF, 1},
}}
utf2aSafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00C0, 0x00DF, 1},
}}
utf2bSafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x00BF, 1},
}}
utf3aRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00A0, 0x00BF, 1},
}}
utf3SafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00E0, 0x00EF, 1},
}}
utf3bRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00E1, 0x00EC, 1},
}}
utf3cRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x009F, 1},
}}
utf3dRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00EE, 0x00EF, 1},
}}
utf4aRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0090, 0x00BF, 1},
}}
utf4SafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00F0, 0x00F7, 1},
}}
utf4bRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00F1, 0x00F3, 1},
}}
utf4cRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x008F, 1},
}}
}