encoding.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. func decodeCompact(key []byte) []byte {
  68. l := len(key) / 2
  69. var res = make([]byte, l)
  70. for i := 0; i < l; i++ {
  71. v1, v0 := key[2*i], key[2*i+1]
  72. res[i] = v1*16 + v0
  73. }
  74. return res
  75. }
  76. // prefixLen returns the length of the common prefix of a and b.
  77. func prefixLen(a, b []byte) int {
  78. var i, length = 0, len(a)
  79. if len(b) < length {
  80. length = len(b)
  81. }
  82. for ; i < length; i++ {
  83. if a[i] != b[i] {
  84. break
  85. }
  86. }
  87. return i
  88. }
  89. func hasTerm(s []byte) bool {
  90. return s[len(s)-1] == 16
  91. }
  92. func remTerm(s []byte) []byte {
  93. if hasTerm(s) {
  94. b := make([]byte, len(s)-1)
  95. copy(b, s)
  96. return b
  97. }
  98. return s
  99. }