GNUnet 0.22.0
bio.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2013 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28
29#define LOG(kind, ...) GNUNET_log_from (kind, "util-bio", __VA_ARGS__)
30
31#ifndef PATH_MAX
35#define PATH_MAX 4096
36#endif
37
38
42#define BIO_BUFFER_SIZE 65536
43
44
52{
57
62};
63
64
69{
74
79
83 char *emsg;
84
88 char *buffer;
89
93 size_t have;
94
98 size_t size;
99
103 off_t pos;
104};
105
106
115{
117 struct GNUNET_BIO_ReadHandle *h;
118
120 if (NULL == fd)
121 return NULL;
123 h->type = IO_FILE;
124 h->buffer = (char *) &h[1];
125 h->size = BIO_BUFFER_SIZE;
126 h->fd = fd;
127 return h;
128}
129
130
140{
141 struct GNUNET_BIO_ReadHandle *h;
142
144 h->type = IO_BUFFER;
145 h->buffer = buffer;
146 h->size = size;
147 return h;
148}
149
150
163{
164 int err;
165
166 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
167 if (NULL != emsg)
168 *emsg = h->emsg;
169 else
170 GNUNET_free (h->emsg);
171 switch (h->type)
172 {
173 case IO_FILE:
175 break;
176 case IO_BUFFER:
177 break;
178 default:
179 break;
180 }
181 GNUNET_free (h);
182 return err;
183}
184
185
186void
188{
189 GNUNET_assert (NULL == h->emsg);
190 h->emsg = GNUNET_strdup (emsg);
191}
192
193
203static int
205 const char *what,
206 char *result,
207 size_t len)
208{
209 size_t pos = 0;
210 size_t min;
211 ssize_t ret;
212
213 do
214 {
215 min = h->have - h->pos;
216 if (0 < min)
217 {
218 if (len - pos < min)
219 min = len - pos;
220 GNUNET_memcpy (&result[pos], &h->buffer[h->pos], min);
221 h->pos += min;
222 pos += min;
223 }
224 if (len == pos)
225 return GNUNET_OK;
226 GNUNET_assert (((off_t) h->have) == h->pos);
227 ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
228 if (-1 == ret)
229 {
230 GNUNET_asprintf (&h->emsg,
231 _ ("Error reading `%s' from file: %s"),
232 what,
233 strerror (errno));
234 return GNUNET_SYSERR;
235 }
236 if (0 == ret)
237 {
238 GNUNET_asprintf (&h->emsg,
239 _ ("Error reading `%s' from file: %s"),
240 what,
241 _ ("End of file"));
242 return GNUNET_SYSERR;
243 }
244 h->pos = 0;
245 h->have = ret;
246 }
247 while (pos < len);
248 return GNUNET_OK;
249}
250
251
261static int
263 const char *what,
264 char *result,
265 size_t len)
266{
267 if ((h->size < len) || (h->size - h->pos < len))
268 {
269 GNUNET_asprintf (&h->emsg,
270 _ ("Error while reading `%s' from buffer: %s"),
271 what,
272 _ ("Not enough data left"));
273 return GNUNET_SYSERR;
274 }
275 GNUNET_memcpy (result, h->buffer + h->pos, len);
276 h->pos += len;
277 return GNUNET_OK;
278}
279
280
292 const char *what,
293 void *result,
294 size_t len)
295{
296 char *dst = result;
297
298 if (NULL != h->emsg)
299 return GNUNET_SYSERR;
300
301 if (0 == len)
302 return GNUNET_OK;
303
304 switch (h->type)
305 {
306 case IO_FILE:
307 return read_from_file (h, what, dst, len);
308 case IO_BUFFER:
309 return read_from_buffer (h, what, dst, len);
310 default:
311 GNUNET_asprintf (&h->emsg,
312 _ ("Invalid handle type while reading `%s'"),
313 what);
314 return GNUNET_SYSERR;
315 }
316}
317
318
331 const char *what,
332 char **result,
333 size_t max_length)
334{
335 char *buf;
336 uint32_t big;
337
339 _ ("string length"),
340 (int32_t *) &big))
341 {
342 char *tmp = h->emsg;
343 if (NULL != tmp)
344 GNUNET_asprintf (&h->emsg,
345 _ ("%s (while reading `%s')"),
346 tmp,
347 what);
348 else
349 GNUNET_asprintf (&h->emsg,
350 _ ("Error reading length of string `%s'"),
351 what);
352 GNUNET_free (tmp);
353 return GNUNET_SYSERR;
354 }
355 if (0 == big)
356 {
357 *result = NULL;
358 return GNUNET_OK;
359 }
360 if (big > max_length)
361 {
362 GNUNET_asprintf (&h->emsg,
363 _ ("String `%s' longer than allowed (%u > %lu)"),
364 what,
365 big,
366 (unsigned long) max_length);
367 return GNUNET_SYSERR;
368 }
369 buf = GNUNET_malloc (big);
370 *result = buf;
371 buf[--big] = '\0';
372 if (0 == big)
373 return GNUNET_OK;
374 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
375 {
376 GNUNET_free (buf);
377 *result = NULL;
378 return GNUNET_SYSERR;
379 }
380 return GNUNET_OK;
381}
382
383
393 const char *what,
394 float *f)
395{
396 int32_t *i = (int32_t *) f;
397 return GNUNET_BIO_read_int32 (h, what, i);
398}
399
400
410 const char *what,
411 double *f)
412{
413 int64_t *i = (int64_t *) f;
414 return GNUNET_BIO_read_int64 (h, what, i);
415}
416
417
428 const char *what,
429 int32_t *i)
430{
431 int32_t big;
432
433 if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int32_t)))
434 return GNUNET_SYSERR;
435 *i = ntohl (big);
436 return GNUNET_OK;
437}
438
439
450 const char *what,
451 int64_t *i)
452{
453 int64_t big;
454
455 if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int64_t)))
456 return GNUNET_SYSERR;
457 *i = GNUNET_ntohll (big);
458 return GNUNET_OK;
459}
460
461
466{
471
476
480 char *emsg;
481
487 void *buffer;
488
492 size_t have;
493
497 size_t size;
498};
499
500
509{
512
519 if (NULL == fd)
520 return NULL;
522 h->buffer = &h[1];
523 h->size = BIO_BUFFER_SIZE;
524 h->fd = fd;
525 return h;
526}
527
528
536{
538
540 h->type = IO_BUFFER;
541 h->buffer = (void *) GNUNET_malloc (sizeof (struct GNUNET_Buffer));
542 return h;
543}
544
545
557 char **emsg)
558{
559 int err;
560
561 err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
562 if (NULL != emsg)
563 *emsg = h->emsg;
564 else
565 GNUNET_free (h->emsg);
566 switch (h->type)
567 {
568 case IO_FILE:
569 if (NULL == h->fd)
570 {
571 err = GNUNET_SYSERR;
572 break;
573 }
575 {
576 if (NULL != emsg)
577 *emsg = h->emsg;
578 else
579 GNUNET_free (h->emsg);
580 err = GNUNET_SYSERR;
581 }
582 else
583 {
585 }
586 break;
587 case IO_BUFFER:
588 GNUNET_buffer_clear ((struct GNUNET_Buffer *) h->buffer);
589 GNUNET_free (h->buffer);
590 break;
591 }
592 GNUNET_free (h);
593 return err;
594}
595
596
608{
609 ssize_t ret;
610
611 if (IO_FILE != h->type)
612 return GNUNET_OK;
614 h->buffer,
615 h->have);
616 if (ret != (ssize_t) h->have)
617 {
619 h->fd = NULL;
620 GNUNET_free (h->emsg);
621 GNUNET_asprintf (&h->emsg,
622 "Unable to flush buffer to file");
623 return GNUNET_SYSERR;
624 }
625 h->have = 0;
626 return GNUNET_OK;
627}
628
629
644 char **emsg,
645 void **contents,
646 size_t *size)
647{
649 if (IO_BUFFER != h->type)
650 return GNUNET_SYSERR;
651 if ((NULL == contents) || (NULL == size))
652 return GNUNET_SYSERR;
653 ret = (NULL != h->emsg) ? GNUNET_SYSERR : GNUNET_OK;
654 if (NULL != emsg)
655 *emsg = h->emsg;
656 else
657 GNUNET_free (h->emsg);
658 *contents = GNUNET_buffer_reap ((struct GNUNET_Buffer *) h->buffer, size);
659 return ret;
660}
661
662
674 const char *what,
675 const char *source,
676 size_t len)
677{
678 size_t min;
679 size_t pos = 0;
680 char *buffer = (char *) h->buffer;
681
682 if (NULL == h->fd)
683 {
684 GNUNET_asprintf (&h->emsg,
685 _ ("Error while writing `%s' to file: %s"),
686 what,
687 _ ("No associated file"));
688 return GNUNET_SYSERR;
689 }
690
691 do
692 {
693 min = h->size - h->have;
694 if (len - pos < min)
695 min = len - pos;
696 GNUNET_memcpy (&buffer[h->have], &source[pos], min);
697 pos += min;
698 h->have += min;
699 if (len == pos)
700 return GNUNET_OK;
701 GNUNET_assert (h->have == h->size);
703 {
704 char *tmp = h->emsg;
705 GNUNET_asprintf (&h->emsg,
706 _ ("Error while writing `%s' to file: %s"),
707 what,
708 tmp);
709 GNUNET_free (tmp);
710 return GNUNET_SYSERR;
711 }
712 }
713 while (pos < len);
714 GNUNET_break (0);
715 return GNUNET_OK;
716}
717
718
728static int
730 const char *what,
731 const char *source,
732 size_t len)
733{
734 GNUNET_buffer_write ((struct GNUNET_Buffer *) h->buffer, source, len);
735 h->have += len;
736 return GNUNET_OK;
737}
738
739
751 const char *what,
752 const void *buffer,
753 size_t n)
754{
755 const char *src = buffer;
756
757 if (NULL != h->emsg)
758 return GNUNET_SYSERR;
759
760 if (0 == n)
761 return GNUNET_OK;
762
763 switch (h->type)
764 {
765 case IO_FILE:
766 return write_to_file (h, what, src, n);
767 case IO_BUFFER:
768 return write_to_buffer (h, what, src, n);
769 default:
770 GNUNET_asprintf (&h->emsg,
771 _ ("Invalid handle type while writing `%s'"),
772 what);
773 return GNUNET_SYSERR;
774 }
775}
776
777
788 const char *what,
789 const char *s)
790{
791 uint32_t slen;
792
793 slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
794 if (GNUNET_OK != GNUNET_BIO_write_int32 (h, _ ("string length"), slen))
795 return GNUNET_SYSERR;
796 if (0 != slen)
797 return GNUNET_BIO_write (h, what, s, slen - 1);
798 return GNUNET_OK;
799}
800
801
811 const char *what,
812 float f)
813{
814 int32_t i = f;
815 return GNUNET_BIO_write_int32 (h, what, i);
816}
817
818
828 const char *what,
829 double f)
830{
831 int64_t i = f;
832 return GNUNET_BIO_write_int64 (h, what, i);
833}
834
835
846 const char *what,
847 int32_t i)
848{
849 int32_t big;
850
851 big = htonl (i);
852 return GNUNET_BIO_write (h, what, &big, sizeof(int32_t));
853}
854
855
866 const char *what,
867 int64_t i)
868{
869 int64_t big;
870
871 big = GNUNET_htonll (i);
872 return GNUNET_BIO_write (h, what, &big, sizeof(int64_t));
873}
874
875
886static int
888 struct GNUNET_BIO_ReadHandle *h,
889 const char *what,
890 void *target,
891 size_t target_size)
892{
893 return GNUNET_BIO_read (h, what, target, target_size);
894}
895
896
907 void *result,
908 size_t len)
909{
910 struct GNUNET_BIO_ReadSpec rs = {
912 .cls = NULL,
913 .what = what,
914 .target = result,
915 .size = len,
916 };
917
918 return rs;
919}
920
921
932static int
934 struct GNUNET_BIO_ReadHandle *h,
935 const char *what,
936 void *target,
937 size_t target_size)
938{
939 char **result = target;
940 return GNUNET_BIO_read_string (h, what, result, target_size);
941}
942
943
955 char **result,
956 size_t max_length)
957{
958 struct GNUNET_BIO_ReadSpec rs = {
960 .cls = NULL,
961 .target = result,
962 .size = max_length,
963 };
964
965 return rs;
966}
967
968
979static int
981 struct GNUNET_BIO_ReadHandle *h,
982 const char *what,
983 void *target,
984 size_t target_size)
985{
986 int32_t *result = target;
988}
989
990
1000 int32_t *i)
1001{
1002 struct GNUNET_BIO_ReadSpec rs = {
1004 .cls = NULL,
1005 .target = i,
1006 .size = 0,
1007 };
1008
1009 return rs;
1010}
1011
1012
1023static int
1025 struct GNUNET_BIO_ReadHandle *h,
1026 const char *what,
1027 void *target,
1028 size_t target_size)
1029{
1030 int64_t *result = target;
1032}
1033
1034
1044 int64_t *i)
1045{
1046 struct GNUNET_BIO_ReadSpec rs = {
1048 .cls = NULL,
1049 .target = i,
1050 .size = 0,
1051 };
1052
1053 return rs;
1054}
1055
1056
1064GNUNET_BIO_read_spec_float (const char *what, float *f)
1065{
1066 struct GNUNET_BIO_ReadSpec rs = {
1068 .cls = NULL,
1069 .target = (int32_t *) f,
1070 .size = 0,
1071 };
1072
1073 return rs;
1074}
1075
1076
1084GNUNET_BIO_read_spec_double (const char *what, double *f)
1085{
1086 struct GNUNET_BIO_ReadSpec rs = {
1088 .cls = NULL,
1089 .target = (int64_t *) f,
1090 .size = 0,
1091 };
1092
1093 return rs;
1094}
1095
1096
1107 struct GNUNET_BIO_ReadSpec *rs)
1108{
1109 int ret = GNUNET_OK;
1110
1111 for (size_t i = 0; NULL!=rs[i].rh; ++i)
1112 {
1113 ret = rs[i].rh (rs[i].cls, h, rs[i].what, rs[i].target, rs[i].size);
1114 if (GNUNET_OK != ret)
1115 return ret;
1116 }
1117
1118 return ret;
1119}
1120
1121
1132static int
1134 struct GNUNET_BIO_WriteHandle *h,
1135 const char *what,
1136 void *source,
1137 size_t source_size)
1138{
1139 return GNUNET_BIO_write (h, what, source, source_size);
1140}
1141
1142
1153 void *source,
1154 size_t size)
1155{
1156 struct GNUNET_BIO_WriteSpec ws = {
1158 .cls = NULL,
1159 .what = what,
1160 .source = source,
1161 .source_size = size,
1162 };
1163
1164 return ws;
1165}
1166
1167
1179static int
1181 struct GNUNET_BIO_WriteHandle *h,
1182 const char *what,
1183 void *source,
1184 size_t source_size)
1185{
1186 const char *s = source;
1187 return GNUNET_BIO_write_string (h, what, s);
1188}
1189
1190
1200 const char *s)
1201{
1202 struct GNUNET_BIO_WriteSpec ws = {
1204 .cls = NULL,
1205 .what = what,
1206 .source = (void *) s,
1207 .source_size = 0,
1208 };
1209
1210 return ws;
1211}
1212
1213
1224static int
1226 struct GNUNET_BIO_WriteHandle *h,
1227 const char *what,
1228 void *source,
1229 size_t source_size)
1230{
1231 int32_t i = *(int32_t *) source;
1232 return GNUNET_BIO_write_int32 (h, what, i);
1233}
1234
1235
1245 int32_t *i)
1246{
1247 struct GNUNET_BIO_WriteSpec ws = {
1249 .cls = NULL,
1250 .what = what,
1251 .source = i,
1252 .source_size = 0,
1253 };
1254
1255 return ws;
1256}
1257
1258
1269static int
1271 struct GNUNET_BIO_WriteHandle *h,
1272 const char *what,
1273 void *source,
1274 size_t source_size)
1275{
1276 int64_t i = *(int64_t *) source;
1277 return GNUNET_BIO_write_int64 (h, what, i);
1278}
1279
1280
1290 int64_t *i)
1291{
1292 struct GNUNET_BIO_WriteSpec ws = {
1294 .cls = NULL,
1295 .what = what,
1296 .source = i,
1297 .source_size = 0,
1298 };
1299
1300 return ws;
1301}
1302
1303
1313{
1314 struct GNUNET_BIO_WriteSpec ws = {
1316 .cls = NULL,
1317 .what = what,
1318 .source = (int32_t *) f,
1319 .source_size = 0,
1320 };
1321
1322 return ws;
1323}
1324
1325
1335{
1336 struct GNUNET_BIO_WriteSpec ws = {
1338 .cls = NULL,
1339 .what = what,
1340 .source = (int64_t *) f,
1341 .source_size = 0,
1342 };
1343
1344 return ws;
1345}
1346
1347
1358 struct GNUNET_BIO_WriteSpec *ws)
1359{
1360 int ret = GNUNET_OK;
1361
1362 for (size_t i = 0; NULL!=ws[i].wh; ++i)
1363 {
1364 ret = ws[i].wh (ws[i].cls, h, ws[i].what, ws[i].source, ws[i].source_size);
1365 if (GNUNET_OK != ret)
1366 return ret;
1367 }
1368
1369 /* If it's a file-based handle, the flush makes sure that the data in the
1370 buffer is actually written to the disk. */
1371 if (IO_FILE == h->type)
1373
1374 return ret;
1375}
1376
1377
1378/* end of bio.c */
static int write_spec_handler_int64(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write an (u)int64_t from within a write spec.
Definition: bio.c:1270
static enum GNUNET_GenericReturnValue write_to_file(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *source, size_t len)
Function used internally to write the contents of a buffer into a file.
Definition: bio.c:673
static int read_spec_handler_object(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read some bytes from within a read spec.
Definition: bio.c:887
#define BIO_BUFFER_SIZE
Size for I/O buffers.
Definition: bio.c:42
IOType
Enum used internally to know how buffering is handled.
Definition: bio.c:52
@ IO_BUFFER
The data is stored entirely in memory.
Definition: bio.c:61
@ IO_FILE
The handle uses a file to read/write data.
Definition: bio.c:56
static int read_spec_handler_int64(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read an (u)int64_t from within a read spec.
Definition: bio.c:1024
static int write_spec_handler_object(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write some bytes from within a write spec.
Definition: bio.c:1133
static int write_spec_handler_string(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write a 0-terminated string from within a write spec.
Definition: bio.c:1180
static int read_from_buffer(struct GNUNET_BIO_ReadHandle *h, const char *what, char *result, size_t len)
Function used internally to read the content of a buffer into a buffer.
Definition: bio.c:262
static int read_spec_handler_int32(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read an (u)int32_t from within a read spec.
Definition: bio.c:980
static int write_to_buffer(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *source, size_t len)
Function used internally to write the contents of a buffer to another buffer.
Definition: bio.c:729
static int read_spec_handler_string(void *cls, struct GNUNET_BIO_ReadHandle *h, const char *what, void *target, size_t target_size)
Function used internally to read a string from within a read spec.
Definition: bio.c:933
static int write_spec_handler_int32(void *cls, struct GNUNET_BIO_WriteHandle *h, const char *what, void *source, size_t source_size)
Function used internally to write an (u)int32_t from within a write spec.
Definition: bio.c:1225
static int read_from_file(struct GNUNET_BIO_ReadHandle *h, const char *what, char *result, size_t len)
Function used internally to read the contents of a file into a buffer.
Definition: bio.c:204
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:98
static int ret
Final status code.
Definition: gnunet-arm.c:93
static GstElement * source
Appsrc instance into which we write data for the pipeline.
static int result
Global testing status.
enum GNUNET_GenericReturnValue GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, const char *what, void *result, size_t len)
Read some contents into a buffer.
Definition: bio.c:291
enum GNUNET_GenericReturnValue GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
Definition: bio.c:556
enum GNUNET_GenericReturnValue GNUNET_BIO_write_double(struct GNUNET_BIO_WriteHandle *h, const char *what, double f)
Write a double.
Definition: bio.c:827
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_float(const char *what, float *f)
Create the specification to read a float.
Definition: bio.c:1064
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_string(const char *what, char **result, size_t max_length)
Create the specification to read a 0-terminated string.
Definition: bio.c:954
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
Definition: bio.c:508
enum GNUNET_GenericReturnValue GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, const char *what, int32_t i)
Write an (u)int32_t.
Definition: bio.c:845
enum GNUNET_GenericReturnValue GNUNET_BIO_read_spec_commit(struct GNUNET_BIO_ReadHandle *h, struct GNUNET_BIO_ReadSpec *rs)
Execute the read specifications in order.
Definition: bio.c:1106
enum GNUNET_GenericReturnValue GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h)
Force a file-based buffered writer to flush its buffer.
Definition: bio.c:607
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_object(const char *what, void *result, size_t len)
Create the specification to read a certain amount of bytes.
Definition: bio.c:906
enum GNUNET_GenericReturnValue GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const char *what, const void *buffer, size_t n)
Write a buffer to a handle.
Definition: bio.c:750
enum GNUNET_GenericReturnValue GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h, const char *what, char **result, size_t max_length)
Read 0-terminated string.
Definition: bio.c:330
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_string(const char *what, const char *s)
Create the specification to write a 0-terminated string.
Definition: bio.c:1199
enum GNUNET_GenericReturnValue GNUNET_BIO_get_buffer_contents(struct GNUNET_BIO_WriteHandle *h, char **emsg, void **contents, size_t *size)
Get the IO handle's contents.
Definition: bio.c:643
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_buffer(void)
Create a handle backed by an in-memory buffer.
Definition: bio.c:535
void GNUNET_BIO_read_set_error(struct GNUNET_BIO_ReadHandle *h, const char *emsg)
Set read error to handle.
Definition: bio.c:187
enum GNUNET_GenericReturnValue GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *s)
Write a 0-terminated string.
Definition: bio.c:787
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_int32(const char *what, int32_t *i)
Create the specification to read an (u)int32_t.
Definition: bio.c:999
enum GNUNET_GenericReturnValue GNUNET_BIO_read_float(struct GNUNET_BIO_ReadHandle *h, const char *what, float *f)
Read a float.
Definition: bio.c:392
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_double(const char *what, double *f)
Create the specification to write an double.
Definition: bio.c:1334
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_float(const char *what, float *f)
Create the specification to write a float.
Definition: bio.c:1312
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_object(const char *what, void *source, size_t size)
Create the specification to read some bytes.
Definition: bio.c:1152
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_double(const char *what, double *f)
Create the specification to read a double.
Definition: bio.c:1084
enum GNUNET_GenericReturnValue GNUNET_BIO_write_spec_commit(struct GNUNET_BIO_WriteHandle *h, struct GNUNET_BIO_WriteSpec *ws)
Execute the write specifications in order.
Definition: bio.c:1357
enum GNUNET_GenericReturnValue GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open handle.
Definition: bio.c:162
enum GNUNET_GenericReturnValue GNUNET_BIO_read_int64(struct GNUNET_BIO_ReadHandle *h, const char *what, int64_t *i)
Read an (u)int64_t.
Definition: bio.c:449
enum GNUNET_GenericReturnValue GNUNET_BIO_write_float(struct GNUNET_BIO_WriteHandle *h, const char *what, float f)
Write a float.
Definition: bio.c:810
enum GNUNET_GenericReturnValue GNUNET_BIO_read_int32(struct GNUNET_BIO_ReadHandle *h, const char *what, int32_t *i)
Read an (u)int32_t.
Definition: bio.c:427
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_int32(const char *what, int32_t *i)
Create the specification to write an (u)int32_t.
Definition: bio.c:1244
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_int64(const char *what, int64_t *i)
Create the specification to read an (u)int64_t.
Definition: bio.c:1043
enum GNUNET_GenericReturnValue GNUNET_BIO_read_double(struct GNUNET_BIO_ReadHandle *h, const char *what, double *f)
Read a double.
Definition: bio.c:409
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open_file(const char *fn)
Open a file for reading.
Definition: bio.c:114
enum GNUNET_GenericReturnValue GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, const char *what, int64_t i)
Write an (u)int64_t.
Definition: bio.c:865
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open_buffer(void *buffer, size_t size)
Create a handle from an existing allocated buffer.
Definition: bio.c:139
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_int64(const char *what, int64_t *i)
Create the specification to write an (u)int64_t.
Definition: bio.c:1289
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1238
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:687
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1309
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:623
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:54
void * GNUNET_buffer_reap(struct GNUNET_Buffer *buf, size_t *size)
Clear the buffer and return its contents.
Definition: buffer.c:149
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition: buffer.c:165
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define min(x, y)
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Handle for buffered reading.
Definition: bio.c:69
off_t pos
Current read offset in buffer.
Definition: bio.c:103
size_t size
Total size of buffer.
Definition: bio.c:98
size_t have
Number of bytes available in buffer.
Definition: bio.c:93
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:83
enum IOType type
The "backend" type.
Definition: bio.c:73
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:78
char * buffer
I/O buffer.
Definition: bio.c:88
Structure specifying a reading operation on an IO handle.
void * cls
Closure for rh.
const char * what
What is being read (for error message creation)
GNUNET_BIO_ReadHandler rh
Function performing data deserialization.
void * target
Destination buffer.
Handle for buffered writing.
Definition: bio.c:466
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:480
size_t have
Number of bytes available in buffer.
Definition: bio.c:492
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:475
size_t size
Total size of buffer.
Definition: bio.c:497
enum IOType type
The "backend" type.
Definition: bio.c:470
void * buffer
I/O buffer.
Definition: bio.c:487
Structure specifying a writing operation on an IO handle.
GNUNET_BIO_WriteHandler wh
Function performing data serialization.
size_t source_size
Size of source.
void * cls
Closure for rh.
const char * what
What is being read (for error message creation)
Dynamically growing buffer.
Handle used to access files (and pipes).
int fd
File handle on Unix-like systems.