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