Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

gov_codec.gno

1.20 Kb · 50 lines
 1package memba_notes_v1
 2
 3import (
 4	"encoding/binary"
 5)
 6
 7const MaxGovPayloadBytes = MaxCipherTitleBytes + MaxCipherBodyBytes + MaxManifestBytes + 128
 8
 9type govReader struct {
10	bytes []byte
11	pos   int
12}
13
14func payloadReader(payload []byte) *govReader {
15	if len(payload) < 1 || len(payload) > MaxGovPayloadBytes || payload[0] != 1 {
16		panic("notes: gov payload format")
17	}
18	return &govReader{payload, 1}
19}
20func (r *govReader) take(n int) []byte {
21	if n < 0 || n > len(r.bytes)-r.pos {
22		panic("notes: gov payload truncated")
23	}
24	b := r.bytes[r.pos : r.pos+n]
25	r.pos += n
26	return b
27}
28func (r *govReader) byte() uint8 { return r.take(1)[0] }
29func (r *govReader) boolean() bool {
30	v := r.byte()
31	if v > 1 {
32		panic("notes: gov bool")
33	}
34	return v == 1
35}
36func (r *govReader) u64() uint64  { return binary.BigEndian.Uint64(r.take(8)) }
37func (r *govReader) u32() uint32  { return binary.BigEndian.Uint32(r.take(4)) }
38func (r *govReader) id() [16]byte { var v [16]byte; copy(v[:], r.take(16)); return v }
39func (r *govReader) blob(cap int) []byte {
40	n := r.u32()
41	if n > uint32(cap) {
42		panic("notes: gov field cap")
43	}
44	return r.take(int(n))
45}
46func (r *govReader) done() {
47	if r.pos != len(r.bytes) {
48		panic("notes: gov trailing bytes")
49	}
50}