hashstr.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * feature.c --- convert between features and strings
  3. *
  4. * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
  5. *
  6. * This file can be redistributed under the terms of the GNU Library General
  7. * Public License
  8. *
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <errno.h>
  15. #include "e2p.h"
  16. struct hash {
  17. int num;
  18. const char *string;
  19. };
  20. static struct hash hash_list[] = {
  21. { EXT2_HASH_LEGACY, "legacy" },
  22. { EXT2_HASH_HALF_MD4, "half_md4" },
  23. { EXT2_HASH_TEA, "tea" },
  24. { 0, 0 },
  25. };
  26. const char *e2p_hash2string(int num)
  27. {
  28. struct hash *p;
  29. static char buf[20];
  30. for (p = hash_list; p->string; p++) {
  31. if (num == p->num)
  32. return p->string;
  33. }
  34. sprintf(buf, "HASHALG_%d", num);
  35. return buf;
  36. }
  37. /*
  38. * Returns the hash algorithm, or -1 on error
  39. */
  40. int e2p_string2hash(char *string)
  41. {
  42. struct hash *p;
  43. char *eptr;
  44. int num;
  45. for (p = hash_list; p->string; p++) {
  46. if (!strcasecmp(string, p->string)) {
  47. return p->num;
  48. }
  49. }
  50. if (strncasecmp(string, "HASHALG_", 8))
  51. return -1;
  52. if (string[8] == 0)
  53. return -1;
  54. num = strtol(string+8, &eptr, 10);
  55. if (num > 255 || num < 0)
  56. return -1;
  57. if (*eptr)
  58. return -1;
  59. return num;
  60. }