mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2026-02-11 23:51:06 +00:00
Add new bin_record() functions Implement them.
2006-08-26 Soren Sandmann <sandmann@daimi.au.dk> * binparser.h: Add new bin_record() functions * binparser.c: Implement them.
This commit is contained in:
committed by
Søren Sandmann Pedersen
parent
62c86a1951
commit
b569c3a34c
@ -1,3 +1,8 @@
|
|||||||
|
2006-08-26 Soren Sandmann <sandmann@daimi.au.dk>
|
||||||
|
|
||||||
|
* binparser.h: Add new bin_record() functions
|
||||||
|
* binparser.c: Implement them.
|
||||||
|
|
||||||
2006-08-22 Soren Sandmann <sandmann@daimi.au.dk>
|
2006-08-22 Soren Sandmann <sandmann@daimi.au.dk>
|
||||||
|
|
||||||
* collector.c (unique_dup): Demangle instead of strdup.
|
* collector.c (unique_dup): Demangle instead of strdup.
|
||||||
|
|||||||
163
binparser.c
163
binparser.c
@ -15,6 +15,14 @@ struct ParserFrame
|
|||||||
ParserFrame * next;
|
ParserFrame * next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct BinRecord
|
||||||
|
{
|
||||||
|
BinFormat * format;
|
||||||
|
int index;
|
||||||
|
gsize offset;
|
||||||
|
BinParser * parser;
|
||||||
|
};
|
||||||
|
|
||||||
struct BinField
|
struct BinField
|
||||||
{
|
{
|
||||||
guint64 offset;
|
guint64 offset;
|
||||||
@ -165,16 +173,65 @@ get_field (BinFormat *format,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
guint64
|
||||||
|
convert_uint (const guchar *data,
|
||||||
|
gboolean big_endian,
|
||||||
|
int width)
|
||||||
|
{
|
||||||
|
guint8 r8;
|
||||||
|
guint16 r16;
|
||||||
|
guint32 r32;
|
||||||
|
guint64 r64;
|
||||||
|
|
||||||
|
switch (width)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
r8 = *(guint8 *)data;
|
||||||
|
return r8;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
r16 = *(guint16 *)data;
|
||||||
|
|
||||||
|
if (big_endian)
|
||||||
|
r16 = GUINT16_FROM_BE (r16);
|
||||||
|
else
|
||||||
|
r16 = GUINT16_FROM_LE (r16);
|
||||||
|
|
||||||
|
return r16;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
r32 = *(guint32 *)data;
|
||||||
|
|
||||||
|
if (big_endian)
|
||||||
|
r32 = GUINT32_FROM_BE (r32);
|
||||||
|
else
|
||||||
|
r32 = GUINT32_FROM_LE (r32);
|
||||||
|
|
||||||
|
return r32;
|
||||||
|
|
||||||
|
case 8:
|
||||||
|
r64 = *(guint64 *)data;
|
||||||
|
|
||||||
|
if (big_endian)
|
||||||
|
r64 = GUINT64_FROM_BE (r64);
|
||||||
|
else
|
||||||
|
r64 = GUINT64_FROM_LE (r64);
|
||||||
|
|
||||||
|
return r64;
|
||||||
|
|
||||||
|
default:
|
||||||
|
g_assert_not_reached();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
guint64
|
guint64
|
||||||
bin_parser_get_uint (BinParser *parser,
|
bin_parser_get_uint (BinParser *parser,
|
||||||
const gchar *name)
|
const gchar *name)
|
||||||
{
|
{
|
||||||
const BinField *field;
|
const BinField *field;
|
||||||
const guint8 *pos;
|
const guint8 *pos;
|
||||||
guint8 r8;
|
|
||||||
guint16 r16;
|
|
||||||
guint32 r32;
|
|
||||||
guint64 r64;
|
|
||||||
BinFormat *format;
|
BinFormat *format;
|
||||||
const guchar *data;
|
const guchar *data;
|
||||||
|
|
||||||
@ -195,50 +252,7 @@ bin_parser_get_uint (BinParser *parser,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
return convert_uint (pos, format->big_endian, field->width);
|
||||||
g_print ("getting %s from offset: %llu\n", name, field->offset);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
switch (field->width)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
r8 = *(guint8 *)pos;
|
|
||||||
return r8;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
r16 = *(guint16 *)pos;
|
|
||||||
|
|
||||||
if (format->big_endian)
|
|
||||||
r16 = GUINT16_FROM_BE (r16);
|
|
||||||
else
|
|
||||||
r16 = GUINT16_FROM_LE (r16);
|
|
||||||
|
|
||||||
return r16;
|
|
||||||
|
|
||||||
case 4:
|
|
||||||
r32 = *(guint32 *)pos;
|
|
||||||
|
|
||||||
if (format->big_endian)
|
|
||||||
r32 = GUINT32_FROM_BE (r32);
|
|
||||||
else
|
|
||||||
r32 = GUINT32_FROM_LE (r32);
|
|
||||||
|
|
||||||
return r32;
|
|
||||||
|
|
||||||
case 8:
|
|
||||||
r64 = *(guint64 *)pos;
|
|
||||||
|
|
||||||
if (format->big_endian)
|
|
||||||
r64 = GUINT64_FROM_BE (r64);
|
|
||||||
else
|
|
||||||
r64 = GUINT64_FROM_LE (r64);
|
|
||||||
|
|
||||||
return r64;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
g_print ("width: %d\n", field->width);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
g_assert_not_reached();
|
g_assert_not_reached();
|
||||||
return 0;
|
return 0;
|
||||||
@ -345,6 +359,59 @@ bin_parser_get_length (BinParser *parser)
|
|||||||
return parser->length;
|
return parser->length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Record */
|
||||||
|
BinRecord *
|
||||||
|
bin_parser_get_record (BinParser *parser,
|
||||||
|
BinFormat *format,
|
||||||
|
gsize offset)
|
||||||
|
{
|
||||||
|
BinRecord *record = g_new0 (BinRecord, 1);
|
||||||
|
record->parser = parser;
|
||||||
|
record->index = 0;
|
||||||
|
record->offset = offset;
|
||||||
|
record->format = format;
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
bin_record_free (BinRecord *record)
|
||||||
|
{
|
||||||
|
g_free (record);
|
||||||
|
}
|
||||||
|
|
||||||
|
guint64
|
||||||
|
bin_record_get_uint (BinRecord *record,
|
||||||
|
const char *name)
|
||||||
|
{
|
||||||
|
const guint8 *pos;
|
||||||
|
const BinField *field;
|
||||||
|
|
||||||
|
field = get_field (record->format, name);
|
||||||
|
pos = record->parser->data + record->offset + field->offset;
|
||||||
|
|
||||||
|
if (record->offset + field->offset + field->width > record->parser->length)
|
||||||
|
{
|
||||||
|
/* FIXME: generate error */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return convert_uint (pos, record->format->big_endian, field->width);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
bin_record_index (BinRecord *record,
|
||||||
|
int index)
|
||||||
|
{
|
||||||
|
gsize format_size = bin_format_get_size (record->format);
|
||||||
|
|
||||||
|
record->offset -= record->index * format_size;
|
||||||
|
record->offset += index * format_size;
|
||||||
|
record->index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Fields */
|
||||||
|
|
||||||
BinField *
|
BinField *
|
||||||
bin_field_new_fixed_array (int n_elements,
|
bin_field_new_fixed_array (int n_elements,
|
||||||
int element_size)
|
int element_size)
|
||||||
|
|||||||
11
binparser.h
11
binparser.h
@ -3,6 +3,7 @@
|
|||||||
typedef struct BinField BinField;
|
typedef struct BinField BinField;
|
||||||
typedef struct BinFormat BinFormat;
|
typedef struct BinFormat BinFormat;
|
||||||
typedef struct BinParser BinParser;
|
typedef struct BinParser BinParser;
|
||||||
|
typedef struct BinRecord BinRecord;
|
||||||
|
|
||||||
/* BinParser */
|
/* BinParser */
|
||||||
BinParser *bin_parser_new (const guchar *data,
|
BinParser *bin_parser_new (const guchar *data,
|
||||||
@ -19,6 +20,16 @@ const char *bin_parser_get_string (BinParser *parser);
|
|||||||
guint64 bin_parser_get_uint (BinParser *parser,
|
guint64 bin_parser_get_uint (BinParser *parser,
|
||||||
const gchar *name);
|
const gchar *name);
|
||||||
|
|
||||||
|
/* Record */
|
||||||
|
BinRecord *bin_parser_get_record (BinParser *parser,
|
||||||
|
BinFormat *format,
|
||||||
|
gsize offset);
|
||||||
|
void bin_record_free (BinRecord *record);
|
||||||
|
guint64 bin_record_get_uint (BinRecord *record,
|
||||||
|
const char *name);
|
||||||
|
void bin_record_index (BinRecord *record,
|
||||||
|
int index);
|
||||||
|
|
||||||
/* BinFormat */
|
/* BinFormat */
|
||||||
BinFormat *bin_format_new (gboolean big_endian,
|
BinFormat *bin_format_new (gboolean big_endian,
|
||||||
const char *name, BinField *field,
|
const char *name, BinField *field,
|
||||||
|
|||||||
Reference in New Issue
Block a user