From 170b4e05fcf3ecb14b4413ba523704ab4152a2c2 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 23 Nov 2010 22:56:30 +0000 Subject: [egg] Add utilities so GByteArray can be used in GHashTable. Update xdg pkcs11 module to use these new utils. --- egg/Makefile.am | 1 + egg/egg-byte-array.c | 65 ++++++++++++++++++++++++++++++++++++++++ egg/egg-byte-array.h | 33 ++++++++++++++++++++ pkcs11/gkm/gkm-util.c | 60 ------------------------------------- pkcs11/xdg-store/gkm-xdg-trust.c | 16 ++++++---- 5 files changed, 109 insertions(+), 66 deletions(-) create mode 100644 egg/egg-byte-array.c create mode 100644 egg/egg-byte-array.h diff --git a/egg/Makefile.am b/egg/Makefile.am index 7b1c7483..a42ce85f 100644 --- a/egg/Makefile.am +++ b/egg/Makefile.am @@ -24,6 +24,7 @@ libegg_la_CFLAGS = \ libegg_la_SOURCES = \ egg-asn1x.c egg-asn1x.h \ egg-buffer.c egg-buffer.h \ + egg-byte-array.c egg-byte-array.h \ egg-cleanup.c egg-cleanup.h \ egg-dh.c egg-dh.h \ egg-dn.c egg-dn.h \ diff --git a/egg/egg-byte-array.c b/egg/egg-byte-array.c new file mode 100644 index 00000000..6f9f7360 --- /dev/null +++ b/egg/egg-byte-array.c @@ -0,0 +1,65 @@ +/* + * gnome-keyring + * + * Copyright (C) 2010 Collabora Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General License for more details. + * + * You should have received a copy of the GNU Lesser General + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Author: Stef Walter + */ + +#include "egg-byte-array.h" + +#include + +guint +egg_byte_array_hash (gconstpointer v) +{ + const GByteArray *array = v; + const signed char *p; + guint32 h = 0; + gsize i; + + g_assert (array); + g_assert (array->data); + p = (signed char*)array->data; + + /* 31 bit hash function */ + for (i = 0; i < array->len; ++i, ++p) + h = (h << 5) - h + *p; + + return h; +} + +gboolean +egg_byte_array_equal (gconstpointer v1, gconstpointer v2) +{ + const GByteArray *array1 = v1; + const GByteArray *array2 = v2; + + if (array1 == array2) + return TRUE; + if (!array1 || !array2) + return FALSE; + + if (array1->len != array2->len) + return FALSE; + + g_assert (array1->data); + g_assert (array2->data); + + return (memcmp (array1->data, array2->data, array1->len) == 0); +} diff --git a/egg/egg-byte-array.h b/egg/egg-byte-array.h new file mode 100644 index 00000000..f03b34de --- /dev/null +++ b/egg/egg-byte-array.h @@ -0,0 +1,33 @@ +/* + * gnome-keyring + * + * Copyright (C) 2010 Collabora Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General License for more details. + * + * You should have received a copy of the GNU Lesser General + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Author: Stef Walter + */ + +#ifndef EGG_BYTE_ARRAY_H_ +#define EGG_BYTE_ARRAY_H_ + +#include + +guint egg_byte_array_hash (gconstpointer v); + +gboolean egg_byte_array_equal (gconstpointer v1, gconstpointer v2); + +#endif /* EGG_BYTE_ARRAY_H_ */ diff --git a/pkcs11/gkm/gkm-util.c b/pkcs11/gkm/gkm-util.c index 6e1f29cb..37561d90 100644 --- a/pkcs11/gkm/gkm-util.c +++ b/pkcs11/gkm/gkm-util.c @@ -67,67 +67,7 @@ /* Only access using atomic operations */ static gint next_handle = 0x00000010; -GkmMemory* -gkm_util_memory_new (gconstpointer data, gsize n_data) -{ - GkmMemory *memory; - - memory = g_malloc (n_data + sizeof (GkmMemory)); - memory->n_data = n_data; - memory->data = memory + 1; - - if (n_data) { - g_assert (data); - memcpy (memory + 1, data, n_data); - } - return memory; -} - -guint -gkm_util_memory_hash (gconstpointer v) -{ - const GkmMemory *memory = v; - const signed char *p; - guint32 h = 0; - gsize i; - - g_assert (memory); - g_assert (memory->data); - p = memory->data; - - /* 31 bit hash function */ - for (i = 0; i < memory->n_data; ++i, ++p) - h = (h << 5) - h + *p; - - return h; -} - -gboolean -gkm_util_memory_equal (gconstpointer v1, gconstpointer v2) -{ - const GkmMemory *memory_1 = v1; - const GkmMemory *memory_2 = v2; - - if (memory_1 == memory_2) - return TRUE; - if (!memory_1 || !memory_2) - return FALSE; - - if (memory_1->n_data != memory_2->n_data) - return FALSE; - - g_assert (memory_1->data); - g_assert (memory_2->data); - - return (memcmp (memory_1->data, memory_2->data, memory_1->n_data) == 0); -} - -void -gkm_util_memory_free (gpointer memory) -{ - g_free (memory); -} gulong* gkm_util_ulong_alloc (gulong value) diff --git a/pkcs11/xdg-store/gkm-xdg-trust.c b/pkcs11/xdg-store/gkm-xdg-trust.c index ff976e82..a8dfba60 100644 --- a/pkcs11/xdg-store/gkm-xdg-trust.c +++ b/pkcs11/xdg-store/gkm-xdg-trust.c @@ -25,6 +25,7 @@ #include "egg/egg-asn1x.h" #include "egg/egg-asn1-defs.h" +#include "egg/egg-byte-array.h" #include "gkm/gkm-assertion.h" #include "gkm/gkm-attributes.h" @@ -281,8 +282,8 @@ dispose_each_assertion (gpointer key, gpointer value, gpointer user_data) static GHashTable* create_assertions (void) { - return g_hash_table_new_full (gkm_util_memory_hash, gkm_util_memory_equal, - gkm_util_memory_free, gkm_util_dispose_unref); + return g_hash_table_new_full (egg_byte_array_hash, egg_byte_array_equal, + (GDestroyNotify)g_byte_array_unref, gkm_util_dispose_unref); } static GkmAssertion* @@ -335,6 +336,7 @@ load_assertions (GkmXdgTrust *self, GNode *asn) GkmAssertion *assertion; NetscapeFlags netscape; gsize n_element; + GByteArray *key; GNode *node; guint count, i; @@ -355,13 +357,14 @@ load_assertions (GkmXdgTrust *self, GNode *asn) g_return_val_if_fail (node, FALSE); /* Double check that this is valid, because it's how we hash */ - g_assert (egg_asn1x_element_length (element, n_element) == n_element); + key = g_byte_array_new (); + g_byte_array_append (key, element, n_element); /* Already have this assertion? */ - assertion = g_hash_table_lookup (self->pv->assertions, element); + assertion = g_hash_table_lookup (self->pv->assertions, key); if (assertion) { g_object_ref (assertion); - g_hash_table_remove (self->pv->assertions, element); + g_hash_table_remove (self->pv->assertions, key); /* Create a new assertion */ } else { @@ -369,7 +372,8 @@ load_assertions (GkmXdgTrust *self, GNode *asn) } if (assertion) - g_hash_table_insert (assertions, g_memdup (element, n_element), assertion); + g_hash_table_insert (assertions, g_byte_array_ref (key), assertion); + g_byte_array_unref (key); } /* Override the stored assertions and netscape trust */ -- cgit v1.2.1