main.c: Remove unused variables
[memprof.git] / src / binparser.c
blob0a2498143eb0d51fe675c0af63f13e361bce7050
1 /* Sysprof -- Sampling, systemwide CPU profiler
2 * Copyright 2006, 2007, Soeren Sandmann
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include <string.h>
20 #include "binparser.h"
22 typedef struct Field Field;
24 struct BinParser
26 const guchar * data;
27 gsize length;
29 gsize offset;
30 const char * error_msg;
31 GList * records;
32 BinEndian endian;
34 gsize saved_offset;
37 struct Field
39 char name[BIN_MAX_NAME];
40 guint offset; /* from beginning of struct */
41 guint width;
42 BinType type;
45 struct BinRecord
47 int n_fields;
48 Field fields[1];
51 BinParser *
52 bin_parser_new (const guchar *data,
53 gsize length)
55 BinParser *parser = g_new0 (BinParser, 1);
57 parser->data = data;
58 parser->length = length;
59 parser->offset = 0;
60 parser->error_msg = NULL;
61 parser->records = NULL;
62 parser->endian = BIN_NATIVE_ENDIAN;
64 return parser;
67 void
68 bin_parser_free (BinParser *parser)
70 GList *list;
72 for (list = parser->records; list != NULL; list = list->next)
74 BinRecord *record = list->data;
76 g_free (record);
79 g_list_free (parser->records);
81 g_free (parser);
84 const guchar *
85 bin_parser_get_data (BinParser *parser)
87 return parser->data;
90 gsize
91 bin_parser_get_length (BinParser *parser)
93 return parser->length;
96 static guint64
97 align (guint64 offset, int alignment)
99 /* Note that we can speed this up by assuming alignment'
100 * is a power of two, since
102 * offset % alignment == offset & (alignemnt - 1)
106 if (offset % alignment != 0)
107 offset += (alignment - (offset % alignment));
109 return offset;
112 static int
113 get_align (const BinField *field)
115 if (field->type == BIN_UNINTERPRETED)
116 return 1;
117 else
118 return field->n_bytes;
121 BinRecord *
122 bin_parser_create_record (BinParser *parser,
123 const BinField *fields)
125 BinRecord *record;
126 int i, n_fields;
127 guint offset;
129 n_fields = 0;
130 while (fields[n_fields].name[0] != '\0')
132 n_fields++;
133 #if 0
134 g_print ("type: %d\n", fields[n_fields].type);
135 #endif
138 record = g_malloc0 (sizeof (BinRecord) +
139 (n_fields - 1) * sizeof (Field));
141 offset = 0;
142 record->n_fields = n_fields;
143 for (i = 0; i < n_fields; ++i)
145 const BinField *bin_field = &(fields[i]);
146 Field *field = &(record->fields[i]);
148 offset = align (offset, get_align (bin_field));
150 strncpy (field->name, bin_field->name, BIN_MAX_NAME - 1);
151 field->offset = offset;
152 field->type = bin_field->type;
153 field->width = bin_field->n_bytes;
155 offset += record->fields[i].width;
158 parser->records = g_list_prepend (parser->records, record);
160 return record;
163 gboolean
164 bin_parser_error (BinParser *parser)
166 return parser->error_msg != NULL;
169 void
170 bin_parser_clear_error (BinParser *parser)
172 parser->error_msg = NULL;
175 const gchar *
176 bin_parser_get_error_msg (BinParser *parser)
178 return parser->error_msg;
181 void
182 bin_parser_set_endian (BinParser *parser,
183 BinEndian endian)
185 parser->endian = endian;
188 /* Move current offset */
189 gsize
190 bin_parser_get_offset (BinParser *parser)
192 return parser->offset;
195 void
196 bin_parser_set_offset (BinParser *parser,
197 gsize offset)
199 parser->offset = offset;
202 void
203 bin_parser_align (BinParser *parser,
204 gsize byte_width)
206 parser->offset = align (parser->offset, byte_width);
209 gsize
210 bin_record_get_size (BinRecord *record)
212 Field *last_field = &(record->fields[record->n_fields - 1]);
213 Field *first_field = &(record->fields[0]);
215 /* align to first field, since that's the alignment of the record
216 * following this one
219 return align (last_field->offset + last_field->width, first_field->width);
222 void
223 bin_parser_seek_record (BinParser *parser,
224 BinRecord *record,
225 int n_records)
227 gsize record_size = bin_record_get_size (record);
229 parser->offset += record_size * n_records;
232 void
233 bin_parser_save (BinParser *parser)
235 parser->saved_offset = parser->offset;
238 void
239 bin_parser_restore (BinParser *parser)
241 parser->offset = parser->saved_offset;
244 /* retrieve data */
245 static guint64
246 convert_uint (const guchar *data,
247 BinEndian endian,
248 int width)
250 guint8 r8;
251 guint16 r16;
252 guint32 r32;
253 guint64 r64;
255 #if 0
256 if (width == 4)
257 g_print ("converting at %p %d %d %d %d\n", data, data[0], data[1], data[2], data[3]);
258 #endif
260 /* FIXME: check that we are within the file */
262 switch (width)
264 case 1:
265 r8 = *(guint8 *)data;
266 return r8;
268 case 2:
269 r16 = *(guint16 *)data;
271 if (endian == BIN_BIG_ENDIAN)
272 r16 = GUINT16_FROM_BE (r16);
273 else if (endian == BIN_LITTLE_ENDIAN)
274 r16 = GUINT16_FROM_LE (r16);
276 return r16;
278 case 4:
279 r32 = *(guint32 *)data;
281 if (endian == BIN_BIG_ENDIAN)
282 r32 = GUINT32_FROM_BE (r32);
283 else if (endian == BIN_LITTLE_ENDIAN)
284 r32 = GUINT32_FROM_LE (r32);
286 return r32;
288 case 8:
289 r64 = *(guint64 *)data;
291 if (endian == BIN_BIG_ENDIAN)
292 r64 = GUINT64_FROM_BE (r64);
293 else if (endian == BIN_LITTLE_ENDIAN)
294 r64 = GUINT64_FROM_LE (r64);
296 return r64;
298 default:
299 g_assert_not_reached();
300 return 0;
304 guint64
305 bin_parser_get_uint (BinParser *parser,
306 int width)
308 guint64 r = convert_uint (parser->data + parser->offset, parser->endian, width);
310 parser->offset += width;
312 return r;
315 const char *
316 bin_parser_get_string (BinParser *parser)
318 const char *result;
320 /* FIXME: check that the string is within the file */
322 result = (const char *)parser->data + parser->offset;
324 parser->offset += strlen (result) + 1;
326 return result;
330 static const Field *
331 get_field (BinRecord *format,
332 const gchar *name)
334 int i;
336 for (i = 0; i < format->n_fields; ++i)
338 Field *field = &(format->fields[i]);
340 if (strcmp (field->name, name) == 0)
342 #if 0
343 g_print ("found field: %s (offset: %d, type %d)\n", field->name, field->offset, field->type);
344 #endif
347 return field;
351 return NULL;
354 guint64
355 bin_parser_get_uint_field (BinParser *parser,
356 BinRecord *record,
357 const char *name)
359 const Field *field = get_field (record, name);
360 const guchar *pos;
362 #if 0
363 g_print ("moving to %d (%d + %d)\n", parser->offset + field->offset, parser->offset, field->offset);
364 #endif
366 pos = parser->data + parser->offset + field->offset;
368 #if 0
369 g_print (" record offset: %d\n", record->offset);
370 g_print (" record index: %d\n", record->index);
371 g_print (" field offset %d\n", field->offset);
372 #endif
374 if (pos > parser->data + parser->length)
376 /* FIXME: generate error */
377 return 0;
380 #if 0
381 g_print (" uint %d at %p => %d\n", field->width, pos, convert_uint (pos, record->format->big_endian, field->width));
382 #endif
384 return convert_uint (pos, parser->endian, field->width);