aboutsummaryrefslogtreecommitdiff
path: root/include/leb128.h
blob: dc3e72a5bf86cc3c7db84b737f391e936dd88039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Utilities for reading leb128 values.
   Copyright (C) 2012-2019 Free Software Foundation, Inc.

This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

Libiberty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB.  If not, write
to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  */

/* The functions defined here can be speed critical.
   Since they are all pretty small we keep things simple and just define
   them all as "static inline".

   WARNING: This file is used by GDB which is stuck at C90. :-(
   Though it can use stdint.h, inttypes.h.
   Therefore if you want to add support for "long long" you need
   to wrap it in #ifdef CC_HAS_LONG_LONG.  */

#ifndef LEB128_H
#define LEB128_H

/* Get a definition for inline.  */
#include "ansidecl.h"

/* Get a definition for NULL, size_t.  */
#include <stddef.h>

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

/* Decode the unsigned LEB128 constant at BUF into the variable pointed to
   by R, and return the number of bytes read.
   If we read off the end of the buffer, zero is returned,
   and nothing is stored in R.

   Note: The result is an int instead of a pointer to the next byte to be
   read to avoid const-vs-non-const problems.  */

static inline size_t
read_uleb128_to_uint64 (const unsigned char *buf, const unsigned char *buf_end,
			uint64_t *r)
{
  const unsigned char *p = buf;
  unsigned int shift = 0;
  uint64_t result = 0;
  unsigned char byte;

  while (1)
    {
      if (p >= buf_end)
	return 0;

      byte = *p++;
      result |= ((uint64_t) (byte & 0x7f)) << shift;
      if ((byte & 0x80) == 0)
	break;
      shift += 7;
    }

  *r = result;
  return p - buf;
}

/* Decode the signed LEB128 constant at BUF into the variable pointed to
   by R, and return the number of bytes read.
   If we read off the end of the buffer, zero is returned,
   and nothing is stored in R.

   Note: The result is an int instead of a pointer to the next byte to be
   read to avoid const-vs-non-const problems.  */

static inline size_t
read_sleb128_to_int64 (const unsigned char *buf, const unsigned char *buf_end,
		       int64_t *r)
{
  const unsigned char *p = buf;
  unsigned int shift = 0;
  int64_t result = 0;
  unsigned char byte;

  while (1)
    {
      if (p >= buf_end)
	return 0;

      byte = *p++;
      result |= ((uint64_t) (byte & 0x7f)) << shift;
      shift += 7;
      if ((byte & 0x80) == 0)
	break;
    }
  if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
    result |= -(((uint64_t) 1) << shift);

  *r = result;
  return p - buf;
}

/* Return the number of bytes to read to skip past an LEB128 number in BUF.
   If the end isn't found before reaching BUF_END, return zero.

   Note: The result is an int instead of a pointer to the next byte to be
   read to avoid const-vs-non-const problems.  */

static inline size_t
skip_leb128 (const unsigned char *buf, const unsigned char *buf_end)
{
  const unsigned char *p = buf;
  unsigned char byte;

  while (1)
    {
      if (p == buf_end)
	return 0;

      byte = *p++;
      if ((byte & 0x80) == 0)
	return p - buf;
    }
}

#endif /* LEB128_H */