Skip to content

Commit b8a363a

Browse files
committed
Moved the (U)LEB128 handling functions into their own file
1 parent 66cd5ce commit b8a363a

File tree

6 files changed

+116
-59
lines changed

6 files changed

+116
-59
lines changed

src/parser/file/dwarf/dwarf_parser.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,12 @@
2020
*/
2121

2222
#include "dwarf_parser.h"
23+
#include "leb128.h"
2324
#include "vector_pair_uint64.h"
2425

2526
#include "v4/definitions.h"
2627
#include "v5/definitions.h"
2728

28-
uint64_t getULEB128(void* begin, size_t* counter) {
29-
uint64_t result = 0,
30-
shift = 0;
31-
32-
bool more = true;
33-
do {
34-
uint8_t b = *((uint8_t*) (begin + *counter));
35-
*counter += 1;
36-
result |= (b & 0x7f) << shift;
37-
shift += 7;
38-
if (b < 0x80) {
39-
more = false;
40-
}
41-
} while (more);
42-
return result;
43-
}
44-
45-
int64_t getLEB128(void* begin, size_t* counter) {
46-
int64_t result = 0,
47-
shift = 0;
48-
49-
bool more = true;
50-
do {
51-
uint8_t b = *((uint8_t*) (begin + *counter));
52-
*counter += 1;
53-
result |= (b & 0x7f) << shift;
54-
shift += 7;
55-
if ((0x80 & b) == 0) {
56-
if (shift < 32 && (b & 0x40) != 0) {
57-
result |= ((uint64_t) ~0 << shift);
58-
}
59-
more = false;
60-
}
61-
} while (more);
62-
return result;
63-
}
64-
6529
char* dwarf_pathConcatenate(const char* string1, const char* string2) {
6630
const size_t len1 = strlen(string1),
6731
len2 = strlen(string2);

src/parser/file/dwarf/dwarf_parser.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,6 @@ bool dwarf_parseLineProgram(struct lcs_section debugLine,
126126
struct lcs_section debugStrOffsets,
127127
dwarf_line_callback cb, void* args);
128128

129-
/**
130-
* @brief Reads an unsigned LEB128 integer from the given memory at the given position.
131-
*
132-
* The given memory position points to the first byte after the read number once this function returns.
133-
*
134-
* @param begin the memory pointer
135-
* @param counter the memory position
136-
* @return the deducted number
137-
*/
138-
uint64_t getULEB128(void* begin, size_t* counter);
139-
140-
/**
141-
* @brief Reads a signed LEB128 integer from the given memory at the given position.
142-
*
143-
* The given memory position points to the first byte after the read number once this function returns.
144-
*
145-
* @param begin the memory pointer
146-
* @param counter the memory position
147-
* @return the deducted number
148-
*/
149-
int64_t getLEB128(void* begin, size_t* counter);
150-
151129
/**
152130
* Concatenates the two given strings as paths.
153131
*

src/parser/file/dwarf/leb128.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* CallstackLibrary - Library creating human-readable call stacks.
3+
*
4+
* Copyright (C) 2024 mhahnFr
5+
*
6+
* This file is part of the CallstackLibrary.
7+
*
8+
* The CallstackLibrary is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* The CallstackLibrary is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License along with the
19+
* CallstackLibrary, see the file LICENSE. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
#include <stdbool.h>
23+
24+
#include "leb128.h"
25+
26+
uint64_t getULEB128(void* begin, size_t* counter) {
27+
uint64_t result = 0,
28+
shift = 0;
29+
30+
bool more = true;
31+
do {
32+
uint8_t b = *((uint8_t*) (begin + *counter));
33+
*counter += 1;
34+
result |= (b & 0x7f) << shift;
35+
shift += 7;
36+
if (b < 0x80) {
37+
more = false;
38+
}
39+
} while (more);
40+
return result;
41+
}
42+
43+
int64_t getLEB128(void* begin, size_t* counter) {
44+
int64_t result = 0,
45+
shift = 0;
46+
47+
bool more = true;
48+
do {
49+
uint8_t b = *((uint8_t*) (begin + *counter));
50+
*counter += 1;
51+
result |= (b & 0x7f) << shift;
52+
shift += 7;
53+
if ((0x80 & b) == 0) {
54+
if (shift < 32 && (b & 0x40) != 0) {
55+
result |= ((uint64_t) ~0 << shift);
56+
}
57+
more = false;
58+
}
59+
} while (more);
60+
return result;
61+
}

src/parser/file/dwarf/leb128.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* CallstackLibrary - Library creating human-readable call stacks.
3+
*
4+
* Copyright (C) 2024 mhahnFr
5+
*
6+
* This file is part of the CallstackLibrary.
7+
*
8+
* The CallstackLibrary is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* The CallstackLibrary is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License along with the
19+
* CallstackLibrary, see the file LICENSE. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef leb128_h
23+
#define leb128_h
24+
25+
#include <stddef.h>
26+
#include <stdint.h>
27+
28+
/**
29+
* @brief Reads an unsigned LEB128 integer from the given memory at the given position.
30+
*
31+
* The given memory position points to the first byte after the read number once this function returns.
32+
*
33+
* @param begin the memory pointer
34+
* @param counter the memory position
35+
* @return the deducted number
36+
*/
37+
uint64_t getULEB128(void* begin, size_t* counter);
38+
39+
/**
40+
* @brief Reads a signed LEB128 integer from the given memory at the given position.
41+
*
42+
* The given memory position points to the first byte after the read number once this function returns.
43+
*
44+
* @param begin the memory pointer
45+
* @param counter the memory position
46+
* @return the deducted number
47+
*/
48+
int64_t getLEB128(void* begin, size_t* counter);
49+
50+
51+
52+
#endif /* leb128_h */

src/parser/file/dwarf/v4/parser.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "parser.h"
2323

2424
#include "../dwarf_parser.h"
25+
#include "../leb128.h"
2526

2627
/**
2728
* Parses the line number program header in version 2, 3 or 4.

src/parser/file/dwarf/v5/parser.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "parser.h"
2525

2626
#include "../dwarf_parser.h"
27+
#include "../leb128.h"
2728
#include "../optional_uint64_t.h"
2829
#include "../vector_pair_uint64.h"
2930

0 commit comments

Comments
 (0)