Coverage Report

Created: 2025-08-26 06:36

/src/libsndfile/src/aiff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** Copyright (C) 1999-2018 Erik de Castro Lopo <erikd@mega-nerd.com>
3
** Copyright (C) 2005 David Viens <davidv@plogue.com>
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU Lesser General Public License as published by
7
** the Free Software Foundation; either version 2.1 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU Lesser General Public License for more details.
14
**
15
** You should have received a copy of the GNU Lesser General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
*/
19
20
#include "sfconfig.h"
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <time.h>
26
#include <ctype.h>
27
#include <inttypes.h>
28
29
#include "sndfile.h"
30
#include "sfendian.h"
31
#include "common.h"
32
#include "chanmap.h"
33
34
/*------------------------------------------------------------------------------
35
 * Macros to handle big/little endian issues.
36
 */
37
38
5.63k
#define FORM_MARKER   (MAKE_MARKER ('F', 'O', 'R', 'M'))
39
5.62k
#define AIFF_MARKER   (MAKE_MARKER ('A', 'I', 'F', 'F'))
40
8.84k
#define AIFC_MARKER   (MAKE_MARKER ('A', 'I', 'F', 'C'))
41
9.77k
#define COMM_MARKER   (MAKE_MARKER ('C', 'O', 'M', 'M'))
42
1.58k
#define SSND_MARKER   (MAKE_MARKER ('S', 'S', 'N', 'D'))
43
1.18k
#define MARK_MARKER   (MAKE_MARKER ('M', 'A', 'R', 'K'))
44
2.85k
#define INST_MARKER   (MAKE_MARKER ('I', 'N', 'S', 'T'))
45
1.68k
#define APPL_MARKER   (MAKE_MARKER ('A', 'P', 'P', 'L'))
46
842
#define CHAN_MARKER   (MAKE_MARKER ('C', 'H', 'A', 'N'))
47
48
875
#define c_MARKER    (MAKE_MARKER ('(', 'c', ')', ' '))
49
1.67k
#define NAME_MARKER   (MAKE_MARKER ('N', 'A', 'M', 'E'))
50
613
#define AUTH_MARKER   (MAKE_MARKER ('A', 'U', 'T', 'H'))
51
383
#define ANNO_MARKER   (MAKE_MARKER ('A', 'N', 'N', 'O'))
52
824
#define COMT_MARKER   (MAKE_MARKER ('C', 'O', 'M', 'T'))
53
133
#define FVER_MARKER   (MAKE_MARKER ('F', 'V', 'E', 'R'))
54
459
#define SFX_MARKER    (MAKE_MARKER ('S', 'F', 'X', '!'))
55
56
527
#define PEAK_MARKER   (MAKE_MARKER ('P', 'E', 'A', 'K'))
57
705
#define basc_MARKER   (MAKE_MARKER ('b', 'a', 's', 'c'))
58
59
/* Supported AIFC encodings.*/
60
3.29k
#define NONE_MARKER   (MAKE_MARKER ('N', 'O', 'N', 'E'))
61
79
#define sowt_MARKER   (MAKE_MARKER ('s', 'o', 'w', 't'))
62
112
#define twos_MARKER   (MAKE_MARKER ('t', 'w', 'o', 's'))
63
136
#define raw_MARKER    (MAKE_MARKER ('r', 'a', 'w', ' '))
64
182
#define in24_MARKER   (MAKE_MARKER ('i', 'n', '2', '4'))
65
90
#define ni24_MARKER   (MAKE_MARKER ('4', '2', 'n', '1'))
66
256
#define in32_MARKER   (MAKE_MARKER ('i', 'n', '3', '2'))
67
114
#define ni32_MARKER   (MAKE_MARKER ('2', '3', 'n', 'i'))
68
69
19.3k
#define fl32_MARKER   (MAKE_MARKER ('f', 'l', '3', '2'))
70
10.2k
#define FL32_MARKER   (MAKE_MARKER ('F', 'L', '3', '2'))
71
19.1k
#define fl64_MARKER   (MAKE_MARKER ('f', 'l', '6', '4'))
72
9.63k
#define FL64_MARKER   (MAKE_MARKER ('F', 'L', '6', '4'))
73
74
1.03k
#define ulaw_MARKER   (MAKE_MARKER ('u', 'l', 'a', 'w'))
75
1.12k
#define ULAW_MARKER   (MAKE_MARKER ('U', 'L', 'A', 'W'))
76
437
#define alaw_MARKER   (MAKE_MARKER ('a', 'l', 'a', 'w'))
77
882
#define ALAW_MARKER   (MAKE_MARKER ('A', 'L', 'A', 'W'))
78
79
1.43k
#define DWVW_MARKER   (MAKE_MARKER ('D', 'W', 'V', 'W'))
80
430
#define GSM_MARKER    (MAKE_MARKER ('G', 'S', 'M', ' '))
81
385
#define ima4_MARKER   (MAKE_MARKER ('i', 'm', 'a', '4'))
82
83
/*
84
**  This value is officially assigned to Mega Nerd Pty Ltd by Apple
85
**  Corportation as the Application marker for libsndfile.
86
**
87
**  See : http://developer.apple.com/faq/datatype.html
88
*/
89
#define m3ga_MARKER   (MAKE_MARKER ('m', '3', 'g', 'a'))
90
91
/* Unsupported AIFC encodings.*/
92
93
#define MAC3_MARKER   (MAKE_MARKER ('M', 'A', 'C', '3'))
94
#define MAC6_MARKER   (MAKE_MARKER ('M', 'A', 'C', '6'))
95
#define ADP4_MARKER   (MAKE_MARKER ('A', 'D', 'P', '4'))
96
97
/* Predefined chunk sizes. */
98
9.77k
#define SIZEOF_AIFF_COMM    18
99
8.41k
#define SIZEOF_AIFC_COMM_MIN  22
100
10.2k
#define SIZEOF_AIFC_COMM    24
101
0
#define SIZEOF_SSND_CHUNK   8
102
3.81k
#define SIZEOF_INST_CHUNK   20
103
104
/* Is it constant? */
105
106
/* AIFC/IMA4 defines. */
107
259
#define AIFC_IMA4_BLOCK_LEN       34
108
259
#define AIFC_IMA4_SAMPLES_PER_BLOCK   64
109
110
525
#define AIFF_PEAK_CHUNK_SIZE(ch)  (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
111
112
/*------------------------------------------------------------------------------
113
 * Typedefs for file chunks.
114
 */
115
116
enum
117
{ HAVE_FORM   = 0x01,
118
  HAVE_AIFF   = 0x02,
119
  HAVE_AIFC   = 0x04,
120
  HAVE_FVER   = 0x08,
121
  HAVE_COMM   = 0x10,
122
  HAVE_SSND   = 0x20
123
} ;
124
125
typedef struct
126
{ uint32_t  size ;
127
  int16_t   numChannels ;
128
  uint32_t  numSampleFrames ;
129
  int16_t   sampleSize ;
130
  uint8_t   sampleRate [10] ;
131
  uint32_t  encoding ;
132
  char      zero_bytes [2] ;
133
} COMM_CHUNK ;
134
135
typedef struct
136
{ uint32_t  offset ;
137
  uint32_t  blocksize ;
138
} SSND_CHUNK ;
139
140
typedef struct
141
{ int16_t   playMode ;
142
  uint16_t  beginLoop ;
143
  uint16_t  endLoop ;
144
} INST_LOOP ;
145
146
typedef struct
147
{ int8_t    baseNote ;    /* all notes are MIDI note numbers */
148
  int8_t    detune ;    /* cents off, only -50 to +50 are significant */
149
  int8_t    lowNote ;
150
  int8_t    highNote ;
151
  int8_t    lowVelocity ; /* 1 to 127 */
152
  int8_t    highVelocity ;  /* 1 to 127 */
153
  int16_t   gain ;      /* in dB, 0 is normal */
154
  INST_LOOP sustain_loop ;
155
  INST_LOOP release_loop ;
156
} INST_CHUNK ;
157
158
159
enum
160
{ basc_SCALE_MINOR = 1,
161
  basc_SCALE_MAJOR,
162
  basc_SCALE_NEITHER,
163
  basc_SCALE_BOTH
164
} ;
165
166
enum
167
{ basc_TYPE_LOOP = 0,
168
  basc_TYPE_ONE_SHOT
169
} ;
170
171
172
typedef struct
173
{ uint32_t  version ;
174
  uint32_t  numBeats ;
175
  uint16_t  rootNote ;
176
  uint16_t  scaleType ;
177
  uint16_t  sigNumerator ;
178
  uint16_t  sigDenominator ;
179
  uint16_t  loopType ;
180
} basc_CHUNK ;
181
182
typedef struct
183
{ uint16_t  markerID ;
184
  uint32_t  position ;
185
} MARK_ID_POS ;
186
187
typedef struct
188
{ sf_count_t  comm_offset ;
189
  sf_count_t  ssnd_offset ;
190
191
  int32_t   chanmap_tag ;
192
193
  MARK_ID_POS *markstr ;
194
} AIFF_PRIVATE ;
195
196
/*------------------------------------------------------------------------------
197
 * Private static functions.
198
 */
