diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2012-07-04 15:04:29 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-07-04 16:00:39 +0200 |
commit | a966f1c37b2573594a6963f13015fddd645e96c3 (patch) | |
tree | b70bd4015b5f6e2f62ccb4f431f210f55e75ef49 | |
parent | 8b1edc1038883541b8fdfd7265d5b2771d168f96 (diff) | |
download | external_libqmi-a966f1c37b2573594a6963f13015fddd645e96c3.zip external_libqmi-a966f1c37b2573594a6963f13015fddd645e96c3.tar.gz external_libqmi-a966f1c37b2573594a6963f13015fddd645e96c3.tar.bz2 |
libqmi-glib,utils: new read/write methods for uint types of arbitrary size
The new qmi_utils_(read|write)_sized_guint_(from|to)_buffer() methods allow
reading unsigned integers of non-standard sizes, like for example 6 bytes. The
read/written number is always provided as a guint64 value.
-rw-r--r-- | libqmi-glib/qmi-utils.c | 40 | ||||
-rw-r--r-- | libqmi-glib/qmi-utils.h | 10 | ||||
-rw-r--r-- | libqmi-glib/test/test-utils.c | 54 |
3 files changed, 104 insertions, 0 deletions
diff --git a/libqmi-glib/qmi-utils.c b/libqmi-glib/qmi-utils.c index 0e99469..39cf285 100644 --- a/libqmi-glib/qmi-utils.c +++ b/libqmi-glib/qmi-utils.c @@ -190,6 +190,26 @@ qmi_utils_read_gint64_from_buffer (guint8 **buffer, } void +qmi_utils_read_sized_guint_from_buffer (guint8 **buffer, + guint16 *buffer_size, + guint n_bytes, + guint64 *out) +{ + guint64 tmp = 0; + + g_assert (out != NULL); + g_assert (buffer != NULL); + g_assert (buffer_size != NULL); + g_assert (*buffer_size >= n_bytes); + + memcpy (&tmp, *buffer, n_bytes); + *out = GUINT64_FROM_LE (tmp); + + *buffer = &((*buffer)[n_bytes]); + *buffer_size = (*buffer_size) - n_bytes; +} + +void qmi_utils_write_guint8_to_buffer (guint8 **buffer, guint16 *buffer_size, guint8 *in) @@ -318,6 +338,26 @@ qmi_utils_write_gint64_to_buffer (guint8 **buffer, } void +qmi_utils_write_sized_guint_to_buffer (guint8 **buffer, + guint16 *buffer_size, + guint n_bytes, + guint64 *in) +{ + guint64 tmp; + + g_assert (in != NULL); + g_assert (buffer != NULL); + g_assert (buffer_size != NULL); + g_assert (*buffer_size >= n_bytes); + + tmp = GUINT64_TO_LE (*in); + memcpy (*buffer, &tmp, n_bytes); + + *buffer = &((*buffer)[n_bytes]); + *buffer_size = (*buffer_size) - n_bytes; +} + +void qmi_utils_read_string_from_buffer (guint8 **buffer, guint16 *buffer_size, gboolean length_prefix, diff --git a/libqmi-glib/qmi-utils.h b/libqmi-glib/qmi-utils.h index 8feda22..d1dc0d4 100644 --- a/libqmi-glib/qmi-utils.h +++ b/libqmi-glib/qmi-utils.h @@ -64,6 +64,11 @@ void qmi_utils_read_gint64_from_buffer (guint8 **buffer, guint16 *buffer_size, gint64 *out); +void qmi_utils_read_sized_guint_from_buffer (guint8 **buffer, + guint16 *buffer_size, + guint n_bytes, + guint64 *out); + void qmi_utils_write_guint8_to_buffer (guint8 **buffer, guint16 *buffer_size, guint8 *in); @@ -92,6 +97,11 @@ void qmi_utils_write_gint64_to_buffer (guint8 **buffer, guint16 *buffer_size, gint64 *in); +void qmi_utils_write_sized_guint_to_buffer (guint8 **buffer, + guint16 *buffer_size, + guint n_bytes, + guint64 *in); + /* Reading/Writing string variables */ void qmi_utils_read_string_from_buffer (guint8 **buffer, diff --git a/libqmi-glib/test/test-utils.c b/libqmi-glib/test/test-utils.c index 290b48d..90d6759 100644 --- a/libqmi-glib/test/test-utils.c +++ b/libqmi-glib/test/test-utils.c @@ -297,6 +297,58 @@ test_utils_int64 (void) g_assert (memcmp (in_buffer, out_buffer, sizeof (in_buffer)) == 0); } +static void +common_test_utils_uint_sized (guint n_bytes) +{ + static guint8 in_buffer[8] = { + 0x0F, 0x50, 0xEB, 0xE2, 0xB6, 0x00, 0x00, 0x00 + }; + guint64 value = 0x000000B6E2EB500F; + guint8 expected_out_buffer[8] = { 0 }; + guint8 out_buffer[8] = { 0 }; + + guint64 tmp; + guint i; + guint16 in_buffer_size; + guint8 *in_buffer_walker; + guint16 out_buffer_size; + guint8 *out_buffer_walker; + + /* Build expected intermediate value */ + tmp = 0xFF; + for (i = 1; i < n_bytes; i++) { + tmp <<= 8; + tmp |= 0xFF; + } + value &= tmp; + + /* Build expected output buffer */ + memcpy (expected_out_buffer, in_buffer, n_bytes); + + in_buffer_size = sizeof (in_buffer); + in_buffer_walker = &in_buffer[0]; + out_buffer_size = sizeof (out_buffer); + out_buffer_walker = &out_buffer[0]; + i = 0; + + qmi_utils_read_sized_guint_from_buffer (&in_buffer_walker, &in_buffer_size, n_bytes, &tmp); + g_assert (tmp == value); + qmi_utils_write_sized_guint_to_buffer (&out_buffer_walker, &out_buffer_size, n_bytes, &tmp); + + g_assert_cmpuint (out_buffer_size, ==, 8 - n_bytes); + g_assert (memcmp (expected_out_buffer, out_buffer, sizeof (expected_out_buffer)) == 0); +} + +static void +test_utils_uint_sized (void) +{ + guint i; + + for (i = 1; i <= 8; i++) { + common_test_utils_uint_sized (i); + } +} + int main (int argc, char **argv) { g_type_init (); @@ -311,5 +363,7 @@ int main (int argc, char **argv) g_test_add_func ("/libqmi-glib/utils/uint64", test_utils_uint64); g_test_add_func ("/libqmi-glib/utils/int64", test_utils_int64); + g_test_add_func ("/libqmi-glib/utils/uint-sized", test_utils_uint_sized); + return g_test_run (); } |