forked from heistp/irtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbuf.go
265 lines (226 loc) · 5.02 KB
/
fbuf.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
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
package nlmt
import "fmt"
// field
type field struct {
pos int
len int
cap int
}
// field index
type fidx int
// fbuf provides access to fields in a byte buffer, each with a position, length
// and capacity. Each field must have a length of either 0 or the field's
// capacity, so that the structure of the buffer can be externalized simply as
// which fields are set. tlen sets a target buffer length, and the payload is
// the padding after the fields needed to meet the target length. The length of
// the buffer must always be at least the length of the fields.
type fbuf struct {
// buffer
buf []byte
// fields
fields []field
// target length
tlen int
}
func newFbuf(fields []field, tlen int, cap int) *fbuf {
blen, fcap := sumFields(fields)
if tlen > blen {
blen = tlen
}
if fcap > cap {
cap = fcap
}
return &fbuf{make([]byte, blen, cap), fields, tlen}
}
func (fb *fbuf) validate() error {
flen, fcap := fb.sumFields()
if flen > len(fb.buf) {
return Errorf(FieldsLengthTooLarge,
"fields length exceeds buffer length, %d > %d", flen, len(fb.buf))
}
if fcap > cap(fb.buf) {
return Errorf(FieldsCapacityTooLarge,
"fields capacity exceeds buffer capacity, %d > %d", fcap, cap(fb.buf))
}
return nil
}
// setFields and addFields are used when changing fields for an existing buffer
func (fb *fbuf) setFields(fidxs []fidx, setLen bool) error {
pos := 0
j := 0
for i := 0; i < len(fidxs); i, j = i+1, j+1 {
for ; j < len(fb.fields); j++ {
if j == int(fidxs[i]) {
fb.fields[j].pos = pos
fb.fields[j].len = fb.fields[j].cap
pos += fb.fields[j].len
break
}
fb.fields[j].len = 0
fb.fields[j].pos = pos
}
}
for ; j < len(fb.fields); j++ {
fb.fields[j].len = 0
fb.fields[j].pos = pos
}
if setLen {
fb.setLen(pos)
}
return fb.validate()
}
func (fb *fbuf) addFields(fidxs []fidx, setLen bool) error {
pos := 0
j := 0
for i := 0; i < len(fidxs); i, j = i+1, j+1 {
for ; j < len(fb.fields); j++ {
if j == int(fidxs[i]) {
fb.fields[j].pos = pos
fb.fields[j].len = fb.fields[j].cap
pos += fb.fields[j].len
break
}
fb.fields[j].pos = pos
pos += fb.fields[j].len
}
}
for ; j < len(fb.fields); j++ {
fb.fields[j].pos = pos
pos += fb.fields[j].len
}
if setLen {
fb.setLen(pos)
}
return fb.validate()
}
// setters
func (fb *fbuf) set(f fidx, b []byte) {
p, l, c := fb.field(f)
if len(b) != c {
panic(fmt.Sprintf("set for field %d with size %d != field cap %d", f, len(b), c))
}
if l != c {
fb.setFieldLen(f, c)
}
copy(fb.buf[p:p+c], b)
}
func (fb *fbuf) setTo(f fidx) []byte {
p, l, c := fb.field(f)
if l != c {
fb.setFieldLen(f, c)
}
return fb.buf[p : p+c]
}
func (fb *fbuf) setb(f fidx, b byte) {
p, l, c := fb.field(f)
if c != 1 {
panic("setb only for one byte fields")
}
if l != 1 {
fb.setFieldLen(f, 1)
}
fb.buf[p] = b
}
func (fb *fbuf) setPayload(b []byte) {
flen := fb.sumLens()
fb.buf = fb.buf[:flen+len(b)]
copy(fb.buf[flen:], b)
}
func (fb *fbuf) zeroPayload() {
zero(fb.payload())
}
func (fb *fbuf) zero(f fidx) {
zero(fb.setTo(f))
}
// getters
func (fb *fbuf) get(f fidx) []byte {
p, l, _ := fb.field(f)
return fb.buf[p : p+l]
}
func (fb *fbuf) getb(f fidx) byte {
p, l, _ := fb.field(f)
if l != 1 {
panic(fmt.Sprintf("getb for non-byte field %d", f))
}
return fb.buf[p]
}
func (fb *fbuf) isset(f fidx) bool {
return fb.fields[f].len > 0
}
func (fb *fbuf) bytes() []byte {
return fb.buf
}
func (fb *fbuf) payload() []byte {
flen := fb.sumLens()
return fb.buf[flen:]
}
// length and capacity
func (fb *fbuf) length() int {
return len(fb.buf)
}
func (fb *fbuf) capacity() int {
return cap(fb.buf)
}
func (fb *fbuf) setLen(tlen int) int {
fb.tlen = tlen
flen := fb.sumLens()
l := tlen
if l < flen {
l = flen
}
if l > cap(fb.buf) {
l = cap(fb.buf)
}
fb.buf = fb.buf[:l]
return l
}
// removal
func (fb *fbuf) remove(f fidx) {
if fb.fields[f].len > 0 {
fb.setFieldLen(f, 0)
}
}
// internal methods
func (fb *fbuf) field(f fidx) (int, int, int) {
return fb.fields[f].pos, fb.fields[f].len, fb.fields[f].cap
}
func (fb *fbuf) setFieldLen(f fidx, newlen int) {
p, l, _ := fb.field(f)
grow := newlen - l
if grow != 0 {
// grow or shrink the buffer and shift bytes
//fmt.Printf("f=%d, newlen=%d, l=%d, len=%d, cap=%d, grow=%d\n",
// f, newlen, l, len(fb.buf), cap(fb.buf), grow)
fb.buf = fb.buf[:len(fb.buf)+grow]
copy(fb.buf[p+grow:], fb.buf[p:])
// update field length
fb.fields[f].len = newlen
// update field positions
newp := fb.fields[f].pos
for i := f; i < fidx(len(fb.fields)); i++ {
fb.fields[i].pos = newp
newp += fb.fields[i].len
}
// update total field length and reset to target length
flen := fb.sumLens()
if fb.tlen >= flen {
fb.buf = fb.buf[0:fb.tlen]
}
}
}
func (fb *fbuf) sumFields() (flen int, fcap int) {
return sumFields(fb.fields)
}
func (fb *fbuf) sumLens() (flen int) {
for _, f := range fb.fields {
flen += f.len
}
return
}
func sumFields(fields []field) (flen int, fcap int) {
for _, f := range fields {
flen += f.len
fcap += f.cap
}
return
}