summaryrefslogtreecommitdiff
path: root/src/util/crypto_hash_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/crypto_hash_file.c')
-rw-r--r--src/util/crypto_hash_file.c167
1 files changed, 82 insertions, 85 deletions
diff --git a/src/util/crypto_hash_file.c b/src/util/crypto_hash_file.c
index 83afe5e91..78d0d0a0d 100644
--- a/src/util/crypto_hash_file.c
+++ b/src/util/crypto_hash_file.c
@@ -11,13 +11,13 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 19
20*/ 20 */
21/** 21/**
22 * @file util/crypto_hash_file.c 22 * @file util/crypto_hash_file.c
23 * @brief incremental hashing of files 23 * @brief incremental hashing of files
@@ -27,17 +27,15 @@
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include <gcrypt.h> 28#include <gcrypt.h>
29 29
30#define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-hash-file", __VA_ARGS__) 30#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-hash-file", __VA_ARGS__)
31 31
32#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-hash-file", syscall, filename) 32#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-crypto-hash-file", syscall, filename)
33 33
34 34
35/** 35/**
36 * Context used when hashing a file. 36 * Context used when hashing a file.
37 */ 37 */
38struct GNUNET_CRYPTO_FileHashContext 38struct GNUNET_CRYPTO_FileHashContext {
39{
40
41 /** 39 /**
42 * Function to call upon completion. 40 * Function to call upon completion.
43 */ 41 */
@@ -92,7 +90,6 @@ struct GNUNET_CRYPTO_FileHashContext
92 * Blocksize. 90 * Blocksize.
93 */ 91 */
94 size_t bsize; 92 size_t bsize;
95
96}; 93};
97 94
98 95
@@ -101,15 +98,15 @@ struct GNUNET_CRYPTO_FileHashContext
101 * and free associated resources. 98 * and free associated resources.
102 */ 99 */
103static void 100static void
104file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc, 101file_hash_finish(struct GNUNET_CRYPTO_FileHashContext *fhc,
105 const struct GNUNET_HashCode * res) 102 const struct GNUNET_HashCode * res)
106{ 103{
107 fhc->callback (fhc->callback_cls, res); 104 fhc->callback(fhc->callback_cls, res);
108 GNUNET_free (fhc->filename); 105 GNUNET_free(fhc->filename);
109 if (!GNUNET_DISK_handle_invalid (fhc->fh)) 106 if (!GNUNET_DISK_handle_invalid(fhc->fh))
110 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh)); 107 GNUNET_break(GNUNET_OK == GNUNET_DISK_file_close(fhc->fh));
111 gcry_md_close (fhc->md); 108 gcry_md_close(fhc->md);
112 GNUNET_free (fhc); /* also frees fhc->buffer */ 109 GNUNET_free(fhc); /* also frees fhc->buffer */
113} 110}
114 111
115 112
@@ -119,7 +116,7 @@ file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
119 * @param cls closure 116 * @param cls closure
120 */ 117 */
121static void 118static void
122file_hash_task (void *cls) 119file_hash_task(void *cls)
123{ 120{
124 struct GNUNET_CRYPTO_FileHashContext *fhc = cls; 121 struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
125 struct GNUNET_HashCode *res; 122 struct GNUNET_HashCode *res;
@@ -127,37 +124,37 @@ file_hash_task (void *cls)
127 ssize_t sret; 124 ssize_t sret;
128 125
129 fhc->task = NULL; 126 fhc->task = NULL;
130 GNUNET_assert (fhc->offset <= fhc->fsize); 127 GNUNET_assert(fhc->offset <= fhc->fsize);
131 delta = fhc->bsize; 128 delta = fhc->bsize;
132 if (fhc->fsize - fhc->offset < delta) 129 if (fhc->fsize - fhc->offset < delta)
133 delta = fhc->fsize - fhc->offset; 130 delta = fhc->fsize - fhc->offset;
134 sret = GNUNET_DISK_file_read (fhc->fh, 131 sret = GNUNET_DISK_file_read(fhc->fh,
135 fhc->buffer, 132 fhc->buffer,
136 delta); 133 delta);
137 if ( (sret < 0) || 134 if ((sret < 0) ||
138 (delta != (size_t) sret) ) 135 (delta != (size_t)sret))
139 { 136 {
140 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, 137 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING,
141 "read", 138 "read",
142 fhc->filename); 139 fhc->filename);
143 file_hash_finish (fhc, 140 file_hash_finish(fhc,
144 NULL); 141 NULL);
145 return; 142 return;
146 } 143 }
147 gcry_md_write (fhc->md, 144 gcry_md_write(fhc->md,
148 fhc->buffer, 145 fhc->buffer,
149 delta); 146 delta);
150 fhc->offset += delta; 147 fhc->offset += delta;
151 if (fhc->offset == fhc->fsize) 148 if (fhc->offset == fhc->fsize)
152 { 149 {
153 res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md, 150 res = (struct GNUNET_HashCode *)gcry_md_read(fhc->md,
154 GCRY_MD_SHA512); 151 GCRY_MD_SHA512);
155 file_hash_finish (fhc, res); 152 file_hash_finish(fhc, res);
156 return; 153 return;
157 } 154 }
158 fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority, 155 fhc->task = GNUNET_SCHEDULER_add_with_priority(fhc->priority,
159 &file_hash_task, 156 &file_hash_task,
160 fhc); 157 fhc);
161} 158}
162 159
163 160
@@ -172,51 +169,51 @@ file_hash_task (void *cls)
172 * @return NULL on (immediate) errror 169 * @return NULL on (immediate) errror
173 */ 170 */
174struct GNUNET_CRYPTO_FileHashContext * 171struct GNUNET_CRYPTO_FileHashContext *
175GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority, 172GNUNET_CRYPTO_hash_file(enum GNUNET_SCHEDULER_Priority priority,
176 const char *filename, 173 const char *filename,
177 size_t blocksize, 174 size_t blocksize,
178 GNUNET_CRYPTO_HashCompletedCallback callback, 175 GNUNET_CRYPTO_HashCompletedCallback callback,
179 void *callback_cls) 176 void *callback_cls)
180{ 177{
181 struct GNUNET_CRYPTO_FileHashContext *fhc; 178 struct GNUNET_CRYPTO_FileHashContext *fhc;
182 179
183 GNUNET_assert (blocksize > 0); 180 GNUNET_assert(blocksize > 0);
184 fhc = 181 fhc =
185 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize); 182 GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize);
186 fhc->callback = callback; 183 fhc->callback = callback;
187 fhc->callback_cls = callback_cls; 184 fhc->callback_cls = callback_cls;
188 fhc->buffer = (unsigned char *) &fhc[1]; 185 fhc->buffer = (unsigned char *)&fhc[1];
189 fhc->filename = GNUNET_strdup (filename); 186 fhc->filename = GNUNET_strdup(filename);
190 if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0)) 187 if (GPG_ERR_NO_ERROR != gcry_md_open(&fhc->md, GCRY_MD_SHA512, 0))
191 { 188 {
192 GNUNET_break (0); 189 GNUNET_break(0);
193 GNUNET_free (fhc); 190 GNUNET_free(fhc);
194 return NULL; 191 return NULL;
195 } 192 }
196 fhc->bsize = blocksize; 193 fhc->bsize = blocksize;
197 if (GNUNET_OK != 194 if (GNUNET_OK !=
198 GNUNET_DISK_file_size (filename, 195 GNUNET_DISK_file_size(filename,
199 &fhc->fsize, 196 &fhc->fsize,
200 GNUNET_NO, 197 GNUNET_NO,
201 GNUNET_YES)) 198 GNUNET_YES))
202 { 199 {
203 GNUNET_free (fhc->filename); 200 GNUNET_free(fhc->filename);
204 GNUNET_free (fhc); 201 GNUNET_free(fhc);
205 return NULL; 202 return NULL;
206 } 203 }
207 fhc->fh = GNUNET_DISK_file_open (filename, 204 fhc->fh = GNUNET_DISK_file_open(filename,
208 GNUNET_DISK_OPEN_READ, 205 GNUNET_DISK_OPEN_READ,
209 GNUNET_DISK_PERM_NONE); 206 GNUNET_DISK_PERM_NONE);
210 if (! fhc->fh) 207 if (!fhc->fh)
211 { 208 {
212 GNUNET_free (fhc->filename); 209 GNUNET_free(fhc->filename);
213 GNUNET_free (fhc); 210 GNUNET_free(fhc);
214 return NULL; 211 return NULL;
215 } 212 }
216 fhc->priority = priority; 213 fhc->priority = priority;
217 fhc->task = GNUNET_SCHEDULER_add_with_priority (priority, 214 fhc->task = GNUNET_SCHEDULER_add_with_priority(priority,
218 &file_hash_task, 215 &file_hash_task,
219 fhc); 216 fhc);
220 return fhc; 217 return fhc;
221} 218}
222 219
@@ -227,13 +224,13 @@ GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
227 * @param fhc operation to cancel (callback must not yet have been invoked) 224 * @param fhc operation to cancel (callback must not yet have been invoked)
228 */ 225 */
229void 226void
230GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc) 227GNUNET_CRYPTO_hash_file_cancel(struct GNUNET_CRYPTO_FileHashContext *fhc)
231{ 228{
232 GNUNET_SCHEDULER_cancel (fhc->task); 229 GNUNET_SCHEDULER_cancel(fhc->task);
233 GNUNET_free (fhc->filename); 230 GNUNET_free(fhc->filename);
234 GNUNET_break (GNUNET_OK == 231 GNUNET_break(GNUNET_OK ==
235 GNUNET_DISK_file_close (fhc->fh)); 232 GNUNET_DISK_file_close(fhc->fh));
236 GNUNET_free (fhc); 233 GNUNET_free(fhc);
237} 234}
238 235
239/* end of crypto_hash_file.c */ 236/* end of crypto_hash_file.c */