github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/common/binary.go (about)

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