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