You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7 KiB

  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 darwin dragonfly freebsd netbsd openbsd
  5. package route
  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. PutUint16([]byte, uint16)
  19. PutUint32([]byte, uint32)
  20. Uint64([]byte) uint64
  21. }
  22. type binaryLittleEndian struct{}
  23. func (binaryLittleEndian) Uint16(b []byte) uint16 {
  24. _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
  25. return uint16(b[0]) | uint16(b[1])<<8
  26. }
  27. func (binaryLittleEndian) PutUint16(b []byte, v uint16) {
  28. _ = b[1] // early bounds check to guarantee safety of writes below
  29. b[0] = byte(v)
  30. b[1] = byte(v >> 8)
  31. }
  32. func (binaryLittleEndian) Uint32(b []byte) uint32 {
  33. _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
  34. return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  35. }
  36. func (binaryLittleEndian) PutUint32(b []byte, v uint32) {
  37. _ = b[3] // early bounds check to guarantee safety of writes below
  38. b[0] = byte(v)
  39. b[1] = byte(v >> 8)
  40. b[2] = byte(v >> 16)
  41. b[3] = byte(v >> 24)
  42. }
  43. func (binaryLittleEndian) Uint64(b []byte) uint64 {
  44. _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
  45. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
  46. uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  47. }
  48. type binaryBigEndian struct{}
  49. func (binaryBigEndian) Uint16(b []byte) uint16 {
  50. _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
  51. return uint16(b[1]) | uint16(b[0])<<8
  52. }
  53. func (binaryBigEndian) PutUint16(b []byte, v uint16) {
  54. _ = b[1] // early bounds check to guarantee safety of writes below
  55. b[0] = byte(v >> 8)
  56. b[1] = byte(v)
  57. }
  58. func (binaryBigEndian) Uint32(b []byte) uint32 {
  59. _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
  60. return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
  61. }
  62. func (binaryBigEndian) PutUint32(b []byte, v uint32) {
  63. _ = b[3] // early bounds check to guarantee safety of writes below
  64. b[0] = byte(v >> 24)
  65. b[1] = byte(v >> 16)
  66. b[2] = byte(v >> 8)
  67. b[3] = byte(v)
  68. }
  69. func (binaryBigEndian) Uint64(b []byte) uint64 {
  70. _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
  71. return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
  72. uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
  73. }