...
Run Format

Source file src/encoding/binary/binary.go

     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 (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 protocol buffers.
    22	package binary
    23	
    24	import (
    25		"errors"
    26		"io"
    27		"math"
    28		"reflect"
    29	)
    30	
    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
    41	}
    42	
    43	// LittleEndian is the little-endian implementation of ByteOrder.
    44	var LittleEndian littleEndian
    45	
    46	// BigEndian is the big-endian implementation of ByteOrder.
    47	var BigEndian bigEndian
    48	
    49	type littleEndian struct{}
    50	
    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
    54	}
    55	
    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)
    60	}
    61	
    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
    65	}
    66	
    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)
    73	}
    74	
    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
    79	}
    80	
    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)
    91	}
    92	
    93	func (littleEndian) String() string { return "LittleEndian" }
    94	
    95	func (littleEndian) GoString() string { return "binary.LittleEndian" }
    96	
    97	type bigEndian struct{}
    98	
    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
   102	}
   103	
   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)
   108	}
   109	
   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
   113	}
   114	
   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)
   121	}
   122	
   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
   127	}
   128	
   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)
   139	}
   140	
   141	func (bigEndian) String() string { return "BigEndian" }
   142	
   143	func (bigEndian) GoString() string { return "binary.BigEndian" }
   144	
   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 reading into structs, the field data for fields with
   151	// blank (_) field names is skipped; i.e., blank field names
   152	// may be used for padding.
   153	// When reading into a struct, all non-blank fields must be exported.
   154	//
   155	// The error is EOF only if no bytes were read.
   156	// If an EOF happens after reading some but not all the bytes,
   157	// Read returns ErrUnexpectedEOF.
   158	func Read(r io.Reader, order ByteOrder, data interface{}) error {
   159		// Fast path for basic types and slices.
   160		if n := intDataSize(data); n != 0 {
   161			var b [8]byte
   162			var bs []byte
   163			if n > len(b) {
   164				bs = make([]byte, n)
   165			} else {
   166				bs = b[:n]
   167			}
   168			if _, err := io.ReadFull(r, bs); err != nil {
   169				return err
   170			}
   171			switch data := data.(type) {
   172			case *int8:
   173				*data = int8(b[0])
   174			case *uint8:
   175				*data = b[0]
   176			case *int16:
   177				*data = int16(order.Uint16(bs))
   178			case *uint16:
   179				*data = order.Uint16(bs)
   180			case *int32:
   181				*data = int32(order.Uint32(bs))
   182			case *uint32:
   183				*data = order.Uint32(bs)
   184			case *int64:
   185				*data = int64(order.Uint64(bs))
   186			case *uint64:
   187				*data = order.Uint64(bs)
   188			case []int8:
   189				for i, x := range bs { // Easier to loop over the input for 8-bit values.
   190					data[i] = int8(x)
   191				}
   192			case []uint8:
   193				copy(data, bs)
   194			case []int16:
   195				for i := range data {
   196					data[i] = int16(order.Uint16(bs[2*i:]))
   197				}
   198			case []uint16:
   199				for i := range data {
   200					data[i] = order.Uint16(bs[2*i:])
   201				}
   202			case []int32:
   203				for i := range data {
   204					data[i] = int32(order.Uint32(bs[4*i:]))
   205				}
   206			case []uint32:
   207				for i := range data {
   208					data[i] = order.Uint32(bs[4*i:])
   209				}
   210			case []int64:
   211				for i := range data {
   212					data[i] = int64(order.Uint64(bs[8*i:]))
   213				}
   214			case []uint64:
   215				for i := range data {
   216					data[i] = order.Uint64(bs[8*i:])
   217				}
   218			}
   219			return nil
   220		}
   221	
   222		// Fallback to reflect-based decoding.
   223		v := reflect.ValueOf(data)
   224		size := -1
   225		switch v.Kind() {
   226		case reflect.Ptr:
   227			v = v.Elem()
   228			size = dataSize(v)
   229		case reflect.Slice:
   230			size = dataSize(v)
   231		}
   232		if size < 0 {
   233			return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String())
   234		}
   235		d := &decoder{order: order, buf: make([]byte, size)}
   236		if _, err := io.ReadFull(r, d.buf); err != nil {
   237			return err
   238		}
   239		d.value(v)
   240		return nil
   241	}
   242	
   243	// Write writes the binary representation of data into w.
   244	// Data must be a fixed-size value or a slice of fixed-size
   245	// values, or a pointer to such data.
   246	// Bytes written to w are encoded using the specified byte order
   247	// and read from successive fields of the data.
   248	// When writing structs, zero values are written for fields
   249	// with blank (_) field names.
   250	func Write(w io.Writer, order ByteOrder, data interface{}) error {
   251		// Fast path for basic types and slices.
   252		if n := intDataSize(data); n != 0 {
   253			var b [8]byte
   254			var bs []byte
   255			if n > len(b) {
   256				bs = make([]byte, n)
   257			} else {
   258				bs = b[:n]
   259			}
   260			switch v := data.(type) {
   261			case *int8:
   262				b[0] = byte(*v)
   263			case int8:
   264				b[0] = byte(v)
   265			case []int8:
   266				for i, x := range v {
   267					bs[i] = byte(x)
   268				}
   269			case *uint8:
   270				b[0] = *v
   271			case uint8:
   272				b[0] = v
   273			case []uint8:
   274				bs = v
   275			case *int16:
   276				order.PutUint16(bs, uint16(*v))
   277			case int16:
   278				order.PutUint16(bs, uint16(v))
   279			case []int16:
   280				for i, x := range v {
   281					order.PutUint16(bs[2*i:], uint16(x))
   282				}
   283			case *uint16:
   284				order.PutUint16(bs, *v)
   285			case uint16:
   286				order.PutUint16(bs, v)
   287			case []uint16:
   288				for i, x := range v {
   289					order.PutUint16(bs[2*i:], x)
   290				}
   291			case *int32:
   292				order.PutUint32(bs, uint32(*v))
   293			case int32:
   294				order.PutUint32(bs, uint32(v))
   295			case []int32:
   296				for i, x := range v {
   297					order.PutUint32(bs[4*i:], uint32(x))
   298				}
   299			case *uint32:
   300				order.PutUint32(bs, *v)
   301			case uint32:
   302				order.PutUint32(bs, v)
   303			case []uint32:
   304				for i, x := range v {
   305					order.PutUint32(bs[4*i:], x)
   306				}
   307			case *int64:
   308				order.PutUint64(bs, uint64(*v))
   309			case int64:
   310				order.PutUint64(bs, uint64(v))
   311			case []int64:
   312				for i, x := range v {
   313					order.PutUint64(bs[8*i:], uint64(x))
   314				}
   315			case *uint64:
   316				order.PutUint64(bs, *v)
   317			case uint64:
   318				order.PutUint64(bs, v)
   319			case []uint64:
   320				for i, x := range v {
   321					order.PutUint64(bs[8*i:], x)
   322				}
   323			}
   324			_, err := w.Write(bs)
   325			return err
   326		}
   327	
   328		// Fallback to reflect-based encoding.
   329		v := reflect.Indirect(reflect.ValueOf(data))
   330		size := dataSize(v)
   331		if size < 0 {
   332			return errors.New("binary.Write: invalid type " + reflect.TypeOf(data).String())
   333		}
   334		buf := make([]byte, size)
   335		e := &encoder{order: order, buf: buf}
   336		e.value(v)
   337		_, err := w.Write(buf)
   338		return err
   339	}
   340	
   341	// Size returns how many bytes Write would generate to encode the value v, which
   342	// must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
   343	// If v is neither of these, Size returns -1.
   344	func Size(v interface{}) int {
   345		return dataSize(reflect.Indirect(reflect.ValueOf(v)))
   346	}
   347	
   348	// dataSize returns the number of bytes the actual data represented by v occupies in memory.
   349	// For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
   350	// it returns the length of the slice times the element size and does not count the memory
   351	// occupied by the header. If the type of v is not acceptable, dataSize returns -1.
   352	func dataSize(v reflect.Value) int {
   353		if v.Kind() == reflect.Slice {
   354			if s := sizeof(v.Type().Elem()); s >= 0 {
   355				return s * v.Len()
   356			}
   357			return -1
   358		}
   359		return sizeof(v.Type())
   360	}
   361	
   362	// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
   363	func sizeof(t reflect.Type) int {
   364		switch t.Kind() {
   365		case reflect.Array:
   366			if s := sizeof(t.Elem()); s >= 0 {
   367				return s * t.Len()
   368			}
   369	
   370		case reflect.Struct:
   371			sum := 0
   372			for i, n := 0, t.NumField(); i < n; i++ {
   373				s := sizeof(t.Field(i).Type)
   374				if s < 0 {
   375					return -1
   376				}
   377				sum += s
   378			}
   379			return sum
   380	
   381		case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
   382			reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
   383			reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
   384			return int(t.Size())
   385		}
   386	
   387		return -1
   388	}
   389	
   390	type coder struct {
   391		order ByteOrder
   392		buf   []byte
   393	}
   394	
   395	type decoder coder
   396	type encoder coder
   397	
   398	func (d *decoder) uint8() uint8 {
   399		x := d.buf[0]
   400		d.buf = d.buf[1:]
   401		return x
   402	}
   403	
   404	func (e *encoder) uint8(x uint8) {
   405		e.buf[0] = x
   406		e.buf = e.buf[1:]
   407	}
   408	
   409	func (d *decoder) uint16() uint16 {
   410		x := d.order.Uint16(d.buf[0:2])
   411		d.buf = d.buf[2:]
   412		return x
   413	}
   414	
   415	func (e *encoder) uint16(x uint16) {
   416		e.order.PutUint16(e.buf[0:2], x)
   417		e.buf = e.buf[2:]
   418	}
   419	
   420	func (d *decoder) uint32() uint32 {
   421		x := d.order.Uint32(d.buf[0:4])
   422		d.buf = d.buf[4:]
   423		return x
   424	}
   425	
   426	func (e *encoder) uint32(x uint32) {
   427		e.order.PutUint32(e.buf[0:4], x)
   428		e.buf = e.buf[4:]
   429	}
   430	
   431	func (d *decoder) uint64() uint64 {
   432		x := d.order.Uint64(d.buf[0:8])
   433		d.buf = d.buf[8:]
   434		return x
   435	}
   436	
   437	func (e *encoder) uint64(x uint64) {
   438		e.order.PutUint64(e.buf[0:8], x)
   439		e.buf = e.buf[8:]
   440	}
   441	
   442	func (d *decoder) int8() int8 { return int8(d.uint8()) }
   443	
   444	func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
   445	
   446	func (d *decoder) int16() int16 { return int16(d.uint16()) }
   447	
   448	func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
   449	
   450	func (d *decoder) int32() int32 { return int32(d.uint32()) }
   451	
   452	func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
   453	
   454	func (d *decoder) int64() int64 { return int64(d.uint64()) }
   455	
   456	func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
   457	
   458	func (d *decoder) value(v reflect.Value) {
   459		switch v.Kind() {
   460		case reflect.Array:
   461			l := v.Len()
   462			for i := 0; i < l; i++ {
   463				d.value(v.Index(i))
   464			}
   465	
   466		case reflect.Struct:
   467			t := v.Type()
   468			l := v.NumField()
   469			for i := 0; i < l; i++ {
   470				// Note: Calling v.CanSet() below is an optimization.
   471				// It would be sufficient to check the field name,
   472				// but creating the StructField info for each field is
   473				// costly (run "go test -bench=ReadStruct" and compare
   474				// results when making changes to this code).
   475				if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
   476					d.value(v)
   477				} else {
   478					d.skip(v)
   479				}
   480			}
   481	
   482		case reflect.Slice:
   483			l := v.Len()
   484			for i := 0; i < l; i++ {
   485				d.value(v.Index(i))
   486			}
   487	
   488		case reflect.Int8:
   489			v.SetInt(int64(d.int8()))
   490		case reflect.Int16:
   491			v.SetInt(int64(d.int16()))
   492		case reflect.Int32:
   493			v.SetInt(int64(d.int32()))
   494		case reflect.Int64:
   495			v.SetInt(d.int64())
   496	
   497		case reflect.Uint8:
   498			v.SetUint(uint64(d.uint8()))
   499		case reflect.Uint16:
   500			v.SetUint(uint64(d.uint16()))
   501		case reflect.Uint32:
   502			v.SetUint(uint64(d.uint32()))
   503		case reflect.Uint64:
   504			v.SetUint(d.uint64())
   505	
   506		case reflect.Float32:
   507			v.SetFloat(float64(math.Float32frombits(d.uint32())))
   508		case reflect.Float64:
   509			v.SetFloat(math.Float64frombits(d.uint64()))
   510	
   511		case reflect.Complex64:
   512			v.SetComplex(complex(
   513				float64(math.Float32frombits(d.uint32())),
   514				float64(math.Float32frombits(d.uint32())),
   515			))
   516		case reflect.Complex128:
   517			v.SetComplex(complex(
   518				math.Float64frombits(d.uint64()),
   519				math.Float64frombits(d.uint64()),
   520			))
   521		}
   522	}
   523	
   524	func (e *encoder) value(v reflect.Value) {
   525		switch v.Kind() {
   526		case reflect.Array:
   527			l := v.Len()
   528			for i := 0; i < l; i++ {
   529				e.value(v.Index(i))
   530			}
   531	
   532		case reflect.Struct:
   533			t := v.Type()
   534			l := v.NumField()
   535			for i := 0; i < l; i++ {
   536				// see comment for corresponding code in decoder.value()
   537				if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
   538					e.value(v)
   539				} else {
   540					e.skip(v)
   541				}
   542			}
   543	
   544		case reflect.Slice:
   545			l := v.Len()
   546			for i := 0; i < l; i++ {
   547				e.value(v.Index(i))
   548			}
   549	
   550		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   551			switch v.Type().Kind() {
   552			case reflect.Int8:
   553				e.int8(int8(v.Int()))
   554			case reflect.Int16:
   555				e.int16(int16(v.Int()))
   556			case reflect.Int32:
   557				e.int32(int32(v.Int()))
   558			case reflect.Int64:
   559				e.int64(v.Int())
   560			}
   561	
   562		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   563			switch v.Type().Kind() {
   564			case reflect.Uint8:
   565				e.uint8(uint8(v.Uint()))
   566			case reflect.Uint16:
   567				e.uint16(uint16(v.Uint()))
   568			case reflect.Uint32:
   569				e.uint32(uint32(v.Uint()))
   570			case reflect.Uint64:
   571				e.uint64(v.Uint())
   572			}
   573	
   574		case reflect.Float32, reflect.Float64:
   575			switch v.Type().Kind() {
   576			case reflect.Float32:
   577				e.uint32(math.Float32bits(float32(v.Float())))
   578			case reflect.Float64:
   579				e.uint64(math.Float64bits(v.Float()))
   580			}
   581	
   582		case reflect.Complex64, reflect.Complex128:
   583			switch v.Type().Kind() {
   584			case reflect.Complex64:
   585				x := v.Complex()
   586				e.uint32(math.Float32bits(float32(real(x))))
   587				e.uint32(math.Float32bits(float32(imag(x))))
   588			case reflect.Complex128:
   589				x := v.Complex()
   590				e.uint64(math.Float64bits(real(x)))
   591				e.uint64(math.Float64bits(imag(x)))
   592			}
   593		}
   594	}
   595	
   596	func (d *decoder) skip(v reflect.Value) {
   597		d.buf = d.buf[dataSize(v):]
   598	}
   599	
   600	func (e *encoder) skip(v reflect.Value) {
   601		n := dataSize(v)
   602		for i := range e.buf[0:n] {
   603			e.buf[i] = 0
   604		}
   605		e.buf = e.buf[n:]
   606	}
   607	
   608	// intDataSize returns the size of the data required to represent the data when encoded.
   609	// It returns zero if the type cannot be implemented by the fast path in Read or Write.
   610	func intDataSize(data interface{}) int {
   611		switch data := data.(type) {
   612		case int8, uint8, *int8, *uint8:
   613			return 1
   614		case []int8:
   615			return len(data)
   616		case []uint8:
   617			return len(data)
   618		case int16, uint16, *int16, *uint16:
   619			return 2
   620		case []int16:
   621			return 2 * len(data)
   622		case []uint16:
   623			return 2 * len(data)
   624		case int32, uint32, *int32, *uint32:
   625			return 4
   626		case []int32:
   627			return 4 * len(data)
   628		case []uint32:
   629			return 4 * len(data)
   630		case int64, uint64, *int64, *uint64:
   631			return 8
   632		case []int64:
   633			return 8 * len(data)
   634		case []uint64:
   635			return 8 * len(data)
   636		}
   637		return 0
   638	}
   639	

View as plain text