File: encode.go

package info (click to toggle)
golang-github-aws-smithy-go 1.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,520 kB
  • sloc: java: 21,276; sh: 131; makefile: 90; xml: 9
file content (218 lines) | stat: -rw-r--r-- 4,520 bytes parent folder | download | duplicates (2)
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
package cbor

import (
	"encoding/binary"
	"math"
)

func (i Uint) len() int {
	return itoarglen(uint64(i))
}

func (i Uint) encode(p []byte) int {
	return encodeArg(majorTypeUint, uint64(i), p)
}

func (i NegInt) len() int {
	return itoarglen(uint64(i) - 1)
}

func (i NegInt) encode(p []byte) int {
	return encodeArg(majorTypeNegInt, uint64(i-1), p)
}

func (s Slice) len() int {
	return itoarglen(len(s)) + len(s)
}

func (s Slice) encode(p []byte) int {
	off := encodeArg(majorTypeSlice, len(s), p)
	copy(p[off:], []byte(s))
	return off + len(s)
}

func (s String) len() int {
	return itoarglen(len(s)) + len(s)
}

func (s String) encode(p []byte) int {
	off := encodeArg(majorTypeString, len(s), p)
	copy(p[off:], []byte(s))
	return off + len(s)
}

func (l List) len() int {
	total := itoarglen(len(l))
	for _, v := range l {
		total += v.len()
	}
	return total
}

func (l List) encode(p []byte) int {
	off := encodeArg(majorTypeList, len(l), p)
	for _, v := range l {
		off += v.encode(p[off:])
	}
	return off
}

func (m Map) len() int {
	total := itoarglen(len(m))
	for k, v := range m {
		total += String(k).len() + v.len()
	}
	return total
}

func (m Map) encode(p []byte) int {
	off := encodeArg(majorTypeMap, len(m), p)
	for k, v := range m {
		off += String(k).encode(p[off:])
		off += v.encode(p[off:])
	}
	return off
}

func (t Tag) len() int {
	return itoarglen(t.ID) + t.Value.len()
}

func (t Tag) encode(p []byte) int {
	off := encodeArg(majorTypeTag, t.ID, p)
	return off + t.Value.encode(p[off:])
}

func (b Bool) len() int {
	return 1
}

func (b Bool) encode(p []byte) int {
	if b {
		p[0] = compose(majorType7, major7True)
	} else {
		p[0] = compose(majorType7, major7False)
	}
	return 1
}

func (*Nil) len() int {
	return 1
}

func (*Nil) encode(p []byte) int {
	p[0] = compose(majorType7, major7Nil)
	return 1
}

func (*Undefined) len() int {
	return 1
}

func (*Undefined) encode(p []byte) int {
	p[0] = compose(majorType7, major7Undefined)
	return 1
}

func (f Float32) len() int {
	return 5
}

func (f Float32) encode(p []byte) int {
	p[0] = compose(majorType7, major7Float32)
	binary.BigEndian.PutUint32(p[1:], math.Float32bits(float32(f)))
	return 5
}

func (f Float64) len() int {
	return 9
}

func (f Float64) encode(p []byte) int {
	p[0] = compose(majorType7, major7Float64)
	binary.BigEndian.PutUint64(p[1:], math.Float64bits(float64(f)))
	return 9
}

func compose(major majorType, minor byte) byte {
	return byte(major)<<5 | minor
}

func itoarglen[I int | uint64](v I) int {
	vv := uint64(v)
	if vv < 24 {
		return 1 // type and len in single byte
	} else if vv < 0x100 {
		return 2 // type + 1-byte len
	} else if vv < 0x10000 {
		return 3 // type + 2-byte len
	} else if vv < 0x100000000 {
		return 5 // type + 4-byte len
	}
	return 9 // type + 8-byte len
}

func encodeArg[I int | uint64](t majorType, arg I, p []byte) int {
	aarg := uint64(arg)
	if aarg < 24 {
		p[0] = byte(t)<<5 | byte(aarg)
		return 1
	} else if aarg < 0x100 {
		p[0] = compose(t, minorArg1)
		p[1] = byte(aarg)
		return 2
	} else if aarg < 0x10000 {
		p[0] = compose(t, minorArg2)
		binary.BigEndian.PutUint16(p[1:], uint16(aarg))
		return 3
	} else if aarg < 0x100000000 {
		p[0] = compose(t, minorArg4)
		binary.BigEndian.PutUint32(p[1:], uint32(aarg))
		return 5
	}

	p[0] = compose(t, minorArg8)
	binary.BigEndian.PutUint64(p[1:], uint64(aarg))
	return 9
}

// EncodeRaw encodes opaque CBOR data.
//
// This is used by the encoder for the purpose of embedding document shapes.
// Decode will never return values of this type.
type EncodeRaw []byte

func (v EncodeRaw) len() int { return len(v) }

func (v EncodeRaw) encode(p []byte) int {
	copy(p, v)
	return len(v)
}

// FixedUint encodes fixed-width Uint values.
//
// This is used by the encoder for the purpose of embedding integrals in
// document shapes. Decode will never return values of this type.
type EncodeFixedUint uint64

func (EncodeFixedUint) len() int { return 9 }

func (v EncodeFixedUint) encode(p []byte) int {
	p[0] = compose(majorTypeUint, minorArg8)
	binary.BigEndian.PutUint64(p[1:], uint64(v))
	return 9
}

// FixedUint encodes fixed-width NegInt values.
//
// This is used by the encoder for the purpose of embedding integrals in
// document shapes. Decode will never return values of this type.
type EncodeFixedNegInt uint64

func (EncodeFixedNegInt) len() int { return 9 }

func (v EncodeFixedNegInt) encode(p []byte) int {
	p[0] = compose(majorTypeNegInt, minorArg8)
	binary.BigEndian.PutUint64(p[1:], uint64(v-1))
	return 9
}