-
Notifications
You must be signed in to change notification settings - Fork 2
/
section.c
247 lines (214 loc) · 7.86 KB
/
section.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
__/¯\____ ___/\__ _/\__ _/\_ _/\__ _/\___ ___/\__ __/\_ _/\__
\_ ____/_> ____ \_/ _ \_ \ < /_ \_/ _>> ____ \_ > \_/ _ \_
_> ___/ ¯>__> <<__// __ _/ |> ></ _/> </ ¯ \\__> <<__// /\ // __ _/
_> \7 <__/:. \__/:. \> \_/ L/ _____/. 7> .\_/:. \__/ <_/ </:. \> \_
|:::::::::::::::::::::::/:::::::::::::>::::::::/::::::::::::::::::::::::/:::::|
|¯¯\::::/\:/¯\::::/¯¯¯¯<::::/\::/¯¯\:/¯¯¯¯¯¯\::\::/¯¯\::::/¯¯\::::/¯¯¯¯<::::/¯|
|__ |¯¯| T _ |¯¯¯| ___ |¯¯| |¯| _ T ______ |¯¯¯¯| _ |¯¯¯| _ |¯¯¯| ___ |¯¯| _|
\|__|/\|/ \|___|/ \|__|/\|_|/ \|/ \| |/ \|___|/ \|___|/dNo\|__|/
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
This file is part of Etripator,
copyright (c) 2009--2024 Vincent Cruz.
Etripator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Etripator 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Etripator. If not, see <http://www.gnu.org/licenses/>.
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
*/
#include "section.h"
#include "jsonhelpers.h"
#include "message.h"
#include <jansson.h>
#include <errno.h>
#include <stdlib.h>
static const int32_t g_default_element_size = 8;
static const int32_t g_default_elements_per_line = 16;
// Retrieves section type name
const char* section_type_name(SectionType type) {
static const char *name[SECTION_TYPE_COUNT] = {
[SECTION_TYPE_DATA] = "data",
[SECTION_TYPE_CODE] = "code"
};
if((type <= SECTION_TYPE_UNKNOWN) || (type >= SECTION_TYPE_COUNT)) {
return "unknown";
}
return name[type];
}
// Retrieves data type name
const char* data_type_name(DataType type) {
static const char *name[DATA_TYPE_COUNT] = {
[DATA_TYPE_BINARY] = "binary",
[DATA_TYPE_HEX] = "hex",
[DATA_TYPE_STRING] = "string",
[DATA_TYPE_JUMP_TABLE] = "jumptable"
};
if((type <= DATA_TYPE_UNKNOWN) || (type >= DATA_TYPE_COUNT)) {
return "unknown";
}
return name[type];
}
// Reset a section to its default values
void section_reset(Section *s) {
assert(s != NULL);
s->name = NULL;
s->type = SECTION_TYPE_UNKNOWN;
s->page = 0;
s->logical = 0;
s->offset = 0;
s->size = 0;
for(unsigned int i=0; i<8U; i++) {
s->mpr[i] = 0;
}
s->output = NULL;
s->data.type = DATA_TYPE_UNKNOWN;
s->data.element_size = g_default_element_size;
s->data.elements_per_line = g_default_elements_per_line;
s->description = NULL;
}
static int section_compare(const void *a, const void *b) {
Section* s0 = (Section*)a;
Section* s1 = (Section*)b;
int cmp = s0->page - s1->page;
if(!cmp) {
cmp = s0->logical - s1->logical;
}
return cmp;
}
// Delete section.
void section_delete(Section *ptr) {
assert(ptr != NULL);
free(ptr->name);
free(ptr->output);
free(ptr->description);
section_reset(ptr);
}
// Reset a section array.
void section_array_reset(SectionArray *arr) {
assert(arr != NULL);
if(arr->data != NULL) {
for(size_t i=0; i<arr->count; i++) {
section_delete(&arr->data[i]);
}
}
arr->count = 0;
}
// Release section array memory.
void section_array_delete(SectionArray *arr) {
assert(arr != NULL);
section_array_reset(arr);
free(arr->data);
arr->data = NULL;
arr->capacity = 0;
}
// Check if 2 sections overlaps.
// \return 1 Both sections has the same time and overlaps
// \return 0 Sections don't overlap
// \return -1 Sections overlap but they are not of the same type.
static int section_overlap(const Section *a, const Section *b) {
assert(a != NULL);
assert(b != NULL);
int ret = 0;
if(a->page == b->page) {
if(a->logical > b->logical) {
const Section *tmp = b;
b = a;
a = tmp;
}
if(a->type != b->type) {
ret = (b->logical < (a->logical + a->size)) ? -1 : 0;
} else if(b->logical <= (a->logical + a->size)) {
if(a->type == SECTION_TYPE_DATA) {
ret = (a->data.type == b->data.type) ? 1 : -1;
} else {
ret = 1;
}
}
}
return ret;
}
static inline uint16_t minu16(uint16_t a, uint16_t b) {
return (a < b) ? a : b;
}
static inline uint16_t maxi32(int32_t a, int32_t b) {
return (a > b) ? a : b;
}
// Merge 2 sections the 2nd section into the 1st one.
static void section_merge(Section *a, const Section *b) {
assert(a != NULL);
assert(b != NULL);
uint16_t begin = minu16(a->logical, b->logical);
int32_t end = maxi32(a->logical+a->size, b->logical+b->size);
a->logical = begin;
a->size = end - begin;
}
// Add a new section.
int section_array_add(SectionArray *arr, const Section* in) {
assert(arr != NULL);
assert(in != NULL);
int ret = -1;
// Check if we need to expand section array buffer
if(arr->count >= arr->capacity) {
size_t n = arr->capacity + 4U;
Section *ptr = realloc(arr->data, n*sizeof(Section));
if(ptr == NULL) {
ERROR_MSG("Failed to expand section array buffer", strerror(errno));
} else {
arr->data = ptr;
arr->capacity = n;
ret = 1;
}
} else {
ret = 1;
}
if(ret > 0) {
arr->data[arr->count] = *in;
arr->count++;
}
return ret;
}
// Retrieve the ith section from the array.
const Section* section_array_get(SectionArray *arr, size_t i) {
const Section *ret = NULL;
if(arr != NULL) {
if((arr->data != NULL) && (i < arr->count)) {
ret = &arr->data[i];
}
}
return ret;
}
// Merge and sort sections.
void section_array_tidy(SectionArray *arr) {
assert(arr != NULL);
assert(arr->count != 0);
Section *section = calloc(arr->count, sizeof(Section));
qsort(arr->data, arr->count, sizeof(Section), section_compare);
size_t j = 0;
section[0] = arr->data[0];
for(size_t i=1; i<arr->count; i++) {
int overlap = section_overlap(§ion[j], &arr->data[i]);
if(overlap == 1) {
section_merge(§ion[j], &arr->data[i]);
INFO_MSG("Section %s has been merged with %s!", arr->data[i].name, section[j].name);
section_delete(&arr->data[i]);
} else {
if(overlap == -1) {
WARNING_MSG("Section %s and %s overlaps!", arr->data[i].name, section[j].name);
}
section[++j] = arr->data[i];
}
}
free(arr->data);
arr->data = section;
arr->count = j+1;
}