1 /*
2 ** Copyright (C) 2010-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
8 ** met:
9 **
10 **     * Redistributions of source code must retain the above copyright
11 **       notice, this list of conditions and the following disclaimer.
12 **     * Redistributions in binary form must reproduce the above copyright
13 **       notice, this list of conditions and the following disclaimer in
14 **       the documentation and/or other materials provided with the
15 **       distribution.
16 **     * Neither the author nor the names of any contributors may be used
17 **       to endorse or promote products derived from this software without
18 **       specific prior written permission.
19 **
20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #include	"sfconfig.h"
34 
35 #include	<stdio.h>
36 #include	<stdlib.h>
37 #include	<string.h>
38 #include	<inttypes.h>
39 #include	<ctype.h>
40 #include	<math.h>
41 #include	<errno.h>
42 #if HAVE_UNISTD_H
43 #include	<unistd.h>
44 #else
45 #include	"sf_unistd.h"
46 #endif
47 #include	<fcntl.h>
48 #include	<sys/stat.h>
49 #include	<sys/types.h>
50 
51 #include	<sndfile.h>
52 
53 #include	"common.h"
54 
55 #define	BUFFER_LEN		(1 << 16)
56 
57 #define	NOT(x)			(! (x))
58 
59 
60 static void usage_exit (const char *progname) ;
61 static void salvage_file (const char * broken_wav, const char * fixed_w64) ;
62 
63 int
main(int argc,char * argv[])64 main (int argc, char *argv [])
65 {
66 	if (argc != 3)
67 		usage_exit (program_name (argv [0])) ;
68 
69 	salvage_file (argv [1], argv [2]) ;
70 
71 	return 0 ;
72 } /* main */
73 
74 /*==============================================================================
75 */
76 
77 static void lseek_or_die (int fd, off_t offset, int whence) ;
78 static sf_count_t get_file_length (int fd, const char * name) ;
79 static sf_count_t find_data_offset (int fd, int format) ;
80 static void copy_data (int fd, SNDFILE * sndfile, int readsize) ;
81 
82 
83 static void
usage_exit(const char * progname)84 usage_exit (const char *progname)
85 {	printf ("Usage :\n\n  %s <broken wav file> <fixed w64 file>\n\n", progname) ;
86 	puts ("Salvages the audio data from WAV files which are more than 4G in length.\n") ;
87 	printf ("Using %s.\n\n", sf_version_string ()) ;
88 	exit (1) ;
89 } /* usage_exit */
90 
91 static void
salvage_file(const char * broken_wav,const char * fixed_w64)92 salvage_file (const char * broken_wav, const char * fixed_w64)
93 {	SNDFILE * sndfile ;
94 	SF_INFO sfinfo ;
95 	sf_count_t broken_len, data_offset ;
96 	int fd, read_size ;
97 
98 	if (strcmp (broken_wav, fixed_w64) == 0)
99 	{	printf ("Error : Input and output files must be different.\n\n") ;
100 		exit (1) ;
101 		} ;
102 
103 	if ((fd = open (broken_wav, O_RDONLY)) < 0)
104 	{	printf ("Error : Not able to open file '%s' : %s\n", broken_wav, strerror (errno)) ;
105 		exit (1) ;
106 		} ;
107 
108 	broken_len = get_file_length (fd, broken_wav) ;
109 	if (broken_len <= 0xffffffff)
110 		printf ("File is not greater than 4Gig but salvaging anyway.\n") ;
111 
112 	/* Grab the format info from the broken file. */
113 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
114 	if ((sndfile = sf_open (broken_wav, SFM_READ, &sfinfo)) == NULL)
115 	{	printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ;
116 		exit (1) ;
117 		} ;
118 	sf_close (sndfile) ;
119 
120 	data_offset = find_data_offset (fd, sfinfo.format & SF_FORMAT_TYPEMASK) ;
121 
122 	printf ("Offset to audio data : %" PRId64 "\n", data_offset) ;
123 
124 	switch (sfinfo.format & SF_FORMAT_TYPEMASK)
125 	{	case SF_FORMAT_WAV :
126 		case SF_FORMAT_WAVEX :
127 			sfinfo.format = SF_FORMAT_W64 | (sfinfo.format & SF_FORMAT_SUBMASK) ;
128 			break ;
129 
130 		default :
131 			printf ("Don't currently support this file type.\n") ;
132 			exit (1) ;
133 		} ;
134 
135 	switch (sfinfo.format & SF_FORMAT_SUBMASK)
136 	{	case SF_FORMAT_PCM_U8 :
137 		case SF_FORMAT_PCM_S8 :
138 				read_size = 1 ;
139 				break ;
140 
141 		case SF_FORMAT_PCM_16 :
142 				read_size = 2 ;
143 				break ;
144 
145 		case SF_FORMAT_PCM_24 :
146 				read_size = 3 ;
147 				break ;
148 
149 		case SF_FORMAT_PCM_32 :
150 		case SF_FORMAT_FLOAT :
151 				read_size = 4 ;
152 				break ;
153 
154 		case SF_FORMAT_DOUBLE :
155 				read_size = 8 ;
156 				break ;
157 
158 		default :
159 			printf ("Sorry, don't currently support this file encoding type.\n") ;
160 			exit (1) ;
161 		} ;
162 
163 	read_size *= sfinfo.channels ;
164 
165 	if ((sndfile = sf_open (fixed_w64, SFM_WRITE, &sfinfo)) == NULL)
166 	{	printf ("sf_open ('%s') failed : %s\n", fixed_w64, sf_strerror (NULL)) ;
167 		exit (1) ;
168 		} ;
169 
170 	lseek_or_die (fd, data_offset, SEEK_SET) ;
171 
172 	copy_data (fd, sndfile, read_size) ;
173 
174 	sf_close (sndfile) ;
175 
176 	puts ("Done!") ;
177 } /* salvage_file */
178 
179 /*------------------------------------------------------------------------------
180 */
181 
182 static void
lseek_or_die(int fd,off_t offset,int whence)183 lseek_or_die (int fd, off_t offset, int whence)
184 {
185 	if (lseek (fd, offset, whence) < 0)
186 	{	printf ("lseek failed : %s\n", strerror (errno)) ;
187 		exit (1) ;
188 		} ;
189 
190 	return ;
191 } /* lseek_or_die */
192 
193 
194 static sf_count_t
get_file_length(int fd,const char * name)195 get_file_length (int fd, const char * name)
196 {	struct stat sbuf ;
197 
198 	if (sizeof (sbuf.st_size) != 8)
199 	{	puts ("Error : sizeof (sbuf.st_size) != 8. Was program compiled with\n"
200 				"        64 bit file offsets?\n") ;
201 		exit (1) ;
202 		} ;
203 
204 	if (fstat (fd, &sbuf) != 0)
205 	{	printf ("Error : fstat ('%s') failed : %s\n", name, strerror (errno)) ;
206 		exit (1) ;
207 		} ;
208 
209 	return sbuf.st_size ;
210 } /* get_file_length */
211 
212 static sf_count_t
find_data_offset(int fd,int format)213 find_data_offset (int fd, int format)
214 {	char buffer [8192], *cptr ;
215 	const char * target = "XXXX" ;
216 	sf_count_t offset = -1, extra ;
217 	int rlen, slen ;
218 
219 	switch (format)
220 	{	case SF_FORMAT_WAV :
221 		case SF_FORMAT_WAVEX :
222 			target = "data" ;
223 			extra = 8 ;
224 			break ;
225 
226 		case SF_FORMAT_AIFF :
227 			target = "SSND" ;
228 			extra = 16 ;
229 			break ;
230 
231 		default :
232 			puts ("Error : Sorry, don't handle this input file format.\n") ;
233 			exit (1) ;
234 		} ;
235 
236 	slen = strlen (target) ;
237 
238 	lseek_or_die (fd, 0, SEEK_SET) ;
239 
240 	printf ("Searching for '%s' maker.\n", target) ;
241 
242 	if ((rlen = read (fd, buffer, sizeof (buffer))) < 0)
243 	{	printf ("Error : failed read : %s\n", strerror (errno)) ;
244 		exit (1) ;
245 		} ;
246 
247 	cptr = memchr (buffer, target [0], rlen - slen) ;
248 	if (cptr && memcmp (cptr, target, slen) == 0)
249 		offset = cptr - buffer ;
250 	else
251 	{	printf ("Error : Could not find data offset.\n") ;
252 		exit (1) ;
253 		} ;
254 
255 	return offset + extra ;
256 } /* find_data_offset */
257 
258 static void
copy_data(int fd,SNDFILE * sndfile,int readsize)259 copy_data (int fd, SNDFILE * sndfile, int readsize)
260 {	static char * buffer ;
261 	sf_count_t readlen, count ;
262 	int bufferlen, done = 0 ;
263 
264 	bufferlen = readsize * 1024 ;
265 	buffer = malloc (bufferlen) ;
266 
267 	while (NOT (done) && (readlen = read (fd, buffer, bufferlen)) >= 0)
268 	{	if (readlen < bufferlen)
269 		{	readlen -= readlen % readsize ;
270 			done = 1 ;
271 			} ;
272 
273 		if ((count = sf_write_raw (sndfile, buffer, readlen)) != readlen)
274 		{	printf ("Error : sf_write_raw returned %" PRId64 " : %s\n", count, sf_strerror (sndfile)) ;
275 			return ;
276 			} ;
277 		} ;
278 
279 	free (buffer) ;
280 
281 	return ;
282 } /* copy_data */
283 
284