gbinary_be.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gbinary
  7. import (
  8. "bytes"
  9. "context"
  10. "encoding/binary"
  11. "fmt"
  12. "math"
  13. "github.com/gogf/gf/v2/errors/gerror"
  14. "github.com/gogf/gf/v2/internal/intlog"
  15. )
  16. // BeEncode encodes one or multiple `values` into bytes using BigEndian.
  17. // It uses type asserting checking the type of each value of `values` and internally
  18. // calls corresponding converting function do the bytes converting.
  19. //
  20. // It supports common variable type asserting, and finally it uses fmt.Sprintf converting
  21. // value to string and then to bytes.
  22. func BeEncode(values ...interface{}) []byte {
  23. buf := new(bytes.Buffer)
  24. for i := 0; i < len(values); i++ {
  25. if values[i] == nil {
  26. return buf.Bytes()
  27. }
  28. switch value := values[i].(type) {
  29. case int:
  30. buf.Write(BeEncodeInt(value))
  31. case int8:
  32. buf.Write(BeEncodeInt8(value))
  33. case int16:
  34. buf.Write(BeEncodeInt16(value))
  35. case int32:
  36. buf.Write(BeEncodeInt32(value))
  37. case int64:
  38. buf.Write(BeEncodeInt64(value))
  39. case uint:
  40. buf.Write(BeEncodeUint(value))
  41. case uint8:
  42. buf.Write(BeEncodeUint8(value))
  43. case uint16:
  44. buf.Write(BeEncodeUint16(value))
  45. case uint32:
  46. buf.Write(BeEncodeUint32(value))
  47. case uint64:
  48. buf.Write(BeEncodeUint64(value))
  49. case bool:
  50. buf.Write(BeEncodeBool(value))
  51. case string:
  52. buf.Write(BeEncodeString(value))
  53. case []byte:
  54. buf.Write(value)
  55. case float32:
  56. buf.Write(BeEncodeFloat32(value))
  57. case float64:
  58. buf.Write(BeEncodeFloat64(value))
  59. default:
  60. if err := binary.Write(buf, binary.BigEndian, value); err != nil {
  61. intlog.Errorf(context.TODO(), `%+v`, err)
  62. buf.Write(BeEncodeString(fmt.Sprintf("%v", value)))
  63. }
  64. }
  65. }
  66. return buf.Bytes()
  67. }
  68. func BeEncodeByLength(length int, values ...interface{}) []byte {
  69. b := BeEncode(values...)
  70. if len(b) < length {
  71. b = append(b, make([]byte, length-len(b))...)
  72. } else if len(b) > length {
  73. b = b[0:length]
  74. }
  75. return b
  76. }
  77. func BeDecode(b []byte, values ...interface{}) error {
  78. var (
  79. err error
  80. buf = bytes.NewBuffer(b)
  81. )
  82. for i := 0; i < len(values); i++ {
  83. if err = binary.Read(buf, binary.BigEndian, values[i]); err != nil {
  84. err = gerror.Wrap(err, `binary.Read failed`)
  85. return err
  86. }
  87. }
  88. return nil
  89. }
  90. func BeEncodeString(s string) []byte {
  91. return []byte(s)
  92. }
  93. func BeDecodeToString(b []byte) string {
  94. return string(b)
  95. }
  96. func BeEncodeBool(b bool) []byte {
  97. if b {
  98. return []byte{1}
  99. } else {
  100. return []byte{0}
  101. }
  102. }
  103. func BeEncodeInt(i int) []byte {
  104. if i <= math.MaxInt8 {
  105. return BeEncodeInt8(int8(i))
  106. } else if i <= math.MaxInt16 {
  107. return BeEncodeInt16(int16(i))
  108. } else if i <= math.MaxInt32 {
  109. return BeEncodeInt32(int32(i))
  110. } else {
  111. return BeEncodeInt64(int64(i))
  112. }
  113. }
  114. func BeEncodeUint(i uint) []byte {
  115. if i <= math.MaxUint8 {
  116. return BeEncodeUint8(uint8(i))
  117. } else if i <= math.MaxUint16 {
  118. return BeEncodeUint16(uint16(i))
  119. } else if i <= math.MaxUint32 {
  120. return BeEncodeUint32(uint32(i))
  121. } else {
  122. return BeEncodeUint64(uint64(i))
  123. }
  124. }
  125. func BeEncodeInt8(i int8) []byte {
  126. return []byte{byte(i)}
  127. }
  128. func BeEncodeUint8(i uint8) []byte {
  129. return []byte{i}
  130. }
  131. func BeEncodeInt16(i int16) []byte {
  132. b := make([]byte, 2)
  133. binary.BigEndian.PutUint16(b, uint16(i))
  134. return b
  135. }
  136. func BeEncodeUint16(i uint16) []byte {
  137. b := make([]byte, 2)
  138. binary.BigEndian.PutUint16(b, i)
  139. return b
  140. }
  141. func BeEncodeInt32(i int32) []byte {
  142. b := make([]byte, 4)
  143. binary.BigEndian.PutUint32(b, uint32(i))
  144. return b
  145. }
  146. func BeEncodeUint32(i uint32) []byte {
  147. b := make([]byte, 4)
  148. binary.BigEndian.PutUint32(b, i)
  149. return b
  150. }
  151. func BeEncodeInt64(i int64) []byte {
  152. b := make([]byte, 8)
  153. binary.BigEndian.PutUint64(b, uint64(i))
  154. return b
  155. }
  156. func BeEncodeUint64(i uint64) []byte {
  157. b := make([]byte, 8)
  158. binary.BigEndian.PutUint64(b, i)
  159. return b
  160. }
  161. func BeEncodeFloat32(f float32) []byte {
  162. bits := math.Float32bits(f)
  163. b := make([]byte, 4)
  164. binary.BigEndian.PutUint32(b, bits)
  165. return b
  166. }
  167. func BeEncodeFloat64(f float64) []byte {
  168. bits := math.Float64bits(f)
  169. b := make([]byte, 8)
  170. binary.BigEndian.PutUint64(b, bits)
  171. return b
  172. }
  173. func BeDecodeToInt(b []byte) int {
  174. if len(b) < 2 {
  175. return int(BeDecodeToUint8(b))
  176. } else if len(b) < 3 {
  177. return int(BeDecodeToUint16(b))
  178. } else if len(b) < 5 {
  179. return int(BeDecodeToUint32(b))
  180. } else {
  181. return int(BeDecodeToUint64(b))
  182. }
  183. }
  184. func BeDecodeToUint(b []byte) uint {
  185. if len(b) < 2 {
  186. return uint(BeDecodeToUint8(b))
  187. } else if len(b) < 3 {
  188. return uint(BeDecodeToUint16(b))
  189. } else if len(b) < 5 {
  190. return uint(BeDecodeToUint32(b))
  191. } else {
  192. return uint(BeDecodeToUint64(b))
  193. }
  194. }
  195. func BeDecodeToBool(b []byte) bool {
  196. if len(b) == 0 {
  197. return false
  198. }
  199. if bytes.Equal(b, make([]byte, len(b))) {
  200. return false
  201. }
  202. return true
  203. }
  204. func BeDecodeToInt8(b []byte) int8 {
  205. if len(b) == 0 {
  206. panic(`empty slice given`)
  207. }
  208. return int8(b[0])
  209. }
  210. func BeDecodeToUint8(b []byte) uint8 {
  211. if len(b) == 0 {
  212. panic(`empty slice given`)
  213. }
  214. return b[0]
  215. }
  216. func BeDecodeToInt16(b []byte) int16 {
  217. return int16(binary.BigEndian.Uint16(BeFillUpSize(b, 2)))
  218. }
  219. func BeDecodeToUint16(b []byte) uint16 {
  220. return binary.BigEndian.Uint16(BeFillUpSize(b, 2))
  221. }
  222. func BeDecodeToInt32(b []byte) int32 {
  223. return int32(binary.BigEndian.Uint32(BeFillUpSize(b, 4)))
  224. }
  225. func BeDecodeToUint32(b []byte) uint32 {
  226. return binary.BigEndian.Uint32(BeFillUpSize(b, 4))
  227. }
  228. func BeDecodeToInt64(b []byte) int64 {
  229. return int64(binary.BigEndian.Uint64(BeFillUpSize(b, 8)))
  230. }
  231. func BeDecodeToUint64(b []byte) uint64 {
  232. return binary.BigEndian.Uint64(BeFillUpSize(b, 8))
  233. }
  234. func BeDecodeToFloat32(b []byte) float32 {
  235. return math.Float32frombits(binary.BigEndian.Uint32(BeFillUpSize(b, 4)))
  236. }
  237. func BeDecodeToFloat64(b []byte) float64 {
  238. return math.Float64frombits(binary.BigEndian.Uint64(BeFillUpSize(b, 8)))
  239. }
  240. // BeFillUpSize fills up the bytes `b` to given length `l` using big BigEndian.
  241. //
  242. // Note that it creates a new bytes slice by copying the original one to avoid changing
  243. // the original parameter bytes.
  244. func BeFillUpSize(b []byte, l int) []byte {
  245. if len(b) >= l {
  246. return b[:l]
  247. }
  248. c := make([]byte, l)
  249. copy(c[l-len(b):], b)
  250. return c
  251. }