changeset 1033:c353e6bcb733

Change handling of filename field in PACKs.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 01 Mar 2015 16:39:46 +0200
parents f8d75a51de2c
children 2e9ba01294b3
files src/dmpack.c src/dmpack.h tools/packed.c
diffstat 3 files changed, 12 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/dmpack.c	Sun Mar 01 16:27:05 2015 +0200
+++ b/src/dmpack.c	Sun Mar 01 16:39:46 2015 +0200
@@ -134,7 +134,7 @@
             goto out;
         }
 
-        if (!dm_fread_str(pack->file, (Uint8 *) &entry->filename, sizeof(entry->filename)) ||
+        if (!dm_fread_str(pack->file, (Uint8 *) &entry->filename, DMRES_NAME_LEN) ||
             !dm_fread_le32(pack->file, &entry->size) ||
             !dm_fread_le32(pack->file, &entry->offset) ||
             !dm_fread_le32(pack->file, &entry->length) ||
@@ -144,10 +144,14 @@
             goto out;
         }
 
+        // Ensure that the filename ends in NUL
+        entry->filename[DMRES_NAME_LEN] = 0;
+
         // Validate
         if (entry->size == 0 || entry->length == 0 ||
             entry->length > hdr.dirOffset ||
-            entry->offset > hdr.dirOffset)
+            entry->offset > hdr.dirOffset ||
+            strlen(entry->filename) == 0)
         {
             ret = DMERR_INVALID;
             goto out;
--- a/src/dmpack.h	Sun Mar 01 16:27:05 2015 +0200
+++ b/src/dmpack.h	Sun Mar 01 16:39:46 2015 +0200
@@ -15,10 +15,10 @@
 
 typedef struct _DMPackEntry
 {
-    char    filename[DMRES_NAME_LEN];
     Uint32  size;              // Size (UNCOMPRESSED)
     Uint32  offset;            // Offset in pack file
     Uint32  length;            // (Compressed) data length
+    char    filename[DMRES_NAME_LEN + 1]; // +1 for NUL byte
 
     Uint32  flags, privFlags;
     
--- a/tools/packed.c	Sun Mar 01 16:27:05 2015 +0200
+++ b/tools/packed.c	Sun Mar 01 16:39:46 2015 +0200
@@ -171,8 +171,8 @@
     if (node == NULL)
         return NULL;
 
-    strncpy(node->filename, src->filename, sizeof(node->filename));
-    node->filename[sizeof(node->filename) - 1] = 0;
+    strncpy(node->filename, src->filename, DMRES_NAME_LEN);
+    node->filename[DMRES_NAME_LEN] = 0;
 
     node->size     = src->size;
     node->offset   = src->offset;
@@ -228,7 +228,7 @@
     while (node != NULL)
     {
         // Write one entry
-        if (!dm_fwrite_str(pack->file, node->filename, sizeof(node->filename)) ||
+        if (!dm_fwrite_str(pack->file, node->filename, DMRES_NAME_LEN) ||
             !dm_fwrite_le32(pack->file, node->size) ||
             !dm_fwrite_le32(pack->file, node->offset) ||
             !dm_fwrite_le32(pack->file, node->length) ||
@@ -362,8 +362,8 @@
     }
 
     // Create directory entry
-    strncpy(entry.filename, filename, sizeof(entry.filename));
-    entry.filename[sizeof(entry.filename) - 1] = 0;
+    strncpy(entry.filename, filename, DMRES_NAME_LEN)
+    entry.filename[DMRES_NAME_LEN] = 0;
     entry.offset = startOffs;
     entry.size   = zstr.total_in;
     entry.length = outSize;