# HG changeset patch # User Matti Hamalainen # Date 1425548047 -7200 # Node ID ae17e0a004f474cf93606e942fee9c5321b6bd26 # Parent 9d32da6ba78566b9019e32cad5d488b233eb7fe7 Move jssXMLoadPatterns() closer to the pattern unpacking functions. diff -r 9d32da6ba785 -r ae17e0a004f4 minijss/jloadxm.c --- a/minijss/jloadxm.c Thu Mar 05 11:33:22 2015 +0200 +++ b/minijss/jloadxm.c Thu Mar 05 11:34:07 2015 +0200 @@ -234,6 +234,87 @@ } +static int jssXMLoadPatterns(DMResource *inFile, JSSModule *module, XMHeader *xmH) +{ + int index, result; + XMPattern xmP; + + for (index = 0; index < module->npatterns; index++) + { + // Get the pattern header + off_t remainder, pos = dmftell(inFile); + + if (!dmf_read_le32(inFile, &xmP.headSize) || + !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); + + + // Check the header + if (xmP.packing != 0) + JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, + "Pattern #%d packing type unsupported (%d)\n", + index, xmP.packing); + + if (xmP.nrows == 0) + JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, + "Pattern #%d has %d rows, invalid data.\n", + index, xmP.nrows); + + if (xmP.size > 0) + { + // Allocate and unpack pattern + module->patterns[index] = jssAllocatePattern(xmP.nrows, module->nchannels); + if (module->patterns[index] == NULL) + JSSERROR(DMERR_MALLOC, DMERR_MALLOC, + "Could not allocate memory for pattern #%d\n", index); + + switch (module->intVersion) + { + case 0x0104: + result = jssXMUnpackPattern104(inFile, xmP.size, module->patterns[index]); + break; + case 0x0102: + result = jssXMUnpackPattern102(inFile, xmP.size, module->patterns[index]); + break; + } + + if (result != 0) + JSSERROR(result, result, "Error in unpacking pattern #%d data\n", index); + } + + // Skip extra data (if the file is damaged) + remainder = xmP.headSize - (dmftell(inFile) - pos); + if (remainder > 0) + { + JSSDEBUG("xmP Skipping: %li\n", remainder); + dmfseek(inFile, remainder, SEEK_CUR); + } + } + + // Allocate the empty pattern + module->patterns[jsetMaxPatterns] = jssAllocatePattern(64, module->nchannels); + + /* Convert song orders list by replacing nonexisting patterns + * with pattern number jsetMaxPatterns. + */ + for (index = 0; index < module->norders; index++) + { + int tmp = xmH->orderList[index]; + if (tmp >= module->npatterns || + module->patterns[tmp] == NULL) + tmp = jsetMaxPatterns; + + module->orderList[index] = tmp; + } + + return DMERR_OK; +} + + /* Convert XM envelope structure to JSS envelope structure */ static int jssXMConvertEnvelope( @@ -673,87 +754,6 @@ } -static int jssXMLoadPatterns(DMResource *inFile, JSSModule *module, XMHeader *xmH) -{ - int index, result; - XMPattern xmP; - - for (index = 0; index < module->npatterns; index++) - { - // Get the pattern header - off_t remainder, pos = dmftell(inFile); - - if (!dmf_read_le32(inFile, &xmP.headSize) || - !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); - - - // Check the header - if (xmP.packing != 0) - JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, - "Pattern #%d packing type unsupported (%d)\n", - index, xmP.packing); - - if (xmP.nrows == 0) - JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, - "Pattern #%d has %d rows, invalid data.\n", - index, xmP.nrows); - - if (xmP.size > 0) - { - // Allocate and unpack pattern - module->patterns[index] = jssAllocatePattern(xmP.nrows, module->nchannels); - if (module->patterns[index] == NULL) - JSSERROR(DMERR_MALLOC, DMERR_MALLOC, - "Could not allocate memory for pattern #%d\n", index); - - switch (module->intVersion) - { - case 0x0104: - result = jssXMUnpackPattern104(inFile, xmP.size, module->patterns[index]); - break; - case 0x0102: - result = jssXMUnpackPattern102(inFile, xmP.size, module->patterns[index]); - break; - } - - if (result != 0) - JSSERROR(result, result, "Error in unpacking pattern #%d data\n", index); - } - - // Skip extra data (if the file is damaged) - remainder = xmP.headSize - (dmftell(inFile) - pos); - if (remainder > 0) - { - JSSDEBUG("xmP Skipping: %li\n", remainder); - dmfseek(inFile, remainder, SEEK_CUR); - } - } - - // Allocate the empty pattern - module->patterns[jsetMaxPatterns] = jssAllocatePattern(64, module->nchannels); - - /* Convert song orders list by replacing nonexisting patterns - * with pattern number jsetMaxPatterns. - */ - for (index = 0; index < module->norders; index++) - { - int tmp = xmH->orderList[index]; - if (tmp >= module->npatterns || - module->patterns[tmp] == NULL) - tmp = jsetMaxPatterns; - - module->orderList[index] = tmp; - } - - return DMERR_OK; -} - - static int jssXMLoadInstruments(DMResource *inFile, JSSModule *module) { int index;