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