bytes.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package common contains various helper functions.
  17. package common
  18. import "encoding/hex"
  19. // ToHex returns the hex representation of b, prefixed with '0x'.
  20. // For empty slices, the return value is "0x0".
  21. //
  22. // Deprecated: use hexutil.Encode instead.
  23. func ToHex(b []byte) string {
  24. hex := Bytes2Hex(b)
  25. if len(hex) == 0 {
  26. hex = "0"
  27. }
  28. return "0x" + hex
  29. }
  30. // FromHex returns the bytes represented by the hexadecimal string s.
  31. // s may be prefixed with "0x".
  32. func FromHex(s string) []byte {
  33. if len(s) > 1 {
  34. if s[0:2] == "0x" || s[0:2] == "0X" {
  35. s = s[2:]
  36. }
  37. }
  38. if len(s)%2 == 1 {
  39. s = "0" + s
  40. }
  41. return Hex2Bytes(s)
  42. }
  43. // CopyBytes returns an exact copy of the provided bytes.
  44. func CopyBytes(b []byte) (copiedBytes []byte) {
  45. if b == nil {
  46. return nil
  47. }
  48. copiedBytes = make([]byte, len(b))
  49. copy(copiedBytes, b)
  50. return
  51. }
  52. // hasHexPrefix validates str begins with '0x' or '0X'.
  53. func hasHexPrefix(str string) bool {
  54. return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
  55. }
  56. // isHexCharacter returns bool of c being a valid hexadecimal.
  57. func isHexCharacter(c byte) bool {
  58. return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
  59. }
  60. // isHex validates whether each byte is valid hexadecimal string.
  61. func isHex(str string) bool {
  62. if len(str)%2 != 0 {
  63. return false
  64. }
  65. for _, c := range []byte(str) {
  66. if !isHexCharacter(c) {
  67. return false
  68. }
  69. }
  70. return true
  71. }
  72. // Bytes2Hex returns the hexadecimal encoding of d.
  73. func Bytes2Hex(d []byte) string {
  74. return hex.EncodeToString(d)
  75. }
  76. // Hex2Bytes returns the bytes represented by the hexadecimal string str.
  77. func Hex2Bytes(str string) []byte {
  78. h, _ := hex.DecodeString(str)
  79. return h
  80. }
  81. // Hex2BytesFixed returns bytes of a specified fixed length flen.
  82. func Hex2BytesFixed(str string, flen int) []byte {
  83. h, _ := hex.DecodeString(str)
  84. if len(h) == flen {
  85. return h
  86. }
  87. if len(h) > flen {
  88. return h[len(h)-flen:]
  89. }
  90. hh := make([]byte, flen)
  91. copy(hh[flen-len(h):flen], h[:])
  92. return hh
  93. }
  94. // RightPadBytes zero-pads slice to the right up to length l.
  95. func RightPadBytes(slice []byte, l int) []byte {
  96. if l <= len(slice) {
  97. return slice
  98. }
  99. padded := make([]byte, l)
  100. copy(padded, slice)
  101. return padded
  102. }
  103. // LeftPadBytes zero-pads slice to the left up to length l.
  104. func LeftPadBytes(slice []byte, l int) []byte {
  105. if l <= len(slice) {
  106. return slice
  107. }
  108. padded := make([]byte, l)
  109. copy(padded[l-len(slice):], slice)
  110. return padded
  111. }