-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer.go
355 lines (297 loc) · 6.7 KB
/
integer.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
package dirsyn
import "math/big"
/*
Integer aliases [big.Int] to implement an unbounded ASN.1 INTEGER syntax
and matching rule capabilities.
From [§ 3.3.16 of RFC 4517]:
Integer = ( HYPHEN LDIGIT *DIGIT ) / number
[§ 3.3.16 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.16
*/
type Integer big.Int
/*
Integer returns an instance of Integer alongside an error following an
analysis of x in the context of an ASN.1 Integer.
*/
func (r RFC4517) Integer(x any) (Integer, error) {
return marshalInteger(x)
}
func marshalInteger(x any) (i Integer, err error) {
var _i *big.Int
if _i, err = assertNumber(x); err == nil {
i = Integer(*_i)
}
return
}
func integer(x any) (result Boolean) {
_, err := marshalInteger(x)
result.Set(err == nil)
return
}
/*
Cast unwraps and returns the underlying instance of *[big.Int].
*/
func (r Integer) Cast() *big.Int {
a := big.Int(r)
return &a
}
/*
UUID returns the [UUID] representation of the receiver instance.
*/
func (r Integer) UUID() (u UUID) {
if !r.IsZero() {
bts := r.Bytes()
if len(bts) == 17 {
if bts[0] == 0x00 || bts[0] == 0x01 {
bts = bts[1:]
}
}
var _u [16]uint8
copy(_u[16-len(bts):], bts)
if ret, err := uuidFromBytes(_u[:]); err == nil {
u = UUID([16]uint8(ret))
}
}
return
}
/*
Bytes wraps [big.Int.Bytes].
Note that the return value is a big endian byte sequence.
Also note that the return value will bear a leading 0x0 if the number is
zero (0) or more, or a leading 0x1 if the number is negative.
*/
func (r Integer) Bytes() (bts []byte) {
if !r.IsZero() {
bts = []byte{0x00}
if r.Cast().Sign() < 0 {
bts[0] = byte(0x01)
}
bts = append(bts, r.Cast().Bytes()...)
}
return
}
/*
SetBytes wraps [big.Int.SetBytes].
Note that input value "bts" must be a a big endian byte sequence.
Also note that if encoding bytes manually for submission to this method,
a leading 0x1 signifies the number is negative. A leading 0x0 indicates
the number is unsigned (zero (0) or more) and is optional.
*/
func (r *Integer) SetBytes(bts []byte) {
if len(bts) > 0 {
_r := big.NewInt(0)
var neg bool
if bts[0] == 0x01 {
bts = bts[1:]
neg = true
} else if bts[0] == 0x00 {
bts = bts[1:]
}
_r.SetBytes(bts)
if neg {
_r.Neg(_r)
}
*r = Integer(*_r)
}
}
/*
IsZero returns a Boolean value indicative of a nil, or unset, receiver.
*/
func (r Integer) IsZero() bool {
return len(r.Cast().Bytes()) == 0
}
/*
String returns the string representation of the receiver instance. If the
receiver is unset, `0` is returned.
*/
func (r Integer) String() (s string) {
s = `0`
if !r.IsZero() {
s = r.Cast().String()
}
return
}
func assertInt(x any) (i *big.Int, err error) {
switch tv := x.(type) {
case int:
i = big.NewInt(0).SetInt64(int64(tv))
case int8:
i = big.NewInt(0).SetInt64(int64(tv))
case int16:
i = big.NewInt(0).SetInt64(int64(tv))
case int32:
i = big.NewInt(0).SetInt64(int64(tv))
case int64:
i = big.NewInt(0).SetInt64(tv)
default:
err = errorBadType("Incompatible int")
}
return
}
func assertUint(x any) (i *big.Int, err error) {
switch tv := x.(type) {
case uint:
i = big.NewInt(0).SetUint64(uint64(tv))
case uint8:
i = big.NewInt(0).SetUint64(uint64(tv))
case uint16:
i = big.NewInt(0).SetUint64(uint64(tv))
case uint32:
i = big.NewInt(0).SetUint64(uint64(tv))
case uint64:
i = big.NewInt(0).SetUint64(tv)
default:
err = errorBadType("Incompatible uint")
}
return
}
func assertNumber(x any) (i *big.Int, err error) {
switch tv := x.(type) {
case int, int8, int16, int32, int64:
i, err = assertInt(tv)
case uint, uint8, uint16, uint32, uint64:
i, err = assertUint(tv)
case Integer:
i = tv.Cast()
case *big.Int:
i = tv
case string:
if len(tv) == 0 {
err = errorBadLength("Integer", 0)
return
}
var ok bool
i, ok = big.NewInt(0).SetString(tv, 10)
if !ok {
err = errorTxt("Unable to convert string '" + tv + "' to Integer")
} else if hasPfx(tv, `-`) {
i.Neg(i)
}
default:
err = errorBadType("Integer")
}
return
}
func isIntegerType(x any) (is bool) {
switch x.(type) {
case int, int8, int16, int32, int64:
is = true
}
return
}
func isNegativeInteger(x any) (is bool) {
switch tv := x.(type) {
case int:
is = tv < 0
case int8:
is = tv < 0
case int16:
is = tv < 0
case int32:
is = tv < 0
case int64:
is = tv < 0
}
return
}
func castInt64(x any) (i int64, err error) {
switch tv := x.(type) {
case int:
i = int64(tv)
case int8:
i = int64(tv)
case int16:
i = int64(tv)
case int32:
i = int64(tv)
case int64:
i = tv
default:
err = errorBadType("any2int64")
}
return
}
func castUint64(x any) (i uint64, err error) {
switch tv := x.(type) {
case uint:
i = uint64(tv)
case uint8:
i = uint64(tv)
case uint16:
i = uint64(tv)
case uint32:
i = uint64(tv)
case uint64:
i = tv
default:
err = errorBadType("any2uint64")
}
return
}
/*
integerMatch implements [§ 4.2.19 of RFC 4517].
OID: 2.5.13.14
[§ 4.2.19 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.19
*/
func integerMatch(a, b any) (Boolean, error) {
return integerMatchingRule(a, b, true)
}
/*
integerOrderingMatch implements [§ 4.2.20 of RFC 4517].
OID: 2.5.13.15
[§ 4.2.20 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.20
*/
func integerOrderingMatch(a, b any) (Boolean, error) {
return integerMatchingRule(a, b, false)
}
/*
integerFirstComponentMatch implements [§ 4.2.18 of RFC 4517].
OID: 2.5.13.29
[§ 4.2.18 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.18
*/
func integerFirstComponentMatch(a, b any) (result Boolean, err error) {
// Use reflection to handle the attribute value.
// This value MUST be a struct (SEQUENCE).
realValue := assertFirstStructField(a)
if realValue == nil {
result.Set(false)
return
}
field, nerr := assertNumber(realValue)
if nerr != nil {
err = nerr
return
}
if assertValue := assertFirstStructField(b); assertValue == nil {
assert, aerr := assertNumber(b)
err = aerr
result.Set(streq(field.String(), assert.String()))
} else {
assert, aerr := assertNumber(assertValue)
err = aerr
result.Set(streq(field.String(), assert.String()))
}
return
}
func integerMatchingRule(a, b any, equality bool) (Boolean, error) {
var result Boolean
bint1, err1 := assertNumber(a)
if err1 != nil {
return result, err1
}
i1 := Integer(*bint1)
bint2, err2 := assertNumber(b)
if err2 != nil {
return result, err2
}
i2 := Integer(*bint2)
result.Set(compareIntegerInteger(i1, i2, equality))
return result, nil
}
func compareIntegerInteger(i, tv Integer, equality bool) (is bool) {
is = i.Cast().Cmp(tv.Cast()) == 0
if !equality {
is = i.Cast().Cmp(tv.Cast()) == 0 ||
i.Cast().Cmp(tv.Cast()) == -1
}
return
}