-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / util / bio.c
index 0a1c213f34f8649b519dd507ead5a5ae96c14835..a31e5448f383cbd6cdd6b83769752c48a92a1fe2 100644 (file)
@@ -1,21 +1,19 @@
 /*
      This file is part of GNUnet.
-     (C) 2006, 2009, 2013 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
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 /**
  * @file util/bio.c
  * @author Christian Grothoff
  */
 #include "platform.h"
-#include "gnunet_bio_lib.h"
-#include "gnunet_disk_lib.h"
+#include "gnunet_util_lib.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "util-bio",__VA_ARGS__)
+
+#ifndef PATH_MAX
+/**
+ * Assumed maximum path length (for source file names).
+ */
+#define PATH_MAX 4096
+#endif
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
 
 /**
  * Size for I/O buffers.
- */ 
+ */
 #define BIO_BUFFER_SIZE 65536
 
 /**
@@ -135,7 +140,7 @@ GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
 int
-GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h, 
+GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
                  const char *what,
                  void *result, size_t len)
 {
@@ -155,15 +160,19 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
     {
       if (min > len - pos)
         min = len - pos;
-      memcpy (&dst[pos], &h->buffer[h->pos], min);
+      GNUNET_memcpy (&dst[pos],
+                    &h->buffer[h->pos],
+                    min);
       h->pos += min;
       pos += min;
     }
     if (pos == len)
       return GNUNET_OK;         /* done! */
-    GNUNET_assert (h->have == h->pos);
+    GNUNET_assert (((off_t) h->have) == h->pos);
     /* fill buffer */
-    ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
+    ret = GNUNET_DISK_file_read (h->fd,
+                                h->buffer,
+                                h->size);
     if (-1 == ret)
     {
       GNUNET_asprintf (&h->emsg,
@@ -175,7 +184,7 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
     if (0 == ret)
     {
       GNUNET_asprintf (&h->emsg,
-                      _("Error reading `%s': %s"), 
+                      _("Error reading `%s': %s"),
                       what,
                        _("End of file"));
       return GNUNET_SYSERR;
@@ -199,13 +208,13 @@ GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
 int
-GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h, 
-                    const char *file, 
+GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
+                    const char *file,
                     int line,
-                    void *result, 
+                    void *result,
                     size_t len)
 {
-  char what[1024];
+  char what[PATH_MAX + 1024];
 
   GNUNET_snprintf (what, sizeof (what), "%s:%d", file, line);
   return GNUNET_BIO_read (h, what, result, len);
@@ -225,7 +234,7 @@ GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
 int
 GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
                         const char *what,
-                        char **result, 
+                        char **result,
                         size_t max_length)
 {
   char *buf;
@@ -272,7 +281,7 @@ GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
 int
-GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h, 
+GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
                            const char *what,
                            struct GNUNET_CONTAINER_MetaData **result)
 {
@@ -280,7 +289,9 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
   char *buf;
   struct GNUNET_CONTAINER_MetaData *meta;
 
-  if (GNUNET_BIO_read_int32 (h, (int32_t *) & size) != GNUNET_OK)
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_int32 (h,
+                            (int32_t *) & size))
     return GNUNET_SYSERR;
   if (size == 0)
   {
@@ -291,20 +302,29 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
   {
     GNUNET_asprintf (&h->emsg,
                      _("Serialized metadata `%s' larger than allowed (%u>%u)"),
-                     what, size, MAX_META_DATA);
+                     what,
+                    size,
+                    MAX_META_DATA);
     return GNUNET_SYSERR;
   }
   buf = GNUNET_malloc (size);
-  if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
+  if (GNUNET_OK !=
+      GNUNET_BIO_read (h,
+                      what,
+                      buf,
+                      size))
   {
     GNUNET_free (buf);
     return GNUNET_SYSERR;
   }
-  meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
-  if (meta == NULL)
+  meta = GNUNET_CONTAINER_meta_data_deserialize (buf,
+                                                size);
+  if (NULL == meta)
   {
     GNUNET_free (buf);
-    GNUNET_asprintf (&h->emsg, _("Metadata `%s' failed to deserialize"), what);
+    GNUNET_asprintf (&h->emsg,
+                    _("Metadata `%s' failed to deserialize"),
+                    what);
     return GNUNET_SYSERR;
   }
   GNUNET_free (buf);
@@ -323,12 +343,19 @@ GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h, const char *file,
-                         int line, int32_t * i)
+GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
+                        const char *file,
+                         int line,
+                        int32_t * i)
 {
   int32_t big;
 
-  if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int32_t)))
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_fn (h,
+                         file,
+                         line,
+                         &big,
+                         sizeof (int32_t)))
     return GNUNET_SYSERR;
   *i = ntohl (big);
   return GNUNET_OK;
@@ -345,14 +372,19 @@ 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, 
+GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
                          const char *file,
-                         int line, 
+                         int line,
                          int64_t *i)
 {
   int64_t big;
 
-  if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof (int64_t)))
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_fn (h,
+                         file,
+                         line,
+                         &big,
+                         sizeof (int64_t)))
     return GNUNET_SYSERR;
   *i = GNUNET_ntohll (big);
   return GNUNET_OK;
@@ -368,7 +400,7 @@ 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.
    */
@@ -425,7 +457,8 @@ GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
   int ret;
 
   ret = GNUNET_SYSERR;
-  if ( (NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
+  if ( (NULL != h->fd) &&
+       (GNUNET_OK == (ret = GNUNET_BIO_flush (h))) )
     GNUNET_DISK_file_close (h->fd);
   GNUNET_free (h);
   return ret;
@@ -444,8 +477,10 @@ GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
 {
   ssize_t ret;
 
-  ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
-  if (ret != h->have)
+  ret = GNUNET_DISK_file_write (h->fd,
+                               h->buffer,
+                               h->have);
+  if (ret != (ssize_t) h->have)
   {
     GNUNET_DISK_file_close (h->fd);
     h->fd = NULL;
@@ -465,7 +500,8 @@ GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
+GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
+                 const void *buffer,
                   size_t n)
 {
   const char *src = buffer;
@@ -481,7 +517,9 @@ GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h, const void *buffer,
     min = h->size - h->have;
     if (min > n - pos)
       min = n - pos;
-    memcpy (&h->buffer[h->have], &src[pos], min);
+    GNUNET_memcpy (&h->buffer[h->have],
+                  &src[pos],
+                  min);
     pos += min;
     h->have += min;
     if (pos == n)
@@ -504,7 +542,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;
@@ -562,7 +600,7 @@ GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, 
+GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
                         int32_t i)
 {
   int32_t big;
@@ -580,7 +618,7 @@ GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, 
+GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h,
                         int64_t i)
 {
   int64_t big;