1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #if !defined _MSC_VER && !defined __MINGW32__
21 /*@@@ don't know how to resolve errno without using LIBC.LIB; must use MSVCRT.LIB only for plugins */
28 #if defined _MSC_VER || defined __MINGW32__
29 #include <sys/utime.h> /* for utime() */
30 #include <io.h> /* for chmod() */
32 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
33 #include <utime.h> /* for utime() */
34 #include <unistd.h> /* for chown(), unlink() */
36 #include <sys/stat.h> /* for stat(), maybe chmod() */
38 #include "private/metadata.h"
40 #include "FLAC/assert.h"
41 #include "FLAC/file_decoder.h"
46 #define max(a,b) ((a)>(b)?(a):(b))
50 #define min(a,b) ((a)<(b)?(a):(b))
53 /****************************************************************************
55 * Local function declarations
57 ***************************************************************************/
59 static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
60 static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
61 static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes);
62 static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes);
63 static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes);
64 static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes);
66 static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator);
67 static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block);
68 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_(FILE *file, FLAC__StreamMetadata_StreamInfo *block);
69 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_(FILE *file, FLAC__StreamMetadata_Padding *block, unsigned block_length);
70 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_(FILE *file, FLAC__StreamMetadata_Application *block, unsigned block_length);
71 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_(FILE *file, FLAC__StreamMetadata_SeekTable *block, unsigned block_length);
72 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_(FILE *file, FLAC__StreamMetadata_VorbisComment_Entry *entry);
73 static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_(FILE *file, FLAC__StreamMetadata_VorbisComment *block);
75 static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
76 static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
77 static FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_streaminfo_(FILE *file, const FLAC__StreamMetadata_StreamInfo *block);
78 static FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_padding_(FILE *file, const FLAC__StreamMetadata_Padding *block, unsigned block_length);
79 static FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_application_(FILE *file, const FLAC__StreamMetadata_Application *block, unsigned block_length);
80 static FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_seektable_(FILE *file, const FLAC__StreamMetadata_SeekTable *block);
81 static FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_vorbis_comment_(FILE *file, const FLAC__StreamMetadata_VorbisComment *block);
82 static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block);
83 static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last);
84 static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append);
86 static FLAC__bool chain_rewrite_chain_(FLAC__Metadata_Chain *chain);
87 static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix);
89 static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator);
90 static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator);
92 /* return 0 if OK, 1 if read error, 2 if not a FLAC file */
93 static unsigned seek_to_first_metadata_block_(FILE *f);
95 static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append);
96 static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, long fixup_is_last_flag_offset, FLAC__bool backup);
98 static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, unsigned bytes/*@@@ 4G limit*/, FLAC__Metadata_SimpleIteratorStatus *status);
99 static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status);
101 static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
102 static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
103 static void cleanup_tempfile_(FILE **tempfile, char **tempfilename);
105 static FLAC__bool get_file_stats_(const char *filename, struct stat *stats);
106 static void set_file_stats_(const char *filename, struct stat *stats);
108 static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status);
111 /****************************************************************************
113 * Level 0 implementation
115 ***************************************************************************/
117 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
118 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
119 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
122 FLAC__bool got_error;
123 FLAC__bool got_streaminfo;
124 FLAC__StreamMetadata *streaminfo;
125 } level0_client_data;
127 FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo)
129 level0_client_data cd;
130 FLAC__FileDecoder *decoder;
132 FLAC__ASSERT(0 != filename);
133 FLAC__ASSERT(0 != streaminfo);
135 decoder = FLAC__file_decoder_new();
140 cd.got_error = false;
141 cd.got_streaminfo = false;
142 cd.streaminfo = streaminfo;
144 FLAC__file_decoder_set_md5_checking(decoder, false);
145 FLAC__file_decoder_set_filename(decoder, filename);
146 FLAC__file_decoder_set_metadata_ignore_all(decoder);
147 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
148 FLAC__file_decoder_set_write_callback(decoder, write_callback_);
149 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
150 FLAC__file_decoder_set_error_callback(decoder, error_callback_);
151 FLAC__file_decoder_set_client_data(decoder, &cd);
153 if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK || cd.got_error) {
154 FLAC__file_decoder_finish(decoder);
155 FLAC__file_decoder_delete(decoder);
159 /* the first thing decoded must be the STREAMINFO block: */
160 if(!FLAC__file_decoder_process_until_end_of_metadata(decoder) || cd.got_error) {
161 FLAC__file_decoder_finish(decoder);
162 FLAC__file_decoder_delete(decoder);
166 FLAC__file_decoder_finish(decoder);
167 FLAC__file_decoder_delete(decoder);
169 return !cd.got_error && cd.got_streaminfo;
172 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
174 (void)decoder, (void)frame, (void)buffer, (void)client_data;
176 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
179 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
181 level0_client_data *cd = (level0_client_data *)client_data;
184 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO && 0 != cd->streaminfo) {
185 *(cd->streaminfo) = *metadata;
186 cd->got_streaminfo = true;
190 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
192 level0_client_data *cd = (level0_client_data *)client_data;
195 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
196 cd->got_error = true;
200 /****************************************************************************
202 * Level 1 implementation
204 ***************************************************************************/
206 #define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4)
207 /* 1 for initial offset, +4 for our own personal use */
209 struct FLAC__Metadata_SimpleIterator {
211 char *filename, *tempfile_path_prefix;
213 FLAC__bool has_stats;
214 FLAC__bool is_writable;
215 FLAC__Metadata_SimpleIteratorStatus status;
216 /*@@@ 2G limits here because of the offset type: */
217 long offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH];
218 long first_offset; /* this is the offset to the STREAMINFO block */
220 /* this is the metadata block header of the current block we are pointing to: */
222 FLAC__MetadataType type;
226 const char * const FLAC__Metadata_SimpleIteratorStatusString[] = {
227 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK",
228 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT",
229 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE",
230 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE",
231 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE",
232 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA",
233 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR",
234 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR",
235 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR",
236 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR",
237 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR",
238 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR",
239 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR"
243 FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new()
245 FLAC__Metadata_SimpleIterator *iterator = malloc(sizeof(FLAC__Metadata_SimpleIterator));
249 iterator->filename = 0;
250 iterator->tempfile_path_prefix = 0;
251 iterator->has_stats = false;
252 iterator->is_writable = false;
253 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
254 iterator->first_offset = iterator->offset[0] = -1;
261 static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator)
263 FLAC__ASSERT(0 != iterator);
265 if(0 != iterator->file) {
266 fclose(iterator->file);
268 if(iterator->has_stats)
269 set_file_stats_(iterator->filename, &iterator->stats);
271 if(0 != iterator->filename) {
272 free(iterator->filename);
273 iterator->filename = 0;
275 if(0 != iterator->tempfile_path_prefix) {
276 free(iterator->tempfile_path_prefix);
277 iterator->tempfile_path_prefix = 0;
281 void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator)
283 FLAC__ASSERT(0 != iterator);
285 simple_iterator_free_guts_(iterator);
289 FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator)
291 FLAC__Metadata_SimpleIteratorStatus status;
293 FLAC__ASSERT(0 != iterator);
295 status = iterator->status;
296 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
300 static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator)
304 FLAC__ASSERT(0 != iterator);
306 iterator->is_writable = false;
308 if(0 == (iterator->file = fopen(iterator->filename, "r+b"))) {
309 #if !defined _MSC_VER && !defined __MINGW32__
310 /*@@@ don't know how to resolve errno without using LIBC.LIB; must use MSVCRT.LIB only for plugins */
311 if(errno == EACCES) {
313 if(0 == (iterator->file = fopen(iterator->filename, "rb"))) {
314 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
317 #if !defined _MSC_VER && !defined __MINGW32__
320 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
326 iterator->is_writable = true;
329 ret = seek_to_first_metadata_block_(iterator->file);
333 iterator->first_offset = iterator->offset[iterator->depth] = ftell(iterator->file);
334 return read_metadata_block_header_(iterator);
336 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
339 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE;
348 @@@ If we decide to finish implementing this, put this comment back in metadata.h
350 * The 'tempfile_path_prefix' allows you to specify a directory where
351 * tempfiles should go. Remember that if your metadata edits cause the
352 * FLAC file to grow, the entire file will have to be rewritten. If
353 * 'tempfile_path_prefix' is NULL, the temp file will be written in the
354 * same directory as the original FLAC file. This makes replacing the
355 * original with the tempfile fast but requires extra space in the same
356 * partition for the tempfile. If space is a problem, you can pass a
357 * directory name belonging to a different partition in
358 * 'tempfile_path_prefix'. Note that you should use the forward slash
359 * '/' as the directory separator. A trailing slash is not needed; it
360 * will be added automatically.
362 FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix);
365 FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats)
367 const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'rename(...)' for what it will take to finish implementing this */
369 FLAC__ASSERT(0 != iterator);
370 FLAC__ASSERT(0 != filename);
372 simple_iterator_free_guts_(iterator);
374 if(preserve_file_stats)
375 iterator->has_stats = get_file_stats_(filename, &iterator->stats);
377 if(0 == (iterator->filename = strdup(filename))) {
378 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
381 if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) {
382 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
386 return simple_iterator_prime_input_(iterator);
389 FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator)
391 FLAC__ASSERT(0 != iterator);
392 FLAC__ASSERT(0 != iterator->file);
394 return iterator->is_writable;
397 FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator)
399 FLAC__ASSERT(0 != iterator);
400 FLAC__ASSERT(0 != iterator->file);
402 if(iterator->is_last)
405 if(0 != fseek(iterator->file, iterator->length, SEEK_CUR)) {
406 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
410 iterator->offset[iterator->depth] = ftell(iterator->file);
412 return read_metadata_block_header_(iterator);
415 FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator)
419 FLAC__ASSERT(0 != iterator);
420 FLAC__ASSERT(0 != iterator->file);
422 if(iterator->offset[iterator->depth] == iterator->first_offset)
425 if(0 != fseek(iterator->file, iterator->first_offset, SEEK_SET)) {
426 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
429 this_offset = iterator->first_offset;
430 if(!read_metadata_block_header_(iterator))
433 /* we ignore any error from ftell() and catch it in fseek() */
434 while(ftell(iterator->file) + (long)iterator->length < iterator->offset[iterator->depth]) {
435 if(0 != fseek(iterator->file, iterator->length, SEEK_CUR)) {
436 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
439 this_offset = ftell(iterator->file);
440 if(!read_metadata_block_header_(iterator))
444 iterator->offset[iterator->depth] = this_offset;
449 FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator)
451 FLAC__ASSERT(0 != iterator);
452 FLAC__ASSERT(0 != iterator->file);
454 return iterator->type;
457 FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator)
459 FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type);
461 FLAC__ASSERT(0 != iterator);
462 FLAC__ASSERT(0 != iterator->file);
465 block->is_last = iterator->is_last;
466 block->length = iterator->length;
468 if(!read_metadata_block_data_(iterator, block)) {
469 FLAC__metadata_object_delete(block);
473 /* back up to the beginning of the block data to stay consistent */
474 if(0 != fseek(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) {
475 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
476 FLAC__metadata_object_delete(block);
481 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
486 FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
488 FLAC__ASSERT_DECLARATION(long debug_target_offset = iterator->offset[iterator->depth];)
491 FLAC__ASSERT(0 != iterator);
492 FLAC__ASSERT(0 != iterator->file);
493 FLAC__ASSERT(0 != block);
495 if(!iterator->is_writable) {
496 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
500 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) {
501 if(iterator->type != block->type) {
502 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
507 block->is_last = iterator->is_last;
509 if(iterator->length == block->length)
510 return write_metadata_block_stationary_(iterator, block);
511 else if(iterator->length > block->length) {
512 if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) {
513 ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last);
514 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
515 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
519 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
520 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
521 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
525 else /* iterator->length < block->length */ {
526 unsigned padding_leftover = 0;
527 FLAC__bool padding_is_last = false;
529 /* first see if we can even use padding */
530 if(iterator->is_last) {
534 const unsigned extra_padding_bytes_required = block->length - iterator->length;
535 simple_iterator_push_(iterator);
536 if(!FLAC__metadata_simple_iterator_next(iterator)) {
537 (void)simple_iterator_pop_(iterator);
540 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
544 if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) {
545 padding_leftover = 0;
546 block->is_last = iterator->is_last;
548 else if(iterator->length < extra_padding_bytes_required)
551 padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required;
552 padding_is_last = iterator->is_last;
553 block->is_last = false;
556 if(!simple_iterator_pop_(iterator))
561 if(padding_leftover == 0) {
562 ret = write_metadata_block_stationary_(iterator, block);
563 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
564 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
568 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
569 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
570 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
571 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
576 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
577 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
578 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
584 FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
586 unsigned padding_leftover = 0;
587 FLAC__bool padding_is_last = false;
589 FLAC__ASSERT_DECLARATION(long debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;)
592 FLAC__ASSERT(0 != iterator);
593 FLAC__ASSERT(0 != iterator->file);
594 FLAC__ASSERT(0 != block);
596 if(!iterator->is_writable)
599 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
600 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
604 block->is_last = iterator->is_last;
607 /* first see if we can even use padding */
608 if(iterator->is_last) {
612 simple_iterator_push_(iterator);
613 if(!FLAC__metadata_simple_iterator_next(iterator)) {
614 (void)simple_iterator_pop_(iterator);
617 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
621 if(iterator->length == block->length) {
622 padding_leftover = 0;
623 block->is_last = iterator->is_last;
625 else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length)
628 padding_leftover = iterator->length - block->length;
629 padding_is_last = iterator->is_last;
630 block->is_last = false;
633 if(!simple_iterator_pop_(iterator))
638 /* move to the next block, which is suitable padding */
639 if(!FLAC__metadata_simple_iterator_next(iterator))
641 if(padding_leftover == 0) {
642 ret = write_metadata_block_stationary_(iterator, block);
643 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
644 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
648 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
649 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
650 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
651 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
656 ret = rewrite_whole_file_(iterator, block, /*append=*/true);
657 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
658 FLAC__ASSERT(ftell(iterator->file) == debug_target_offset + (long)FLAC__STREAM_METADATA_HEADER_LENGTH);
663 FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)
665 FLAC__ASSERT_DECLARATION(long debug_target_offset = iterator->offset[iterator->depth];)
668 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
669 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
674 FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
676 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
679 padding->length = iterator->length;
680 if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) {
681 FLAC__metadata_object_delete(padding);
684 FLAC__metadata_object_delete(padding);
685 if(!FLAC__metadata_simple_iterator_prev(iterator))
687 FLAC__ASSERT(iterator->offset[iterator->depth] + (long)FLAC__STREAM_METADATA_HEADER_LENGTH + (long)iterator->length == debug_target_offset);
688 FLAC__ASSERT(ftell(iterator->file) + (long)iterator->length == debug_target_offset);
692 ret = rewrite_whole_file_(iterator, 0, /*append=*/false);
693 FLAC__ASSERT(iterator->offset[iterator->depth] + (long)FLAC__STREAM_METADATA_HEADER_LENGTH + (long)iterator->length == debug_target_offset);
694 FLAC__ASSERT(ftell(iterator->file) + (long)iterator->length == debug_target_offset);
701 /****************************************************************************
703 * Level 2 implementation
705 ***************************************************************************/
708 typedef struct FLAC__Metadata_Node {
709 FLAC__StreamMetadata *data;
710 struct FLAC__Metadata_Node *prev, *next;
711 } FLAC__Metadata_Node;
713 struct FLAC__Metadata_Chain {
715 FLAC__Metadata_Node *head;
716 FLAC__Metadata_Node *tail;
718 FLAC__Metadata_ChainStatus status;
719 long first_offset, last_offset; /*@@@ 2G limit */
721 * This is the length of the chain initially read from the FLAC file.
722 * it is used to compare against the current length to decide whether
723 * or not the whole file has to be rewritten.
725 unsigned initial_length; /*@@@ 4G limit */
728 struct FLAC__Metadata_Iterator {
729 FLAC__Metadata_Chain *chain;
730 FLAC__Metadata_Node *current;
733 const char * const FLAC__Metadata_ChainStatusString[] = {
734 "FLAC__METADATA_CHAIN_STATUS_OK",
735 "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT",
736 "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE",
737 "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE",
738 "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE",
739 "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA",
740 "FLAC__METADATA_CHAIN_STATUS_READ_ERROR",
741 "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR",
742 "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR",
743 "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR",
744 "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR",
745 "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR",
746 "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR"
750 static FLAC__Metadata_Node *node_new_()
752 FLAC__Metadata_Node *node = (FLAC__Metadata_Node*)malloc(sizeof(FLAC__Metadata_Node));
754 memset(node, 0, sizeof(FLAC__Metadata_Node));
758 static void node_delete_(FLAC__Metadata_Node *node)
760 FLAC__ASSERT(0 != node);
762 FLAC__metadata_object_delete(node->data);
766 static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
768 FLAC__ASSERT(0 != chain);
769 FLAC__ASSERT(0 != node);
770 FLAC__ASSERT(0 != node->data);
772 node->next = node->prev = 0;
773 node->data->is_last = true;
775 chain->tail->data->is_last = false;
780 FLAC__ASSERT(0 != chain->tail);
781 chain->tail->next = node;
782 node->prev = chain->tail;
788 static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
790 FLAC__ASSERT(0 != chain);
791 FLAC__ASSERT(0 != node);
793 if(node == chain->head)
794 chain->head = node->next;
796 node->prev->next = node->next;
798 if(node == chain->tail)
799 chain->tail = node->prev;
801 node->next->prev = node->prev;
804 chain->tail->data->is_last = true;
809 static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
811 chain_remove_node_(chain, node);
815 static unsigned chain_calculate_length_(FLAC__Metadata_Chain *chain)
817 const FLAC__Metadata_Node *node;
819 for(node = chain->head; node; node = node->next)
820 length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
824 static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
826 FLAC__ASSERT(0 != node);
827 FLAC__ASSERT(0 != node->data);
828 FLAC__ASSERT(0 != iterator);
829 FLAC__ASSERT(0 != iterator->current);
830 FLAC__ASSERT(0 != iterator->chain);
831 FLAC__ASSERT(0 != iterator->chain->head);
832 FLAC__ASSERT(0 != iterator->chain->tail);
834 node->data->is_last = false;
836 node->prev = iterator->current->prev;
837 node->next = iterator->current;
840 iterator->chain->head = node;
842 node->prev->next = node;
844 iterator->current->prev = node;
846 iterator->chain->nodes++;
849 static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
851 FLAC__ASSERT(0 != node);
852 FLAC__ASSERT(0 != node->data);
853 FLAC__ASSERT(0 != iterator);
854 FLAC__ASSERT(0 != iterator->current);
855 FLAC__ASSERT(0 != iterator->chain);
856 FLAC__ASSERT(0 != iterator->chain->head);
857 FLAC__ASSERT(0 != iterator->chain->tail);
859 iterator->current->data->is_last = false;
861 node->prev = iterator->current;
862 node->next = iterator->current->next;
865 iterator->chain->tail = node;
867 node->next->prev = node;
869 node->prev->next = node;
871 iterator->chain->tail->data->is_last = true;
873 iterator->chain->nodes++;
876 /* return true iff node and node->next are both padding */
877 static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
879 if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) {
880 const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
881 node->data->length += growth;
883 chain_delete_node_(chain, node->next);
890 FLAC__Metadata_Chain *FLAC__metadata_chain_new()
892 FLAC__Metadata_Chain *chain = malloc(sizeof(FLAC__Metadata_Chain));
896 chain->head = chain->tail = 0;
898 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
899 chain->initial_length = 0;
905 void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain)
907 FLAC__Metadata_Node *node, *next;
909 FLAC__ASSERT(0 != chain);
911 for(node = chain->head; node; ) {
917 if(0 != chain->filename)
918 free(chain->filename);
923 FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain)
925 FLAC__Metadata_ChainStatus status;
927 FLAC__ASSERT(0 != chain);
929 status = chain->status;
930 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
934 FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
936 FLAC__Metadata_SimpleIterator *iterator;
937 FLAC__Metadata_Node *node;
939 FLAC__ASSERT(0 != chain);
940 FLAC__ASSERT(0 != filename);
942 if(0 == (chain->filename = strdup(filename))) {
943 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
947 if(0 == (iterator = FLAC__metadata_simple_iterator_new())) {
948 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
952 if(!FLAC__metadata_simple_iterator_init(iterator, filename, /*preserve_file_stats=*/false)) {
953 chain->status = get_equivalent_status_(iterator->status);
957 chain->first_offset = iterator->offset[iterator->depth];
962 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
965 node->data = FLAC__metadata_simple_iterator_get_block(iterator);
966 if(0 == node->data) {
968 chain->status = get_equivalent_status_(iterator->status);
971 chain_append_node_(chain, node);
972 } while(FLAC__metadata_simple_iterator_next(iterator));
974 if(!iterator->is_last || iterator->status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) {
975 chain->status = get_equivalent_status_(iterator->status);
979 chain->last_offset = ftell(iterator->file) + iterator->length;
980 FLAC__metadata_simple_iterator_delete(iterator);
982 chain->initial_length = chain_calculate_length_(chain);
986 FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats)
989 const char *tempfile_path_prefix = 0;
990 unsigned current_length;
992 FLAC__ASSERT(0 != chain);
994 current_length = chain_calculate_length_(chain);
997 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
998 const unsigned delta = chain->initial_length - current_length;
999 chain->tail->data->length += delta;
1000 current_length += delta;
1001 FLAC__ASSERT(current_length == chain->initial_length);
1003 else if(current_length + FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
1004 FLAC__StreamMetadata *padding;
1005 FLAC__Metadata_Node *node;
1006 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) {
1007 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1010 padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
1011 if(0 == (node = node_new_())) {
1012 FLAC__metadata_object_delete(padding);
1013 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1016 node->data = padding;
1017 chain_append_node_(chain, node);
1018 current_length = chain_calculate_length_(chain);
1019 FLAC__ASSERT(current_length == chain->initial_length);
1021 else if(current_length > chain->initial_length) {
1022 const unsigned delta = current_length - chain->initial_length;
1023 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1024 if(chain->tail->data->length + FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
1025 chain_delete_node_(chain, chain->tail);
1026 current_length = chain_calculate_length_(chain);
1027 FLAC__ASSERT(current_length == chain->initial_length);
1029 else if(chain->tail->data->length >= delta) {
1030 chain->tail->data->length -= delta;
1031 current_length -= delta;
1032 FLAC__ASSERT(current_length == chain->initial_length);
1038 if(preserve_file_stats)
1039 get_file_stats_(chain->filename, &stats);
1041 if(current_length == chain->initial_length) {
1042 if(!chain_rewrite_chain_(chain))
1046 if(!chain_rewrite_file_(chain, tempfile_path_prefix))
1050 if(preserve_file_stats)
1051 set_file_stats_(chain->filename, &stats);
1053 /* recompute lengths and offsets if necessary */
1054 if(chain->initial_length != current_length) {
1055 const FLAC__Metadata_Node *node;
1056 chain->initial_length = current_length;
1057 chain->last_offset = chain->first_offset;
1058 for(node = chain->head; node; node = node->next)
1059 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1065 void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain)
1067 FLAC__Metadata_Node *node;
1069 FLAC__ASSERT(0 != chain);
1071 for(node = chain->head; node; ) {
1072 if(!chain_merge_adjacent_padding_(chain, node))
1077 void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
1079 FLAC__Metadata_Node *node, *save;
1082 FLAC__ASSERT(0 != chain);
1085 * Don't try and be too smart... this simple algo is good enough for
1086 * the small number of nodes that we deal with.
1088 for(i = 0, node = chain->head; i < chain->nodes; i++) {
1089 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1091 chain_remove_node_(chain, node);
1092 chain_append_node_(chain, node);
1100 FLAC__metadata_chain_merge_padding(chain);
1104 FLAC__Metadata_Iterator *FLAC__metadata_iterator_new()
1106 FLAC__Metadata_Iterator *iterator = malloc(sizeof(FLAC__Metadata_Iterator));
1109 iterator->current = 0;
1110 iterator->chain = 0;
1116 void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator)
1118 FLAC__ASSERT(0 != iterator);
1123 void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain)
1125 FLAC__ASSERT(0 != iterator);
1126 FLAC__ASSERT(0 != chain);
1127 FLAC__ASSERT(0 != chain->head);
1129 iterator->chain = chain;
1130 iterator->current = chain->head;
1133 FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator)
1135 FLAC__ASSERT(0 != iterator);
1137 if(0 == iterator->current || 0 == iterator->current->next)
1140 iterator->current = iterator->current->next;
1144 FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator)
1146 FLAC__ASSERT(0 != iterator);
1148 if(0 == iterator->current || 0 == iterator->current->prev)
1151 iterator->current = iterator->current->prev;
1155 FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator)
1157 FLAC__ASSERT(0 != iterator);
1158 FLAC__ASSERT(0 != iterator->current);
1159 FLAC__ASSERT(0 != iterator->current->data);
1161 return iterator->current->data->type;
1164 FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator)
1166 FLAC__ASSERT(0 != iterator);
1167 FLAC__ASSERT(0 != iterator->current);
1169 return iterator->current->data;
1172 FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1174 FLAC__ASSERT(0 != iterator);
1175 FLAC__ASSERT(0 != block);
1176 return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block);
1179 FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding)
1181 FLAC__Metadata_Node *save;
1183 FLAC__ASSERT(0 != iterator);
1184 FLAC__ASSERT(0 != iterator->current);
1186 if(0 == iterator->current->prev) {
1187 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1191 save = iterator->current->prev;
1193 if(replace_with_padding) {
1194 FLAC__metadata_object_delete_data(iterator->current->data);
1195 iterator->current->data->type = FLAC__METADATA_TYPE_PADDING;
1198 chain_delete_node_(iterator->chain, iterator->current);
1201 iterator->current = save;
1205 FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1207 FLAC__Metadata_Node *node;
1209 FLAC__ASSERT(0 != iterator);
1210 FLAC__ASSERT(0 != iterator->current);
1211 FLAC__ASSERT(0 != block);
1213 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1216 if(0 == iterator->current->prev) {
1217 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1221 if(0 == (node = node_new_()))
1225 iterator_insert_node_(iterator, node);
1226 iterator->current = node;
1230 FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
1232 FLAC__Metadata_Node *node;
1234 FLAC__ASSERT(0 != iterator);
1235 FLAC__ASSERT(0 != iterator->current);
1236 FLAC__ASSERT(0 != block);
1238 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1241 if(0 == (node = node_new_()))
1245 iterator_insert_node_after_(iterator, node);
1246 iterator->current = node;
1251 /****************************************************************************
1253 * Local function definitions
1255 ***************************************************************************/
1257 void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1263 for(i = 0; i < bytes; i++) {
1264 *(--b) = (FLAC__byte)(val & 0xff);
1269 void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1273 for(i = 0; i < bytes; i++) {
1274 *(b++) = (FLAC__byte)(val & 0xff);
1279 void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes)
1285 for(i = 0; i < bytes; i++) {
1286 *(--b) = (FLAC__byte)(val & 0xff);
1291 FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes)
1293 FLAC__uint32 ret = 0;
1296 for(i = 0; i < bytes; i++)
1297 ret = (ret << 8) | (FLAC__uint32)(*b++);
1302 FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes)
1304 FLAC__uint32 ret = 0;
1309 for(i = 0; i < bytes; i++)
1310 ret = (ret << 8) | (FLAC__uint32)(*--b);
1315 FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes)
1317 FLAC__uint64 ret = 0;
1320 for(i = 0; i < bytes; i++)
1321 ret = (ret << 8) | (FLAC__uint64)(*b++);
1326 FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator)
1328 FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH];
1330 FLAC__ASSERT(0 != iterator);
1331 FLAC__ASSERT(0 != iterator->file);
1333 if(fread(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, iterator->file) != FLAC__STREAM_METADATA_HEADER_LENGTH) {
1334 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1338 iterator->is_last = raw_header[0] & 0x80? true : false;
1339 iterator->type = (FLAC__MetadataType)(raw_header[0] & 0x7f);
1340 iterator->length = unpack_uint32_(raw_header + 1, 3);
1342 /* do some checking */
1343 if(iterator->type > FLAC__METADATA_TYPE_VORBIS_COMMENT) {
1344 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA;
1351 FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block)
1353 FLAC__ASSERT(0 != iterator);
1354 FLAC__ASSERT(0 != iterator->file);
1356 switch(block->type) {
1357 case FLAC__METADATA_TYPE_STREAMINFO:
1358 iterator->status = read_metadata_block_data_streaminfo_(iterator->file, &block->data.stream_info);
1360 case FLAC__METADATA_TYPE_PADDING:
1361 iterator->status = read_metadata_block_data_padding_(iterator->file, &block->data.padding, block->length);
1363 case FLAC__METADATA_TYPE_APPLICATION:
1364 iterator->status = read_metadata_block_data_application_(iterator->file, &block->data.application, block->length);
1366 case FLAC__METADATA_TYPE_SEEKTABLE:
1367 iterator->status = read_metadata_block_data_seektable_(iterator->file, &block->data.seek_table, block->length);
1369 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1370 iterator->status = read_metadata_block_data_vorbis_comment_(iterator->file, &block->data.vorbis_comment);
1374 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR;
1377 return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
1380 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_(FILE *file, FLAC__StreamMetadata_StreamInfo *block)
1382 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b;
1384 FLAC__ASSERT(0 != file);
1386 if(fread(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, file) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
1387 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1391 /* @@@ we are using hardcoded numbers for simplicity but we should
1392 * probably eventually write a bit-level unpacker and use the
1393 * _STREAMINFO_ constants.
1395 block->min_blocksize = unpack_uint32_(b, 2); b += 2;
1396 block->max_blocksize = unpack_uint32_(b, 2); b += 2;
1397 block->min_framesize = unpack_uint32_(b, 3); b += 3;
1398 block->max_framesize = unpack_uint32_(b, 3); b += 3;
1399 block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
1400 block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
1401 block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
1402 block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4);
1403 memcpy(block->md5sum, b+8, 16);
1405 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1409 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_(FILE *file, FLAC__StreamMetadata_Padding *block, unsigned block_length)
1411 FLAC__ASSERT(0 != file);
1413 (void)block; /* nothing to do; we don't care about reading the padding bytes */
1415 if(0 != fseek(file, block_length, SEEK_CUR))
1416 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
1418 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1421 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_(FILE *file, FLAC__StreamMetadata_Application *block, unsigned block_length)
1423 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
1425 FLAC__ASSERT(0 != file);
1427 if(fread(block->id, 1, id_bytes, file) != id_bytes)
1428 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1430 block_length -= id_bytes;
1432 if(block_length == 0) {
1436 if(0 == (block->data = malloc(block_length)))
1437 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
1439 if(fread(block->data, 1, block_length, file) != block_length)
1440 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1443 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1446 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_(FILE *file, FLAC__StreamMetadata_SeekTable *block, unsigned block_length)
1449 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
1451 FLAC__ASSERT(0 != file);
1452 FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0);
1454 block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1456 if(block->num_points == 0)
1458 else if(0 == (block->points = malloc(block->num_points * sizeof(FLAC__StreamMetadata_SeekPoint))))
1459 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
1461 for(i = 0; i < block->num_points; i++) {
1462 if(fread(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, file) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
1463 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1464 /*@@@ some MAGIC NUMBERs here */
1465 block->points[i].sample_number = unpack_uint64_(buffer, 8);
1466 block->points[i].stream_offset = unpack_uint64_(buffer+8, 8);
1467 block->points[i].frame_samples = unpack_uint32_(buffer+16, 2);
1470 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1473 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_(FILE *file, FLAC__StreamMetadata_VorbisComment_Entry *entry)
1475 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
1476 FLAC__byte buffer[4]; /* magic number is asserted below */
1478 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == 4);
1479 FLAC__ASSERT(0 != file);
1481 if(fread(buffer, 1, entry_length_len, file) != entry_length_len)
1482 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1483 entry->length = unpack_uint32_little_endian_(buffer, entry_length_len);
1485 if(entry->length == 0) {
1489 if(0 == (entry->entry = malloc(entry->length)))
1490 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
1492 if(fread(entry->entry, 1, entry->length, file) != entry->length)
1493 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1496 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1499 FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_(FILE *file, FLAC__StreamMetadata_VorbisComment *block)
1502 FLAC__Metadata_SimpleIteratorStatus status;
1503 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
1504 FLAC__byte buffer[4]; /* magic number is asserted below */
1506 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == 4);
1507 FLAC__ASSERT(0 != file);
1509 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_(file, &(block->vendor_string))))
1512 if(fread(buffer, 1, num_comments_len, file) != num_comments_len)
1513 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
1514 block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len);
1516 if(block->num_comments == 0) {
1517 block->comments = 0;
1519 else if(0 == (block->comments = malloc(block->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry))))
1520 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
1522 for(i = 0; i < block->num_comments; i++) {
1523 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_(file, block->comments + i)))
1527 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1530 FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
1532 FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];
1534 FLAC__ASSERT(0 != file);
1535 FLAC__ASSERT(0 != status);
1536 FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
1538 buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
1539 pack_uint32_(block->length, buffer + 1, 3);
1541 if(fwrite(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, file) != FLAC__STREAM_METADATA_HEADER_LENGTH) {
1542 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1549 FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
1551 FLAC__ASSERT(0 != file);
1552 FLAC__ASSERT(0 != status);
1554 switch(block->type) {
1555 case FLAC__METADATA_TYPE_STREAMINFO:
1556 *status = write_metadata_block_data_streaminfo_(file, &block->data.stream_info);
1558 case FLAC__METADATA_TYPE_PADDING:
1559 *status = write_metadata_block_data_padding_(file, &block->data.padding, block->length);
1561 case FLAC__METADATA_TYPE_APPLICATION:
1562 *status = write_metadata_block_data_application_(file, &block->data.application, block->length);
1564 case FLAC__METADATA_TYPE_SEEKTABLE:
1565 *status = write_metadata_block_data_seektable_(file, &block->data.seek_table);
1567 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1568 *status = write_metadata_block_data_vorbis_comment_(file, &block->data.vorbis_comment);
1572 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR;
1574 return (*status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
1577 FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_streaminfo_(FILE *file, const FLAC__StreamMetadata_StreamInfo *block)
1579 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
1580 const unsigned channels1 = block->channels - 1;
1581 const unsigned bps1 = block->bits_per_sample - 1;
1583 FLAC__ASSERT(0 != file);
1585 /* @@@ we are using hardcoded numbers for simplicity but we should
1586 * probably eventually write a bit-level packer and use the
1587 * _STREAMINFO_ constants.
1589 pack_uint32_(block->min_blocksize, buffer, 2);
1590 pack_uint32_(block->max_blocksize, buffer+2, 2);
1591 pack_uint32_(block->min_framesize, buffer+4, 3);
1592 pack_uint32_(block->max_framesize, buffer+7, 3);
1593 buffer[10] = (block->sample_rate >> 12) & 0xff;
1594 buffer[11] = (block->sample_rate >> 4) & 0xff;
1595 buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4);
1596 buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f));
1597 pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
1598 memcpy(buffer+18, block->md5sum, 16);
1600 if(fwrite(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, file) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
1601 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1603 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1606 FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_padding_(FILE *file, const FLAC__StreamMetadata_Padding *block, unsigned block_length)
1608 unsigned i, n = block_length;
1609 FLAC__byte buffer[1024];
1611 FLAC__ASSERT(0 != file);
1615 memset(buffer, 0, 1024);
1617 for(i = 0; i < n/1024; i++)
1618 if(fwrite(buffer, 1, 1024, file) != 1024)
1619 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1623 if(fwrite(buffer, 1, n, file) != n)
1624 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1626 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1629 FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_application_(FILE *file, const FLAC__StreamMetadata_Application *block, unsigned block_length)
1631 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
1633 FLAC__ASSERT(0 != file);
1635 if(fwrite(block->id, 1, id_bytes, file) != id_bytes)
1636 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1638 block_length -= id_bytes;
1640 if(fwrite(block->data, 1, block_length, file) != block_length)
1641 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1643 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1646 FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_seektable_(FILE *file, const FLAC__StreamMetadata_SeekTable *block)
1649 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
1651 FLAC__ASSERT(0 != file);
1653 for(i = 0; i < block->num_points; i++) {
1654 /*@@@ some MAGIC NUMBERs here */
1655 pack_uint64_(block->points[i].sample_number, buffer, 8);
1656 pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
1657 pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
1658 if(fwrite(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, file) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
1659 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1662 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1665 FLAC__Metadata_SimpleIteratorStatus write_metadata_block_data_vorbis_comment_(FILE *file, const FLAC__StreamMetadata_VorbisComment *block)
1668 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
1669 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
1670 FLAC__byte buffer[4]; /* magic number is asserted below */
1672 FLAC__ASSERT(max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == 4);
1673 FLAC__ASSERT(0 != file);
1675 pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
1676 if(fwrite(buffer, 1, entry_length_len, file) != entry_length_len)
1677 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1678 if(fwrite(block->vendor_string.entry, 1, block->vendor_string.length, file) != block->vendor_string.length)
1679 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1681 pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
1682 if(fwrite(buffer, 1, num_comments_len, file) != num_comments_len)
1683 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1685 for(i = 0; i < block->num_comments; i++) {
1686 pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
1687 if(fwrite(buffer, 1, entry_length_len, file) != entry_length_len)
1688 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1689 if(fwrite(block->comments[i].entry, 1, block->comments[i].length, file) != block->comments[i].length)
1690 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
1693 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
1696 FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block)
1698 if(0 != fseek(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
1699 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
1703 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
1706 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
1709 if(0 != fseek(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
1710 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
1714 return read_metadata_block_header_(iterator);
1717 FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last)
1719 FLAC__StreamMetadata *padding;
1721 if(0 != fseek(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
1722 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
1726 block->is_last = false;
1728 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
1731 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
1734 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
1735 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
1737 padding->is_last = padding_is_last;
1738 padding->length = padding_length;
1740 if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) {
1741 FLAC__metadata_object_delete(padding);
1745 if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) {
1746 FLAC__metadata_object_delete(padding);
1750 FLAC__metadata_object_delete(padding);
1752 if(0 != fseek(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
1753 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
1757 return read_metadata_block_header_(iterator);
1760 FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append)
1764 int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */
1765 long fixup_is_last_flag_offset = -1;
1767 FLAC__ASSERT(0 != block || append == false);
1769 if(iterator->is_last) {
1771 fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */
1772 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
1774 else if(0 == block) {
1775 simple_iterator_push_(iterator);
1776 if(!FLAC__metadata_simple_iterator_prev(iterator)) {
1777 (void)simple_iterator_pop_(iterator);
1780 fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */
1781 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
1782 if(!simple_iterator_pop_(iterator))
1787 if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append))
1791 if(!write_metadata_block_header_(tempfile, &iterator->status, block)) {
1792 cleanup_tempfile_(&tempfile, &tempfilename);
1796 if(!write_metadata_block_data_(tempfile, &iterator->status, block)) {
1797 cleanup_tempfile_(&tempfile, &tempfilename);
1802 if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0))
1806 return FLAC__metadata_simple_iterator_next(iterator);
1811 FLAC__bool chain_rewrite_chain_(FLAC__Metadata_Chain *chain)
1814 FLAC__Metadata_Node *node;
1815 FLAC__Metadata_SimpleIteratorStatus status;
1817 FLAC__ASSERT(0 != chain);
1818 FLAC__ASSERT(0 != chain->filename);
1819 FLAC__ASSERT(0 != chain->head);
1821 if(0 == (f = fopen(chain->filename, "r+b"))) {
1822 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1825 if(0 != fseek(f, chain->first_offset, SEEK_SET)) {
1826 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1830 for(node = chain->head; node; node = node->next) {
1831 if(!write_metadata_block_header_(f, &status, node->data)) {
1832 chain->status = get_equivalent_status_(status);
1835 if(!write_metadata_block_data_(f, &status, node->data)) {
1836 chain->status = get_equivalent_status_(status);
1841 /*FLAC__ASSERT(fflush(), ftell() == chain->last_offset);*/
1848 FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
1852 FLAC__Metadata_SimpleIteratorStatus status;
1853 const FLAC__Metadata_Node *node;
1855 FLAC__ASSERT(0 != chain);
1856 FLAC__ASSERT(0 != chain->filename);
1857 FLAC__ASSERT(0 != chain->head);
1859 /* copy the file prefix (data up to first metadata block */
1860 if(0 == (f = fopen(chain->filename, "rb"))) {
1861 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1864 if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) {
1865 chain->status = get_equivalent_status_(status);
1866 cleanup_tempfile_(&tempfile, &tempfilename);
1869 if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) {
1870 chain->status = get_equivalent_status_(status);
1871 cleanup_tempfile_(&tempfile, &tempfilename);
1875 /* write the metadata */
1876 for(node = chain->head; node; node = node->next) {
1877 if(!write_metadata_block_header_(tempfile, &status, node->data)) {
1878 chain->status = get_equivalent_status_(status);
1881 if(!write_metadata_block_data_(tempfile, &status, node->data)) {
1882 chain->status = get_equivalent_status_(status);
1886 /*FLAC__ASSERT(fflush(), ftell() == chain->last_offset);*/
1888 /* copy the file postfix (everything after the metadata) */
1889 if(0 != fseek(f, chain->last_offset, SEEK_SET)) {
1890 cleanup_tempfile_(&tempfile, &tempfilename);
1891 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1894 if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) {
1895 cleanup_tempfile_(&tempfile, &tempfilename);
1896 chain->status = get_equivalent_status_(status);
1900 /* move the tempfile on top of the original */
1902 if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status))
1908 void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator)
1910 FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH);
1911 iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth];
1915 FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator)
1917 FLAC__ASSERT(iterator->depth > 0);
1919 if(0 != fseek(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
1920 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
1924 return read_metadata_block_header_(iterator);
1927 unsigned seek_to_first_metadata_block_(FILE *f)
1929 FLAC__byte buffer[4];
1933 FLAC__ASSERT(0 != f);
1934 FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == 4);
1936 /* skip any id3v2 tag */
1937 #if !defined _MSC_VER && !defined __MINGW32__
1938 /*@@@ don't know how to resolve errno without using LIBC.LIB; must use MSVCRT.LIB only for plugins */
1941 n = fread(buffer, 1, 4, f);
1942 #if !defined _MSC_VER && !defined __MINGW32__
1943 /*@@@ don't know how to resolve errno without using LIBC.LIB; must use MSVCRT.LIB only for plugins */
1950 else if(0 == memcmp(buffer, "ID3", 3)) {
1951 unsigned tag_length = 0;
1953 /* skip to the tag length */
1954 if(fseek(f, 2, SEEK_CUR) < 0)
1957 /* read the length */
1958 for(i = 0; i < 4; i++) {
1959 if(fread(buffer, 1, 1, f) < 1 || buffer[0] & 0x80)
1962 tag_length |= (buffer[0] & 0x7f);
1965 /* skip the rest of the tag */
1966 if(fseek(f, tag_length, SEEK_CUR) < 0)
1969 /* read the stream sync code */
1970 #if !defined _MSC_VER && !defined __MINGW32__
1971 /*@@@ don't know how to resolve errno without using LIBC.LIB; must use MSVCRT.LIB only for plugins */
1974 n = fread(buffer, 1, 4, f);
1975 #if !defined _MSC_VER && !defined __MINGW32__
1976 /*@@@ don't know how to resolve errno without using LIBC.LIB; must use MSVCRT.LIB only for plugins */
1985 /* check for the fLaC signature */
1986 if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH))
1992 FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append)
1994 const long offset_end = append? iterator->offset[iterator->depth] + (long)FLAC__STREAM_METADATA_HEADER_LENGTH + (long)iterator->length : iterator->offset[iterator->depth];
1996 if(0 != fseek(iterator->file, 0, SEEK_SET)) {
1997 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2000 if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) {
2001 cleanup_tempfile_(tempfile, tempfilename);
2004 if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) {
2005 cleanup_tempfile_(tempfile, tempfilename);
2012 FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, long fixup_is_last_flag_offset, FLAC__bool backup)
2014 long save_offset = iterator->offset[iterator->depth]; /*@@@ 2G limit */
2015 FLAC__ASSERT(0 != *tempfile);
2017 if(0 != fseek(iterator->file, save_offset + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length, SEEK_SET)) {
2018 cleanup_tempfile_(tempfile, tempfilename);
2019 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2022 if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) {
2023 cleanup_tempfile_(tempfile, tempfilename);
2027 if(fixup_is_last_code != 0) {
2029 * if code == 1, it means a block was appended to the end so
2030 * we have to clear the is_last flag of the previous block
2031 * if code == -1, it means the last block was deleted so
2032 * we have to set the is_last flag of the previous block
2034 /*@@@ MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */
2036 if(0 != fseek(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
2037 cleanup_tempfile_(tempfile, tempfilename);
2038 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2041 if(fread(&x, 1, 1, *tempfile) != 1) {
2042 cleanup_tempfile_(tempfile, tempfilename);
2043 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2046 if(fixup_is_last_code > 0) {
2047 FLAC__ASSERT(x & 0x80);
2051 FLAC__ASSERT(!(x & 0x80));
2054 if(0 != fseek(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
2055 cleanup_tempfile_(tempfile, tempfilename);
2056 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2059 if(fwrite(&x, 1, 1, *tempfile) != 1) {
2060 cleanup_tempfile_(tempfile, tempfilename);
2061 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2066 (void)fclose(iterator->file);
2068 if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status))
2071 if(iterator->has_stats)
2072 set_file_stats_(iterator->filename, &iterator->stats);
2074 if(!simple_iterator_prime_input_(iterator))
2077 while(iterator->offset[iterator->depth] + (long)FLAC__STREAM_METADATA_HEADER_LENGTH + (long)iterator->length < save_offset)
2078 if(!FLAC__metadata_simple_iterator_next(iterator))
2083 /* move the iterator to it's original block faster by faking a push, then doing a pop_ */
2084 FLAC__ASSERT(iterator->depth == 0);
2085 iterator->offset[0] = save_offset;
2087 return simple_iterator_pop_(iterator);
2091 FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, unsigned bytes/*@@@ 4G limit*/, FLAC__Metadata_SimpleIteratorStatus *status)
2093 FLAC__byte buffer[8192];
2097 n = min(sizeof(buffer), bytes);
2098 if(fread(buffer, 1, n, file) != n) {
2099 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2102 if(fwrite(buffer, 1, n, tempfile) != n) {
2103 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2112 FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status)
2114 FLAC__byte buffer[8192];
2117 while(!feof(file)) {
2118 n = fread(buffer, 1, sizeof(buffer), file);
2119 if(n == 0 && !feof(file)) {
2120 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2123 if(n > 0 && fwrite(buffer, 1, n, tempfile) != n) {
2124 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2132 FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
2134 static const char *tempfile_suffix = ".metadata_edit";
2135 if(0 == tempfile_path_prefix) {
2136 if(0 == (*tempfilename = malloc(strlen(filename) + strlen(tempfile_suffix) + 1))) {
2137 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2140 strcpy(*tempfilename, filename);
2141 strcat(*tempfilename, tempfile_suffix);
2144 const char *p = strrchr(filename, '/');
2150 if(0 == (*tempfilename = malloc(strlen(tempfile_path_prefix) + 1 + strlen(p) + strlen(tempfile_suffix) + 1))) {
2151 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2154 strcpy(*tempfilename, tempfile_path_prefix);
2155 strcat(*tempfilename, "/");
2156 strcat(*tempfilename, p);
2157 strcat(*tempfilename, tempfile_suffix);
2160 if(0 == (*tempfile = fopen(*tempfilename, "w+b"))) {
2161 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
2168 FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
2170 FLAC__ASSERT(0 != filename);
2171 FLAC__ASSERT(0 != tempfile);
2172 FLAC__ASSERT(0 != *tempfile);
2173 FLAC__ASSERT(0 != tempfilename);
2174 FLAC__ASSERT(0 != *tempfilename);
2175 FLAC__ASSERT(0 != status);
2177 (void)fclose(*tempfile);
2180 #if defined _MSC_VER || defined __MINGW32__
2181 if(unlink(filename) < 0) {
2182 cleanup_tempfile_(tempfile, tempfilename);
2183 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR;
2188 /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just rename(): */
2189 if(0 != rename(*tempfilename, filename)) {
2190 cleanup_tempfile_(tempfile, tempfilename);
2191 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR;
2195 cleanup_tempfile_(tempfile, tempfilename);
2200 void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
2202 if(0 != *tempfile) {
2203 (void)fclose(*tempfile);
2207 if(0 != *tempfilename) {
2208 (void)unlink(*tempfilename);
2209 free(*tempfilename);
2214 FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
2216 FLAC__ASSERT(0 != filename);
2217 FLAC__ASSERT(0 != stats);
2218 return (0 == stat(filename, stats));
2221 void set_file_stats_(const char *filename, struct stat *stats)
2223 struct utimbuf srctime;
2225 FLAC__ASSERT(0 != filename);
2226 FLAC__ASSERT(0 != stats);
2228 srctime.actime = stats->st_atime;
2229 srctime.modtime = stats->st_mtime;
2230 (void)chmod(filename, stats->st_mode);
2231 (void)utime(filename, &srctime);
2232 #if !defined _MSC_VER && !defined __MINGW32__
2233 (void)chown(filename, stats->st_uid, -1);
2234 (void)chown(filename, -1, stats->st_gid);
2238 FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status)
2241 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK:
2242 return FLAC__METADATA_CHAIN_STATUS_OK;
2243 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
2244 return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT;
2245 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
2246 return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
2247 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
2248 return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
2249 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE:
2250 return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE;
2251 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA:
2252 return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
2253 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR:
2254 return FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
2255 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR:
2256 return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
2257 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR:
2258 return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
2259 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR:
2260 return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR;
2261 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR:
2262 return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR;
2263 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR:
2264 return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
2265 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR:
2267 return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;