github.com/icodeface/tls@v0.0.0-20230910023335-34df9250cd12/internal/x/net/lif/binary.go (about) 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 solaris 6 7 package lif 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 the 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 Uint64([]byte) uint64 24 PutUint16([]byte, uint16) 25 PutUint32([]byte, uint32) 26 PutUint64([]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 func (binaryLittleEndian) PutUint64(b []byte, v uint64) { 62 _ = b[7] // early bounds check to guarantee safety of writes below 63 b[0] = byte(v) 64 b[1] = byte(v >> 8) 65 b[2] = byte(v >> 16) 66 b[3] = byte(v >> 24) 67 b[4] = byte(v >> 32) 68 b[5] = byte(v >> 40) 69 b[6] = byte(v >> 48) 70 b[7] = byte(v >> 56) 71 } 72 73 type binaryBigEndian struct{} 74 75 func (binaryBigEndian) Uint16(b []byte) uint16 { 76 _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 77 return uint16(b[1]) | uint16(b[0])<<8 78 } 79 80 func (binaryBigEndian) PutUint16(b []byte, v uint16) { 81 _ = b[1] // early bounds check to guarantee safety of writes below 82 b[0] = byte(v >> 8) 83 b[1] = byte(v) 84 } 85 86 func (binaryBigEndian) Uint32(b []byte) uint32 { 87 _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 88 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 89 } 90 91 func (binaryBigEndian) PutUint32(b []byte, v uint32) { 92 _ = b[3] // early bounds check to guarantee safety of writes below 93 b[0] = byte(v >> 24) 94 b[1] = byte(v >> 16) 95 b[2] = byte(v >> 8) 96 b[3] = byte(v) 97 } 98 99 func (binaryBigEndian) Uint64(b []byte) uint64 { 100 _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 101 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | 102 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 103 } 104 105 func (binaryBigEndian) PutUint64(b []byte, v uint64) { 106 _ = b[7] // early bounds check to guarantee safety of writes below 107 b[0] = byte(v >> 56) 108 b[1] = byte(v >> 48) 109 b[2] = byte(v >> 40) 110 b[3] = byte(v >> 32) 111 b[4] = byte(v >> 24) 112 b[5] = byte(v >> 16) 113 b[6] = byte(v >> 8) 114 b[7] = byte(v) 115 }