encode.go 6.2 KB

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