[gcr] egg-hex: Use a full string as the hex delimiter



commit b271623e5c3044099fdbae8e6037fd23e3eee5e9
Author: Stef Walter <stefw gnome org>
Date:   Wed Nov 7 09:19:19 2012 +0100

    egg-hex: Use a full string as the hex delimiter
    
    So that we can better print out escape encodings in our test data.

 egg/egg-hex.c                |   29 ++++++++++++++++++-----------
 egg/egg-hex.h                |    4 ++--
 egg/tests/test-hex.c         |    8 ++++----
 gck/gck-dump.c               |    2 +-
 gcr/gcr-certificate.c        |    2 +-
 gcr/gcr-display-view.c       |    2 +-
 gcr/gcr-gnupg-util.c         |    2 +-
 gcr/gcr-openpgp.c            |   14 +++++++-------
 gcr/tests/frob-certificate.c |    4 ++--
 9 files changed, 37 insertions(+), 30 deletions(-)
---
diff --git a/egg/egg-hex.c b/egg/egg-hex.c
index 4540829..fdc234a 100644
--- a/egg/egg-hex.c
+++ b/egg/egg-hex.c
@@ -35,11 +35,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;
@@ -51,20 +55,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) {
@@ -108,12 +112,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;
@@ -131,8 +138,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 83b5fdc..4575a43 100644
--- a/egg/egg-hex.h
+++ b/egg/egg-hex.h
@@ -30,7 +30,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);
 
@@ -40,7 +40,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 93ad4bf..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)
@@ -58,7 +58,7 @@ test_encode_spaces (void)
 	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);
 
@@ -84,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);
@@ -106,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);
 }
 
diff --git a/gck/gck-dump.c b/gck/gck-dump.c
index 4e9aeab..0f1e1fb 100644
--- a/gck/gck-dump.c
+++ b/gck/gck-dump.c
@@ -198,7 +198,7 @@ dump_attribute_value (const GckAttribute *attr)
 	};
 
 	len = MIN (20, attr->length);
-	data = egg_hex_encode_full (attr->value, len, TRUE, ':', 1);
+	data = egg_hex_encode_full (attr->value, len, TRUE, ":", 1);
 	g_printerr ("%s%s", data, len < attr->length ? "..." : "");
 	g_free (data);
 }
diff --git a/gcr/gcr-certificate.c b/gcr/gcr-certificate.c
index 34006ac..1327108 100644
--- a/gcr/gcr-certificate.c
+++ b/gcr/gcr-certificate.c
@@ -864,7 +864,7 @@ gcr_certificate_get_fingerprint_hex (GcrCertificate *self, GChecksumType type)
 	digest = g_malloc (length);
 	n_digest = length;
 	g_checksum_get_digest (sum, digest, &n_digest);
-	hex = egg_hex_encode_full (digest, n_digest, TRUE, ' ', 1);
+	hex = egg_hex_encode_full (digest, n_digest, TRUE, " ", 1);
 	g_checksum_free (sum);
 	g_free (digest);
 	return hex;
