~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/src/sd2.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-11-21 02:38:07 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20061121023807-flsqb0p8gb5a942v
Tags: 1.2.6-0ubuntu1
* New upstream release.
* debian/{control,rules}:
  - Use system portaudio 19 to gain native ALSA and JACK support,
  - Use system soundtouch and samplerate,
  - Suggest the LAME libs (Closes Ubuntu: #51063),
  - Disable resample support.
* {configure,lib-src/Makefile}.in: Build & link against system
  portaudio 19.
* Reinvoke autoconf to fix FTBFS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
 
2
** Copyright (C) 2001-2006 Erik de Castro Lopo <erikd@mega-nerd.com>
3
3
** Copyright (C) 2004 Paavo Jumppanen
4
4
**
5
5
** This program is free software; you can redistribute it and/or modify
27
27
** http://developer.apple.com/documentation/mac/MoreToolbox/MoreToolbox-99.html
28
28
*/
29
29
 
 
30
#include        "sfconfig.h"
 
31
 
30
32
#include        <stdio.h>
31
33
#include        <stdlib.h>
32
34
#include        <string.h>
33
35
#include        <ctype.h>
34
36
 
35
37
#include        "sndfile.h"
36
 
#include        "config.h"
37
38
#include        "sfendian.h"
38
39
#include        "common.h"
39
40
 
97
98
 
98
99
int
99
100
sd2_open (SF_PRIVATE *psf)
100
 
{       int saved_filedes, subformat, error = 0 ;
 
101
{       int subformat, error = 0, valid ;
101
102
 
102
103
        /* SD2 is always big endian. */
103
104
        psf->endian = SF_ENDIAN_BIG ;
104
105
 
105
106
        if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->rsrclength > 0))
106
 
        {       if (psf->rsrcdes < 0)
107
 
        {       psf_log_printf (psf, "sd2_open : psf->rsrcdes < 0\n") ;
108
 
                return SFE_SD2_BAD_RSRC ;
109
 
                } ;
110
 
 
111
 
                saved_filedes = psf->filedes ;
112
 
                psf->filedes = psf->rsrcdes ;
 
107
        {       psf_use_rsrc (psf, SF_TRUE) ;
 
108
                valid = psf_file_valid (psf) ;
 
109
                psf_use_rsrc (psf, SF_FALSE) ;
 
110
                if (! valid)
 
111
                {       psf_log_printf (psf, "sd2_open : psf->rsrcdes < 0\n") ;
 
112
                        return SFE_SD2_BAD_RSRC ;
 
113
                        } ;
113
114
 
114
115
                error = sd2_parse_rsrc_fork (psf) ;
115
116
 
116
 
                psf->filedes = saved_filedes ;
117
117
                if (error)
118
118
                        goto error_cleanup ;
119
119
                } ;
130
130
        if (psf->mode == SFM_WRITE || (psf->mode == SFM_RDWR && psf->rsrclength == 0))
131
131
        {       psf_open_rsrc (psf, psf->mode) ;
132
132
 
133
 
                saved_filedes = psf->filedes ;
134
 
                psf->filedes = psf->rsrcdes ;
135
 
 
136
133
                error = sd2_write_rsrc_fork (psf, SF_FALSE) ;
137
134
 
138
 
                psf->filedes = saved_filedes ;
139
135
                if (error)
140
136
                        goto error_cleanup ;
141
137
 
143
139
                psf->write_header = NULL ;
144
140
                } ;
145
141
 
146
 
        psf->close = sd2_close ;
 
142
        psf->container_close = sd2_close ;
147
143
 
148
144
        psf->blockwidth = psf->bytewidth * psf->sf.channels ;
149
145
 
207
203
        data [offset + 3] = value ;
208
204
} /* write_int */
209
205
 
 
206
static inline void
 
207
write_marker (unsigned char * data, int offset, int value)
 
208
{
 
209
        if (CPU_IS_BIG_ENDIAN)
 
210
        {       data [offset] = value >> 24 ;
 
211
                data [offset + 1] = value >> 16 ;
 
212
                data [offset + 2] = value >> 8 ;
 
213
                data [offset + 3] = value ;
 
214
                }
 
215
        else
 
216
        {       data [offset] = value ;
 
217
                data [offset + 1] = value >> 8 ;
 
218
                data [offset + 2] = value >> 16 ;
 
219
                data [offset + 3] = value >> 24 ;
 
220
                } ;
 
221
} /* write_marker */
 
222
 
210
223
static void
211
224
write_str (unsigned char * data, int offset, char * buffer, int buffer_len)
212
225
{       memcpy (data + offset, buffer, buffer_len) ;
224
237
 
225
238
        int k, str_offset, data_offset, next_str ;
226
239
 
 
240
        psf_use_rsrc (psf, SF_TRUE) ;
 
241
 
227
242
        memset (&rsrc, 0, sizeof (rsrc)) ;
228
243
 
229
244
        rsrc.sample_rate = psf->sf.samplerate ;
269
284
        write_str (rsrc.rsrc_data, 0x31, psf->filename, strlen (psf->filename)) ;
270
285
 
271
286
        write_short (rsrc.rsrc_data, 0x50, 0) ;
272
 
        write_int (rsrc.rsrc_data, 0x52, Sd2f_MARKER) ;
273
 
        write_int (rsrc.rsrc_data, 0x56, lsf1_MARKER) ;
 
287
        write_marker (rsrc.rsrc_data, 0x52, Sd2f_MARKER) ;
 
288
        write_marker (rsrc.rsrc_data, 0x56, lsf1_MARKER) ;
274
289
 
275
290
        /* Very start of resource map. */
276
291
        write_int (rsrc.rsrc_data, rsrc.map_offset + 0, rsrc.data_offset) ;
304
319
 
305
320
        /* Write 'STR ' resource type. */
306
321
        rsrc.str_count = 3 ;
307
 
        write_int (rsrc.rsrc_data, rsrc.type_offset, STR_MARKER) ;
 
322
        write_marker (rsrc.rsrc_data, rsrc.type_offset, STR_MARKER) ;
308
323
        write_short (rsrc.rsrc_data, rsrc.type_offset + 4, rsrc.str_count - 1) ;
309
324
        write_short (rsrc.rsrc_data, rsrc.type_offset + 6, 0x12) ;
310
325
 
311
326
        /* Write 'sdML' resource type. */
312
 
        write_int (rsrc.rsrc_data, rsrc.type_offset + 8, sdML_MARKER) ;
 
327
        write_marker (rsrc.rsrc_data, rsrc.type_offset + 8, sdML_MARKER) ;
313
328
        write_short (rsrc.rsrc_data, rsrc.type_offset + 12, 0) ;
314
329
        write_short (rsrc.rsrc_data, rsrc.type_offset + 14, 0x36) ;
315
330
 
340
355
        rsrc.rsrc_len = rsrc.map_offset + rsrc.map_length ;
341
356
 
342
357
        psf_fwrite (rsrc.rsrc_data, rsrc.rsrc_len, 1, psf) ;
 
358
 
 
359
        psf_use_rsrc (psf, SF_FALSE) ;
 
360
 
343
361
        if (psf->error)
344
362
                return psf->error ;
345
363
 
357
375
static inline int
358
376
read_short (const unsigned char * data, int offset)
359
377
{       return (data [offset] << 8) + data [offset + 1] ;
360
 
} /* read_char */
 
378
} /* read_short */
361
379
 
362
380
static inline int
363
381
read_int (const unsigned char * data, int offset)
364
382
{       return (data [offset] << 24) + (data [offset + 1] << 16) + (data [offset + 2] << 8) + data [offset + 3] ;
365
 
} /* read_char */
 
383
} /* read_int */
 
