Merge tag 'upstream/1.0.31' into tizen
[platform/upstream/libsndfile.git] / src / aiff.c
index 6352247..763b493 100644 (file)
@@ -1,5 +1,5 @@
 /*
-** Copyright (C) 1999-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
+** Copyright (C) 1999-2018 Erik de Castro Lopo <erikd@mega-nerd.com>
 ** Copyright (C) 2005 David Viens <davidv@plogue.com>
 **
 ** This program is free software; you can redistribute it and/or modify
@@ -243,6 +243,8 @@ aiff_open (SF_PRIVATE *psf)
        if ((psf->container_data = calloc (1, sizeof (AIFF_PRIVATE))) == NULL)
                return SFE_MALLOC_FAILED ;
 
+       psf->container_close = aiff_close ;
+
        if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
        {       if ((error = aiff_read_header (psf, &comm_fmt)))
                        return error ;
@@ -283,7 +285,6 @@ aiff_open (SF_PRIVATE *psf)
                psf->set_chunk          = aiff_set_chunk ;
                } ;
 
-       psf->container_close = aiff_close ;
        psf->command = aiff_command ;
 
        switch (SF_CODEC (psf->sf.format))
@@ -359,12 +360,6 @@ aiff_open (SF_PRIVATE *psf)
                                break ;
                /* Lite remove end */
 
-               case SF_FORMAT_GSM610 :
-                               error = gsm610_init (psf) ;
-                               if (psf->sf.frames > comm_fmt.numSampleFrames)
-                                       psf->sf.frames = comm_fmt.numSampleFrames ;
-                               break ;
-
                default : return SFE_UNIMPLEMENTED ;
                } ;
 
@@ -498,6 +493,11 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
                                                return SFE_WAV_BAD_PEAK ;
                                                } ;
 
+                                       if (psf->peak_info)
+                                       {       psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
+                                               free (psf->peak_info) ;
+                                               psf->peak_info = NULL ;
+                                               } ;
                                        if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
                                                return SFE_MALLOC_FAILED ;
 
@@ -794,12 +794,16 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
                                                if (paiff->markstr == NULL)
                                                        return SFE_MALLOC_FAILED ;
 
-                                               if (mark_count > 1000)
-                                               {       psf_log_printf (psf, "  More than 1000 markers, skipping!\n") ;
+                                               if (mark_count > 2500) /* 2500 is close to the largest number of cues possible because of block sizes */
+                                               {       psf_log_printf (psf, "  More than 2500 markers, skipping!\n") ;
                                                        psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
                                                        break ;
                                                } ;
 
+                                               if (psf->cues)
+                                               {       free (psf->cues) ;
+                                                       psf->cues = NULL ;
+                                                       } ;
                                                if ((psf->cues = psf_cues_alloc (mark_count)) == NULL)
                                                        return SFE_MALLOC_FAILED ;
 
@@ -849,7 +853,7 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
 
                        case FVER_MARKER :
                                        found_chunk |= HAVE_FVER ;
-                                       /* Fall through to next case. */
+                                       /* Falls through. */
 
                        case SFX_MARKER :
                                        psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
@@ -950,7 +954,7 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
        if (psf->sf.channels < 1)
                return SFE_CHANNEL_COUNT_ZERO ;
 
-       if (psf->sf.channels >= SF_MAX_CHANNELS)
+       if (psf->sf.channels > SF_MAX_CHANNELS)
                return SFE_CHANNEL_COUNT ;
 
        if (! (found_chunk & HAVE_FORM))
@@ -1030,7 +1034,7 @@ aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
        psf_log_printf (psf, "  Sample Rate : %d\n", samplerate) ;
        psf_log_printf (psf, "  Frames      : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
 
-       if (comm_fmt->numChannels < 1 || comm_fmt->numChannels >= SF_MAX_CHANNELS)
+       if (comm_fmt->numChannels < 1 || comm_fmt->numChannels > SF_MAX_CHANNELS)
        {       psf_log_printf (psf, "  Channels    : %d (should be >= 1 and < %d)\n", comm_fmt->numChannels, SF_MAX_CHANNELS) ;
                return SFE_CHANNEL_COUNT_BAD ;
                } ;
@@ -1120,7 +1124,6 @@ aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
                                break ;
 
                case GSM_MARKER :
-                               psf->sf.format = SF_FORMAT_AIFF ;
                                psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
                                break ;
 
@@ -1163,30 +1166,30 @@ aiff_rewrite_header (SF_PRIVATE *psf)
        psf->header.indx = 0 ;
 
        /* FORM chunk. */
-       psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
+       psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
 
        /* COMM chunk. */
        if ((k = psf_find_read_chunk_m32 (&psf->rchunks, COMM_MARKER)) >= 0)
        {       psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
                comm_frames = psf->sf.frames ;
                comm_size = psf->rchunks.chunks [k].len ;
-               psf_binheader_writef (psf, "Em42t4", COMM_MARKER, comm_size, psf->sf.channels, comm_frames) ;
+               psf_binheader_writef (psf, "Em42t4", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames)) ;
                } ;
 
        /* PEAK chunk. */
        if ((k = psf_find_read_chunk_m32 (&psf->rchunks, PEAK_MARKER)) >= 0)
        {       psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
-               psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
-               psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
+               psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
+               psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
                for (ch = 0 ; ch < psf->sf.channels ; ch++)
-                       psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [ch].value, psf->peak_info->peaks [ch].position) ;
+                       psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [ch].value), BHW8 (psf->peak_info->peaks [ch].position)) ;
                } ;
 
 
        /* SSND chunk. */
        if ((k = psf_find_read_chunk_m32 (&psf->rchunks, SSND_MARKER)) >= 0)
        {       psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
-               psf_binheader_writef (psf, "Etm8", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK) ;
+               psf_binheader_writef (psf, "Etm8", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK)) ;
                } ;
 
        /* Header mangling complete so write it out. */
