1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002,2003,2004,2005,2006 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 #if defined _MSC_VER || defined __MINGW32__
42 #include <sys/utime.h> /* for utime() */
43 #include <io.h> /* for chmod() */
44 #include <sys/types.h> /* for off_t */
45 //@@@ [2G limit] hacks for MSVC6
49 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
50 #include <utime.h> /* for utime() */
51 #include <unistd.h> /* for chown(), unlink() */
53 #include <sys/stat.h> /* for stat(), maybe chmod() */
55 #include "private/metadata.h"
57 #include "FLAC/assert.h"
58 #include "FLAC/stream_decoder.h"
63 #define max(a,b) ((a)>(b)?(a):(b))
67 #define min(a,b) ((a)<(b)?(a):(b))
70 /****************************************************************************
72 * Local function declarations
74 ***************************************************************************/
76 static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
77 static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
78 static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes);
79 static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes);
80 static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes);
81 static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes);
83 static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator);
84 static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block);
85 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);
86 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);
87 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block);
88 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);
89 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);
90 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);
91 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);
92 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block);
93 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track);
94 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block);
95 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block);
96 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);
98 static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
99 static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
100 static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
101 static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
102 static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block);
103 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);
104 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);
105 static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block);
106 static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block);
107 static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block);
108 static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block);
109 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);
111 static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block);
112 static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last);
113 static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append);
115 static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator);
116 static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator);
118 static unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb);
119 static unsigned seek_to_first_metadata_block_(FILE *f);
121 static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append);
122 static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, off_t fixup_is_last_flag_offset, FLAC__bool backup);
124 static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
125 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, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
126 static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status);
127 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);
129 static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
130 static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
131 static void cleanup_tempfile_(FILE **tempfile, char **tempfilename);
133 static FLAC__bool get_file_stats_(const char *filename, struct stat *stats);
134 static void set_file_stats_(const char *filename, struct stat *stats);
136 static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence);
137 static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle);
139 static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status);
142 #ifdef FLAC__VALGRIND_TESTING
143 static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
145 size_t ret = fwrite(ptr, size, nmemb, stream);
151 #define local__fwrite fwrite
154 /****************************************************************************
156 * Level 0 implementation
158 ***************************************************************************/
160 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
161 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
162 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
165 FLAC__bool got_error;
166 FLAC__StreamMetadata *object;
167 } level0_client_data;
169 static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type)
171 level0_client_data cd;
172 FLAC__StreamDecoder *decoder;
174 FLAC__ASSERT(0 != filename);
176 cd.got_error = false;
179 decoder = FLAC__stream_decoder_new();
184 FLAC__stream_decoder_set_md5_checking(decoder, false);
185 FLAC__stream_decoder_set_metadata_ignore_all(decoder);
186 FLAC__stream_decoder_set_metadata_respond(decoder, type);
188 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) {
189 FLAC__stream_decoder_finish(decoder);
190 FLAC__stream_decoder_delete(decoder);
194 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) {
195 FLAC__stream_decoder_finish(decoder);
196 FLAC__stream_decoder_delete(decoder);
198 FLAC__metadata_object_delete(cd.object);
202 FLAC__stream_decoder_finish(decoder);
203 FLAC__stream_decoder_delete(decoder);
208 FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo)
210 FLAC__StreamMetadata *object;
212 FLAC__ASSERT(0 != filename);
213 FLAC__ASSERT(0 != streaminfo);
215 object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO);
218 /* can just copy the contents since STREAMINFO has no internal structure */
219 *streaminfo = *object;
220 FLAC__metadata_object_delete(object);
228 FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags)
230 FLAC__ASSERT(0 != filename);
231 FLAC__ASSERT(0 != tags);
233 *tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT);
238 FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet)
240 FLAC__ASSERT(0 != filename);
241 FLAC__ASSERT(0 != cuesheet);
243 *cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET);
245 return 0 != *cuesheet;
248 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
250 (void)decoder, (void)frame, (void)buffer, (void)client_data;
252 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
255 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
257 level0_client_data *cd = (level0_client_data *)client_data;
261 * we assume we only get here when the one metadata block we were
262 * looking for was passed to us
264 if(!cd->got_error && 0 == cd->object) {
265 if(0 == (cd->object = FLAC__metadata_object_clone(metadata)))
266 cd->got_error = true;
270 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
272 level0_client_data *cd = (level0_client_data *)client_data;
275 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
276 cd->got_error = true;
279 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)
281 FLAC__Metadata_SimpleIterator *it;
282 FLAC__uint64 max_area_seen = 0;
283 FLAC__uint64 max_depth_seen = 0;
285 FLAC__ASSERT(0 != filename);
286 FLAC__ASSERT(0 != picture);
290 it = FLAC__metadata_simple_iterator_new();
293 if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
294 FLAC__metadata_simple_iterator_delete(it);
298 if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) {
299 FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it);
300 FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height;
301 /* check constraints */
303 (type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) &&
304 (mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) &&
305 (description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) &&
306 obj->data.picture.width <= max_width &&
307 obj->data.picture.height <= max_height &&
308 obj->data.picture.depth <= max_depth &&
309 (area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen))
312 FLAC__metadata_object_delete(*picture);
314 max_area_seen = area;
315 max_depth_seen = obj->data.picture.depth;
318 FLAC__metadata_object_delete(obj);
321 } while(FLAC__metadata_simple_iterator_next(it));
323 FLAC__metadata_simple_iterator_delete(it);
325 return (0 != *picture);
329 /****************************************************************************
331 * Level 1 implementation
333 ***************************************************************************/
335 #define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4)
336 /* 1 for initial offset, +4 for our own personal use */
338 struct FLAC__Metadata_SimpleIterator {
340 char *filename, *tempfile_path_prefix;
342 FLAC__bool has_stats;
343 FLAC__bool is_writable;
344 FLAC__Metadata_SimpleIteratorStatus status;
345 off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH];
346 off_t first_offset; /* this is the offset to the STREAMINFO block */
348 /* this is the metadata block header of the current block we are pointing to: */
350 FLAC__MetadataType type;
354 FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = {
355 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK",
356 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT",
357 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE",
358 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE",
359 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE",
360 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA",
361 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR",
362 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR",
363 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR",
364 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR",
365 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR",
366 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR",
367 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR"
371 FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new()
373 FLAC__Metadata_SimpleIterator *iterator = (FLAC__Metadata_SimpleIterator*)calloc(1, sizeof(FLAC__Metadata_SimpleIterator));
377 iterator->filename = 0;
378 iterator->tempfile_path_prefix = 0;
379 iterator->has_stats = false;
380 iterator->is_writable = false;
381 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
382 iterator->first_offset = iterator->offset[0] = -1;
389 static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator)
391 FLAC__ASSERT(0 != iterator);
393 if(0 != iterator->file) {
394 fclose(iterator->file);
396 if(iterator->has_stats)
397 set_file_stats_(iterator->filename, &iterator->stats);
399 if(0 != iterator->filename) {
400 free(iterator->filename);
401 iterator->filename = 0;
403 if(0 != iterator->tempfile_path_prefix) {
404 free(iterator->tempfile_path_prefix);
405 iterator->tempfile_path_prefix = 0;
409 FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator)
411 FLAC__ASSERT(0 != iterator);
413 simple_iterator_free_guts_(iterator);
417 FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator)
419 FLAC__Metadata_SimpleIteratorStatus status;
421 FLAC__ASSERT(0 != iterator);
423 status = iterator->status;
424 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
428 static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only)
432 FLAC__ASSERT(0 != iterator);
434 if(read_only || 0 == (iterator->file = fopen(iterator->filename, "r+b"))) {
435 iterator->is_writable = false;
436 if(read_only || errno == EACCES) {
437 if(0 == (iterator->file = fopen(iterator->filename, "rb"))) {
438 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
443 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
448 iterator->is_writable = true;
451 ret = seek_to_first_metadata_block_(iterator->file);
455 iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file);
456 return read_metadata_block_header_(iterator);
458 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
461 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
464 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE;
473 @@@ If we decide to finish implementing this, put this comment back in metadata.h
475 * The 'tempfile_path_prefix' allows you to specify a directory where
476 * tempfiles should go. Remember that if your metadata edits cause the
477 * FLAC file to grow, the entire file will have to be rewritten. If
478 * 'tempfile_path_prefix' is NULL, the temp file will be written in the
479 * same directory as the original FLAC file. This makes replacing the
480 * original with the tempfile fast but requires extra space in the same
481 * partition for the tempfile. If space is a problem, you can pass a
482 * directory name belonging to a different partition in
483 * 'tempfile_path_prefix'. Note that you should use the forward slash
484 * '/' as the directory separator. A trailing slash is not needed; it
485 * will be added automatically.
487 FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix);
490 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)
492 const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'rename(...)' for what it will take to finish implementing this */
494 FLAC__ASSERT(0 != iterator);
495 FLAC__ASSERT(0 != filename);
497 simple_iterator_free_guts_(iterator);
499 if(!read_only && preserve_file_stats)
500 iterator->has_stats = get_file_stats_(filename, &iterator->stats);
502 if(0 == (iterator->filename = strdup(filename))) {
503 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
506 if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) {
507 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
511 return simple_iterator_prime_input_(iterator, read_only);
514 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator)
516 FLAC__ASSERT(0 != iterator);
517 FLAC__ASSERT(0 != iterator->file);
519 return iterator->is_writable;
522 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator)
524 FLAC__ASSERT(0 != iterator);
525 FLAC__ASSERT(0 != iterator->file);
527 if(iterator->is_last)
530 if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
531 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
535 iterator->offset[iterator->depth] = ftello(iterator->file);
537 return read_metadata_block_header_(iterator);
540 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator)
544 FLAC__ASSERT(0 != iterator);
545 FLAC__ASSERT(0 != iterator->file);
547 if(iterator->offset[iterator->depth] == iterator->first_offset)
550 if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) {
551 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
554 this_offset = iterator->first_offset;
555 if(!read_metadata_block_header_(iterator))
558 /* we ignore any error from ftello() and catch it in fseeko() */
559 while(ftello(iterator->file) + (off_t)iterator->length < iterator->offset[iterator->depth]) {
560 if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
561 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
564 this_offset = ftello(iterator->file);
565 if(!read_metadata_block_header_(iterator))
569 iterator->offset[iterator->depth] = this_offset;
574 FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator)
576 FLAC__ASSERT(0 != iterator);
577 FLAC__ASSERT(0 != iterator->file);
579 return iterator->type;
582 FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator)
584 FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type);
586 FLAC__ASSERT(0 != iterator);
587 FLAC__ASSERT(0 != iterator->file);
590 block->is_last = iterator->is_last;
591 block->length = iterator->length;
593 if(!read_metadata_block_data_(iterator, block)) {
594 FLAC__metadata_object_delete(block);
598 /* back up to the beginning of the block data to stay consistent */
599 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) {
600 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
601 FLAC__metadata_object_delete(block);
606 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
611 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
613 FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth];)
616 FLAC__ASSERT(0 != iterator);
617 FLAC__ASSERT(0 != iterator->file);
618 FLAC__ASSERT(0 != block);
620 if(!iterator->is_writable) {
621 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
625 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) {
626 if(iterator->type != block->type) {
627 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
632 block->is_last = iterator->is_last;
634 if(iterator->length == block->length)
635 return write_metadata_block_stationary_(iterator, block);
636 else if(iterator->length > block->length) {
637 if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) {
638 ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last);
639 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
640 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
644 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
645 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
646 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
650 else /* iterator->length < block->length */ {
651 unsigned padding_leftover = 0;
652 FLAC__bool padding_is_last = false;
654 /* first see if we can even use padding */
655 if(iterator->is_last) {
659 const unsigned extra_padding_bytes_required = block->length - iterator->length;
660 simple_iterator_push_(iterator);
661 if(!FLAC__metadata_simple_iterator_next(iterator)) {
662 (void)simple_iterator_pop_(iterator);
665 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
669 if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) {
670 padding_leftover = 0;
671 block->is_last = iterator->is_last;
673 else if(iterator->length < extra_padding_bytes_required)
676 padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required;
677 padding_is_last = iterator->is_last;
678 block->is_last = false;
681 if(!simple_iterator_pop_(iterator))
686 if(padding_leftover == 0) {
687 ret = write_metadata_block_stationary_(iterator, block);
688 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
689 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
693 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
694 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
695 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
696 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
701 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
702 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
703 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
709 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
711 unsigned padding_leftover = 0;
712 FLAC__bool padding_is_last = false;
714 FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;)
717 FLAC__ASSERT(0 != iterator);
718 FLAC__ASSERT(0 != iterator->file);
719 FLAC__ASSERT(0 != block);
721 if(!iterator->is_writable)
724 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
725 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
729 block->is_last = iterator->is_last;
732 /* first see if we can even use padding */
733 if(iterator->is_last) {
737 simple_iterator_push_(iterator);
738 if(!FLAC__metadata_simple_iterator_next(iterator)) {
739 (void)simple_iterator_pop_(iterator);
742 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
746 if(iterator->length == block->length) {
747 padding_leftover = 0;
748 block->is_last = iterator->is_last;
750 else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length)
753 padding_leftover = iterator->length - block->length;
754 padding_is_last = iterator->is_last;
755 block->is_last = false;
758 if(!simple_iterator_pop_(iterator))
763 /* move to the next block, which is suitable padding */
764 if(!FLAC__metadata_simple_iterator_next(iterator))
766 if(padding_leftover == 0) {
767 ret = write_metadata_block_stationary_(iterator, block);
768 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
769 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
773 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
774 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
775 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
776 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
781 ret = rewrite_whole_file_(iterator, block, /*append=*/true);
782 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
783 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
788 FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)
790 FLAC__ASSERT_DECLARATION(off_t debug_target_offset = iterator->offset[iterator->depth];)
793 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
794 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
799 FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
801 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
804 padding->length = iterator->length;
805 if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) {
806 FLAC__metadata_object_delete(padding);
809 FLAC__metadata_object_delete(padding);
810 if(!FLAC__metadata_simple_iterator_prev(iterator))
812 FLAC__ASSERT(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length == debug_target_offset);
813 FLAC__ASSERT(ftello(iterator->file) + (off_t)iterator->length == debug_target_offset);
817 ret = rewrite_whole_file_(iterator, 0, /*append=*/false);
818 FLAC__ASSERT(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length == debug_target_offset);
819 FLAC__ASSERT(ftello(iterator->file) + (off_t)iterator->length == debug_target_offset);
826 /****************************************************************************
828 * Level 2 implementation
830 ***************************************************************************/
833 typedef struct FLAC__Metadata_Node {
834 FLAC__StreamMetadata *data;
835 struct FLAC__Metadata_Node *prev, *next;
836 } FLAC__Metadata_Node;
838 struct FLAC__Metadata_Chain {
839 char *filename; /* will be NULL if using callbacks */
840 FLAC__Metadata_Node *head;
841 FLAC__Metadata_Node *tail;
843 FLAC__Metadata_ChainStatus status;
844 off_t first_offset, last_offset;
846 * This is the length of the chain initially read from the FLAC file.
847 * it is used to compare against the current length to decide whether
848 * or not the whole file has to be rewritten.
850 off_t initial_length;
853 struct FLAC__Metadata_Iterator {
854 FLAC__Metadata_Chain *chain;
855 FLAC__Metadata_Node *current;
858 FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = {
859 "FLAC__METADATA_CHAIN_STATUS_OK",
860 "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT",
861 "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE",
862 "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE",
863 "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE",
864 "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA",
865 "FLAC__METADATA_CHAIN_STATUS_READ_ERROR",
866 "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR",
867 "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR",
868 "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR",
869 "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR",
870 "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR",
871 "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR",
872 "FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS",
873 "FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH",
874 "FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL"
878 static FLAC__Metadata_Node *node_new_()
880 return (FLAC__Metadata_Node*)calloc(1, sizeof(FLAC__Metadata_Node));
883 static void node_delete_(FLAC__Metadata_Node *node)
885 FLAC__ASSERT(0 != node);
887 FLAC__metadata_object_delete(node->data);
891 static void chain_init_(FLAC__Metadata_Chain *chain)
893 FLAC__ASSERT(0 != chain);
896 chain->head = chain->tail = 0;
898 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
899 chain->initial_length = 0;
902 static void chain_clear_(FLAC__Metadata_Chain *chain)
904 FLAC__Metadata_Node *node, *next;
906 FLAC__ASSERT(0 != chain);
908 for(node = chain->head; node; ) {
914 if(0 != chain->filename)
915 free(chain->filename);
920 static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
922 FLAC__ASSERT(0 != chain);
923 FLAC__ASSERT(0 != node);
924 FLAC__ASSERT(0 != node->data);
926 node->next = node->prev = 0;
927 node->data->is_last = true;
929 chain->tail->data->is_last = false;
934 FLAC__ASSERT(0 != chain->tail);
935 chain->tail->next = node;
936 node->prev = chain->tail;
942 static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
944 FLAC__ASSERT(0 != chain);
945 FLAC__ASSERT(0 != node);
947 if(node == chain->head)
948 chain->head = node->next;
950 node->prev->next = node->next;
952 if(node == chain->tail)
953 chain->tail = node->prev;
955 node->next->prev = node->prev;
958 chain->tail->data->is_last = true;
963 static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
965 chain_remove_node_(chain, node);
969 static off_t chain_calculate_length_(FLAC__Metadata_Chain *chain)
971 const FLAC__Metadata_Node *node;
973 for(node = chain->head; node; node = node->next)
974 length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
978 static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
980 FLAC__ASSERT(0 != node);
981 FLAC__ASSERT(0 != node->data);
982 FLAC__ASSERT(0 != iterator);
983 FLAC__ASSERT(0 != iterator->current);
984 FLAC__ASSERT(0 != iterator->chain);
985 FLAC__ASSERT(0 != iterator->chain->head);
986 FLAC__ASSERT(0 != iterator->chain->tail);
988 node->data->is_last = false;
990 node->prev = iterator->current->prev;
991 node->next = iterator->current;
994 iterator->chain->head = node;
996 node->prev->next = node;
998 iterator->current->prev = node;
1000 iterator->chain->nodes++;
1003 static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
1005 FLAC__ASSERT(0 != node);
1006 FLAC__ASSERT(0 != node->data);
1007 FLAC__ASSERT(0 != iterator);
1008 FLAC__ASSERT(0 != iterator->current);
1009 FLAC__ASSERT(0 != iterator->chain);
1010 FLAC__ASSERT(0 != iterator->chain->head);
1011 FLAC__ASSERT(0 != iterator->chain->tail);
1013 iterator->current->data->is_last = false;
1015 node->prev = iterator->current;
1016 node->next = iterator->current->next;
1019 iterator->chain->tail = node;
1021 node->next->prev = node;
1023 node->prev->next = node;
1025 iterator->chain->tail->data->is_last = true;
1027 iterator->chain->nodes++;
1030 /* return true iff node and node->next are both padding */
1031 static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
1033 if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) {
1034 const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
1035 node->data->length += growth;
1037 chain_delete_node_(chain, node->next);
1044 /* Returns the new length of the chain, or 0 if there was an error. */
1045 /* WATCHOUT: This can get called multiple times before a write, so
1046 * it should still work when this happens.
1048 /* WATCHOUT: Make sure to also update the logic in
1049 * FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes.
1051 static off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
1053 off_t current_length = chain_calculate_length_(chain);
1056 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1057 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1058 const off_t delta = chain->initial_length - current_length;
1059 chain->tail->data->length += delta;
1060 current_length += delta;
1061 FLAC__ASSERT(current_length == chain->initial_length);
1063 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
1064 else if(current_length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
1065 FLAC__StreamMetadata *padding;
1066 FLAC__Metadata_Node *node;
1067 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) {
1068 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1071 padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
1072 if(0 == (node = node_new_())) {
1073 FLAC__metadata_object_delete(padding);
1074 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1077 node->data = padding;
1078 chain_append_node_(chain, node);
1079 current_length = chain_calculate_length_(chain);
1080 FLAC__ASSERT(current_length == chain->initial_length);
1082 /* 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 */
1083 else if(current_length > chain->initial_length) {
1084 const off_t delta = current_length - chain->initial_length;
1085 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1086 /* if the delta is exactly the size of the last padding block, remove the padding block */
1087 if((off_t)chain->tail->data->length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
1088 chain_delete_node_(chain, chain->tail);
1089 current_length = chain_calculate_length_(chain);
1090 FLAC__ASSERT(current_length == chain->initial_length);
1092 /* if there is at least 'delta' bytes of padding, trim the padding down */
1093 else if((off_t)chain->tail->data->length >= delta) {
1094 chain->tail->data->length -= delta;
1095 current_length -= delta;
1096 FLAC__ASSERT(current_length == chain->initial_length);
1102 return current_length;
1105 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)
1107 FLAC__Metadata_Node *node;
1109 FLAC__ASSERT(0 != chain);
1111 /* we assume we're already at the beginning of the file */
1113 switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) {
1117 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1120 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1123 chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
1131 FLAC__int64 pos = tell_cb(handle);
1133 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1136 chain->first_offset = (off_t)pos;
1141 FLAC__MetadataType type;
1147 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1151 if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) {
1152 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1156 node->data = FLAC__metadata_object_new(type);
1157 if(0 == node->data) {
1159 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1163 node->data->is_last = is_last;
1164 node->data->length = length;
1166 chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data));
1167 if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
1171 chain_append_node_(chain, node);
1176 FLAC__int64 pos = tell_cb(handle);
1178 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1181 chain->last_offset = (off_t)pos;
1184 chain->initial_length = chain_calculate_length_(chain);
1189 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)
1191 FLAC__Metadata_Node *node;
1193 FLAC__ASSERT(0 != chain);
1194 FLAC__ASSERT(0 != chain->head);
1196 if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) {
1197 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1201 for(node = chain->head; node; node = node->next) {
1202 if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) {
1203 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1206 if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) {
1207 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1212 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
1214 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1218 static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain)
1223 FLAC__ASSERT(0 != chain->filename);
1225 if(0 == (file = fopen(chain->filename, "r+b"))) {
1226 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1230 /* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */
1231 ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_);
1238 static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
1242 FLAC__Metadata_SimpleIteratorStatus status;
1243 const FLAC__Metadata_Node *node;
1245 FLAC__ASSERT(0 != chain);
1246 FLAC__ASSERT(0 != chain->filename);
1247 FLAC__ASSERT(0 != chain->head);
1249 /* copy the file prefix (data up to first metadata block */
1250 if(0 == (f = fopen(chain->filename, "rb"))) {
1251 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1254 if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) {
1255 chain->status = get_equivalent_status_(status);
1256 cleanup_tempfile_(&tempfile, &tempfilename);
1259 if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) {
1260 chain->status = get_equivalent_status_(status);
1261 cleanup_tempfile_(&tempfile, &tempfilename);
1265 /* write the metadata */
1266 for(node = chain->head; node; node = node->next) {
1267 if(!write_metadata_block_header_(tempfile, &status, node->data)) {
1268 chain->status = get_equivalent_status_(status);
1271 if(!write_metadata_block_data_(tempfile, &status, node->data)) {
1272 chain->status = get_equivalent_status_(status);
1276 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
1278 /* copy the file postfix (everything after the metadata) */
1279 if(0 != fseeko(f, chain->last_offset, SEEK_SET)) {
1280 cleanup_tempfile_(&tempfile, &tempfilename);
1281 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1284 if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) {
1285 cleanup_tempfile_(&tempfile, &tempfilename);
1286 chain->status = get_equivalent_status_(status);
1290 /* move the tempfile on top of the original */
1292 if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status))
1298 /* assumes 'handle' is already at beginning of file */
1299 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)
1301 FLAC__Metadata_SimpleIteratorStatus status;
1302 const FLAC__Metadata_Node *node;
1304 FLAC__ASSERT(0 != chain);
1305 FLAC__ASSERT(0 == chain->filename);
1306 FLAC__ASSERT(0 != chain->head);
1308 /* copy the file prefix (data up to first metadata block */
1309 if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) {
1310 chain->status = get_equivalent_status_(status);
1314 /* write the metadata */
1315 for(node = chain->head; node; node = node->next) {
1316 if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) {
1317 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1320 if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) {
1321 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1325 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
1327 /* copy the file postfix (everything after the metadata) */
1328 if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) {
1329 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1332 if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) {
1333 chain->status = get_equivalent_status_(status);
1340 FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new()
1342 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)calloc(1, sizeof(FLAC__Metadata_Chain));
1350 FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain)
1352 FLAC__ASSERT(0 != chain);
1354 chain_clear_(chain);
1359 FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain)
1361 FLAC__Metadata_ChainStatus status;
1363 FLAC__ASSERT(0 != chain);
1365 status = chain->status;
1366 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1370 FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
1375 FLAC__ASSERT(0 != chain);
1376 FLAC__ASSERT(0 != filename);
1378 chain_clear_(chain);
1380 if(0 == (chain->filename = strdup(filename))) {
1381 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1385 if(0 == (file = fopen(filename, "rb"))) {
1386 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1390 /* chain_read_cb_() sets chain->status for us */
1391 ret = chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_);
1398 FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1400 FLAC__ASSERT(0 != chain);
1402 chain_clear_(chain);
1404 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) {
1405 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1410 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1411 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1415 if(!chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell))
1416 return false; /* chain->status is already set by chain_read_cb_ */
1421 FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
1423 /* This does all the same checks that are in chain_prepare_for_write_()
1424 * but doesn't actually alter the chain. Make sure to update the logic
1425 * here if chain_prepare_for_write_() changes.
1427 const off_t current_length = chain_calculate_length_(chain);
1429 FLAC__ASSERT(0 != chain);
1432 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1433 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING)
1435 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
1436 else if(current_length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length)
1438 /* 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 */
1439 else if(current_length > chain->initial_length) {
1440 const off_t delta = current_length - chain->initial_length;
1441 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1442 /* if the delta is exactly the size of the last padding block, remove the padding block */
1443 if((off_t)chain->tail->data->length + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta)
1445 /* if there is at least 'delta' bytes of padding, trim the padding down */
1446 else if((off_t)chain->tail->data->length >= delta)
1452 return (current_length != chain->initial_length);
1455 FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats)
1458 const char *tempfile_path_prefix = 0;
1459 off_t current_length;
1461 FLAC__ASSERT(0 != chain);
1463 if (0 == chain->filename) {
1464 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1468 current_length = chain_prepare_for_write_(chain, use_padding);
1470 /* a return value of 0 means there was an error; chain->status is already set */
1471 if (0 == current_length)
1474 if(preserve_file_stats)
1475 get_file_stats_(chain->filename, &stats);
1477 if(current_length == chain->initial_length) {
1478 if(!chain_rewrite_metadata_in_place_(chain))
1482 if(!chain_rewrite_file_(chain, tempfile_path_prefix))
1485 /* recompute lengths and offsets */
1487 const FLAC__Metadata_Node *node;
1488 chain->initial_length = current_length;
1489 chain->last_offset = chain->first_offset;
1490 for(node = chain->head; node; node = node->next)
1491 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1495 if(preserve_file_stats)
1496 set_file_stats_(chain->filename, &stats);
1501 FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1503 off_t current_length;
1505 FLAC__ASSERT(0 != chain);
1507 if (0 != chain->filename) {
1508 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1512 if (0 == callbacks.write || 0 == callbacks.seek) {
1513 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1517 if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1518 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1522 current_length = chain_prepare_for_write_(chain, use_padding);
1524 /* a return value of 0 means there was an error; chain->status is already set */
1525 if (0 == current_length)
1528 FLAC__ASSERT(current_length == chain->initial_length);
1530 return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek);
1533 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)
1535 off_t current_length;
1537 FLAC__ASSERT(0 != chain);
1539 if (0 != chain->filename) {
1540 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1544 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) {
1545 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1548 if (0 == temp_callbacks.write) {
1549 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1553 if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1554 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1558 current_length = chain_prepare_for_write_(chain, use_padding);
1560 /* a return value of 0 means there was an error; chain->status is already set */
1561 if (0 == current_length)
1564 FLAC__ASSERT(current_length != chain->initial_length);
1567 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1568 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1572 if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write))
1575 /* recompute lengths and offsets */
1577 const FLAC__Metadata_Node *node;
1578 chain->initial_length = current_length;
1579 chain->last_offset = chain->first_offset;
1580 for(node = chain->head; node; node = node->next)
1581 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1587 FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain)
1589 FLAC__Metadata_Node *node;
1591 FLAC__ASSERT(0 != chain);
1593 for(node = chain->head; node; ) {
1594 if(!chain_merge_adjacent_padding_(chain, node))
1599 FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
1601 FLAC__Metadata_Node *node, *save;
1604 FLAC__ASSERT(0 != chain);
1607 * Don't try and be too smart... this simple algo is good enough for
1608 * the small number of nodes that we deal with.
1610 for(i = 0, node = chain->head; i < chain->nodes; i++) {
1611 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1613 chain_remove_node_(chain, node);
1614 chain_append_node_(chain, node);
1622 FLAC__metadata_chain_merge_padding(chain);
1626 FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new()
1628 FLAC__Metadata_Iterator *iterator = (FLAC__Metadata_Iterator*)calloc(1, sizeof(FLAC__Metadata_Iterator));
1630 /* calloc() implies:
1631 iterator->current = 0;
1632 iterator->chain = 0;
1638 FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator)
1640 FLAC__ASSERT(0 != iterator);
1645 FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain)
1647 FLAC__ASSERT(0 != iterator);
1648 FLAC__ASSERT(0 != chain);
1649 FLAC__ASSERT(0 != chain->head);
1651 iterator->chain = chain;
1652 iterator->current = chain->head;
1655 FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator)
1657 FLAC__ASSERT(0 != iterator);
1659 if(0 == iterator->current || 0 == iterator->current->next)
1662 iterator->current = iterator->current->next;
1666 FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator)
1668 FLAC__ASSERT(0 != iterator);
1670 if(0 == iterator->current || 0 == iterator->current->prev)
1673 iterator->current = iterator->current->prev;
1677 FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator)
1679 FLAC__ASSERT(0 != iterator);
1680 FLAC__ASSERT(0 != iterator->current);
1681 FLAC__ASSERT(0 != iterator->current->data);
1683 return iterator->current->data->type;
1686 FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator)
1688 FLAC__ASSERT(0 != iterator);
1689 FLAC__ASSERT(0 != iterator->current);
1691 return iterator->current->data;
1694 FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1696 FLAC__ASSERT(0 != iterator);
1697 FLAC__ASSERT(0 != block);
1698 return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block);
1701 FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding)
1703 FLAC__Metadata_Node *save;
1705 FLAC__ASSERT(0 != iterator);
1706 FLAC__ASSERT(0 != iterator->current);
1708 if(0 == iterator->current->prev) {
1709 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1713 save = iterator->current->prev;
1715 if(replace_with_padding) {
1716 FLAC__metadata_object_delete_data(iterator->current->data);
1717 iterator->current->data->type = FLAC__METADATA_TYPE_PADDING;
1720 chain_delete_node_(iterator->chain, iterator->current);
1723 iterator->current = save;
1727 FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1729 FLAC__Metadata_Node *node;
1731 FLAC__ASSERT(0 != iterator);
1732 FLAC__ASSERT(0 != iterator->current);
1733 FLAC__ASSERT(0 != block);
1735 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1738 if(0 == iterator->current->prev) {
1739 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1743 if(0 == (node = node_new_()))
1747 iterator_insert_node_(iterator, node);
1748 iterator->current = node;
1752 FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1754 FLAC__Metadata_Node *node;
1756 FLAC__ASSERT(0 != iterator);
1757 FLAC__ASSERT(0 != iterator->current);
1758 FLAC__ASSERT(0 != block);
1760 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1763 if(0 == (node = node_new_()))
1767 iterator_insert_node_after_(iterator, node);
1768 iterator->current = node;
1773 /****************************************************************************
1775 * Local function definitions
1777 ***************************************************************************/
1779 void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1785 for(i = 0; i < bytes; i++) {
1786 *(--b) = (FLAC__byte)(val & 0xff);
1791 void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1795 for(i = 0; i < bytes; i++) {
1796 *(b++) = (FLAC__byte)(val & 0xff);
1801 void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes)
1807 for(i = 0; i < bytes; i++) {
1808 *(--b) = (FLAC__byte)(val & 0xff);
1813 FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes)
1815 FLAC__uint32 ret = 0;
1818 for(i = 0; i < bytes; i++)
1819 ret = (ret << 8) | (FLAC__uint32)(*b++);
1824 FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes)
1826 FLAC__uint32 ret = 0;
1831 for(i = 0; i < bytes; i++)
1832 ret = (ret << 8) | (FLAC__uint32)(*--b);
1837 FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes)
1839 FLAC__uint64 ret = 0;
1842 for(i = 0; i < bytes; i++)
1843 ret = (ret << 8) | (FLAC__uint64)(*b++);
1848 FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator)
1850 FLAC__ASSERT(0 != iterator);
1851 FLAC__ASSERT(0 != iterator->file);
1853 if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) {
1854 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1861 FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block)
1863 FLAC__ASSERT(0 != iterator);
1864 FLAC__ASSERT(0 != iterator->file);
1866 iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block);
1868 return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
1871 FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length)
1873 FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH];
1875 if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
1878 *is_last = raw_header[0] & 0x80? true : false;
1879 *type = (FLAC__MetadataType)(raw_header[0] & 0x7f);
1880 *length = unpack_uint32_(raw_header + 1, 3);
1882 /* Note that we don't check:
1883 * if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED)
1884 * we just will read in an opaque block
1890 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block)
1892 switch(block->type) {
1893 case FLAC__METADATA_TYPE_STREAMINFO:
1894 return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info);
1895 case FLAC__METADATA_TYPE_PADDING:
1896 return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length);
1897 case FLAC__METADATA_TYPE_APPLICATION:
1898 return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length);
1899 case FLAC__METADATA_TYPE_SEEKTABLE:
1900 return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length);
1901 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1902 return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, &block->data.vorbis_comment);
1903 case FLAC__METADATA_TYPE_CUESHEET:
1904 return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet);
1905 case FLAC__METADATA_TYPE_PICTURE:
1906 return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture);
1908 return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length);
1912 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block)
1914 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b;
1916 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
1917 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1921 /* we are using hardcoded numbers for simplicity but we should
1922 * probably eventually write a bit-level unpacker and use the
1923 * _STREAMINFO_ constants.
1925 block->min_blocksize = unpack_uint32_(b, 2); b += 2;
1926 block->max_blocksize = unpack_uint32_(b, 2); b += 2;
1927 block->min_framesize = unpack_uint32_(b, 3); b += 3;
1928 block->max_framesize = unpack_uint32_(b, 3); b += 3;
1929 block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
1930 block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
1931 block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
1932 block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4);
1933 memcpy(block->md5sum, b+8, 16);
1935 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1938 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length)
1940 (void)block; /* nothing to do; we don't care about reading the padding bytes */
1942 if(0 != seek_cb(handle, block_length, SEEK_CUR))
1943 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
1945 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1948 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length)
1950 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
1952 if(read_cb(block->id, 1, id_bytes, handle) != id_bytes)
1953 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1955 block_length -= id_bytes;
1957 if(block_length == 0) {
1961 if(0 == (block->data = (FLAC__byte*)malloc(block_length)))
1962 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
1964 if(read_cb(block->data, 1, block_length, handle) != block_length)
1965 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1968 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1971 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length)
1974 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
1976 FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0);
1978 block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1980 if(block->num_points == 0)
1982 else if(0 == (block->points = (FLAC__StreamMetadata_SeekPoint*)malloc(block->num_points * sizeof(FLAC__StreamMetadata_SeekPoint))))
1983 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
1985 for(i = 0; i < block->num_points; i++) {
1986 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
1987 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1988 /* some MAGIC NUMBERs here */
1989 block->points[i].sample_number = unpack_uint64_(buffer, 8);
1990 block->points[i].stream_offset = unpack_uint64_(buffer+8, 8);
1991 block->points[i].frame_samples = unpack_uint32_(buffer+16, 2);
1994 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1997 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry)
1999 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
2000 FLAC__byte buffer[4]; /* magic number is asserted below */
2002 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer));
2004 if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2005 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2006 entry->length = unpack_uint32_little_endian_(buffer, entry_length_len);
2008 if(0 != entry->entry)
2011 if(entry->length == 0) {
2015 if(0 == (entry->entry = (FLAC__byte*)malloc(entry->length+1)))
2016 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2018 if(read_cb(entry->entry, 1, entry->length, handle) != entry->length)
2019 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2021 entry->entry[entry->length] = '\0';
2024 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2027 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block)
2030 FLAC__Metadata_SimpleIteratorStatus status;
2031 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
2032 FLAC__byte buffer[4]; /* magic number is asserted below */
2034 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer));
2036 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string))))
2039 if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
2040 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2041 block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len);
2043 if(block->num_comments == 0) {
2044 block->comments = 0;
2046 else if(0 == (block->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry))))
2047 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2049 for(i = 0; i < block->num_comments; i++) {
2050 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i)))
2054 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2057 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track)
2060 FLAC__byte buffer[32]; /* asserted below that this is big enough */
2062 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
2063 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2064 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);
2066 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2067 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
2068 if(read_cb(buffer, 1, len, handle) != len)
2069 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2070 track->offset = unpack_uint64_(buffer, len);
2072 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2073 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
2074 if(read_cb(buffer, 1, len, handle) != len)
2075 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2076 track->number = (FLAC__byte)unpack_uint32_(buffer, len);
2078 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2079 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
2080 if(read_cb(track->isrc, 1, len, handle) != len)
2081 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2083 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);
2084 len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
2085 if(read_cb(buffer, 1, len, handle) != len)
2086 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2087 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1);
2088 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1);
2089 track->type = buffer[0] >> 7;
2090 track->pre_emphasis = (buffer[0] >> 6) & 1;
2092 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2093 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
2094 if(read_cb(buffer, 1, len, handle) != len)
2095 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2096 track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len);
2098 if(track->num_indices == 0) {
2101 else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
2102 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2104 for(i = 0; i < track->num_indices; i++) {
2105 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2106 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
2107 if(read_cb(buffer, 1, len, handle) != len)
2108 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2109 track->indices[i].offset = unpack_uint64_(buffer, len);
2111 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2112 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
2113 if(read_cb(buffer, 1, len, handle) != len)
2114 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2115 track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len);
2117 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2118 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
2119 if(read_cb(buffer, 1, len, handle) != len)
2120 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2123 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2126 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block)
2129 FLAC__Metadata_SimpleIteratorStatus status;
2130 FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */
2132 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer));
2133 FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer));
2135 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2136 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
2137 if(read_cb(block->media_catalog_number, 1, len, handle) != len)
2138 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2140 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2141 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
2142 if(read_cb(buffer, 1, len, handle) != len)
2143 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2144 block->lead_in = unpack_uint64_(buffer, len);
2146 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2147 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
2148 if(read_cb(buffer, 1, len, handle) != len)
2149 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2150 block->is_cd = buffer[0]&0x80? true : false;
2152 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2153 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
2154 if(read_cb(buffer, 1, len, handle) != len)
2155 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2156 block->num_tracks = unpack_uint32_(buffer, len);
2158 if(block->num_tracks == 0) {
2161 else if(0 == (block->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
2162 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2164 for(i = 0; i < block->num_tracks; i++) {
2165 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i)))
2169 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2172 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)
2174 FLAC__byte buffer[sizeof(FLAC__uint32)];
2176 FLAC__ASSERT(0 != data);
2177 FLAC__ASSERT(length_len%8 == 0);
2179 length_len /= 8; /* convert to bytes */
2181 FLAC__ASSERT(sizeof(buffer) >= length_len);
2183 if(read_cb(buffer, 1, length_len, handle) != length_len)
2184 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2185 *length = unpack_uint32_(buffer, length_len);
2190 if(0 == (*data = (FLAC__byte*)malloc(*length+1)))
2191 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2194 if(read_cb(*data, 1, *length, handle) != *length)
2195 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2198 (*data)[*length] = '\0';
2200 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2203 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block)
2205 FLAC__Metadata_SimpleIteratorStatus status;
2206 FLAC__byte buffer[4]; /* asserted below that this is big enough */
2209 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2210 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2211 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2212 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
2214 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0);
2215 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8;
2216 if(read_cb(buffer, 1, len, handle) != len)
2217 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2218 block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len);
2220 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)
2223 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)
2226 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0);
2227 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8;
2228 if(read_cb(buffer, 1, len, handle) != len)
2229 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2230 block->width = unpack_uint32_(buffer, len);
2232 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0);
2233 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8;
2234 if(read_cb(buffer, 1, len, handle) != len)
2235 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2236 block->height = unpack_uint32_(buffer, len);
2238 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0);
2239 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8;
2240 if(read_cb(buffer, 1, len, handle) != len)
2241 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2242 block->depth = unpack_uint32_(buffer, len);
2244 /* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */
2245 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)
2248 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2251 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length)
2253 if(block_length == 0) {
2257 if(0 == (block->data = (FLAC__byte*)malloc(block_length)))
2258 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2260 if(read_cb(block->data, 1, block_length, handle) != block_length)
2261 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2264 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2267 FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
2269 FLAC__ASSERT(0 != file);
2270 FLAC__ASSERT(0 != status);
2272 if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
2273 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2280 FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
2282 FLAC__ASSERT(0 != file);
2283 FLAC__ASSERT(0 != status);
2285 if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
2286 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2290 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2295 FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2297 FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];
2299 FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
2301 buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
2302 pack_uint32_(block->length, buffer + 1, 3);
2304 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
2310 FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2312 FLAC__ASSERT(0 != block);
2314 switch(block->type) {
2315 case FLAC__METADATA_TYPE_STREAMINFO:
2316 return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info);
2317 case FLAC__METADATA_TYPE_PADDING:
2318 return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length);
2319 case FLAC__METADATA_TYPE_APPLICATION:
2320 return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length);
2321 case FLAC__METADATA_TYPE_SEEKTABLE:
2322 return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table);
2323 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
2324 return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment);
2325 case FLAC__METADATA_TYPE_CUESHEET:
2326 return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet);
2327 case FLAC__METADATA_TYPE_PICTURE:
2328 return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture);
2330 return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length);
2334 FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block)
2336 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
2337 const unsigned channels1 = block->channels - 1;
2338 const unsigned bps1 = block->bits_per_sample - 1;
2340 /* we are using hardcoded numbers for simplicity but we should
2341 * probably eventually write a bit-level packer and use the
2342 * _STREAMINFO_ constants.
2344 pack_uint32_(block->min_blocksize, buffer, 2);
2345 pack_uint32_(block->max_blocksize, buffer+2, 2);
2346 pack_uint32_(block->min_framesize, buffer+4, 3);
2347 pack_uint32_(block->max_framesize, buffer+7, 3);
2348 buffer[10] = (block->sample_rate >> 12) & 0xff;
2349 buffer[11] = (block->sample_rate >> 4) & 0xff;
2350 buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4);
2351 buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f));
2352 pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
2353 memcpy(buffer+18, block->md5sum, 16);
2355 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
2361 FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length)
2363 unsigned i, n = block_length;
2364 FLAC__byte buffer[1024];
2368 memset(buffer, 0, 1024);
2370 for(i = 0; i < n/1024; i++)
2371 if(write_cb(buffer, 1, 1024, handle) != 1024)
2376 if(write_cb(buffer, 1, n, handle) != n)
2382 FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length)
2384 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
2386 if(write_cb(block->id, 1, id_bytes, handle) != id_bytes)
2389 block_length -= id_bytes;
2391 if(write_cb(block->data, 1, block_length, handle) != block_length)
2397 FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block)
2400 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
2402 for(i = 0; i < block->num_points; i++) {
2403 /* some MAGIC NUMBERs here */
2404 pack_uint64_(block->points[i].sample_number, buffer, 8);
2405 pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
2406 pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
2407 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
2414 FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block)
2417 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
2418 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
2419 FLAC__byte buffer[4]; /* magic number is asserted below */
2421 FLAC__ASSERT(max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer));
2423 pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
2424 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2426 if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length)
2429 pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
2430 if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
2433 for(i = 0; i < block->num_comments; i++) {
2434 pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
2435 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2437 if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length)
2444 FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block)
2447 FLAC__byte buffer[1024]; /* asserted below that this is big enough */
2449 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
2450 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8);
2451 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);
2452 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2454 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2455 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
2456 if(write_cb(block->media_catalog_number, 1, len, handle) != len)
2459 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2460 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
2461 pack_uint64_(block->lead_in, buffer, len);
2462 if(write_cb(buffer, 1, len, handle) != len)
2465 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2466 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
2467 memset(buffer, 0, len);
2470 if(write_cb(buffer, 1, len, handle) != len)
2473 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2474 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
2475 pack_uint32_(block->num_tracks, buffer, len);
2476 if(write_cb(buffer, 1, len, handle) != len)
2479 for(i = 0; i < block->num_tracks; i++) {
2480 FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i;
2482 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2483 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
2484 pack_uint64_(track->offset, buffer, len);
2485 if(write_cb(buffer, 1, len, handle) != len)
2488 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2489 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
2490 pack_uint32_(track->number, buffer, len);
2491 if(write_cb(buffer, 1, len, handle) != len)
2494 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2495 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
2496 if(write_cb(track->isrc, 1, len, handle) != len)
2499 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);
2500 len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
2501 memset(buffer, 0, len);
2502 buffer[0] = (track->type << 7) | (track->pre_emphasis << 6);
2503 if(write_cb(buffer, 1, len, handle) != len)
2506 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2507 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
2508 pack_uint32_(track->num_indices, buffer, len);
2509 if(write_cb(buffer, 1, len, handle) != len)
2512 for(j = 0; j < track->num_indices; j++) {
2513 FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;
2515 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2516 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
2517 pack_uint64_(index->offset, buffer, len);
2518 if(write_cb(buffer, 1, len, handle) != len)
2521 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2522 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
2523 pack_uint32_(index->number, buffer, len);
2524 if(write_cb(buffer, 1, len, handle) != len)
2527 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2528 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
2529 memset(buffer, 0, len);
2530 if(write_cb(buffer, 1, len, handle) != len)
2538 FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block)
2542 FLAC__byte buffer[4]; /* magic number is asserted below */
2544 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8);
2545 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8);
2546 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8);
2547 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8);
2548 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8);
2549 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8);
2550 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8);
2551 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2552 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8);
2553 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8);
2554 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2555 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2556 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
2557 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8);
2559 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8;
2560 pack_uint32_(block->type, buffer, len);
2561 if(write_cb(buffer, 1, len, handle) != len)
2564 len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8;
2565 slen = strlen(block->mime_type);
2566 pack_uint32_(slen, buffer, len);
2567 if(write_cb(buffer, 1, len, handle) != len)
2569 if(write_cb(block->mime_type, 1, slen, handle) != slen)
2572 len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8;
2573 slen = strlen((const char *)block->description);
2574 pack_uint32_(slen, buffer, len);
2575 if(write_cb(buffer, 1, len, handle) != len)
2577 if(write_cb(block->description, 1, slen, handle) != slen)
2580 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8;
2581 pack_uint32_(block->width, buffer, len);
2582 if(write_cb(buffer, 1, len, handle) != len)
2585 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8;
2586 pack_uint32_(block->height, buffer, len);
2587 if(write_cb(buffer, 1, len, handle) != len)
2590 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8;
2591 pack_uint32_(block->depth, buffer, len);
2592 if(write_cb(buffer, 1, len, handle) != len)
2595 len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8;
2596 pack_uint32_(block->data_length, buffer, len);
2597 if(write_cb(buffer, 1, len, handle) != len)
2599 if(write_cb(block->data, 1, block->data_length, handle) != block->data_length)
2605 FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length)
2607 if(write_cb(block->data, 1, block_length, handle) != block_length)
2613 FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block)
2615 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2616 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2620 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2623 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2626 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2627 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2631 return read_metadata_block_header_(iterator);
2634 FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last)
2636 FLAC__StreamMetadata *padding;
2638 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2639 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2643 block->is_last = false;
2645 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2648 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2651 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
2652 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2654 padding->is_last = padding_is_last;
2655 padding->length = padding_length;
2657 if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) {
2658 FLAC__metadata_object_delete(padding);
2662 if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) {
2663 FLAC__metadata_object_delete(padding);
2667 FLAC__metadata_object_delete(padding);
2669 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2670 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2674 return read_metadata_block_header_(iterator);
2677 FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append)
2681 int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */
2682 off_t fixup_is_last_flag_offset = -1;
2684 FLAC__ASSERT(0 != block || append == false);
2686 if(iterator->is_last) {
2688 fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */
2689 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
2691 else if(0 == block) {
2692 simple_iterator_push_(iterator);
2693 if(!FLAC__metadata_simple_iterator_prev(iterator)) {
2694 (void)simple_iterator_pop_(iterator);
2697 fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */
2698 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
2699 if(!simple_iterator_pop_(iterator))
2704 if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append))
2708 if(!write_metadata_block_header_(tempfile, &iterator->status, block)) {
2709 cleanup_tempfile_(&tempfile, &tempfilename);
2713 if(!write_metadata_block_data_(tempfile, &iterator->status, block)) {
2714 cleanup_tempfile_(&tempfile, &tempfilename);
2719 if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0))
2723 return FLAC__metadata_simple_iterator_next(iterator);
2728 void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator)
2730 FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH);
2731 iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth];
2735 FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator)
2737 FLAC__ASSERT(iterator->depth > 0);
2739 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
2740 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2744 return read_metadata_block_header_(iterator);
2751 * 3: not a FLAC file
2753 unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb)
2755 FLAC__byte buffer[4];
2759 FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer));
2761 /* skip any id3v2 tag */
2763 n = read_cb(buffer, 1, 4, handle);
2768 else if(0 == memcmp(buffer, "ID3", 3)) {
2769 unsigned tag_length = 0;
2771 /* skip to the tag length */
2772 if(seek_cb(handle, 2, SEEK_CUR) < 0)
2775 /* read the length */
2776 for(i = 0; i < 4; i++) {
2777 if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80)
2780 tag_length |= (buffer[0] & 0x7f);
2783 /* skip the rest of the tag */
2784 if(seek_cb(handle, tag_length, SEEK_CUR) < 0)
2787 /* read the stream sync code */
2789 n = read_cb(buffer, 1, 4, handle);
2796 /* check for the fLaC signature */
2797 if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH))
2803 unsigned seek_to_first_metadata_block_(FILE *f)
2805 return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_);
2808 FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append)
2810 const off_t offset_end = append? iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length : iterator->offset[iterator->depth];
2812 if(0 != fseeko(iterator->file, 0, SEEK_SET)) {
2813 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2816 if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) {
2817 cleanup_tempfile_(tempfile, tempfilename);
2820 if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) {
2821 cleanup_tempfile_(tempfile, tempfilename);
2828 FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, off_t fixup_is_last_flag_offset, FLAC__bool backup)
2830 off_t save_offset = iterator->offset[iterator->depth];
2831 FLAC__ASSERT(0 != *tempfile);
2833 if(0 != fseeko(iterator->file, save_offset + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length, SEEK_SET)) {
2834 cleanup_tempfile_(tempfile, tempfilename);
2835 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2838 if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) {
2839 cleanup_tempfile_(tempfile, tempfilename);
2843 if(fixup_is_last_code != 0) {
2845 * if code == 1, it means a block was appended to the end so
2846 * we have to clear the is_last flag of the previous block
2847 * if code == -1, it means the last block was deleted so
2848 * we have to set the is_last flag of the previous block
2850 /* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */
2852 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
2853 cleanup_tempfile_(tempfile, tempfilename);
2854 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2857 if(fread(&x, 1, 1, *tempfile) != 1) {
2858 cleanup_tempfile_(tempfile, tempfilename);
2859 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2862 if(fixup_is_last_code > 0) {
2863 FLAC__ASSERT(x & 0x80);
2867 FLAC__ASSERT(!(x & 0x80));
2870 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
2871 cleanup_tempfile_(tempfile, tempfilename);
2872 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2875 if(local__fwrite(&x, 1, 1, *tempfile) != 1) {
2876 cleanup_tempfile_(tempfile, tempfilename);
2877 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2882 (void)fclose(iterator->file);
2884 if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status))
2887 if(iterator->has_stats)
2888 set_file_stats_(iterator->filename, &iterator->stats);
2890 if(!simple_iterator_prime_input_(iterator, !iterator->is_writable))
2893 while(iterator->offset[iterator->depth] + (off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (off_t)iterator->length < save_offset)
2894 if(!FLAC__metadata_simple_iterator_next(iterator))
2899 /* move the iterator to it's original block faster by faking a push, then doing a pop_ */
2900 FLAC__ASSERT(iterator->depth == 0);
2901 iterator->offset[0] = save_offset;
2903 return simple_iterator_pop_(iterator);
2907 FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
2909 FLAC__byte buffer[8192];
2913 n = min(sizeof(buffer), bytes);
2914 if(fread(buffer, 1, n, file) != n) {
2915 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2918 if(local__fwrite(buffer, 1, n, tempfile) != n) {
2919 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2928 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, off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
2930 FLAC__byte buffer[8192];
2934 n = min(sizeof(buffer), bytes);
2935 if(read_cb(buffer, 1, n, handle) != n) {
2936 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2939 if(temp_write_cb(buffer, 1, n, temp_handle) != n) {
2940 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2949 FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status)
2951 FLAC__byte buffer[8192];
2954 while(!feof(file)) {
2955 n = fread(buffer, 1, sizeof(buffer), file);
2956 if(n == 0 && !feof(file)) {
2957 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2960 if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) {
2961 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2969 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)
2971 FLAC__byte buffer[8192];
2974 while(!eof_cb(handle)) {
2975 n = read_cb(buffer, 1, sizeof(buffer), handle);
2976 if(n == 0 && !eof_cb(handle)) {
2977 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2980 if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) {
2981 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2989 FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
2991 static const char *tempfile_suffix = ".metadata_edit";
2992 if(0 == tempfile_path_prefix) {
2993 if(0 == (*tempfilename = (char*)malloc(strlen(filename) + strlen(tempfile_suffix) + 1))) {
2994 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2997 strcpy(*tempfilename, filename);
2998 strcat(*tempfilename, tempfile_suffix);
3001 const char *p = strrchr(filename, '/');
3007 if(0 == (*tempfilename = (char*)malloc(strlen(tempfile_path_prefix) + 1 + strlen(p) + strlen(tempfile_suffix) + 1))) {
3008 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
3011 strcpy(*tempfilename, tempfile_path_prefix);
3012 strcat(*tempfilename, "/");
3013 strcat(*tempfilename, p);
3014 strcat(*tempfilename, tempfile_suffix);
3017 if(0 == (*tempfile = fopen(*tempfilename, "w+b"))) {
3018 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
3025 FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
3027 FLAC__ASSERT(0 != filename);
3028 FLAC__ASSERT(0 != tempfile);
3029 FLAC__ASSERT(0 != *tempfile);
3030 FLAC__ASSERT(0 != tempfilename);
3031 FLAC__ASSERT(0 != *tempfilename);
3032 FLAC__ASSERT(0 != status);
3034 (void)fclose(*tempfile);
3037 #if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
3038 /* on some flavors of windows, rename() will fail if the destination already exists */
3039 if(unlink(filename) < 0) {
3040 cleanup_tempfile_(tempfile, tempfilename);
3041 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR;
3046 /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just rename(): */
3047 if(0 != rename(*tempfilename, filename)) {
3048 cleanup_tempfile_(tempfile, tempfilename);
3049 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR;
3053 cleanup_tempfile_(tempfile, tempfilename);
3058 void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
3060 if(0 != *tempfile) {
3061 (void)fclose(*tempfile);
3065 if(0 != *tempfilename) {
3066 (void)unlink(*tempfilename);
3067 free(*tempfilename);
3072 FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
3074 FLAC__ASSERT(0 != filename);
3075 FLAC__ASSERT(0 != stats);
3076 return (0 == stat(filename, stats));
3079 void set_file_stats_(const char *filename, struct stat *stats)
3081 struct utimbuf srctime;
3083 FLAC__ASSERT(0 != filename);
3084 FLAC__ASSERT(0 != stats);
3086 srctime.actime = stats->st_atime;
3087 srctime.modtime = stats->st_mtime;
3088 (void)chmod(filename, stats->st_mode);
3089 (void)utime(filename, &srctime);
3090 #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
3091 (void)chown(filename, stats->st_uid, -1);
3092 (void)chown(filename, -1, stats->st_gid);
3096 int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
3098 return fseeko((FILE*)handle, (off_t)offset, whence);
3101 FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle)
3103 return ftello((FILE*)handle);
3106 FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status)
3109 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK:
3110 return FLAC__METADATA_CHAIN_STATUS_OK;
3111 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
3112 return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT;
3113 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
3114 return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
3115 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
3116 return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
3117 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE:
3118 return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE;
3119 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA:
3120 return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
3121 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR:
3122 return FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
3123 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR:
3124 return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
3125 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR:
3126 return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
3127 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR:
3128 return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR;
3129 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR:
3130 return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR;
3131 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR:
3132 return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
3133 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR:
3135 return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;