Coverage Report

Created: 2024-06-17 06:31

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