[gnome-keyring/gck-work] [gck] Expose CK_BBOOL and CK_ULONG casting helpers.



commit ffecb6ddcf23fe63bca084a6c988eb5b3e0320a4
Author: Stef Walter <stef memberwebs com>
Date:   Sat Sep 18 03:02:18 2010 +0000

    [gck] Expose CK_BBOOL and CK_ULONG casting helpers.
    
    Add gck_value_to_ulong() and gck_value_to_boolean() to check
    and cast CK_BBOOL and CK_ULONG.

 gck/gck-attributes.c            |   16 ++++++++-----
 gck/gck-misc.c                  |   20 +++++++++++++++++
 gck/gck.h                       |    8 +++++++
 gck/tests/test-gck-attributes.c |   44 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 82 insertions(+), 6 deletions(-)
---
diff --git a/gck/gck-attributes.c b/gck/gck-attributes.c
index 8e6705a..eb5b3aa 100644
--- a/gck/gck-attributes.c
+++ b/gck/gck-attributes.c
@@ -420,12 +420,14 @@ gck_attribute_is_invalid (GckAttribute *attr)
 gboolean
 gck_attribute_get_boolean (GckAttribute *attr)
 {
+	gboolean value;
+
 	g_return_val_if_fail (attr, FALSE);
 	if (gck_attribute_is_invalid (attr))
 		return FALSE;
-	g_return_val_if_fail (attr->length == sizeof (CK_BBOOL), FALSE);
-	g_return_val_if_fail (attr->value, FALSE);
-	return *((CK_BBOOL*)attr->value) == CK_TRUE ? TRUE : FALSE;
+	if (!gck_value_to_boolean (attr->value, attr->length, &value))
+		g_return_val_if_reached (FALSE);
+	return value;
 }
 
 /**
@@ -442,12 +444,14 @@ gck_attribute_get_boolean (GckAttribute *attr)
 gulong
 gck_attribute_get_ulong (GckAttribute *attr)
 {
+	gulong value;
+
 	g_return_val_if_fail (attr, FALSE);
 	if (gck_attribute_is_invalid (attr))
 		return 0;
-	g_return_val_if_fail (attr->length == sizeof (CK_ULONG), (gulong)-1);
-	g_return_val_if_fail (attr->value, (gulong)-1);
-	return *((CK_ULONG*)attr->value);
+	if (!gck_value_to_ulong (attr->value, attr->length, &value))
+		g_return_val_if_reached ((gulong)-1);
+	return value;
 }
 
 /**
diff --git a/gck/gck-misc.c b/gck/gck-misc.c
index 6fc691b..a08bbc5 100644
--- a/gck/gck-misc.c
+++ b/gck/gck-misc.c
@@ -435,3 +435,23 @@ gck_mechanism_unref (GckMechanism* mech)
 		g_slice_free (GckMechanism, mech);
 	}
 }
+
+gboolean
+gck_value_to_ulong (gconstpointer value, gsize length, gulong *result)
+{
+	if (!value || length != sizeof (CK_ULONG))
+		return FALSE;
+	if (result)
+		*result = *((CK_ULONG*)value);
+	return TRUE;
+}
+
+gboolean
+gck_value_to_boolean (gconstpointer value, gsize length, gboolean *result)
+{
+	if (!value || length != sizeof (CK_BBOOL))
+		return FALSE;
+	if (result)
+		*result = *((CK_BBOOL*)value) ? TRUE : FALSE;
+	return TRUE;
+}
diff --git a/gck/gck.h b/gck/gck.h
index e8a52d9..f4b321d 100644
--- a/gck/gck.h
+++ b/gck/gck.h
@@ -80,6 +80,14 @@ enum {
 	GCK_AUTHENTICATE_OBJECTS = 4
 };
 
+gboolean            gck_value_to_ulong                      (gconstpointer value,
+                                                             gsize length,
+                                                             gulong *result);
+
+gboolean            gck_value_to_boolean                    (gconstpointer value,
+                                                             gsize length,
+                                                             gboolean *result);
+
 void                gck_attribute_init                      (GckAttribute *attr,
                                                              gulong attr_type,
                                                              gconstpointer value,
diff --git a/gck/tests/test-gck-attributes.c b/gck/tests/test-gck-attributes.c
index f2926ae..e24b897 100644
--- a/gck/tests/test-gck-attributes.c
+++ b/gck/tests/test-gck-attributes.c
@@ -23,6 +23,50 @@ DEFINE_TEST(init_memory)
 	gck_attribute_clear (&attr);
 }
 
+DEFINE_TEST(value_to_boolean)
+{
+	CK_BBOOL data = CK_TRUE;
+	gboolean result = FALSE;
+
+	if (!gck_value_to_boolean (&data, sizeof (data), &result))
+		g_assert_not_reached ();
+
+	g_assert (result == TRUE);
+
+	if (!gck_value_to_boolean (&data, sizeof (data), NULL))
+		g_assert_not_reached ();
+
+	/* Should fail */
+	if (gck_value_to_boolean (&data, 0, NULL))
+		g_assert_not_reached ();
+	if (gck_value_to_boolean (&data, 2, NULL))
+		g_assert_not_reached ();
+	if (gck_value_to_boolean (&data, (CK_ULONG)-1, NULL))
+		g_assert_not_reached ();
+}
+
+DEFINE_TEST(value_to_ulong)
+{
+	CK_ULONG data = 34343;
+	gulong result = 0;
+
+	if (!gck_value_to_ulong (&data, sizeof (data), &result))
+		g_assert_not_reached ();
+
+	g_assert (result == 34343);
+
+	if (!gck_value_to_ulong (&data, sizeof (data), NULL))
+		g_assert_not_reached ();
+
+	/* Should fail */
+	if (gck_value_to_ulong (&data, 0, NULL))
+		g_assert_not_reached ();
+	if (gck_value_to_ulong (&data, 2, NULL))
+		g_assert_not_reached ();
+	if (gck_value_to_ulong (&data, (CK_ULONG)-1, NULL))
+		g_assert_not_reached ();
+}
+
 DEFINE_TEST(init_boolean)
 {
 	GckAttribute attr;



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