github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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 //go:build solaris 6 // +build solaris 7 8 package lif 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 Uint64([]byte) uint64 25 PutUint16([]byte, uint16) 26 PutUint32([]byte, uint32) 27 PutUint64([]byte, uint64) 28 } 29 30 type binaryLittleEndian struct{} 31 32 func (binaryLittleEndian) Uint16(b []byte) uint16 { 33 _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 34 return uint16(b[0]) | uint16(b[1])<<8 35 } 36 37 func (binaryLittleEndian) PutUint16(b []byte, v uint16) { 38 _ = b[1] // early bounds check to guarantee safety of writes below 39 b[0] = byte(v) 40 b[1] = byte(v >> 8) 41 } 42 43 func (binaryLittleEndian) Uint32(b []byte) uint32 { 44 _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 45 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 46 } 47 48 func (binaryLittleEndian) PutUint32(b []byte, v uint32) { 49 _ = b[3] // early bounds check to guarantee safety of writes below 50 b[0] = byte(v) 51 b[1] = byte(v >> 8) 52 b[2] = byte(v >> 16) 53 b[3] = byte(v >> 24) 54 } 55 56 func (binaryLittleEndian) Uint64(b []byte) uint64 { 57 _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 58 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | 59 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 60 } 61 62 func (binaryLittleEndian) PutUint64(b []byte, v uint64) { 63 _ = b[7] // early bounds check to guarantee safety of writes below 64 b[0] = byte(v) 65 b[1] = byte(v >> 8) 66 b[2] = byte(v >> 16) 67 b[3] = byte(v >> 24) 68 b[4] = byte(v >> 32) 69 b[5] = byte(v >> 40) 70 b[6] = byte(v >> 48) 71 b[7] = byte(v >> 56) 72 } 73 74 type binaryBigEndian struct{} 75 76 func (binaryBigEndian) Uint16(b []byte) uint16 { 77 _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 78 return uint16(b[1]) | uint16(b[0])<<8 79 } 80 81 func (binaryBigEndian) PutUint16(b []byte, v uint16) { 82 _ = b[1] // early bounds check to guarantee safety of writes below 83 b[0] = byte(v >> 8) 84 b[1] = byte(v) 85 } 86 87 func (binaryBigEndian) Uint32(b []byte) uint32 { 88 _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 89 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 90 } 91 92 func (binaryBigEndian) PutUint32(b []byte, v uint32) { 93 _ = b[3] // early bounds check to guarantee safety of writes below 94 b[0] = byte(v >> 24) 95 b[1] = byte(v >> 16) 96 b[2] = byte(v >> 8) 97 b[3] = byte(v) 98 } 99 100 func (binaryBigEndian) Uint64(b []byte) uint64 { 101 _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 102 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | 103 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 104 } 105 106 func (binaryBigEndian) PutUint64(b []byte, v uint64) { 107 _ = b[7] // early bounds check to guarantee safety of writes below 108 b[0] = byte(v >> 56) 109 b[1] = byte(v >> 48) 110 b[2] = byte(v >> 40) 111 b[3] = byte(v >> 32) 112 b[4] = byte(v >> 24) 113 b[5] = byte(v >> 16) 114 b[6] = byte(v >> 8) 115 b[7] = byte(v) 116 }