binary.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. //go:build solaris
  5. // +build solaris
  6. package lif
  7. // This file contains duplicates of encoding/binary package.
  8. //
  9. // This package is supposed to be used by the net package of standard
  10. // library. Therefore the package set used in the package must be the
  11. // same as net package.
  12. var (
  13. littleEndian binaryLittleEndian
  14. bigEndian binaryBigEndian
  15. )
  16. type binaryByteOrder interface {
  17. Uint16([]byte) uint16
  18. Uint32([]byte) uint32
  19. Uint64([]byte) uint64
  20. PutUint16([]byte, uint16)
  21. PutUint32([]byte, uint32)
  22. PutUint64([]byte, uint64)
  23. }
  24. type binaryLittleEndian struct{}
  25. func (binaryLittleEndian) Uint16(b []byte) uint16 {
  26. _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
  27. return uint16(b[0]) | uint16(b[1])<<8
  28. }
  29. func (binaryLittleEndian) PutUint16(b []byte, v uint16) {
  30. _ = b[1] // early bounds check to guarantee safety of writes below
  31. b[0] = byte(v)
  32. b[1] = byte(v >> 8)
  33. }
  34. func (binaryLittleEndian) Uint32(b []byte) uint32 {
  35. _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
  36. return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  37. }
  38. func (binaryLittleEndian) PutUint32(b []byte, v uint32) {
  39. _ = b[3] // early bounds check to guarantee safety of writes below
  40. b[0] = byte(v)
  41. b[1] = byte(v >> 8)
  42. b[2] = byte(v >> 16)
  43. b[3] = byte(v >> 24)
  44. }
  45. func (binaryLittleEndian) Uint64(b []byte) uint64 {
  46. _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
  47. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
  48. uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  49. }
  50. func (binaryLittleEndian) PutUint64(b []byte, v uint64) {
  51. _ = b[7] // early bounds check to guarantee safety of writes below
  52. b[0] = byte(v)
  53. b[1] = byte(v >> 8)
  54. b[2] = byte(v >> 16)
  55. b[3] = byte(v >> 24)
  56. b[4] = byte(v >> 32)
  57. b[5] = byte(v >> 40)
  58. b[6] = byte(v >> 48)
  59. b[7] = byte(v >> 56)
  60. }
  61. type binaryBigEndian struct{}
  62. func (binaryBigEndian) Uint16(b []byte) uint16 {
  63. _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
  64. return uint16(b[1]) | uint16(b[0])<<8
  65. }
  66. func (binaryBigEndian) PutUint16(b []byte, v uint16) {
  67. _ = b[1] // early bounds check to guarantee safety of writes below
  68. b[0] = byte(v >> 8)
  69. b[1] = byte(v)
  70. }
  71. func (binaryBigEndian) Uint32(b []byte) uint32 {
  72. _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
  73. return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
  74. }
  75. func (binaryBigEndian) PutUint32(b []byte, v uint32) {
  76. _ = b[3] // early bounds check to guarantee safety of writes below
  77. b[0] = byte(v >> 24)
  78. b[1] = byte(v >> 16)
  79. b[2] = byte(v >> 8)
  80. b[3] = byte(v)
  81. }
  82. func (binaryBigEndian) Uint64(b []byte) uint64 {
  83. _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
  84. return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
  85. uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
  86. }
  87. func (binaryBigEndian) PutUint64(b []byte, v uint64) {
  88. _ = b[7] // early bounds check to guarantee safety of writes below
  89. b[0] = byte(v >> 56)
  90. b[1] = byte(v >> 48)
  91. b[2] = byte(v >> 40)
  92. b[3] = byte(v >> 32)
  93. b[4] = byte(v >> 24)
  94. b[5] = byte(v >> 16)
  95. b[6] = byte(v >> 8)
  96. b[7] = byte(v)
  97. }