199
200
static int  aiff_close (SF_PRIVATE *psf) ;
201
202
static int  tenbytefloat2int (uint8_t *bytes) ;
203
static void uint2tenbytefloat (uint32_t num, uint8_t *bytes) ;
204
205
static int  aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
206
207
static int  aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
208
209
static int  aiff_write_header (SF_PRIVATE *psf, int calc_length) ;
210
static int  aiff_write_tailer (SF_PRIVATE *psf) ;
211
static void aiff_write_strings (SF_PRIVATE *psf, int location) ;
212
213
static int  aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
214
215
static const char *get_loop_mode_str (int16_t mode) ;
216
217
static int16_t get_loop_mode (int16_t mode) ;
218
219
static int aiff_read_basc_chunk (SF_PRIVATE * psf, int) ;
220
221
static int aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword) ;
222
223
static uint32_t marker_to_position (const MARK_ID_POS *m, uint16_t n, int marksize) ;
224
225
static int aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
226
static SF_CHUNK_ITERATOR * aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
227
static int aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
228
static int aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
229
230
/*------------------------------------------------------------------------------
231
** Public function.
232
*/
233
234
int
235
aiff_open (SF_PRIVATE *psf)
236
5.62k
{ COMM_CHUNK comm_fmt ;
237
5.62k
  int error = 0, subformat ;
238
239
5.62k
  memset (&comm_fmt, 0, sizeof (comm_fmt)) ;
240
241
5.62k
  subformat = SF_CODEC (psf->sf.format) ;
242
243
5.62k
  if ((psf->container_data = calloc (1, sizeof (AIFF_PRIVATE))) == NULL)
244
0
    return SFE_MALLOC_FAILED ;
245
246
5.62k
  psf->container_close = aiff_close ;
247
248
5.62k
  if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
249
5.62k
  { if ((error = aiff_read_header (psf, &comm_fmt)))
250
3.98k
      return error ;
251
252
1.64k
    psf->next_chunk_iterator = aiff_next_chunk_iterator ;
253
1.64k
    psf->get_chunk_size = aiff_get_chunk_size ;
254
1.64k
    psf->get_chunk_data = aiff_get_chunk_data ;
255
256
1.64k
    psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
257
1.64k
    } ;
258
259
1.64k
  if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
260
0
  { if (psf->is_pipe)
261
0
      return SFE_NO_PIPE_WRITE ;
262
263
0
    if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_AIFF)
264
0
      return SFE_BAD_OPEN_FORMAT ;
265
266
0
    if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
267
0
    { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
268
0
        return SFE_MALLOC_FAILED ;
269
0
      psf->peak_info->peak_loc = SF_PEAK_START ;
270
0
      } ;
271
272
0
    if (psf->file.mode != SFM_RDWR || psf->filelength < 40)
273
0
    { psf->filelength = 0 ;
274
0
      psf->datalength = 0 ;
275
0
      psf->dataoffset = 0 ;
276
0
      psf->sf.frames = 0 ;
277
0
      } ;
278
279
0
    psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
280
281
0
    if ((error = aiff_write_header (psf, SF_FALSE)))
282
0
      return error ;
283
284
0
    psf->write_header = aiff_write_header ;
285
0
    psf->set_chunk    = aiff_set_chunk ;
286
1.64k
    } ;
287
288
1.64k
  psf->command = aiff_command ;
289
290
1.64k
  switch (SF_CODEC (psf->sf.format))
291
1.64k
  { case SF_FORMAT_PCM_U8 :
292
50
        error = pcm_init (psf) ;
293
50
        break ;
294
295
4
    case SF_FORMAT_PCM_S8 :
296
4
        error = pcm_init (psf) ;
297
4
        break ;
298
299
19
    case SF_FORMAT_PCM_16 :
300
38
    case SF_FORMAT_PCM_24 :
301
47
    case SF_FORMAT_PCM_32 :
302
47
        error = pcm_init (psf) ;
303
47
        break ;
304
305
33
    case SF_FORMAT_ULAW :
306
33
        error = ulaw_init (psf) ;
307
33
        break ;
308
309
37
    case SF_FORMAT_ALAW :
310
37
        error = alaw_init (psf) ;
311
37
        break ;
312
313
    /* Lite remove start */
314
47
    case SF_FORMAT_FLOAT :
315
47
        error = float32_init (psf) ;
316
47
        break ;
317
318
55
    case SF_FORMAT_DOUBLE :
319
55
        error = double64_init (psf) ;
320
55
        break ;
321
322
17
    case SF_FORMAT_DWVW_12 :
323
17
        if (psf->sf.frames > comm_fmt.numSampleFrames)
324
0
          psf->sf.frames = comm_fmt.numSampleFrames ;
325
17
        break ;
326
327
120
    case SF_FORMAT_DWVW_16 :
328
120
        error = dwvw_init (psf, 16) ;
329
120
        if (psf->sf.frames > comm_fmt.numSampleFrames)
330
25
          psf->sf.frames = comm_fmt.numSampleFrames ;
331
120
        break ;
332
333
38
    case SF_FORMAT_DWVW_24 :
334
38
        error = dwvw_init (psf, 24) ;
335
38
        if (psf->sf.frames > comm_fmt.numSampleFrames)
336
14
          psf->sf.frames = comm_fmt.numSampleFrames ;
337
38
        break ;
338
339
390
    case SF_FORMAT_DWVW_N :
340
390
        if (psf->file.mode != SFM_READ)
341
0
        { error = SFE_DWVW_BAD_BITWIDTH ;
342
0
          break ;
343
390
          } ;
344
390
        if (comm_fmt.sampleSize >= 8 && comm_fmt.sampleSize < 24)
345
360
        { error = dwvw_init (psf, comm_fmt.sampleSize) ;
346
360
          if (psf->sf.frames > comm_fmt.numSampleFrames)
347
35
            psf->sf.frames = comm_fmt.numSampleFrames ;
348
360
          break ;
349
360
          } ;
350
30
        psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
351
30
        error = SFE_DWVW_BAD_BITWIDTH ;
352
30
        break ;
353
354
259
    case SF_FORMAT_IMA_ADPCM :
355
        /*
356
        **  IMA ADPCM encoded AIFF files always have a block length
357
        **  of 34 which decodes to 64 samples.
358
        */
359
259
        error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
360
259
        break ;
361
    /* Lite remove end */
362
363
330
    case SF_FORMAT_GSM610 :
364
330
        error = gsm610_init (psf) ;
365
330
        if (psf->sf.frames > comm_fmt.numSampleFrames)
366
48
          psf->sf.frames = comm_fmt.numSampleFrames ;
367
330
        break ;
368
369
220
    default : return SFE_UNIMPLEMENTED ;
370
1.64k
    } ;
371
372
1.42k
  if (psf->file.mode != SFM_WRITE && psf->sf.frames - comm_fmt.numSampleFrames != 0)
373
1.16k
  { psf_log_printf (psf,
374
1.16k
      "*** Frame count read from 'COMM' chunk (%u) not equal to frame count\n"
375
1.16k
      "*** calculated from length of 'SSND' chunk (%u).\n",
376
1.16k
      comm_fmt.numSampleFrames, (uint32_t) psf->sf.frames) ;
377
1.16k
    } ;
378
379
1.42k
  return error ;
