summaryrefslogtreecommitdiff
path: root/egg/egg-byte-array.c
blob: abada4e437db0fe9c7bc74073c11368c35f51392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * 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 Public 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 Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Stef Walter <stefw@collabora.co.uk>
 */

#include "egg-byte-array.h"

#include <string.h>

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);
}