• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _WIN32
60 typedef off_t sf_off_t ;
61 #else
62 typedef long long sf_off_t ;
63 #endif
64 
65 
66 static void usage_exit (const char *progname) ;
67 static void salvage_file (const char * broken_wav, const char * fixed_w64) ;
68 
69 int
main(int argc,char * argv[])70 main (int argc, char *argv [])
71 {
72 	if (argc != 3)
73 		usage_exit (program_name (argv [0])) ;
74 
75 	salvage_file (argv [1], argv [2]) ;
76 
77 	return 0 ;
78 } /* main */
79 
80 /*==============================================================================
81 */
82 
83 static void lseek_or_die (int fd, sf_off_t offset, int whence) ;
84 static sf_off_t get_file_length (int fd, const char * name) ;
85 static sf_count_t find_data_offset (int fd, int format) ;
86 static void copy_data (int fd, SNDFILE * sndfile, int readsize) ;
87 
88 
89 static void
usage_exit(const char * progname)90 usage_exit (const char *progname)
91 {	printf ("Usage :\n\n  %s <broken wav file> <fixed w64 file>\n\n", progname) ;
92 	puts ("Salvages the audio data from WAV files which are more than 4G in length.\n") ;
93 	printf ("Using %s.\n\n", sf_version_string ()) ;
94 	exit (1) ;
95 } /* usage_exit */
96 
97 static void
salvage_file(const char * broken_wav,const char * fixed_w64)98 salvage_file (const char * broken_wav, const char * fixed_w64)
99 {	SNDFILE * sndfile ;
100 	SF_INFO sfinfo ;
101 	sf_count_t broken_len, data_offset ;
102 	int fd, read_size ;
103 
104 	if (strcmp (broken_wav, fixed_w64) == 0)
105 	{	printf ("Error : Input and output files must be different.\n\n") ;
106 		exit (1) ;
107 		} ;
108 
109 	if ((fd = open (broken_wav, O_RDONLY)) < 0)
110 	{	printf ("Error : Not able to open file '%s' : %s\n", broken_wav, strerror (errno)) ;
111 		exit (1) ;
112 		} ;
113 
114 	broken_len = get_file_length (fd, broken_wav) ;
115 	if (broken_len <= 0xffffffff)
116 		printf ("File is not greater than 4Gig but salvaging anyway.\n") ;
117 
118 	/* Grab the format info from the broken file. */
119 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
120 	if ((sndfile = sf_open (broken_wav, SFM_READ, &sfinfo)) == NULL)
121 	{	printf ("sf_open ('%s') failed : %s\n", broken_wav, sf_strerror (NULL)) ;
122 		exit (1) ;
123 		} ;
124 	sf_close (sndfile) ;
125 
126 	data_offset = find_data_offset (fd, sfinfo.format & SF_FORMAT_TYPEMASK) ;
127 
128 	printf ("Offset to audio data : %" PRId64 "\n", data_offset) ;
129 
130 	switch (sfinfo.format & SF_FORMAT_TYPEMASK)
131 	{	case SF_FORMAT_WAV :
132 		case SF_FORMAT_WAVEX :
133 			sfinfo.format = SF_FORMAT_W64 | (sfinfo.format & SF_FORMAT_SUBMASK) ;
134 			break ;
135 
136 		default :
137 			printf ("Don't currently support this file type.\n") ;
138 			exit (1) ;
139 		} ;
140 
141 	switch (sfinfo.format & SF_FORMAT_SUBMASK)
142 	{	case SF_FORMAT_PCM_U8 :
143 		case SF_FORMAT_PCM_S8 :
144 				read_size = 1 ;
145 				break ;
146 
147 		case SF_FORMAT_PCM_16 :
148 				read_size = 2 ;
149 				break ;
150 
151 		case SF_FORMAT_PCM_24 :
152 				read_size = 3 ;
153 				break ;
154 
155 		case SF_FORMAT_PCM_32 :
156 		case SF_FORMAT_FLOAT :
157 				read_size = 4 ;
158 				break ;
159 
160 		case SF_FORMAT_DOUBLE :
161 				read_size = 8 ;
162 				break ;
163 
164 		default :
165 			printf ("Sorry, don't currently support this file encoding type.\n") ;
166 			exit (1) ;
167 		} ;
168 
169 	read_size *= sfinfo.channels ;
170 
171 	if ((sndfile = sf_open (fixed_w64, SFM_WRITE, &sfinfo)) == NULL)
172 	{	printf ("sf_open ('%s') failed : %s\n", fixed_w64, sf_strerror (NULL)) ;
173 		exit (1) ;
174 		} ;
175 
176 	lseek_or_die (fd, data_offset, SEEK_SET) ;
177 
178 	copy_data (fd, sndfile, read_size) ;
179 
180 	sf_close (sndfile) ;
181 
182 	puts ("Done!") ;
183 } /* salvage_file */
184 
185 /*------------------------------------------------------------------------------
186 */
187 
188 static void
lseek_or_die(int fd,sf_off_t offset,int whence)189 lseek_or_die (int fd, sf_off_t offset, int whence)
190 {
191 #ifndef _WIN32
192 	if (lseek (fd, offset, whence) < 0)
193 #else
194 	if (_lseeki64 (fd, offset, whence) < 0)
195 #endif
196 	{	printf ("lseek failed : %s\n", strerror (errno)) ;
197 		exit (1) ;
198 		} ;
199 
200 	return ;
201 } /* lseek_or_die */
202 
203 
204 static sf_off_t
get_file_length(int fd,const char * name)205 get_file_length (int fd, const char * name)
206 {
207 #ifndef _WIN32
208 	struct stat sbuf ;
209 #else
210 	struct _stat64 sbuf ;
211 #endif
212 
213 	if (sizeof (sbuf.st_size) != 8)
214 	{	puts ("Error : sizeof (sbuf.st_size) != 8. Was program compiled with\n"
215 				"        64 bit file offsets?\n") ;
216 		exit (1) ;
217 		} ;
218 
219 #ifndef _WIN32
220 	if (fstat (fd, &sbuf) != 0)
221 #else
222 	if (_fstat64 (fd, &sbuf) != 0)
223 #endif
224 	{	printf ("Error : fstat ('%s') failed : %s\n", name, strerror (errno)) ;
225 		exit (1) ;
226 		} ;
227 
228 	return sbuf.st_size ;
229 } /* get_file_length */
230 
231 static sf_count_t
find_data_offset(int fd,int format)232 find_data_offset (int fd, int format)
233 {	char buffer [8192], *cptr ;
234 	const char * target = "XXXX" ;
235 	sf_count_t offset = -1, extra ;
236 	int rlen, slen ;
237 
238 	switch (format)
239 	{	case SF_FORMAT_WAV :
240 		case SF_FORMAT_WAVEX :
241 			target = "data" ;
242 			extra = 8 ;
243 			break ;
244 
245 		case SF_FORMAT_AIFF :
246 			target = "SSND" ;
247 			extra = 16 ;
248 			break ;
249 
250 		default :
251 			puts ("Error : Sorry, don't handle this input file format.\n") ;
252 			exit (1) ;
253 		} ;
254 
255 	slen = (int) strlen (target) ;
256 
257 	lseek_or_die (fd, 0, SEEK_SET) ;
258 
259 	printf ("Searching for '%s' maker.\n", target) ;
260 
261 	if ((rlen = read (fd, buffer, sizeof (buffer))) < 0)
262 	{	printf ("Error : failed read : %s\n", strerror (errno)) ;
263 		exit (1) ;
264 		} ;
265 
266 	cptr = memchr (buffer, target [0], rlen - slen) ;
267 	if (cptr && memcmp (cptr, target, slen) == 0)
268 		offset = cptr - buffer ;
269 	else
270 	{	printf ("Error : Could not find data offset.\n") ;
271 		exit (1) ;
272 		} ;
273 
274 	return offset + extra ;
275 } /* find_data_offset */
276 
277 static void
copy_data(int fd,SNDFILE * sndfile,int readsize)278 copy_data (int fd, SNDFILE * sndfile, int readsize)
279 {	static char * buffer ;
280 	sf_count_t readlen, count ;
281 	int bufferlen, done = 0 ;
282 
283 	bufferlen = readsize * 1024 ;
284 	buffer = malloc (bufferlen) ;
285 
286 	while (NOT (done) && (readlen = read (fd, buffer, bufferlen)) >= 0)
287 	{	if (readlen < bufferlen)
288 		{	readlen -= readlen % readsize ;
289 			done = 1 ;
290 			} ;
291 
292 		if ((count = sf_write_raw (sndfile, buffer, readlen)) != readlen)
293 		{	printf ("Error : sf_write_raw returned %" PRId64 " : %s\n", count, sf_strerror (sndfile)) ;
294 			return ;
295 			} ;
296 		} ;
297 
298 	free (buffer) ;
299 
300 	return ;
301 } /* copy_data */
302 
303