-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlookup_cbor.go
229 lines (197 loc) · 5.61 KB
/
lookup_cbor.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
package gson
import "strconv"
import "bytes"
import "fmt"
func cborGet(doc []byte, segments [][]byte, item []byte, config *Config) int {
_, _, start, end := cborLookup(doc, segments)
if start < 0 {
key := bytes2str(segments[len(segments)-1])
panic(fmt.Sprintf("key %v not found", key))
}
return copy(item, doc[start:end])
}
func cborSet(
doc []byte, segments [][]byte, item, newdoc, olditem []byte,
config *Config) (int, int) {
var x int
cont, _, start, end := cborLookup(doc, segments)
m := copy(newdoc, doc[:cont])
if start < 0 {
x, m = addlength(cont, m, doc, newdoc)
key := bytes2str(segments[len(segments)-1])
m += valtext2cbor(key, newdoc[m:])
m += copy(newdoc[m:], item)
m += copy(newdoc[m:], doc[x:])
n := copy(olditem, item)
return m, n
}
m += copy(newdoc[m:], doc[cont:start])
m += copy(newdoc[m:], item)
m += copy(newdoc[m:], doc[end:])
n := copy(olditem, doc[start:end])
return m, n
}
func cborPrepend(
doc []byte, segments [][]byte, item, newdoc []byte, config *Config) int {
var x int
_, _, start, _ := cborLookup(doc, segments)
major, _ := cborMajor(doc[start]), cborInfo(doc[start])
if major != cborType4 {
panic("cannot prepend to non array containers")
}
m := copy(newdoc, doc[:start])
x, m = addlength(start, m, doc, newdoc)
m += copy(newdoc[m:], item)
m += copy(newdoc[m:], doc[x:])
return m
}
func cborAppend(
doc []byte, segments [][]byte, item, newdoc []byte, config *Config) int {
var x int
_, _, start, end := cborLookup(doc, segments)
major, info := cborMajor(doc[start]), cborInfo(doc[start])
if major != cborType4 {
panic("cannot append to non array containers")
}
m := copy(newdoc, doc[:start])
x, m = addlength(start, m, doc, newdoc)
if info == cborIndefiniteLength {
m += copy(newdoc[m:], doc[x:end-1])
m += copy(newdoc[m:], item)
m += copy(newdoc[m:], doc[end-1:])
return m
}
m += copy(newdoc[m:], doc[x:end])
m += copy(newdoc[m:], item)
m += copy(newdoc[m:], doc[end:])
return m
}
func cborDel(
doc []byte, segments [][]byte, newdoc, deleted []byte,
config *Config) (int, int) {
var x int
cont, keyn, start, end := cborLookup(doc, segments)
m := copy(newdoc, doc[:cont])
major, _ := cborMajor(doc[cont]), cborInfo(doc[cont])
switch major {
case cborType4:
if keyn >= 0 {
panic("cborType4 expected keyn to be -1")
}
x, m = deletelength(cont, m, doc, newdoc)
m += copy(newdoc[m:], doc[x:start])
m += copy(newdoc[m:], doc[end:])
n := copy(deleted, doc[start:end])
return m, n
case cborType5:
if keyn < 0 {
panic("cborType5 expected keyn to be > 0")
}
x, m = deletelength(cont, m, doc, newdoc)
m += copy(newdoc[m:], doc[x:keyn])
m += copy(newdoc[m:], doc[end:])
n := copy(deleted, doc[start:end])
return m, n
}
panic("unreachable code")
}
func cborLookup(doc []byte, segments [][]byte) (cont, keyn, start, end int) {
var ln int
end = len(doc)
nextseg:
for i, segment := range segments {
major, info := cborMajor(doc[start]), cborInfo(doc[start])
switch major {
case cborType4:
idx, count := segment2idx(segment), 0
cont, keyn = start, -1
if info == cborIndefiniteLength {
for end = start + 1; doc[end] != brkstp; count++ {
_, n := cborItem(doc[end:])
start, end = end, end+n
if count == idx {
continue nextseg
}
}
panic(fmt.Sprintf("index %v overflow", idx))
}
ln, end = cborItemLength(doc[start:])
end += start
for ; count < ln; count++ {
_, n := cborItem(doc[end:])
start, end = end, end+n
if count == idx {
continue nextseg
}
}
panic(fmt.Sprintf("index %v overflow", idx))
case cborType5:
cont = start
if info == cborIndefiniteLength {
for end = start + 1; doc[end] != brkstp; {
_, m := cborItem(doc[end:])
_, n := cborItem(doc[end+m:])
keyn, start, end = end, end+m, end+m+n
x, y := cborItemLength(doc[keyn:])
if bytes.Compare(doc[keyn+y:keyn+y+x], segment) == 0 {
continue nextseg
}
}
if i == (len(segments) - 1) { // leaf
return cont, -1, -1, -1
}
panic(fmt.Sprintf("key %v not found", bytes2str(segment)))
}
ln, end = cborItemLength(doc[start:])
end += start
for i := 0; i < ln; i++ {
_, m := cborItem(doc[end:])
_, n := cborItem(doc[end+m:])
keyn, start, end = end, end+m, end+m+n
x, y := cborItemLength(doc[keyn:])
if bytes.Compare(doc[keyn+y:keyn+y+x], segment) == 0 {
continue nextseg
}
}
if i == (len(segments) - 1) { // missing
return cont, -1, -1, -1
}
panic(fmt.Sprintf("key %v not found", bytes2str(segment)))
}
}
return
}
func segment2idx(segment []byte) int {
idx, err := strconv.Atoi(bytes2str(segment))
if err != nil {
fmsg := "pointer %v expected to be array index"
panic(fmt.Sprintf(fmsg, bytes2str(segment)))
} else if idx < 0 {
panic(fmt.Sprintf("array index %v can be < 0", idx))
}
return idx
}
func addlength(cont, m int, doc, newdoc []byte) (int, int) {
var x, ln int
major, info := cborMajor(doc[cont]), cborInfo(doc[cont])
if info == cborIndefiniteLength {
newdoc[m] = doc[cont]
return cont + 1, m + 1
}
ln, x = cborItemLength(doc[cont:])
y := valuint642cbor(uint64(ln+1), newdoc[m:])
newdoc[m] = (newdoc[m] & 0x1f) | major // fix the type from type0->type4
return cont + x, m + y
}
func deletelength(cont, m int, doc, newdoc []byte) (int, int) {
var x, ln int
major, info := cborMajor(doc[cont]), cborInfo(doc[cont])
if info == cborIndefiniteLength {
newdoc[m] = doc[cont]
return cont + 1, m + 1
}
ln, x = cborItemLength(doc[cont:])
y := valuint642cbor(uint64(ln-1), newdoc[m:])
newdoc[m] = (newdoc[m] & 0x1f) | major // fix the type from type0->type4
return x + cont, m + y
}