forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathleb128.h
146 lines (130 loc) · 4.28 KB
/
leb128.h
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
137
138
139
140
141
142
143
144
145
146
#ifndef LEB128_H
#define LEB128_H
// ****************************************************************************
// leb128.h DB48X project
// ****************************************************************************
//
// File Description:
//
// Operations on LEB128-encoded data
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X 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.
// ****************************************************************************
#include "types.h"
#include <cstdint>
#ifdef DM42
# pragma GCC push_options
# pragma GCC optimize("-O3")
#endif // DM42
template <typename Int = uint, typename Data>
inline Int leb128(Data *&p)
// ----------------------------------------------------------------------------
// Return the leb128 value at pointer
// ----------------------------------------------------------------------------
{
const bool is_signed = Int(~0ULL) < Int(0);
byte *bp = (byte *) p;
Int result = 0;
unsigned shift = 0;
do
{
result |= Int(*bp & 0x7F) << shift;
shift += 7;
} while (*bp++ & 0x80);
p = (Data *) bp;
if (is_signed && (bp[-1] & 0x40))
result |= Int(~0ULL << (shift - 1));
return result;
}
inline INLINE uint16_t leb128_u16(byte *bp)
// ----------------------------------------------------------------------------
// Return the leb128 value at pointer
// ----------------------------------------------------------------------------
{
uint16_t b1 = *bp;
if (b1 < 0x80)
return b1;
return (b1 & 0x7F) | (uint16_t(bp[1]) << 7);
}
template<typename Data, typename Int = uint>
inline Data *leb128(Data *p, Int value)
// ----------------------------------------------------------------------------
// Write the LEB value at pointer
// ----------------------------------------------------------------------------
{
const bool is_signed = Int(~0ULL) < Int(0);
byte *bp = (byte *) p;
do
{
*bp++ = (value & 0x7F) | 0x80;
value = Int(value >> 7);
} while ((!is_signed && value != 0) ||
(is_signed
&& (value != Int(~0ULL) || (~bp[-1] & 0x40))
&& (value != 0 || ( bp[-1] & 0x40))));
bp[-1] &= ~0x80;
return (Data *) bp;
}
template<typename Int>
inline size_t leb128size(Int value)
// ----------------------------------------------------------------------------
// Compute the size required for a given integer value
// ----------------------------------------------------------------------------
{
const bool is_signed = Int(~0ULL) < Int(0);
size_t result = 0;
bool signbit = false;
do
{
signbit = value & 0x40;
value = Int(value >> 7);
result++;
} while ((!is_signed && value != 0) ||
(is_signed
&& (value != Int(~0ULL) || !signbit)
&& (value != 0 || signbit)));
return result;
}
template<typename Data>
inline size_t leb128size(Data *ptr)
// ----------------------------------------------------------------------------
// Compute the size of an LEB128 value at pointer
// ----------------------------------------------------------------------------
{
byte *s = (byte *) ptr;
byte *p = s;
do { } while (*p++ & 0x80);
return p - s;
}
template<typename Data>
inline Data *leb128skip(Data *ptr)
// ----------------------------------------------------------------------------
// Skip LEB128 data
// ----------------------------------------------------------------------------
{
const byte *p = (const byte *) ptr;
while ((*p++) & 0x80);
return (Data *) p;
}
#ifdef DM42
# pragma GCC pop_options
#endif // DM42
#endif // LEB128_H