@@ -1196,21 +1199,6 @@ aiff_rewrite_header (SF_PRIVATE *psf)
        return ;
 } /* aiff_rewrite_header */
 
-static uint16_t
-convert_loop_mode (int type_mode)
-{      switch (type_mode)
-       {       case SF_LOOP_NONE:
-                       return 0 ;
-               case SF_LOOP_FORWARD :
-                       return 1 ;
-               case SF_LOOP_ALTERNATING :
-                       return 2 ;
-               default : break ;
-               } ;
-
-       return 0 ;
-} /* convert_loop_mode */
-
 static int
 aiff_write_header (SF_PRIVATE *psf, int calc_length)
 {      sf_count_t              current ;
@@ -1415,167 +1403,36 @@ aiff_write_header (SF_PRIVATE *psf, int calc_length)
        psf->header.indx = 0 ;
        psf_fseek (psf, 0, SEEK_SET) ;
 
-       psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
+       psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
 
        /* Write AIFF/AIFC marker and COM chunk. */
        if (comm_type == AIFC_MARKER)
                /* AIFC must have an FVER chunk. */
-               psf_binheader_writef (psf, "Emm44", comm_type, FVER_MARKER, 4, 0xA2805140) ;
+               psf_binheader_writef (psf, "Emm44", BHWm (comm_type), BHWm (FVER_MARKER), BHW4 (4), BHW4 (0xA2805140)) ;
        else
-               psf_binheader_writef (psf, "Em", comm_type) ;
+               psf_binheader_writef (psf, "Em", BHWm (comm_type)) ;
 
        paiff->comm_offset = psf->header.indx - 8 ;
 
        memset (comm_sample_rate, 0, sizeof (comm_sample_rate)) ;
        uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
 
-       psf_binheader_writef (psf, "Em42t42", COMM_MARKER, comm_size, psf->sf.channels, comm_frames, bit_width) ;
-       psf_binheader_writef (psf, "b", comm_sample_rate, sizeof (comm_sample_rate)) ;
+       psf_binheader_writef (psf, "Em42t42", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames), BHW2 (bit_width)) ;
+       psf_binheader_writef (psf, "b", BHWv (comm_sample_rate), BHWz (sizeof (comm_sample_rate))) ;
 
        /* AIFC chunks have some extra data. */
        if (comm_type == AIFC_MARKER)
-               psf_binheader_writef (psf, "mb", comm_encoding, comm_zero_bytes, sizeof (comm_zero_bytes)) ;
+               psf_binheader_writef (psf, "mb", BHWm (comm_encoding), BHWv (comm_zero_bytes), BHWz (sizeof (comm_zero_bytes))) ;
 
        if (psf->channel_map && paiff->chanmap_tag)
-               psf_binheader_writef (psf, "Em4444", CHAN_MARKER, 12, paiff->chanmap_tag, 0, 0) ;
+               psf_binheader_writef (psf, "Em4444", BHWm (CHAN_MARKER), BHW4 (12), BHW4 (paiff->chanmap_tag), BHW4 (0), BHW4 (0)) ;
 
        /* Check if there's a INST chunk to write */
        if (psf->instrument != NULL && psf->cues != NULL)