380
1.64k
} /* aiff_open */
381
382
/*==========================================================================================
383
** Private functions.
384
*/
385
386
/* This function ought to check size */
387
static uint32_t
388
marker_to_position (const MARK_ID_POS *m, uint16_t n, int marksize)
389
222
{ int i ;
390
391
197k
  for (i = 0 ; i < marksize ; i++)
392
197k
    if (m [i].markerID == n)
393
111
      return m [i].position ;
394
111
  return 0 ;
395
222
} /* marker_to_position */
396
397
static int
398
aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
399
5.62k
{ SSND_CHUNK  ssnd_fmt ;
400
5.62k
  AIFF_PRIVATE *paiff ;
401
5.62k
  BUF_UNION ubuf ;
402
5.62k
  uint32_t  chunk_size = 0, FORMsize, SSNDsize, bytesread, mark_count = 0 ;
403
5.62k
  int     k, found_chunk = 0, done = 0, error = 0 ;
404
5.62k
  char    *cptr ;
405
5.62k
  int     instr_found = 0, mark_found = 0 ;
406
407
5.62k
  if (psf->filelength > 0xFFFFFFFFLL)
408
0
    psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
409
410
5.62k
  if ((paiff = psf->container_data) == NULL)
411
0
    return SFE_INTERNAL ;
412
413
5.62k
  paiff->comm_offset = 0 ;
414
5.62k
  paiff->ssnd_offset = 0 ;
415
416
  /* Set position to start of file to begin reading header. */
417
5.62k
  psf_binheader_readf (psf, "p", 0) ;
418
419
5.62k
  memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
420
421
  /* Until recently AIF* file were all BIG endian. */
422
5.62k
  psf->endian = SF_ENDIAN_BIG ;
423
424
  /*  AIFF files can apparently have their chunks in any order. However, they
425
  **  must have a FORM chunk. Approach here is to read all the chunks one by
426
  **  one and then check for the mandatory chunks at the end.
427
  */
428
35.5k
  while (! done)
429
35.4k
  { unsigned  marker ;
430
35.4k
    size_t jump = chunk_size & 1 ;
431
432
35.4k
    marker = chunk_size = 0 ;
433
35.4k
    psf_binheader_readf (psf, "Ejm4", jump, &marker, &chunk_size) ;
434
35.4k
    if (marker == 0)
435
26
    { sf_count_t pos = psf_ftell (psf) ;
436
26
      psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
437
26
      break ;
438
35.4k
      } ;
439
440
35.4k
    if (psf->file.mode == SFM_RDWR && (found_chunk & HAVE_SSND))
441
0
      return SFE_AIFF_RW_SSND_NOT_LAST ;
442
443
35.4k
    psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
444
445
35.4k
    switch (marker)
446
35.4k
    { case FORM_MARKER :
447
5.63k
          if (found_chunk)
448
4
            return SFE_AIFF_NO_FORM ;
449
450
5.62k
          FORMsize = chunk_size ;
451
452
5.62k
          found_chunk |= HAVE_FORM ;
453
5.62k
          psf_binheader_readf (psf, "m", &marker) ;
454
5.62k
          switch (marker)
455
5.62k
          { case AIFC_MARKER :
456
5.62k
            case AIFF_MARKER :
457
5.62k
              found_chunk |= (marker == AIFC_MARKER) ? (HAVE_AIFC | HAVE_AIFF) : HAVE_AIFF ;
458
5.62k
              break ;
459
0
            default :
460
0
              break ;
461
5.62k
            } ;
462
463
5.62k
          if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
464
82
          { /* Set file length. */
465
82
            psf->filelength = FORMsize + 8 ;
466
82
            psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
467
82
            }
468
5.54k
          else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (chunk_size))
469
5.54k
          { chunk_size = psf->filelength - 2 * sizeof (chunk_size) ;
470
5.54k
            psf_log_printf (psf, "FORM : %u (should be %u)\n %M\n", FORMsize, chunk_size, marker) ;
471
5.54k
            FORMsize = chunk_size ;
472
5.54k
            }
473
2
          else
474
2
            psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
475
          /* Set this to 0, so we don't jump a byte when parsing the next marker. */
476
5.62k
          chunk_size = 0 ;
477
5.62k
          break ;
478
479
480
9.77k
      case COMM_MARKER :
481
9.77k
          paiff->comm_offset = psf_ftell (psf) - 8 ;
482
9.77k
          chunk_size += chunk_size & 1 ;
483
9.77k
          comm_fmt->size = chunk_size ;
484
9.77k
          if ((error = aiff_read_comm_chunk (psf, comm_fmt)) != 0)
485
941
            return error ;
486
487
8.83k
          found_chunk |= HAVE_COMM ;
488
8.83k
          break ;
489
490
527
      case PEAK_MARKER :
491
          /* Must have COMM chunk before PEAK chunk. */
492
527
          if ((found_chunk & (HAVE_FORM | HAVE_AIFF | HAVE_COMM)) != (HAVE_FORM | HAVE_AIFF | HAVE_COMM))
493
2
            return SFE_AIFF_PEAK_B4_COMM ;
494
495
525
          psf_log_printf (psf, "%M : %d\n", marker, chunk_size) ;
496
525
          if (chunk_size != AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))
497
16
          { psf_binheader_readf (psf, "j", chunk_size) ;
498
16
            psf_log_printf (psf, "*** File PEAK chunk too big.\n") ;
499
16
            return SFE_WAV_BAD_PEAK ;
500
509
            } ;
501
502
509
          if (psf->peak_info)
503
254
          { psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
504
254
            free (psf->peak_info) ;
505
254
            psf->peak_info = NULL ;
506
254
            } ;
507
509
          if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
508
0
            return SFE_MALLOC_FAILED ;
509
510
          /* Read in rest of PEAK chunk. */
511
509
          psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ;
512
513
509
          if (psf->peak_info->version != 1)
514
377
            psf_log_printf (psf, "  version    : %d *** (should be version 1)\n", psf->peak_info->version) ;
515
132
          else
516
132
            psf_log_printf (psf, "  version    : %d\n", psf->peak_info->version) ;
517
518
509
          psf_log_printf (psf, "  time stamp : %d\n", psf->peak_info->timestamp) ;
519
509
          psf_log_printf (psf, "    Ch   Position       Value\n") ;
520
521
509
          cptr = ubuf.cbuf ;
522
6.98k
          for (k = 0 ; k < psf->sf.channels ; k++)
523
6.47k
          { float value ;
524
6.47k
            uint32_t position ;
525
526
6.47k
            psf_binheader_readf (psf, "Ef4", &value, &position) ;
527
6.47k
            psf->peak_info->peaks [k].value = value ;
528
6.47k
            psf->peak_info->peaks [k].position = position ;
529
530
6.47k
            snprintf (cptr, sizeof (ubuf.scbuf), "    %2d   %-12" PRId64 "   %g\n",
531
6.47k
                k, psf->peak_info->peaks [k].position, psf->peak_info->peaks [k].value) ;
532
6.47k
            cptr [sizeof (ubuf.scbuf) - 1] = 0 ;
533
6.47k
            psf_log_printf (psf, "%s", cptr) ;
534
6.47k
            } ;
535
536
509
          psf->peak_info->peak_loc = ((found_chunk & HAVE_SSND) == 0) ? SF_PEAK_START : SF_PEAK_END ;
537
509
          break ;
538
539
1.58k
      case SSND_MARKER :
540
1.58k
          if ((found_chunk & HAVE_AIFC) && (found_chunk & HAVE_FVER) == 0)
541
675
            psf_log_printf (psf, "*** Valid AIFC files should have an FVER chunk.\n") ;
542
543
1.58k
          paiff->ssnd_offset = psf_ftell (psf) - 8 ;
544
1.58k
          SSNDsize = chunk_size ;
545
1.58k
          psf_binheader_readf (psf, "E44", &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
546
547
1.58k
          psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
548
1.58k
          psf->dataoffset = psf_ftell (psf) ;
549
550
1.58k
          if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
551
719
          { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
552
719
            psf->datalength = psf->filelength - psf->dataoffset ;
553
719
            }
554
862
          else
555
862
            psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
556
557
1.58k
          if (ssnd_fmt.offset == 0 || psf->dataoffset + ssnd_fmt.offset == ssnd_fmt.blocksize)
558
821
          { psf_log_printf (psf, "  Offset     : %u\n", ssnd_fmt.offset) ;
559
821
            psf_log_printf (psf, "  Block Size : %u\n", ssnd_fmt.blocksize) ;
560
561
821
            psf->dataoffset += ssnd_fmt.offset ;
562
821
            psf->datalength -= ssnd_fmt.offset ;
563
821
            }
564
760
          else
565
760
          { psf_log_printf (psf, "  Offset     : %u\n", ssnd_fmt.offset) ;
566
760
            psf_log_printf (psf, "  Block Size : %u ???\n", ssnd_fmt.blocksize) ;
567
760
            psf->dataoffset += ssnd_fmt.offset ;
568
760
            psf->datalength -= ssnd_fmt.offset ;
569
760
            } ;
570
571
          /* Only set dataend if there really is data at the end. */
572
1.58k
          if (psf->datalength + psf->dataoffset < psf->filelength)
573
846
            psf->dataend = psf->datalength + psf->dataoffset ;
574
575
1.58k
          found_chunk |= HAVE_SSND ;
576
577
1.58k
          if (! psf->sf.seekable)
578
0
            break ;
579
580
          /* Seek to end of SSND chunk. */
581
1.58k
          psf_fseek (psf, psf->dataoffset + psf->datalength, SEEK_SET) ;
582
1.58k
          break ;
583
584
875
      case c_MARKER :
585
875
          if (chunk_size == 0)
586
292
            break ;
587
583
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf))
588
75
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
589
75
            return SFE_INTERNAL ;
590
508
            } ;
591
592
508
          cptr = ubuf.cbuf ;
593
508
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
594
508
          cptr [chunk_size] = 0 ;
595
596
508
          psf_sanitize_string (cptr, chunk_size) ;
597
598
508
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
599
508
          psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
600
508
          chunk_size += chunk_size & 1 ;
601
508
          break ;
602
603
613
      case AUTH_MARKER :
604
613
          if (chunk_size == 0)
605
404
            break ;
606
209
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
607
59
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
608
59
            return SFE_INTERNAL ;
609
150
            } ;
610
611
150
          cptr = ubuf.cbuf ;
612
150
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
613
150
          cptr [chunk_size] = 0 ;
614
150
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
615
150
          psf_store_string (psf, SF_STR_ARTIST, cptr) ;
616
150
          chunk_size += chunk_size & 1 ;
617
150
          break ;
618
619
824
      case COMT_MARKER :
620
824
        { uint16_t count, id, len ;
621
824
          uint32_t timestamp, bytes ;
622
623
824
          if (chunk_size == 0)
624
67
            break ;
625
757
          bytes = chunk_size ;
626
757
          bytes -= psf_binheader_readf (psf, "E2", &count) ;
627
757
          psf_log_printf (psf, " %M : %d\n  count  : %d\n", marker, chunk_size, count) ;
628
629
2.31M
          for (k = 0 ; k < count ; k++)
630
2.31M
          { bytes -= psf_binheader_readf (psf, "E422", &timestamp, &id, &len) ;
631
2.31M
            psf_log_printf (psf, "   time   : 0x%x\n   marker : %x\n   length : %d\n", timestamp, id, len) ;
632
633
2.31M
            if (len + 1 > SIGNED_SIZEOF (ubuf.scbuf))
634
25
            { psf_log_printf (psf, "\nError : string length (%d) too big.\n", len) ;
635
25
              return SFE_INTERNAL ;
636
2.31M
              } ;
637
638
2.31M
            cptr = ubuf.cbuf ;
639
2.31M
            bytes -= psf_binheader_readf (psf, "b", cptr, len) ;
640
2.31M
            cptr [len] = 0 ;
641
2.31M
            psf_log_printf (psf, "   string : %s\n", cptr) ;
642
2.31M
            } ;
643
644
732
          if (bytes > 0)
645
429
            psf_binheader_readf (psf, "j", bytes) ;
646
732
          } ;
647
732
          break ;
648
649
1.68k
      case APPL_MARKER :
650
1.68k
        { unsigned appl_marker ;
651
652
1.68k
          if (chunk_size == 0)
653
82
            break ;
654
1.60k
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
655
115
          { psf_log_printf (psf, " %M : %u (too big, skipping)\n", marker, chunk_size) ;
656
115
            psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
657
115
            break ;
658
1.48k
            } ;
659
660
1.48k
          if (chunk_size < 4)
661
49
          { psf_log_printf (psf, " %M : %d (too small, skipping)\n", marker, chunk_size) ;
662
49
            psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
663
49
            break ;
664
1.43k
            } ;
665
666
1.43k
          cptr = ubuf.cbuf ;
667
1.43k
          psf_binheader_readf (psf, "mb", &appl_marker, cptr, chunk_size + (chunk_size & 1) - 4) ;
668
1.43k
          cptr [chunk_size] = 0 ;
669
670
4.22k
          for (k = 0 ; k < (int) chunk_size ; k++)
671
3.82k
            if (! psf_isprint (cptr [k]))
672
1.04k
            { cptr [k] = 0 ;
673
1.04k
              break ;
674
1.04k
              } ;
675
676
1.43k
          psf_log_printf (psf, " %M : %d\n  AppSig : %M\n  Name   : %s\n", marker, chunk_size, appl_marker, cptr) ;
677
1.43k
          psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
678
1.43k
          chunk_size += chunk_size & 1 ;
679
1.43k
          } ;
680
1.43k
          break ;
681
682
1.67k
      case NAME_MARKER :
683
1.67k
          if (chunk_size == 0)
684
337
            break ;
685
1.33k
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
686
55
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
687
55
            return SFE_INTERNAL ;
688
1.27k
            } ;
689
690
1.27k
          cptr = ubuf.cbuf ;
691
1.27k
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
692
1.27k
          cptr [chunk_size] = 0 ;
693
1.27k
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
694
1.27k
          psf_store_string (psf, SF_STR_TITLE, cptr) ;
695
1.27k
          chunk_size += chunk_size & 1 ;
696
1.27k
          break ;
697
698
383
      case ANNO_MARKER :
699
383
          if (chunk_size == 0)
700
242
            break ;
701
141
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
702
59
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
703
59
            return SFE_INTERNAL ;
704
82
            } ;
705
706
82
          cptr = ubuf.cbuf ;
707
82
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
708
82
          cptr [chunk_size] = 0 ;
709
82
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
710
82
          psf_store_string (psf, SF_STR_COMMENT, cptr) ;
711
82
          chunk_size += chunk_size & 1 ;
712
82
          break ;
713
714
2.85k
      case INST_MARKER :
