aboutsummaryrefslogtreecommitdiff
path: root/src/util/bio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/bio.c')
-rw-r--r--src/util/bio.c262
1 files changed, 125 insertions, 137 deletions
diff --git a/src/util/bio.c b/src/util/bio.c
index 3fc7d05e6..b29d75eb9 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -56,8 +56,7 @@ GNUNET_BIO_read_open (const char *fn)
56 struct GNUNET_DISK_FileHandle *fd; 56 struct GNUNET_DISK_FileHandle *fd;
57 struct GNUNET_BIO_ReadHandle *h; 57 struct GNUNET_BIO_ReadHandle *h;
58 58
59 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, 59 fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
60 GNUNET_DISK_PERM_NONE);
61 if (NULL == fd) 60 if (NULL == fd)
62 return NULL; 61 return NULL;
63 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); 62 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
@@ -114,39 +113,37 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
114 return GNUNET_SYSERR; 113 return GNUNET_SYSERR;
115 pos = 0; 114 pos = 0;
116 do 115 do
116 {
117 /* first, use buffer */
118 min = h->have - h->pos;
119 if (min > 0)
117 { 120 {
118 /* first, use buffer */ 121 if (min > len - pos)
119 min = h->have - h->pos; 122 min = len - pos;
120 if (min > 0) 123 memcpy (&dst[pos], &h->buffer[h->pos], min);
121 { 124 h->pos += min;
122 if (min > len - pos) 125 pos += min;
123 min = len - pos; 126 }
124 memcpy (&dst[pos], &h->buffer[h->pos], min); 127 if (pos == len)
125 h->pos += min; 128 return GNUNET_OK; /* done! */
126 pos += min; 129 GNUNET_assert (h->have == h->pos);
127 } 130 /* fill buffer */
128 if (pos == len) 131 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
129 return GNUNET_OK; /* done! */ 132 if (ret == -1)
130 GNUNET_assert (h->have == h->pos); 133 {
131 /* fill buffer */ 134 GNUNET_asprintf (&h->emsg,
132 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size); 135 _("Error reading `%s': %s"), what, STRERROR (errno));
133 if (ret == -1) 136 return GNUNET_SYSERR;
134 { 137 }
135 GNUNET_asprintf (&h->emsg, 138 if (ret == 0)
136 _("Error reading `%s': %s"), 139 {
137 what, STRERROR (errno)); 140 GNUNET_asprintf (&h->emsg,
138 return GNUNET_SYSERR; 141 _("Error reading `%s': %s"), what, _("End of file"));
139 } 142 return GNUNET_SYSERR;
140 if (ret == 0)
141 {
142 GNUNET_asprintf (&h->emsg,
143 _("Error reading `%s': %s"),
144 what, _("End of file"));
145 return GNUNET_SYSERR;
146 }
147 h->pos = 0;
148 h->have = ret;
149 } 143 }
144 h->pos = 0;
145 h->have = ret;
146 }
150 while (pos < len); /* should always be true */ 147 while (pos < len); /* should always be true */
151 return GNUNET_OK; 148 return GNUNET_OK;
152} 149}
@@ -162,16 +159,13 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
162 * @param len the number of bytes to read 159 * @param len the number of bytes to read
163 * @return GNUNET_OK on success, GNUNET_SYSERR on failure 160 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
164 */ 161 */
165int GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, 162int
166 const char *file, int line, 163GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
167 void *result, 164 const char *file, int line, void *result, size_t len)
168 size_t len)
169{ 165{
170 char what[1024]; 166 char what[1024];
171 GNUNET_snprintf (what, 167
172 sizeof(what), 168 GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
173 "%s:%d",
174 file, line);
175 return GNUNET_BIO_read (h, what, result, len); 169 return GNUNET_BIO_read (h, what, result, len);
176} 170}
177 171
@@ -194,36 +188,34 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
194 uint32_t big; 188 uint32_t big;
195 189
196 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big)) 190 if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
197 { 191 {
198 GNUNET_free_non_null (h->emsg); 192 GNUNET_free_non_null (h->emsg);
199 GNUNET_asprintf (&h->emsg, 193 GNUNET_asprintf (&h->emsg, _("Error reading length of string `%s'"), what);
200 _("Error reading length of string `%s'"), 194 return GNUNET_SYSERR;
201 what); 195 }
202 return GNUNET_SYSERR;
203 }
204 if (big == 0) 196 if (big == 0)
205 { 197 {
206 *result = NULL; 198 *result = NULL;
207 return GNUNET_OK; 199 return GNUNET_OK;
208 } 200 }
209 if (big > maxLen) 201 if (big > maxLen)
210 { 202 {
211 GNUNET_asprintf (&h->emsg, 203 GNUNET_asprintf (&h->emsg,
212 _("String `%s' longer than allowed (%u > %u)"), 204 _("String `%s' longer than allowed (%u > %u)"),
213 what, big, maxLen); 205 what, big, maxLen);
214 return GNUNET_SYSERR; 206 return GNUNET_SYSERR;
215 } 207 }
216 buf = GNUNET_malloc (big); 208 buf = GNUNET_malloc (big);
217 *result = buf; 209 *result = buf;
218 buf[--big] = '\0'; 210 buf[--big] = '\0';
219 if (big == 0) 211 if (big == 0)
220 return GNUNET_OK; 212 return GNUNET_OK;
221 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) 213 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
222 { 214 {
223 GNUNET_free (buf); 215 GNUNET_free (buf);
224 *result = NULL; 216 *result = NULL;
225 return GNUNET_SYSERR; 217 return GNUNET_SYSERR;
226 } 218 }
227 return GNUNET_OK; 219 return GNUNET_OK;
228} 220}
229 221
@@ -245,34 +237,33 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
245 char *buf; 237 char *buf;
246 struct GNUNET_CONTAINER_MetaData *meta; 238 struct GNUNET_CONTAINER_MetaData *meta;
247 239
248 if (GNUNET_BIO_read_int32 (h, (int32_t *) &size) != GNUNET_OK) 240 if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
249 return GNUNET_SYSERR; 241 return GNUNET_SYSERR;
250 if (size == 0) 242 if (size == 0)
251 { 243 {
252 *result = NULL; 244 *result = NULL;
253 return GNUNET_OK; 245 return GNUNET_OK;
254 } 246 }
255 if (size > MAX_META_DATA) 247 if (size > MAX_META_DATA)
256 { 248 {
257 GNUNET_asprintf (&h->emsg, 249 GNUNET_asprintf (&h->emsg,
258 _("Serialized metadata `%s' larger than allowed (%u>%u)"), 250 _("Serialized metadata `%s' larger than allowed (%u>%u)"),
259 what, size, MAX_META_DATA); 251 what, size, MAX_META_DATA);
260 return GNUNET_SYSERR; 252 return GNUNET_SYSERR;
261 } 253 }
262 buf = GNUNET_malloc (size); 254 buf = GNUNET_malloc (size);
263 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size)) 255 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
264 { 256 {
265 GNUNET_free (buf); 257 GNUNET_free (buf);
266 return GNUNET_SYSERR; 258 return GNUNET_SYSERR;
267 } 259 }
268 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size); 260 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
269 if (meta == NULL) 261 if (meta == NULL)
270 { 262 {
271 GNUNET_free (buf); 263 GNUNET_free (buf);
272 GNUNET_asprintf (&h->emsg, 264 GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
273 _("Metadata `%s' failed to deserialize"), what); 265 return GNUNET_SYSERR;
274 return GNUNET_SYSERR; 266 }
275 }
276 GNUNET_free (buf); 267 GNUNET_free (buf);
277 *result = meta; 268 *result = meta;
278 return GNUNET_OK; 269 return GNUNET_OK;
@@ -290,9 +281,7 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
290 */ 281 */
291int 282int
292GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, 283GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
293 const char *file, 284 const char *file, int line, int32_t * i)
294 int line,
295 int32_t * i)
296{ 285{
297 int32_t big; 286 int32_t big;
298 287
@@ -314,9 +303,7 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
314 */ 303 */
315int 304int
316GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, 305GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
317 const char *file, 306 const char *file, int line, int64_t * i)
318 int line,
319 int64_t * i)
320{ 307{
321 int64_t big; 308 int64_t big;
322 309
@@ -359,8 +346,7 @@ GNUNET_BIO_write_open (const char *fn)
359 GNUNET_DISK_PERM_USER_WRITE); 346 GNUNET_DISK_PERM_USER_WRITE);
360 if (NULL == fd) 347 if (NULL == fd)
361 return NULL; 348 return NULL;
362 h = 349 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
363 GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
364 h->buffer = (char *) &h[1]; 350 h->buffer = (char *) &h[1];
365 h->size = BIO_BUFFER_SIZE; 351 h->size = BIO_BUFFER_SIZE;
366 h->fd = fd; 352 h->fd = fd;
@@ -382,18 +368,18 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
382 int ret; 368 int ret;
383 369
384 if (NULL == h->fd) 370 if (NULL == h->fd)
385 { 371 {
386 ret = GNUNET_SYSERR; 372 ret = GNUNET_SYSERR;
387 } 373 }
388 else 374 else
389 { 375 {
390 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); 376 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
391 if (wrt == h->have) 377 if (wrt == h->have)
392 ret = GNUNET_OK; 378 ret = GNUNET_OK;
393 else 379 else
394 ret = GNUNET_SYSERR; 380 ret = GNUNET_SYSERR;
395 GNUNET_DISK_file_close (h->fd); 381 GNUNET_DISK_file_close (h->fd);
396 } 382 }
397 GNUNET_free (h); 383 GNUNET_free (h);
398 return ret; 384 return ret;
399} 385}
@@ -420,26 +406,26 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
420 return GNUNET_SYSERR; 406 return GNUNET_SYSERR;
421 pos = 0; 407 pos = 0;
422 do 408 do
409 {
410 /* first, just use buffer */
411 min = h->size - h->have;
412 if (min > n - pos)
413 min = n - pos;
414 memcpy (&h->buffer[h->have], &src[pos], min);
415 pos += min;
416 h->have += min;
417 if (pos == n)
418 return GNUNET_OK; /* done */
419 GNUNET_assert (h->have == h->size);
420 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
421 if (ret != h->size)
423 { 422 {
424 /* first, just use buffer */ 423 GNUNET_DISK_file_close (h->fd);
425 min = h->size - h->have; 424 h->fd = NULL;
426 if (min > n - pos) 425 return GNUNET_SYSERR; /* error */
427 min = n - pos;
428 memcpy (&h->buffer[h->have], &src[pos], min);
429 pos += min;
430 h->have += min;
431 if (pos == n)
432 return GNUNET_OK; /* done */
433 GNUNET_assert (h->have == h->size);
434 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
435 if (ret != h->size)
436 {
437 GNUNET_DISK_file_close (h->fd);
438 h->fd = NULL;
439 return GNUNET_SYSERR; /* error */
440 }
441 h->have = 0;
442 } 426 }
427 h->have = 0;
428 }
443 while (pos < n); /* should always be true */ 429 while (pos < n); /* should always be true */
444 GNUNET_break (0); 430 GNUNET_break (0);
445 return GNUNET_OK; 431 return GNUNET_OK;
@@ -480,25 +466,25 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
480{ 466{
481 ssize_t size; 467 ssize_t size;
482 char *buf; 468 char *buf;
483 469
484 if (m == NULL) 470 if (m == NULL)
485 return GNUNET_BIO_write_int32 (h, 0); 471 return GNUNET_BIO_write_int32 (h, 0);
486 buf = NULL; 472 buf = NULL;
487 size = GNUNET_CONTAINER_meta_data_serialize (m, 473 size = GNUNET_CONTAINER_meta_data_serialize (m,
488 &buf, 474 &buf,
489 MAX_META_DATA, 475 MAX_META_DATA,
490 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); 476 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
491 if (size == -1) 477 if (size == -1)
492 { 478 {
493 GNUNET_free (buf); 479 GNUNET_free (buf);
494 return GNUNET_SYSERR; 480 return GNUNET_SYSERR;
495 } 481 }
496 if ( (GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) || 482 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
497 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)) ) 483 (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
498 { 484 {
499 GNUNET_free (buf); 485 GNUNET_free (buf);
500 return GNUNET_SYSERR; 486 return GNUNET_SYSERR;
501 } 487 }
502 GNUNET_free (buf); 488 GNUNET_free (buf);
503 return GNUNET_OK; 489 return GNUNET_OK;
504} 490}
@@ -515,6 +501,7 @@ int
515GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i) 501GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
516{ 502{
517 int32_t big; 503 int32_t big;
504
518 big = htonl (i); 505 big = htonl (i);
519 return GNUNET_BIO_write (h, &big, sizeof (int32_t)); 506 return GNUNET_BIO_write (h, &big, sizeof (int32_t));
520} 507}
@@ -531,6 +518,7 @@ int
531GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i) 518GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
532{ 519{
533 int64_t big; 520 int64_t big;
521
534 big = GNUNET_htonll (i); 522 big = GNUNET_htonll (i);
535 return GNUNET_BIO_write (h, &big, sizeof (int64_t)); 523 return GNUNET_BIO_write (h, &big, sizeof (int64_t));
536} 524}