authentication of ciphertexts (+ seed)
[oweals/gnunet.git] / src / fs / fs_directory.c
index de100c33990232014070b66ab4140d99c9b8beef..22419e107b890bfb7e37d07de40192711147e727 100644 (file)
@@ -4,7 +4,7 @@
 
      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 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -179,8 +179,11 @@ find_full_data (void *cls,
  * @param offset offset of data in the directory
  * @param dep function to call on each entry
  * @param dep_cls closure for dep
+ * @return GNUNET_OK if this could be a block in a directory,
+ *         GNUNET_NO if this could be part of a directory (but not 100% OK)
+ *         GNUNET_SYSERR if 'data' does not represent a directory
  */
-void 
+int 
 GNUNET_FS_directory_list_contents (size_t size,
                                   const void *data,
                                   uint64_t offset,
@@ -198,10 +201,16 @@ GNUNET_FS_directory_list_contents (size_t size,
   struct GNUNET_CONTAINER_MetaData *md;
   char *filename;
 
+  if ( (offset == 0) &&
+       ( (size < 8 + sizeof (uint32_t)) ||
+        (0 != memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8)) ) )
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("MAGIC mismatch.  This is not a GNUnet directory.\n"));
+      return GNUNET_SYSERR;
+    }
   pos = offset;
-  if ( (pos == 0) && 
-       (size >= 8 + sizeof (uint32_t)) &&
-       (0 == memcmp (cdata, GNUNET_FS_DIRECTORY_MAGIC, 8)) )
+  if (offset == 0) 
     {
       memcpy (&mdSize, &cdata[8], sizeof (uint32_t));
       mdSize = ntohl (mdSize);
@@ -209,16 +218,16 @@ GNUNET_FS_directory_list_contents (size_t size,
        {
          /* invalid size */
          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                     _("Not a GNUnet directory.\n"));
-         return;
+                     _("MAGIC mismatch.  This is not a GNUnet directory.\n"));
+         return GNUNET_SYSERR;
        }
       md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[8 +
                                                         sizeof (uint32_t)],
                                                   mdSize);
       if (md == NULL)
         {
-          GNUNET_break (0);
-          return; /* malformed ! */
+         GNUNET_break (0);
+          return GNUNET_SYSERR; /* malformed ! */
         }
       dep (dep_cls,
           NULL,
@@ -254,7 +263,7 @@ GNUNET_FS_directory_list_contents (size_t size,
       while ((epos < size) && (cdata[epos] != '\0'))
         epos++;
       if (epos >= size)
-        return;   /* malformed - or partial download */
+        return GNUNET_NO;   /* malformed - or partial download */
       
       uri = GNUNET_FS_uri_parse (&cdata[pos], &emsg);
       pos = epos + 1;
@@ -268,7 +277,7 @@ GNUNET_FS_directory_list_contents (size_t size,
         {
           GNUNET_FS_uri_destroy (uri);
           GNUNET_break (0);
-          return; /* illegal in directory! */
+          return GNUNET_NO; /* illegal in directory! */
         }
 
       memcpy (&mdSize, &cdata[pos], sizeof (uint32_t));
@@ -277,7 +286,7 @@ GNUNET_FS_directory_list_contents (size_t size,
       if (pos + mdSize > size)
         {
           GNUNET_FS_uri_destroy (uri);
-          return; /* malformed - or partial download */
+          return GNUNET_NO; /* malformed - or partial download */
         }
 
       md = GNUNET_CONTAINER_meta_data_deserialize (&cdata[pos], mdSize);
@@ -285,7 +294,7 @@ GNUNET_FS_directory_list_contents (size_t size,
         {
           GNUNET_FS_uri_destroy (uri);
           GNUNET_break (0);
-          return; /* malformed ! */
+          return GNUNET_NO; /* malformed ! */
         }
       pos += mdSize;
       filename = GNUNET_CONTAINER_meta_data_get_by_type (md,
@@ -309,6 +318,7 @@ GNUNET_FS_directory_list_contents (size_t size,
       GNUNET_CONTAINER_meta_data_destroy (md);
       GNUNET_FS_uri_destroy (uri);
     }
+  return GNUNET_OK;
 }
 
 /**