leb128.c
3.76 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
LEB128.C
Encoding/decoding routines for leb128
compression.
*/
#include <sys/types.h>
#include <sys/syssgi.h>
#include "leb128.h"
#define MORE_BYTES 0x80
#define DATA_MASK 0x7f
#define DIGIT_WIDTH 7
#define SIGN_BIT 0x40
/*******************************************************
Function: uncompress_u4
LEB128 decoding of a compressed 4 byte
unsigned number.
*******************************************************/
void *
uncompress_u4(char *ptr, __uint32_t * const value)
{
char byte = 0;
unsigned shift = 0;
*value = 0;
do {
byte = *ptr++;
*value |= ((0x7f & byte) << shift);
shift+=7;
/*
* more bytes to come
*/
} while(byte & 0x80);
return(ptr);
}
/*******************************************************
Function: uncompress_u8
LEB128 decoding of a compressed 8 byte
unsigned number.
*******************************************************/
void *
uncompress_u8(char *ptr, __uint64_t * const value)
{
char byte = 0;
unsigned shift = 0;
*value = 0;
do{
byte = *ptr++;
*value |= ((0x7f & byte) << shift);
shift+=7;
/*
* more bytes to come
*/
} while(byte & 0x80);
return(ptr);
}
/*******************************************************
Function: uncompress_s4
LEB128 decoding of a compressed 4 byte
signed number.
*******************************************************/
void *
uncompress_s4(char *ptr, __int32_t * const value)
{
char byte = 0;
__uint32_t shift = 0;
__uint32_t size = (sizeof(__int32_t))*8;
*value = 0;
while(1) {
byte = *ptr++;
*value |= ((0x7f & byte) << shift);
shift+=7;
/*
* The sign bit of byte is 2nd high
* order bit 0x40. Break if the
* high order bit is clear indicating
* the end.
*/
if (!(byte & 0x80))
break;
}
if ((shift < size) && (byte & 0x40))
*value |= -(1<<shift);
return(ptr);
}
/*******************************************************
Function: uncompress_s8
LEB128 decoding of a compressed 8 byte
signed number.
*******************************************************/
void *
uncompress_s8(char *ptr, __int64_t * const value)
{
char byte = 0;
__uint32_t shift = 0;
__uint32_t size = (sizeof(__int64_t))*8;
*value = 0;
while(1) {
byte = *ptr++;
*value |= ((0x7f & byte) << shift);
shift+=7;
/*
* The sign bit of byte is 2nd high
* order bit 0x40. Break if the
* high order bit is clear indicating
* the end.
*/
if (!(byte & 0x80))
break;
}
if ((shift < size) && (byte & 0x40))
*value |= -(1<<shift);
return(ptr);
}
/*********************************************************
Function: compress_u8
LEB128 encoding of a 8 byte unsigned number.
**********************************************************/
__uint32_t compress_u8(char *buf, __uint64_t value)
{
char byte;
__uint32_t count = 0;
do{
byte = value & DATA_MASK;
value >>= DIGIT_WIDTH;
count++;
/* check if there are more bytes to come */
if (value !=0) byte |= MORE_BYTES;
*buf++ = byte;
} while (value != 0);
return count;
}
/*******************************************************
Function: compress_s8
LEB128 encoding of a 8 byte signed number.
*******************************************************/
__uint32_t compress_s8 (char *buf, __int64_t value)
{
char byte;
__uint32_t count = 0;
__uint32_t more = 1;
__int64_t sign = - (value < 0);
do {
byte = value & DATA_MASK;
value >>= DIGIT_WIDTH;
count++;
/* Remaining chunks would just contain the sign bit, and this chunk
has already captured at least one sign bit.
*/
if (value == sign && (byte & SIGN_BIT) == (sign & SIGN_BIT))
more = 0;
else
byte |= MORE_BYTES;
*buf++ = byte;
} while (more);
return count;
}