egg-hex: Bring over changes from gnome-keyring and gcr

This commit is contained in:
Stef Walter 2012-11-07 23:15:44 +01:00
parent e666db528e
commit 446648da52
3 changed files with 35 additions and 25 deletions

View File

@ -37,11 +37,15 @@ egg_hex_decode (const gchar *data, gssize n_data, gsize *n_decoded)
} }
gpointer gpointer
egg_hex_decode_full (const gchar *data, gssize n_data, egg_hex_decode_full (const gchar *data,
gchar delim, guint group, gsize *n_decoded) gssize n_data,
const gchar *delim,
guint group,
gsize *n_decoded)
{ {
guchar *result; guchar *result;
guchar *decoded; guchar *decoded;
gsize n_delim;
gushort j; gushort j;
gint state = 0; gint state = 0;
gint part = 0; gint part = 0;
@ -53,20 +57,20 @@ egg_hex_decode_full (const gchar *data, gssize n_data,
if (n_data == -1) if (n_data == -1)
n_data = strlen (data); n_data = strlen (data);
n_delim = delim ? strlen (delim) : 0;
decoded = result = g_malloc0 ((n_data / 2) + 1); decoded = result = g_malloc0 ((n_data / 2) + 1);
*n_decoded = 0; *n_decoded = 0;
while (n_data > 0 && state == 0) { while (n_data > 0 && state == 0) {
if (decoded != result && delim) { if (decoded != result && delim) {
if (*data != delim) { if (n_data < n_delim || memcmp (data, delim, n_delim) != 0) {
state = -1; state = -1;
break; break;
} }
++data; data += n_delim;
--n_data; n_data -= n_delim;
} }
while (part < group && n_data > 0) { while (part < group && n_data > 0) {
@ -110,12 +114,15 @@ egg_hex_decode_full (const gchar *data, gssize n_data,
gchar* gchar*
egg_hex_encode (gconstpointer data, gsize n_data) 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* gchar*
egg_hex_encode_full (gconstpointer data, gsize n_data, egg_hex_encode_full (gconstpointer data,
gboolean upper_case, gchar delim, guint group) gsize n_data,
gboolean upper_case,
const gchar *delim,
guint group)
{ {
GString *result; GString *result;
const gchar *input; const gchar *input;
@ -133,8 +140,8 @@ egg_hex_encode_full (gconstpointer data, gsize n_data,
while (n_data > 0) { while (n_data > 0) {
if (group && bytes && (bytes % group) == 0) if (delim && group && bytes && (bytes % group) == 0)
g_string_append_c (result, delim); g_string_append (result, delim);
j = *(input) >> 4 & 0xf; j = *(input) >> 4 & 0xf;
g_string_append_c (result, hexc[j]); g_string_append_c (result, hexc[j]);

View File

@ -32,7 +32,7 @@ gpointer egg_hex_decode (const gchar *data,
gpointer egg_hex_decode_full (const gchar *data, gpointer egg_hex_decode_full (const gchar *data,
gssize n_data, gssize n_data,
gchar delim, const gchar *delim,
guint group, guint group,
gsize *n_decoded); gsize *n_decoded);
@ -42,7 +42,7 @@ gchar* egg_hex_encode (gconstpointer data
gchar* egg_hex_encode_full (gconstpointer data, gchar* egg_hex_encode_full (gconstpointer data,
gsize n_data, gsize n_data,
gboolean upper_case, gboolean upper_case,
gchar delim, const gchar *delim,
guint group); guint group);
#endif /* EGG_HEX_H_ */ #endif /* EGG_HEX_H_ */

View File

@ -31,7 +31,7 @@
static const guchar TEST_DATA[] = { 0x05, 0xD6, 0x95, 0x96, 0x10, 0x12, 0xAE, 0x35 }; static const guchar TEST_DATA[] = { 0x05, 0xD6, 0x95, 0x96, 0x10, 0x12, 0xAE, 0x35 };
static const gchar *TEST_HEX = "05D695961012AE35"; 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 static void
test_encode (void) test_encode (void)
@ -41,6 +41,7 @@ test_encode (void)
hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA)); hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA));
g_assert (hex); g_assert (hex);
g_assert_cmpstr (hex, ==, TEST_HEX); g_assert_cmpstr (hex, ==, TEST_HEX);
g_free (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); hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, 0, 0);
g_assert (hex); g_assert (hex);
g_assert_cmpstr (hex, ==, TEST_HEX); g_assert_cmpstr (hex, ==, TEST_HEX);
g_free (hex); g_free (hex);
/* Encode with spaces */ /* 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 (hex);
g_assert_cmpstr (hex, ==, TEST_HEX_DELIM); g_assert_cmpstr (hex, ==, TEST_HEX_DELIM);
g_free (hex); g_free (hex);
} }
@ -81,7 +84,7 @@ test_decode (void)
g_free (data); g_free (data);
/* Delimited*/ /* 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 (data);
g_assert (n_data == sizeof (TEST_DATA)); g_assert (n_data == sizeof (TEST_DATA));
g_assert (memcmp (data, TEST_DATA, n_data) == 0); g_assert (memcmp (data, TEST_DATA, n_data) == 0);
@ -103,7 +106,7 @@ test_decode_fail (void)
g_assert (!data); g_assert (!data);
/* Not Delimited, null out*/ /* 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); g_assert (!data);
} }