X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fbio.c;h=522c28cf811ed2e908daf9c916b08bf4f4045eba;hb=4707789ebfb4cef9672db31e3ceb8f98381901d0;hp=b4f33f5894b0b78e644c761b804b979a5c5b6f25;hpb=e5ba359af49fac05185f5ec0b4dbb47c7060167a;p=oweals%2Fgnunet.git diff --git a/src/util/bio.c b/src/util/bio.c index b4f33f589..522c28cf8 100644 --- a/src/util/bio.c +++ b/src/util/bio.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2006, 2009 Christian Grothoff (and other contributing authors) + Copyright (C) 2006, 2009, 2013 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ /** * @file util/bio.c @@ -28,20 +28,51 @@ #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) +/** + * Size for I/O buffers. + */ #define BIO_BUFFER_SIZE 65536 +/** + * Maximum size allowed for meta data written/read from disk. + * File-sharing limits to 64k, so this should be rather generous. + */ #define MAX_META_DATA (1024 * 1024) + /** * Handle for buffered reading. */ struct GNUNET_BIO_ReadHandle { + /** + * Underlying file abstraction. + */ struct GNUNET_DISK_FileHandle *fd; + + /** + * Error message, NULL if there were no errors. + */ char *emsg; + + /** + * I/O buffer. Allocated at the end of the struct, do not free! + */ char *buffer; + + /** + * Number of bytes available in read @e buffer. + */ size_t have; + + /** + * Total size of @e buffer. + */ size_t size; + + /** + * Current read offset in @e buffer. + */ off_t pos; }; @@ -78,7 +109,8 @@ GNUNET_BIO_read_open (const char *fn) * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise */ int -GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) +GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, + char **emsg) { int err; @@ -103,7 +135,8 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg) * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure */ int -GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, +GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, + const char *what, void *result, size_t len) { char *dst = result; @@ -142,7 +175,7 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, if (0 == ret) { GNUNET_asprintf (&h->emsg, - _("Error reading `%s': %s"), + _("Error reading `%s': %s"), what, _("End of file")); return GNUNET_SYSERR; @@ -166,8 +199,11 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, const char *what, * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure */ int -GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line, - void *result, size_t len) +GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, + const char *file, + int line, + void *result, + size_t len) { char what[1024]; @@ -183,12 +219,14 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, const char *file, int line, * @param what describes what is being read (for error message creation) * @param result the buffer to store a pointer to the (allocated) string to * (note that *result could be set to NULL as well) - * @param maxLen maximum allowed length for the string + * @param max_length maximum allowed length for the string * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure */ int -GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, - char **result, size_t maxLen) +GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, + const char *what, + char **result, + size_t max_length) { char *buf; uint32_t big; @@ -204,10 +242,10 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, *result = NULL; return GNUNET_OK; } - if (big > maxLen) + if (big > max_length) { GNUNET_asprintf (&h->emsg, _("String `%s' longer than allowed (%u > %u)"), - what, big, maxLen); + what, big, max_length); return GNUNET_SYSERR; } buf = GNUNET_malloc (big); @@ -234,7 +272,8 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h, const char *what, * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure */ int -GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, const char *what, +GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, + const char *what, struct GNUNET_CONTAINER_MetaData **result) { uint32_t size; @@ -306,8 +345,10 @@ GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file, * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ int -GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file, - int line, int64_t * i) +GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, + const char *file, + int line, + int64_t *i) { int64_t big; @@ -323,9 +364,24 @@ GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h, const char *file, */ struct GNUNET_BIO_WriteHandle { + /** + * Underlying file handle. + */ struct GNUNET_DISK_FileHandle *fd; + + /** + * I/O buffer. Do not free, allocated at the end of the struct. + */ char *buffer; + + /** + * Number of bytes already in @e buffer. + */ size_t have; + + /** + * Total size of @e buffer. + */ size_t size; }; @@ -448,7 +504,7 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer, * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ int -GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, +GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s) { uint32_t slen; @@ -503,10 +559,11 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h, * * @param h hande to open file * @param i 32-bit integer to write - * @return GNUNET_OK on success, GNUNET_SYSERR on error + * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ int -GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i) +GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, + int32_t i) { int32_t big; @@ -520,10 +577,11 @@ GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i) * * @param h hande to open file * @param i 64-bit integer to write - * @return GNUNET_OK on success, GNUNET_SYSERR on error + * @return #GNUNET_OK on success, #GNUNET_SYSERR on error */ int -GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i) +GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, + int64_t i) { int64_t big;