aboutsummaryrefslogtreecommitdiff
path: root/src/util/bio.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-11-01 20:47:52 +0000
committerChristian Grothoff <christian@grothoff.org>2009-11-01 20:47:52 +0000
commit652e89b59ed2207c2c12172fdabcd6e659995c81 (patch)
treef054c819d483c1056e18c1099afd4c7fcd2582a0 /src/util/bio.c
parent5e4113e83368849500792e57946c3d8dd9e548d8 (diff)
downloadgnunet-652e89b59ed2207c2c12172fdabcd6e659995c81.tar.gz
gnunet-652e89b59ed2207c2c12172fdabcd6e659995c81.zip
fixing bio testcase and a bug in bio.c, also indenting
Diffstat (limited to 'src/util/bio.c')
-rw-r--r--src/util/bio.c197
1 files changed, 92 insertions, 105 deletions
diff --git a/src/util/bio.c b/src/util/bio.c
index ea9415d63..860e9e63e 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -57,9 +57,9 @@ GNUNET_BIO_read_open (const char *fn)
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,
60 GNUNET_DISK_PERM_NONE); 60 GNUNET_DISK_PERM_NONE);
61 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE); 61 h = GNUNET_malloc (sizeof (struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
62 h->buffer = (char*) &h[1]; 62 h->buffer = (char *) &h[1];
63 h->size = BIO_BUFFER_SIZE; 63 h->size = BIO_BUFFER_SIZE;
64 h->fd = fd; 64 h->fd = fd;
65 return h; 65 return h;
@@ -74,8 +74,8 @@ GNUNET_BIO_read_open (const char *fn)
74 * @param emsg set to the error message 74 * @param emsg set to the error message
75 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 75 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
76 */ 76 */
77int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, 77int
78 char **emsg) 78GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
79{ 79{
80 *emsg = h->emsg; 80 *emsg = h->emsg;
81 GNUNET_DISK_file_close (h->fd); 81 GNUNET_DISK_file_close (h->fd);
@@ -93,10 +93,9 @@ int GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
93 * @param len the number of bytes to read 93 * @param len the number of bytes to read
94 * @return GNUNET_OK on success, GNUNET_SYSERR on failure 94 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
95 */ 95 */
96int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, 96int
97 const char *what, 97GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
98 void *result, 98 const char *what, void *result, size_t len)
99 size_t len)
100{ 99{
101 char *dst = result; 100 char *dst = result;
102 size_t min; 101 size_t min;
@@ -119,32 +118,28 @@ int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
119 pos += min; 118 pos += min;
120 } 119 }
121 if (pos == len) 120 if (pos == len)
122 return GNUNET_OK; /* done! */ 121 return GNUNET_OK; /* done! */
123 GNUNET_assert (h->have == h->pos); 122 GNUNET_assert (h->have == h->pos);
124 /* fill buffer */ 123 /* fill buffer */
125 ret = GNUNET_DISK_file_read (h->fd, 124 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
126 h->buffer,
127 h->size);
128 if (ret == -1) 125 if (ret == -1)
129 { 126 {
130 GNUNET_asprintf (&h->emsg, 127 GNUNET_asprintf (&h->emsg,
131 _("Error reading `%s': %s"), 128 _("Error reading `%s': %s"),
132 what, 129 what, STRERROR (errno));
133 STRERROR (errno)); 130 return GNUNET_SYSERR;
134 return GNUNET_SYSERR;
135 } 131 }
136 if (ret == 0) 132 if (ret == 0)
137 { 133 {
138 GNUNET_asprintf (&h->emsg, 134 GNUNET_asprintf (&h->emsg,
139 _("Error reading `%s': %s"), 135 _("Error reading `%s': %s"),
140 what, 136 what, _("End of file"));
141 _("End of file")); 137 return GNUNET_SYSERR;
142 return GNUNET_SYSERR; 138 }
143 }
144 h->pos = 0; 139 h->pos = 0;
145 h->have = ret; 140 h->have = ret;
146 } 141 }
147 while (pos < len); /* should always be true */ 142 while (pos < len); /* should always be true */
148 return GNUNET_OK; 143 return GNUNET_OK;
149} 144}
150 145
@@ -159,10 +154,9 @@ int GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
159 * @param maxLen maximum allowed length for the string 154 * @param maxLen maximum allowed length for the string
160 * @return GNUNET_OK on success, GNUNET_SYSERR on failure 155 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
161 */ 156 */
162int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, 157int
163 const char *what, 158GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
164 char **result, 159 const char *what, char **result, size_t maxLen)
165 size_t maxLen)
166{ 160{
167 char *buf; 161 char *buf;
168 uint32_t big; 162 uint32_t big;
@@ -177,14 +171,14 @@ int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
177 if (big > maxLen) 171 if (big > maxLen)
178 { 172 {
179 GNUNET_asprintf (&h->emsg, 173 GNUNET_asprintf (&h->emsg,
180 _("String `%s' longer than allowed (%u > %u)"), 174 _("String `%s' longer than allowed (%u > %u)"),
181 what, 175 what, big, maxLen);
182 big,
183 maxLen);
184 return GNUNET_SYSERR; 176 return GNUNET_SYSERR;
185 } 177 }
186 buf = GNUNET_malloc (big); 178 buf = GNUNET_malloc (big);
187 buf[--big] = '\0'; 179 buf[--big] = '\0';
180 if (big == 0)
181 return GNUNET_OK;
188 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big)) 182 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
189 { 183 {
190 GNUNET_free (buf); 184 GNUNET_free (buf);
@@ -203,30 +197,27 @@ int GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
203 * @param result the buffer to store a pointer to the (allocated) metadata 197 * @param result the buffer to store a pointer to the (allocated) metadata
204 * @return GNUNET_OK on success, GNUNET_SYSERR on failure 198 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
205 */ 199 */
206int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, 200int
207 const char *what, 201GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
208 struct GNUNET_CONTAINER_MetaData **result) 202 const char *what,
203 struct GNUNET_CONTAINER_MetaData **result)
209{ 204{
210 uint32_t size; 205 uint32_t size;
211 char *buf; 206 char *buf;
212 struct GNUNET_CONTAINER_MetaData *meta; 207 struct GNUNET_CONTAINER_MetaData *meta;
213 208
214 if (GNUNET_BIO_read_int32__ (h, 209 if (GNUNET_BIO_read_int32__ (h, what, (int32_t *) & size) != GNUNET_OK)
215 what,
216 (int32_t*) &size) != GNUNET_OK)
217 return GNUNET_SYSERR; 210 return GNUNET_SYSERR;
218 if (size > MAX_META_DATA) 211 if (size > MAX_META_DATA)
219 { 212 {
220 GNUNET_asprintf (&h->emsg, 213 GNUNET_asprintf (&h->emsg,
221 _("Serialized metadata `%s' larger than allowed (%u > %u)"), 214 _
222 what, 215 ("Serialized metadata `%s' larger than allowed (%u > %u)"),
223 size, 216 what, size, MAX_META_DATA);
224 MAX_META_DATA);
225 return GNUNET_SYSERR; 217 return GNUNET_SYSERR;
226 } 218 }
227 buf = GNUNET_malloc (size); 219 buf = GNUNET_malloc (size);
228 if (GNUNET_OK != 220 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
229 GNUNET_BIO_read (h, what, buf, size))
230 { 221 {
231 GNUNET_free (buf); 222 GNUNET_free (buf);
232 return GNUNET_SYSERR; 223 return GNUNET_SYSERR;
@@ -236,8 +227,7 @@ int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
236 { 227 {
237 GNUNET_free (buf); 228 GNUNET_free (buf);
238 GNUNET_asprintf (&h->emsg, 229 GNUNET_asprintf (&h->emsg,
239 _("Metadata `%s' failed to deserialize"), 230 _("Metadata `%s' failed to deserialize"), what);
240 what);
241 return GNUNET_SYSERR; 231 return GNUNET_SYSERR;
242 } 232 }
243 GNUNET_free (buf); 233 GNUNET_free (buf);
@@ -253,18 +243,14 @@ int GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
253 * @param what describes what is being read (for error message creation) 243 * @param what describes what is being read (for error message creation)
254 * @param i address of 32-bit integer to read 244 * @param i address of 32-bit integer to read
255 * @return GNUNET_OK on success, GNUNET_SYSERR on error 245 * @return GNUNET_OK on success, GNUNET_SYSERR on error
256 */ 246 */
257int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, 247int
258 const char *what, 248GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
259 int32_t *i) 249 const char *what, int32_t * i)
260{ 250{
261 int32_t big; 251 int32_t big;
262 252
263 if (GNUNET_OK != 253 if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof (int32_t)))
264 GNUNET_BIO_read (h,
265 what,
266 &big,
267 sizeof (int32_t)))
268 return GNUNET_SYSERR; 254 return GNUNET_SYSERR;
269 *i = ntohl (big); 255 *i = ntohl (big);
270 return GNUNET_OK; 256 return GNUNET_OK;
@@ -278,18 +264,14 @@ int GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
278 * @param what describes what is being read (for error message creation) 264 * @param what describes what is being read (for error message creation)
279 * @param i address of 64-bit integer to read 265 * @param i address of 64-bit integer to read
280 * @return GNUNET_OK on success, GNUNET_SYSERR on error 266 * @return GNUNET_OK on success, GNUNET_SYSERR on error
281 */ 267 */
282int GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, 268int
283 const char *what, 269GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
284 int64_t *i) 270 const char *what, int64_t * i)
285{ 271{
286 int64_t big; 272 int64_t big;
287 273
288 if (GNUNET_OK != 274 if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof (int64_t)))
289 GNUNET_BIO_read (h,
290 what,
291 &big,
292 sizeof (int64_t)))
293 return GNUNET_SYSERR; 275 return GNUNET_SYSERR;
294 *i = GNUNET_ntohll (big); 276 *i = GNUNET_ntohll (big);
295 return GNUNET_OK; 277 return GNUNET_OK;
@@ -314,16 +296,21 @@ struct GNUNET_BIO_WriteHandle
314 * @param fn file name to be opened 296 * @param fn file name to be opened
315 * @return IO handle on success, NULL on error 297 * @return IO handle on success, NULL on error
316 */ 298 */
317struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn) 299struct GNUNET_BIO_WriteHandle *
300GNUNET_BIO_write_open (const char *fn)
318{ 301{
319 struct GNUNET_DISK_FileHandle *fd; 302 struct GNUNET_DISK_FileHandle *fd;
320 struct GNUNET_BIO_WriteHandle *h; 303 struct GNUNET_BIO_WriteHandle *h;
321 304
322 fd = GNUNET_DISK_file_open (fn, 305 fd = GNUNET_DISK_file_open (fn,
323 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE, 306 GNUNET_DISK_OPEN_WRITE |
324 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE); 307 GNUNET_DISK_OPEN_TRUNCATE |
325 h = GNUNET_malloc (sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE); 308 GNUNET_DISK_OPEN_CREATE,
326 h->buffer = (char*) &h[1]; 309 GNUNET_DISK_PERM_USER_READ |
310 GNUNET_DISK_PERM_USER_WRITE);
311 h =
312 GNUNET_malloc (sizeof (struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
313 h->buffer = (char *) &h[1];
327 h->size = BIO_BUFFER_SIZE; 314 h->size = BIO_BUFFER_SIZE;
328 h->fd = fd; 315 h->fd = fd;
329 316
@@ -337,7 +324,8 @@ struct GNUNET_BIO_WriteHandle *GNUNET_BIO_write_open (const char *fn)
337 * @param h file handle 324 * @param h file handle
338 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise 325 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
339 */ 326 */
340int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h) 327int
328GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
341{ 329{
342 ssize_t wrt; 330 ssize_t wrt;
343 int ret; 331 int ret;
@@ -350,9 +338,9 @@ int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
350 { 338 {
351 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have); 339 wrt = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
352 if (wrt == h->have) 340 if (wrt == h->have)
353 ret = GNUNET_OK; 341 ret = GNUNET_OK;
354 else 342 else
355 ret = GNUNET_SYSERR; 343 ret = GNUNET_SYSERR;
356 GNUNET_DISK_file_close (h->fd); 344 GNUNET_DISK_file_close (h->fd);
357 } 345 }
358 GNUNET_free (h); 346 GNUNET_free (h);
@@ -368,9 +356,9 @@ int GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
368 * @param n number of bytes to write 356 * @param n number of bytes to write
369 * @return GNUNET_OK on success, GNUNET_SYSERR on error 357 * @return GNUNET_OK on success, GNUNET_SYSERR on error
370 */ 358 */
371int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, 359int
372 const void *buffer, 360GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
373 size_t n) 361 const void *buffer, size_t n)
374{ 362{
375 const char *src = buffer; 363 const char *src = buffer;
376 size_t min; 364 size_t min;
@@ -390,18 +378,18 @@ int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
390 pos += min; 378 pos += min;
391 h->have += min; 379 h->have += min;
392 if (pos == n) 380 if (pos == n)
393 return GNUNET_OK; /* done */ 381 return GNUNET_OK; /* done */
394 GNUNET_assert (h->have == h->size); 382 GNUNET_assert (h->have == h->size);
395 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size); 383 ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->size);
396 if (ret != h->size) 384 if (ret != h->size)
397 { 385 {
398 GNUNET_DISK_file_close (h->fd); 386 GNUNET_DISK_file_close (h->fd);
399 h->fd = NULL; 387 h->fd = NULL;
400 return GNUNET_SYSERR; /* error */ 388 return GNUNET_SYSERR; /* error */
401 } 389 }
402 h->have = 0; 390 h->have = 0;
403 } 391 }
404 while (pos < n); /* should always be true */ 392 while (pos < n); /* should always be true */
405 return GNUNET_OK; 393 return GNUNET_OK;
406} 394}
407 395
@@ -413,14 +401,13 @@ int GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
413 * @param s string to write (can be NULL) 401 * @param s string to write (can be NULL)
414 * @return GNUNET_OK on success, GNUNET_SYSERR on error 402 * @return GNUNET_OK on success, GNUNET_SYSERR on error
415 */ 403 */
416int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, 404int
417 const char *s) 405GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
418{ 406{
419 uint32_t slen; 407 uint32_t slen;
420 408
421 slen = (uint32_t) ((s == NULL) ? 0 : strlen(s) + 1); 409 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
422 if (GNUNET_OK != 410 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
423 GNUNET_BIO_write_int32 (h, slen))
424 return GNUNET_SYSERR; 411 return GNUNET_SYSERR;
425 if (0 != slen) 412 if (0 != slen)
426 return GNUNET_BIO_write (h, s, slen - 1); 413 return GNUNET_BIO_write (h, s, slen - 1);
@@ -435,28 +422,28 @@ int GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
435 * @param m metadata to write 422 * @param m metadata to write
436 * @return GNUNET_OK on success, GNUNET_SYSERR on error 423 * @return GNUNET_OK on success, GNUNET_SYSERR on error
437 */ 424 */
438int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, 425int
439 const struct GNUNET_CONTAINER_MetaData *m) 426GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
427 const struct GNUNET_CONTAINER_MetaData *m)
440{ 428{
441 unsigned int size; 429 unsigned int size;
442 char *buf; 430 char *buf;
443 431
444 size = GNUNET_CONTAINER_meta_data_get_serialized_size (m, 432 size = GNUNET_CONTAINER_meta_data_get_serialized_size (m,
445 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL 433 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL
446 | 434 |
447 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS); 435 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
448 if (size > MAX_META_DATA) 436 if (size > MAX_META_DATA)
449 size = MAX_META_DATA; 437 size = MAX_META_DATA;
450 buf = GNUNET_malloc (size); 438 buf = GNUNET_malloc (size);
451 GNUNET_CONTAINER_meta_data_serialize (m, 439 GNUNET_CONTAINER_meta_data_serialize (m,
452 buf, 440 buf,
453 size, 441 size,
454 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART | 442 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART
455 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS); 443 |
456 if ( (GNUNET_OK != 444 GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS);
457 GNUNET_BIO_write_int32 (h, size)) || 445 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, size))
458 (GNUNET_OK != 446 || (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
459 GNUNET_BIO_write (h, buf, size)) )
460 { 447 {
461 GNUNET_free (buf); 448 GNUNET_free (buf);
462 return GNUNET_SYSERR; 449 return GNUNET_SYSERR;
@@ -472,9 +459,9 @@ int GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
472 * @param h hande to open file 459 * @param h hande to open file
473 * @param i address of 32-bit integer to write 460 * @param i address of 32-bit integer to write
474 * @return GNUNET_OK on success, GNUNET_SYSERR on error 461 * @return GNUNET_OK on success, GNUNET_SYSERR on error
475 */ 462 */
476int GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, 463int
477 int32_t i) 464GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
478{ 465{
479 int32_t big; 466 int32_t big;
480 big = htonl (i); 467 big = htonl (i);
@@ -488,9 +475,9 @@ int GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
488 * @param h hande to open file 475 * @param h hande to open file
489 * @param i address of 64-bit integer to write 476 * @param i address of 64-bit integer to write
490 * @return GNUNET_OK on success, GNUNET_SYSERR on error 477 * @return GNUNET_OK on success, GNUNET_SYSERR on error
491 */ 478 */
492int GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, 479int
493 int64_t i) 480GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
494{ 481{
495 int64_t big; 482 int64_t big;
496 big = GNUNET_htonll (i); 483 big = GNUNET_htonll (i);