1package nlenc 2 3import ( 4 "fmt" 5 "unsafe" 6) 7 8// PutUint8 encodes a uint8 into b. 9// If b is not exactly 1 byte in length, PutUint8 will panic. 10func PutUint8(b []byte, v uint8) { 11 if l := len(b); l != 1 { 12 panic(fmt.Sprintf("PutUint8: unexpected byte slice length: %d", l)) 13 } 14 15 b[0] = v 16} 17 18// PutUint16 encodes a uint16 into b using the host machine's native endianness. 19// If b is not exactly 2 bytes in length, PutUint16 will panic. 20func PutUint16(b []byte, v uint16) { 21 if l := len(b); l != 2 { 22 panic(fmt.Sprintf("PutUint16: unexpected byte slice length: %d", l)) 23 } 24 25 *(*uint16)(unsafe.Pointer(&b[0])) = v 26} 27 28// PutUint32 encodes a uint32 into b using the host machine's native endianness. 29// If b is not exactly 4 bytes in length, PutUint32 will panic. 30func PutUint32(b []byte, v uint32) { 31 if l := len(b); l != 4 { 32 panic(fmt.Sprintf("PutUint32: unexpected byte slice length: %d", l)) 33 } 34 35 *(*uint32)(unsafe.Pointer(&b[0])) = v 36} 37 38// PutUint64 encodes a uint64 into b using the host machine's native endianness. 39// If b is not exactly 8 bytes in length, PutUint64 will panic. 40func PutUint64(b []byte, v uint64) { 41 if l := len(b); l != 8 { 42 panic(fmt.Sprintf("PutUint64: unexpected byte slice length: %d", l)) 43 } 44 45 *(*uint64)(unsafe.Pointer(&b[0])) = v 46} 47 48// PutInt32 encodes a int32 into b using the host machine's native endianness. 49// If b is not exactly 4 bytes in length, PutInt32 will panic. 50func PutInt32(b []byte, v int32) { 51 if l := len(b); l != 4 { 52 panic(fmt.Sprintf("PutInt32: unexpected byte slice length: %d", l)) 53 } 54 55 *(*int32)(unsafe.Pointer(&b[0])) = v 56} 57 58// Uint8 decodes a uint8 from b. 59// If b is not exactly 1 byte in length, Uint8 will panic. 60func Uint8(b []byte) uint8 { 61 if l := len(b); l != 1 { 62 panic(fmt.Sprintf("Uint8: unexpected byte slice length: %d", l)) 63 } 64 65 return b[0] 66} 67 68// Uint16 decodes a uint16 from b using the host machine's native endianness. 69// If b is not exactly 2 bytes in length, Uint16 will panic. 70func Uint16(b []byte) uint16 { 71 if l := len(b); l != 2 { 72 panic(fmt.Sprintf("Uint16: unexpected byte slice length: %d", l)) 73 } 74 75 return *(*uint16)(unsafe.Pointer(&b[0])) 76} 77 78// Uint32 decodes a uint32 from b using the host machine's native endianness. 79// If b is not exactly 4 bytes in length, Uint32 will panic. 80func Uint32(b []byte) uint32 { 81 if l := len(b); l != 4 { 82 panic(fmt.Sprintf("Uint32: unexpected byte slice length: %d", l)) 83 } 84 85 return *(*uint32)(unsafe.Pointer(&b[0])) 86} 87 88// Uint64 decodes a uint64 from b using the host machine's native endianness. 89// If b is not exactly 8 bytes in length, Uint64 will panic. 90func Uint64(b []byte) uint64 { 91 if l := len(b); l != 8 { 92 panic(fmt.Sprintf("Uint64: unexpected byte slice length: %d", l)) 93 } 94 95 return *(*uint64)(unsafe.Pointer(&b[0])) 96} 97 98// Int32 decodes an int32 from b using the host machine's native endianness. 99// If b is not exactly 4 bytes in length, Int32 will panic. 100func Int32(b []byte) int32 { 101 if l := len(b); l != 4 { 102 panic(fmt.Sprintf("Int32: unexpected byte slice length: %d", l)) 103 } 104 105 return *(*int32)(unsafe.Pointer(&b[0])) 106} 107 108// Uint8Bytes encodes a uint8 into a newly-allocated byte slice. It is a 109// shortcut for allocating a new byte slice and filling it using PutUint8. 110func Uint8Bytes(v uint8) []byte { 111 b := make([]byte, 1) 112 PutUint8(b, v) 113 return b 114} 115 116// Uint16Bytes encodes a uint16 into a newly-allocated byte slice using the 117// host machine's native endianness. It is a shortcut for allocating a new 118// byte slice and filling it using PutUint16. 119func Uint16Bytes(v uint16) []byte { 120 b := make([]byte, 2) 121 PutUint16(b, v) 122 return b 123} 124 125// Uint32Bytes encodes a uint32 into a newly-allocated byte slice using the 126// host machine's native endianness. It is a shortcut for allocating a new 127// byte slice and filling it using PutUint32. 128func Uint32Bytes(v uint32) []byte { 129 b := make([]byte, 4) 130 PutUint32(b, v) 131 return b 132} 133 134// Uint64Bytes encodes a uint64 into a newly-allocated byte slice using the 135// host machine's native endianness. It is a shortcut for allocating a new 136// byte slice and filling it using PutUint64. 137func Uint64Bytes(v uint64) []byte { 138 b := make([]byte, 8) 139 PutUint64(b, v) 140 return b 141} 142 143// Int32Bytes encodes a int32 into a newly-allocated byte slice using the 144// host machine's native endianness. It is a shortcut for allocating a new 145// byte slice and filling it using PutInt32. 146func Int32Bytes(v int32) []byte { 147 b := make([]byte, 4) 148 PutInt32(b, v) 149 return b 150} 151