bytes.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package ethutil
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "fmt"
  7. "math/big"
  8. "strings"
  9. )
  10. type Bytes []byte
  11. func (self Bytes) String() string {
  12. return string(self)
  13. }
  14. func DeleteFromByteSlice(s [][]byte, hash []byte) [][]byte {
  15. for i, h := range s {
  16. if bytes.Compare(h, hash) == 0 {
  17. return append(s[:i:i], s[i+1:]...)
  18. }
  19. }
  20. return s
  21. }
  22. // Number to bytes
  23. //
  24. // Returns the number in bytes with the specified base
  25. func NumberToBytes(num interface{}, bits int) []byte {
  26. buf := new(bytes.Buffer)
  27. err := binary.Write(buf, binary.BigEndian, num)
  28. if err != nil {
  29. fmt.Println("NumberToBytes failed:", err)
  30. }
  31. return buf.Bytes()[buf.Len()-(bits/8):]
  32. }
  33. // Bytes to number
  34. //
  35. // Attempts to cast a byte slice to a unsigned integer
  36. func BytesToNumber(b []byte) uint64 {
  37. var number uint64
  38. // Make sure the buffer is 64bits
  39. data := make([]byte, 8)
  40. data = append(data[:len(b)], b...)
  41. buf := bytes.NewReader(data)
  42. err := binary.Read(buf, binary.BigEndian, &number)
  43. if err != nil {
  44. fmt.Println("BytesToNumber failed:", err)
  45. }
  46. return number
  47. }
  48. // Read variable int
  49. //
  50. // Read a variable length number in big endian byte order
  51. func ReadVarInt(buff []byte) (ret uint64) {
  52. switch l := len(buff); {
  53. case l > 4:
  54. d := LeftPadBytes(buff, 8)
  55. binary.Read(bytes.NewReader(d), binary.BigEndian, &ret)
  56. case l > 2:
  57. var num uint32
  58. d := LeftPadBytes(buff, 4)
  59. binary.Read(bytes.NewReader(d), binary.BigEndian, &num)
  60. ret = uint64(num)
  61. case l > 1:
  62. var num uint16
  63. d := LeftPadBytes(buff, 2)
  64. binary.Read(bytes.NewReader(d), binary.BigEndian, &num)
  65. ret = uint64(num)
  66. default:
  67. var num uint8
  68. binary.Read(bytes.NewReader(buff), binary.BigEndian, &num)
  69. ret = uint64(num)
  70. }
  71. return
  72. }
  73. // Binary length
  74. //
  75. // Returns the true binary length of the given number
  76. func BinaryLength(num int) int {
  77. if num == 0 {
  78. return 0
  79. }
  80. return 1 + BinaryLength(num>>8)
  81. }
  82. // Copy bytes
  83. //
  84. // Returns an exact copy of the provided bytes
  85. func CopyBytes(b []byte) (copiedBytes []byte) {
  86. copiedBytes = make([]byte, len(b))
  87. copy(copiedBytes, b)
  88. return
  89. }
  90. func IsHex(str string) bool {
  91. l := len(str)
  92. return l >= 4 && l%2 == 0 && str[0:2] == "0x"
  93. }
  94. func Bytes2Hex(d []byte) string {
  95. return hex.EncodeToString(d)
  96. }
  97. func Hex2Bytes(str string) []byte {
  98. h, _ := hex.DecodeString(str)
  99. return h
  100. }
  101. func StringToByteFunc(str string, cb func(str string) []byte) (ret []byte) {
  102. if len(str) > 1 && str[0:2] == "0x" && !strings.Contains(str, "\n") {
  103. ret = Hex2Bytes(str[2:])
  104. } else {
  105. ret = cb(str)
  106. }
  107. return
  108. }
  109. func FormatData(data string) []byte {
  110. if len(data) == 0 {
  111. return nil
  112. }
  113. // Simple stupid
  114. d := new(big.Int)
  115. if data[0:1] == "\"" && data[len(data)-1:] == "\"" {
  116. return RightPadBytes([]byte(data[1:len(data)-1]), 32)
  117. } else if len(data) > 1 && data[:2] == "0x" {
  118. d.SetBytes(Hex2Bytes(data[2:]))
  119. } else {
  120. d.SetString(data, 0)
  121. }
  122. return BigToBytes(d, 256)
  123. }
  124. func ParseData(data ...interface{}) (ret []byte) {
  125. for _, item := range data {
  126. switch t := item.(type) {
  127. case string:
  128. var str []byte
  129. if IsHex(t) {
  130. str = Hex2Bytes(t[2:])
  131. } else {
  132. str = []byte(t)
  133. }
  134. ret = append(ret, RightPadBytes(str, 32)...)
  135. case []byte:
  136. ret = append(ret, LeftPadBytes(t, 32)...)
  137. }
  138. }
  139. return
  140. }
  141. func RightPadBytes(slice []byte, l int) []byte {
  142. if l < len(slice) {
  143. return slice
  144. }
  145. padded := make([]byte, l)
  146. copy(padded[0:len(slice)], slice)
  147. return padded
  148. }
  149. func LeftPadBytes(slice []byte, l int) []byte {
  150. if l < len(slice) {
  151. return slice
  152. }
  153. padded := make([]byte, l)
  154. copy(padded[l-len(slice):], slice)
  155. return padded
  156. }
  157. func LeftPadString(str string, l int) string {
  158. if l < len(str) {
  159. return str
  160. }
  161. zeros := Bytes2Hex(make([]byte, (l-len(str))/2))
  162. return zeros + str
  163. }
  164. func RightPadString(str string, l int) string {
  165. if l < len(str) {
  166. return str
  167. }
  168. zeros := Bytes2Hex(make([]byte, (l-len(str))/2))
  169. return str + zeros
  170. }
  171. func Address(slice []byte) (addr []byte) {
  172. if len(slice) < 20 {
  173. addr = LeftPadBytes(slice, 20)
  174. } else if len(slice) > 20 {
  175. addr = slice[len(slice)-20:]
  176. } else {
  177. addr = slice
  178. }
  179. addr = CopyBytes(addr)
  180. return
  181. }
  182. func ByteSliceToInterface(slice [][]byte) (ret []interface{}) {
  183. for _, i := range slice {
  184. ret = append(ret, i)
  185. }
  186. return
  187. }