diff options
Diffstat (limited to 'src/util/crypto_hash_file.c')
-rw-r--r-- | src/util/crypto_hash_file.c | 164 |
1 files changed, 85 insertions, 79 deletions
diff --git a/src/util/crypto_hash_file.c b/src/util/crypto_hash_file.c index 78d0d0a0d..0dff6326c 100644 --- a/src/util/crypto_hash_file.c +++ b/src/util/crypto_hash_file.c | |||
@@ -27,15 +27,21 @@ | |||
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", \ |
31 | __VA_ARGS__) | ||
31 | 32 | ||
32 | #define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-crypto-hash-file", syscall, filename) | 33 | #define LOG_STRERROR_FILE(kind, syscall, \ |
34 | filename) GNUNET_log_from_strerror_file (kind, \ | ||
35 | "util-crypto-hash-file", \ | ||
36 | syscall, \ | ||
37 | filename) | ||
33 | 38 | ||
34 | 39 | ||
35 | /** | 40 | /** |
36 | * Context used when hashing a file. | 41 | * Context used when hashing a file. |
37 | */ | 42 | */ |
38 | struct GNUNET_CRYPTO_FileHashContext { | 43 | struct GNUNET_CRYPTO_FileHashContext |
44 | { | ||
39 | /** | 45 | /** |
40 | * Function to call upon completion. | 46 | * Function to call upon completion. |
41 | */ | 47 | */ |
@@ -79,7 +85,7 @@ struct GNUNET_CRYPTO_FileHashContext { | |||
79 | /** | 85 | /** |
80 | * Current task for hashing. | 86 | * Current task for hashing. |
81 | */ | 87 | */ |
82 | struct GNUNET_SCHEDULER_Task * task; | 88 | struct GNUNET_SCHEDULER_Task *task; |
83 | 89 | ||
84 | /** | 90 | /** |
85 | * Priority we use. | 91 | * Priority we use. |
@@ -98,15 +104,15 @@ struct GNUNET_CRYPTO_FileHashContext { | |||
98 | * and free associated resources. | 104 | * and free associated resources. |
99 | */ | 105 | */ |
100 | static void | 106 | static void |
101 | file_hash_finish(struct GNUNET_CRYPTO_FileHashContext *fhc, | 107 | file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc, |
102 | const struct GNUNET_HashCode * res) | 108 | const struct GNUNET_HashCode *res) |
103 | { | 109 | { |
104 | fhc->callback(fhc->callback_cls, res); | 110 | fhc->callback (fhc->callback_cls, res); |
105 | GNUNET_free(fhc->filename); | 111 | GNUNET_free (fhc->filename); |
106 | if (!GNUNET_DISK_handle_invalid(fhc->fh)) | 112 | if (! GNUNET_DISK_handle_invalid (fhc->fh)) |
107 | GNUNET_break(GNUNET_OK == GNUNET_DISK_file_close(fhc->fh)); | 113 | GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh)); |
108 | gcry_md_close(fhc->md); | 114 | gcry_md_close (fhc->md); |
109 | GNUNET_free(fhc); /* also frees fhc->buffer */ | 115 | GNUNET_free (fhc); /* also frees fhc->buffer */ |
110 | } | 116 | } |
111 | 117 | ||
112 | 118 | ||
@@ -116,7 +122,7 @@ file_hash_finish(struct GNUNET_CRYPTO_FileHashContext *fhc, | |||
116 | * @param cls closure | 122 | * @param cls closure |
117 | */ | 123 | */ |
118 | static void | 124 | static void |
119 | file_hash_task(void *cls) | 125 | file_hash_task (void *cls) |
120 | { | 126 | { |
121 | struct GNUNET_CRYPTO_FileHashContext *fhc = cls; | 127 | struct GNUNET_CRYPTO_FileHashContext *fhc = cls; |
122 | struct GNUNET_HashCode *res; | 128 | struct GNUNET_HashCode *res; |
@@ -124,37 +130,37 @@ file_hash_task(void *cls) | |||
124 | ssize_t sret; | 130 | ssize_t sret; |
125 | 131 | ||
126 | fhc->task = NULL; | 132 | fhc->task = NULL; |
127 | GNUNET_assert(fhc->offset <= fhc->fsize); | 133 | GNUNET_assert (fhc->offset <= fhc->fsize); |
128 | delta = fhc->bsize; | 134 | delta = fhc->bsize; |
129 | if (fhc->fsize - fhc->offset < delta) | 135 | if (fhc->fsize - fhc->offset < delta) |
130 | delta = fhc->fsize - fhc->offset; | 136 | delta = fhc->fsize - fhc->offset; |
131 | sret = GNUNET_DISK_file_read(fhc->fh, | 137 | sret = GNUNET_DISK_file_read (fhc->fh, |
132 | fhc->buffer, | 138 | fhc->buffer, |
133 | delta); | 139 | delta); |
134 | if ((sret < 0) || | 140 | if ((sret < 0) || |
135 | (delta != (size_t)sret)) | 141 | (delta != (size_t) sret)) |
136 | { | 142 | { |
137 | LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, | 143 | LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, |
138 | "read", | 144 | "read", |
139 | fhc->filename); | 145 | fhc->filename); |
140 | file_hash_finish(fhc, | 146 | file_hash_finish (fhc, |
141 | NULL); | 147 | NULL); |
142 | return; | 148 | return; |
143 | } | 149 | } |
144 | gcry_md_write(fhc->md, | 150 | gcry_md_write (fhc->md, |
145 | fhc->buffer, | 151 | fhc->buffer, |
146 | delta); | 152 | delta); |
147 | fhc->offset += delta; | 153 | fhc->offset += delta; |
148 | if (fhc->offset == fhc->fsize) | 154 | if (fhc->offset == fhc->fsize) |
149 | { | 155 | { |
150 | res = (struct GNUNET_HashCode *)gcry_md_read(fhc->md, | 156 | res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md, |
151 | GCRY_MD_SHA512); | 157 | GCRY_MD_SHA512); |
152 | file_hash_finish(fhc, res); | 158 | file_hash_finish (fhc, res); |
153 | return; | 159 | return; |
154 | } | 160 | } |
155 | fhc->task = GNUNET_SCHEDULER_add_with_priority(fhc->priority, | 161 | fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority, |
156 | &file_hash_task, | 162 | &file_hash_task, |
157 | fhc); | 163 | fhc); |
158 | } | 164 | } |
159 | 165 | ||
160 | 166 | ||
@@ -169,51 +175,51 @@ file_hash_task(void *cls) | |||
169 | * @return NULL on (immediate) errror | 175 | * @return NULL on (immediate) errror |
170 | */ | 176 | */ |
171 | struct GNUNET_CRYPTO_FileHashContext * | 177 | struct GNUNET_CRYPTO_FileHashContext * |
172 | GNUNET_CRYPTO_hash_file(enum GNUNET_SCHEDULER_Priority priority, | 178 | GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority, |
173 | const char *filename, | 179 | const char *filename, |
174 | size_t blocksize, | 180 | size_t blocksize, |
175 | GNUNET_CRYPTO_HashCompletedCallback callback, | 181 | GNUNET_CRYPTO_HashCompletedCallback callback, |
176 | void *callback_cls) | 182 | void *callback_cls) |
177 | { | 183 | { |
178 | struct GNUNET_CRYPTO_FileHashContext *fhc; | 184 | struct GNUNET_CRYPTO_FileHashContext *fhc; |
179 | 185 | ||
180 | GNUNET_assert(blocksize > 0); | 186 | GNUNET_assert (blocksize > 0); |
181 | fhc = | 187 | fhc = |
182 | GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize); | 188 | GNUNET_malloc (sizeof(struct GNUNET_CRYPTO_FileHashContext) + blocksize); |
183 | fhc->callback = callback; | 189 | fhc->callback = callback; |
184 | fhc->callback_cls = callback_cls; | 190 | fhc->callback_cls = callback_cls; |
185 | fhc->buffer = (unsigned char *)&fhc[1]; | 191 | fhc->buffer = (unsigned char *) &fhc[1]; |
186 | fhc->filename = GNUNET_strdup(filename); | 192 | fhc->filename = GNUNET_strdup (filename); |
187 | if (GPG_ERR_NO_ERROR != gcry_md_open(&fhc->md, GCRY_MD_SHA512, 0)) | 193 | if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0)) |
188 | { | 194 | { |
189 | GNUNET_break(0); | 195 | GNUNET_break (0); |
190 | GNUNET_free(fhc); | 196 | GNUNET_free (fhc); |
191 | return NULL; | 197 | return NULL; |
192 | } | 198 | } |
193 | fhc->bsize = blocksize; | 199 | fhc->bsize = blocksize; |
194 | if (GNUNET_OK != | 200 | if (GNUNET_OK != |
195 | GNUNET_DISK_file_size(filename, | 201 | GNUNET_DISK_file_size (filename, |
196 | &fhc->fsize, | 202 | &fhc->fsize, |
197 | GNUNET_NO, | 203 | GNUNET_NO, |
198 | GNUNET_YES)) | 204 | GNUNET_YES)) |
199 | { | 205 | { |
200 | GNUNET_free(fhc->filename); | 206 | GNUNET_free (fhc->filename); |
201 | GNUNET_free(fhc); | 207 | GNUNET_free (fhc); |
202 | return NULL; | 208 | return NULL; |
203 | } | 209 | } |
204 | fhc->fh = GNUNET_DISK_file_open(filename, | 210 | fhc->fh = GNUNET_DISK_file_open (filename, |
205 | GNUNET_DISK_OPEN_READ, | 211 | GNUNET_DISK_OPEN_READ, |
206 | GNUNET_DISK_PERM_NONE); | 212 | GNUNET_DISK_PERM_NONE); |
207 | if (!fhc->fh) | 213 | if (! fhc->fh) |
208 | { | 214 | { |
209 | GNUNET_free(fhc->filename); | 215 | GNUNET_free (fhc->filename); |
210 | GNUNET_free(fhc); | 216 | GNUNET_free (fhc); |
211 | return NULL; | 217 | return NULL; |
212 | } | 218 | } |
213 | fhc->priority = priority; | 219 | fhc->priority = priority; |
214 | fhc->task = GNUNET_SCHEDULER_add_with_priority(priority, | 220 | fhc->task = GNUNET_SCHEDULER_add_with_priority (priority, |
215 | &file_hash_task, | 221 | &file_hash_task, |
216 | fhc); | 222 | fhc); |
217 | return fhc; | 223 | return fhc; |
218 | } | 224 | } |
219 | 225 | ||
@@ -224,13 +230,13 @@ GNUNET_CRYPTO_hash_file(enum GNUNET_SCHEDULER_Priority priority, | |||
224 | * @param fhc operation to cancel (callback must not yet have been invoked) | 230 | * @param fhc operation to cancel (callback must not yet have been invoked) |
225 | */ | 231 | */ |
226 | void | 232 | void |
227 | GNUNET_CRYPTO_hash_file_cancel(struct GNUNET_CRYPTO_FileHashContext *fhc) | 233 | GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc) |
228 | { | 234 | { |
229 | GNUNET_SCHEDULER_cancel(fhc->task); | 235 | GNUNET_SCHEDULER_cancel (fhc->task); |
230 | GNUNET_free(fhc->filename); | 236 | GNUNET_free (fhc->filename); |
231 | GNUNET_break(GNUNET_OK == | 237 | GNUNET_break (GNUNET_OK == |
232 | GNUNET_DISK_file_close(fhc->fh)); | 238 | GNUNET_DISK_file_close (fhc->fh)); |
233 | GNUNET_free(fhc); | 239 | GNUNET_free (fhc); |
234 | } | 240 | } |
235 | 241 | ||
236 | /* end of crypto_hash_file.c */ | 242 | /* end of crypto_hash_file.c */ |