leb128.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Utilities for reading leb128 values.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of the libiberty library.
  4. Libiberty is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. Libiberty is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with libiberty; see the file COPYING.LIB. If not, write
  14. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /* The functions defined here can be speed critical.
  17. Since they are all pretty small we keep things simple and just define
  18. them all as "static inline".
  19. WARNING: This file is used by GDB which is stuck at C90. :-(
  20. Though it can use stdint.h, inttypes.h.
  21. Therefore if you want to add support for "long long" you need
  22. to wrap it in #ifdef CC_HAS_LONG_LONG. */
  23. #ifndef LEB128_H
  24. #define LEB128_H
  25. /* Get a definition for inline. */
  26. #include "ansidecl.h"
  27. /* Get a definition for NULL, size_t. */
  28. #include <stddef.h>
  29. #ifdef HAVE_STDINT_H
  30. #include <stdint.h>
  31. #endif
  32. #ifdef HAVE_INTTYPES_H
  33. #include <inttypes.h>
  34. #endif
  35. /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
  36. by R, and return the number of bytes read.
  37. If we read off the end of the buffer, zero is returned,
  38. and nothing is stored in R.
  39. Note: The result is an int instead of a pointer to the next byte to be
  40. read to avoid const-vs-non-const problems. */
  41. static inline size_t
  42. read_uleb128_to_uint64 (const unsigned char *buf, const unsigned char *buf_end,
  43. uint64_t *r)
  44. {
  45. const unsigned char *p = buf;
  46. unsigned int shift = 0;
  47. uint64_t result = 0;
  48. unsigned char byte;
  49. while (1)
  50. {
  51. if (p >= buf_end)
  52. return 0;
  53. byte = *p++;
  54. result |= ((uint64_t) (byte & 0x7f)) << shift;
  55. if ((byte & 0x80) == 0)
  56. break;
  57. shift += 7;
  58. }
  59. *r = result;
  60. return p - buf;
  61. }
  62. /* Decode the signed LEB128 constant at BUF into the variable pointed to
  63. by R, and return the number of bytes read.
  64. If we read off the end of the buffer, zero is returned,
  65. and nothing is stored in R.
  66. Note: The result is an int instead of a pointer to the next byte to be
  67. read to avoid const-vs-non-const problems. */
  68. static inline size_t
  69. read_sleb128_to_int64 (const unsigned char *buf, const unsigned char *buf_end,
  70. int64_t *r)
  71. {
  72. const unsigned char *p = buf;
  73. unsigned int shift = 0;
  74. int64_t result = 0;
  75. unsigned char byte;
  76. while (1)
  77. {
  78. if (p >= buf_end)
  79. return 0;
  80. byte = *p++;
  81. result |= ((uint64_t) (byte & 0x7f)) << shift;
  82. shift += 7;
  83. if ((byte & 0x80) == 0)
  84. break;
  85. }
  86. if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
  87. result |= -(((uint64_t) 1) << shift);
  88. *r = result;
  89. return p - buf;
  90. }
  91. /* Return the number of bytes to read to skip past an LEB128 number in BUF.
  92. If the end isn't found before reaching BUF_END, return zero.
  93. Note: The result is an int instead of a pointer to the next byte to be
  94. read to avoid const-vs-non-const problems. */
  95. static inline size_t
  96. skip_leb128 (const unsigned char *buf, const unsigned char *buf_end)
  97. {
  98. const unsigned char *p = buf;
  99. unsigned char byte;
  100. while (1)
  101. {
  102. if (p == buf_end)
  103. return 0;
  104. byte = *p++;
  105. if ((byte & 0x80) == 0)
  106. return p - buf;
  107. }
  108. }
  109. #endif /* LEB128_H */