shithub: pt2-clone

Download patch

ref: 6155437096ad7fd125b8cde49c1c344d7bb6f312
parent: 47752ff46560097dfeeb04cafe91b350312074ee
author: Olav Sørensen <olav.sorensen@live.no>
date: Fri Jul 3 06:39:20 EDT 2020

Fix >64kB misalignment in .MOD loader

If samples longer than 64kB were present in a .MOD during load, they would mess up the alignment of the next samples being loaded.

--- a/src/pt2_header.h
+++ b/src/pt2_header.h
@@ -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 '\\'
--- a/src/pt2_module_loader.c
+++ b/src/pt2_module_loader.c
@@ -37,6 +37,7 @@
 
 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);
@@ -310,7 +311,8 @@
 			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.
@@ -601,8 +603,6 @@
 	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.
 		*/
@@ -614,13 +614,11 @@
 		}
 
 		/* 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;
@@ -631,7 +629,6 @@
 		}
 
 		mread(&newMod->sampleData[s->offset], 1, s->length, m);
-
 		if (bytesToSkip > 0)
 			mseek(m, bytesToSkip, SEEK_CUR);
 
--- a/src/pt2_sample_loader.c
+++ b/src/pt2_sample_loader.c
@@ -31,12 +31,12 @@
 	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)
 {