pro_encode.c
1.47 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
/*
pro_encode.c
$Revision: 1.1.1.1 $ $Date: 2002/05/02 03:29:20 $
*/
#include "xlateTypes.h"
/*
This routine encodes the given unsigned number in leb128 mode,
and stores the bytes at address buffer. It returns the number
of bytes in the encoding. It is the user's responsibility to
make sure there is enough storage at buffer. The details of
leb128 encoding are in the Dwarf document.
*/
Elf32_Sword
_xlate_leb128_unsigned_encode (Elf32_Word number, char *buffer)
{
char *bufPtr;
char byte;
bufPtr = buffer;
do {
byte = (number & 0x7f);
number >>= 7;
if (number != 0)
byte |= 0x80;
*bufPtr = byte;
bufPtr++;
} while (number != 0);
return bufPtr - buffer;
}
/*
This routine encodes the given signed number in leb128 mode,
and stores the bytes at address buffer. It returns the number
of bytes in the encoding. It is the user's responsibility to
make sure there is enough storage at buffer. The details of
leb128 encoding are in the Dwarf document.
*/
Elf32_Sword
_xlate_leb128_signed_encode (Elf32_Sword number, char *buffer)
{
Elf32_Sword sign;
char *bufPtr;
char more;
char byte;
sign = (number < 0) ? -1 : 0;
bufPtr = buffer;
more = 1;
do {
byte = (number & 0x7f);
number >>= 7;
if (number == sign && (byte & 0x40) == ((char)sign & 0x40))
more = 0;
else
byte |= 0x80;
*bufPtr = byte;
bufPtr++;
} while (more);
return bufPtr - buffer;
}