-
Notifications
You must be signed in to change notification settings - Fork 227
/
msgpack.go
161 lines (147 loc) · 6.29 KB
/
msgpack.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
package msgpack
// https://github.com/msgpack/msgpack/blob/master/spec.md
// TODO: ext types done correctly?
import (
"embed"
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
)
//go:embed msgpack.jq
//go:embed msgpack.md
var msgPackFS embed.FS
func init() {
interp.RegisterFormat(
format.MsgPack,
&decode.Format{
Description: "MessagePack",
DecodeFn: decodeMsgPack,
Functions: []string{"torepr"},
})
interp.RegisterFS(msgPackFS)
}
type formatEntry struct {
r [2]byte
s scalar.Uint
d func(d *decode.D)
}
type formatEntries []formatEntry
func (fes formatEntries) lookup(u byte) (formatEntry, bool) {
for _, fe := range fes {
if u >= fe.r[0] && u <= fe.r[1] {
return fe, true
}
}
return formatEntry{}, false
}
func (fes formatEntries) MapUint(s scalar.Uint) (scalar.Uint, error) {
u := s.Actual
if fe, ok := fes.lookup(byte(u)); ok {
s = fe.s
s.Actual = u
}
return s, nil
}
func decodeMsgPackValue(d *decode.D) {
arrayFn := func(seekBits int64, lengthBits int) func(d *decode.D) {
return func(d *decode.D) {
d.SeekRel(seekBits)
length := d.FieldU("length", lengthBits)
d.FieldArray("elements", func(d *decode.D) {
for i := uint64(0); i < length; i++ {
d.FieldStruct("element", decodeMsgPackValue)
}
})
}
}
mapFn := func(seekBits int64, lengthBits int) func(d *decode.D) {
return func(d *decode.D) {
d.SeekRel(seekBits)
length := d.FieldU("length", lengthBits)
d.FieldArray("pairs", func(d *decode.D) {
for i := uint64(0); i < length; i++ {
d.FieldStruct("pair", func(d *decode.D) {
d.FieldStruct("key", decodeMsgPackValue)
d.FieldStruct("value", decodeMsgPackValue)
})
}
})
}
}
extFn := func(lengthBits int) func(d *decode.D) {
return func(d *decode.D) {
length := d.FieldU8("length")
d.FieldS8("fixtype")
d.FieldRawLen("value", int64(length)*8)
}
}
// is defined here as a global map would cause a init dependency cycle
formatMap := formatEntries{
{r: [2]byte{0x00, 0x7f}, s: scalar.Uint{Sym: "positive_fixint"}, d: func(d *decode.D) {
d.SeekRel(-8)
d.FieldU8("value")
}},
{r: [2]byte{0x80, 0x8f}, s: scalar.Uint{Sym: "fixmap"}, d: mapFn(-4, 4)},
{r: [2]byte{0x90, 0x9f}, s: scalar.Uint{Sym: "fixarray"}, d: arrayFn(-4, 4)},
{r: [2]byte{0xa0, 0xbf}, s: scalar.Uint{Sym: "fixstr"}, d: func(d *decode.D) {
d.SeekRel(-5)
length := d.FieldU5("length")
d.FieldUTF8("value", int(length))
}},
{r: [2]byte{0xc0, 0xc0}, s: scalar.Uint{Sym: "nil"}, d: func(d *decode.D) {
d.FieldValueAny("value", nil)
}},
{r: [2]byte{0xc1, 0xc1}, s: scalar.Uint{Sym: "never_used"}, d: func(d *decode.D) {
d.Fatalf("0xc1 never used")
}},
{r: [2]byte{0xc2, 0xc2}, s: scalar.Uint{Sym: "false"}, d: func(d *decode.D) {
d.FieldValueBool("value", false)
}},
{r: [2]byte{0xc3, 0xc3}, s: scalar.Uint{Sym: "true"}, d: func(d *decode.D) {
d.FieldValueBool("value", true)
}},
{r: [2]byte{0xc4, 0xc4}, s: scalar.Uint{Sym: "bin8"}, d: func(d *decode.D) { d.FieldRawLen("value", int64(d.FieldU8("length"))*8) }},
{r: [2]byte{0xc5, 0xc5}, s: scalar.Uint{Sym: "bin16"}, d: func(d *decode.D) { d.FieldRawLen("value", int64(d.FieldU16("length"))*8) }},
{r: [2]byte{0xc6, 0xc6}, s: scalar.Uint{Sym: "bin32"}, d: func(d *decode.D) { d.FieldRawLen("value", int64(d.FieldU32("length"))*8) }},
{r: [2]byte{0xc7, 0xc7}, s: scalar.Uint{Sym: "ext8"}, d: extFn(8)},
{r: [2]byte{0xc8, 0xc8}, s: scalar.Uint{Sym: "ext16"}, d: extFn(16)},
{r: [2]byte{0xc9, 0xc9}, s: scalar.Uint{Sym: "ext32"}, d: extFn(32)},
{r: [2]byte{0xca, 0xca}, s: scalar.Uint{Sym: "float32"}, d: func(d *decode.D) { d.FieldF32("value") }},
{r: [2]byte{0xcb, 0xcb}, s: scalar.Uint{Sym: "float64"}, d: func(d *decode.D) { d.FieldF64("value") }},
{r: [2]byte{0xcc, 0xcc}, s: scalar.Uint{Sym: "uint8"}, d: func(d *decode.D) { d.FieldU8("value") }},
{r: [2]byte{0xcd, 0xcd}, s: scalar.Uint{Sym: "uint16"}, d: func(d *decode.D) { d.FieldU16("value") }},
{r: [2]byte{0xce, 0xce}, s: scalar.Uint{Sym: "uint32"}, d: func(d *decode.D) { d.FieldU32("value") }},
{r: [2]byte{0xcf, 0xcf}, s: scalar.Uint{Sym: "uint64"}, d: func(d *decode.D) { d.FieldU64("value") }},
{r: [2]byte{0xd0, 0xd0}, s: scalar.Uint{Sym: "int8"}, d: func(d *decode.D) { d.FieldS8("value") }},
{r: [2]byte{0xd1, 0xd1}, s: scalar.Uint{Sym: "int16"}, d: func(d *decode.D) { d.FieldS16("value") }},
{r: [2]byte{0xd2, 0xd2}, s: scalar.Uint{Sym: "int32"}, d: func(d *decode.D) { d.FieldS32("value") }},
{r: [2]byte{0xd3, 0xd3}, s: scalar.Uint{Sym: "int64"}, d: func(d *decode.D) { d.FieldS64("value") }},
{r: [2]byte{0xd4, 0xd4}, s: scalar.Uint{Sym: "fixext1"}, d: func(d *decode.D) { d.FieldS8("fixtype"); d.FieldRawLen("value", 1*8) }},
{r: [2]byte{0xd5, 0xd5}, s: scalar.Uint{Sym: "fixext2"}, d: func(d *decode.D) { d.FieldS8("fixtype"); d.FieldRawLen("value", 2*8) }},
{r: [2]byte{0xd6, 0xd6}, s: scalar.Uint{Sym: "fixext4"}, d: func(d *decode.D) { d.FieldS8("fixtype"); d.FieldRawLen("value", 4*8) }},
{r: [2]byte{0xd7, 0xd7}, s: scalar.Uint{Sym: "fixext8"}, d: func(d *decode.D) { d.FieldS8("fixtype"); d.FieldRawLen("value", 8*8) }},
{r: [2]byte{0xd8, 0xd8}, s: scalar.Uint{Sym: "fixext16"}, d: func(d *decode.D) { d.FieldS8("fixtype"); d.FieldRawLen("value", 16*8) }},
{r: [2]byte{0xd9, 0xd9}, s: scalar.Uint{Sym: "str8"}, d: func(d *decode.D) { d.FieldUTF8("value", int(d.FieldU8("length"))) }},
{r: [2]byte{0xda, 0xda}, s: scalar.Uint{Sym: "str16"}, d: func(d *decode.D) { d.FieldUTF8("value", int(d.FieldU16("length"))) }},
{r: [2]byte{0xdb, 0xdb}, s: scalar.Uint{Sym: "str32"}, d: func(d *decode.D) { d.FieldUTF8("value", int(d.FieldU32("length"))) }},
{r: [2]byte{0xdc, 0xdc}, s: scalar.Uint{Sym: "array16"}, d: arrayFn(0, 16)},
{r: [2]byte{0xdd, 0xdd}, s: scalar.Uint{Sym: "array32"}, d: arrayFn(0, 32)},
{r: [2]byte{0xde, 0xde}, s: scalar.Uint{Sym: "map16"}, d: mapFn(0, 16)},
{r: [2]byte{0xdf, 0xdf}, s: scalar.Uint{Sym: "map32"}, d: mapFn(0, 32)},
{r: [2]byte{0xe0, 0xff}, s: scalar.Uint{Sym: "negative_fixint"}, d: func(d *decode.D) {
d.SeekRel(-8)
d.FieldS8("value")
}},
}
typ := d.FieldU8("type", formatMap, scalar.UintHex)
if fe, ok := formatMap.lookup(byte(typ)); ok {
fe.d(d)
} else {
panic("unreachable")
}
}
func decodeMsgPack(d *decode.D) any {
decodeMsgPackValue(d)
return nil
}