715
2.85k
          if (chunk_size != SIZEOF_INST_CHUNK)
716
953
          { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, chunk_size, SIZEOF_INST_CHUNK) ;
717
953
            psf_binheader_readf (psf, "j", chunk_size) ;
718
953
            break ;
719
1.90k
            } ;
720
1.90k
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
721
1.90k
          { uint8_t bytes [6] ;
722
1.90k
            int16_t gain ;
723
724
1.90k
            if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
725
0
              return SFE_MALLOC_FAILED ;
726
727
1.90k
            psf_binheader_readf (psf, "b", bytes, 6) ;
728
1.90k
            psf_log_printf (psf, "  Base Note : %u\n  Detune    : %u\n"
729
1.90k
                      "  Low  Note : %u\n  High Note : %u\n"
730
1.90k
                      "  Low  Vel. : %u\n  High Vel. : %u\n",
731
1.90k
                      bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
732
1.90k
            psf->instrument->basenote = bytes [0] ;
733
1.90k
            psf->instrument->detune = bytes [1] ;
734
1.90k
            psf->instrument->key_lo = bytes [2] ;
735
1.90k
            psf->instrument->key_hi = bytes [3] ;
736
1.90k
            psf->instrument->velocity_lo = bytes [4] ;
737
1.90k
            psf->instrument->velocity_hi = bytes [5] ;
738
1.90k
            psf_binheader_readf (psf, "E2", &gain) ;
739
1.90k
            psf->instrument->gain = gain ;
740
1.90k
            psf_log_printf (psf, "  Gain (dB) : %d\n", gain) ;
741
1.90k
            } ;
742
1.90k
          { int16_t mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
743
1.90k
            const char  *loop_mode ;
744
1.90k
            uint16_t begin, end ;
745
746
1.90k
            psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
747
1.90k
            loop_mode = get_loop_mode_str (mode) ;
748
1.90k
            mode = get_loop_mode (mode) ;
749
1.90k
            if (mode == SF_LOOP_NONE)
750
1.15k
            { psf->instrument->loop_count = 0 ;
751
1.15k
              psf->instrument->loops [0].mode = SF_LOOP_NONE ;
752
1.15k
              }
753
752
            else
754
752
            { psf->instrument->loop_count = 1 ;
755
752
              psf->instrument->loops [0].mode = SF_LOOP_FORWARD ;
756
752
              psf->instrument->loops [0].start = begin ;
757
752
              psf->instrument->loops [0].end = end ;
758
752
              psf->instrument->loops [0].count = 0 ;
759
752
              } ;
760
1.90k
            psf_log_printf (psf, "  Sustain\n   mode  : %d => %s\n   begin : %u\n   end   : %u\n",
761
1.90k
                      mode, loop_mode, begin, end) ;
762
1.90k
            psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
763
1.90k
            loop_mode = get_loop_mode_str (mode) ;
764
1.90k
            mode = get_loop_mode (mode) ;
765
1.90k
            if (mode == SF_LOOP_NONE)
766
1.59k
              psf->instrument->loops [1].mode = SF_LOOP_NONE ;
767
314
            else
768
314
            { psf->instrument->loop_count += 1 ;
769
314
              psf->instrument->loops [1].mode = SF_LOOP_FORWARD ;
770
314
              psf->instrument->loops [1].start = begin ;
771
314
              psf->instrument->loops [1].end = end ;
772
314
              psf->instrument->loops [1].count = 0 ;
773
314
              } ;
774
1.90k
            psf_log_printf (psf, "  Release\n   mode  : %d => %s\n   begin : %u\n   end   : %u\n",
775
1.90k
                    mode, loop_mode, begin, end) ;
776
1.90k
            } ;
777
1.90k
          instr_found++ ;
778
1.90k
          break ;
779
780
705
      case basc_MARKER :
781
705
          psf_log_printf (psf, " basc : %u\n", chunk_size) ;
782
783
705
          if ((error = aiff_read_basc_chunk (psf, chunk_size)))
784
0
            return error ;
785
705
          break ;
786
787
1.18k
      case MARK_MARKER :
788
1.18k
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
789
1.18k
          { uint16_t mark_id, n = 0 ;
790
1.18k
            uint32_t position ;
791
792
1.18k
            bytesread = psf_binheader_readf (psf, "E2", &n) ;
793
1.18k
            mark_count = n ;
794
1.18k
            psf_log_printf (psf, "  Count : %u\n", mark_count) ;
795
1.18k
            if (paiff->markstr != NULL)
796
944
            { psf_log_printf (psf, "*** Second MARK chunk found. Throwing away the first.\n") ;
797
944
              free (paiff->markstr) ;
798
944
              } ;
799
1.18k
            paiff->markstr = calloc (mark_count, sizeof (MARK_ID_POS)) ;
800
1.18k
            if (paiff->markstr == NULL)
801
0
              return SFE_MALLOC_FAILED ;
802
803
1.18k
            if (mark_count > 2500) /* 2500 is close to the largest number of cues possible because of block sizes */
804
347
            { psf_log_printf (psf, "  More than 2500 markers, skipping!\n") ;
805
347
              psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
806
347
              break ;
807
833
            } ;
808
809
833
            if (psf->cues)
810
636
            { free (psf->cues) ;
811
636
              psf->cues = NULL ;
812
636
              } ;
813
833
            if ((psf->cues = psf_cues_alloc (mark_count)) == NULL)
814
0
              return SFE_MALLOC_FAILED ;
815
816
73.7k
            for (n = 0 ; n < mark_count && bytesread < chunk_size ; n++)
817
72.9k
            { uint32_t pstr_len ;
818
72.9k
              uint8_t ch ;
819
820
72.9k
              bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &ch) ;
821
72.9k
              psf_log_printf (psf, "   Mark ID  : %u\n   Position : %u\n", mark_id, position) ;
822
823
72.9k
              psf->cues->cue_points [n].indx = mark_id ;
824
72.9k
              psf->cues->cue_points [n].position = 0 ;
825
72.9k
              psf->cues->cue_points [n].fcc_chunk = MAKE_MARKER ('d', 'a', 't', 'a') ; /* always data */
826
72.9k
              psf->cues->cue_points [n].chunk_start = 0 ;
827
72.9k
              psf->cues->cue_points [n].block_start = 0 ;
828
72.9k
              psf->cues->cue_points [n].sample_offset = position ;
829
830
72.9k
              pstr_len = (ch & 1) ? ch : ch + 1 ;
831
832
72.9k
              if (pstr_len < sizeof (ubuf.scbuf) - 1)
833
72.9k
              { bytesread += psf_binheader_readf (psf, "b", ubuf.scbuf, pstr_len) ;
834
72.9k
                ubuf.scbuf [pstr_len] = 0 ;
835
72.9k
                }
836
0
              else
837
0
              { uint32_t read_len = pstr_len - (sizeof (ubuf.scbuf) - 1) ;
838
0
                bytesread += psf_binheader_readf (psf, "bj", ubuf.scbuf, read_len, pstr_len - read_len) ;
839
0
                ubuf.scbuf [sizeof (ubuf.scbuf) - 1] = 0 ;
840
0
                }
841
842
72.9k
              psf_log_printf (psf, "   Name     : %s\n", ubuf.scbuf) ;
843
844
72.9k
              psf_strlcpy (psf->cues->cue_points [n].name, sizeof (psf->cues->cue_points [n].name), ubuf.cbuf) ;
845
846
72.9k
              paiff->markstr [n].markerID = mark_id ;
847
72.9k
              paiff->markstr [n].position = position ;
848
              /*
849
              **  TODO if ubuf.scbuf is equal to
850
              **  either Beg_loop, Beg loop or beg loop and spam
851
              **  if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
852
              **    return SFE_MALLOC_FAILED ;
853
              */
854
72.9k
              } ;
855
833
            } ;
856
833
          mark_found++ ;
857
833
          psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
858
833
          break ;
859
860
133
      case FVER_MARKER :
861
133
          found_chunk |= HAVE_FVER ;
862
          /* Falls through. */
863
864
459
      case SFX_MARKER :
865
459
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
866
459
          psf_binheader_readf (psf, "j", chunk_size) ;
867
459
          break ;
868
869
275
      case NONE_MARKER :
870
          /* Fix for broken AIFC files with incorrect COMM chunk length. */
871
275
          chunk_size = (chunk_size >> 24) - 3 ;
872
275
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
873
275
          psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ;
874
275
          break ;
875
876
842
      case CHAN_MARKER :
877
842
          if (chunk_size < 12)
878
35
          { psf_log_printf (psf, " %M : %d (should be >= 12)\n", marker, chunk_size) ;
879
35
            psf_binheader_readf (psf, "j", chunk_size) ;
880
35
            break ;
881
35
            }
882
883
807
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
884
885
807
          if ((error = aiff_read_chanmap (psf, chunk_size)))
886
0
            return error ;
887
807
          break ;
888
889
5.55k
      default :
890
5.55k
          if (chunk_size >= 0xffff0000)
891
116
          { done = SF_TRUE ;
892
116
            psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
893
116
            break ;
894
5.44k
            } ;
895
896
5.44k
          if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
897
5.44k
            && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
898
1.90k
          { psf_log_printf (psf, " %M : %u (unknown marker)\n", marker, chunk_size) ;
899
900
1.90k
            psf_binheader_readf (psf, "j", chunk_size) ;
901
1.90k
            break ;
902
3.53k
            } ;
903
904
3.53k
          if (psf_ftell (psf) & 0x03)
905
2.81k
          { psf_log_printf (psf, "  Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
906
2.81k
            psf_binheader_readf (psf, "j", -3) ;
907
2.81k
            break ;
908
2.81k
            } ;
909
715
          psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
910
715
          done = SF_TRUE ;
911
715
          break ;
912
35.4k
      } ; /* switch (marker) */
913
914
34.2k
    if (chunk_size >= psf->filelength)
915
3.36k
    { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
916
3.36k
      break ;
917
30.8k
      } ;
918
919
30.8k
    if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
920
0
      break ;
921
922
30.8k
    if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (int32_t)))
923
959
      break ;
924
30.8k
    } ; /* while (1) */
925
926
4.39k
  if (instr_found && mark_found)