-       {       /* Both loops and cues exist */
-               uint16_t sustainLoopMode, releaseLoopMode ;
-               uint32_t idx, sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
-               int totalStringLength = 0, stringLength ;
-
-               /* Here we count how many bytes will the pascal strings need */
-               for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
-               {       stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
-                       if (stringLength % 2 == 0)
-                               totalStringLength += stringLength ;
-                       else
-                               totalStringLength += (stringLength + 1) ; /* The pascal string must have an even count */
-                       }
-
-               /* First we check which loops are active and create the necessary MARK chunk for markers */
-               /* The first written markers will be references from loop points then comes the real markers */
-               if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
-               {       /* There's both a sustain loop and a release loop */
-                       psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
-                                       MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 19) + 2 * (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 4 + psf->cues->cue_count,
-                                       1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
-                                       2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
-                                       3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
-                                       4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
-                       /* Now comes true markers from cues struct */
-                       for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
-                               psf_binheader_writef (psf, "E24p", 5 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
-
-                       /* Change the loops to be references to the markers */
-                       sLoopStart = 1 ;
-                       sLoopEnd = 2 ;
-                       rLoopStart = 3 ;
-                       rLoopEnd = 4 ;
-                       }
-               else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
-               {       /* There's a sustain loop but no release loop */
-                       psf_binheader_writef (psf, "Em42241b241b",
-                                       MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
-                                       1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
-                                       2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
-                       /* Now comes true markers from cues struct */
-                       for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
-                               psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
-
-                       /* Change the loops to be references to the markers */
-                       sLoopStart = 1 ;
-                       sLoopEnd = 2 ;
-                       rLoopStart = 0 ;
-                       rLoopEnd = 0 ;
-                       }
-               else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
-               {       /* There's a release loop but no sustain loop! Strange indeed! */
-                       psf_binheader_writef (psf, "Em42241b241b",
-                                       MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
-                                       1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
-                                       2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
-                       /* Now comes true markers from cues struct */
-                       for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
-                               psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
-
-                       /* Change the loops to be references to the markers */
-                       sLoopStart = 0 ;
-                       sLoopEnd = 0 ;
-                       rLoopStart = 1 ;
-                       rLoopEnd = 2 ;
-                       } ;
-
-               /* First convert loop modes to aiff standard */
-               sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
-               releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
-
-               /* Now we finally write the actual INST chunk */
-               psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
-                       psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
-               psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
-                       sustainLoopMode, sLoopStart, sLoopEnd,
-                       releaseLoopMode, rLoopStart, rLoopEnd) ;
-
-               }
-       else if (psf->instrument != NULL && psf->cues == NULL)
-       {       /* There are loops but no cues */
-               uint16_t sustainLoopMode, releaseLoopMode ;
-               uint32_t sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
-
-               /* First we check which loops are active and create the necessary MARK chunk for markers */
-               if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
-               {       /* There's both a sustain loop and a release loop */
-                       psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
-                                       MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 19) + 2 * (2 + 4 + 1 + 17), 4,
-                                       1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
-                                       2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
-                                       3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
-                                       4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
-                       /* Change the loops to be references to the markers */
-                       sLoopStart = 1 ;
-                       sLoopEnd = 2 ;
-                       rLoopStart = 3 ;
-                       rLoopEnd = 4 ;
-                       }
-               else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
-               {       /* There's a sustain loop but no release loop */
-                       psf_binheader_writef (psf, "Em42241b241b",
-                                       MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
-                                       1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
-                                       2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
-                       /* Change the loops to be references to the markers */
-                       sLoopStart = 1 ;
-                       sLoopEnd = 2 ;
-                       rLoopStart = 0 ;
-                       rLoopEnd = 0 ;
-                       }
-               else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
-               {       /* There's a release loop but no sustain loop! Strange indeed! */
-                       psf_binheader_writef (psf, "Em42241b241b",
-                                       MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
-                                       1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
-                                       2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
-                       /* Change the loops to be references to the markers */
-                       sLoopStart = 0 ;
-                       sLoopEnd = 0 ;
-                       rLoopStart = 1 ;
-                       rLoopEnd = 2 ;
-                       } ;
-
-               /* First convert loop modes to aiff standard */
-               sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
-               releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
-
-               /* Now we finally write the actual INST chunk */
-               psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
-                       psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
-               psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
-                       sustainLoopMode, sLoopStart, sLoopEnd,
-                       releaseLoopMode, rLoopStart, rLoopEnd) ;
-
+       {       /* Huge chunk of code removed here because it had egregious errors that were
+               ** not detected by either the compiler or the tests. It was found when updating
+               ** the way psf_binheader_writef works.
+               */
                }
        else if (psf->instrument == NULL && psf->cues != NULL)
        {       /* There are cues but no loops */
@@ -1589,29 +1446,29 @@ aiff_write_header (SF_PRIVATE *psf, int calc_length)
                        } ;
 
                psf_binheader_writef (psf, "Em42",
-                       MARK_MARKER, 2 + psf->cues->cue_count * (2 + 4) + totalStringLength, psf->cues->cue_count) ;
+                       BHWm (MARK_MARKER), BHW4 (2 + psf->cues->cue_count * (2 + 4) + totalStringLength), BHW2 (psf->cues->cue_count)) ;
 
                for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
-                       psf_binheader_writef (psf, "E24p", psf->cues->cue_points [idx].indx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
+                       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)) ;
                } ;
 
        if (psf->strings.flags & SF_STR_LOCATE_START)
                aiff_write_strings (psf, SF_STR_LOCATE_START) ;
 
        if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
