From fddbc09c4a9125fcb168fb31ff300d4132919ea6 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 27 Jan 2005 23:39:26 +0000 Subject: 2005-01-27 Havoc Pennington * dbus/dbus-message.c: byteswap the message if you init an iterator to read/write from it * dbus/dbus-marshal-byteswap.c: new file implementing _dbus_marshal_byteswap() * dbus/dbus-marshal-basic.c: add _dbus_swap_array() --- dbus/dbus-marshal-basic.c | 77 ++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 28 deletions(-) (limited to 'dbus/dbus-marshal-basic.c') diff --git a/dbus/dbus-marshal-basic.c b/dbus/dbus-marshal-basic.c index 5cb43b88..cd5d4e45 100644 --- a/dbus/dbus-marshal-basic.c +++ b/dbus/dbus-marshal-basic.c @@ -820,6 +820,53 @@ marshal_1_octets_array (DBusString *str, return TRUE; } +/** + * Swaps the elements of an array to the opposite byte order + * + * @param data start of array + * @param n_elements number of elements + * @param alignment size of each element + */ +void +_dbus_swap_array (unsigned char *data, + int n_elements, + int alignment) +{ + unsigned char *d; + unsigned char *end; + + _dbus_assert (_DBUS_ALIGN_ADDRESS (data, alignment) == data); + + /* we use const_data and cast it off so DBusString can be a const string + * for the unit tests. don't ask. + */ + d = data; + end = d + (n_elements * alignment); + + if (alignment == 8) + { + while (d != end) + { +#ifdef DBUS_HAVE_INT64 + *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d)); +#else + swap_8_bytes ((DBusBasicValue*) d); +#endif + d += 8; + } + } + else + { + _dbus_assert (alignment == 4); + + while (d != end) + { + *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d)); + d += 4; + } + } +} + static void swap_array (DBusString *str, int array_start, @@ -831,37 +878,11 @@ swap_array (DBusString *str, if (byte_order != DBUS_COMPILER_BYTE_ORDER) { - unsigned char *d; - unsigned char *end; - /* we use const_data and cast it off so DBusString can be a const string * for the unit tests. don't ask. */ - d = (unsigned char*) _dbus_string_get_const_data (str) + array_start; - end = d + n_elements * alignment; - - if (alignment == 8) - { - while (d != end) - { -#ifdef DBUS_HAVE_INT64 - *((dbus_uint64_t*)d) = DBUS_UINT64_SWAP_LE_BE (*((dbus_uint64_t*)d)); -#else - swap_8_bytes ((DBusBasicValue*) d); -#endif - d += 8; - } - } - else - { - _dbus_assert (alignment == 4); - - while (d != end) - { - *((dbus_uint32_t*)d) = DBUS_UINT32_SWAP_LE_BE (*((dbus_uint32_t*)d)); - d += 4; - } - } + _dbus_swap_array ((unsigned char*) (_dbus_string_get_const_data (str) + array_start), + n_elements, alignment); } } -- cgit