927
101
  { int ji, str_index ;
928
    /* Next loop will convert markers to loop positions for internal handling */
929
212
    for (ji = 0 ; ji < psf->instrument->loop_count ; ji ++)
930
111
    { if (ji < ARRAY_LEN (psf->instrument->loops))
931
111
      { psf->instrument->loops [ji].start = marker_to_position (paiff->markstr, psf->instrument->loops [ji].start, mark_count) ;
932
111
        psf->instrument->loops [ji].end = marker_to_position (paiff->markstr, psf->instrument->loops [ji].end, mark_count) ;
933
111
        psf->instrument->loops [ji].mode = SF_LOOP_FORWARD ;
934
111
        } ;
935
111
      } ;
936
937
    /* The markers that correspond to loop positions can now be removed from cues struct */
938
101
    if (psf->cues->cue_count > (uint32_t) (psf->instrument->loop_count * 2))
939
82
    { uint32_t j ;
940
941
56.6k
      for (j = 0 ; j < psf->cues->cue_count - (uint32_t) (psf->instrument->loop_count * 2) ; j ++)
942
56.5k
      { /* This simply copies the information in cues above loop positions and writes it at current count instead */
943
56.5k
        psf->cues->cue_points [j].indx = psf->cues->cue_points [j + psf->instrument->loop_count * 2].indx ;
944
56.5k
        psf->cues->cue_points [j].position = psf->cues->cue_points [j + psf->instrument->loop_count * 2].position ;
945
56.5k
        psf->cues->cue_points [j].fcc_chunk = psf->cues->cue_points [j + psf->instrument->loop_count * 2].fcc_chunk ;
946
56.5k
        psf->cues->cue_points [j].chunk_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].chunk_start ;
947
56.5k
        psf->cues->cue_points [j].block_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].block_start ;
948
56.5k
        psf->cues->cue_points [j].sample_offset = psf->cues->cue_points [j + psf->instrument->loop_count * 2].sample_offset ;
949
14.5M
        for (str_index = 0 ; str_index < 256 ; str_index++)
950
14.4M
          psf->cues->cue_points [j].name [str_index] = psf->cues->cue_points [j + psf->instrument->loop_count * 2].name [str_index] ;
951
56.5k
        } ;
952
82
      psf->cues->cue_count -= psf->instrument->loop_count * 2 ;
953
82
      } else
954
19
      { /* All the cues were in fact loop positions so we can actually remove the cues altogether */
955
19
        free (psf->cues) ;
956
19
        psf->cues = NULL ;
957
19
        }
958
101
    } ;
959
960
4.39k
  if (psf->sf.channels < 1)
961
2.74k
    return SFE_CHANNEL_COUNT_ZERO ;
962
963
1.64k
  if (psf->sf.channels > SF_MAX_CHANNELS)
964
0
    return SFE_CHANNEL_COUNT ;
965
966
1.64k
  if (! (found_chunk & HAVE_FORM))
967
0
    return SFE_AIFF_NO_FORM ;
968
969
1.64k
  if (! (found_chunk & HAVE_AIFF))
970
0
    return SFE_AIFF_COMM_NO_FORM ;
971
972
1.64k
  if (! (found_chunk & HAVE_COMM))
973
0
    return SFE_AIFF_SSND_NO_COMM ;
974
975
1.64k
  if (! psf->dataoffset)
976
0
    return SFE_AIFF_NO_DATA ;
977
978
1.64k
  return 0 ;
979
1.64k
} /* aiff_read_header */
980
981
static int
982
aiff_close (SF_PRIVATE *psf)
983
5.62k
{ AIFF_PRIVATE *paiff = psf->container_data ;
984
985
5.62k
  if (paiff != NULL && paiff->markstr != NULL)
986
236
  { free (paiff->markstr) ;
987
236
    paiff->markstr = NULL ;
988
236
    } ;
989
990
5.62k
  if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
991
0
  { aiff_write_tailer (psf) ;
992
0
    aiff_write_header (psf, SF_TRUE) ;
993
0
    } ;
994
995
5.62k
  return 0 ;
996
5.62k
} /* aiff_close */
997
998
static int
999
aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
1000
9.77k
{ BUF_UNION ubuf ;
1001
9.77k
  int subformat, samplerate ;
1002
1003
9.77k
  ubuf.scbuf [0] = 0 ;
1004
1005
  /* The COMM chunk has an int aligned to an odd word boundary. Some
1006
  ** processors are not able to deal with this (ie bus fault) so we have
1007
  ** to take special care.
1008
  */
1009
1010
9.77k
  psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
1011
9.77k
        &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
1012
1013
9.77k
  if (comm_fmt->size > 0x10000 && (comm_fmt->size & 0xffff) == 0)
1014
264
  { psf_log_printf (psf, " COMM : %d (0x%x) *** should be ", comm_fmt->size, comm_fmt->size) ;
1015
264
    comm_fmt->size = ENDSWAP_32 (comm_fmt->size) ;
1016
264
    psf_log_printf (psf, "%d (0x%x)\n", comm_fmt->size, comm_fmt->size) ;
1017
264
    }
1018
9.51k
  else
1019
9.51k
    psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
1020
1021
9.77k
  if (comm_fmt->size == SIZEOF_AIFF_COMM)
1022
1.35k
    comm_fmt->encoding = NONE_MARKER ;
1023
8.41k
  else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
1024
1.04k
    psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
1025
7.37k
  else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
1026
2.91k
  { uint8_t encoding_len ;
1027
2.91k
    unsigned read_len ;
1028
1029
2.91k
    psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
1030
1031
2.91k
    comm_fmt->size = SF_MIN (sizeof (ubuf.scbuf), make_size_t (comm_fmt->size)) ;
1032
2.91k
    memset (ubuf.scbuf, 0, comm_fmt->size) ;
1033
2.91k
    read_len = comm_fmt->size - SIZEOF_AIFC_COMM + 1 ;
1034
2.91k
    psf_binheader_readf (psf, "b", ubuf.scbuf, read_len) ;
1035
2.91k
    ubuf.scbuf [read_len + 1] = 0 ;
1036
2.91k
    } ;
1037
1038
9.77k
  samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
1039
1040
9.77k
  psf_log_printf (psf, "  Sample Rate : %d\n", samplerate) ;
1041
9.77k
  psf_log_printf (psf, "  Frames      : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
1042
1043
9.77k
  if (comm_fmt->numChannels < 1 || comm_fmt->numChannels > SF_MAX_CHANNELS)
1044
236
  { psf_log_printf (psf, "  Channels    : %d (should be >= 1 and < %d)\n", comm_fmt->numChannels, SF_MAX_CHANNELS) ;
1045
236
    return SFE_CHANNEL_COUNT_BAD ;
1046
9.53k
    } ;
1047
1048
9.53k
  psf_log_printf (psf, "  Channels    : %d\n", comm_fmt->numChannels) ;
1049
1050
  /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
1051
9.53k
  if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
1052
419
  { psf_log_printf (psf, "  Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
1053
419
    comm_fmt->sampleSize = 32 ;
1054
419
    }
1055
9.11k
  else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
1056
734
  { psf_log_printf (psf, "  Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
1057
734
    comm_fmt->sampleSize = 64 ;
1058
734
    }
1059
8.38k
  else
1060
8.38k
    psf_log_printf (psf, "  Sample Size : %d\n", comm_fmt->sampleSize) ;
1061
1062
1063
9.53k
  if ((psf->sf.channels != comm_fmt->numChannels) && psf->peak_info)
1064
174
  { psf_log_printf (psf, "  *** channel count changed, discarding existing PEAK chunk\n") ;
1065
174
    free (psf->peak_info) ;
1066
174
    psf->peak_info = NULL ;
1067
174
    } ;
1068
1069
9.53k
  subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
1070
1071
9.53k
  psf->sf.samplerate = samplerate ;
1072
9.53k
  psf->sf.frames = comm_fmt->numSampleFrames ;
1073
9.53k
  psf->sf.channels = comm_fmt->numChannels ;
1074
9.53k
  psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
1075
1076
9.53k
  psf->endian = SF_ENDIAN_BIG ;
1077
1078
9.53k
  switch (comm_fmt->encoding)
1079
9.53k
  { case NONE_MARKER :
1080
1.66k
        psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1081
1.66k
        break ;
1082
1083
112
    case twos_MARKER :
1084
182
    case in24_MARKER :
1085
256
    case in32_MARKER :
1086
256
        psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1087
256
        break ;
1088
1089
79
    case sowt_MARKER :
1090
90
    case ni24_MARKER :
1091
114
    case ni32_MARKER :
1092
114
        psf->endian = SF_ENDIAN_LITTLE ;
1093
114
        psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1094
114
        break ;
1095
1096
300
    case fl32_MARKER :
1097
968
    case FL32_MARKER :
1098
968
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1099
968
        break ;
1100
1101
1.03k
    case ulaw_MARKER :
1102
1.12k
    case ULAW_MARKER :
1103
1.12k
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1104
1.12k
        break ;
1105
1106
437
    case alaw_MARKER :
1107
882
    case ALAW_MARKER :
1108
882
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1109
882
        break ;
1110
1111
922
    case fl64_MARKER :
1112
1.44k
    case FL64_MARKER :
1113
1.44k
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1114
1.44k
        break ;
1115
1116
136
    case raw_MARKER :
1117
136
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1118
136
        break ;
1119
1120
1.43k
    case DWVW_MARKER :
1121
1.43k
        psf->sf.format = SF_FORMAT_AIFF ;
1122
1.43k
        switch (comm_fmt->sampleSize)
1123
1.43k
        { case 12 :
1124
74
            psf->sf.format |= SF_FORMAT_DWVW_12 ;
1125
74
            break ;
1126
626
          case 16 :
1127
626
            psf->sf.format |= SF_FORMAT_DWVW_16 ;
1128
626
            break ;
1129
137
          case 24 :
1130
137
            psf->sf.format |= SF_FORMAT_DWVW_24 ;
1131
137
            break ;
1132
1133
597
          default :
1134
597
            psf->sf.format |= SF_FORMAT_DWVW_N ;
1135
597
            break ;
1136
1.43k
          } ;
1137
1.43k
        break ;
1138
1139
430
    case GSM_MARKER :
1140
430
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1141
430
        break ;
1142
1143
1144
385
    case ima4_MARKER :
1145
385
        psf->endian = SF_ENDIAN_BIG ;
1146
385
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1147
385
        break ;
1148
1149
705
    default :
1150
705
      psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1151
705
      return SFE_UNIMPLEMENTED ;
1152
9.53k
    } ;
1153
1154
8.83k
  if (! ubuf.scbuf [0])
1155
7.39k
    psf_log_printf (psf, "  Encoding    : %M\n", comm_fmt->encoding) ;
1156
1.44k
  else
1157
1.44k
    psf_log_printf (psf, "  Encoding    : %M => %s\n", comm_fmt->encoding, ubuf.scbuf) ;
1158
1159
8.83k
  return 0 ;
1160
9.53k
} /* aiff_read_comm_chunk */
1161
1162
1163
/*==========================================================================================
1164
*/
1165
1166
static int
1167
aiff_rewrite_header (SF_PRIVATE *psf)
1168
0
{ AIFF_PRIVATE *paiff = psf->container_data ;
1169
1170
  /* Assuming here that the header has already been written and just
1171
  ** needs to be corrected for new data length. That means that we
1172
  ** only change the length fields of the FORM and SSND chunks ;
1173
  ** everything else can be skipped over.
1174
  */
1175
0
  int k, ch, comm_size, comm_frames ;
1176
0
  sf_count_t header_len ;
1177
1178
  /* Calculate the header length rather than use dataoffset, as AIFF files
1179
  ** can have additional padding offset bytes which aren't usefully a part of
1180
  ** the header.
1181
  */
1182
0
  header_len = paiff->ssnd_offset + 8 + SIZEOF_SSND_CHUNK ;
1183
0
  if (psf->header.len < header_len || header_len > psf->dataoffset)
1184
0
    return SFE_INTERNAL ;
1185
0
  psf_fseek (psf, 0, SEEK_SET) ;
1186
0
  psf_fread (psf->header.ptr, header_len, 1, psf) ;
1187
1188
0
  psf->header.indx = 0 ;
1189
1190
  /* FORM chunk. */
1191
0
  psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
1192
1193
  /* COMM chunk. */
1194
0
  if ((k = psf_find_read_chunk_m32 (&psf->rchunks, COMM_MARKER)) >= 0)
1195
0
  { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1196
0
    comm_frames = psf->sf.frames ;
1197
0
    comm_size = psf->rchunks.chunks [k].len ;
1198
0
    psf_binheader_writef (psf, "Em42t4", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames)) ;
1199
0
    } ;
1200
1201
  /* PEAK chunk. */
1202
0
  if ((k = psf_find_read_chunk_m32 (&psf->rchunks, PEAK_MARKER)) >= 0)
1203
0
  { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1204
0
    psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1205
0
    psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1206
0
    for (ch = 0 ; ch < psf->sf.channels ; ch++)
1207
0
      psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [ch].value), BHW8 (psf->peak_info->peaks [ch].position)) ;
1208
0
    } ;
1209
1210
1211
  /* SSND chunk. */
1212
0
  if ((k = psf_find_read_chunk_m32 (&psf->rchunks, SSND_MARKER)) >= 0)
1213
0
  { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1214
0
    psf_binheader_writef (psf, "Etm8", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK)) ;
1215
0
    } ;
