aboutsummaryrefslogtreecommitdiff
path: root/src/util/bio.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <schanzen@gnunet.org>2022-12-04 23:26:25 +0900
committerMartin Schanzenbach <schanzen@gnunet.org>2022-12-04 23:26:25 +0900
commit395bc9345a005a55e29a7882fdcc82f35c06d2e5 (patch)
treeb111592f9f57025a19ddafe11877f6038263ae7f /src/util/bio.c
parent3f0d91045d29435feef723f09f9ff75c80296d3d (diff)
downloadgnunet-395bc9345a005a55e29a7882fdcc82f35c06d2e5.tar.gz
gnunet-395bc9345a005a55e29a7882fdcc82f35c06d2e5.zip
Large refactor in order to restore some sanity with respect to private defines used in headers
Diffstat (limited to 'src/util/bio.c')
-rw-r--r--src/util/bio.c198
1 files changed, 1 insertions, 197 deletions
diff --git a/src/util/bio.c b/src/util/bio.c
index 1abe6e324..a19e4f3ba 100644
--- a/src/util/bio.c
+++ b/src/util/bio.c
@@ -41,12 +41,6 @@
41 */ 41 */
42#define BIO_BUFFER_SIZE 65536 42#define BIO_BUFFER_SIZE 65536
43 43
44/**
45 * Maximum size allowed for meta data written/read from disk.
46 * File-sharing limits to 64k, so this should be rather generous.
47 */
48#define MAX_META_DATA (1024 * 1024)
49
50 44
51/** 45/**
52 * Enum used internally to know how buffering is handled. 46 * Enum used internally to know how buffering is handled.
@@ -160,7 +154,7 @@ GNUNET_BIO_read_open_buffer (void *buffer, size_t size)
160 * 154 *
161 * @param h file handle 155 * @param h file handle
162 * @param emsg set to the (allocated) error message 156 * @param emsg set to the (allocated) error message
163 * if the handle has an error message, the return 157 * if the handle has an error message, the return
164 * value is #GNUNET_SYSERR 158 * value is #GNUNET_SYSERR
165 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 159 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
166 */ 160 */
@@ -379,60 +373,6 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
379} 373}
380 374
381 375
382/**
383 * Read a metadata container.
384 *
385 * @param h handle to an open file
386 * @param what describes what is being read (for error message creation)
387 * @param result the buffer to store a pointer to the (allocated) metadata
388 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
389 */
390int
391GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
392 const char *what,
393 struct GNUNET_CONTAINER_MetaData **result)
394{
395 uint32_t size;
396 char *buf;
397 struct GNUNET_CONTAINER_MetaData *meta;
398
399 if (GNUNET_OK != GNUNET_BIO_read_int32 (h,
400 _ ("metadata length"),
401 (int32_t *) &size))
402 return GNUNET_SYSERR;
403 if (0 == size)
404 {
405 *result = NULL;
406 return GNUNET_OK;
407 }
408 if (MAX_META_DATA < size)
409 {
410 GNUNET_asprintf (
411 &h->emsg,
412 _ ("Serialized metadata `%s' larger than allowed (%u > %u)"),
413 what,
414 size,
415 MAX_META_DATA);
416 return GNUNET_SYSERR;
417 }
418 buf = GNUNET_malloc (size);
419 if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
420 {
421 GNUNET_free (buf);
422 return GNUNET_SYSERR;
423 }
424 meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
425 if (NULL == meta)
426 {
427 GNUNET_free (buf);
428 GNUNET_asprintf (&h->emsg, _ ("Failed to deserialize metadata `%s'"), what);
429 return GNUNET_SYSERR;
430 }
431 GNUNET_free (buf);
432 *result = meta;
433 return GNUNET_OK;
434}
435
436 376
437/** 377/**
438 * Read a float. 378 * Read a float.
@@ -846,51 +786,6 @@ GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
846} 786}
847 787
848 788
849/**
850 * Write a metadata container.
851 *
852 * @param h the IO handle to write to
853 * @param what what is being written (for error message creation)
854 * @param m metadata to write
855 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
856 */
857int
858GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
859 const char *what,
860 const struct GNUNET_CONTAINER_MetaData *m)
861{
862 ssize_t size;
863 char *buf;
864
865 if (m == NULL)
866 return GNUNET_BIO_write_int32 (h, _ ("metadata length"), 0);
867 buf = NULL;
868 size = GNUNET_CONTAINER_meta_data_serialize (
869 m,
870 &buf,
871 MAX_META_DATA,
872 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
873 if (-1 == size)
874 {
875 GNUNET_free (buf);
876 GNUNET_free (h->emsg);
877 GNUNET_asprintf (&h->emsg,
878 _ ("Failed to serialize metadata `%s'"),
879 what);
880 return GNUNET_SYSERR;
881 }
882 if ((GNUNET_OK != GNUNET_BIO_write_int32 (h,
883 _ ("metadata length"),
884 (uint32_t) size))
885 || (GNUNET_OK != GNUNET_BIO_write (h, what, buf, size)))
886 {
887 GNUNET_free (buf);
888 return GNUNET_SYSERR;
889 }
890 GNUNET_free (buf);
891 return GNUNET_OK;
892}
893
894 789
895/** 790/**
896 * Write a float. 791 * Write a float.
@@ -1060,51 +955,6 @@ GNUNET_BIO_read_spec_string (const char *what,
1060 955
1061 956
1062/** 957/**
1063 * Function used internally to read a metadata container from within a read
1064 * spec.
1065 *
1066 * @param cls ignored, always NULL
1067 * @param h the IO handle to read from
1068 * @param what what is being read (for error message creation)
1069 * @param target where to store the data
1070 * @param target_size ignored
1071 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1072 */
1073static int
1074read_spec_handler_meta_data (void *cls,
1075 struct GNUNET_BIO_ReadHandle *h,
1076 const char *what,
1077 void *target,
1078 size_t target_size)
1079{
1080 struct GNUNET_CONTAINER_MetaData **result = target;
1081 return GNUNET_BIO_read_meta_data (h, what, result);
1082}
1083
1084
1085/**
1086 * Create the specification to read a metadata container.
1087 *
1088 * @param what describes what is being read (for error message creation)
1089 * @param result the buffer to store a pointer to the (allocated) metadata
1090 * @return the read spec
1091 */
1092struct GNUNET_BIO_ReadSpec
1093GNUNET_BIO_read_spec_meta_data (const char *what,
1094 struct GNUNET_CONTAINER_MetaData **result)
1095{
1096 struct GNUNET_BIO_ReadSpec rs = {
1097 .rh = &read_spec_handler_meta_data,
1098 .cls = NULL,
1099 .target = result,
1100 .size = 0,
1101 };
1102
1103 return rs;
1104}
1105
1106
1107/**
1108 * Function used internally to read an (u)int32_t from within a read spec. 958 * Function used internally to read an (u)int32_t from within a read spec.
1109 * 959 *
1110 * @param cls ignored, always NULL 960 * @param cls ignored, always NULL
@@ -1350,52 +1200,6 @@ GNUNET_BIO_write_spec_string (const char *what,
1350 1200
1351 1201
1352/** 1202/**
1353 * Function used internally to write a metadata container from within a write
1354 * spec.
1355 *
1356 * @param cls ignored, always NULL
1357 * @param h the IO handle to write to
1358 * @param what what is being written (for error message creation)
1359 * @param source the data to write
1360 * @param source_size ignored
1361 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1362 */
1363static int
1364write_spec_handler_meta_data (void *cls,
1365 struct GNUNET_BIO_WriteHandle *h,
1366 const char *what,
1367 void *source,
1368 size_t source_size)
1369{
1370 const struct GNUNET_CONTAINER_MetaData *m = source;
1371 return GNUNET_BIO_write_meta_data (h, what, m);
1372}
1373
1374
1375/**
1376 * Create the specification to write a metadata container.
1377 *
1378 * @param what what is being written (for error message creation)
1379 * @param m metadata to write
1380 * @return the write spec
1381 */
1382struct GNUNET_BIO_WriteSpec
1383GNUNET_BIO_write_spec_meta_data (const char *what,
1384 const struct GNUNET_CONTAINER_MetaData *m)
1385{
1386 struct GNUNET_BIO_WriteSpec ws = {
1387 .wh = &write_spec_handler_meta_data,
1388 .cls = NULL,
1389 .what = what,
1390 .source = (void *) m,
1391 .source_size = 0,
1392 };
1393
1394 return ws;
1395}
1396
1397
1398/**
1399 * Function used internally to write an (u)int32_t from within a write spec. 1203 * Function used internally to write an (u)int32_t from within a write spec.
1400 * 1204 *
1401 * @param cls ignored, always NULL 1205 * @param cls ignored, always NULL