bytes.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 (
  19. "encoding/hex"
  20. )
  21. // FromHex returns the bytes represented by the hexadecimal string s.
  22. // s may be prefixed with "0x".
  23. func FromHex(s string) []byte {
  24. if has0xPrefix(s) {
  25. s = s[2:]
  26. }
  27. if len(s)%2 == 1 {
  28. s = "0" + s
  29. }
  30. return Hex2Bytes(s)
  31. }
  32. // CopyBytes returns an exact copy of the provided bytes.
  33. func CopyBytes(b []byte) (copiedBytes []byte) {
  34. if b == nil {
  35. return nil
  36. }
  37. copiedBytes = make([]byte, len(b))
  38. copy(copiedBytes, b)
  39. return
  40. }
  41. // has0xPrefix validates str begins with '0x' or '0X'.
  42. func has0xPrefix(str string) bool {
  43. return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
  44. }
  45. // isHexCharacter returns bool of c being a valid hexadecimal.
  46. func isHexCharacter(c byte) bool {
  47. return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
  48. }
  49. // isHex validates whether each byte is valid hexadecimal string.
  50. func isHex(str string) bool {
  51. if len(str)%2 != 0 {
  52. return false
  53. }
  54. for _, c := range []byte(str) {
  55. if !isHexCharacter(c) {
  56. return false
  57. }
  58. }
  59. return true
  60. }
  61. // Bytes2Hex returns the hexadecimal encoding of d.
  62. func Bytes2Hex(d []byte) string {
  63. return hex.EncodeToString(d)
  64. }
  65. // Hex2Bytes returns the bytes represented by the hexadecimal string str.
  66. func Hex2Bytes(str string) []byte {
  67. h, _ := hex.DecodeString(str)
  68. return h
  69. }
  70. // Hex2BytesFixed returns bytes of a specified fixed length flen.
  71. func Hex2BytesFixed(str string, flen int) []byte {
  72. h, _ := hex.DecodeString(str)
  73. if len(h) == flen {
  74. return h
  75. }
  76. if len(h) > flen {
  77. return h[len(h)-flen:]
  78. }
  79. hh := make([]byte, flen)
  80. copy(hh[flen-len(h):flen], h)
  81. return hh
  82. }
  83. // RightPadBytes zero-pads slice to the right up to length l.
  84. func RightPadBytes(slice []byte, l int) []byte {
  85. if l <= len(slice) {
  86. return slice
  87. }
  88. padded := make([]byte, l)
  89. copy(padded, slice)
  90. return padded
  91. }
  92. // LeftPadBytes zero-pads slice to the left up to length l.
  93. func LeftPadBytes(slice []byte, l int) []byte {
  94. if l <= len(slice) {
  95. return slice
  96. }
  97. padded := make([]byte, l)
  98. copy(padded[l-len(slice):], slice)
  99. return padded
  100. }
  101. // TrimLeftZeroes returns a subslice of s without leading zeroes
  102. func TrimLeftZeroes(s []byte) []byte {
  103. idx := 0
  104. for ; idx < len(s); idx++ {
  105. if s[idx] != 0 {
  106. break
  107. }
  108. }
  109. return s[idx:]
  110. }
  111. // TrimRightZeroes returns a subslice of s without trailing zeroes
  112. func TrimRightZeroes(s []byte) []byte {
  113. idx := len(s)
  114. for ; idx > 0; idx-- {
  115. if s[idx-1] != 0 {
  116. break
  117. }
  118. }
  119. return s[:idx]
  120. }