...
Run Format

Source file src/encoding/binary/binary.go

Documentation: encoding/binary

     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 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 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.
   157  //
   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]
   170  		}
   171  		if _, err := io.ReadFull(r, bs); err != nil {
   172  			return err
   173  		}
   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
   196  			}
   197  		case []int8:
   198  			for i, x := range bs {
   199  				data[i] = int8(x)
   200  			}
   201  		case []uint8:
   202  			copy(data, bs)
   203  		case []int16:
   204  			for i := range data {
   205  				data[i] = int16(order.Uint16(bs[2*i:]))
   206  			}
   207  		case []uint16:
   208  			for i := range data {
   209  				data[i] = order.Uint16(bs[2*i:])
   210  			}
   211  		case []int32:
   212  			for i := range data {
   213  				data[i] = int32(order.Uint32(bs[4*i:]))
   214  			}
   215  		case []uint32:
   216  			for i := range data {
   217  				data[i] = order.Uint32(bs[4*i:])
   218  			}
   219  		case []int64:
   220  			for i := range data {
   221  				data[i] = int64(order.Uint64(bs[8*i:]))
   222  			}
   223  		case []uint64:
   224  			for i := range data {
   225  				data[i] = order.Uint64(bs[8*i:])
   226  			}
   227  		}
   228  		return nil
   229  	}
   230  
   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)
   240  	}
   241  	if size < 0 {
   242  		return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String())
   243  	}
   244  	d := &decoder{order: order, buf: make([]byte, size)}
   245  	if _, err := io.ReadFull(r, d.buf); err != nil {
   246  		return err
   247  	}
   248  	d.value(v)
   249  	return nil
   250  }
   251  
   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]
   269  		}
   270  		switch v := data.(type) {
   271  		case *bool:
   272  			if *v {
   273  				b[0] = 1
   274  			} else {
   275  				b[0] = 0
   276  			}
   277  		case bool:
   278  			if v {
   279  				b[0] = 1
   280  			} else {
   281  				b[0] = 0
   282  			}
   283  		case []bool:
   284  			for i, x := range v {
   285  				if x {
   286  					bs[i] = 1
   287  				} else {
   288  					bs[i] = 0
   289  				}
   290  			}
   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)
   298  			}
   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))
   312  			}
   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)
   320  			}
   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))
   328  			}
   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)
   336  			}
   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))
   344  			}
   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)
   352  			}
   353  		}
   354  		_, err := w.Write(bs)
   355  		return err
   356  	}
   357  
   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())
   363  	}
   364  	buf := make([]byte, size)
   365  	e := &encoder{order: order, buf: buf}
   366  	e.value(v)
   367  	_, err := w.Write(buf)
   368  	return err
   369  }
   370  
   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)))
   376  }
   377  
   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()
   386  		}
   387  		return -1
   388  	}
   389  	return sizeof(v.Type())
   390  }
   391  
   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()
   398  		}
   399  
   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
   406  			}
   407  			sum += s
   408  		}
   409  		return sum
   410  
   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())
   416  	}
   417  
   418  	return -1
   419  }
   420  
   421  type coder struct {
   422  	order  ByteOrder
   423  	buf    []byte
   424  	offset int
   425  }
   426  
   427  type decoder coder
   428  type encoder coder
   429  
   430  func (d *decoder) bool() bool {
   431  	x := d.buf[d.offset]
   432  	d.offset++
   433  	return x != 0
   434  }
   435  
   436  func (e *encoder) bool(x bool) {
   437  	if x {
   438  		e.buf[e.offset] = 1
   439  	} else {
   440  		e.buf[e.offset] = 0
   441  	}
   442  	e.offset++
   443  }
   444  
   445  func (d *decoder) uint8() uint8 {
   446  	x := d.buf[d.offset]
   447  	d.offset++
   448  	return x
   449  }
   450  
   451  func (e *encoder) uint8(x uint8) {
   452  	e.buf[e.offset] = x
   453  	e.offset++
   454  }
   455  
   456  func (d *decoder) uint16() uint16 {
   457  	x := d.order.Uint16(d.buf[d.offset : d.offset+2])
   458  	d.offset += 2
   459  	return x
   460  }
   461  
   462  func (e *encoder) uint16(x uint16) {
   463  	e.order.PutUint16(e.buf[e.offset:e.offset+2], x)
   464  	e.offset += 2
   465  }
   466  
   467  func (d *decoder) uint32() uint32 {
   468  	x := d.order.Uint32(d.buf[d.offset : d.offset+4])
   469  	d.offset += 4
   470  	return x
   471  }
   472  
   473  func (e *encoder) uint32(x uint32) {
   474  	e.order.PutUint32(e.buf[e.offset:e.offset+4], x)
   475  	e.offset += 4
   476  }
   477  
   478  func (d *decoder) uint64() uint64 {
   479  	x := d.order.Uint64(d.buf[d.offset : d.offset+8])
   480  	d.offset += 8
   481  	return x
   482  }
   483  
   484  func (e *encoder) uint64(x uint64) {
   485  	e.order.PutUint64(e.buf[e.offset:e.offset+8], x)
   486  	e.offset += 8
   487  }
   488  
   489  func (d *decoder) int8() int8 { return int8(d.uint8()) }
   490  
   491  func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
   492  
   493  func (d *decoder) int16() int16 { return int16(d.uint16()) }
   494  
   495  func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
   496  
   497  func (d *decoder) int32() int32 { return int32(d.uint32()) }
   498  
   499  func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
   500  
   501  func (d *decoder) int64() int64 { return int64(d.uint64()) }
   502  
   503  func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
   504  
   505  func (d *decoder) value(v reflect.Value) {
   506  	switch v.Kind() {
   507  	case reflect.Array:
   508  		l := v.Len()
   509  		for i := 0; i < l; i++ {
   510  			d.value(v.Index(i))
   511  		}
   512  
   513  	case reflect.Struct:
   514  		t := v.Type()
   515  		l := v.NumField()
   516  		for i := 0; i < l; i++ {
   517  			// Note: Calling v.CanSet() below is an optimization.
   518  			// It would be sufficient to check the field name,
   519  			// but creating the StructField info for each field is
   520  			// costly (run "go test -bench=ReadStruct" and compare
   521  			// results when making changes to this code).
   522  			if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
   523  				d.value(v)
   524  			} else {
   525  				d.skip(v)
   526  			}
   527  		}
   528  
   529  	case reflect.Slice:
   530  		l := v.Len()
   531  		for i := 0; i < l; i++ {
   532  			d.value(v.Index(i))
   533  		}
   534  
   535  	case reflect.Bool:
   536  		v.SetBool(d.bool())
   537  
   538  	case reflect.Int8:
   539  		v.SetInt(int64(d.int8()))
   540  	case reflect.Int16:
   541  		v.SetInt(int64(d.int16()))
   542  	case reflect.Int32:
   543  		v.SetInt(int64(d.int32()))
   544  	case reflect.Int64:
   545  		v.SetInt(d.int64())
   546  
   547  	case reflect.Uint8:
   548  		v.SetUint(uint64(d.uint8()))
   549  	case reflect.Uint16:
   550  		v.SetUint(uint64(d.uint16()))
   551  	case reflect.Uint32:
   552  		v.SetUint(uint64(d.uint32()))
   553  	case reflect.Uint64:
   554  		v.SetUint(d.uint64())
   555  
   556  	case reflect.Float32:
   557  		v.SetFloat(float64(math.Float32frombits(d.uint32())))
   558  	case reflect.Float64:
   559  		v.SetFloat(math.Float64frombits(d.uint64()))
   560  
   561  	case reflect.Complex64:
   562  		v.SetComplex(complex(
   563  			float64(math.Float32frombits(d.uint32())),
   564  			float64(math.Float32frombits(d.uint32())),
   565  		))
   566  	case reflect.Complex128:
   567  		v.SetComplex(complex(
   568  			math.Float64frombits(d.uint64()),
   569  			math.Float64frombits(d.uint64()),
   570  		))
   571  	}
   572  }
   573  
   574  func (e *encoder) value(v reflect.Value) {
   575  	switch v.Kind() {
   576  	case reflect.Array:
   577  		l := v.Len()
   578  		for i := 0; i < l; i++ {
   579  			e.value(v.Index(i))
   580  		}
   581  
   582  	case reflect.Struct:
   583  		t := v.Type()
   584  		l := v.NumField()
   585  		for i := 0; i < l; i++ {
   586  			// see comment for corresponding code in decoder.value()
   587  			if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
   588  				e.value(v)
   589  			} else {
   590  				e.skip(v)
   591  			}
   592  		}
   593  
   594  	case reflect.Slice:
   595  		l := v.Len()
   596  		for i := 0; i < l; i++ {
   597  			e.value(v.Index(i))
   598  		}
   599  
   600  	case reflect.Bool:
   601  		e.bool(v.Bool())
   602  
   603  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   604  		switch v.Type().Kind() {
   605  		case reflect.Int8:
   606  			e.int8(int8(v.Int()))
   607  		case reflect.Int16:
   608  			e.int16(int16(v.Int()))
   609  		case reflect.Int32:
   610  			e.int32(int32(v.Int()))
   611  		case reflect.Int64:
   612  			e.int64(v.Int())
   613  		}
   614  
   615  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   616  		switch v.Type().Kind() {
   617  		case reflect.Uint8:
   618  			e.uint8(uint8(v.Uint()))
   619  		case reflect.Uint16:
   620  			e.uint16(uint16(v.Uint()))
   621  		case reflect.Uint32:
   622  			e.uint32(uint32(v.Uint()))
   623  		case reflect.Uint64:
   624  			e.uint64(v.Uint())
   625  		}
   626  
   627  	case reflect.Float32, reflect.Float64:
   628  		switch v.Type().Kind() {
   629  		case reflect.Float32:
   630  			e.uint32(math.Float32bits(float32(v.Float())))
   631  		case reflect.Float64:
   632  			e.uint64(math.Float64bits(v.Float()))
   633  		}
   634  
   635  	case reflect.Complex64, reflect.Complex128:
   636  		switch v.Type().Kind() {
   637  		case reflect.Complex64:
   638  			x := v.Complex()
   639  			e.uint32(math.Float32bits(float32(real(x))))
   640  			e.uint32(math.Float32bits(float32(imag(x))))
   641  		case reflect.Complex128:
   642  			x := v.Complex()
   643  			e.uint64(math.Float64bits(real(x)))
   644  			e.uint64(math.Float64bits(imag(x)))
   645  		}
   646  	}
   647  }
   648  
   649  func (d *decoder) skip(v reflect.Value) {
   650  	d.offset += dataSize(v)
   651  }
   652  
   653  func (e *encoder) skip(v reflect.Value) {
   654  	n := dataSize(v)
   655  	zero := e.buf[e.offset : e.offset+n]
   656  	for i := range zero {
   657  		zero[i] = 0
   658  	}
   659  	e.offset += n
   660  }
   661  
   662  // intDataSize returns the size of the data required to represent the data when encoded.
   663  // It returns zero if the type cannot be implemented by the fast path in Read or Write.
   664  func intDataSize(data interface{}) int {
   665  	switch data := data.(type) {
   666  	case bool, int8, uint8, *bool, *int8, *uint8:
   667  		return 1
   668  	case []bool:
   669  		return len(data)
   670  	case []int8:
   671  		return len(data)
   672  	case []uint8:
   673  		return len(data)
   674  	case int16, uint16, *int16, *uint16:
   675  		return 2
   676  	case []int16:
   677  		return 2 * len(data)
   678  	case []uint16:
   679  		return 2 * len(data)
   680  	case int32, uint32, *int32, *uint32:
   681  		return 4
   682  	case []int32:
   683  		return 4 * len(data)
   684  	case []uint32:
   685  		return 4 * len(data)
   686  	case int64, uint64, *int64, *uint64:
   687  		return 8
   688  	case []int64:
   689  		return 8 * len(data)
   690  	case []uint64:
   691  		return 8 * len(data)
   692  	}
   693  	return 0
   694  }
   695  

View as plain text