encoding.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 trie
  17. func compactEncode(hexSlice []byte) []byte {
  18. terminator := byte(0)
  19. if hexSlice[len(hexSlice)-1] == 16 {
  20. terminator = 1
  21. hexSlice = hexSlice[:len(hexSlice)-1]
  22. }
  23. var (
  24. odd = byte(len(hexSlice) % 2)
  25. buflen = len(hexSlice)/2 + 1
  26. bi, hi = 0, 0 // indices
  27. hs = byte(0) // shift: flips between 0 and 4
  28. )
  29. if odd == 0 {
  30. bi = 1
  31. hs = 4
  32. }
  33. buf := make([]byte, buflen)
  34. buf[0] = terminator<<5 | byte(odd)<<4
  35. for bi < len(buf) && hi < len(hexSlice) {
  36. buf[bi] |= hexSlice[hi] << hs
  37. if hs == 0 {
  38. bi++
  39. }
  40. hi, hs = hi+1, hs^(1<<2)
  41. }
  42. return buf
  43. }
  44. func compactDecode(str []byte) []byte {
  45. base := compactHexDecode(str)
  46. base = base[:len(base)-1]
  47. if base[0] >= 2 {
  48. base = append(base, 16)
  49. }
  50. if base[0]%2 == 1 {
  51. base = base[1:]
  52. } else {
  53. base = base[2:]
  54. }
  55. return base
  56. }
  57. func compactHexDecode(str []byte) []byte {
  58. l := len(str)*2 + 1
  59. var nibbles = make([]byte, l)
  60. for i, b := range str {
  61. nibbles[i*2] = b / 16
  62. nibbles[i*2+1] = b % 16
  63. }
  64. nibbles[l-1] = 16
  65. return nibbles
  66. }
  67. // compactHexEncode encodes a series of nibbles into a byte array
  68. func compactHexEncode(nibbles []byte) []byte {
  69. nl := len(nibbles)
  70. if nl == 0 {
  71. return nil
  72. }
  73. if nibbles[nl-1] == 16 {
  74. nl--
  75. }
  76. l := (nl + 1) / 2
  77. var str = make([]byte, l)
  78. for i := range str {
  79. b := nibbles[i*2] * 16
  80. if nl > i*2 {
  81. b += nibbles[i*2+1]
  82. }
  83. str[i] = b
  84. }
  85. return str
  86. }
  87. func decodeCompact(key []byte) []byte {
  88. l := len(key) / 2
  89. var res = make([]byte, l)
  90. for i := 0; i < l; i++ {
  91. v1, v0 := key[2*i], key[2*i+1]
  92. res[i] = v1*16 + v0
  93. }
  94. return res
  95. }
  96. // prefixLen returns the length of the common prefix of a and b.
  97. func prefixLen(a, b []byte) int {
  98. var i, length = 0, len(a)
  99. if len(b) < length {
  100. length = len(b)
  101. }
  102. for ; i < length; i++ {
  103. if a[i] != b[i] {
  104. break
  105. }
  106. }
  107. return i
  108. }
  109. func hasTerm(s []byte) bool {
  110. return s[len(s)-1] == 16
  111. }
  112. func remTerm(s []byte) []byte {
  113. if hasTerm(s) {
  114. b := make([]byte, len(s)-1)
  115. copy(b, s)
  116. return b
  117. }
  118. return s
  119. }