1//===- llvm/Support/LEB128.h - [SU]LEB128 utility functions -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares some utility functions for encoding SLEB128 and
11// ULEB128 values.
12//
13//===----------------------------------------------------------------------===//
14
15/* Capstone Disassembly Engine */
16/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
17
18#ifndef CS_LLVM_SUPPORT_LEB128_H
19#define CS_LLVM_SUPPORT_LEB128_H
20
21#if !defined(_MSC_VER) || !defined(_KERNEL_MODE)
22#include <stdint.h>
23#endif
24
25/// Utility function to decode a ULEB128 value.
26static inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n)
27{
28 const uint8_t *orig_p = p;
29 uint64_t Value = 0;
30 unsigned Shift = 0;
31 do {
32 Value += (*p & 0x7f) << Shift;
33 Shift += 7;
34 } while (*p++ >= 128);
35 if (n)
36 *n = (unsigned)(p - orig_p);
37 return Value;
38}
39
40#endif // LLVM_SYSTEM_LEB128_H
41