[gnome-keyring/trust-store: 3/7] [egg] Add egg_asn1x_append() for adding to SEQ OF or SET OF



commit ef82d2cf986f0f70f9bfe9942e5a898a6826f9d2
Author: Stef Walter <stef memberwebs com>
Date:   Wed Sep 15 22:41:32 2010 +0000

    [egg] Add egg_asn1x_append() for adding to SEQ OF or SET OF
    
    To add a value to a SEQUENCE OF or SET OF, added the function
    egg_asn1x_append().

 egg/egg-asn1x.c       |   25 +++++++++++++++++++++++++
 egg/egg-asn1x.h       |    2 ++
 egg/tests/test-asn1.c |   33 +++++++++++++++++++++++++++++++++
 egg/tests/test.asn    |    2 ++
 4 files changed, 62 insertions(+), 0 deletions(-)
---
diff --git a/egg/egg-asn1x.c b/egg/egg-asn1x.c
index 78d434a..a669b8b 100644
--- a/egg/egg-asn1x.c
+++ b/egg/egg-asn1x.c
@@ -2277,6 +2277,31 @@ egg_asn1x_count (GNode *node)
 	return g_node_n_children (node);
 }
 
+GNode*
+egg_asn1x_append (GNode *node)
+{
+	GNode *child;
+	gint type;
+
+	g_return_val_if_fail (node, NULL);
+
+	type = anode_def_type (node);
+	if (type != TYPE_SEQUENCE_OF && type != TYPE_SET_OF) {
+		g_warning ("node passed to egg_asn1x_append was not a sequence of or set of");
+		return NULL;
+	}
+
+	/* There must be at least one child */
+	child = node->children;
+	g_return_val_if_fail (child, NULL);
+
+	child = anode_clone (child);
+	anode_clear (child);
+	g_node_append (node, child);
+
+	return child;
+}
+
 gboolean
 egg_asn1x_have (GNode *node)
 {
diff --git a/egg/egg-asn1x.h b/egg/egg-asn1x.h
index 705e226..2bff216 100644
--- a/egg/egg-asn1x.h
+++ b/egg/egg-asn1x.h
@@ -69,6 +69,8 @@ const gchar*        egg_asn1x_name                   (GNode *asn);
 
 guint               egg_asn1x_count                  (GNode *node);
 
+GNode*              egg_asn1x_append                 (GNode *node);
+
 gboolean            egg_asn1x_have                   (GNode *node);
 
 GNode*              egg_asn1x_get_choice             (GNode *node);
diff --git a/egg/tests/test-asn1.c b/egg/tests/test-asn1.c
index 57d4aac..809ba93 100644
--- a/egg/tests/test-asn1.c
+++ b/egg/tests/test-asn1.c
@@ -47,6 +47,12 @@ const gchar BITS_TEST[] =  "\x03\x04\x06\x6e\x5d\xc0";
 const gchar BITS_BAD[] =  "\x03\x04\x06\x6e\x5d\xc1";
 const gchar BITS_ZERO[] =  "\x03\x01\x00";
 
+/* SEQUENCE OF with one INTEGER = 1 */
+const gchar SEQOF_ONE[] =  "\x30\x03\x02\x01\x01";
+
+/* SEQUENCE OF with two INTEGER = 1, 2 */
+const gchar SEQOF_TWO[] =  "\x30\x06\x02\x01\x01\x02\x01\x02";
+
 #define XL(x) G_N_ELEMENTS (x) - 1
 
 DEFINE_TEST(asn1_boolean)
@@ -415,3 +421,30 @@ DEFINE_TEST(asn1_any_set_raw)
 	egg_asn1x_destroy (asn);
 	g_assert (is_freed);
 }
+
+DEFINE_TEST(asn1_append)
+{
+	GNode *asn;
+	GNode *child;
+	gpointer data;
+	gsize n_data;
+
+	asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestSeqOf", SEQOF_ONE, XL (SEQOF_ONE));
+	g_assert (asn);
+
+	child = egg_asn1x_append (asn);
+	g_assert (child);
+
+	/* Second integer is 2 */
+	if (!egg_asn1x_set_integer_as_ulong (child, 2))
+		g_assert_not_reached ();
+
+	data = egg_asn1x_encode (asn, NULL, &n_data);
+	g_assert (data);
+
+	g_assert (n_data == XL (SEQOF_TWO));
+	g_assert (memcmp (data, SEQOF_TWO, n_data) == 0);
+
+	g_free (data);
+	egg_asn1x_destroy (asn);
+}
diff --git a/egg/tests/test.asn b/egg/tests/test.asn
index f5f3053..d55dba8 100644
--- a/egg/tests/test.asn
+++ b/egg/tests/test.asn
@@ -40,4 +40,6 @@ TestAnySeq ::= SEQUENCE {
 	contents                ANY
 }
 
+TestSeqOf ::= SEQUENCE OF INTEGER
+
 END



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