symtab.c 885 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implementation of the symbol table type.
  4. *
  5. * Author : Stephen Smalley, <sds@tycho.nsa.gov>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/string.h>
  9. #include <linux/errno.h>
  10. #include "symtab.h"
  11. static unsigned int symhash(struct hashtab *h, const void *key)
  12. {
  13. const char *p, *keyp;
  14. unsigned int size;
  15. unsigned int val;
  16. val = 0;
  17. keyp = key;
  18. size = strlen(keyp);
  19. for (p = keyp; (p - keyp) < size; p++)
  20. val = (val << 4 | (val >> (8*sizeof(unsigned int)-4))) ^ (*p);
  21. return val & (h->size - 1);
  22. }
  23. static int symcmp(struct hashtab *h, const void *key1, const void *key2)
  24. {
  25. const char *keyp1, *keyp2;
  26. keyp1 = key1;
  27. keyp2 = key2;
  28. return strcmp(keyp1, keyp2);
  29. }
  30. int symtab_init(struct symtab *s, unsigned int size)
  31. {
  32. s->table = hashtab_create(symhash, symcmp, size);
  33. if (!s->table)
  34. return -ENOMEM;
  35. s->nprim = 0;
  36. return 0;
  37. }