rehash-util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
  3. This file is part of GNU Guix.
  4. GNU Guix is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Guix is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <gnunet/gnunet_config.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include "rehash_types.h"
  18. #include "rehash_util.h"
  19. #include "fs_api.h"
  20. static const int hash_lengths[] = {
  21. [GNUNET_REHASH_HASH_NONE] = 0,
  22. [GNUNET_REHASH_HASH_SHA256] = 32,
  23. [GNUNET_REHASH_HASH_SHA512] = 64,
  24. [GNUNET_REHASH_HASH_SHA3_256] = 32,
  25. [GNUNET_REHASH_HASH_SHA3_512] = 64,
  26. /* TODO check */
  27. [GNUNET_REHASH_HASH_BLAKE2S_256] = 32,
  28. /* */
  29. [GNUNET_REHASH_HASH_FS_REGULAR] = sizeof(struct FileIdentifier),
  30. };
  31. static const char hash_names[][12] = {
  32. [GNUNET_REHASH_HASH_SHA256] = "sha256",
  33. [GNUNET_REHASH_HASH_SHA512] = "sha512",
  34. [GNUNET_REHASH_HASH_SHA3_256] = "sha3-256",
  35. [GNUNET_REHASH_HASH_SHA3_512] = "sha3-512",
  36. [GNUNET_REHASH_HASH_BLAKE2S_256] = "blake2s-256",
  37. [GNUNET_REHASH_HASH_FS_REGULAR] = "fs-regular",
  38. };
  39. int
  40. GNUNET_REHASH_hashtype_strsize (enum GNUNET_REHASH_Hash_Type type)
  41. {
  42. int index;
  43. index = type > GNUNET_REHASH_HASH_LAST ? 0 : (int) type;
  44. return hash_lengths[index];
  45. }
  46. int
  47. GNUNET_REHASH_unparse_hashtype (enum GNUNET_REHASH_Hash_Type type, int size, char outbuf[])
  48. {
  49. if ((type == GNUNET_REHASH_HASH_NONE) || (type > GNUNET_REHASH_HASH_LAST))
  50. return 0;
  51. strncpy(outbuf, hash_names[type], size);
  52. return size;
  53. }
  54. enum GNUNET_REHASH_Hash_Type
  55. GNUNET_REHASH_parse_hashtype (const char *name)
  56. {
  57. enum GNUNET_REHASH_Hash_Type candidate;
  58. for (candidate = 1; candidate <= GNUNET_REHASH_HASH_LAST; candidate++)
  59. {
  60. if (0 == strcmp(name, hash_names[(int) candidate]))
  61. return candidate;
  62. }
  63. return GNUNET_REHASH_HASH_NONE;
  64. }