Skip to content

Commit d5bf76b

Browse files
committed
issue #27, do not overwrite stack on corrupt RF64 file
1 parent 76eab10 commit d5bf76b

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

Diff for: cli/riff.c

+32-7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef struct {
4242

4343
#pragma pack(pop)
4444

45+
#define CS64ChunkFormat "4D"
4546
#define DS64ChunkFormat "DDDL"
4647

4748
#define WAVPACK_NO_ERROR 0
@@ -101,13 +102,13 @@ int ParseRiffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
101102

102103
if (!strncmp (chunk_header.ckID, "ds64", 4)) {
103104
if (chunk_header.ckSize < sizeof (DS64Chunk) ||
104-
!DoReadFile (infile, &ds64_chunk, chunk_header.ckSize, &bcount) ||
105-
bcount != chunk_header.ckSize) {
105+
!DoReadFile (infile, &ds64_chunk, sizeof (DS64Chunk), &bcount) ||
106+
bcount != sizeof (DS64Chunk)) {
106107
error_line ("%s is not a valid .WAV file!", infilename);
107108
return WAVPACK_SOFT_ERROR;
108109
}
109110
else if (!(config->qmode & QMODE_NO_STORE_WRAPPER) &&
110-
!WavpackAddWrapper (wpc, &ds64_chunk, chunk_header.ckSize)) {
111+
!WavpackAddWrapper (wpc, &ds64_chunk, sizeof (DS64Chunk))) {
111112
error_line ("%s", WavpackGetErrorMessage (wpc));
112113
return WAVPACK_SOFT_ERROR;
113114
}
@@ -315,10 +316,11 @@ int ParseRiffHeaderConfig (FILE *infile, char *infilename, char *fourcc, Wavpack
315316

316317
int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples, int qmode)
317318
{
318-
int do_rf64 = 0, write_junk = 1;
319+
int do_rf64 = 0, write_junk = 1, table_length = 0;
319320
ChunkHeader ds64hdr, datahdr, fmthdr;
320321
RiffChunkHeader riffhdr;
321322
DS64Chunk ds64_chunk;
323+
CS64Chunk cs64_chunk;
322324
JunkChunk junkchunk;
323325
WaveHeader wavhdr;
324326
uint32_t bcount;
@@ -380,6 +382,7 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
380382
strncpy (riffhdr.formType, "WAVE", sizeof (riffhdr.formType));
381383
total_riff_bytes = sizeof (riffhdr) + wavhdrsize + sizeof (datahdr) + ((total_data_bytes + 1) & ~(int64_t)1);
382384
if (do_rf64) total_riff_bytes += sizeof (ds64hdr) + sizeof (ds64_chunk);
385+
total_riff_bytes += table_length * sizeof (CS64Chunk);
383386
if (write_junk) total_riff_bytes += sizeof (junkchunk);
384387
strncpy (fmthdr.ckID, "fmt ", sizeof (fmthdr.ckID));
385388
strncpy (datahdr.ckID, "data", sizeof (datahdr.ckID));
@@ -394,11 +397,12 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
394397

395398
if (do_rf64) {
396399
strncpy (ds64hdr.ckID, "ds64", sizeof (ds64hdr.ckID));
397-
ds64hdr.ckSize = sizeof (ds64_chunk);
400+
ds64hdr.ckSize = sizeof (ds64_chunk) + (table_length * sizeof (CS64Chunk));
398401
CLEAR (ds64_chunk);
399402
ds64_chunk.riffSize64 = total_riff_bytes;
400403
ds64_chunk.dataSize64 = total_data_bytes;
401404
ds64_chunk.sampleCount64 = total_samples;
405+
ds64_chunk.tableLength = table_length;
402406
riffhdr.ckSize = (uint32_t) -1;
403407
datahdr.ckSize = (uint32_t) -1;
404408
WavpackNativeToLittleEndian (&ds64hdr, ChunkHeaderFormat);
@@ -409,6 +413,14 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
409413
datahdr.ckSize = (uint32_t) total_data_bytes;
410414
}
411415

416+
// this "table" is just a dummy placeholder for testing (normally not written)
417+
418+
if (table_length) {
419+
strncpy (cs64_chunk.ckID, "dmmy", sizeof (cs64_chunk.ckID));
420+
cs64_chunk.chunkSize64 = 12345678;
421+
WavpackNativeToLittleEndian (&cs64_chunk, CS64ChunkFormat);
422+
}
423+
412424
// write the RIFF chunks up to just before the data starts
413425

414426
WavpackNativeToLittleEndian (&riffhdr, ChunkHeaderFormat);
@@ -418,8 +430,21 @@ int WriteRiffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
418430

419431
if (!DoWriteFile (outfile, &riffhdr, sizeof (riffhdr), &bcount) || bcount != sizeof (riffhdr) ||
420432
(do_rf64 && (!DoWriteFile (outfile, &ds64hdr, sizeof (ds64hdr), &bcount) || bcount != sizeof (ds64hdr))) ||
421-
(do_rf64 && (!DoWriteFile (outfile, &ds64_chunk, sizeof (ds64_chunk), &bcount) || bcount != sizeof (ds64_chunk))) ||
422-
(write_junk && (!DoWriteFile (outfile, &junkchunk, sizeof (junkchunk), &bcount) || bcount != sizeof (junkchunk))) ||
433+
(do_rf64 && (!DoWriteFile (outfile, &ds64_chunk, sizeof (ds64_chunk), &bcount) || bcount != sizeof (ds64_chunk)))) {
434+
error_line ("can't write .WAV data, disk probably full!");
435+
return FALSE;
436+
}
437+
438+
// again, this is normally not written except for testing
439+
440+
while (table_length--)
441+
if (!DoWriteFile (outfile, &cs64_chunk, sizeof (cs64_chunk), &bcount) || bcount != sizeof (cs64_chunk)) {
442+
error_line ("can't write .WAV data, disk probably full!");
443+
return FALSE;
444+
}
445+
446+
447+
if ((write_junk && (!DoWriteFile (outfile, &junkchunk, sizeof (junkchunk), &bcount) || bcount != sizeof (junkchunk))) ||
423448
!DoWriteFile (outfile, &fmthdr, sizeof (fmthdr), &bcount) || bcount != sizeof (fmthdr) ||
424449
!DoWriteFile (outfile, &wavhdr, wavhdrsize, &bcount) || bcount != wavhdrsize ||
425450
!DoWriteFile (outfile, &datahdr, sizeof (datahdr), &bcount) || bcount != sizeof (datahdr)) {

0 commit comments

Comments
 (0)