changeset 372:911814a9d5ee

Fix in-data load address handling.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 08 Oct 2021 14:51:19 +0300
parents 22e8ee2df9ac
children 5554e9ec9146
files sidlib.c
diffstat 1 files changed, 14 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/sidlib.c	Fri Oct 08 14:04:00 2021 +0300
+++ b/sidlib.c	Fri Oct 08 14:51:19 2021 +0300
@@ -78,12 +78,10 @@
 }
 
 
-static int sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid,
-    th_md5state_t *state, const BOOL newSLDB)
+static int sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid, th_md5state_t *state)
 {
     int ret = THERR_OK;
     uint8_t *data = NULL;
-    BOOL first = TRUE;
     size_t read;
 
     if ((data = (uint8_t *) th_malloc(SIDLIB_BUFFER_SIZE)) == NULL)
@@ -100,33 +98,9 @@
         read = thfread(data, sizeof(uint8_t), SIDLIB_BUFFER_SIZE, ctx);
         psid->dataSize += read;
 
-        // If load address is 0 in header and we have the first block, grab it
-        if (first && psid->loadAddress == 0)
-        {
-            if (read < 4)
-            {
-                ret = th_io_error(ctx, THERR_FREAD,
-                    "Error reading song data, unexpectedly small file.");
-                goto exit;
-            }
-
-            // Grab the load address
-            psid->loadAddress = TH_LE16_TO_NATIVE(*(uint16_t *) data);
+        if (read > 0)
+            th_md5_append(state, data, read);
 
-            // .. do not include the load address to the hash if NEW SLDB format
-            if (newSLDB)
-                th_md5_append(state, &data[2], read - 2);
-            else
-                th_md5_append(state, data, read);
-
-            first = FALSE;
-        }
-        else
-        if (read > 0)
-        {
-            // Append data "as is"
-            th_md5_append(state, data, read);
-        }
     } while (read > 0 && !thfeof(ctx));
 
 exit:
@@ -209,6 +183,15 @@
 
     hdrEnd = thftell(ctx);
 
+    // If load address is 0, we need to get it
+    if (psid->loadAddress == 0 &&
+        !thfread_be16(ctx, &psid->loadAddress))
+    {
+        ret = th_io_error(ctx, ctx->status,
+            "Error reading SID data (load address).");
+        goto exit;
+    }
+
     // Initialize MD5-hash calculation
     th_md5_init(&state);
 
@@ -218,7 +201,7 @@
         // We just hash the whole file, so seek back to beginning ..
         thfseek(ctx, hdrStart, SEEK_SET);
 
-        if ((ret = sidlib_read_hash_data(ctx, psid, &state, FALSE)) != THERR_OK)
+        if ((ret = sidlib_read_hash_data(ctx, psid, &state)) != THERR_OK)
             goto exit;
 
         psid->dataSize -= hdrEnd - hdrStart;
@@ -227,7 +210,7 @@
     {
         // "Old" Songlengths.txt style MD5 hash calculation
         // We need to separately hash data etc.
-        if ((ret = sidlib_read_hash_data(ctx, psid, &state, TRUE)) != THERR_OK)
+        if ((ret = sidlib_read_hash_data(ctx, psid, &state)) != THERR_OK)
             goto exit;
 
         // Append header data to hash