binary.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2016 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. // +build solaris
  5. package lif
  6. // This file contains duplicates of encoding/binary package.
  7. //
  8. // This package is supposed to be used by the net package of standard
  9. // library. Therefore the package set used in the package must be the
  10. // same as net package.
  11. var littleEndian binaryLittleEndian
  12. type binaryByteOrder 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. }
  20. type binaryLittleEndian struct{}
  21. func (binaryLittleEndian) Uint16(b []byte) uint16 {
  22. _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
  23. return uint16(b[0]) | uint16(b[1])<<8
  24. }
  25. func (binaryLittleEndian) PutUint16(b []byte, v uint16) {
  26. _ = b[1] // early bounds check to guarantee safety of writes below
  27. b[0] = byte(v)
  28. b[1] = byte(v >> 8)
  29. }
  30. func (binaryLittleEndian) Uint32(b []byte) uint32 {
  31. _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
  32. return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  33. }
  34. func (binaryLittleEndian) PutUint32(b []byte, v uint32) {
  35. _ = b[3] // early bounds check to guarantee safety of writes below
  36. b[0] = byte(v)
  37. b[1] = byte(v >> 8)
  38. b[2] = byte(v >> 16)
  39. b[3] = byte(v >> 24)
  40. }
  41. func (binaryLittleEndian) Uint64(b []byte) uint64 {
  42. _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
  43. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
  44. uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  45. }
  46. func (binaryLittleEndian) PutUint64(b []byte, v uint64) {
  47. _ = b[7] // early bounds check to guarantee safety of writes below
  48. b[0] = byte(v)
  49. b[1] = byte(v >> 8)
  50. b[2] = byte(v >> 16)
  51. b[3] = byte(v >> 24)
  52. b[4] = byte(v >> 32)
  53. b[5] = byte(v >> 40)
  54. b[6] = byte(v >> 48)
  55. b[7] = byte(v >> 56)
  56. }