PR tree-optimization/86274 - SEGFAULT when logging std::to_string(NAN)
[official-gcc.git] / libgo / go / encoding / binary / binary.go
blob2d01a3cb287b5dc32201e64c50a8ae5db65ab6a5
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.
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.
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.
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 protocol buffers.
22 package binary
24 import (
25 "errors"
26 "io"
27 "math"
28 "reflect"
31 // A ByteOrder specifies how to convert byte sequences into
32 // 16-, 32-, or 64-bit unsigned integers.
33 type ByteOrder interface {
34 Uint16([]byte) uint16
35 Uint32([]byte) uint32
36 Uint64([]byte) uint64
37 PutUint16([]byte, uint16)
38 PutUint32([]byte, uint32)
39 PutUint64([]byte, uint64)
40 String() string
43 // LittleEndian is the little-endian implementation of ByteOrder.
44 var LittleEndian littleEndian
46 // BigEndian is the big-endian implementation of ByteOrder.
47 var BigEndian bigEndian
49 type littleEndian struct{}
51 func (littleEndian) Uint16(b []byte) uint16 {
52 _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
53 return uint16(b[0]) | uint16(b[1])<<8
56 func (littleEndian) PutUint16(b []byte, v uint16) {
57 _ = b[1] // early bounds check to guarantee safety of writes below
58 b[0] = byte(v)
59 b[1] = byte(v >> 8)
62 func (littleEndian) Uint32(b []byte) uint32 {
63 _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
64 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
67 func (littleEndian) PutUint32(b []byte, v uint32) {
68 _ = b[3] // early bounds check to guarantee safety of writes below
69 b[0] = byte(v)
70 b[1] = byte(v >> 8)
71 b[2] = byte(v >> 16)
72 b[3] = byte(v >> 24)
75 func (littleEndian) Uint64(b []byte) uint64 {
76 _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
77 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
78 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
81 func (littleEndian) PutUint64(b []byte, v uint64) {
82 _ = b[7] // early bounds check to guarantee safety of writes below
83 b[0] = byte(v)
84 b[1] = byte(v >> 8)
85 b[2] = byte(v >> 16)
86 b[3] = byte(v >> 24)
87 b[4] = byte(v >> 32)
88 b[5] = byte(v >> 40)
89 b[6] = byte(v >> 48)
90 b[7] = byte(v >> 56)
93 func (littleEndian) String() string { return "LittleEndian" }
95 func (littleEndian) GoString() string { return "binary.LittleEndian" }
97 type bigEndian struct{}
99 func (bigEndian) Uint16(b []byte) uint16 {
100 _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
101 return uint16(b[1]) | uint16(b[0])<<8
104 func (bigEndian) PutUint16(b []byte, v uint16) {
105 _ = b[1] // early bounds check to guarantee safety of writes below
106 b[0] = byte(v >> 8)
107 b[1] = byte(v)
110 func (bigEndian) Uint32(b []byte) uint32 {
111 _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
112 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
115 func (bigEndian) PutUint32(b []byte, v uint32) {
116 _ = b[3] // early bounds check to guarantee safety of writes below
117 b[0] = byte(v >> 24)
118 b[1] = byte(v >> 16)
119 b[2] = byte(v >> 8)
120 b[3] = byte(v)
123 func (bigEndian) Uint64(b []byte) uint64 {
124 _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
125 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
126 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
129 func (bigEndian) PutUint64(b []byte, v uint64) {
130 _ = b[7] // early bounds check to guarantee safety of writes below
131 b[0] = byte(v >> 56)
132 b[1] = byte(v >> 48)
133 b[2] = byte(v >> 40)
134 b[3] = byte(v >> 32)
135 b[4] = byte(v >> 24)
136 b[5] = byte(v >> 16)
137 b[6] = byte(v >> 8)
138 b[7] = byte(v)
141 func (bigEndian) String() string { return "BigEndian" }
143 func (bigEndian) GoString() string { return "binary.BigEndian" }
145 // Read reads structured binary data from r into data.
146 // Data must be a pointer to a fixed-size value or a slice
147 // of fixed-size values.
148 // Bytes read from r are decoded using the specified byte order
149 // and written to successive fields of the data.
150 // When decoding boolean values, a zero byte is decoded as false, and
151 // any other non-zero byte is decoded as true.
152 // When reading into structs, the field data for fields with
153 // blank (_) field names is skipped; i.e., blank field names
154 // may be used for padding.
155 // When reading into a struct, all non-blank fields must be exported
156 // or Read may panic.
158 // The error is EOF only if no bytes were read.
159 // If an EOF happens after reading some but not all the bytes,
160 // Read returns ErrUnexpectedEOF.
161 func Read(r io.Reader, order ByteOrder, data interface{}) error {
162 // Fast path for basic types and slices.
163 if n := intDataSize(data); n != 0 {
164 var b [8]byte
165 var bs []byte
166 if n > len(b) {
167 bs = make([]byte, n)
168 } else {
169 bs = b[:n]
171 if _, err := io.ReadFull(r, bs); err != nil {
172 return err
174 switch data := data.(type) {
175 case *bool:
176 *data = b[0] != 0
177 case *int8:
178 *data = int8(b[0])
179 case *uint8:
180 *data = b[0]
181 case *int16:
182 *data = int16(order.Uint16(bs))
183 case *uint16:
184 *data = order.Uint16(bs)
185 case *int32:
186 *data = int32(order.Uint32(bs))
187 case *uint32:
188 *data = order.Uint32(bs)
189 case *int64:
190 *data = int64(order.Uint64(bs))
191 case *uint64:
192 *data = order.Uint64(bs)
193 case []bool:
194 for i, x := range bs { // Easier to loop over the input for 8-bit values.
195 data[i] = x != 0
197 case []int8:
198 for i, x := range bs {
199 data[i] = int8(x)
201 case []uint8:
202 copy(data, bs)
203 case []int16:
204 for i := range data {
205 data[i] = int16(order.Uint16(bs[2*i:]))
207 case []uint16:
208 for i := range data {
209 data[i] = order.Uint16(bs[2*i:])
211 case []int32:
212 for i := range data {
213 data[i] = int32(order.Uint32(bs[4*i:]))
215 case []uint32:
216 for i := range data {
217 data[i] = order.Uint32(bs[4*i:])
219 case []int64:
220 for i := range data {
221 data[i] = int64(order.Uint64(bs[8*i:]))
223 case []uint64:
224 for i := range data {
225 data[i] = order.Uint64(bs[8*i:])
228 return nil
231 // Fallback to reflect-based decoding.
232 v := reflect.ValueOf(data)
233 size := -1
234 switch v.Kind() {
235 case reflect.Ptr:
236 v = v.Elem()
237 size = dataSize(v)
238 case reflect.Slice:
239 size = dataSize(v)
241 if size < 0 {
242 return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String())
244 d := &decoder{order: order, buf: make([]byte, size)}
245 if _, err := io.ReadFull(r, d.buf); err != nil {
246 return err
248 d.value(v)
249 return nil
252 // Write writes the binary representation of data into w.
253 // Data must be a fixed-size value or a slice of fixed-size
254 // values, or a pointer to such data.
255 // Boolean values encode as one byte: 1 for true, and 0 for false.
256 // Bytes written to w are encoded using the specified byte order
257 // and read from successive fields of the data.
258 // When writing structs, zero values are written for fields
259 // with blank (_) field names.
260 func Write(w io.Writer, order ByteOrder, data interface{}) error {
261 // Fast path for basic types and slices.
262 if n := intDataSize(data); n != 0 {
263 var b [8]byte
264 var bs []byte
265 if n > len(b) {
266 bs = make([]byte, n)
267 } else {
268 bs = b[:n]
270 switch v := data.(type) {
271 case *bool:
272 if *v {
273 b[0] = 1
274 } else {
275 b[0] = 0
277 case bool:
278 if v {
279 b[0] = 1
280 } else {
281 b[0] = 0
283 case []bool:
284 for i, x := range v {
285 if x {
286 bs[i] = 1
287 } else {
288 bs[i] = 0
291 case *int8:
292 b[0] = byte(*v)
293 case int8:
294 b[0] = byte(v)
295 case []int8:
296 for i, x := range v {
297 bs[i] = byte(x)
299 case *uint8:
300 b[0] = *v
301 case uint8:
302 b[0] = v
303 case []uint8:
304 bs = v
305 case *int16:
306 order.PutUint16(bs, uint16(*v))
307 case int16:
308 order.PutUint16(bs, uint16(v))
309 case []int16:
310 for i, x := range v {
311 order.PutUint16(bs[2*i:], uint16(x))
313 case *uint16:
314 order.PutUint16(bs, *v)
315 case uint16:
316 order.PutUint16(bs, v)
317 case []uint16:
318 for i, x := range v {
319 order.PutUint16(bs[2*i:], x)
321 case *int32:
322 order.PutUint32(bs, uint32(*v))
323 case int32:
324 order.PutUint32(bs, uint32(v))
325 case []int32:
326 for i, x := range v {
327 order.PutUint32(bs[4*i:], uint32(x))
329 case *uint32:
330 order.PutUint32(bs, *v)
331 case uint32:
332 order.PutUint32(bs, v)
333 case []uint32:
334 for i, x := range v {
335 order.PutUint32(bs[4*i:], x)
337 case *int64:
338 order.PutUint64(bs, uint64(*v))
339 case int64:
340 order.PutUint64(bs, uint64(v))
341 case []int64:
342 for i, x := range v {
343 order.PutUint64(bs[8*i:], uint64(x))
345 case *uint64:
346 order.PutUint64(bs, *v)
347 case uint64:
348 order.PutUint64(bs, v)
349 case []uint64:
350 for i, x := range v {
351 order.PutUint64(bs[8*i:], x)
354 _, err := w.Write(bs)
355 return err
358 // Fallback to reflect-based encoding.
359 v := reflect.Indirect(reflect.ValueOf(data))
360 size := dataSize(v)
361 if size < 0 {
362 return errors.New("binary.Write: invalid type " + reflect.TypeOf(data).String())
364 buf := make([]byte, size)
365 e := &encoder{order: order, buf: buf}
366 e.value(v)
367 _, err := w.Write(buf)
368 return err
371 // Size returns how many bytes Write would generate to encode the value v, which
372 // must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
373 // If v is neither of these, Size returns -1.
374 func Size(v interface{}) int {
375 return dataSize(reflect.Indirect(reflect.ValueOf(v)))
378 // dataSize returns the number of bytes the actual data represented by v occupies in memory.
379 // For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
380 // it returns the length of the slice times the element size and does not count the memory
381 // occupied by the header. If the type of v is not acceptable, dataSize returns -1.
382 func dataSize(v reflect.Value) int {
383 if v.Kind() == reflect.Slice {
384 if s := sizeof(v.Type().Elem()); s >= 0 {
385 return s * v.Len()
387 return -1
389 return sizeof(v.Type())
392 // sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
393 func sizeof(t reflect.Type) int {
394 switch t.Kind() {
395 case reflect.Array:
396 if s := sizeof(t.Elem()); s >= 0 {
397 return s * t.Len()
400 case reflect.Struct:
401 sum := 0
402 for i, n := 0, t.NumField(); i < n; i++ {
403 s := sizeof(t.Field(i).Type)
404 if s < 0 {
405 return -1
407 sum += s
409 return sum
411 case reflect.Bool,
412 reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
413 reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
414 reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
415 return int(t.Size())
418 return -1
421 type coder struct {
422 order ByteOrder
423 buf []byte
426 type decoder coder
427 type encoder coder
429 func (d *decoder) bool() bool {
430 x := d.buf[0]
431 d.buf = d.buf[1:]
432 return x != 0
435 func (e *encoder) bool(x bool) {
436 if x {
437 e.buf[0] = 1
438 } else {
439 e.buf[0] = 0
441 e.buf = e.buf[1:]
444 func (d *decoder) uint8() uint8 {
445 x := d.buf[0]
446 d.buf = d.buf[1:]
447 return x
450 func (e *encoder) uint8(x uint8) {
451 e.buf[0] = x
452 e.buf = e.buf[1:]
455 func (d *decoder) uint16() uint16 {
456 x := d.order.Uint16(d.buf[0:2])
457 d.buf = d.buf[2:]
458 return x
461 func (e *encoder) uint16(x uint16) {
462 e.order.PutUint16(e.buf[0:2], x)
463 e.buf = e.buf[2:]
466 func (d *decoder) uint32() uint32 {
467 x := d.order.Uint32(d.buf[0:4])
468 d.buf = d.buf[4:]
469 return x
472 func (e *encoder) uint32(x uint32) {
473 e.order.PutUint32(e.buf[0:4], x)
474 e.buf = e.buf[4:]
477 func (d *decoder) uint64() uint64 {
478 x := d.order.Uint64(d.buf[0:8])
479 d.buf = d.buf[8:]
480 return x
483 func (e *encoder) uint64(x uint64) {
484 e.order.PutUint64(e.buf[0:8], x)
485 e.buf = e.buf[8:]
488 func (d *decoder) int8() int8 { return int8(d.uint8()) }
490 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
492 func (d *decoder) int16() int16 { return int16(d.uint16()) }
494 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
496 func (d *decoder) int32() int32 { return int32(d.uint32()) }
498 func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
500 func (d *decoder) int64() int64 { return int64(d.uint64()) }
502 func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
504 func (d *decoder) value(v reflect.Value) {
505 switch v.Kind() {
506 case reflect.Array:
507 l := v.Len()
508 for i := 0; i < l; i++ {
509 d.value(v.Index(i))
512 case reflect.Struct:
513 t := v.Type()
514 l := v.NumField()
515 for i := 0; i < l; i++ {
516 // Note: Calling v.CanSet() below is an optimization.
517 // It would be sufficient to check the field name,
518 // but creating the StructField info for each field is
519 // costly (run "go test -bench=ReadStruct" and compare
520 // results when making changes to this code).
521 if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
522 d.value(v)
523 } else {
524 d.skip(v)
528 case reflect.Slice:
529 l := v.Len()
530 for i := 0; i < l; i++ {
531 d.value(v.Index(i))
534 case reflect.Bool:
535 v.SetBool(d.bool())
537 case reflect.Int8:
538 v.SetInt(int64(d.int8()))
539 case reflect.Int16:
540 v.SetInt(int64(d.int16()))
541 case reflect.Int32:
542 v.SetInt(int64(d.int32()))
543 case reflect.Int64:
544 v.SetInt(d.int64())
546 case reflect.Uint8:
547 v.SetUint(uint64(d.uint8()))
548 case reflect.Uint16:
549 v.SetUint(uint64(d.uint16()))
550 case reflect.Uint32:
551 v.SetUint(uint64(d.uint32()))
552 case reflect.Uint64:
553 v.SetUint(d.uint64())
555 case reflect.Float32:
556 v.SetFloat(float64(math.Float32frombits(d.uint32())))
557 case reflect.Float64:
558 v.SetFloat(math.Float64frombits(d.uint64()))
560 case reflect.Complex64:
561 v.SetComplex(complex(
562 float64(math.Float32frombits(d.uint32())),
563 float64(math.Float32frombits(d.uint32())),
565 case reflect.Complex128:
566 v.SetComplex(complex(
567 math.Float64frombits(d.uint64()),
568 math.Float64frombits(d.uint64()),
573 func (e *encoder) value(v reflect.Value) {
574 switch v.Kind() {
575 case reflect.Array:
576 l := v.Len()
577 for i := 0; i < l; i++ {
578 e.value(v.Index(i))
581 case reflect.Struct:
582 t := v.Type()
583 l := v.NumField()
584 for i := 0; i < l; i++ {
585 // see comment for corresponding code in decoder.value()
586 if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
587 e.value(v)
588 } else {
589 e.skip(v)
593 case reflect.Slice:
594 l := v.Len()
595 for i := 0; i < l; i++ {
596 e.value(v.Index(i))
599 case reflect.Bool:
600 e.bool(v.Bool())
602 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
603 switch v.Type().Kind() {
604 case reflect.Int8:
605 e.int8(int8(v.Int()))
606 case reflect.Int16:
607 e.int16(int16(v.Int()))
608 case reflect.Int32:
609 e.int32(int32(v.Int()))
610 case reflect.Int64:
611 e.int64(v.Int())
614 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
615 switch v.Type().Kind() {
616 case reflect.Uint8:
617 e.uint8(uint8(v.Uint()))
618 case reflect.Uint16:
619 e.uint16(uint16(v.Uint()))
620 case reflect.Uint32:
621 e.uint32(uint32(v.Uint()))
622 case reflect.Uint64:
623 e.uint64(v.Uint())
626 case reflect.Float32, reflect.Float64:
627 switch v.Type().Kind() {
628 case reflect.Float32:
629 e.uint32(math.Float32bits(float32(v.Float())))
630 case reflect.Float64:
631 e.uint64(math.Float64bits(v.Float()))
634 case reflect.Complex64, reflect.Complex128:
635 switch v.Type().Kind() {
636 case reflect.Complex64:
637 x := v.Complex()
638 e.uint32(math.Float32bits(float32(real(x))))
639 e.uint32(math.Float32bits(float32(imag(x))))
640 case reflect.Complex128:
641 x := v.Complex()
642 e.uint64(math.Float64bits(real(x)))
643 e.uint64(math.Float64bits(imag(x)))
648 func (d *decoder) skip(v reflect.Value) {
649 d.buf = d.buf[dataSize(v):]
652 func (e *encoder) skip(v reflect.Value) {
653 n := dataSize(v)
654 for i := range e.buf[0:n] {
655 e.buf[i] = 0
657 e.buf = e.buf[n:]
660 // intDataSize returns the size of the data required to represent the data when encoded.
661 // It returns zero if the type cannot be implemented by the fast path in Read or Write.
662 func intDataSize(data interface{}) int {
663 switch data := data.(type) {
664 case bool, int8, uint8, *bool, *int8, *uint8:
665 return 1
666 case []int8:
667 return len(data)
668 case []uint8:
669 return len(data)
670 case int16, uint16, *int16, *uint16:
671 return 2
672 case []int16:
673 return 2 * len(data)
674 case []uint16:
675 return 2 * len(data)
676 case int32, uint32, *int32, *uint32:
677 return 4
678 case []int32:
679 return 4 * len(data)
680 case []uint32:
681 return 4 * len(data)
682 case int64, uint64, *int64, *uint64:
683 return 8
684 case []int64:
685 return 8 * len(data)
686 case []uint64:
687 return 8 * len(data)
689 return 0