1216
1217
  /* Header mangling complete so write it out. */
1218
0
  psf_fseek (psf, 0, SEEK_SET) ;
1219
0
  psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1220
1221
0
  return 0 ;
1222
0
} /* aiff_rewrite_header */
1223
1224
static int
1225
aiff_write_header (SF_PRIVATE *psf, int calc_length)
1226
0
{ sf_count_t    current ;
1227
0
  AIFF_PRIVATE  *paiff ;
1228
0
  uint8_t comm_sample_rate [10], comm_zero_bytes [2] = { 0, 0 } ;
1229
0
  uint32_t  comm_type, comm_size, comm_encoding, comm_frames = 0, uk ;
1230
0
  int       ret, k, endian, has_data = SF_FALSE ;
1231
0
  int16_t     bit_width ;
1232
1233
0
  if ((paiff = psf->container_data) == NULL)
1234
0
    return SFE_INTERNAL ;
1235
1236
0
  current = psf_ftell (psf) ;
1237
1238
0
  if (current > psf->dataoffset)
1239
0
    has_data = SF_TRUE ;
1240
1241
0
  if (calc_length)
1242
0
  { psf->filelength = psf_get_filelen (psf) ;
1243
1244
0
    psf->datalength = psf->filelength - psf->dataoffset ;
1245
0
    if (psf->dataend)
1246
0
      psf->datalength -= psf->filelength - psf->dataend ;
1247
1248
0
    if (psf->bytewidth > 0)
1249
0
      psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1250
0
    } ;
1251
1252
0
  if (psf->file.mode == SFM_RDWR && psf->dataoffset > 0 && psf->rchunks.count > 0)
1253
0
  { if ((ret = aiff_rewrite_header (psf)) != 0)
1254
0
      return ret ;
1255
0
    if (current > 0)
1256
0
      psf_fseek (psf, current, SEEK_SET) ;
1257
0
    return 0 ;
1258
0
    } ;
1259
1260
0
  endian = SF_ENDIAN (psf->sf.format) ;
1261
0
  if (CPU_IS_LITTLE_ENDIAN && endian == SF_ENDIAN_CPU)
1262
0
    endian = SF_ENDIAN_LITTLE ;
1263
1264
  /* Standard value here. */
1265
0
  bit_width = psf->bytewidth * 8 ;
1266
0
  comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1267
1268
0
  switch (SF_CODEC (psf->sf.format) | endian)
1269
0
  { case SF_FORMAT_PCM_S8 | SF_ENDIAN_BIG :
1270
0
      psf->endian = SF_ENDIAN_BIG ;
1271
0
      comm_type = AIFC_MARKER ;
1272
0
      comm_size = SIZEOF_AIFC_COMM ;
1273
0
      comm_encoding = twos_MARKER ;
1274
0
      break ;
1275
1276
0
    case SF_FORMAT_PCM_S8 | SF_ENDIAN_LITTLE :
1277
0
      psf->endian = SF_ENDIAN_LITTLE ;
1278
0
      comm_type = AIFC_MARKER ;
1279
0
      comm_size = SIZEOF_AIFC_COMM ;
1280
0
      comm_encoding = sowt_MARKER ;
1281
0
      break ;
1282
1283
0
    case SF_FORMAT_PCM_16 | SF_ENDIAN_BIG :
1284
0
      psf->endian = SF_ENDIAN_BIG ;
1285
0
      comm_type = AIFC_MARKER ;
1286
0
      comm_size = SIZEOF_AIFC_COMM ;
1287
0
      comm_encoding = twos_MARKER ;
1288
0
      break ;
1289
1290
0
    case SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE :
1291
0
      psf->endian = SF_ENDIAN_LITTLE ;
1292
0
      comm_type = AIFC_MARKER ;
1293
0
      comm_size = SIZEOF_AIFC_COMM ;
1294
0
      comm_encoding = sowt_MARKER ;
1295
0
      break ;
1296
1297
0
    case SF_FORMAT_PCM_24 | SF_ENDIAN_BIG :
1298
0
      psf->endian = SF_ENDIAN_BIG ;
1299
0
      comm_type = AIFC_MARKER ;
1300
0
      comm_size = SIZEOF_AIFC_COMM ;
1301
0
      comm_encoding = in24_MARKER ;
1302
0
      break ;
1303
1304
0
    case SF_FORMAT_PCM_24 | SF_ENDIAN_LITTLE :
1305
0
      psf->endian = SF_ENDIAN_LITTLE ;
1306
0
      comm_type = AIFC_MARKER ;
1307
0
      comm_size = SIZEOF_AIFC_COMM ;
1308
0
      comm_encoding = ni24_MARKER ;
1309
0
      break ;
1310
1311
0
    case SF_FORMAT_PCM_32 | SF_ENDIAN_BIG :
1312
0
      psf->endian = SF_ENDIAN_BIG ;
1313
0
      comm_type = AIFC_MARKER ;
1314
0
      comm_size = SIZEOF_AIFC_COMM ;
1315
0
      comm_encoding = in32_MARKER ;
1316
0
      break ;
1317
1318
0
    case SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE :
1319
0
      psf->endian = SF_ENDIAN_LITTLE ;
1320
0
      comm_type = AIFC_MARKER ;
1321
0
      comm_size = SIZEOF_AIFC_COMM ;
1322
0
      comm_encoding = ni32_MARKER ;
1323
0
      break ;
1324
1325
0
    case SF_FORMAT_PCM_S8 :     /* SF_ENDIAN_FILE */
1326
0
    case SF_FORMAT_PCM_16 :
1327
0
    case SF_FORMAT_PCM_24 :
1328
0
    case SF_FORMAT_PCM_32 :
1329
0
      psf->endian = SF_ENDIAN_BIG ;
1330
0
      comm_type = AIFF_MARKER ;
1331
0
      comm_size = SIZEOF_AIFF_COMM ;
1332
0
      comm_encoding = 0 ;
1333
0
      break ;
1334
1335
0
    case SF_FORMAT_FLOAT :          /* Big endian floating point. */
1336
0
        psf->endian = SF_ENDIAN_BIG ;
1337
0
        comm_type = AIFC_MARKER ;
1338
0
        comm_size = SIZEOF_AIFC_COMM ;
1339
0
        comm_encoding = FL32_MARKER ;  /* Use 'FL32' because its easier to read. */
1340
0
        break ;
1341
1342
0
    case SF_FORMAT_DOUBLE :         /* Big endian double precision floating point. */
1343
0
        psf->endian = SF_ENDIAN_BIG ;
1344
0
        comm_type = AIFC_MARKER ;
1345
0
        comm_size = SIZEOF_AIFC_COMM ;
1346
0
        comm_encoding = FL64_MARKER ;  /* Use 'FL64' because its easier to read. */
1347
0
        break ;
1348
1349
0
    case SF_FORMAT_ULAW :
1350
0
        psf->endian = SF_ENDIAN_BIG ;
1351
0
        comm_type = AIFC_MARKER ;
1352
0
        comm_size = SIZEOF_AIFC_COMM ;
1353
0
        comm_encoding = ulaw_MARKER ;
1354
0
        break ;
1355
1356
0
    case SF_FORMAT_ALAW :
1357
0
        psf->endian = SF_ENDIAN_BIG ;
1358
0
        comm_type = AIFC_MARKER ;
1359
0
        comm_size = SIZEOF_AIFC_COMM ;
1360
0
        comm_encoding = alaw_MARKER ;
1361
0
        break ;
1362
1363
0
    case SF_FORMAT_PCM_U8 :
1364
0
        psf->endian = SF_ENDIAN_BIG ;
1365
0
        comm_type = AIFC_MARKER ;
1366
0
        comm_size = SIZEOF_AIFC_COMM ;
1367
0
        comm_encoding = raw_MARKER ;
1368
0
        break ;
1369
1370
0
    case SF_FORMAT_DWVW_12 :
1371
0
        psf->endian = SF_ENDIAN_BIG ;
1372
0
        comm_type = AIFC_MARKER ;
1373
0
        comm_size = SIZEOF_AIFC_COMM ;
1374
0
        comm_encoding = DWVW_MARKER ;
1375
1376
        /* Override standard value here.*/
1377
0
        bit_width = 12 ;
1378
0
        break ;
1379
1380
0
    case SF_FORMAT_DWVW_16 :
1381
0
        psf->endian = SF_ENDIAN_BIG ;
1382
0
        comm_type = AIFC_MARKER ;
1383
0
        comm_size = SIZEOF_AIFC_COMM ;
1384
0
        comm_encoding = DWVW_MARKER ;
1385
1386
        /* Override standard value here.*/
1387
0
        bit_width = 16 ;
1388
0
        break ;
1389
1390
0
    case SF_FORMAT_DWVW_24 :
1391
0
        psf->endian = SF_ENDIAN_BIG ;
1392
0
        comm_type = AIFC_MARKER ;
1393
0
        comm_size = SIZEOF_AIFC_COMM ;
1394
0
        comm_encoding = DWVW_MARKER ;
1395
1396
        /* Override standard value here.*/
1397
0
        bit_width = 24 ;
1398
0
        break ;
1399
1400
0
    case SF_FORMAT_GSM610 :
1401
0
        psf->endian = SF_ENDIAN_BIG ;
1402
0
        comm_type = AIFC_MARKER ;
1403
0
        comm_size = SIZEOF_AIFC_COMM ;
1404
0
        comm_encoding = GSM_MARKER ;
1405
1406
        /* Override standard value here.*/
1407
0
        bit_width = 16 ;
1408
0
        break ;
1409
1410
0
    case SF_FORMAT_IMA_ADPCM :
1411
0
        psf->endian = SF_ENDIAN_BIG ;
1412
0
        comm_type = AIFC_MARKER ;
1413
0
        comm_size = SIZEOF_AIFC_COMM ;
1414
0
        comm_encoding = ima4_MARKER ;
1415
1416
        /* Override standard value here.*/
1417
0
        bit_width = 16 ;
1418
0
        comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1419
0
        break ;
1420
1421
0
    default : return SFE_BAD_OPEN_FORMAT ;
1422
0
    } ;
1423
1424
  /* Reset the current header length to zero. */
1425
0
  psf->header.ptr [0] = 0 ;
1426
0
  psf->header.indx = 0 ;
1427
0
  psf_fseek (psf, 0, SEEK_SET) ;
1428
1429
0
  psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
1430
1431
  /* Write AIFF/AIFC marker and COM chunk. */
1432
0
  if (comm_type == AIFC_MARKER)
1433
    /* AIFC must have an FVER chunk. */
1434
0
    psf_binheader_writef (psf, "Emm44", BHWm (comm_type), BHWm (FVER_MARKER), BHW4 (4), BHW4 (0xA2805140)) ;
1435
0
  else
1436
0
    psf_binheader_writef (psf, "Em", BHWm (comm_type)) ;
1437
1438
0
  paiff->comm_offset = psf->header.indx - 8 ;
1439
1440
0
  memset (comm_sample_rate, 0, sizeof (comm_sample_rate)) ;
1441
0
  uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1442
1443
0
  psf_binheader_writef (psf, "Em42t42", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames), BHW2 (bit_width)) ;
