binary.go 3.3 KB

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