# HG changeset patch # User Matti Hamalainen # Date 1425552538 -7200 # Node ID d18c50f88d44e4780aa8d579484420dddc21fd9b # Parent f195dd7bfa4866c154a7161e56a94b8c7c9f4473 Clean up the pattern header parsing. diff -r f195dd7bfa48 -r d18c50f88d44 minijss/jloadxm.c --- 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",