Chris@40: /* Chris@40: ** Copyright (C) 2010-2014 Erik de Castro Lopo Chris@40: ** Chris@40: ** All rights reserved. Chris@40: ** Chris@40: ** Redistribution and use in source and binary forms, with or without Chris@40: ** modification, are permitted provided that the following conditions are Chris@40: ** met: Chris@40: ** Chris@40: ** * Redistributions of source code must retain the above copyright Chris@40: ** notice, this list of conditions and the following disclaimer. Chris@40: ** * Redistributions in binary form must reproduce the above copyright Chris@40: ** notice, this list of conditions and the following disclaimer in Chris@40: ** the documentation and/or other materials provided with the Chris@40: ** distribution. Chris@40: ** * Neither the author nor the names of any contributors may be used Chris@40: ** to endorse or promote products derived from this software without Chris@40: ** specific prior written permission. Chris@40: ** Chris@40: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@40: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@40: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@40: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@40: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@40: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@40: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@40: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@40: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@40: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@40: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@40: */ Chris@40: Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: #include Chris@40: Chris@40: #include Chris@40: Chris@40: #include "common.h" Chris@40: Chris@40: #define BUFFER_LEN (1 << 16) Chris@40: Chris@40: #define NOT(x) (! (x)) Chris@40: Chris@40: Chris@40: static void usage_exit (const char *progname) ; Chris@40: static void salvage_file (const char * broken_wav, const char * fixed_w64) ; Chris@40: Chris@40: int Chris@40: main (int argc, char *argv []) Chris@40: { Chris@40: if (argc != 3) Chris@40: usage_exit (program_name (argv [0])) ; Chris@40: Chris@40: salvage_file (argv [1], argv [2]) ; Chris@40: Chris@40: return 0 ; Chris@40: } /* main */ Chris@40: Chris@40: /*============================================================================== Chris@40: */ Chris@40: Chris@40: static void lseek_or_die (int fd, off_t offset, int whence) ; Chris@40: static sf_count_t get_file_length (int fd, const char * name) ; Chris@40: static sf_count_t find_data_offset (int fd, int format) ; Chris@40: static void copy_data (int fd, SNDFILE * sndfile, int readsize) ; Chris@40: Chris@40: Chris@40: static void Chris@40: usage_exit (const char *progname) Chris@40: { printf ("Usage :\n\n %s \n\n", progname) ; Chris@40: puts ("Salvages the audio data from WAV files which are more than 4G in length.\n") ; Chris@40: printf ("Using %s.\n\n", sf_version_string ()) ; Chris@40: exit (1) ; Chris@40: } /* usage_exit */ Chris@40: Chris@40: static void Chris@40: salvage_file (const char * broken_wav, const char * fixed_w64) Chris@40: { SNDFILE * sndfile ; Chris@40: SF_INFO sfinfo ; Chris@40: sf_count_t broken_len, data_offset ; Chris@40: int fd, read_size ; Chris@40: Chris@40: if (strcmp (broken_wav, fixed_w64) == 0) Chris@40: { printf ("Error : Input and output files must be different.\n\n") ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: if ((fd = open (broken_wav, O_RDONLY)) < 0) Chris@40: { printf ("Error : Not able to open file '%s' : %s\n", broken_wav, strerror (errno)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: broken_len = get_file_length (fd, broken_wav) ; Chris@40: if (broken_len <= 0xffffffff) Chris@40: printf ("File is not greater than 4Gig but salvaging anyway.\n") ; Chris@40: Chris@40: /* Grab the format info from the broken file. */ Chris@40: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@40: if ((sndfile = sf_open (broken_wav, SFM_READ, &sfinfo)) == NULL) Chris@40: { printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: sf_close (sndfile) ; Chris@40: Chris@40: data_offset = find_data_offset (fd, sfinfo.format & SF_FORMAT_TYPEMASK) ; Chris@40: Chris@40: printf ("Offset to audio data : %" PRId64 "\n", data_offset) ; Chris@40: Chris@40: switch (sfinfo.format & SF_FORMAT_TYPEMASK) Chris@40: { case SF_FORMAT_WAV : Chris@40: case SF_FORMAT_WAVEX : Chris@40: sfinfo.format = SF_FORMAT_W64 | (sfinfo.format & SF_FORMAT_SUBMASK) ; Chris@40: break ; Chris@40: Chris@40: default : Chris@40: printf ("Don't currently support this file type.\n") ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: switch (sfinfo.format & SF_FORMAT_SUBMASK) Chris@40: { case SF_FORMAT_PCM_U8 : Chris@40: case SF_FORMAT_PCM_S8 : Chris@40: read_size = 1 ; Chris@40: break ; Chris@40: Chris@40: case SF_FORMAT_PCM_16 : Chris@40: read_size = 2 ; Chris@40: break ; Chris@40: Chris@40: case SF_FORMAT_PCM_24 : Chris@40: read_size = 3 ; Chris@40: break ; Chris@40: Chris@40: case SF_FORMAT_PCM_32 : Chris@40: case SF_FORMAT_FLOAT : Chris@40: read_size = 4 ; Chris@40: break ; Chris@40: Chris@40: case SF_FORMAT_DOUBLE : Chris@40: read_size = 8 ; Chris@40: break ; Chris@40: Chris@40: default : Chris@40: printf ("Sorry, don't currently support this file encoding type.\n") ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: read_size *= sfinfo.channels ; Chris@40: Chris@40: if ((sndfile = sf_open (fixed_w64, SFM_WRITE, &sfinfo)) == NULL) Chris@40: { printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: lseek_or_die (fd, data_offset, SEEK_SET) ; Chris@40: Chris@40: copy_data (fd, sndfile, read_size) ; Chris@40: Chris@40: sf_close (sndfile) ; Chris@40: Chris@40: puts ("Done!") ; Chris@40: } /* salvage_file */ Chris@40: Chris@40: /*------------------------------------------------------------------------------ Chris@40: */ Chris@40: Chris@40: static void Chris@40: lseek_or_die (int fd, off_t offset, int whence) Chris@40: { Chris@40: if (lseek (fd, offset, whence) < 0) Chris@40: { printf ("lseek failed : %s\n", strerror (errno)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return ; Chris@40: } /* lseek_or_die */ Chris@40: Chris@40: Chris@40: static sf_count_t Chris@40: get_file_length (int fd, const char * name) Chris@40: { struct stat sbuf ; Chris@40: Chris@40: if (sizeof (sbuf.st_size) != 8) Chris@40: { puts ("Error : sizeof (sbuf.st_size) != 8. Was program compiled with\n" Chris@40: " 64 bit file offsets?\n") ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: if (fstat (fd, &sbuf) != 0) Chris@40: { printf ("Error : fstat ('%s') failed : %s\n", name, strerror (errno)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return sbuf.st_size ; Chris@40: } /* get_file_length */ Chris@40: Chris@40: static sf_count_t Chris@40: find_data_offset (int fd, int format) Chris@40: { char buffer [8192], *cptr ; Chris@40: const char * target = "XXXX" ; Chris@40: sf_count_t offset = -1, extra ; Chris@40: int rlen, slen ; Chris@40: Chris@40: switch (format) Chris@40: { case SF_FORMAT_WAV : Chris@40: case SF_FORMAT_WAVEX : Chris@40: target = "data" ; Chris@40: extra = 8 ; Chris@40: break ; Chris@40: Chris@40: case SF_FORMAT_AIFF : Chris@40: target = "SSND" ; Chris@40: extra = 16 ; Chris@40: break ; Chris@40: Chris@40: default : Chris@40: puts ("Error : Sorry, don't handle this input file format.\n") ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: slen = strlen (target) ; Chris@40: Chris@40: lseek_or_die (fd, 0, SEEK_SET) ; Chris@40: Chris@40: printf ("Searching for '%s' maker.\n", target) ; Chris@40: Chris@40: if ((rlen = read (fd, buffer, sizeof (buffer))) < 0) Chris@40: { printf ("Error : failed read : %s\n", strerror (errno)) ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: cptr = memchr (buffer, target [0], rlen - slen) ; Chris@40: if (cptr && memcmp (cptr, target, slen) == 0) Chris@40: offset = cptr - buffer ; Chris@40: else Chris@40: { printf ("Error : Could not find data offset.\n") ; Chris@40: exit (1) ; Chris@40: } ; Chris@40: Chris@40: return offset + extra ; Chris@40: } /* find_data_offset */ Chris@40: Chris@40: static void Chris@40: copy_data (int fd, SNDFILE * sndfile, int readsize) Chris@40: { static char * buffer ; Chris@40: sf_count_t readlen, count ; Chris@40: int bufferlen, done = 0 ; Chris@40: Chris@40: bufferlen = readsize * 1024 ; Chris@40: buffer = malloc (bufferlen) ; Chris@40: Chris@40: while (NOT (done) && (readlen = read (fd, buffer, bufferlen)) >= 0) Chris@40: { if (readlen < bufferlen) Chris@40: { readlen -= readlen % readsize ; Chris@40: done = 1 ; Chris@40: } ; Chris@40: Chris@40: if ((count = sf_write_raw (sndfile, buffer, readlen)) != readlen) Chris@40: { printf ("Error : sf_write_raw returned %" PRId64 " : %s\n", count, sf_strerror (sndfile)) ; Chris@40: return ; Chris@40: } ; Chris@40: } ; Chris@40: Chris@40: free (buffer) ; Chris@40: Chris@40: return ; Chris@40: } /* copy_data */ Chris@40: