diff options
Diffstat (limited to 'src/util/bio.c')
-rw-r--r-- | src/util/bio.c | 384 |
1 files changed, 192 insertions, 192 deletions
diff --git a/src/util/bio.c b/src/util/bio.c index 1df249e40..5cbeee084 100644 --- a/src/util/bio.c +++ b/src/util/bio.c | |||
@@ -11,12 +11,12 @@ | |||
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 | * @file util/bio.c | 21 | * @file util/bio.c |
22 | * @brief functions for buffering IO | 22 | * @brief functions for buffering IO |
@@ -25,7 +25,7 @@ | |||
25 | #include "platform.h" | 25 | #include "platform.h" |
26 | #include "gnunet_util_lib.h" | 26 | #include "gnunet_util_lib.h" |
27 | 27 | ||
28 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-bio", __VA_ARGS__) | 28 | #define LOG(kind, ...) GNUNET_log_from(kind, "util-bio", __VA_ARGS__) |
29 | 29 | ||
30 | #ifndef PATH_MAX | 30 | #ifndef PATH_MAX |
31 | /** | 31 | /** |
@@ -50,8 +50,7 @@ | |||
50 | /** | 50 | /** |
51 | * Handle for buffered reading. | 51 | * Handle for buffered reading. |
52 | */ | 52 | */ |
53 | struct GNUNET_BIO_ReadHandle | 53 | struct GNUNET_BIO_ReadHandle { |
54 | { | ||
55 | /** | 54 | /** |
56 | * Underlying file abstraction. | 55 | * Underlying file abstraction. |
57 | */ | 56 | */ |
@@ -91,16 +90,16 @@ struct GNUNET_BIO_ReadHandle | |||
91 | * @return IO handle on success, NULL on error | 90 | * @return IO handle on success, NULL on error |
92 | */ | 91 | */ |
93 | struct GNUNET_BIO_ReadHandle * | 92 | struct GNUNET_BIO_ReadHandle * |
94 | GNUNET_BIO_read_open (const char *fn) | 93 | GNUNET_BIO_read_open(const char *fn) |
95 | { | 94 | { |
96 | struct GNUNET_DISK_FileHandle *fd; | 95 | struct GNUNET_DISK_FileHandle *fd; |
97 | struct GNUNET_BIO_ReadHandle *h; | 96 | struct GNUNET_BIO_ReadHandle *h; |
98 | 97 | ||
99 | fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); | 98 | fd = GNUNET_DISK_file_open(fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE); |
100 | if (NULL == fd) | 99 | if (NULL == fd) |
101 | return NULL; | 100 | return NULL; |
102 | h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); | 101 | h = GNUNET_malloc(sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); |
103 | h->buffer = (char *) &h[1]; | 102 | h->buffer = (char *)&h[1]; |
104 | h->size = BIO_BUFFER_SIZE; | 103 | h->size = BIO_BUFFER_SIZE; |
105 | h->fd = fd; | 104 | h->fd = fd; |
106 | return h; | 105 | return h; |
@@ -116,7 +115,7 @@ GNUNET_BIO_read_open (const char *fn) | |||
116 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise | 115 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise |
117 | */ | 116 | */ |
118 | int | 117 | int |
119 | GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) | 118 | GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg) |
120 | { | 119 | { |
121 | int err; | 120 | int err; |
122 | 121 | ||
@@ -124,9 +123,9 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) | |||
124 | if (emsg != NULL) | 123 | if (emsg != NULL) |
125 | *emsg = h->emsg; | 124 | *emsg = h->emsg; |
126 | else | 125 | else |
127 | GNUNET_free_non_null (h->emsg); | 126 | GNUNET_free_non_null(h->emsg); |
128 | GNUNET_DISK_file_close (h->fd); | 127 | GNUNET_DISK_file_close(h->fd); |
129 | GNUNET_free (h); | 128 | GNUNET_free(h); |
130 | return err; | 129 | return err; |
131 | } | 130 | } |
132 | 131 | ||
@@ -141,10 +140,10 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) | |||
141 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 140 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
142 | */ | 141 | */ |
143 | int | 142 | int |
144 | GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, | 143 | GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, |
145 | const char *what, | 144 | const char *what, |
146 | void *result, | 145 | void *result, |
147 | size_t len) | 146 | size_t len) |
148 | { | 147 | { |
149 | char *dst = result; | 148 | char *dst = result; |
150 | size_t min; | 149 | size_t min; |
@@ -155,41 +154,42 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, | |||
155 | return GNUNET_SYSERR; | 154 | return GNUNET_SYSERR; |
156 | pos = 0; | 155 | pos = 0; |
157 | do | 156 | do |
158 | { | ||
159 | /* first, use buffer */ | ||
160 | min = h->have - h->pos; | ||
161 | if (min > 0) | ||
162 | { | ||
163 | if (min > len - pos) | ||
164 | min = len - pos; | ||
165 | GNUNET_memcpy (&dst[pos], &h->buffer[h->pos], min); | ||
166 | h->pos += min; | ||
167 | pos += min; | ||
168 | } | ||
169 | if (pos == len) | ||
170 | return GNUNET_OK; /* done! */ | ||
171 | GNUNET_assert (((off_t) h->have) == h->pos); | ||
172 | /* fill buffer */ | ||
173 | ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); | ||
174 | if (-1 == ret) | ||
175 | { | ||
176 | GNUNET_asprintf (&h->emsg, | ||
177 | _ ("Error reading `%s': %s"), | ||
178 | what, | ||
179 | strerror (errno)); | ||
180 | return GNUNET_SYSERR; | ||
181 | } | ||
182 | if (0 == ret) | ||
183 | { | 157 | { |
184 | GNUNET_asprintf (&h->emsg, | 158 | /* first, use buffer */ |
185 | _ ("Error reading `%s': %s"), | 159 | min = h->have - h->pos; |
186 | what, | 160 | if (min > 0) |
187 | _ ("End of file")); | 161 | { |
188 | return GNUNET_SYSERR; | 162 | if (min > len - pos) |
163 | min = len - pos; | ||
164 | GNUNET_memcpy(&dst[pos], &h->buffer[h->pos], min); | ||
165 | h->pos += min; | ||
166 | pos += min; | ||
167 | } | ||
168 | if (pos == len) | ||
169 | return GNUNET_OK; /* done! */ | ||
170 | GNUNET_assert(((off_t)h->have) == h->pos); | ||
171 | /* fill buffer */ | ||
172 | ret = GNUNET_DISK_file_read(h->fd, h->buffer, h->size); | ||
173 | if (-1 == ret) | ||
174 | { | ||
175 | GNUNET_asprintf(&h->emsg, | ||
176 | _("Error reading `%s': %s"), | ||
177 | what, | ||
178 | strerror(errno)); | ||
179 | return GNUNET_SYSERR; | ||
180 | } | ||
181 | if (0 == ret) | ||
182 | { | ||
183 | GNUNET_asprintf(&h->emsg, | ||
184 | _("Error reading `%s': %s"), | ||
185 | what, | ||
186 | _("End of file")); | ||
187 | return GNUNET_SYSERR; | ||
188 | } | ||
189 | h->pos = 0; | ||
190 | h->have = ret; | ||
189 | } | 191 | } |
190 | h->pos = 0; | 192 | while (pos < len); /* should always be true */ |
191 | h->have = ret; | ||
192 | } while (pos < len); /* should always be true */ | ||
193 | return GNUNET_OK; | 193 | return GNUNET_OK; |
194 | } | 194 | } |
195 | 195 | ||
@@ -205,16 +205,16 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, | |||
205 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 205 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
206 | */ | 206 | */ |
207 | int | 207 | int |
208 | GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, | 208 | GNUNET_BIO_read_fn(struct GNUNET_BIO_ReadHandle *h, |
209 | const char *file, | 209 | const char *file, |
210 | int line, | 210 | int line, |
211 | void *result, | 211 | void *result, |
212 | size_t len) | 212 | size_t len) |
213 | { | 213 | { |
214 | char what[PATH_MAX + 1024]; | 214 | char what[PATH_MAX + 1024]; |
215 | 215 | ||
216 | GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line); | 216 | GNUNET_snprintf(what, sizeof(what), "%s:%d", file, line); |
217 | return GNUNET_BIO_read (h, what, result, len); | 217 | return GNUNET_BIO_read(h, what, result, len); |
218 | } | 218 | } |
219 | 219 | ||
220 | 220 | ||
@@ -229,45 +229,45 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, | |||
229 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 229 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
230 | */ | 230 | */ |
231 | int | 231 | int |
232 | GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, | 232 | GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h, |
233 | const char *what, | 233 | const char *what, |
234 | char **result, | 234 | char **result, |
235 | size_t max_length) | 235 | size_t max_length) |
236 | { | 236 | { |
237 | char *buf; | 237 | char *buf; |
238 | uint32_t big; | 238 | uint32_t big; |
239 | 239 | ||
240 | if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) | 240 | if (GNUNET_OK != GNUNET_BIO_read_int32(h, &big)) |
241 | { | 241 | { |
242 | GNUNET_free_non_null (h->emsg); | 242 | GNUNET_free_non_null(h->emsg); |
243 | GNUNET_asprintf (&h->emsg, _ ("Error reading length of string `%s'"), what); | 243 | GNUNET_asprintf(&h->emsg, _("Error reading length of string `%s'"), what); |
244 | return GNUNET_SYSERR; | 244 | return GNUNET_SYSERR; |
245 | } | 245 | } |
246 | if (0 == big) | 246 | if (0 == big) |
247 | { | 247 | { |
248 | *result = NULL; | 248 | *result = NULL; |
249 | return GNUNET_OK; | 249 | return GNUNET_OK; |
250 | } | 250 | } |
251 | if (big > max_length) | 251 | if (big > max_length) |
252 | { | 252 | { |
253 | GNUNET_asprintf (&h->emsg, | 253 | GNUNET_asprintf(&h->emsg, |
254 | _ ("String `%s' longer than allowed (%u > %u)"), | 254 | _("String `%s' longer than allowed (%u > %u)"), |
255 | what, | 255 | what, |
256 | big, | 256 | big, |
257 | max_length); | 257 | max_length); |
258 | return GNUNET_SYSERR; | 258 | return GNUNET_SYSERR; |
259 | } | 259 | } |
260 | buf = GNUNET_malloc (big); | 260 | buf = GNUNET_malloc(big); |
261 | *result = buf; | 261 | *result = buf; |
262 | buf[--big] = '\0'; | 262 | buf[--big] = '\0'; |
263 | if (0 == big) | 263 | if (0 == big) |
264 | return GNUNET_OK; | 264 | return GNUNET_OK; |
265 | if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) | 265 | if (GNUNET_OK != GNUNET_BIO_read(h, what, buf, big)) |
266 | { | 266 | { |
267 | GNUNET_free (buf); | 267 | GNUNET_free(buf); |
268 | *result = NULL; | 268 | *result = NULL; |
269 | return GNUNET_SYSERR; | 269 | return GNUNET_SYSERR; |
270 | } | 270 | } |
271 | return GNUNET_OK; | 271 | return GNUNET_OK; |
272 | } | 272 | } |
273 | 273 | ||
@@ -281,44 +281,44 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, | |||
281 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 281 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
282 | */ | 282 | */ |
283 | int | 283 | int |
284 | GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, | 284 | GNUNET_BIO_read_meta_data(struct GNUNET_BIO_ReadHandle *h, |
285 | const char *what, | 285 | const char *what, |
286 | struct GNUNET_CONTAINER_MetaData **result) | 286 | struct GNUNET_CONTAINER_MetaData **result) |
287 | { | 287 | { |
288 | uint32_t size; | 288 | uint32_t size; |
289 | char *buf; | 289 | char *buf; |
290 | struct GNUNET_CONTAINER_MetaData *meta; | 290 | struct GNUNET_CONTAINER_MetaData *meta; |
291 | 291 | ||
292 | if (GNUNET_OK != GNUNET_BIO_read_int32 (h, (int32_t *) &size)) | 292 | if (GNUNET_OK != GNUNET_BIO_read_int32(h, (int32_t *)&size)) |
293 | return GNUNET_SYSERR; | 293 | return GNUNET_SYSERR; |
294 | if (size == 0) | 294 | if (size == 0) |
295 | { | 295 | { |
296 | *result = NULL; | 296 | *result = NULL; |
297 | return GNUNET_OK; | 297 | return GNUNET_OK; |
298 | } | 298 | } |
299 | if (size > MAX_META_DATA) | 299 | if (size > MAX_META_DATA) |
300 | { | 300 | { |
301 | GNUNET_asprintf (&h->emsg, | 301 | GNUNET_asprintf(&h->emsg, |
302 | _ ("Serialized metadata `%s' larger than allowed (%u>%u)"), | 302 | _("Serialized metadata `%s' larger than allowed (%u>%u)"), |
303 | what, | 303 | what, |
304 | size, | 304 | size, |
305 | MAX_META_DATA); | 305 | MAX_META_DATA); |
306 | return GNUNET_SYSERR; | 306 | return GNUNET_SYSERR; |
307 | } | 307 | } |
308 | buf = GNUNET_malloc (size); | 308 | buf = GNUNET_malloc(size); |
309 | if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) | 309 | if (GNUNET_OK != GNUNET_BIO_read(h, what, buf, size)) |
310 | { | 310 | { |
311 | GNUNET_free (buf); | 311 | GNUNET_free(buf); |
312 | return GNUNET_SYSERR; | 312 | return GNUNET_SYSERR; |
313 | } | 313 | } |
314 | meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); | 314 | meta = GNUNET_CONTAINER_meta_data_deserialize(buf, size); |
315 | if (NULL == meta) | 315 | if (NULL == meta) |
316 | { | 316 | { |
317 | GNUNET_free (buf); | 317 | GNUNET_free(buf); |
318 | GNUNET_asprintf (&h->emsg, _ ("Metadata `%s' failed to deserialize"), what); | 318 | GNUNET_asprintf(&h->emsg, _("Metadata `%s' failed to deserialize"), what); |
319 | return GNUNET_SYSERR; | 319 | return GNUNET_SYSERR; |
320 | } | 320 | } |
321 | GNUNET_free (buf); | 321 | GNUNET_free(buf); |
322 | *result = meta; | 322 | *result = meta; |
323 | return GNUNET_OK; | 323 | return GNUNET_OK; |
324 | } | 324 | } |
@@ -334,16 +334,16 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, | |||
334 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 334 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
335 | */ | 335 | */ |
336 | int | 336 | int |
337 | GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, | 337 | GNUNET_BIO_read_int32__(struct GNUNET_BIO_ReadHandle *h, |
338 | const char *file, | 338 | const char *file, |
339 | int line, | 339 | int line, |
340 | int32_t *i) | 340 | int32_t *i) |
341 | { | 341 | { |
342 | int32_t big; | 342 | int32_t big; |
343 | 343 | ||
344 | if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t))) | 344 | if (GNUNET_OK != GNUNET_BIO_read_fn(h, file, line, &big, sizeof(int32_t))) |
345 | return GNUNET_SYSERR; | 345 | return GNUNET_SYSERR; |
346 | *i = ntohl (big); | 346 | *i = ntohl(big); |
347 | return GNUNET_OK; | 347 | return GNUNET_OK; |
348 | } | 348 | } |
349 | 349 | ||
@@ -358,16 +358,16 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, | |||
358 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 358 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
359 | */ | 359 | */ |
360 | int | 360 | int |
361 | GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, | 361 | GNUNET_BIO_read_int64__(struct GNUNET_BIO_ReadHandle *h, |
362 | const char *file, | 362 | const char *file, |
363 | int line, | 363 | int line, |
364 | int64_t *i) | 364 | int64_t *i) |
365 | { | 365 | { |
366 | int64_t big; | 366 | int64_t big; |
367 | 367 | ||
368 | if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t))) | 368 | if (GNUNET_OK != GNUNET_BIO_read_fn(h, file, line, &big, sizeof(int64_t))) |
369 | return GNUNET_SYSERR; | 369 | return GNUNET_SYSERR; |
370 | *i = GNUNET_ntohll (big); | 370 | *i = GNUNET_ntohll(big); |
371 | return GNUNET_OK; | 371 | return GNUNET_OK; |
372 | } | 372 | } |
373 | 373 | ||
@@ -375,8 +375,7 @@ GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, | |||
375 | /** | 375 | /** |
376 | * Handle for buffered writing. | 376 | * Handle for buffered writing. |
377 | */ | 377 | */ |
378 | struct GNUNET_BIO_WriteHandle | 378 | struct GNUNET_BIO_WriteHandle { |
379 | { | ||
380 | /** | 379 | /** |
381 | * Underlying file handle. | 380 | * Underlying file handle. |
382 | */ | 381 | */ |
@@ -406,21 +405,21 @@ struct GNUNET_BIO_WriteHandle | |||
406 | * @return IO handle on success, NULL on error | 405 | * @return IO handle on success, NULL on error |
407 | */ | 406 | */ |
408 | struct GNUNET_BIO_WriteHandle * | 407 | struct GNUNET_BIO_WriteHandle * |
409 | GNUNET_BIO_write_open (const char *fn) | 408 | GNUNET_BIO_write_open(const char *fn) |
410 | { | 409 | { |
411 | struct GNUNET_DISK_FileHandle *fd; | 410 | struct GNUNET_DISK_FileHandle *fd; |
412 | struct GNUNET_BIO_WriteHandle *h; | 411 | struct GNUNET_BIO_WriteHandle *h; |
413 | 412 | ||
414 | fd = | 413 | fd = |
415 | GNUNET_DISK_file_open (fn, | 414 | GNUNET_DISK_file_open(fn, |
416 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | | 415 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | |
417 | GNUNET_DISK_OPEN_CREATE, | 416 | GNUNET_DISK_OPEN_CREATE, |
418 | GNUNET_DISK_PERM_USER_READ | | 417 | GNUNET_DISK_PERM_USER_READ | |
419 | GNUNET_DISK_PERM_USER_WRITE); | 418 | GNUNET_DISK_PERM_USER_WRITE); |
420 | if (NULL == fd) | 419 | if (NULL == fd) |
421 | return NULL; | 420 | return NULL; |
422 | h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); | 421 | h = GNUNET_malloc(sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); |
423 | h->buffer = (char *) &h[1]; | 422 | h->buffer = (char *)&h[1]; |
424 | h->size = BIO_BUFFER_SIZE; | 423 | h->size = BIO_BUFFER_SIZE; |
425 | h->fd = fd; | 424 | h->fd = fd; |
426 | return h; | 425 | return h; |
@@ -434,14 +433,14 @@ GNUNET_BIO_write_open (const char *fn) | |||
434 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise | 433 | * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise |
435 | */ | 434 | */ |
436 | int | 435 | int |
437 | GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) | 436 | GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h) |
438 | { | 437 | { |
439 | int ret; | 438 | int ret; |
440 | 439 | ||
441 | ret = GNUNET_SYSERR; | 440 | ret = GNUNET_SYSERR; |
442 | if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h)))) | 441 | if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush(h)))) |
443 | GNUNET_DISK_file_close (h->fd); | 442 | GNUNET_DISK_file_close(h->fd); |
444 | GNUNET_free (h); | 443 | GNUNET_free(h); |
445 | return ret; | 444 | return ret; |
446 | } | 445 | } |
447 | 446 | ||
@@ -454,17 +453,17 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) | |||
454 | * the file is closed | 453 | * the file is closed |
455 | */ | 454 | */ |
456 | int | 455 | int |
457 | GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h) | 456 | GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h) |
458 | { | 457 | { |
459 | ssize_t ret; | 458 | ssize_t ret; |
460 | 459 | ||
461 | ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); | 460 | ret = GNUNET_DISK_file_write(h->fd, h->buffer, h->have); |
462 | if (ret != (ssize_t) h->have) | 461 | if (ret != (ssize_t)h->have) |
463 | { | 462 | { |
464 | GNUNET_DISK_file_close (h->fd); | 463 | GNUNET_DISK_file_close(h->fd); |
465 | h->fd = NULL; | 464 | h->fd = NULL; |
466 | return GNUNET_SYSERR; /* error */ | 465 | return GNUNET_SYSERR; /* error */ |
467 | } | 466 | } |
468 | h->have = 0; | 467 | h->have = 0; |
469 | return GNUNET_OK; | 468 | return GNUNET_OK; |
470 | } | 469 | } |
@@ -479,9 +478,9 @@ GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h) | |||
479 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 478 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
480 | */ | 479 | */ |
481 | int | 480 | int |
482 | GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, | 481 | GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, |
483 | const void *buffer, | 482 | const void *buffer, |
484 | size_t n) | 483 | size_t n) |
485 | { | 484 | { |
486 | const char *src = buffer; | 485 | const char *src = buffer; |
487 | size_t min; | 486 | size_t min; |
@@ -491,21 +490,22 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, | |||
491 | return GNUNET_SYSERR; | 490 | return GNUNET_SYSERR; |
492 | pos = 0; | 491 | pos = 0; |
493 | do | 492 | do |
494 | { | 493 | { |
495 | /* first, just use buffer */ | 494 | /* first, just use buffer */ |
496 | min = h->size - h->have; | 495 | min = h->size - h->have; |
497 | if (min > n - pos) | 496 | if (min > n - pos) |
498 | min = n - pos; | 497 | min = n - pos; |
499 | GNUNET_memcpy (&h->buffer[h->have], &src[pos], min); | 498 | GNUNET_memcpy(&h->buffer[h->have], &src[pos], min); |
500 | pos += min; | 499 | pos += min; |
501 | h->have += min; | 500 | h->have += min; |
502 | if (pos == n) | 501 | if (pos == n) |
503 | return GNUNET_OK; /* done */ | 502 | return GNUNET_OK; /* done */ |
504 | GNUNET_assert (h->have == h->size); | 503 | GNUNET_assert(h->have == h->size); |
505 | if (GNUNET_OK != GNUNET_BIO_flush (h)) | 504 | if (GNUNET_OK != GNUNET_BIO_flush(h)) |
506 | return GNUNET_SYSERR; /* error */ | 505 | return GNUNET_SYSERR; /* error */ |
507 | } while (pos < n); /* should always be true */ | 506 | } |
508 | GNUNET_break (0); | 507 | while (pos < n); /* should always be true */ |
508 | GNUNET_break(0); | ||
509 | return GNUNET_OK; | 509 | return GNUNET_OK; |
510 | } | 510 | } |
511 | 511 | ||
@@ -518,15 +518,15 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, | |||
518 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 518 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
519 | */ | 519 | */ |
520 | int | 520 | int |
521 | GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s) | 521 | GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *s) |
522 | { | 522 | { |
523 | uint32_t slen; | 523 | uint32_t slen; |
524 | 524 | ||
525 | slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1); | 525 | slen = (uint32_t)((s == NULL) ? 0 : strlen(s) + 1); |
526 | if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen)) | 526 | if (GNUNET_OK != GNUNET_BIO_write_int32(h, slen)) |
527 | return GNUNET_SYSERR; | 527 | return GNUNET_SYSERR; |
528 | if (0 != slen) | 528 | if (0 != slen) |
529 | return GNUNET_BIO_write (h, s, slen - 1); | 529 | return GNUNET_BIO_write(h, s, slen - 1); |
530 | return GNUNET_OK; | 530 | return GNUNET_OK; |
531 | } | 531 | } |
532 | 532 | ||
@@ -539,32 +539,32 @@ GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s) | |||
539 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 539 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
540 | */ | 540 | */ |
541 | int | 541 | int |
542 | GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, | 542 | GNUNET_BIO_write_meta_data(struct GNUNET_BIO_WriteHandle *h, |
543 | const struct GNUNET_CONTAINER_MetaData *m) | 543 | const struct GNUNET_CONTAINER_MetaData *m) |
544 | { | 544 | { |
545 | ssize_t size; | 545 | ssize_t size; |
546 | char *buf; | 546 | char *buf; |
547 | 547 | ||
548 | if (m == NULL) | 548 | if (m == NULL) |
549 | return GNUNET_BIO_write_int32 (h, 0); | 549 | return GNUNET_BIO_write_int32(h, 0); |
550 | buf = NULL; | 550 | buf = NULL; |
551 | size = GNUNET_CONTAINER_meta_data_serialize ( | 551 | size = GNUNET_CONTAINER_meta_data_serialize( |
552 | m, | 552 | m, |
553 | &buf, | 553 | &buf, |
554 | MAX_META_DATA, | 554 | MAX_META_DATA, |
555 | GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); | 555 | GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); |
556 | if (size == -1) | 556 | if (size == -1) |
557 | { | 557 | { |
558 | GNUNET_free (buf); | 558 | GNUNET_free(buf); |
559 | return GNUNET_SYSERR; | 559 | return GNUNET_SYSERR; |
560 | } | 560 | } |
561 | if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || | 561 | if ((GNUNET_OK != GNUNET_BIO_write_int32(h, (uint32_t)size)) || |
562 | (GNUNET_OK != GNUNET_BIO_write (h, buf, size))) | 562 | (GNUNET_OK != GNUNET_BIO_write(h, buf, size))) |
563 | { | 563 | { |
564 | GNUNET_free (buf); | 564 | GNUNET_free(buf); |
565 | return GNUNET_SYSERR; | 565 | return GNUNET_SYSERR; |
566 | } | 566 | } |
567 | GNUNET_free (buf); | 567 | GNUNET_free(buf); |
568 | return GNUNET_OK; | 568 | return GNUNET_OK; |
569 | } | 569 | } |
570 | 570 | ||
@@ -577,12 +577,12 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, | |||
577 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 577 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
578 | */ | 578 | */ |
579 | int | 579 | int |
580 | GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i) | 580 | GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, int32_t i) |
581 | { | 581 | { |
582 | int32_t big; | 582 | int32_t big; |
583 | 583 | ||
584 | big = htonl (i); | 584 | big = htonl(i); |
585 | return GNUNET_BIO_write (h, &big, sizeof (int32_t)); | 585 | return GNUNET_BIO_write(h, &big, sizeof(int32_t)); |
586 | } | 586 | } |
587 | 587 | ||
588 | 588 | ||
@@ -594,12 +594,12 @@ GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i) | |||
594 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 594 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
595 | */ | 595 | */ |
596 | int | 596 | int |
597 | GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i) | 597 | GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, int64_t i) |
598 | { | 598 | { |
599 | int64_t big; | 599 | int64_t big; |
600 | 600 | ||
601 | big = GNUNET_htonll (i); | 601 | big = GNUNET_htonll(i); |
602 | return GNUNET_BIO_write (h, &big, sizeof (int64_t)); | 602 | return GNUNET_BIO_write(h, &big, sizeof(int64_t)); |
603 | } | 603 | } |
604 | 604 | ||
605 | 605 | ||