GNUnet  0.19.4
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 
51 enum IOType
52 {
56  IO_FILE = 0,
57 
62 };
63 
64 
69 {
73  enum IOType type;
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 
113 struct GNUNET_BIO_ReadHandle *
115 {
116  struct GNUNET_DISK_FileHandle *fd;
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 
138 struct GNUNET_BIO_ReadHandle *
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 
161 int
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 
186 void
188 {
189  GNUNET_assert (NULL == h->emsg);
190  h->emsg = GNUNET_strdup (emsg);
191 }
192 
193 
203 static 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 
261 static 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 
290 int
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 
329 int
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 
391 int
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 
408 int
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 
426 int
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 
448 int
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 {
470  enum IOType type;
471 
476 
480  char *emsg;
481 
487  void *buffer;
488 
492  size_t have;
493 
497  size_t size;
498 };
499 
500 
507 struct GNUNET_BIO_WriteHandle *
509 {
510  struct GNUNET_DISK_FileHandle *fd;
511  struct GNUNET_BIO_WriteHandle *h;
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 
534 struct GNUNET_BIO_WriteHandle *
536 {
537  struct GNUNET_BIO_WriteHandle *h;
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  }
574  if (GNUNET_OK != GNUNET_BIO_flush (h))
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 {
648  if (IO_BUFFER != h->type)
649  return GNUNET_SYSERR;
650  if ((NULL == contents) || (NULL == size))
651  return GNUNET_SYSERR;
653  = (NULL != h->emsg)
654  ? GNUNET_SYSERR
655  : GNUNET_OK;
656  if (NULL != emsg)
657  *emsg = h->emsg;
658  else
659  GNUNET_free (h->emsg);
660  *contents = GNUNET_buffer_reap ((struct GNUNET_Buffer *) h->buffer, size);
661  return ret;
662 }
663 
664 
674 static enum GNUNET_GenericReturnValue
676  const char *what,
677  const char *source,
678  size_t len)
679 {
680  size_t min;
681  size_t pos = 0;
682  char *buffer = (char *) h->buffer;
683 
684  if (NULL == h->fd)
685  {
686  GNUNET_asprintf (&h->emsg,
687  _ ("Error while writing `%s' to file: %s"),
688  what,
689  _ ("No associated file"));
690  return GNUNET_SYSERR;
691  }
692 
693  do
694  {
695  min = h->size - h->have;
696  if (len - pos < min)
697  min = len - pos;
698  GNUNET_memcpy (&buffer[h->have], &source[pos], min);
699  pos += min;
700  h->have += min;
701  if (len == pos)
702  return GNUNET_OK;
703  GNUNET_assert (h->have == h->size);
704  if (GNUNET_OK != GNUNET_BIO_flush (h))
705  {
706  char *tmp = h->emsg;
707  GNUNET_asprintf (&h->emsg,
708  _ ("Error while writing `%s' to file: %s"),
709  what,
710  tmp);
711  GNUNET_free (tmp);
712  return GNUNET_SYSERR;
713  }
714  }
715  while (pos < len);
716  GNUNET_break (0);
717  return GNUNET_OK;
718 }
719 
720 
730 static int
732  const char *what,
733  const char *source,
734  size_t len)
735 {
736  GNUNET_buffer_write ((struct GNUNET_Buffer *) h->buffer, source, len);
737  h->have += len;
738  return GNUNET_OK;
739 }
740 
741 
751 int
753  const char *what,
754  const void *buffer,
755  size_t n)
756 {
757  const char *src = buffer;
758 
759  if (NULL != h->emsg)
760  return GNUNET_SYSERR;
761 
762  if (0 == n)
763  return GNUNET_OK;
764 
765  switch (h->type)
766  {
767  case IO_FILE:
768  return write_to_file (h, what, src, n);
769  case IO_BUFFER:
770  return write_to_buffer (h, what, src, n);
771  default:
772  GNUNET_asprintf (&h->emsg,
773  _ ("Invalid handle type while writing `%s'"),
774  what);
775  return GNUNET_SYSERR;
776  }
777 }
778 
779 
788 int
790  const char *what,
791  const char *s)
792 {
793  uint32_t slen;
794 
795  slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
796  if (GNUNET_OK != GNUNET_BIO_write_int32 (h, _ ("string length"), slen))
797  return GNUNET_SYSERR;
798  if (0 != slen)
799  return GNUNET_BIO_write (h, what, s, slen - 1);
800  return GNUNET_OK;
801 }
802 
803 
811 int
813  const char *what,
814  float f)
815 {
816  int32_t i = f;
817  return GNUNET_BIO_write_int32 (h, what, i);
818 }
819 
820 
828 int
830  const char *what,
831  double f)
832 {
833  int64_t i = f;
834  return GNUNET_BIO_write_int64 (h, what, i);
835 }
836 
837 
846 int
848  const char *what,
849  int32_t i)
850 {
851  int32_t big;
852 
853  big = htonl (i);
854  return GNUNET_BIO_write (h, what, &big, sizeof(int32_t));
855 }
856 
857 
866 int
868  const char *what,
869  int64_t i)
870 {
871  int64_t big;
872 
873  big = GNUNET_htonll (i);
874  return GNUNET_BIO_write (h, what, &big, sizeof(int64_t));
875 }
876 
877 
888 static int
890  struct GNUNET_BIO_ReadHandle *h,
891  const char *what,
892  void *target,
893  size_t target_size)
894 {
895  return GNUNET_BIO_read (h, what, target, target_size);
896 }
897 
898 
907 struct GNUNET_BIO_ReadSpec
908 GNUNET_BIO_read_spec_object (const char *what,
909  void *result,
910  size_t len)
911 {
912  struct GNUNET_BIO_ReadSpec rs = {
914  .cls = NULL,
915  .what = what,
916  .target = result,
917  .size = len,
918  };
919 
920  return rs;
921 }
922 
923 
934 static int
936  struct GNUNET_BIO_ReadHandle *h,
937  const char *what,
938  void *target,
939  size_t target_size)
940 {
941  char **result = target;
942  return GNUNET_BIO_read_string (h, what, result, target_size);
943 }
944 
945 
955 struct GNUNET_BIO_ReadSpec
956 GNUNET_BIO_read_spec_string (const char *what,
957  char **result,
958  size_t max_length)
959 {
960  struct GNUNET_BIO_ReadSpec rs = {
962  .cls = NULL,
963  .target = result,
964  .size = max_length,
965  };
966 
967  return rs;
968 }
969 
970 
981 static int
983  struct GNUNET_BIO_ReadHandle *h,
984  const char *what,
985  void *target,
986  size_t target_size)
987 {
988  int32_t *result = target;
989  return GNUNET_BIO_read_int32 (h, what, result);
990 }
991 
992 
1000 struct GNUNET_BIO_ReadSpec
1001 GNUNET_BIO_read_spec_int32 (const char *what,
1002  int32_t *i)
1003 {
1004  struct GNUNET_BIO_ReadSpec rs = {
1006  .cls = NULL,
1007  .target = i,
1008  .size = 0,
1009  };
1010 
1011  return rs;
1012 }
1013 
1014 
1025 static int
1027  struct GNUNET_BIO_ReadHandle *h,
1028  const char *what,
1029  void *target,
1030  size_t target_size)
1031 {
1032  int64_t *result = target;
1033  return GNUNET_BIO_read_int64 (h, what, result);
1034 }
1035 
1036 
1044 struct GNUNET_BIO_ReadSpec
1045 GNUNET_BIO_read_spec_int64 (const char *what,
1046  int64_t *i)
1047 {
1048  struct GNUNET_BIO_ReadSpec rs = {
1050  .cls = NULL,
1051  .target = i,
1052  .size = 0,
1053  };
1054 
1055  return rs;
1056 }
1057 
1058 
1065 struct GNUNET_BIO_ReadSpec
1066 GNUNET_BIO_read_spec_float (const char *what, float *f)
1067 {
1068  struct GNUNET_BIO_ReadSpec rs = {
1070  .cls = NULL,
1071  .target = (int32_t *) f,
1072  .size = 0,
1073  };
1074 
1075  return rs;
1076 }
1077 
1078 
1085 struct GNUNET_BIO_ReadSpec
1086 GNUNET_BIO_read_spec_double (const char *what, double *f)
1087 {
1088  struct GNUNET_BIO_ReadSpec rs = {
1090  .cls = NULL,
1091  .target = (int64_t *) f,
1092  .size = 0,
1093  };
1094 
1095  return rs;
1096 }
1097 
1098 
1107 int
1109  struct GNUNET_BIO_ReadSpec *rs)
1110 {
1111  int ret = GNUNET_OK;
1112 
1113  for (size_t i = 0; NULL!=rs[i].rh; ++i)
1114  {
1115  ret = rs[i].rh (rs[i].cls, h, rs[i].what, rs[i].target, rs[i].size);
1116  if (GNUNET_OK != ret)
1117  return ret;
1118  }
1119 
1120  return ret;
1121 }
1122 
1123 
1134 static int
1136  struct GNUNET_BIO_WriteHandle *h,
1137  const char *what,
1138  void *source,
1139  size_t source_size)
1140 {
1141  return GNUNET_BIO_write (h, what, source, source_size);
1142 }
1143 
1144 
1153 struct GNUNET_BIO_WriteSpec
1154 GNUNET_BIO_write_spec_object (const char *what,
1155  void *source,
1156  size_t size)
1157 {
1158  struct GNUNET_BIO_WriteSpec ws = {
1160  .cls = NULL,
1161  .what = what,
1162  .source = source,
1163  .source_size = size,
1164  };
1165 
1166  return ws;
1167 }
1168 
1169 
1181 static int
1183  struct GNUNET_BIO_WriteHandle *h,
1184  const char *what,
1185  void *source,
1186  size_t source_size)
1187 {
1188  const char *s = source;
1189  return GNUNET_BIO_write_string (h, what, s);
1190 }
1191 
1192 
1200 struct GNUNET_BIO_WriteSpec
1201 GNUNET_BIO_write_spec_string (const char *what,
1202  const char *s)
1203 {
1204  struct GNUNET_BIO_WriteSpec ws = {
1206  .cls = NULL,
1207  .what = what,
1208  .source = (void *) s,
1209  .source_size = 0,
1210  };
1211 
1212  return ws;
1213 }
1214 
1215 
1226 static int
1228  struct GNUNET_BIO_WriteHandle *h,
1229  const char *what,
1230  void *source,
1231  size_t source_size)
1232 {
1233  int32_t i = *(int32_t *) source;
1234  return GNUNET_BIO_write_int32 (h, what, i);
1235 }
1236 
1237 
1245 struct GNUNET_BIO_WriteSpec
1246 GNUNET_BIO_write_spec_int32 (const char *what,
1247  int32_t *i)
1248 {
1249  struct GNUNET_BIO_WriteSpec ws = {
1251  .cls = NULL,
1252  .what = what,
1253  .source = i,
1254  .source_size = 0,
1255  };
1256 
1257  return ws;
1258 }
1259 
1260 
1271 static int
1273  struct GNUNET_BIO_WriteHandle *h,
1274  const char *what,
1275  void *source,
1276  size_t source_size)
1277 {
1278  int64_t i = *(int64_t *) source;
1279  return GNUNET_BIO_write_int64 (h, what, i);
1280 }
1281 
1282 
1290 struct GNUNET_BIO_WriteSpec
1291 GNUNET_BIO_write_spec_int64 (const char *what,
1292  int64_t *i)
1293 {
1294  struct GNUNET_BIO_WriteSpec ws = {
1296  .cls = NULL,
1297  .what = what,
1298  .source = i,
1299  .source_size = 0,
1300  };
1301 
1302  return ws;
1303 }
1304 
1305 
1313 struct GNUNET_BIO_WriteSpec
1314 GNUNET_BIO_write_spec_float (const char *what, float *f)
1315 {
1316  struct GNUNET_BIO_WriteSpec ws = {
1318  .cls = NULL,
1319  .what = what,
1320  .source = (int32_t *) f,
1321  .source_size = 0,
1322  };
1323 
1324  return ws;
1325 }
1326 
1327 
1335 struct GNUNET_BIO_WriteSpec
1336 GNUNET_BIO_write_spec_double (const char *what, double *f)
1337 {
1338  struct GNUNET_BIO_WriteSpec ws = {
1340  .cls = NULL,
1341  .what = what,
1342  .source = (int64_t *) f,
1343  .source_size = 0,
1344  };
1345 
1346  return ws;
1347 }
1348 
1349 
1358 int
1360  struct GNUNET_BIO_WriteSpec *ws)
1361 {
1362  int ret = GNUNET_OK;
1363 
1364  for (size_t i = 0; NULL!=ws[i].wh; ++i)
1365  {
1366  ret = ws[i].wh (ws[i].cls, h, ws[i].what, ws[i].source, ws[i].source_size);
1367  if (GNUNET_OK != ret)
1368  return ret;
1369  }
1370 
1371  /* If it's a file-based handle, the flush makes sure that the data in the
1372  buffer is actually written to the disk. */
1373  if (IO_FILE == h->type)
1374  ret = GNUNET_BIO_flush (h);
1375 
1376  return ret;
1377 }
1378 
1379 
1380 /* 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:1272
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:675
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:889
#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:1026
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:1135
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:1182
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:982
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:731
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:935
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:1227
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 int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static GstElement * source
Appsrc instance into which we write data for the pipeline.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static int result
Global testing status.
static char buf[2048]
int GNUNET_BIO_write_float(struct GNUNET_BIO_WriteHandle *h, const char *what, float f)
Write a float.
Definition: bio.c:812
int GNUNET_BIO_read_float(struct GNUNET_BIO_ReadHandle *h, const char *what, float *f)
Read a float.
Definition: bio.c:392
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_float(const char *what, float *f)
Create the specification to read a float.
Definition: bio.c:1066
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:956
int 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
int GNUNET_BIO_write_spec_commit(struct GNUNET_BIO_WriteHandle *h, struct GNUNET_BIO_WriteSpec *ws)
Execute the write specifications in order.
Definition: bio.c:1359
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_buffer(void)
Create a handle backed by an in-memory buffer.
Definition: bio.c:535
int 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_ReadHandle * GNUNET_BIO_read_open_file(const char *fn)
Open a file for reading.
Definition: bio.c:114
int 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:752
int GNUNET_BIO_read_int64(struct GNUNET_BIO_ReadHandle *h, const char *what, int64_t *i)
Read an (u)int64_t.
Definition: bio.c:449
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:908
int GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *what, const char *s)
Write a 0-terminated string.
Definition: bio.c:789
enum GNUNET_GenericReturnValue GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
Definition: bio.c:556
int GNUNET_BIO_write_int32(struct GNUNET_BIO_WriteHandle *h, const char *what, int32_t i)
Write an (u)int32_t.
Definition: bio.c:847
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:1201
int GNUNET_BIO_read_spec_commit(struct GNUNET_BIO_ReadHandle *h, struct GNUNET_BIO_ReadSpec *rs)
Execute the read specifications in order.
Definition: bio.c:1108
void GNUNET_BIO_read_set_error(struct GNUNET_BIO_ReadHandle *h, const char *emsg)
Set read error to handle.
Definition: bio.c:187
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:1001
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
int GNUNET_BIO_write_double(struct GNUNET_BIO_WriteHandle *h, const char *what, double f)
Write a double.
Definition: bio.c:829
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
int GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open handle.
Definition: bio.c:162
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_double(const char *what, double *f)
Create the specification to write an double.
Definition: bio.c:1336
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_float(const char *what, float *f)
Create the specification to write a float.
Definition: bio.c:1314
int GNUNET_BIO_write_int64(struct GNUNET_BIO_WriteHandle *h, const char *what, int64_t i)
Write an (u)int64_t.
Definition: bio.c:867
int 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
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:1154
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
Definition: bio.c:508
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_double(const char *what, double *f)
Create the specification to read a double.
Definition: bio.c:1086
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:1246
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:1045
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_WriteSpec GNUNET_BIO_write_spec_int64(const char *what, int64_t *i)
Create the specification to write an (u)int64_t.
Definition: bio.c:1291
int GNUNET_BIO_read_double(struct GNUNET_BIO_ReadHandle *h, const char *what, double *f)
Read a double.
Definition: bio.c:409
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:1237
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:686
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1308
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:622
@ 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.
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_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:54
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:164
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:177
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.