-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathname_encoding.go
More file actions
464 lines (371 loc) · 11.7 KB
/
Copy pathname_encoding.go
File metadata and controls
464 lines (371 loc) · 11.7 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
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
package main
import (
"cmp"
"errors"
"iter"
"math/big"
"math/rand"
"slices"
"strings"
"github.com/monoidic/dns"
)
type labelConverter struct {
alphabet string
mults []big.Int
maxLabelNum *big.Int
}
const (
MAX_LABEL_LEN = 63
MAX_NAME_LEN = 255
)
func newLabelConverter(alphabet string) *labelConverter {
mults := make([]big.Int, MAX_LABEL_LEN)
alphaLen := big.NewInt(int64(len(alphabet)))
iter := big.NewInt(0)
for i := range MAX_LABEL_LEN {
iter.Mul(iter, alphaLen)
iter.Add(iter, big1)
mults[i].Set(iter)
}
iter.Mul(iter, alphaLen)
iter.Sub(iter, big1)
return &labelConverter{
alphabet: alphabet,
mults: mults,
maxLabelNum: iter,
}
}
var (
bigneg1 = big.NewInt(-1)
big0 = big.NewInt(0)
big1 = big.NewInt(1)
)
// converts a DNS label into a bigint
func (lc *labelConverter) labelToNum(labelS string) (*big.Int, error) {
label := []byte(labelS)
if !(1 <= len(label) && len(label) <= MAX_LABEL_LEN) {
return nil, errInvalidLabel
}
multI := MAX_LABEL_LEN - 1
tmp := &big.Int{}
ret := (&big.Int{}).SetInt64(int64(len(label) - 1))
for _, v := range label {
idx := strings.IndexByte(lc.alphabet, v)
if idx == -1 {
return nil, errLabelCharset
}
tmp.SetInt64(int64(idx))
tmp.Mul(tmp, &lc.mults[multI])
ret.Add(ret, tmp)
multI--
}
return ret, nil
}
// convert a bigint into a DNS label
func (lc *labelConverter) numToLabel(num *big.Int) (string, error) {
if !lc.numValid(num) {
return "", errInvalidLabelNum
}
// local copy
num = (&big.Int{}).Set(num)
multI := MAX_LABEL_LEN - 1
var ret []byte
chunk := &big.Int{}
for num.Cmp(bigneg1) == 1 {
chunk.QuoRem(num, &lc.mults[multI], num)
ret = append(ret, lc.alphabet[chunk.Uint64()])
num.Sub(num, big1)
multI--
}
return string(ret), nil
}
func (lc *labelConverter) numValid(num *big.Int) bool {
return num.Cmp(big0) >= 0 && num.Cmp(lc.maxLabelNum) <= 0
}
func (lc *labelConverter) prevWithLen(num *big.Int, length int, repeatOk bool) (*big.Int, bool) {
if !lc.numValid(num) {
return nil, false
}
// local copy
num = (&big.Int{}).Set(num)
if !repeatOk {
// ensure we can't just return the same value
num.Sub(num, big1)
}
tmp := &big.Int{}
for {
if num.Cmp(big0) == -1 {
return nil, false
}
label, err := lc.numToLabel(num)
if err != nil {
return nil, false
}
switch cmp.Compare(len(label), length) {
case 0: // match
return num, true
case -1: // current label is shorter than target
tmp.SetInt64(int64(len(label) - 1))
num.Sub(num, tmp)
case 1: // current label is longer than target
// seek back from e.g "abc" to "aba", then back 1 to "ab"
// num -= label[-1] * mult[-len(label)]
v := strings.IndexByte(lc.alphabet, label[len(label)-1])
tmp.SetInt64(int64(v))
tmp.Mul(tmp, &lc.mults[MAX_LABEL_LEN-len(label)])
tmp.Add(tmp, big1)
num.Sub(num, tmp)
}
}
}
func (lc *labelConverter) nextWithLen(num *big.Int, length int, repeatOk bool) (*big.Int, bool) {
if !lc.numValid(num) {
return nil, false
}
// local copy
num = (&big.Int{}).Set(num)
if !repeatOk {
// ensure we can't just return the same value
num.Add(num, big1)
}
tmp := &big.Int{}
for {
if num.Cmp(lc.maxLabelNum) == 1 {
return nil, false
}
label, err := lc.numToLabel(num)
if err != nil {
return nil, false
}
switch cmp.Compare(len(label), length) {
case 0: // match
return num, true
case -1: // current label is shorter than target
// just walk forwards by the diff lol
// log.Printf("%s -1 iter %d", num, iterNum)
tmp.SetInt64(int64(length - len(label)))
num.Add(num, tmp)
case 1: // current label is longer than target
// go to next break on this label length
// num += (len(alphabet)-label[-1]) * mult[len(label)-1]
// log.Printf("%s 1 length %d iter %d", num, length, iterNum)
v := len(lc.alphabet) - strings.IndexByte(lc.alphabet, label[len(label)-1])
tmp.SetInt64(int64(v))
tmp.Mul(tmp, &lc.mults[MAX_LABEL_LEN-len(label)])
num.Add(num, tmp)
}
}
}
func (lc *labelConverter) bisectLabel(start, end *big.Int, length int) iter.Seq[string] {
// TODO use length for calculating division mask or something
// TODO FINISH THIS FUNCTION
return func(yield func(string) bool) {
var mid big.Int
rnd := rand.New(rand.NewSource(rand.Int63()))
// get a random number in the range of start to end
mid.Sub(end, start)
mid.Rand(rnd, &mid)
mid.Add(start, &mid)
label, err := lc.numToLabel(&mid)
if err != nil {
return
}
if len(label) == length {
if !yield(label) {
return
}
}
for _, f := range []func(*big.Int, int, bool) (*big.Int, bool){lc.nextWithLen, lc.prevWithLen} {
num, ok := f(&mid, length, false)
if !ok {
continue
}
label, err := lc.numToLabel(num)
if err != nil {
continue
}
if !yield(label) {
return
}
}
}
}
var (
// very limited range
lcAscii = newLabelConverter("-0123456789_abcdefghijklmnopqrstuvwxyz")
// more symbols
lcSymbols = newLabelConverter("!#$%&*+-/0123456789:;<=>?@[]^_`abcdefghijklmnopqrstuvwxyz{|}~")
// full valid label range
lcFull = newLabelConverter("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff")
ncAscii = newNameConverter("-0123456789_abcdefghijklmnopqrstuvwxyz")
ncSymbols = newNameConverter("!#$%&*+-/0123456789:;<=>?@[]^_`abcdefghijklmnopqrstuvwxyz{|}~")
ncFull = newNameConverter("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff")
errInvalidLabel = errors.New("invalid label for this label converter")
errInvalidLabelNum = errors.New("invalid label num for this label converter")
errLabelCharset = errors.New("invalid label for this label converter")
errNameCharset = errors.New("invalid name for this name converter")
errInvalidNameNum = errors.New("invalid name num for this name converter")
errNameTooLongForZoneEnd = errors.New("name too long for zone end calculation")
)
type nameConverter struct {
alphabet string
alphaLen *big.Int
maxNameNum *big.Int
stepDiffs [MAX_NAME_LEN - 2][MAX_LABEL_LEN]big.Int
expandDiffs [MAX_NAME_LEN - 2]big.Int
}
func newNameConverter(alphabet string) *nameConverter {
ret := &nameConverter{
alphabet: alphabet,
alphaLen: big.NewInt(int64(len(alphabet))),
}
// expandDiffs relies on stepDiffs
ret.mkStepDiffs()
ret.mkExpandDiffs()
ret.maxNameNum = (&big.Int{}).Set(&ret.stepDiffs[MAX_NAME_LEN-3][0])
ret.maxNameNum.Mul(ret.maxNameNum, ret.alphaLen)
return ret
}
func (nc *nameConverter) mkStepDiffs() {
nc.mkStepDiffsRecurse(MAX_NAME_LEN-3, 0)
}
func (nc *nameConverter) mkStepDiffsRecurse(spacesToLeft, leftmostLabelLen int) *big.Int {
num := &nc.stepDiffs[spacesToLeft][leftmostLabelLen]
if num.Cmp(big0) != 0 {
return num
}
roomForNewLabel := spacesToLeft >= 2
roomForLabelExpansion := spacesToLeft >= 1 && (leftmostLabelLen+1) < MAX_LABEL_LEN
if roomForNewLabel {
addend := nc.mkStepDiffsRecurse(spacesToLeft-2, 0)
num.Add(num, addend)
}
if roomForLabelExpansion {
addend := nc.mkStepDiffsRecurse(spacesToLeft-1, leftmostLabelLen+1)
num.Add(num, addend)
}
num.Mul(num, nc.alphaLen)
num.Add(num, big1)
// already updated in nc.stepDiffs
return num
}
func (nc *nameConverter) mkExpandDiffs() {
// expandDiffs should be set already lol
for spacesToLeft := 1; spacesToLeft < MAX_NAME_LEN-1; spacesToLeft++ {
idx := MAX_NAME_LEN - 4 - spacesToLeft
num := &nc.expandDiffs[spacesToLeft-1]
if idx < 0 {
num.SetInt64(1)
continue
}
num.Set(&nc.stepDiffs[idx][0])
num.Mul(num, nc.alphaLen)
num.Add(num, big1)
}
}
func (nc *nameConverter) nameToNum(name dns.Name) (*big.Int, error) {
labels := name.SplitRaw()
num := big.NewInt(0)
tmp := &big.Int{}
for len(labels) > 0 {
firstLabel := labels[0]
// stray end label
if firstLabel == nc.alphabet[:1] {
num.Add(num, big1)
labels = labels[1:]
continue
}
currentNameLen := len(labels) + 1
for _, label := range labels {
currentNameLen += len(label)
}
spacesToLeft := MAX_NAME_LEN - currentNameLen
lastChar := firstLabel[len(firstLabel)-1:]
// negate expansion
if lastChar == nc.alphabet[:1] {
idx := MAX_NAME_LEN - spacesToLeft - 3 - 1
expand := &nc.expandDiffs[idx]
num.Add(num, expand)
labels[0] = firstLabel[:len(firstLabel)-1]
continue
}
// step
leftmostLabelLen := len(firstLabel)
step := &nc.stepDiffs[spacesToLeft][leftmostLabelLen-1]
stepOff := strings.Index(nc.alphabet, lastChar)
if stepOff == -1 {
return nil, errNameCharset
}
tmp.SetInt64(int64(stepOff))
tmp.Mul(tmp, step)
num.Add(num, tmp)
labels[0] = firstLabel[:leftmostLabelLen-1] + nc.alphabet[:1]
}
return num, nil
}
func (nc *nameConverter) numToName(num *big.Int) (dns.Name, error) {
if !(num != nil && num.Cmp(big0) >= 0 && num.Cmp(nc.maxNameNum) <= 0) {
return dns.Name{}, errInvalidNameNum
}
var labelsB [][]byte
if num.Cmp(big0) == 0 {
return dns.NameFromString(".")
}
num.Sub(num, big1)
labelsB = [][]byte{{nc.alphabet[0]}}
steps := &big.Int{}
for num.Cmp(big0) == 1 {
firstLabel := labelsB[0]
lastChar := string(firstLabel[len(firstLabel)-1:])
// step
currentNameLen := len(labelsB) + 1
for _, label := range labelsB {
currentNameLen += len(label)
}
spacesToLeft := MAX_NAME_LEN - currentNameLen
leftmostLabelLen := len(firstLabel)
step := &nc.stepDiffs[spacesToLeft][leftmostLabelLen-1]
steps.QuoRem(num, step, num)
if steps.Cmp(big0) == 1 {
vIDX := strings.Index(nc.alphabet, lastChar)
vIDX += int(steps.Int64())
v := nc.alphabet[vIDX]
// labelsB[0] = firstLabel[:len(firstLabel)-2] + v
labelsB[0][leftmostLabelLen-1] = v
continue
}
// expand
idx := MAX_NAME_LEN - spacesToLeft - 3
if leftmostLabelLen < MAX_LABEL_LEN && idx >= 0 {
expand := &nc.expandDiffs[idx]
if expand.Cmp(num) <= 0 {
num.Sub(num, expand)
labelsB[0] = append(labelsB[0], nc.alphabet[0])
continue
}
}
// add new label
num.Sub(num, big1)
labelsB = slices.Insert(labelsB, 0, []byte{nc.alphabet[0]})
}
labels := make([]string, len(labelsB))
for i, v := range labelsB {
labels[i] = string(v)
}
return dns.NameFromLabels(labels)
}
// last encodable name still falling under the given zone forin the given alphabet
func (nc *nameConverter) getZoneEndNum(zone dns.Name) (*big.Int, error) {
idx := MAX_NAME_LEN - zone.EncodedLen() - 2
if idx < 0 {
return nil, errNameTooLongForZoneEnd
}
num, err := nc.nameToNum(zone)
if err != nil {
return nil, err
}
tmp := (&big.Int{}).Set(&nc.stepDiffs[idx][0])
tmp.Mul(tmp, nc.alphaLen)
num.Add(num, tmp)
return num, nil
}