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