crypto_hash_file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet 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 GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/crypto_hash_file.c
  18. * @brief incremental hashing of files
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include <gcrypt.h>
  24. #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hash-file", \
  25. __VA_ARGS__)
  26. #define LOG_STRERROR_FILE(kind, syscall, \
  27. filename) GNUNET_log_from_strerror_file (kind, \
  28. "util-crypto-hash-file", \
  29. syscall, \
  30. filename)
  31. /**
  32. * Context used when hashing a file.
  33. */
  34. struct GNUNET_CRYPTO_FileHashContext
  35. {
  36. /**
  37. * Function to call upon completion.
  38. */
  39. GNUNET_CRYPTO_HashCompletedCallback callback;
  40. /**
  41. * Closure for callback.
  42. */
  43. void *callback_cls;
  44. /**
  45. * IO buffer.
  46. */
  47. unsigned char *buffer;
  48. /**
  49. * Name of the file we are hashing.
  50. */
  51. char *filename;
  52. /**
  53. * File descriptor.
  54. */
  55. struct GNUNET_DISK_FileHandle *fh;
  56. /**
  57. * Cummulated hash.
  58. */
  59. gcry_md_hd_t md;
  60. /**
  61. * Size of the file.
  62. */
  63. uint64_t fsize;
  64. /**
  65. * Current offset.
  66. */
  67. uint64_t offset;
  68. /**
  69. * Current task for hashing.
  70. */
  71. struct GNUNET_SCHEDULER_Task *task;
  72. /**
  73. * Priority we use.
  74. */
  75. enum GNUNET_SCHEDULER_Priority priority;
  76. /**
  77. * Blocksize.
  78. */
  79. size_t bsize;
  80. };
  81. /**
  82. * Report result of hash computation to callback
  83. * and free associated resources.
  84. */
  85. static void
  86. file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
  87. const struct GNUNET_HashCode *res)
  88. {
  89. fhc->callback (fhc->callback_cls, res);
  90. GNUNET_free (fhc->filename);
  91. if (! GNUNET_DISK_handle_invalid (fhc->fh))
  92. GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
  93. gcry_md_close (fhc->md);
  94. GNUNET_free (fhc); /* also frees fhc->buffer */
  95. }
  96. /**
  97. * File hashing task.
  98. *
  99. * @param cls closure
  100. */
  101. static void
  102. file_hash_task (void *cls)
  103. {
  104. struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
  105. struct GNUNET_HashCode *res;
  106. size_t delta;
  107. ssize_t sret;
  108. fhc->task = NULL;
  109. GNUNET_assert (fhc->offset <= fhc->fsize);
  110. delta = fhc->bsize;
  111. if (fhc->fsize - fhc->offset < delta)
  112. delta = fhc->fsize - fhc->offset;
  113. sret = GNUNET_DISK_file_read (fhc->fh,
  114. fhc->buffer,
  115. delta);
  116. if ((sret < 0) ||
  117. (delta != (size_t) sret))
  118. {
  119. LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
  120. "read",
  121. fhc->filename);
  122. file_hash_finish (fhc,
  123. NULL);
  124. return;
  125. }
  126. gcry_md_write (fhc->md,
  127. fhc->buffer,
  128. delta);
  129. fhc->offset += delta;
  130. if (fhc->offset == fhc->fsize)
  131. {
  132. res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md,
  133. GCRY_MD_SHA512);
  134. file_hash_finish (fhc, res);
  135. return;
  136. }
  137. fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
  138. &file_hash_task,
  139. fhc);
  140. }
  141. /**
  142. * Compute the hash of an entire file.
  143. *
  144. * @param priority scheduling priority to use
  145. * @param filename name of file to hash
  146. * @param blocksize number of bytes to process in one task
  147. * @param callback function to call upon completion
  148. * @param callback_cls closure for @a callback
  149. * @return NULL on (immediate) error
  150. */
  151. struct GNUNET_CRYPTO_FileHashContext *
  152. GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
  153. const char *filename,
  154. size_t blocksize,
  155. GNUNET_CRYPTO_HashCompletedCallback callback,
  156. void *callback_cls)
  157. {
  158. struct GNUNET_CRYPTO_FileHashContext *fhc;
  159. GNUNET_assert (blocksize > 0);
  160. fhc =
  161. GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize);
  162. fhc->callback = callback;
  163. fhc->callback_cls = callback_cls;
  164. fhc->buffer = (unsigned char *) &fhc[1];
  165. fhc->filename = GNUNET_strdup (filename);
  166. if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
  167. {
  168. GNUNET_break (0);
  169. GNUNET_free (fhc);
  170. return NULL;
  171. }
  172. fhc->bsize = blocksize;
  173. if (GNUNET_OK !=
  174. GNUNET_DISK_file_size (filename,
  175. &fhc->fsize,
  176. GNUNET_NO,
  177. GNUNET_YES))
  178. {
  179. GNUNET_free (fhc->filename);
  180. GNUNET_free (fhc);
  181. return NULL;
  182. }
  183. fhc->fh = GNUNET_DISK_file_open (filename,
  184. GNUNET_DISK_OPEN_READ,
  185. GNUNET_DISK_PERM_NONE);
  186. if (! fhc->fh)
  187. {
  188. GNUNET_free (fhc->filename);
  189. GNUNET_free (fhc);
  190. return NULL;
  191. }
  192. fhc->priority = priority;
  193. fhc->task = GNUNET_SCHEDULER_add_with_priority (priority,
  194. &file_hash_task,
  195. fhc);
  196. return fhc;
  197. }
  198. /**
  199. * Cancel a file hashing operation.
  200. *
  201. * @param fhc operation to cancel (callback must not yet have been invoked)
  202. */
  203. void
  204. GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
  205. {
  206. GNUNET_SCHEDULER_cancel (fhc->task);
  207. GNUNET_free (fhc->filename);
  208. GNUNET_break (GNUNET_OK ==
  209. GNUNET_DISK_file_close (fhc->fh));
  210. GNUNET_free (fhc);
  211. }
  212. /* end of crypto_hash_file.c */