| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package binary implements simple translation between numbers and byte |
| 6 |
// sequences and encoding and decoding of varints. |
| 7 |
// |
| 8 |
// Numbers are translated by reading and writing fixed-size values. |
| 9 |
// A fixed-size value is either a fixed-size arithmetic |
| 10 |
// type (bool, int8, uint8, int16, float32, complex64, ...) |
| 11 |
// or an array or struct containing only fixed-size values. |
| 12 |
// |
| 13 |
// The varint functions encode and decode single integer values using |
| 14 |
// a variable-length encoding; smaller values require fewer bytes. |
| 15 |
// For a specification, see |
| 16 |
// https://developers.google.com/protocol-buffers/docs/encoding. |
| 17 |
// |
| 18 |
// This package favors simplicity over efficiency. Clients that require |
| 19 |
// high-performance serialization, especially for large data structures, |
| 20 |
// should look at more advanced solutions such as the [encoding/gob] |
| 21 |
// package or [google.golang.org/protobuf] for protocol buffers. |
| 22 |
package binary |
| 23 |
|
| 24 |
import ( |
| 25 |
"errors" |
| 26 |
"io" |
| 27 |
"math" |
| 28 |
"reflect" |
| 29 |
"slices" |
| 30 |
"sync" |
| 31 |
) |
| 32 |
|
| 33 |
var errBufferTooSmall = errors.New("buffer too small") |
| 34 |
|
| 35 |
// A ByteOrder specifies how to convert byte slices into |
| 36 |
// 16-, 32-, or 64-bit unsigned integers. |
| 37 |
// |
| 38 |
// It is implemented by [LittleEndian], [BigEndian], and [NativeEndian]. |
| 39 |
type ByteOrder interface { |
| 40 |
Uint16([]byte) uint16 |
| 41 |
Uint32([]byte) uint32 |
| 42 |
Uint64([]byte) uint64 |
| 43 |
PutUint16([]byte, uint16) |
| 44 |
PutUint32([]byte, uint32) |
| 45 |
PutUint64([]byte, uint64) |
| 46 |
String() string |
| 47 |
} |
| 48 |
|
| 49 |
// AppendByteOrder specifies how to append 16-, 32-, or 64-bit unsigned integers |
| 50 |
// into a byte slice. |
| 51 |
// |
| 52 |
// It is implemented by [LittleEndian], [BigEndian], and [NativeEndian]. |
| 53 |
type AppendByteOrder interface { |
| 54 |
AppendUint16([]byte, uint16) []byte |
| 55 |
AppendUint32([]byte, uint32) []byte |
| 56 |
AppendUint64([]byte, uint64) []byte |
| 57 |
String() string |
| 58 |
} |
| 59 |
|
| 60 |
// LittleEndian is the little-endian implementation of [ByteOrder] and [AppendByteOrder]. |
| 61 |
var LittleEndian littleEndian |
| 62 |
|
| 63 |
// BigEndian is the big-endian implementation of [ByteOrder] and [AppendByteOrder]. |
| 64 |
var BigEndian bigEndian |
| 65 |
|
| 66 |
type littleEndian struct{} |
| 67 |
|
| 68 |
// Uint16 returns the uint16 representation of b[0:2]. |
| 69 |
func (littleEndian) Uint16(b []byte) uint16 { |
| 70 |
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 |
| 71 |
return uint16(b[0]) | uint16(b[1])<<8 |
| 72 |
} |
| 73 |
|
| 74 |
// PutUint16 stores v into b[0:2]. |
| 75 |
func (littleEndian) PutUint16(b []byte, v uint16) { |
| 76 |
_ = b[1] // early bounds check to guarantee safety of writes below |
| 77 |
b[0] = byte(v) |
| 78 |
b[1] = byte(v >> 8) |
| 79 |
} |
| 80 |
|
| 81 |
// AppendUint16 appends the bytes of v to b and returns the appended slice. |
| 82 |
func (littleEndian) AppendUint16(b []byte, v uint16) []byte { |
| 83 |
return append(b, |
| 84 |
byte(v), |
| 85 |
byte(v>>8), |
| 86 |
) |
| 87 |
} |
| 88 |
|
| 89 |
// Uint32 returns the uint32 representation of b[0:4]. |
| 90 |
func (littleEndian) Uint32(b []byte) uint32 { |
| 91 |
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 |
| 92 |
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 |
| 93 |
} |
| 94 |
|
| 95 |
// PutUint32 stores v into b[0:4]. |
| 96 |
func (littleEndian) PutUint32(b []byte, v uint32) { |
| 97 |
_ = b[3] // early bounds check to guarantee safety of writes below |
| 98 |
b[0] = byte(v) |
| 99 |
b[1] = byte(v >> 8) |
| 100 |
b[2] = byte(v >> 16) |
| 101 |
b[3] = byte(v >> 24) |
| 102 |
} |
| 103 |
|
| 104 |
// AppendUint32 appends the bytes of v to b and returns the appended slice. |
| 105 |
func (littleEndian) AppendUint32(b []byte, v uint32) []byte { |
| 106 |
return append(b, |
| 107 |
byte(v), |
| 108 |
byte(v>>8), |
| 109 |
byte(v>>16), |
| 110 |
byte(v>>24), |
| 111 |
) |
| 112 |
} |
| 113 |
|
| 114 |
// Uint64 returns the uint64 representation of b[0:8]. |
| 115 |
func (littleEndian) Uint64(b []byte) uint64 { |
| 116 |
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 |
| 117 |
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | |
| 118 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 |
| 119 |
} |
| 120 |
|
| 121 |
// PutUint64 stores v into b[0:8]. |
| 122 |
func (littleEndian) PutUint64(b []byte, v uint64) { |
| 123 |
_ = b[7] // early bounds check to guarantee safety of writes below |
| 124 |
b[0] = byte(v) |
| 125 |
b[1] = byte(v >> 8) |
| 126 |
b[2] = byte(v >> 16) |
| 127 |
b[3] = byte(v >> 24) |
| 128 |
b[4] = byte(v >> 32) |
| 129 |
b[5] = byte(v >> 40) |
| 130 |
b[6] = byte(v >> 48) |
| 131 |
b[7] = byte(v >> 56) |
| 132 |
} |
| 133 |
|
| 134 |
// AppendUint64 appends the bytes of v to b and returns the appended slice. |
| 135 |
func (littleEndian) AppendUint64(b []byte, v uint64) []byte { |
| 136 |
return append(b, |
| 137 |
byte(v), |
| 138 |
byte(v>>8), |
| 139 |
byte(v>>16), |
| 140 |
byte(v>>24), |
| 141 |
byte(v>>32), |
| 142 |
byte(v>>40), |
| 143 |
byte(v>>48), |
| 144 |
byte(v>>56), |
| 145 |
) |
| 146 |
} |
| 147 |
|
| 148 |
func (littleEndian) String() string { return "LittleEndian" } |
| 149 |
|
| 150 |
func (littleEndian) GoString() string { return "binary.LittleEndian" } |
| 151 |
|
| 152 |
type bigEndian struct{} |
| 153 |
|
| 154 |
// Uint16 returns the uint16 representation of b[0:2]. |
| 155 |
func (bigEndian) Uint16(b []byte) uint16 { |
| 156 |
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 |
| 157 |
return uint16(b[1]) | uint16(b[0])<<8 |
| 158 |
} |
| 159 |
|
| 160 |
// PutUint16 stores v into b[0:2]. |
| 161 |
func (bigEndian) PutUint16(b []byte, v uint16) { |
| 162 |
_ = b[1] // early bounds check to guarantee safety of writes below |
| 163 |
b[0] = byte(v >> 8) |
| 164 |
b[1] = byte(v) |
| 165 |
} |
| 166 |
|
| 167 |
// AppendUint16 appends the bytes of v to b and returns the appended slice. |
| 168 |
func (bigEndian) AppendUint16(b []byte, v uint16) []byte { |
| 169 |
return append(b, |
| 170 |
byte(v>>8), |
| 171 |
byte(v), |
| 172 |
) |
| 173 |
} |
| 174 |
|
| 175 |
// Uint32 returns the uint32 representation of b[0:4]. |
| 176 |
func (bigEndian) Uint32(b []byte) uint32 { |
| 177 |
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 |
| 178 |
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 |
| 179 |
} |
| 180 |
|
| 181 |
// PutUint32 stores v into b[0:4]. |
| 182 |
func (bigEndian) PutUint32(b []byte, v uint32) { |
| 183 |
_ = b[3] // early bounds check to guarantee safety of writes below |
| 184 |
b[0] = byte(v >> 24) |
| 185 |
b[1] = byte(v >> 16) |
| 186 |
b[2] = byte(v >> 8) |
| 187 |
b[3] = byte(v) |
| 188 |
} |
| 189 |
|
| 190 |
// AppendUint32 appends the bytes of v to b and returns the appended slice. |
| 191 |
func (bigEndian) AppendUint32(b []byte, v uint32) []byte { |
| 192 |
return append(b, |
| 193 |
byte(v>>24), |
| 194 |
byte(v>>16), |
| 195 |
byte(v>>8), |
| 196 |
byte(v), |
| 197 |
) |
| 198 |
} |
| 199 |
|
| 200 |
// Uint64 returns the uint64 representation of b[0:8]. |
| 201 |
func (bigEndian) Uint64(b []byte) uint64 { |
| 202 |
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 |
| 203 |
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | |
| 204 |
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 |
| 205 |
} |
| 206 |
|
| 207 |
// PutUint64 stores v into b[0:8]. |
| 208 |
func (bigEndian) PutUint64(b []byte, v uint64) { |
| 209 |
_ = b[7] // early bounds check to guarantee safety of writes below |
| 210 |
b[0] = byte(v >> 56) |
| 211 |
b[1] = byte(v >> 48) |
| 212 |
b[2] = byte(v >> 40) |
| 213 |
b[3] = byte(v >> 32) |
| 214 |
b[4] = byte(v >> 24) |
| 215 |
b[5] = byte(v >> 16) |
| 216 |
b[6] = byte(v >> 8) |
| 217 |
b[7] = byte(v) |
| 218 |
} |
| 219 |
|
| 220 |
// AppendUint64 appends the bytes of v to b and returns the appended slice. |
| 221 |
func (bigEndian) AppendUint64(b []byte, v uint64) []byte { |
| 222 |
return append(b, |
| 223 |
byte(v>>56), |
| 224 |
byte(v>>48), |
| 225 |
byte(v>>40), |
| 226 |
byte(v>>32), |
| 227 |
byte(v>>24), |
| 228 |
byte(v>>16), |
| 229 |
byte(v>>8), |
| 230 |
byte(v), |
| 231 |
) |
| 232 |
} |
| 233 |
|
| 234 |
func (bigEndian) String() string { return "BigEndian" } |
| 235 |
|
| 236 |
func (bigEndian) GoString() string { return "binary.BigEndian" } |
| 237 |
|
| 238 |
func (nativeEndian) String() string { return "NativeEndian" } |
| 239 |
|
| 240 |
func (nativeEndian) GoString() string { return "binary.NativeEndian" } |
| 241 |
|
| 242 |
// Read reads structured binary data from r into data. |
| 243 |
// Data must be a pointer to a fixed-size value or a slice |
| 244 |
// of fixed-size values. |
| 245 |
// Bytes read from r are decoded using the specified byte order |
| 246 |
// and written to successive fields of the data. |
| 247 |
// When decoding boolean values, a zero byte is decoded as false, and |
| 248 |
// any other non-zero byte is decoded as true. |
| 249 |
// When reading into structs, the field data for fields with |
| 250 |
// blank (_) field names is skipped; i.e., blank field names |
| 251 |
// may be used for padding. |
| 252 |
// When reading into a struct, all non-blank fields must be exported |
| 253 |
// or Read may panic. |
| 254 |
// |
| 255 |
// The error is [io.EOF] only if no bytes were read. |
| 256 |
// If an [io.EOF] happens after reading some but not all the bytes, |
| 257 |
// Read returns [io.ErrUnexpectedEOF]. |
| 258 |
func Read(r io.Reader, order ByteOrder, data any) error { |
| 259 |
// Fast path for basic types and slices. |
| 260 |
if n, _ := intDataSize(data); n != 0 { |
| 261 |
bs := make([]byte, n) |
| 262 |
if _, err := io.ReadFull(r, bs); err != nil { |
| 263 |
return err |
| 264 |
} |
| 265 |
|
| 266 |
if decodeFast(bs, order, data) { |
| 267 |
return nil |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
// Fallback to reflect-based decoding. |
| 272 |
v := reflect.ValueOf(data) |
| 273 |
size := -1 |
| 274 |
switch v.Kind() { |
| 275 |
case reflect.Pointer: |
| 276 |
v = v.Elem() |
| 277 |
size = dataSize(v) |
| 278 |
case reflect.Slice: |
| 279 |
size = dataSize(v) |
| 280 |
} |
| 281 |
if size < 0 { |
| 282 |
return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String()) |
| 283 |
} |
| 284 |
|
| 285 |
d := &decoder{order: order, buf: make([]byte, size)} |
| 286 |
if _, err := io.ReadFull(r, d.buf); err != nil { |
| 287 |
return err |
| 288 |
} |
| 289 |
d.value(v) |
| 290 |
return nil |
| 291 |
} |
| 292 |
|
| 293 |
// Decode decodes binary data from buf into data according to |
| 294 |
// the given byte order. |
| 295 |
// It returns an error if buf is too small, otherwise the number of |
| 296 |
// bytes consumed from buf. |
| 297 |
func Decode(buf []byte, order ByteOrder, data any) (int, error) { |
| 298 |
if n, _ := intDataSize(data); n != 0 { |
| 299 |
if len(buf) < n { |
| 300 |
return 0, errBufferTooSmall |
| 301 |
} |
| 302 |
|
| 303 |
if decodeFast(buf, order, data) { |
| 304 |
return n, nil |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// Fallback to reflect-based decoding. |
| 309 |
v := reflect.ValueOf(data) |
| 310 |
size := -1 |
| 311 |
switch v.Kind() { |
| 312 |
case reflect.Pointer: |
| 313 |
v = v.Elem() |
| 314 |
size = dataSize(v) |
| 315 |
case reflect.Slice: |
| 316 |
size = dataSize(v) |
| 317 |
} |
| 318 |
if size < 0 { |
| 319 |
return 0, errors.New("binary.Decode: invalid type " + reflect.TypeOf(data).String()) |
| 320 |
} |
| 321 |
|
| 322 |
if len(buf) < size { |
| 323 |
return 0, errBufferTooSmall |
| 324 |
} |
| 325 |
d := &decoder{order: order, buf: buf[:size]} |
| 326 |
d.value(v) |
| 327 |
return size, nil |
| 328 |
} |
| 329 |
|
| 330 |
func decodeFast(bs []byte, order ByteOrder, data any) bool { |
| 331 |
switch data := data.(type) { |
| 332 |
case *bool: |
| 333 |
*data = bs[0] != 0 |
| 334 |
case *int8: |
| 335 |
*data = int8(bs[0]) |
| 336 |
case *uint8: |
| 337 |
*data = bs[0] |
| 338 |
case *int16: |
| 339 |
*data = int16(order.Uint16(bs)) |
| 340 |
case *uint16: |
| 341 |
*data = order.Uint16(bs) |
| 342 |
case *int32: |
| 343 |
*data = int32(order.Uint32(bs)) |
| 344 |
case *uint32: |
| 345 |
*data = order.Uint32(bs) |
| 346 |
case *int64: |
| 347 |
*data = int64(order.Uint64(bs)) |
| 348 |
case *uint64: |
| 349 |
*data = order.Uint64(bs) |
| 350 |
case *float32: |
| 351 |
*data = math.Float32frombits(order.Uint32(bs)) |
| 352 |
case *float64: |
| 353 |
*data = math.Float64frombits(order.Uint64(bs)) |
| 354 |
case []bool: |
| 355 |
for i, x := range bs { // Easier to loop over the input for 8-bit values. |
| 356 |
data[i] = x != 0 |
| 357 |
} |
| 358 |
case []int8: |
| 359 |
for i, x := range bs { |
| 360 |
data[i] = int8(x) |
| 361 |
} |
| 362 |
case []uint8: |
| 363 |
copy(data, bs) |
| 364 |
case []int16: |
| 365 |
for i := range data { |
| 366 |
data[i] = int16(order.Uint16(bs[2*i:])) |
| 367 |
} |
| 368 |
case []uint16: |
| 369 |
for i := range data { |
| 370 |
data[i] = order.Uint16(bs[2*i:]) |
| 371 |
} |
| 372 |
case []int32: |
| 373 |
for i := range data { |
| 374 |
data[i] = int32(order.Uint32(bs[4*i:])) |
| 375 |
} |
| 376 |
case []uint32: |
| 377 |
for i := range data { |
| 378 |
data[i] = order.Uint32(bs[4*i:]) |
| 379 |
} |
| 380 |
case []int64: |
| 381 |
for i := range data { |
| 382 |
data[i] = int64(order.Uint64(bs[8*i:])) |
| 383 |
} |
| 384 |
case []uint64: |
| 385 |
for i := range data { |
| 386 |
data[i] = order.Uint64(bs[8*i:]) |
| 387 |
} |
| 388 |
case []float32: |
| 389 |
for i := range data { |
| 390 |
data[i] = math.Float32frombits(order.Uint32(bs[4*i:])) |
| 391 |
} |
| 392 |
case []float64: |
| 393 |
for i := range data { |
| 394 |
data[i] = math.Float64frombits(order.Uint64(bs[8*i:])) |
| 395 |
} |
| 396 |
default: |
| 397 |
return false |
| 398 |
} |
| 399 |
return true |
| 400 |
} |
| 401 |
|
| 402 |
// Write writes the binary representation of data into w. |
| 403 |
// Data must be a fixed-size value or a slice of fixed-size |
| 404 |
// values, or a pointer to such data. |
| 405 |
// Boolean values encode as one byte: 1 for true, and 0 for false. |
| 406 |
// Bytes written to w are encoded using the specified byte order |
| 407 |
// and read from successive fields of the data. |
| 408 |
// When writing structs, zero values are written for fields |
| 409 |
// with blank (_) field names. |
| 410 |
func Write(w io.Writer, order ByteOrder, data any) error { |
| 411 |
// Fast path for basic types and slices. |
| 412 |
if n, bs := intDataSize(data); n != 0 { |
| 413 |
if bs == nil { |
| 414 |
bs = make([]byte, n) |
| 415 |
encodeFast(bs, order, data) |
| 416 |
} |
| 417 |
|
| 418 |
_, err := w.Write(bs) |
| 419 |
return err |
| 420 |
} |
| 421 |
|
| 422 |
// Fallback to reflect-based encoding. |
| 423 |
v := reflect.Indirect(reflect.ValueOf(data)) |
| 424 |
size := dataSize(v) |
| 425 |
if size < 0 { |
| 426 |
return errors.New("binary.Write: some values are not fixed-sized in type " + reflect.TypeOf(data).String()) |
| 427 |
} |
| 428 |
|
| 429 |
buf := make([]byte, size) |
| 430 |
e := &encoder{order: order, buf: buf} |
| 431 |
e.value(v) |
| 432 |
_, err := w.Write(buf) |
| 433 |
return err |
| 434 |
} |
| 435 |
|
| 436 |
// Encode encodes the binary representation of data into buf according to |
| 437 |
// the given byte order. |
| 438 |
// It returns an error if buf is too small, otherwise the number of |
| 439 |
// bytes written into buf. |
| 440 |
func Encode(buf []byte, order ByteOrder, data any) (int, error) { |
| 441 |
// Fast path for basic types and slices. |
| 442 |
if n, _ := intDataSize(data); n != 0 { |
| 443 |
if len(buf) < n { |
| 444 |
return 0, errBufferTooSmall |
| 445 |
} |
| 446 |
|
| 447 |
encodeFast(buf, order, data) |
| 448 |
return n, nil |
| 449 |
} |
| 450 |
|
| 451 |
// Fallback to reflect-based encoding. |
| 452 |
v := reflect.Indirect(reflect.ValueOf(data)) |
| 453 |
size := dataSize(v) |
| 454 |
if size < 0 { |
| 455 |
return 0, errors.New("binary.Encode: some values are not fixed-sized in type " + reflect.TypeOf(data).String()) |
| 456 |
} |
| 457 |
|
| 458 |
if len(buf) < size { |
| 459 |
return 0, errBufferTooSmall |
| 460 |
} |
| 461 |
e := &encoder{order: order, buf: buf} |
| 462 |
e.value(v) |
| 463 |
return size, nil |
| 464 |
} |
| 465 |
|
| 466 |
// Append appends the binary representation of data to buf. |
| 467 |
// buf may be nil, in which case a new buffer will be allocated. |
| 468 |
// See [Write] on which data are acceptable. |
| 469 |
// It returns the (possibly extended) buffer containing data or an error. |
| 470 |
func Append(buf []byte, order ByteOrder, data any) ([]byte, error) { |
| 471 |
// Fast path for basic types and slices. |
| 472 |
if n, _ := intDataSize(data); n != 0 { |
| 473 |
buf, pos := ensure(buf, n) |
| 474 |
encodeFast(pos, order, data) |
| 475 |
return buf, nil |
| 476 |
} |
| 477 |
|
| 478 |
// Fallback to reflect-based encoding. |
| 479 |
v := reflect.Indirect(reflect.ValueOf(data)) |
| 480 |
size := dataSize(v) |
| 481 |
if size < 0 { |
| 482 |
return nil, errors.New("binary.Append: some values are not fixed-sized in type " + reflect.TypeOf(data).String()) |
| 483 |
} |
| 484 |
|
| 485 |
buf, pos := ensure(buf, size) |
| 486 |
e := &encoder{order: order, buf: pos} |
| 487 |
e.value(v) |
| 488 |
return buf, nil |
| 489 |
} |
| 490 |
|
| 491 |
func encodeFast(bs []byte, order ByteOrder, data any) { |
| 492 |
switch v := data.(type) { |
| 493 |
case *bool: |
| 494 |
if *v { |
| 495 |
bs[0] = 1 |
| 496 |
} else { |
| 497 |
bs[0] = 0 |
| 498 |
} |
| 499 |
case bool: |
| 500 |
if v { |
| 501 |
bs[0] = 1 |
| 502 |
} else { |
| 503 |
bs[0] = 0 |
| 504 |
} |
| 505 |
case []bool: |
| 506 |
for i, x := range v { |
| 507 |
if x { |
| 508 |
bs[i] = 1 |
| 509 |
} else { |
| 510 |
bs[i] = 0 |
| 511 |
} |
| 512 |
} |
| 513 |
case *int8: |
| 514 |
bs[0] = byte(*v) |
| 515 |
case int8: |
| 516 |
bs[0] = byte(v) |
| 517 |
case []int8: |
| 518 |
for i, x := range v { |
| 519 |
bs[i] = byte(x) |
| 520 |
} |
| 521 |
case *uint8: |
| 522 |
bs[0] = *v |
| 523 |
case uint8: |
| 524 |
bs[0] = v |
| 525 |
case []uint8: |
| 526 |
copy(bs, v) |
| 527 |
case *int16: |
| 528 |
order.PutUint16(bs, uint16(*v)) |
| 529 |
case int16: |
| 530 |
order.PutUint16(bs, uint16(v)) |
| 531 |
case []int16: |
| 532 |
for i, x := range v { |
| 533 |
order.PutUint16(bs[2*i:], uint16(x)) |
| 534 |
} |
| 535 |
case *uint16: |
| 536 |
order.PutUint16(bs, *v) |
| 537 |
case uint16: |
| 538 |
order.PutUint16(bs, v) |
| 539 |
case []uint16: |
| 540 |
for i, x := range v { |
| 541 |
order.PutUint16(bs[2*i:], x) |
| 542 |
} |
| 543 |
case *int32: |
| 544 |
order.PutUint32(bs, uint32(*v)) |
| 545 |
case int32: |
| 546 |
order.PutUint32(bs, uint32(v)) |
| 547 |
case []int32: |
| 548 |
for i, x := range v { |
| 549 |
order.PutUint32(bs[4*i:], uint32(x)) |
| 550 |
} |
| 551 |
case *uint32: |
| 552 |
order.PutUint32(bs, *v) |
| 553 |
case uint32: |
| 554 |
order.PutUint32(bs, v) |
| 555 |
case []uint32: |
| 556 |
for i, x := range v { |
| 557 |
order.PutUint32(bs[4*i:], x) |
| 558 |
} |
| 559 |
case *int64: |
| 560 |
order.PutUint64(bs, uint64(*v)) |
| 561 |
case int64: |
| 562 |
order.PutUint64(bs, uint64(v)) |
| 563 |
case []int64: |
| 564 |
for i, x := range v { |
| 565 |
order.PutUint64(bs[8*i:], uint64(x)) |
| 566 |
} |
| 567 |
case *uint64: |
| 568 |
order.PutUint64(bs, *v) |
| 569 |
case uint64: |
| 570 |
order.PutUint64(bs, v) |
| 571 |
case []uint64: |
| 572 |
for i, x := range v { |
| 573 |
order.PutUint64(bs[8*i:], x) |
| 574 |
} |
| 575 |
case *float32: |
| 576 |
order.PutUint32(bs, math.Float32bits(*v)) |
| 577 |
case float32: |
| 578 |
order.PutUint32(bs, math.Float32bits(v)) |
| 579 |
case []float32: |
| 580 |
for i, x := range v { |
| 581 |
order.PutUint32(bs[4*i:], math.Float32bits(x)) |
| 582 |
} |
| 583 |
case *float64: |
| 584 |
order.PutUint64(bs, math.Float64bits(*v)) |
| 585 |
case float64: |
| 586 |
order.PutUint64(bs, math.Float64bits(v)) |
| 587 |
case []float64: |
| 588 |
for i, x := range v { |
| 589 |
order.PutUint64(bs[8*i:], math.Float64bits(x)) |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
// Size returns how many bytes [Write] would generate to encode the value v, which |
| 595 |
// must be a fixed-size value or a slice of fixed-size values, or a pointer to such data. |
| 596 |
// If v is neither of these, Size returns -1. |
| 597 |
func Size(v any) int { |
| 598 |
switch data := v.(type) { |
| 599 |
case bool, int8, uint8: |
| 600 |
return 1 |
| 601 |
case *bool: |
| 602 |
if data == nil { |
| 603 |
return -1 |
| 604 |
} |
| 605 |
return 1 |
| 606 |
case *int8: |
| 607 |
if data == nil { |
| 608 |
return -1 |
| 609 |
} |
| 610 |
return 1 |
| 611 |
case *uint8: |
| 612 |
if data == nil { |
| 613 |
return -1 |
| 614 |
} |
| 615 |
return 1 |
| 616 |
case []bool: |
| 617 |
return len(data) |
| 618 |
case []int8: |
| 619 |
return len(data) |
| 620 |
case []uint8: |
| 621 |
return len(data) |
| 622 |
case int16, uint16: |
| 623 |
return 2 |
| 624 |
case *int16: |
| 625 |
if data == nil { |
| 626 |
return -1 |
| 627 |
} |
| 628 |
return 2 |
| 629 |
case *uint16: |
| 630 |
if data == nil { |
| 631 |
return -1 |
| 632 |
} |
| 633 |
return 2 |
| 634 |
case []int16: |
| 635 |
return 2 * len(data) |
| 636 |
case []uint16: |
| 637 |
return 2 * len(data) |
| 638 |
case int32, uint32: |
| 639 |
return 4 |
| 640 |
case *int32: |
| 641 |
if data == nil { |
| 642 |
return -1 |
| 643 |
} |
| 644 |
return 4 |
| 645 |
case *uint32: |
| 646 |
if data == nil { |
| 647 |
return -1 |
| 648 |
} |
| 649 |
return 4 |
| 650 |
case []int32: |
| 651 |
return 4 * len(data) |
| 652 |
case []uint32: |
| 653 |
return 4 * len(data) |
| 654 |
case int64, uint64: |
| 655 |
return 8 |
| 656 |
case *int64: |
| 657 |
if data == nil { |
| 658 |
return -1 |
| 659 |
} |
| 660 |
return 8 |
| 661 |
case *uint64: |
| 662 |
if data == nil { |
| 663 |
return -1 |
| 664 |
} |
| 665 |
return 8 |
| 666 |
case []int64: |
| 667 |
return 8 * len(data) |
| 668 |
case []uint64: |
| 669 |
return 8 * len(data) |
| 670 |
case float32: |
| 671 |
return 4 |
| 672 |
case *float32: |
| 673 |
if data == nil { |
| 674 |
return -1 |
| 675 |
} |
| 676 |
return 4 |
| 677 |
case float64: |
| 678 |
return 8 |
| 679 |
case *float64: |
| 680 |
if data == nil { |
| 681 |
return -1 |
| 682 |
} |
| 683 |
return 8 |
| 684 |
case []float32: |
| 685 |
return 4 * len(data) |
| 686 |
case []float64: |
| 687 |
return 8 * len(data) |
| 688 |
} |
| 689 |
return dataSize(reflect.Indirect(reflect.ValueOf(v))) |
| 690 |
} |
| 691 |
|
| 692 |
var structSize sync.Map // map[reflect.Type]int |
| 693 |
|
| 694 |
// dataSize returns the number of bytes the actual data represented by v occupies in memory. |
| 695 |
// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice |
| 696 |
// it returns the length of the slice times the element size and does not count the memory |
| 697 |
// occupied by the header. If the type of v is not acceptable, dataSize returns -1. |
| 698 |
func dataSize(v reflect.Value) int { |
| 699 |
switch v.Kind() { |
| 700 |
case reflect.Slice, reflect.Array: |
| 701 |
t := v.Type().Elem() |
| 702 |
if size, ok := structSize.Load(t); ok { |
| 703 |
return size.(int) * v.Len() |
| 704 |
} |
| 705 |
|
| 706 |
size := sizeof(t) |
| 707 |
if size >= 0 { |
| 708 |
if t.Kind() == reflect.Struct { |
| 709 |
structSize.Store(t, size) |
| 710 |
} |
| 711 |
return size * v.Len() |
| 712 |
} |
| 713 |
|
| 714 |
case reflect.Struct: |
| 715 |
t := v.Type() |
| 716 |
if size, ok := structSize.Load(t); ok { |
| 717 |
return size.(int) |
| 718 |
} |
| 719 |
size := sizeof(t) |
| 720 |
structSize.Store(t, size) |
| 721 |
return size |
| 722 |
|
| 723 |
default: |
| 724 |
if v.IsValid() { |
| 725 |
return sizeof(v.Type()) |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
return -1 |
| 730 |
} |
| 731 |
|
| 732 |
// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable. |
| 733 |
func sizeof(t reflect.Type) int { |
| 734 |
switch t.Kind() { |
| 735 |
case reflect.Array: |
| 736 |
if s := sizeof(t.Elem()); s >= 0 { |
| 737 |
return s * t.Len() |
| 738 |
} |
| 739 |
|
| 740 |
case reflect.Struct: |
| 741 |
sum := 0 |
| 742 |
for i, n := 0, t.NumField(); i < n; i++ { |
| 743 |
s := sizeof(t.Field(i).Type) |
| 744 |
if s < 0 { |
| 745 |
return -1 |
| 746 |
} |
| 747 |
sum += s |
| 748 |
} |
| 749 |
return sum |
| 750 |
|
| 751 |
case reflect.Bool, |
| 752 |
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, |
| 753 |
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 754 |
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128: |
| 755 |
return int(t.Size()) |
| 756 |
} |
| 757 |
|
| 758 |
return -1 |
| 759 |
} |
| 760 |
|
| 761 |
type coder struct { |
| 762 |
order ByteOrder |
| 763 |
buf []byte |
| 764 |
offset int |
| 765 |
} |
| 766 |
|
| 767 |
type decoder coder |
| 768 |
type encoder coder |
| 769 |
|
| 770 |
func (d *decoder) bool() bool { |
| 771 |
x := d.buf[d.offset] |
| 772 |
d.offset++ |
| 773 |
return x != 0 |
| 774 |
} |
| 775 |
|
| 776 |
func (e *encoder) bool(x bool) { |
| 777 |
if x { |
| 778 |
e.buf[e.offset] = 1 |
| 779 |
} else { |
| 780 |
e.buf[e.offset] = 0 |
| 781 |
} |
| 782 |
e.offset++ |
| 783 |
} |
| 784 |
|
| 785 |
func (d *decoder) uint8() uint8 { |
| 786 |
x := d.buf[d.offset] |
| 787 |
d.offset++ |
| 788 |
return x |
| 789 |
} |
| 790 |
|
| 791 |
func (e *encoder) uint8(x uint8) { |
| 792 |
e.buf[e.offset] = x |
| 793 |
e.offset++ |
| 794 |
} |
| 795 |
|
| 796 |
func (d *decoder) uint16() uint16 { |
| 797 |
x := d.order.Uint16(d.buf[d.offset : d.offset+2]) |
| 798 |
d.offset += 2 |
| 799 |
return x |
| 800 |
} |
| 801 |
|
| 802 |
func (e *encoder) uint16(x uint16) { |
| 803 |
e.order.PutUint16(e.buf[e.offset:e.offset+2], x) |
| 804 |
e.offset += 2 |
| 805 |
} |
| 806 |
|
| 807 |
func (d *decoder) uint32() uint32 { |
| 808 |
x := d.order.Uint32(d.buf[d.offset : d.offset+4]) |
| 809 |
d.offset += 4 |
| 810 |
return x |
| 811 |
} |
| 812 |
|
| 813 |
func (e *encoder) uint32(x uint32) { |
| 814 |
e.order.PutUint32(e.buf[e.offset:e.offset+4], x) |
| 815 |
e.offset += 4 |
| 816 |
} |
| 817 |
|
| 818 |
func (d *decoder) uint64() uint64 { |
| 819 |
x := d.order.Uint64(d.buf[d.offset : d.offset+8]) |
| 820 |
d.offset += 8 |
| 821 |
return x |
| 822 |
} |
| 823 |
|
| 824 |
func (e *encoder) uint64(x uint64) { |
| 825 |
e.order.PutUint64(e.buf[e.offset:e.offset+8], x) |
| 826 |
e.offset += 8 |
| 827 |
} |
| 828 |
|
| 829 |
func (d *decoder) int8() int8 { return int8(d.uint8()) } |
| 830 |
|
| 831 |
func (e *encoder) int8(x int8) { e.uint8(uint8(x)) } |
| 832 |
|
| 833 |
func (d *decoder) int16() int16 { return int16(d.uint16()) } |
| 834 |
|
| 835 |
func (e *encoder) int16(x int16) { e.uint16(uint16(x)) } |
| 836 |
|
| 837 |
func (d *decoder) int32() int32 { return int32(d.uint32()) } |
| 838 |
|
| 839 |
func (e *encoder) int32(x int32) { e.uint32(uint32(x)) } |
| 840 |
|
| 841 |
func (d *decoder) int64() int64 { return int64(d.uint64()) } |
| 842 |
|
| 843 |
func (e *encoder) int64(x int64) { e.uint64(uint64(x)) } |
| 844 |
|
| 845 |
func (d *decoder) value(v reflect.Value) { |
| 846 |
switch v.Kind() { |
| 847 |
case reflect.Array: |
| 848 |
l := v.Len() |
| 849 |
for i := 0; i < l; i++ { |
| 850 |
d.value(v.Index(i)) |
| 851 |
} |
| 852 |
|
| 853 |
case reflect.Struct: |
| 854 |
t := v.Type() |
| 855 |
l := v.NumField() |
| 856 |
for i := 0; i < l; i++ { |
| 857 |
// Note: Calling v.CanSet() below is an optimization. |
| 858 |
// It would be sufficient to check the field name, |
| 859 |
// but creating the StructField info for each field is |
| 860 |
// costly (run "go test -bench=ReadStruct" and compare |
| 861 |
// results when making changes to this code). |
| 862 |
if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" { |
| 863 |
d.value(v) |
| 864 |
} else { |
| 865 |
d.skip(v) |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
case reflect.Slice: |
| 870 |
l := v.Len() |
| 871 |
for i := 0; i < l; i++ { |
| 872 |
d.value(v.Index(i)) |
| 873 |
} |
| 874 |
|
| 875 |
case reflect.Bool: |
| 876 |
v.SetBool(d.bool()) |
| 877 |
|
| 878 |
case reflect.Int8: |
| 879 |
v.SetInt(int64(d.int8())) |
| 880 |
case reflect.Int16: |
| 881 |
v.SetInt(int64(d.int16())) |
| 882 |
case reflect.Int32: |
| 883 |
v.SetInt(int64(d.int32())) |
| 884 |
case reflect.Int64: |
| 885 |
v.SetInt(d.int64()) |
| 886 |
|
| 887 |
case reflect.Uint8: |
| 888 |
v.SetUint(uint64(d.uint8())) |
| 889 |
case reflect.Uint16: |
| 890 |
v.SetUint(uint64(d.uint16())) |
| 891 |
case reflect.Uint32: |
| 892 |
v.SetUint(uint64(d.uint32())) |
| 893 |
case reflect.Uint64: |
| 894 |
v.SetUint(d.uint64()) |
| 895 |
|
| 896 |
case reflect.Float32: |
| 897 |
v.SetFloat(float64(math.Float32frombits(d.uint32()))) |
| 898 |
case reflect.Float64: |
| 899 |
v.SetFloat(math.Float64frombits(d.uint64())) |
| 900 |
|
| 901 |
case reflect.Complex64: |
| 902 |
v.SetComplex(complex( |
| 903 |
float64(math.Float32frombits(d.uint32())), |
| 904 |
float64(math.Float32frombits(d.uint32())), |
| 905 |
)) |
| 906 |
case reflect.Complex128: |
| 907 |
v.SetComplex(complex( |
| 908 |
math.Float64frombits(d.uint64()), |
| 909 |
math.Float64frombits(d.uint64()), |
| 910 |
)) |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
func (e *encoder) value(v reflect.Value) { |
| 915 |
switch v.Kind() { |
| 916 |
case reflect.Array: |
| 917 |
l := v.Len() |
| 918 |
for i := 0; i < l; i++ { |
| 919 |
e.value(v.Index(i)) |
| 920 |
} |
| 921 |
|
| 922 |
case reflect.Struct: |
| 923 |
t := v.Type() |
| 924 |
l := v.NumField() |
| 925 |
for i := 0; i < l; i++ { |
| 926 |
// see comment for corresponding code in decoder.value() |
| 927 |
if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" { |
| 928 |
e.value(v) |
| 929 |
} else { |
| 930 |
e.skip(v) |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
case reflect.Slice: |
| 935 |
l := v.Len() |
| 936 |
for i := 0; i < l; i++ { |
| 937 |
e.value(v.Index(i)) |
| 938 |
} |
| 939 |
|
| 940 |
case reflect.Bool: |
| 941 |
e.bool(v.Bool()) |
| 942 |
|
| 943 |
case reflect.Int8: |
| 944 |
e.int8(int8(v.Int())) |
| 945 |
case reflect.Int16: |
| 946 |
e.int16(int16(v.Int())) |
| 947 |
case reflect.Int32: |
| 948 |
e.int32(int32(v.Int())) |
| 949 |
case reflect.Int64: |
| 950 |
e.int64(v.Int()) |
| 951 |
|
| 952 |
case reflect.Uint8: |
| 953 |
e.uint8(uint8(v.Uint())) |
| 954 |
case reflect.Uint16: |
| 955 |
e.uint16(uint16(v.Uint())) |
| 956 |
case reflect.Uint32: |
| 957 |
e.uint32(uint32(v.Uint())) |
| 958 |
case reflect.Uint64: |
| 959 |
e.uint64(v.Uint()) |
| 960 |
|
| 961 |
case reflect.Float32: |
| 962 |
e.uint32(math.Float32bits(float32(v.Float()))) |
| 963 |
case reflect.Float64: |
| 964 |
e.uint64(math.Float64bits(v.Float())) |
| 965 |
|
| 966 |
case reflect.Complex64: |
| 967 |
x := v.Complex() |
| 968 |
e.uint32(math.Float32bits(float32(real(x)))) |
| 969 |
e.uint32(math.Float32bits(float32(imag(x)))) |
| 970 |
case reflect.Complex128: |
| 971 |
x := v.Complex() |
| 972 |
e.uint64(math.Float64bits(real(x))) |
| 973 |
e.uint64(math.Float64bits(imag(x))) |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
func (d *decoder) skip(v reflect.Value) { |
| 978 |
d.offset += dataSize(v) |
| 979 |
} |
| 980 |
|
| 981 |
func (e *encoder) skip(v reflect.Value) { |
| 982 |
n := dataSize(v) |
| 983 |
clear(e.buf[e.offset : e.offset+n]) |
| 984 |
e.offset += n |
| 985 |
} |
| 986 |
|
| 987 |
// intDataSize returns the size of the data required to represent the data when encoded, |
| 988 |
// and optionally a byte slice containing the encoded data if no conversion is necessary. |
| 989 |
// It returns zero, nil if the type cannot be implemented by the fast path in Read or Write. |
| 990 |
func intDataSize(data any) (int, []byte) { |
| 991 |
switch data := data.(type) { |
| 992 |
case bool, int8, uint8, *bool, *int8, *uint8: |
| 993 |
return 1, nil |
| 994 |
case []bool: |
| 995 |
return len(data), nil |
| 996 |
case []int8: |
| 997 |
return len(data), nil |
| 998 |
case []uint8: |
| 999 |
return len(data), data |
| 1000 |
case int16, uint16, *int16, *uint16: |
| 1001 |
return 2, nil |
| 1002 |
case []int16: |
| 1003 |
return 2 * len(data), nil |
| 1004 |
case []uint16: |
| 1005 |
return 2 * len(data), nil |
| 1006 |
case int32, uint32, *int32, *uint32: |
| 1007 |
return 4, nil |
| 1008 |
case []int32: |
| 1009 |
return 4 * len(data), nil |
| 1010 |
case []uint32: |
| 1011 |
return 4 * len(data), nil |
| 1012 |
case int64, uint64, *int64, *uint64: |
| 1013 |
return 8, nil |
| 1014 |
case []int64: |
| 1015 |
return 8 * len(data), nil |
| 1016 |
case []uint64: |
| 1017 |
return 8 * len(data), nil |
| 1018 |
case float32, *float32: |
| 1019 |
return 4, nil |
| 1020 |
case float64, *float64: |
| 1021 |
return 8, nil |
| 1022 |
case []float32: |
| 1023 |
return 4 * len(data), nil |
| 1024 |
case []float64: |
| 1025 |
return 8 * len(data), nil |
| 1026 |
} |
| 1027 |
return 0, nil |
| 1028 |
} |
| 1029 |
|
| 1030 |
// ensure grows buf to length len(buf) + n and returns the grown buffer |
| 1031 |
// and a slice starting at the original length of buf (that is, buf2[len(buf):]). |
| 1032 |
func ensure(buf []byte, n int) (buf2, pos []byte) { |
| 1033 |
l := len(buf) |
| 1034 |
buf = slices.Grow(buf, n)[:l+n] |
| 1035 |
return buf, buf[l:] |
| 1036 |
} |