Heraia  0.1.8
decode.c File Reference

This file include all functions that may help in decoding a binary stream to something else such as numbers, dates, other binary stream and so on. More...

#include <libheraia.h>
Include dependency graph for decode.c:

Go to the source code of this file.

Functions

static gboolean bissextile_year (guint32 year)
 Says whether a year is a leap one or not. More...
 
static void calc_which_month_day (date_and_time_t *mydate, guint32 day, guint tab_ns_months[12])
 Says, from a number of days (eg 154), which month it is (eg may) and which day in the corresponding month (eg 2 (leap year) or 3) More...
 
static void which_month_day (date_and_time_t *mydate, guint32 day, gboolean bi)
 Front end function for the calc_which_month_day function ! More...
 
static guint32 remove_days_from_first_january (guint32 base_year, guint8 base_month, guint8 base_day)
 Returns the number of days since 01/01/base_year eg 15/02/base_year –> 31 + 15 = 46. More...
 
static void which_year_month_day (date_and_time_t *mydate, guint32 days, guint32 base_year, guint base_month, guint8 base_day)
 About date calculation : Leap years are periods of 4 years except the years that we can divide by 100 and not 400. More...
 
static void make_date_and_time (date_and_time_t *mydate, guchar *data, guint8 len, guint64 nbticks, guint32 base_year, guint base_month, guint8 base_day)
 Reads the data from the stream (specified length !! <= 8 or 64 bits to decode) More...
 
static void transform_bcd_to_human (gchar *bcd, guint8 part, guint8 part_number)
 transcribes the bcd number "part" into a More...
 
gchar * decode_8bits_signed (guchar *data, gpointer data_struct)
 General purpose of this function is to take a 1 byte data stream and convert it as if it is an 8 bits signed number. More...
 
