big_little_endian.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package util
  2. //
  3. // 注意:RTMP模式下都是大端模式
  4. //
  5. var LittleEndian littleEndian
  6. // BigEndian is the big-endian implementation of ByteOrder.
  7. var BigEndian bigEndian
  8. // 低位字节排放在内存的低地址端,高位字节排放在内存的高地址端.
  9. type littleEndian struct{}
  10. // b == 0x1234, b[0] == 0x12, b[1] == 0x34
  11. // b[0]低字节 b[1]高字节
  12. // 内存地址 低 -> 高
  13. // 0x34 0x12
  14. // byte(v)低字节 b[0]内存低地址
  15. // byte(v>>8)高字节 b[1]内存高地址
  16. // b == 2222 2222 1111 1111
  17. // b >> 8 -> 0000 0000 2222 2222
  18. // b << 8 -> 1111 1111 0000 0000
  19. func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 }
  20. func (littleEndian) Uint24(b []byte) uint32 { return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 }
  21. func (littleEndian) Uint32(b []byte) uint32 {
  22. return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  23. }
  24. func (littleEndian) Uint40(b []byte) uint64 {
  25. return uint64(b[0]) | uint64(b[1])<<8 |
  26. uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32
  27. }
  28. func (littleEndian) Uint48(b []byte) uint64 {
  29. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 |
  30. uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40
  31. }
  32. func (littleEndian) Uint64(b []byte) uint64 {
  33. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
  34. uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  35. }
  36. //
  37. // Put
  38. //
  39. func (littleEndian) PutUint16(b []byte, v uint16) {
  40. b[0] = byte(v)
  41. b[1] = byte(v >> 8)
  42. }
  43. func (littleEndian) PutUint24(b []byte, v uint32) {
  44. b[0] = byte(v)
  45. b[1] = byte(v >> 8)
  46. b[2] = byte(v >> 16)
  47. }
  48. func (littleEndian) PutUint32(b []byte, v uint32) {
  49. b[0] = byte(v)
  50. b[1] = byte(v >> 8)
  51. b[2] = byte(v >> 16)
  52. b[3] = byte(v >> 24)
  53. }
  54. func (littleEndian) PutUint64(b []byte, v uint64) {
  55. b[0] = byte(v)
  56. b[1] = byte(v >> 8)
  57. b[2] = byte(v >> 16)
  58. b[3] = byte(v >> 24)
  59. b[4] = byte(v >> 32)
  60. b[5] = byte(v >> 40)
  61. b[6] = byte(v >> 48)
  62. b[7] = byte(v >> 56)
  63. }
  64. //
  65. // To
  66. //
  67. func (littleEndian) ToUint16(v uint16) []byte {
  68. b := make([]byte, 2)
  69. b[0] = byte(v)
  70. b[1] = byte(v >> 8)
  71. return b
  72. }
  73. func (littleEndian) ToUint24(v uint32) []byte {
  74. b := make([]byte, 3)
  75. b[0] = byte(v)
  76. b[1] = byte(v >> 8)
  77. b[2] = byte(v >> 16)
  78. return b
  79. }
  80. func (littleEndian) ToUint32(v uint32) []byte {
  81. b := make([]byte, 4)
  82. b[0] = byte(v)
  83. b[1] = byte(v >> 8)
  84. b[2] = byte(v >> 16)
  85. b[3] = byte(v >> 24)
  86. return b
  87. }
  88. func (littleEndian) ToUint40(v uint64) []byte {
  89. b := make([]byte, 5)
  90. b[0] = byte(v)
  91. b[1] = byte(v >> 8)
  92. b[2] = byte(v >> 16)
  93. b[3] = byte(v >> 24)
  94. b[4] = byte(v >> 32)
  95. return b
  96. }
  97. func (littleEndian) ToUint48(v uint64) []byte {
  98. b := make([]byte, 6)
  99. b[0] = byte(v)
  100. b[1] = byte(v >> 8)
  101. b[2] = byte(v >> 16)
  102. b[3] = byte(v >> 24)
  103. b[4] = byte(v >> 32)
  104. b[5] = byte(v >> 40)
  105. return b
  106. }
  107. func (littleEndian) ToUint64(v uint64) []byte {
  108. b := make([]byte, 8)
  109. b[0] = byte(v)
  110. b[1] = byte(v >> 8)
  111. b[2] = byte(v >> 16)
  112. b[3] = byte(v >> 24)
  113. b[4] = byte(v >> 32)
  114. b[5] = byte(v >> 40)
  115. b[6] = byte(v >> 48)
  116. b[7] = byte(v >> 56)
  117. return b
  118. }
  119. // 高位字节排放在内存的低地址端,低位字节排放在内存的高地址端
  120. type bigEndian struct{}
  121. // b == 0x1234, b[0] == 0x12, b[1] == 0x34
  122. // 内存地址 低 -> 高
  123. // 0x12 0x34
  124. func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 }
  125. func (bigEndian) Uint24(b []byte) uint32 { return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16 }
  126. func (bigEndian) Uint32(b []byte) uint32 {
  127. return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
  128. }
  129. func (bigEndian) Uint40(b []byte) uint64 {
  130. return uint64(b[4]) | uint64(b[3])<<8 |
  131. uint64(b[2])<<16 | uint64(b[1])<<24 | uint64(b[0])<<32
  132. }
  133. func (bigEndian) Uint48(b []byte) uint64 {
  134. return uint64(b[5]) | uint64(b[4])<<8 | uint64(b[3])<<16 |
  135. uint64(b[2])<<24 | uint64(b[1])<<32 | uint64(b[0])<<40
  136. }
  137. func (bigEndian) Uint64(b []byte) uint64 {
  138. return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
  139. uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
  140. }
  141. //
  142. // Put
  143. //
  144. func (bigEndian) PutUint16(b []byte, v uint16) {
  145. b[0] = byte(v >> 8)
  146. b[1] = byte(v)
  147. }
  148. func (bigEndian) PutUint24(b []byte, v uint32) {
  149. b[0] = byte(v >> 16)
  150. b[1] = byte(v >> 8)
  151. b[2] = byte(v)
  152. }
  153. func (bigEndian) PutUint32(b []byte, v uint32) {
  154. b[0] = byte(v >> 24)
  155. b[1] = byte(v >> 16)
  156. b[2] = byte(v >> 8)
  157. b[3] = byte(v)
  158. }
  159. func (bigEndian) PutUint64(b []byte, v uint64) {
  160. b[0] = byte(v >> 56)
  161. b[1] = byte(v >> 48)
  162. b[2] = byte(v >> 40)
  163. b[3] = byte(v >> 32)
  164. b[4] = byte(v >> 24)
  165. b[5] = byte(v >> 16)
  166. b[6] = byte(v >> 8)
  167. b[7] = byte(v)
  168. }
  169. //
  170. // To
  171. //
  172. func (bigEndian) ToUint16(v uint16) []byte {
  173. b := make([]byte, 2)
  174. b[0] = byte(v >> 8)
  175. b[1] = byte(v)
  176. return b
  177. }
  178. func (bigEndian) ToUint24(v uint32) []byte {
  179. b := make([]byte, 3)
  180. b[0] = byte(v >> 16)
  181. b[1] = byte(v >> 8)
  182. b[2] = byte(v)
  183. return b
  184. }
  185. func (bigEndian) ToUint32(v uint32) []byte {
  186. b := make([]byte, 4)
  187. b[0] = byte(v >> 24)
  188. b[1] = byte(v >> 16)
  189. b[2] = byte(v >> 8)
  190. b[3] = byte(v)
  191. return b
  192. }
  193. func (bigEndian) ToUint40(v uint64) []byte {
  194. b := make([]byte, 5)
  195. b[0] = byte(v >> 32)
  196. b[1] = byte(v >> 24)
  197. b[2] = byte(v >> 16)
  198. b[3] = byte(v >> 8)
  199. b[4] = byte(v)
  200. return b
  201. }
  202. func (bigEndian) ToUint48(v uint64) []byte {
  203. b := make([]byte, 6)
  204. b[0] = byte(v >> 40)
  205. b[1] = byte(v >> 32)
  206. b[2] = byte(v >> 24)
  207. b[3] = byte(v >> 16)
  208. b[4] = byte(v >> 8)
  209. b[5] = byte(v)
  210. return b
  211. }
  212. func (bigEndian) ToUint64(v uint64) []byte {
  213. b := make([]byte, 8)
  214. b[0] = byte(v >> 56)
  215. b[1] = byte(v >> 48)
  216. b[2] = byte(v >> 40)
  217. b[3] = byte(v >> 32)
  218. b[4] = byte(v >> 24)
  219. b[5] = byte(v >> 16)
  220. b[6] = byte(v >> 8)
  221. b[7] = byte(v)
  222. return b
  223. }
  224. //哥伦布解码
  225. func GetUev(buff []byte, start int) (value int, pos int) {
  226. l := len(buff)
  227. var nZeroNum uint = 0
  228. for start < l*8 {
  229. if (buff[start/8] & (0x80 >> uint(start%8))) > 0 {
  230. break
  231. }
  232. nZeroNum += 1
  233. start += 1
  234. }
  235. dwRet := 0
  236. start += 1
  237. var i uint
  238. for i = 0; i < nZeroNum; i++ {
  239. dwRet <<= 1
  240. if (buff[start/8] & (0x80 >> uint(start%8))) > 0 {
  241. dwRet += 1
  242. }
  243. start += 1
  244. }
  245. return (1 << nZeroNum) - 1 + dwRet, start
  246. }
  247. func BigLittleSwap(v uint) uint {
  248. return (v >> 24) | ((v>>16)&0xff)<<8 | ((v>>8)&0xff)<<16 | (v&0xff)<<24
  249. }