384
 
 
385
static inline int
 
386
read_marker (const unsigned char * data, int offset)
 
387
{
 
388
        if (CPU_IS_BIG_ENDIAN)
 
389
                return (data [offset] << 24) + (data [offset + 1] << 16) + (data [offset + 2] << 8) + data [offset + 3] ;
 
390
        else if (CPU_IS_LITTLE_ENDIAN)
 
391
                return data [offset] + (data [offset + 1] << 8) + (data [offset + 2] << 16) + (data [offset + 3] << 24) ;
 
392
        else
 
393
                return 0x666 ;
 
394
} /* read_marker */
366
395
 
367
396
static void
368
397
read_str (const unsigned char * data, int offset, char * buffer, int buffer_len)
383
412
{       SD2_RSRC rsrc ;
384
413
        int k, marker, error = 0 ;
385
414
 
 
415
        psf_use_rsrc (psf, SF_TRUE) ;
 
416
 
386
417
        memset (&rsrc, 0, sizeof (rsrc)) ;
387
418
 
388
419
        rsrc.rsrc_len = psf_get_filelen (psf) ;
404
435
        rsrc.data_length = read_int (rsrc.rsrc_data, 8) ;
405
436
        rsrc.map_length = read_int (rsrc.rsrc_data, 12) ;
406
437
 
 
438
        if (rsrc.data_offset == 0x51607 && rsrc.map_offset == 0x20000)
 
439
        {       psf_log_printf (psf, "Trying offset of 0x52 bytes.\n") ;
 
440
                rsrc.data_offset = read_int (rsrc.rsrc_data, 0x52 + 0) + 0x52 ;
 
441
                rsrc.map_offset = read_int (rsrc.rsrc_data, 0x52 + 4) + 0x52 ;
 
442
                rsrc.data_length = read_int (rsrc.rsrc_data, 0x52 + 8) ;
 
443
                rsrc.map_length = read_int (rsrc.rsrc_data, 0x52 + 12) ;
 
444
                } ;
 
445
 
407
446
        psf_log_printf (psf, "  data offset : 0x%04X\n  map  offset : 0x%04X\n"
408
447
                                "  data length : 0x%04X\n  map  length : 0x%04X\n",
409
448
                                rsrc.data_offset, rsrc.map_offset, rsrc.data_length, rsrc.map_length) ;
410
449
 
411
450
        if (rsrc.data_offset > rsrc.rsrc_len)
412
 
        {       psf_log_printf (psf, "Error : rsrc.data_offset > len\n") ;
 
451
        {       psf_log_printf (psf, "Error : rsrc.data_offset (%d, 0x%x) > len\n", rsrc.data_offset, rsrc.data_offset) ;
413
452
                error = SFE_SD2_BAD_DATA_OFFSET ;
414
453
                goto parse_rsrc_fork_cleanup ;
415
454
                } ;
463
502
 
464
503
        rsrc.str_index = -1 ;
465
504
        for (k = 0 ; k < rsrc.type_count ; k ++)
466
 
        {       marker = read_int (rsrc.rsrc_data, rsrc.type_offset + k * 8) ;
 
505
        {       marker = read_marker (rsrc.rsrc_data, rsrc.type_offset + k * 8) ;
467
506
 
468
507
                if (marker == STR_MARKER)
469
508
                {       rsrc.str_index = k ;
478
517
 
479
518
parse_rsrc_fork_cleanup :
480
519
 
 
520
        psf_use_rsrc (psf, SF_FALSE) ;
 
521
 
481
522
        if ((void *) rsrc.rsrc_data < (void *) psf || (void *) rsrc.rsrc_data > (void *) (psf + 1))
482
523
                free (rsrc.rsrc_data) ;
483
524