You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.6 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package p2p
  17. import "encoding/binary"
  18. // boost varint can encode upto 1 gb size
  19. func Decode_Boost_Varint(buf []byte) (uint64, int) {
  20. first_byte := buf[0]
  21. num_bytes := first_byte & 3 // lower 2 bits contain bytes count, which follow current byte
  22. // grab full 4 bytes
  23. value := binary.LittleEndian.Uint32(buf)
  24. switch num_bytes {
  25. case 0:
  26. value &= 0xff
  27. case 1:
  28. value &= 0xffff
  29. case 2:
  30. value &= 0xffffff
  31. case 3:
  32. value &= 0xffffffff
  33. }
  34. value = value >> 2 // discard lower 2 bits
  35. if num_bytes == 2 { // holy hell, let god have mercy on this boost parser
  36. num_bytes = 3
  37. }
  38. return uint64(value), int(num_bytes + 1)
  39. }
  40. // this function encodes Value to bu in boost varint style
  41. func Encode_Boost_Varint(buf []byte, Value uint64) int {
  42. bytes_required := byte(0)
  43. switch {
  44. case Value > 1073741823:
  45. panic("Exceeded boost varint capacity while encoding\n") // (2^30) -1
  46. case Value > 4194303:
  47. bytes_required = 4
  48. case Value > 16383:
  49. bytes_required = 3
  50. case Value > 63:
  51. bytes_required = 2
  52. default:
  53. bytes_required = 1
  54. }
  55. first_byte := (Value % 64) << 2
  56. Value = Value >> 6
  57. second_byte := Value % 256
  58. Value = Value >> 8
  59. third_byte := Value % 256
  60. Value = Value >> 8
  61. fourth_byte := Value % 256
  62. Value = Value >> 8
  63. // encode bytes length in lower 2 bits of first byte
  64. first_byte |= uint64(byte(bytes_required - 1))
  65. buf[0] = byte(first_byte)
  66. buf[1] = byte(second_byte)
  67. buf[2] = byte(third_byte)
  68. buf[3] = byte(fourth_byte)
  69. if bytes_required == 3 { // thank god we are soon going to migrate from boost hell, once and for all
  70. bytes_required = 4
  71. }
  72. return int(bytes_required)
  73. }