# HG changeset patch # User Matti Hamalainen # Date 1633693879 -10800 # Node ID 911814a9d5ee3cb60325c63790ec744ce46bae26 # Parent 22e8ee2df9ac423ad759f34f3ca86f22d1299f69 Fix in-data load address handling. diff -r 22e8ee2df9ac -r 911814a9d5ee sidlib.c --- 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