-
Notifications
You must be signed in to change notification settings - Fork 1
/
X.go
223 lines (206 loc) · 3.73 KB
/
X.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
package rdx
import (
hex2 "encoding/hex"
"github.com/drpcorg/chotki/protocol"
)
func Xparse(rdt byte, val string) (tlv []byte) {
switch rdt {
case 'F':
tlv = Fparse(val)
case 'I':
tlv = Iparse(val)
case 'R':
tlv = Rparse(val)
case 'S':
tlv = Sparse(val)
case 'T':
tlv = Tparse(val)
case 'N':
tlv = Nparse(val)
case 'Z':
tlv = Zparse(val)
}
return
}
func FIRSTparsee(rdt byte, val string) (tlv []byte) {
switch rdt {
case 'F':
tlv = protocol.Record(rdt, Fparse(val))
case 'I':
tlv = protocol.Record(rdt, Iparse(val))
case 'R':
tlv = protocol.Record(rdt, Rparse(val))
case 'S':
tlv = protocol.Record(rdt, Sparse(val))
case 'T':
tlv = protocol.Record(rdt, Tparse(val))
}
return
}
func Xmerge(rdt byte, tlvs [][]byte) (tlv []byte) {
switch rdt {
case 'O', 'Y': // object's ref is immutable
tlv = NOmerge(tlvs)
case 'C':
tlv = Amerge(tlvs)
case 'F':
tlv = Fmerge(tlvs)
case 'I':
tlv = Imerge(tlvs)
case 'R':
tlv = Rmerge(tlvs)
case 'S':
tlv = Smerge(tlvs)
case 'T':
tlv = Tmerge(tlvs)
case 'N':
tlv = Nmerge(tlvs)
case 'Z':
tlv = Zmerge(tlvs)
case 'E':
tlv = Emerge(tlvs)
case 'L':
tlv = Lmerge(tlvs)
case 'M':
tlv = Mmerge(tlvs)
case 'V':
tlv = Vmerge(tlvs)
default:
tlv = NoMerge(tlvs)
}
return
}
func Xstring(rdt byte, tlv []byte) string {
switch rdt {
case 'C':
return Cstring(tlv)
case 'O', 'Y':
return OYstring(tlv)
case 'F':
return Fstring(tlv)
case 'I':
return Istring(tlv)
case 'R':
return Rstring(tlv)
case 'S':
return Sstring(tlv)
case 'T':
return Tstring(tlv)
case 'N':
return Nstring(tlv)
case 'M':
return Mstring(tlv)
case 'V':
return Vstring(tlv)
default:
hex := make([]byte, len(tlv)*2)
hex2.Encode(hex, tlv)
return string(hex)
}
}
func Xdefault(rdt byte) (tlv []byte) {
switch rdt {
case 'C', 'O', 'Y':
return COYdefault()
case 'F', 'I', 'R', 'S', 'T':
return FIRSTdefault(rdt)
case 'N':
return Ndefault()
case 'E', 'L', 'M':
return ELMdefault()
default:
return nil
}
}
func Xvalid(rdt byte, bare []byte) bool {
switch rdt {
case 'C', 'O', 'Y':
return OValid(bare)
case 'F':
return Fvalid(bare)
case 'I':
return Ivalid(bare)
case 'R':
return Rvalid(bare)
case 'S':
return Svalid(bare)
case 'T':
return Tvalid(bare)
case 'N':
return Nvalid(bare)
case 'Z':
return Zvalid(bare)
case 'E':
return Evalid(bare)
case 'L':
return Lvalid(bare)
case 'M':
return Mvalid(bare)
case 'V':
return Vvalid(bare)
default:
return false
}
}
func Xvalide(tlve []byte) bool {
rdt, hlen, blen := protocol.ProbeHeader(tlve)
if len(tlve) != hlen+blen {
return false
}
bare := tlve[hlen : hlen+blen]
return Xvalid(rdt, bare)
}
func X2string(rdt byte, tlv []byte, new_val string, src uint64) (delta []byte) {
switch rdt {
case 'C', 'O', 'Y':
delta = nil
case 'N':
delta = N2string(tlv, new_val, src)
default:
delta = nil
}
return
}
func Xdiff(rdt byte, tlv []byte, sendvv VV) (diff []byte) {
switch rdt {
case 'C', 'O', 'Y':
diff = nil
case 'F':
diff = Fdiff(tlv, sendvv)
case 'I':
diff = Idiff(tlv, sendvv)
case 'R':
diff = Rdiff(tlv, sendvv)
case 'S':
diff = Sdiff(tlv, sendvv)
case 'T':
diff = Tdiff(tlv, sendvv)
case 'N':
diff = Ndiff(tlv, sendvv)
default:
diff = tlv
}
return
}
func COYdefault() []byte {
return ID0.ZipBytes()
}
func NOmerge(inputs [][]byte) []byte {
return inputs[0]
}
func Cstring(tlv []byte) string {
return Mstring(tlv)
}
func OYstring(tlv []byte) string {
return IDFromZipBytes(tlv).String()
}
func NoMerge(inputs [][]byte) []byte {
ret := make([]byte, 0, protocol.TotalLen(inputs))
for _, input := range inputs {
ret = append(ret, input...)
}
return ret
}
func OValid(tlv []byte) bool {
return len(tlv) <= 16 && (ValidZipPairLen&(1<<len(tlv))) != 0
}