GNUnet  0.20.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 "gnunet_common.h"
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 
30 #define LOG(kind, ...) GNUNET_log_from (kind, "util-bio", __VA_ARGS__)
31 
32 #ifndef PATH_MAX
36 #define PATH_MAX 4096
37 #endif
38 
39 
43 #define BIO_BUFFER_SIZE 65536
44 
45 
52 enum IOType
53 {
57  IO_FILE = 0,
58 
63 };
64 
65 
70 {
74  enum IOType type;
75 
80 
84  char *emsg;
85 
89  char *buffer;
90 
94  size_t have;
95 
99  size_t size;
100 
104  off_t pos;
105 };
106 
107 
114 struct GNUNET_BIO_ReadHandle *
116 {
117  struct GNUNET_DISK_FileHandle *fd;
118  struct GNUNET_BIO_ReadHandle *h;
119 
121  if (NULL == fd)
122  return NULL;
124  h->type = IO_FILE;
125  h->buffer = (char *) &h[1];
126  h->size = BIO_BUFFER_SIZE;
127  h->fd = fd;
128  return h;
129 }
130 
131 
139 struct GNUNET_BIO_ReadHandle *
141 {
142  struct GNUNET_BIO_ReadHandle *h;
143 
145  h->type = IO_BUFFER;
146  h->buffer = buffer;
147  h->size = size;
148  return h;
149 }
150 
151 
164 {
165  int err;
166 
167  err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
168  if (NULL != emsg)
169  *emsg = h->emsg;
170  else
171  GNUNET_free (h->emsg);
172  switch (h->type)
173  {
174  case IO_FILE:
176  break;
177  case IO_BUFFER:
178  break;
179  default:
180  break;
181  }
182  GNUNET_free (h);
183  return err;
184 }
185 
186 
187 void
189 {
190  GNUNET_assert (NULL == h->emsg);
191  h->emsg = GNUNET_strdup (emsg);
192 }
193 
194 
204 static int
206  const char *what,
207  char *result,
208  size_t len)
209 {
210  size_t pos = 0;
211  size_t min;
212  ssize_t ret;
213 
214  do
215  {
216  min = h->have - h->pos;
217  if (0 < min)
218  {
219  if (len - pos < min)
220  min = len - pos;
221  GNUNET_memcpy (&result[pos], &h->buffer[h->pos], min);
222  h->pos += min;
223  pos += min;
224  }
225  if (len == pos)
226  return GNUNET_OK;
227  GNUNET_assert (((off_t) h->have) == h->pos);
228  ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
229  if (-1 == ret)
230  {
231  GNUNET_asprintf (&h->emsg,
232  _ ("Error reading `%s' from file: %s"),
233  what,
234  strerror (errno));
235  return GNUNET_SYSERR;
236  }
237  if (0 == ret)
238  {
239  GNUNET_asprintf (&h->emsg,
240  _ ("Error reading `%s' from file: %s"),
241  what,
242  _ ("End of file"));
243  return GNUNET_SYSERR;
244  }
245  h->pos = 0;
246  h->have = ret;
247  }
248  while (pos < len);
249  return GNUNET_OK;
250 }
251 
252 
262 static int
264  const char *what,
265  char *result,
266  size_t len)
267 {
268  if ((h->size < len) || (h->size - h->pos < len))
269  {
270  GNUNET_asprintf (&h->emsg,
271  _ ("Error while reading `%s' from buffer: %s"),
272  what,
273  _ ("Not enough data left"));
274  return GNUNET_SYSERR;
275  }
276  GNUNET_memcpy (result, h->buffer + h->pos, len);
277  h->pos += len;
278  return GNUNET_OK;
279 }
280 
281 
293  const char *what,
294  void *result,
295  size_t len)
296 {
297  char *dst = result;
298 
299  if (NULL != h->emsg)
300  return GNUNET_SYSERR;
301 
302  if (0 == len)
303  return GNUNET_OK;
304 
305  switch (h->type)
306  {
307  case IO_FILE:
308  return read_from_file (h, what, dst, len);
309  case IO_BUFFER:
310  return read_from_buffer (h, what, dst, len);
311  default:
312  GNUNET_asprintf (&h->emsg,
313  _ ("Invalid handle type while reading `%s'"),
314  what);
315  return GNUNET_SYSERR;
316  }
317 }
318 
319 
332  const char *what,
333  char **result,
334  size_t max_length)
335 {
336  char *buf;
337  uint32_t big;
338 
340  _ ("string length"),
341  (int32_t *) &big))
342  {
343  char *tmp = h->emsg;
344  if (NULL != tmp)
345  GNUNET_asprintf (&h->emsg,
346  _ ("%s (while reading `%s')"),
347  tmp,
348  what);
349  else
350  GNUNET_asprintf (&h->emsg,
351  _ ("Error reading length of string `%s'"),
352  what);
353  GNUNET_free (tmp);
354  return GNUNET_SYSERR;
355  }
356  if (0 == big)
357  {
358  *result = NULL;
359  return GNUNET_OK;
360  }
361  if (big > max_length)
362  {
363  GNUNET_asprintf (&h->emsg,
364  _ ("String `%s' longer than allowed (%u > %lu)"),
365  what,
366  big,
367  (unsigned long) max_length);
368  return GNUNET_SYSERR;
369  }
370  buf = GNUNET_malloc (big);
371  *result = buf;
372  buf[--big] = '\0';
373  if (0 == big)
374  return GNUNET_OK;
375  if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
376  {
377  GNUNET_free (buf);
378  *result = NULL;
379  return GNUNET_SYSERR;
380  }
381  return GNUNET_OK;
382 }
383 
384 
394  const char *what,
395  float *f)
396 {
397  int32_t *i = (int32_t *) f;
398  return GNUNET_BIO_read_int32 (h, what, i);
399 }
400 
401 
411  const char *what,
412  double *f)
413 {
414  int64_t *i = (int64_t *) f;
415  return GNUNET_BIO_read_int64 (h, what, i);
416 }
417 
418 
429  const char *what,
430  int32_t *i)
431 {
432  int32_t big;
433 
434  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int32_t)))
435  return GNUNET_SYSERR;
436  *i = ntohl (big);
437  return GNUNET_OK;
438 }
439 
440 
451  const char *what,
452  int64_t *i)
453 {
454  int64_t big;
455 
456  if (GNUNET_OK != GNUNET_BIO_read (h, what, &big, sizeof(int64_t)))
457  return GNUNET_SYSERR;
458  *i = GNUNET_ntohll (big);
459  return GNUNET_OK;
460 }
461 
462 
467 {
471  enum IOType type;
472 
477 
481  char *emsg;
482 
488  void *buffer;
489 
493  size_t have;
494 
498  size_t size;
499 };
500 
501 
508 struct GNUNET_BIO_WriteHandle *
510 {
511  struct GNUNET_DISK_FileHandle *fd;
512  struct GNUNET_BIO_WriteHandle *h;
513 
520  if (NULL == fd)
521  return NULL;
523  h->buffer = &h[1];
524  h->size = BIO_BUFFER_SIZE;
525  h->fd = fd;
526  return h;
527 }
528 
529 
535 struct GNUNET_BIO_WriteHandle *
537 {
538  struct GNUNET_BIO_WriteHandle *h;
539 
541  h->type = IO_BUFFER;
542  h->buffer = (void *) GNUNET_malloc (sizeof (struct GNUNET_Buffer));
543  return h;
544 }
545 
546 
558  char **emsg)
559 {
560  int err;
561 
562  err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
563  if (NULL != emsg)
564  *emsg = h->emsg;
565  else
566  GNUNET_free (h->emsg);
567  switch (h->type)
568  {
569  case IO_FILE:
570  if (NULL == h->fd)
571  {
572  err = GNUNET_SYSERR;
573  break;
574  }
575  if (GNUNET_OK != GNUNET_BIO_flush (h))
576  {
577  if (NULL != emsg)
578  *emsg = h->emsg;
579  else
580  GNUNET_free (h->emsg);
581  err = GNUNET_SYSERR;
582  }
583  else
584  {
586  }
587  break;
588  case IO_BUFFER:
589  GNUNET_buffer_clear ((struct GNUNET_Buffer *) h->buffer);
590  GNUNET_free (h->buffer);
591  break;
592  }
593  GNUNET_free (h);
594  return err;
595 }
596 
597 
609 {
610  ssize_t ret;
611 
612  if (IO_FILE != h->type)
613  return GNUNET_OK;
615  h->buffer,
616  h->have);
617  if (ret != (ssize_t) h->have)
618  {
620  h->fd = NULL;
621  GNUNET_free (h->emsg);
622  GNUNET_asprintf (&h->emsg,
623  "Unable to flush buffer to file");
624  return GNUNET_SYSERR;
625  }
626  h->have = 0;
627  return GNUNET_OK;
628 }
629 
630 
645  char **emsg,
646  void **contents,
647  size_t *size)
648 {
649  if (IO_BUFFER != h->type)
650  return GNUNET_SYSERR;
651  if ((NULL == contents) || (NULL == size))
652  return GNUNET_SYSERR;
654  = (NULL != h->emsg)
655  ? GNUNET_SYSERR
656  : GNUNET_OK;
657  if (NULL != emsg)
658  *emsg = h->emsg;
659  else
660  GNUNET_free (h->emsg);
661  *contents = GNUNET_buffer_reap ((struct GNUNET_Buffer *) h->buffer, size);
662  return ret;
663 }
664 
665 
675 static enum GNUNET_GenericReturnValue
677  const char *what,
678  const char *source,
679  size_t len)
680 {
681  size_t min;
682  size_t pos = 0;
683  char *buffer = (char *) h->buffer;
684 
685  if (NULL == h->fd)
686  {
687  GNUNET_asprintf (&h->emsg,
688  _ ("Error while writing `%s' to file: %s"),
689  what,
690  _ ("No associated file"));
691  return GNUNET_SYSERR;
692  }
693 
694  do
695  {
696  min = h->size - h->have;
697  if (len - pos < min)
698  min = len - pos;
699  GNUNET_memcpy (&buffer[h->have], &source[pos], min);
700  pos += min;
701  h->have += min;
702  if (len == pos)
703  return GNUNET_OK;
704  GNUNET_assert (h->have == h->size);
705  if (GNUNET_OK != GNUNET_BIO_flush (h))
706  {
707  char *tmp = h->emsg;
708  GNUNET_asprintf (&h->emsg,
709  _ ("Error while writing `%s' to file: %s"),
710  what,
711  tmp);
712  GNUNET_free (tmp);
713  return GNUNET_SYSERR;
714  }
715  }
716  while (pos < len);
717  GNUNET_break (0);
718  return GNUNET_OK;
719 }
720 
721 
731 static int
733  const char *what,
734  const char *source,
735  size_t len)
736 {
737  GNUNET_buffer_write ((struct GNUNET_Buffer *) h->buffer, source, len);
738  h->have += len;
739  return GNUNET_OK;
740 }
741 
742 
754  const char *what,
755  const void *buffer,
756  size_t n)
757 {
758  const char *src = buffer;
759 
760  if (NULL != h->emsg)
761  return GNUNET_SYSERR;
762 
763  if (0 == n)
764  return GNUNET_OK;
765 
766  switch (h->type)
767  {
768  case IO_FILE:
769  return write_to_file (h, what, src, n);
770  case IO_BUFFER:
771  return write_to_buffer (h, what, src, n);
772  default:
773  GNUNET_asprintf (&h->emsg,
774  _ ("Invalid handle type while writing `%s'"),
775  what);
776  return GNUNET_SYSERR;
777  }
778 }
779 
780 
791  const char *what,
792  const char *s)
793 {
794  uint32_t slen;
795 
796  slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
797  if (GNUNET_OK != GNUNET_BIO_write_int32 (h, _ ("string length"), slen))
798  return GNUNET_SYSERR;
799  if (0 != slen)
800  return GNUNET_BIO_write (h, what, s, slen - 1);
801  return GNUNET_OK;
802 }
803 
804 
814  const char *what,
815  float f)
816 {
817  int32_t i = f;
818  return GNUNET_BIO_write_int32 (h, what, i);
819 }
820 
821 
831  const char *what,
832  double f)
833 {
834  int64_t i = f;
835  return GNUNET_BIO_write_int64 (h, what, i);
836 }
837 
838 
849  const char *what,
850  int32_t i)
851 {
852  int32_t big;
853 
854  big = htonl (i);
855  return GNUNET_BIO_write (h, what, &big, sizeof(int32_t));
856 }
857 
858 
869  const char *what,
870  int64_t i)
871 {
872  int64_t big;
873 
874  big = GNUNET_htonll (i);
875  return GNUNET_BIO_write (h, what, &big, sizeof(int64_t));
876 }
877 
878 
889 static int
891  struct GNUNET_BIO_ReadHandle *h,
892  const char *what,
893  void *target,
894  size_t target_size)
895 {
896  return GNUNET_BIO_read (h, what, target, target_size);
897 }
898 
899 
908 struct GNUNET_BIO_ReadSpec
909 GNUNET_BIO_read_spec_object (const char *what,
910  void *result,
911  size_t len)
912 {
913  struct GNUNET_BIO_ReadSpec rs = {
915  .cls = NULL,
916  .what = what,
917  .target = result,
918  .size = len,
919  };
920 
921  return rs;
922 }
923 
924 
935 static int
937  struct GNUNET_BIO_ReadHandle *h,
938  const char *what,
939  void *target,
940  size_t target_size)
941 {
942  char **result = target;
943  return GNUNET_BIO_read_string (h, what, result, target_size);
944 }
945 
946 
956 struct GNUNET_BIO_ReadSpec
957 GNUNET_BIO_read_spec_string (const char *what,
958  char **result,
959  size_t max_length)
960 {
961  struct GNUNET_BIO_ReadSpec rs = {
963  .cls = NULL,
964  .target = result,
965  .size = max_length,
966  };
967 
968  return rs;
969 }
970 
971 
982 static int
984  struct GNUNET_BIO_ReadHandle *h,
985  const char *what,
986  void *target,
987  size_t target_size)
988 {
989  int32_t *result = target;
990  return GNUNET_BIO_read_int32 (h, what, result);
991 }
992 
993 
1001 struct GNUNET_BIO_ReadSpec
1002 GNUNET_BIO_read_spec_int32 (const char *what,
1003  int32_t *i)
1004 {
1005  struct GNUNET_BIO_ReadSpec rs = {
1007  .cls = NULL,
1008  .target = i,
1009  .size = 0,
1010  };
1011 
1012  return rs;
1013 }
1014 
1015 
1026 static int
1028  struct GNUNET_BIO_ReadHandle *h,
1029  const char *what,
1030  void *target,
1031  size_t target_size)
1032 {
1033  int64_t *result = target;
1034  return GNUNET_BIO_read_int64 (h, what, result);
1035 }
1036 
1037 
1045 struct GNUNET_BIO_ReadSpec
1046 GNUNET_BIO_read_spec_int64 (const char *what,
1047  int64_t *i)
1048 {
1049  struct GNUNET_BIO_ReadSpec rs = {
1051  .cls = NULL,
1052  .target = i,
1053  .size = 0,
1054  };
1055 
1056  return rs;
1057 }
1058 
1059 
1066 struct GNUNET_BIO_ReadSpec
1067 GNUNET_BIO_read_spec_float (const char *what, float *f)
1068 {
1069  struct GNUNET_BIO_ReadSpec rs = {
1071  .cls = NULL,
1072  .target = (int32_t *) f,
1073  .size = 0,
1074  };
1075 
1076  return rs;
1077 }
1078 
1079 
1086 struct GNUNET_BIO_ReadSpec
1087 GNUNET_BIO_read_spec_double (const char *what, double *f)
1088 {
1089  struct GNUNET_BIO_ReadSpec rs = {
1091  .cls = NULL,
1092  .target = (int64_t *) f,
1093  .size = 0,
1094  };
1095 
1096  return rs;
1097 }
1098 
1099 
1110  struct GNUNET_BIO_ReadSpec *rs)
1111 {
1112  int ret = GNUNET_OK;
1113 
1114  for (size_t i = 0; NULL!=rs[i].rh; ++i)
1115  {
1116  ret = rs[i].rh (rs[i].cls, h, rs[i].what, rs[i].target, rs[i].size);
1117  if (GNUNET_OK != ret)
1118  return ret;
1119  }
1120 
1121  return ret;
1122 }
1123 
1124 
1135 static int
1137  struct GNUNET_BIO_WriteHandle *h,
1138  const char *what,
1139  void *source,
1140  size_t source_size)
1141 {
1142  return GNUNET_BIO_write (h, what, source, source_size);
1143 }
1144 
1145 
1154 struct GNUNET_BIO_WriteSpec
1155 GNUNET_BIO_write_spec_object (const char *what,
1156  void *source,
1157  size_t size)
1158 {
1159  struct GNUNET_BIO_WriteSpec ws = {
1161  .cls = NULL,
1162  .what = what,
1163  .source = source,
1164  .source_size = size,
1165  };
1166 
1167  return ws;
1168 }
1169 
1170 
1182 static int
1184  struct GNUNET_BIO_WriteHandle *h,
1185  const char *what,
1186  void *source,
1187  size_t source_size)
1188 {
1189  const char *s = source;
1190  return GNUNET_BIO_write_string (h, what, s);
1191 }
1192 
1193 
1201 struct GNUNET_BIO_WriteSpec
1202 GNUNET_BIO_write_spec_string (const char *what,
1203  const char *s)
1204 {
1205  struct GNUNET_BIO_WriteSpec ws = {
1207  .cls = NULL,
1208  .what = what,
1209  .source = (void *) s,
1210  .source_size = 0,
1211  };
1212 
1213  return ws;
1214 }
1215 
1216 
1227 static int
1229  struct GNUNET_BIO_WriteHandle *h,
1230  const char *what,
1231  void *source,
1232  size_t source_size)
1233 {
1234  int32_t i = *(int32_t *) source;
1235  return GNUNET_BIO_write_int32 (h, what, i);
1236 }
1237 
1238 
1246 struct GNUNET_BIO_WriteSpec
1247 GNUNET_BIO_write_spec_int32 (const char *what,
1248  int32_t *i)
1249 {
1250  struct GNUNET_BIO_WriteSpec ws = {
1252  .cls = NULL,
1253  .what = what,
1254  .source = i,
1255  .source_size = 0,
1256  };
1257 
1258  return ws;
1259 }
1260 
1261 
1272 static int
1274  struct GNUNET_BIO_WriteHandle *h,
1275  const char *what,
1276  void *source,
1277  size_t source_size)
1278 {
1279  int64_t i = *(int64_t *) source;
1280  return GNUNET_BIO_write_int64 (h, what, i);
1281 }
1282 
1283 
1291 struct GNUNET_BIO_WriteSpec
1292 GNUNET_BIO_write_spec_int64 (const char *what,
1293  int64_t *i)
1294 {
1295  struct GNUNET_BIO_WriteSpec ws = {
1297  .cls = NULL,
1298  .what = what,
1299  .source = i,
1300  .source_size = 0,
1301  };
1302 
1303  return ws;
1304 }
1305 
1306 
1314 struct GNUNET_BIO_WriteSpec
1315 GNUNET_BIO_write_spec_float (const char *what, float *f)
1316 {
1317  struct GNUNET_BIO_WriteSpec ws = {
1319  .cls = NULL,
1320  .what = what,
1321  .source = (int32_t *) f,
1322  .source_size = 0,
1323  };
1324 
1325  return ws;
1326 }
1327 
1328 
1336 struct GNUNET_BIO_WriteSpec
1337 GNUNET_BIO_write_spec_double (const char *what, double *f)
1338 {
1339  struct GNUNET_BIO_WriteSpec ws = {
1341  .cls = NULL,
1342  .what = what,
1343  .source = (int64_t *) f,
1344  .source_size = 0,
1345  };
1346 
1347  return ws;
1348 }
1349 
1350 
1361  struct GNUNET_BIO_WriteSpec *ws)
1362 {
1363  int ret = GNUNET_OK;
1364 
1365  for (size_t i = 0; NULL!=ws[i].wh; ++i)
1366  {
1367  ret = ws[i].wh (ws[i].cls, h, ws[i].what, ws[i].source, ws[i].source_size);
1368  if (GNUNET_OK != ret)
1369  return ret;
1370  }
1371 
1372  /* If it's a file-based handle, the flush makes sure that the data in the
1373  buffer is actually written to the disk. */
1374  if (IO_FILE == h->type)
1375  ret = GNUNET_BIO_flush (h);
1376 
1377  return ret;
1378 }
1379 
1380 
1381 /* 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:1273
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:676
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:890
#define BIO_BUFFER_SIZE
Size for I/O buffers.
Definition: bio.c:43
IOType
Enum used internally to know how buffering is handled.
Definition: bio.c:53
@ IO_BUFFER
The data is stored entirely in memory.
Definition: bio.c:62
@ IO_FILE
The handle uses a file to read/write data.
Definition: bio.c:57
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:1027
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:1136
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:1183
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:263
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:983
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:732
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:936
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:1228
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:205
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]
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
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:292
enum GNUNET_GenericReturnValue GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
Definition: bio.c:557
enum GNUNET_GenericReturnValue GNUNET_BIO_write_double(struct GNUNET_BIO_WriteHandle *h, const char *what, double f)
Write a double.
Definition: bio.c:830
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_float(const char *what, float *f)
Create the specification to read a float.
Definition: bio.c:1067
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:957
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:848
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_buffer(void)
Create a handle backed by an in-memory buffer.
Definition: bio.c:536
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:1109
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open_file(const char *fn)
Open a file for reading.
Definition: bio.c:115
enum GNUNET_GenericReturnValue GNUNET_BIO_flush(struct GNUNET_BIO_WriteHandle *h)
Force a file-based buffered writer to flush its buffer.
Definition: bio.c:608
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:909
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:753
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:331
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:1202
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:644
void GNUNET_BIO_read_set_error(struct GNUNET_BIO_ReadHandle *h, const char *emsg)
Set read error to handle.
Definition: bio.c:188
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:790
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:1002
enum GNUNET_GenericReturnValue GNUNET_BIO_read_float(struct GNUNET_BIO_ReadHandle *h, const char *what, float *f)
Read a float.
Definition: bio.c:393
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:140
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_double(const char *what, double *f)
Create the specification to write an double.
Definition: bio.c:1337
struct GNUNET_BIO_WriteSpec GNUNET_BIO_write_spec_float(const char *what, float *f)
Create the specification to write a float.
Definition: bio.c:1315
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:1155
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
Definition: bio.c:509
struct GNUNET_BIO_ReadSpec GNUNET_BIO_read_spec_double(const char *what, double *f)
Create the specification to read a double.
Definition: bio.c:1087
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:1360
enum GNUNET_GenericReturnValue GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open handle.
Definition: bio.c:163
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:450
enum GNUNET_GenericReturnValue GNUNET_BIO_write_float(struct GNUNET_BIO_WriteHandle *h, const char *what, float f)
Write a float.
Definition: bio.c:813
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:428
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:1247
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:1046
enum GNUNET_GenericReturnValue GNUNET_BIO_read_double(struct GNUNET_BIO_ReadHandle *h, const char *what, double *f)
Read a double.
Definition: bio.c:410
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:868
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:1292
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:178
Handle for buffered reading.
Definition: bio.c:70
off_t pos
Current read offset in buffer.
Definition: bio.c:104
size_t size
Total size of buffer.
Definition: bio.c:99
size_t have
Number of bytes available in buffer.
Definition: bio.c:94
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:84
enum IOType type
The "backend" type.
Definition: bio.c:74
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:79
char * buffer
I/O buffer.
Definition: bio.c:89
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:467
char * emsg
Error message, NULL if there were no errors.
Definition: bio.c:481
size_t have
Number of bytes available in buffer.
Definition: bio.c:493
struct GNUNET_DISK_FileHandle * fd
Handle to a file on disk, if type is IO_FILE.
Definition: bio.c:476
size_t size
Total size of buffer.
Definition: bio.c:498
enum IOType type
The "backend" type.
Definition: bio.c:471
void * buffer
I/O buffer.
Definition: bio.c:488
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.