-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
134 lines (103 loc) · 3.42 KB
/
encoder.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
package jq
import "nikand.dev/go/cbor"
type (
Encoder struct {
CBOR cbor.Encoder
}
)
const (
// 0bxxx0_yyyy // xxx - Array or Map, yyyy (count) of 1-byte offsets (or pairs)
// 0bxxx1_llss // ll - length size, ss - offset size
// // 1<<ll elements of size 1<<ss bytes are followed. 1<<(ll+1) for Map
arrEmbedMask = 0b0001_0000
)
func MakeEncoder() Encoder { return Encoder{CBOR: cbor.MakeEncoder()} }
func (e Encoder) AppendArray(b []byte, off Off, items []Off) []byte {
return e.AppendArrayMap(b, cbor.Array, off, items)
}
func (e Encoder) AppendMap(b []byte, off Off, items []Off) []byte {
return e.AppendArrayMap(b, cbor.Map, off, items)
}
func (e Encoder) AppendArrayMap(b []byte, tag Tag, off Off, items []Off) []byte {
// reset := len(b)
tagLen := len(items)
if tag == cbor.Map {
tagLen /= 2
}
min := off
for _, item := range items {
if item < 0 {
continue
}
if item < min {
min = item
}
}
d := off - min
size := 0x100
if d < 0x100-offReserve && tagLen < 16 {
b = append(b, byte(tag)|byte(tagLen))
for _, item := range items {
if item < 0 {
b = append(b, byte(size)+byte(item))
continue
}
b = append(b, byte(off-item))
}
// log.Printf("append array/map %x %x % x -> 0 0 % x", tag, off, items, b[reset:])
return b
}
var ll, ss byte = 0, 0
for size = 0x100; tagLen >= size; {
ll++
size *= size
}
for size = 0x100; int(d) >= size-offReserve; {
ss++
size *= size
}
t := byte(tag) | 0b0001_0000 | ll<<2 | ss
b = append(b, t)
b = e.AppendIntX(b, 1<<ll, tagLen)
for _, item := range items {
if item < 0 {
b = e.AppendIntX(b, 1<<ss, size+int(item))
continue
}
b = e.AppendIntX(b, 1<<ss, int(off)-int(item))
}
// log.Printf("append array/map %x %x % x -> %d %d % x", tag, off, items, ll, ss, b[reset:])
return b
}
func (e Encoder) AppendIntX(b []byte, x, v int) []byte {
switch x {
case 1:
return append(b, byte(v))
case 2:
return append(b, byte(v>>8), byte(v))
case 4:
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
default:
panic("overflow")
}
}
func (e Encoder) AppendRaw(b, raw []byte) []byte { return append(b, raw...) }
func (e Encoder) AppendInt(b []byte, v int) []byte { return e.CBOR.AppendInt(b, v) }
func (e Encoder) AppendInt64(b []byte, v int64) []byte { return e.CBOR.AppendInt64(b, v) }
func (e Encoder) AppendUint(b []byte, v uint) []byte { return e.CBOR.AppendUint(b, v) }
func (e Encoder) AppendUint64(b []byte, v uint64) []byte { return e.CBOR.AppendUint64(b, v) }
func (e Encoder) AppendFloat(b []byte, v float64) []byte { return e.CBOR.AppendFloat(b, v) }
func (e Encoder) AppendFloat32(b []byte, v float32) []byte { return e.CBOR.AppendFloat32(b, v) }
func (e Encoder) AppendBool(b []byte, v bool) []byte { return e.CBOR.AppendBool(b, v) }
func (e Encoder) AppendNull(b []byte) []byte { return e.CBOR.AppendNull(b) }
func (e Encoder) AppendString(b []byte, v string) []byte { return e.CBOR.AppendString(b, v) }
func (e Encoder) AppendBytes(b []byte, v []byte) []byte { return e.CBOR.AppendBytes(b, v) }
func (e Encoder) AppendTagString(b []byte, tag Tag, v string) []byte {
return e.CBOR.AppendTagString(b, tag, v)
}
func (e Encoder) AppendTagBytes(b []byte, tag Tag, v []byte) []byte {
return e.CBOR.AppendTagBytes(b, tag, v)
}
func (e Encoder) AppendTagUnsigned(b []byte, tag Tag, v uint64) []byte {
return e.CBOR.AppendTagUnsigned(b, tag, v)
}