Skip to content

Commit

Permalink
more robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
Mukunda Johnson committed Dec 9, 2008
1 parent 03965e1 commit 7ef0c38
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions source/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ int file_tell_write( void )
return ftell( fout );
}

int file_tell_size( void )
{
int pos = ftell(fin);
fseek( fin, 0, SEEK_END );
int size = ftell(fin);
fseek(fin, pos, SEEK_SET );
return size;
}

u8 read8( void )
{
u8 a;
Expand Down
2 changes: 2 additions & 0 deletions source/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ bool file_exists( char* filename );

int file_get_byte_count();

int file_tell_size( void );

#define FILE_OPEN_OKAY 0
#define FILE_OPEN_ERROR -1

Expand Down
10 changes: 9 additions & 1 deletion source/wav.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ int Load_WAV( Sample* samp, bool verbose, bool fix )
// initialize data
memset( samp, 0, sizeof( Sample ) );

file_size = file_tell_size();

read32(); // "RIFF"
file_size = read32() + 8;
read32(); // filesize-8
read32(); // "WAVE"

while( 1 )
Expand Down Expand Up @@ -125,6 +127,12 @@ int Load_WAV( Sample* samp, bool verbose, bool fix )
if( verbose )
printf( "Loading Sample Data...\n" );

// clip chunk size against end of file (for some borked wavs...)
{
int br = file_size - file_tell_read();
chunk_size = chunk_size > br ? br : chunk_size;
}

samp->sample_length = chunk_size / (bit_depth/8) / num_channels;
samp->data = malloc( chunk_size );

Expand Down

0 comments on commit 7ef0c38

Please sign in to comment.