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 n |= uint16(buf[0]) 40 n |= uint16(buf[1]) << 8 41 return 42} 43 44// GetUint32 decodes a little-endian uint32 from a byte slice. 45func GetUint32(buf []byte) (n uint32) { 46 n |= uint32(buf[0]) 47 n |= uint32(buf[1]) << 8 48 n |= uint32(buf[2]) << 16 49 n |= uint32(buf[3]) << 24 50 return 51} 52 53// GetUint64 decodes a little-endian uint64 from a byte slice. 54func GetUint64(buf []byte) (n uint64) { 55 n |= uint64(buf[0]) 56 n |= uint64(buf[1]) << 8 57 n |= uint64(buf[2]) << 16 58 n |= uint64(buf[3]) << 24 59 n |= uint64(buf[4]) << 32 60 n |= uint64(buf[5]) << 40 61 n |= uint64(buf[6]) << 48 62 n |= uint64(buf[7]) << 56 63 return 64} 65 66// GetInt8 decodes a little-endian int8 from a byte slice. 67func GetInt8(buf []byte) (n int8) { 68 n = int8(buf[0]) 69 return 70} 71 72// GetInt16 decodes a little-endian int16 from a byte slice. 73func GetInt16(buf []byte) (n int16) { 74 n |= int16(buf[0]) 75 n |= int16(buf[1]) << 8 76 return 77} 78 79// GetInt32 decodes a little-endian int32 from a byte slice. 80func GetInt32(buf []byte) (n int32) { 81 n |= int32(buf[0]) 82 n |= int32(buf[1]) << 8 83 n |= int32(buf[2]) << 16 84 n |= int32(buf[3]) << 24 85 return 86} 87 88// GetInt64 decodes a little-endian int64 from a byte slice. 89func GetInt64(buf []byte) (n int64) { 90 n |= int64(buf[0]) 91 n |= int64(buf[1]) << 8 92 n |= int64(buf[2]) << 16 93 n |= int64(buf[3]) << 24 94 n |= int64(buf[4]) << 32 95 n |= int64(buf[5]) << 40 96 n |= int64(buf[6]) << 48 97 n |= int64(buf[7]) << 56 98 return 99} 100 101// GetFloat32 decodes a little-endian float32 from a byte slice. 102func GetFloat32(buf []byte) float32 { 103 x := GetUint32(buf) 104 return math.Float32frombits(x) 105} 106 107// GetFloat64 decodes a little-endian float64 from a byte slice. 108func GetFloat64(buf []byte) float64 { 109 x := GetUint64(buf) 110 return math.Float64frombits(x) 111} 112 113// GetUOffsetT decodes a little-endian UOffsetT from a byte slice. 114func GetUOffsetT(buf []byte) UOffsetT { 115 return UOffsetT(GetInt32(buf)) 116} 117 118// GetSOffsetT decodes a little-endian SOffsetT from a byte slice. 119func GetSOffsetT(buf []byte) SOffsetT { 120 return SOffsetT(GetInt32(buf)) 121} 122 123// GetVOffsetT decodes a little-endian VOffsetT from a byte slice. 124func GetVOffsetT(buf []byte) VOffsetT { 125 return VOffsetT(GetUint16(buf)) 126} 127 128// WriteByte encodes a little-endian uint8 into a byte slice. 129func WriteByte(buf []byte, n byte) { 130 WriteUint8(buf, uint8(n)) 131} 132 133// WriteBool encodes a little-endian bool into a byte slice. 134func WriteBool(buf []byte, b bool) { 135 buf[0] = 0 136 if b { 137 buf[0] = 1 138 } 139} 140 141// WriteUint8 encodes a little-endian uint8 into a byte slice. 142func WriteUint8(buf []byte, n uint8) { 143 buf[0] = byte(n) 144} 145 146// WriteUint16 encodes a little-endian uint16 into a byte slice. 147func WriteUint16(buf []byte, n uint16) { 148 buf[0] = byte(n) 149 buf[1] = byte(n >> 8) 150} 151 152// WriteUint32 encodes a little-endian uint32 into a byte slice. 153func WriteUint32(buf []byte, n uint32) { 154 buf[0] = byte(n) 155 buf[1] = byte(n >> 8) 156 buf[2] = byte(n >> 16) 157 buf[3] = byte(n >> 24) 158} 159 160// WriteUint64 encodes a little-endian uint64 into a byte slice. 161func WriteUint64(buf []byte, n uint64) { 162 for i := uint(0); i < uint(SizeUint64); i++ { 163 buf[i] = byte(n >> (i * 8)) 164 } 165} 166 167// WriteInt8 encodes a little-endian int8 into a byte slice. 168func WriteInt8(buf []byte, n int8) { 169 buf[0] = byte(n) 170} 171 172// WriteInt16 encodes a little-endian int16 into a byte slice. 173func WriteInt16(buf []byte, n int16) { 174 buf[0] = byte(n) 175 buf[1] = byte(n >> 8) 176} 177 178// WriteInt32 encodes a little-endian int32 into a byte slice. 179func WriteInt32(buf []byte, n int32) { 180 buf[0] = byte(n) 181 buf[1] = byte(n >> 8) 182 buf[2] = byte(n >> 16) 183 buf[3] = byte(n >> 24) 184} 185 186// WriteInt64 encodes a little-endian int64 into a byte slice. 187func WriteInt64(buf []byte, n int64) { 188 for i := uint(0); i < uint(SizeInt64); i++ { 189 buf[i] = byte(n >> (i * 8)) 190 } 191} 192 193// WriteFloat32 encodes a little-endian float32 into a byte slice. 194func WriteFloat32(buf []byte, n float32) { 195 WriteUint32(buf, math.Float32bits(n)) 196} 197 198// WriteFloat64 encodes a little-endian float64 into a byte slice. 199func WriteFloat64(buf []byte, n float64) { 200 WriteUint64(buf, math.Float64bits(n)) 201} 202 203// WriteVOffsetT encodes a little-endian VOffsetT into a byte slice. 204func WriteVOffsetT(buf []byte, n VOffsetT) { 205 WriteUint16(buf, uint16(n)) 206} 207 208// WriteSOffsetT encodes a little-endian SOffsetT into a byte slice. 209func WriteSOffsetT(buf []byte, n SOffsetT) { 210 WriteInt32(buf, int32(n)) 211} 212 213// WriteUOffsetT encodes a little-endian UOffsetT into a byte slice. 214func WriteUOffsetT(buf []byte, n UOffsetT) { 215 WriteUint32(buf, uint32(n)) 216} 217