1444
0
  psf_binheader_writef (psf, "b", BHWv (comm_sample_rate), BHWz (sizeof (comm_sample_rate))) ;
1445
1446
  /* AIFC chunks have some extra data. */
1447
0
  if (comm_type == AIFC_MARKER)
1448
0
    psf_binheader_writef (psf, "mb", BHWm (comm_encoding), BHWv (comm_zero_bytes), BHWz (sizeof (comm_zero_bytes))) ;
1449
1450
0
  if (psf->channel_map && paiff->chanmap_tag)
1451
0
    psf_binheader_writef (psf, "Em4444", BHWm (CHAN_MARKER), BHW4 (12), BHW4 (paiff->chanmap_tag), BHW4 (0), BHW4 (0)) ;
1452
1453
  /* Check if there's a INST chunk to write */
1454
0
  if (psf->instrument != NULL && psf->cues != NULL)
1455
0
  { /* Huge chunk of code removed here because it had egregious errors that were
1456
    ** not detected by either the compiler or the tests. It was found when updating
1457
    ** the way psf_binheader_writef works.
1458
    */
1459
0
    }
1460
0
  else if (psf->instrument == NULL && psf->cues != NULL)
1461
0
  { /* There are cues but no loops */
1462
0
    uint32_t idx ;
1463
0
    int totalStringLength = 0, stringLength ;
1464
1465
    /* Here we count how many bytes will the pascal strings need */
1466
0
    for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1467
0
    { stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
1468
0
      totalStringLength += stringLength + (stringLength % 2 == 0 ? 0 : 1) ;
1469
0
      } ;
1470
1471
0
    psf_binheader_writef (psf, "Em42",
1472
0
      BHWm (MARK_MARKER), BHW4 (2 + psf->cues->cue_count * (2 + 4) + totalStringLength), BHW2 (psf->cues->cue_count)) ;
1473
1474
0
    for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1475
0
      psf_binheader_writef (psf, "E24p", BHW2 (psf->cues->cue_points [idx].indx), BHW4 (psf->cues->cue_points [idx].sample_offset), BHWp (psf->cues->cue_points [idx].name)) ;
1476
0
    } ;
1477
1478
0
  if (psf->strings.flags & SF_STR_LOCATE_START)
1479
0
    aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1480
1481
0
  if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1482
0
  { psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1483
0
    psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1484
0
    for (k = 0 ; k < psf->sf.channels ; k++)
1485
0
      psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1486
0
    } ;
1487
1488
  /* Write custom headers. */
1489
0
  for (uk = 0 ; uk < psf->wchunks.used ; uk++)
1490
0
    psf_binheader_writef (psf, "Em4b", BHWm (psf->wchunks.chunks [uk].mark32), BHW4 (psf->wchunks.chunks [uk].len), BHWv (psf->wchunks.chunks [uk].data), BHWz (psf->wchunks.chunks [uk].len)) ;
1491
1492
  /* Write SSND chunk. */
1493
0
  paiff->ssnd_offset = psf->header.indx ;
1494
0
  psf_binheader_writef (psf, "Etm844", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK), BHW4 (0), BHW4 (0)) ;
1495
1496
  /* Header construction complete so write it out. */
1497
0
  psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1498
1499
0
  if (psf->error)
1500
0
    return psf->error ;
1501
1502
0
  if (has_data && psf->dataoffset != psf->header.indx)
1503
0
    return psf->error = SFE_INTERNAL ;
1504
1505
0
  psf->dataoffset = psf->header.indx ;
1506
1507
0
  if (! has_data)
1508
0
    psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1509
0
  else if (current > 0)
1510
0
    psf_fseek (psf, current, SEEK_SET) ;
1511
1512
0
  return psf->error ;
