2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Christian Grothoff
27 * @defgroup bio BIO library
28 * Buffered binary disk IO (with endianess conversion)
32 #ifndef GNUNET_BIO_LIB_H
33 #define GNUNET_BIO_LIB_H
35 #include "gnunet_container_lib.h"
40 #if 0 /* keep Emacsens' auto-indent happy */
45 /****************************** READING API *******************************/
48 * Handle for buffered reading.
50 struct GNUNET_BIO_ReadHandle;
54 * Open a file for reading.
56 * @param fn file name to be opened
57 * @return IO handle on success, NULL on error
59 struct GNUNET_BIO_ReadHandle *
60 GNUNET_BIO_read_open_file (const char *fn);
64 * Create a handle from an existing allocated buffer.
66 * @param buffer the buffer to use as source
67 * @param size the total size in bytes of the buffer
68 * @return IO handle on sucess, NULL on error
70 struct GNUNET_BIO_ReadHandle *
71 GNUNET_BIO_read_open_buffer (void *buffer, size_t size);
75 * Close an open handle. Reports if any errors reading
76 * from the file were encountered.
78 * @param h file handle
79 * @param emsg set to the error message
80 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
83 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg);
87 * Read some contents into a buffer.
89 * @param h the IO handle to read from
90 * @param what describes what is being read (for error message creation)
91 * @param result the buffer to write the result to
92 * @param len the number of bytes to read
93 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
96 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
103 * Read 0-terminated string.
105 * @param h the IO handle to read from
106 * @param what describes what is being read (for error message creation)
107 * @param result where to store the pointer to the (allocated) string
108 * (note that *result could be set to NULL as well)
109 * @param max_length maximum allowed length for the string
110 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
113 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
120 * Read a metadata container.
122 * @param h handle to an open file
123 * @param what describes what is being read (for error message creation)
124 * @param result the buffer to store a pointer to the (allocated) metadata
125 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
128 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
130 struct GNUNET_CONTAINER_MetaData **result);
136 * @param h the IO handle to read from
137 * @param what describes what is being read (for error message creation)
138 * @param f address of float to read
141 GNUNET_BIO_read_float(struct GNUNET_BIO_ReadHandle *h,
149 * @param h the IO handle to read from
150 * @param what describes what is being read (for error message creation)
151 * @param f address of double to read
154 GNUNET_BIO_read_double(struct GNUNET_BIO_ReadHandle *h,
161 * Read an (u)int32_t.
163 * @param h the IO handle to read from
164 * @param what describes what is being read (for error message creation)
165 * @param i where to store the data
166 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
169 GNUNET_BIO_read_int32 (struct GNUNET_BIO_ReadHandle *h,
176 * Read an (u)int64_t.
178 * @param h the IO handle to read from
179 * @param what describes what is being read (for error message creation)
180 * @param i where to store the data
181 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
184 GNUNET_BIO_read_int64 (struct GNUNET_BIO_ReadHandle *h,
190 /****************************** WRITING API *******************************/
193 * Handle for buffered writing.
195 struct GNUNET_BIO_WriteHandle;
198 * Open a file for writing.
200 * @param fn name of the file to be opened
201 * @return IO handle on success, NULL on error
203 struct GNUNET_BIO_WriteHandle *
204 GNUNET_BIO_write_open_file (const char *fn);
208 * Create a handle backed by an in-memory buffer.
210 * @return IO handle on success, NULL on error
212 struct GNUNET_BIO_WriteHandle *
213 GNUNET_BIO_write_open_buffer (void);
217 * Force a file-based buffered writer to flush its buffer.
218 * If the handle does not use a file, this function returs #GNUNET_OK
219 * without doing anything.
221 * @param h the IO handle
222 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned
223 * and the file is closed
226 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h);
230 * Get the IO handle's contents.
231 * If the handle doesn't use an in-memory buffer, this function returns
234 * @param h the IO handle
235 * @param emsg set to the (allocated) error message
236 * if the handle has an error message the return value is #GNUNET_SYSERR
237 * @param contents where to store the pointer to the handle's contents
238 * @param size where to store the size of @e contents
239 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
242 GNUNET_BIO_get_buffer_contents (struct GNUNET_BIO_WriteHandle *h,
249 * Close an IO handle.
250 * If the handle was using a file, the file will be closed.
252 * @param h file handle
253 * @param emsg set to the (allocated) error message
254 * if the handle has an error message, the return value is #GNUNET_SYSERR
255 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
258 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h, char **emsg);
262 * Write a buffer to a handle.
264 * @param h the IO handle to write to
265 * @param what what is being written (for error message creation)
266 * @param buffer the data to write
267 * @param n number of bytes to write
268 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
271 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
278 * Write a 0-terminated string.
280 * @param h the IO handle to write to
281 * @param what what is being written (for error message creation)
282 * @param s string to write (can be NULL)
283 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
286 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h,
292 * Write a metadata container.
294 * @param h the IO handle to write to
295 * @param what what is being written (for error message creation)
296 * @param m metadata to write
297 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
300 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
302 const struct GNUNET_CONTAINER_MetaData *m);
308 * @param h the IO handle to write to
309 * @param what what is being written (for error message creation)
310 * @param f float to write (must be a variable)
313 GNUNET_BIO_write_float(struct GNUNET_BIO_WriteHandle *h,
320 * @param h the IO handle to write to
321 * @param what what is being written (for error message creation)
322 * @param f double to write (must be a variable)
325 GNUNET_BIO_write_double(struct GNUNET_BIO_WriteHandle *h,
331 * Write an (u)int32_t.
333 * @param h the IO handle to write to
334 * @param what what is being written (for error message creation)
335 * @param i 32-bit integer to write
336 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
339 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
345 * Write an (u)int64_t.
347 * @param h the IO handle to write to
348 * @param what what is being written (for error message creation)
349 * @param i 64-bit integer to write
350 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
353 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
358 /****************************** READ SPEC API ***************************/
362 * Function used to deserialize data read from @a h and store it into @a
365 * @param cls closure (can be NULL)
366 * @param h the IO handle to read from
367 * @param what what is being read (for error message creation)
368 * @param target where to store the data
369 * @param target_size how many bytes can be written in @a target
370 * can be 0 if the size is unknown or is not fixed
371 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
374 (*GNUNET_BIO_ReadHandler)(void *cls,
375 struct GNUNET_BIO_ReadHandle *h,
382 * Structure specifying a reading operation on an IO handle.
384 struct GNUNET_BIO_ReadSpec
387 * Function performing data deserialization.
389 GNUNET_BIO_ReadHandler rh;
392 * Closure for @e rh. Can be NULL.
397 * What is being read (for error message creation)
402 * Destination buffer. Can also be a pointer to a pointer, especially for
403 * dynamically allocated structures.
408 * Size of @e target. Can be 0 if unknown or not fixed.
415 * End of specifications marker.
417 #define GNUNET_BIO_read_spec_end() \
418 { NULL, NULL, NULL, NULL, 0 }
422 * Create the specification to read a certain amount of bytes.
424 * @param what describes what is being read (for error message creation)
425 * @param result the buffer to write the result to
426 * @param len the number of bytes to read
427 * @return the read spec
429 struct GNUNET_BIO_ReadSpec
430 GNUNET_BIO_read_spec_object (const char *what,
436 * Create the specification to read a 0-terminated string.
438 * @param what describes what is being read (for error message creation)
439 * @param result where to store the pointer to the (allocated) string
440 * (note that *result could be set to NULL as well)
441 * @param max_length maximum allowed length for the string
442 * @return the read spec
444 struct GNUNET_BIO_ReadSpec
445 GNUNET_BIO_read_spec_string (const char *what,
451 * Create the specification to read a metadata container.
453 * @param what describes what is being read (for error message creation)
454 * @param result the buffer to store a pointer to the (allocated) metadata
455 * @return the read spec
457 struct GNUNET_BIO_ReadSpec
458 GNUNET_BIO_read_spec_meta_data (const char *what,
459 struct GNUNET_CONTAINER_MetaData **result);
463 * Create the specification to read an (u)int32_t.
465 * @param what describes what is being read (for error message creation)
466 * @param i where to store the data
467 * @return the read spec
469 struct GNUNET_BIO_ReadSpec
470 GNUNET_BIO_read_spec_int32 (const char *what,
475 * Create the specification to read an (u)int64_t.
477 * @param what describes what is being read (for error message creation)
478 * @param i where to store the data
479 * @return the read spec
481 struct GNUNET_BIO_ReadSpec
482 GNUNET_BIO_read_spec_int64 (const char *what,
487 * Create the specification to read a float.
489 * @param what describes what is being read (for error message creation)
490 * @param f address of float to read
492 struct GNUNET_BIO_ReadSpec
493 GNUNET_BIO_read_spec_float(const char *what, float *f);
497 * Create the specification to read a double.
499 * @param what describes what is being read (for error message creation)
500 * @param f address of double to read
502 struct GNUNET_BIO_ReadSpec
503 GNUNET_BIO_read_spec_double(const char *what, double *f);
507 * Execute the read specifications in order.
509 * @param h the IO handle to read from
510 * @param rs array of read specs
511 * the last element must be #GNUNET_BIO_read_spec_end
512 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
515 GNUNET_BIO_read_spec_commit (struct GNUNET_BIO_ReadHandle *h,
516 struct GNUNET_BIO_ReadSpec *rs);
519 /******************************* WRITE SPEC API *****************************/
523 * Function used to serialize data from a buffer and write it to @a h.
525 * @param cls closure (can be NULL)
526 * @param h the IO handle to write to
527 * @param what what is being written (for error message creation)
528 * @param source the data to write
529 * @param source_size how many bytes should be written
530 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
533 (*GNUNET_BIO_WriteHandler) (void *cls,
534 struct GNUNET_BIO_WriteHandle *h,
541 * Structure specifying a writing operation on an IO handle.
543 struct GNUNET_BIO_WriteSpec
546 * Function performing data serialization.
548 GNUNET_BIO_WriteHandler wh;
551 * Closure for @e rh. Can be NULL.
556 * What is being read (for error message creation)
561 * Source buffer. The data in this buffer will be written to the handle.
566 * Size of @e source. If it's smaller than the real size of @e source, only
567 * this many bytes will be written.
574 * End of specifications marker.
576 #define GNUNET_BIO_write_spec_end() \
577 { NULL, NULL, NULL, NULL, 0 }
581 * Create the specification to read some bytes.
583 * @param what describes what is being written (for error message creation)
584 * @param source the data to write
585 * @param size how many bytes should be written
586 * @return the write spec
588 struct GNUNET_BIO_WriteSpec
589 GNUNET_BIO_write_spec_object (const char *what,
595 * Create the specification to write a 0-terminated string.
597 * @param what describes what is being read (for error message creation)
598 * @param s string to write (can be NULL)
599 * @return the read spec
601 struct GNUNET_BIO_WriteSpec
602 GNUNET_BIO_write_spec_string (const char *what,
607 * Create the specification to write a metadata container.
609 * @param what what is being written (for error message creation)
610 * @param m metadata to write
611 * @return the write spec
613 struct GNUNET_BIO_WriteSpec
614 GNUNET_BIO_write_spec_meta_data (const char *what,
615 const struct GNUNET_CONTAINER_MetaData *m);
619 * Create the specification to write an (u)int32_t.
621 * @param what describes what is being written (for error message creation)
622 * @param i pointer to a 32-bit integer
623 * @return the write spec
625 struct GNUNET_BIO_WriteSpec
626 GNUNET_BIO_write_spec_int32 (const char *what,
631 * Create the specification to write an (u)int64_t.
633 * @param what describes what is being written (for error message creation)
634 * @param i pointer to a 64-bit integer
635 * @return the write spec
637 struct GNUNET_BIO_WriteSpec
638 GNUNET_BIO_write_spec_int64 (const char *what,
643 * Create the specification to write a float.
645 * @param what describes what is being written (for error message creation)
646 * @param f pointer to a float
647 * @return the write spec
649 struct GNUNET_BIO_WriteSpec
650 GNUNET_BIO_write_spec_float(const char *what, float *f);
654 * Create the specification to write an double.
656 * @param what describes what is being written (for error message creation)
657 * @param f pointer to a double
658 * @return the write spec
660 struct GNUNET_BIO_WriteSpec
661 GNUNET_BIO_write_spec_double(const char *what, double *f);
665 * Execute the write specifications in order.
667 * @param h the IO handle to write to
668 * @param ws array of write specs
669 * the last element must be #GNUNET_BIO_write_spec_end
670 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
673 GNUNET_BIO_write_spec_commit (struct GNUNET_BIO_WriteHandle *h,
674 struct GNUNET_BIO_WriteSpec *ws);
677 #if 0 /* keep Emacsens' auto-indent happy */
684 /* ifndef GNUNET_BIO_LIB_H */
687 /** @} */ /* end of group bio */
689 /* end of gnunet_bio_lib.h */