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