changeset 1916:34ba8e2d2dd7

Split dmC64GetOpSize() to dmC64GetSubjectSize() and dmC64GetOpSubjectSize().
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 27 Jun 2018 18:36:03 +0300
parents 788cfc7096f3
children ade671278806
files tools/gfxconv.c tools/lib64gfx.c tools/lib64gfx.h
diffstat 3 files changed, 22 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Wed Jun 27 15:43:20 2018 +0300
+++ b/tools/gfxconv.c	Wed Jun 27 18:36:03 2018 +0300
@@ -1162,7 +1162,7 @@
         if (op->type == DO_LAST)
             break;
 
-        dmC64GetOpSize(op, dstFmt, &size);
+        size = dmC64GetOpSubjectSize(op, dstFmt);
         switch (op->type)
         {
             case DO_COPY:
--- a/tools/lib64gfx.c	Wed Jun 27 15:43:20 2018 +0300
+++ b/tools/lib64gfx.c	Wed Jun 27 18:36:03 2018 +0300
@@ -768,21 +768,21 @@
 }
 
 
-BOOL dmC64GetOpSize(const DMC64EncDecOp *op, const DMC64ImageFormat *fmt, size_t *size)
+size_t dmC64GetSubjectSize(const int subject, const DMC64ImageFormat *fmt)
 {
-    switch (op->subject)
+    switch (subject)
     {
         case DS_SCREEN_RAM:
         case DS_COLOR_RAM:
-            *size = fmt->format->chHeight * fmt->format->chWidth;
+            return fmt->format->chHeight * fmt->format->chWidth;
             break;
 
         case DS_BITMAP_RAM:
-            *size = fmt->format->chHeight * fmt->format->chWidth * 8;
+            return fmt->format->chHeight * fmt->format->chWidth * 8;
             break;
 
         case DS_CHAR_DATA:
-            *size = C64_MAX_CHARS * C64_CHR_SIZE;
+            return C64_MAX_CHARS * C64_CHR_SIZE;
             break;
 
         case DS_D020:
@@ -791,20 +791,26 @@
         case DS_D022:
         case DS_D023:
         case DS_D024:
-            *size = 1;
+            return 1;
             break;
 
         default:
             // Default to size of 0
-            *size = 0;
+            return 0;
             break;
     }
+}
+
+
+size_t dmC64GetOpSubjectSize(const DMC64EncDecOp *op, const DMC64ImageFormat *fmt)
+{
+    size_t size = dmC64GetSubjectSize(op->subject, fmt);
 
     // If the operator specified size is larger, use it.
-    if (op->size > *size)
-        *size = op->size;
+    if (op->size > size)
+        size = op->size;
 
-    return TRUE;
+    return size;
 }
 
 
@@ -882,17 +888,8 @@
         if ((res = dmC64SanityCheckEncDecOp(i, op, img)) != DMERR_OK)
             return res;
 
-        // Check size
-        if (!dmC64GetOpSize(op, fmt, &size))
-        {
-            return dmError(DMERR_INVALID_DATA,
-                "Decode op SIZE out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
-                "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
-                i, op->type, subjname, op->offs, op->offs, op->bank,
-                size, size, op->size, op->size);
-        }
-
         // Is the operation inside the bounds?
+        size = dmC64GetOpSubjectSize(op, fmt);
         if (op->type == DO_COPY && op->offs + size > buf->len + 1)
         {
             return dmError(DMERR_INVALID_DATA,
@@ -1088,17 +1085,8 @@
         if ((res = dmC64SanityCheckEncDecOp(i, op, img)) != DMERR_OK)
             goto err;
 
-        // Check size
-        if (!dmC64GetOpSize(op, fmt, &size))
-        {
-            res = dmError(DMERR_INVALID_DATA,
-                "Encode op SIZE out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
-                "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
-                subjname, i, op->type, subjname, op->offs, op->offs, op->bank, size, size, buf->size, buf->size);
-            goto err;
-        }
-
         // Do we need to reallocate some more space?
+        size = dmC64GetOpSubjectSize(op, fmt);
         chksize = buf->offs + op->offs + size;
         if (!dmGrowBufCheckGrow(buf, chksize))
         {
--- a/tools/lib64gfx.h	Wed Jun 27 15:43:20 2018 +0300
+++ b/tools/lib64gfx.h	Wed Jun 27 18:36:03 2018 +0300
@@ -320,7 +320,9 @@
 const char *dmC64GetOpSubjectName(const int subject);
 void      dmC64GetOpMemBlock(const DMC64Image *img, const int subject, const int bank, const DMC64MemBlock **blk);
 int       dmC64SanityCheckEncDecOp(const int i, const DMC64EncDecOp *op, const DMC64Image *img);
-BOOL      dmC64GetOpSize(const DMC64EncDecOp *op, const DMC64ImageFormat *fmt, size_t *size);
+
+size_t    dmC64GetSubjectSize(const int subject, const DMC64ImageFormat *fmt);
+size_t    dmC64GetOpSubjectSize(const DMC64EncDecOp *op, const DMC64ImageFormat *fmt);
 
 
 int       dmC64DecodeGenericBMP(DMC64Image *img, const DMGrowBuf *buf, const DMC64ImageFormat *fmt);