-
Notifications
You must be signed in to change notification settings - Fork 0
/
tel.go
587 lines (483 loc) · 14.8 KB
/
tel.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package dirsyn
import "encoding/asn1"
var (
ftnPRM map[string]uint
ttxs map[string]uint8
)
const (
UBTelephoneNumber int = 32 // X.520: ub-telephone-number INTEGER ::= 32
UBTeletexTerminalID int = 1024 // X.520: ub-teletex-terminal-id INTEGER ::= 1024
UBTeletexPrivateUse int = 128 // X.411: ub-teletex-private-use-length INTEGER ::= 128
)
/*
FacsimileTelephoneNumber implements [§ 3.3.11 of RFC 4517] and [ITU-T Rec.
X.520 clause 6.7.4].
From [§ 3.3.11 of RFC 4517]:
fax-number = telephone-number *( DOLLAR fax-parameter )
telephone-number = PrintableString
fax-parameter = "twoDimensional" /
"fineResolution" /
"unlimitedLength" /
"b4Length" /
"a3Width" /
"b4Width" /
"uncompressed"
ASN.1 definitions:
FacsimileTelephoneNumber ::= SEQUENCE {
telephoneNumber PrintableString (SIZE(1.. ub-telephone-number)),
parameters G3FacsimileNonBasicParameters OPTIONAL}
G3FacsimileNonBasicParameters ::= BIT STRING {
two-dimensional (8),
fine-resolution (9),
unlimited-length (20),
b4-length (21),
a3-width (22),
b4-width (23),
uncompressed (30) }
[§ 3.3.11 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.11
[ITU-T Rec. X.520 clause 6.7.4]: https://www.itu.int/rec/T-REC-X.520
*/
type FacsimileTelephoneNumber struct {
TelephoneNumber PrintableString `asn1:"printable"`
G3FacsimileNonBasicParameters asn1.BitString `asn1:"optional"`
}
func (r FacsimileTelephoneNumber) String() (ftn string) {
if len(r.TelephoneNumber) == 0 {
return
}
var prms []string
for name, bit := range ftnPRM {
if r.isSet(bit) && !strInSlice(name, prms) {
prms = append(prms, name)
}
}
if len(prms) == 0 {
ftn = r.TelephoneNumber.String()
return
}
ftn = r.TelephoneNumber.String() + `$` + join(prms, `$`)
return
}
func (r FacsimileTelephoneNumber) isSet(bit uint) bool {
if bit > 31 || len(r.G3FacsimileNonBasicParameters.Bytes) == 0 {
return false
}
index := bit / 8
pos := bit % 8
return r.G3FacsimileNonBasicParameters.Bytes[index]&(1<<pos) != 0
}
func (r *FacsimileTelephoneNumber) IsZero() bool { return &r == nil }
func (r *FacsimileTelephoneNumber) set(bit uint) {
if bit > 31 || r.IsZero() {
return
}
index := bit / 8
pos := bit % 8
r.G3FacsimileNonBasicParameters.Bytes[index] |= 1 << pos
}
/*
FacsimileTelephoneNumber returns an instance of [FacsimileTelephoneNumber]
alongside an error.
From [§ 3.3.11 of RFC 4517]:
fax-number = telephone-number *( DOLLAR fax-parameter )
telephone-number = PrintableString
fax-parameter = "twoDimensional" /
"fineResolution" /
"unlimitedLength" /
"b4Length" /
"a3Width" /
"b4Width" /
"uncompressed"
From [§ 1.4 of RFC 4512]:
DOLLAR = %x24 ; dollar sign ("$")
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
[§ 3.3.11 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.11
*/
func (r RFC4517) FacsimileTelephoneNumber(x any) (FacsimileTelephoneNumber, error) {
return marshalFacsimileTelephoneNumber(x)
}
func facsimileTelephoneNumber(x any) (result Boolean) {
_, err := marshalFacsimileTelephoneNumber(x)
result.Set(err == nil)
return
}
func marshalFacsimileTelephoneNumber(x any) (ftn FacsimileTelephoneNumber, err error) {
var raw string
if raw, err = assertString(x, 1, "Facsimile Telephone Number"); err != nil {
return
}
raws := splitUnescaped(raw, `$`, `\`)
if len(raws) <= 1 {
err = errorTxt("Invalid Facsimile Telephone Number")
return
} else if ftn.TelephoneNumber, err = marshalPrintableString(raws[0]); err != nil || len(raws) == 1 {
return
}
ftn.G3FacsimileNonBasicParameters = asn1.BitString{
Bytes: make([]byte, 4),
BitLength: 32,
}
raws = raws[1:]
for _, slice := range raws {
bit, found := ftnPRM[slice]
if !found {
err = errorTxt("Unknown Facsimile Telephone Number PRM value: " + slice)
break
} else if ftn.isSet(bit) {
err = errorTxt("Duplicate Facsimile Telephone Number PRM value: " +
slice + " at bit " + fmtInt(int64(bit), 10))
break
}
ftn.set(bit)
}
return
}
/*
TelephoneNumber implements [§ 3.3.31 of RFC 4517] and [ITU-T Rec. X.520 clause 6.7.1]:
PrintableString (SIZE(1..ub-telephone-number))
[ITU-T Rec. X.520 clause 6.7.1]: https://www.itu.int/rec/T-REC-X.520
[§ 3.3.31 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.31
*/
type TelephoneNumber PrintableString
/*
String returns the string representation of the receiver instance.
*/
func (r TelephoneNumber) String() string {
return `+` + string(r)
}
/*
TelephoneNumber returns an instance of [TelephoneNumber] alongside an error
following an analysis of x in the context of a Telephone Number.
*/
func (r RFC4517) TelephoneNumber(x any) (TelephoneNumber, error) {
return marshalTelephoneNumber(x)
}
func telephoneNumber(x any) (result Boolean) {
_, err := marshalTelephoneNumber(x)
result.Set(err == nil)
return
}
func marshalTelephoneNumber(x any) (tn TelephoneNumber, err error) {
var raw string
switch tv := x.(type) {
case string:
l := len(tv)
if !(1 <= l && l <= UBTelephoneNumber) {
err = errorBadLength("Telephone Number", l)
return
} else if tv[0] != '+' {
err = errorTxt("Telephone Number has invalid prefix: " + string(tv[0]))
return
}
raw = tv[1:]
default:
err = errorBadType("Telephone Number")
return
}
// TODO: conform more closely to E.123.
for _, ch := range raw {
char := rune(ch)
if !isTelephoneNumberChar(char) {
err = errorBadType("Invalid Telephone Number character: " + string(char))
return
}
}
if _, err = marshalPrintableString(raw); err == nil {
tn = TelephoneNumber(raw)
}
return
}
/*
TelexNumber implements TelexNumber per [§ 3.3.33 of RFC 4517]:
telex-number = actual-number DOLLAR country-code DOLLAR answerback
actual-number = PrintableString
country-code = PrintableString
answerback = PrintableString
From [§ 3.2 of RFC 4517]:
PrintableCharacter = ALPHA / DIGIT / SQUOTE / LPAREN / RPAREN /
PLUS / COMMA / HYPHEN / DOT / EQUALS /
SLASH / COLON / QUESTION / SPACE
PrintableString = 1*PrintableCharacter
From [§ 1.4 of RFC 4512]:
DOLLAR = %x24 ; dollar sign ("$")
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
[§ 3.2 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.2
[§ 3.3.33 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.33
*/
type TelexNumber [3]string
/*
TelexNumber returns an error following an analysis of x in the context
of a Telex Number.
*/
func (r RFC4517) TelexNumber(x any) (TelexNumber, error) {
return marshalTelexNumber(x)
}
func telexNumber(x any) (result Boolean) {
_, err := marshalTelexNumber(x)
result.Set(err == nil)
return
}
func marshalTelexNumber(x any) (tn TelexNumber, err error) {
var raw string
if raw, err = assertString(x, 1, "Telex Number"); err != nil {
return
}
raws := splitUnescaped(raw, `$`, `\`)
if len(raws) != 3 {
err = errorTxt("Invalid Telex Number value")
return
}
var _tn []string
var ct int
for _, slice := range raws {
if _, err = marshalPrintableString(slice); err == nil {
_tn = append(_tn, slice)
ct++
}
}
if ct != 3 {
err = errorTxt("Invalid Telex Number component count; expected 3")
return
}
tn = TelexNumber(_tn)
return
}
/*
String returns the string representation of the receiver instance.
*/
func (r TelexNumber) String() (str string) {
if r[0] != "" && r[1] != "" && r[2] != "" {
str = r[0] + `$` + r[1] + `$` + r[2]
}
return
}
/*
TeletexTerminalIdentifier implements [§ 3.3.32 of RFC 4517] and
teletex-id = ttx-term *(DOLLAR ttx-param)
ttx-term = PrintableString ; terminal identifier
ttx-param = ttx-key COLON ttx-value ; parameter
ttx-key = "graphic" / "control" / "misc" / "page" / "private"
ttx-value = *ttx-value-octet
ttx-value-octet = %x00-23
/ (%x5C "24") ; escaped "$"
/ %x25-5B
/ (%x5C "5C") ; escaped "\"
/ %x5D-FF
ASN.1 definition, per [ITU-T Rec. X.520]:
TeletexTerminalIdentifier ::= SEQUENCE {
teletexTerminal PrintableString (SIZE(1..ub-teletex-terminal-id)),
parameters TeletexNonBasicParameters OPTIONAL
}
ub-teletex-terminal-id INTEGER ::= 1024
From [§ 3.2 of RFC 4517]:
PrintableCharacter = ALPHA / DIGIT / SQUOTE / LPAREN / RPAREN /
PLUS / COMMA / HYPHEN / DOT / EQUALS /
SLASH / COLON / QUESTION / SPACE
PrintableString = 1*PrintableCharacter
COLON = %x3A ; colon (":")
From [§ 1.4 of RFC 4512]:
DOLLAR = %x24 ; dollar sign ("$")
[ITU-T Rec. X.520]: https://www.itu.int/rec/T-REC-X.520
[§ 3.2 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.2
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
[§ 3.3.32 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.32
*/
type TeletexTerminalIdentifier struct {
TeletexTerminal string `asn1:"printable"` // (SIZE(1..ub-teletex-terminal-id)),
Parameters TeletexNonBasicParameters `asn1:"set,optional"`
}
/*
TeletexNonBasicParameters is defined in [ITU-T Rec. X.420].
[ITU-T Rec. X.420]: https://www.itu.int/rec/T-REC-X.420
*/
type TeletexNonBasicParameters struct {
GraphicCharacterSets TeletexString `asn1:"tag:0,optional"` // TeletexString OPTIONAL
CtrlCharacterSets TeletexString `asn1:"tag:1,optional"` // TeletexString OPTIONAL
PageFormats OctetString `asn1:"tag:2,optional"` // OCTET STRING OPTIONAL
MiscTerminalCapabilities TeletexString `asn1:"tag:3,optional"` // TeletexString OPTIONAL
PrivateUse OctetString `asn1:"tag:4,optional"` // OCTET STRING OPTIONAL
}
func (r TeletexTerminalIdentifier) String() (s string) {
s = r.TeletexTerminal
if r.Parameters.string() != "" {
s += `$` + r.Parameters.string()
}
return
}
func (r TeletexNonBasicParameters) string() string {
var slice []string
if len(r.GraphicCharacterSets) > 0 {
slice = append(slice, string(r.GraphicCharacterSets))
}
if len(r.CtrlCharacterSets) > 0 {
slice = append(slice, string(r.CtrlCharacterSets))
}
if len(r.PageFormats) > 0 {
slice = append(slice, string(r.PageFormats))
}
if len(r.MiscTerminalCapabilities) > 0 {
slice = append(slice, string(r.MiscTerminalCapabilities))
}
if len(r.PrivateUse) > 0 {
slice = append(slice, string(r.PrivateUse))
}
var nbp string
if len(slice) > 0 {
nbp = join(slice, `$`)
}
return nbp
}
/*
TeletexTerminalIdentifier returns an error following an analysis of x in
the context of a Teletex Terminal Identifier.
*/
func (r RFC4517) TeletexTerminalIdentifier(x any) (TeletexTerminalIdentifier, error) {
return marshalTeletexTerminalIdentifier(x)
}
func teletexTerminalIdentifier(x any) (result Boolean) {
_, err := marshalTeletexTerminalIdentifier(x)
result.Set(err == nil)
return
}
func marshalTeletexTerminalIdentifier(x any) (tti TeletexTerminalIdentifier, err error) {
var (
raw string
raws,
vals []string
)
if raw, err = assertString(x, 1, "Teletex Terminal Identifier"); err != nil {
return
}
_raws := splitUnescaped(raw, `$`, `\`)
if raws, vals, err = marshalTeletex(_raws[1:]); err != nil {
return
} else if _, err = marshalPrintableString(_raws[0]); err != nil {
return
}
tti.TeletexTerminal = _raws[0]
var ct uint8
for idx, slice := range raws {
bit, found := ttxs[slice]
if !found {
err = errorTxt("Unknown Teletex Terminal Identifier TTXPRM value: " + slice)
break
} else if ct&bit > 0 {
err = errorTxt("Duplicate Teletex Terminal Identifier TTXPRM value: " + slice)
break
}
ct |= bit
if idx < len(vals) {
value := vals[idx]
switch slice {
case `graphic`:
tti.Parameters.GraphicCharacterSets = TeletexString(value)
case `control`:
tti.Parameters.CtrlCharacterSets = TeletexString(value)
case `private`:
tti.Parameters.PrivateUse = OctetString(value)
case `misc`:
tti.Parameters.MiscTerminalCapabilities = TeletexString(value)
case `page`:
tti.Parameters.PageFormats = OctetString(value)
}
}
}
return
}
func marshalTeletex(_raws []string) (raws, vals []string, err error) {
var cfound bool
for i := 0; i < len(_raws); i++ {
if idx := idxr(_raws[i], ':'); idx != -1 {
cfound = true
raw := _raws[i][:idx]
aft := _raws[i][idx+1:]
raws = append(raws, raw)
if len(aft) > 0 {
if err = teletexSuffixValue(aft); err != nil {
return
}
vals = append(vals, raw+`:`+aft)
} else {
vals = append(vals, raw+`:`)
}
} else {
err = errorTxt("Teletex Terminal Identifier missing ttx-value")
return
}
}
if !cfound {
err = errorTxt("Teletex Terminal Identifier missing ':' token")
} else if len(raws) == 0 {
err = errorTxt("Missing Teletex Terminal Identifier value")
} else if len(raws) > UBTeletexTerminalID {
err = errorTxt("Teletex Terminal Identifier value length out of bounds (>1024)")
}
return
}
// RFC 4518 § 2.6.3
func prepareTelephoneNumberAssertion(a, b any) (str1, str2 string, err error) {
if str1, err = assertString(a, 0, "numericString"); err != nil {
return
}
if str2, err = assertString(b, 0, "numericString"); err != nil {
return
}
for _, roon := range []rune{
'\u002d', '\u058a', '\u2010', '\u2011',
'\u2212', '\ufe63', '\uff0d', '\u0020',
} {
str1 = repAll(str1, string(roon), ``)
str2 = repAll(str2, string(roon), ``)
}
return
}
func teletexSuffixValue(x string) (err error) {
var last rune
for _, ch := range x {
if ch == '$' && last != '\\' {
err = errorTxt("Unescaped '$' character found in TTID suffix")
break
} else if !(ucIs(ttxRange, ch) || ch == '\\') {
err = errorTxt("Incompatible char for UTF0 (in UTFMB): " + string(ch))
break
}
last = rune(ch)
}
return
}
func telephoneNumberSubstringsMatch(a, b any) (result Boolean, err error) {
var str1, str2 string
if str1, str2, err = prepareTelephoneNumberAssertion(a, b); err == nil {
result, err = caseIgnoreSubstringsMatch(str1, str2)
}
return
}
func telephoneNumberMatch(a, b any) (result Boolean, err error) {
var str1, str2 string
if str1, str2, err = prepareTelephoneNumberAssertion(a, b); err == nil {
result.Set(streqf(str1, str2))
}
return
}
func isTelephoneNumberChar(x rune) bool {
return isAlphaNumeric(x) ||
runeInSlice(x, telephoneNumberRunes)
}
func init() {
ttxs = map[string]uint8{
`graphic`: uint8(1),
`control`: uint8(2),
`page`: uint8(4),
`misc`: uint8(8),
`private`: uint8(16),
}
ftnPRM = map[string]uint{
`twoDimensional`: uint(8),
`fineResolution`: uint(9),
`unlimitedLength`: uint(20),
`b4Length`: uint(21),
`a3Width`: uint(22),
`b4Width`: uint(23),
`uncompressed`: uint(30),
}
}