Improve format detection to avoid hijacking other files

This commit is contained in:
bnnm 2017-07-15 11:27:43 +02:00
parent c501129cb1
commit d592199d6f
3 changed files with 30 additions and 31 deletions

View File

@ -4,50 +4,43 @@
VGMSTREAM * init_vgmstream_ngc_adpdtk(STREAMFILE *streamFile) { VGMSTREAM * init_vgmstream_ngc_adpdtk(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL; VGMSTREAM * vgmstream = NULL;
STREAMFILE * chstreamfile; off_t start_offset = 0;
char filename[PATH_LIMIT]; int channel_count = 2, loop_flag = 0; /* always stereo, no loop */
size_t file_size;
int i;
/* check extension, case insensitive */ /* check extension, case insensitive */
streamFile->get_name(streamFile,filename,sizeof(filename)); if ( !check_extensions(streamFile,"dtk,adp"))
if (strcasecmp("adp",filename_extension(filename)) && goto fail;
strcasecmp("dtk",filename_extension(filename))) goto fail;
/* file size is the only way to determine sample count */ /* .adp files have no header, and the ext is common, so all we can do is look for valid first frames */
file_size = get_streamfile_size(streamFile); if (check_extensions(streamFile,"adp")) {
int i;
for (i = 0; i < 10; i++) { /* try a bunch of frames */
if (read_8bit(0x00 + i*0x20,streamFile) != read_8bit(0x02 + i*0x20,streamFile) ||
read_8bit(0x01 + i*0x20,streamFile) != read_8bit(0x03 + i*0x20,streamFile))
goto fail;
}
}
/* .adp files have no header, so all we can do is look for a valid first frame */
if (read_8bit(0,streamFile)!=read_8bit(2,streamFile) || read_8bit(1,streamFile)!=read_8bit(3,streamFile)) goto fail;
/* Hopefully we haven't falsely detected something else... */
/* build the VGMSTREAM */ /* build the VGMSTREAM */
vgmstream = allocate_vgmstream(2,0); /* always stereo, no loop */ vgmstream = allocate_vgmstream(channel_count, loop_flag);
if (!vgmstream) goto fail; if (!vgmstream) goto fail;
vgmstream->num_samples = file_size/32*28; vgmstream->num_samples = get_streamfile_size(streamFile) / 32 * 28;
vgmstream->sample_rate = 48000; vgmstream->sample_rate = 48000;
vgmstream->coding_type = coding_NGC_DTK; vgmstream->coding_type = coding_NGC_DTK;
vgmstream->layout_type = layout_none; vgmstream->layout_type = layout_none;
vgmstream->meta_type = meta_NGC_ADPDTK; vgmstream->meta_type = meta_NGC_ADPDTK;
/* locality is such that two streamfiles is silly */
chstreamfile = streamFile->open(streamFile,filename,32*0x400);
if (!chstreamfile) goto fail;
for (i=0;i<2;i++) { /* open the file for reading */
vgmstream->ch[i].channel_start_offset = if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
vgmstream->ch[i].offset = 0; goto fail;
vgmstream->ch[i].streamfile = chstreamfile;
}
return vgmstream; return vgmstream;
/* clean up anything we may have opened */
fail: fail:
if (vgmstream) close_vgmstream(vgmstream); close_vgmstream(vgmstream);
return NULL; return NULL;
} }

View File

@ -60,9 +60,9 @@ VGMSTREAM * init_vgmstream_pc_adp_otns(STREAMFILE *streamFile) {
/* no ID, only a basic 0x10 header with filesize and nulls; do some extra checks */ /* no ID, only a basic 0x10 header with filesize and nulls; do some extra checks */
datasize = read_32bitLE(0x00,streamFile) & 0x00FFFFFF; /*24 bit*/ datasize = read_32bitLE(0x00,streamFile) & 0x00FFFFFF; /*24 bit*/
if (datasize + 0x10 != streamFile->get_size(streamFile) if (datasize + 0x10 != streamFile->get_size(streamFile)
&& read_32bitLE(0x04,streamFile) != 0 || read_32bitLE(0x04,streamFile) != 0
&& read_32bitLE(0x08,streamFile) != 0 || read_32bitLE(0x08,streamFile) != 0
&& read_32bitLE(0x10,streamFile) != 0) || read_32bitLE(0x10,streamFile) != 0)
goto fail; goto fail;
stereo_flag = read_8bit(0x03, streamFile); stereo_flag = read_8bit(0x03, streamFile);

View File

@ -23,8 +23,14 @@ VGMSTREAM * init_vgmstream_ps2_strlr(STREAMFILE *streamFile) {
goto fail; goto fail;
#endif #endif
/* don't hijack Sonic & Sega All Stars Racing X360 (xma) */ /* don't hijack Sonic & Sega All Stars Racing X360 (xma) */
if (read_32bitBE(0x00,streamFile) == 0x52494646) /* "RIFF"*/ if (read_32bitBE(0x00,streamFile) == 0x52494646)
goto fail; /* "RIFF"*/
/* don't hijack Mad Dash Racing (Xbox) */
if (read_32bitLE(0x0c,streamFile) == 1
&& read_32bitLE(0x010,streamFile) == 0
&& read_32bitLE(0x400,streamFile) == 0
&& read_32bitLE(0x7f0,streamFile) == 0)
goto fail; goto fail;
loop_flag = 0; loop_flag = 0;