From 446648da529d8aeedae8539eceba5052f421bdac Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Wed, 7 Nov 2012 23:15:44 +0100 Subject: [PATCH] egg-hex: Bring over changes from gnome-keyring and gcr --- egg/egg-hex.c | 37 ++++++++++++++++++++++--------------- egg/egg-hex.h | 12 ++++++------ egg/tests/test-hex.c | 11 +++++++---- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/egg/egg-hex.c b/egg/egg-hex.c index 434af39..f4d0f18 100644 --- a/egg/egg-hex.c +++ b/egg/egg-hex.c @@ -2,17 +2,17 @@ * gnome-keyring * * Copyright (C) 2008 Stefan Walter - * - * This program is free software; you can redistribute it and/or modify + * + * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, @@ -37,11 +37,15 @@ egg_hex_decode (const gchar *data, gssize n_data, gsize *n_decoded) } gpointer -egg_hex_decode_full (const gchar *data, gssize n_data, - gchar delim, guint group, gsize *n_decoded) +egg_hex_decode_full (const gchar *data, + gssize n_data, + const gchar *delim, + guint group, + gsize *n_decoded) { guchar *result; guchar *decoded; + gsize n_delim; gushort j; gint state = 0; gint part = 0; @@ -53,20 +57,20 @@ egg_hex_decode_full (const gchar *data, gssize n_data, if (n_data == -1) n_data = strlen (data); - + n_delim = delim ? strlen (delim) : 0; decoded = result = g_malloc0 ((n_data / 2) + 1); *n_decoded = 0; while (n_data > 0 && state == 0) { if (decoded != result && delim) { - if (*data != delim) { + if (n_data < n_delim || memcmp (data, delim, n_delim) != 0) { state = -1; break; } - ++data; - --n_data; + data += n_delim; + n_data -= n_delim; } while (part < group && n_data > 0) { @@ -110,12 +114,15 @@ egg_hex_decode_full (const gchar *data, gssize n_data, gchar* egg_hex_encode (gconstpointer data, gsize n_data) { - return egg_hex_encode_full (data, n_data, TRUE, '\0', 0); + return egg_hex_encode_full (data, n_data, TRUE, NULL, 0); } gchar* -egg_hex_encode_full (gconstpointer data, gsize n_data, - gboolean upper_case, gchar delim, guint group) +egg_hex_encode_full (gconstpointer data, + gsize n_data, + gboolean upper_case, + const gchar *delim, + guint group) { GString *result; const gchar *input; @@ -133,8 +140,8 @@ egg_hex_encode_full (gconstpointer data, gsize n_data, while (n_data > 0) { - if (group && bytes && (bytes % group) == 0) - g_string_append_c (result, delim); + if (delim && group && bytes && (bytes % group) == 0) + g_string_append (result, delim); j = *(input) >> 4 & 0xf; g_string_append_c (result, hexc[j]); diff --git a/egg/egg-hex.h b/egg/egg-hex.h index c064150..0e90aee 100644 --- a/egg/egg-hex.h +++ b/egg/egg-hex.h @@ -2,17 +2,17 @@ * gnome-keyring * * Copyright (C) 2008 Stefan Walter - * - * This program is free software; you can redistribute it and/or modify + * + * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, @@ -32,7 +32,7 @@ gpointer egg_hex_decode (const gchar *data, gpointer egg_hex_decode_full (const gchar *data, gssize n_data, - gchar delim, + const gchar *delim, guint group, gsize *n_decoded); @@ -42,7 +42,7 @@ gchar* egg_hex_encode (gconstpointer data gchar* egg_hex_encode_full (gconstpointer data, gsize n_data, gboolean upper_case, - gchar delim, + const gchar *delim, guint group); #endif /* EGG_HEX_H_ */ diff --git a/egg/tests/test-hex.c b/egg/tests/test-hex.c index d2c6ba7..992f0aa 100644 --- a/egg/tests/test-hex.c +++ b/egg/tests/test-hex.c @@ -31,7 +31,7 @@ static const guchar TEST_DATA[] = { 0x05, 0xD6, 0x95, 0x96, 0x10, 0x12, 0xAE, 0x35 }; static const gchar *TEST_HEX = "05D695961012AE35"; -static const gchar *TEST_HEX_DELIM = "05 D6 95 96 10 12 AE 35"; +static const gchar *TEST_HEX_DELIM = "05 D6 95 96 10 12 AE 35"; static void test_encode (void) @@ -41,6 +41,7 @@ test_encode (void) hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA)); g_assert (hex); g_assert_cmpstr (hex, ==, TEST_HEX); + g_free (hex); } @@ -53,12 +54,14 @@ test_encode_spaces (void) hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, 0, 0); g_assert (hex); g_assert_cmpstr (hex, ==, TEST_HEX); + g_free (hex); /* Encode with spaces */ - hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, ' ', 1); + hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, " ", 1); g_assert (hex); g_assert_cmpstr (hex, ==, TEST_HEX_DELIM); + g_free (hex); } @@ -81,7 +84,7 @@ test_decode (void) g_free (data); /* Delimited*/ - data = egg_hex_decode_full (TEST_HEX_DELIM, -1, ' ', 1, &n_data); + data = egg_hex_decode_full (TEST_HEX_DELIM, -1, " ", 1, &n_data); g_assert (data); g_assert (n_data == sizeof (TEST_DATA)); g_assert (memcmp (data, TEST_DATA, n_data) == 0); @@ -103,7 +106,7 @@ test_decode_fail (void) g_assert (!data); /* Not Delimited, null out*/ - data = egg_hex_decode_full ("ABABAB", -1, ':', 1, &n_data); + data = egg_hex_decode_full ("ABABAB", -1, ":", 1, &n_data); g_assert (!data); }