From 32ed49e5c5f80880ec3f6896f290b1f78953a634 Mon Sep 17 00:00:00 2001
From: hendricks266 <hendricks266@1a8010ca-5511-0410-912e-c29ae57300e0>
Date: Fri, 28 Jul 2017 08:27:38 +0000
Subject: [PATCH] Slightly change readarrayfromfile so that attempting to read
 arrays of a size that does not evenly divide into the requested type rounds
 up instead of down and zero-pads the difference.

This should help hackish persuits such as reading map files through this command.

git-svn-id: https://svn.eduke32.com/eduke32@6394 1a8010ca-5511-0410-912e-c29ae57300e0
---
 source/duke3d/src/gameexec.cpp | 6 ++++--
 source/duke3d/src/gamevars.cpp | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/source/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp
index d451b9e88..e580a1c95 100644
--- a/source/duke3d/src/gameexec.cpp
+++ b/source/duke3d/src/gameexec.cpp
@@ -4398,6 +4398,7 @@ finish_qsprintf:
                 if (numElements > 0)
                 {
                     size_t const newBytes = Gv_GetArrayAllocSizeForCount(arrayNum, numElements);
+                    size_t const readBytes = min(newBytes, filelength);
                     size_t const oldBytes = Gv_GetArrayAllocSize(arrayNum);
 
                     intptr_t *& pValues = aGameArrays[arrayNum].pValues;
@@ -4419,7 +4420,7 @@ finish_qsprintf:
                     {
                         void * const pArray = Xcalloc(1, newBytes);
 
-                        kread(kFile, pArray, newBytes);
+                        kread(kFile, pArray, readBytes);
 
                         if (flags & GAMEARRAY_UNSIGNED)
                         {
@@ -4437,7 +4438,8 @@ finish_qsprintf:
                     }
 #endif
                     default:
-                        kread(kFile, pValues, newBytes);
+                        memset((char *)pValues + readBytes, 0, newBytes - readBytes);
+                        kread(kFile, pValues, readBytes);
                         break;
                     }
                 }
diff --git a/source/duke3d/src/gamevars.cpp b/source/duke3d/src/gamevars.cpp
index ee9642a58..eba571c1a 100644
--- a/source/duke3d/src/gamevars.cpp
+++ b/source/duke3d/src/gamevars.cpp
@@ -602,7 +602,7 @@ size_t __fastcall Gv_GetArrayCountFromFile(int const arrayIdx, size_t const file
 
     size_t const elementSize = Gv_GetArrayElementSize(arrayIdx);
     size_t const denominator = min(elementSize, sizeof(uint32_t));
-    return filelength / denominator;
+    return (filelength + denominator - 1) / denominator;
 }
 
 int __fastcall Gv_GetArrayValue(int const id, int index)