1513
0
} /* aiff_write_header */
1514
1515
static int
1516
aiff_write_tailer (SF_PRIVATE *psf)
1517
0
{ int   k ;
1518
1519
  /* Reset the current header length to zero. */
1520
0
  psf->header.ptr [0] = 0 ;
1521
0
  psf->header.indx = 0 ;
1522
1523
0
  psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1524
1525
  /* Make sure tailer data starts at even byte offset. Pad if necessary. */
1526
0
  if (psf->dataend % 2 == 1)
1527
0
  { psf_fwrite (psf->header.ptr, 1, 1, psf) ;
1528
0
    psf->dataend ++ ;
1529
0
    } ;
1530
1531
0
  if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1532
0
  { psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1533
0
    psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1534
0
    for (k = 0 ; k < psf->sf.channels ; k++)
1535
0
      psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1536
0
    } ;
1537
1538
0
  if (psf->strings.flags & SF_STR_LOCATE_END)
1539
0
    aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1540
1541
  /* Write the tailer. */
1542
0
  if (psf->header.indx > 0)
1543
0
    psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1544
1545
0
  return 0 ;
1546
0
} /* aiff_write_tailer */
1547
1548
static void
1549
aiff_write_strings (SF_PRIVATE *psf, int location)
1550
0
{ int k, slen ;
1551
1552
0
  for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1553
0
  { if (psf->strings.data [k].type == 0)
1554
0
      break ;
1555
1556
0
    if (psf->strings.data [k].flags != location)
1557
0
      continue ;
1558
1559
0
    switch (psf->strings.data [k].type)
1560
0
    { case SF_STR_SOFTWARE :
1561
0
        slen = strlen (psf->strings.storage + psf->strings.data [k].offset) ;
1562
0
        psf_binheader_writef (psf, "Em4mb", BHWm (APPL_MARKER), BHW4 (slen + 4), BHWm (m3ga_MARKER), BHWv (psf->strings.storage + psf->strings.data [k].offset), BHWz (slen + (slen & 1))) ;
1563
0
        break ;
1564
1565
0
      case SF_STR_TITLE :
1566
0
        psf_binheader_writef (psf, "EmS", BHWm (NAME_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1567
0
        break ;
1568
1569
0
      case SF_STR_COPYRIGHT :
1570
0
        psf_binheader_writef (psf, "EmS", BHWm (c_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1571
0
        break ;
1572
1573
0
      case SF_STR_ARTIST :
1574
0
        psf_binheader_writef (psf, "EmS", BHWm (AUTH_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1575
0
        break ;
1576
1577
0
      case SF_STR_COMMENT :
1578
0
        psf_binheader_writef (psf, "EmS", BHWm (ANNO_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1579
0
        break ;
1580
1581
      /*
1582
      case SF_STR_DATE :
1583
        psf_binheader_writef (psf, "Ems", BHWm (ICRD_MARKER), BHWs (psf->strings.data [k].str)) ;
1584
        break ;
1585
      */
1586
0
      } ;
1587
0
    } ;
1588
1589
0
  return ;
1590
0
} /* aiff_write_strings */
1591
1592
static int
1593
aiff_command (SF_PRIVATE * psf, int command, void * UNUSED (data), int UNUSED (datasize))
1594
0
{ AIFF_PRIVATE  *paiff ;
1595
1596
0
  if ((paiff = psf->container_data) == NULL)
1597
0
    return SFE_INTERNAL ;
1598
1599
0
  switch (command)
1600
0
  { case SFC_SET_CHANNEL_MAP_INFO :
1601
0
      paiff->chanmap_tag = aiff_caf_find_channel_layout_tag (psf->channel_map, psf->sf.channels) ;
1602
0
      return (paiff->chanmap_tag != 0) ;
1603
1604
0
    default :
1605
0
      break ;
1606
0
  } ;
1607
1608
0
  return 0 ;
1609
0
} /* aiff_command */
1610
1611
static const char*
1612
get_loop_mode_str (int16_t mode)
1613
3.81k
{ switch (mode)
1614
3.81k
  { case 0 : return "none" ;
1615
669
    case 1 : return "forward" ;
1616
397
    case 2 : return "backward" ;
1617
3.81k
    } ;
1618
1619
1.47k
  return "*** unknown" ;
1620
3.81k
} /* get_loop_mode_str */
1621
1622
static int16_t
1623
get_loop_mode (int16_t mode)
1624
3.81k
{ switch (mode)
1625
3.81k
  { case 0 : return SF_LOOP_NONE ;
1626
669
    case 1 : return SF_LOOP_FORWARD ;
1627
397
    case 2 : return SF_LOOP_BACKWARD ;
1628
3.81k
    } ;
1629
1630
1.47k
  return SF_LOOP_NONE ;
1631
3.81k
} /* get_loop_mode */
1632
1633
/*==========================================================================================
1634
**  Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1635
**  back again. It assumes that all sample rates are between 1 and 800MHz, which
1636
**  should be OK as other sound file formats use a 32 bit integer to store sample
1637
**  rate.
1638
**  There is another (probably better) version in the source code to the SoX but it
1639
**  has a copyright which probably prevents it from being allowable as GPL/LGPL.
1640
*/
1641
1642
static int
1643
tenbytefloat2int (uint8_t *bytes)
1644
9.77k
{ int val = 3 ;
1645
1646
9.77k
  if (bytes [0] & 0x80)  /* Negative number. */
1647
1.85k
    return 0 ;
1648
1649
7.91k
  if (bytes [0] <= 0x3F)  /* Less than 1. */
1650
4.90k
    return 1 ;
1651
1652
3.01k
  if (bytes [0] > 0x40)  /* Way too big. */
1653
1.25k
    return 0x4000000 ;
1654
1655
1.76k
  if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1656
1.11k
    return 800000000 ;
1657
1658
  /* Ok, can handle it. */
1659
1660
649
  val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1661
1662
649
  val >>= (29 - bytes [1]) ;
1663
1664
649
  return val ;
1665
1.76k
} /* tenbytefloat2int */
1666
1667
static void
1668
uint2tenbytefloat (uint32_t num, uint8_t *bytes)
1669
0
{ uint32_t mask = 0x40000000 ;
1670
0
  int count ;
1671
1672
0
  if (num <= 1)
1673
0
  { bytes [0] = 0x3F ;
1674
0
    bytes [1] = 0xFF ;
1675
0
    bytes [2] = 0x80 ;
1676
0
    return ;
1677
0
    } ;
1678
1679
0
  bytes [0] = 0x40 ;
1680
1681
0
  if (num >= mask)
1682
0
  { bytes [1] = 0x1D ;
1683
0
    return ;
1684
0
    } ;
1685
1686
0
  for (count = 0 ; count < 32 ; count ++)
1687
0
  { if (num & mask)
1688
0
      break ;
1689
0
    mask >>= 1 ;
1690
0
    } ;
1691
1692
0
  num = count < 31 ? num << (count + 1) : 0 ;
1693
0
  bytes [1] = 29 - count ;
1694
0
  bytes [2] = (num >> 24) & 0xFF ;
1695
0
  bytes [3] = (num >> 16) & 0xFF ;
1696
0
  bytes [4] = (num >> 8) & 0xFF ;
1697
0
  bytes [5] = num & 0xFF ;
1698
1699
0
} /* uint2tenbytefloat */
1700
1701
static int
1702
aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
1703
705
{ const char * type_str ;
1704
705
  basc_CHUNK bc ;
1705
705
  sf_count_t count ;
1706
1707
705
  count = psf_binheader_readf (psf, "E442", &bc.version, &bc.numBeats, &bc.rootNote) ;
1708
705
  count += psf_binheader_readf (psf, "E222", &bc.scaleType, &bc.sigNumerator, &bc.sigDenominator) ;
1709
705
  count += psf_binheader_readf (psf, "E2j", &bc.loopType, datasize - sizeof (bc)) ;
1710
1711
705
  psf_log_printf (psf, "  Version ? : %u\n  Num Beats : %u\n  Root Note : 0x%x\n",
1712
705
            bc.version, bc.numBeats, bc.rootNote) ;
1713
1714
705
  switch (bc.scaleType)
1715
705
  { case basc_SCALE_MINOR :
1716
7
        type_str = "MINOR" ;
1717
7
        break ;
1718
2
    case basc_SCALE_MAJOR :
1719
2
        type_str = "MAJOR" ;
1720
2
        break ;
1721
121
    case basc_SCALE_NEITHER :
1722
121
        type_str = "NEITHER" ;
1723
121
        break ;
1724
144
    case basc_SCALE_BOTH :
1725
144
        type_str = "BOTH" ;
1726
144
        break ;
1727
431
    default :
1728
431
        type_str = "!!WRONG!!" ;
1729
431
        break ;
1730
705
    } ;
1731
1732
705
  psf_log_printf (psf, "  ScaleType : 0x%x (%s)\n", bc.scaleType, type_str) ;
1733
705
  psf_log_printf (psf, "  Time Sig  : %d/%d\n", bc.sigNumerator, bc.sigDenominator) ;
1734
1735
705
  switch (bc.loopType)
1736
705
  { case basc_TYPE_ONE_SHOT :
1737
8
        type_str = "One Shot" ;
1738
8
        break ;
1739
70
    case basc_TYPE_LOOP :
1740
70
        type_str = "Loop" ;
1741
70
        break ;
1742
627
    default:
1743
627
        type_str = "!!WRONG!!" ;
1744
627
        break ;
1745
705
    } ;
1746
1747
705
  psf_log_printf (psf, "  Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
1748
1749
705
  if (psf->loop_info)
1750
597
  { psf_log_printf (psf, "  Found existing loop info, using last one.\n") ;
1751
597
    free (psf->loop_info) ;
1752
597
    psf->loop_info = NULL ;
1753
597
    } ;
1754
705
  if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1755
0
    return SFE_MALLOC_FAILED ;
1756
1757
705
  psf->loop_info->time_sig_num  = bc.sigNumerator ;
1758
705
  psf->loop_info->time_sig_den  = bc.sigDenominator ;
1759
705
  psf->loop_info->loop_mode   = (bc.loopType == basc_TYPE_ONE_SHOT) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1760
705
  psf->loop_info->num_beats   = bc.numBeats ;
1761
1762
  /* Can always be recalculated from other known fields. */
1763
705
  psf->loop_info->bpm = (1.0 / psf->sf.frames) * psf->sf.samplerate
1764
705
              * ((bc.numBeats * 4.0) / bc.sigDenominator) * 60.0 ;
1765
705
  psf->loop_info->root_key = bc.rootNote ;
1766
1767
705
  if (count < datasize)
1768
684
    psf_binheader_readf (psf, "j", datasize - count) ;
1769
1770
705
  return 0 ;
1771
705
} /* aiff_read_basc_chunk */
1772
1773
1774
static int
1775
aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
1776
807
{ const AIFF_CAF_CHANNEL_MAP * map_info ;
1777
807
  unsigned channel_bitmap, channel_decriptions, bytesread ;
1778
807
  int layout_tag ;
1779
1780
807
  bytesread = psf_binheader_readf (psf, "444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
1781
1782
807
  if ((map_info = aiff_caf_of_channel_layout_tag (layout_tag)) == NULL)
1783
284
    return 0 ;
1784
1785
523
  psf_log_printf (psf, "  Tag    : %x\n", layout_tag) ;
1786
523
  if (map_info)
1787
523
    psf_log_printf (psf, "  Layout : %s\n", map_info->name) ;
1788
1789
523
  if (bytesread < dword)
1790
201
    psf_binheader_readf (psf, "j", dword - bytesread) ;
1791
1792
523
  if (map_info->channel_map != NULL)
1793
320
  { size_t chanmap_size = SF_MIN (psf->sf.channels, layout_tag & 0xffff) * sizeof (psf->channel_map [0]) ;
1794
1795
320
    free (psf->channel_map) ;
1796
1797
320
    if ((psf->channel_map = malloc (chanmap_size)) == NULL)
1798
0
      return SFE_MALLOC_FAILED ;
1799
1800
320
    memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
1801
523
    } ;
1802
1803
523
  return 0 ;
1804
523
} /* aiff_read_chanmap */
1805
1806
/*==============================================================================
1807
*/
1808
1809
static int
1810
aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1811
0
{ return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1812
0
} /* aiff_set_chunk */
1813
1814
static SF_CHUNK_ITERATOR *
1815
aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1816
0
{ return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1817
0
} /* aiff_next_chunk_iterator */
1818
1819
static int
1820
aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1821
0
{ int indx ;
1822
1823
0
  if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1824
0
    return SFE_UNKNOWN_CHUNK ;
1825
1826
0
  chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1827
1828
0
  return SFE_NO_ERROR ;
1829
0
} /* aiff_get_chunk_size */
1830
1831
static int
1832
aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1833
0
{ sf_count_t pos ;
1834
0
  int indx ;
1835
1836
0
  if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1837
0
    return SFE_UNKNOWN_CHUNK ;
1838
1839
0
  if (chunk_info->data == NULL)
1840
0
    return SFE_BAD_CHUNK_DATA_PTR ;
1841
1842
0
  chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1843
0
  memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1844
1845
0
  pos = psf_ftell (psf) ;
1846
0
  psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1847
0
  psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1848
0
  psf_fseek (psf, pos, SEEK_SET) ;
1849
1850
0
  return SFE_NO_ERROR ;
1851
0
} /* aiff_get_chunk_data */