Bug Summary

File:libs/libsndfile/src/alac.c
Location:line 440, column 3
Description:Argument to free() is the address of the local variable 'byte_buffer', which is not memory allocated by malloc()

Annotated Source Code

1/*
2** Copyright (C) 2011-2013 Erik de Castro Lopo <erikd@mega-nerd.com>
3**
4** This program is free software; you can redistribute it and/or modify
5** it under the terms of the GNU Lesser General Public License as published by
6** the Free Software Foundation; either version 2.1 of the License, or
7** (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12** GNU Lesser General Public License for more details.
13**
14** You should have received a copy of the GNU Lesser General Public License
15** along with this program; if not, write to the Free Software
16** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#include "sfconfig.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <math.h>
25#include <errno(*__errno_location ()).h>
26
27#include "sndfile.h"
28#include "sfendian.h"
29#include "common.h"
30#include "ALAC/alac_codec.h"
31#include "ALAC/ALACBitUtilities.h"
32
33#define ALAC_MAX_FRAME_SIZE8192 8192
34#define ALAC_BYTE_BUFFER_SIZE82000 82000
35
36
37typedef struct
38{ uint32_t current, count, allocated ;
39 uint32_t packet_size [] ;
40} PAKT_INFO ;
41
42typedef struct
43{ sf_count_t input_data_pos ;
44
45 PAKT_INFO * pakt_info ;
46
47 int channels, final_write_block ;
48
49 uint32_t frames_this_block, partial_block_frames, frames_per_block ;
50 uint32_t bits_per_sample, kuki_size ;
51
52
53 /* Can't have a decoder and an encoder at the same time so stick
54 ** them in an un-named union.
55 */
56 union
57 { ALAC_DECODER decoder ;
58 ALAC_ENCODER encoder ;
59 } ;
60
61 char enctmpname [512] ;
62 FILE *enctmp ;
63
64 int buffer [] ;
65
66} ALAC_PRIVATE ;
67
68/*============================================================================================
69*/
70
71static int alac_reader_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info) ;
72static int alac_writer_init (SF_PRIVATE *psf) ;
73
74static sf_count_t alac_reader_calc_frames (SF_PRIVATE *psf, ALAC_PRIVATE *plac) ;
75
76static sf_count_t alac_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
77static sf_count_t alac_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
78static sf_count_t alac_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
79static sf_count_t alac_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
80
81static sf_count_t alac_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
82static sf_count_t alac_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
83static sf_count_t alac_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
84static sf_count_t alac_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
85
86static sf_count_t alac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
87
88static int alac_close (SF_PRIVATE *psf) ;
89static int alac_byterate (SF_PRIVATE *psf) ;
90
91static int alac_decode_block (SF_PRIVATE *psf, ALAC_PRIVATE *plac) ;
92static int alac_encode_block (SF_PRIVATE *psf, ALAC_PRIVATE *plac) ;
93
94static uint32_t alac_kuki_read (SF_PRIVATE * psf, uint32_t kuki_offset, uint8_t * kuki, size_t kuki_maxlen) ;
95
96static PAKT_INFO * alac_pakt_alloc (uint32_t initial_count) ;
97static PAKT_INFO * alac_pakt_read_decode (SF_PRIVATE * psf, uint32_t pakt_offset) ;
98static PAKT_INFO * alac_pakt_append (PAKT_INFO * info, uint32_t value) ;
99static uint8_t * alac_pakt_encode (const SF_PRIVATE *psf, uint32_t * pakt_size) ;
100static sf_count_t alac_pakt_block_offset (const PAKT_INFO *info, uint32_t block) ;
101
102/*============================================================================================
103** ALAC Reader initialisation function.
104*/
105
106int
107alac_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info)
108{ int error ;
109
110 if ((psf->codec_data = calloc (1, sizeof (ALAC_PRIVATE) + psf->sf.channels * sizeof (int) * ALAC_MAX_FRAME_SIZE8192)) == NULL((void*)0))
111 return SFE_MALLOC_FAILED ;
112
113 psf->codec_close = alac_close ;
114
115 switch (psf->file.mode)
116 { case SFM_RDWR :
117 return SFE_BAD_MODE_RW ;
118
119 case SFM_READ :
120 if ((error = alac_reader_init (psf, info)))
121 return error ;
122 break ;
123
124 case SFM_WRITE :
125 if ((error = alac_writer_init (psf)))
126 return error ;
127 break ;
128
129 default :
130 psf_log_printf (psf, "%s : Bad psf->file.mode.\n", __func__) ;
131 return SFE_INTERNAL ;
132 } ;
133
134 psf->byterate = alac_byterate ;
135
136 return 0 ;
137} /* aiff_alac_init */
138
139void
140alac_get_desc_chunk_items (int subformat, uint32_t *fmt_flags, uint32_t *frames_per_packet)
141{ switch (subformat)
142 { case SF_FORMAT_ALAC_16 :
143 *fmt_flags = 1 ;
144 break ;
145 case SF_FORMAT_ALAC_20 :
146 *fmt_flags = 2 ;
147 break ;
148 case SF_FORMAT_ALAC_24 :
149 *fmt_flags = 3 ;
150 break ;
151 case SF_FORMAT_ALAC_32 :
152 *fmt_flags = 4 ;
153 break ;
154 default :
155 break ;
156 } ;
157 *frames_per_packet = ALAC_FRAME_LENGTH4096 ;
158} /* alac_get_desc_chunk_items */
159
160static int
161alac_close (SF_PRIVATE *psf)
162{ ALAC_PRIVATE *plac ;
163 BUF_UNION ubuf ;
164
165 plac = psf->codec_data ;
166
167 if (psf->file.mode == SFM_WRITE)
1
Taking true branch
168 { ALAC_ENCODER *penc = &plac->encoder ;
169 SF_CHUNK_INFO chunk_info ;
170 sf_count_t readcount ;
171 uint32_t pakt_size = 0, saved_partial_block_frames ;
172#ifndef _MSC_VER
173 uint8_t *kuki_data [plac->kuki_size];
174#else
175 uint8_t *kuki_data = (uint8_t *)_alloca(plac->kuki_size);
176#endif
177
178 plac->final_write_block = 1 ;
179 saved_partial_block_frames = plac->partial_block_frames ;
180
181 /* If a block has been partially assembled, write it out as the final block. */
182 if (plac->partial_block_frames && plac->partial_block_frames < plac->frames_per_block)
2
Taking true branch
183 alac_encode_block (psf, plac) ;
3
Calling 'alac_encode_block'
184
185 plac->partial_block_frames = saved_partial_block_frames ;
186
187 alac_get_magic_cookie (penc, kuki_data, &plac->kuki_size) ;
188
189 memset (&chunk_info, 0, sizeof (chunk_info)) ;
190 chunk_info.id_size = snprintf (chunk_info.id, sizeof (chunk_info.id), "kuki")__builtin___snprintf_chk (chunk_info.id, sizeof (chunk_info.id
), 2 - 1, __builtin_object_size (chunk_info.id, 2 > 1), "kuki"
)
;
191 chunk_info.data = kuki_data ;
192 chunk_info.datalen = plac->kuki_size ;
193 psf_save_write_chunk (&psf->wchunks, &chunk_info) ;
194
195 memset (&chunk_info, 0, sizeof (chunk_info)) ;
196 chunk_info.id_size = snprintf (chunk_info.id, sizeof (chunk_info.id), "pakt")__builtin___snprintf_chk (chunk_info.id, sizeof (chunk_info.id
), 2 - 1, __builtin_object_size (chunk_info.id, 2 > 1), "pakt"
)
;
197 chunk_info.data = alac_pakt_encode (psf, &pakt_size) ;
198 chunk_info.datalen = pakt_size ;
199 psf_save_write_chunk (&psf->wchunks, &chunk_info) ;
200
201 free (chunk_info.data) ;
202 chunk_info.data = NULL((void*)0) ;
203
204 psf->write_header (psf, 1) ;
205
206 if (plac->enctmp != NULL((void*)0))
207 { fseek (plac->enctmp, 0, SEEK_SET0) ;
208
209 while ((readcount = fread (ubuf.ucbuf, 1, sizeof (ubuf.ucbuf), plac->enctmp)) > 0)
210 psf_fwrite (ubuf.ucbuf, 1, readcount, psf) ;
211 fclose (plac->enctmp) ;
212 remove (plac->enctmpname) ;
213 } ;
214 free(kuki_data);
215 } ;
216
217 if (plac->pakt_info)
218 free (plac->pakt_info) ;
219 plac->pakt_info = NULL((void*)0) ;
220
221 return 0 ;
222} /* alac_close */
223
224static int
225alac_byterate (SF_PRIVATE *psf)
226{
227 if (psf->file.mode == SFM_READ)
228 return (psf->datalength * psf->sf.samplerate) / psf->sf.frames ;
229
230 return -1 ;
231} /* alac_byterate */
232
233/*============================================================================================
234** ALAC initialisation Functions.
235*/
236
237static int
238alac_reader_init (SF_PRIVATE *psf, const ALAC_DECODER_INFO * info)
239{ ALAC_PRIVATE *plac ;
240 uint32_t kuki_size ;
241 union { uint8_t kuki [512] ; uint32_t alignment ; } u ;
242
243 if (info == NULL((void*)0))
244 { psf_log_printf (psf, "%s : ALAC_DECODER_INFO is NULL.\n", __func__) ;
245 return SFE_INTERNAL ;
246 } ;
247
248 plac = psf->codec_data ;
249
250 plac->channels = psf->sf.channels ;
251 plac->frames_per_block = info->frames_per_packet ;
252 plac->bits_per_sample = info->bits_per_sample ;
253
254 if (plac->pakt_info != NULL((void*)0))
255 free (plac->pakt_info) ;
256 plac->pakt_info = alac_pakt_read_decode (psf, info->pakt_offset) ;
257
258
259 if (plac->pakt_info == NULL((void*)0))
260 { psf_log_printf (psf, "%s : alac_pkt_read() returns NULL.\n", __func__) ;
261 return SFE_MALLOC_FAILED ;
262 } ;
263
264 /* Read in the ALAC cookie data and pass it to the init function. */
265 kuki_size = alac_kuki_read (psf, info->kuki_offset, u.kuki, sizeof (u.kuki)) ;
266
267 alac_decoder_init (&plac->decoder, u.kuki, kuki_size) ;
268
269 switch (info->bits_per_sample)
270 { case 16 :
271 case 20 :
272 case 24 :
273 case 32 :
274 psf->read_short = alac_read_s ;
275 psf->read_int = alac_read_i ;
276 psf->read_float = alac_read_f ;
277 psf->read_double = alac_read_d ;
278 break ;
279
280 default :
281 printf ("%s : info->bits_per_sample %u\n", __func__, info->bits_per_sample)__printf_chk (2 - 1, "%s : info->bits_per_sample %u\n", __func__
, info->bits_per_sample)
;
282 return SFE_UNSUPPORTED_ENCODING ;
283 } ;
284
285 psf->codec_close = alac_close ;
286 psf->seek = alac_seek ;
287
288 psf->sf.frames = alac_reader_calc_frames (psf, plac) ;
289 alac_seek (psf, SFM_READ, 0) ;
290
291 return 0 ;
292} /* alac_reader_init */
293
294static int
295alac_writer_init (SF_PRIVATE *psf)
296{ ALAC_PRIVATE *plac ;
297 uint32_t alac_format_flags = 0 ;
298
299 plac = psf->codec_data ;
300
301 if (psf->file.mode != SFM_WRITE)
302 return SFE_BAD_MODE_RW ;
303
304 plac->channels = psf->sf.channels ;
305 plac->kuki_size = alac_get_magic_cookie_size (psf->sf.channels) ;
306
307 psf->write_short = alac_write_s ;
308 psf->write_int = alac_write_i ;
309 psf->write_float = alac_write_f ;
310 psf->write_double = alac_write_d ;
311
312 switch (SF_CODEC (psf->sf.format)((psf->sf.format) & SF_FORMAT_SUBMASK))
313 { case SF_FORMAT_ALAC_16 :
314 alac_format_flags = 1 ;
315 plac->bits_per_sample = 16 ;
316 break ;
317
318 case SF_FORMAT_ALAC_20 :
319 alac_format_flags = 2 ;
320 plac->bits_per_sample = 20 ;
321 break ;
322
323 case SF_FORMAT_ALAC_24 :
324 alac_format_flags = 3 ;
325 plac->bits_per_sample = 24 ;
326 break ;
327
328 case SF_FORMAT_ALAC_32 :
329 alac_format_flags = 4 ;
330 plac->bits_per_sample = 32 ;
331 break ;
332
333 default :
334 psf_log_printf (psf, "%s : Can't figure out bits per sample.\n", __func__) ;
335 return SFE_UNIMPLEMENTED ;
336 } ;
337
338 plac->frames_per_block = ALAC_FRAME_LENGTH4096 ;
339
340 plac->pakt_info = alac_pakt_alloc (2000) ;
341
342 if ((plac->enctmp = psf_open_tmpfile (plac->enctmpname, sizeof (plac->enctmpname))) == NULL((void*)0))
343 { psf_log_printf (psf, "Error : Failed to open temp file '%s' : \n", plac->enctmpname, strerror (errno(*__errno_location ()))) ;
344 return SFE_ALAC_FAIL_TMPFILE ;
345 } ;
346
347 alac_encoder_init (&plac->encoder, psf->sf.samplerate, psf->sf.channels, alac_format_flags, ALAC_FRAME_LENGTH4096) ;
348
349 return 0 ;
350} /* alac_writer_init */
351
352/*============================================================================================
353** ALAC block decoder and encoder.
354*/
355
356static inline uint32_t
357alac_reader_next_packet_size (PAKT_INFO * info)
358{ if (info->current >= info->count)
359 return 0 ;
360 return info->packet_size [info->current++] ;
361} /* alac_reader_next_packet_size */
362
363static sf_count_t
364alac_reader_calc_frames (SF_PRIVATE *psf, ALAC_PRIVATE *plac)
365{ sf_count_t frames = 0 ;
366 uint32_t current_pos = 1, blocks = 0 ;
367
368 plac->pakt_info->current = 0 ;
369
370 while (current_pos < psf->filelength && current_pos > 0)
371 { current_pos = alac_reader_next_packet_size (plac->pakt_info) ;
372 blocks = current_pos > 0 ? blocks + 1 : blocks ;
373 } ;
374
375 if (blocks == 0)
376 return 0 ;
377
378 /* Only count full blocks. */
379 frames = plac->frames_per_block * (blocks - 1) ;
380
381 alac_seek (psf, SFM_READ, frames) ;
382 alac_decode_block (psf, plac) ;
383 frames += plac->frames_this_block ;
384
385 plac->pakt_info->current = 0 ;
386
387 return frames ;
388} /* alac_reader_calc_frames */
389
390static int
391alac_decode_block (SF_PRIVATE *psf, ALAC_PRIVATE *plac)
392{ ALAC_DECODER *pdec = &plac->decoder ;
393 uint8_t byte_buffer [ALAC_BYTE_BUFFER_SIZE82000] ;
394 uint32_t packet_size ;
395 BitBuffer bit_buffer ;
396
397 packet_size = alac_reader_next_packet_size (plac->pakt_info) ;
398 if (packet_size == 0)
399 { if (plac->pakt_info->current < plac->pakt_info->count)
400 psf_log_printf (psf, "packet_size is 0 (%d of %d)\n", plac->pakt_info->current, plac->pakt_info->count) ;
401 return 0 ;
402 } ;
403
404 psf_fseek (psf, plac->input_data_pos, SEEK_SET0) ;
405
406 if (packet_size > SIGNED_SIZEOF (byte_buffer)((int) sizeof (byte_buffer)))
407 { psf_log_printf (psf, "%s : bad packet_size (%u)\n", __func__, packet_size) ;
408 return 0 ;
409 } ;
410
411 if ((packet_size != psf_fread (byte_buffer, 1, packet_size, psf)))
412 return 0 ;
413
414 BitBufferInit (&bit_buffer, byte_buffer, packet_size) ;
415
416 plac->input_data_pos += packet_size ;
417 plac->frames_this_block = 0 ;
418 alac_decode (pdec, &bit_buffer, plac->buffer, plac->frames_per_block, psf->sf.channels, &plac->frames_this_block) ;
419
420 plac->partial_block_frames = 0 ;
421
422 return 1 ;
423} /* alac_decode_block */
424
425
426static int
427alac_encode_block (SF_PRIVATE * psf, ALAC_PRIVATE *plac)
428{ ALAC_ENCODER *penc = &plac->encoder ;
429 uint32_t num_bytes = 0 ;
430#ifndef _MSC_VER
431 uint8_t byte_buffer [psf->sf.channels * ALAC_BYTE_BUFFER_SIZE82000] ;
432#else
433 uint8_t* byte_buffer = (uint8_t*)_alloca (psf->sf.channels * ALAC_BYTE_BUFFER_SIZE82000) ;
434#endif
435
436 alac_encode (penc, plac->channels, plac->partial_block_frames, plac->buffer, byte_buffer, &num_bytes) ;
437
438 if (fwrite (byte_buffer, 1, num_bytes, plac->enctmp) != num_bytes)
4
Taking true branch
439 {
440 free (byte_buffer);
5
Argument to free() is the address of the local variable 'byte_buffer', which is not memory allocated by malloc()
441 return 0 ;
442 }
443 free(byte_buffer);
444
445 if ((plac->pakt_info = alac_pakt_append (plac->pakt_info, num_bytes)) == NULL((void*)0))
446 return 0 ;
447
448 plac->partial_block_frames = 0 ;
449
450 return 1 ;
451} /* alac_encode_block */
452
453/*============================================================================================
454** ALAC read functions.
455*/
456
457static sf_count_t
458alac_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
459{ ALAC_PRIVATE *plac ;
460 int *iptr ;
461 int k, readcount ;
462 sf_count_t total = 0 ;
463
464 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
465 return 0 ;
466
467 while (len > 0)
468 { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
469 break ;
470
471 readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
472 readcount = readcount > len ? len : readcount ;
473
474 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
475
476 for (k = 0 ; k < readcount ; k++)
477 ptr [total + k] = iptr [k] >> 16 ;
478
479 plac->partial_block_frames += readcount / plac->channels ;
480 total += readcount ;
481 len -= readcount ;
482 } ;
483
484 return total ;
485} /* alac_read_s */
486
487static sf_count_t
488alac_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
489{ ALAC_PRIVATE *plac ;
490 int *iptr ;
491 int k, readcount ;
492 sf_count_t total = 0 ;
493
494 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
495 return 0 ;
496
497 while (len > 0)
498 { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
499 break ;
500
501 readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
502 readcount = readcount > len ? len : readcount ;
503
504 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
505
506 for (k = 0 ; k < readcount ; k++)
507 ptr [total + k] = iptr [k] ;
508
509 plac->partial_block_frames += readcount / plac->channels ;
510 total += readcount ;
511 len -= readcount ;
512 } ;
513
514 return total ;
515} /* alac_read_i */
516
517static sf_count_t
518alac_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
519{ ALAC_PRIVATE *plac ;
520 int *iptr ;
521 int k, readcount ;
522 sf_count_t total = 0 ;
523 float normfact ;
524
525 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
526 return 0 ;
527
528 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
529
530 while (len > 0)
531 { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
532 break ;
533
534 readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
535 readcount = readcount > len ? len : readcount ;
536
537 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
538
539 for (k = 0 ; k < readcount ; k++)
540 ptr [total + k] = normfact * iptr [k] ;
541
542 plac->partial_block_frames += readcount / plac->channels ;
543 total += readcount ;
544 len -= readcount ;
545 } ;
546
547 return total ;
548} /* alac_read_f */
549
550static sf_count_t
551alac_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
552{ ALAC_PRIVATE *plac ;
553 int *iptr ;
554 int k, readcount ;
555 sf_count_t total = 0 ;
556 double normfact ;
557
558 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
559 return 0 ;
560
561 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
562
563 while (len > 0)
564 { if (plac->partial_block_frames >= plac->frames_this_block && alac_decode_block (psf, plac) == 0)
565 break ;
566
567 readcount = (plac->frames_this_block - plac->partial_block_frames) * plac->channels ;
568 readcount = readcount > len ? len : readcount ;
569
570 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
571
572 for (k = 0 ; k < readcount ; k++)
573 ptr [total + k] = normfact * iptr [k] ;
574
575 plac->partial_block_frames += readcount / plac->channels ;
576 total += readcount ;
577 len -= readcount ;
578 } ;
579
580 return total ;
581} /* alac_read_d */
582
583/*============================================================================================
584*/
585
586static sf_count_t
587alac_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
588{ ALAC_PRIVATE *plac ;
589 int newblock, newsample ;
590
591 if (! psf->codec_data)
592 return 0 ;
593 plac = (ALAC_PRIVATE*) psf->codec_data ;
594
595 if (psf->datalength < 0 || psf->dataoffset < 0)
596 { psf->error = SFE_BAD_SEEK ;
597 return PSF_SEEK_ERROR((sf_count_t) -1) ;
598 } ;
599
600 if (offset == 0)
601 { psf_fseek (psf, psf->dataoffset, SEEK_SET0) ;
602
603 plac->frames_this_block = 0 ;
604 plac->input_data_pos = psf->dataoffset ;
605 plac->pakt_info->current = 0 ;
606 return 0 ;
607 } ;
608
609 if (offset < 0 || offset > plac->pakt_info->count * plac->frames_per_block)
610 { psf->error = SFE_BAD_SEEK ;
611 return PSF_SEEK_ERROR((sf_count_t) -1) ;
612 } ;
613
614 newblock = offset / plac->frames_per_block ;
615 newsample = offset % plac->frames_per_block ;
616
617 if (mode == SFM_READ)
618 { plac->input_data_pos = psf->dataoffset + alac_pakt_block_offset (plac->pakt_info, newblock) ;
619
620 plac->pakt_info->current = newblock ;
621 alac_decode_block (psf, plac) ;
622 plac->partial_block_frames = newsample ;
623 }
624 else
625 { /* What to do about write??? */
626 psf->error = SFE_BAD_SEEK ;
627 return PSF_SEEK_ERROR((sf_count_t) -1) ;
628 } ;
629
630 return newblock * plac->frames_per_block + newsample ;
631} /* alac_seek */
632
633/*==========================================================================================
634** ALAC Write Functions.
635*/
636
637static sf_count_t
638alac_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
639{ ALAC_PRIVATE *plac ;
640 int *iptr ;
641 int k, writecount ;
642 sf_count_t total = 0 ;
643
644 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
645 return 0 ;
646
647 while (len > 0)
648 { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
649 writecount = (writecount == 0 || writecount > len) ? len : writecount ;
650
651 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
652
653 for (k = 0 ; k < writecount ; k++)
654 iptr [k] = ptr [total + k] << 16 ;
655
656 plac->partial_block_frames += writecount / plac->channels ;
657 total += writecount ;
658 len -= writecount ;
659
660 if (plac->partial_block_frames >= plac->frames_per_block)
661 alac_encode_block (psf, plac) ;
662 } ;
663
664 return total ;
665} /* alac_write_s */
666
667static sf_count_t
668alac_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
669{ ALAC_PRIVATE *plac ;
670 int *iptr ;
671 int k, writecount ;
672 sf_count_t total = 0 ;
673
674 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
675 return 0 ;
676
677 while (len > 0)
678 { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
679 writecount = (writecount == 0 || writecount > len) ? len : writecount ;
680
681 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
682
683 for (k = 0 ; k < writecount ; k++)
684 iptr [k] = ptr [total + k] ;
685
686 plac->partial_block_frames += writecount / plac->channels ;
687 total += writecount ;
688 len -= writecount ;
689
690 if (plac->partial_block_frames >= plac->frames_per_block)
691 alac_encode_block (psf, plac) ;
692 } ;
693
694 return total ;
695} /* alac_write_i */
696
697static sf_count_t
698alac_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
699{ ALAC_PRIVATE *plac ;
700 void (*convert) (const float *, int *t, int, int) ;
701 int *iptr ;
702 int writecount ;
703 sf_count_t total = 0 ;
704
705 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
706 return 0 ;
707
708 convert = (psf->add_clipping) ? psf_f2i_clip_array : psf_f2i_array ;
709
710 while (len > 0)
711 { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
712 writecount = (writecount == 0 || writecount > len) ? len : writecount ;
713
714 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
715
716 convert (ptr, iptr, writecount, psf->norm_float) ;
717
718 plac->partial_block_frames += writecount / plac->channels ;
719 total += writecount ;
720 len -= writecount ;
721
722 if (plac->partial_block_frames >= plac->frames_per_block)
723 alac_encode_block (psf, plac) ;
724 } ;
725
726 return total ;
727} /* alac_write_f */
728
729static sf_count_t
730alac_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
731{ ALAC_PRIVATE *plac ;
732 void (*convert) (const double *, int *t, int, int) ;
733 int *iptr ;
734 int writecount ;
735 sf_count_t total = 0 ;
736
737 if ((plac = (ALAC_PRIVATE*) psf->codec_data) == NULL((void*)0))
738 return 0 ;
739
740 convert = (psf->add_clipping) ? psf_d2i_clip_array : psf_d2i_array ;
741
742 while (len > 0)
743 { writecount = (plac->frames_per_block - plac->partial_block_frames) * plac->channels ;
744 writecount = (writecount == 0 || writecount > len) ? len : writecount ;
745
746 iptr = plac->buffer + plac->partial_block_frames * plac->channels ;
747
748 convert (ptr, iptr, writecount, psf->norm_float) ;
749
750 plac->partial_block_frames += writecount / plac->channels ;
751 total += writecount ;
752 len -= writecount ;
753
754 if (plac->partial_block_frames >= plac->frames_per_block)
755 alac_encode_block (psf, plac) ;
756 } ;
757
758 return total ;
759} /* alac_write_d */
760
761/*==============================================================================
762** PAKT_INFO handling.
763*/
764
765static PAKT_INFO *
766alac_pakt_alloc (uint32_t initial_count)
767{ PAKT_INFO * info ;
768
769 if ((info = calloc (1, sizeof (PAKT_INFO) + initial_count * sizeof (info->packet_size [0]))) == NULL((void*)0))
770 return NULL((void*)0) ;
771
772 info->allocated = initial_count ;
773 info->current = 0 ;
774 info->count = 0 ;
775
776 return info ;
777} /* alac_pakt_alloc */
778
779static PAKT_INFO *
780alac_pakt_append (PAKT_INFO * info, uint32_t value)
781{
782 if (info->count >= info->allocated)
783 { PAKT_INFO * temp ;
784 uint32_t newcount = info->allocated + info->allocated / 2 ;
785
786 if ((temp = realloc (info, sizeof (PAKT_INFO) + newcount * sizeof (info->packet_size [0]))) == NULL((void*)0))
787 return NULL((void*)0) ;
788
789 info = temp ;
790 info->allocated = newcount ;
791 } ;
792
793 info->packet_size [info->count++] = value ;
794 return info ;
795} /* alac_pakt_append */
796
797static PAKT_INFO *
798alac_pakt_read_decode (SF_PRIVATE * psf, uint32_t UNUSED (pakt_offset)UNUSED_pakt_offset __attribute__ ((unused)))
799{ SF_CHUNK_INFO chunk_info ;
800 PAKT_INFO * info = NULL((void*)0) ;
801 uint8_t *pakt_data = NULL((void*)0) ;
802 uint32_t bcount, value = 1, pakt_size ;
803 SF_CHUNK_ITERATOR * chunk_iterator ;
804
805
806 memset (&chunk_info, 0, sizeof (chunk_info)) ;
807 snprintf (chunk_info.id, sizeof (chunk_info.id), "pakt")__builtin___snprintf_chk (chunk_info.id, sizeof (chunk_info.id
), 2 - 1, __builtin_object_size (chunk_info.id, 2 > 1), "pakt"
)
;
808 chunk_info.id_size = 4 ;
809
810 if ((chunk_iterator = psf_get_chunk_iterator (psf, chunk_info.id)) == NULL((void*)0))
811 { printf ("%s %d : no chunk iterator found\n\n", __func__, __LINE__)__printf_chk (2 - 1, "%s %d : no chunk iterator found\n\n", __func__
, 811)
;
812 free (chunk_info.data) ;
813 chunk_info.data = NULL((void*)0) ;
814 return NULL((void*)0) ;
815 } ;
816
817 psf->get_chunk_size (psf, chunk_iterator, &chunk_info) ;
818
819 pakt_size = chunk_info.datalen ;
820 chunk_info.data = pakt_data = malloc (pakt_size + 5) ;
821
822 if ((bcount = psf->get_chunk_data (psf, chunk_iterator, &chunk_info)) != SF_ERR_NO_ERROR)
823 { printf ("%s %d : %s\n\n", __func__, __LINE__, sf_error_number (bcount))__printf_chk (2 - 1, "%s %d : %s\n\n", __func__, 823, sf_error_number
(bcount))
;
824 while (chunk_iterator)
825 chunk_iterator = psf->next_chunk_iterator (psf, chunk_iterator) ;
826 free (chunk_info.data) ;
827 chunk_info.data = NULL((void*)0) ;
828 return NULL((void*)0) ;
829 } ;
830
831 while (chunk_iterator)
832 chunk_iterator = psf->next_chunk_iterator (psf, chunk_iterator) ;
833
834 info = alac_pakt_alloc (pakt_size / 4) ;
835
836 /* Start at 24 bytes in, skipping over the 'pakt' chunks header. */
837 for (bcount = 24 ; bcount < pakt_size && value != 0 ; )
838 { uint8_t byte ;
839 int32_t count = 0 ;
840
841 value = 0 ;
842 do
843 { byte = pakt_data [bcount + count] ;
844 value = (value << 7) + (byte & 0x7F) ;
845
846 count ++ ;
847 if (count > 5 || bcount + count > pakt_size)
848 { printf ("%s %d : Ooops! count %d bcount %d\n", __func__, __LINE__, count, bcount)__printf_chk (2 - 1, "%s %d : Ooops! count %d bcount %d\n"
, __func__, 848, count, bcount)
;
849 value = 0 ;
850 break ;
851 } ;
852 }
853 while (byte & 0x80) ;
854
855 bcount += count ;
856
857 if ((info = alac_pakt_append (info, value)) == NULL((void*)0))
858 goto FreeExit ;
859 } ;
860
861 free (pakt_data) ;
862
863 return info ;
864
865FreeExit :
866 free (pakt_data) ;
867 free (info) ;
868 return NULL((void*)0) ;
869} /* alac_pakt_read_decode */
870
871static uint8_t *
872alac_pakt_encode (const SF_PRIVATE *psf, uint32_t * pakt_size_out)
873{ const ALAC_PRIVATE *plac ;
874 const PAKT_INFO *info ;
875 uint8_t *data ;
876 uint32_t k, allocated, pakt_size ;
877
878 plac = psf->codec_data ;
879 info = plac->pakt_info ;
880
881 allocated = 100 + 2 * info->count ;
882 if ((data = calloc (1, allocated)) == NULL((void*)0))
883 return NULL((void*)0) ;
884
885 psf_put_be64 (data, 0, info->count) ;
886 psf_put_be64 (data, 8, psf->sf.frames) ;
887 psf_put_be32 (data, 20, kALACDefaultFramesPerPacket - plac->partial_block_frames) ;
888
889 /* Real 'pakt' data starts after 24 byte header. */
890 pakt_size = 24 ;
891
892 for (k = 0 ; k < info->count ; k++)
893 { int32_t value = info->packet_size [k] ;
894
895 if ((value & 0x7f) == value)
896 { data [pakt_size++] = value ;
897 continue ;
898 } ;
899
900 if ((value & 0x3fff) == value)
901 { data [pakt_size++] = (value >> 7) | 0x80 ;
902 data [pakt_size++] = value & 0x7f ;
903 continue ;
904 } ;
905
906 if ((value & 0x1fffff) == value)
907 { data [pakt_size++] = (value >> 14) | 0x80 ;
908 data [pakt_size++] = ((value >> 7) & 0x7f) | 0x80 ;
909 data [pakt_size++] = value & 0x7f ;
910 continue ;
911 } ;
912
913 if ((value & 0x0fffffff) == value)
914 { data [pakt_size++] = (value >> 21) | 0x80 ;
915 data [pakt_size++] = ((value >> 14) & 0x7f) | 0x80 ;
916 data [pakt_size++] = ((value >> 7) & 0x7f) | 0x80 ;
917 data [pakt_size++] = value & 0x7f ;
918 continue ;
919 } ;
920
921 *pakt_size_out = 0 ;
922 free (data) ;
923 return NULL((void*)0) ;
924 } ;
925
926 *pakt_size_out = pakt_size ;
927 return data ;
928} /* alac_pakt_encode */
929
930static sf_count_t
931alac_pakt_block_offset (const PAKT_INFO *info, uint32_t block)
932{ sf_count_t offset = 0 ;
933 uint32_t k ;
934
935 for (k = 0 ; k < block ; k++)
936 offset += info->packet_size [k] ;
937
938 return offset ;
939} /* alac_pakt_block_offset */
940
941static uint32_t
942alac_kuki_read (SF_PRIVATE * psf, uint32_t kuki_offset, uint8_t * kuki, size_t kuki_maxlen)
943{ uint32_t marker ;
944 uint64_t kuki_size ;
945
946 if (psf_fseek (psf, kuki_offset, SEEK_SET0) != kuki_offset)
947 return 0 ;
948
949 psf_fread (&marker, 1, sizeof (marker), psf) ;
950 if (marker != MAKE_MARKER ('k', 'u', 'k', 'i')((uint32_t) (('k') | (('u') << 8) | (('k') << 16)
| (((uint32_t) ('i')) << 24)))
)
951 return 0 ;
952
953 psf_fread (&kuki_size, 1, sizeof (kuki_size), psf) ;
954 kuki_size = BE2H_64 (kuki_size)ENDSWAP_64X (kuki_size) ;
955
956 if (kuki_size == 0 || kuki_size > kuki_maxlen)
957 { psf_log_printf (psf, "%s : Bad size (%D) of 'kuki' chunk.\n", __func__, kuki_size) ;
958 return 0 ;
959 } ;
960
961 psf_fread (kuki, 1, kuki_size, psf) ;
962
963 return kuki_size ;
964} /* alac_kuki_read */