1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/stat.h> /* for stat(), maybe chmod() */
43 #include "private/metadata.h"
45 #include "FLAC/assert.h"
46 #include "FLAC/stream_decoder.h"
47 #include "share/alloc.h"
48 #include "share/compat.h"
49 #include "share/macros.h"
50 #include "share/safe_str.h"
51 #include "private/macros.h"
52 #include "private/memory.h"
54 /* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
55 #define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
57 /****************************************************************************
59 * Local function declarations
61 ***************************************************************************/
63 static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
64 static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
65 static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes);
66 static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes);
67 static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes);
68 static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes);
70 static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator);
71 static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block);
72 static FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length);
73 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block);
74 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block);
75 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length);
76 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length);
77 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length);
78 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry);
79 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block);
80 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track);
81 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block);
82 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block);
83 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length);
85 static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
86 static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
87 static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
88 static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
89 static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block);
90 static FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length);
91 static FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length);
92 static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block);
93 static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block);
94 static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block);
95 static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block);
96 static FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length);
98 static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block);
99 static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last);
100 static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append);
102 static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator);
103 static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator);
105 static unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb);
106 static unsigned seek_to_first_metadata_block_(FILE *f);
108 static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append);
109 static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup);
111 static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
112 static FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
113 static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status);
114 static FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status);
116 static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
117 static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
118 static void cleanup_tempfile_(FILE **tempfile, char **tempfilename);
120 static FLAC__bool get_file_stats_(const char *filename, struct stat *stats);
121 static void set_file_stats_(const char *filename, struct stat *stats);
123 static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence);
124 static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle);
126 static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status);
129 #ifdef FLAC__VALGRIND_TESTING
130 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
132 size_t ret = fwrite(ptr, size, nmemb, stream);
138 #define local__fwrite fwrite
141 /****************************************************************************
143 * Level 0 implementation
145 ***************************************************************************/
147 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
148 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
149 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
152 FLAC__bool got_error;
153 FLAC__StreamMetadata *object;
154 } level0_client_data;
156 static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type)
158 level0_client_data cd;
159 FLAC__StreamDecoder *decoder;
161 FLAC__ASSERT(0 != filename);
163 cd.got_error = false;
166 decoder = FLAC__stream_decoder_new();
171 FLAC__stream_decoder_set_md5_checking(decoder, false);
172 FLAC__stream_decoder_set_metadata_ignore_all(decoder);
173 FLAC__stream_decoder_set_metadata_respond(decoder, type);
175 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) {
176 (void)FLAC__stream_decoder_finish(decoder);
177 FLAC__stream_decoder_delete(decoder);
181 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) {
182 (void)FLAC__stream_decoder_finish(decoder);
183 FLAC__stream_decoder_delete(decoder);
185 FLAC__metadata_object_delete(cd.object);
189 (void)FLAC__stream_decoder_finish(decoder);
190 FLAC__stream_decoder_delete(decoder);
195 FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo)
197 FLAC__StreamMetadata *object;
199 FLAC__ASSERT(0 != filename);
200 FLAC__ASSERT(0 != streaminfo);
202 object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO);
205 /* can just copy the contents since STREAMINFO has no internal structure */
206 *streaminfo = *object;
207 FLAC__metadata_object_delete(object);
215 FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags)
217 FLAC__ASSERT(0 != filename);
218 FLAC__ASSERT(0 != tags);
220 *tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT);
225 FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet)
227 FLAC__ASSERT(0 != filename);
228 FLAC__ASSERT(0 != cuesheet);
230 *cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET);
232 return 0 != *cuesheet;
235 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
237 (void)decoder, (void)frame, (void)buffer, (void)client_data;
239 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
242 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
244 level0_client_data *cd = (level0_client_data *)client_data;
248 * we assume we only get here when the one metadata block we were
249 * looking for was passed to us
251 if(!cd->got_error && 0 == cd->object) {
252 if(0 == (cd->object = FLAC__metadata_object_clone(metadata)))
253 cd->got_error = true;
257 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
259 level0_client_data *cd = (level0_client_data *)client_data;
262 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
263 cd->got_error = true;
266 FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors)
268 FLAC__Metadata_SimpleIterator *it;
269 FLAC__uint64 max_area_seen = 0;
270 FLAC__uint64 max_depth_seen = 0;
272 FLAC__ASSERT(0 != filename);
273 FLAC__ASSERT(0 != picture);
277 it = FLAC__metadata_simple_iterator_new();
280 if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
281 FLAC__metadata_simple_iterator_delete(it);
285 if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) {
286 FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it);
287 FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height;
288 /* check constraints */
290 (type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) &&
291 (mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) &&
292 (description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) &&
293 obj->data.picture.width <= max_width &&
294 obj->data.picture.height <= max_height &&
295 obj->data.picture.depth <= max_depth &&
296 obj->data.picture.colors <= max_colors &&
297 (area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen))
300 FLAC__metadata_object_delete(*picture);
302 max_area_seen = area;
303 max_depth_seen = obj->data.picture.depth;
306 FLAC__metadata_object_delete(obj);
309 } while(FLAC__metadata_simple_iterator_next(it));
311 FLAC__metadata_simple_iterator_delete(it);
313 return (0 != *picture);
317 /****************************************************************************
319 * Level 1 implementation
321 ***************************************************************************/
323 #define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4)
324 /* 1 for initial offset, +4 for our own personal use */
326 struct FLAC__Metadata_SimpleIterator {
328 char *filename, *tempfile_path_prefix;
330 FLAC__bool has_stats;
331 FLAC__bool is_writable;
332 FLAC__Metadata_SimpleIteratorStatus status;
333 FLAC__off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH];
334 FLAC__off_t first_offset; /* this is the offset to the STREAMINFO block */
336 /* this is the metadata block header of the current block we are pointing to: */
338 FLAC__MetadataType type;
342 FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = {
343 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK",
344 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT",
345 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE",
346 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE",
347 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE",
348 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA",
349 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR",
350 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR",
351 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR",
352 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR",
353 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR",
354 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR",
355 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR"
359 FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void)
361 FLAC__Metadata_SimpleIterator *iterator = calloc(1, sizeof(FLAC__Metadata_SimpleIterator));
365 iterator->filename = 0;
366 iterator->tempfile_path_prefix = 0;
367 iterator->has_stats = false;
368 iterator->is_writable = false;
369 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
370 iterator->first_offset = iterator->offset[0] = -1;
377 static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator)
379 FLAC__ASSERT(0 != iterator);
381 if(0 != iterator->file) {
382 fclose(iterator->file);
384 if(iterator->has_stats)
385 set_file_stats_(iterator->filename, &iterator->stats);
387 if(0 != iterator->filename) {
388 free(iterator->filename);
389 iterator->filename = 0;
391 if(0 != iterator->tempfile_path_prefix) {
392 free(iterator->tempfile_path_prefix);
393 iterator->tempfile_path_prefix = 0;
397 FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator)
399 FLAC__ASSERT(0 != iterator);
401 simple_iterator_free_guts_(iterator);
405 FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator)
407 FLAC__Metadata_SimpleIteratorStatus status;
409 FLAC__ASSERT(0 != iterator);
411 status = iterator->status;
412 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
416 static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only)
420 FLAC__ASSERT(0 != iterator);
422 if(read_only || 0 == (iterator->file = fopen(iterator->filename, "r+b"))) {
423 iterator->is_writable = false;
424 if(read_only || errno == EACCES) {
425 if(0 == (iterator->file = fopen(iterator->filename, "rb"))) {
426 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
431 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
436 iterator->is_writable = true;
439 ret = seek_to_first_metadata_block_(iterator->file);
443 iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file);
444 return read_metadata_block_header_(iterator);
446 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
449 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
452 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE;
461 @@@ If we decide to finish implementing this, put this comment back in metadata.h
463 * The 'tempfile_path_prefix' allows you to specify a directory where
464 * tempfiles should go. Remember that if your metadata edits cause the
465 * FLAC file to grow, the entire file will have to be rewritten. If
466 * 'tempfile_path_prefix' is NULL, the temp file will be written in the
467 * same directory as the original FLAC file. This makes replacing the
468 * original with the tempfile fast but requires extra space in the same
469 * partition for the tempfile. If space is a problem, you can pass a
470 * directory name belonging to a different partition in
471 * 'tempfile_path_prefix'. Note that you should use the forward slash
472 * '/' as the directory separator. A trailing slash is not needed; it
473 * will be added automatically.
475 FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix);
478 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats)
480 const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'rename(...)' for what it will take to finish implementing this */
482 FLAC__ASSERT(0 != iterator);
483 FLAC__ASSERT(0 != filename);
485 simple_iterator_free_guts_(iterator);
487 if(!read_only && preserve_file_stats)
488 iterator->has_stats = get_file_stats_(filename, &iterator->stats);
490 if(0 == (iterator->filename = strdup(filename))) {
491 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
494 if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) {
495 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
499 return simple_iterator_prime_input_(iterator, read_only);
502 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator)
504 FLAC__ASSERT(0 != iterator);
505 FLAC__ASSERT(0 != iterator->file);
507 return iterator->is_writable;
510 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator)
512 FLAC__ASSERT(0 != iterator);
513 FLAC__ASSERT(0 != iterator->file);
515 if(iterator->is_last)
518 if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
519 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
523 iterator->offset[iterator->depth] = ftello(iterator->file);
525 return read_metadata_block_header_(iterator);
528 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator)
530 FLAC__off_t this_offset;
532 FLAC__ASSERT(0 != iterator);
533 FLAC__ASSERT(0 != iterator->file);
535 if(iterator->offset[iterator->depth] == iterator->first_offset)
538 if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) {
539 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
542 this_offset = iterator->first_offset;
543 if(!read_metadata_block_header_(iterator))
546 /* we ignore any error from ftello() and catch it in fseeko() */
547 while(ftello(iterator->file) + (FLAC__off_t)iterator->length < iterator->offset[iterator->depth]) {
548 if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
549 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
552 this_offset = ftello(iterator->file);
553 if(!read_metadata_block_header_(iterator))
557 iterator->offset[iterator->depth] = this_offset;
563 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator)
565 FLAC__ASSERT(0 != iterator);
566 FLAC__ASSERT(0 != iterator->file);
568 return iterator->is_last;
572 FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator)
574 FLAC__ASSERT(0 != iterator);
575 FLAC__ASSERT(0 != iterator->file);
577 return iterator->offset[iterator->depth];
580 FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator)
582 FLAC__ASSERT(0 != iterator);
583 FLAC__ASSERT(0 != iterator->file);
585 return iterator->type;
589 FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator)
591 FLAC__ASSERT(0 != iterator);
592 FLAC__ASSERT(0 != iterator->file);
594 return iterator->length;
598 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id)
600 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
602 FLAC__ASSERT(0 != iterator);
603 FLAC__ASSERT(0 != iterator->file);
604 FLAC__ASSERT(0 != id);
606 if(iterator->type != FLAC__METADATA_TYPE_APPLICATION) {
607 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
611 if(fread(id, 1, id_bytes, iterator->file) != id_bytes) {
612 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
617 if(0 != fseeko(iterator->file, -((int)id_bytes), SEEK_CUR)) {
618 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
625 FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator)
627 FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type);
629 FLAC__ASSERT(0 != iterator);
630 FLAC__ASSERT(0 != iterator->file);
633 block->is_last = iterator->is_last;
634 block->length = iterator->length;
636 if(!read_metadata_block_data_(iterator, block)) {
637 FLAC__metadata_object_delete(block);
641 /* back up to the beginning of the block data to stay consistent */
642 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) {
643 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
644 FLAC__metadata_object_delete(block);
649 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
654 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
656 FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
659 FLAC__ASSERT(0 != iterator);
660 FLAC__ASSERT(0 != iterator->file);
661 FLAC__ASSERT(0 != block);
663 if(!iterator->is_writable) {
664 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
668 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) {
669 if(iterator->type != block->type) {
670 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
675 block->is_last = iterator->is_last;
677 if(iterator->length == block->length)
678 return write_metadata_block_stationary_(iterator, block);
679 else if(iterator->length > block->length) {
680 if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) {
681 ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last);
682 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
683 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
687 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
688 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
689 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
693 else /* iterator->length < block->length */ {
694 unsigned padding_leftover = 0;
695 FLAC__bool padding_is_last = false;
697 /* first see if we can even use padding */
698 if(iterator->is_last) {
702 const unsigned extra_padding_bytes_required = block->length - iterator->length;
703 simple_iterator_push_(iterator);
704 if(!FLAC__metadata_simple_iterator_next(iterator)) {
705 (void)simple_iterator_pop_(iterator);
708 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
712 if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) {
713 padding_leftover = 0;
714 block->is_last = iterator->is_last;
716 else if(iterator->length < extra_padding_bytes_required)
719 padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required;
720 padding_is_last = iterator->is_last;
721 block->is_last = false;
724 if(!simple_iterator_pop_(iterator))
729 if(padding_leftover == 0) {
730 ret = write_metadata_block_stationary_(iterator, block);
731 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
732 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
736 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
737 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
738 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
739 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
744 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
745 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
746 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
752 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
754 unsigned padding_leftover = 0;
755 FLAC__bool padding_is_last = false;
757 FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;)
760 FLAC__ASSERT(0 != iterator);
761 FLAC__ASSERT(0 != iterator->file);
762 FLAC__ASSERT(0 != block);
764 if(!iterator->is_writable)
767 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
768 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
772 block->is_last = iterator->is_last;
775 /* first see if we can even use padding */
776 if(iterator->is_last) {
780 simple_iterator_push_(iterator);
781 if(!FLAC__metadata_simple_iterator_next(iterator)) {
782 (void)simple_iterator_pop_(iterator);
785 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
789 if(iterator->length == block->length) {
790 padding_leftover = 0;
791 block->is_last = iterator->is_last;
793 else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length)
796 padding_leftover = iterator->length - block->length;
797 padding_is_last = iterator->is_last;
798 block->is_last = false;
801 if(!simple_iterator_pop_(iterator))
806 /* move to the next block, which is suitable padding */
807 if(!FLAC__metadata_simple_iterator_next(iterator))
809 if(padding_leftover == 0) {
810 ret = write_metadata_block_stationary_(iterator, block);
811 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
812 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
816 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
817 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
818 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
819 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
824 ret = rewrite_whole_file_(iterator, block, /*append=*/true);
825 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
826 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
831 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)
833 FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
836 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
837 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
842 FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
844 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
847 padding->length = iterator->length;
848 if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) {
849 FLAC__metadata_object_delete(padding);
852 FLAC__metadata_object_delete(padding);
853 if(!FLAC__metadata_simple_iterator_prev(iterator))
855 FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
856 FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
860 ret = rewrite_whole_file_(iterator, 0, /*append=*/false);
861 FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
862 FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
869 /****************************************************************************
871 * Level 2 implementation
873 ***************************************************************************/
876 typedef struct FLAC__Metadata_Node {
877 FLAC__StreamMetadata *data;
878 struct FLAC__Metadata_Node *prev, *next;
879 } FLAC__Metadata_Node;
881 struct FLAC__Metadata_Chain {
882 char *filename; /* will be NULL if using callbacks */
884 FLAC__Metadata_Node *head;
885 FLAC__Metadata_Node *tail;
887 FLAC__Metadata_ChainStatus status;
888 FLAC__off_t first_offset, last_offset;
890 * This is the length of the chain initially read from the FLAC file.
891 * it is used to compare against the current length to decide whether
892 * or not the whole file has to be rewritten.
894 FLAC__off_t initial_length;
895 /* @@@ hacky, these are currently only needed by ogg reader */
896 FLAC__IOHandle handle;
897 FLAC__IOCallback_Read read_cb;
900 struct FLAC__Metadata_Iterator {
901 FLAC__Metadata_Chain *chain;
902 FLAC__Metadata_Node *current;
905 FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = {
906 "FLAC__METADATA_CHAIN_STATUS_OK",
907 "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT",
908 "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE",
909 "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE",
910 "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE",
911 "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA",
912 "FLAC__METADATA_CHAIN_STATUS_READ_ERROR",
913 "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR",
914 "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR",
915 "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR",
916 "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR",
917 "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR",
918 "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR",
919 "FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS",
920 "FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH",
921 "FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL"
925 static FLAC__Metadata_Node *node_new_(void)
927 return calloc(1, sizeof(FLAC__Metadata_Node));
930 static void node_delete_(FLAC__Metadata_Node *node)
932 FLAC__ASSERT(0 != node);
934 FLAC__metadata_object_delete(node->data);
938 static void chain_init_(FLAC__Metadata_Chain *chain)
940 FLAC__ASSERT(0 != chain);
943 chain->is_ogg = false;
944 chain->head = chain->tail = 0;
946 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
947 chain->initial_length = 0;
951 static void chain_clear_(FLAC__Metadata_Chain *chain)
953 FLAC__Metadata_Node *node, *next;
955 FLAC__ASSERT(0 != chain);
957 for(node = chain->head; node; ) {
963 if(0 != chain->filename)
964 free(chain->filename);
969 static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
971 FLAC__ASSERT(0 != chain);
972 FLAC__ASSERT(0 != node);
973 FLAC__ASSERT(0 != node->data);
975 node->next = node->prev = 0;
976 node->data->is_last = true;
978 chain->tail->data->is_last = false;
983 FLAC__ASSERT(0 != chain->tail);
984 chain->tail->next = node;
985 node->prev = chain->tail;
991 static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
993 FLAC__ASSERT(0 != chain);
994 FLAC__ASSERT(0 != node);
996 if(node == chain->head)
997 chain->head = node->next;
999 node->prev->next = node->next;
1001 if(node == chain->tail)
1002 chain->tail = node->prev;
1004 node->next->prev = node->prev;
1006 if(0 != chain->tail)
1007 chain->tail->data->is_last = true;
1012 static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
1014 chain_remove_node_(chain, node);
1018 static FLAC__off_t chain_calculate_length_(FLAC__Metadata_Chain *chain)
1020 const FLAC__Metadata_Node *node;
1021 FLAC__off_t length = 0;
1022 for(node = chain->head; node; node = node->next)
1023 length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1027 static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
1029 FLAC__ASSERT(0 != node);
1030 FLAC__ASSERT(0 != node->data);
1031 FLAC__ASSERT(0 != iterator);
1032 FLAC__ASSERT(0 != iterator->current);
1033 FLAC__ASSERT(0 != iterator->chain);
1034 FLAC__ASSERT(0 != iterator->chain->head);
1035 FLAC__ASSERT(0 != iterator->chain->tail);
1037 node->data->is_last = false;
1039 node->prev = iterator->current->prev;
1040 node->next = iterator->current;
1043 iterator->chain->head = node;
1045 node->prev->next = node;
1047 iterator->current->prev = node;
1049 iterator->chain->nodes++;
1052 static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
1054 FLAC__ASSERT(0 != node);
1055 FLAC__ASSERT(0 != node->data);
1056 FLAC__ASSERT(0 != iterator);
1057 FLAC__ASSERT(0 != iterator->current);
1058 FLAC__ASSERT(0 != iterator->chain);
1059 FLAC__ASSERT(0 != iterator->chain->head);
1060 FLAC__ASSERT(0 != iterator->chain->tail);
1062 iterator->current->data->is_last = false;
1064 node->prev = iterator->current;
1065 node->next = iterator->current->next;
1068 iterator->chain->tail = node;
1070 node->next->prev = node;
1072 node->prev->next = node;
1074 iterator->chain->tail->data->is_last = true;
1076 iterator->chain->nodes++;
1079 /* return true iff node and node->next are both padding */
1080 static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
1082 if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) {
1083 const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
1084 node->data->length += growth;
1086 chain_delete_node_(chain, node->next);
1093 /* Returns the new length of the chain, or 0 if there was an error. */
1094 /* WATCHOUT: This can get called multiple times before a write, so
1095 * it should still work when this happens.
1097 /* WATCHOUT: Make sure to also update the logic in
1098 * FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes.
1100 static FLAC__off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
1102 FLAC__off_t current_length = chain_calculate_length_(chain);
1105 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1106 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1107 const FLAC__off_t delta = chain->initial_length - current_length;
1108 chain->tail->data->length += delta;
1109 current_length += delta;
1110 FLAC__ASSERT(current_length == chain->initial_length);
1112 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
1113 else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
1114 FLAC__StreamMetadata *padding;
1115 FLAC__Metadata_Node *node;
1116 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) {
1117 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1120 padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
1121 if(0 == (node = node_new_())) {
1122 FLAC__metadata_object_delete(padding);
1123 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1126 node->data = padding;
1127 chain_append_node_(chain, node);
1128 current_length = chain_calculate_length_(chain);
1129 FLAC__ASSERT(current_length == chain->initial_length);
1131 /* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
1132 else if(current_length > chain->initial_length) {
1133 const FLAC__off_t delta = current_length - chain->initial_length;
1134 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1135 /* if the delta is exactly the size of the last padding block, remove the padding block */
1136 if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
1137 chain_delete_node_(chain, chain->tail);
1138 current_length = chain_calculate_length_(chain);
1139 FLAC__ASSERT(current_length == chain->initial_length);
1141 /* if there is at least 'delta' bytes of padding, trim the padding down */
1142 else if((FLAC__off_t)chain->tail->data->length >= delta) {
1143 chain->tail->data->length -= delta;
1144 current_length -= delta;
1145 FLAC__ASSERT(current_length == chain->initial_length);
1151 return current_length;
1154 static FLAC__bool chain_read_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Tell tell_cb)
1156 FLAC__Metadata_Node *node;
1158 FLAC__ASSERT(0 != chain);
1160 /* we assume we're already at the beginning of the file */
1162 switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) {
1166 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1169 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1172 chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
1180 FLAC__int64 pos = tell_cb(handle);
1182 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1185 chain->first_offset = (FLAC__off_t)pos;
1190 FLAC__MetadataType type;
1196 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1200 if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) {
1202 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1206 node->data = FLAC__metadata_object_new(type);
1207 if(0 == node->data) {
1209 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1213 node->data->is_last = is_last;
1214 node->data->length = length;
1216 chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data));
1217 if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
1221 chain_append_node_(chain, node);
1226 FLAC__int64 pos = tell_cb(handle);
1228 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1231 chain->last_offset = (FLAC__off_t)pos;
1234 chain->initial_length = chain_calculate_length_(chain);
1239 static FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
1241 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
1243 if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) {
1244 *bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle);
1246 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
1248 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
1251 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
1254 static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
1256 (void)decoder, (void)frame, (void)buffer, (void)client_data;
1257 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
1260 static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
1262 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
1263 FLAC__Metadata_Node *node;
1269 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1273 node->data = FLAC__metadata_object_clone(metadata);
1274 if(0 == node->data) {
1276 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1280 chain_append_node_(chain, node);
1283 static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
1285 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
1286 (void)decoder, (void)status;
1287 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1290 static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb)
1292 FLAC__StreamDecoder *decoder;
1294 FLAC__ASSERT(0 != chain);
1296 /* we assume we're already at the beginning of the file */
1298 chain->handle = handle;
1299 chain->read_cb = read_cb;
1300 if(0 == (decoder = FLAC__stream_decoder_new())) {
1301 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1304 FLAC__stream_decoder_set_metadata_respond_all(decoder);
1305 if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
1306 FLAC__stream_decoder_delete(decoder);
1307 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1311 chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
1313 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
1314 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1315 if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
1316 FLAC__stream_decoder_delete(decoder);
1320 FLAC__stream_decoder_delete(decoder);
1322 chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
1324 chain->initial_length = chain_calculate_length_(chain);
1329 static FLAC__bool chain_rewrite_metadata_in_place_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, FLAC__IOCallback_Seek seek_cb)
1331 FLAC__Metadata_Node *node;
1333 FLAC__ASSERT(0 != chain);
1334 FLAC__ASSERT(0 != chain->head);
1336 if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) {
1337 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1341 for(node = chain->head; node; node = node->next) {
1342 if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) {
1343 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1346 if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) {
1347 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1352 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
1354 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1358 static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain)
1363 FLAC__ASSERT(0 != chain->filename);
1365 if(0 == (file = fopen(chain->filename, "r+b"))) {
1366 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1370 /* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */
1371 ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_);
1378 static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
1382 FLAC__Metadata_SimpleIteratorStatus status;
1383 const FLAC__Metadata_Node *node;
1385 FLAC__ASSERT(0 != chain);
1386 FLAC__ASSERT(0 != chain->filename);
1387 FLAC__ASSERT(0 != chain->head);
1389 /* copy the file prefix (data up to first metadata block */
1390 if(0 == (f = fopen(chain->filename, "rb"))) {
1391 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1394 if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) {
1395 chain->status = get_equivalent_status_(status);
1398 if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) {
1399 chain->status = get_equivalent_status_(status);
1403 /* write the metadata */
1404 for(node = chain->head; node; node = node->next) {
1405 if(!write_metadata_block_header_(tempfile, &status, node->data)) {
1406 chain->status = get_equivalent_status_(status);
1409 if(!write_metadata_block_data_(tempfile, &status, node->data)) {
1410 chain->status = get_equivalent_status_(status);
1414 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
1416 /* copy the file postfix (everything after the metadata) */
1417 if(0 != fseeko(f, chain->last_offset, SEEK_SET)) {
1418 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1421 if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) {
1422 chain->status = get_equivalent_status_(status);
1426 /* move the tempfile on top of the original */
1428 if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status))
1435 cleanup_tempfile_(&tempfile, &tempfilename);
1439 /* assumes 'handle' is already at beginning of file */
1440 static FLAC__bool chain_rewrite_file_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb)
1442 FLAC__Metadata_SimpleIteratorStatus status;
1443 const FLAC__Metadata_Node *node;
1445 FLAC__ASSERT(0 != chain);
1446 FLAC__ASSERT(0 == chain->filename);
1447 FLAC__ASSERT(0 != chain->head);
1449 /* copy the file prefix (data up to first metadata block */
1450 if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) {
1451 chain->status = get_equivalent_status_(status);
1455 /* write the metadata */
1456 for(node = chain->head; node; node = node->next) {
1457 if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) {
1458 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1461 if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) {
1462 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1466 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
1468 /* copy the file postfix (everything after the metadata) */
1469 if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) {
1470 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1473 if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) {
1474 chain->status = get_equivalent_status_(status);
1481 FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void)
1483 FLAC__Metadata_Chain *chain = calloc(1, sizeof(FLAC__Metadata_Chain));
1491 FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain)
1493 FLAC__ASSERT(0 != chain);
1495 chain_clear_(chain);
1500 FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain)
1502 FLAC__Metadata_ChainStatus status;
1504 FLAC__ASSERT(0 != chain);
1506 status = chain->status;
1507 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1511 static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg)
1516 FLAC__ASSERT(0 != chain);
1517 FLAC__ASSERT(0 != filename);
1519 chain_clear_(chain);
1521 if(0 == (chain->filename = strdup(filename))) {
1522 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1526 chain->is_ogg = is_ogg;
1528 if(0 == (file = fopen(filename, "rb"))) {
1529 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1533 /* the function also sets chain->status for us */
1535 chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) :
1536 chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_)
1544 FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
1546 return chain_read_(chain, filename, /*is_ogg=*/false);
1549 /*@@@@add to tests*/
1550 FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename)
1552 return chain_read_(chain, filename, /*is_ogg=*/true);
1555 static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg)
1559 FLAC__ASSERT(0 != chain);
1561 chain_clear_(chain);
1563 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) {
1564 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1568 chain->is_ogg = is_ogg;
1571 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1572 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1576 /* the function also sets chain->status for us */
1578 chain_read_ogg_cb_(chain, handle, callbacks.read) :
1579 chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell)
1585 FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1587 return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false);
1590 /*@@@@add to tests*/
1591 FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1593 return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true);
1596 FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
1598 /* This does all the same checks that are in chain_prepare_for_write_()
1599 * but doesn't actually alter the chain. Make sure to update the logic
1600 * here if chain_prepare_for_write_() changes.
1602 const FLAC__off_t current_length = chain_calculate_length_(chain);
1604 FLAC__ASSERT(0 != chain);
1607 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1608 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING)
1610 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
1611 else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length)
1613 /* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
1614 else if(current_length > chain->initial_length) {
1615 const FLAC__off_t delta = current_length - chain->initial_length;
1616 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1617 /* if the delta is exactly the size of the last padding block, remove the padding block */
1618 if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta)
1620 /* if there is at least 'delta' bytes of padding, trim the padding down */
1621 else if((FLAC__off_t)chain->tail->data->length >= delta)
1627 return (current_length != chain->initial_length);
1630 FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats)
1633 const char *tempfile_path_prefix = 0;
1634 FLAC__off_t current_length;
1636 FLAC__ASSERT(0 != chain);
1638 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1639 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1643 if (0 == chain->filename) {
1644 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1648 current_length = chain_prepare_for_write_(chain, use_padding);
1650 /* a return value of 0 means there was an error; chain->status is already set */
1651 if (0 == current_length)
1654 if(preserve_file_stats)
1655 get_file_stats_(chain->filename, &stats);
1657 if(current_length == chain->initial_length) {
1658 if(!chain_rewrite_metadata_in_place_(chain))
1662 if(!chain_rewrite_file_(chain, tempfile_path_prefix))
1665 /* recompute lengths and offsets */
1667 const FLAC__Metadata_Node *node;
1668 chain->initial_length = current_length;
1669 chain->last_offset = chain->first_offset;
1670 for(node = chain->head; node; node = node->next)
1671 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1675 if(preserve_file_stats)
1676 set_file_stats_(chain->filename, &stats);
1681 FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1683 FLAC__off_t current_length;
1685 FLAC__ASSERT(0 != chain);
1687 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1688 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1692 if (0 != chain->filename) {
1693 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1697 if (0 == callbacks.write || 0 == callbacks.seek) {
1698 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1702 if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1703 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1707 current_length = chain_prepare_for_write_(chain, use_padding);
1709 /* a return value of 0 means there was an error; chain->status is already set */
1710 if (0 == current_length)
1713 FLAC__ASSERT(current_length == chain->initial_length);
1715 return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek);
1718 FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks)
1720 FLAC__off_t current_length;
1722 FLAC__ASSERT(0 != chain);
1724 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1725 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1729 if (0 != chain->filename) {
1730 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1734 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) {
1735 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1738 if (0 == temp_callbacks.write) {
1739 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1743 if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1744 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1748 current_length = chain_prepare_for_write_(chain, use_padding);
1750 /* a return value of 0 means there was an error; chain->status is already set */
1751 if (0 == current_length)
1754 FLAC__ASSERT(current_length != chain->initial_length);
1757 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1758 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1762 if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write))
1765 /* recompute lengths and offsets */
1767 const FLAC__Metadata_Node *node;
1768 chain->initial_length = current_length;
1769 chain->last_offset = chain->first_offset;
1770 for(node = chain->head; node; node = node->next)
1771 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1777 FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain)
1779 FLAC__Metadata_Node *node;
1781 FLAC__ASSERT(0 != chain);
1783 for(node = chain->head; node; ) {
1784 if(!chain_merge_adjacent_padding_(chain, node))
1789 FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
1791 FLAC__Metadata_Node *node, *save;
1794 FLAC__ASSERT(0 != chain);
1797 * Don't try and be too smart... this simple algo is good enough for
1798 * the small number of nodes that we deal with.
1800 for(i = 0, node = chain->head; i < chain->nodes; i++) {
1801 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1803 chain_remove_node_(chain, node);
1804 chain_append_node_(chain, node);
1812 FLAC__metadata_chain_merge_padding(chain);
1816 FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void)
1818 FLAC__Metadata_Iterator *iterator = calloc(1, sizeof(FLAC__Metadata_Iterator));
1820 /* calloc() implies:
1821 iterator->current = 0;
1822 iterator->chain = 0;
1828 FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator)
1830 FLAC__ASSERT(0 != iterator);
1835 FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain)
1837 FLAC__ASSERT(0 != iterator);
1838 FLAC__ASSERT(0 != chain);
1839 FLAC__ASSERT(0 != chain->head);
1841 iterator->chain = chain;
1842 iterator->current = chain->head;
1845 FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator)
1847 FLAC__ASSERT(0 != iterator);
1849 if(0 == iterator->current || 0 == iterator->current->next)
1852 iterator->current = iterator->current->next;
1856 FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator)
1858 FLAC__ASSERT(0 != iterator);
1860 if(0 == iterator->current || 0 == iterator->current->prev)
1863 iterator->current = iterator->current->prev;
1867 FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator)
1869 FLAC__ASSERT(0 != iterator);
1870 FLAC__ASSERT(0 != iterator->current);
1871 FLAC__ASSERT(0 != iterator->current->data);
1873 return iterator->current->data->type;
1876 FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator)
1878 FLAC__ASSERT(0 != iterator);
1879 FLAC__ASSERT(0 != iterator->current);
1881 return iterator->current->data;
1884 FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1886 FLAC__ASSERT(0 != iterator);
1887 FLAC__ASSERT(0 != block);
1888 return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block);
1891 FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding)
1893 FLAC__Metadata_Node *save;
1895 FLAC__ASSERT(0 != iterator);
1896 FLAC__ASSERT(0 != iterator->current);
1898 if(0 == iterator->current->prev) {
1899 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1903 save = iterator->current->prev;
1905 if(replace_with_padding) {
1906 FLAC__metadata_object_delete_data(iterator->current->data);
1907 iterator->current->data->type = FLAC__METADATA_TYPE_PADDING;
1910 chain_delete_node_(iterator->chain, iterator->current);
1913 iterator->current = save;
1917 FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1919 FLAC__Metadata_Node *node;
1921 FLAC__ASSERT(0 != iterator);
1922 FLAC__ASSERT(0 != iterator->current);
1923 FLAC__ASSERT(0 != block);
1925 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1928 if(0 == iterator->current->prev) {
1929 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1933 if(0 == (node = node_new_()))
1937 iterator_insert_node_(iterator, node);
1938 iterator->current = node;
1942 FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1944 FLAC__Metadata_Node *node;
1946 FLAC__ASSERT(0 != iterator);
1947 FLAC__ASSERT(0 != iterator->current);
1948 FLAC__ASSERT(0 != block);
1950 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1953 if(0 == (node = node_new_()))
1957 iterator_insert_node_after_(iterator, node);
1958 iterator->current = node;
1963 /****************************************************************************
1965 * Local function definitions
1967 ***************************************************************************/
1969 void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1975 for(i = 0; i < bytes; i++) {
1976 *(--b) = (FLAC__byte)(val & 0xff);
1981 void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1985 for(i = 0; i < bytes; i++) {
1986 *(b++) = (FLAC__byte)(val & 0xff);
1991 void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes)
1997 for(i = 0; i < bytes; i++) {
1998 *(--b) = (FLAC__byte)(val & 0xff);
2003 FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes)
2005 FLAC__uint32 ret = 0;
2008 for(i = 0; i < bytes; i++)
2009 ret = (ret << 8) | (FLAC__uint32)(*b++);
2014 FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes)
2016 FLAC__uint32 ret = 0;
2021 for(i = 0; i < bytes; i++)
2022 ret = (ret << 8) | (FLAC__uint32)(*--b);
2027 FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes)
2029 FLAC__uint64 ret = 0;
2032 for(i = 0; i < bytes; i++)
2033 ret = (ret << 8) | (FLAC__uint64)(*b++);
2038 FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator)
2040 FLAC__ASSERT(0 != iterator);
2041 FLAC__ASSERT(0 != iterator->file);
2043 if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) {
2044 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2051 FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block)
2053 FLAC__ASSERT(0 != iterator);
2054 FLAC__ASSERT(0 != iterator->file);
2056 iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block);
2058 return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
2061 FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length)
2063 FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH];
2065 if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
2068 *is_last = raw_header[0] & 0x80? true : false;
2069 *type = (FLAC__MetadataType)(raw_header[0] & 0x7f);
2070 *length = unpack_uint32_(raw_header + 1, 3);
2072 /* Note that we don't check:
2073 * if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED)
2074 * we just will read in an opaque block
2080 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block)
2082 switch(block->type) {
2083 case FLAC__METADATA_TYPE_STREAMINFO:
2084 return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info);
2085 case FLAC__METADATA_TYPE_PADDING:
2086 return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length);
2087 case FLAC__METADATA_TYPE_APPLICATION:
2088 return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length);
2089 case FLAC__METADATA_TYPE_SEEKTABLE:
2090 return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length);
2091 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
2092 return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, &block->data.vorbis_comment);
2093 case FLAC__METADATA_TYPE_CUESHEET:
2094 return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet);
2095 case FLAC__METADATA_TYPE_PICTURE:
2096 return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture);
2098 return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length);
2102 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block)
2104 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b;
2106 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
2107 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2111 /* we are using hardcoded numbers for simplicity but we should
2112 * probably eventually write a bit-level unpacker and use the
2113 * _STREAMINFO_ constants.
2115 block->min_blocksize = unpack_uint32_(b, 2); b += 2;
2116 block->max_blocksize = unpack_uint32_(b, 2); b += 2;
2117 block->min_framesize = unpack_uint32_(b, 3); b += 3;
2118 block->max_framesize = unpack_uint32_(b, 3); b += 3;
2119 block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
2120 block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
2121 block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
2122 block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4);
2123 memcpy(block->md5sum, b+8, 16);
2125 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2128 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length)
2130 (void)block; /* nothing to do; we don't care about reading the padding bytes */
2132 if(0 != seek_cb(handle, block_length, SEEK_CUR))
2133 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2135 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2138 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length)
2140 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
2142 if(read_cb(block->id, 1, id_bytes, handle) != id_bytes)
2143 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2145 if(block_length < id_bytes)
2146 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2148 block_length -= id_bytes;
2150 if(block_length == 0) {
2154 if(0 == (block->data = malloc(block_length)))
2155 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2157 if(read_cb(block->data, 1, block_length, handle) != block_length)
2158 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2161 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2164 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length)
2167 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
2169 FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0);
2171 block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
2173 if(block->num_points == 0)
2175 else if(0 == (block->points = safe_malloc_mul_2op_p(block->num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint))))
2176 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2178 for(i = 0; i < block->num_points; i++) {
2179 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
2180 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2181 /* some MAGIC NUMBERs here */
2182 block->points[i].sample_number = unpack_uint64_(buffer, 8);
2183 block->points[i].stream_offset = unpack_uint64_(buffer+8, 8);
2184 block->points[i].frame_samples = unpack_uint32_(buffer+16, 2);
2187 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2190 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry)
2192 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
2193 FLAC__byte buffer[4]; /* magic number is asserted below */
2195 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer));
2197 if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2198 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2199 entry->length = unpack_uint32_little_endian_(buffer, entry_length_len);
2201 if(0 != entry->entry)
2204 if(entry->length == 0) {
2208 if(0 == (entry->entry = safe_malloc_add_2op_(entry->length, /*+*/1)))
2209 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2211 if(read_cb(entry->entry, 1, entry->length, handle) != entry->length)
2212 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2214 entry->entry[entry->length] = '\0';
2217 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2220 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block)
2223 FLAC__Metadata_SimpleIteratorStatus status;
2224 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
2225 FLAC__byte buffer[4]; /* magic number is asserted below */
2227 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer));
2229 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string))))
2232 if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
2233 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2234 block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len);
2236 if(block->num_comments == 0) {
2237 block->comments = 0;
2239 else if(0 == (block->comments = calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry))))
2240 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2242 for(i = 0; i < block->num_comments; i++) {
2243 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i)))
2247 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2250 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track)
2253 FLAC__byte buffer[32]; /* asserted below that this is big enough */
2255 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
2256 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2257 FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8);
2259 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2260 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
2261 if(read_cb(buffer, 1, len, handle) != len)
2262 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2263 track->offset = unpack_uint64_(buffer, len);
2265 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2266 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
2267 if(read_cb(buffer, 1, len, handle) != len)
2268 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2269 track->number = (FLAC__byte)unpack_uint32_(buffer, len);
2271 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2272 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
2273 if(read_cb(track->isrc, 1, len, handle) != len)
2274 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2276 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0);
2277 len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
2278 if(read_cb(buffer, 1, len, handle) != len)
2279 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2280 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1);
2281 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1);
2282 track->type = buffer[0] >> 7;
2283 track->pre_emphasis = (buffer[0] >> 6) & 1;
2285 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2286 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
2287 if(read_cb(buffer, 1, len, handle) != len)
2288 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2289 track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len);
2291 if(track->num_indices == 0) {
2294 else if(0 == (track->indices = calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
2295 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2297 for(i = 0; i < track->num_indices; i++) {
2298 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2299 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
2300 if(read_cb(buffer, 1, len, handle) != len)
2301 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2302 track->indices[i].offset = unpack_uint64_(buffer, len);
2304 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2305 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
2306 if(read_cb(buffer, 1, len, handle) != len)
2307 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2308 track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len);
2310 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2311 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
2312 if(read_cb(buffer, 1, len, handle) != len)
2313 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2316 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2319 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block)
2322 FLAC__Metadata_SimpleIteratorStatus status;
2323 FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */
2325 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer));
2326 FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer));
2328 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2329 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
2330 if(read_cb(block->media_catalog_number, 1, len, handle) != len)
2331 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2333 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2334 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
2335 if(read_cb(buffer, 1, len, handle) != len)
2336 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2337 block->lead_in = unpack_uint64_(buffer, len);
2339 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2340 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
2341 if(read_cb(buffer, 1, len, handle) != len)
2342 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2343 block->is_cd = buffer[0]&0x80? true : false;
2345 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2346 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
2347 if(read_cb(buffer, 1, len, handle) != len)
2348 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2349 block->num_tracks = unpack_uint32_(buffer, len);
2351 if(block->num_tracks == 0) {
2354 else if(0 == (block->tracks = calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
2355 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2357 for(i = 0; i < block->num_tracks; i++) {
2358 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i)))
2362 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2365 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cstring_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__byte **data, FLAC__uint32 *length, FLAC__uint32 length_len)
2367 FLAC__byte buffer[sizeof(FLAC__uint32)];
2369 FLAC__ASSERT(0 != data);
2370 FLAC__ASSERT(length_len%8 == 0);
2372 length_len /= 8; /* convert to bytes */
2374 FLAC__ASSERT(sizeof(buffer) >= length_len);
2376 if(read_cb(buffer, 1, length_len, handle) != length_len)
2377 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2378 *length = unpack_uint32_(buffer, length_len);
2383 if(0 == (*data = safe_malloc_add_2op_(*length, /*+*/1)))
2384 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2387 if(read_cb(*data, 1, *length, handle) != *length)
2388 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2391 (*data)[*length] = '\0';
2393 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2396 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block)
2398 FLAC__Metadata_SimpleIteratorStatus status;
2399 FLAC__byte buffer[4]; /* asserted below that this is big enough */
2402 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2403 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2404 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2405 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
2406 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
2408 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0);
2409 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8;
2410 if(read_cb(buffer, 1, len, handle) != len)
2411 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2412 block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len);
2414 if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, (FLAC__byte**)(&(block->mime_type)), &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
2417 if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->description), &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
2420 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0);
2421 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8;
2422 if(read_cb(buffer, 1, len, handle) != len)
2423 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2424 block->width = unpack_uint32_(buffer, len);
2426 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0);
2427 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8;
2428 if(read_cb(buffer, 1, len, handle) != len)
2429 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2430 block->height = unpack_uint32_(buffer, len);
2432 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0);
2433 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8;
2434 if(read_cb(buffer, 1, len, handle) != len)
2435 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2436 block->depth = unpack_uint32_(buffer, len);
2438 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0);
2439 len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8;
2440 if(read_cb(buffer, 1, len, handle) != len)
2441 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2442 block->colors = unpack_uint32_(buffer, len);
2444 /* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */
2445 if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->data), &(block->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
2448 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2451 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length)
2453 if(block_length == 0) {
2457 if(0 == (block->data = malloc(block_length)))
2458 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2460 if(read_cb(block->data, 1, block_length, handle) != block_length)
2461 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2464 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2467 FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
2469 FLAC__ASSERT(0 != file);
2470 FLAC__ASSERT(0 != status);
2472 if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
2473 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2480 FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
2482 FLAC__ASSERT(0 != file);
2483 FLAC__ASSERT(0 != status);
2485 if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
2486 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2490 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2495 FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2497 FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];
2499 FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
2501 buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
2502 pack_uint32_(block->length, buffer + 1, 3);
2504 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
2510 FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2512 FLAC__ASSERT(0 != block);
2514 switch(block->type) {
2515 case FLAC__METADATA_TYPE_STREAMINFO:
2516 return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info);
2517 case FLAC__METADATA_TYPE_PADDING:
2518 return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length);
2519 case FLAC__METADATA_TYPE_APPLICATION:
2520 return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length);
2521 case FLAC__METADATA_TYPE_SEEKTABLE:
2522 return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table);
2523 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
2524 return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment);
2525 case FLAC__METADATA_TYPE_CUESHEET:
2526 return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet);
2527 case FLAC__METADATA_TYPE_PICTURE:
2528 return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture);
2530 return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length);
2534 FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block)
2536 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
2537 const unsigned channels1 = block->channels - 1;
2538 const unsigned bps1 = block->bits_per_sample - 1;
2540 /* we are using hardcoded numbers for simplicity but we should
2541 * probably eventually write a bit-level packer and use the
2542 * _STREAMINFO_ constants.
2544 pack_uint32_(block->min_blocksize, buffer, 2);
2545 pack_uint32_(block->max_blocksize, buffer+2, 2);
2546 pack_uint32_(block->min_framesize, buffer+4, 3);
2547 pack_uint32_(block->max_framesize, buffer+7, 3);
2548 buffer[10] = (block->sample_rate >> 12) & 0xff;
2549 buffer[11] = (block->sample_rate >> 4) & 0xff;
2550 buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4);
2551 buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f));
2552 pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
2553 memcpy(buffer+18, block->md5sum, 16);
2555 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
2561 FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length)
2563 unsigned i, n = block_length;
2564 FLAC__byte buffer[1024];
2568 memset(buffer, 0, 1024);
2570 for(i = 0; i < n/1024; i++)
2571 if(write_cb(buffer, 1, 1024, handle) != 1024)
2576 if(write_cb(buffer, 1, n, handle) != n)
2582 FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length)
2584 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
2586 if(write_cb(block->id, 1, id_bytes, handle) != id_bytes)
2589 block_length -= id_bytes;
2591 if(write_cb(block->data, 1, block_length, handle) != block_length)
2597 FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block)
2600 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
2602 for(i = 0; i < block->num_points; i++) {
2603 /* some MAGIC NUMBERs here */
2604 pack_uint64_(block->points[i].sample_number, buffer, 8);
2605 pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
2606 pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
2607 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
2614 FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block)
2617 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
2618 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
2619 FLAC__byte buffer[4]; /* magic number is asserted below */
2621 FLAC__ASSERT(flac_max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer));
2623 pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
2624 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2626 if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length)
2629 pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
2630 if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
2633 for(i = 0; i < block->num_comments; i++) {
2634 pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
2635 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2637 if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length)
2644 FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block)
2647 FLAC__byte buffer[1024]; /* asserted below that this is big enough */
2649 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
2650 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8);
2651 FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)/8);
2652 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2654 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2655 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
2656 if(write_cb(block->media_catalog_number, 1, len, handle) != len)
2659 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2660 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
2661 pack_uint64_(block->lead_in, buffer, len);
2662 if(write_cb(buffer, 1, len, handle) != len)
2665 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2666 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
2667 memset(buffer, 0, len);
2670 if(write_cb(buffer, 1, len, handle) != len)
2673 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2674 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
2675 pack_uint32_(block->num_tracks, buffer, len);
2676 if(write_cb(buffer, 1, len, handle) != len)
2679 for(i = 0; i < block->num_tracks; i++) {
2680 FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i;
2682 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2683 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
2684 pack_uint64_(track->offset, buffer, len);
2685 if(write_cb(buffer, 1, len, handle) != len)
2688 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2689 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
2690 pack_uint32_(track->number, buffer, len);
2691 if(write_cb(buffer, 1, len, handle) != len)
2694 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2695 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
2696 if(write_cb(track->isrc, 1, len, handle) != len)
2699 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0);
2700 len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
2701 memset(buffer, 0, len);
2702 buffer[0] = (track->type << 7) | (track->pre_emphasis << 6);
2703 if(write_cb(buffer, 1, len, handle) != len)
2706 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2707 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
2708 pack_uint32_(track->num_indices, buffer, len);
2709 if(write_cb(buffer, 1, len, handle) != len)
2712 for(j = 0; j < track->num_indices; j++) {
2713 FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;
2715 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2716 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
2717 pack_uint64_(index->offset, buffer, len);
2718 if(write_cb(buffer, 1, len, handle) != len)
2721 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2722 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
2723 pack_uint32_(index->number, buffer, len);
2724 if(write_cb(buffer, 1, len, handle) != len)
2727 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2728 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
2729 memset(buffer, 0, len);
2730 if(write_cb(buffer, 1, len, handle) != len)
2738 FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block)
2742 FLAC__byte buffer[4]; /* magic number is asserted below */
2744 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8);
2745 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8);
2746 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8);
2747 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8);
2748 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8);
2749 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8);
2750 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8);
2751 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8);
2752 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2753 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8);
2754 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8);
2755 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2756 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2757 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
2758 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
2759 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8);
2761 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8;
2762 pack_uint32_(block->type, buffer, len);
2763 if(write_cb(buffer, 1, len, handle) != len)
2766 len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8;
2767 slen = strlen(block->mime_type);
2768 pack_uint32_(slen, buffer, len);
2769 if(write_cb(buffer, 1, len, handle) != len)
2771 if(write_cb(block->mime_type, 1, slen, handle) != slen)
2774 len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8;
2775 slen = strlen((const char *)block->description);
2776 pack_uint32_(slen, buffer, len);
2777 if(write_cb(buffer, 1, len, handle) != len)
2779 if(write_cb(block->description, 1, slen, handle) != slen)
2782 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8;
2783 pack_uint32_(block->width, buffer, len);
2784 if(write_cb(buffer, 1, len, handle) != len)
2787 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8;
2788 pack_uint32_(block->height, buffer, len);
2789 if(write_cb(buffer, 1, len, handle) != len)
2792 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8;
2793 pack_uint32_(block->depth, buffer, len);
2794 if(write_cb(buffer, 1, len, handle) != len)
2797 len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8;
2798 pack_uint32_(block->colors, buffer, len);
2799 if(write_cb(buffer, 1, len, handle) != len)
2802 len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8;
2803 pack_uint32_(block->data_length, buffer, len);
2804 if(write_cb(buffer, 1, len, handle) != len)
2806 if(write_cb(block->data, 1, block->data_length, handle) != block->data_length)
2812 FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length)
2814 if(write_cb(block->data, 1, block_length, handle) != block_length)
2820 FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block)
2822 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2823 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2827 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2830 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2833 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2834 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2838 return read_metadata_block_header_(iterator);
2841 FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last)
2843 FLAC__StreamMetadata *padding;
2845 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2846 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2850 block->is_last = false;
2852 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2855 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2858 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
2859 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2861 padding->is_last = padding_is_last;
2862 padding->length = padding_length;
2864 if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) {
2865 FLAC__metadata_object_delete(padding);
2869 if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) {
2870 FLAC__metadata_object_delete(padding);
2874 FLAC__metadata_object_delete(padding);
2876 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2877 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2881 return read_metadata_block_header_(iterator);
2884 FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append)
2886 FILE *tempfile = NULL;
2887 char *tempfilename = NULL;
2888 int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */
2889 FLAC__off_t fixup_is_last_flag_offset = -1;
2891 FLAC__ASSERT(0 != block || append == false);
2893 if(iterator->is_last) {
2895 fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */
2896 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
2898 else if(0 == block) {
2899 simple_iterator_push_(iterator);
2900 if(!FLAC__metadata_simple_iterator_prev(iterator)) {
2901 (void)simple_iterator_pop_(iterator);
2904 fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */
2905 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
2906 if(!simple_iterator_pop_(iterator))
2911 if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append))
2915 if(!write_metadata_block_header_(tempfile, &iterator->status, block)) {
2916 cleanup_tempfile_(&tempfile, &tempfilename);
2920 if(!write_metadata_block_data_(tempfile, &iterator->status, block)) {
2921 cleanup_tempfile_(&tempfile, &tempfilename);
2926 if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0))
2930 return FLAC__metadata_simple_iterator_next(iterator);
2935 void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator)
2937 FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH);
2938 iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth];
2942 FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator)
2944 FLAC__ASSERT(iterator->depth > 0);
2946 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2947 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2951 return read_metadata_block_header_(iterator);
2958 * 3: not a FLAC file
2960 unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb)
2962 FLAC__byte buffer[4];
2966 FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer));
2968 /* skip any id3v2 tag */
2970 n = read_cb(buffer, 1, 4, handle);
2975 else if(0 == memcmp(buffer, "ID3", 3)) {
2976 unsigned tag_length = 0;
2978 /* skip to the tag length */
2979 if(seek_cb(handle, 2, SEEK_CUR) < 0)
2982 /* read the length */
2983 for(i = 0; i < 4; i++) {
2984 if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80)
2987 tag_length |= (buffer[0] & 0x7f);
2990 /* skip the rest of the tag */
2991 if(seek_cb(handle, tag_length, SEEK_CUR) < 0)
2994 /* read the stream sync code */
2996 n = read_cb(buffer, 1, 4, handle);
3003 /* check for the fLaC signature */
3004 if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH))
3010 unsigned seek_to_first_metadata_block_(FILE *f)
3012 return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_);
3015 FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append)
3017 const FLAC__off_t offset_end = append? iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length : iterator->offset[iterator->depth];
3019 if(0 != fseeko(iterator->file, 0, SEEK_SET)) {
3020 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3023 if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) {
3024 cleanup_tempfile_(tempfile, tempfilename);
3027 if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) {
3028 cleanup_tempfile_(tempfile, tempfilename);
3035 FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup)
3037 FLAC__off_t save_offset = iterator->offset[iterator->depth];
3038 FLAC__ASSERT(0 != *tempfile);
3040 if(0 != fseeko(iterator->file, save_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length, SEEK_SET)) {
3041 cleanup_tempfile_(tempfile, tempfilename);
3042 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3045 if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) {
3046 cleanup_tempfile_(tempfile, tempfilename);
3050 if(fixup_is_last_code != 0) {
3052 * if code == 1, it means a block was appended to the end so
3053 * we have to clear the is_last flag of the previous block
3054 * if code == -1, it means the last block was deleted so
3055 * we have to set the is_last flag of the previous block
3057 /* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */
3059 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
3060 cleanup_tempfile_(tempfile, tempfilename);
3061 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3064 if(fread(&x, 1, 1, *tempfile) != 1) {
3065 cleanup_tempfile_(tempfile, tempfilename);
3066 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3069 if(fixup_is_last_code > 0) {
3070 FLAC__ASSERT(x & 0x80);
3074 FLAC__ASSERT(!(x & 0x80));
3077 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
3078 cleanup_tempfile_(tempfile, tempfilename);
3079 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3082 if(local__fwrite(&x, 1, 1, *tempfile) != 1) {
3083 cleanup_tempfile_(tempfile, tempfilename);
3084 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3089 (void)fclose(iterator->file);
3091 if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status))
3094 if(iterator->has_stats)
3095 set_file_stats_(iterator->filename, &iterator->stats);
3097 if(!simple_iterator_prime_input_(iterator, !iterator->is_writable))
3100 while(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length < save_offset)
3101 if(!FLAC__metadata_simple_iterator_next(iterator))
3106 /* move the iterator to it's original block faster by faking a push, then doing a pop_ */
3107 FLAC__ASSERT(iterator->depth == 0);
3108 iterator->offset[0] = save_offset;
3110 return simple_iterator_pop_(iterator);
3114 FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
3116 FLAC__byte buffer[8192];
3119 FLAC__ASSERT(bytes >= 0);
3121 n = flac_min(sizeof(buffer), (size_t)bytes);
3122 if(fread(buffer, 1, n, file) != n) {
3123 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3126 if(local__fwrite(buffer, 1, n, tempfile) != n) {
3127 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3136 FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
3138 FLAC__byte buffer[8192];
3141 FLAC__ASSERT(bytes >= 0);
3143 n = flac_min(sizeof(buffer), (size_t)bytes);
3144 if(read_cb(buffer, 1, n, handle) != n) {
3145 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3148 if(temp_write_cb(buffer, 1, n, temp_handle) != n) {
3149 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3158 FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status)
3160 FLAC__byte buffer[8192];
3163 while(!feof(file)) {
3164 n = fread(buffer, 1, sizeof(buffer), file);
3165 if(n == 0 && !feof(file)) {
3166 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3169 if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) {
3170 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3178 FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status)
3180 FLAC__byte buffer[8192];
3183 while(!eof_cb(handle)) {
3184 n = read_cb(buffer, 1, sizeof(buffer), handle);
3185 if(n == 0 && !eof_cb(handle)) {
3186 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3189 if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) {
3190 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3198 FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
3200 static const char *tempfile_suffix = ".metadata_edit";
3201 if(0 == tempfile_path_prefix) {
3202 size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1;
3203 if(0 == (*tempfilename = safe_malloc_(dest_len))) {
3204 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
3207 safe_strncpy(*tempfilename, filename, dest_len);
3208 safe_strncat(*tempfilename, tempfile_suffix, dest_len);
3211 const char *p = strrchr(filename, '/');
3218 dest_len = strlen(tempfile_path_prefix) + strlen(p) + strlen(tempfile_suffix) + 2;
3220 if(0 == (*tempfilename = safe_malloc_(dest_len))) {
3221 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
3224 safe_strncpy(*tempfilename, tempfile_path_prefix, dest_len);
3225 safe_strncat(*tempfilename, "/", dest_len);
3226 safe_strncat(*tempfilename, p, dest_len);
3227 safe_strncat(*tempfilename, tempfile_suffix, dest_len);
3230 if(0 == (*tempfile = fopen(*tempfilename, "w+b"))) {
3231 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
3238 FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
3240 FLAC__ASSERT(0 != filename);
3241 FLAC__ASSERT(0 != tempfile);
3242 FLAC__ASSERT(0 != *tempfile);
3243 FLAC__ASSERT(0 != tempfilename);
3244 FLAC__ASSERT(0 != *tempfilename);
3245 FLAC__ASSERT(0 != status);
3247 (void)fclose(*tempfile);
3250 #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__
3251 /* on some flavors of windows, rename() will fail if the destination already exists */
3252 if(unlink(filename) < 0) {
3253 cleanup_tempfile_(tempfile, tempfilename);
3254 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR;
3259 /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just rename(): */
3260 if(0 != rename(*tempfilename, filename)) {
3261 cleanup_tempfile_(tempfile, tempfilename);
3262 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR;
3266 cleanup_tempfile_(tempfile, tempfilename);
3271 void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
3273 if(0 != *tempfile) {
3274 (void)fclose(*tempfile);
3278 if(0 != *tempfilename) {
3279 (void)unlink(*tempfilename);
3280 free(*tempfilename);
3285 FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
3287 FLAC__ASSERT(0 != filename);
3288 FLAC__ASSERT(0 != stats);
3289 return (0 == stat(filename, stats));
3292 void set_file_stats_(const char *filename, struct stat *stats)
3294 struct utimbuf srctime;
3296 FLAC__ASSERT(0 != filename);
3297 FLAC__ASSERT(0 != stats);
3299 srctime.actime = stats->st_atime;
3300 srctime.modtime = stats->st_mtime;
3301 (void)chmod(filename, stats->st_mode);
3302 (void)utime(filename, &srctime);
3303 #if !defined _MSC_VER && !defined __BORLANDC__ && !defined __MINGW32__
3304 FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1));
3305 FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid));
3309 int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
3311 return fseeko((FILE*)handle, (FLAC__off_t)offset, whence);
3314 FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle)
3316 return ftello((FILE*)handle);
3319 FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status)
3322 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK:
3323 return FLAC__METADATA_CHAIN_STATUS_OK;
3324 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
3325 return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT;
3326 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
3327 return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
3328 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
3329 return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
3330 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE:
3331 return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE;
3332 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA:
3333 return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
3334 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR:
3335 return FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
3336 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR:
3337 return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
3338 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR:
3339 return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
3340 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR:
3341 return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR;
3342 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR:
3343 return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR;
3344 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR:
3345 return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
3346 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR:
3348 return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;