You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varerrorGarlicBridgeUpstreamNegativeHop=fmt.Errorf("A garlic bridge can't have a negative number of hops (your alterator is making this), check upstream.")
291
+
varerrorGarlicBridgeDownstreamNegativeHop=fmt.Errorf("A garlic bridge can't have a negative number of hops (your alterator is making this), check downstream.")
292
+
293
+
funcgarlicBridgeStB(sstring) ([]byte, error) {
294
+
sl:=strings.SplitN(s, "|", 7)
295
+
iflen(sl) !=6 {
296
+
returnnil, fmt.Errorf("A garlic bridge should have 6 params separated by 5 \"|\", not %d.", len(sl))
297
+
}
298
+
v1, err:=strconv.Atoi(sl[0])
299
+
iferr!=nil {
300
+
returnnil, err
301
+
}
302
+
ifv1<0||v1>7 {
303
+
returnnil, fmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check upstream.", v1)
304
+
}
305
+
v2, err:=strconv.Atoi(sl[1])
306
+
iferr!=nil {
307
+
returnnil, err
308
+
}
309
+
ifv2<0||v2>7 {
310
+
returnnil, fmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check downstream.", v2)
311
+
}
312
+
v3, err:=strconv.Atoi(sl[2])
313
+
iferr!=nil {
314
+
returnnil, err
315
+
}
316
+
ifv3<1||v3>6 {
317
+
returnnil, fmt.Errorf("A garlic bridge must have 1 up to 6 tunnel, not %d, check upstream.", v3)
318
+
}
319
+
v4, err:=strconv.Atoi(sl[3])
320
+
iferr!=nil {
321
+
returnnil, err
322
+
}
323
+
ifv4<1||v4>6 {
324
+
returnnil, fmt.Errorf("A garlic bridge must have 1 up to 6 tunnel, not %d, check downstream.", v4)
325
+
}
326
+
v5, err:=strconv.Atoi(sl[4])
327
+
iferr!=nil {
328
+
returnnil, err
329
+
}
330
+
ifv5<-1||v5>2 {
331
+
returnnil, fmt.Errorf("A garlic bridge alterator must be between -1 and 2 (included), not %d, check upstream.", v5)
332
+
}
333
+
ifv5==-1 {
334
+
ifv1==0 {
335
+
returnnil, errorGarlicBridgeUpstreamNegativeHop
336
+
}
337
+
v1-=1
338
+
v5=2
339
+
}
340
+
v6, err:=strconv.Atoi(sl[5])
341
+
iferr!=nil {
342
+
returnnil, err
343
+
}
344
+
ifv6<-1||v6>2 {
345
+
returnnil, fmt.Errorf("A garlic bridge alterator must be between -1 and 2 (included), not %d, check downstream.", v6)
346
+
}
347
+
ifv6==-1 {
348
+
ifv2==0 {
349
+
returnnil, errorGarlicBridgeDownstreamNegativeHop
350
+
}
351
+
v2-=1
352
+
v6=2
353
+
}
354
+
355
+
return []byte{
356
+
byte((v1<<5) + (v2<<2) + (v3>>1)),
357
+
byte((v3<<7) + (v4<<4) + (v5<<2) +v6),
358
+
}, nil
359
+
}
360
+
361
+
funcgarlicExtractor(b []byte) [6]uint8 {
362
+
// Don't transform the uint8 in a uint, this stricity is used to colide with
363
+
// border in conversion.
364
+
u:= [2]uint8{uint8(b[0]), uint8(b[1])}
365
+
varr [6]uint8
366
+
r[5] = (u[1] <<6) >>6
367
+
r[4] = (u[1] <<4) >>6
368
+
r[3] = (u[1] <<1) >>5
369
+
r[2] = ((u[0] <<6) >>5) + (u[1] >>7)
370
+
r[1] = (u[0] <<3) >>5
371
+
r[0] =u[0] >>5
372
+
373
+
returnr
374
+
}
375
+
376
+
funcgarlicBridgeBtS(b []byte) (string, error) {
377
+
varrsstring
378
+
fori, e:=rangegarlicExtractor(b) {
379
+
rs+=strconv.Itoa(int(e))
380
+
ifi!=5 {
381
+
rs+="|"
382
+
}
383
+
}
384
+
returnrs, nil
385
+
}
386
+
387
+
funcgarlicBridgeValidate(b []byte) error {
388
+
iflen(b) !=2 {
389
+
returnfmt.Errorf("A garlic bridge compiled version length must be 2, not %d.", len(b))
390
+
}
391
+
lv:=garlicExtractor(b)
392
+
393
+
iflv[0] >7 {
394
+
returnfmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check upstream.", lv[0])
395
+
}
396
+
iflv[1] >7 {
397
+
returnfmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check downstream.", lv[1])
398
+
}
399
+
iflv[2] <1||lv[2] >6 {
400
+
returnfmt.Errorf("A garlic bridge must have 1 up to 6 tunnel, not %d, check upstream.", lv[2])
401
+
}
402
+
iflv[3] <1||lv[3] >6 {
403
+
returnfmt.Errorf("A garlic bridge must have 1 up to 6 tunnel, not %d, check downstream.", lv[3])
404
+
}
405
+
iflv[4] >2 {
406
+
returnfmt.Errorf("A garlic bridge alterator compiled can't be bigger than 2, not %d, check upstream.", lv[4])
407
+
}
408
+
iflv[5] >2 {
409
+
returnfmt.Errorf("A garlic bridge alterator compiled can't be bigger than 2, not %d, check downstream.", lv[5])
0 commit comments