Bug Summary

File:libs/libsndfile/src/sndfile.c
Location:line 2627, column 3
Description:Value stored to 'error' is never read

Annotated Source Code

1/*
2** Copyright (C) 1999-2013 Erik de Castro Lopo <erikd@mega-nerd.com>
3**
4** This program is free software; you can redistribute it and/or modify
5** it under the terms of the GNU Lesser General Public License as published by
6** the Free Software Foundation; either version 2.1 of the License, or
7** (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12** GNU Lesser General Public License for more details.
13**
14** You should have received a copy of the GNU Lesser General Public License
15** along with this program; if not, write to the Free Software
16** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#include "sfconfig.h"
20
21#include <stdlib.h>
22#include <string.h>
23#include <ctype.h>
24#include <assert.h>
25
26#include "sndfile.h"
27#include "sfendian.h"
28#include "common.h"
29
30#define SNDFILE_MAGICK0x1234C0DE 0x1234C0DE
31
32#ifdef __APPLE__
33 /*
34 ** Detect if a compile for a universal binary is being attempted and barf if it is.
35 ** See the URL below for the rationale.
36 */
37 #ifdef __BIG_ENDIAN__
38 #if (CPU_IS_LITTLE_ENDIAN1 == 1)
39 #error "Universal binary compile detected. See http://www.mega-nerd.com/libsndfile/FAQ.html#Q018"
40 #endif
41 #endif
42
43 #ifdef __LITTLE_ENDIAN__1
44 #if (CPU_IS_BIG_ENDIAN0 == 1)
45 #error "Universal binary compile detected. See http://www.mega-nerd.com/libsndfile/FAQ.html#Q018"
46 #endif
47 #endif
48#endif
49
50
51typedef struct
52{ int error ;
53 const char *str ;
54} ErrorStruct ;
55
56static
57ErrorStruct SndfileErrors [] =
58{
59 /* Public error values and their associated strings. */
60 { SF_ERR_NO_ERROR , "No Error." },
61 { SF_ERR_UNRECOGNISED_FORMAT , "Format not recognised." },
62 { SF_ERR_SYSTEM , "System error." /* Often replaced. */ },
63 { SF_ERR_MALFORMED_FILE , "Supported file format but file is malformed." },
64 { SF_ERR_UNSUPPORTED_ENCODING , "Supported file format but unsupported encoding." },
65
66 /* Private error values and their associated strings. */
67 { SFE_ZERO_MAJOR_FORMAT , "Error : major format is 0." },
68 { SFE_ZERO_MINOR_FORMAT , "Error : minor format is 0." },
69 { SFE_BAD_FILE , "File does not exist or is not a regular file (possibly a pipe?)." },
70 { SFE_BAD_FILE_READ , "File exists but no data could be read." },
71 { SFE_OPEN_FAILED , "Could not open file." },
72 { SFE_BAD_SNDFILE_PTR , "Not a valid SNDFILE* pointer." },
73 { SFE_BAD_SF_INFO_PTR , "NULL SF_INFO pointer passed to libsndfile." },
74 { SFE_BAD_SF_INCOMPLETE , "SF_PRIVATE struct incomplete and end of header parsing." },
75 { SFE_BAD_FILE_PTR , "Bad FILE pointer." },
76 { SFE_BAD_INT_PTR , "Internal error, Bad pointer." },
77 { SFE_BAD_STAT_SIZE , "Error : software was misconfigured at compile time (sizeof statbuf.st_size)." },
78 { SFE_NO_TEMP_DIR , "Error : Could not file temp dir." },
79
80 { SFE_MALLOC_FAILED , "Internal malloc () failed." },
81 { SFE_UNIMPLEMENTED , "File contains data in an unimplemented format." },
82 { SFE_BAD_READ_ALIGN , "Attempt to read a non-integer number of channels." },
83 { SFE_BAD_WRITE_ALIGN , "Attempt to write a non-integer number of channels." },
84 { SFE_UNKNOWN_FORMAT , "File contains data in an unknown format." },
85 { SFE_NOT_READMODE , "Read attempted on file currently open for write." },
86 { SFE_NOT_WRITEMODE , "Write attempted on file currently open for read." },
87 { SFE_BAD_MODE_RW , "Error : This file format does not support read/write mode." },
88 { SFE_BAD_SF_INFO , "Internal error : SF_INFO struct incomplete." },
89 { SFE_BAD_OFFSET , "Error : supplied offset beyond end of file." },
90 { SFE_NO_EMBED_SUPPORT , "Error : embedding not supported for this file format." },
91 { SFE_NO_EMBEDDED_RDWR , "Error : cannot open embedded file read/write." },
92 { SFE_NO_PIPE_WRITE , "Error : this file format does not support pipe write." },
93 { SFE_BAD_VIRTUAL_IO , "Error : bad pointer on SF_VIRTUAL_IO struct." },
94 { SFE_BAD_BROADCAST_INFO_SIZE
95 , "Error : bad coding_history_size in SF_BROADCAST_INFO struct." },
96 { SFE_BAD_BROADCAST_INFO_TOO_BIG
97 , "Error : SF_BROADCAST_INFO struct too large." },
98 { SFE_BAD_CART_INFO_SIZE , "Error: SF_CART_INFO struct too large." },
99 { SFE_BAD_CART_INFO_TOO_BIG , "Error: bag tag_text_size in SF_CART_INFO struct." },
100 { SFE_INTERLEAVE_MODE , "Attempt to write to file with non-interleaved data." },
101 { SFE_INTERLEAVE_SEEK , "Bad karma in seek during interleave read operation." },
102 { SFE_INTERLEAVE_READ , "Bad karma in read during interleave read operation." },
103
104 { SFE_INTERNAL , "Unspecified internal error." },
105 { SFE_BAD_COMMAND_PARAM , "Bad parameter passed to function sf_command." },
106 { SFE_BAD_ENDIAN , "Bad endian-ness. Try default endian-ness" },
107 { SFE_CHANNEL_COUNT_ZERO , "Channel count is zero." },
108 { SFE_CHANNEL_COUNT , "Too many channels specified." },
109
110 { SFE_BAD_SEEK , "Internal psf_fseek() failed." },
111 { SFE_NOT_SEEKABLE , "Seek attempted on unseekable file type." },
112 { SFE_AMBIGUOUS_SEEK , "Error : combination of file open mode and seek command is ambiguous." },
113 { SFE_WRONG_SEEK , "Error : invalid seek parameters." },
114 { SFE_SEEK_FAILED , "Error : parameters OK, but psf_seek() failed." },
115
116 { SFE_BAD_OPEN_MODE , "Error : bad mode parameter for file open." },
117 { SFE_OPEN_PIPE_RDWR , "Error : attempt to open a pipe in read/write mode." },
118 { SFE_RDWR_POSITION , "Error on RDWR position (cryptic)." },
119 { SFE_RDWR_BAD_HEADER , "Error : Cannot open file in read/write mode due to string data in header." },
120 { SFE_CMD_HAS_DATA , "Error : Command fails because file already has audio data." },
121
122 { SFE_STR_NO_SUPPORT , "Error : File type does not support string data." },
123 { SFE_STR_NOT_WRITE , "Error : Trying to set a string when file is not in write mode." },
124 { SFE_STR_MAX_DATA , "Error : Maximum string data storage reached." },
125 { SFE_STR_MAX_COUNT , "Error : Maximum string data count reached." },
126 { SFE_STR_BAD_TYPE , "Error : Bad string data type." },
127 { SFE_STR_NO_ADD_END , "Error : file type does not support strings added at end of file." },
128 { SFE_STR_BAD_STRING , "Error : bad string." },
129 { SFE_STR_WEIRD , "Error : Weird string error." },
130
131 { SFE_WAV_NO_RIFF , "Error in WAV file. No 'RIFF' chunk marker." },
132 { SFE_WAV_NO_WAVE , "Error in WAV file. No 'WAVE' chunk marker." },
133 { SFE_WAV_NO_FMT , "Error in WAV/W64/RF64 file. No 'fmt ' chunk marker." },
134 { SFE_WAV_BAD_FMT , "Error in WAV/W64/RF64 file. Malformed 'fmt ' chunk." },
135 { SFE_WAV_FMT_SHORT , "Error in WAV/W64/RF64 file. Short 'fmt ' chunk." },
136
137 { SFE_WAV_BAD_FACT , "Error in WAV file. 'fact' chunk out of place." },
138 { SFE_WAV_BAD_PEAK , "Error in WAV file. Bad 'PEAK' chunk." },
139 { SFE_WAV_PEAK_B4_FMT , "Error in WAV file. 'PEAK' chunk found before 'fmt ' chunk." },
140
141 { SFE_WAV_BAD_FORMAT , "Error in WAV file. Errors in 'fmt ' chunk." },
142 { SFE_WAV_BAD_BLOCKALIGN , "Error in WAV file. Block alignment in 'fmt ' chunk is incorrect." },
143 { SFE_WAV_NO_DATA , "Error in WAV file. No 'data' chunk marker." },
144 { SFE_WAV_BAD_LIST , "Error in WAV file. Malformed LIST chunk." },
145 { SFE_WAV_UNKNOWN_CHUNK , "Error in WAV file. File contains an unknown chunk marker." },
146 { SFE_WAV_WVPK_DATA , "Error in WAV file. Data is in WAVPACK format." },
147
148 { SFE_WAV_ADPCM_NOT4BIT , "Error in ADPCM WAV file. Invalid bit width." },
149 { SFE_WAV_ADPCM_CHANNELS , "Error in ADPCM WAV file. Invalid number of channels." },
150 { SFE_WAV_GSM610_FORMAT , "Error in GSM610 WAV file. Invalid format chunk." },
151
152 { SFE_AIFF_NO_FORM , "Error in AIFF file, bad 'FORM' marker." },
153 { SFE_AIFF_AIFF_NO_FORM , "Error in AIFF file, 'AIFF' marker without 'FORM'." },
154 { SFE_AIFF_COMM_NO_FORM , "Error in AIFF file, 'COMM' marker without 'FORM'." },
155 { SFE_AIFF_SSND_NO_COMM , "Error in AIFF file, 'SSND' marker without 'COMM'." },
156 { SFE_AIFF_UNKNOWN_CHUNK , "Error in AIFF file, unknown chunk." },
157 { SFE_AIFF_COMM_CHUNK_SIZE, "Error in AIFF file, bad 'COMM' chunk size." },
158 { SFE_AIFF_BAD_COMM_CHUNK , "Error in AIFF file, bad 'COMM' chunk." },
159 { SFE_AIFF_PEAK_B4_COMM , "Error in AIFF file. 'PEAK' chunk found before 'COMM' chunk." },
160 { SFE_AIFF_BAD_PEAK , "Error in AIFF file. Bad 'PEAK' chunk." },
161 { SFE_AIFF_NO_SSND , "Error in AIFF file, bad 'SSND' chunk." },
162 { SFE_AIFF_NO_DATA , "Error in AIFF file, no sound data." },
163 { SFE_AIFF_RW_SSND_NOT_LAST, "Error in AIFF file, RDWR only possible if SSND chunk at end of file." },
164
165 { SFE_AU_UNKNOWN_FORMAT , "Error in AU file, unknown format." },
166 { SFE_AU_NO_DOTSND , "Error in AU file, missing '.snd' or 'dns.' marker." },
167 { SFE_AU_EMBED_BAD_LEN , "Embedded AU file with unknown length." },
168
169 { SFE_RAW_READ_BAD_SPEC , "Error while opening RAW file for read. Must specify format and channels.\n"
170 "Possibly trying to open unsupported format." },
171 { SFE_RAW_BAD_BITWIDTH , "Error. RAW file bitwidth must be a multiple of 8." },
172 { SFE_RAW_BAD_FORMAT , "Error. Bad format field in SF_INFO struct when openning a RAW file for read." },
173
174 { SFE_PAF_NO_MARKER , "Error in PAF file, no marker." },
175 { SFE_PAF_VERSION , "Error in PAF file, bad version." },
176 { SFE_PAF_UNKNOWN_FORMAT , "Error in PAF file, unknown format." },
177 { SFE_PAF_SHORT_HEADER , "Error in PAF file. File shorter than minimal header." },
178 { SFE_PAF_BAD_CHANNELS , "Error in PAF file. Bad channel count." },
179
180 { SFE_SVX_NO_FORM , "Error in 8SVX / 16SV file, no 'FORM' marker." },
181 { SFE_SVX_NO_BODY , "Error in 8SVX / 16SV file, no 'BODY' marker." },
182 { SFE_SVX_NO_DATA , "Error in 8SVX / 16SV file, no sound data." },
183 { SFE_SVX_BAD_COMP , "Error in 8SVX / 16SV file, unsupported compression format." },
184 { SFE_SVX_BAD_NAME_LENGTH , "Error in 8SVX / 16SV file, NAME chunk too long." },
185
186 { SFE_NIST_BAD_HEADER , "Error in NIST file, bad header." },
187 { SFE_NIST_CRLF_CONVERISON, "Error : NIST file damaged by Windows CR -> CRLF conversion process." },
188 { SFE_NIST_BAD_ENCODING , "Error in NIST file, unsupported compression format." },
189
190 { SFE_VOC_NO_CREATIVE , "Error in VOC file, no 'Creative Voice File' marker." },
191 { SFE_VOC_BAD_FORMAT , "Error in VOC file, bad format." },
192 { SFE_VOC_BAD_VERSION , "Error in VOC file, bad version number." },
193 { SFE_VOC_BAD_MARKER , "Error in VOC file, bad marker in file." },
194 { SFE_VOC_BAD_SECTIONS , "Error in VOC file, incompatible VOC sections." },
195 { SFE_VOC_MULTI_SAMPLERATE, "Error in VOC file, more than one sample rate defined." },
196 { SFE_VOC_MULTI_SECTION , "Unimplemented VOC file feature, file contains multiple sound sections." },
197 { SFE_VOC_MULTI_PARAM , "Error in VOC file, file contains multiple bit or channel widths." },
198 { SFE_VOC_SECTION_COUNT , "Error in VOC file, too many sections." },
199 { SFE_VOC_NO_PIPE , "Error : not able to operate on VOC files over a pipe." },
200
201 { SFE_IRCAM_NO_MARKER , "Error in IRCAM file, bad IRCAM marker." },
202 { SFE_IRCAM_BAD_CHANNELS , "Error in IRCAM file, bad channel count." },
203 { SFE_IRCAM_UNKNOWN_FORMAT, "Error in IRCAM file, unknow encoding format." },
204
205 { SFE_W64_64_BIT , "Error in W64 file, file contains 64 bit offset." },
206 { SFE_W64_NO_RIFF , "Error in W64 file. No 'riff' chunk marker." },
207 { SFE_W64_NO_WAVE , "Error in W64 file. No 'wave' chunk marker." },
208 { SFE_W64_NO_DATA , "Error in W64 file. No 'data' chunk marker." },
209 { SFE_W64_ADPCM_NOT4BIT , "Error in ADPCM W64 file. Invalid bit width." },
210 { SFE_W64_ADPCM_CHANNELS , "Error in ADPCM W64 file. Invalid number of channels." },
211 { SFE_W64_GSM610_FORMAT , "Error in GSM610 W64 file. Invalid format chunk." },
212
213 { SFE_MAT4_BAD_NAME , "Error in MAT4 file. No variable name." },
214 { SFE_MAT4_NO_SAMPLERATE , "Error in MAT4 file. No sample rate." },
215
216 { SFE_MAT5_BAD_ENDIAN , "Error in MAT5 file. Not able to determine endian-ness." },
217 { SFE_MAT5_NO_BLOCK , "Error in MAT5 file. Bad block structure." },
218 { SFE_MAT5_SAMPLE_RATE , "Error in MAT5 file. Not able to determine sample rate." },
219
220 { SFE_PVF_NO_PVF1 , "Error in PVF file. No PVF1 marker." },
221 { SFE_PVF_BAD_HEADER , "Error in PVF file. Bad header." },
222 { SFE_PVF_BAD_BITWIDTH , "Error in PVF file. Bad bit width." },
223
224 { SFE_XI_BAD_HEADER , "Error in XI file. Bad header." },
225 { SFE_XI_EXCESS_SAMPLES , "Error in XI file. Excess samples in file." },
226 { SFE_XI_NO_PIPE , "Error : not able to operate on XI files over a pipe." },
227
228 { SFE_HTK_NO_PIPE , "Error : not able to operate on HTK files over a pipe." },
229
230 { SFE_SDS_NOT_SDS , "Error : not an SDS file." },
231 { SFE_SDS_BAD_BIT_WIDTH , "Error : bad bit width for SDS file." },
232
233 { SFE_SD2_FD_DISALLOWED , "Error : cannot open SD2 file without a file name." },
234 { SFE_SD2_BAD_DATA_OFFSET , "Error : bad data offset." },
235 { SFE_SD2_BAD_MAP_OFFSET , "Error : bad map offset." },
236 { SFE_SD2_BAD_DATA_LENGTH , "Error : bad data length." },
237 { SFE_SD2_BAD_MAP_LENGTH , "Error : bad map length." },
238 { SFE_SD2_BAD_RSRC , "Error : bad resource fork." },
239 { SFE_SD2_BAD_SAMPLE_SIZE , "Error : bad sample size." },
240
241 { SFE_FLAC_BAD_HEADER , "Error : bad flac header." },
242 { SFE_FLAC_NEW_DECODER , "Error : problem while creating flac decoder." },
243 { SFE_FLAC_INIT_DECODER , "Error : problem while initialization of the flac decoder." },
244 { SFE_FLAC_LOST_SYNC , "Error : flac decoder lost sync." },
245 { SFE_FLAC_BAD_SAMPLE_RATE, "Error : flac does not support this sample rate." },
246 { SFE_FLAC_UNKOWN_ERROR , "Error : unknown error in flac decoder." },
247
248 { SFE_WVE_NOT_WVE , "Error : not a WVE file." },
249 { SFE_WVE_NO_PIPE , "Error : not able to operate on WVE files over a pipe." },
250
251 { SFE_DWVW_BAD_BITWIDTH , "Error : Bad bit width for DWVW encoding. Must be 12, 16 or 24." },
252 { SFE_G72X_NOT_MONO , "Error : G72x encoding does not support more than 1 channel." },
253
254 { SFE_VORBIS_ENCODER_BUG , "Error : Sample rate chosen is known to trigger a Vorbis encoder bug on this CPU." },
255
256 { SFE_RF64_NOT_RF64 , "Error : Not an RF64 file." },
257 { SFE_ALAC_FAIL_TMPFILE , "Error : Failed to open tmp file for ALAC encoding." },
258
259 { SFE_BAD_CHUNK_PTR , "Error : Bad SF_CHUNK_INFO pointer." },
260 { SFE_UNKNOWN_CHUNK , "Error : Unknown chunk marker." },
261 { SFE_BAD_CHUNK_FORMAT , "Error : Reading/writing chunks from this file format is not supported." },
262 { SFE_BAD_CHUNK_MARKER , "Error : Bad chunk marker." },
263 { SFE_BAD_CHUNK_DATA_PTR , "Error : Bad data pointer in SF_CHUNK_INFO struct." },
264
265 { SFE_MAX_ERROR , "Maximum error number." },
266 { SFE_MAX_ERROR + 1 , NULL((void*)0) }
267} ;
268
269/*------------------------------------------------------------------------------
270*/
271
272static int format_from_extension (SF_PRIVATE *psf) ;
273static int guess_file_type (SF_PRIVATE *psf) ;
274static int validate_sfinfo (SF_INFO *sfinfo) ;
275static int validate_psf (SF_PRIVATE *psf) ;
276static void save_header_info (SF_PRIVATE *psf) ;
277static void copy_filename (SF_PRIVATE *psf, const char *path) ;
278static int psf_close (SF_PRIVATE *psf) ;
279
280static int try_resource_fork (SF_PRIVATE * psf) ;
281
282/*------------------------------------------------------------------------------
283** Private (static) variables.
284*/
285
286int sf_errno = 0 ;
287static char sf_parselog [SF_BUFFER_LEN(8192)] = { 0 } ;
288static char sf_syserr [SF_SYSERR_LEN(256)] = { 0 } ;
289
290/*------------------------------------------------------------------------------
291*/
292
293#define VALIDATE_SNDFILE_AND_ASSIGN_PSF(a, b, c){ if ((a) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR ; return
0 ; } ; (b) = (SF_PRIVATE*) (a) ; if ((b)->virtual_io == SF_FALSE
&& psf_file_valid (b) == 0) { (b)->error = SFE_BAD_FILE_PTR
; return 0 ; } ; if ((b)->Magick != 0x1234C0DE) { (b)->
error = SFE_BAD_SNDFILE_PTR ; return 0 ; } ; if (c) (b)->error
= 0 ; }
\
294 { if ((a) == NULL((void*)0)) \
295 { sf_errno = SFE_BAD_SNDFILE_PTR ; \
296 return 0 ; \
297 } ; \
298 (b) = (SF_PRIVATE*) (a) ; \
299 if ((b)->virtual_io == SF_FALSE && \
300 psf_file_valid (b) == 0) \
301 { (b)->error = SFE_BAD_FILE_PTR ; \
302 return 0 ; \
303 } ; \
304 if ((b)->Magick != SNDFILE_MAGICK0x1234C0DE) \
305 { (b)->error = SFE_BAD_SNDFILE_PTR ; \
306 return 0 ; \
307 } ; \
308 if (c) (b)->error = 0 ; \
309 }
310
311/*------------------------------------------------------------------------------
312** Public functions.
313*/
314
315SNDFILE*
316sf_open (const char *path, int mode, SF_INFO *sfinfo)
317{ SF_PRIVATE *psf ;
318
319 /* Ultimate sanity check. */
320 assert (sizeof (sf_count_t) == 8)((sizeof (sf_count_t) == 8) ? (void) (0) : __assert_fail ("sizeof (sf_count_t) == 8"
, "sndfile.c", 320, __PRETTY_FUNCTION__))
;
321
322 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL((void*)0))
323 { sf_errno = SFE_MALLOC_FAILED ;
324 return NULL((void*)0) ;
325 } ;
326
327 psf_init_files (psf) ;
328
329 psf_log_printf (psf, "File : %s\n", path) ;
330
331 copy_filename (psf, path) ;
332
333 psf->file.mode = mode ;
334 if (strcmp (path, "-")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(path) && __builtin_constant_p ("-") && (__s1_len
= __builtin_strlen (path), __s2_len = __builtin_strlen ("-")
, (!((size_t)(const void *)((path) + 1) - (size_t)(const void
*)(path) == 1) || __s1_len >= 4) && (!((size_t)(const
void *)(("-") + 1) - (size_t)(const void *)("-") == 1) || __s2_len
>= 4)) ? __builtin_strcmp (path, "-") : (__builtin_constant_p
(path) && ((size_t)(const void *)((path) + 1) - (size_t
)(const void *)(path) == 1) && (__s1_len = __builtin_strlen
(path), __s1_len < 4) ? (__builtin_constant_p ("-") &&
((size_t)(const void *)(("-") + 1) - (size_t)(const void *)(
"-") == 1) ? __builtin_strcmp (path, "-") : (__extension__ ({
const unsigned char *__s2 = (const unsigned char *) (const char
*) ("-"); int __result = (((const unsigned char *) (const char
*) (path))[0] - __s2[0]); if (__s1_len > 0 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
path))[1] - __s2[1]); if (__s1_len > 1 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
path))[2] - __s2[2]); if (__s1_len > 2 && __result
== 0) __result = (((const unsigned char *) (const char *) (path
))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p (
"-") && ((size_t)(const void *)(("-") + 1) - (size_t)
(const void *)("-") == 1) && (__s2_len = __builtin_strlen
("-"), __s2_len < 4) ? (__builtin_constant_p (path) &&
((size_t)(const void *)((path) + 1) - (size_t)(const void *)
(path) == 1) ? __builtin_strcmp (path, "-") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (path); int __result = (((const unsigned char *) (const
char *) ("-"))[0] - __s2[0]); if (__s2_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("-"))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("-"))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) ("-"))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(path, "-")))); })
== 0)
335 psf->error = psf_set_stdio (psf) ;
336 else
337 psf->error = psf_fopen (psf) ;
338
339 return psf_open_file (psf, sfinfo) ;
340} /* sf_open */
341
342SNDFILE*
343sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc)
344{ SF_PRIVATE *psf ;
345
346 if ((SF_CONTAINER (sfinfo->format)((sfinfo->format) & SF_FORMAT_TYPEMASK)) == SF_FORMAT_SD2)
347 { sf_errno = SFE_SD2_FD_DISALLOWED ;
348 return NULL((void*)0) ;
349 } ;
350
351 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL((void*)0))
352 { sf_errno = SFE_MALLOC_FAILED ;
353 return NULL((void*)0) ;
354 } ;
355
356 psf_init_files (psf) ;
357 copy_filename (psf, "") ;
358
359 psf->file.mode = mode ;
360 psf_set_file (psf, fd) ;
361 psf->is_pipe = psf_is_pipe (psf) ;
362 psf->fileoffset = psf_ftell (psf) ;
363
364 if (! close_desc)
365 psf->file.do_not_close_descriptor = SF_TRUE ;
366
367 return psf_open_file (psf, sfinfo) ;
368} /* sf_open_fd */
369
370SNDFILE*
371sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data)
372{ SF_PRIVATE *psf ;
373
374 /* Make sure we have a valid set ot virtual pointers. */
375 if (sfvirtual->get_filelen == NULL((void*)0) || sfvirtual->seek == NULL((void*)0) || sfvirtual->tell == NULL((void*)0))
376 { sf_errno = SFE_BAD_VIRTUAL_IO ;
377 snprintf (sf_parselog, sizeof (sf_parselog), "Bad vio_get_filelen / vio_seek / vio_tell in SF_VIRTUAL_IO struct.\n")__builtin___snprintf_chk (sf_parselog, sizeof (sf_parselog), 2
- 1, __builtin_object_size (sf_parselog, 2 > 1), "Bad vio_get_filelen / vio_seek / vio_tell in SF_VIRTUAL_IO struct.\n"
)
;
378 return NULL((void*)0) ;
379 } ;
380
381 if ((mode == SFM_READ || mode == SFM_RDWR) && sfvirtual->read == NULL((void*)0))
382 { sf_errno = SFE_BAD_VIRTUAL_IO ;
383 snprintf (sf_parselog, sizeof (sf_parselog), "Bad vio_read in SF_VIRTUAL_IO struct.\n")__builtin___snprintf_chk (sf_parselog, sizeof (sf_parselog), 2
- 1, __builtin_object_size (sf_parselog, 2 > 1), "Bad vio_read in SF_VIRTUAL_IO struct.\n"
)
;
384 return NULL((void*)0) ;
385 } ;
386
387 if ((mode == SFM_WRITE || mode == SFM_RDWR) && sfvirtual->write == NULL((void*)0))
388 { sf_errno = SFE_BAD_VIRTUAL_IO ;
389 snprintf (sf_parselog, sizeof (sf_parselog), "Bad vio_write in SF_VIRTUAL_IO struct.\n")__builtin___snprintf_chk (sf_parselog, sizeof (sf_parselog), 2
- 1, __builtin_object_size (sf_parselog, 2 > 1), "Bad vio_write in SF_VIRTUAL_IO struct.\n"
)
;
390 return NULL((void*)0) ;
391 } ;
392
393 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL((void*)0))
394 { sf_errno = SFE_MALLOC_FAILED ;
395 return NULL((void*)0) ;
396 } ;
397
398 psf_init_files (psf) ;
399
400 psf->virtual_io = SF_TRUE ;
401 psf->vio = *sfvirtual ;
402 psf->vio_user_data = user_data ;
403
404 psf->file.mode = mode ;
405
406 return psf_open_file (psf, sfinfo) ;
407} /* sf_open_virtual */
408
409int
410sf_close (SNDFILE *sndfile)
411{ SF_PRIVATE *psf ;
412
413 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
414
415 return psf_close (psf) ;
416} /* sf_close */
417
418void
419sf_write_sync (SNDFILE *sndfile)
420{ SF_PRIVATE *psf ;
421
422 if ((psf = (SF_PRIVATE *) sndfile) == NULL((void*)0))
423 return ;
424
425 psf_fsync (psf) ;
426
427 return ;
428} /* sf_write_sync */
429
430/*==============================================================================
431*/
432
433const char*
434sf_error_number (int errnum)
435{ static const char *bad_errnum =
436 "No error defined for this error number. This is a bug in libsndfile." ;
437 int k ;
438
439 if (errnum == SFE_MAX_ERROR)
440 return SndfileErrors [0].str ;
441
442 if (errnum < 0 || errnum > SFE_MAX_ERROR)
443 { /* This really shouldn't happen in release versions. */
444 printf ("Not a valid error number (%d).\n", errnum)__printf_chk (2 - 1, "Not a valid error number (%d).\n", errnum
)
;
445 return bad_errnum ;
446 } ;
447
448 for (k = 0 ; SndfileErrors [k].str ; k++)
449 if (errnum == SndfileErrors [k].error)
450 return SndfileErrors [k].str ;
451
452 return bad_errnum ;
453} /* sf_error_number */
454
455const char*
456sf_strerror (SNDFILE *sndfile)
457{ SF_PRIVATE *psf = NULL((void*)0) ;
458 int errnum ;
459
460 if (sndfile == NULL((void*)0))
461 { errnum = sf_errno ;
462 if (errnum == SFE_SYSTEM && sf_syserr [0])
463 return sf_syserr ;
464 }
465 else
466 { psf = (SF_PRIVATE *) sndfile ;
467
468 if (psf->Magick != SNDFILE_MAGICK0x1234C0DE)
469 return "sf_strerror : Bad magic number." ;
470
471 errnum = psf->error ;
472
473 if (errnum == SFE_SYSTEM && psf->syserr [0])
474 return psf->syserr ;
475 } ;
476
477 return sf_error_number (errnum) ;
478} /* sf_strerror */
479
480/*------------------------------------------------------------------------------
481*/
482
483int
484sf_error (SNDFILE *sndfile)
485{ SF_PRIVATE *psf ;
486
487 if (sndfile == NULL((void*)0))
488 return sf_errno ;
489
490 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (0) (psf)->error = 0 ; }
;
491
492 if (psf->error)
493 return psf->error ;
494
495 return 0 ;
496} /* sf_error */
497
498/*------------------------------------------------------------------------------
499*/
500
501int
502sf_perror (SNDFILE *sndfile)
503{ SF_PRIVATE *psf ;
504 int errnum ;
505
506 if (sndfile == NULL((void*)0))
507 { errnum = sf_errno ;
508 }
509 else
510 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (0) (psf)->error = 0 ; }
;
511 errnum = psf->error ;
512 } ;
513
514 fprintf (stderr, "%s\n", sf_error_number (errnum))__fprintf_chk (stderr, 2 - 1, "%s\n", sf_error_number (errnum
))
;
515 return SFE_NO_ERROR ;
516} /* sf_perror */
517
518
519/*------------------------------------------------------------------------------
520*/
521
522int
523sf_error_str (SNDFILE *sndfile, char *str, size_t maxlen)
524{ SF_PRIVATE *psf ;
525 int errnum ;
526
527 if (str == NULL((void*)0))
528 return SFE_INTERNAL ;
529
530 if (sndfile == NULL((void*)0))
531 errnum = sf_errno ;
532 else
533 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (0) (psf)->error = 0 ; }
;
534 errnum = psf->error ;
535 } ;
536
537 snprintf (str, maxlen, "%s", sf_error_number (errnum))__builtin___snprintf_chk (str, maxlen, 2 - 1, __builtin_object_size
(str, 2 > 1), "%s", sf_error_number (errnum))
;
538
539 return SFE_NO_ERROR ;
540} /* sf_error_str */
541
542/*==============================================================================
543*/
544
545int
546sf_format_check (const SF_INFO *info)
547{ int subformat, endian ;
548
549 subformat = SF_CODEC (info->format)((info->format) & SF_FORMAT_SUBMASK) ;
550 endian = SF_ENDIAN (info->format)((info->format) & SF_FORMAT_ENDMASK) ;
551
552 /* This is the place where each file format can check if the suppiled
553 ** SF_INFO struct is valid.
554 ** Return 0 on failure, 1 ons success.
555 */
556
557 if (info->channels < 1 || info->channels > SF_MAX_CHANNELS256)
558 return 0 ;
559
560 if (info->samplerate < 0)
561 return 0 ;
562
563 switch (SF_CONTAINER (info->format)((info->format) & SF_FORMAT_TYPEMASK))
564 { case SF_FORMAT_WAV :
565 /* WAV now allows both endian, RIFF or RIFX (little or big respectively) */
566 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
567 return 1 ;
568 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
569 return 1 ;
570 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
571 return 1 ;
572 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
573 return 1 ;
574 if (subformat == SF_FORMAT_G721_32 && info->channels == 1)
575 return 1 ;
576 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
577 return 1 ;
578 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
579 return 1 ;
580 break ;
581
582 case SF_FORMAT_WAVEX :
583 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
584 return 0 ;
585 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
586 return 1 ;
587 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
588 return 1 ;
589 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
590 return 1 ;
591 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
592 return 1 ;
593 break ;
594
595 case SF_FORMAT_AIFF :
596 /* AIFF does allow both endian-nesses for PCM data.*/
597 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
598 return 1 ;
599 /* For other encodings reject any endian-ness setting. */
600 if (endian != 0)
601 return 0 ;
602 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8)
603 return 1 ;
604 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
605 return 1 ;
606 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
607 return 1 ;
608 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
609 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
610 return 1 ;
611 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
612 return 1 ;
613 if (subformat == SF_FORMAT_IMA_ADPCM && (info->channels == 1 || info->channels == 2))
614 return 1 ;
615 break ;
616
617 case SF_FORMAT_AU :
618 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
619 return 1 ;
620 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
621 return 1 ;
622 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
623 return 1 ;
624 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
625 return 1 ;
626 if (subformat == SF_FORMAT_G721_32 && info->channels == 1)
627 return 1 ;
628 if (subformat == SF_FORMAT_G723_24 && info->channels == 1)
629 return 1 ;
630 if (subformat == SF_FORMAT_G723_40 && info->channels == 1)
631 return 1 ;
632 break ;
633
634 case SF_FORMAT_CAF :
635 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
636 return 1 ;
637 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
638 return 1 ;
639 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
640 return 1 ;
641 if (subformat == SF_FORMAT_ALAC_16 || subformat == SF_FORMAT_ALAC_20)
642 return 1 ;
643 if (subformat == SF_FORMAT_ALAC_24 || subformat == SF_FORMAT_ALAC_32)
644 return 1 ;
645 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
646 return 1 ;
647 break ;
648
649 case SF_FORMAT_RAW :
650 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
651 return 1 ;
652 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
653 return 1 ;
654 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
655 return 1 ;
656 if (subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_ULAW)
657 return 1 ;
658 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
659 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
660 return 1 ;
661 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
662 return 1 ;
663 if (subformat == SF_FORMAT_VOX_ADPCM && info->channels == 1)
664 return 1 ;
665 break ;
666
667 case SF_FORMAT_PAF :
668 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
669 return 1 ;
670 break ;
671
672 case SF_FORMAT_SVX :
673 /* SVX only supports writing mono SVX files. */
674 if (info->channels > 1)
675 return 0 ;
676 /* Always big endian. */
677 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
678 return 0 ;
679
680 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
681 return 1 ;
682 break ;
683
684 case SF_FORMAT_NIST :
685 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
686 return 1 ;
687 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
688 return 1 ;
689 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
690 return 1 ;
691 break ;
692
693 case SF_FORMAT_IRCAM :
694 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
695 return 1 ;
696 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_FLOAT)
697 return 1 ;
698 break ;
699
700 case SF_FORMAT_VOC :
701 /* VOC is strictly little endian. */
702 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
703 return 0 ;
704 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
705 return 1 ;
706 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
707 return 1 ;
708 break ;
709
710 case SF_FORMAT_W64 :
711 /* W64 is strictly little endian. */
712 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
713 return 0 ;
714 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
715 return 1 ;
716 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
717 return 1 ;
718 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
719 return 1 ;
720 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
721 return 1 ;
722 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
723 return 1 ;
724 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
725 return 1 ;
726 break ;
727
728 case SF_FORMAT_MAT4 :
729 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
730 return 1 ;
731 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
732 return 1 ;
733 break ;
734
735 case SF_FORMAT_MAT5 :
736 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
737 return 1 ;
738 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
739 return 1 ;
740 break ;
741
742 case SF_FORMAT_PVF :
743 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
744 return 1 ;
745 break ;
746
747 case SF_FORMAT_XI :
748 if (info->channels != 1)
749 return 0 ;
750 if (subformat == SF_FORMAT_DPCM_8 || subformat == SF_FORMAT_DPCM_16)
751 return 1 ;
752 break ;
753
754 case SF_FORMAT_HTK :
755 /* HTK is strictly big endian. */
756 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
757 return 0 ;
758 if (info->channels != 1)
759 return 0 ;
760 if (subformat == SF_FORMAT_PCM_16)
761 return 1 ;
762 break ;
763
764 case SF_FORMAT_SDS :
765 /* SDS is strictly big endian. */
766 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
767 return 0 ;
768 if (info->channels != 1)
769 return 0 ;
770 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
771 return 1 ;
772 break ;
773
774 case SF_FORMAT_AVR :
775 /* SDS is strictly big endian. */
776 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
777 return 0 ;
778 if (info->channels > 2)
779 return 0 ;
780 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
781 return 1 ;
782 break ;
783
784 case SF_FORMAT_FLAC :
785 /* FLAC can't do more than 8 channels. */
786 if (info->channels > 8)
787 return 0 ;
788 if (endian != SF_ENDIAN_FILE)
789 return 0 ;
790 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
791 return 1 ;
792 break ;
793
794 case SF_FORMAT_SD2 :
795 /* SD2 is strictly big endian. */
796 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
797 return 0 ;
798 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
799 return 1 ;
800 break ;
801
802 case SF_FORMAT_WVE :
803 /* WVE is strictly big endian. */
804 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
805 return 0 ;
806 if (info->channels > 1)
807 return 0 ;
808 if (subformat == SF_FORMAT_ALAW)
809 return 1 ;
810 break ;
811
812 case SF_FORMAT_OGG :
813 if (endian != SF_ENDIAN_FILE)
814 return 0 ;
815 if (subformat == SF_FORMAT_VORBIS)
816 return 1 ;
817 break ;
818
819 case SF_FORMAT_MPC2K :
820 /* MPC2000 is strictly little endian. */
821 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
822 return 0 ;
823 if (info->channels > 2)
824 return 0 ;
825 if (subformat == SF_FORMAT_PCM_16)
826 return 1 ;
827 break ;
828
829 case SF_FORMAT_RF64 :
830 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
831 return 0 ;
832 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
833 return 1 ;
834 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
835 return 1 ;
836 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
837 return 1 ;
838 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
839 return 1 ;
840 break ;
841 default : break ;
842 } ;
843
844 return 0 ;
845} /* sf_format_check */
846
847/*------------------------------------------------------------------------------
848*/
849
850const char *
851sf_version_string (void)
852{
853#if ENABLE_EXPERIMENTAL_CODE0
854 return PACKAGE_NAME"libsndfile" "-" PACKAGE_VERSION"1.0.26pre5" "-exp" ;
855#else
856 return PACKAGE_NAME"libsndfile" "-" PACKAGE_VERSION"1.0.26pre5" ;
857#endif
858}
859
860
861/*------------------------------------------------------------------------------
862*/
863
864int
865sf_command (SNDFILE *sndfile, int command, void *data, int datasize)
866{ SF_PRIVATE *psf = (SF_PRIVATE *) sndfile ;
867 double quality ;
868 int old_value ;
869
870 /* This set of commands do not need the sndfile parameter. */
871 switch (command)
872 { case SFC_GET_LIB_VERSION :
873 if (data == NULL((void*)0))
874 { if (psf)
875 psf->error = SFE_BAD_COMMAND_PARAM ;
876 return SFE_BAD_COMMAND_PARAM ;
877 } ;
878 snprintf (data, datasize, "%s", sf_version_string ())__builtin___snprintf_chk (data, datasize, 2 - 1, __builtin_object_size
(data, 2 > 1), "%s", sf_version_string ())
;
879 return strlen (data) ;
880
881 case SFC_GET_SIMPLE_FORMAT_COUNT :
882 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (int)((int) sizeof (int)))
883 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
884 *((int*) data) = psf_get_format_simple_count () ;
885 return 0 ;
886
887 case SFC_GET_SIMPLE_FORMAT :
888 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO)((int) sizeof (SF_FORMAT_INFO)))
889 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
890 return psf_get_format_simple (data) ;
891
892 case SFC_GET_FORMAT_MAJOR_COUNT :
893 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (int)((int) sizeof (int)))
894 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
895 *((int*) data) = psf_get_format_major_count () ;
896 return 0 ;
897
898 case SFC_GET_FORMAT_MAJOR :
899 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO)((int) sizeof (SF_FORMAT_INFO)))
900 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
901 return psf_get_format_major (data) ;
902
903 case SFC_GET_FORMAT_SUBTYPE_COUNT :
904 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (int)((int) sizeof (int)))
905 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
906 *((int*) data) = psf_get_format_subtype_count () ;
907 return 0 ;
908
909 case SFC_GET_FORMAT_SUBTYPE :
910 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO)((int) sizeof (SF_FORMAT_INFO)))
911 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
912 return psf_get_format_subtype (data) ;
913
914 case SFC_GET_FORMAT_INFO :
915 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO)((int) sizeof (SF_FORMAT_INFO)))
916 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
917 return psf_get_format_info (data) ;
918 } ;
919
920 if (sndfile == NULL((void*)0) && command == SFC_GET_LOG_INFO)
921 { if (data == NULL((void*)0))
922 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
923 snprintf (data, datasize, "%s", sf_parselog)__builtin___snprintf_chk (data, datasize, 2 - 1, __builtin_object_size
(data, 2 > 1), "%s", sf_parselog)
;
924 return strlen (data) ;
925 } ;
926
927 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
928
929 switch (command)
930 { case SFC_SET_NORM_FLOAT :
931 old_value = psf->norm_float ;
932 psf->norm_float = (datasize) ? SF_TRUE : SF_FALSE ;
933 return old_value ;
934
935 case SFC_GET_CURRENT_SF_INFO :
936 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (SF_INFO)((int) sizeof (SF_INFO)))
937 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
938 memcpy (data, &psf->sf, sizeof (SF_INFO)) ;
939 break ;
940
941 case SFC_SET_NORM_DOUBLE :
942 old_value = psf->norm_double ;
943 psf->norm_double = (datasize) ? SF_TRUE : SF_FALSE ;
944 return old_value ;
945
946 case SFC_GET_NORM_FLOAT :
947 return psf->norm_float ;
948
949 case SFC_GET_NORM_DOUBLE :
950 return psf->norm_double ;
951
952 case SFC_SET_SCALE_FLOAT_INT_READ :
953 old_value = psf->float_int_mult ;
954
955 psf->float_int_mult = (datasize != 0) ? SF_TRUE : SF_FALSE ;
956 if (psf->float_int_mult && psf->float_max < 0.0)
957 psf->float_max = psf_calc_signal_max (psf, SF_FALSE) ;
958 return old_value ;
959
960 case SFC_SET_SCALE_INT_FLOAT_WRITE :
961 old_value = psf->scale_int_float ;
962 psf->scale_int_float = (datasize != 0) ? SF_TRUE : SF_FALSE ;
963 return old_value ;
964
965 case SFC_SET_ADD_PEAK_CHUNK :
966 { int format = SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK) ;
967
968 /* Only WAV and AIFF support the PEAK chunk. */
969 switch (format)
970 { case SF_FORMAT_AIFF :
971 case SF_FORMAT_CAF :
972 case SF_FORMAT_WAV :
973 case SF_FORMAT_WAVEX :
974 break ;
975
976 default :
977 return SF_FALSE ;
978 } ;
979
980 format = SF_CODEC (psf->sf.format)((psf->sf.format) & SF_FORMAT_SUBMASK) ;
981
982 /* Only files containg the following data types support the PEAK chunk. */
983 if (format != SF_FORMAT_FLOAT && format != SF_FORMAT_DOUBLE)
984 return SF_FALSE ;
985
986 } ;
987 /* Can only do this is in SFM_WRITE mode. */
988 if (psf->file.mode != SFM_WRITE && psf->file.mode != SFM_RDWR)
989 return SF_FALSE ;
990 /* If data has already been written this must fail. */
991 if (psf->have_written)
992 { psf->error = SFE_CMD_HAS_DATA ;
993 return SF_FALSE ;
994 } ;
995 /* Everything seems OK, so set psf->has_peak and re-write header. */
996 if (datasize == SF_FALSE && psf->peak_info != NULL((void*)0))
997 { free (psf->peak_info) ;
998 psf->peak_info = NULL((void*)0) ;
999 }
1000 else if (psf->peak_info == NULL((void*)0))
1001 { psf->peak_info = peak_info_calloc (psf->sf.channels) ;
1002 if (psf->peak_info != NULL((void*)0))
1003 psf->peak_info->peak_loc = SF_PEAK_START ;
1004 } ;
1005
1006 if (psf->write_header)
1007 psf->write_header (psf, SF_TRUE) ;
1008 return datasize ;
1009
1010 case SFC_SET_ADD_HEADER_PAD_CHUNK :
1011 return SF_FALSE ;
1012
1013 case SFC_GET_LOG_INFO :
1014 if (data == NULL((void*)0))
1015 return SFE_BAD_COMMAND_PARAM ;
1016 snprintf (data, datasize, "%s", psf->parselog.buf)__builtin___snprintf_chk (data, datasize, 2 - 1, __builtin_object_size
(data, 2 > 1), "%s", psf->parselog.buf)
;
1017 break ;
1018
1019 case SFC_CALC_SIGNAL_MAX :
1020 if (data == NULL((void*)0) || datasize != sizeof (double))
1021 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1022 *((double*) data) = psf_calc_signal_max (psf, SF_FALSE) ;
1023 break ;
1024
1025 case SFC_CALC_NORM_SIGNAL_MAX :
1026 if (data == NULL((void*)0) || datasize != sizeof (double))
1027 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1028 *((double*) data) = psf_calc_signal_max (psf, SF_TRUE) ;
1029 break ;
1030
1031 case SFC_CALC_MAX_ALL_CHANNELS :
1032 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (double)((int) sizeof (double)) * psf->sf.channels)
1033 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1034 return psf_calc_max_all_channels (psf, (double*) data, SF_FALSE) ;
1035
1036 case SFC_CALC_NORM_MAX_ALL_CHANNELS :
1037 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (double)((int) sizeof (double)) * psf->sf.channels)
1038 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1039 return psf_calc_max_all_channels (psf, (double*) data, SF_TRUE) ;
1040
1041 case SFC_GET_SIGNAL_MAX :
1042 if (data == NULL((void*)0) || datasize != sizeof (double))
1043 { psf->error = SFE_BAD_COMMAND_PARAM ;
1044 return SF_FALSE ;
1045 } ;
1046 return psf_get_signal_max (psf, (double *) data) ;
1047
1048 case SFC_GET_MAX_ALL_CHANNELS :
1049 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (double)((int) sizeof (double)) * psf->sf.channels)
1050 { psf->error = SFE_BAD_COMMAND_PARAM ;
1051 return SF_FALSE ;
1052 } ;
1053 return psf_get_max_all_channels (psf, (double*) data) ;
1054
1055 case SFC_UPDATE_HEADER_NOW :
1056 if (psf->write_header)
1057 psf->write_header (psf, SF_TRUE) ;
1058 break ;
1059
1060 case SFC_SET_UPDATE_HEADER_AUTO :
1061 psf->auto_header = datasize ? SF_TRUE : SF_FALSE ;
1062 return psf->auto_header ;
1063 break ;
1064
1065 case SFC_SET_ADD_DITHER_ON_WRITE :
1066 case SFC_SET_ADD_DITHER_ON_READ :
1067 /*
1068 ** FIXME !
1069 ** These are obsolete. Just return.
1070 ** Remove some time after version 1.0.8.
1071 */
1072 break ;
1073
1074 case SFC_SET_DITHER_ON_WRITE :
1075 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (SF_DITHER_INFO)((int) sizeof (SF_DITHER_INFO)))
1076 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1077 memcpy (&psf->write_dither, data, sizeof (psf->write_dither)) ;
1078 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1079 dither_init (psf, SFM_WRITE) ;
1080 break ;
1081
1082 case SFC_SET_DITHER_ON_READ :
1083 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (SF_DITHER_INFO)((int) sizeof (SF_DITHER_INFO)))
1084 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1085 memcpy (&psf->read_dither, data, sizeof (psf->read_dither)) ;
1086 if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
1087 dither_init (psf, SFM_READ) ;
1088 break ;
1089
1090 case SFC_FILE_TRUNCATE :
1091 if (psf->file.mode != SFM_WRITE && psf->file.mode != SFM_RDWR)
1092 return SF_TRUE ;
1093 if (datasize != sizeof (sf_count_t))
1094 return SF_TRUE ;
1095 if (data == NULL((void*)0) || datasize != sizeof (sf_count_t))
1096 { psf->error = SFE_BAD_COMMAND_PARAM ;
1097 return SF_FALSE ;
1098 }
1099 else
1100 { sf_count_t position ;
1101
1102 position = *((sf_count_t*) data) ;
1103
1104 if (sf_seek (sndfile, position, SEEK_SET0) != position)
1105 return SF_TRUE ;
1106
1107 psf->sf.frames = position ;
1108
1109 position = psf_fseek (psf, 0, SEEK_CUR1) ;
1110
1111 return psf_ftruncate (psf, position) ;
1112 } ;
1113 break ;
1114
1115 case SFC_SET_RAW_START_OFFSET :
1116 if (data == NULL((void*)0) || datasize != sizeof (sf_count_t))
1117 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1118
1119 if ((SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK)) != SF_FORMAT_RAW)
1120 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1121
1122 psf->dataoffset = *((sf_count_t*) data) ;
1123 sf_seek (sndfile, 0, SEEK_CUR1) ;
1124 break ;
1125
1126 case SFC_GET_EMBED_FILE_INFO :
1127 if (data == NULL((void*)0) || datasize != sizeof (SF_EMBED_FILE_INFO))
1128 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1129
1130 ((SF_EMBED_FILE_INFO*) data)->offset = psf->fileoffset ;
1131 ((SF_EMBED_FILE_INFO*) data)->length = psf->filelength ;
1132 break ;
1133
1134 /* Lite remove start */
1135 case SFC_TEST_IEEE_FLOAT_REPLACE :
1136 psf->ieee_replace = (datasize) ? SF_TRUE : SF_FALSE ;
1137 if ((SF_CODEC (psf->sf.format)((psf->sf.format) & SF_FORMAT_SUBMASK)) == SF_FORMAT_FLOAT)
1138 float32_init (psf) ;
1139 else if ((SF_CODEC (psf->sf.format)((psf->sf.format) & SF_FORMAT_SUBMASK)) == SF_FORMAT_DOUBLE)
1140 double64_init (psf) ;
1141 else
1142 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1143 break ;
1144 /* Lite remove end */
1145
1146 case SFC_SET_CLIPPING :
1147 psf->add_clipping = (datasize) ? SF_TRUE : SF_FALSE ;
1148 return psf->add_clipping ;
1149
1150 case SFC_GET_CLIPPING :
1151 return psf->add_clipping ;
1152
1153 case SFC_GET_LOOP_INFO :
1154 if (datasize != sizeof (SF_LOOP_INFO) || data == NULL((void*)0))
1155 { psf->error = SFE_BAD_COMMAND_PARAM ;
1156 return SF_FALSE ;
1157 } ;
1158 if (psf->loop_info == NULL((void*)0))
1159 return SF_FALSE ;
1160 memcpy (data, psf->loop_info, sizeof (SF_LOOP_INFO)) ;
1161 return SF_TRUE ;
1162
1163 case SFC_SET_BROADCAST_INFO :
1164 { int format = SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK) ;
1165
1166 /* Only WAV and RF64 supports the BEXT (Broadcast) chunk. */
1167 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX && format != SF_FORMAT_RF64)
1168 return SF_FALSE ;
1169 } ;
1170
1171 /* Only makes sense in SFM_WRITE or SFM_RDWR mode. */
1172 if ((psf->file.mode != SFM_WRITE) && (psf->file.mode != SFM_RDWR))
1173 return SF_FALSE ;
1174 /* If data has already been written this must fail. */
1175 if (psf->broadcast_16k == NULL((void*)0) && psf->have_written)
1176 { psf->error = SFE_CMD_HAS_DATA ;
1177 return SF_FALSE ;
1178 } ;
1179
1180 if (NOT (broadcast_var_set (psf, data, datasize))(! (broadcast_var_set (psf, data, datasize))))
1181 return SF_FALSE ;
1182
1183 if (psf->write_header)
1184 psf->write_header (psf, SF_TRUE) ;
1185 return SF_TRUE ;
1186
1187 case SFC_GET_BROADCAST_INFO :
1188 if (data == NULL((void*)0))
1189 { psf->error = SFE_BAD_COMMAND_PARAM ;
1190 return SF_FALSE ;
1191 } ;
1192 return broadcast_var_get (psf, data, datasize) ;
1193
1194 case SFC_SET_CART_INFO :
1195 { int format = SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK) ;
1196 /* Only WAV and RF64 support cart chunk format */
1197 if (format != SF_FORMAT_WAV && format != SF_FORMAT_RF64)
1198 return SF_FALSE ;
1199 } ;
1200
1201 /* Only makes sense in SFM_WRITE or SFM_RDWR mode */
1202 if ((psf->file.mode != SFM_WRITE) && (psf->file.mode != SFM_RDWR))
1203 return SF_FALSE ;
1204 /* If data has already been written this must fail. */
1205 if (psf->cart_16k == NULL((void*)0) && psf->have_written)
1206 { psf->error = SFE_CMD_HAS_DATA ;
1207 return SF_FALSE ;
1208 } ;
1209 if (NOT (cart_var_set (psf, data, datasize))(! (cart_var_set (psf, data, datasize))))
1210 return SF_FALSE ;
1211 if (psf->write_header)
1212 psf->write_header (psf, SF_TRUE) ;
1213 return SF_TRUE ;
1214
1215 case SFC_GET_CART_INFO :
1216 if (data == NULL((void*)0))
1217 { psf->error = SFE_BAD_COMMAND_PARAM ;
1218 return SF_FALSE ;
1219 } ;
1220 return cart_var_get (psf, data, datasize) ;
1221
1222 case SFC_GET_INSTRUMENT :
1223 if (datasize != sizeof (SF_INSTRUMENT) || data == NULL((void*)0))
1224 { psf->error = SFE_BAD_COMMAND_PARAM ;
1225 return SF_FALSE ;
1226 } ;
1227 if (psf->instrument == NULL((void*)0))
1228 return SF_FALSE ;
1229 memcpy (data, psf->instrument, sizeof (SF_INSTRUMENT)) ;
1230 return SF_TRUE ;
1231
1232 case SFC_SET_INSTRUMENT :
1233 /* If data has already been written this must fail. */
1234 if (psf->have_written)
1235 { psf->error = SFE_CMD_HAS_DATA ;
1236 return SF_FALSE ;
1237 } ;
1238 if (datasize != sizeof (SF_INSTRUMENT) || data == NULL((void*)0))
1239 { psf->error = SFE_BAD_COMMAND_PARAM ;
1240 return SF_FALSE ;
1241 } ;
1242
1243 if (psf->instrument == NULL((void*)0) && (psf->instrument = psf_instrument_alloc ()) == NULL((void*)0))
1244 { psf->error = SFE_MALLOC_FAILED ;
1245 return SF_FALSE ;
1246 } ;
1247 memcpy (psf->instrument, data, sizeof (SF_INSTRUMENT)) ;
1248 return SF_TRUE ;
1249
1250 case SFC_RAW_DATA_NEEDS_ENDSWAP :
1251 return psf->data_endswap ;
1252
1253 case SFC_GET_CHANNEL_MAP_INFO :
1254 if (psf->channel_map == NULL((void*)0))
1255 return SF_FALSE ;
1256
1257 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (psf->channel_map [0])((int) sizeof (psf->channel_map [0])) * psf->sf.channels)
1258 { psf->error = SFE_BAD_COMMAND_PARAM ;
1259 return SF_FALSE ;
1260 } ;
1261
1262 memcpy (data, psf->channel_map, datasize) ;
1263 return SF_TRUE ;
1264
1265 case SFC_SET_CHANNEL_MAP_INFO :
1266 if (psf->have_written)
1267 { psf->error = SFE_CMD_HAS_DATA ;
1268 return SF_FALSE ;
1269 } ;
1270 if (data == NULL((void*)0) || datasize != SIGNED_SIZEOF (psf->channel_map [0])((int) sizeof (psf->channel_map [0])) * psf->sf.channels)
1271 { psf->error = SFE_BAD_COMMAND_PARAM ;
1272 return SF_FALSE ;
1273 } ;
1274
1275 { int *iptr ;
1276
1277 for (iptr = data ; iptr < (int*) data + psf->sf.channels ; iptr++)
1278 { if (*iptr <= SF_CHANNEL_MAP_INVALID || *iptr >= SF_CHANNEL_MAP_MAX)
1279 { psf->error = SFE_BAD_COMMAND_PARAM ;
1280 return SF_FALSE ;
1281 } ;
1282 } ;
1283 } ;
1284
1285 free (psf->channel_map) ;
1286 if ((psf->channel_map = malloc (datasize)) == NULL((void*)0))
1287 { psf->error = SFE_MALLOC_FAILED ;
1288 return SF_FALSE ;
1289 } ;
1290
1291 memcpy (psf->channel_map, data, datasize) ;
1292
1293 /*
1294 ** Pass the command down to the container's command handler.
1295 ** Don't pass user data, use validated psf->channel_map data instead.
1296 */
1297 if (psf->command)
1298 return psf->command (psf, command, NULL((void*)0), 0) ;
1299 return SF_FALSE ;
1300
1301 case SFC_SET_VBR_ENCODING_QUALITY :
1302 if (data == NULL((void*)0) || datasize != sizeof (double))
1303 return SF_FALSE ;
1304
1305 quality = *((double *) data) ;
1306 quality = 1.0 - SF_MAX (0.0, SF_MIN (1.0, quality))({ typeof (0.0) sf_max_x1 = (0.0) ; typeof (({ typeof (1.0) sf_min_x2
= (1.0) ; typeof (quality) sf_min_y2 = (quality) ; (void) (&
sf_min_x2 == &sf_min_y2) ; sf_min_x2 < sf_min_y2 ? sf_min_x2
: sf_min_y2 ; })) sf_max_y1 = (({ typeof (1.0) sf_min_x2 = (
1.0) ; typeof (quality) sf_min_y2 = (quality) ; (void) (&
sf_min_x2 == &sf_min_y2) ; sf_min_x2 < sf_min_y2 ? sf_min_x2
: sf_min_y2 ; })) ; (void) (&sf_max_x1 == &sf_max_y1
) ; sf_max_x1 > sf_max_y1 ? sf_max_x1 : sf_max_y1 ; })
;
1307 return sf_command (sndfile, SFC_SET_COMPRESSION_LEVEL, &quality, sizeof (quality)) ;
1308
1309
1310 default :
1311 /* Must be a file specific command. Pass it on. */
1312 if (psf->command)
1313 return psf->command (psf, command, data, datasize) ;
1314
1315 psf_log_printf (psf, "*** sf_command : cmd = 0x%X\n", command) ;
1316 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1317 } ;
1318
1319 return 0 ;
1320} /* sf_command */
1321
1322/*------------------------------------------------------------------------------
1323*/
1324
1325sf_count_t
1326sf_seek (SNDFILE *sndfile, sf_count_t offset, int whence)
1327{ SF_PRIVATE *psf ;
1328 sf_count_t seek_from_start = 0, retval ;
1329
1330 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1331
1332 if (! psf->sf.seekable)
1333 { psf->error = SFE_NOT_SEEKABLE ;
1334 return PSF_SEEK_ERROR((sf_count_t) -1) ;
1335 } ;
1336
1337 /* If the whence parameter has a mode ORed in, check to see that
1338 ** it makes sense.
1339 */
1340 if (((whence & SFM_MASK(SFM_READ | SFM_WRITE | SFM_RDWR)) == SFM_WRITE && psf->file.mode == SFM_READ) ||
1341 ((whence & SFM_MASK(SFM_READ | SFM_WRITE | SFM_RDWR)) == SFM_READ && psf->file.mode == SFM_WRITE))
1342 { psf->error = SFE_WRONG_SEEK ;
1343 return PSF_SEEK_ERROR((sf_count_t) -1) ;
1344 } ;
1345
1346 /* Convert all SEEK_CUR and SEEK_END into seek_from_start to be
1347 ** used with SEEK_SET.
1348 */
1349 switch (whence)
1350 { /* The SEEK_SET behaviour is independant of mode. */
1351 case SEEK_SET0 :
1352 case SEEK_SET0 | SFM_READ :
1353 case SEEK_SET0 | SFM_WRITE :
1354 case SEEK_SET0 | SFM_RDWR :
1355 seek_from_start = offset ;
1356 break ;
1357
1358 /* The SEEK_CUR is a little more tricky. */
1359 case SEEK_CUR1 :
1360 if (offset == 0)
1361 { if (psf->file.mode == SFM_READ)
1362 return psf->read_current ;
1363 if (psf->file.mode == SFM_WRITE)
1364 return psf->write_current ;
1365 } ;
1366 if (psf->file.mode == SFM_READ)
1367 seek_from_start = psf->read_current + offset ;
1368 else if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1369 seek_from_start = psf->write_current + offset ;
1370 else
1371 psf->error = SFE_AMBIGUOUS_SEEK ;
1372 break ;
1373
1374 case SEEK_CUR1 | SFM_READ :
1375 if (offset == 0)
1376 return psf->read_current ;
1377 seek_from_start = psf->read_current + offset ;
1378 break ;
1379
1380 case SEEK_CUR1 | SFM_WRITE :
1381 if (offset == 0)
1382 return psf->write_current ;
1383 seek_from_start = psf->write_current + offset ;
1384 break ;
1385
1386 /* The SEEK_END */
1387 case SEEK_END2 :
1388 case SEEK_END2 | SFM_READ :
1389 case SEEK_END2 | SFM_WRITE :
1390 seek_from_start = psf->sf.frames + offset ;
1391 break ;
1392
1393 default :
1394 psf->error = SFE_BAD_SEEK ;
1395 break ;
1396 } ;
1397
1398 if (psf->error)
1399 return PSF_SEEK_ERROR((sf_count_t) -1) ;
1400
1401 if (psf->file.mode == SFM_RDWR || psf->file.mode == SFM_WRITE)
1402 { if (seek_from_start < 0)
1403 { psf->error = SFE_BAD_SEEK ;
1404 return PSF_SEEK_ERROR((sf_count_t) -1) ;
1405 } ;
1406 }
1407 else if (seek_from_start < 0 || seek_from_start > psf->sf.frames)
1408 { psf->error = SFE_BAD_SEEK ;
1409 return PSF_SEEK_ERROR((sf_count_t) -1) ;
1410 } ;
1411
1412 if (psf->seek)
1413 { int new_mode = (whence & SFM_MASK(SFM_READ | SFM_WRITE | SFM_RDWR)) ? (whence & SFM_MASK(SFM_READ | SFM_WRITE | SFM_RDWR)) : psf->file.mode ;
1414
1415 retval = psf->seek (psf, new_mode, seek_from_start) ;
1416
1417 switch (new_mode)
1418 { case SFM_READ :
1419 psf->read_current = retval ;
1420 break ;
1421 case SFM_WRITE :
1422 psf->write_current = retval ;
1423 break ;
1424 case SFM_RDWR :
1425 psf->read_current = retval ;
1426 psf->write_current = retval ;
1427 new_mode = SFM_READ ;
1428 break ;
1429 } ;
1430
1431 psf->last_op = new_mode ;
1432
1433 return retval ;
1434 } ;
1435
1436 psf->error = SFE_AMBIGUOUS_SEEK ;
1437 return PSF_SEEK_ERROR((sf_count_t) -1) ;
1438} /* sf_seek */
1439
1440/*------------------------------------------------------------------------------
1441*/
1442
1443const char*
1444sf_get_string (SNDFILE *sndfile, int str_type)
1445{ SF_PRIVATE *psf ;
1446
1447 if ((psf = (SF_PRIVATE*) sndfile) == NULL((void*)0))
1448 return NULL((void*)0) ;
1449 if (psf->Magick != SNDFILE_MAGICK0x1234C0DE)
1450 return NULL((void*)0) ;
1451
1452 return psf_get_string (psf, str_type) ;
1453} /* sf_get_string */
1454
1455int
1456sf_set_string (SNDFILE *sndfile, int str_type, const char* str)
1457{ SF_PRIVATE *psf ;
1458
1459 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1460
1461 return psf_set_string (psf, str_type, str) ;
1462} /* sf_get_string */
1463
1464/*------------------------------------------------------------------------------
1465*/
1466
1467int
1468sf_current_byterate (SNDFILE *sndfile)
1469{ SF_PRIVATE *psf ;
1470
1471 if ((psf = (SF_PRIVATE*) sndfile) == NULL((void*)0))
1472 return -1 ;
1473 if (psf->Magick != SNDFILE_MAGICK0x1234C0DE)
1474 return -1 ;
1475
1476 /* This should cover all PCM and floating point formats. */
1477 if (psf->bytewidth)
1478 return psf->sf.samplerate * psf->sf.channels * psf->bytewidth ;
1479
1480 if (psf->byterate)
1481 return psf->byterate (psf) ;
1482
1483 switch (SF_CODEC (psf->sf.format)((psf->sf.format) & SF_FORMAT_SUBMASK))
1484 { case SF_FORMAT_IMA_ADPCM :
1485 case SF_FORMAT_MS_ADPCM :
1486 case SF_FORMAT_VOX_ADPCM :
1487 return (psf->sf.samplerate * psf->sf.channels) / 2 ;
1488
1489 case SF_FORMAT_GSM610 :
1490 return (psf->sf.samplerate * psf->sf.channels * 13000) / 8000 ;
1491
1492 case SF_FORMAT_G721_32 : /* 32kbs G721 ADPCM encoding. */
1493 return (psf->sf.samplerate * psf->sf.channels) / 2 ;
1494
1495 case SF_FORMAT_G723_24 : /* 24kbs G723 ADPCM encoding. */
1496 return (psf->sf.samplerate * psf->sf.channels * 3) / 8 ;
1497
1498 case SF_FORMAT_G723_40 : /* 40kbs G723 ADPCM encoding. */
1499 return (psf->sf.samplerate * psf->sf.channels * 5) / 8 ;
1500
1501 default :
1502 break ;
1503 } ;
1504
1505 return -1 ;
1506} /* sf_current_byterate */
1507
1508/*==============================================================================
1509*/
1510
1511sf_count_t
1512sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes)
1513{ SF_PRIVATE *psf ;
1514 sf_count_t count, extra ;
1515 int bytewidth, blockwidth ;
1516
1517 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1518
1519 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
1520 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
1521
1522 if (psf->file.mode == SFM_WRITE)
1523 { psf->error = SFE_NOT_READMODE ;
1524 return 0 ;
1525 } ;
1526
1527 if (bytes < 0 || psf->read_current >= psf->sf.frames)
1528 { psf_memset (ptr, 0, bytes) ;
1529 return 0 ;
1530 } ;
1531
1532 if (bytes % (psf->sf.channels * bytewidth))
1533 { psf->error = SFE_BAD_READ_ALIGN ;
1534 return 0 ;
1535 } ;
1536
1537 if (psf->last_op != SFM_READ)
1538 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1539 return 0 ;
1540
1541 count = psf_fread (ptr, 1, bytes, psf) ;
1542
1543 if (psf->read_current + count / blockwidth <= psf->sf.frames)
1544 psf->read_current += count / blockwidth ;
1545 else
1546 { count = (psf->sf.frames - psf->read_current) * blockwidth ;
1547 extra = bytes - count ;
1548 psf_memset (((char *) ptr) + count, 0, extra) ;
1549 psf->read_current = psf->sf.frames ;
1550 } ;
1551
1552 psf->last_op = SFM_READ ;
1553
1554 return count ;
1555} /* sf_read_raw */
1556
1557/*------------------------------------------------------------------------------
1558*/
1559
1560sf_count_t
1561sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t len)
1562{ SF_PRIVATE *psf ;
1563 sf_count_t count, extra ;
1564
1565 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1566
1567 if (psf->file.mode == SFM_WRITE)
1568 { psf->error = SFE_NOT_READMODE ;
1569 return 0 ;
1570 } ;
1571
1572 if (len % psf->sf.channels)
1573 { psf->error = SFE_BAD_READ_ALIGN ;
1574 return 0 ;
1575 } ;
1576
1577 if (len <= 0 || psf->read_current >= psf->sf.frames)
1578 { psf_memset (ptr, 0, len * sizeof (short)) ;
1579 return 0 ; /* End of file. */
1580 } ;
1581
1582 if (psf->read_short == NULL((void*)0) || psf->seek == NULL((void*)0))
1583 { psf->error = SFE_UNIMPLEMENTED ;
1584 return 0 ;
1585 } ;
1586
1587 if (psf->last_op != SFM_READ)
1588 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1589 return 0 ;
1590
1591 count = psf->read_short (psf, ptr, len) ;
1592
1593 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1594 psf->read_current += count / psf->sf.channels ;
1595 else
1596 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1597 extra = len - count ;
1598 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
1599 psf->read_current = psf->sf.frames ;
1600 } ;
1601
1602 psf->last_op = SFM_READ ;
1603
1604 return count ;
1605} /* sf_read_short */
1606
1607sf_count_t
1608sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)
1609{ SF_PRIVATE *psf ;
1610 sf_count_t count, extra ;
1611
1612 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1613
1614 if (psf->file.mode == SFM_WRITE)
1615 { psf->error = SFE_NOT_READMODE ;
1616 return 0 ;
1617 } ;
1618
1619 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1620 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (short)) ;
1621 return 0 ; /* End of file. */
1622 } ;
1623
1624 if (psf->read_short == NULL((void*)0) || psf->seek == NULL((void*)0))
1625 { psf->error = SFE_UNIMPLEMENTED ;
1626 return 0 ;
1627 } ;
1628
1629 if (psf->last_op != SFM_READ)
1630 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1631 return 0 ;
1632
1633 count = psf->read_short (psf, ptr, frames * psf->sf.channels) ;
1634
1635 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1636 psf->read_current += count / psf->sf.channels ;
1637 else
1638 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1639 extra = frames * psf->sf.channels - count ;
1640 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
1641 psf->read_current = psf->sf.frames ;
1642 } ;
1643
1644 psf->last_op = SFM_READ ;
1645
1646 return count / psf->sf.channels ;
1647} /* sf_readf_short */
1648
1649/*------------------------------------------------------------------------------
1650*/
1651
1652sf_count_t
1653sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t len)
1654{ SF_PRIVATE *psf ;
1655 sf_count_t count, extra ;
1656
1657 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1658
1659 if (psf->file.mode == SFM_WRITE)
1660 { psf->error = SFE_NOT_READMODE ;
1661 return 0 ;
1662 } ;
1663
1664 if (len % psf->sf.channels)
1665 { psf->error = SFE_BAD_READ_ALIGN ;
1666 return 0 ;
1667 } ;
1668
1669 if (len <= 0 || psf->read_current >= psf->sf.frames)
1670 { psf_memset (ptr, 0, len * sizeof (int)) ;
1671 return 0 ;
1672 } ;
1673
1674 if (psf->read_int == NULL((void*)0) || psf->seek == NULL((void*)0))
1675 { psf->error = SFE_UNIMPLEMENTED ;
1676 return 0 ;
1677 } ;
1678
1679 if (psf->last_op != SFM_READ)
1680 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1681 return 0 ;
1682
1683 count = psf->read_int (psf, ptr, len) ;
1684
1685 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1686 psf->read_current += count / psf->sf.channels ;
1687 else
1688 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1689 extra = len - count ;
1690 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
1691 psf->read_current = psf->sf.frames ;
1692 } ;
1693
1694 psf->last_op = SFM_READ ;
1695
1696 return count ;
1697} /* sf_read_int */
1698
1699sf_count_t
1700sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames)
1701{ SF_PRIVATE *psf ;
1702 sf_count_t count, extra ;
1703
1704 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1705
1706 if (psf->file.mode == SFM_WRITE)
1707 { psf->error = SFE_NOT_READMODE ;
1708 return 0 ;
1709 } ;
1710
1711 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1712 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (int)) ;
1713 return 0 ;
1714 } ;
1715
1716 if (psf->read_int == NULL((void*)0) || psf->seek == NULL((void*)0))
1717 { psf->error = SFE_UNIMPLEMENTED ;
1718 return 0 ;
1719 } ;
1720
1721 if (psf->last_op != SFM_READ)
1722 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1723 return 0 ;
1724
1725 count = psf->read_int (psf, ptr, frames * psf->sf.channels) ;
1726
1727 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1728 psf->read_current += count / psf->sf.channels ;
1729 else
1730 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1731 extra = frames * psf->sf.channels - count ;
1732 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
1733 psf->read_current = psf->sf.frames ;
1734 } ;
1735
1736 psf->last_op = SFM_READ ;
1737
1738 return count / psf->sf.channels ;
1739} /* sf_readf_int */
1740
1741/*------------------------------------------------------------------------------
1742*/
1743
1744sf_count_t
1745sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t len)
1746{ SF_PRIVATE *psf ;
1747 sf_count_t count, extra ;
1748
1749 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1750
1751 if (psf->file.mode == SFM_WRITE)
1752 { psf->error = SFE_NOT_READMODE ;
1753 return 0 ;
1754 } ;
1755
1756 if (len % psf->sf.channels)
1757 { psf->error = SFE_BAD_READ_ALIGN ;
1758 return 0 ;
1759 } ;
1760
1761 if (len <= 0 || psf->read_current >= psf->sf.frames)
1762 { psf_memset (ptr, 0, len * sizeof (float)) ;
1763 return 0 ;
1764 } ;
1765
1766 if (psf->read_float == NULL((void*)0) || psf->seek == NULL((void*)0))
1767 { psf->error = SFE_UNIMPLEMENTED ;
1768 return 0 ;
1769 } ;
1770
1771 if (psf->last_op != SFM_READ)
1772 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1773 return 0 ;
1774
1775 count = psf->read_float (psf, ptr, len) ;
1776
1777 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1778 psf->read_current += count / psf->sf.channels ;
1779 else
1780 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1781 extra = len - count ;
1782 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
1783 psf->read_current = psf->sf.frames ;
1784 } ;
1785
1786 psf->last_op = SFM_READ ;
1787
1788 return count ;
1789} /* sf_read_float */
1790
1791sf_count_t
1792sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames)
1793{ SF_PRIVATE *psf ;
1794 sf_count_t count, extra ;
1795
1796 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1797
1798 if (psf->file.mode == SFM_WRITE)
1799 { psf->error = SFE_NOT_READMODE ;
1800 return 0 ;
1801 } ;
1802
1803 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1804 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (float)) ;
1805 return 0 ;
1806 } ;
1807
1808 if (psf->read_float == NULL((void*)0) || psf->seek == NULL((void*)0))
1809 { psf->error = SFE_UNIMPLEMENTED ;
1810 return 0 ;
1811 } ;
1812
1813 if (psf->last_op != SFM_READ)
1814 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1815 return 0 ;
1816
1817 count = psf->read_float (psf, ptr, frames * psf->sf.channels) ;
1818
1819 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1820 psf->read_current += count / psf->sf.channels ;
1821 else
1822 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1823 extra = frames * psf->sf.channels - count ;
1824 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
1825 psf->read_current = psf->sf.frames ;
1826 } ;
1827
1828 psf->last_op = SFM_READ ;
1829
1830 return count / psf->sf.channels ;
1831} /* sf_readf_float */
1832
1833/*------------------------------------------------------------------------------
1834*/
1835
1836sf_count_t
1837sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t len)
1838{ SF_PRIVATE *psf ;
1839 sf_count_t count, extra ;
1840
1841 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1842
1843 if (psf->file.mode == SFM_WRITE)
1844 { psf->error = SFE_NOT_READMODE ;
1845 return 0 ;
1846 } ;
1847
1848 if (len % psf->sf.channels)
1849 { psf->error = SFE_BAD_READ_ALIGN ;
1850 return 0 ;
1851 } ;
1852
1853 if (len <= 0 || psf->read_current >= psf->sf.frames)
1854 { psf_memset (ptr, 0, len * sizeof (double)) ;
1855 return 0 ;
1856 } ;
1857
1858 if (psf->read_double == NULL((void*)0) || psf->seek == NULL((void*)0))
1859 { psf->error = SFE_UNIMPLEMENTED ;
1860 return 0 ;
1861 } ;
1862
1863 if (psf->last_op != SFM_READ)
1864 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1865 return 0 ;
1866
1867 count = psf->read_double (psf, ptr, len) ;
1868
1869 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1870 psf->read_current += count / psf->sf.channels ;
1871 else
1872 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1873 extra = len - count ;
1874 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
1875 psf->read_current = psf->sf.frames ;
1876 } ;
1877
1878 psf->last_op = SFM_READ ;
1879
1880 return count ;
1881} /* sf_read_double */
1882
1883sf_count_t
1884sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames)
1885{ SF_PRIVATE *psf ;
1886 sf_count_t count, extra ;
1887
1888 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1889
1890 if (psf->file.mode == SFM_WRITE)
1891 { psf->error = SFE_NOT_READMODE ;
1892 return 0 ;
1893 } ;
1894
1895 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1896 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (double)) ;
1897 return 0 ;
1898 } ;
1899
1900 if (psf->read_double == NULL((void*)0) || psf->seek == NULL((void*)0))
1901 { psf->error = SFE_UNIMPLEMENTED ;
1902 return 0 ;
1903 } ;
1904
1905 if (psf->last_op != SFM_READ)
1906 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1907 return 0 ;
1908
1909 count = psf->read_double (psf, ptr, frames * psf->sf.channels) ;
1910
1911 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1912 psf->read_current += count / psf->sf.channels ;
1913 else
1914 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1915 extra = frames * psf->sf.channels - count ;
1916 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
1917 psf->read_current = psf->sf.frames ;
1918 } ;
1919
1920 psf->last_op = SFM_READ ;
1921
1922 return count / psf->sf.channels ;
1923} /* sf_readf_double */
1924
1925/*------------------------------------------------------------------------------
1926*/
1927
1928sf_count_t
1929sf_write_raw (SNDFILE *sndfile, const void *ptr, sf_count_t len)
1930{ SF_PRIVATE *psf ;
1931 sf_count_t count ;
1932 int bytewidth, blockwidth ;
1933
1934 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1935
1936 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
1937 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
1938
1939 if (psf->file.mode == SFM_READ)
1940 { psf->error = SFE_NOT_WRITEMODE ;
1941 return 0 ;
1942 } ;
1943
1944 if (len % (psf->sf.channels * bytewidth))
1945 { psf->error = SFE_BAD_WRITE_ALIGN ;
1946 return 0 ;
1947 } ;
1948
1949 if (psf->last_op != SFM_WRITE)
1950 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
1951 return 0 ;
1952
1953 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
1954 psf->write_header (psf, SF_FALSE) ;
1955 psf->have_written = SF_TRUE ;
1956
1957 count = psf_fwrite (ptr, 1, len, psf) ;
1958
1959 psf->write_current += count / blockwidth ;
1960
1961 psf->last_op = SFM_WRITE ;
1962
1963 if (psf->write_current > psf->sf.frames)
1964 { psf->sf.frames = psf->write_current ;
1965 psf->dataend = 0 ;
1966 } ;
1967
1968 if (psf->auto_header && psf->write_header != NULL((void*)0))
1969 psf->write_header (psf, SF_TRUE) ;
1970
1971 return count ;
1972} /* sf_write_raw */
1973
1974/*------------------------------------------------------------------------------
1975*/
1976
1977sf_count_t
1978sf_write_short (SNDFILE *sndfile, const short *ptr, sf_count_t len)
1979{ SF_PRIVATE *psf ;
1980 sf_count_t count ;
1981
1982 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
1983
1984 if (psf->file.mode == SFM_READ)
1985 { psf->error = SFE_NOT_WRITEMODE ;
1986 return 0 ;
1987 } ;
1988
1989 if (len % psf->sf.channels)
1990 { psf->error = SFE_BAD_WRITE_ALIGN ;
1991 return 0 ;
1992 } ;
1993
1994 if (psf->write_short == NULL((void*)0) || psf->seek == NULL((void*)0))
1995 { psf->error = SFE_UNIMPLEMENTED ;
1996 return 0 ;
1997 } ;
1998
1999 if (psf->last_op != SFM_WRITE)
2000 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2001 return 0 ;
2002
2003 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2004 psf->write_header (psf, SF_FALSE) ;
2005 psf->have_written = SF_TRUE ;
2006
2007 count = psf->write_short (psf, ptr, len) ;
2008
2009 psf->write_current += count / psf->sf.channels ;
2010
2011 psf->last_op = SFM_WRITE ;
2012
2013 if (psf->write_current > psf->sf.frames)
2014 { psf->sf.frames = psf->write_current ;
2015 psf->dataend = 0 ;
2016 } ;
2017
2018 if (psf->auto_header && psf->write_header != NULL((void*)0))
2019 psf->write_header (psf, SF_TRUE) ;
2020
2021 return count ;
2022} /* sf_write_short */
2023
2024sf_count_t
2025sf_writef_short (SNDFILE *sndfile, const short *ptr, sf_count_t frames)
2026{ SF_PRIVATE *psf ;
2027 sf_count_t count ;
2028
2029 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
2030
2031 if (psf->file.mode == SFM_READ)
2032 { psf->error = SFE_NOT_WRITEMODE ;
2033 return 0 ;
2034 } ;
2035
2036 if (psf->write_short == NULL((void*)0) || psf->seek == NULL((void*)0))
2037 { psf->error = SFE_UNIMPLEMENTED ;
2038 return 0 ;
2039 } ;
2040
2041 if (psf->last_op != SFM_WRITE)
2042 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2043 return 0 ;
2044
2045 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2046 psf->write_header (psf, SF_FALSE) ;
2047 psf->have_written = SF_TRUE ;
2048
2049 count = psf->write_short (psf, ptr, frames * psf->sf.channels) ;
2050
2051 psf->write_current += count / psf->sf.channels ;
2052
2053 psf->last_op = SFM_WRITE ;
2054
2055 if (psf->write_current > psf->sf.frames)
2056 { psf->sf.frames = psf->write_current ;
2057 psf->dataend = 0 ;
2058 } ;
2059
2060 if (psf->auto_header && psf->write_header != NULL((void*)0))
2061 psf->write_header (psf, SF_TRUE) ;
2062
2063 return count / psf->sf.channels ;
2064} /* sf_writef_short */
2065
2066/*------------------------------------------------------------------------------
2067*/
2068
2069sf_count_t
2070sf_write_int (SNDFILE *sndfile, const int *ptr, sf_count_t len)
2071{ SF_PRIVATE *psf ;
2072 sf_count_t count ;
2073
2074 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
2075
2076 if (psf->file.mode == SFM_READ)
2077 { psf->error = SFE_NOT_WRITEMODE ;
2078 return 0 ;
2079 } ;
2080
2081 if (len % psf->sf.channels)
2082 { psf->error = SFE_BAD_WRITE_ALIGN ;
2083 return 0 ;
2084 } ;
2085
2086 if (psf->write_int == NULL((void*)0) || psf->seek == NULL((void*)0))
2087 { psf->error = SFE_UNIMPLEMENTED ;
2088 return 0 ;
2089 } ;
2090
2091 if (psf->last_op != SFM_WRITE)
2092 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2093 return 0 ;
2094
2095 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2096 psf->write_header (psf, SF_FALSE) ;
2097 psf->have_written = SF_TRUE ;
2098
2099 count = psf->write_int (psf, ptr, len) ;
2100
2101 psf->write_current += count / psf->sf.channels ;
2102
2103 psf->last_op = SFM_WRITE ;
2104
2105 if (psf->write_current > psf->sf.frames)
2106 { psf->sf.frames = psf->write_current ;
2107 psf->dataend = 0 ;
2108 } ;
2109
2110 if (psf->auto_header && psf->write_header != NULL((void*)0))
2111 psf->write_header (psf, SF_TRUE) ;
2112
2113 return count ;
2114} /* sf_write_int */
2115
2116sf_count_t
2117sf_writef_int (SNDFILE *sndfile, const int *ptr, sf_count_t frames)
2118{ SF_PRIVATE *psf ;
2119 sf_count_t count ;
2120
2121 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
2122
2123 if (psf->file.mode == SFM_READ)
2124 { psf->error = SFE_NOT_WRITEMODE ;
2125 return 0 ;
2126 } ;
2127
2128 if (psf->write_int == NULL((void*)0) || psf->seek == NULL((void*)0))
2129 { psf->error = SFE_UNIMPLEMENTED ;
2130 return 0 ;
2131 } ;
2132
2133 if (psf->last_op != SFM_WRITE)
2134 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2135 return 0 ;
2136
2137 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2138 psf->write_header (psf, SF_FALSE) ;
2139 psf->have_written = SF_TRUE ;
2140
2141 count = psf->write_int (psf, ptr, frames * psf->sf.channels) ;
2142
2143 psf->write_current += count / psf->sf.channels ;
2144
2145 psf->last_op = SFM_WRITE ;
2146
2147 if (psf->write_current > psf->sf.frames)
2148 { psf->sf.frames = psf->write_current ;
2149 psf->dataend = 0 ;
2150 } ;
2151
2152 if (psf->auto_header && psf->write_header != NULL((void*)0))
2153 psf->write_header (psf, SF_TRUE) ;
2154
2155 return count / psf->sf.channels ;
2156} /* sf_writef_int */
2157
2158/*------------------------------------------------------------------------------
2159*/
2160
2161sf_count_t
2162sf_write_float (SNDFILE *sndfile, const float *ptr, sf_count_t len)
2163{ SF_PRIVATE *psf ;
2164 sf_count_t count ;
2165
2166 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
2167
2168 if (psf->file.mode == SFM_READ)
2169 { psf->error = SFE_NOT_WRITEMODE ;
2170 return 0 ;
2171 } ;
2172
2173 if (len % psf->sf.channels)
2174 { psf->error = SFE_BAD_WRITE_ALIGN ;
2175 return 0 ;
2176 } ;
2177
2178 if (psf->write_float == NULL((void*)0) || psf->seek == NULL((void*)0))
2179 { psf->error = SFE_UNIMPLEMENTED ;
2180 return 0 ;
2181 } ;
2182
2183 if (psf->last_op != SFM_WRITE)
2184 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2185 return 0 ;
2186
2187 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2188 psf->write_header (psf, SF_FALSE) ;
2189 psf->have_written = SF_TRUE ;
2190
2191 count = psf->write_float (psf, ptr, len) ;
2192
2193 psf->write_current += count / psf->sf.channels ;
2194
2195 psf->last_op = SFM_WRITE ;
2196
2197 if (psf->write_current > psf->sf.frames)
2198 { psf->sf.frames = psf->write_current ;
2199 psf->dataend = 0 ;
2200 } ;
2201
2202 if (psf->auto_header && psf->write_header != NULL((void*)0))
2203 psf->write_header (psf, SF_TRUE) ;
2204
2205 return count ;
2206} /* sf_write_float */
2207
2208sf_count_t
2209sf_writef_float (SNDFILE *sndfile, const float *ptr, sf_count_t frames)
2210{ SF_PRIVATE *psf ;
2211 sf_count_t count ;
2212
2213 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
2214
2215 if (psf->file.mode == SFM_READ)
2216 { psf->error = SFE_NOT_WRITEMODE ;
2217 return 0 ;
2218 } ;
2219
2220 if (psf->write_float == NULL((void*)0) || psf->seek == NULL((void*)0))
2221 { psf->error = SFE_UNIMPLEMENTED ;
2222 return 0 ;
2223 } ;
2224
2225 if (psf->last_op != SFM_WRITE)
2226 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2227 return 0 ;
2228
2229 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2230 psf->write_header (psf, SF_FALSE) ;
2231 psf->have_written = SF_TRUE ;
2232
2233 count = psf->write_float (psf, ptr, frames * psf->sf.channels) ;
2234
2235 psf->write_current += count / psf->sf.channels ;
2236
2237 psf->last_op = SFM_WRITE ;
2238
2239 if (psf->write_current > psf->sf.frames)
2240 { psf->sf.frames = psf->write_current ;
2241 psf->dataend = 0 ;
2242 } ;
2243
2244 if (psf->auto_header && psf->write_header != NULL((void*)0))
2245 psf->write_header (psf, SF_TRUE) ;
2246
2247 return count / psf->sf.channels ;
2248} /* sf_writef_float */
2249
2250/*------------------------------------------------------------------------------
2251*/
2252
2253sf_count_t
2254sf_write_double (SNDFILE *sndfile, const double *ptr, sf_count_t len)
2255{ SF_PRIVATE *psf ;
2256 sf_count_t count ;
2257
2258 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
2259
2260 if (psf->file.mode == SFM_READ)
2261 { psf->error = SFE_NOT_WRITEMODE ;
2262 return 0 ;
2263 } ;
2264
2265 if (len % psf->sf.channels)
2266 { psf->error = SFE_BAD_WRITE_ALIGN ;
2267 return 0 ;
2268 } ;
2269
2270 if (psf->write_double == NULL((void*)0) || psf->seek == NULL((void*)0))
2271 { psf->error = SFE_UNIMPLEMENTED ;
2272 return 0 ;
2273 } ;
2274
2275 if (psf->last_op != SFM_WRITE)
2276 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2277 return 0 ;
2278
2279 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2280 psf->write_header (psf, SF_FALSE) ;
2281 psf->have_written = SF_TRUE ;
2282
2283 count = psf->write_double (psf, ptr, len) ;
2284
2285 psf->write_current += count / psf->sf.channels ;
2286
2287 psf->last_op = SFM_WRITE ;
2288
2289 if (psf->write_current > psf->sf.frames)
2290 { psf->sf.frames = psf->write_current ;
2291 psf->dataend = 0 ;
2292 } ;
2293
2294 if (psf->auto_header && psf->write_header != NULL((void*)0))
2295 psf->write_header (psf, SF_TRUE) ;
2296
2297 return count ;
2298} /* sf_write_double */
2299
2300sf_count_t
2301sf_writef_double (SNDFILE *sndfile, const double *ptr, sf_count_t frames)
2302{ SF_PRIVATE *psf ;
2303 sf_count_t count ;
2304
2305 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
2306
2307 if (psf->file.mode == SFM_READ)
2308 { psf->error = SFE_NOT_WRITEMODE ;
2309 return 0 ;
2310 } ;
2311
2312 if (psf->write_double == NULL((void*)0) || psf->seek == NULL((void*)0))
2313 { psf->error = SFE_UNIMPLEMENTED ;
2314 return 0 ;
2315 } ;
2316
2317 if (psf->last_op != SFM_WRITE)
2318 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2319 return 0 ;
2320
2321 if (psf->have_written == SF_FALSE && psf->write_header != NULL((void*)0))
2322 psf->write_header (psf, SF_FALSE) ;
2323 psf->have_written = SF_TRUE ;
2324
2325 count = psf->write_double (psf, ptr, frames * psf->sf.channels) ;
2326
2327 psf->write_current += count / psf->sf.channels ;
2328
2329 psf->last_op = SFM_WRITE ;
2330
2331 if (psf->write_current > psf->sf.frames)
2332 { psf->sf.frames = psf->write_current ;
2333 psf->dataend = 0 ;
2334 } ;
2335
2336 if (psf->auto_header && psf->write_header != NULL((void*)0))
2337 psf->write_header (psf, SF_TRUE) ;
2338
2339 return count / psf->sf.channels ;
2340} /* sf_writef_double */
2341
2342/*=========================================================================
2343** Private functions.
2344*/
2345
2346static int
2347try_resource_fork (SF_PRIVATE * psf)
2348{ int old_error = psf->error ;
2349
2350 /* Set READ mode now, to see if resource fork exists. */
2351 psf->rsrc.mode = SFM_READ ;
2352 if (psf_open_rsrc (psf) != 0)
2353 { psf->error = old_error ;
2354 return 0 ;
2355 } ;
2356
2357 /* More checking here. */
2358 psf_log_printf (psf, "Resource fork : %s\n", psf->rsrc.path.c) ;
2359
2360 return SF_FORMAT_SD2 ;
2361} /* try_resource_fork */
2362
2363static int
2364format_from_extension (SF_PRIVATE *psf)
2365{ char *cptr ;
2366 char buffer [16] ;
2367 int format = 0 ;
2368
2369 if ((cptr = strrchr (psf->file.name.c, '.')) == NULL((void*)0))
2370 return 0 ;
2371
2372 cptr ++ ;
2373 if (strlen (cptr) > sizeof (buffer) - 1)
2374 return 0 ;
2375
2376 psf_strlcpy (buffer, sizeof (buffer), cptr) ;
2377 buffer [sizeof (buffer) - 1] = 0 ;
2378
2379 /* Convert everything in the buffer to lower case. */
2380 cptr = buffer ;
2381 while (*cptr)
2382 { *cptr = tolower (*cptr)(__extension__ ({ int __res; if (sizeof (*cptr) > 1) { if (
__builtin_constant_p (*cptr)) { int __c = (*cptr); __res = __c
< -128 || __c > 255 ? __c : (*__ctype_tolower_loc ())[
__c]; } else __res = tolower (*cptr); } else __res = (*__ctype_tolower_loc
())[(int) (*cptr)]; __res; }))
;
2383 cptr ++ ;
2384 } ;
2385
2386 cptr = buffer ;
2387
2388 if (strcmp (cptr, "au")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(cptr) && __builtin_constant_p ("au") && (__s1_len
= __builtin_strlen (cptr), __s2_len = __builtin_strlen ("au"
), (!((size_t)(const void *)((cptr) + 1) - (size_t)(const void
*)(cptr) == 1) || __s1_len >= 4) && (!((size_t)(const
void *)(("au") + 1) - (size_t)(const void *)("au") == 1) || __s2_len
>= 4)) ? __builtin_strcmp (cptr, "au") : (__builtin_constant_p
(cptr) && ((size_t)(const void *)((cptr) + 1) - (size_t
)(const void *)(cptr) == 1) && (__s1_len = __builtin_strlen
(cptr), __s1_len < 4) ? (__builtin_constant_p ("au") &&
((size_t)(const void *)(("au") + 1) - (size_t)(const void *)
("au") == 1) ? __builtin_strcmp (cptr, "au") : (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) ("au"); int __result = (((const unsigned char *) (const
char *) (cptr))[0] - __s2[0]); if (__s1_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[1] - __s2[1]); if (__s1_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[2] - __s2[2]); if (__s1_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) (cptr))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("au") && ((size_t)(const void *)(("au") + 1) - (size_t
)(const void *)("au") == 1) && (__s2_len = __builtin_strlen
("au"), __s2_len < 4) ? (__builtin_constant_p (cptr) &&
((size_t)(const void *)((cptr) + 1) - (size_t)(const void *)
(cptr) == 1) ? __builtin_strcmp (cptr, "au") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (cptr); int __result = (((const unsigned char *) (const
char *) ("au"))[0] - __s2[0]); if (__s2_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("au"))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("au"))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) ("au"))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(cptr, "au")))); })
== 0)
2389 { psf->sf.channels = 1 ;
2390 psf->sf.samplerate = 8000 ;
2391 format = SF_FORMAT_RAW | SF_FORMAT_ULAW ;
2392 }
2393 else if (strcmp (cptr, "snd")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(cptr) && __builtin_constant_p ("snd") && (__s1_len
= __builtin_strlen (cptr), __s2_len = __builtin_strlen ("snd"
), (!((size_t)(const void *)((cptr) + 1) - (size_t)(const void
*)(cptr) == 1) || __s1_len >= 4) && (!((size_t)(const
void *)(("snd") + 1) - (size_t)(const void *)("snd") == 1) ||
__s2_len >= 4)) ? __builtin_strcmp (cptr, "snd") : (__builtin_constant_p
(cptr) && ((size_t)(const void *)((cptr) + 1) - (size_t
)(const void *)(cptr) == 1) && (__s1_len = __builtin_strlen
(cptr), __s1_len < 4) ? (__builtin_constant_p ("snd") &&
((size_t)(const void *)(("snd") + 1) - (size_t)(const void *
)("snd") == 1) ? __builtin_strcmp (cptr, "snd") : (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) ("snd"); int __result = (((const unsigned char *) (const
char *) (cptr))[0] - __s2[0]); if (__s1_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[1] - __s2[1]); if (__s1_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[2] - __s2[2]); if (__s1_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) (cptr))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("snd") && ((size_t)(const void *)(("snd") + 1) - (size_t
)(const void *)("snd") == 1) && (__s2_len = __builtin_strlen
("snd"), __s2_len < 4) ? (__builtin_constant_p (cptr) &&
((size_t)(const void *)((cptr) + 1) - (size_t)(const void *)
(cptr) == 1) ? __builtin_strcmp (cptr, "snd") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (cptr); int __result = (((const unsigned char *) (const
char *) ("snd"))[0] - __s2[0]); if (__s2_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("snd"))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("snd"))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) ("snd"))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(cptr, "snd")))); })
== 0)
2394 { psf->sf.channels = 1 ;
2395 psf->sf.samplerate = 8000 ;
2396 format = SF_FORMAT_RAW | SF_FORMAT_ULAW ;
2397 }
2398
2399 else if (strcmp (cptr, "vox")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(cptr) && __builtin_constant_p ("vox") && (__s1_len
= __builtin_strlen (cptr), __s2_len = __builtin_strlen ("vox"
), (!((size_t)(const void *)((cptr) + 1) - (size_t)(const void
*)(cptr) == 1) || __s1_len >= 4) && (!((size_t)(const
void *)(("vox") + 1) - (size_t)(const void *)("vox") == 1) ||
__s2_len >= 4)) ? __builtin_strcmp (cptr, "vox") : (__builtin_constant_p
(cptr) && ((size_t)(const void *)((cptr) + 1) - (size_t
)(const void *)(cptr) == 1) && (__s1_len = __builtin_strlen
(cptr), __s1_len < 4) ? (__builtin_constant_p ("vox") &&
((size_t)(const void *)(("vox") + 1) - (size_t)(const void *
)("vox") == 1) ? __builtin_strcmp (cptr, "vox") : (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) ("vox"); int __result = (((const unsigned char *) (const
char *) (cptr))[0] - __s2[0]); if (__s1_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[1] - __s2[1]); if (__s1_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[2] - __s2[2]); if (__s1_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) (cptr))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("vox") && ((size_t)(const void *)(("vox") + 1) - (size_t
)(const void *)("vox") == 1) && (__s2_len = __builtin_strlen
("vox"), __s2_len < 4) ? (__builtin_constant_p (cptr) &&
((size_t)(const void *)((cptr) + 1) - (size_t)(const void *)
(cptr) == 1) ? __builtin_strcmp (cptr, "vox") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (cptr); int __result = (((const unsigned char *) (const
char *) ("vox"))[0] - __s2[0]); if (__s2_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("vox"))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("vox"))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) ("vox"))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(cptr, "vox")))); })
== 0 || strcmp (cptr, "vox8")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(cptr) && __builtin_constant_p ("vox8") && (
__s1_len = __builtin_strlen (cptr), __s2_len = __builtin_strlen
("vox8"), (!((size_t)(const void *)((cptr) + 1) - (size_t)(const
void *)(cptr) == 1) || __s1_len >= 4) && (!((size_t
)(const void *)(("vox8") + 1) - (size_t)(const void *)("vox8"
) == 1) || __s2_len >= 4)) ? __builtin_strcmp (cptr, "vox8"
) : (__builtin_constant_p (cptr) && ((size_t)(const void
*)((cptr) + 1) - (size_t)(const void *)(cptr) == 1) &&
(__s1_len = __builtin_strlen (cptr), __s1_len < 4) ? (__builtin_constant_p
("vox8") && ((size_t)(const void *)(("vox8") + 1) - (
size_t)(const void *)("vox8") == 1) ? __builtin_strcmp (cptr,
"vox8") : (__extension__ ({ const unsigned char *__s2 = (const
unsigned char *) (const char *) ("vox8"); int __result = (((
const unsigned char *) (const char *) (cptr))[0] - __s2[0]); if
(__s1_len > 0 && __result == 0) { __result = (((const
unsigned char *) (const char *) (cptr))[1] - __s2[1]); if (__s1_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) (cptr))[2] - __s2[2]); if (__s1_len >
2 && __result == 0) __result = (((const unsigned char
*) (const char *) (cptr))[3] - __s2[3]); } } __result; }))) :
(__builtin_constant_p ("vox8") && ((size_t)(const void
*)(("vox8") + 1) - (size_t)(const void *)("vox8") == 1) &&
(__s2_len = __builtin_strlen ("vox8"), __s2_len < 4) ? (__builtin_constant_p
(cptr) && ((size_t)(const void *)((cptr) + 1) - (size_t
)(const void *)(cptr) == 1) ? __builtin_strcmp (cptr, "vox8")
: (- (__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (cptr); int __result = (((const unsigned
char *) (const char *) ("vox8"))[0] - __s2[0]); if (__s2_len
> 0 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("vox8"))[1] - __s2[1]); if (__s2_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("vox8"))[2] - __s2[2]); if (__s2_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) ("vox8"))[3] - __s2[3]); } } __result
; })))) : __builtin_strcmp (cptr, "vox8")))); })
== 0)
2400 { psf->sf.channels = 1 ;
2401 psf->sf.samplerate = 8000 ;
2402 format = SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM ;
2403 }
2404 else if (strcmp (cptr, "vox6")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(cptr) && __builtin_constant_p ("vox6") && (
__s1_len = __builtin_strlen (cptr), __s2_len = __builtin_strlen
("vox6"), (!((size_t)(const void *)((cptr) + 1) - (size_t)(const
void *)(cptr) == 1) || __s1_len >= 4) && (!((size_t
)(const void *)(("vox6") + 1) - (size_t)(const void *)("vox6"
) == 1) || __s2_len >= 4)) ? __builtin_strcmp (cptr, "vox6"
) : (__builtin_constant_p (cptr) && ((size_t)(const void
*)((cptr) + 1) - (size_t)(const void *)(cptr) == 1) &&
(__s1_len = __builtin_strlen (cptr), __s1_len < 4) ? (__builtin_constant_p
("vox6") && ((size_t)(const void *)(("vox6") + 1) - (
size_t)(const void *)("vox6") == 1) ? __builtin_strcmp (cptr,
"vox6") : (__extension__ ({ const unsigned char *__s2 = (const
unsigned char *) (const char *) ("vox6"); int __result = (((
const unsigned char *) (const char *) (cptr))[0] - __s2[0]); if
(__s1_len > 0 && __result == 0) { __result = (((const
unsigned char *) (const char *) (cptr))[1] - __s2[1]); if (__s1_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) (cptr))[2] - __s2[2]); if (__s1_len >
2 && __result == 0) __result = (((const unsigned char
*) (const char *) (cptr))[3] - __s2[3]); } } __result; }))) :
(__builtin_constant_p ("vox6") && ((size_t)(const void
*)(("vox6") + 1) - (size_t)(const void *)("vox6") == 1) &&
(__s2_len = __builtin_strlen ("vox6"), __s2_len < 4) ? (__builtin_constant_p
(cptr) && ((size_t)(const void *)((cptr) + 1) - (size_t
)(const void *)(cptr) == 1) ? __builtin_strcmp (cptr, "vox6")
: (- (__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (cptr); int __result = (((const unsigned
char *) (const char *) ("vox6"))[0] - __s2[0]); if (__s2_len
> 0 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("vox6"))[1] - __s2[1]); if (__s2_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) ("vox6"))[2] - __s2[2]); if (__s2_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) ("vox6"))[3] - __s2[3]); } } __result
; })))) : __builtin_strcmp (cptr, "vox6")))); })
== 0)
2405 { psf->sf.channels = 1 ;
2406 psf->sf.samplerate = 6000 ;
2407 format = SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM ;
2408 }
2409 else if (strcmp (cptr, "gsm")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(cptr) && __builtin_constant_p ("gsm") && (__s1_len
= __builtin_strlen (cptr), __s2_len = __builtin_strlen ("gsm"
), (!((size_t)(const void *)((cptr) + 1) - (size_t)(const void
*)(cptr) == 1) || __s1_len >= 4) && (!((size_t)(const
void *)(("gsm") + 1) - (size_t)(const void *)("gsm") == 1) ||
__s2_len >= 4)) ? __builtin_strcmp (cptr, "gsm") : (__builtin_constant_p
(cptr) && ((size_t)(const void *)((cptr) + 1) - (size_t
)(const void *)(cptr) == 1) && (__s1_len = __builtin_strlen
(cptr), __s1_len < 4) ? (__builtin_constant_p ("gsm") &&
((size_t)(const void *)(("gsm") + 1) - (size_t)(const void *
)("gsm") == 1) ? __builtin_strcmp (cptr, "gsm") : (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) ("gsm"); int __result = (((const unsigned char *) (const
char *) (cptr))[0] - __s2[0]); if (__s1_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[1] - __s2[1]); if (__s1_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (cptr))[2] - __s2[2]); if (__s1_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) (cptr))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("gsm") && ((size_t)(const void *)(("gsm") + 1) - (size_t
)(const void *)("gsm") == 1) && (__s2_len = __builtin_strlen
("gsm"), __s2_len < 4) ? (__builtin_constant_p (cptr) &&
((size_t)(const void *)((cptr) + 1) - (size_t)(const void *)
(cptr) == 1) ? __builtin_strcmp (cptr, "gsm") : (- (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (cptr); int __result = (((const unsigned char *) (const
char *) ("gsm"))[0] - __s2[0]); if (__s2_len > 0 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("gsm"))[1] - __s2[1]); if (__s2_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) ("gsm"))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) ("gsm"))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp
(cptr, "gsm")))); })
== 0)
2410 { psf->sf.channels = 1 ;
2411 psf->sf.samplerate = 8000 ;
2412 format = SF_FORMAT_RAW | SF_FORMAT_GSM610 ;
2413 }
2414
2415 /* For RAW files, make sure the dataoffset if set correctly. */
2416 if ((SF_CONTAINER (format)((format) & SF_FORMAT_TYPEMASK)) == SF_FORMAT_RAW)
2417 psf->dataoffset = 0 ;
2418
2419 return format ;
2420} /* format_from_extension */
2421
2422static int
2423guess_file_type (SF_PRIVATE *psf)
2424{ uint32_t buffer [3], format ;
2425
2426 if (psf_binheader_readf (psf, "b", &buffer, SIGNED_SIZEOF (buffer)((int) sizeof (buffer))) != SIGNED_SIZEOF (buffer)((int) sizeof (buffer)))
2427 { psf->error = SFE_BAD_FILE_READ ;
2428 return 0 ;
2429 } ;
2430
2431 if ((buffer [0] == MAKE_MARKER ('R', 'I', 'F', 'F')((uint32_t) (('R') | (('I') << 8) | (('F') << 16)
| (((uint32_t) ('F')) << 24)))
|| buffer [0] == MAKE_MARKER ('R', 'I', 'F', 'X')((uint32_t) (('R') | (('I') << 8) | (('F') << 16)
| (((uint32_t) ('X')) << 24)))
)
2432 && buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E')((uint32_t) (('W') | (('A') << 8) | (('V') << 16)
| (((uint32_t) ('E')) << 24)))
)
2433 return SF_FORMAT_WAV ;
2434
2435 if (buffer [0] == MAKE_MARKER ('F', 'O', 'R', 'M')((uint32_t) (('F') | (('O') << 8) | (('R') << 16)
| (((uint32_t) ('M')) << 24)))
)
2436 { if (buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'F')((uint32_t) (('A') | (('I') << 8) | (('F') << 16)
| (((uint32_t) ('F')) << 24)))
|| buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'C')((uint32_t) (('A') | (('I') << 8) | (('F') << 16)
| (((uint32_t) ('C')) << 24)))
)
2437 return SF_FORMAT_AIFF ;
2438 if (buffer [2] == MAKE_MARKER ('8', 'S', 'V', 'X')((uint32_t) (('8') | (('S') << 8) | (('V') << 16)
| (((uint32_t) ('X')) << 24)))
|| buffer [2] == MAKE_MARKER ('1', '6', 'S', 'V')((uint32_t) (('1') | (('6') << 8) | (('S') << 16)
| (((uint32_t) ('V')) << 24)))
)
2439 return SF_FORMAT_SVX ;
2440 return 0 ;
2441 } ;
2442
2443 if (buffer [0] == MAKE_MARKER ('.', 's', 'n', 'd')((uint32_t) (('.') | (('s') << 8) | (('n') << 16)
| (((uint32_t) ('d')) << 24)))
|| buffer [0] == MAKE_MARKER ('d', 'n', 's', '.')((uint32_t) (('d') | (('n') << 8) | (('s') << 16)
| (((uint32_t) ('.')) << 24)))
)
2444 return SF_FORMAT_AU ;
2445
2446 if ((buffer [0] == MAKE_MARKER ('f', 'a', 'p', ' ')((uint32_t) (('f') | (('a') << 8) | (('p') << 16)
| (((uint32_t) (' ')) << 24)))
|| buffer [0] == MAKE_MARKER (' ', 'p', 'a', 'f')((uint32_t) ((' ') | (('p') << 8) | (('a') << 16)
| (((uint32_t) ('f')) << 24)))
))
2447 return SF_FORMAT_PAF ;
2448
2449 if (buffer [0] == MAKE_MARKER ('N', 'I', 'S', 'T')((uint32_t) (('N') | (('I') << 8) | (('S') << 16)
| (((uint32_t) ('T')) << 24)))
)
2450 return SF_FORMAT_NIST ;
2451
2452 if (buffer [0] == MAKE_MARKER ('C', 'r', 'e', 'a')((uint32_t) (('C') | (('r') << 8) | (('e') << 16)
| (((uint32_t) ('a')) << 24)))
&& buffer [1] == MAKE_MARKER ('t', 'i', 'v', 'e')((uint32_t) (('t') | (('i') << 8) | (('v') << 16)
| (((uint32_t) ('e')) << 24)))
)
2453 return SF_FORMAT_VOC ;
2454
2455 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0xF8, 0xFF)((uint32_t) ((0xFF) | ((0xFF) << 8) | ((0xF8) << 16
) | (((uint32_t) (0xFF)) << 24)))
) == MAKE_MARKER (0x64, 0xA3, 0x00, 0x00)((uint32_t) ((0x64) | ((0xA3) << 8) | ((0x00) << 16
) | (((uint32_t) (0x00)) << 24)))
||
2456 (buffer [0] & MAKE_MARKER (0xFF, 0xF8, 0xFF, 0xFF)((uint32_t) ((0xFF) | ((0xF8) << 8) | ((0xFF) << 16
) | (((uint32_t) (0xFF)) << 24)))
) == MAKE_MARKER (0x00, 0x00, 0xA3, 0x64)((uint32_t) ((0x00) | ((0x00) << 8) | ((0xA3) << 16
) | (((uint32_t) (0x64)) << 24)))
)
2457 return SF_FORMAT_IRCAM ;
2458
2459 if (buffer [0] == MAKE_MARKER ('r', 'i', 'f', 'f')((uint32_t) (('r') | (('i') << 8) | (('f') << 16)
| (((uint32_t) ('f')) << 24)))
)
2460 return SF_FORMAT_W64 ;
2461
2462 if (buffer [0] == MAKE_MARKER (0, 0, 0x03, 0xE8)((uint32_t) ((0) | ((0) << 8) | ((0x03) << 16) | (
((uint32_t) (0xE8)) << 24)))
&& buffer [1] == MAKE_MARKER (0, 0, 0, 1)((uint32_t) ((0) | ((0) << 8) | ((0) << 16) | (((
uint32_t) (1)) << 24)))
&&
2463 buffer [2] == MAKE_MARKER (0, 0, 0, 1)((uint32_t) ((0) | ((0) << 8) | ((0) << 16) | (((
uint32_t) (1)) << 24)))
)
2464 return SF_FORMAT_MAT4 ;
2465
2466 if (buffer [0] == MAKE_MARKER (0, 0, 0, 0)((uint32_t) ((0) | ((0) << 8) | ((0) << 16) | (((
uint32_t) (0)) << 24)))
&& buffer [1] == MAKE_MARKER (1, 0, 0, 0)((uint32_t) ((1) | ((0) << 8) | ((0) << 16) | (((
uint32_t) (0)) << 24)))
&&
2467 buffer [2] == MAKE_MARKER (1, 0, 0, 0)((uint32_t) ((1) | ((0) << 8) | ((0) << 16) | (((
uint32_t) (0)) << 24)))
)
2468 return SF_FORMAT_MAT4 ;
2469
2470 if (buffer [0] == MAKE_MARKER ('M', 'A', 'T', 'L')((uint32_t) (('M') | (('A') << 8) | (('T') << 16)
| (((uint32_t) ('L')) << 24)))
&& buffer [1] == MAKE_MARKER ('A', 'B', ' ', '5')((uint32_t) (('A') | (('B') << 8) | ((' ') << 16)
| (((uint32_t) ('5')) << 24)))
)
2471 return SF_FORMAT_MAT5 ;
2472
2473 if (buffer [0] == MAKE_MARKER ('P', 'V', 'F', '1')((uint32_t) (('P') | (('V') << 8) | (('F') << 16)
| (((uint32_t) ('1')) << 24)))
)
2474 return SF_FORMAT_PVF ;
2475
2476 if (buffer [0] == MAKE_MARKER ('E', 'x', 't', 'e')((uint32_t) (('E') | (('x') << 8) | (('t') << 16)
| (((uint32_t) ('e')) << 24)))
&& buffer [1] == MAKE_MARKER ('n', 'd', 'e', 'd')((uint32_t) (('n') | (('d') << 8) | (('e') << 16)
| (((uint32_t) ('d')) << 24)))
&&
2477 buffer [2] == MAKE_MARKER (' ', 'I', 'n', 's')((uint32_t) ((' ') | (('I') << 8) | (('n') << 16)
| (((uint32_t) ('s')) << 24)))
)
2478 return SF_FORMAT_XI ;
2479
2480 if (buffer [0] == MAKE_MARKER ('c', 'a', 'f', 'f')((uint32_t) (('c') | (('a') << 8) | (('f') << 16)
| (((uint32_t) ('f')) << 24)))
&& buffer [2] == MAKE_MARKER ('d', 'e', 's', 'c')((uint32_t) (('d') | (('e') << 8) | (('s') << 16)
| (((uint32_t) ('c')) << 24)))
)
2481 return SF_FORMAT_CAF ;
2482
2483 if (buffer [0] == MAKE_MARKER ('O', 'g', 'g', 'S')((uint32_t) (('O') | (('g') << 8) | (('g') << 16)
| (((uint32_t) ('S')) << 24)))
)
2484 return SF_FORMAT_OGG ;
2485
2486 if (buffer [0] == MAKE_MARKER ('A', 'L', 'a', 'w')((uint32_t) (('A') | (('L') << 8) | (('a') << 16)
| (((uint32_t) ('w')) << 24)))
&& buffer [1] == MAKE_MARKER ('S', 'o', 'u', 'n')((uint32_t) (('S') | (('o') << 8) | (('u') << 16)
| (((uint32_t) ('n')) << 24)))
2487 && buffer [2] == MAKE_MARKER ('d', 'F', 'i', 'l')((uint32_t) (('d') | (('F') << 8) | (('i') << 16)
| (((uint32_t) ('l')) << 24)))
)
2488 return SF_FORMAT_WVE ;
2489
2490 if (buffer [0] == MAKE_MARKER ('D', 'i', 'a', 'm')((uint32_t) (('D') | (('i') << 8) | (('a') << 16)
| (((uint32_t) ('m')) << 24)))
&& buffer [1] == MAKE_MARKER ('o', 'n', 'd', 'W')((uint32_t) (('o') | (('n') << 8) | (('d') << 16)
| (((uint32_t) ('W')) << 24)))
2491 && buffer [2] == MAKE_MARKER ('a', 'r', 'e', ' ')((uint32_t) (('a') | (('r') << 8) | (('e') << 16)
| (((uint32_t) (' ')) << 24)))
)
2492 return SF_FORMAT_DWD ;
2493
2494 if (buffer [0] == MAKE_MARKER ('L', 'M', '8', '9')((uint32_t) (('L') | (('M') << 8) | (('8') << 16)
| (((uint32_t) ('9')) << 24)))
|| buffer [0] == MAKE_MARKER ('5', '3', 0, 0)((uint32_t) (('5') | (('3') << 8) | ((0) << 16) |
(((uint32_t) (0)) << 24)))
)
2495 return SF_FORMAT_TXW ;
2496
2497 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0x80, 0xFF)((uint32_t) ((0xFF) | ((0xFF) << 8) | ((0x80) << 16
) | (((uint32_t) (0xFF)) << 24)))
) == MAKE_MARKER (0xF0, 0x7E, 0, 0x01)((uint32_t) ((0xF0) | ((0x7E) << 8) | ((0) << 16)
| (((uint32_t) (0x01)) << 24)))
)
2498 return SF_FORMAT_SDS ;
2499
2500 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0, 0)((uint32_t) ((0xFF) | ((0xFF) << 8) | ((0) << 16)
| (((uint32_t) (0)) << 24)))
) == MAKE_MARKER (1, 4, 0, 0)((uint32_t) ((1) | ((4) << 8) | ((0) << 16) | (((
uint32_t) (0)) << 24)))
)
2501 return SF_FORMAT_MPC2K ;
2502
2503 if (buffer [0] == MAKE_MARKER ('C', 'A', 'T', ' ')((uint32_t) (('C') | (('A') << 8) | (('T') << 16)
| (((uint32_t) (' ')) << 24)))
&& buffer [2] == MAKE_MARKER ('R', 'E', 'X', '2')((uint32_t) (('R') | (('E') << 8) | (('X') << 16)
| (((uint32_t) ('2')) << 24)))
)
2504 return SF_FORMAT_REX2 ;
2505
2506 if (buffer [0] == MAKE_MARKER (0x30, 0x26, 0xB2, 0x75)((uint32_t) ((0x30) | ((0x26) << 8) | ((0xB2) << 16
) | (((uint32_t) (0x75)) << 24)))
&& buffer [1] == MAKE_MARKER (0x8E, 0x66, 0xCF, 0x11)((uint32_t) ((0x8E) | ((0x66) << 8) | ((0xCF) << 16
) | (((uint32_t) (0x11)) << 24)))
)
2507 return 0 /*-SF_FORMAT_WMA-*/ ;
2508
2509 /* HMM (Hidden Markov Model) Tool Kit. */
2510 if (2 * BE2H_32 (buffer [0])ENDSWAP_32 (buffer [0]) + 12 == psf->filelength && buffer [2] == MAKE_MARKER (0, 2, 0, 0)((uint32_t) ((0) | ((2) << 8) | ((0) << 16) | (((
uint32_t) (0)) << 24)))
)
2511 return SF_FORMAT_HTK ;
2512
2513 if (buffer [0] == MAKE_MARKER ('f', 'L', 'a', 'C')((uint32_t) (('f') | (('L') << 8) | (('a') << 16)
| (((uint32_t) ('C')) << 24)))
)
2514 return SF_FORMAT_FLAC ;
2515
2516 if (buffer [0] == MAKE_MARKER ('2', 'B', 'I', 'T')((uint32_t) (('2') | (('B') << 8) | (('I') << 16)
| (((uint32_t) ('T')) << 24)))
)
2517 return SF_FORMAT_AVR ;
2518
2519 if (buffer [0] == MAKE_MARKER ('R', 'F', '6', '4')((uint32_t) (('R') | (('F') << 8) | (('6') << 16)
| (((uint32_t) ('4')) << 24)))
&& buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E')((uint32_t) (('W') | (('A') << 8) | (('V') << 16)
| (((uint32_t) ('E')) << 24)))
)
2520 return SF_FORMAT_RF64 ;
2521
2522 if (buffer [0] == MAKE_MARKER ('I', 'D', '3', 3)((uint32_t) (('I') | (('D') << 8) | (('3') << 16)
| (((uint32_t) (3)) << 24)))
)
2523 { psf_log_printf (psf, "Found 'ID3' marker.\n") ;
2524 if (id3_skip (psf))
2525 return guess_file_type (psf) ;
2526 return 0 ;
2527 } ;
2528
2529 /* Turtle Beach SMP 16-bit */
2530 if (buffer [0] == MAKE_MARKER ('S', 'O', 'U', 'N')((uint32_t) (('S') | (('O') << 8) | (('U') << 16)
| (((uint32_t) ('N')) << 24)))
&& buffer [1] == MAKE_MARKER ('D', ' ', 'S', 'A')((uint32_t) (('D') | ((' ') << 8) | (('S') << 16)
| (((uint32_t) ('A')) << 24)))
)
2531 return 0 ;
2532
2533 /* Yamaha sampler format. */
2534 if (buffer [0] == MAKE_MARKER ('S', 'Y', '8', '0')((uint32_t) (('S') | (('Y') << 8) | (('8') << 16)
| (((uint32_t) ('0')) << 24)))
|| buffer [0] == MAKE_MARKER ('S', 'Y', '8', '5')((uint32_t) (('S') | (('Y') << 8) | (('8') << 16)
| (((uint32_t) ('5')) << 24)))
)
2535 return 0 ;
2536
2537 if (buffer [0] == MAKE_MARKER ('a', 'j', 'k', 'g')((uint32_t) (('a') | (('j') << 8) | (('k') << 16)
| (((uint32_t) ('g')) << 24)))
)
2538 return 0 /*-SF_FORMAT_SHN-*/ ;
2539
2540 /* This must be the last one. */
2541 if (psf->filelength > 0 && (format = try_resource_fork (psf)) != 0)
2542 return format ;
2543
2544 return 0 ;
2545} /* guess_file_type */
2546
2547
2548static int
2549validate_sfinfo (SF_INFO *sfinfo)
2550{ if (sfinfo->samplerate < 1)
2551 return 0 ;
2552 if (sfinfo->frames < 0)
2553 return 0 ;
2554 if (sfinfo->channels < 1)
2555 return 0 ;
2556 if ((SF_CONTAINER (sfinfo->format)((sfinfo->format) & SF_FORMAT_TYPEMASK)) == 0)
2557 return 0 ;
2558 if ((SF_CODEC (sfinfo->format)((sfinfo->format) & SF_FORMAT_SUBMASK)) == 0)
2559 return 0 ;
2560 if (sfinfo->sections < 1)
2561 return 0 ;
2562 return 1 ;
2563} /* validate_sfinfo */
2564
2565static int
2566validate_psf (SF_PRIVATE *psf)
2567{
2568 if (psf->datalength < 0)
2569 { psf_log_printf (psf, "Invalid SF_PRIVATE field : datalength == %D.\n", psf->datalength) ;
2570 return 0 ;
2571 } ;
2572 if (psf->dataoffset < 0)
2573 { psf_log_printf (psf, "Invalid SF_PRIVATE field : dataoffset == %D.\n", psf->dataoffset) ;
2574 return 0 ;
2575 } ;
2576 if (psf->blockwidth && psf->blockwidth != psf->sf.channels * psf->bytewidth)
2577 { psf_log_printf (psf, "Invalid SF_PRIVATE field : channels * bytewidth == %d.\n",
2578 psf->sf.channels * psf->bytewidth) ;
2579 return 0 ;
2580 } ;
2581 return 1 ;
2582} /* validate_psf */
2583
2584static void
2585save_header_info (SF_PRIVATE *psf)
2586{ snprintf (sf_parselog, sizeof (sf_parselog), "%s", psf->parselog.buf)__builtin___snprintf_chk (sf_parselog, sizeof (sf_parselog), 2
- 1, __builtin_object_size (sf_parselog, 2 > 1), "%s", psf
->parselog.buf)
;
2587} /* save_header_info */
2588
2589static void
2590copy_filename (SF_PRIVATE *psf, const char *path)
2591{ const char *ccptr ;
2592 char *cptr ;
2593
2594 snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", path)__builtin___snprintf_chk (psf->file.path.c, sizeof (psf->
file.path.c), 2 - 1, __builtin_object_size (psf->file.path
.c, 2 > 1), "%s", path)
;
2595 if ((ccptr = strrchr (path, '/')) || (ccptr = strrchr (path, '\\')))
2596 ccptr ++ ;
2597 else
2598 ccptr = path ;
2599
2600 snprintf (psf->file.name.c, sizeof (psf->file.name.c), "%s", ccptr)__builtin___snprintf_chk (psf->file.name.c, sizeof (psf->
file.name.c), 2 - 1, __builtin_object_size (psf->file.name
.c, 2 > 1), "%s", ccptr)
;
2601
2602 /* Now grab the directory. */
2603 snprintf (psf->file.dir.c, sizeof (psf->file.dir.c), "%s", path)__builtin___snprintf_chk (psf->file.dir.c, sizeof (psf->
file.dir.c), 2 - 1, __builtin_object_size (psf->file.dir.c
, 2 > 1), "%s", path)
;
2604 if ((cptr = strrchr (psf->file.dir.c, '/')) || (cptr = strrchr (psf->file.dir.c, '\\')))
2605 cptr [1] = 0 ;
2606 else
2607 psf->file.dir.c [0] = 0 ;
2608
2609 return ;
2610} /* copy_filename */
2611
2612/*==============================================================================
2613*/
2614
2615static int
2616psf_close (SF_PRIVATE *psf)
2617{ uint32_t k ;
2618 int error = 0 ;
2619
2620 if (psf->codec_close)
2621 { error = psf->codec_close (psf) ;
2622 /* To prevent it being called in psf->container_close(). */
2623 psf->codec_close = NULL((void*)0) ;
2624 } ;
2625
2626 if (psf->container_close)
2627 error = psf->container_close (psf) ;
Value stored to 'error' is never read
2628
2629 error = psf_fclose (psf) ;
2630 psf_close_rsrc (psf) ;
2631
2632 /* For an ISO C compliant implementation it is ok to free a NULL pointer. */
2633 free (psf->container_data) ;
2634 free (psf->codec_data) ;
2635 free (psf->interleave) ;
2636 free (psf->dither) ;
2637 free (psf->peak_info) ;
2638 free (psf->broadcast_16k) ;
2639 free (psf->loop_info) ;
2640 free (psf->instrument) ;
2641 free (psf->channel_map) ;
2642 free (psf->format_desc) ;
2643 free (psf->strings.storage) ;
2644
2645 if (psf->wchunks.chunks)
2646 for (k = 0 ; k < psf->wchunks.used ; k++)
2647 free (psf->wchunks.chunks [k].data) ;
2648 free (psf->rchunks.chunks) ;
2649 free (psf->wchunks.chunks) ;
2650 free (psf->iterator) ;
2651 free (psf->cart_16k) ;
2652
2653 memset (psf, 0, sizeof (SF_PRIVATE)) ;
2654 free (psf) ;
2655
2656 return error ;
2657} /* psf_close */
2658
2659SNDFILE *
2660psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo)
2661{ int error, format ;
2662
2663 sf_errno = error = 0 ;
2664 sf_parselog [0] = 0 ;
2665
2666 if (psf->error)
2667 { error = psf->error ;
2668 goto error_exit ;
2669 } ;
2670
2671 if (psf->file.mode != SFM_READ && psf->file.mode != SFM_WRITE && psf->file.mode != SFM_RDWR)
2672 { error = SFE_BAD_OPEN_MODE ;
2673 goto error_exit ;
2674 } ;
2675
2676 if (sfinfo == NULL((void*)0))
2677 { error = SFE_BAD_SF_INFO_PTR ;
2678 goto error_exit ;
2679 } ;
2680
2681 /* Zero out these fields. */
2682 sfinfo->frames = 0 ;
2683 sfinfo->sections = 0 ;
2684 sfinfo->seekable = 0 ;
2685
2686 if (psf->file.mode == SFM_READ)
2687 { if ((SF_CONTAINER (sfinfo->format)((sfinfo->format) & SF_FORMAT_TYPEMASK)) == SF_FORMAT_RAW)
2688 { if (sf_format_check (sfinfo) == 0)
2689 { error = SFE_RAW_BAD_FORMAT ;
2690 goto error_exit ;
2691 } ;
2692 }
2693 else
2694 memset (sfinfo, 0, sizeof (SF_INFO)) ;
2695 } ;
2696
2697 memcpy (&psf->sf, sfinfo, sizeof (SF_INFO)) ;
2698
2699 psf->Magick = SNDFILE_MAGICK0x1234C0DE ;
2700 psf->norm_float = SF_TRUE ;
2701 psf->norm_double = SF_TRUE ;
2702 psf->dataoffset = -1 ;
2703 psf->datalength = -1 ;
2704 psf->read_current = -1 ;
2705 psf->write_current = -1 ;
2706 psf->auto_header = SF_FALSE ;
2707 psf->rwf_endian = SF_ENDIAN_LITTLE ;
2708 psf->seek = psf_default_seek ;
2709 psf->float_int_mult = 0 ;
2710 psf->float_max = -1.0 ;
2711
2712 /* An attempt at a per SF_PRIVATE unique id. */
2713 psf->unique_id = psf_rand_int32 () ;
2714
2715 psf->sf.sections = 1 ;
2716
2717 psf->is_pipe = psf_is_pipe (psf) ;
2718
2719 if (psf->is_pipe)
2720 { psf->sf.seekable = SF_FALSE ;
2721 psf->filelength = SF_COUNT_MAX0x7FFFFFFFFFFFFFFFLL ;
2722 }
2723 else
2724 { psf->sf.seekable = SF_TRUE ;
2725
2726 /* File is open, so get the length. */
2727 psf->filelength = psf_get_filelen (psf) ;
2728 } ;
2729
2730 if (psf->fileoffset > 0)
2731 { switch (psf->file.mode)
2732 { case SFM_READ :
2733 if (psf->filelength < 44)
2734 { psf_log_printf (psf, "Short filelength: %D (fileoffset: %D)\n", psf->filelength, psf->fileoffset) ;
2735 error = SFE_BAD_OFFSET ;
2736 goto error_exit ;
2737 } ;
2738 break ;
2739
2740 case SFM_WRITE :
2741 psf->fileoffset = 0 ;
2742 psf_fseek (psf, 0, SEEK_END2) ;
2743 psf->fileoffset = psf_ftell (psf) ;
2744 break ;
2745
2746 case SFM_RDWR :
2747 error = SFE_NO_EMBEDDED_RDWR ;
2748 goto error_exit ;
2749 } ;
2750
2751 psf_log_printf (psf, "Embedded file offset : %D\n", psf->fileoffset) ;
2752 } ;
2753
2754 if (psf->filelength == SF_COUNT_MAX0x7FFFFFFFFFFFFFFFLL)
2755 psf_log_printf (psf, "Length : unknown\n") ;
2756 else
2757 psf_log_printf (psf, "Length : %D\n", psf->filelength) ;
2758
2759 if (psf->file.mode == SFM_WRITE || (psf->file.mode == SFM_RDWR && psf->filelength == 0))
2760 { /* If the file is being opened for write or RDWR and the file is currently
2761 ** empty, then the SF_INFO struct must contain valid data.
2762 */
2763 if ((SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK)) == 0)
2764 { error = SFE_ZERO_MAJOR_FORMAT ;
2765 goto error_exit ;
2766 } ;
2767 if ((SF_CODEC (psf->sf.format)((psf->sf.format) & SF_FORMAT_SUBMASK)) == 0)
2768 { error = SFE_ZERO_MINOR_FORMAT ;
2769 goto error_exit ;
2770 } ;
2771
2772 if (sf_format_check (&psf->sf) == 0)
2773 { error = SFE_BAD_OPEN_FORMAT ;
2774 goto error_exit ;
2775 } ;
2776 }
2777 else if ((SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK)) != SF_FORMAT_RAW)
2778 { /* If type RAW has not been specified then need to figure out file type. */
2779 psf->sf.format = guess_file_type (psf) ;
2780
2781 if (psf->sf.format == 0)
2782 psf->sf.format = format_from_extension (psf) ;
2783 } ;
2784
2785 /* Prevent unnecessary seeks */
2786 psf->last_op = psf->file.mode ;
2787
2788 /* Set bytewidth if known. */
2789 switch (SF_CODEC (psf->sf.format)((psf->sf.format) & SF_FORMAT_SUBMASK))
2790 { case SF_FORMAT_PCM_S8 :
2791 case SF_FORMAT_PCM_U8 :
2792 case SF_FORMAT_ULAW :
2793 case SF_FORMAT_ALAW :
2794 case SF_FORMAT_DPCM_8 :
2795 psf->bytewidth = 1 ;
2796 break ;
2797
2798 case SF_FORMAT_PCM_16 :
2799 case SF_FORMAT_DPCM_16 :
2800 psf->bytewidth = 2 ;
2801 break ;
2802
2803 case SF_FORMAT_PCM_24 :
2804 psf->bytewidth = 3 ;
2805 break ;
2806
2807 case SF_FORMAT_PCM_32 :
2808 case SF_FORMAT_FLOAT :
2809 psf->bytewidth = 4 ;
2810 break ;
2811
2812 case SF_FORMAT_DOUBLE :
2813 psf->bytewidth = 8 ;
2814 break ;
2815 } ;
2816
2817 /* Call the initialisation function for the relevant file type. */
2818 switch (SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK))
2819 { case SF_FORMAT_WAV :
2820 case SF_FORMAT_WAVEX :
2821 error = wav_open (psf) ;
2822 break ;
2823
2824 case SF_FORMAT_AIFF :
2825 error = aiff_open (psf) ;
2826 break ;
2827
2828 case SF_FORMAT_AU :
2829 error = au_open (psf) ;
2830 break ;
2831
2832 case SF_FORMAT_RAW :
2833 error = raw_open (psf) ;
2834 break ;
2835
2836 case SF_FORMAT_W64 :
2837 error = w64_open (psf) ;
2838 break ;
2839
2840 case SF_FORMAT_RF64 :
2841 error = rf64_open (psf) ;
2842 break ;
2843
2844 /* Lite remove start */
2845 case SF_FORMAT_PAF :
2846 error = paf_open (psf) ;
2847 break ;
2848
2849 case SF_FORMAT_SVX :
2850 error = svx_open (psf) ;
2851 break ;
2852
2853 case SF_FORMAT_NIST :
2854 error = nist_open (psf) ;
2855 break ;
2856
2857 case SF_FORMAT_IRCAM :
2858 error = ircam_open (psf) ;
2859 break ;
2860
2861 case SF_FORMAT_VOC :
2862 error = voc_open (psf) ;
2863 break ;
2864
2865 case SF_FORMAT_SDS :
2866 error = sds_open (psf) ;
2867 break ;
2868
2869 case SF_FORMAT_OGG :
2870 error = ogg_open (psf) ;
2871 break ;
2872
2873 case SF_FORMAT_TXW :
2874 error = txw_open (psf) ;
2875 break ;
2876
2877 case SF_FORMAT_WVE :
2878 error = wve_open (psf) ;
2879 break ;
2880
2881 case SF_FORMAT_DWD :
2882 error = dwd_open (psf) ;
2883 break ;
2884
2885 case SF_FORMAT_MAT4 :
2886 error = mat4_open (psf) ;
2887 break ;
2888
2889 case SF_FORMAT_MAT5 :
2890 error = mat5_open (psf) ;
2891 break ;
2892
2893 case SF_FORMAT_PVF :
2894 error = pvf_open (psf) ;
2895 break ;
2896
2897 case SF_FORMAT_XI :
2898 error = xi_open (psf) ;
2899 break ;
2900
2901 case SF_FORMAT_HTK :
2902 error = htk_open (psf) ;
2903 break ;
2904
2905 case SF_FORMAT_SD2 :
2906 error = sd2_open (psf) ;
2907 break ;
2908
2909 case SF_FORMAT_REX2 :
2910 error = rx2_open (psf) ;
2911 break ;
2912
2913 case SF_FORMAT_AVR :
2914 error = avr_open (psf) ;
2915 break ;
2916
2917 case SF_FORMAT_FLAC :
2918 error = flac_open (psf) ;
2919 break ;
2920
2921 case SF_FORMAT_CAF :
2922 error = caf_open (psf) ;
2923 break ;
2924
2925 case SF_FORMAT_MPC2K :
2926 error = mpc2k_open (psf) ;
2927 break ;
2928
2929 /* Lite remove end */
2930
2931 default :
2932 error = SFE_UNKNOWN_FORMAT ;
2933 } ;
2934
2935 if (error)
2936 goto error_exit ;
2937
2938 /* For now, check whether embedding is supported. */
2939 format = SF_CONTAINER (psf->sf.format)((psf->sf.format) & SF_FORMAT_TYPEMASK) ;
2940 if (psf->fileoffset > 0)
2941 { switch (format)
2942 { case SF_FORMAT_WAV :
2943 case SF_FORMAT_WAVEX :
2944 case SF_FORMAT_AIFF :
2945 case SF_FORMAT_AU :
2946 /* Actual embedded files. */
2947 break ;
2948
2949 case SF_FORMAT_FLAC :
2950 /* Flac with an ID3v2 header? */
2951 break ;
2952
2953 default :
2954 error = SFE_NO_EMBED_SUPPORT ;
2955 goto error_exit ;
2956 } ;
2957 } ;
2958
2959 if (psf->fileoffset > 0)
2960 psf_log_printf (psf, "Embedded file length : %D\n", psf->filelength) ;
2961
2962 if (psf->file.mode == SFM_RDWR && sf_format_check (&psf->sf) == 0)
2963 { error = SFE_BAD_MODE_RW ;
2964 goto error_exit ;
2965 } ;
2966
2967 if (validate_sfinfo (&psf->sf) == 0)
2968 { psf_log_SF_INFO (psf) ;
2969 save_header_info (psf) ;
2970 error = SFE_BAD_SF_INFO ;
2971 goto error_exit ;
2972 } ;
2973
2974 if (validate_psf (psf) == 0)
2975 { save_header_info (psf) ;
2976 error = SFE_INTERNAL ;
2977 goto error_exit ;
2978 } ;
2979
2980 psf->read_current = 0 ;
2981 psf->write_current = 0 ;
2982 if (psf->file.mode == SFM_RDWR)
2983 { psf->write_current = psf->sf.frames ;
2984 psf->have_written = psf->sf.frames > 0 ? SF_TRUE : SF_FALSE ;
2985 } ;
2986
2987 memcpy (sfinfo, &psf->sf, sizeof (SF_INFO)) ;
2988
2989 return (SNDFILE *) psf ;
2990
2991error_exit :
2992 sf_errno = error ;
2993
2994 if (error == SFE_SYSTEM)
2995 snprintf (sf_syserr, sizeof (sf_syserr), "%s", psf->syserr)__builtin___snprintf_chk (sf_syserr, sizeof (sf_syserr), 2 - 1
, __builtin_object_size (sf_syserr, 2 > 1), "%s", psf->
syserr)
;
2996 snprintf (sf_parselog, sizeof (sf_parselog), "%s", psf->parselog.buf)__builtin___snprintf_chk (sf_parselog, sizeof (sf_parselog), 2
- 1, __builtin_object_size (sf_parselog, 2 > 1), "%s", psf
->parselog.buf)
;
2997
2998 switch (error)
2999 { case SF_ERR_SYSTEM :
3000 case SF_ERR_UNSUPPORTED_ENCODING :
3001 case SFE_UNIMPLEMENTED :
3002 break ;
3003
3004 case SFE_RAW_BAD_FORMAT :
3005 break ;
3006
3007 default :
3008 if (psf->file.mode == SFM_READ)
3009 { psf_log_printf (psf, "Parse error : %s\n", sf_error_number (error)) ;
3010 error = SF_ERR_MALFORMED_FILE ;
3011 } ;
3012 } ;
3013
3014 psf_close (psf) ;
3015 return NULL((void*)0) ;
3016} /* psf_open_file */
3017
3018/*==============================================================================
3019** Chunk getting and setting.
3020*/
3021
3022int
3023sf_set_chunk (SNDFILE * sndfile, const SF_CHUNK_INFO * chunk_info)
3024{ SF_PRIVATE *psf ;
3025
3026 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
3027
3028 if (chunk_info == NULL((void*)0) || chunk_info->data == NULL((void*)0))
3029 return SFE_BAD_CHUNK_PTR ;
3030
3031 if (psf->set_chunk)
3032 return psf->set_chunk (psf, chunk_info) ;
3033
3034 return SFE_BAD_CHUNK_FORMAT ;
3035} /* sf_set_chunk */
3036
3037SF_CHUNK_ITERATOR *
3038sf_get_chunk_iterator (SNDFILE * sndfile, const SF_CHUNK_INFO * chunk_info)
3039{ SF_PRIVATE *psf ;
3040
3041 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
3042
3043 if (chunk_info)
3044 return psf_get_chunk_iterator (psf, chunk_info->id) ;
3045
3046 return psf_get_chunk_iterator (psf, NULL((void*)0)) ;
3047} /* sf_get_chunk_iterator */
3048
3049SF_CHUNK_ITERATOR *
3050sf_next_chunk_iterator (SF_CHUNK_ITERATOR * iterator)
3051{ SF_PRIVATE *psf ;
3052 SNDFILE *sndfile = iterator ? iterator->sndfile : NULL((void*)0) ;
3053
3054 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
3055
3056 if (psf->next_chunk_iterator)
3057 return psf->next_chunk_iterator (psf, iterator) ;
3058
3059 return NULL((void*)0) ;
3060} /* sf_get_chunk_iterator_next */
3061
3062int
3063sf_get_chunk_size (const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
3064{ SF_PRIVATE *psf ;
3065 SNDFILE *sndfile = iterator ? iterator->sndfile : NULL((void*)0) ;
3066
3067 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
3068
3069 if (chunk_info == NULL((void*)0))
3070 return SFE_BAD_CHUNK_PTR ;
3071
3072 if (psf->get_chunk_size)
3073 return psf->get_chunk_size (psf, iterator, chunk_info) ;
3074
3075 return SFE_BAD_CHUNK_FORMAT ;
3076 return 0 ;
3077} /* sf_get_chunk_size */
3078
3079int
3080sf_get_chunk_data (const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
3081{ SF_PRIVATE *psf ;
3082 SNDFILE *sndfile = iterator ? iterator->sndfile : NULL((void*)0) ;
3083
3084 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1){ if ((sndfile) == ((void*)0)) { sf_errno = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; (psf) = (SF_PRIVATE*) (sndfile) ; if ((psf)
->virtual_io == SF_FALSE && psf_file_valid (psf) ==
0) { (psf)->error = SFE_BAD_FILE_PTR ; return 0 ; } ; if (
(psf)->Magick != 0x1234C0DE) { (psf)->error = SFE_BAD_SNDFILE_PTR
; return 0 ; } ; if (1) (psf)->error = 0 ; }
;
3085
3086 if (chunk_info == NULL((void*)0) || chunk_info->data == NULL((void*)0))
3087 return SFE_BAD_CHUNK_PTR ;
3088
3089 if (psf->get_chunk_data)
3090 return psf->get_chunk_data (psf, iterator, chunk_info) ;
3091
3092 return SFE_BAD_CHUNK_FORMAT ;
3093} /* sf_get_chunk_data */