From 9f09c5741b7644342f0165f52715cc8b0b76ef83 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sun, 17 Sep 2017 10:12:30 +0200 Subject: [PATCH] Add function to swap bytes of the fields of a structure This function will be used to convert from one endianness type (little/big endian) to the other. Signed-off-by: Sebastien GODARD --- sa.h | 2 ++ sa_common.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/sa.h b/sa.h index f83314c..88bd38e 100644 --- a/sa.h +++ b/sa.h @@ -1203,5 +1203,7 @@ void set_hdr_rectime (unsigned int, struct tm *, struct file_header *); void set_record_timestamp_string (unsigned int, struct record_header *, char *, char *, int, struct tm *); +void swap_struct + (int [], void *, int); #endif /* _SA_H */ diff --git a/sa_common.c b/sa_common.c index 82c0f41..87d16e8 100644 --- a/sa_common.c +++ b/sa_common.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include /* For STDOUT_FILENO, among others */ @@ -977,6 +978,54 @@ void select_default_activity(struct activity *act[]) } } +/* + *************************************************************************** + * Swap bytes for every numerical field in structure. Used to convert from + * one endianness type (big-endian or little-endian) to the other. + * + * IN: + * @types_nr Number of fields in structure for each following types: + * unsigned long long, unsigned long and int. + * @ps Pointer on structure. + * @is64bit TRUE if data come from a 64-bit machine. + *************************************************************************** + */ +void swap_struct(int types_nr[], void *ps, int is64bit) +{ + int i; + uint64_t *x; + uint32_t *y; + + x = (uint64_t *) ps; + /* For each field of type long long (or double) */ + for (i = 0; i < types_nr[0]; i++) { + *x = __builtin_bswap64(*x); + x = (uint64_t *) ((char *) x + ULL_ALIGNMENT_WIDTH); + } + + y = (uint32_t *) x; + /* For each field of type long */ + for (i = 0; i < types_nr[1]; i++) { + if (is64bit) { + *x = __builtin_bswap64(*x); + x = (uint64_t *) ((char *) x + UL_ALIGNMENT_WIDTH); + } + else { + *y = __builtin_bswap32(*y); + y = (uint32_t *) ((char *) y + UL_ALIGNMENT_WIDTH); + } + } + + if (is64bit) { + y = (uint32_t *) x; + } + /* For each field of type int */ + for (i = 0; i < types_nr[2]; i++) { + *y = __builtin_bswap32(*y); + y = (uint32_t *) ((char *) y + U_ALIGNMENT_WIDTH); + } +} + /* *************************************************************************** * Read data from a system activity data file. -- 2.40.0