binary.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 darwin || dragonfly || freebsd || netbsd || openbsd
  5. // +build darwin dragonfly freebsd netbsd openbsd
  6. package route
  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. PutUint16([]byte, uint16)
  20. PutUint32([]byte, uint32)
  21. Uint64([]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. type binaryBigEndian struct{}
  50. func (binaryBigEndian) Uint16(b []byte) uint16 {
  51. _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
  52. return uint16(b[1]) | uint16(b[0])<<8
  53. }
  54. func (binaryBigEndian) PutUint16(b []byte, v uint16) {
  55. _ = b[1] // early bounds check to guarantee safety of writes below
  56. b[0] = byte(v >> 8)
  57. b[1] = byte(v)
  58. }
  59. func (binaryBigEndian) Uint32(b []byte) uint32 {
  60. _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
  61. return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
  62. }
  63. func (binaryBigEndian) PutUint32(b []byte, v uint32) {
  64. _ = b[3] // early bounds check to guarantee safety of writes below
  65. b[0] = byte(v >> 24)
  66. b[1] = byte(v >> 16)
  67. b[2] = byte(v >> 8)
  68. b[3] = byte(v)
  69. }
  70. func (binaryBigEndian) Uint64(b []byte) uint64 {
  71. _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
  72. return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
  73. uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
  74. }