Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Erik de Castro Lopo | b1982fb | 2013-05-25 17:11:19 +1000 | [diff] [blame] | 2 | * Copyright (C) 2001-2009 Josh Coalson |
Erik de Castro Lopo | 1437391 | 2014-11-24 22:07:15 +1100 | [diff] [blame] | 3 | * Copyright (C) 2011-2014 Xiph.Org Foundation |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 4 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 8 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 9 | * - Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 11 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 12 | * - Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Erik de Castro Lopo | 006b835 | 2014-03-23 21:59:46 +1100 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 34 | # include <config.h> |
| 35 | #endif |
| 36 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 37 | #include <errno.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 41 | #include <stdarg.h> |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 42 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 43 | #include <sys/stat.h> /* for stat(), maybe chmod() */ |
| 44 | |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 45 | #include "private/metadata.h" |
| 46 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 47 | #include "FLAC/assert.h" |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 48 | #include "FLAC/stream_decoder.h" |
Josh Coalson | 0f008d2 | 2007-09-11 04:49:56 +0000 | [diff] [blame] | 49 | #include "share/alloc.h" |
Erik de Castro Lopo | 59afe36 | 2012-02-04 16:13:37 +1100 | [diff] [blame] | 50 | #include "share/compat.h" |
Erik de Castro Lopo | 91790ef | 2013-03-05 22:19:27 +1100 | [diff] [blame] | 51 | #include "share/macros.h" |
Erik de Castro Lopo | 2d6354f | 2013-03-19 20:23:44 +1100 | [diff] [blame] | 52 | #include "share/safe_str.h" |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 53 | #include "private/macros.h" |
Erik de Castro Lopo | 8749dc2 | 2012-06-22 14:23:56 +1000 | [diff] [blame] | 54 | #include "private/memory.h" |
| 55 | |
| 56 | /* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */ |
| 57 | #define safe_malloc_mul_2op_ safe_malloc_mul_2op_p |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 58 | |
| 59 | /**************************************************************************** |
| 60 | * |
| 61 | * Local function declarations |
| 62 | * |
| 63 | ***************************************************************************/ |
| 64 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 65 | static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes); |
| 66 | static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes); |
| 67 | static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes); |
| 68 | static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes); |
| 69 | static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes); |
| 70 | static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes); |
| 71 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 72 | static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator); |
| 73 | static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 74 | 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); |
| 75 | 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); |
| 76 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block); |
| 77 | 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); |
| 78 | 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); |
| 79 | 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); |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 80 | 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, unsigned max_length); |
| 81 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, unsigned block_length); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 82 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track); |
| 83 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block); |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 84 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 85 | 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); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 86 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 87 | static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block); |
| 88 | static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 89 | static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block); |
| 90 | static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block); |
| 91 | static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block); |
| 92 | 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); |
| 93 | 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); |
| 94 | static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block); |
| 95 | static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block); |
| 96 | static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block); |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 97 | static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 98 | 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); |
| 99 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 100 | static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block); |
| 101 | static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last); |
| 102 | static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 103 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 104 | static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator); |
| 105 | static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 106 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 107 | static unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 108 | static unsigned seek_to_first_metadata_block_(FILE *f); |
| 109 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 110 | static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 111 | static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 112 | |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 113 | static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status); |
| 114 | static FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status); |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 115 | static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 116 | 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); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 117 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 118 | static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status); |
| 119 | static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 120 | static void cleanup_tempfile_(FILE **tempfile, char **tempfilename); |
| 121 | |
Erik de Castro Lopo | 7dc5e3a | 2013-03-22 18:22:57 +1100 | [diff] [blame] | 122 | static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats); |
| 123 | static void set_file_stats_(const char *filename, struct flac_stat_s *stats); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 124 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 125 | static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence); |
| 126 | static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle); |
| 127 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 128 | static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 129 | |
| 130 | |
Josh Coalson | f91251d | 2002-12-28 07:04:49 +0000 | [diff] [blame] | 131 | #ifdef FLAC__VALGRIND_TESTING |
| 132 | static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 133 | { |
| 134 | size_t ret = fwrite(ptr, size, nmemb, stream); |
| 135 | if(!ferror(stream)) |
| 136 | fflush(stream); |
| 137 | return ret; |
| 138 | } |
| 139 | #else |
| 140 | #define local__fwrite fwrite |
| 141 | #endif |
| 142 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 143 | /**************************************************************************** |
| 144 | * |
| 145 | * Level 0 implementation |
| 146 | * |
| 147 | ***************************************************************************/ |
| 148 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 149 | static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 150 | static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 151 | static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 152 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 153 | typedef struct { |
| 154 | FLAC__bool got_error; |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 155 | FLAC__StreamMetadata *object; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 156 | } level0_client_data; |
| 157 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 158 | static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 159 | { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 160 | level0_client_data cd; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 161 | FLAC__StreamDecoder *decoder; |
Josh Coalson | 9b86c96 | 2002-07-10 07:11:15 +0000 | [diff] [blame] | 162 | |
| 163 | FLAC__ASSERT(0 != filename); |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 164 | |
| 165 | cd.got_error = false; |
| 166 | cd.object = 0; |
| 167 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 168 | decoder = FLAC__stream_decoder_new(); |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 169 | |
| 170 | if(0 == decoder) |
| 171 | return 0; |
| 172 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 173 | FLAC__stream_decoder_set_md5_checking(decoder, false); |
| 174 | FLAC__stream_decoder_set_metadata_ignore_all(decoder); |
| 175 | FLAC__stream_decoder_set_metadata_respond(decoder, type); |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 176 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 177 | if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) { |
Josh Coalson | fb84f25 | 2006-11-09 06:53:10 +0000 | [diff] [blame] | 178 | (void)FLAC__stream_decoder_finish(decoder); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 179 | FLAC__stream_decoder_delete(decoder); |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 183 | if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) { |
Josh Coalson | fb84f25 | 2006-11-09 06:53:10 +0000 | [diff] [blame] | 184 | (void)FLAC__stream_decoder_finish(decoder); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 185 | FLAC__stream_decoder_delete(decoder); |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 186 | if(0 != cd.object) |
| 187 | FLAC__metadata_object_delete(cd.object); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Josh Coalson | fb84f25 | 2006-11-09 06:53:10 +0000 | [diff] [blame] | 191 | (void)FLAC__stream_decoder_finish(decoder); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 192 | FLAC__stream_decoder_delete(decoder); |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 193 | |
| 194 | return cd.object; |
| 195 | } |
| 196 | |
| 197 | FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo) |
| 198 | { |
| 199 | FLAC__StreamMetadata *object; |
| 200 | |
| 201 | FLAC__ASSERT(0 != filename); |
Josh Coalson | 9b86c96 | 2002-07-10 07:11:15 +0000 | [diff] [blame] | 202 | FLAC__ASSERT(0 != streaminfo); |
| 203 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 204 | object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 205 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 206 | if (object) { |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 207 | /* can just copy the contents since STREAMINFO has no internal structure */ |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 208 | *streaminfo = *object; |
| 209 | FLAC__metadata_object_delete(object); |
| 210 | return true; |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 211 | } |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 212 | else { |
| 213 | return false; |
| 214 | } |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags) |
| 218 | { |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 219 | FLAC__ASSERT(0 != filename); |
| 220 | FLAC__ASSERT(0 != tags); |
| 221 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 222 | *tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT); |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 223 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 224 | return 0 != *tags; |
| 225 | } |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 226 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 227 | FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet) |
| 228 | { |
| 229 | FLAC__ASSERT(0 != filename); |
| 230 | FLAC__ASSERT(0 != cuesheet); |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 231 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 232 | *cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET); |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 233 | |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 234 | return 0 != *cuesheet; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 237 | FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 238 | { |
| 239 | (void)decoder, (void)frame, (void)buffer, (void)client_data; |
| 240 | |
Josh Coalson | 2835f43 | 2002-06-04 05:54:44 +0000 | [diff] [blame] | 241 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 244 | void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 245 | { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 246 | level0_client_data *cd = (level0_client_data *)client_data; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 247 | (void)decoder; |
| 248 | |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 249 | /* |
| 250 | * we assume we only get here when the one metadata block we were |
| 251 | * looking for was passed to us |
| 252 | */ |
Josh Coalson | 7cfac0b | 2006-04-10 05:37:34 +0000 | [diff] [blame] | 253 | if(!cd->got_error && 0 == cd->object) { |
Josh Coalson | 1aca6b1 | 2004-07-30 01:54:29 +0000 | [diff] [blame] | 254 | if(0 == (cd->object = FLAC__metadata_object_clone(metadata))) |
| 255 | cd->got_error = true; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 256 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 259 | void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 260 | { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 261 | level0_client_data *cd = (level0_client_data *)client_data; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 262 | (void)decoder; |
| 263 | |
Josh Coalson | 2835f43 | 2002-06-04 05:54:44 +0000 | [diff] [blame] | 264 | if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC) |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 265 | cd->got_error = true; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Josh Coalson | 74ed294 | 2006-09-23 23:15:05 +0000 | [diff] [blame] | 268 | FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors) |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 269 | { |
| 270 | FLAC__Metadata_SimpleIterator *it; |
| 271 | FLAC__uint64 max_area_seen = 0; |
| 272 | FLAC__uint64 max_depth_seen = 0; |
| 273 | |
| 274 | FLAC__ASSERT(0 != filename); |
| 275 | FLAC__ASSERT(0 != picture); |
| 276 | |
| 277 | *picture = 0; |
| 278 | |
| 279 | it = FLAC__metadata_simple_iterator_new(); |
| 280 | if(0 == it) |
| 281 | return false; |
| 282 | if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) { |
| 283 | FLAC__metadata_simple_iterator_delete(it); |
| 284 | return false; |
| 285 | } |
| 286 | do { |
| 287 | if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) { |
| 288 | FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it); |
| 289 | FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height; |
| 290 | /* check constraints */ |
| 291 | if( |
| 292 | (type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) && |
| 293 | (mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) && |
| 294 | (description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) && |
| 295 | obj->data.picture.width <= max_width && |
| 296 | obj->data.picture.height <= max_height && |
| 297 | obj->data.picture.depth <= max_depth && |
Josh Coalson | 74ed294 | 2006-09-23 23:15:05 +0000 | [diff] [blame] | 298 | obj->data.picture.colors <= max_colors && |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 299 | (area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen)) |
| 300 | ) { |
| 301 | if(*picture) |
| 302 | FLAC__metadata_object_delete(*picture); |
| 303 | *picture = obj; |
| 304 | max_area_seen = area; |
| 305 | max_depth_seen = obj->data.picture.depth; |
| 306 | } |
| 307 | else { |
| 308 | FLAC__metadata_object_delete(obj); |
| 309 | } |
| 310 | } |
| 311 | } while(FLAC__metadata_simple_iterator_next(it)); |
| 312 | |
| 313 | FLAC__metadata_simple_iterator_delete(it); |
| 314 | |
| 315 | return (0 != *picture); |
| 316 | } |
| 317 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 318 | |
| 319 | /**************************************************************************** |
| 320 | * |
| 321 | * Level 1 implementation |
| 322 | * |
| 323 | ***************************************************************************/ |
| 324 | |
| 325 | #define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4) |
| 326 | /* 1 for initial offset, +4 for our own personal use */ |
| 327 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 328 | struct FLAC__Metadata_SimpleIterator { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 329 | FILE *file; |
| 330 | char *filename, *tempfile_path_prefix; |
Erik de Castro Lopo | 7dc5e3a | 2013-03-22 18:22:57 +1100 | [diff] [blame] | 331 | struct flac_stat_s stats; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 332 | FLAC__bool has_stats; |
| 333 | FLAC__bool is_writable; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 334 | FLAC__Metadata_SimpleIteratorStatus status; |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 335 | FLAC__off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH]; |
| 336 | FLAC__off_t first_offset; /* this is the offset to the STREAMINFO block */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 337 | unsigned depth; |
| 338 | /* this is the metadata block header of the current block we are pointing to: */ |
| 339 | FLAC__bool is_last; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 340 | FLAC__MetadataType type; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 341 | unsigned length; |
| 342 | }; |
| 343 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 344 | FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 345 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK", |
| 346 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT", |
| 347 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE", |
| 348 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE", |
| 349 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE", |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 350 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA", |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 351 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR", |
| 352 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR", |
| 353 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR", |
| 354 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR", |
| 355 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR", |
| 356 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR", |
| 357 | "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR" |
| 358 | }; |
| 359 | |
| 360 | |
Josh Coalson | e3ec2ad | 2007-01-31 03:53:22 +0000 | [diff] [blame] | 361 | FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 362 | { |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 363 | FLAC__Metadata_SimpleIterator *iterator = calloc(1, sizeof(FLAC__Metadata_SimpleIterator)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 364 | |
| 365 | if(0 != iterator) { |
| 366 | iterator->file = 0; |
| 367 | iterator->filename = 0; |
| 368 | iterator->tempfile_path_prefix = 0; |
| 369 | iterator->has_stats = false; |
| 370 | iterator->is_writable = false; |
| 371 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 372 | iterator->first_offset = iterator->offset[0] = -1; |
| 373 | iterator->depth = 0; |
| 374 | } |
| 375 | |
| 376 | return iterator; |
| 377 | } |
| 378 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 379 | static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 380 | { |
| 381 | FLAC__ASSERT(0 != iterator); |
| 382 | |
| 383 | if(0 != iterator->file) { |
| 384 | fclose(iterator->file); |
| 385 | iterator->file = 0; |
| 386 | if(iterator->has_stats) |
| 387 | set_file_stats_(iterator->filename, &iterator->stats); |
| 388 | } |
| 389 | if(0 != iterator->filename) { |
| 390 | free(iterator->filename); |
| 391 | iterator->filename = 0; |
| 392 | } |
| 393 | if(0 != iterator->tempfile_path_prefix) { |
| 394 | free(iterator->tempfile_path_prefix); |
| 395 | iterator->tempfile_path_prefix = 0; |
| 396 | } |
| 397 | } |
| 398 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 399 | FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 400 | { |
| 401 | FLAC__ASSERT(0 != iterator); |
| 402 | |
| 403 | simple_iterator_free_guts_(iterator); |
| 404 | free(iterator); |
| 405 | } |
| 406 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 407 | FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 408 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 409 | FLAC__Metadata_SimpleIteratorStatus status; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 410 | |
| 411 | FLAC__ASSERT(0 != iterator); |
| 412 | |
| 413 | status = iterator->status; |
| 414 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 415 | return status; |
| 416 | } |
| 417 | |
Josh Coalson | 3ac6693 | 2002-08-30 05:41:31 +0000 | [diff] [blame] | 418 | static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 419 | { |
| 420 | unsigned ret; |
| 421 | |
| 422 | FLAC__ASSERT(0 != iterator); |
| 423 | |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 424 | if(read_only || 0 == (iterator->file = flac_fopen(iterator->filename, "r+b"))) { |
Josh Coalson | 3ac6693 | 2002-08-30 05:41:31 +0000 | [diff] [blame] | 425 | iterator->is_writable = false; |
Josh Coalson | 3ac6693 | 2002-08-30 05:41:31 +0000 | [diff] [blame] | 426 | if(read_only || errno == EACCES) { |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 427 | if(0 == (iterator->file = flac_fopen(iterator->filename, "rb"))) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 428 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 429 | return false; |
| 430 | } |
| 431 | } |
| 432 | else { |
| 433 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 434 | return false; |
| 435 | } |
| 436 | } |
| 437 | else { |
| 438 | iterator->is_writable = true; |
| 439 | } |
| 440 | |
| 441 | ret = seek_to_first_metadata_block_(iterator->file); |
| 442 | switch(ret) { |
| 443 | case 0: |
| 444 | iterator->depth = 0; |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 445 | iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 446 | return read_metadata_block_header_(iterator); |
| 447 | case 1: |
| 448 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 449 | return false; |
| 450 | case 2: |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 451 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 452 | return false; |
| 453 | case 3: |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 454 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE; |
| 455 | return false; |
| 456 | default: |
| 457 | FLAC__ASSERT(0); |
| 458 | return false; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | #if 0 |
| 463 | @@@ If we decide to finish implementing this, put this comment back in metadata.h |
| 464 | /* |
| 465 | * The 'tempfile_path_prefix' allows you to specify a directory where |
| 466 | * tempfiles should go. Remember that if your metadata edits cause the |
| 467 | * FLAC file to grow, the entire file will have to be rewritten. If |
| 468 | * 'tempfile_path_prefix' is NULL, the temp file will be written in the |
| 469 | * same directory as the original FLAC file. This makes replacing the |
| 470 | * original with the tempfile fast but requires extra space in the same |
| 471 | * partition for the tempfile. If space is a problem, you can pass a |
| 472 | * directory name belonging to a different partition in |
| 473 | * 'tempfile_path_prefix'. Note that you should use the forward slash |
| 474 | * '/' as the directory separator. A trailing slash is not needed; it |
| 475 | * will be added automatically. |
| 476 | */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 477 | FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 478 | #endif |
| 479 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 480 | 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) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 481 | { |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 482 | const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'flac_rename(...)' for what it will take to finish implementing this */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 483 | |
| 484 | FLAC__ASSERT(0 != iterator); |
| 485 | FLAC__ASSERT(0 != filename); |
| 486 | |
| 487 | simple_iterator_free_guts_(iterator); |
| 488 | |
Josh Coalson | 3ac6693 | 2002-08-30 05:41:31 +0000 | [diff] [blame] | 489 | if(!read_only && preserve_file_stats) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 490 | iterator->has_stats = get_file_stats_(filename, &iterator->stats); |
| 491 | |
| 492 | if(0 == (iterator->filename = strdup(filename))) { |
| 493 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 494 | return false; |
| 495 | } |
| 496 | if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) { |
| 497 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 498 | return false; |
| 499 | } |
| 500 | |
Josh Coalson | 3ac6693 | 2002-08-30 05:41:31 +0000 | [diff] [blame] | 501 | return simple_iterator_prime_input_(iterator, read_only); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 504 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 505 | { |
| 506 | FLAC__ASSERT(0 != iterator); |
| 507 | FLAC__ASSERT(0 != iterator->file); |
| 508 | |
| 509 | return iterator->is_writable; |
| 510 | } |
| 511 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 512 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 513 | { |
| 514 | FLAC__ASSERT(0 != iterator); |
| 515 | FLAC__ASSERT(0 != iterator->file); |
| 516 | |
| 517 | if(iterator->is_last) |
| 518 | return false; |
| 519 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 520 | if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 521 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 522 | return false; |
| 523 | } |
| 524 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 525 | iterator->offset[iterator->depth] = ftello(iterator->file); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 526 | |
| 527 | return read_metadata_block_header_(iterator); |
| 528 | } |
| 529 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 530 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 531 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 532 | FLAC__off_t this_offset; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 533 | |
| 534 | FLAC__ASSERT(0 != iterator); |
| 535 | FLAC__ASSERT(0 != iterator->file); |
| 536 | |
| 537 | if(iterator->offset[iterator->depth] == iterator->first_offset) |
| 538 | return false; |
| 539 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 540 | if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 541 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 542 | return false; |
| 543 | } |
| 544 | this_offset = iterator->first_offset; |
| 545 | if(!read_metadata_block_header_(iterator)) |
| 546 | return false; |
| 547 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 548 | /* we ignore any error from ftello() and catch it in fseeko() */ |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 549 | while(ftello(iterator->file) + (FLAC__off_t)iterator->length < iterator->offset[iterator->depth]) { |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 550 | if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 551 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 552 | return false; |
| 553 | } |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 554 | this_offset = ftello(iterator->file); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 555 | if(!read_metadata_block_header_(iterator)) |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | iterator->offset[iterator->depth] = this_offset; |
| 560 | |
| 561 | return true; |
| 562 | } |
| 563 | |
Josh Coalson | 504dcaf | 2007-09-13 15:42:47 +0000 | [diff] [blame] | 564 | /*@@@@add to tests*/ |
Josh Coalson | 09ccf20 | 2007-08-14 00:34:50 +0000 | [diff] [blame] | 565 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator) |
| 566 | { |
| 567 | FLAC__ASSERT(0 != iterator); |
| 568 | FLAC__ASSERT(0 != iterator->file); |
| 569 | |
| 570 | return iterator->is_last; |
| 571 | } |
| 572 | |
Josh Coalson | 504dcaf | 2007-09-13 15:42:47 +0000 | [diff] [blame] | 573 | /*@@@@add to tests*/ |
Josh Coalson | 09ccf20 | 2007-08-14 00:34:50 +0000 | [diff] [blame] | 574 | FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator) |
| 575 | { |
| 576 | FLAC__ASSERT(0 != iterator); |
| 577 | FLAC__ASSERT(0 != iterator->file); |
| 578 | |
| 579 | return iterator->offset[iterator->depth]; |
| 580 | } |
| 581 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 582 | FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 583 | { |
| 584 | FLAC__ASSERT(0 != iterator); |
| 585 | FLAC__ASSERT(0 != iterator->file); |
| 586 | |
| 587 | return iterator->type; |
| 588 | } |
| 589 | |
Josh Coalson | 504dcaf | 2007-09-13 15:42:47 +0000 | [diff] [blame] | 590 | /*@@@@add to tests*/ |
Josh Coalson | 09ccf20 | 2007-08-14 00:34:50 +0000 | [diff] [blame] | 591 | FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator) |
| 592 | { |
| 593 | FLAC__ASSERT(0 != iterator); |
| 594 | FLAC__ASSERT(0 != iterator->file); |
| 595 | |
| 596 | return iterator->length; |
| 597 | } |
| 598 | |
Josh Coalson | 504dcaf | 2007-09-13 15:42:47 +0000 | [diff] [blame] | 599 | /*@@@@add to tests*/ |
Josh Coalson | 4e56b06 | 2007-08-15 05:59:09 +0000 | [diff] [blame] | 600 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id) |
| 601 | { |
| 602 | const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 603 | |
| 604 | FLAC__ASSERT(0 != iterator); |
| 605 | FLAC__ASSERT(0 != iterator->file); |
| 606 | FLAC__ASSERT(0 != id); |
| 607 | |
| 608 | if(iterator->type != FLAC__METADATA_TYPE_APPLICATION) { |
| 609 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | if(fread(id, 1, id_bytes, iterator->file) != id_bytes) { |
| 614 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | /* back up */ |
| 619 | if(0 != fseeko(iterator->file, -((int)id_bytes), SEEK_CUR)) { |
| 620 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 627 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 628 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 629 | FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 630 | |
| 631 | FLAC__ASSERT(0 != iterator); |
| 632 | FLAC__ASSERT(0 != iterator->file); |
| 633 | |
| 634 | if(0 != block) { |
| 635 | block->is_last = iterator->is_last; |
| 636 | block->length = iterator->length; |
| 637 | |
| 638 | if(!read_metadata_block_data_(iterator, block)) { |
| 639 | FLAC__metadata_object_delete(block); |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /* back up to the beginning of the block data to stay consistent */ |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 644 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 645 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 646 | FLAC__metadata_object_delete(block); |
| 647 | return 0; |
| 648 | } |
| 649 | } |
| 650 | else |
| 651 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 652 | |
| 653 | return block; |
| 654 | } |
| 655 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 656 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 657 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 658 | FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];) |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 659 | FLAC__bool ret; |
| 660 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 661 | FLAC__ASSERT(0 != iterator); |
| 662 | FLAC__ASSERT(0 != iterator->file); |
Josh Coalson | d8ab346 | 2002-07-11 05:54:05 +0000 | [diff] [blame] | 663 | FLAC__ASSERT(0 != block); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 664 | |
| 665 | if(!iterator->is_writable) { |
| 666 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE; |
| 667 | return false; |
| 668 | } |
| 669 | |
| 670 | if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 671 | if(iterator->type != block->type) { |
| 672 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | block->is_last = iterator->is_last; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 678 | |
| 679 | if(iterator->length == block->length) |
| 680 | return write_metadata_block_stationary_(iterator, block); |
| 681 | else if(iterator->length > block->length) { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 682 | if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) { |
Josh Coalson | ccce9ed | 2003-01-13 02:06:40 +0000 | [diff] [blame] | 683 | ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last); |
| 684 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 685 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 686 | return ret; |
| 687 | } |
| 688 | else { |
| 689 | ret = rewrite_whole_file_(iterator, block, /*append=*/false); |
Josh Coalson | ccce9ed | 2003-01-13 02:06:40 +0000 | [diff] [blame] | 690 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 691 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 692 | return ret; |
| 693 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 694 | } |
| 695 | else /* iterator->length < block->length */ { |
| 696 | unsigned padding_leftover = 0; |
| 697 | FLAC__bool padding_is_last = false; |
| 698 | if(use_padding) { |
| 699 | /* first see if we can even use padding */ |
| 700 | if(iterator->is_last) { |
| 701 | use_padding = false; |
| 702 | } |
| 703 | else { |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 704 | const unsigned extra_padding_bytes_required = block->length - iterator->length; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 705 | simple_iterator_push_(iterator); |
| 706 | if(!FLAC__metadata_simple_iterator_next(iterator)) { |
| 707 | (void)simple_iterator_pop_(iterator); |
| 708 | return false; |
| 709 | } |
| 710 | if(iterator->type != FLAC__METADATA_TYPE_PADDING) { |
| 711 | use_padding = false; |
| 712 | } |
| 713 | else { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 714 | if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 715 | padding_leftover = 0; |
| 716 | block->is_last = iterator->is_last; |
| 717 | } |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 718 | else if(iterator->length < extra_padding_bytes_required) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 719 | use_padding = false; |
| 720 | else { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 721 | padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 722 | padding_is_last = iterator->is_last; |
| 723 | block->is_last = false; |
| 724 | } |
| 725 | } |
| 726 | if(!simple_iterator_pop_(iterator)) |
| 727 | return false; |
| 728 | } |
| 729 | } |
| 730 | if(use_padding) { |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 731 | if(padding_leftover == 0) { |
| 732 | ret = write_metadata_block_stationary_(iterator, block); |
Josh Coalson | ccce9ed | 2003-01-13 02:06:40 +0000 | [diff] [blame] | 733 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 734 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 735 | return ret; |
| 736 | } |
| 737 | else { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 738 | FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 739 | ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last); |
Josh Coalson | ccce9ed | 2003-01-13 02:06:40 +0000 | [diff] [blame] | 740 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 741 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 742 | return ret; |
| 743 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 744 | } |
| 745 | else { |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 746 | ret = rewrite_whole_file_(iterator, block, /*append=*/false); |
Josh Coalson | ccce9ed | 2003-01-13 02:06:40 +0000 | [diff] [blame] | 747 | FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 748 | FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 749 | return ret; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 754 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 755 | { |
| 756 | unsigned padding_leftover = 0; |
| 757 | FLAC__bool padding_is_last = false; |
| 758 | |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 759 | FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;) |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 760 | FLAC__bool ret; |
| 761 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 762 | FLAC__ASSERT(0 != iterator); |
| 763 | FLAC__ASSERT(0 != iterator->file); |
Josh Coalson | d8ab346 | 2002-07-11 05:54:05 +0000 | [diff] [blame] | 764 | FLAC__ASSERT(0 != block); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 765 | |
Erik de Castro Lopo | 7efe8f3 | 2014-11-14 23:35:37 -0800 | [diff] [blame] | 766 | if(!iterator->is_writable) { |
| 767 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 768 | return false; |
Erik de Castro Lopo | 7efe8f3 | 2014-11-14 23:35:37 -0800 | [diff] [blame] | 769 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 770 | |
| 771 | if(block->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 772 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 773 | return false; |
| 774 | } |
| 775 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 776 | block->is_last = iterator->is_last; |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 777 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 778 | if(use_padding) { |
| 779 | /* first see if we can even use padding */ |
| 780 | if(iterator->is_last) { |
| 781 | use_padding = false; |
| 782 | } |
| 783 | else { |
| 784 | simple_iterator_push_(iterator); |
| 785 | if(!FLAC__metadata_simple_iterator_next(iterator)) { |
| 786 | (void)simple_iterator_pop_(iterator); |
| 787 | return false; |
| 788 | } |
| 789 | if(iterator->type != FLAC__METADATA_TYPE_PADDING) { |
| 790 | use_padding = false; |
| 791 | } |
| 792 | else { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 793 | if(iterator->length == block->length) { |
| 794 | padding_leftover = 0; |
| 795 | block->is_last = iterator->is_last; |
| 796 | } |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 797 | else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 798 | use_padding = false; |
| 799 | else { |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 800 | padding_leftover = iterator->length - block->length; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 801 | padding_is_last = iterator->is_last; |
| 802 | block->is_last = false; |
| 803 | } |
| 804 | } |
| 805 | if(!simple_iterator_pop_(iterator)) |
| 806 | return false; |
| 807 | } |
| 808 | } |
| 809 | if(use_padding) { |
| 810 | /* move to the next block, which is suitable padding */ |
| 811 | if(!FLAC__metadata_simple_iterator_next(iterator)) |
| 812 | return false; |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 813 | if(padding_leftover == 0) { |
| 814 | ret = write_metadata_block_stationary_(iterator, block); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 815 | FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 816 | FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 817 | return ret; |
| 818 | } |
| 819 | else { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 820 | FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 821 | ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 822 | FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 823 | FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 824 | return ret; |
| 825 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 826 | } |
| 827 | else { |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 828 | ret = rewrite_whole_file_(iterator, block, /*append=*/true); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 829 | FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 830 | FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 831 | return ret; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 835 | FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 836 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 837 | FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];) |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 838 | FLAC__bool ret; |
| 839 | |
Erik de Castro Lopo | 4684608 | 2016-01-16 07:57:26 +1100 | [diff] [blame] | 840 | if(!iterator->is_writable) { |
| 841 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE; |
| 842 | return false; |
| 843 | } |
| 844 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 845 | if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 846 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 847 | return false; |
| 848 | } |
| 849 | |
| 850 | if(use_padding) { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 851 | FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 852 | if(0 == padding) { |
| 853 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 854 | return false; |
| 855 | } |
| 856 | padding->length = iterator->length; |
| 857 | if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) { |
| 858 | FLAC__metadata_object_delete(padding); |
| 859 | return false; |
| 860 | } |
| 861 | FLAC__metadata_object_delete(padding); |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 862 | if(!FLAC__metadata_simple_iterator_prev(iterator)) |
| 863 | return false; |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 864 | FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset); |
| 865 | FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 866 | return true; |
| 867 | } |
| 868 | else { |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 869 | ret = rewrite_whole_file_(iterator, 0, /*append=*/false); |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 870 | FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset); |
| 871 | FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset); |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 872 | return ret; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | |
| 877 | |
| 878 | /**************************************************************************** |
| 879 | * |
| 880 | * Level 2 implementation |
| 881 | * |
| 882 | ***************************************************************************/ |
| 883 | |
| 884 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 885 | typedef struct FLAC__Metadata_Node { |
| 886 | FLAC__StreamMetadata *data; |
| 887 | struct FLAC__Metadata_Node *prev, *next; |
| 888 | } FLAC__Metadata_Node; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 889 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 890 | struct FLAC__Metadata_Chain { |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 891 | char *filename; /* will be NULL if using callbacks */ |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 892 | FLAC__bool is_ogg; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 893 | FLAC__Metadata_Node *head; |
| 894 | FLAC__Metadata_Node *tail; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 895 | unsigned nodes; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 896 | FLAC__Metadata_ChainStatus status; |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 897 | FLAC__off_t first_offset, last_offset; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 898 | /* |
| 899 | * This is the length of the chain initially read from the FLAC file. |
Josh Coalson | 3ecb117 | 2002-06-15 05:09:47 +0000 | [diff] [blame] | 900 | * it is used to compare against the current length to decide whether |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 901 | * or not the whole file has to be rewritten. |
| 902 | */ |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 903 | FLAC__off_t initial_length; |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 904 | /* @@@ hacky, these are currently only needed by ogg reader */ |
| 905 | FLAC__IOHandle handle; |
| 906 | FLAC__IOCallback_Read read_cb; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 907 | }; |
| 908 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 909 | struct FLAC__Metadata_Iterator { |
| 910 | FLAC__Metadata_Chain *chain; |
| 911 | FLAC__Metadata_Node *current; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 912 | }; |
| 913 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 914 | FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 915 | "FLAC__METADATA_CHAIN_STATUS_OK", |
| 916 | "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT", |
| 917 | "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE", |
| 918 | "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE", |
| 919 | "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE", |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 920 | "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA", |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 921 | "FLAC__METADATA_CHAIN_STATUS_READ_ERROR", |
| 922 | "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR", |
| 923 | "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR", |
| 924 | "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR", |
| 925 | "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR", |
| 926 | "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR", |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 927 | "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR", |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 928 | "FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS", |
| 929 | "FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", |
| 930 | "FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL" |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | |
Josh Coalson | e3ec2ad | 2007-01-31 03:53:22 +0000 | [diff] [blame] | 934 | static FLAC__Metadata_Node *node_new_(void) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 935 | { |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 936 | return calloc(1, sizeof(FLAC__Metadata_Node)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 939 | static void node_delete_(FLAC__Metadata_Node *node) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 940 | { |
| 941 | FLAC__ASSERT(0 != node); |
| 942 | if(0 != node->data) |
| 943 | FLAC__metadata_object_delete(node->data); |
| 944 | free(node); |
| 945 | } |
| 946 | |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 947 | static void chain_init_(FLAC__Metadata_Chain *chain) |
| 948 | { |
| 949 | FLAC__ASSERT(0 != chain); |
| 950 | |
| 951 | chain->filename = 0; |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 952 | chain->is_ogg = false; |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 953 | chain->head = chain->tail = 0; |
| 954 | chain->nodes = 0; |
| 955 | chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 956 | chain->initial_length = 0; |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 957 | chain->read_cb = 0; |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | static void chain_clear_(FLAC__Metadata_Chain *chain) |
| 961 | { |
| 962 | FLAC__Metadata_Node *node, *next; |
| 963 | |
| 964 | FLAC__ASSERT(0 != chain); |
| 965 | |
| 966 | for(node = chain->head; node; ) { |
| 967 | next = node->next; |
| 968 | node_delete_(node); |
| 969 | node = next; |
| 970 | } |
| 971 | |
| 972 | if(0 != chain->filename) |
| 973 | free(chain->filename); |
| 974 | |
| 975 | chain_init_(chain); |
| 976 | } |
| 977 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 978 | static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 979 | { |
| 980 | FLAC__ASSERT(0 != chain); |
| 981 | FLAC__ASSERT(0 != node); |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 982 | FLAC__ASSERT(0 != node->data); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 983 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 984 | node->next = node->prev = 0; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 985 | node->data->is_last = true; |
| 986 | if(0 != chain->tail) |
| 987 | chain->tail->data->is_last = false; |
| 988 | |
| 989 | if(0 == chain->head) |
| 990 | chain->head = node; |
| 991 | else { |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 992 | FLAC__ASSERT(0 != chain->tail); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 993 | chain->tail->next = node; |
| 994 | node->prev = chain->tail; |
| 995 | } |
| 996 | chain->tail = node; |
| 997 | chain->nodes++; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1000 | static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1001 | { |
| 1002 | FLAC__ASSERT(0 != chain); |
| 1003 | FLAC__ASSERT(0 != node); |
| 1004 | |
| 1005 | if(node == chain->head) |
| 1006 | chain->head = node->next; |
| 1007 | else |
| 1008 | node->prev->next = node->next; |
| 1009 | |
| 1010 | if(node == chain->tail) |
| 1011 | chain->tail = node->prev; |
| 1012 | else |
| 1013 | node->next->prev = node->prev; |
| 1014 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 1015 | if(0 != chain->tail) |
| 1016 | chain->tail->data->is_last = true; |
| 1017 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1018 | chain->nodes--; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1021 | static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1022 | { |
| 1023 | chain_remove_node_(chain, node); |
| 1024 | node_delete_(node); |
| 1025 | } |
| 1026 | |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1027 | static FLAC__off_t chain_calculate_length_(FLAC__Metadata_Chain *chain) |
Josh Coalson | 3ecb117 | 2002-06-15 05:09:47 +0000 | [diff] [blame] | 1028 | { |
| 1029 | const FLAC__Metadata_Node *node; |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1030 | FLAC__off_t length = 0; |
Josh Coalson | 3ecb117 | 2002-06-15 05:09:47 +0000 | [diff] [blame] | 1031 | for(node = chain->head; node; node = node->next) |
| 1032 | length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1033 | return length; |
| 1034 | } |
| 1035 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1036 | static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1037 | { |
| 1038 | FLAC__ASSERT(0 != node); |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 1039 | FLAC__ASSERT(0 != node->data); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1040 | FLAC__ASSERT(0 != iterator); |
| 1041 | FLAC__ASSERT(0 != iterator->current); |
| 1042 | FLAC__ASSERT(0 != iterator->chain); |
| 1043 | FLAC__ASSERT(0 != iterator->chain->head); |
| 1044 | FLAC__ASSERT(0 != iterator->chain->tail); |
| 1045 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 1046 | node->data->is_last = false; |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1047 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1048 | node->prev = iterator->current->prev; |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1049 | node->next = iterator->current; |
| 1050 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1051 | if(0 == node->prev) |
| 1052 | iterator->chain->head = node; |
| 1053 | else |
| 1054 | node->prev->next = node; |
| 1055 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1056 | iterator->current->prev = node; |
| 1057 | |
| 1058 | iterator->chain->nodes++; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1061 | static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1062 | { |
| 1063 | FLAC__ASSERT(0 != node); |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 1064 | FLAC__ASSERT(0 != node->data); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1065 | FLAC__ASSERT(0 != iterator); |
| 1066 | FLAC__ASSERT(0 != iterator->current); |
| 1067 | FLAC__ASSERT(0 != iterator->chain); |
| 1068 | FLAC__ASSERT(0 != iterator->chain->head); |
| 1069 | FLAC__ASSERT(0 != iterator->chain->tail); |
| 1070 | |
| 1071 | iterator->current->data->is_last = false; |
| 1072 | |
| 1073 | node->prev = iterator->current; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1074 | node->next = iterator->current->next; |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1075 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1076 | if(0 == node->next) |
| 1077 | iterator->chain->tail = node; |
| 1078 | else |
| 1079 | node->next->prev = node; |
| 1080 | |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1081 | node->prev->next = node; |
| 1082 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1083 | iterator->chain->tail->data->is_last = true; |
| 1084 | |
| 1085 | iterator->chain->nodes++; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1088 | /* return true iff node and node->next are both padding */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1089 | static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1090 | { |
| 1091 | if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 1092 | const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length; |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1093 | node->data->length += growth; /* new block size can be greater than max metadata block size, but it'll be fixed later in chain_prepare_for_write_() */ |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1094 | |
| 1095 | chain_delete_node_(chain, node->next); |
| 1096 | return true; |
| 1097 | } |
| 1098 | else |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1102 | /* Returns the new length of the chain, or 0 if there was an error. */ |
| 1103 | /* WATCHOUT: This can get called multiple times before a write, so |
| 1104 | * it should still work when this happens. |
| 1105 | */ |
| 1106 | /* WATCHOUT: Make sure to also update the logic in |
| 1107 | * FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes. |
| 1108 | */ |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1109 | static FLAC__off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding) |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1110 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1111 | FLAC__off_t current_length = chain_calculate_length_(chain); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1112 | |
| 1113 | if(use_padding) { |
| 1114 | /* if the metadata shrank and the last block is padding, we just extend the last padding block */ |
| 1115 | if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1116 | const FLAC__off_t delta = chain->initial_length - current_length; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1117 | chain->tail->data->length += delta; |
| 1118 | current_length += delta; |
| 1119 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1120 | } |
| 1121 | /* if the metadata shrank more than 4 bytes then there's room to add another padding block */ |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1122 | else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1123 | FLAC__StreamMetadata *padding; |
| 1124 | FLAC__Metadata_Node *node; |
| 1125 | if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) { |
| 1126 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1127 | return 0; |
| 1128 | } |
| 1129 | padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length); |
| 1130 | if(0 == (node = node_new_())) { |
| 1131 | FLAC__metadata_object_delete(padding); |
| 1132 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1133 | return 0; |
| 1134 | } |
| 1135 | node->data = padding; |
| 1136 | chain_append_node_(chain, node); |
| 1137 | current_length = chain_calculate_length_(chain); |
| 1138 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1139 | } |
| 1140 | /* 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 */ |
| 1141 | else if(current_length > chain->initial_length) { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1142 | const FLAC__off_t delta = current_length - chain->initial_length; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1143 | if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1144 | /* if the delta is exactly the size of the last padding block, remove the padding block */ |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1145 | if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1146 | chain_delete_node_(chain, chain->tail); |
| 1147 | current_length = chain_calculate_length_(chain); |
| 1148 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1149 | } |
| 1150 | /* if there is at least 'delta' bytes of padding, trim the padding down */ |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1151 | else if((FLAC__off_t)chain->tail->data->length >= delta) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1152 | chain->tail->data->length -= delta; |
| 1153 | current_length -= delta; |
| 1154 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1160 | /* check sizes of all metadata blocks; reduce padding size if necessary */ |
| 1161 | { |
| 1162 | FLAC__Metadata_Node *node; |
| 1163 | for (node = chain->head; node; node = node->next) { |
| 1164 | if(node->data->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) { |
| 1165 | if(node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1166 | node->data->length = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1; |
| 1167 | current_length = chain_calculate_length_(chain); |
| 1168 | } else { |
| 1169 | chain->status = FLAC__METADATA_CHAIN_STATUS_BAD_METADATA; |
| 1170 | return 0; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1176 | return current_length; |
| 1177 | } |
| 1178 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1179 | 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) |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 1180 | { |
| 1181 | FLAC__Metadata_Node *node; |
| 1182 | |
| 1183 | FLAC__ASSERT(0 != chain); |
| 1184 | |
| 1185 | /* we assume we're already at the beginning of the file */ |
| 1186 | |
| 1187 | switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) { |
| 1188 | case 0: |
| 1189 | break; |
| 1190 | case 1: |
| 1191 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1192 | return false; |
| 1193 | case 2: |
| 1194 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1195 | return false; |
| 1196 | case 3: |
| 1197 | chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE; |
| 1198 | return false; |
| 1199 | default: |
| 1200 | FLAC__ASSERT(0); |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
| 1204 | { |
| 1205 | FLAC__int64 pos = tell_cb(handle); |
| 1206 | if(pos < 0) { |
| 1207 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1208 | return false; |
| 1209 | } |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1210 | chain->first_offset = (FLAC__off_t)pos; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | { |
| 1214 | FLAC__bool is_last; |
| 1215 | FLAC__MetadataType type; |
| 1216 | unsigned length; |
| 1217 | |
| 1218 | do { |
| 1219 | node = node_new_(); |
| 1220 | if(0 == node) { |
| 1221 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) { |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1226 | node_delete_(node); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 1227 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | node->data = FLAC__metadata_object_new(type); |
| 1232 | if(0 == node->data) { |
| 1233 | node_delete_(node); |
| 1234 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1235 | return false; |
| 1236 | } |
| 1237 | |
| 1238 | node->data->is_last = is_last; |
| 1239 | node->data->length = length; |
| 1240 | |
| 1241 | chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data)); |
| 1242 | if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1243 | node_delete_(node); |
| 1244 | return false; |
| 1245 | } |
| 1246 | chain_append_node_(chain, node); |
| 1247 | } while(!is_last); |
| 1248 | } |
| 1249 | |
| 1250 | { |
| 1251 | FLAC__int64 pos = tell_cb(handle); |
| 1252 | if(pos < 0) { |
| 1253 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1254 | return false; |
| 1255 | } |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1256 | chain->last_offset = (FLAC__off_t)pos; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 1259 | chain->initial_length = chain_calculate_length_(chain); |
| 1260 | |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
Josh Coalson | f9cf27f | 2007-07-25 03:11:00 +0000 | [diff] [blame] | 1264 | static FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1265 | { |
| 1266 | FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1267 | (void)decoder; |
| 1268 | if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1269 | *bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle); |
| 1270 | if(*bytes == 0) |
| 1271 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 1272 | else |
| 1273 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 1274 | } |
| 1275 | else |
| 1276 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 1277 | } |
| 1278 | |
| 1279 | static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 1280 | { |
| 1281 | (void)decoder, (void)frame, (void)buffer, (void)client_data; |
| 1282 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 1283 | } |
| 1284 | |
| 1285 | static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 1286 | { |
| 1287 | FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
Josh Coalson | b0627a4 | 2006-11-17 06:05:02 +0000 | [diff] [blame] | 1288 | FLAC__Metadata_Node *node; |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1289 | |
| 1290 | (void)decoder; |
| 1291 | |
Josh Coalson | b0627a4 | 2006-11-17 06:05:02 +0000 | [diff] [blame] | 1292 | node = node_new_(); |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1293 | if(0 == node) { |
| 1294 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | node->data = FLAC__metadata_object_clone(metadata); |
| 1299 | if(0 == node->data) { |
| 1300 | node_delete_(node); |
| 1301 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | chain_append_node_(chain, node); |
| 1306 | } |
| 1307 | |
| 1308 | static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 1309 | { |
| 1310 | FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1311 | (void)decoder, (void)status; |
| 1312 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1313 | } |
| 1314 | |
| 1315 | static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb) |
| 1316 | { |
| 1317 | FLAC__StreamDecoder *decoder; |
| 1318 | |
| 1319 | FLAC__ASSERT(0 != chain); |
| 1320 | |
| 1321 | /* we assume we're already at the beginning of the file */ |
| 1322 | |
| 1323 | chain->handle = handle; |
| 1324 | chain->read_cb = read_cb; |
| 1325 | if(0 == (decoder = FLAC__stream_decoder_new())) { |
| 1326 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1327 | return false; |
| 1328 | } |
| 1329 | FLAC__stream_decoder_set_metadata_respond_all(decoder); |
| 1330 | if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { |
| 1331 | FLAC__stream_decoder_delete(decoder); |
| 1332 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */ |
| 1337 | |
| 1338 | if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) |
| 1339 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1340 | if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1341 | FLAC__stream_decoder_delete(decoder); |
| 1342 | return false; |
| 1343 | } |
| 1344 | |
| 1345 | FLAC__stream_decoder_delete(decoder); |
| 1346 | |
| 1347 | chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */ |
| 1348 | |
| 1349 | chain->initial_length = chain_calculate_length_(chain); |
| 1350 | |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1354 | 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) |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1355 | { |
| 1356 | FLAC__Metadata_Node *node; |
| 1357 | |
| 1358 | FLAC__ASSERT(0 != chain); |
| 1359 | FLAC__ASSERT(0 != chain->head); |
| 1360 | |
| 1361 | if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1362 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| 1366 | for(node = chain->head; node; node = node->next) { |
| 1367 | if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1368 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1369 | return false; |
| 1370 | } |
| 1371 | if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1372 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1373 | return false; |
| 1374 | } |
| 1375 | } |
| 1376 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 1377 | /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1378 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1379 | chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 1380 | return true; |
| 1381 | } |
| 1382 | |
| 1383 | static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain) |
| 1384 | { |
| 1385 | FILE *file; |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1386 | FLAC__bool ret; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1387 | |
| 1388 | FLAC__ASSERT(0 != chain->filename); |
| 1389 | |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 1390 | if(0 == (file = flac_fopen(chain->filename, "r+b"))) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1391 | chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1395 | /* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */ |
| 1396 | ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_); |
| 1397 | |
| 1398 | fclose(file); |
| 1399 | |
| 1400 | return ret; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix) |
| 1404 | { |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 1405 | FILE *f, *tempfile = NULL; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1406 | char *tempfilename; |
| 1407 | FLAC__Metadata_SimpleIteratorStatus status; |
| 1408 | const FLAC__Metadata_Node *node; |
| 1409 | |
| 1410 | FLAC__ASSERT(0 != chain); |
| 1411 | FLAC__ASSERT(0 != chain->filename); |
| 1412 | FLAC__ASSERT(0 != chain->head); |
| 1413 | |
| 1414 | /* copy the file prefix (data up to first metadata block */ |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 1415 | if(0 == (f = flac_fopen(chain->filename, "rb"))) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1416 | chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1417 | return false; |
| 1418 | } |
| 1419 | if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) { |
| 1420 | chain->status = get_equivalent_status_(status); |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1421 | goto err; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1422 | } |
| 1423 | if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) { |
| 1424 | chain->status = get_equivalent_status_(status); |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1425 | goto err; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | /* write the metadata */ |
| 1429 | for(node = chain->head; node; node = node->next) { |
| 1430 | if(!write_metadata_block_header_(tempfile, &status, node->data)) { |
| 1431 | chain->status = get_equivalent_status_(status); |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1432 | goto err; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1433 | } |
| 1434 | if(!write_metadata_block_data_(tempfile, &status, node->data)) { |
| 1435 | chain->status = get_equivalent_status_(status); |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1436 | goto err; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 1439 | /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1440 | |
| 1441 | /* copy the file postfix (everything after the metadata) */ |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 1442 | if(0 != fseeko(f, chain->last_offset, SEEK_SET)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1443 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1444 | goto err; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1445 | } |
| 1446 | if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1447 | chain->status = get_equivalent_status_(status); |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1448 | goto err; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /* move the tempfile on top of the original */ |
| 1452 | (void)fclose(f); |
| 1453 | if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status)) |
| 1454 | return false; |
| 1455 | |
| 1456 | return true; |
Felipe Contreras | 6c8d740 | 2011-04-28 20:43:21 +0300 | [diff] [blame] | 1457 | |
| 1458 | err: |
| 1459 | (void)fclose(f); |
| 1460 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 1461 | return false; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Josh Coalson | 9aac670 | 2004-09-08 00:50:21 +0000 | [diff] [blame] | 1464 | /* assumes 'handle' is already at beginning of file */ |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1465 | 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) |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1466 | { |
| 1467 | FLAC__Metadata_SimpleIteratorStatus status; |
| 1468 | const FLAC__Metadata_Node *node; |
| 1469 | |
| 1470 | FLAC__ASSERT(0 != chain); |
| 1471 | FLAC__ASSERT(0 == chain->filename); |
| 1472 | FLAC__ASSERT(0 != chain->head); |
| 1473 | |
| 1474 | /* copy the file prefix (data up to first metadata block */ |
| 1475 | if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1476 | chain->status = get_equivalent_status_(status); |
| 1477 | return false; |
| 1478 | } |
| 1479 | |
| 1480 | /* write the metadata */ |
| 1481 | for(node = chain->head; node; node = node->next) { |
| 1482 | if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1483 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1484 | return false; |
| 1485 | } |
| 1486 | if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1487 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1488 | return false; |
| 1489 | } |
| 1490 | } |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 1491 | /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1492 | |
| 1493 | /* copy the file postfix (everything after the metadata) */ |
| 1494 | if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1495 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1496 | return false; |
| 1497 | } |
| 1498 | if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1499 | chain->status = get_equivalent_status_(status); |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1503 | return true; |
| 1504 | } |
| 1505 | |
Josh Coalson | e3ec2ad | 2007-01-31 03:53:22 +0000 | [diff] [blame] | 1506 | FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1507 | { |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 1508 | FLAC__Metadata_Chain *chain = calloc(1, sizeof(FLAC__Metadata_Chain)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1509 | |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1510 | if(0 != chain) |
| 1511 | chain_init_(chain); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1512 | |
| 1513 | return chain; |
| 1514 | } |
| 1515 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1516 | FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1517 | { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1518 | FLAC__ASSERT(0 != chain); |
| 1519 | |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1520 | chain_clear_(chain); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1521 | |
| 1522 | free(chain); |
| 1523 | } |
| 1524 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1525 | FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1526 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1527 | FLAC__Metadata_ChainStatus status; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1528 | |
| 1529 | FLAC__ASSERT(0 != chain); |
| 1530 | |
| 1531 | status = chain->status; |
| 1532 | chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 1533 | return status; |
| 1534 | } |
| 1535 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1536 | static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1537 | { |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 1538 | FILE *file; |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1539 | FLAC__bool ret; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1540 | |
| 1541 | FLAC__ASSERT(0 != chain); |
| 1542 | FLAC__ASSERT(0 != filename); |
| 1543 | |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1544 | chain_clear_(chain); |
| 1545 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1546 | if(0 == (chain->filename = strdup(filename))) { |
| 1547 | chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1551 | chain->is_ogg = is_ogg; |
| 1552 | |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 1553 | if(0 == (file = flac_fopen(filename, "rb"))) { |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 1554 | chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1555 | return false; |
| 1556 | } |
| 1557 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1558 | /* the function also sets chain->status for us */ |
| 1559 | ret = is_ogg? |
| 1560 | chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) : |
| 1561 | chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_) |
| 1562 | ; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1563 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1564 | fclose(file); |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1565 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1566 | return ret; |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1569 | FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename) |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1570 | { |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1571 | return chain_read_(chain, filename, /*is_ogg=*/false); |
| 1572 | } |
| 1573 | |
Josh Coalson | 504dcaf | 2007-09-13 15:42:47 +0000 | [diff] [blame] | 1574 | /*@@@@add to tests*/ |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1575 | FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename) |
| 1576 | { |
| 1577 | return chain_read_(chain, filename, /*is_ogg=*/true); |
| 1578 | } |
| 1579 | |
| 1580 | static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg) |
| 1581 | { |
| 1582 | FLAC__bool ret; |
| 1583 | |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1584 | FLAC__ASSERT(0 != chain); |
| 1585 | |
| 1586 | chain_clear_(chain); |
| 1587 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1588 | if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) { |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1589 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1590 | return false; |
| 1591 | } |
| 1592 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1593 | chain->is_ogg = is_ogg; |
| 1594 | |
Josh Coalson | 9aac670 | 2004-09-08 00:50:21 +0000 | [diff] [blame] | 1595 | /* rewind */ |
| 1596 | if(0 != callbacks.seek(handle, 0, SEEK_SET)) { |
| 1597 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1598 | return false; |
| 1599 | } |
| 1600 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1601 | /* the function also sets chain->status for us */ |
| 1602 | ret = is_ogg? |
| 1603 | chain_read_ogg_cb_(chain, handle, callbacks.read) : |
| 1604 | chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell) |
| 1605 | ; |
Josh Coalson | 91748be | 2004-07-10 22:28:03 +0000 | [diff] [blame] | 1606 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1607 | return ret; |
| 1608 | } |
| 1609 | |
| 1610 | FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1611 | { |
| 1612 | return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false); |
| 1613 | } |
| 1614 | |
Josh Coalson | 504dcaf | 2007-09-13 15:42:47 +0000 | [diff] [blame] | 1615 | /*@@@@add to tests*/ |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1616 | FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1617 | { |
| 1618 | return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Erik de Castro Lopo | fdc1ccf | 2016-05-22 11:06:29 +1000 | [diff] [blame^] | 1621 | typedef enum { |
| 1622 | LBS_NONE = 0, |
| 1623 | LBS_SIZE_CHANGED, |
| 1624 | LBS_BLOCK_ADDED, |
| 1625 | LBS_BLOCK_REMOVED |
| 1626 | } LastBlockState; |
| 1627 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1628 | FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding) |
| 1629 | { |
| 1630 | /* This does all the same checks that are in chain_prepare_for_write_() |
| 1631 | * but doesn't actually alter the chain. Make sure to update the logic |
| 1632 | * here if chain_prepare_for_write_() changes. |
| 1633 | */ |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1634 | FLAC__off_t current_length; |
Erik de Castro Lopo | fdc1ccf | 2016-05-22 11:06:29 +1000 | [diff] [blame^] | 1635 | LastBlockState lbs_state = LBS_NONE; |
| 1636 | unsigned lbs_size = 0; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1637 | |
| 1638 | FLAC__ASSERT(0 != chain); |
| 1639 | |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1640 | current_length = chain_calculate_length_(chain); |
| 1641 | |
Erik de Castro Lopo | fdc1ccf | 2016-05-22 11:06:29 +1000 | [diff] [blame^] | 1642 | if(use_padding) { |
| 1643 | const FLAC__Metadata_Node * const node = chain->tail; |
| 1644 | /* if the metadata shrank and the last block is padding, we just extend the last padding block */ |
| 1645 | if(current_length < chain->initial_length && node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1646 | lbs_state = LBS_SIZE_CHANGED; |
| 1647 | lbs_size = node->data->length + (chain->initial_length - current_length); |
| 1648 | } |
| 1649 | /* if the metadata shrank more than 4 bytes then there's room to add another padding block */ |
| 1650 | else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) { |
| 1651 | lbs_state = LBS_BLOCK_ADDED; |
| 1652 | lbs_size = chain->initial_length - (current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 1653 | } |
| 1654 | /* 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 */ |
| 1655 | else if(current_length > chain->initial_length) { |
| 1656 | const FLAC__off_t delta = current_length - chain->initial_length; |
| 1657 | if(node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1658 | /* if the delta is exactly the size of the last padding block, remove the padding block */ |
| 1659 | if((FLAC__off_t)node->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) { |
| 1660 | lbs_state = LBS_BLOCK_REMOVED; |
| 1661 | lbs_size = 0; |
| 1662 | } |
| 1663 | /* if there is at least 'delta' bytes of padding, trim the padding down */ |
| 1664 | else if((FLAC__off_t)node->data->length >= delta) { |
| 1665 | lbs_state = LBS_SIZE_CHANGED; |
| 1666 | lbs_size = node->data->length - delta; |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1667 | } |
| 1668 | } |
Erik de Castro Lopo | fdc1ccf | 2016-05-22 11:06:29 +1000 | [diff] [blame^] | 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | current_length = 0; |
| 1673 | /* check sizes of all metadata blocks; reduce padding size if necessary */ |
| 1674 | { |
| 1675 | const FLAC__Metadata_Node *node; |
| 1676 | for(node = chain->head; node; node = node->next) { |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1677 | unsigned block_len = node->data->length; |
Erik de Castro Lopo | fdc1ccf | 2016-05-22 11:06:29 +1000 | [diff] [blame^] | 1678 | if(node == chain->tail) { |
| 1679 | if(lbs_state == LBS_BLOCK_REMOVED) |
| 1680 | continue; |
| 1681 | else if(lbs_state == LBS_SIZE_CHANGED) |
| 1682 | block_len = lbs_size; |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1683 | } |
Erik de Castro Lopo | fdc1ccf | 2016-05-22 11:06:29 +1000 | [diff] [blame^] | 1684 | if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) { |
| 1685 | if(node->data->type == FLAC__METADATA_TYPE_PADDING) |
| 1686 | block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1; |
| 1687 | else |
| 1688 | return false /* the return value doesn't matter */; |
Erik de Castro Lopo | 387992b | 2016-05-01 20:57:15 +1000 | [diff] [blame] | 1689 | } |
Erik de Castro Lopo | fdc1ccf | 2016-05-22 11:06:29 +1000 | [diff] [blame^] | 1690 | current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len); |
| 1691 | } |
| 1692 | |
| 1693 | if(lbs_state == LBS_BLOCK_ADDED) { |
| 1694 | /* test added padding block */ |
| 1695 | unsigned block_len = lbs_size; |
| 1696 | if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) |
| 1697 | block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1; |
| 1698 | current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
Josh Coalson | 028d97e | 2004-07-14 00:49:56 +0000 | [diff] [blame] | 1702 | return (current_length != chain->initial_length); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1705 | FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1706 | { |
Erik de Castro Lopo | 7dc5e3a | 2013-03-22 18:22:57 +1100 | [diff] [blame] | 1707 | struct flac_stat_s stats; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1708 | const char *tempfile_path_prefix = 0; |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1709 | FLAC__off_t current_length; |
Josh Coalson | 3ecb117 | 2002-06-15 05:09:47 +0000 | [diff] [blame] | 1710 | |
| 1711 | FLAC__ASSERT(0 != chain); |
| 1712 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1713 | if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1714 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1715 | return false; |
| 1716 | } |
| 1717 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1718 | if (0 == chain->filename) { |
| 1719 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1720 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1723 | current_length = chain_prepare_for_write_(chain, use_padding); |
| 1724 | |
| 1725 | /* a return value of 0 means there was an error; chain->status is already set */ |
| 1726 | if (0 == current_length) |
| 1727 | return false; |
| 1728 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1729 | if(preserve_file_stats) |
| 1730 | get_file_stats_(chain->filename, &stats); |
| 1731 | |
Josh Coalson | 3ecb117 | 2002-06-15 05:09:47 +0000 | [diff] [blame] | 1732 | if(current_length == chain->initial_length) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1733 | if(!chain_rewrite_metadata_in_place_(chain)) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1734 | return false; |
| 1735 | } |
| 1736 | else { |
| 1737 | if(!chain_rewrite_file_(chain, tempfile_path_prefix)) |
| 1738 | return false; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1739 | |
| 1740 | /* recompute lengths and offsets */ |
| 1741 | { |
| 1742 | const FLAC__Metadata_Node *node; |
| 1743 | chain->initial_length = current_length; |
| 1744 | chain->last_offset = chain->first_offset; |
| 1745 | for(node = chain->head; node; node = node->next) |
| 1746 | chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1747 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | if(preserve_file_stats) |
| 1751 | set_file_stats_(chain->filename, &stats); |
| 1752 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1753 | return true; |
| 1754 | } |
| 1755 | |
| 1756 | FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1757 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1758 | FLAC__off_t current_length; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1759 | |
| 1760 | FLAC__ASSERT(0 != chain); |
| 1761 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1762 | if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1763 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1764 | return false; |
| 1765 | } |
| 1766 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1767 | if (0 != chain->filename) { |
| 1768 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1769 | return false; |
| 1770 | } |
| 1771 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1772 | if (0 == callbacks.write || 0 == callbacks.seek) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1773 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1774 | return false; |
| 1775 | } |
| 1776 | |
| 1777 | if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) { |
| 1778 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL; |
| 1779 | return false; |
| 1780 | } |
| 1781 | |
| 1782 | current_length = chain_prepare_for_write_(chain, use_padding); |
| 1783 | |
| 1784 | /* a return value of 0 means there was an error; chain->status is already set */ |
| 1785 | if (0 == current_length) |
| 1786 | return false; |
| 1787 | |
| 1788 | FLAC__ASSERT(current_length == chain->initial_length); |
| 1789 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1790 | return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | 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) |
| 1794 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 1795 | FLAC__off_t current_length; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1796 | |
| 1797 | FLAC__ASSERT(0 != chain); |
| 1798 | |
Josh Coalson | 7d273b4 | 2006-11-15 06:12:30 +0000 | [diff] [blame] | 1799 | if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1800 | chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1801 | return false; |
| 1802 | } |
| 1803 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1804 | if (0 != chain->filename) { |
| 1805 | chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1809 | if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1810 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1811 | return false; |
| 1812 | } |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1813 | if (0 == temp_callbacks.write) { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1814 | chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1815 | return false; |
| 1816 | } |
| 1817 | |
| 1818 | if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) { |
| 1819 | chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL; |
| 1820 | return false; |
| 1821 | } |
| 1822 | |
| 1823 | current_length = chain_prepare_for_write_(chain, use_padding); |
| 1824 | |
| 1825 | /* a return value of 0 means there was an error; chain->status is already set */ |
| 1826 | if (0 == current_length) |
| 1827 | return false; |
| 1828 | |
| 1829 | FLAC__ASSERT(current_length != chain->initial_length); |
| 1830 | |
Josh Coalson | 9aac670 | 2004-09-08 00:50:21 +0000 | [diff] [blame] | 1831 | /* rewind */ |
| 1832 | if(0 != callbacks.seek(handle, 0, SEEK_SET)) { |
| 1833 | chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1834 | return false; |
| 1835 | } |
| 1836 | |
Josh Coalson | 0e46bf5 | 2004-07-15 00:04:46 +0000 | [diff] [blame] | 1837 | if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write)) |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 1838 | return false; |
| 1839 | |
| 1840 | /* recompute lengths and offsets */ |
| 1841 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1842 | const FLAC__Metadata_Node *node; |
Josh Coalson | 3ecb117 | 2002-06-15 05:09:47 +0000 | [diff] [blame] | 1843 | chain->initial_length = current_length; |
Josh Coalson | e2ba10f | 2002-05-14 06:03:17 +0000 | [diff] [blame] | 1844 | chain->last_offset = chain->first_offset; |
| 1845 | for(node = chain->head; node; node = node->next) |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 1846 | chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
Josh Coalson | e2ba10f | 2002-05-14 06:03:17 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
| 1851 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1852 | FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1853 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1854 | FLAC__Metadata_Node *node; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1855 | |
| 1856 | FLAC__ASSERT(0 != chain); |
| 1857 | |
| 1858 | for(node = chain->head; node; ) { |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1859 | if(!chain_merge_adjacent_padding_(chain, node)) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1860 | node = node->next; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1861 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1864 | FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1865 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1866 | FLAC__Metadata_Node *node, *save; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1867 | unsigned i; |
| 1868 | |
| 1869 | FLAC__ASSERT(0 != chain); |
| 1870 | |
| 1871 | /* |
| 1872 | * Don't try and be too smart... this simple algo is good enough for |
| 1873 | * the small number of nodes that we deal with. |
| 1874 | */ |
| 1875 | for(i = 0, node = chain->head; i < chain->nodes; i++) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1876 | if(node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1877 | save = node->next; |
| 1878 | chain_remove_node_(chain, node); |
| 1879 | chain_append_node_(chain, node); |
| 1880 | node = save; |
| 1881 | } |
| 1882 | else { |
| 1883 | node = node->next; |
| 1884 | } |
| 1885 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1886 | |
| 1887 | FLAC__metadata_chain_merge_padding(chain); |
| 1888 | } |
| 1889 | |
| 1890 | |
Josh Coalson | e3ec2ad | 2007-01-31 03:53:22 +0000 | [diff] [blame] | 1891 | FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1892 | { |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 1893 | FLAC__Metadata_Iterator *iterator = calloc(1, sizeof(FLAC__Metadata_Iterator)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1894 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 1895 | /* calloc() implies: |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1896 | iterator->current = 0; |
| 1897 | iterator->chain = 0; |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 1898 | */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1899 | |
| 1900 | return iterator; |
| 1901 | } |
| 1902 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1903 | FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1904 | { |
| 1905 | FLAC__ASSERT(0 != iterator); |
| 1906 | |
| 1907 | free(iterator); |
| 1908 | } |
| 1909 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1910 | FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1911 | { |
| 1912 | FLAC__ASSERT(0 != iterator); |
| 1913 | FLAC__ASSERT(0 != chain); |
| 1914 | FLAC__ASSERT(0 != chain->head); |
| 1915 | |
| 1916 | iterator->chain = chain; |
| 1917 | iterator->current = chain->head; |
| 1918 | } |
| 1919 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1920 | FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1921 | { |
| 1922 | FLAC__ASSERT(0 != iterator); |
| 1923 | |
| 1924 | if(0 == iterator->current || 0 == iterator->current->next) |
| 1925 | return false; |
| 1926 | |
| 1927 | iterator->current = iterator->current->next; |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1931 | FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1932 | { |
| 1933 | FLAC__ASSERT(0 != iterator); |
| 1934 | |
| 1935 | if(0 == iterator->current || 0 == iterator->current->prev) |
| 1936 | return false; |
| 1937 | |
| 1938 | iterator->current = iterator->current->prev; |
| 1939 | return true; |
| 1940 | } |
| 1941 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1942 | FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1943 | { |
| 1944 | FLAC__ASSERT(0 != iterator); |
| 1945 | FLAC__ASSERT(0 != iterator->current); |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1946 | FLAC__ASSERT(0 != iterator->current->data); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1947 | |
| 1948 | return iterator->current->data->type; |
| 1949 | } |
| 1950 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1951 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1952 | { |
| 1953 | FLAC__ASSERT(0 != iterator); |
| 1954 | FLAC__ASSERT(0 != iterator->current); |
| 1955 | |
| 1956 | return iterator->current->data; |
| 1957 | } |
| 1958 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1959 | FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1960 | { |
Josh Coalson | 9b86c96 | 2002-07-10 07:11:15 +0000 | [diff] [blame] | 1961 | FLAC__ASSERT(0 != iterator); |
| 1962 | FLAC__ASSERT(0 != block); |
Josh Coalson | 2745fe7 | 2002-05-11 05:31:56 +0000 | [diff] [blame] | 1963 | return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block); |
| 1964 | } |
| 1965 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1966 | FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1967 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1968 | FLAC__Metadata_Node *save; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1969 | |
| 1970 | FLAC__ASSERT(0 != iterator); |
| 1971 | FLAC__ASSERT(0 != iterator->current); |
| 1972 | |
| 1973 | if(0 == iterator->current->prev) { |
| 1974 | FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 1975 | return false; |
| 1976 | } |
| 1977 | |
| 1978 | save = iterator->current->prev; |
| 1979 | |
| 1980 | if(replace_with_padding) { |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 1981 | FLAC__metadata_object_delete_data(iterator->current->data); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1982 | iterator->current->data->type = FLAC__METADATA_TYPE_PADDING; |
| 1983 | } |
| 1984 | else { |
| 1985 | chain_delete_node_(iterator->chain, iterator->current); |
| 1986 | } |
| 1987 | |
| 1988 | iterator->current = save; |
| 1989 | return true; |
| 1990 | } |
| 1991 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1992 | FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1993 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1994 | FLAC__Metadata_Node *node; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1995 | |
| 1996 | FLAC__ASSERT(0 != iterator); |
| 1997 | FLAC__ASSERT(0 != iterator->current); |
Josh Coalson | 9b86c96 | 2002-07-10 07:11:15 +0000 | [diff] [blame] | 1998 | FLAC__ASSERT(0 != block); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 1999 | |
| 2000 | if(block->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 2001 | return false; |
| 2002 | |
| 2003 | if(0 == iterator->current->prev) { |
| 2004 | FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 2005 | return false; |
| 2006 | } |
| 2007 | |
| 2008 | if(0 == (node = node_new_())) |
| 2009 | return false; |
| 2010 | |
| 2011 | node->data = block; |
| 2012 | iterator_insert_node_(iterator, node); |
| 2013 | iterator->current = node; |
| 2014 | return true; |
| 2015 | } |
| 2016 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 2017 | FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2018 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2019 | FLAC__Metadata_Node *node; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2020 | |
| 2021 | FLAC__ASSERT(0 != iterator); |
| 2022 | FLAC__ASSERT(0 != iterator->current); |
Josh Coalson | 9b86c96 | 2002-07-10 07:11:15 +0000 | [diff] [blame] | 2023 | FLAC__ASSERT(0 != block); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2024 | |
| 2025 | if(block->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 2026 | return false; |
| 2027 | |
| 2028 | if(0 == (node = node_new_())) |
| 2029 | return false; |
| 2030 | |
| 2031 | node->data = block; |
| 2032 | iterator_insert_node_after_(iterator, node); |
| 2033 | iterator->current = node; |
| 2034 | return true; |
| 2035 | } |
| 2036 | |
| 2037 | |
| 2038 | /**************************************************************************** |
| 2039 | * |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2040 | * Local function definitions |
| 2041 | * |
| 2042 | ***************************************************************************/ |
| 2043 | |
| 2044 | void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes) |
| 2045 | { |
| 2046 | unsigned i; |
| 2047 | |
| 2048 | b += bytes; |
| 2049 | |
| 2050 | for(i = 0; i < bytes; i++) { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 2051 | *(--b) = (FLAC__byte)(val & 0xff); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2052 | val >>= 8; |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes) |
| 2057 | { |
| 2058 | unsigned i; |
| 2059 | |
| 2060 | for(i = 0; i < bytes; i++) { |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 2061 | *(b++) = (FLAC__byte)(val & 0xff); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2062 | val >>= 8; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes) |
| 2067 | { |
| 2068 | unsigned i; |
| 2069 | |
| 2070 | b += bytes; |
| 2071 | |
| 2072 | for(i = 0; i < bytes; i++) { |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2073 | *(--b) = (FLAC__byte)(val & 0xff); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2074 | val >>= 8; |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes) |
| 2079 | { |
| 2080 | FLAC__uint32 ret = 0; |
| 2081 | unsigned i; |
| 2082 | |
| 2083 | for(i = 0; i < bytes; i++) |
| 2084 | ret = (ret << 8) | (FLAC__uint32)(*b++); |
| 2085 | |
| 2086 | return ret; |
| 2087 | } |
| 2088 | |
| 2089 | FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes) |
| 2090 | { |
| 2091 | FLAC__uint32 ret = 0; |
| 2092 | unsigned i; |
| 2093 | |
| 2094 | b += bytes; |
| 2095 | |
| 2096 | for(i = 0; i < bytes; i++) |
| 2097 | ret = (ret << 8) | (FLAC__uint32)(*--b); |
| 2098 | |
| 2099 | return ret; |
| 2100 | } |
| 2101 | |
| 2102 | FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes) |
| 2103 | { |
| 2104 | FLAC__uint64 ret = 0; |
| 2105 | unsigned i; |
| 2106 | |
| 2107 | for(i = 0; i < bytes; i++) |
| 2108 | ret = (ret << 8) | (FLAC__uint64)(*b++); |
| 2109 | |
| 2110 | return ret; |
| 2111 | } |
| 2112 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2113 | FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2114 | { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2115 | FLAC__ASSERT(0 != iterator); |
| 2116 | FLAC__ASSERT(0 != iterator->file); |
| 2117 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2118 | if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2119 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2120 | return false; |
| 2121 | } |
| 2122 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2123 | return true; |
| 2124 | } |
| 2125 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2126 | FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2127 | { |
| 2128 | FLAC__ASSERT(0 != iterator); |
| 2129 | FLAC__ASSERT(0 != iterator->file); |
| 2130 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2131 | iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2132 | |
| 2133 | return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 2134 | } |
| 2135 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2136 | FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length) |
| 2137 | { |
| 2138 | FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH]; |
| 2139 | |
| 2140 | if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH) |
| 2141 | return false; |
| 2142 | |
| 2143 | *is_last = raw_header[0] & 0x80? true : false; |
| 2144 | *type = (FLAC__MetadataType)(raw_header[0] & 0x7f); |
| 2145 | *length = unpack_uint32_(raw_header + 1, 3); |
| 2146 | |
| 2147 | /* Note that we don't check: |
| 2148 | * if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED) |
| 2149 | * we just will read in an opaque block |
| 2150 | */ |
| 2151 | |
| 2152 | return true; |
| 2153 | } |
| 2154 | |
| 2155 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block) |
| 2156 | { |
| 2157 | switch(block->type) { |
| 2158 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 2159 | return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info); |
| 2160 | case FLAC__METADATA_TYPE_PADDING: |
| 2161 | return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length); |
| 2162 | case FLAC__METADATA_TYPE_APPLICATION: |
| 2163 | return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length); |
| 2164 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 2165 | return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length); |
| 2166 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2167 | return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, seek_cb, &block->data.vorbis_comment, block->length); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2168 | case FLAC__METADATA_TYPE_CUESHEET: |
| 2169 | return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet); |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2170 | case FLAC__METADATA_TYPE_PICTURE: |
| 2171 | return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2172 | default: |
| 2173 | return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length); |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2178 | { |
| 2179 | FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b; |
| 2180 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2181 | if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2182 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2183 | |
| 2184 | b = buffer; |
| 2185 | |
Josh Coalson | f0a8c4f | 2003-01-15 03:17:51 +0000 | [diff] [blame] | 2186 | /* we are using hardcoded numbers for simplicity but we should |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2187 | * probably eventually write a bit-level unpacker and use the |
| 2188 | * _STREAMINFO_ constants. |
| 2189 | */ |
| 2190 | block->min_blocksize = unpack_uint32_(b, 2); b += 2; |
| 2191 | block->max_blocksize = unpack_uint32_(b, 2); b += 2; |
| 2192 | block->min_framesize = unpack_uint32_(b, 3); b += 3; |
| 2193 | block->max_framesize = unpack_uint32_(b, 3); b += 3; |
| 2194 | block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4); |
| 2195 | block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1; |
Josh Coalson | b4e186a | 2002-08-12 21:35:43 +0000 | [diff] [blame] | 2196 | block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2197 | block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4); |
| 2198 | memcpy(block->md5sum, b+8, 16); |
| 2199 | |
| 2200 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2201 | } |
| 2202 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2203 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2204 | { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2205 | (void)block; /* nothing to do; we don't care about reading the padding bytes */ |
| 2206 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2207 | if(0 != seek_cb(handle, block_length, SEEK_CUR)) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2208 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2209 | |
| 2210 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2211 | } |
| 2212 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2213 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2214 | { |
| 2215 | const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 2216 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2217 | if(read_cb(block->id, 1, id_bytes, handle) != id_bytes) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2218 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2219 | |
Josh Coalson | 0f008d2 | 2007-09-11 04:49:56 +0000 | [diff] [blame] | 2220 | if(block_length < id_bytes) |
| 2221 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2222 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2223 | block_length -= id_bytes; |
| 2224 | |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2225 | if(block_length == 0) { |
| 2226 | block->data = 0; |
| 2227 | } |
| 2228 | else { |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 2229 | if(0 == (block->data = malloc(block_length))) |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2230 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2231 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2232 | if(read_cb(block->data, 1, block_length, handle) != block_length) |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2233 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2234 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2235 | |
| 2236 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2237 | } |
| 2238 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2239 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2240 | { |
| 2241 | unsigned i; |
| 2242 | FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH]; |
| 2243 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2244 | FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0); |
| 2245 | |
| 2246 | block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 2247 | |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2248 | if(block->num_points == 0) |
| 2249 | block->points = 0; |
Erik de Castro Lopo | 8749dc2 | 2012-06-22 14:23:56 +1000 | [diff] [blame] | 2250 | else if(0 == (block->points = safe_malloc_mul_2op_p(block->num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2251 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2252 | |
| 2253 | for(i = 0; i < block->num_points; i++) { |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2254 | if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2255 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
Josh Coalson | f0a8c4f | 2003-01-15 03:17:51 +0000 | [diff] [blame] | 2256 | /* some MAGIC NUMBERs here */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2257 | block->points[i].sample_number = unpack_uint64_(buffer, 8); |
| 2258 | block->points[i].stream_offset = unpack_uint64_(buffer+8, 8); |
| 2259 | block->points[i].frame_samples = unpack_uint32_(buffer+16, 2); |
| 2260 | } |
| 2261 | |
| 2262 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2263 | } |
| 2264 | |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2265 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry, unsigned max_length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2266 | { |
| 2267 | const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 2268 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2269 | |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2270 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2271 | |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2272 | if(max_length < entry_length_len) |
| 2273 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2274 | |
| 2275 | max_length -= entry_length_len; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2276 | if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2277 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
Josh Coalson | eb643a2 | 2002-05-20 05:58:50 +0000 | [diff] [blame] | 2278 | entry->length = unpack_uint32_little_endian_(buffer, entry_length_len); |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2279 | if(max_length < entry->length) { |
| 2280 | entry->length = 0; |
| 2281 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2282 | } else max_length -= entry->length; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2283 | |
Josh Coalson | 6ffe8da | 2002-10-26 04:35:00 +0000 | [diff] [blame] | 2284 | if(0 != entry->entry) |
| 2285 | free(entry->entry); |
| 2286 | |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2287 | if(entry->length == 0) { |
| 2288 | entry->entry = 0; |
| 2289 | } |
| 2290 | else { |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 2291 | if(0 == (entry->entry = safe_malloc_add_2op_(entry->length, /*+*/1))) |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2292 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2293 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2294 | if(read_cb(entry->entry, 1, entry->length, handle) != entry->length) |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2295 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
Josh Coalson | def597e | 2004-12-30 00:59:30 +0000 | [diff] [blame] | 2296 | |
| 2297 | entry->entry[entry->length] = '\0'; |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2298 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2299 | |
| 2300 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2301 | } |
| 2302 | |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2303 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, unsigned block_length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2304 | { |
| 2305 | unsigned i; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2306 | FLAC__Metadata_SimpleIteratorStatus status; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2307 | const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8; |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 2308 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2309 | |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2310 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2311 | |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2312 | status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string), block_length); |
| 2313 | if(block_length >= 4) |
| 2314 | block_length -= 4; |
| 2315 | if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA) |
| 2316 | goto skip; |
| 2317 | else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2318 | return status; |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2319 | block_length -= block->vendor_string.length; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2320 | |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2321 | if(block_length < num_comments_len) goto skip; else block_length -= num_comments_len; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2322 | if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2323 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2324 | block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len); |
| 2325 | |
Josh Coalson | 7294309 | 2002-05-22 05:35:43 +0000 | [diff] [blame] | 2326 | if(block->num_comments == 0) { |
| 2327 | block->comments = 0; |
| 2328 | } |
Erik de Castro Lopo | ff50779 | 2015-07-05 21:21:44 +1000 | [diff] [blame] | 2329 | else if(0 == (block->comments = calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { |
| 2330 | block->num_comments = 0; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2331 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
Erik de Castro Lopo | ff50779 | 2015-07-05 21:21:44 +1000 | [diff] [blame] | 2332 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2333 | |
| 2334 | for(i = 0; i < block->num_comments; i++) { |
Erik de Castro Lopo | 25db9b2 | 2014-09-27 08:52:27 +1000 | [diff] [blame] | 2335 | status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i, block_length); |
| 2336 | if(block_length >= 4) block_length -= 4; |
| 2337 | if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA) { |
| 2338 | block->num_comments = i; |
| 2339 | goto skip; |
| 2340 | } |
| 2341 | else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) return status; |
| 2342 | block_length -= block->comments[i].length; |
| 2343 | } |
| 2344 | |
| 2345 | skip: |
| 2346 | if(block_length > 0) { |
| 2347 | /* bad metadata */ |
| 2348 | if(0 != seek_cb(handle, block_length, SEEK_CUR)) |
| 2349 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2353 | } |
| 2354 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2355 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2356 | { |
| 2357 | unsigned i, len; |
| 2358 | FLAC__byte buffer[32]; /* asserted below that this is big enough */ |
| 2359 | |
| 2360 | FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64)); |
| 2361 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8); |
| 2362 | 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); |
| 2363 | |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2364 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0); |
| 2365 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2366 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2367 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2368 | track->offset = unpack_uint64_(buffer, len); |
| 2369 | |
| 2370 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0); |
| 2371 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2372 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2373 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2374 | track->number = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2375 | |
| 2376 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 2377 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2378 | if(read_cb(track->isrc, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2379 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2380 | |
| 2381 | 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); |
| 2382 | len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2383 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2384 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2385 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1); |
| 2386 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1); |
| 2387 | track->type = buffer[0] >> 7; |
| 2388 | track->pre_emphasis = (buffer[0] >> 6) & 1; |
| 2389 | |
| 2390 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0); |
| 2391 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2392 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2393 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2394 | track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2395 | |
| 2396 | if(track->num_indices == 0) { |
| 2397 | track->indices = 0; |
| 2398 | } |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 2399 | else if(0 == (track->indices = calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2400 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2401 | |
| 2402 | for(i = 0; i < track->num_indices; i++) { |
| 2403 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0); |
| 2404 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2405 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2406 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2407 | track->indices[i].offset = unpack_uint64_(buffer, len); |
| 2408 | |
| 2409 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0); |
| 2410 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2411 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2412 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2413 | track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2414 | |
| 2415 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0); |
| 2416 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2417 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2418 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2419 | } |
| 2420 | |
| 2421 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2422 | } |
| 2423 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2424 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2425 | { |
| 2426 | unsigned i, len; |
| 2427 | FLAC__Metadata_SimpleIteratorStatus status; |
Josh Coalson | bfc8e31 | 2002-11-21 09:00:25 +0000 | [diff] [blame] | 2428 | FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */ |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2429 | |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 2430 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer)); |
Josh Coalson | bfc8e31 | 2002-11-21 09:00:25 +0000 | [diff] [blame] | 2431 | FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer)); |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2432 | |
| 2433 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 2434 | len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2435 | if(read_cb(block->media_catalog_number, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2436 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2437 | |
| 2438 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0); |
| 2439 | len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2440 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2441 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2442 | block->lead_in = unpack_uint64_(buffer, len); |
| 2443 | |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 2444 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0); |
| 2445 | len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2446 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | 38162d5 | 2002-11-16 06:31:54 +0000 | [diff] [blame] | 2447 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 2448 | block->is_cd = buffer[0]&0x80? true : false; |
Josh Coalson | 38162d5 | 2002-11-16 06:31:54 +0000 | [diff] [blame] | 2449 | |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2450 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0); |
| 2451 | len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2452 | if(read_cb(buffer, 1, len, handle) != len) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2453 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2454 | block->num_tracks = unpack_uint32_(buffer, len); |
| 2455 | |
| 2456 | if(block->num_tracks == 0) { |
| 2457 | block->tracks = 0; |
| 2458 | } |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 2459 | else if(0 == (block->tracks = calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2460 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2461 | |
| 2462 | for(i = 0; i < block->num_tracks; i++) { |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2463 | if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i))) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2464 | return status; |
| 2465 | } |
| 2466 | |
| 2467 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2468 | } |
| 2469 | |
Josh Coalson | f9cf27f | 2007-07-25 03:11:00 +0000 | [diff] [blame] | 2470 | static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cstring_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__byte **data, FLAC__uint32 *length, FLAC__uint32 length_len) |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2471 | { |
| 2472 | FLAC__byte buffer[sizeof(FLAC__uint32)]; |
| 2473 | |
| 2474 | FLAC__ASSERT(0 != data); |
| 2475 | FLAC__ASSERT(length_len%8 == 0); |
| 2476 | |
| 2477 | length_len /= 8; /* convert to bytes */ |
| 2478 | |
| 2479 | FLAC__ASSERT(sizeof(buffer) >= length_len); |
| 2480 | |
| 2481 | if(read_cb(buffer, 1, length_len, handle) != length_len) |
| 2482 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2483 | *length = unpack_uint32_(buffer, length_len); |
| 2484 | |
| 2485 | if(0 != *data) |
| 2486 | free(*data); |
| 2487 | |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 2488 | if(0 == (*data = safe_malloc_add_2op_(*length, /*+*/1))) |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2489 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2490 | |
| 2491 | if(*length > 0) { |
| 2492 | if(read_cb(*data, 1, *length, handle) != *length) |
| 2493 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2494 | } |
| 2495 | |
| 2496 | (*data)[*length] = '\0'; |
| 2497 | |
| 2498 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2499 | } |
| 2500 | |
| 2501 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block) |
| 2502 | { |
| 2503 | FLAC__Metadata_SimpleIteratorStatus status; |
| 2504 | FLAC__byte buffer[4]; /* asserted below that this is big enough */ |
| 2505 | FLAC__uint32 len; |
| 2506 | |
| 2507 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8); |
| 2508 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8); |
| 2509 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8); |
| 2510 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8); |
Josh Coalson | 74ed294 | 2006-09-23 23:15:05 +0000 | [diff] [blame] | 2511 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8); |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2512 | |
| 2513 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0); |
| 2514 | len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8; |
| 2515 | if(read_cb(buffer, 1, len, handle) != len) |
| 2516 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2517 | block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len); |
| 2518 | |
| 2519 | 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) |
| 2520 | return status; |
| 2521 | |
| 2522 | 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) |
| 2523 | return status; |
| 2524 | |
| 2525 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0); |
| 2526 | len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8; |
| 2527 | if(read_cb(buffer, 1, len, handle) != len) |
| 2528 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2529 | block->width = unpack_uint32_(buffer, len); |
| 2530 | |
| 2531 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0); |
| 2532 | len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8; |
| 2533 | if(read_cb(buffer, 1, len, handle) != len) |
| 2534 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2535 | block->height = unpack_uint32_(buffer, len); |
| 2536 | |
| 2537 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0); |
| 2538 | len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8; |
| 2539 | if(read_cb(buffer, 1, len, handle) != len) |
| 2540 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2541 | block->depth = unpack_uint32_(buffer, len); |
| 2542 | |
Josh Coalson | 74ed294 | 2006-09-23 23:15:05 +0000 | [diff] [blame] | 2543 | FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0); |
| 2544 | len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8; |
| 2545 | if(read_cb(buffer, 1, len, handle) != len) |
| 2546 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2547 | block->colors = unpack_uint32_(buffer, len); |
| 2548 | |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2549 | /* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */ |
| 2550 | 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) |
| 2551 | return status; |
| 2552 | |
| 2553 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2554 | } |
| 2555 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2556 | FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length) |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 2557 | { |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 2558 | if(block_length == 0) { |
| 2559 | block->data = 0; |
| 2560 | } |
| 2561 | else { |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 2562 | if(0 == (block->data = malloc(block_length))) |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 2563 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2564 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 2565 | if(read_cb(block->data, 1, block_length, handle) != block_length) |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 2566 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2567 | } |
| 2568 | |
| 2569 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2570 | } |
| 2571 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2572 | FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2573 | { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2574 | FLAC__ASSERT(0 != file); |
| 2575 | FLAC__ASSERT(0 != status); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2576 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2577 | if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2578 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 2579 | return false; |
| 2580 | } |
| 2581 | |
| 2582 | return true; |
| 2583 | } |
| 2584 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2585 | FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2586 | { |
| 2587 | FLAC__ASSERT(0 != file); |
| 2588 | FLAC__ASSERT(0 != status); |
| 2589 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2590 | if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) { |
| 2591 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2592 | return true; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2593 | } |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2594 | else { |
| 2595 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 2596 | return false; |
| 2597 | } |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2600 | FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block) |
| 2601 | { |
| 2602 | FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH]; |
| 2603 | |
| 2604 | FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN)); |
Erik de Castro Lopo | 94386fd | 2016-05-01 20:34:26 +1000 | [diff] [blame] | 2605 | /* double protection */ |
| 2606 | if(block->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) |
| 2607 | return false; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2608 | |
| 2609 | buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type; |
| 2610 | pack_uint32_(block->length, buffer + 1, 3); |
| 2611 | |
| 2612 | if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH) |
| 2613 | return false; |
| 2614 | |
| 2615 | return true; |
| 2616 | } |
| 2617 | |
| 2618 | FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block) |
| 2619 | { |
| 2620 | FLAC__ASSERT(0 != block); |
| 2621 | |
| 2622 | switch(block->type) { |
| 2623 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 2624 | return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info); |
| 2625 | case FLAC__METADATA_TYPE_PADDING: |
| 2626 | return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length); |
| 2627 | case FLAC__METADATA_TYPE_APPLICATION: |
| 2628 | return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length); |
| 2629 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 2630 | return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table); |
| 2631 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 2632 | return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment); |
| 2633 | case FLAC__METADATA_TYPE_CUESHEET: |
| 2634 | return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet); |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2635 | case FLAC__METADATA_TYPE_PICTURE: |
| 2636 | return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2637 | default: |
| 2638 | return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length); |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2643 | { |
| 2644 | FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH]; |
| 2645 | const unsigned channels1 = block->channels - 1; |
| 2646 | const unsigned bps1 = block->bits_per_sample - 1; |
| 2647 | |
Josh Coalson | f0a8c4f | 2003-01-15 03:17:51 +0000 | [diff] [blame] | 2648 | /* we are using hardcoded numbers for simplicity but we should |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2649 | * probably eventually write a bit-level packer and use the |
| 2650 | * _STREAMINFO_ constants. |
| 2651 | */ |
| 2652 | pack_uint32_(block->min_blocksize, buffer, 2); |
| 2653 | pack_uint32_(block->max_blocksize, buffer+2, 2); |
| 2654 | pack_uint32_(block->min_framesize, buffer+4, 3); |
| 2655 | pack_uint32_(block->max_framesize, buffer+7, 3); |
| 2656 | buffer[10] = (block->sample_rate >> 12) & 0xff; |
| 2657 | buffer[11] = (block->sample_rate >> 4) & 0xff; |
| 2658 | buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 2659 | buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2660 | pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4); |
| 2661 | memcpy(buffer+18, block->md5sum, 16); |
| 2662 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2663 | if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) |
| 2664 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2665 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2666 | return true; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2669 | FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2670 | { |
| 2671 | unsigned i, n = block_length; |
| 2672 | FLAC__byte buffer[1024]; |
| 2673 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2674 | (void)block; |
| 2675 | |
| 2676 | memset(buffer, 0, 1024); |
| 2677 | |
| 2678 | for(i = 0; i < n/1024; i++) |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2679 | if(write_cb(buffer, 1, 1024, handle) != 1024) |
| 2680 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2681 | |
| 2682 | n %= 1024; |
Josh Coalson | 765ff50 | 2002-08-27 05:46:11 +0000 | [diff] [blame] | 2683 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2684 | if(write_cb(buffer, 1, n, handle) != n) |
| 2685 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2686 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2687 | return true; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2690 | FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2691 | { |
| 2692 | const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 2693 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2694 | if(write_cb(block->id, 1, id_bytes, handle) != id_bytes) |
| 2695 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2696 | |
| 2697 | block_length -= id_bytes; |
| 2698 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2699 | if(write_cb(block->data, 1, block_length, handle) != block_length) |
| 2700 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2701 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2702 | return true; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2705 | FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2706 | { |
| 2707 | unsigned i; |
| 2708 | FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH]; |
| 2709 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2710 | for(i = 0; i < block->num_points; i++) { |
Josh Coalson | f0a8c4f | 2003-01-15 03:17:51 +0000 | [diff] [blame] | 2711 | /* some MAGIC NUMBERs here */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2712 | pack_uint64_(block->points[i].sample_number, buffer, 8); |
| 2713 | pack_uint64_(block->points[i].stream_offset, buffer+8, 8); |
| 2714 | pack_uint32_(block->points[i].frame_samples, buffer+16, 2); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2715 | if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) |
| 2716 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2719 | return true; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2722 | FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2723 | { |
| 2724 | unsigned i; |
| 2725 | const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; |
| 2726 | const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8; |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 2727 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2728 | |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 2729 | FLAC__ASSERT(flac_max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2730 | |
| 2731 | pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2732 | if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2733 | return false; |
| 2734 | if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length) |
| 2735 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2736 | |
| 2737 | pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2738 | if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len) |
| 2739 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2740 | |
| 2741 | for(i = 0; i < block->num_comments; i++) { |
| 2742 | pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2743 | if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2744 | return false; |
| 2745 | if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length) |
| 2746 | return false; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2749 | return true; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2752 | FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2753 | { |
| 2754 | unsigned i, j, len; |
Josh Coalson | 38162d5 | 2002-11-16 06:31:54 +0000 | [diff] [blame] | 2755 | FLAC__byte buffer[1024]; /* asserted below that this is big enough */ |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2756 | |
| 2757 | FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64)); |
Josh Coalson | 38162d5 | 2002-11-16 06:31:54 +0000 | [diff] [blame] | 2758 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8); |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 2759 | 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); |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2760 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8); |
| 2761 | |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2762 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 2763 | len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2764 | if(write_cb(block->media_catalog_number, 1, len, handle) != len) |
| 2765 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2766 | |
| 2767 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0); |
| 2768 | len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8; |
| 2769 | pack_uint64_(block->lead_in, buffer, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2770 | if(write_cb(buffer, 1, len, handle) != len) |
| 2771 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2772 | |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 2773 | FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0); |
| 2774 | len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8; |
Josh Coalson | 38162d5 | 2002-11-16 06:31:54 +0000 | [diff] [blame] | 2775 | memset(buffer, 0, len); |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 2776 | if(block->is_cd) |
| 2777 | buffer[0] |= 0x80; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2778 | if(write_cb(buffer, 1, len, handle) != len) |
| 2779 | return false; |
Josh Coalson | 38162d5 | 2002-11-16 06:31:54 +0000 | [diff] [blame] | 2780 | |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2781 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0); |
| 2782 | len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8; |
| 2783 | pack_uint32_(block->num_tracks, buffer, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2784 | if(write_cb(buffer, 1, len, handle) != len) |
| 2785 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2786 | |
| 2787 | for(i = 0; i < block->num_tracks; i++) { |
| 2788 | FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i; |
| 2789 | |
| 2790 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0); |
| 2791 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8; |
| 2792 | pack_uint64_(track->offset, buffer, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2793 | if(write_cb(buffer, 1, len, handle) != len) |
| 2794 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2795 | |
| 2796 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0); |
| 2797 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8; |
| 2798 | pack_uint32_(track->number, buffer, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2799 | if(write_cb(buffer, 1, len, handle) != len) |
| 2800 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2801 | |
| 2802 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 2803 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2804 | if(write_cb(track->isrc, 1, len, handle) != len) |
| 2805 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2806 | |
| 2807 | 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); |
| 2808 | len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8; |
| 2809 | memset(buffer, 0, len); |
| 2810 | buffer[0] = (track->type << 7) | (track->pre_emphasis << 6); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2811 | if(write_cb(buffer, 1, len, handle) != len) |
| 2812 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2813 | |
| 2814 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0); |
| 2815 | len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8; |
| 2816 | pack_uint32_(track->num_indices, buffer, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2817 | if(write_cb(buffer, 1, len, handle) != len) |
| 2818 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2819 | |
| 2820 | for(j = 0; j < track->num_indices; j++) { |
Erik de Castro Lopo | ae7eda1 | 2013-04-05 20:21:22 +1100 | [diff] [blame] | 2821 | FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + j; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2822 | |
| 2823 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0); |
| 2824 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8; |
Erik de Castro Lopo | ae7eda1 | 2013-04-05 20:21:22 +1100 | [diff] [blame] | 2825 | pack_uint64_(indx->offset, buffer, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2826 | if(write_cb(buffer, 1, len, handle) != len) |
| 2827 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2828 | |
| 2829 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0); |
| 2830 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8; |
Erik de Castro Lopo | ae7eda1 | 2013-04-05 20:21:22 +1100 | [diff] [blame] | 2831 | pack_uint32_(indx->number, buffer, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2832 | if(write_cb(buffer, 1, len, handle) != len) |
| 2833 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2834 | |
| 2835 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0); |
| 2836 | len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8; |
| 2837 | memset(buffer, 0, len); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2838 | if(write_cb(buffer, 1, len, handle) != len) |
| 2839 | return false; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2843 | return true; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2846 | FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block) |
| 2847 | { |
| 2848 | unsigned len; |
| 2849 | size_t slen; |
| 2850 | FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2851 | |
| 2852 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8); |
| 2853 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8); |
| 2854 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8); |
| 2855 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8); |
| 2856 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8); |
| 2857 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8); |
Josh Coalson | 74ed294 | 2006-09-23 23:15:05 +0000 | [diff] [blame] | 2858 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8); |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2859 | FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8); |
| 2860 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8); |
| 2861 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8); |
| 2862 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8); |
| 2863 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8); |
| 2864 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8); |
| 2865 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8); |
Josh Coalson | 74ed294 | 2006-09-23 23:15:05 +0000 | [diff] [blame] | 2866 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8); |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2867 | FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8); |
| 2868 | |
| 2869 | len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8; |
| 2870 | pack_uint32_(block->type, buffer, len); |
| 2871 | if(write_cb(buffer, 1, len, handle) != len) |
| 2872 | return false; |
| 2873 | |
| 2874 | len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8; |
| 2875 | slen = strlen(block->mime_type); |
| 2876 | pack_uint32_(slen, buffer, len); |
| 2877 | if(write_cb(buffer, 1, len, handle) != len) |
| 2878 | return false; |
| 2879 | if(write_cb(block->mime_type, 1, slen, handle) != slen) |
| 2880 | return false; |
| 2881 | |
| 2882 | len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8; |
| 2883 | slen = strlen((const char *)block->description); |
| 2884 | pack_uint32_(slen, buffer, len); |
| 2885 | if(write_cb(buffer, 1, len, handle) != len) |
| 2886 | return false; |
| 2887 | if(write_cb(block->description, 1, slen, handle) != slen) |
| 2888 | return false; |
| 2889 | |
| 2890 | len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8; |
| 2891 | pack_uint32_(block->width, buffer, len); |
| 2892 | if(write_cb(buffer, 1, len, handle) != len) |
| 2893 | return false; |
| 2894 | |
| 2895 | len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8; |
| 2896 | pack_uint32_(block->height, buffer, len); |
| 2897 | if(write_cb(buffer, 1, len, handle) != len) |
| 2898 | return false; |
| 2899 | |
| 2900 | len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8; |
| 2901 | pack_uint32_(block->depth, buffer, len); |
| 2902 | if(write_cb(buffer, 1, len, handle) != len) |
| 2903 | return false; |
| 2904 | |
Josh Coalson | 74ed294 | 2006-09-23 23:15:05 +0000 | [diff] [blame] | 2905 | len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8; |
| 2906 | pack_uint32_(block->colors, buffer, len); |
| 2907 | if(write_cb(buffer, 1, len, handle) != len) |
| 2908 | return false; |
| 2909 | |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 2910 | len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8; |
| 2911 | pack_uint32_(block->data_length, buffer, len); |
| 2912 | if(write_cb(buffer, 1, len, handle) != len) |
| 2913 | return false; |
| 2914 | if(write_cb(block->data, 1, block->data_length, handle) != block->data_length) |
| 2915 | return false; |
| 2916 | |
| 2917 | return true; |
| 2918 | } |
| 2919 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2920 | FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length) |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 2921 | { |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2922 | if(write_cb(block->data, 1, block_length, handle) != block_length) |
| 2923 | return false; |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 2924 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 2925 | return true; |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2928 | FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2929 | { |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 2930 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2931 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2932 | return false; |
| 2933 | } |
| 2934 | |
| 2935 | if(!write_metadata_block_header_(iterator->file, &iterator->status, block)) |
| 2936 | return false; |
| 2937 | |
| 2938 | if(!write_metadata_block_data_(iterator->file, &iterator->status, block)) |
| 2939 | return false; |
| 2940 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 2941 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2942 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2943 | return false; |
| 2944 | } |
| 2945 | |
| 2946 | return read_metadata_block_header_(iterator); |
| 2947 | } |
| 2948 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2949 | FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2950 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2951 | FLAC__StreamMetadata *padding; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2952 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 2953 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2954 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2955 | return false; |
| 2956 | } |
| 2957 | |
| 2958 | block->is_last = false; |
| 2959 | |
| 2960 | if(!write_metadata_block_header_(iterator->file, &iterator->status, block)) |
| 2961 | return false; |
| 2962 | |
| 2963 | if(!write_metadata_block_data_(iterator->file, &iterator->status, block)) |
| 2964 | return false; |
| 2965 | |
| 2966 | if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) |
| 2967 | return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2968 | |
| 2969 | padding->is_last = padding_is_last; |
| 2970 | padding->length = padding_length; |
| 2971 | |
| 2972 | if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) { |
| 2973 | FLAC__metadata_object_delete(padding); |
| 2974 | return false; |
| 2975 | } |
| 2976 | |
| 2977 | if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) { |
| 2978 | FLAC__metadata_object_delete(padding); |
| 2979 | return false; |
| 2980 | } |
| 2981 | |
| 2982 | FLAC__metadata_object_delete(padding); |
| 2983 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 2984 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2985 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2986 | return false; |
| 2987 | } |
| 2988 | |
| 2989 | return read_metadata_block_header_(iterator); |
| 2990 | } |
| 2991 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 2992 | FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2993 | { |
Erik de Castro Lopo | 5de949f | 2012-04-05 21:24:06 +1000 | [diff] [blame] | 2994 | FILE *tempfile = NULL; |
| 2995 | char *tempfilename = NULL; |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 2996 | int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */ |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 2997 | FLAC__off_t fixup_is_last_flag_offset = -1; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 2998 | |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 2999 | FLAC__ASSERT(0 != block || append == false); |
| 3000 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 3001 | if(iterator->is_last) { |
| 3002 | if(append) { |
| 3003 | fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */ |
| 3004 | fixup_is_last_flag_offset = iterator->offset[iterator->depth]; |
| 3005 | } |
| 3006 | else if(0 == block) { |
| 3007 | simple_iterator_push_(iterator); |
| 3008 | if(!FLAC__metadata_simple_iterator_prev(iterator)) { |
| 3009 | (void)simple_iterator_pop_(iterator); |
| 3010 | return false; |
| 3011 | } |
| 3012 | fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */ |
| 3013 | fixup_is_last_flag_offset = iterator->offset[iterator->depth]; |
| 3014 | if(!simple_iterator_pop_(iterator)) |
| 3015 | return false; |
| 3016 | } |
| 3017 | } |
| 3018 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3019 | if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append)) |
| 3020 | return false; |
| 3021 | |
| 3022 | if(0 != block) { |
| 3023 | if(!write_metadata_block_header_(tempfile, &iterator->status, block)) { |
| 3024 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 3025 | return false; |
| 3026 | } |
| 3027 | |
| 3028 | if(!write_metadata_block_data_(tempfile, &iterator->status, block)) { |
| 3029 | cleanup_tempfile_(&tempfile, &tempfilename); |
| 3030 | return false; |
| 3031 | } |
| 3032 | } |
| 3033 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 3034 | if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0)) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3035 | return false; |
| 3036 | |
Josh Coalson | 51ee465 | 2002-05-09 05:46:23 +0000 | [diff] [blame] | 3037 | if(append) |
| 3038 | return FLAC__metadata_simple_iterator_next(iterator); |
| 3039 | |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3040 | return true; |
| 3041 | } |
| 3042 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 3043 | void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3044 | { |
| 3045 | FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH); |
| 3046 | iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth]; |
| 3047 | iterator->depth++; |
| 3048 | } |
| 3049 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 3050 | FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3051 | { |
| 3052 | FLAC__ASSERT(iterator->depth > 0); |
| 3053 | iterator->depth--; |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 3054 | if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3055 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3056 | return false; |
| 3057 | } |
| 3058 | |
| 3059 | return read_metadata_block_header_(iterator); |
| 3060 | } |
| 3061 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3062 | /* return meanings: |
| 3063 | * 0: ok |
| 3064 | * 1: read error |
| 3065 | * 2: seek error |
| 3066 | * 3: not a FLAC file |
| 3067 | */ |
| 3068 | unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3069 | { |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3070 | FLAC__byte buffer[4]; |
| 3071 | size_t n; |
| 3072 | unsigned i; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3073 | |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 3074 | FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3075 | |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3076 | /* skip any id3v2 tag */ |
| 3077 | errno = 0; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3078 | n = read_cb(buffer, 1, 4, handle); |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3079 | if(errno) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3080 | return 1; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3081 | else if(n != 4) |
| 3082 | return 3; |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3083 | else if(0 == memcmp(buffer, "ID3", 3)) { |
| 3084 | unsigned tag_length = 0; |
| 3085 | |
| 3086 | /* skip to the tag length */ |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3087 | if(seek_cb(handle, 2, SEEK_CUR) < 0) |
| 3088 | return 2; |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3089 | |
| 3090 | /* read the length */ |
| 3091 | for(i = 0; i < 4; i++) { |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3092 | if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80) |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3093 | return 1; |
| 3094 | tag_length <<= 7; |
| 3095 | tag_length |= (buffer[0] & 0x7f); |
| 3096 | } |
| 3097 | |
| 3098 | /* skip the rest of the tag */ |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3099 | if(seek_cb(handle, tag_length, SEEK_CUR) < 0) |
| 3100 | return 2; |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3101 | |
| 3102 | /* read the stream sync code */ |
| 3103 | errno = 0; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3104 | n = read_cb(buffer, 1, 4, handle); |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3105 | if(errno) |
| 3106 | return 1; |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3107 | else if(n != 4) |
| 3108 | return 3; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3109 | } |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3110 | |
| 3111 | /* check for the fLaC signature */ |
| 3112 | if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH)) |
| 3113 | return 0; |
| 3114 | else |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3115 | return 3; |
| 3116 | } |
| 3117 | |
| 3118 | unsigned seek_to_first_metadata_block_(FILE *f) |
| 3119 | { |
| 3120 | return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 3123 | FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3124 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3125 | const FLAC__off_t offset_end = append? iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length : iterator->offset[iterator->depth]; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3126 | |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 3127 | if(0 != fseeko(iterator->file, 0, SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3128 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3129 | return false; |
| 3130 | } |
| 3131 | if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) { |
| 3132 | cleanup_tempfile_(tempfile, tempfilename); |
| 3133 | return false; |
| 3134 | } |
| 3135 | if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) { |
| 3136 | cleanup_tempfile_(tempfile, tempfilename); |
| 3137 | return false; |
| 3138 | } |
| 3139 | |
| 3140 | return true; |
| 3141 | } |
| 3142 | |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3143 | FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3144 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3145 | FLAC__off_t save_offset = iterator->offset[iterator->depth]; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3146 | FLAC__ASSERT(0 != *tempfile); |
| 3147 | |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3148 | if(0 != fseeko(iterator->file, save_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length, SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3149 | cleanup_tempfile_(tempfile, tempfilename); |
| 3150 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3151 | return false; |
| 3152 | } |
| 3153 | if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) { |
| 3154 | cleanup_tempfile_(tempfile, tempfilename); |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 3158 | if(fixup_is_last_code != 0) { |
| 3159 | /* |
| 3160 | * if code == 1, it means a block was appended to the end so |
| 3161 | * we have to clear the is_last flag of the previous block |
| 3162 | * if code == -1, it means the last block was deleted so |
| 3163 | * we have to set the is_last flag of the previous block |
| 3164 | */ |
Josh Coalson | f0a8c4f | 2003-01-15 03:17:51 +0000 | [diff] [blame] | 3165 | /* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */ |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3166 | FLAC__byte x; |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 3167 | if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3168 | cleanup_tempfile_(tempfile, tempfilename); |
| 3169 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3170 | return false; |
| 3171 | } |
| 3172 | if(fread(&x, 1, 1, *tempfile) != 1) { |
| 3173 | cleanup_tempfile_(tempfile, tempfilename); |
| 3174 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3175 | return false; |
| 3176 | } |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 3177 | if(fixup_is_last_code > 0) { |
| 3178 | FLAC__ASSERT(x & 0x80); |
| 3179 | x &= 0x7f; |
| 3180 | } |
| 3181 | else { |
| 3182 | FLAC__ASSERT(!(x & 0x80)); |
| 3183 | x |= 0x80; |
| 3184 | } |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 3185 | if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3186 | cleanup_tempfile_(tempfile, tempfilename); |
| 3187 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3188 | return false; |
| 3189 | } |
Josh Coalson | f91251d | 2002-12-28 07:04:49 +0000 | [diff] [blame] | 3190 | if(local__fwrite(&x, 1, 1, *tempfile) != 1) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3191 | cleanup_tempfile_(tempfile, tempfilename); |
| 3192 | iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3193 | return false; |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | (void)fclose(iterator->file); |
| 3198 | |
| 3199 | if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status)) |
| 3200 | return false; |
| 3201 | |
| 3202 | if(iterator->has_stats) |
| 3203 | set_file_stats_(iterator->filename, &iterator->stats); |
| 3204 | |
Josh Coalson | 3ac6693 | 2002-08-30 05:41:31 +0000 | [diff] [blame] | 3205 | if(!simple_iterator_prime_input_(iterator, !iterator->is_writable)) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3206 | return false; |
| 3207 | if(backup) { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3208 | while(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length < save_offset) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3209 | if(!FLAC__metadata_simple_iterator_next(iterator)) |
| 3210 | return false; |
| 3211 | return true; |
| 3212 | } |
| 3213 | else { |
| 3214 | /* move the iterator to it's original block faster by faking a push, then doing a pop_ */ |
| 3215 | FLAC__ASSERT(iterator->depth == 0); |
| 3216 | iterator->offset[0] = save_offset; |
| 3217 | iterator->depth++; |
| 3218 | return simple_iterator_pop_(iterator); |
| 3219 | } |
| 3220 | } |
| 3221 | |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3222 | FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3223 | { |
| 3224 | FLAC__byte buffer[8192]; |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 3225 | size_t n; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3226 | |
Josh Coalson | acd4a43 | 2006-11-11 22:43:25 +0000 | [diff] [blame] | 3227 | FLAC__ASSERT(bytes >= 0); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3228 | while(bytes > 0) { |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3229 | n = flac_min(sizeof(buffer), (size_t)bytes); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3230 | if(fread(buffer, 1, n, file) != n) { |
| 3231 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3232 | return false; |
| 3233 | } |
Josh Coalson | f91251d | 2002-12-28 07:04:49 +0000 | [diff] [blame] | 3234 | if(local__fwrite(buffer, 1, n, tempfile) != n) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3235 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3236 | return false; |
| 3237 | } |
| 3238 | bytes -= n; |
| 3239 | } |
| 3240 | |
| 3241 | return true; |
| 3242 | } |
| 3243 | |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3244 | FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status) |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 3245 | { |
| 3246 | FLAC__byte buffer[8192]; |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 3247 | size_t n; |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 3248 | |
Josh Coalson | acd4a43 | 2006-11-11 22:43:25 +0000 | [diff] [blame] | 3249 | FLAC__ASSERT(bytes >= 0); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 3250 | while(bytes > 0) { |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3251 | n = flac_min(sizeof(buffer), (size_t)bytes); |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 3252 | if(read_cb(buffer, 1, n, handle) != n) { |
| 3253 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3254 | return false; |
| 3255 | } |
| 3256 | if(temp_write_cb(buffer, 1, n, temp_handle) != n) { |
| 3257 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3258 | return false; |
| 3259 | } |
| 3260 | bytes -= n; |
| 3261 | } |
| 3262 | |
| 3263 | return true; |
| 3264 | } |
| 3265 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 3266 | FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3267 | { |
| 3268 | FLAC__byte buffer[8192]; |
| 3269 | size_t n; |
| 3270 | |
| 3271 | while(!feof(file)) { |
| 3272 | n = fread(buffer, 1, sizeof(buffer), file); |
| 3273 | if(n == 0 && !feof(file)) { |
| 3274 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3275 | return false; |
| 3276 | } |
Josh Coalson | f91251d | 2002-12-28 07:04:49 +0000 | [diff] [blame] | 3277 | if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3278 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3279 | return false; |
| 3280 | } |
| 3281 | } |
| 3282 | |
| 3283 | return true; |
| 3284 | } |
| 3285 | |
Josh Coalson | 463a22c | 2004-07-11 06:41:00 +0000 | [diff] [blame] | 3286 | 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) |
| 3287 | { |
| 3288 | FLAC__byte buffer[8192]; |
| 3289 | size_t n; |
| 3290 | |
| 3291 | while(!eof_cb(handle)) { |
| 3292 | n = read_cb(buffer, 1, sizeof(buffer), handle); |
| 3293 | if(n == 0 && !eof_cb(handle)) { |
| 3294 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3295 | return false; |
| 3296 | } |
| 3297 | if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) { |
| 3298 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3299 | return false; |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | return true; |
| 3304 | } |
| 3305 | |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 3306 | static int |
| 3307 | local_snprintf(char *str, size_t size, const char *fmt, ...) |
| 3308 | { |
| 3309 | va_list va; |
Erik de Castro Lopo | 1c7e3f1 | 2014-09-22 18:29:32 +1000 | [diff] [blame] | 3310 | int rc; |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 3311 | |
Erik de Castro Lopo | 1c7e3f1 | 2014-09-22 18:29:32 +1000 | [diff] [blame] | 3312 | #if defined _MSC_VER |
| 3313 | if (size == 0) |
| 3314 | return 1024; |
Julien Nabet | d908b29 | 2016-01-22 20:57:24 +0100 | [diff] [blame] | 3315 | #endif |
| 3316 | |
| 3317 | va_start (va, fmt); |
| 3318 | |
| 3319 | #if defined _MSC_VER |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 3320 | rc = vsnprintf_s (str, size, _TRUNCATE, fmt, va); |
Erik de Castro Lopo | 1c7e3f1 | 2014-09-22 18:29:32 +1000 | [diff] [blame] | 3321 | if (rc < 0) |
| 3322 | rc = size - 1; |
| 3323 | #elif defined __MINGW32__ |
| 3324 | rc = __mingw_vsnprintf (str, size, fmt, va); |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 3325 | #else |
| 3326 | rc = vsnprintf (str, size, fmt, va); |
| 3327 | #endif |
| 3328 | va_end (va); |
| 3329 | |
| 3330 | return rc; |
| 3331 | } |
| 3332 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 3333 | FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3334 | { |
| 3335 | static const char *tempfile_suffix = ".metadata_edit"; |
| 3336 | if(0 == tempfile_path_prefix) { |
Erik de Castro Lopo | 2d6354f | 2013-03-19 20:23:44 +1100 | [diff] [blame] | 3337 | size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1; |
| 3338 | if(0 == (*tempfilename = safe_malloc_(dest_len))) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3339 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3340 | return false; |
| 3341 | } |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 3342 | local_snprintf(*tempfilename, dest_len, "%s%s", filename, tempfile_suffix); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3343 | } |
| 3344 | else { |
| 3345 | const char *p = strrchr(filename, '/'); |
Erik de Castro Lopo | 2d6354f | 2013-03-19 20:23:44 +1100 | [diff] [blame] | 3346 | size_t dest_len; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3347 | if(0 == p) |
| 3348 | p = filename; |
| 3349 | else |
| 3350 | p++; |
| 3351 | |
Erik de Castro Lopo | 2d6354f | 2013-03-19 20:23:44 +1100 | [diff] [blame] | 3352 | dest_len = strlen(tempfile_path_prefix) + strlen(p) + strlen(tempfile_suffix) + 2; |
| 3353 | |
| 3354 | if(0 == (*tempfilename = safe_malloc_(dest_len))) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3355 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3356 | return false; |
| 3357 | } |
Erik de Castro Lopo | 14c28ae | 2013-03-21 17:28:23 +1100 | [diff] [blame] | 3358 | local_snprintf(*tempfilename, dest_len, "%s/%s%s", tempfile_path_prefix, p, tempfile_suffix); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 3361 | if(0 == (*tempfile = flac_fopen(*tempfilename, "w+b"))) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3362 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 3363 | return false; |
| 3364 | } |
| 3365 | |
| 3366 | return true; |
| 3367 | } |
| 3368 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 3369 | FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3370 | { |
| 3371 | FLAC__ASSERT(0 != filename); |
| 3372 | FLAC__ASSERT(0 != tempfile); |
| 3373 | FLAC__ASSERT(0 != *tempfile); |
| 3374 | FLAC__ASSERT(0 != tempfilename); |
| 3375 | FLAC__ASSERT(0 != *tempfilename); |
| 3376 | FLAC__ASSERT(0 != status); |
| 3377 | |
| 3378 | (void)fclose(*tempfile); |
| 3379 | *tempfile = 0; |
Josh Coalson | d57c8d3 | 2002-06-11 06:15:28 +0000 | [diff] [blame] | 3380 | |
Josh Coalson | 7581d12 | 2006-11-20 07:19:15 +0000 | [diff] [blame] | 3381 | #if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__ |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 3382 | /* on some flavors of windows, flac_rename() will fail if the destination already exists */ |
| 3383 | if(flac_unlink(filename) < 0) { |
Josh Coalson | d57c8d3 | 2002-06-11 06:15:28 +0000 | [diff] [blame] | 3384 | cleanup_tempfile_(tempfile, tempfilename); |
| 3385 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR; |
| 3386 | return false; |
| 3387 | } |
| 3388 | #endif |
| 3389 | |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 3390 | /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just flac_rename(): */ |
| 3391 | if(0 != flac_rename(*tempfilename, filename)) { |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3392 | cleanup_tempfile_(tempfile, tempfilename); |
| 3393 | *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR; |
| 3394 | return false; |
| 3395 | } |
| 3396 | |
| 3397 | cleanup_tempfile_(tempfile, tempfilename); |
| 3398 | |
| 3399 | return true; |
| 3400 | } |
| 3401 | |
| 3402 | void cleanup_tempfile_(FILE **tempfile, char **tempfilename) |
| 3403 | { |
| 3404 | if(0 != *tempfile) { |
| 3405 | (void)fclose(*tempfile); |
| 3406 | *tempfile = 0; |
| 3407 | } |
| 3408 | |
| 3409 | if(0 != *tempfilename) { |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 3410 | (void)flac_unlink(*tempfilename); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3411 | free(*tempfilename); |
| 3412 | *tempfilename = 0; |
| 3413 | } |
| 3414 | } |
| 3415 | |
Erik de Castro Lopo | 7dc5e3a | 2013-03-22 18:22:57 +1100 | [diff] [blame] | 3416 | FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3417 | { |
| 3418 | FLAC__ASSERT(0 != filename); |
| 3419 | FLAC__ASSERT(0 != stats); |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 3420 | return (0 == flac_stat(filename, stats)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
Erik de Castro Lopo | 7dc5e3a | 2013-03-22 18:22:57 +1100 | [diff] [blame] | 3423 | void set_file_stats_(const char *filename, struct flac_stat_s *stats) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3424 | { |
| 3425 | struct utimbuf srctime; |
| 3426 | |
| 3427 | FLAC__ASSERT(0 != filename); |
| 3428 | FLAC__ASSERT(0 != stats); |
| 3429 | |
| 3430 | srctime.actime = stats->st_atime; |
| 3431 | srctime.modtime = stats->st_mtime; |
Erik de Castro Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 3432 | (void)flac_chmod(filename, stats->st_mode); |
| 3433 | (void)flac_utime(filename, &srctime); |
Erik de Castro Lopo | dac99fa | 2012-02-08 21:07:08 +1100 | [diff] [blame] | 3434 | #if !defined _MSC_VER && !defined __BORLANDC__ && !defined __MINGW32__ |
Erik de Castro Lopo | 91790ef | 2013-03-05 22:19:27 +1100 | [diff] [blame] | 3435 | FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1)); |
| 3436 | FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid)); |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3437 | #endif |
| 3438 | } |
| 3439 | |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3440 | int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence) |
| 3441 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 3442 | return fseeko((FILE*)handle, (FLAC__off_t)offset, whence); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle) |
| 3446 | { |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 3447 | return ftello((FILE*)handle); |
Josh Coalson | ea25cb3 | 2004-07-10 00:39:01 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 3450 | FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status) |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3451 | { |
| 3452 | switch(status) { |
| 3453 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK: |
| 3454 | return FLAC__METADATA_CHAIN_STATUS_OK; |
| 3455 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT: |
| 3456 | return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT; |
| 3457 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE: |
| 3458 | return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 3459 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE: |
| 3460 | return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE; |
| 3461 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE: |
| 3462 | return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE; |
Josh Coalson | a7b673c | 2002-05-25 02:13:38 +0000 | [diff] [blame] | 3463 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA: |
| 3464 | return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA; |
Josh Coalson | cae5873 | 2002-05-04 17:30:32 +0000 | [diff] [blame] | 3465 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR: |
| 3466 | return FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 3467 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR: |
| 3468 | return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 3469 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR: |
| 3470 | return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 3471 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR: |
| 3472 | return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR; |
| 3473 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR: |
| 3474 | return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR; |
| 3475 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR: |
| 3476 | return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3477 | case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR: |
| 3478 | default: |
| 3479 | return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 3480 | } |
| 3481 | } |