blob: dc2ce94165d36a4dd164b4ddacc916f63453236a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Implementation of the symbol table type.
4 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04005 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/string.h>
9#include <linux/errno.h>
10#include "symtab.h"
11
Chad Sellersbb242492006-11-06 12:38:17 -050012static unsigned int symhash(struct hashtab *h, const void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
Chad Sellersbb242492006-11-06 12:38:17 -050014 const char *p, *keyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 unsigned int size;
16 unsigned int val;
17
18 val = 0;
19 keyp = key;
20 size = strlen(keyp);
21 for (p = keyp; (p - keyp) < size; p++)
22 val = (val << 4 | (val >> (8*sizeof(unsigned int)-4))) ^ (*p);
23 return val & (h->size - 1);
24}
25
Chad Sellersbb242492006-11-06 12:38:17 -050026static int symcmp(struct hashtab *h, const void *key1, const void *key2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Chad Sellersbb242492006-11-06 12:38:17 -050028 const char *keyp1, *keyp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 keyp1 = key1;
31 keyp2 = key2;
32 return strcmp(keyp1, keyp2);
33}
34
35
36int symtab_init(struct symtab *s, unsigned int size)
37{
38 s->table = hashtab_create(symhash, symcmp, size);
39 if (!s->table)
Dan Carpenter9a798272010-06-12 20:57:39 +020040 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 s->nprim = 0;
42 return 0;
43}
44