Skip to content

Commit

Permalink
Fix >64kB misalignment in .MOD loader
Browse files Browse the repository at this point in the history
If samples longer than 64kB were present in a .MOD during load, they would mess up the alignment of the next samples being loaded.
  • Loading branch information
8bitbubsy committed Jul 3, 2020
1 parent 47752ff commit 6155437
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/pt2_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "pt2_unicode.h"
#include "pt2_palette.h"

#define PROG_VER_STR "1.20"
#define PROG_VER_STR "1.21"

#ifdef _WIN32
#define DIR_DELIMITER '\\'
Expand Down
17 changes: 7 additions & 10 deletions src/pt2_module_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct mem_t

static bool oldAutoPlay;
static char oldFullPath[(PATH_MAX * 2) + 2];
static int32_t realSampleLengths[MOD_SAMPLES];
static uint32_t oldFullPathLen;

static MEMFILE *mopen(const uint8_t *src, uint32_t length);
Expand Down Expand Up @@ -310,7 +311,8 @@ module_t *modLoad(UNICHAR *fileName)
fixZeroesInString(s->text, 22);
}

s->length = (uint16_t)(((mgetc(m) << 8) | mgetc(m)) * 2);
realSampleLengths[i] = ((mgetc(m) << 8) | mgetc(m)) * 2;
s->length = (realSampleLengths[i] > MAX_SAMPLE_LEN) ? MAX_SAMPLE_LEN : (uint16_t)realSampleLengths[i];

/* Only late versions of Ultimate SoundTracker could have samples larger than 9999 bytes.
** If found, we know for sure that this is a late STK module.
Expand Down Expand Up @@ -601,8 +603,6 @@ module_t *modLoad(UNICHAR *fileName)
s = newMod->samples;
for (i = 0; i < numSamples; i++, s++)
{
uint32_t bytesToSkip = 0;

/* For Ultimate SoundTracker modules, only the loop area of a looped sample is played.
** Skip loading of eventual data present before loop start.
*/
Expand All @@ -614,13 +614,11 @@ module_t *modLoad(UNICHAR *fileName)
}

/* We don't support loading samples bigger than 65534 bytes in our PT2 clone,
** so clamp length and skip overflown data.
** so skip overflown data in .MOD file if present.
*/
if (s->length > MAX_SAMPLE_LEN)
{
s->length = MAX_SAMPLE_LEN;
bytesToSkip = s->length - MAX_SAMPLE_LEN;
}
int32_t bytesToSkip = 0;
if (realSampleLengths[i] > MAX_SAMPLE_LEN)
bytesToSkip = realSampleLengths[i] - MAX_SAMPLE_LEN;

// For Ultimate SoundTracker modules, don't load sample data after loop end
uint16_t loopEnd = s->loopStart + s->loopLength;
Expand All @@ -631,7 +629,6 @@ module_t *modLoad(UNICHAR *fileName)
}

mread(&newMod->sampleData[s->offset], 1, s->length, m);

if (bytesToSkip > 0)
mseek(m, bytesToSkip, SEEK_CUR);

Expand Down
4 changes: 2 additions & 2 deletions src/pt2_sample_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ enum
WAV_FORMAT_IEEE_FLOAT = 0x0003
};

static bool loadedFileWasAIFF;

static bool loadWAVSample(UNICHAR *fileName, char *entryName, int8_t forceDownSampling);
static bool loadIFFSample(UNICHAR *fileName, char *entryName);
static bool loadRAWSample(UNICHAR *fileName, char *entryName);
static bool loadAIFFSample(UNICHAR *fileName, char *entryName, int8_t forceDownSampling);

static bool loadedFileWasAIFF;

static bool lowPassSample8Bit(int8_t *buffer, int32_t length, int32_t sampleFrequency, double cutoff)
{
rcFilter_t filter;
Expand Down

0 comments on commit 6155437

Please sign in to comment.