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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/*
This file is part of odin, a memory profiler with fragmentation analysis.
Copyright (C) 2007 Chris Wilson <chris@chris-wilson.co.uk>
Based on:
Sysprof -- Sampling, systemwide CPU profiler
Copyright 2006, 2007, Soeren Sandmann
odin 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.
odin 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 odin. If not, see <http://www.gnu.org/licenses/>/
The GNU General Public License is contained in the file COPYING.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "binparser.h"
typedef struct _field Field;
struct _field {
guint offset; /* from beginning of struct */
guint width;
BinType type;
};
struct _bin_record {
BinRecord *next;
guint size;
guint n_fields;
Field fields[0];
};
void
bin_parser_init (BinParser *parser, const guchar *data, gsize length)
{
parser->data = data;
parser->length = length;
parser->offset = 0;
parser->records = NULL;
parser->endian = BIN_NATIVE_ENDIAN;
}
void
bin_parser_fini (BinParser *parser)
{
while (parser->records != NULL) {
BinRecord *record = parser->records;
parser->records = record->next;
g_free (record);
}
}
const guchar *
bin_parser_get_data (BinParser *parser)
{
return parser->data;
}
gsize
bin_parser_get_length (BinParser *parser)
{
return parser->length;
}
#define align(offset__, alignment__) \
((offset__ + alignment__ - 1) &~ (alignment__ - 1))
static int
get_align (const BinField *field)
{
if (field->type == BIN_UNINTERPRETED)
return 1;
else
return field->n_bytes;
}
BinRecord *
bin_parser_create_record (BinParser *parser, const BinField *fields)
{
BinRecord *record;
guint i, n_fields;
guint offset;
n_fields = 0;
while (fields[n_fields].index != -1)
n_fields++;
record = g_malloc (sizeof (BinRecord) + n_fields * sizeof (Field));
offset = 0;
record->n_fields = n_fields;
for (i = 0; i < n_fields; ++i) {
const BinField *bin_field = &fields[i];
Field *field;
offset = align (offset, get_align (bin_field));
field = &record->fields[bin_field->index];
field->offset = offset;
field->type = bin_field->type;
field->width = bin_field->n_bytes;
offset += field->width;
}
record->size = align (offset, get_align (fields));
record->next = parser->records;
parser->records = record;
return record;
}
void
bin_parser_set_endian (BinParser *parser, BinEndian endian)
{
parser->endian = endian;
}
/* Move current offset */
gsize
bin_parser_get_offset (BinParser *parser)
{
return parser->offset;
}
void
bin_parser_set_offset (BinParser *parser, gsize offset)
{
parser->offset = offset;
}
void
bin_parser_align (BinParser *parser, gsize byte_width)
{
parser->offset = align (parser->offset, byte_width);
}
gsize
bin_record_get_size (BinRecord *record)
{
return record->size;
}
void
bin_parser_seek_record (BinParser *parser,
BinRecord *record,
int n_records)
{
gsize record_size = bin_record_get_size (record);
parser->offset += record_size * n_records;
}
void
bin_parser_save (BinParser *parser)
{
parser->saved_offset = parser->offset;
}
void
bin_parser_restore (BinParser *parser)
{
parser->offset = parser->saved_offset;
}
/* retrieve data */
static guint64
convert_uint (const guchar *data, BinEndian endian, int width)
{
guint8 r8;
guint16 r16;
guint32 r32;
guint64 r64;
/* FIXME: check that we are within the file */
switch (width) {
case 1:
r8 = *(guint8 *)data;
return r8;
case 2:
r16 = *(guint16 *)data;
if (endian == BIN_BIG_ENDIAN)
r16 = GUINT16_FROM_BE (r16);
else if (endian == BIN_LITTLE_ENDIAN)
r16 = GUINT16_FROM_LE (r16);
return r16;
case 4:
r32 = *(guint32 *)data;
if (endian == BIN_BIG_ENDIAN)
r32 = GUINT32_FROM_BE (r32);
else if (endian == BIN_LITTLE_ENDIAN)
r32 = GUINT32_FROM_LE (r32);
return r32;
case 8:
r64 = *(guint64 *)data;
if (endian == BIN_BIG_ENDIAN)
r64 = GUINT64_FROM_BE (r64);
else if (endian == BIN_LITTLE_ENDIAN)
r64 = GUINT64_FROM_LE (r64);
return r64;
default:
g_assert_not_reached();
return 0;
}
}
guint64
bin_parser_get_uint (BinParser *parser, int width)
{
guint64 r = convert_uint (parser->data + parser->offset,
parser->endian,
width);
parser->offset += width;
return r;
}
guint64
bin_parser_get_uleb128 (BinParser *parser)
{
guint64 result = 0;
guint shift = 0;
guint8 byte;
do {
byte = *(const guint8 *) (parser->data + parser->offset++);
result |= ((guint64) (byte & 0x7f)) << shift;
shift += 7;
} while (byte & 0x80);
return result;
}
gint64
bin_parser_get_sleb128 (BinParser *parser)
{
guint64 result = 0;
guint shift = 0;
guint8 byte;
do {
byte = *(const guint8 *) (parser->data + parser->offset++);
result |= ((guint64) (byte & 0x7f)) << shift;
shift += 7;
} while (byte & 0x80);
if (shift < 64 && (byte & 0x40))
result |= (guint64) -1 << shift;
return (gint64) result;
}
const char *
bin_parser_get_string (BinParser *parser)
{
const char *result;
/* FIXME: check that the string is within the file */
result = (const char *) parser->data + parser->offset;
parser->offset += strlen (result) + 1;
return result;
}
guint64
bin_parser_get_uint_field (BinParser *parser,
BinRecord *record,
int field_ident)
{
const Field *field = &record->fields[field_ident];
const guchar *pos;
pos = parser->data + parser->offset + field->offset;
if (pos > parser->data + parser->length) {
/* FIXME: generate error */
return 0;
}
return convert_uint (pos, parser->endian, field->width);
}
void
bin_parser_advance (BinParser *parser, gint64 offset)
{
parser->offset += offset;
}
|