From f87d1c43bda33e1abc85661c51f1cb2652d10e1e Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 23 Nov 2010 22:56:30 +0000 Subject: [PATCH] [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 ++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) 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 3fd25ee..7b30f7f 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 0000000..6f9f736 --- /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 0000000..f03b34d --- /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_ */ -- 2.7.4