gchar * decode_8bits_unsigned (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 1 byte data stream and convert it as if it is an 8 bits unsigned number More...
 
gchar * decode_16bits_signed (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 2 byte data stream and convert it as if it is a 16 bits signed number More...
 
gchar * decode_16bits_unsigned (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 2 byte data stream and convert it as if it is a 16 bits unsigned number More...
 
gchar * decode_32bits_signed (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 4 byte data stream and convert it as if it is a 32 bits signed number More...
 
gchar * decode_32bits_unsigned (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 4 byte data stream and convert it as if it is a 32 bits unsigned number More...
 
gchar * decode_64bits_signed (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 8 byte data stream and convert it as if it is a 64 bits signed number More...
 
gchar * decode_64bits_unsigned (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 8 byte data stream and convert it as if it is a 64 bits unsigned number More...
 
gchar * decode_float_normal (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 4 byte data stream and convert it as if it is a float number normal notation More...
 
gchar * decode_float_scientific (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 4 byte data stream and convert it as if it is a float number normal notation More...
 
gchar * decode_double_normal (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 8 byte data stream and convert it as if it is a float number normal notation More...
 
gchar * decode_double_scientific (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 8 byte data stream and convert it as if it is a float number normal notation More...
 
static gchar * date_printf (date_and_time_t *mydate)
 Return a gchar * that contains the date and time encoded from the values contained in the date_and_time_t structure it may be freed when no longer needed We do not use any of the g_date_strftime or strftime function because interpreted dates are not always valid ! More...
 
gchar * decode_dos_date (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 4 byte data stream and convert it as if it is a dos date. More...
 
gchar * decode_filetime_date (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 8 byte data stream and convert it as if it is a filetime date. More...
 
gchar * decode_C_date (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 4 byte data stream and convert it as if it is a C date. More...
 
gchar * decode_HFS_date (guchar *data, gpointer data_struct)
 general purpose of this function is to take a 4 byte data stream and convert it as if it is a HFS date. More...
 
static guchar data_to_one_bit (guchar data)
 Returns a guchar that is 1 or 0 respectiveky if data is > 0 or not. More...
 
gchar * decode_to_bits (guchar *data, gpointer data_struct)
 decodes the stream represented by *data (one byte) to a string containing eight 0 or 1 (Little Endian style) More...
 
gchar * decode_packed_BCD (guchar *data, gpointer data_struct)
 Decode one byte as a Packed BCD (Binary Coded Decimal) and return a gchar* that may be freed when no longer needed. More...
 
gboolean swap_bytes (guchar *to_swap, guint first, guint last)
 Swap bytes from the buffer to_swap. More...
 
void reverse_byte_order (guchar *to_reverse)
 Reverse the byte order LSB -> MSB in MSB -> LSB 12345678 in 87654321. More...
 
decode_parameters_tnew_decode_parameters_t (guint endianness, guint stream_size)
 Make an new decode_parameters_t in order to pass to the functions. More...
 
decode_tnew_decode_t (DecodeFunc decode_func, GtkWidget *entry, const gchar *err_msg)
 Make a new decode_t structure. More...
 
decode_generic_tnew_decode_generic_t (const gchar *label, guint data_size, gboolean fixed_size, const gchar *err_msg, guint nb_cols,...)
 Make a new decode_generic_t structure and creates the associated widgets. More...
 

Detailed Description

This file include all functions that may help in decoding a binary stream to something else such as numbers, dates, other binary stream and so on.

Todo:
I see, while putting some functions documentation that there may be things to do to improve speed with date calculations -> should we fill tab_ns_months at every function calls ? This may not be necessary

Definition in file decode.c.

Function Documentation

static gboolean bissextile_year ( guint32  year)
static

Says whether a year is a leap one or not.

Parameters
year: a guint32 representing a year such as 2009
Returns
TRUE if it's a leap year FALSE instead.

Definition at line 323 of file decode.c.

Referenced by remove_days_from_first_january(), and which_year_month_day().

Here is the caller graph for this function:

static void calc_which_month_day ( date_and_time_t mydate,
guint32  day,
guint  tab_ns_months[12] 
)
static

Says, from a number of days (eg 154), which month it is (eg may) and which day in the corresponding month (eg 2 (leap year) or 3)

Parameters
[out]mydate: Filled date structure
day: guint32 representing the number of day in the year (1..365/366)
tab_ns_months: an array filled with the cumulative number of days for each month (31 to 365/366)

Definition at line 357 of file decode.c.

References date_and_time_t::day, and date_and_time_t::month.

Referenced by which_month_day().

Here is the caller graph for this function:

static guchar data_to_one_bit ( guchar  data)
static

Returns a guchar that is 1 or 0 respectiveky if data is > 0 or not.

Parameters
data: a value to analyse
Returns
: 1 or 0 f data is > 0 or not

Definition at line 718 of file decode.c.

Referenced by decode_to_bits().

Here is the caller graph for this function:

static gchar* date_printf ( date_and_time_t mydate)
static

Return a gchar * that contains the date and time encoded from the values contained in the date_and_time_t structure it may be freed when no longer needed We do not use any of the g_date_strftime or strftime function because interpreted dates are not always valid !

Parameters
[in]mydate: structure that contains a date
Returns
returns a gchar * printed out in a french like format "dd/mm/aaaa - hh:mm:ss"
Todo:
Add a way to use a user defined format and/or predefined formats

Definition at line 549 of file decode.c.

References date_and_time_t::day, date_and_time_t::hour, date_and_time_t::minutes, date_and_time_t::month, date_and_time_t::seconds, and date_and_time_t::year.

Referenced by decode_C_date(), decode_dos_date(), decode_filetime_date(), and decode_HFS_date().

Here is the caller graph for this function:

gchar* decode_16bits_signed ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 2 byte data stream and convert it as if it is a 16 bits signed number

Parameters
data: 2 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 95 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_16bits_unsigned ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 2 byte data stream and convert it as if it is a 16 bits unsigned number

Parameters
data: 2 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 118 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_32bits_signed ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 4 byte data stream and convert it as if it is a 32 bits signed number

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 141 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_32bits_unsigned ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 4 byte data stream and convert it as if it is a 32 bits unsigned number

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 164 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_64bits_signed ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 8 byte data stream and convert it as if it is a 64 bits signed number

Parameters
data: 8 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 187 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_64bits_unsigned ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 8 byte data stream and convert it as if it is a 64 bits unsigned number

Parameters
data: 8 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 210 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_8bits_signed ( guchar *  data,
gpointer  data_struct 
)

General purpose of this function is to take a 1 byte data stream and convert it as if it is an 8 bits signed number.

Parameters
data: 1 guchar
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 49 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_8bits_unsigned ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 1 byte data stream and convert it as if it is an 8 bits unsigned number

Parameters
data: 1 guchar
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 72 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_C_date ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 4 byte data stream and convert it as if it is a C date.

If it is not, the result may be funny ! Counting seconds from 01/01/1970

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

< date resulting of interpretation

< text that is the result of date interpretation

Definition at line 659 of file decode.c.

References date_printf(), and make_date_and_time().

Referenced by add_default_tabs().

Here is the call graph for this function:

Here is the caller graph for this function:

gchar* decode_dos_date ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 4 byte data stream and convert it as if it is a dos date.

If it is not, the result may be funny !

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

< date resulting of interpretation

< text that is the result of date interpretation

Definition at line 563 of file decode.c.

References date_printf(), date_and_time_t::day, date_and_time_t::hour, date_and_time_t::minutes, date_and_time_t::month, date_and_time_t::seconds, and date_and_time_t::year.

Referenced by add_default_tabs().

Here is the call graph for this function:

Here is the caller graph for this function:

gchar* decode_double_normal ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 8 byte data stream and convert it as if it is a float number normal notation

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 279 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_double_scientific ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 8 byte data stream and convert it as if it is a float number normal notation

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 302 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_filetime_date ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 8 byte data stream and convert it as if it is a filetime date.

If it is not, the result may be funny ! Counting 100th of nanoseconds from 01/01/1601

Parameters
data: 8 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

< date resulting of interpretation

< text that is the result of date interpretation

Definition at line 628 of file decode.c.

References date_printf(), and make_date_and_time().

Referenced by add_default_tabs().

Here is the call graph for this function:

Here is the caller graph for this function:

gchar* decode_float_normal ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 4 byte data stream and convert it as if it is a float number normal notation

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 233 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_float_scientific ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 4 byte data stream and convert it as if it is a float number normal notation

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar * that may be freed when no longer needed

Definition at line 256 of file decode.c.

Referenced by add_default_tabs(), and test_decode_functions().

Here is the caller graph for this function:

gchar* decode_HFS_date ( guchar *  data,
gpointer  data_struct 
)

general purpose of this function is to take a 4 byte data stream and convert it as if it is a HFS date.

If it is not, the result may be funny ! Counting seconds 01/01/1904

Parameters
data: 4 guchars
data_structa pointer to a user defined data structure
Returns
returns a gchar* that may be freed when no longer needed

< date resulting of interpretation

< text that is the result of date interpretation

Definition at line 690 of file decode.c.

References date_printf(), and make_date_and_time().

Referenced by add_default_tabs().

Here is the call graph for this function:

Here is the caller graph for this function:

gchar* decode_packed_BCD ( guchar *  data,
gpointer  data_struct 
)

Decode one byte as a Packed BCD (Binary Coded Decimal) and return a gchar* that may be freed when no longer needed.

Parameters
data: stream to decode as 1 guchar
data_structa pointer to a user defined data structure
Returns
returns a gchar * that contain the packed BCD interpretation

Definition at line 872 of file decode.c.

References decode_parameters_t::stream_size, and transform_bcd_to_human().

Referenced by add_default_tabs().

Here is the call graph for this function:

Here is the caller graph for this function:

gchar* decode_to_bits ( guchar *  data,
gpointer  data_struct 
)

decodes the stream represented by *data (one byte) to a string containing eight 0 or 1 (Little Endian style)

Parameters
data: 1 guchar
data_structa pointer to a user defined data structure
Returns
n*9 gchars that are either '1' or '0' or ' ' (as a separator), the string may be freed when no longer needed

Definition at line 739 of file decode.c.

References data_to_one_bit(), and decode_parameters_t::stream_size.

Referenced by add_default_tabs().

Here is the call graph for this function:

Here is the caller graph for this function:

static void make_date_and_time ( date_and_time_t mydate,
guchar *  data,
guint8  len,
guint64  nbticks,
guint32  base_year,
guint  base_month,
guint8  base_day 
)
static

Reads the data from the stream (specified length !! <= 8 or 64 bits to decode)

Parameters
[out]mydate: date_and_time_t * structure that contain the resulting date
data: a guchar * as a stream to decode as a date
len: guint8 as length of the stream in bytes (must be <= 8 or 64 bits to decode)
nbticks: guint64 that tells the number of ticks per seconds (1, 1000, ...)
base_year: guint32, Epoch year (1970, 1904, ...)
base_month: guint32, Epoch month (january, ...)
base_day: guint32 Epoch day (01, 15, ...)
Returns
populates the date_and_time_t structure (my_date)

Definition at line 602 of file decode.c.

References date_and_time_t::hour, date_and_time_t::minutes, date_and_time_t::seconds, and which_year_month_day().

Referenced by decode_C_date(), decode_filetime_date(), and decode_HFS_date().

Here is the call graph for this function:

Here is the caller graph for this function:

decode_generic_t* new_decode_generic_t ( const gchar *  label,
guint  data_size,
gboolean  fixed_size,
const gchar *  err_msg,
guint  nb_cols,
  ... 
)

Make a new decode_generic_t structure and creates the associated widgets.

Parameters
label: the label for this row
data_size: a default data_size
fixed_size: TRUE if the size is fixed and should not be updated, FALSE otherwise
err_msg: an error message to be displayed if dcoding can not be processed
nb_cols: number of decoding columns we want
...: va_list of functions to fill in the columns (you MUST have the same number of columns and functions you passes here as arguments)
Returns
returns a newly allocated decode_generic_t structure filled with the right parameters

< va_list arguments : decoding function names

structure to be initialized and returned

Entry and associated function

< one decoding function

< entry associated to the decoding function

< To keep track of those couples

Definition at line 1018 of file decode.c.

References decode_generic_t::data_size, decode_generic_t::decode_array, decode_generic_t::fixed_size, decode_generic_t::label, and new_decode_t().

Referenced by add_default_tabs().

Here is the call graph for this function:

Here is the caller graph for this function:

decode_parameters_t* new_decode_parameters_t ( guint  endianness,
guint  stream_size 
)

Make an new decode_parameters_t in order to pass to the functions.

Parameters
endianness: endianness as setup in data interpertor's window
stream_size: stream size as setup with the spin button
Returns
returns a newly allocated decode_parameters_t structure which may be freed when no longer needed

Definition at line 961 of file decode.c.

References decode_parameters_t::endianness, and decode_parameters_t::stream_size.

Referenced by fdft_search_direction(), and refresh_data_interpretor_window().

Here is the caller graph for this function:

decode_t* new_decode_t ( DecodeFunc  decode_func,
GtkWidget *  entry,
const gchar *  err_msg 
)

Make a new decode_t structure.

Parameters
decode_func: pointer to a function that may decode a stream this function must follow DecodeFunc prototype
entry: A GtkWidget entry that will receive the result of the decoding function
Returns
returns a newly allocated decode_t structure filled with the two parameters.

Definition at line 983 of file decode.c.

References decode_t::entry, decode_t::err_msg, and decode_t::func.

Referenced by new_decode_generic_t().

Here is the caller graph for this function:

static guint32 remove_days_from_first_january ( guint32  base_year,
guint8  base_month,
guint8  base_day 
)
static

Returns the number of days since 01/01/base_year eg 15/02/base_year –> 31 + 15 = 46.

Parameters
base_year: year where we want to begin the calculation
base_month: month (in the example 02)
base_day: day in the month (from the example 15)
Returns
returns a number of days since begining of the year (eg 1..365/366)

Definition at line 425 of file decode.c.

References bissextile_year().

Referenced by which_year_month_day().

Here is the call graph for this function:

Here is the caller graph for this function:

void reverse_byte_order ( guchar *  to_reverse)

Reverse the byte order LSB -> MSB in MSB -> LSB 12345678 in 87654321.

Parameters
[in,out]to_reverse: one guchar to be reversed

Definition at line 936 of file decode.c.

Referenced by change_endianness().

Here is the caller graph for this function:

gboolean swap_bytes ( guchar *  to_swap,
guint  first,
guint  last 
)

Swap bytes from the buffer to_swap.

Warning
recursive function !! Call with first = 0 and last = last byte of buffer to swap
Parameters
[in,out]to_swap: buffer to swap
first: first byte in the buffer to swap
last: last byte in the buffer to swap
Returns
returns TRUE when first is >= to last and this end recursivity

Definition at line 913 of file decode.c.

References swap_bytes().

Referenced by change_endianness(), and swap_bytes().

Here is the call graph for this function:

Here is the caller graph for this function:

static void transform_bcd_to_human ( gchar *  bcd,
guint8  part,
guint8  part_number 
)
static

transcribes the bcd number "part" into a

Parameters
[out]bcd: gchar * human readable string
part: guint8 as an half byte to decode
part_number: 0 or 1 as MSB and LSB Coding style is from ETSI GSM 04.08 ETS 300557 p387
Todo:
give choice of coding style (eg for numbers >=10)

Definition at line 805 of file decode.c.

Referenced by decode_packed_BCD().

Here is the caller graph for this function:

static void which_month_day ( date_and_time_t mydate,
guint32  day,
gboolean  bi 
)
static

Front end function for the calc_which_month_day function !

Parameters
[out]mydate: Filled date structure
day: guint32 representing the number of day in the year (1..365/366)
bi: TRUE if it's a leap year, FALSE instead

Definition at line 385 of file decode.c.

References calc_which_month_day(), date_and_time_t::day, and date_and_time_t::month.

Referenced by which_year_month_day().

Here is the call graph for this function:

Here is the caller graph for this function:

static void which_year_month_day ( date_and_time_t mydate,
guint32  days,
guint32  base_year,
guint  base_month,
guint8  base_day 
)
static

About date calculation : Leap years are periods of 4 years except the years that we can divide by 100 and not 400.

So we can distinguish 2 periods : one of 400 years and one of 4 years.

  • we have 100 bissextiles years in a period of 400 years this means that every 400 years we have exactly 146100 days.
  • we have 1 bissextile year every 4 years : this means that we have exactly 1461 days every 4 years. As we can calculate exactly the number of days in a filetime or C_date format, we could calculate exactly the number of periods of 400 years and then the number of periods of 4 years.
    Parameters
    [out]mydate: Filled date structure
    days: number of days calculated
    base_year: base year used for calculation (eg 1601 for filetime)
    base_month: base month used for calculation (eg 01, january for filetime)
    base_day: base day used for calculation (eg 01 for filetime)

Definition at line 498 of file decode.c.

References bissextile_year(), remove_days_from_first_january(), which_month_day(), and date_and_time_t::year.

Referenced by make_date_and_time().

Here is the call graph for this function:

Here is the caller graph for this function: