[gnome-keyring/asn1-work: 11/18] [egg] Add asn functionality for clearing a tree.



commit 83a8ec545f064d153ae9fd2de64b6a5a2d39f6a4
Author: Stef Walter <stef memberwebs com>
Date:   Fri Dec 25 00:11:28 2009 +0000

    [egg] Add asn functionality for clearing a tree.

 egg/egg-asn1x.c        |   65 +++++++++++++++++++++++++++++++++++++++++++----
 egg/egg-asn1x.h        |    2 +
 egg/tests/test-asn1x.c |   64 ++++++++++++++---------------------------------
 3 files changed, 80 insertions(+), 51 deletions(-)
---
diff --git a/egg/egg-asn1x.c b/egg/egg-asn1x.c
index 4cde798..2fa30d8 100644
--- a/egg/egg-asn1x.c
+++ b/egg/egg-asn1x.c
@@ -119,12 +119,20 @@ anode_new (const ASN1_ARRAY_TYPE *def)
 	return g_node_new (an);
 }
 
-static gboolean
-anode_free_func (GNode *node, gpointer unused)
+static void
+anode_clear (GNode *node)
 {
 	Anode *an = node->data;
 	if (an->data);
 		g_slice_free_chain (Atlv, an->data, next);
+	an->data = NULL;
+}
+
+static gboolean
+anode_free_func (GNode *node, gpointer unused)
+{
+	Anode *an = node->data;
+	anode_clear (node);
 	g_slice_free (Anode, an);
 	return FALSE;
 }
@@ -132,9 +140,10 @@ anode_free_func (GNode *node, gpointer unused)
 static void
 anode_destroy (GNode *node)
 {
+	if (!G_NODE_IS_ROOT (node))
+		g_node_unlink (node);
 	g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, anode_free_func, NULL);
 	g_node_destroy (node);
-
 }
 
 static gpointer
@@ -762,6 +771,8 @@ egg_asn1x_decode (GNode *asn, gconstpointer data, gsize n_data)
 	g_return_val_if_fail (data, FALSE);
 	g_return_val_if_fail (n_data, FALSE);
 
+	egg_asn1x_clear (asn);
+
 	if (!anode_decode_tlv_for_data (data, (const guchar*)data + n_data, &tlv))
 		return FALSE;
 
@@ -911,8 +922,12 @@ egg_asn1x_create (const ASN1_ARRAY_TYPE *defs, const gchar *identifier)
 	return root;
 }
 
+/* -----------------------------------------------------------------------------------
+ * DUMPING
+ */
+
 static gboolean
-traverse_and_dump (GNode *node, gpointer data)
+traverse_and_dump (GNode *node, gpointer unused)
 {
 	guint i, depth;
 	GString *output;
@@ -956,9 +971,47 @@ traverse_and_dump (GNode *node, gpointer data)
 void
 egg_asn1x_dump (GNode *asn)
 {
-	guint depth = 0;
 	g_return_if_fail (asn);
-	g_node_traverse (asn, G_PRE_ORDER, G_TRAVERSE_ALL, -1, traverse_and_dump, &depth);
+	g_node_traverse (asn, G_PRE_ORDER, G_TRAVERSE_ALL, -1, traverse_and_dump, NULL);
+}
+
+/* -----------------------------------------------------------------------------------
+ * CLEARING and DESTROYING
+ */
+
+static gboolean
+traverse_and_clear (GNode *node, gpointer unused)
+{
+	GNode *child, *next;
+	gint type;
+
+	anode_clear (node);
+
+	type = anode_def_type (node);
+	if (type == TYPE_SET_OF || type == TYPE_SEQUENCE_OF) {
+
+		/* The first 'real' child is the template */
+		child = anode_child_with_real_type (node);
+		g_return_val_if_fail (child, TRUE);
+
+		/* And any others are extras */
+		child = anode_next_with_real_type (child);
+		while (child) {
+			next = anode_next_with_real_type (child);
+			anode_destroy (child);
+			child = next;
+		}
+	}
+
+	/* Don't stop traversal */
+	return FALSE;
+}
+
+void
+egg_asn1x_clear (GNode *asn)
+{
+	g_return_if_fail (asn);
+	g_node_traverse (asn, G_POST_ORDER, G_TRAVERSE_ALL, -1, traverse_and_clear, NULL);
 }
 
 void
diff --git a/egg/egg-asn1x.h b/egg/egg-asn1x.h
index 94acb55..43c72b7 100644
--- a/egg/egg-asn1x.h
+++ b/egg/egg-asn1x.h
@@ -39,6 +39,8 @@ GNode*              egg_asn1x_create             (const ASN1_ARRAY_TYPE *defs,
 
 void                egg_asn1x_dump               (GNode *asn);
 
+void                egg_asn1x_clear              (GNode *asn);
+
 gboolean            egg_asn1x_decode             (GNode *asn,
                                                   gconstpointer data,
                                                   gsize n_data);
diff --git a/egg/tests/test-asn1x.c b/egg/tests/test-asn1x.c
index 99f4515..ce966cf 100644
--- a/egg/tests/test-asn1x.c
+++ b/egg/tests/test-asn1x.c
@@ -43,65 +43,39 @@ build_personal_name (void)
 }
 #endif
 
-static int
-run (void)
+static void
+test_some_asn1_stuff (const ASN1_ARRAY_TYPE *defs, const gchar *file, const gchar *identifier)
 {
 	GNode *asn;
 	gpointer data;
 	gsize n_data;
 
-	/* Build up a personal name, which is a set */
-#if 0
-	build_personal_name ();
-#endif
-
-	data = testing_data_read ("test-certificate-1.der", &n_data);
-	asn = egg_asn1x_create (pkix_asn1_tab, "Certificate");
-	egg_asn1x_dump (asn);
-	if (!egg_asn1x_decode (asn, data, n_data))
-		g_assert_not_reached ();
-	egg_asn1x_destroy (asn);
-	g_free (data);
-
-	data = testing_data_read ("test-pkcs8-1.der", &n_data);
-	asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-8-PrivateKeyInfo");
+	data = testing_data_read (file, &n_data);
+	asn = egg_asn1x_create (defs, identifier);
 	egg_asn1x_dump (asn);
 	if (!egg_asn1x_decode (asn, data, n_data))
 		g_assert_not_reached ();
+	egg_asn1x_clear (asn);
 	egg_asn1x_destroy (asn);
 	g_free (data);
+}
 
-	data = testing_data_read ("test-rsakey-1.der", &n_data);
-	asn = egg_asn1x_create (pk_asn1_tab, "RSAPrivateKey");
-	egg_asn1x_dump (asn);
-	if (!egg_asn1x_decode (asn, data, n_data))
-		g_assert_not_reached ();
-	egg_asn1x_destroy (asn);
-	g_free (data);
+static int
+run (void)
+{
 
-	data = testing_data_read ("test-personalname-1.der", &n_data);
-	asn = egg_asn1x_create (pkix_asn1_tab, "PersonalName");
-	egg_asn1x_dump (asn);
-	if (!egg_asn1x_decode (asn, data, n_data))
-		g_assert_not_reached ();
-	egg_asn1x_destroy (asn);
-	g_free (data);
+	/* Build up a personal name, which is a set */
+#if 0
+	build_personal_name ();
+#endif
 
-	data = testing_data_read ("test-pkcs7-1.der", &n_data);
-	asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-7-ContentInfo");
-	egg_asn1x_dump (asn);
-	if (!egg_asn1x_decode (asn, data, n_data))
-		g_assert_not_reached ();
-	egg_asn1x_destroy (asn);
-	g_free (data);
 
-	data = testing_data_read ("test-pkcs12-1.der", &n_data);
-	asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-12-PFX");
-	egg_asn1x_dump (asn);
-	if (!egg_asn1x_decode (asn, data, n_data))
-		g_assert_not_reached ();
-	egg_asn1x_destroy (asn);
-	g_free (data);
+	test_some_asn1_stuff (pkix_asn1_tab, "test-certificate-1.der", "Certificate");
+	test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo");
+	test_some_asn1_stuff (pk_asn1_tab, "test-rsakey-1.der", "RSAPrivateKey");
+	test_some_asn1_stuff (pkix_asn1_tab, "test-personalname-1.der", "PersonalName");
+	test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs7-1.der", "pkcs-7-ContentInfo");
+	test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs12-1.der", "pkcs-12-PFX");
 
 	return 0;
 }



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