Skip to content

Commit af00e21

Browse files
authored
Merge pull request #36 from sagamusix/master
Do not overwrite const file data provided by the user in PSM loader.
2 parents e9fc46e + 6e38479 commit af00e21

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

src/load_psm.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void swap_PSMSAMPLE(PSMSAMPLE* p){
9898
BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
9999
//-----------------------------------------------------------
100100
{
101-
PSMCHUNK *pfh = (PSMCHUNK *)lpStream;
101+
PSMCHUNK pfh = *(const PSMCHUNK *)lpStream;
102102
DWORD dwMemPos, dwSongPos;
103103
DWORD smpnames[MAX_SAMPLES];
104104
DWORD patptrs[MAX_PATTERNS];
@@ -108,17 +108,17 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
108108
if (dwMemLength < 256) return FALSE;
109109

110110
// Swap chunk
111-
swap_PSMCHUNK(pfh);
111+
swap_PSMCHUNK(&pfh);
112112

113113
// Chunk0: "PSM ",filesize,"FILE"
114-
if (pfh->id == PSM_ID_OLD)
114+
if (pfh.id == PSM_ID_OLD)
115115
{
116116
#ifdef PSM_LOG
117117
Log("Old PSM format not supported\n");
118118
#endif
119119
return FALSE;
120120
}
121-
if ((pfh->id != PSM_ID_NEW) || (pfh->len+12 > dwMemLength) || (pfh->listid != IFFID_FILE)) return FALSE;
121+
if ((pfh.id != PSM_ID_NEW) || (pfh.len+12 > dwMemLength) || (pfh.listid != IFFID_FILE)) return FALSE;
122122
m_nType = MOD_TYPE_PSM;
123123
m_nChannels = 16;
124124
m_nSamples = 0;
@@ -132,13 +132,13 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
132132
}
133133
while (dwMemPos+8 < dwMemLength)
134134
{
135-
PSMCHUNK *pchunk = (PSMCHUNK *)(lpStream+dwMemPos);
136-
swap_PSMCHUNK(pchunk);
137-
if ((pchunk->len >= dwMemLength - 8) || (dwMemPos + pchunk->len + 8 > dwMemLength)) break;
135+
PSMCHUNK pchunk = *(const PSMCHUNK *)(lpStream+dwMemPos);
136+
swap_PSMCHUNK(&pchunk);
137+
if ((pchunk.len >= dwMemLength - 8) || (dwMemPos + pchunk.len + 8 > dwMemLength)) break;
138138
dwMemPos += 8;
139139
PUCHAR pdata = (PUCHAR)(lpStream+dwMemPos);
140-
ULONG len = pchunk->len;
141-
if (len) switch(pchunk->id)
140+
ULONG len = pchunk.len;
141+
if (len) switch(pchunk.id)
142142
{
143143
// "TITL": Song title
144144
case IFFID_TITL:
@@ -166,21 +166,21 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
166166
{
167167
m_nSamples++;
168168
MODINSTRUMENT *pins = &Ins[m_nSamples];
169-
PSMSAMPLE *psmp = (PSMSAMPLE *)pdata;
170-
swap_PSMSAMPLE(psmp);
171-
smpnames[m_nSamples] = psmp->smpid;
172-
memcpy(m_szNames[m_nSamples], psmp->samplename, 31);
169+
PSMSAMPLE psmp = *(PSMSAMPLE *)pdata;
170+
swap_PSMSAMPLE(&psmp);
171+
smpnames[m_nSamples] = psmp.smpid;
172+
memcpy(m_szNames[m_nSamples], psmp.samplename, 31);
173173
m_szNames[m_nSamples][31] = 0;
174174
samplemap[m_nSamples-1] = (BYTE)m_nSamples;
175175
// Init sample
176176
pins->nGlobalVol = 0x40;
177-
pins->nC4Speed = psmp->samplerate;
178-
pins->nLength = psmp->length;
179-
pins->nLoopStart = psmp->loopstart;
180-
pins->nLoopEnd = psmp->loopend;
177+
pins->nC4Speed = psmp.samplerate;
178+
pins->nLength = psmp.length;
179+
pins->nLoopStart = psmp.loopstart;
180+
pins->nLoopEnd = psmp.loopend;
181181
pins->nPan = 128;
182-
pins->nVolume = (psmp->defvol+1) * 2;
183-
pins->uFlags = (psmp->flags & 0x80) ? CHN_LOOP : 0;
182+
pins->nVolume = (psmp.defvol+1) * 2;
183+
pins->uFlags = (psmp.flags & 0x80) ? CHN_LOOP : 0;
184184
if (pins->nLoopStart > 0) pins->nLoopStart--;
185185
// Point to sample data
186186
pdata += 0x60;
@@ -199,17 +199,17 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
199199
default:
200200
{
201201
CHAR s[8], s2[64];
202-
*(DWORD *)s = pchunk->id;
202+
*(DWORD *)s = pchunk.id;
203203
s[4] = 0;
204-
wsprintf(s2, "%s: %4d bytes @ %4d\n", s, pchunk->len, dwMemPos);
204+
wsprintf(s2, "%s: %4d bytes @ %4d\n", s, pchunk.len, dwMemPos);
205205
OutputDebugString(s2);
206206
}
207207
#endif
208208
}
209-
dwMemPos += pchunk->len;
209+
dwMemPos += pchunk.len;
210210
}
211211
// Step #1: convert song structure
212-
PSMSONGHDR *pSong = (PSMSONGHDR *)(lpStream+dwSongPos+8);
212+
const PSMSONGHDR *pSong = (const PSMSONGHDR *)(lpStream+dwSongPos+8);
213213
if ((!dwSongPos) || (pSong->channels < 2) || (pSong->channels > 32)) return TRUE;
214214
m_nChannels = pSong->channels;
215215
// Valid song header -> convert attached chunks
@@ -218,13 +218,13 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
218218
dwMemPos = dwSongPos + 8 + 11; // sizeof(PSMCHUNK)+sizeof(PSMSONGHDR)
219219
while (dwMemPos + 8 < dwSongEnd)
220220
{
221-
PSMCHUNK *pchunk = (PSMCHUNK *)(lpStream+dwMemPos);
222-
swap_PSMCHUNK(pchunk);
221+
PSMCHUNK pchunk = *(const PSMCHUNK *)(lpStream+dwMemPos);
222+
swap_PSMCHUNK(&pchunk);
223223
dwMemPos += 8;
224-
if ((pchunk->len > dwSongEnd) || (dwMemPos + pchunk->len > dwSongEnd)) break;
224+
if ((pchunk.len > dwSongEnd) || (dwMemPos + pchunk.len > dwSongEnd)) break;
225225
PUCHAR pdata = (PUCHAR)(lpStream+dwMemPos);
226-
ULONG len = pchunk->len;
227-
switch(pchunk->id)
226+
ULONG len = pchunk.len;
227+
switch(pchunk.id)
228228
{
229229
case IFFID_OPLH:
230230
if (len >= 0x20)
@@ -237,7 +237,7 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
237237
DWORD dwName = *(DWORD *)(pdata+pos);
238238
for (UINT i=0; i<nPatterns; i++)
239239
{
240-
DWORD dwPatName = ((PSMPATTERN *)(lpStream+patptrs[i]+8))->name;
240+
DWORD dwPatName = ((const PSMPATTERN *)(lpStream+patptrs[i]+8))->name;
241241
if (dwName == dwPatName)
242242
{
243243
bFound = TRUE;
@@ -258,7 +258,7 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
258258
DWORD dwName = *(DWORD *)(pdata+pos);
259259
for (UINT i=0; i<nPatterns; i++)
260260
{
261-
DWORD dwPatName = ((PSMPATTERN *)(lpStream+patptrs[i]+8))->name;
261+
DWORD dwPatName = ((const PSMPATTERN *)(lpStream+patptrs[i]+8))->name;
262262
if (dwName == dwPatName)
263263
{
264264
Order[iOrd++] = i;
@@ -270,23 +270,23 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
270270
}
271271
break;
272272
}
273-
dwMemPos += pchunk->len;
273+
dwMemPos += pchunk.len;
274274
}
275275
}
276276

277277
// Step #2: convert patterns
278278
for (UINT nPat=0; nPat<nPatterns; nPat++)
279279
{
280-
PSMPATTERN *pPsmPat = (PSMPATTERN *)(lpStream+patptrs[nPat]+8);
281-
swap_PSMPATTERN(pPsmPat);
280+
PSMPATTERN pPsmPat = *(const PSMPATTERN *)(lpStream+patptrs[nPat]+8);
281+
swap_PSMPATTERN(&pPsmPat);
282282
ULONG len = *(DWORD *)(lpStream+patptrs[nPat]+4) - 12;
283-
UINT nRows = pPsmPat->rows;
284-
if (len > pPsmPat->size) len = pPsmPat->size;
283+
UINT nRows = pPsmPat.rows;
284+
if (len > pPsmPat.size) len = pPsmPat.size;
285285
if ((nRows < 64) || (nRows > 256)) nRows = 64;
286286
PatternSize[nPat] = nRows;
287287
if ((Patterns[nPat] = AllocatePattern(nRows, m_nChannels)) == NULL) break;
288288
MODCOMMAND *m = Patterns[nPat];
289-
BYTE *p = pPsmPat->data;
289+
BYTE *p = pPsmPat.data;
290290
MODCOMMAND *sp, dummy;
291291
UINT pos = 0;
292292
UINT row = 0;
@@ -295,7 +295,7 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
295295
Log("Pattern %d at offset 0x%04X\n", nPat, (DWORD)(p - (BYTE *)lpStream));
296296
#endif
297297
UINT flags, ch;
298-
rowlim = bswapLE16(pPsmPat->reserved1)-2;
298+
rowlim = bswapLE16(pPsmPat.reserved1)-2;
299299
while ((row < nRows) && (pos+3 < len))
300300
{
301301
if ((pos+1) >= rowlim) {
@@ -393,7 +393,7 @@ BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength)
393393
#ifdef PSM_LOG
394394
if (pos < len)
395395
{
396-
Log("Pattern %d: %d/%d[%d] rows (%d bytes) -> %d bytes left\n", nPat, row, nRows, pPsmPat->rows, pPsmPat->size, len-pos);
396+
Log("Pattern %d: %d/%d[%d] rows (%d bytes) -> %d bytes left\n", nPat, row, nRows, pPsmPat.rows, pPsmPat.size, len-pos);
397397
}
398398
#endif
399399
}

0 commit comments

Comments
 (0)