1 //===-- sanitizer_leb128.h --------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef SANITIZER_LEB128_H
10 #define SANITIZER_LEB128_H
12 #include "sanitizer_common.h"
13 #include "sanitizer_internal_defs.h"
15 namespace __sanitizer
{
17 template <typename T
, typename It
>
18 It
EncodeSLEB128(T value
, It begin
, It end
) {
21 u8 byte
= value
& 0x7f;
22 // NOTE: this assumes that this signed shift is an arithmetic right shift.
24 more
= !((((value
== 0) && ((byte
& 0x40) == 0)) ||
25 ((value
== -1) && ((byte
& 0x40) != 0))));
28 if (UNLIKELY(begin
== end
))
35 template <typename T
, typename It
>
36 It
DecodeSLEB128(It begin
, It end
, T
* v
) {
41 if (UNLIKELY(begin
== end
))
44 T slice
= byte
& 0x7f;
45 value
|= slice
<< shift
;
47 } while (byte
>= 128);
48 if (shift
< 64 && (byte
& 0x40))
49 value
|= (-1ULL) << shift
;
54 template <typename T
, typename It
>
55 It
EncodeULEB128(T value
, It begin
, It end
) {
57 u8 byte
= value
& 0x7f;
61 if (UNLIKELY(begin
== end
))
68 template <typename T
, typename It
>
69 It
DecodeULEB128(It begin
, It end
, T
* v
) {
74 if (UNLIKELY(begin
== end
))
77 T slice
= byte
& 0x7f;
78 value
+= slice
<< shift
;
80 } while (byte
>= 128);
85 } // namespace __sanitizer
87 #endif // SANITIZER_LEB128_H