shim_fs_hash.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * This file contains functions to generate hash values for FS paths.
  15. */
  16. #include <shim_internal.h>
  17. static HASHTYPE __hash(const char* p, size_t len) {
  18. HASHTYPE hash = 0;
  19. for (; len >= sizeof(hash); p += sizeof(hash), len -= sizeof(hash)) {
  20. hash += *((HASHTYPE*)p);
  21. hash *= 9;
  22. }
  23. if (len) {
  24. HASHTYPE rest = 0;
  25. for (; len > 0; p++, len--) {
  26. rest <<= 8;
  27. rest += (HASHTYPE)*p;
  28. }
  29. hash += rest;
  30. hash *= 9;
  31. }
  32. return hash;
  33. }
  34. HASHTYPE hash_path(const char* path, size_t size) {
  35. HASHTYPE digest = 0;
  36. const char* elem_start = path;
  37. const char* c = path;
  38. for (; c < path + size && *c; c++) {
  39. if (*c == '/') {
  40. digest ^= __hash(elem_start, c - elem_start);
  41. elem_start = c + 1;
  42. }
  43. }
  44. digest ^= __hash(elem_start, c - elem_start);
  45. return digest;
  46. }
  47. HASHTYPE rehash_name(HASHTYPE parent_hbuf, const char* name, size_t size) {
  48. return parent_hbuf ^ __hash(name, size);
  49. }
  50. HASHTYPE rehash_path(HASHTYPE ancestor_hbuf, const char* path, size_t size) {
  51. return ancestor_hbuf ^ hash_path(path, size);
  52. }