changeset 1188:d18c50f88d44

Clean up the pattern header parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 05 Mar 2015 12:48:58 +0200
parents f195dd7bfa48
children bd2615822bbf
files minijss/jloadxm.c
diffstat 1 files changed, 24 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/minijss/jloadxm.c	Thu Mar 05 12:14:43 2015 +0200
+++ b/minijss/jloadxm.c	Thu Mar 05 12:48:58 2015 +0200
@@ -243,45 +243,44 @@
     {
         // Get the pattern header
         off_t remainder, pos = dmftell(inFile);
+        Uint32 headSize;
 
-        if (!dmf_read_le32(inFile, &xmP.headSize))
+        // Get the pattern header size
+        if (!dmf_read_le32(inFile, &xmP.headSize) ||
+            !dmf_read_byte(inFile, &xmP.packing))
             JSSERROR(DMERR_FREAD, DMERR_FREAD,
             "Could not read pattern header size #%d.\n",
             index);
 
+        // Different format versions have slightly different headers
         if (module->intVersion == 0x0102)
         {
             Uint8 tmp;
-            if (xmP.headSize != 4 + 1 + 1 + 2)
-                JSSERROR(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED,
-                "Invalid pattern #%d header size %d, expected %d bytes.\n",
-                index, xmP.headSize, 4 + 1 + 1 + 2);
-
-            if (!dmf_read_byte(inFile, &xmP.packing) ||
-                !dmf_read_byte(inFile, &tmp) ||
-                !dmf_read_le16(inFile, &xmP.size))
-                JSSERROR(DMERR_FREAD, DMERR_FREAD,
-                "Could not read pattern header data #%d.\n",
-                index);
-
+            ret = dmf_read_byte(inFile, &tmp);
             xmP.nrows = ((Uint16) tmp) + 1;
+            headSize = 4 + 1 + 1 + 2;
         }
         else
         {
-            if (xmP.headSize != 4 + 1 + 2 + 2)
-                JSSERROR(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED,
-                "Invalid pattern #%d header size %d, expected %d bytes.\n",
-                index, xmP.headSize, 4 + 1 + 2 + 2);
-
-            if (!dmf_read_byte(inFile, &xmP.packing) ||
-                !dmf_read_le16(inFile, &xmP.nrows) ||
-                !dmf_read_le16(inFile, &xmP.size))
-                JSSERROR(DMERR_FREAD, DMERR_FREAD,
-                "Could not read pattern header data #%d.\n",
-                index);
+            ret = dmf_read_le16(inFile, &xmP.nrows);
+            headSize = 4 + 1 + 2 + 2;
         }
 
-        // Check the header
+        if (headSize != xmP.headSize)
+        {
+            JSSERROR(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED,
+            "Invalid pattern #%d header size %d, expected %d bytes.\n",
+            index, xmP.headSize, headSize);
+        }
+
+        if (!ret || !dmf_read_le16(inFile, &xmP.size))
+        {
+            JSSERROR(DMERR_FREAD, DMERR_FREAD,
+            "Could not read pattern header data #%d.\n",
+            index);
+        }
+
+        // Check rest of the header data
         if (xmP.packing != 0)
             JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
             "Pattern #%d packing type unsupported (%d)\n",