-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
109 lines (103 loc) · 1.81 KB
/
utils.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
package main
import (
"encoding/hex"
"fmt"
"io"
"net/url"
)
type Calendars []*url.URL
func (cs *Calendars) String() (str string) {
for _, val := range *cs {
str += fmt.Sprintf(" %s", val.String())
}
return
}
func (cs *Calendars) Set(value string) error {
if u, e := url.Parse(value); e != nil {
return e
} else {
*cs = append(*cs, u)
}
return nil
}
func write_varint(w io.Writer, j int64) (int64, error) {
if j < 0 {
return 0, fmt.Errorf("must be non-negative")
}
for {
if j > 127 {
if _, e := w.Write([]byte{128 + byte(j%128)}); e != nil {
return 0, e
}
j = j / 128
} else {
k, e := w.Write([]byte{byte(j)})
return int64(k), e
}
}
}
func read_varint(r io.Reader) (j int64) {
var b [1]byte
builder := make([]byte, 0)
for {
if _, e := r.Read(b[:]); e != nil {
panic(e)
}
if b[0] > 128 {
builder = append(builder, b[0]-128)
} else {
builder = append(builder, b[0])
break
}
}
var power int64 = 1
for _, v := range builder {
j += int64(v) * power
power *= 128
}
return
}
func (p *Proof) WriteTo(f io.Writer) (n int64, err error) {
if k, e := f.Write(HEADER_MAGIC[:]); e != nil {
err = e
return
} else {
n += int64(k)
}
if k, e := f.Write([]byte{MAJOR_VERSION, 0x08}); e != nil {
err = e
return
} else {
n += int64(k)
}
if k, e := f.Write(p.Leaf.digest[:]); e != nil {
err = e
return
} else {
n += int64(hex.EncodedLen(k))
}
for _, i := range p.Proof {
if k, e := f.Write([]byte{i.Tag}); e != nil {
err = e
return
} else {
n += int64(k)
}
switch {
case i.Tag == 0xf1 || i.Tag == 0xf0:
if k, e := write_varint(f, int64(len(i.Arg))); e != nil {
err = e
return
} else {
n += int64(k)
}
if k, e := f.Write(i.Arg); e != nil {
err = e
return
} else {
n += int64(k)
}
}
}
return
}