Source file src/vendor/golang.org/x/net/route/binary.go

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

View as plain text