forked from glycerine/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils2_test.go
386 lines (334 loc) · 10.4 KB
/
utils2_test.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package capn_test
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"unsafe"
)
var zerohi32 uint64
func init() {
// initialize zerohi32 once
var minus1 int32 = -1
u32 := uint32(minus1)
zerohi32 = uint64(u32)
}
func CapnpEncode(msg string, typ string) []byte {
capnpPath, err := exec.LookPath("capnp")
//capnpPath, err := exec.LookPath("tee")
if err != nil {
panic(err)
}
if !FileExists(capnpPath) {
panic(fmt.Sprintf("could not locate capnp tool in PATH"))
}
schfn := "aircraftlib/aircraft.capnp"
args := []string{"encode", schfn, typ}
cmdline := fmt.Sprintf("%s %s %s %s", capnpPath, "encode", schfn, typ)
//fmt.Printf("cmdline = %s\n", cmdline)
c := exec.Command(capnpPath, args...)
var o bytes.Buffer
c.Stdout = &o
var in bytes.Buffer
in.Write([]byte(msg))
c.Stdin = &in
err = c.Run()
if err != nil {
panic(fmt.Errorf("tried to run %s, got err:%s", cmdline, err))
}
return o.Bytes()
}
func CapnpDecode(input []byte, typ string) []byte {
capnpPath, err := exec.LookPath("capnp")
//capnpPath, err := exec.LookPath("tee")
if err != nil {
panic(err)
}
if !FileExists(capnpPath) {
panic(fmt.Sprintf("could not locate capnp tool in PATH"))
}
schfn := "aircraftlib/aircraft.capnp"
args := []string{"decode", "--short", schfn, typ}
cmdline := fmt.Sprintf("%s %s %s %s %s", capnpPath, "decode", "--short", schfn, typ)
fmt.Printf("cmdline = %s\n", cmdline)
c := exec.Command(capnpPath, args...)
var o bytes.Buffer
c.Stdout = &o
var e bytes.Buffer
c.Stderr = &e
var in bytes.Buffer
in.Write(input)
c.Stdin = &in
err = c.Run()
if err != nil {
fmt.Printf("tried to run %s, got err:%s and stderr: '%s'", cmdline, err, e.Bytes())
panic(err)
}
return o.Bytes()
}
func MakeAndMoveToTempDir() (origdir string, tmpdir string) {
var err error
origdir, err = os.Getwd()
if err != nil {
panic(err)
}
tmpdir, err = ioutil.TempDir(origdir, "tempgocapnpdir")
if err != nil {
panic(err)
}
err = os.Chdir(tmpdir)
if err != nil {
panic(err)
}
return origdir, tmpdir
}
func TempDirCleanup(origdir string, tmpdir string) {
// cleanup
os.Chdir(origdir)
err := os.RemoveAll(tmpdir)
if err != nil {
panic(err)
}
}
func ShowBytes(b []byte, indent int) {
c := NewCap()
k := 0
ind := strings.Repeat(" ", indent)
fmt.Printf("\n%s", ind)
line := 0
for i := 0; i < len(b)/8; i++ {
for j := 0; j < 8; j++ {
fmt.Printf("%02x ", b[k])
k++
if k == len(b) {
break
}
}
fmt.Printf(" ==(line %02d)> %s\n%s", line, c.Interp(line, binary.LittleEndian.Uint64(b[k-8:k]), b), ind)
line++
}
}
type Cap struct {
nextTag bool
expected map[int]string
}
func NewCap() *Cap {
return &Cap{
expected: make(map[int]string),
}
}
func (c *Cap) Interp(line int, val uint64, b []byte) string {
r := ""
// allowing store of state and re-discovery
if k, ok := c.expected[line]; ok {
return k
}
if line == 0 {
numSeg := val&zerohi32 + 1
words := val >> 32
return fmt.Sprintf("stream header: %d segment(s), this segment has %d words", numSeg, words)
} else {
// assume single segment for now
switch A(val) {
case structPointer:
return c.StructPointer(val, line)
case listPointer:
//fmt.Printf("\ndetected List with element count = %d (unless this is a composite). ListB = %d, ListC = %d\n", ListD(val), B(val), ListC(val))
if ListC(val) == bit1List {
listSize := ListD(val)
bytesRequired := (listSize + 7) / 8
szBytesWordBoundary := (bytesRequired + 7) &^ 7
eline := line + 1 + B(val)
listContent := BytesToWordString(b[eline*8 : (eline*8 + szBytesWordBoundary)])
c.expected[eline] = fmt.Sprintf("bit-list contents: %s", listContent)
return fmt.Sprintf("list of %d bits (pointer to: '%s' at line %d)", listSize, listContent, eline)
}
if ListC(val) == byte1List {
// assume it will be text
eline := line + 1 + B(val)
c.expected[eline] = fmt.Sprintf("text contents: %s", string(b[eline*8:(eline*8+ListD(val))]))
return fmt.Sprintf("list of bytes/Text (pointer to: '%s' at line %d)", string(b[eline*8:(eline*8+ListD(val)-1)]), eline)
}
if ListC(val) == compositeList {
c.nextTag = true
tagline := line + 1 + B(val)
tag := binary.LittleEndian.Uint64(b[(tagline)*8 : (tagline+1)*8])
r = fmt.Sprintf("list-of-composite, count: %d. (from tag at line %d). total-words-not-counting-tag-word: %d", B(tag), line+1+B(val), ListD(val))
c.expected[tagline] = CompositeTag(tag)
return r
}
eline := line + 1 + B(val)
return fmt.Sprintf("list, first element starts %d words from here (at line %d). Size: %s, num-elem: %d", B(val), eline, ListCString(val), ListD(val))
default:
r += "other"
}
}
return r
}
// lsb struct pointer msb
// +-+-----------------------------+---------------+---------------+
// |A| B | C | D |
// +-+-----------------------------+---------------+---------------+
//
// A (2 bits) = 0, to indicate that this is a struct pointer.
// B (30 bits) = Offset, in words, from the end of the pointer to the
// start of the struct's data section. Signed.
// C (16 bits) = Size of the struct's data section, in words.
// D (16 bits) = Size of the struct's pointer section, in words.
//
// (B is the same for list pointers, but C and D have different size
// and meaning)
//
// B(): extract the count from the B section of a struct pointer
// a.k.a. signedOffsetFromStructPointer()
func B(val uint64) int {
u64 := uint64(val) & zerohi32
u32 := uint32(u64)
s32 := int32(u32) >> 2
return int(s32)
}
func A(val uint64) int {
return int(val & 3)
}
func StructC(val uint64) int {
return int(uint16(val >> 32))
}
func StructD(val uint64) int {
return int(uint16(val >> 48))
}
func ListC(val uint64) int {
return int((val >> 32) & 7)
}
func ListCString(val uint64) string {
switch ListC(val) {
case voidList:
return "void"
case bit1List:
return "1bit"
case byte1List:
return "1byte"
case byte2List:
return "2bytes"
case byte4List:
return "4bytes"
case byte8List:
return "8bytes"
case pointerList:
return "pointer"
case compositeList:
return "composite"
default:
panic("unknown list element size")
}
return ""
}
func ListD(val uint64) int {
return int(uint32(val >> 35))
}
const (
structPointer = 0
listPointer = 1
farPointer = 2
doubleFarPointer = 6
voidList = 0
bit1List = 1
byte1List = 2
byte2List = 3
byte4List = 4
byte8List = 5
pointerList = 6
compositeList = 7
)
/*
lsb list pointer msb
+-+-----------------------------+--+----------------------------+
|A| B |C | D |
+-+-----------------------------+--+----------------------------+
A (2 bits) = 1, to indicate that this is a list pointer.
B (30 bits) = Offset, in words, from the end of the pointer to the
start of the first element of the list. Signed.
C (3 bits) = Size of each element:
0 = 0 (e.g. List(Void))
1 = 1 bit
2 = 1 byte
3 = 2 bytes
4 = 4 bytes
5 = 8 bytes (non-pointer)
6 = 8 bytes (pointer)
7 = composite (see below)
D (29 bits) = Number of elements in the list, except when C is 7
(see below).
The pointed-to values are tightly-packed. In particular, Bools are packed bit-by-bit in little-endian order (the first bit is the least-significant bit of the first byte).
Lists of structs use the smallest element size in which the struct can fit. So, a list of structs that each contain two UInt8 fields and nothing else could be encoded with C = 3 (2-byte elements). A list of structs that each contain a single Text field would be encoded as C = 6 (pointer elements). A list of structs that each contain a single Bool field would be encoded using C = 1 (1-bit elements). A list structs which are each more than one word in size must be be encoded using C = 7 (composite).
When C = 7, the elements of the list are fixed-width composite values – usually, structs. In this case, the list content is prefixed by a "tag" word that describes each individual element. The tag has the same layout as a struct pointer, except that the pointer offset (B) instead indicates the number of elements in the list. Meanwhile, section (D) of the list pointer – which normally would store this element count – instead stores the total number of words in the list (not counting the tag word). The reason we store a word count in the pointer rather than an element count is to ensure that the extents of the list’s location can always be determined by inspecting the pointer alone, without having to look at the tag; this may allow more-efficient prefetching in some use cases. The reason we don’t store struct lists as a list of pointers is because doing so would take significantly more space (an extra pointer per element) and may be less cache-friendly.
*/
func CompositeTag(val uint64) string {
//return fmt.Sprintf("composite-tag, num elements in list: %d. Each elem: {prim: %d words. pointers: %d words}.", B(val), StructC(val), StructD(val))
return fmt.Sprintf("composite-tag {prim: %d, pointers: %d words}.", StructC(val), StructD(val))
}
func (c *Cap) StructPointer(val uint64, line int) string {
if val == 0 {
return "empty struct, zero valued."
}
eline := line + 1 + B(val)
numprim := StructC(val)
if numprim > 0 {
for i := 0; i < numprim; i++ {
c.expected[eline+i] = fmt.Sprintf("primitive data for struct on line %d", line)
}
}
return fmt.Sprintf("struct-pointer, data starts at +%d words (line %d). {prim: %d, pointers: %d words}.", B(val), eline, StructC(val), StructD(val))
}
func save(b []byte, fn string) {
file, err := os.Create(fn)
if err != nil {
panic(err)
}
file.Write(b)
file.Close()
}
func InspectSlice(slice []byte) {
// Capture the address to the slice structure
address := unsafe.Pointer(&slice)
// Create a pointer to the underlying array
addPtr := (*[8]byte)(unsafe.Pointer(*(*uintptr)(address)))
fmt.Printf("underlying array Addr[%p]\n", addPtr)
fmt.Printf("\n\n")
}
func FileExists(name string) bool {
fi, err := os.Stat(name)
if err != nil {
return false
}
if fi.IsDir() {
return false
}
return true
}
func DirExists(name string) bool {
fi, err := os.Stat(name)
if err != nil {
return false
}
if fi.IsDir() {
return true
}
return false
}
func BytesToWordString(b []byte) string {
var s string
k := 0
for i := 0; i < len(b)/8; i++ {
for j := 0; j < 8; j++ {
s += fmt.Sprintf("%02x ", b[k])
k++
if k == len(b) {
break
}
}
}
return s
}