3
0

hashstr.c 1.2 KB

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