1package flatbuffers 2 3import ( 4 "math" 5) 6 7type ( 8 // A SOffsetT stores a signed offset into arbitrary data. 9 SOffsetT int32 10 // A UOffsetT stores an unsigned offset into vector data. 11 UOffsetT uint32 12 // A VOffsetT stores an unsigned offset in a vtable. 13 VOffsetT uint16 14) 15 16const ( 17 // VtableMetadataFields is the count of metadata fields in each vtable. 18 VtableMetadataFields = 2 19) 20 21// GetByte decodes a little-endian byte from a byte slice. 22func GetByte(buf []byte) byte { 23 return byte(GetUint8(buf)) 24} 25 26// GetBool decodes a little-endian bool from a byte slice. 27func GetBool(buf []byte) bool { 28 return buf[0] == 1 29} 30 31// GetUint8 decodes a little-endian uint8 from a byte slice. 32func GetUint8(buf []byte) (n uint8) { 33 n = uint8(buf[0]) 34 return 35} 36 37// GetUint16 decodes a little-endian uint16 from a byte slice. 38func GetUint16(buf []byte) (n uint16) { 39 _ = buf[1] // Force one bounds check. See: golang.org/issue/14808 40 n |= uint16(buf[0]) 41 n |= uint16(buf[1]) << 8 42 return 43} 44 45// GetUint32 decodes a little-endian uint32 from a byte slice. 46func GetUint32(buf []byte) (n uint32) { 47 _ = buf[3] // Force one bounds check. See: golang.org/issue/14808 48 n |= uint32(buf[0]) 49 n |= uint32(buf[1]) << 8 50 n |= uint32(buf[2]) << 16 51 n |= uint32(buf[3]) << 24 52 return 53} 54 55// GetUint64 decodes a little-endian uint64 from a byte slice. 56func GetUint64(buf []byte) (n uint64) { 57 _ = buf[7] // Force one bounds check. See: golang.org/issue/14808 58 n |= uint64(buf[0]) 59 n |= uint64(buf[1]) << 8 60 n |= uint64(buf[2]) << 16 61 n |= uint64(buf[3]) << 24 62 n |= uint64(buf[4]) << 32 63 n |= uint64(buf[5]) << 40 64 n |= uint64(buf[6]) << 48 65 n |= uint64(buf[7]) << 56 66 return 67} 68 69// GetInt8 decodes a little-endian int8 from a byte slice. 70func GetInt8(buf []byte) (n int8) { 71 n = int8(buf[0]) 72 return 73} 74 75// GetInt16 decodes a little-endian int16 from a byte slice. 76func GetInt16(buf []byte) (n int16) { 77 _ = buf[1] // Force one bounds check. See: golang.org/issue/14808 78 n |= int16(buf[0]) 79 n |= int16(buf[1]) << 8 80 return 81} 82 83// GetInt32 decodes a little-endian int32 from a byte slice. 84func GetInt32(buf []byte) (n int32) { 85 _ = buf[3] // Force one bounds check. See: golang.org/issue/14808 86 n |= int32(buf[0]) 87 n |= int32(buf[1]) << 8 88 n |= int32(buf[2]) << 16 89 n |= int32(buf[3]) << 24 90 return 91} 92 93// GetInt64 decodes a little-endian int64 from a byte slice. 94func GetInt64(buf []byte) (n int64) { 95 _ = buf[7] // Force one bounds check. See: golang.org/issue/14808 96 n |= int64(buf[0]) 97 n |= int64(buf[1]) << 8 98 n |= int64(buf[2]) << 16 99 n |= int64(buf[3]) << 24 100 n |= int64(buf[4]) << 32 101 n |= int64(buf[5]) << 40 102 n |= int64(buf[6]) << 48 103 n |= int64(buf[7]) << 56 104 return 105} 106 107// GetFloat32 decodes a little-endian float32 from a byte slice. 108func GetFloat32(buf []byte) float32 { 109 x := GetUint32(buf) 110 return math.Float32frombits(x) 111} 112 113// GetFloat64 decodes a little-endian float64 from a byte slice. 114func GetFloat64(buf []byte) float64 { 115 x := GetUint64(buf) 116 return math.Float64frombits(x) 117} 118 119// GetUOffsetT decodes a little-endian UOffsetT from a byte slice. 120func GetUOffsetT(buf []byte) UOffsetT { 121 return UOffsetT(GetUint32(buf)) 122} 123 124// GetSOffsetT decodes a little-endian SOffsetT from a byte slice. 125func GetSOffsetT(buf []byte) SOffsetT { 126 return SOffsetT(GetInt32(buf)) 127} 128 129// GetVOffsetT decodes a little-endian VOffsetT from a byte slice. 130func GetVOffsetT(buf []byte) VOffsetT { 131 return VOffsetT(GetUint16(buf)) 132} 133 134// WriteByte encodes a little-endian uint8 into a byte slice. 135func WriteByte(buf []byte, n byte) { 136 WriteUint8(buf, uint8(n)) 137} 138 139// WriteBool encodes a little-endian bool into a byte slice. 140func WriteBool(buf []byte, b bool) { 141 buf[0] = 0 142 if b { 143 buf[0] = 1 144 } 145} 146 147// WriteUint8 encodes a little-endian uint8 into a byte slice. 148func WriteUint8(buf []byte, n uint8) { 149 buf[0] = byte(n) 150} 151 152// WriteUint16 encodes a little-endian uint16 into a byte slice. 153func WriteUint16(buf []byte, n uint16) { 154 _ = buf[1] // Force one bounds check. See: golang.org/issue/14808 155 buf[0] = byte(n) 156 buf[1] = byte(n >> 8) 157} 158 159// WriteUint32 encodes a little-endian uint32 into a byte slice. 160func WriteUint32(buf []byte, n uint32) { 161 _ = buf[3] // Force one bounds check. See: golang.org/issue/14808 162 buf[0] = byte(n) 163 buf[1] = byte(n >> 8) 164 buf[2] = byte(n >> 16) 165 buf[3] = byte(n >> 24) 166} 167 168// WriteUint64 encodes a little-endian uint64 into a byte slice. 169func WriteUint64(buf []byte, n uint64) { 170 _ = buf[7] // Force one bounds check. See: golang.org/issue/14808 171 buf[0] = byte(n) 172 buf[1] = byte(n >> 8) 173 buf[2] = byte(n >> 16) 174 buf[3] = byte(n >> 24) 175 buf[4] = byte(n >> 32) 176 buf[5] = byte(n >> 40) 177 buf[6] = byte(n >> 48) 178 buf[7] = byte(n >> 56) 179} 180 181// WriteInt8 encodes a little-endian int8 into a byte slice. 182func WriteInt8(buf []byte, n int8) { 183 buf[0] = byte(n) 184} 185 186// WriteInt16 encodes a little-endian int16 into a byte slice. 187func WriteInt16(buf []byte, n int16) { 188 _ = buf[1] // Force one bounds check. See: golang.org/issue/14808 189 buf[0] = byte(n) 190 buf[1] = byte(n >> 8) 191} 192 193// WriteInt32 encodes a little-endian int32 into a byte slice. 194func WriteInt32(buf []byte, n int32) { 195 _ = buf[3] // Force one bounds check. See: golang.org/issue/14808 196 buf[0] = byte(n) 197 buf[1] = byte(n >> 8) 198 buf[2] = byte(n >> 16) 199 buf[3] = byte(n >> 24) 200} 201 202// WriteInt64 encodes a little-endian int64 into a byte slice. 203func WriteInt64(buf []byte, n int64) { 204 _ = buf[7] // Force one bounds check. See: golang.org/issue/14808 205 buf[0] = byte(n) 206 buf[1] = byte(n >> 8) 207 buf[2] = byte(n >> 16) 208 buf[3] = byte(n >> 24) 209 buf[4] = byte(n >> 32) 210 buf[5] = byte(n >> 40) 211 buf[6] = byte(n >> 48) 212 buf[7] = byte(n >> 56) 213} 214 215// WriteFloat32 encodes a little-endian float32 into a byte slice. 216func WriteFloat32(buf []byte, n float32) { 217 WriteUint32(buf, math.Float32bits(n)) 218} 219 220// WriteFloat64 encodes a little-endian float64 into a byte slice. 221func WriteFloat64(buf []byte, n float64) { 222 WriteUint64(buf, math.Float64bits(n)) 223} 224 225// WriteVOffsetT encodes a little-endian VOffsetT into a byte slice. 226func WriteVOffsetT(buf []byte, n VOffsetT) { 227 WriteUint16(buf, uint16(n)) 228} 229 230// WriteSOffsetT encodes a little-endian SOffsetT into a byte slice. 231func WriteSOffsetT(buf []byte, n SOffsetT) { 232 WriteInt32(buf, int32(n)) 233} 234 235// WriteUOffsetT encodes a little-endian UOffsetT into a byte slice. 236func WriteUOffsetT(buf []byte, n UOffsetT) { 237 WriteUint32(buf, uint32(n)) 238} 239