aboutsummaryrefslogtreecommitdiff
path: root/src/util/bio.c
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-06 22:46:29 +0000
committerng0 <ng0@n0.is>2019-09-06 22:46:29 +0000
commit6e599264ad13e8fc105493d74d7c11d46f8739ed (patch)
tree169bef1ecbade5a659831fb169f3ae6943af127f /src/util/bio.c
parent4f13bc15113021ebf71d5d81e99bc29f8a07fc9c (diff)
downloadgnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.tar.gz
gnunet-6e599264ad13e8fc105493d74d7c11d46f8739ed.zip
first step to remove plibc
Diffstat (limited to 'src/util/bio.c')
-rw-r--r--src/util/bio.c133
1 files changed, 53 insertions, 80 deletions
diff --git a/src/util/bio.c b/src/util/bio.c
index def9b02e1..1df249e40 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -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/**
@@ -116,8 +116,7 @@ GNUNET_BIO_read_open (const char *fn)
116 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 116 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
117 */ 117 */
118int 118int
119GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, 119GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
120 char **emsg)
121{ 120{
122 int err; 121 int err;
123 122
@@ -144,7 +143,8 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
144int 143int
145GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, 144GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
146 const char *what, 145 const char *what,
147 void *result, size_t len) 146 void *result,
147 size_t len)
148{ 148{
149 char *dst = result; 149 char *dst = result;
150 size_t min; 150 size_t min;
@@ -162,39 +162,34 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
162 { 162 {
163 if (min > len - pos) 163 if (min > len - pos)
164 min = len - pos; 164 min = len - pos;
165 GNUNET_memcpy (&dst[pos], 165 GNUNET_memcpy (&dst[pos], &h->buffer[h->pos], min);
166 &h->buffer[h->pos],
167 min);
168 h->pos += min; 166 h->pos += min;
169 pos += min; 167 pos += min;
170 } 168 }
171 if (pos == len) 169 if (pos == len)
172 return GNUNET_OK; /* done! */ 170 return GNUNET_OK; /* done! */
173 GNUNET_assert (((off_t) h->have) == h->pos); 171 GNUNET_assert (((off_t) h->have) == h->pos);
174 /* fill buffer */ 172 /* fill buffer */
175 ret = GNUNET_DISK_file_read (h->fd, 173 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
176 h->buffer,
177 h->size);
178 if (-1 == ret) 174 if (-1 == ret)
179 { 175 {
180 GNUNET_asprintf (&h->emsg, 176 GNUNET_asprintf (&h->emsg,
181 _("Error reading `%s': %s"), 177 _ ("Error reading `%s': %s"),
182 what, 178 what,
183 STRERROR (errno)); 179 strerror (errno));
184 return GNUNET_SYSERR; 180 return GNUNET_SYSERR;
185 } 181 }
186 if (0 == ret) 182 if (0 == ret)
187 { 183 {
188 GNUNET_asprintf (&h->emsg, 184 GNUNET_asprintf (&h->emsg,
189 _("Error reading `%s': %s"), 185 _ ("Error reading `%s': %s"),
190 what, 186 what,
191 _("End of file")); 187 _ ("End of file"));
192 return GNUNET_SYSERR; 188 return GNUNET_SYSERR;
193 } 189 }
194 h->pos = 0; 190 h->pos = 0;
195 h->have = ret; 191 h->have = ret;
196 } 192 } while (pos < len); /* should always be true */
197 while (pos < len); /* should always be true */
198 return GNUNET_OK; 193 return GNUNET_OK;
199} 194}
200 195
@@ -245,7 +240,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
245 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) 240 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
246 { 241 {
247 GNUNET_free_non_null (h->emsg); 242 GNUNET_free_non_null (h->emsg);
248 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what); 243 GNUNET_asprintf (&h->emsg, _ ("Error reading length of string `%s'"), what);
249 return GNUNET_SYSERR; 244 return GNUNET_SYSERR;
250 } 245 }
251 if (0 == big) 246 if (0 == big)
@@ -255,8 +250,11 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
255 } 250 }
256 if (big > max_length) 251 if (big > max_length)
257 { 252 {
258 GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"), 253 GNUNET_asprintf (&h->emsg,
259 what, big, max_length); 254 _ ("String `%s' longer than allowed (%u > %u)"),
255 what,
256 big,
257 max_length);
260 return GNUNET_SYSERR; 258 return GNUNET_SYSERR;
261 } 259 }
262 buf = GNUNET_malloc (big); 260 buf = GNUNET_malloc (big);
@@ -291,9 +289,7 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
291 char *buf; 289 char *buf;
292 struct GNUNET_CONTAINER_MetaData *meta; 290 struct GNUNET_CONTAINER_MetaData *meta;
293 291
294 if (GNUNET_OK != 292 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, (int32_t *) &size))
295 GNUNET_BIO_read_int32 (h,
296 (int32_t *) & size))
297 return GNUNET_SYSERR; 293 return GNUNET_SYSERR;
298 if (size == 0) 294 if (size == 0)
299 { 295 {
@@ -303,30 +299,23 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
303 if (size > MAX_META_DATA) 299 if (size > MAX_META_DATA)
304 { 300 {
305 GNUNET_asprintf (&h->emsg, 301 GNUNET_asprintf (&h->emsg,
306 _("Serialized metadata `%s' larger than allowed (%u>%u)"), 302 _ ("Serialized metadata `%s' larger than allowed (%u>%u)"),
307 what, 303 what,
308 size, 304 size,
309 MAX_META_DATA); 305 MAX_META_DATA);
310 return GNUNET_SYSERR; 306 return GNUNET_SYSERR;
311 } 307 }
312 buf = GNUNET_malloc (size); 308 buf = GNUNET_malloc (size);
313 if (GNUNET_OK != 309 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
314 GNUNET_BIO_read (h,
315 what,
316 buf,
317 size))
318 { 310 {
319 GNUNET_free (buf); 311 GNUNET_free (buf);
320 return GNUNET_SYSERR; 312 return GNUNET_SYSERR;
321 } 313 }
322 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, 314 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
323 size);
324 if (NULL == meta) 315 if (NULL == meta)
325 { 316 {
326 GNUNET_free (buf); 317 GNUNET_free (buf);
327 GNUNET_asprintf (&h->emsg, 318 GNUNET_asprintf (&h->emsg, _ ("Metadata `%s' failed to deserialize"), what);
328 _("Metadata `%s' failed to deserialize"),
329 what);
330 return GNUNET_SYSERR; 319 return GNUNET_SYSERR;
331 } 320 }
332 GNUNET_free (buf); 321 GNUNET_free (buf);
@@ -346,18 +335,13 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
346 */ 335 */
347int 336int
348GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, 337GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
349 const char *file, 338 const char *file,
350 int line, 339 int line,
351 int32_t * i) 340 int32_t *i)
352{ 341{
353 int32_t big; 342 int32_t big;
354 343
355 if (GNUNET_OK != 344 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
356 GNUNET_BIO_read_fn (h,
357 file,
358 line,
359 &big,
360 sizeof (int32_t)))
361 return GNUNET_SYSERR; 345 return GNUNET_SYSERR;
362 *i = ntohl (big); 346 *i = ntohl (big);
363 return GNUNET_OK; 347 return GNUNET_OK;
@@ -381,12 +365,7 @@ GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
381{ 365{
382 int64_t big; 366 int64_t big;
383 367
384 if (GNUNET_OK != 368 if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
385 GNUNET_BIO_read_fn (h,
386 file,
387 line,
388 &big,
389 sizeof (int64_t)))
390 return GNUNET_SYSERR; 369 return GNUNET_SYSERR;
391 *i = GNUNET_ntohll (big); 370 *i = GNUNET_ntohll (big);
392 return GNUNET_OK; 371 return GNUNET_OK;
@@ -432,11 +411,12 @@ GNUNET_BIO_write_open (const char *fn)
432 struct GNUNET_DISK_FileHandle *fd; 411 struct GNUNET_DISK_FileHandle *fd;
433 struct GNUNET_BIO_WriteHandle *h; 412 struct GNUNET_BIO_WriteHandle *h;
434 413
435 fd = GNUNET_DISK_file_open (fn, 414 fd =
436 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE 415 GNUNET_DISK_file_open (fn,
437 | GNUNET_DISK_OPEN_CREATE, 416 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE |
438 GNUNET_DISK_PERM_USER_READ | 417 GNUNET_DISK_OPEN_CREATE,
439 GNUNET_DISK_PERM_USER_WRITE); 418 GNUNET_DISK_PERM_USER_READ |
419 GNUNET_DISK_PERM_USER_WRITE);
440 if (NULL == fd) 420 if (NULL == fd)
441 return NULL; 421 return NULL;
442 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); 422 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
@@ -459,8 +439,7 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
459 int ret; 439 int ret;
460 440
461 ret = GNUNET_SYSERR; 441 ret = GNUNET_SYSERR;
462 if ( (NULL != h->fd) && 442 if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))))
463 (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
464 GNUNET_DISK_file_close (h->fd); 443 GNUNET_DISK_file_close (h->fd);
465 GNUNET_free (h); 444 GNUNET_free (h);
466 return ret; 445 return ret;
@@ -479,14 +458,12 @@ GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
479{ 458{
480 ssize_t ret; 459 ssize_t ret;
481 460
482 ret = GNUNET_DISK_file_write (h->fd, 461 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
483 h->buffer,
484 h->have);
485 if (ret != (ssize_t) h->have) 462 if (ret != (ssize_t) h->have)
486 { 463 {
487 GNUNET_DISK_file_close (h->fd); 464 GNUNET_DISK_file_close (h->fd);
488 h->fd = NULL; 465 h->fd = NULL;
489 return GNUNET_SYSERR; /* error */ 466 return GNUNET_SYSERR; /* error */
490 } 467 }
491 h->have = 0; 468 h->have = 0;
492 return GNUNET_OK; 469 return GNUNET_OK;
@@ -503,7 +480,7 @@ GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
503 */ 480 */
504int 481int
505GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, 482GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
506 const void *buffer, 483 const void *buffer,
507 size_t n) 484 size_t n)
508{ 485{
509 const char *src = buffer; 486 const char *src = buffer;
@@ -519,18 +496,15 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
519 min = h->size - h->have; 496 min = h->size - h->have;
520 if (min > n - pos) 497 if (min > n - pos)
521 min = n - pos; 498 min = n - pos;
522 GNUNET_memcpy (&h->buffer[h->have], 499 GNUNET_memcpy (&h->buffer[h->have], &src[pos], min);
523 &src[pos],
524 min);
525 pos += min; 500 pos += min;
526 h->have += min; 501 h->have += min;
527 if (pos == n) 502 if (pos == n)
528 return GNUNET_OK; /* done */ 503 return GNUNET_OK; /* done */
529 GNUNET_assert (h->have == h->size); 504 GNUNET_assert (h->have == h->size);
530 if (GNUNET_OK != GNUNET_BIO_flush (h)) 505 if (GNUNET_OK != GNUNET_BIO_flush (h))
531 return GNUNET_SYSERR; /* error */ 506 return GNUNET_SYSERR; /* error */
532 } 507 } while (pos < n); /* should always be true */
533 while (pos < n); /* should always be true */
534 GNUNET_break (0); 508 GNUNET_break (0);
535 return GNUNET_OK; 509 return GNUNET_OK;
536} 510}
@@ -544,8 +518,7 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
544 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 518 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
545 */ 519 */
546int 520int
547GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, 521GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
548 const char *s)
549{ 522{
550 uint32_t slen; 523 uint32_t slen;
551 524
@@ -575,9 +548,11 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
575 if (m == NULL) 548 if (m == NULL)
576 return GNUNET_BIO_write_int32 (h, 0); 549 return GNUNET_BIO_write_int32 (h, 0);
577 buf = NULL; 550 buf = NULL;
578 size = 551 size = GNUNET_CONTAINER_meta_data_serialize (
579 GNUNET_CONTAINER_meta_data_serialize (m, &buf, MAX_META_DATA, 552 m,
580 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); 553 &buf,
554 MAX_META_DATA,
555 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
581 if (size == -1) 556 if (size == -1)
582 { 557 {
583 GNUNET_free (buf); 558 GNUNET_free (buf);
@@ -602,8 +577,7 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
602 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 577 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
603 */ 578 */
604int 579int
605GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, 580GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
606 int32_t i)
607{ 581{
608 int32_t big; 582 int32_t big;
609 583
@@ -620,8 +594,7 @@ GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
620 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 594 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
621 */ 595 */
622int 596int
623GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, 597GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
624 int64_t i)
625{ 598{
626 int64_t big; 599 int64_t big;
627 600