-       {       psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
-               psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
+       {       psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
+               psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
                for (k = 0 ; k < psf->sf.channels ; k++)
-                       psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
+                       psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
                } ;
 
        /* Write custom headers. */
        for (uk = 0 ; uk < psf->wchunks.used ; uk++)
-               psf_binheader_writef (psf, "Em4b", psf->wchunks.chunks [uk].mark32, psf->wchunks.chunks [uk].len, psf->wchunks.chunks [uk].data, make_size_t (psf->wchunks.chunks [uk].len)) ;
+               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)) ;
 
        /* Write SSND chunk. */
        paiff->ssnd_offset = psf->header.indx ;
-       psf_binheader_writef (psf, "Etm844", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK, 0, 0) ;
+       psf_binheader_writef (psf, "Etm844", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK), BHW4 (0), BHW4 (0)) ;
 
        /* Header construction complete so write it out. */
        psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
@@ -1649,10 +1506,10 @@ aiff_write_tailer (SF_PRIVATE *psf)
                } ;
 
        if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
-       {       psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
-               psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
+       {       psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
+               psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
                for (k = 0 ; k < psf->sf.channels ; k++)
-                       psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
+                       psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
                } ;
 
        if (psf->strings.flags & SF_STR_LOCATE_END)
@@ -1679,28 +1536,28 @@ aiff_write_strings (SF_PRIVATE *psf, int location)
                switch (psf->strings.data [k].type)
                {       case SF_STR_SOFTWARE :
                                slen = strlen (psf->strings.storage + psf->strings.data [k].offset) ;
-                               psf_binheader_writef (psf, "Em4mb", APPL_MARKER, slen + 4, m3ga_MARKER, psf->strings.storage + psf->strings.data [k].offset, make_size_t (slen + (slen & 1))) ;
+                               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))) ;
                                break ;
 
                        case SF_STR_TITLE :
-                               psf_binheader_writef (psf, "EmS", NAME_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
+                               psf_binheader_writef (psf, "EmS", BHWm (NAME_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
                                break ;
 
                        case SF_STR_COPYRIGHT :
-                               psf_binheader_writef (psf, "EmS", c_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
+                               psf_binheader_writef (psf, "EmS", BHWm (c_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
                                break ;
 
                        case SF_STR_ARTIST :
-                               psf_binheader_writef (psf, "EmS", AUTH_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
+                               psf_binheader_writef (psf, "EmS", BHWm (AUTH_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
                                break ;
 
                        case SF_STR_COMMENT :
-                               psf_binheader_writef (psf, "EmS", ANNO_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
+                               psf_binheader_writef (psf, "EmS", BHWm (ANNO_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
                                break ;
 
                        /*
                        case SF_STR_DATE :
-                               psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings.data [k].str) ;
+                               psf_binheader_writef (psf, "Ems", BHWm (ICRD_MARKER), BHWs (psf->strings.data [k].str)) ;
                                break ;
                        */
                        } ;
@@ -1866,6 +1723,11 @@ aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
 
        psf_log_printf (psf, "  Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
 
+       if (psf->loop_info)
+       {       psf_log_printf (psf, "  Found existing loop info, using last one.\n") ;
+               free (psf->loop_info) ;
+               psf->loop_info = NULL ;
+               } ;
        if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
                return SFE_MALLOC_FAILED ;
 
@@ -1905,7 +1767,7 @@ aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
                psf_binheader_readf (psf, "j", dword - bytesread) ;
 
        if (map_info->channel_map != NULL)
-       {       size_t chanmap_size = psf->sf.channels * sizeof (psf->channel_map [0]) ;
+       {       size_t chanmap_size = SF_MIN (psf->sf.channels, layout_tag & 0xffff) * sizeof (psf->channel_map [0]) ;
 
                free (psf->channel_map) ;