blob: da4140cab731a1efcb9b29afe54d78cc045fdfc1 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001//===- 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
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +080015/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +080016/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017
18#ifndef CS_LLVM_SUPPORT_LEB128_H
19#define CS_LLVM_SUPPORT_LEB128_H
20
Koutheir Attouchib914a1b2016-04-07 10:35:52 +020021#include "include/capstone/capstone.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022
23/// Utility function to decode a ULEB128 value.
24static inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n)
25{
26 const uint8_t *orig_p = p;
27 uint64_t Value = 0;
28 unsigned Shift = 0;
29 do {
30 Value += (*p & 0x7f) << Shift;
31 Shift += 7;
32 } while (*p++ >= 128);
33 if (n)
34 *n = (unsigned)(p - orig_p);
35 return Value;
36}
37
38#endif // LLVM_SYSTEM_LEB128_H