diff --git a/gcr/gcr-display-view.c b/gcr/gcr-display-view.c
index 8010a68..4bbefbc 100644
--- a/gcr/gcr-display-view.c
+++ b/gcr/gcr-display-view.c
@@ -1042,7 +1042,7 @@ _gcr_display_view_append_hex (GcrDisplayView *self, GcrRenderer *renderer,
 {
 	gchar *display;
 
-	display = egg_hex_encode_full (value, n_value, TRUE, ' ', 1);
+	display = egg_hex_encode_full (value, n_value, TRUE, " ", 1);
 	_gcr_display_view_append_value (self, renderer, field, display, TRUE);
 	g_free (display);
 }
diff --git a/gcr/gcr-gnupg-util.c b/gcr/gcr-gnupg-util.c
index b85836b..5f8a6ed 100644
--- a/gcr/gcr-gnupg-util.c
+++ b/gcr/gcr-gnupg-util.c
@@ -54,7 +54,7 @@ _gcr_gnupg_build_xa1_record (GcrRecord *meta, gpointer attribute,
 	record = _gcr_record_new (GCR_RECORD_SCHEMA_XA1, GCR_RECORD_XA1_MAX, ':');
 
 	gcry_md_hash_buffer (GCRY_MD_RMD160, hash, attribute, n_attribute);
-	hex = egg_hex_encode_full (hash, sizeof (hash), TRUE, 0, 1);
+	hex = egg_hex_encode_full (hash, sizeof (hash), TRUE, NULL, 1);
 	_gcr_record_take_raw (record, GCR_RECORD_XA1_FINGERPRINT, hex);
 
 	if (!_gcr_record_get_uint (meta, GCR_RECORD_ATTRIBUTE_FLAGS, &flags))
diff --git a/gcr/gcr-openpgp.c b/gcr/gcr-openpgp.c
index 8cd4d88..8c87561 100644
--- a/gcr/gcr-openpgp.c
+++ b/gcr/gcr-openpgp.c
@@ -277,7 +277,7 @@ hash_user_id_or_attribute (const guchar *beg,
 	g_assert (end > beg);
 
 	gcry_md_hash_buffer (GCRY_MD_RMD160, digest, beg, end - beg);
-	return egg_hex_encode_full (digest, sizeof (digest), TRUE, 0, 0);
+	return egg_hex_encode_full (digest, sizeof (digest), TRUE, NULL, 0);
 }
 
 static gboolean
@@ -303,7 +303,7 @@ parse_v3_rsa_bits_and_keyid (const guchar **at,
 		return FALSE;
 	}
 
-	*keyid = egg_hex_encode_full (n + (bytes - 8), 8, TRUE, 0, 0);
+	*keyid = egg_hex_encode_full (n + (bytes - 8), 8, TRUE, NULL, 0);
 	return TRUE;
 }
 
@@ -341,9 +341,9 @@ hash_v4_keyid (const guchar *data,
 	gcry_md_write (mdh, data, len);
 
 	digest = gcry_md_read (mdh, 0);
-	keyid = egg_hex_encode_full (digest + 12, 8, TRUE, 0, 0);
+	keyid = egg_hex_encode_full (digest + 12, 8, TRUE, NULL, 0);
 	if (fingerprint)
-		*fingerprint = egg_hex_encode_full (digest, 20, TRUE, 0, 0);
+		*fingerprint = egg_hex_encode_full (digest, 20, TRUE, NULL, 0);
 	gcry_md_close (mdh);
 
 	return keyid;
@@ -656,7 +656,7 @@ parse_v3_signature (const guchar **at,
 	if (flags & GCR_OPENPGP_PARSE_SIGNATURES) {
 		record = _gcr_record_new (GCR_RECORD_SCHEMA_SIG, GCR_RECORD_SIG_MAX, ':');
 		_gcr_record_set_uint (record, GCR_RECORD_SIG_ALGO, key_algo);
-		value = egg_hex_encode_full (keyid, sizeof (keyid), TRUE, 0, 0);
+		value = egg_hex_encode_full (keyid, sizeof (keyid), TRUE, NULL, 0);
 		_gcr_record_take_raw (record, GCR_RECORD_SIG_KEYID, value);
 		_gcr_record_set_ulong (record, GCR_RECORD_SIG_TIMESTAMP, sig_time);
 		value = g_strdup_printf ("%02xx", (guint)sig_type);
@@ -691,7 +691,7 @@ parse_v4_signature_revocation (const guchar **at,
 		return FALSE;
 
 	_gcr_record_set_uint (revocation, GCR_RECORD_RVK_ALGO, algo);
-	value = egg_hex_encode_full (fingerprint, 20, TRUE, 0, 0);
+	value = egg_hex_encode_full (fingerprint, 20, TRUE, NULL, 0);
 	_gcr_record_take_raw (revocation, GCR_RECORD_RVK_FINGERPRINT, value);
 	value = g_strdup_printf ("%02X", (guint)klass);
 	_gcr_record_take_raw (revocation, GCR_RECORD_RVK_CLASS, value);
@@ -724,7 +724,7 @@ parse_v4_signature_subpacket (const guchar **at,
 	case OPENPGP_SIG_ISSUER:
 		if (!read_bytes (at, end, keyid, 8))
 			return FALSE;
-		value = egg_hex_encode_full (keyid, 8, TRUE, 0, 0);
+		value = egg_hex_encode_full (keyid, 8, TRUE, NULL, 0);
 		_gcr_record_take_raw (record, GCR_RECORD_SIG_KEYID, value);
 		return TRUE;
 	case OPENPGP_SIG_KEY_EXPIRY:
diff --git a/gcr/tests/frob-certificate.c b/gcr/tests/frob-certificate.c
index caf8827..2524dc2 100644
--- a/gcr/tests/frob-certificate.c
+++ b/gcr/tests/frob-certificate.c
@@ -50,13 +50,13 @@ on_parser_parsed (GcrParser *parser, gpointer user_data)
 	cert = gcr_certificate_widget_get_certificate (details);
 
 	dn = gcr_certificate_get_subject_raw (cert, &n_dn);
-	string = egg_hex_encode_full (dn, n_dn, TRUE, '\\', 1);
+	string = egg_hex_encode_full (dn, n_dn, TRUE, "\\", 1);
 	g_print ("subject: %s\n", string);
 	g_free (string);
 	g_free (dn);
 
 	dn = gcr_certificate_get_issuer_raw (cert, &n_dn);
-	string = egg_hex_encode_full (dn, n_dn, TRUE, '\\', 1);
+	string = egg_hex_encode_full (dn, n_dn, TRUE, "\\", 1);
 	g_print ("issuer: %s\n", string);
 	g_free (string);
 	g_free (dn);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]