VarInt.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef VarInt_H
  16. #define VarInt_H
  17. #include "util/Gcc.h"
  18. #include <stdint.h>
  19. // Thank you Satoshi
  20. struct VarInt_Iter {
  21. uint8_t* ptr;
  22. uint8_t* end;
  23. uint8_t* start;
  24. };
  25. static inline void VarInt_clone(struct VarInt_Iter* out, const struct VarInt_Iter* in)
  26. {
  27. out->ptr = in->ptr;
  28. out->start = in->start;
  29. out->end = in->end;
  30. }
  31. static inline void VarInt_mk(struct VarInt_Iter* out, uint8_t* ptr, int length)
  32. {
  33. out->ptr = ptr;
  34. out->end = ptr + length;
  35. out->start = ptr;
  36. }
  37. static inline void VarInt_toStart(struct VarInt_Iter* iter)
  38. {
  39. iter->ptr = iter->start;
  40. }
  41. static inline void VarInt_toEnd(struct VarInt_Iter* iter)
  42. {
  43. iter->ptr = iter->end;
  44. }
  45. static inline int VarInt_hasMore(struct VarInt_Iter* iter)
  46. {
  47. return iter->end > iter->ptr;
  48. }
  49. static inline int VarInt_sizeOf(uint64_t val)
  50. {
  51. return (!!(val >> 32)) * 4 + (!!(val >> 16)) * 2 + (!!((val + 3) >> 8)) + 1;
  52. }
  53. static inline int VarInt_pop(struct VarInt_Iter* iter, uint64_t* _out)
  54. {
  55. uint64_t out = 0;
  56. uint8_t* bytes = iter->ptr;
  57. int len = iter->end - bytes;
  58. if (len < 9) {
  59. if (len < 5) {
  60. if (len < 3) {
  61. if (len < 1) { return -1; }
  62. if (*bytes >= 0xfd) { return -1; }
  63. } else if (*bytes >= 0xfe) { return -1; }
  64. } else if (*bytes >= 0xff) { return -1; }
  65. }
  66. switch (*bytes) {
  67. case 0xff:
  68. out |= *++bytes; out <<= 8;
  69. out |= *++bytes; out <<= 8;
  70. out |= *++bytes; out <<= 8;
  71. out |= *++bytes; out <<= 8;
  72. Gcc_FALLTHRU
  73. case 0xfe:
  74. out |= *++bytes; out <<= 8;
  75. out |= *++bytes; out <<= 8;
  76. Gcc_FALLTHRU
  77. case 0xfd:
  78. out |= *++bytes; out <<= 8;
  79. bytes++;
  80. Gcc_FALLTHRU
  81. default:
  82. out |= *bytes++;
  83. }
  84. iter->ptr = bytes;
  85. if (_out) { *_out = out; }
  86. return 0;
  87. }
  88. static inline int VarInt_push(struct VarInt_Iter* iter, uint64_t val)
  89. {
  90. uint8_t* ptr = iter->ptr;
  91. int padding = ptr - iter->start;
  92. if (padding < 9) {
  93. if (padding < 5) {
  94. if (padding < 3) {
  95. if (padding < 1) { return -1; }
  96. if (val > 0xfc) { return -1; }
  97. } else if (val > 0xffff) { return -1; }
  98. } else if (val > 0xffffffff) { return -1; }
  99. }
  100. int i = VarInt_sizeOf(val);
  101. for (int j = 0; j < i; j++) { *--ptr = val & 0xff; val >>= 8; }
  102. switch (i) {
  103. case 8: *--ptr = 0xff; break;
  104. case 4: *--ptr = 0xfe; break;
  105. case 2: *--ptr = 0xfd; break;
  106. }
  107. iter->ptr = ptr;
  108. return 0;
  109. }
  110. #endif