# HG changeset patch # User Matti Hamalainen # Date 1369347551 -10800 # Node ID ed60a7ee3ebb7fc8513695cfaf2213a83c858af2 # Parent 9acebca96dccbb70faa116bf04be5b21c4430f90 Change JSSMOD loader to use DMResources. diff -r 9acebca96dcc -r ed60a7ee3ebb minijss/jloadjss.c --- a/minijss/jloadjss.c Thu May 23 23:48:24 2013 +0300 +++ b/minijss/jloadjss.c Fri May 24 01:19:11 2013 +0300 @@ -16,40 +16,12 @@ #endif -static BOOL jsGetBufData(Uint8 **buf, size_t *bufLeft, void *data, const size_t dataSize) -{ - if (*bufLeft >= dataSize) - { - memcpy(data, *buf, dataSize); - *buf += dataSize; - *bufLeft -= dataSize; - return TRUE; - } - else - return FALSE; -} - - -static BOOL jsGetBufByte(Uint8 **buf, size_t *bufLeft, Uint8 *data) -{ - if (*bufLeft > 0) - { - *data = **buf; - (*buf)++; - (*bufLeft)--; - return TRUE; - } - else - return FALSE; -} - - -#define JSGETBUF(XV, XT) if (!jsGetBufData(buf, bufLeft, XV, sizeof(XT))) return DMERR_OUT_OF_DATA -#define JSGETBYTE(XV) if (!jsGetBufByte(buf, bufLeft, XV)) return DMERR_OUT_OF_DATA +#define JSGETBUF(XV, XT) if (!dmf_read_str(inFile, XV, sizeof(XT))) return DMERR_OUT_OF_DATA +#define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA #if defined(JM_SUP_PATMODE_1) || defined(JM_SUP_PATMODE_3) -static int jssGetConvertedNote(Uint8 **buf, size_t *bufLeft, JSSNote *note) +static int jssGetConvertedNote(DMResource *inFile, JSSNote *note) { Uint8 tmp; @@ -80,7 +52,7 @@ #if defined(JM_SUP_PATMODE_2) || defined(JM_SUP_PATMODE_4) -static int jssGetCompressedNote(Uint8 **buf, size_t *bufLeft, JSSNote *note) +static int jssGetCompressedNote(DMResource *inFile, JSSNote *note) { Uint8 packb, tmp; @@ -151,7 +123,7 @@ #ifdef JM_SUP_PATMODE_2 -static int jssGetPatternCompHoriz(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternCompHoriz(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -162,7 +134,7 @@ for (channel = 0; channel < pattern->nchannels; channel++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetCompressedNote(&buf, bufLeft, note); + int res = jssGetCompressedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", row, channel); } @@ -173,7 +145,7 @@ #ifdef JM_SUP_PATMODE_4 -static int jssGetPatternCompVert(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternCompVert(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -184,7 +156,7 @@ for (row = 0; row < pattern->nrows; row++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetCompressedNote(&buf, bufLeft, note); + int res = jssGetCompressedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", row, channel); } @@ -195,7 +167,7 @@ #ifdef JM_SUP_PATMODE_1 -static int jssGetPatternRawHoriz(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternRawHoriz(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -206,7 +178,7 @@ for (channel = 0; channel < pattern->nchannels; channel++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetConvertedNote(&buf, bufLeft, note); + int res = jssGetConvertedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", row, channel); } @@ -217,7 +189,7 @@ #ifdef JM_SUP_PATMODE_3 -static int jssGetPatternRawVert(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternRawVert(DMResource *inFile, JSSPattern *pattern) { int row, channel; @@ -228,7 +200,7 @@ for (row = 0; row < pattern->nrows; row++) { JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; - int res = jssGetConvertedNote(&buf, bufLeft, note); + int res = jssGetConvertedNote(inFile, note); if (res != DMERR_OK) JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", row, channel); } @@ -241,7 +213,7 @@ #ifdef JM_SUP_PATMODE_5 #undef JSGETBYTE -#define JSGETBYTE(XV) if (!jsGetBufByte(&buf, bufLeft, XV)) return DMERR_OUT_OF_DATA +#define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA #define JSFOREACHNOTE1 \ for (channel = 0; channel < pattern->nchannels; channel++) \ @@ -250,7 +222,7 @@ #define JSFOREACHNOTE2 } -static int jssGetPatternRawVertElem(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) +static int jssGetPatternRawVertElem(DMResource *inFile, JSSPattern *pattern) { int row, channel; Uint8 tmp; @@ -296,11 +268,11 @@ #undef JSGETBUF #undef JSGETBYTE #define JSGETBUF(XV, XT) do { \ - if (!jsGetBufData(&buf, &bufLeft, XV, sizeof(XT))) \ + if (!dmf_read_str(inFile, XV, sizeof(XT))) \ JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, \ "Out of data at getting " # XT " (%d bytes)\n", sizeof(XT)); \ } while (0) -#define JSGETBYTE(XV) if (!jsGetBufByte(&buf, &bufLeft, XV)) return DMERR_OUT_OF_DATA +#define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA #ifdef JM_SUP_EXT_INSTR @@ -323,16 +295,12 @@ #endif -int jssLoadJSSMOD(Uint8 *bufStart, const size_t bufSize, JSSModule **ppModule) +int jssLoadJSSMOD(DMResource *inFile, JSSModule **ppModule) { JSSModule *module; JSSMODHeader jssH; - Uint8 *buf = bufStart; - size_t bufLeft = bufSize; int index; - assert(ppModule != NULL); - assert(bufStart != NULL); *ppModule = NULL; // Check the JSSMOD header @@ -386,17 +354,10 @@ { JSSMODPattern jssP; int result = DMERR_INVALID_DATA; - size_t bufSize; // Get header and check size memset(&jssP, 0, sizeof(jssP)); JSGETBUF(&jssP, JSSMODPattern); - bufSize = jssP.size; - if (bufLeft < jssP.size) - { - JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, - "Out of data for pattern #%d.\n", index); - } // Allocate pattern module->patterns[index] = jssAllocatePattern(jssP.nrows, module->nchannels); @@ -411,27 +372,27 @@ { #ifdef JM_SUP_PATMODE_1 case PATMODE_RAW_HORIZ: - result = jssGetPatternRawHoriz(buf, &bufSize, module->patterns[index]); + result = jssGetPatternRawHoriz(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_2 case PATMODE_COMP_HORIZ: - result = jssGetPatternCompHoriz(buf, &bufSize, module->patterns[index]); + result = jssGetPatternCompHoriz(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_3 case PATMODE_RAW_VERT: - result = jssGetPatternRawVert(buf, &bufSize, module->patterns[index]); + result = jssGetPatternRawVert(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_4 case PATMODE_COMP_VERT: - result = jssGetPatternCompVert(buf, &bufSize, module->patterns[index]); + result = jssGetPatternCompVert(inFile, module->patterns[index]); break; #endif #ifdef JM_SUP_PATMODE_5 case PATMODE_RAW_ELEM: - result = jssGetPatternRawVertElem(buf, &bufSize, module->patterns[index]); + result = jssGetPatternRawVertElem(inFile, module->patterns[index]); break; #endif default: @@ -440,19 +401,10 @@ break; } - if (bufSize > 0) - { - JSSWARNING(DMERR_EXTRA_DATA, DMERR_EXTRA_DATA, - "Unparsed data after pattern (%d bytes), possibly broken file.\n", bufSize); - } - if (result != DMERR_OK) { JSSERROR(result, result, "Error in unpacking pattern #%i data\n", index); } - - buf += jssP.size; - bufLeft -= jssP.size; } #ifdef JM_SUP_EXT_INSTR @@ -538,14 +490,6 @@ else sz = inst->size * sizeof(Uint8); - // Check if we can get as much? - if (bufLeft < sz) - { - JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, - "Out of data for instrument sample #%d (%d < %d)\n", - index, bufLeft, sz); - } - // Allocate if ((inst->data = dmMalloc(sz)) == NULL) { @@ -554,9 +498,11 @@ } // Copy data - memcpy(inst->data, buf, sz); - buf += sz; - bufLeft -= sz; + if (!dmf_read_str(inFile, inst->data, sz)) + { + JSSERROR(DMERR_FREAD, DMERR_FREAD, + "Could not read sample data for #%d\n", index); + } // Convert, if needed if (inst->flags & jsf16bit) diff -r 9acebca96dcc -r ed60a7ee3ebb minijss/jssmod.h --- a/minijss/jssmod.h Thu May 23 23:48:24 2013 +0300 +++ b/minijss/jssmod.h Fri May 24 01:19:11 2013 +0300 @@ -305,7 +305,7 @@ #endif #ifdef JSS_SUP_JSSMOD -int jssLoadJSSMOD(Uint8 *, const size_t, JSSModule **); +int jssLoadJSSMOD(DMResource *, JSSModule **); #endif #ifdef __cplusplus diff -r 9acebca96dcc -r ed60a7ee3ebb tests/plrtest.c --- a/tests/plrtest.c Thu May 23 23:48:24 2013 +0300 +++ b/tests/plrtest.c Fri May 24 01:19:11 2013 +0300 @@ -146,18 +146,9 @@ #ifdef JSS_SUP_JSSMOD if (result != 0) { - size_t bufgot, bufsize = dmfsize(file); - Uint8 *buf = dmMalloc(bufsize); dmfseek(file, 0L, SEEK_SET); - fprintf(stderr, "* Trying JSSMOD (%d bytes, %p)...\n", bufsize, buf); - if ((bufgot = dmfread(buf, 1, bufsize, file)) != bufsize) - { - fprintf(stderr, "Error reading file (not enough data %d), #%d: %s\n", - bufgot, dmferror(file), dmErrorStr(dmferror(file))); - return 2; - } - result = jssLoadJSSMOD(buf, bufsize, &mod); - dmFree(buf); + dmMsg(1, "* Trying JSSMOD ...\n"); + result = jssLoadJSSMOD(file, &mod); } #endif dmf_close(file); diff -r 9acebca96dcc -r ed60a7ee3ebb tools/mod2wav.c --- a/tools/mod2wav.c Thu May 23 23:48:24 2013 +0300 +++ b/tools/mod2wav.c Fri May 24 01:19:11 2013 +0300 @@ -169,18 +169,9 @@ #ifdef JSS_SUP_JSSMOD if (result != 0) { - size_t bufgot, bufsize = dmfsize(inFile); - Uint8 *buf = dmMalloc(bufsize); dmfseek(inFile, 0L, SEEK_SET); - fprintf(stderr, "* Trying JSSMOD (%d bytes, %p)...\n", bufsize, buf); - if ((bufgot = dmfread(buf, 1, bufsize, inFile)) != bufsize) - { - fprintf(stderr, "Error reading file (not enough data %d), #%d: %s\n", - bufgot, dmferror(inFile), dmErrorStr(dmferror(inFile))); - return 2; - } - result = jssLoadJSSMOD(buf, bufsize, &mod); - dmFree(buf); + dmMsg(1, "* Trying JSSMOD ...\n"); + result = jssLoadJSSMOD(inFile, &mod); } #endif dmf_close(inFile); diff -r 9acebca96dcc -r ed60a7ee3ebb tools/ppl.c --- a/tools/ppl.c Thu May 23 23:48:24 2013 +0300 +++ b/tools/ppl.c Fri May 24 01:19:11 2013 +0300 @@ -600,19 +600,9 @@ #ifdef JSS_SUP_JSSMOD if (result != 0) { - size_t bufgot, bufsize = dmfsize(file); - Uint8 *buf = dmMalloc(bufsize); dmfseek(file, 0L, SEEK_SET); - dmMsg(2, "* Trying JSSMOD (%d bytes, %p)...\n", bufsize, buf); - if ((bufgot = dmfread(buf, 1, bufsize, file)) != bufsize) - { - dmf_close(file); - dmError("Error reading file (not enough data %d), #%d: %s\n", - bufgot, dmferror(file), dmErrorStr(dmferror(file))); - goto error_exit; - } - result = jssLoadJSSMOD(buf, bufsize, &engine.mod); - dmFree(buf); + dmMsg(1, "* Trying JSSMOD ...\n"); + result = jssLoadJSSMOD(file, &engine.mod); } #endif dmf_close(file); diff -r 9acebca96dcc -r ed60a7ee3ebb tools/viewmod.c --- a/tools/viewmod.c Thu May 23 23:48:24 2013 +0300 +++ b/tools/viewmod.c Fri May 24 01:19:11 2013 +0300 @@ -371,8 +371,8 @@ int main(int argc, char *argv[]) { int result = -1, i; - DMResource *file; - JSSModule *mod; + DMResource *file = NULL; + JSSModule *mod = NULL; dmInitProg("viewmod", "miniJSS Module Viewer", "0.4", NULL, NULL); dmVerbosity = 0; @@ -408,18 +408,9 @@ #ifdef JSS_SUP_JSSMOD if (result != 0) { - size_t bufgot, bufsize = dmfsize(file); - Uint8 *buf = dmMalloc(bufsize); dmfseek(file, 0L, SEEK_SET); - dmMsg(1, "* Trying JSSMOD (%d bytes, %p)...\n", bufsize, buf); - if ((bufgot = dmfread(buf, 1, bufsize, file)) != bufsize) - { - dmError("Error reading file (not enough data %d), #%d: %s\n", - bufgot, dmferror(file), dmErrorStr(dmferror(file))); - return 2; - } - result = jssLoadJSSMOD(buf, bufsize, &mod); - dmFree(buf); + dmMsg(1, "* Trying JSSMOD ...\n"); + result = jssLoadJSSMOD(file, &mod); } #endif dmf_close(file);