forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
decoder_bytes.go
249 lines (227 loc) · 6.37 KB
/
decoder_bytes.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package pb
import (
"bytes"
"errors"
"math"
"strings"
"unicode/utf8"
"github.com/tinylib/msgp/msgp"
)
// repairUTF8 ensures all characters in s are UTF-8 by replacing non-UTF-8 characters
// with the replacement char �
func repairUTF8(s string) string {
in := strings.NewReader(s)
var out bytes.Buffer
out.Grow(len(s))
for {
r, _, err := in.ReadRune()
if err != nil {
// note: by contract, if `in` contains non-valid utf-8, no error is returned. Rather the utf-8 replacement
// character is returned. Therefore, the only error should usually be io.EOF indicating end of string.
// If any other error is returned by chance, we quit as well, outputting whatever part of the string we
// had already constructed.
return out.String()
}
out.WriteRune(r)
}
}
// parseStringBytes reads the next type in the msgpack payload and
// converts the BinType or the StrType in a valid string.
func parseStringBytes(bts []byte) (string, []byte, error) {
if msgp.IsNil(bts) {
bts, err := msgp.ReadNilBytes(bts)
return "", bts, err
}
// read the generic representation type without decoding
t := msgp.NextType(bts)
var (
err error
i []byte
)
switch t {
case msgp.BinType:
i, bts, err = msgp.ReadBytesZC(bts)
case msgp.StrType:
i, bts, err = msgp.ReadStringZC(bts)
default:
return "", bts, msgp.TypeError{Encoded: t, Method: msgp.StrType}
}
if err != nil {
return "", bts, err
}
if utf8.Valid(i) {
return string(i), bts, nil
}
return repairUTF8(msgp.UnsafeString(i)), bts, nil
}
// parseFloat64Bytes parses a float64 even if the sent value is an int64 or an uint64;
// this is required because the encoding library could remove bytes from the encoded
// payload to reduce the size, if they're not needed.
func parseFloat64Bytes(bts []byte) (float64, []byte, error) {
if msgp.IsNil(bts) {
bts, err := msgp.ReadNilBytes(bts)
return 0, bts, err
}
// read the generic representation type without decoding
t := msgp.NextType(bts)
var err error
switch t {
case msgp.IntType:
var i int64
i, bts, err = msgp.ReadInt64Bytes(bts)
if err != nil {
return 0, bts, err
}
return float64(i), bts, nil
case msgp.UintType:
var i uint64
i, bts, err = msgp.ReadUint64Bytes(bts)
if err != nil {
return 0, bts, err
}
return float64(i), bts, nil
case msgp.Float64Type:
var f float64
f, bts, err = msgp.ReadFloat64Bytes(bts)
if err != nil {
return 0, bts, err
}
return f, bts, nil
default:
return 0, bts, msgp.TypeError{Encoded: t, Method: msgp.Float64Type}
}
}
// cast to int64 values that are int64 but that are sent in uint64
// over the wire. Set to 0 if they overflow the MaxInt64 size. This
// cast should be used ONLY while decoding int64 values that are
// sent as uint64 to reduce the payload size, otherwise the approach
// is not correct in the general sense.
func castInt64(v uint64) (int64, bool) {
if v > math.MaxInt64 {
return 0, false
}
return int64(v), true
}
// parseInt64Bytes parses an int64 even if the sent value is an uint64;
// this is required because the encoding library could remove bytes from the encoded
// payload to reduce the size, if they're not needed.
func parseInt64Bytes(bts []byte) (int64, []byte, error) {
if msgp.IsNil(bts) {
bts, err := msgp.ReadNilBytes(bts)
return 0, bts, err
}
// read the generic representation type without decoding
t := msgp.NextType(bts)
var (
i int64
u uint64
err error
)
switch t {
case msgp.IntType:
i, bts, err = msgp.ReadInt64Bytes(bts)
if err != nil {
return 0, bts, err
}
return i, bts, nil
case msgp.UintType:
u, bts, err = msgp.ReadUint64Bytes(bts)
if err != nil {
return 0, bts, err
}
// force-cast
i, ok := castInt64(u)
if !ok {
return 0, bts, errors.New("found uint64, overflows int64")
}
return i, bts, nil
default:
return 0, bts, msgp.TypeError{Encoded: t, Method: msgp.IntType}
}
}
// parseUint64Bytes parses an uint64 even if the sent value is an int64;
// this is required because the language used for the encoding library
// may not have unsigned types. An example is early version of Java
// (and so JRuby interpreter) that encodes uint64 as int64:
// http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
func parseUint64Bytes(bts []byte) (uint64, []byte, error) {
if msgp.IsNil(bts) {
bts, err := msgp.ReadNilBytes(bts)
return 0, bts, err
}
// read the generic representation type without decoding
t := msgp.NextType(bts)
var (
i int64
u uint64
err error
)
switch t {
case msgp.UintType:
u, bts, err = msgp.ReadUint64Bytes(bts)
if err != nil {
return 0, bts, err
}
return u, bts, err
case msgp.IntType:
i, bts, err = msgp.ReadInt64Bytes(bts)
if err != nil {
return 0, bts, err
}
return uint64(i), bts, nil
default:
return 0, bts, msgp.TypeError{Encoded: t, Method: msgp.IntType}
}
}
// cast to int32 values that are int32 but that are sent in uint32
// over the wire. Set to 0 if they overflow the MaxInt32 size. This
// cast should be used ONLY while decoding int32 values that are
// sent as uint32 to reduce the payload size, otherwise the approach
// is not correct in the general sense.
func castInt32(v uint32) (int32, bool) {
if v > math.MaxInt32 {
return 0, false
}
return int32(v), true
}
// parseInt32Bytes parses an int32 even if the sent value is an uint32;
// this is required because the encoding library could remove bytes from the encoded
// payload to reduce the size, if they're not needed.
func parseInt32Bytes(bts []byte) (int32, []byte, error) {
if msgp.IsNil(bts) {
bts, err := msgp.ReadNilBytes(bts)
return 0, bts, err
}
// read the generic representation type without decoding
t := msgp.NextType(bts)
var (
i int32
u uint32
err error
)
switch t {
case msgp.IntType:
i, bts, err = msgp.ReadInt32Bytes(bts)
if err != nil {
return 0, bts, err
}
return i, bts, nil
case msgp.UintType:
u, bts, err = msgp.ReadUint32Bytes(bts)
if err != nil {
return 0, bts, err
}
// force-cast
i, ok := castInt32(u)
if !ok {
return 0, bts, errors.New("found uint32, overflows int32")
}
return i, bts, nil
default:
return 0, bts, msgp.TypeError{Encoded: t, Method: msgp.IntType}
}
}