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 */
46 * Handle for buffered reading.
48 struct GNUNET_BIO_ReadHandle;
52 * Open a file for reading.
54 * @param fn file name to be opened
55 * @return IO handle on success, NULL on error
57 struct GNUNET_BIO_ReadHandle *
58 GNUNET_BIO_read_open (const char *fn);
62 * Close an open file. Reports if any errors reading
63 * from the file were encountered.
65 * @param h file handle
66 * @param emsg set to the error message
67 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
70 GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg);
74 * Read the contents of a binary file into a buffer.
76 * @param h handle to an open file
77 * @param what describes what is being read (for error message creation)
78 * @param result the buffer to write the result to
79 * @param len the number of bytes to read
80 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
83 GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what,
84 void *result, size_t len);
88 * Read the contents of a binary file into a buffer.
90 * @param h handle to an open file
91 * @param file name of the source file
92 * @param line line number in the source file
93 * @param result the buffer to write the result to
94 * @param len the number of bytes to read
95 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
98 GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
99 const char *file, int line,
100 void *result, size_t len);
103 * Read 0-terminated string from a file.
105 * @param h handle to an open file
106 * @param what describes what is being read (for error message creation)
107 * @param result the buffer to store a pointer to the (allocated) string to
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, const char *what,
114 char **result, size_t max_length);
118 * Read metadata container from a file.
120 * @param h handle to an open file
121 * @param what describes what is being read (for error message creation)
122 * @param result the buffer to store a pointer to the (allocated) metadata
123 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
126 GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what,
127 struct GNUNET_CONTAINER_MetaData **result);
133 * @param h hande to open file
134 * @param f address of float to read
136 #define GNUNET_BIO_read_float(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, \
143 * @param h hande to open file
144 * @param f address of double to read
146 #define GNUNET_BIO_read_double(h, f) (GNUNET_BIO_read_fn (h, __FILE__, __LINE__, \
151 * Read an (u)int32_t.
153 * @param h hande to open file
154 * @param file name of the source file
155 * @param line line number in the code
156 * @param i address of 32-bit integer to read
157 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
160 GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
161 int line, int32_t *i);
165 * Read an (u)int32_t.
167 * @param h hande to open file
168 * @param i address of 32-bit integer to read
170 #define GNUNET_BIO_read_int32(h, i) GNUNET_BIO_read_int32__ (h, __FILE__, \
176 * Read an (u)int64_t.
178 * @param h hande to open file
179 * @param file name of the source file
180 * @param line line number in the code
181 * @param i address of 64-bit integer to read
182 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
185 GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
186 int line, int64_t *i);
190 * Read an (u)int64_t.
192 * @param h hande to open file
193 * @param i address of 64-bit integer to read
195 #define GNUNET_BIO_read_int64(h, i) GNUNET_BIO_read_int64__ (h, __FILE__, \
201 * Handle for buffered writing.
203 struct GNUNET_BIO_WriteHandle;
206 * Open a file for writing.
208 * @param fn file name to be opened
209 * @return IO handle on success, NULL on error
211 struct GNUNET_BIO_WriteHandle *
212 GNUNET_BIO_write_open (const char *fn);
216 * Close an open file for writing.
218 * @param h file handle
219 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
222 GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h);
226 * Write a buffer to a file.
228 * @param h handle to open file
229 * @param buffer the data to write
230 * @param n number of bytes to write
231 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
234 GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
239 * Force a buffered writer to flush its buffer
241 * @param h the writer handle
242 * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
246 GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h);
250 * Write a string to a file.
252 * @param h handle to open file
253 * @param s string to write (can be NULL)
254 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
257 GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s);
261 * Write metadata container to a file.
263 * @param h handle to open file
264 * @param m metadata to write
265 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
268 GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
269 const struct GNUNET_CONTAINER_MetaData *m);
275 * @param h hande to open file
276 * @param f float to write (must be a variable)
278 #define GNUNET_BIO_write_float(h, f) GNUNET_BIO_write (h, &f, sizeof(float))
284 * @param h hande to open file
285 * @param f double to write (must be a variable)
287 #define GNUNET_BIO_write_double(h, f) GNUNET_BIO_write (h, &f, sizeof(double))
291 * Write an (u)int32_t.
293 * @param h hande to open file
294 * @param i 32-bit integer to write
295 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
298 GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i);
302 * Write an (u)int64_t.
304 * @param h hande to open file
305 * @param i 64-bit integer to write
306 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
309 GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i);
312 #if 0 /* keep Emacsens' auto-indent happy */
319 /* ifndef GNUNET_BIO_LIB_H */
322 /** @} */ /* end of group bio */
324 /* end of gnunet_bio_lib.h */