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