blob: 778bb8c68bda8c9babac9358bf778f892b29643c [file] [log] [blame]
Josh Coalsoncae58732002-05-04 17:30:32 +00001/* libFLAC - Free Lossless Audio Codec library
Erik de Castro Lopob1982fb2013-05-25 17:11:19 +10002 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
Josh Coalsoncae58732002-05-04 17:30:32 +00004 *
Josh Coalsonafd81072003-01-31 23:34:56 +00005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
Josh Coalsoncae58732002-05-04 17:30:32 +00008 *
Josh Coalsonafd81072003-01-31 23:34:56 +00009 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
Josh Coalsoncae58732002-05-04 17:30:32 +000011 *
Josh Coalsonafd81072003-01-31 23:34:56 +000012 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalsoncae58732002-05-04 17:30:32 +000031 */
32
Erik de Castro Lopo006b8352014-03-23 21:59:46 +110033#ifdef HAVE_CONFIG_H
Josh Coalsonb1ec7962006-05-24 04:41:36 +000034# include <config.h>
35#endif
36
Josh Coalsoncae58732002-05-04 17:30:32 +000037#include <errno.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +110041#include <stdarg.h>
Josh Coalsoncae58732002-05-04 17:30:32 +000042
Josh Coalsoncae58732002-05-04 17:30:32 +000043#include <sys/stat.h> /* for stat(), maybe chmod() */
44
Josh Coalson90ced912002-05-30 05:23:38 +000045#include "private/metadata.h"
46
Josh Coalsoncae58732002-05-04 17:30:32 +000047#include "FLAC/assert.h"
Josh Coalson6b21f662006-09-13 01:42:27 +000048#include "FLAC/stream_decoder.h"
Josh Coalson0f008d22007-09-11 04:49:56 +000049#include "share/alloc.h"
Erik de Castro Lopo59afe362012-02-04 16:13:37 +110050#include "share/compat.h"
Erik de Castro Lopo91790ef2013-03-05 22:19:27 +110051#include "share/macros.h"
Erik de Castro Lopo2d6354f2013-03-19 20:23:44 +110052#include "share/safe_str.h"
Cristian Rodríguezf0296252012-04-05 19:39:37 -030053#include "private/macros.h"
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +100054#include "private/memory.h"
55
56/* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
57#define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
Josh Coalsoncae58732002-05-04 17:30:32 +000058
59/****************************************************************************
60 *
61 * Local function declarations
62 *
63 ***************************************************************************/
64
Josh Coalsoncae58732002-05-04 17:30:32 +000065static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
66static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
67static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes);
68static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes);
69static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes);
70static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes);
71
Josh Coalsoncc682512002-06-08 04:53:42 +000072static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator);
73static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block);
Josh Coalsonea25cb32004-07-10 00:39:01 +000074static FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length);
75static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block);
76static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block);
77static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length);
78static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length);
79static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length);
80static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry);
81static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block);
82static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track);
83static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block);
Josh Coalsone343ab22006-09-23 19:21:19 +000084static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block);
Josh Coalsonea25cb32004-07-10 00:39:01 +000085static 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 Coalsoncae58732002-05-04 17:30:32 +000086
Josh Coalsoncc682512002-06-08 04:53:42 +000087static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
88static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
Josh Coalson463a22c2004-07-11 06:41:00 +000089static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
90static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
91static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block);
92static FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length);
93static FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length);
94static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block);
95static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block);
96static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block);
Josh Coalsone343ab22006-09-23 19:21:19 +000097static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block);
Josh Coalson463a22c2004-07-11 06:41:00 +000098static FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length);
99
Josh Coalsoncc682512002-06-08 04:53:42 +0000100static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block);
101static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last);
102static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append);
Josh Coalsoncae58732002-05-04 17:30:32 +0000103
Josh Coalsoncc682512002-06-08 04:53:42 +0000104static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator);
105static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator);
Josh Coalsoncae58732002-05-04 17:30:32 +0000106
Josh Coalsonea25cb32004-07-10 00:39:01 +0000107static unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb);
Josh Coalsoncae58732002-05-04 17:30:32 +0000108static unsigned seek_to_first_metadata_block_(FILE *f);
109
Josh Coalsoncc682512002-06-08 04:53:42 +0000110static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100111static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup);
Josh Coalsoncae58732002-05-04 17:30:32 +0000112
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100113static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
114static FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
Josh Coalsoncc682512002-06-08 04:53:42 +0000115static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status);
Josh Coalson463a22c2004-07-11 06:41:00 +0000116static 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 Coalsoncae58732002-05-04 17:30:32 +0000117
Josh Coalsoncc682512002-06-08 04:53:42 +0000118static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
119static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
Josh Coalsoncae58732002-05-04 17:30:32 +0000120static void cleanup_tempfile_(FILE **tempfile, char **tempfilename);
121
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +1100122static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats);
123static void set_file_stats_(const char *filename, struct flac_stat_s *stats);
Josh Coalsoncae58732002-05-04 17:30:32 +0000124
Josh Coalsonea25cb32004-07-10 00:39:01 +0000125static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence);
126static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle);
127
Josh Coalsoncc682512002-06-08 04:53:42 +0000128static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status);
Josh Coalsoncae58732002-05-04 17:30:32 +0000129
130
Josh Coalsonf91251d2002-12-28 07:04:49 +0000131#ifdef FLAC__VALGRIND_TESTING
132static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
133{
134 size_t ret = fwrite(ptr, size, nmemb, stream);
135 if(!ferror(stream))
136 fflush(stream);
137 return ret;
138}
139#else
140#define local__fwrite fwrite
141#endif
142
Josh Coalsoncae58732002-05-04 17:30:32 +0000143/****************************************************************************
144 *
145 * Level 0 implementation
146 *
147 ***************************************************************************/
148
Josh Coalson6b21f662006-09-13 01:42:27 +0000149static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
150static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
151static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalsoncae58732002-05-04 17:30:32 +0000152
Josh Coalson57ba6f42002-06-07 05:27:37 +0000153typedef struct {
154 FLAC__bool got_error;
Josh Coalson1aca6b12004-07-30 01:54:29 +0000155 FLAC__StreamMetadata *object;
Josh Coalson57ba6f42002-06-07 05:27:37 +0000156} level0_client_data;
157
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000158static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type)
Josh Coalsoncae58732002-05-04 17:30:32 +0000159{
Josh Coalson57ba6f42002-06-07 05:27:37 +0000160 level0_client_data cd;
Josh Coalson6b21f662006-09-13 01:42:27 +0000161 FLAC__StreamDecoder *decoder;
Josh Coalson9b86c962002-07-10 07:11:15 +0000162
163 FLAC__ASSERT(0 != filename);
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000164
165 cd.got_error = false;
166 cd.object = 0;
167
Josh Coalson6b21f662006-09-13 01:42:27 +0000168 decoder = FLAC__stream_decoder_new();
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000169
170 if(0 == decoder)
171 return 0;
172
Josh Coalson6b21f662006-09-13 01:42:27 +0000173 FLAC__stream_decoder_set_md5_checking(decoder, false);
174 FLAC__stream_decoder_set_metadata_ignore_all(decoder);
175 FLAC__stream_decoder_set_metadata_respond(decoder, type);
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000176
Josh Coalson6b21f662006-09-13 01:42:27 +0000177 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 Coalsonfb84f252006-11-09 06:53:10 +0000178 (void)FLAC__stream_decoder_finish(decoder);
Josh Coalson6b21f662006-09-13 01:42:27 +0000179 FLAC__stream_decoder_delete(decoder);
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000180 return 0;
181 }
182
Josh Coalson6b21f662006-09-13 01:42:27 +0000183 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) {
Josh Coalsonfb84f252006-11-09 06:53:10 +0000184 (void)FLAC__stream_decoder_finish(decoder);
Josh Coalson6b21f662006-09-13 01:42:27 +0000185 FLAC__stream_decoder_delete(decoder);
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000186 if(0 != cd.object)
187 FLAC__metadata_object_delete(cd.object);
188 return 0;
189 }
190
Josh Coalsonfb84f252006-11-09 06:53:10 +0000191 (void)FLAC__stream_decoder_finish(decoder);
Josh Coalson6b21f662006-09-13 01:42:27 +0000192 FLAC__stream_decoder_delete(decoder);
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000193
194 return cd.object;
195}
196
197FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo)
198{
199 FLAC__StreamMetadata *object;
200
201 FLAC__ASSERT(0 != filename);
Josh Coalson9b86c962002-07-10 07:11:15 +0000202 FLAC__ASSERT(0 != streaminfo);
203
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000204 object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO);
Josh Coalsoncae58732002-05-04 17:30:32 +0000205
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000206 if (object) {
Josh Coalson1aca6b12004-07-30 01:54:29 +0000207 /* can just copy the contents since STREAMINFO has no internal structure */
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000208 *streaminfo = *object;
209 FLAC__metadata_object_delete(object);
210 return true;
Josh Coalson1aca6b12004-07-30 01:54:29 +0000211 }
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000212 else {
213 return false;
214 }
Josh Coalson1aca6b12004-07-30 01:54:29 +0000215}
216
217FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags)
218{
Josh Coalson1aca6b12004-07-30 01:54:29 +0000219 FLAC__ASSERT(0 != filename);
220 FLAC__ASSERT(0 != tags);
221
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000222 *tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson1aca6b12004-07-30 01:54:29 +0000223
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000224 return 0 != *tags;
225}
Josh Coalson1aca6b12004-07-30 01:54:29 +0000226
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000227FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet)
228{
229 FLAC__ASSERT(0 != filename);
230 FLAC__ASSERT(0 != cuesheet);
Josh Coalson1aca6b12004-07-30 01:54:29 +0000231
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000232 *cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET);
Josh Coalson1aca6b12004-07-30 01:54:29 +0000233
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000234 return 0 != *cuesheet;
Josh Coalsoncae58732002-05-04 17:30:32 +0000235}
236
Josh Coalson6b21f662006-09-13 01:42:27 +0000237FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
Josh Coalsoncae58732002-05-04 17:30:32 +0000238{
239 (void)decoder, (void)frame, (void)buffer, (void)client_data;
240
Josh Coalson2835f432002-06-04 05:54:44 +0000241 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
Josh Coalsoncae58732002-05-04 17:30:32 +0000242}
243
Josh Coalson6b21f662006-09-13 01:42:27 +0000244void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalsoncae58732002-05-04 17:30:32 +0000245{
Josh Coalson57ba6f42002-06-07 05:27:37 +0000246 level0_client_data *cd = (level0_client_data *)client_data;
Josh Coalsoncae58732002-05-04 17:30:32 +0000247 (void)decoder;
248
Josh Coalson1aca6b12004-07-30 01:54:29 +0000249 /*
250 * we assume we only get here when the one metadata block we were
251 * looking for was passed to us
252 */
Josh Coalson7cfac0b2006-04-10 05:37:34 +0000253 if(!cd->got_error && 0 == cd->object) {
Josh Coalson1aca6b12004-07-30 01:54:29 +0000254 if(0 == (cd->object = FLAC__metadata_object_clone(metadata)))
255 cd->got_error = true;
Josh Coalson57ba6f42002-06-07 05:27:37 +0000256 }
Josh Coalsoncae58732002-05-04 17:30:32 +0000257}
258
Josh Coalson6b21f662006-09-13 01:42:27 +0000259void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
Josh Coalsoncae58732002-05-04 17:30:32 +0000260{
Josh Coalson57ba6f42002-06-07 05:27:37 +0000261 level0_client_data *cd = (level0_client_data *)client_data;
Josh Coalsoncae58732002-05-04 17:30:32 +0000262 (void)decoder;
263
Josh Coalson2835f432002-06-04 05:54:44 +0000264 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000265 cd->got_error = true;
Josh Coalsoncae58732002-05-04 17:30:32 +0000266}
267
Josh Coalson74ed2942006-09-23 23:15:05 +0000268FLAC_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 Coalsone343ab22006-09-23 19:21:19 +0000269{
270 FLAC__Metadata_SimpleIterator *it;
271 FLAC__uint64 max_area_seen = 0;
272 FLAC__uint64 max_depth_seen = 0;
273
274 FLAC__ASSERT(0 != filename);
275 FLAC__ASSERT(0 != picture);
276
277 *picture = 0;
278
279 it = FLAC__metadata_simple_iterator_new();
280 if(0 == it)
281 return false;
282 if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
283 FLAC__metadata_simple_iterator_delete(it);
284 return false;
285 }
286 do {
287 if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) {
288 FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it);
289 FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height;
290 /* check constraints */
291 if(
292 (type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) &&
293 (mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) &&
294 (description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) &&
295 obj->data.picture.width <= max_width &&
296 obj->data.picture.height <= max_height &&
297 obj->data.picture.depth <= max_depth &&
Josh Coalson74ed2942006-09-23 23:15:05 +0000298 obj->data.picture.colors <= max_colors &&
Josh Coalsone343ab22006-09-23 19:21:19 +0000299 (area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen))
300 ) {
301 if(*picture)
302 FLAC__metadata_object_delete(*picture);
303 *picture = obj;
304 max_area_seen = area;
305 max_depth_seen = obj->data.picture.depth;
306 }
307 else {
308 FLAC__metadata_object_delete(obj);
309 }
310 }
311 } while(FLAC__metadata_simple_iterator_next(it));
312
313 FLAC__metadata_simple_iterator_delete(it);
314
315 return (0 != *picture);
316}
317
Josh Coalsoncae58732002-05-04 17:30:32 +0000318
319/****************************************************************************
320 *
321 * Level 1 implementation
322 *
323 ***************************************************************************/
324
325#define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4)
326/* 1 for initial offset, +4 for our own personal use */
327
Josh Coalsoncc682512002-06-08 04:53:42 +0000328struct FLAC__Metadata_SimpleIterator {
Josh Coalsoncae58732002-05-04 17:30:32 +0000329 FILE *file;
330 char *filename, *tempfile_path_prefix;
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +1100331 struct flac_stat_s stats;
Josh Coalsoncae58732002-05-04 17:30:32 +0000332 FLAC__bool has_stats;
333 FLAC__bool is_writable;
Josh Coalsoncc682512002-06-08 04:53:42 +0000334 FLAC__Metadata_SimpleIteratorStatus status;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100335 FLAC__off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH];
336 FLAC__off_t first_offset; /* this is the offset to the STREAMINFO block */
Josh Coalsoncae58732002-05-04 17:30:32 +0000337 unsigned depth;
338 /* this is the metadata block header of the current block we are pointing to: */
339 FLAC__bool is_last;
Josh Coalsoncc682512002-06-08 04:53:42 +0000340 FLAC__MetadataType type;
Josh Coalsoncae58732002-05-04 17:30:32 +0000341 unsigned length;
342};
343
Josh Coalson6afed9f2002-10-16 22:29:47 +0000344FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = {
Josh Coalsoncae58732002-05-04 17:30:32 +0000345 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK",
346 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT",
347 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE",
348 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE",
349 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE",
Josh Coalsona7b673c2002-05-25 02:13:38 +0000350 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA",
Josh Coalsoncae58732002-05-04 17:30:32 +0000351 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR",
352 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR",
353 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR",
354 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR",
355 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR",
356 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR",
357 "FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR"
358};
359
360
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000361FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void)
Josh Coalsoncae58732002-05-04 17:30:32 +0000362{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000363 FLAC__Metadata_SimpleIterator *iterator = calloc(1, sizeof(FLAC__Metadata_SimpleIterator));
Josh Coalsoncae58732002-05-04 17:30:32 +0000364
365 if(0 != iterator) {
366 iterator->file = 0;
367 iterator->filename = 0;
368 iterator->tempfile_path_prefix = 0;
369 iterator->has_stats = false;
370 iterator->is_writable = false;
371 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
372 iterator->first_offset = iterator->offset[0] = -1;
373 iterator->depth = 0;
374 }
375
376 return iterator;
377}
378
Josh Coalsoncc682512002-06-08 04:53:42 +0000379static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000380{
381 FLAC__ASSERT(0 != iterator);
382
383 if(0 != iterator->file) {
384 fclose(iterator->file);
385 iterator->file = 0;
386 if(iterator->has_stats)
387 set_file_stats_(iterator->filename, &iterator->stats);
388 }
389 if(0 != iterator->filename) {
390 free(iterator->filename);
391 iterator->filename = 0;
392 }
393 if(0 != iterator->tempfile_path_prefix) {
394 free(iterator->tempfile_path_prefix);
395 iterator->tempfile_path_prefix = 0;
396 }
397}
398
Josh Coalson6afed9f2002-10-16 22:29:47 +0000399FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000400{
401 FLAC__ASSERT(0 != iterator);
402
403 simple_iterator_free_guts_(iterator);
404 free(iterator);
405}
406
Josh Coalson6afed9f2002-10-16 22:29:47 +0000407FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000408{
Josh Coalsoncc682512002-06-08 04:53:42 +0000409 FLAC__Metadata_SimpleIteratorStatus status;
Josh Coalsoncae58732002-05-04 17:30:32 +0000410
411 FLAC__ASSERT(0 != iterator);
412
413 status = iterator->status;
414 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
415 return status;
416}
417
Josh Coalson3ac66932002-08-30 05:41:31 +0000418static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only)
Josh Coalsoncae58732002-05-04 17:30:32 +0000419{
420 unsigned ret;
421
422 FLAC__ASSERT(0 != iterator);
423
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100424 if(read_only || 0 == (iterator->file = flac_fopen(iterator->filename, "r+b"))) {
Josh Coalson3ac66932002-08-30 05:41:31 +0000425 iterator->is_writable = false;
Josh Coalson3ac66932002-08-30 05:41:31 +0000426 if(read_only || errno == EACCES) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100427 if(0 == (iterator->file = flac_fopen(iterator->filename, "rb"))) {
Josh Coalsoncae58732002-05-04 17:30:32 +0000428 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
429 return false;
430 }
431 }
432 else {
433 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
434 return false;
435 }
436 }
437 else {
438 iterator->is_writable = true;
439 }
440
441 ret = seek_to_first_metadata_block_(iterator->file);
442 switch(ret) {
443 case 0:
444 iterator->depth = 0;
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000445 iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file);
Josh Coalsoncae58732002-05-04 17:30:32 +0000446 return read_metadata_block_header_(iterator);
447 case 1:
448 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
449 return false;
450 case 2:
Josh Coalsonea25cb32004-07-10 00:39:01 +0000451 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
452 return false;
453 case 3:
Josh Coalsoncae58732002-05-04 17:30:32 +0000454 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE;
455 return false;
456 default:
457 FLAC__ASSERT(0);
458 return false;
459 }
460}
461
462#if 0
463@@@ If we decide to finish implementing this, put this comment back in metadata.h
464/*
465 * The 'tempfile_path_prefix' allows you to specify a directory where
466 * tempfiles should go. Remember that if your metadata edits cause the
467 * FLAC file to grow, the entire file will have to be rewritten. If
468 * 'tempfile_path_prefix' is NULL, the temp file will be written in the
469 * same directory as the original FLAC file. This makes replacing the
470 * original with the tempfile fast but requires extra space in the same
471 * partition for the tempfile. If space is a problem, you can pass a
472 * directory name belonging to a different partition in
473 * 'tempfile_path_prefix'. Note that you should use the forward slash
474 * '/' as the directory separator. A trailing slash is not needed; it
475 * will be added automatically.
476 */
Josh Coalsoncc682512002-06-08 04:53:42 +0000477FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix);
Josh Coalsoncae58732002-05-04 17:30:32 +0000478#endif
479
Josh Coalson6afed9f2002-10-16 22:29:47 +0000480FLAC_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 Coalsoncae58732002-05-04 17:30:32 +0000481{
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100482 const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'flac_rename(...)' for what it will take to finish implementing this */
Josh Coalsoncae58732002-05-04 17:30:32 +0000483
484 FLAC__ASSERT(0 != iterator);
485 FLAC__ASSERT(0 != filename);
486
487 simple_iterator_free_guts_(iterator);
488
Josh Coalson3ac66932002-08-30 05:41:31 +0000489 if(!read_only && preserve_file_stats)
Josh Coalsoncae58732002-05-04 17:30:32 +0000490 iterator->has_stats = get_file_stats_(filename, &iterator->stats);
491
492 if(0 == (iterator->filename = strdup(filename))) {
493 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
494 return false;
495 }
496 if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) {
497 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
498 return false;
499 }
500
Josh Coalson3ac66932002-08-30 05:41:31 +0000501 return simple_iterator_prime_input_(iterator, read_only);
Josh Coalsoncae58732002-05-04 17:30:32 +0000502}
503
Josh Coalson6afed9f2002-10-16 22:29:47 +0000504FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000505{
506 FLAC__ASSERT(0 != iterator);
507 FLAC__ASSERT(0 != iterator->file);
508
509 return iterator->is_writable;
510}
511
Josh Coalson6afed9f2002-10-16 22:29:47 +0000512FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000513{
514 FLAC__ASSERT(0 != iterator);
515 FLAC__ASSERT(0 != iterator->file);
516
517 if(iterator->is_last)
518 return false;
519
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000520 if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
Josh Coalsoncae58732002-05-04 17:30:32 +0000521 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
522 return false;
523 }
524
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000525 iterator->offset[iterator->depth] = ftello(iterator->file);
Josh Coalsoncae58732002-05-04 17:30:32 +0000526
527 return read_metadata_block_header_(iterator);
528}
529
Josh Coalson6afed9f2002-10-16 22:29:47 +0000530FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000531{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100532 FLAC__off_t this_offset;
Josh Coalsoncae58732002-05-04 17:30:32 +0000533
534 FLAC__ASSERT(0 != iterator);
535 FLAC__ASSERT(0 != iterator->file);
536
537 if(iterator->offset[iterator->depth] == iterator->first_offset)
538 return false;
539
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000540 if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +0000541 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
542 return false;
543 }
544 this_offset = iterator->first_offset;
545 if(!read_metadata_block_header_(iterator))
546 return false;
547
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000548 /* we ignore any error from ftello() and catch it in fseeko() */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100549 while(ftello(iterator->file) + (FLAC__off_t)iterator->length < iterator->offset[iterator->depth]) {
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000550 if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
Josh Coalsoncae58732002-05-04 17:30:32 +0000551 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
552 return false;
553 }
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000554 this_offset = ftello(iterator->file);
Josh Coalsoncae58732002-05-04 17:30:32 +0000555 if(!read_metadata_block_header_(iterator))
556 return false;
557 }
558
559 iterator->offset[iterator->depth] = this_offset;
560
561 return true;
562}
563
Josh Coalson504dcaf2007-09-13 15:42:47 +0000564/*@@@@add to tests*/
Josh Coalson09ccf202007-08-14 00:34:50 +0000565FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator)
566{
567 FLAC__ASSERT(0 != iterator);
568 FLAC__ASSERT(0 != iterator->file);
569
570 return iterator->is_last;
571}
572
Josh Coalson504dcaf2007-09-13 15:42:47 +0000573/*@@@@add to tests*/
Josh Coalson09ccf202007-08-14 00:34:50 +0000574FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator)
575{
576 FLAC__ASSERT(0 != iterator);
577 FLAC__ASSERT(0 != iterator->file);
578
579 return iterator->offset[iterator->depth];
580}
581
Josh Coalson6afed9f2002-10-16 22:29:47 +0000582FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000583{
584 FLAC__ASSERT(0 != iterator);
585 FLAC__ASSERT(0 != iterator->file);
586
587 return iterator->type;
588}
589
Josh Coalson504dcaf2007-09-13 15:42:47 +0000590/*@@@@add to tests*/
Josh Coalson09ccf202007-08-14 00:34:50 +0000591FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator)
592{
593 FLAC__ASSERT(0 != iterator);
594 FLAC__ASSERT(0 != iterator->file);
595
596 return iterator->length;
597}
598
Josh Coalson504dcaf2007-09-13 15:42:47 +0000599/*@@@@add to tests*/
Josh Coalson4e56b062007-08-15 05:59:09 +0000600FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id)
601{
602 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
603
604 FLAC__ASSERT(0 != iterator);
605 FLAC__ASSERT(0 != iterator->file);
606 FLAC__ASSERT(0 != id);
607
608 if(iterator->type != FLAC__METADATA_TYPE_APPLICATION) {
609 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
610 return false;
611 }
612
613 if(fread(id, 1, id_bytes, iterator->file) != id_bytes) {
614 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
615 return false;
616 }
617
618 /* back up */
619 if(0 != fseeko(iterator->file, -((int)id_bytes), SEEK_CUR)) {
620 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
621 return false;
622 }
623
624 return true;
625}
626
Josh Coalson6afed9f2002-10-16 22:29:47 +0000627FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +0000628{
Josh Coalsoncc682512002-06-08 04:53:42 +0000629 FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type);
Josh Coalsoncae58732002-05-04 17:30:32 +0000630
631 FLAC__ASSERT(0 != iterator);
632 FLAC__ASSERT(0 != iterator->file);
633
634 if(0 != block) {
635 block->is_last = iterator->is_last;
636 block->length = iterator->length;
637
638 if(!read_metadata_block_data_(iterator, block)) {
639 FLAC__metadata_object_delete(block);
640 return 0;
641 }
642
643 /* back up to the beginning of the block data to stay consistent */
Josh Coalsonb1ec7962006-05-24 04:41:36 +0000644 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +0000645 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
646 FLAC__metadata_object_delete(block);
647 return 0;
648 }
649 }
650 else
651 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
652
653 return block;
654}
655
Josh Coalson6afed9f2002-10-16 22:29:47 +0000656FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
Josh Coalsoncae58732002-05-04 17:30:32 +0000657{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100658 FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
Josh Coalson51ee4652002-05-09 05:46:23 +0000659 FLAC__bool ret;
660
Josh Coalsoncae58732002-05-04 17:30:32 +0000661 FLAC__ASSERT(0 != iterator);
662 FLAC__ASSERT(0 != iterator->file);
Josh Coalsond8ab3462002-07-11 05:54:05 +0000663 FLAC__ASSERT(0 != block);
Josh Coalsoncae58732002-05-04 17:30:32 +0000664
665 if(!iterator->is_writable) {
666 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
667 return false;
668 }
669
670 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) {
671 if(iterator->type != block->type) {
672 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
673 return false;
674 }
675 }
676
677 block->is_last = iterator->is_last;
Josh Coalsoncae58732002-05-04 17:30:32 +0000678
679 if(iterator->length == block->length)
680 return write_metadata_block_stationary_(iterator, block);
681 else if(iterator->length > block->length) {
Josh Coalsoneb643a22002-05-20 05:58:50 +0000682 if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) {
Josh Coalsonccce9ed2003-01-13 02:06:40 +0000683 ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last);
684 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100685 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000686 return ret;
687 }
688 else {
689 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
Josh Coalsonccce9ed2003-01-13 02:06:40 +0000690 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100691 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000692 return ret;
693 }
Josh Coalsoncae58732002-05-04 17:30:32 +0000694 }
695 else /* iterator->length < block->length */ {
696 unsigned padding_leftover = 0;
697 FLAC__bool padding_is_last = false;
698 if(use_padding) {
699 /* first see if we can even use padding */
700 if(iterator->is_last) {
701 use_padding = false;
702 }
703 else {
Josh Coalson51ee4652002-05-09 05:46:23 +0000704 const unsigned extra_padding_bytes_required = block->length - iterator->length;
Josh Coalsoncae58732002-05-04 17:30:32 +0000705 simple_iterator_push_(iterator);
706 if(!FLAC__metadata_simple_iterator_next(iterator)) {
707 (void)simple_iterator_pop_(iterator);
708 return false;
709 }
710 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
711 use_padding = false;
712 }
713 else {
Josh Coalsoneb643a22002-05-20 05:58:50 +0000714 if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) {
Josh Coalsoncae58732002-05-04 17:30:32 +0000715 padding_leftover = 0;
716 block->is_last = iterator->is_last;
717 }
Josh Coalson51ee4652002-05-09 05:46:23 +0000718 else if(iterator->length < extra_padding_bytes_required)
Josh Coalsoncae58732002-05-04 17:30:32 +0000719 use_padding = false;
720 else {
Josh Coalsoneb643a22002-05-20 05:58:50 +0000721 padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required;
Josh Coalsoncae58732002-05-04 17:30:32 +0000722 padding_is_last = iterator->is_last;
723 block->is_last = false;
724 }
725 }
726 if(!simple_iterator_pop_(iterator))
727 return false;
728 }
729 }
730 if(use_padding) {
Josh Coalson51ee4652002-05-09 05:46:23 +0000731 if(padding_leftover == 0) {
732 ret = write_metadata_block_stationary_(iterator, block);
Josh Coalsonccce9ed2003-01-13 02:06:40 +0000733 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100734 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000735 return ret;
736 }
737 else {
Josh Coalsoneb643a22002-05-20 05:58:50 +0000738 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
739 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
Josh Coalsonccce9ed2003-01-13 02:06:40 +0000740 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100741 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000742 return ret;
743 }
Josh Coalsoncae58732002-05-04 17:30:32 +0000744 }
745 else {
Josh Coalson51ee4652002-05-09 05:46:23 +0000746 ret = rewrite_whole_file_(iterator, block, /*append=*/false);
Josh Coalsonccce9ed2003-01-13 02:06:40 +0000747 FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100748 FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000749 return ret;
Josh Coalsoncae58732002-05-04 17:30:32 +0000750 }
751 }
752}
753
Josh Coalson6afed9f2002-10-16 22:29:47 +0000754FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
Josh Coalsoncae58732002-05-04 17:30:32 +0000755{
756 unsigned padding_leftover = 0;
757 FLAC__bool padding_is_last = false;
758
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100759 FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;)
Josh Coalson51ee4652002-05-09 05:46:23 +0000760 FLAC__bool ret;
761
Josh Coalsoncae58732002-05-04 17:30:32 +0000762 FLAC__ASSERT(0 != iterator);
763 FLAC__ASSERT(0 != iterator->file);
Josh Coalsond8ab3462002-07-11 05:54:05 +0000764 FLAC__ASSERT(0 != block);
Josh Coalsoncae58732002-05-04 17:30:32 +0000765
766 if(!iterator->is_writable)
767 return false;
768
769 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
770 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
771 return false;
772 }
773
Josh Coalson6b02a752002-05-10 06:42:02 +0000774 block->is_last = iterator->is_last;
Josh Coalson51ee4652002-05-09 05:46:23 +0000775
Josh Coalsoncae58732002-05-04 17:30:32 +0000776 if(use_padding) {
777 /* first see if we can even use padding */
778 if(iterator->is_last) {
779 use_padding = false;
780 }
781 else {
782 simple_iterator_push_(iterator);
783 if(!FLAC__metadata_simple_iterator_next(iterator)) {
784 (void)simple_iterator_pop_(iterator);
785 return false;
786 }
787 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
788 use_padding = false;
789 }
790 else {
Josh Coalsoncae58732002-05-04 17:30:32 +0000791 if(iterator->length == block->length) {
792 padding_leftover = 0;
793 block->is_last = iterator->is_last;
794 }
Josh Coalsoneb643a22002-05-20 05:58:50 +0000795 else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length)
Josh Coalsoncae58732002-05-04 17:30:32 +0000796 use_padding = false;
797 else {
Josh Coalson6b02a752002-05-10 06:42:02 +0000798 padding_leftover = iterator->length - block->length;
Josh Coalsoncae58732002-05-04 17:30:32 +0000799 padding_is_last = iterator->is_last;
800 block->is_last = false;
801 }
802 }
803 if(!simple_iterator_pop_(iterator))
804 return false;
805 }
806 }
807 if(use_padding) {
808 /* move to the next block, which is suitable padding */
809 if(!FLAC__metadata_simple_iterator_next(iterator))
810 return false;
Josh Coalson51ee4652002-05-09 05:46:23 +0000811 if(padding_leftover == 0) {
812 ret = write_metadata_block_stationary_(iterator, block);
Josh Coalson51ee4652002-05-09 05:46:23 +0000813 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100814 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000815 return ret;
816 }
817 else {
Josh Coalsoneb643a22002-05-20 05:58:50 +0000818 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
819 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
Josh Coalson51ee4652002-05-09 05:46:23 +0000820 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100821 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000822 return ret;
823 }
Josh Coalsoncae58732002-05-04 17:30:32 +0000824 }
825 else {
Josh Coalson51ee4652002-05-09 05:46:23 +0000826 ret = rewrite_whole_file_(iterator, block, /*append=*/true);
Josh Coalson51ee4652002-05-09 05:46:23 +0000827 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100828 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000829 return ret;
Josh Coalsoncae58732002-05-04 17:30:32 +0000830 }
831}
832
Josh Coalson6afed9f2002-10-16 22:29:47 +0000833FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)
Josh Coalsoncae58732002-05-04 17:30:32 +0000834{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100835 FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
Josh Coalson51ee4652002-05-09 05:46:23 +0000836 FLAC__bool ret;
837
Josh Coalsoncae58732002-05-04 17:30:32 +0000838 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
839 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
840 return false;
841 }
842
843 if(use_padding) {
Josh Coalsoncc682512002-06-08 04:53:42 +0000844 FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
Josh Coalsoncae58732002-05-04 17:30:32 +0000845 if(0 == padding) {
846 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
847 return false;
848 }
849 padding->length = iterator->length;
850 if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) {
851 FLAC__metadata_object_delete(padding);
852 return false;
853 }
854 FLAC__metadata_object_delete(padding);
Josh Coalson6b02a752002-05-10 06:42:02 +0000855 if(!FLAC__metadata_simple_iterator_prev(iterator))
856 return false;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100857 FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
858 FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
Josh Coalsoncae58732002-05-04 17:30:32 +0000859 return true;
860 }
861 else {
Josh Coalson51ee4652002-05-09 05:46:23 +0000862 ret = rewrite_whole_file_(iterator, 0, /*append=*/false);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100863 FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
864 FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
Josh Coalson51ee4652002-05-09 05:46:23 +0000865 return ret;
Josh Coalsoncae58732002-05-04 17:30:32 +0000866 }
867}
868
869
870
871/****************************************************************************
872 *
873 * Level 2 implementation
874 *
875 ***************************************************************************/
876
877
Josh Coalsoncc682512002-06-08 04:53:42 +0000878typedef struct FLAC__Metadata_Node {
879 FLAC__StreamMetadata *data;
880 struct FLAC__Metadata_Node *prev, *next;
881} FLAC__Metadata_Node;
Josh Coalsoncae58732002-05-04 17:30:32 +0000882
Josh Coalsoncc682512002-06-08 04:53:42 +0000883struct FLAC__Metadata_Chain {
Josh Coalson91748be2004-07-10 22:28:03 +0000884 char *filename; /* will be NULL if using callbacks */
Josh Coalson7d273b42006-11-15 06:12:30 +0000885 FLAC__bool is_ogg;
Josh Coalsoncc682512002-06-08 04:53:42 +0000886 FLAC__Metadata_Node *head;
887 FLAC__Metadata_Node *tail;
Josh Coalsoncae58732002-05-04 17:30:32 +0000888 unsigned nodes;
Josh Coalsoncc682512002-06-08 04:53:42 +0000889 FLAC__Metadata_ChainStatus status;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100890 FLAC__off_t first_offset, last_offset;
Josh Coalsoncae58732002-05-04 17:30:32 +0000891 /*
892 * This is the length of the chain initially read from the FLAC file.
Josh Coalson3ecb1172002-06-15 05:09:47 +0000893 * it is used to compare against the current length to decide whether
Josh Coalsoncae58732002-05-04 17:30:32 +0000894 * or not the whole file has to be rewritten.
895 */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100896 FLAC__off_t initial_length;
Josh Coalson7d273b42006-11-15 06:12:30 +0000897 /* @@@ hacky, these are currently only needed by ogg reader */
898 FLAC__IOHandle handle;
899 FLAC__IOCallback_Read read_cb;
Josh Coalsoncae58732002-05-04 17:30:32 +0000900};
901
Josh Coalsoncc682512002-06-08 04:53:42 +0000902struct FLAC__Metadata_Iterator {
903 FLAC__Metadata_Chain *chain;
904 FLAC__Metadata_Node *current;
Josh Coalsoncae58732002-05-04 17:30:32 +0000905};
906
Josh Coalson6afed9f2002-10-16 22:29:47 +0000907FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = {
Josh Coalsoncae58732002-05-04 17:30:32 +0000908 "FLAC__METADATA_CHAIN_STATUS_OK",
909 "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT",
910 "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE",
911 "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE",
912 "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE",
Josh Coalsona7b673c2002-05-25 02:13:38 +0000913 "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA",
Josh Coalsoncae58732002-05-04 17:30:32 +0000914 "FLAC__METADATA_CHAIN_STATUS_READ_ERROR",
915 "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR",
916 "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR",
917 "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR",
918 "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR",
919 "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR",
Josh Coalson91748be2004-07-10 22:28:03 +0000920 "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR",
Josh Coalson463a22c2004-07-11 06:41:00 +0000921 "FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS",
922 "FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH",
923 "FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL"
Josh Coalsoncae58732002-05-04 17:30:32 +0000924};
925
926
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000927static FLAC__Metadata_Node *node_new_(void)
Josh Coalsoncae58732002-05-04 17:30:32 +0000928{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000929 return calloc(1, sizeof(FLAC__Metadata_Node));
Josh Coalsoncae58732002-05-04 17:30:32 +0000930}
931
Josh Coalsoncc682512002-06-08 04:53:42 +0000932static void node_delete_(FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +0000933{
934 FLAC__ASSERT(0 != node);
935 if(0 != node->data)
936 FLAC__metadata_object_delete(node->data);
937 free(node);
938}
939
Josh Coalson91748be2004-07-10 22:28:03 +0000940static void chain_init_(FLAC__Metadata_Chain *chain)
941{
942 FLAC__ASSERT(0 != chain);
943
944 chain->filename = 0;
Josh Coalson7d273b42006-11-15 06:12:30 +0000945 chain->is_ogg = false;
Josh Coalson91748be2004-07-10 22:28:03 +0000946 chain->head = chain->tail = 0;
947 chain->nodes = 0;
948 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
949 chain->initial_length = 0;
Josh Coalson7d273b42006-11-15 06:12:30 +0000950 chain->read_cb = 0;
Josh Coalson91748be2004-07-10 22:28:03 +0000951}
952
953static void chain_clear_(FLAC__Metadata_Chain *chain)
954{
955 FLAC__Metadata_Node *node, *next;
956
957 FLAC__ASSERT(0 != chain);
958
959 for(node = chain->head; node; ) {
960 next = node->next;
961 node_delete_(node);
962 node = next;
963 }
964
965 if(0 != chain->filename)
966 free(chain->filename);
967
968 chain_init_(chain);
969}
970
Josh Coalsoncc682512002-06-08 04:53:42 +0000971static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +0000972{
973 FLAC__ASSERT(0 != chain);
974 FLAC__ASSERT(0 != node);
Josh Coalson6b02a752002-05-10 06:42:02 +0000975 FLAC__ASSERT(0 != node->data);
Josh Coalsoncae58732002-05-04 17:30:32 +0000976
Josh Coalson6b02a752002-05-10 06:42:02 +0000977 node->next = node->prev = 0;
Josh Coalsoncae58732002-05-04 17:30:32 +0000978 node->data->is_last = true;
979 if(0 != chain->tail)
980 chain->tail->data->is_last = false;
981
982 if(0 == chain->head)
983 chain->head = node;
984 else {
Josh Coalson6b02a752002-05-10 06:42:02 +0000985 FLAC__ASSERT(0 != chain->tail);
Josh Coalsoncae58732002-05-04 17:30:32 +0000986 chain->tail->next = node;
987 node->prev = chain->tail;
988 }
989 chain->tail = node;
990 chain->nodes++;
Josh Coalsoncae58732002-05-04 17:30:32 +0000991}
992
Josh Coalsoncc682512002-06-08 04:53:42 +0000993static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +0000994{
995 FLAC__ASSERT(0 != chain);
996 FLAC__ASSERT(0 != node);
997
998 if(node == chain->head)
999 chain->head = node->next;
1000 else
1001 node->prev->next = node->next;
1002
1003 if(node == chain->tail)
1004 chain->tail = node->prev;
1005 else
1006 node->next->prev = node->prev;
1007
Josh Coalson6b02a752002-05-10 06:42:02 +00001008 if(0 != chain->tail)
1009 chain->tail->data->is_last = true;
1010
Josh Coalsoncae58732002-05-04 17:30:32 +00001011 chain->nodes--;
Josh Coalsoncae58732002-05-04 17:30:32 +00001012}
1013
Josh Coalsoncc682512002-06-08 04:53:42 +00001014static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +00001015{
1016 chain_remove_node_(chain, node);
1017 node_delete_(node);
1018}
1019
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001020static FLAC__off_t chain_calculate_length_(FLAC__Metadata_Chain *chain)
Josh Coalson3ecb1172002-06-15 05:09:47 +00001021{
1022 const FLAC__Metadata_Node *node;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001023 FLAC__off_t length = 0;
Josh Coalson3ecb1172002-06-15 05:09:47 +00001024 for(node = chain->head; node; node = node->next)
1025 length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1026 return length;
1027}
1028
Josh Coalsoncc682512002-06-08 04:53:42 +00001029static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +00001030{
1031 FLAC__ASSERT(0 != node);
Josh Coalson6b02a752002-05-10 06:42:02 +00001032 FLAC__ASSERT(0 != node->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001033 FLAC__ASSERT(0 != iterator);
1034 FLAC__ASSERT(0 != iterator->current);
1035 FLAC__ASSERT(0 != iterator->chain);
1036 FLAC__ASSERT(0 != iterator->chain->head);
1037 FLAC__ASSERT(0 != iterator->chain->tail);
1038
Josh Coalson6b02a752002-05-10 06:42:02 +00001039 node->data->is_last = false;
Josh Coalson2745fe72002-05-11 05:31:56 +00001040
Josh Coalsoncae58732002-05-04 17:30:32 +00001041 node->prev = iterator->current->prev;
Josh Coalson2745fe72002-05-11 05:31:56 +00001042 node->next = iterator->current;
1043
Josh Coalsoncae58732002-05-04 17:30:32 +00001044 if(0 == node->prev)
1045 iterator->chain->head = node;
1046 else
1047 node->prev->next = node;
1048
Josh Coalsoncae58732002-05-04 17:30:32 +00001049 iterator->current->prev = node;
1050
1051 iterator->chain->nodes++;
Josh Coalsoncae58732002-05-04 17:30:32 +00001052}
1053
Josh Coalsoncc682512002-06-08 04:53:42 +00001054static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +00001055{
1056 FLAC__ASSERT(0 != node);
Josh Coalson6b02a752002-05-10 06:42:02 +00001057 FLAC__ASSERT(0 != node->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001058 FLAC__ASSERT(0 != iterator);
1059 FLAC__ASSERT(0 != iterator->current);
1060 FLAC__ASSERT(0 != iterator->chain);
1061 FLAC__ASSERT(0 != iterator->chain->head);
1062 FLAC__ASSERT(0 != iterator->chain->tail);
1063
1064 iterator->current->data->is_last = false;
1065
1066 node->prev = iterator->current;
Josh Coalsoncae58732002-05-04 17:30:32 +00001067 node->next = iterator->current->next;
Josh Coalson2745fe72002-05-11 05:31:56 +00001068
Josh Coalsoncae58732002-05-04 17:30:32 +00001069 if(0 == node->next)
1070 iterator->chain->tail = node;
1071 else
1072 node->next->prev = node;
1073
Josh Coalson2745fe72002-05-11 05:31:56 +00001074 node->prev->next = node;
1075
Josh Coalsoncae58732002-05-04 17:30:32 +00001076 iterator->chain->tail->data->is_last = true;
1077
1078 iterator->chain->nodes++;
Josh Coalsoncae58732002-05-04 17:30:32 +00001079}
1080
Josh Coalson2745fe72002-05-11 05:31:56 +00001081/* return true iff node and node->next are both padding */
Josh Coalsoncc682512002-06-08 04:53:42 +00001082static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalson2745fe72002-05-11 05:31:56 +00001083{
1084 if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) {
Josh Coalsoneb643a22002-05-20 05:58:50 +00001085 const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
Josh Coalson2745fe72002-05-11 05:31:56 +00001086 node->data->length += growth;
Josh Coalson2745fe72002-05-11 05:31:56 +00001087
1088 chain_delete_node_(chain, node->next);
1089 return true;
1090 }
1091 else
1092 return false;
1093}
1094
Josh Coalson463a22c2004-07-11 06:41:00 +00001095/* Returns the new length of the chain, or 0 if there was an error. */
1096/* WATCHOUT: This can get called multiple times before a write, so
1097 * it should still work when this happens.
1098 */
1099/* WATCHOUT: Make sure to also update the logic in
1100 * FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes.
1101 */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001102static FLAC__off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
Josh Coalson463a22c2004-07-11 06:41:00 +00001103{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001104 FLAC__off_t current_length = chain_calculate_length_(chain);
Josh Coalson463a22c2004-07-11 06:41:00 +00001105
1106 if(use_padding) {
1107 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1108 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001109 const FLAC__off_t delta = chain->initial_length - current_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001110 chain->tail->data->length += delta;
1111 current_length += delta;
1112 FLAC__ASSERT(current_length == chain->initial_length);
1113 }
1114 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001115 else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001116 FLAC__StreamMetadata *padding;
1117 FLAC__Metadata_Node *node;
1118 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) {
1119 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1120 return 0;
1121 }
1122 padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
1123 if(0 == (node = node_new_())) {
1124 FLAC__metadata_object_delete(padding);
1125 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1126 return 0;
1127 }
1128 node->data = padding;
1129 chain_append_node_(chain, node);
1130 current_length = chain_calculate_length_(chain);
1131 FLAC__ASSERT(current_length == chain->initial_length);
1132 }
1133 /* 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 */
1134 else if(current_length > chain->initial_length) {
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001135 const FLAC__off_t delta = current_length - chain->initial_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001136 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1137 /* if the delta is exactly the size of the last padding block, remove the padding block */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001138 if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001139 chain_delete_node_(chain, chain->tail);
1140 current_length = chain_calculate_length_(chain);
1141 FLAC__ASSERT(current_length == chain->initial_length);
1142 }
1143 /* if there is at least 'delta' bytes of padding, trim the padding down */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001144 else if((FLAC__off_t)chain->tail->data->length >= delta) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001145 chain->tail->data->length -= delta;
1146 current_length -= delta;
1147 FLAC__ASSERT(current_length == chain->initial_length);
1148 }
1149 }
1150 }
1151 }
1152
1153 return current_length;
1154}
1155
Josh Coalson0e46bf52004-07-15 00:04:46 +00001156static 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 Coalsonea25cb32004-07-10 00:39:01 +00001157{
1158 FLAC__Metadata_Node *node;
1159
1160 FLAC__ASSERT(0 != chain);
1161
1162 /* we assume we're already at the beginning of the file */
1163
1164 switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) {
1165 case 0:
1166 break;
1167 case 1:
1168 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1169 return false;
1170 case 2:
1171 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1172 return false;
1173 case 3:
1174 chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
1175 return false;
1176 default:
1177 FLAC__ASSERT(0);
1178 return false;
1179 }
1180
1181 {
1182 FLAC__int64 pos = tell_cb(handle);
1183 if(pos < 0) {
1184 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1185 return false;
1186 }
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001187 chain->first_offset = (FLAC__off_t)pos;
Josh Coalsonea25cb32004-07-10 00:39:01 +00001188 }
1189
1190 {
1191 FLAC__bool is_last;
1192 FLAC__MetadataType type;
1193 unsigned length;
1194
1195 do {
1196 node = node_new_();
1197 if(0 == node) {
1198 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1199 return false;
1200 }
1201
1202 if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) {
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001203 node_delete_(node);
Josh Coalsonea25cb32004-07-10 00:39:01 +00001204 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1205 return false;
1206 }
1207
1208 node->data = FLAC__metadata_object_new(type);
1209 if(0 == node->data) {
1210 node_delete_(node);
1211 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1212 return false;
1213 }
1214
1215 node->data->is_last = is_last;
1216 node->data->length = length;
1217
1218 chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data));
1219 if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
1220 node_delete_(node);
1221 return false;
1222 }
1223 chain_append_node_(chain, node);
1224 } while(!is_last);
1225 }
1226
1227 {
1228 FLAC__int64 pos = tell_cb(handle);
1229 if(pos < 0) {
1230 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1231 return false;
1232 }
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001233 chain->last_offset = (FLAC__off_t)pos;
Josh Coalsonea25cb32004-07-10 00:39:01 +00001234 }
1235
Josh Coalsonea25cb32004-07-10 00:39:01 +00001236 chain->initial_length = chain_calculate_length_(chain);
1237
1238 return true;
1239}
1240
Josh Coalsonf9cf27f2007-07-25 03:11:00 +00001241static FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalson7d273b42006-11-15 06:12:30 +00001242{
1243 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
1244 (void)decoder;
1245 if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) {
1246 *bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle);
1247 if(*bytes == 0)
1248 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
1249 else
1250 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
1251 }
1252 else
1253 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
1254}
1255
1256static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
1257{
1258 (void)decoder, (void)frame, (void)buffer, (void)client_data;
1259 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
1260}
1261
1262static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
1263{
1264 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
Josh Coalsonb0627a42006-11-17 06:05:02 +00001265 FLAC__Metadata_Node *node;
Josh Coalson7d273b42006-11-15 06:12:30 +00001266
1267 (void)decoder;
1268
Josh Coalsonb0627a42006-11-17 06:05:02 +00001269 node = node_new_();
Josh Coalson7d273b42006-11-15 06:12:30 +00001270 if(0 == node) {
1271 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1272 return;
1273 }
1274
1275 node->data = FLAC__metadata_object_clone(metadata);
1276 if(0 == node->data) {
1277 node_delete_(node);
1278 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1279 return;
1280 }
1281
1282 chain_append_node_(chain, node);
1283}
1284
1285static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
1286{
1287 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
1288 (void)decoder, (void)status;
1289 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1290}
1291
1292static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb)
1293{
1294 FLAC__StreamDecoder *decoder;
1295
1296 FLAC__ASSERT(0 != chain);
1297
1298 /* we assume we're already at the beginning of the file */
1299
1300 chain->handle = handle;
1301 chain->read_cb = read_cb;
1302 if(0 == (decoder = FLAC__stream_decoder_new())) {
1303 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1304 return false;
1305 }
1306 FLAC__stream_decoder_set_metadata_respond_all(decoder);
1307 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) {
1308 FLAC__stream_decoder_delete(decoder);
1309 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1310 return false;
1311 }
1312
1313 chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
1314
1315 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
1316 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1317 if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
1318 FLAC__stream_decoder_delete(decoder);
1319 return false;
1320 }
1321
1322 FLAC__stream_decoder_delete(decoder);
1323
1324 chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
1325
1326 chain->initial_length = chain_calculate_length_(chain);
1327
1328 return true;
1329}
1330
Josh Coalson0e46bf52004-07-15 00:04:46 +00001331static 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 Coalson463a22c2004-07-11 06:41:00 +00001332{
1333 FLAC__Metadata_Node *node;
1334
1335 FLAC__ASSERT(0 != chain);
1336 FLAC__ASSERT(0 != chain->head);
1337
1338 if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001339 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1340 return false;
1341 }
1342
1343 for(node = chain->head; node; node = node->next) {
1344 if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001345 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1346 return false;
1347 }
1348 if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001349 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1350 return false;
1351 }
1352 }
1353
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001354 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
Josh Coalson463a22c2004-07-11 06:41:00 +00001355
Josh Coalson463a22c2004-07-11 06:41:00 +00001356 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1357 return true;
1358}
1359
1360static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain)
1361{
1362 FILE *file;
Josh Coalson0e46bf52004-07-15 00:04:46 +00001363 FLAC__bool ret;
Josh Coalson463a22c2004-07-11 06:41:00 +00001364
1365 FLAC__ASSERT(0 != chain->filename);
1366
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11001367 if(0 == (file = flac_fopen(chain->filename, "r+b"))) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001368 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1369 return false;
1370 }
1371
Josh Coalson0e46bf52004-07-15 00:04:46 +00001372 /* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */
1373 ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_);
1374
1375 fclose(file);
1376
1377 return ret;
Josh Coalson463a22c2004-07-11 06:41:00 +00001378}
1379
1380static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
1381{
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11001382 FILE *f, *tempfile = NULL;
Josh Coalson463a22c2004-07-11 06:41:00 +00001383 char *tempfilename;
1384 FLAC__Metadata_SimpleIteratorStatus status;
1385 const FLAC__Metadata_Node *node;
1386
1387 FLAC__ASSERT(0 != chain);
1388 FLAC__ASSERT(0 != chain->filename);
1389 FLAC__ASSERT(0 != chain->head);
1390
1391 /* copy the file prefix (data up to first metadata block */
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11001392 if(0 == (f = flac_fopen(chain->filename, "rb"))) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001393 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1394 return false;
1395 }
1396 if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) {
1397 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001398 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001399 }
1400 if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) {
1401 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001402 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001403 }
1404
1405 /* write the metadata */
1406 for(node = chain->head; node; node = node->next) {
1407 if(!write_metadata_block_header_(tempfile, &status, node->data)) {
1408 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001409 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001410 }
1411 if(!write_metadata_block_data_(tempfile, &status, node->data)) {
1412 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001413 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001414 }
1415 }
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001416 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
Josh Coalson463a22c2004-07-11 06:41:00 +00001417
1418 /* copy the file postfix (everything after the metadata) */
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001419 if(0 != fseeko(f, chain->last_offset, SEEK_SET)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001420 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001421 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001422 }
1423 if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001424 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001425 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001426 }
1427
1428 /* move the tempfile on top of the original */
1429 (void)fclose(f);
1430 if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status))
1431 return false;
1432
1433 return true;
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001434
1435err:
1436 (void)fclose(f);
1437 cleanup_tempfile_(&tempfile, &tempfilename);
1438 return false;
Josh Coalson463a22c2004-07-11 06:41:00 +00001439}
1440
Josh Coalson9aac6702004-09-08 00:50:21 +00001441/* assumes 'handle' is already at beginning of file */
Josh Coalson0e46bf52004-07-15 00:04:46 +00001442static 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 Coalson463a22c2004-07-11 06:41:00 +00001443{
1444 FLAC__Metadata_SimpleIteratorStatus status;
1445 const FLAC__Metadata_Node *node;
1446
1447 FLAC__ASSERT(0 != chain);
1448 FLAC__ASSERT(0 == chain->filename);
1449 FLAC__ASSERT(0 != chain->head);
1450
1451 /* copy the file prefix (data up to first metadata block */
1452 if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001453 chain->status = get_equivalent_status_(status);
1454 return false;
1455 }
1456
1457 /* write the metadata */
1458 for(node = chain->head; node; node = node->next) {
1459 if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001460 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1461 return false;
1462 }
1463 if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001464 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1465 return false;
1466 }
1467 }
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001468 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
Josh Coalson463a22c2004-07-11 06:41:00 +00001469
1470 /* copy the file postfix (everything after the metadata) */
1471 if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001472 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1473 return false;
1474 }
1475 if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001476 chain->status = get_equivalent_status_(status);
1477 return false;
1478 }
1479
Josh Coalson463a22c2004-07-11 06:41:00 +00001480 return true;
1481}
1482
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001483FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void)
Josh Coalsoncae58732002-05-04 17:30:32 +00001484{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001485 FLAC__Metadata_Chain *chain = calloc(1, sizeof(FLAC__Metadata_Chain));
Josh Coalsoncae58732002-05-04 17:30:32 +00001486
Josh Coalson91748be2004-07-10 22:28:03 +00001487 if(0 != chain)
1488 chain_init_(chain);
Josh Coalsoncae58732002-05-04 17:30:32 +00001489
1490 return chain;
1491}
1492
Josh Coalson6afed9f2002-10-16 22:29:47 +00001493FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001494{
Josh Coalsoncae58732002-05-04 17:30:32 +00001495 FLAC__ASSERT(0 != chain);
1496
Josh Coalson91748be2004-07-10 22:28:03 +00001497 chain_clear_(chain);
Josh Coalsoncae58732002-05-04 17:30:32 +00001498
1499 free(chain);
1500}
1501
Josh Coalson6afed9f2002-10-16 22:29:47 +00001502FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001503{
Josh Coalsoncc682512002-06-08 04:53:42 +00001504 FLAC__Metadata_ChainStatus status;
Josh Coalsoncae58732002-05-04 17:30:32 +00001505
1506 FLAC__ASSERT(0 != chain);
1507
1508 status = chain->status;
1509 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1510 return status;
1511}
1512
Josh Coalson7d273b42006-11-15 06:12:30 +00001513static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg)
Josh Coalsoncae58732002-05-04 17:30:32 +00001514{
Josh Coalsonea25cb32004-07-10 00:39:01 +00001515 FILE *file;
Josh Coalson0e46bf52004-07-15 00:04:46 +00001516 FLAC__bool ret;
Josh Coalsoncae58732002-05-04 17:30:32 +00001517
1518 FLAC__ASSERT(0 != chain);
1519 FLAC__ASSERT(0 != filename);
1520
Josh Coalson91748be2004-07-10 22:28:03 +00001521 chain_clear_(chain);
1522
Josh Coalsoncae58732002-05-04 17:30:32 +00001523 if(0 == (chain->filename = strdup(filename))) {
1524 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1525 return false;
1526 }
1527
Josh Coalson7d273b42006-11-15 06:12:30 +00001528 chain->is_ogg = is_ogg;
1529
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11001530 if(0 == (file = flac_fopen(filename, "rb"))) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00001531 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
Josh Coalsoncae58732002-05-04 17:30:32 +00001532 return false;
1533 }
1534
Josh Coalson7d273b42006-11-15 06:12:30 +00001535 /* the function also sets chain->status for us */
1536 ret = is_ogg?
1537 chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) :
1538 chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_)
1539 ;
Josh Coalsoncae58732002-05-04 17:30:32 +00001540
Josh Coalson0e46bf52004-07-15 00:04:46 +00001541 fclose(file);
Josh Coalson91748be2004-07-10 22:28:03 +00001542
Josh Coalson0e46bf52004-07-15 00:04:46 +00001543 return ret;
Josh Coalson91748be2004-07-10 22:28:03 +00001544}
1545
Josh Coalson7d273b42006-11-15 06:12:30 +00001546FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
Josh Coalson91748be2004-07-10 22:28:03 +00001547{
Josh Coalson7d273b42006-11-15 06:12:30 +00001548 return chain_read_(chain, filename, /*is_ogg=*/false);
1549}
1550
Josh Coalson504dcaf2007-09-13 15:42:47 +00001551/*@@@@add to tests*/
Josh Coalson7d273b42006-11-15 06:12:30 +00001552FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename)
1553{
1554 return chain_read_(chain, filename, /*is_ogg=*/true);
1555}
1556
1557static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg)
1558{
1559 FLAC__bool ret;
1560
Josh Coalson91748be2004-07-10 22:28:03 +00001561 FLAC__ASSERT(0 != chain);
1562
1563 chain_clear_(chain);
1564
Josh Coalson0e46bf52004-07-15 00:04:46 +00001565 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) {
Josh Coalson91748be2004-07-10 22:28:03 +00001566 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1567 return false;
1568 }
1569
Josh Coalson7d273b42006-11-15 06:12:30 +00001570 chain->is_ogg = is_ogg;
1571
Josh Coalson9aac6702004-09-08 00:50:21 +00001572 /* rewind */
1573 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1574 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1575 return false;
1576 }
1577
Josh Coalson7d273b42006-11-15 06:12:30 +00001578 /* the function also sets chain->status for us */
1579 ret = is_ogg?
1580 chain_read_ogg_cb_(chain, handle, callbacks.read) :
1581 chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell)
1582 ;
Josh Coalson91748be2004-07-10 22:28:03 +00001583
Josh Coalson7d273b42006-11-15 06:12:30 +00001584 return ret;
1585}
1586
1587FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1588{
1589 return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false);
1590}
1591
Josh Coalson504dcaf2007-09-13 15:42:47 +00001592/*@@@@add to tests*/
Josh Coalson7d273b42006-11-15 06:12:30 +00001593FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1594{
1595 return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true);
Josh Coalsoncae58732002-05-04 17:30:32 +00001596}
1597
Josh Coalson463a22c2004-07-11 06:41:00 +00001598FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
1599{
1600 /* This does all the same checks that are in chain_prepare_for_write_()
1601 * but doesn't actually alter the chain. Make sure to update the logic
1602 * here if chain_prepare_for_write_() changes.
1603 */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001604 const FLAC__off_t current_length = chain_calculate_length_(chain);
Josh Coalson463a22c2004-07-11 06:41:00 +00001605
1606 FLAC__ASSERT(0 != chain);
1607
1608 if(use_padding) {
1609 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1610 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING)
1611 return false;
1612 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001613 else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length)
Josh Coalson463a22c2004-07-11 06:41:00 +00001614 return false;
1615 /* 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 */
1616 else if(current_length > chain->initial_length) {
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001617 const FLAC__off_t delta = current_length - chain->initial_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001618 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1619 /* if the delta is exactly the size of the last padding block, remove the padding block */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001620 if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta)
Josh Coalson463a22c2004-07-11 06:41:00 +00001621 return false;
1622 /* if there is at least 'delta' bytes of padding, trim the padding down */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001623 else if((FLAC__off_t)chain->tail->data->length >= delta)
Josh Coalson463a22c2004-07-11 06:41:00 +00001624 return false;
1625 }
1626 }
1627 }
1628
Josh Coalson028d97e2004-07-14 00:49:56 +00001629 return (current_length != chain->initial_length);
Josh Coalson463a22c2004-07-11 06:41:00 +00001630}
1631
Josh Coalson6afed9f2002-10-16 22:29:47 +00001632FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats)
Josh Coalsoncae58732002-05-04 17:30:32 +00001633{
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +11001634 struct flac_stat_s stats;
Josh Coalsoncae58732002-05-04 17:30:32 +00001635 const char *tempfile_path_prefix = 0;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001636 FLAC__off_t current_length;
Josh Coalson3ecb1172002-06-15 05:09:47 +00001637
1638 FLAC__ASSERT(0 != chain);
1639
Josh Coalson7d273b42006-11-15 06:12:30 +00001640 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1641 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1642 return false;
1643 }
1644
Josh Coalson463a22c2004-07-11 06:41:00 +00001645 if (0 == chain->filename) {
1646 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1647 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00001648 }
1649
Josh Coalson463a22c2004-07-11 06:41:00 +00001650 current_length = chain_prepare_for_write_(chain, use_padding);
1651
1652 /* a return value of 0 means there was an error; chain->status is already set */
1653 if (0 == current_length)
1654 return false;
1655
Josh Coalsoncae58732002-05-04 17:30:32 +00001656 if(preserve_file_stats)
1657 get_file_stats_(chain->filename, &stats);
1658
Josh Coalson3ecb1172002-06-15 05:09:47 +00001659 if(current_length == chain->initial_length) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001660 if(!chain_rewrite_metadata_in_place_(chain))
Josh Coalsoncae58732002-05-04 17:30:32 +00001661 return false;
1662 }
1663 else {
1664 if(!chain_rewrite_file_(chain, tempfile_path_prefix))
1665 return false;
Josh Coalson463a22c2004-07-11 06:41:00 +00001666
1667 /* recompute lengths and offsets */
1668 {
1669 const FLAC__Metadata_Node *node;
1670 chain->initial_length = current_length;
1671 chain->last_offset = chain->first_offset;
1672 for(node = chain->head; node; node = node->next)
1673 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1674 }
Josh Coalsoncae58732002-05-04 17:30:32 +00001675 }
1676
1677 if(preserve_file_stats)
1678 set_file_stats_(chain->filename, &stats);
1679
Josh Coalson463a22c2004-07-11 06:41:00 +00001680 return true;
1681}
1682
1683FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1684{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001685 FLAC__off_t current_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001686
1687 FLAC__ASSERT(0 != chain);
1688
Josh Coalson7d273b42006-11-15 06:12:30 +00001689 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1690 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1691 return false;
1692 }
1693
Josh Coalson463a22c2004-07-11 06:41:00 +00001694 if (0 != chain->filename) {
1695 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1696 return false;
1697 }
1698
Josh Coalson0e46bf52004-07-15 00:04:46 +00001699 if (0 == callbacks.write || 0 == callbacks.seek) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001700 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1701 return false;
1702 }
1703
1704 if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1705 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1706 return false;
1707 }
1708
1709 current_length = chain_prepare_for_write_(chain, use_padding);
1710
1711 /* a return value of 0 means there was an error; chain->status is already set */
1712 if (0 == current_length)
1713 return false;
1714
1715 FLAC__ASSERT(current_length == chain->initial_length);
1716
Josh Coalson0e46bf52004-07-15 00:04:46 +00001717 return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek);
Josh Coalson463a22c2004-07-11 06:41:00 +00001718}
1719
1720FLAC_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)
1721{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001722 FLAC__off_t current_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001723
1724 FLAC__ASSERT(0 != chain);
1725
Josh Coalson7d273b42006-11-15 06:12:30 +00001726 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1727 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1728 return false;
1729 }
1730
Josh Coalson463a22c2004-07-11 06:41:00 +00001731 if (0 != chain->filename) {
1732 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1733 return false;
1734 }
1735
Josh Coalson0e46bf52004-07-15 00:04:46 +00001736 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001737 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1738 return false;
1739 }
Josh Coalson0e46bf52004-07-15 00:04:46 +00001740 if (0 == temp_callbacks.write) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001741 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1742 return false;
1743 }
1744
1745 if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1746 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1747 return false;
1748 }
1749
1750 current_length = chain_prepare_for_write_(chain, use_padding);
1751
1752 /* a return value of 0 means there was an error; chain->status is already set */
1753 if (0 == current_length)
1754 return false;
1755
1756 FLAC__ASSERT(current_length != chain->initial_length);
1757
Josh Coalson9aac6702004-09-08 00:50:21 +00001758 /* rewind */
1759 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1760 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1761 return false;
1762 }
1763
Josh Coalson0e46bf52004-07-15 00:04:46 +00001764 if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write))
Josh Coalson463a22c2004-07-11 06:41:00 +00001765 return false;
1766
1767 /* recompute lengths and offsets */
1768 {
Josh Coalsoncc682512002-06-08 04:53:42 +00001769 const FLAC__Metadata_Node *node;
Josh Coalson3ecb1172002-06-15 05:09:47 +00001770 chain->initial_length = current_length;
Josh Coalsone2ba10f2002-05-14 06:03:17 +00001771 chain->last_offset = chain->first_offset;
1772 for(node = chain->head; node; node = node->next)
Josh Coalsoneb643a22002-05-20 05:58:50 +00001773 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
Josh Coalsone2ba10f2002-05-14 06:03:17 +00001774 }
1775
Josh Coalsoncae58732002-05-04 17:30:32 +00001776 return true;
1777}
1778
Josh Coalson6afed9f2002-10-16 22:29:47 +00001779FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001780{
Josh Coalsoncc682512002-06-08 04:53:42 +00001781 FLAC__Metadata_Node *node;
Josh Coalsoncae58732002-05-04 17:30:32 +00001782
1783 FLAC__ASSERT(0 != chain);
1784
1785 for(node = chain->head; node; ) {
Josh Coalson2745fe72002-05-11 05:31:56 +00001786 if(!chain_merge_adjacent_padding_(chain, node))
Josh Coalsoncae58732002-05-04 17:30:32 +00001787 node = node->next;
Josh Coalsoncae58732002-05-04 17:30:32 +00001788 }
Josh Coalsoncae58732002-05-04 17:30:32 +00001789}
1790
Josh Coalson6afed9f2002-10-16 22:29:47 +00001791FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001792{
Josh Coalsoncc682512002-06-08 04:53:42 +00001793 FLAC__Metadata_Node *node, *save;
Josh Coalsoncae58732002-05-04 17:30:32 +00001794 unsigned i;
1795
1796 FLAC__ASSERT(0 != chain);
1797
1798 /*
1799 * Don't try and be too smart... this simple algo is good enough for
1800 * the small number of nodes that we deal with.
1801 */
1802 for(i = 0, node = chain->head; i < chain->nodes; i++) {
Josh Coalsoncae58732002-05-04 17:30:32 +00001803 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1804 save = node->next;
1805 chain_remove_node_(chain, node);
1806 chain_append_node_(chain, node);
1807 node = save;
1808 }
1809 else {
1810 node = node->next;
1811 }
1812 }
Josh Coalsoncae58732002-05-04 17:30:32 +00001813
1814 FLAC__metadata_chain_merge_padding(chain);
1815}
1816
1817
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001818FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void)
Josh Coalsoncae58732002-05-04 17:30:32 +00001819{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001820 FLAC__Metadata_Iterator *iterator = calloc(1, sizeof(FLAC__Metadata_Iterator));
Josh Coalsoncae58732002-05-04 17:30:32 +00001821
Josh Coalsonea7155f2002-10-18 05:49:19 +00001822 /* calloc() implies:
Josh Coalsoncae58732002-05-04 17:30:32 +00001823 iterator->current = 0;
1824 iterator->chain = 0;
Josh Coalsonea7155f2002-10-18 05:49:19 +00001825 */
Josh Coalsoncae58732002-05-04 17:30:32 +00001826
1827 return iterator;
1828}
1829
Josh Coalson6afed9f2002-10-16 22:29:47 +00001830FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001831{
1832 FLAC__ASSERT(0 != iterator);
1833
1834 free(iterator);
1835}
1836
Josh Coalson6afed9f2002-10-16 22:29:47 +00001837FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001838{
1839 FLAC__ASSERT(0 != iterator);
1840 FLAC__ASSERT(0 != chain);
1841 FLAC__ASSERT(0 != chain->head);
1842
1843 iterator->chain = chain;
1844 iterator->current = chain->head;
1845}
1846
Josh Coalson6afed9f2002-10-16 22:29:47 +00001847FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001848{
1849 FLAC__ASSERT(0 != iterator);
1850
1851 if(0 == iterator->current || 0 == iterator->current->next)
1852 return false;
1853
1854 iterator->current = iterator->current->next;
1855 return true;
1856}
1857
Josh Coalson6afed9f2002-10-16 22:29:47 +00001858FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001859{
1860 FLAC__ASSERT(0 != iterator);
1861
1862 if(0 == iterator->current || 0 == iterator->current->prev)
1863 return false;
1864
1865 iterator->current = iterator->current->prev;
1866 return true;
1867}
1868
Josh Coalson6afed9f2002-10-16 22:29:47 +00001869FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001870{
1871 FLAC__ASSERT(0 != iterator);
1872 FLAC__ASSERT(0 != iterator->current);
Josh Coalson2745fe72002-05-11 05:31:56 +00001873 FLAC__ASSERT(0 != iterator->current->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001874
1875 return iterator->current->data->type;
1876}
1877
Josh Coalson6afed9f2002-10-16 22:29:47 +00001878FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001879{
1880 FLAC__ASSERT(0 != iterator);
1881 FLAC__ASSERT(0 != iterator->current);
1882
1883 return iterator->current->data;
1884}
1885
Josh Coalson6afed9f2002-10-16 22:29:47 +00001886FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
Josh Coalson2745fe72002-05-11 05:31:56 +00001887{
Josh Coalson9b86c962002-07-10 07:11:15 +00001888 FLAC__ASSERT(0 != iterator);
1889 FLAC__ASSERT(0 != block);
Josh Coalson2745fe72002-05-11 05:31:56 +00001890 return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block);
1891}
1892
Josh Coalson6afed9f2002-10-16 22:29:47 +00001893FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding)
Josh Coalsoncae58732002-05-04 17:30:32 +00001894{
Josh Coalsoncc682512002-06-08 04:53:42 +00001895 FLAC__Metadata_Node *save;
Josh Coalsoncae58732002-05-04 17:30:32 +00001896
1897 FLAC__ASSERT(0 != iterator);
1898 FLAC__ASSERT(0 != iterator->current);
1899
1900 if(0 == iterator->current->prev) {
1901 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1902 return false;
1903 }
1904
1905 save = iterator->current->prev;
1906
1907 if(replace_with_padding) {
Josh Coalson90ced912002-05-30 05:23:38 +00001908 FLAC__metadata_object_delete_data(iterator->current->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001909 iterator->current->data->type = FLAC__METADATA_TYPE_PADDING;
1910 }
1911 else {
1912 chain_delete_node_(iterator->chain, iterator->current);
1913 }
1914
1915 iterator->current = save;
1916 return true;
1917}
1918
Josh Coalson6afed9f2002-10-16 22:29:47 +00001919FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00001920{
Josh Coalsoncc682512002-06-08 04:53:42 +00001921 FLAC__Metadata_Node *node;
Josh Coalsoncae58732002-05-04 17:30:32 +00001922
1923 FLAC__ASSERT(0 != iterator);
1924 FLAC__ASSERT(0 != iterator->current);
Josh Coalson9b86c962002-07-10 07:11:15 +00001925 FLAC__ASSERT(0 != block);
Josh Coalsoncae58732002-05-04 17:30:32 +00001926
1927 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1928 return false;
1929
1930 if(0 == iterator->current->prev) {
1931 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1932 return false;
1933 }
1934
1935 if(0 == (node = node_new_()))
1936 return false;
1937
1938 node->data = block;
1939 iterator_insert_node_(iterator, node);
1940 iterator->current = node;
1941 return true;
1942}
1943
Josh Coalson6afed9f2002-10-16 22:29:47 +00001944FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00001945{
Josh Coalsoncc682512002-06-08 04:53:42 +00001946 FLAC__Metadata_Node *node;
Josh Coalsoncae58732002-05-04 17:30:32 +00001947
1948 FLAC__ASSERT(0 != iterator);
1949 FLAC__ASSERT(0 != iterator->current);
Josh Coalson9b86c962002-07-10 07:11:15 +00001950 FLAC__ASSERT(0 != block);
Josh Coalsoncae58732002-05-04 17:30:32 +00001951
1952 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1953 return false;
1954
1955 if(0 == (node = node_new_()))
1956 return false;
1957
1958 node->data = block;
1959 iterator_insert_node_after_(iterator, node);
1960 iterator->current = node;
1961 return true;
1962}
1963
1964
1965/****************************************************************************
1966 *
Josh Coalsoncae58732002-05-04 17:30:32 +00001967 * Local function definitions
1968 *
1969 ***************************************************************************/
1970
1971void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1972{
1973 unsigned i;
1974
1975 b += bytes;
1976
1977 for(i = 0; i < bytes; i++) {
Josh Coalsoneb643a22002-05-20 05:58:50 +00001978 *(--b) = (FLAC__byte)(val & 0xff);
Josh Coalsoncae58732002-05-04 17:30:32 +00001979 val >>= 8;
1980 }
1981}
1982
1983void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
1984{
1985 unsigned i;
1986
1987 for(i = 0; i < bytes; i++) {
Josh Coalsoneb643a22002-05-20 05:58:50 +00001988 *(b++) = (FLAC__byte)(val & 0xff);
Josh Coalsoncae58732002-05-04 17:30:32 +00001989 val >>= 8;
1990 }
1991}
1992
1993void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes)
1994{
1995 unsigned i;
1996
1997 b += bytes;
1998
1999 for(i = 0; i < bytes; i++) {
Josh Coalson72943092002-05-22 05:35:43 +00002000 *(--b) = (FLAC__byte)(val & 0xff);
Josh Coalsoncae58732002-05-04 17:30:32 +00002001 val >>= 8;
2002 }
2003}
2004
2005FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes)
2006{
2007 FLAC__uint32 ret = 0;
2008 unsigned i;
2009
2010 for(i = 0; i < bytes; i++)
2011 ret = (ret << 8) | (FLAC__uint32)(*b++);
2012
2013 return ret;
2014}
2015
2016FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes)
2017{
2018 FLAC__uint32 ret = 0;
2019 unsigned i;
2020
2021 b += bytes;
2022
2023 for(i = 0; i < bytes; i++)
2024 ret = (ret << 8) | (FLAC__uint32)(*--b);
2025
2026 return ret;
2027}
2028
2029FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes)
2030{
2031 FLAC__uint64 ret = 0;
2032 unsigned i;
2033
2034 for(i = 0; i < bytes; i++)
2035 ret = (ret << 8) | (FLAC__uint64)(*b++);
2036
2037 return ret;
2038}
2039
Josh Coalsoncc682512002-06-08 04:53:42 +00002040FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00002041{
Josh Coalsoncae58732002-05-04 17:30:32 +00002042 FLAC__ASSERT(0 != iterator);
2043 FLAC__ASSERT(0 != iterator->file);
2044
Josh Coalsonea25cb32004-07-10 00:39:01 +00002045 if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002046 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2047 return false;
2048 }
2049
Josh Coalsoncae58732002-05-04 17:30:32 +00002050 return true;
2051}
2052
Josh Coalsoncc682512002-06-08 04:53:42 +00002053FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002054{
2055 FLAC__ASSERT(0 != iterator);
2056 FLAC__ASSERT(0 != iterator->file);
2057
Josh Coalsonea25cb32004-07-10 00:39:01 +00002058 iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block);
Josh Coalsoncae58732002-05-04 17:30:32 +00002059
2060 return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
2061}
2062
Josh Coalsonea25cb32004-07-10 00:39:01 +00002063FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length)
2064{
2065 FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH];
2066
2067 if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
2068 return false;
2069
2070 *is_last = raw_header[0] & 0x80? true : false;
2071 *type = (FLAC__MetadataType)(raw_header[0] & 0x7f);
2072 *length = unpack_uint32_(raw_header + 1, 3);
2073
2074 /* Note that we don't check:
2075 * if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED)
2076 * we just will read in an opaque block
2077 */
2078
2079 return true;
2080}
2081
2082FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block)
2083{
2084 switch(block->type) {
2085 case FLAC__METADATA_TYPE_STREAMINFO:
2086 return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info);
2087 case FLAC__METADATA_TYPE_PADDING:
2088 return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length);
2089 case FLAC__METADATA_TYPE_APPLICATION:
2090 return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length);
2091 case FLAC__METADATA_TYPE_SEEKTABLE:
2092 return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length);
2093 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
2094 return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, &block->data.vorbis_comment);
2095 case FLAC__METADATA_TYPE_CUESHEET:
2096 return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +00002097 case FLAC__METADATA_TYPE_PICTURE:
2098 return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture);
Josh Coalsonea25cb32004-07-10 00:39:01 +00002099 default:
2100 return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length);
2101 }
2102}
2103
2104FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002105{
2106 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b;
2107
Josh Coalsonea25cb32004-07-10 00:39:01 +00002108 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
Josh Coalsoncae58732002-05-04 17:30:32 +00002109 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2110
2111 b = buffer;
2112
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002113 /* we are using hardcoded numbers for simplicity but we should
Josh Coalsoncae58732002-05-04 17:30:32 +00002114 * probably eventually write a bit-level unpacker and use the
2115 * _STREAMINFO_ constants.
2116 */
2117 block->min_blocksize = unpack_uint32_(b, 2); b += 2;
2118 block->max_blocksize = unpack_uint32_(b, 2); b += 2;
2119 block->min_framesize = unpack_uint32_(b, 3); b += 3;
2120 block->max_framesize = unpack_uint32_(b, 3); b += 3;
2121 block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
2122 block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
Josh Coalsonb4e186a2002-08-12 21:35:43 +00002123 block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
Josh Coalsoncae58732002-05-04 17:30:32 +00002124 block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4);
2125 memcpy(block->md5sum, b+8, 16);
2126
2127 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2128}
2129
Josh Coalsonea25cb32004-07-10 00:39:01 +00002130FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length)
Josh Coalsoncae58732002-05-04 17:30:32 +00002131{
Josh Coalsoncae58732002-05-04 17:30:32 +00002132 (void)block; /* nothing to do; we don't care about reading the padding bytes */
2133
Josh Coalsonea25cb32004-07-10 00:39:01 +00002134 if(0 != seek_cb(handle, block_length, SEEK_CUR))
Josh Coalsoncae58732002-05-04 17:30:32 +00002135 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2136
2137 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2138}
2139
Josh Coalsonea25cb32004-07-10 00:39:01 +00002140FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length)
Josh Coalsoncae58732002-05-04 17:30:32 +00002141{
2142 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
2143
Josh Coalsonea25cb32004-07-10 00:39:01 +00002144 if(read_cb(block->id, 1, id_bytes, handle) != id_bytes)
Josh Coalsoncae58732002-05-04 17:30:32 +00002145 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2146
Josh Coalson0f008d22007-09-11 04:49:56 +00002147 if(block_length < id_bytes)
2148 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2149
Josh Coalsoncae58732002-05-04 17:30:32 +00002150 block_length -= id_bytes;
2151
Josh Coalson72943092002-05-22 05:35:43 +00002152 if(block_length == 0) {
2153 block->data = 0;
2154 }
2155 else {
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002156 if(0 == (block->data = malloc(block_length)))
Josh Coalson72943092002-05-22 05:35:43 +00002157 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncae58732002-05-04 17:30:32 +00002158
Josh Coalsonea25cb32004-07-10 00:39:01 +00002159 if(read_cb(block->data, 1, block_length, handle) != block_length)
Josh Coalson72943092002-05-22 05:35:43 +00002160 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2161 }
Josh Coalsoncae58732002-05-04 17:30:32 +00002162
2163 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2164}
2165
Josh Coalsonea25cb32004-07-10 00:39:01 +00002166FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length)
Josh Coalsoncae58732002-05-04 17:30:32 +00002167{
2168 unsigned i;
2169 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
2170
Josh Coalsoncae58732002-05-04 17:30:32 +00002171 FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0);
2172
2173 block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
2174
Josh Coalson72943092002-05-22 05:35:43 +00002175 if(block->num_points == 0)
2176 block->points = 0;
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +10002177 else if(0 == (block->points = safe_malloc_mul_2op_p(block->num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint))))
Josh Coalsoncae58732002-05-04 17:30:32 +00002178 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2179
2180 for(i = 0; i < block->num_points; i++) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00002181 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
Josh Coalsoncae58732002-05-04 17:30:32 +00002182 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002183 /* some MAGIC NUMBERs here */
Josh Coalsoncae58732002-05-04 17:30:32 +00002184 block->points[i].sample_number = unpack_uint64_(buffer, 8);
2185 block->points[i].stream_offset = unpack_uint64_(buffer+8, 8);
2186 block->points[i].frame_samples = unpack_uint32_(buffer+16, 2);
2187 }
2188
2189 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2190}
2191
Josh Coalsonea25cb32004-07-10 00:39:01 +00002192FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry)
Josh Coalsoncae58732002-05-04 17:30:32 +00002193{
2194 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
Josh Coalson7ee4d172002-06-01 06:31:27 +00002195 FLAC__byte buffer[4]; /* magic number is asserted below */
Josh Coalsoncae58732002-05-04 17:30:32 +00002196
Josh Coalsone343ab22006-09-23 19:21:19 +00002197 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer));
Josh Coalsoncae58732002-05-04 17:30:32 +00002198
Josh Coalsonea25cb32004-07-10 00:39:01 +00002199 if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
Josh Coalsoncae58732002-05-04 17:30:32 +00002200 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalsoneb643a22002-05-20 05:58:50 +00002201 entry->length = unpack_uint32_little_endian_(buffer, entry_length_len);
Josh Coalsoncae58732002-05-04 17:30:32 +00002202
Josh Coalson6ffe8da2002-10-26 04:35:00 +00002203 if(0 != entry->entry)
2204 free(entry->entry);
2205
Josh Coalson72943092002-05-22 05:35:43 +00002206 if(entry->length == 0) {
2207 entry->entry = 0;
2208 }
2209 else {
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002210 if(0 == (entry->entry = safe_malloc_add_2op_(entry->length, /*+*/1)))
Josh Coalson72943092002-05-22 05:35:43 +00002211 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncae58732002-05-04 17:30:32 +00002212
Josh Coalsonea25cb32004-07-10 00:39:01 +00002213 if(read_cb(entry->entry, 1, entry->length, handle) != entry->length)
Josh Coalson72943092002-05-22 05:35:43 +00002214 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalsondef597e2004-12-30 00:59:30 +00002215
2216 entry->entry[entry->length] = '\0';
Josh Coalson72943092002-05-22 05:35:43 +00002217 }
Josh Coalsoncae58732002-05-04 17:30:32 +00002218
2219 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2220}
2221
Josh Coalsonea25cb32004-07-10 00:39:01 +00002222FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002223{
2224 unsigned i;
Josh Coalsoncc682512002-06-08 04:53:42 +00002225 FLAC__Metadata_SimpleIteratorStatus status;
Josh Coalsoncae58732002-05-04 17:30:32 +00002226 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
Josh Coalson7ee4d172002-06-01 06:31:27 +00002227 FLAC__byte buffer[4]; /* magic number is asserted below */
Josh Coalsoncae58732002-05-04 17:30:32 +00002228
Josh Coalsone343ab22006-09-23 19:21:19 +00002229 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer));
Josh Coalsoncae58732002-05-04 17:30:32 +00002230
Josh Coalsonea25cb32004-07-10 00:39:01 +00002231 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string))))
Josh Coalsoncae58732002-05-04 17:30:32 +00002232 return status;
2233
Josh Coalsonea25cb32004-07-10 00:39:01 +00002234 if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
Josh Coalsoncae58732002-05-04 17:30:32 +00002235 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2236 block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len);
2237
Josh Coalson72943092002-05-22 05:35:43 +00002238 if(block->num_comments == 0) {
2239 block->comments = 0;
2240 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002241 else if(0 == (block->comments = calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry))))
Josh Coalsoncae58732002-05-04 17:30:32 +00002242 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2243
2244 for(i = 0; i < block->num_comments; i++) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00002245 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i)))
Josh Coalsoncae58732002-05-04 17:30:32 +00002246 return status;
2247 }
2248
2249 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2250}
2251
Josh Coalsonea25cb32004-07-10 00:39:01 +00002252FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track)
Josh Coalsone4869382002-11-15 05:41:48 +00002253{
2254 unsigned i, len;
2255 FLAC__byte buffer[32]; /* asserted below that this is big enough */
2256
2257 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
2258 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2259 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);
2260
Josh Coalsone4869382002-11-15 05:41:48 +00002261 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2262 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002263 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002264 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2265 track->offset = unpack_uint64_(buffer, len);
2266
2267 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2268 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002269 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002270 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2271 track->number = (FLAC__byte)unpack_uint32_(buffer, len);
2272
2273 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2274 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002275 if(read_cb(track->isrc, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002276 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2277
2278 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);
2279 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 Coalsonea25cb32004-07-10 00:39:01 +00002280 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002281 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2282 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1);
2283 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1);
2284 track->type = buffer[0] >> 7;
2285 track->pre_emphasis = (buffer[0] >> 6) & 1;
2286
2287 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2288 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002289 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002290 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2291 track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len);
2292
2293 if(track->num_indices == 0) {
2294 track->indices = 0;
2295 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002296 else if(0 == (track->indices = calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
Josh Coalsone4869382002-11-15 05:41:48 +00002297 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2298
2299 for(i = 0; i < track->num_indices; i++) {
2300 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2301 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002302 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002303 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2304 track->indices[i].offset = unpack_uint64_(buffer, len);
2305
2306 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2307 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002308 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002309 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2310 track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len);
2311
2312 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2313 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002314 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002315 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2316 }
2317
2318 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2319}
2320
Josh Coalsonea25cb32004-07-10 00:39:01 +00002321FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block)
Josh Coalsone4869382002-11-15 05:41:48 +00002322{
2323 unsigned i, len;
2324 FLAC__Metadata_SimpleIteratorStatus status;
Josh Coalsonbfc8e312002-11-21 09:00:25 +00002325 FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */
Josh Coalsone4869382002-11-15 05:41:48 +00002326
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002327 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer));
Josh Coalsonbfc8e312002-11-21 09:00:25 +00002328 FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer));
Josh Coalsone4869382002-11-15 05:41:48 +00002329
2330 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2331 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002332 if(read_cb(block->media_catalog_number, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002333 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2334
2335 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2336 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002337 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002338 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2339 block->lead_in = unpack_uint64_(buffer, len);
2340
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002341 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2342 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002343 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalson38162d52002-11-16 06:31:54 +00002344 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002345 block->is_cd = buffer[0]&0x80? true : false;
Josh Coalson38162d52002-11-16 06:31:54 +00002346
Josh Coalsone4869382002-11-15 05:41:48 +00002347 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2348 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002349 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002350 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2351 block->num_tracks = unpack_uint32_(buffer, len);
2352
2353 if(block->num_tracks == 0) {
2354 block->tracks = 0;
2355 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002356 else if(0 == (block->tracks = calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
Josh Coalsone4869382002-11-15 05:41:48 +00002357 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2358
2359 for(i = 0; i < block->num_tracks; i++) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00002360 if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i)))
Josh Coalsone4869382002-11-15 05:41:48 +00002361 return status;
2362 }
2363
2364 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2365}
2366
Josh Coalsonf9cf27f2007-07-25 03:11:00 +00002367static 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 Coalsone343ab22006-09-23 19:21:19 +00002368{
2369 FLAC__byte buffer[sizeof(FLAC__uint32)];
2370
2371 FLAC__ASSERT(0 != data);
2372 FLAC__ASSERT(length_len%8 == 0);
2373
2374 length_len /= 8; /* convert to bytes */
2375
2376 FLAC__ASSERT(sizeof(buffer) >= length_len);
2377
2378 if(read_cb(buffer, 1, length_len, handle) != length_len)
2379 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2380 *length = unpack_uint32_(buffer, length_len);
2381
2382 if(0 != *data)
2383 free(*data);
2384
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002385 if(0 == (*data = safe_malloc_add_2op_(*length, /*+*/1)))
Josh Coalsone343ab22006-09-23 19:21:19 +00002386 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2387
2388 if(*length > 0) {
2389 if(read_cb(*data, 1, *length, handle) != *length)
2390 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2391 }
2392
2393 (*data)[*length] = '\0';
2394
2395 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2396}
2397
2398FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block)
2399{
2400 FLAC__Metadata_SimpleIteratorStatus status;
2401 FLAC__byte buffer[4]; /* asserted below that this is big enough */
2402 FLAC__uint32 len;
2403
2404 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2405 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2406 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2407 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
Josh Coalson74ed2942006-09-23 23:15:05 +00002408 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
Josh Coalsone343ab22006-09-23 19:21:19 +00002409
2410 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0);
2411 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8;
2412 if(read_cb(buffer, 1, len, handle) != len)
2413 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2414 block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len);
2415
2416 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)
2417 return status;
2418
2419 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)
2420 return status;
2421
2422 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0);
2423 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8;
2424 if(read_cb(buffer, 1, len, handle) != len)
2425 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2426 block->width = unpack_uint32_(buffer, len);
2427
2428 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0);
2429 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8;
2430 if(read_cb(buffer, 1, len, handle) != len)
2431 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2432 block->height = unpack_uint32_(buffer, len);
2433
2434 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0);
2435 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8;
2436 if(read_cb(buffer, 1, len, handle) != len)
2437 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2438 block->depth = unpack_uint32_(buffer, len);
2439
Josh Coalson74ed2942006-09-23 23:15:05 +00002440 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0);
2441 len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8;
2442 if(read_cb(buffer, 1, len, handle) != len)
2443 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2444 block->colors = unpack_uint32_(buffer, len);
2445
Josh Coalsone343ab22006-09-23 19:21:19 +00002446 /* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */
2447 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)
2448 return status;
2449
2450 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2451}
2452
Josh Coalsonea25cb32004-07-10 00:39:01 +00002453FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length)
Josh Coalsond0609472003-01-10 05:37:13 +00002454{
Josh Coalsond0609472003-01-10 05:37:13 +00002455 if(block_length == 0) {
2456 block->data = 0;
2457 }
2458 else {
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002459 if(0 == (block->data = malloc(block_length)))
Josh Coalsond0609472003-01-10 05:37:13 +00002460 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2461
Josh Coalsonea25cb32004-07-10 00:39:01 +00002462 if(read_cb(block->data, 1, block_length, handle) != block_length)
Josh Coalsond0609472003-01-10 05:37:13 +00002463 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2464 }
2465
2466 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2467}
2468
Josh Coalsoncc682512002-06-08 04:53:42 +00002469FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002470{
Josh Coalsoncae58732002-05-04 17:30:32 +00002471 FLAC__ASSERT(0 != file);
2472 FLAC__ASSERT(0 != status);
Josh Coalsoncae58732002-05-04 17:30:32 +00002473
Josh Coalson463a22c2004-07-11 06:41:00 +00002474 if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002475 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2476 return false;
2477 }
2478
2479 return true;
2480}
2481
Josh Coalsoncc682512002-06-08 04:53:42 +00002482FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002483{
2484 FLAC__ASSERT(0 != file);
2485 FLAC__ASSERT(0 != status);
2486
Josh Coalson463a22c2004-07-11 06:41:00 +00002487 if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
2488 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2489 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002490 }
Josh Coalson463a22c2004-07-11 06:41:00 +00002491 else {
2492 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2493 return false;
2494 }
Josh Coalsoncae58732002-05-04 17:30:32 +00002495}
2496
Josh Coalson463a22c2004-07-11 06:41:00 +00002497FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2498{
2499 FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];
2500
2501 FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
2502
2503 buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
2504 pack_uint32_(block->length, buffer + 1, 3);
2505
2506 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
2507 return false;
2508
2509 return true;
2510}
2511
2512FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2513{
2514 FLAC__ASSERT(0 != block);
2515
2516 switch(block->type) {
2517 case FLAC__METADATA_TYPE_STREAMINFO:
2518 return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info);
2519 case FLAC__METADATA_TYPE_PADDING:
2520 return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length);
2521 case FLAC__METADATA_TYPE_APPLICATION:
2522 return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length);
2523 case FLAC__METADATA_TYPE_SEEKTABLE:
2524 return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table);
2525 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
2526 return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment);
2527 case FLAC__METADATA_TYPE_CUESHEET:
2528 return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +00002529 case FLAC__METADATA_TYPE_PICTURE:
2530 return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture);
Josh Coalson463a22c2004-07-11 06:41:00 +00002531 default:
2532 return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length);
2533 }
2534}
2535
2536FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002537{
2538 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
2539 const unsigned channels1 = block->channels - 1;
2540 const unsigned bps1 = block->bits_per_sample - 1;
2541
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002542 /* we are using hardcoded numbers for simplicity but we should
Josh Coalsoncae58732002-05-04 17:30:32 +00002543 * probably eventually write a bit-level packer and use the
2544 * _STREAMINFO_ constants.
2545 */
2546 pack_uint32_(block->min_blocksize, buffer, 2);
2547 pack_uint32_(block->max_blocksize, buffer+2, 2);
2548 pack_uint32_(block->min_framesize, buffer+4, 3);
2549 pack_uint32_(block->max_framesize, buffer+7, 3);
2550 buffer[10] = (block->sample_rate >> 12) & 0xff;
2551 buffer[11] = (block->sample_rate >> 4) & 0xff;
2552 buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4);
Josh Coalson7ee4d172002-06-01 06:31:27 +00002553 buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f));
Josh Coalsoncae58732002-05-04 17:30:32 +00002554 pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
2555 memcpy(buffer+18, block->md5sum, 16);
2556
Josh Coalson463a22c2004-07-11 06:41:00 +00002557 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
2558 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002559
Josh Coalson463a22c2004-07-11 06:41:00 +00002560 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002561}
2562
Josh Coalson463a22c2004-07-11 06:41:00 +00002563FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length)
Josh Coalsoncae58732002-05-04 17:30:32 +00002564{
2565 unsigned i, n = block_length;
2566 FLAC__byte buffer[1024];
2567
Josh Coalsoncae58732002-05-04 17:30:32 +00002568 (void)block;
2569
2570 memset(buffer, 0, 1024);
2571
2572 for(i = 0; i < n/1024; i++)
Josh Coalson463a22c2004-07-11 06:41:00 +00002573 if(write_cb(buffer, 1, 1024, handle) != 1024)
2574 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002575
2576 n %= 1024;
Josh Coalson765ff502002-08-27 05:46:11 +00002577
Josh Coalson463a22c2004-07-11 06:41:00 +00002578 if(write_cb(buffer, 1, n, handle) != n)
2579 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002580
Josh Coalson463a22c2004-07-11 06:41:00 +00002581 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002582}
2583
Josh Coalson463a22c2004-07-11 06:41:00 +00002584FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length)
Josh Coalsoncae58732002-05-04 17:30:32 +00002585{
2586 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
2587
Josh Coalson463a22c2004-07-11 06:41:00 +00002588 if(write_cb(block->id, 1, id_bytes, handle) != id_bytes)
2589 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002590
2591 block_length -= id_bytes;
2592
Josh Coalson463a22c2004-07-11 06:41:00 +00002593 if(write_cb(block->data, 1, block_length, handle) != block_length)
2594 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002595
Josh Coalson463a22c2004-07-11 06:41:00 +00002596 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002597}
2598
Josh Coalson463a22c2004-07-11 06:41:00 +00002599FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002600{
2601 unsigned i;
2602 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
2603
Josh Coalsoncae58732002-05-04 17:30:32 +00002604 for(i = 0; i < block->num_points; i++) {
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002605 /* some MAGIC NUMBERs here */
Josh Coalsoncae58732002-05-04 17:30:32 +00002606 pack_uint64_(block->points[i].sample_number, buffer, 8);
2607 pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
2608 pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
Josh Coalson463a22c2004-07-11 06:41:00 +00002609 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
2610 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002611 }
2612
Josh Coalson463a22c2004-07-11 06:41:00 +00002613 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002614}
2615
Josh Coalson463a22c2004-07-11 06:41:00 +00002616FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002617{
2618 unsigned i;
2619 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
2620 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
Josh Coalson7ee4d172002-06-01 06:31:27 +00002621 FLAC__byte buffer[4]; /* magic number is asserted below */
Josh Coalsoncae58732002-05-04 17:30:32 +00002622
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002623 FLAC__ASSERT(flac_max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer));
Josh Coalsoncae58732002-05-04 17:30:32 +00002624
2625 pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002626 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2627 return false;
2628 if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length)
2629 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002630
2631 pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002632 if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
2633 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002634
2635 for(i = 0; i < block->num_comments; i++) {
2636 pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002637 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2638 return false;
2639 if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length)
2640 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002641 }
2642
Josh Coalson463a22c2004-07-11 06:41:00 +00002643 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002644}
2645
Josh Coalson463a22c2004-07-11 06:41:00 +00002646FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block)
Josh Coalsone4869382002-11-15 05:41:48 +00002647{
2648 unsigned i, j, len;
Josh Coalson38162d52002-11-16 06:31:54 +00002649 FLAC__byte buffer[1024]; /* asserted below that this is big enough */
Josh Coalsone4869382002-11-15 05:41:48 +00002650
2651 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
Josh Coalson38162d52002-11-16 06:31:54 +00002652 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8);
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002653 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 Coalsone4869382002-11-15 05:41:48 +00002654 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2655
Josh Coalsone4869382002-11-15 05:41:48 +00002656 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2657 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
Josh Coalson463a22c2004-07-11 06:41:00 +00002658 if(write_cb(block->media_catalog_number, 1, len, handle) != len)
2659 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002660
2661 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2662 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
2663 pack_uint64_(block->lead_in, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002664 if(write_cb(buffer, 1, len, handle) != len)
2665 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002666
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002667 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2668 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
Josh Coalson38162d52002-11-16 06:31:54 +00002669 memset(buffer, 0, len);
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002670 if(block->is_cd)
2671 buffer[0] |= 0x80;
Josh Coalson463a22c2004-07-11 06:41:00 +00002672 if(write_cb(buffer, 1, len, handle) != len)
2673 return false;
Josh Coalson38162d52002-11-16 06:31:54 +00002674
Josh Coalsone4869382002-11-15 05:41:48 +00002675 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2676 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
2677 pack_uint32_(block->num_tracks, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002678 if(write_cb(buffer, 1, len, handle) != len)
2679 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002680
2681 for(i = 0; i < block->num_tracks; i++) {
2682 FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i;
2683
2684 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2685 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
2686 pack_uint64_(track->offset, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002687 if(write_cb(buffer, 1, len, handle) != len)
2688 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002689
2690 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2691 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
2692 pack_uint32_(track->number, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002693 if(write_cb(buffer, 1, len, handle) != len)
2694 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002695
2696 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2697 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
Josh Coalson463a22c2004-07-11 06:41:00 +00002698 if(write_cb(track->isrc, 1, len, handle) != len)
2699 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002700
2701 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);
2702 len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
2703 memset(buffer, 0, len);
2704 buffer[0] = (track->type << 7) | (track->pre_emphasis << 6);
Josh Coalson463a22c2004-07-11 06:41:00 +00002705 if(write_cb(buffer, 1, len, handle) != len)
2706 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002707
2708 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2709 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
2710 pack_uint32_(track->num_indices, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002711 if(write_cb(buffer, 1, len, handle) != len)
2712 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002713
2714 for(j = 0; j < track->num_indices; j++) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11002715 FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + j;
Josh Coalsone4869382002-11-15 05:41:48 +00002716
2717 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2718 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11002719 pack_uint64_(indx->offset, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002720 if(write_cb(buffer, 1, len, handle) != len)
2721 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002722
2723 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2724 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11002725 pack_uint32_(indx->number, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002726 if(write_cb(buffer, 1, len, handle) != len)
2727 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002728
2729 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2730 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
2731 memset(buffer, 0, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002732 if(write_cb(buffer, 1, len, handle) != len)
2733 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002734 }
2735 }
2736
Josh Coalson463a22c2004-07-11 06:41:00 +00002737 return true;
Josh Coalsone4869382002-11-15 05:41:48 +00002738}
2739
Josh Coalsone343ab22006-09-23 19:21:19 +00002740FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block)
2741{
2742 unsigned len;
2743 size_t slen;
2744 FLAC__byte buffer[4]; /* magic number is asserted below */
2745
2746 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8);
2747 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8);
2748 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8);
2749 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8);
2750 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8);
2751 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8);
Josh Coalson74ed2942006-09-23 23:15:05 +00002752 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8);
Josh Coalsone343ab22006-09-23 19:21:19 +00002753 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8);
2754 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2755 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8);
2756 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8);
2757 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2758 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2759 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
Josh Coalson74ed2942006-09-23 23:15:05 +00002760 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
Josh Coalsone343ab22006-09-23 19:21:19 +00002761 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8);
2762
2763 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8;
2764 pack_uint32_(block->type, buffer, len);
2765 if(write_cb(buffer, 1, len, handle) != len)
2766 return false;
2767
2768 len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8;
2769 slen = strlen(block->mime_type);
2770 pack_uint32_(slen, buffer, len);
2771 if(write_cb(buffer, 1, len, handle) != len)
2772 return false;
2773 if(write_cb(block->mime_type, 1, slen, handle) != slen)
2774 return false;
2775
2776 len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8;
2777 slen = strlen((const char *)block->description);
2778 pack_uint32_(slen, buffer, len);
2779 if(write_cb(buffer, 1, len, handle) != len)
2780 return false;
2781 if(write_cb(block->description, 1, slen, handle) != slen)
2782 return false;
2783
2784 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8;
2785 pack_uint32_(block->width, buffer, len);
2786 if(write_cb(buffer, 1, len, handle) != len)
2787 return false;
2788
2789 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8;
2790 pack_uint32_(block->height, buffer, len);
2791 if(write_cb(buffer, 1, len, handle) != len)
2792 return false;
2793
2794 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8;
2795 pack_uint32_(block->depth, buffer, len);
2796 if(write_cb(buffer, 1, len, handle) != len)
2797 return false;
2798
Josh Coalson74ed2942006-09-23 23:15:05 +00002799 len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8;
2800 pack_uint32_(block->colors, buffer, len);
2801 if(write_cb(buffer, 1, len, handle) != len)
2802 return false;
2803
Josh Coalsone343ab22006-09-23 19:21:19 +00002804 len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8;
2805 pack_uint32_(block->data_length, buffer, len);
2806 if(write_cb(buffer, 1, len, handle) != len)
2807 return false;
2808 if(write_cb(block->data, 1, block->data_length, handle) != block->data_length)
2809 return false;
2810
2811 return true;
2812}
2813
Josh Coalson463a22c2004-07-11 06:41:00 +00002814FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length)
Josh Coalsond0609472003-01-10 05:37:13 +00002815{
Josh Coalson463a22c2004-07-11 06:41:00 +00002816 if(write_cb(block->data, 1, block_length, handle) != block_length)
2817 return false;
Josh Coalsond0609472003-01-10 05:37:13 +00002818
Josh Coalson463a22c2004-07-11 06:41:00 +00002819 return true;
Josh Coalsond0609472003-01-10 05:37:13 +00002820}
2821
Josh Coalsoncc682512002-06-08 04:53:42 +00002822FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002823{
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002824 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002825 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2826 return false;
2827 }
2828
2829 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2830 return false;
2831
2832 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2833 return false;
2834
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002835 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002836 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2837 return false;
2838 }
2839
2840 return read_metadata_block_header_(iterator);
2841}
2842
Josh Coalsoncc682512002-06-08 04:53:42 +00002843FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last)
Josh Coalsoncae58732002-05-04 17:30:32 +00002844{
Josh Coalsoncc682512002-06-08 04:53:42 +00002845 FLAC__StreamMetadata *padding;
Josh Coalsoncae58732002-05-04 17:30:32 +00002846
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002847 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002848 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2849 return false;
2850 }
2851
2852 block->is_last = false;
2853
2854 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2855 return false;
2856
2857 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2858 return false;
2859
2860 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
2861 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2862
2863 padding->is_last = padding_is_last;
2864 padding->length = padding_length;
2865
2866 if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) {
2867 FLAC__metadata_object_delete(padding);
2868 return false;
2869 }
2870
2871 if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) {
2872 FLAC__metadata_object_delete(padding);
2873 return false;
2874 }
2875
2876 FLAC__metadata_object_delete(padding);
2877
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002878 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002879 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2880 return false;
2881 }
2882
2883 return read_metadata_block_header_(iterator);
2884}
2885
Josh Coalsoncc682512002-06-08 04:53:42 +00002886FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append)
Josh Coalsoncae58732002-05-04 17:30:32 +00002887{
Erik de Castro Lopo5de949f2012-04-05 21:24:06 +10002888 FILE *tempfile = NULL;
2889 char *tempfilename = NULL;
Josh Coalson6b02a752002-05-10 06:42:02 +00002890 int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11002891 FLAC__off_t fixup_is_last_flag_offset = -1;
Josh Coalsoncae58732002-05-04 17:30:32 +00002892
Josh Coalson51ee4652002-05-09 05:46:23 +00002893 FLAC__ASSERT(0 != block || append == false);
2894
Josh Coalson6b02a752002-05-10 06:42:02 +00002895 if(iterator->is_last) {
2896 if(append) {
2897 fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */
2898 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
2899 }
2900 else if(0 == block) {
2901 simple_iterator_push_(iterator);
2902 if(!FLAC__metadata_simple_iterator_prev(iterator)) {
2903 (void)simple_iterator_pop_(iterator);
2904 return false;
2905 }
2906 fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */
2907 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
2908 if(!simple_iterator_pop_(iterator))
2909 return false;
2910 }
2911 }
2912
Josh Coalsoncae58732002-05-04 17:30:32 +00002913 if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append))
2914 return false;
2915
2916 if(0 != block) {
2917 if(!write_metadata_block_header_(tempfile, &iterator->status, block)) {
2918 cleanup_tempfile_(&tempfile, &tempfilename);
2919 return false;
2920 }
2921
2922 if(!write_metadata_block_data_(tempfile, &iterator->status, block)) {
2923 cleanup_tempfile_(&tempfile, &tempfilename);
2924 return false;
2925 }
2926 }
2927
Josh Coalson6b02a752002-05-10 06:42:02 +00002928 if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0))
Josh Coalsoncae58732002-05-04 17:30:32 +00002929 return false;
2930
Josh Coalson51ee4652002-05-09 05:46:23 +00002931 if(append)
2932 return FLAC__metadata_simple_iterator_next(iterator);
2933
Josh Coalsoncae58732002-05-04 17:30:32 +00002934 return true;
2935}
2936
Josh Coalsoncc682512002-06-08 04:53:42 +00002937void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00002938{
2939 FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH);
2940 iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth];
2941 iterator->depth++;
2942}
2943
Josh Coalsoncc682512002-06-08 04:53:42 +00002944FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00002945{
2946 FLAC__ASSERT(iterator->depth > 0);
2947 iterator->depth--;
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002948 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002949 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2950 return false;
2951 }
2952
2953 return read_metadata_block_header_(iterator);
2954}
2955
Josh Coalsonea25cb32004-07-10 00:39:01 +00002956/* return meanings:
2957 * 0: ok
2958 * 1: read error
2959 * 2: seek error
2960 * 3: not a FLAC file
2961 */
2962unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb)
Josh Coalsoncae58732002-05-04 17:30:32 +00002963{
Josh Coalsona7b673c2002-05-25 02:13:38 +00002964 FLAC__byte buffer[4];
2965 size_t n;
2966 unsigned i;
Josh Coalsoncae58732002-05-04 17:30:32 +00002967
Josh Coalsone343ab22006-09-23 19:21:19 +00002968 FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer));
Josh Coalsoncae58732002-05-04 17:30:32 +00002969
Josh Coalsona7b673c2002-05-25 02:13:38 +00002970 /* skip any id3v2 tag */
2971 errno = 0;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002972 n = read_cb(buffer, 1, 4, handle);
Josh Coalsona7b673c2002-05-25 02:13:38 +00002973 if(errno)
Josh Coalsoncae58732002-05-04 17:30:32 +00002974 return 1;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002975 else if(n != 4)
2976 return 3;
Josh Coalsona7b673c2002-05-25 02:13:38 +00002977 else if(0 == memcmp(buffer, "ID3", 3)) {
2978 unsigned tag_length = 0;
2979
2980 /* skip to the tag length */
Josh Coalsonea25cb32004-07-10 00:39:01 +00002981 if(seek_cb(handle, 2, SEEK_CUR) < 0)
2982 return 2;
Josh Coalsona7b673c2002-05-25 02:13:38 +00002983
2984 /* read the length */
2985 for(i = 0; i < 4; i++) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00002986 if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80)
Josh Coalsona7b673c2002-05-25 02:13:38 +00002987 return 1;
2988 tag_length <<= 7;
2989 tag_length |= (buffer[0] & 0x7f);
2990 }
2991
2992 /* skip the rest of the tag */
Josh Coalsonea25cb32004-07-10 00:39:01 +00002993 if(seek_cb(handle, tag_length, SEEK_CUR) < 0)
2994 return 2;
Josh Coalsona7b673c2002-05-25 02:13:38 +00002995
2996 /* read the stream sync code */
2997 errno = 0;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002998 n = read_cb(buffer, 1, 4, handle);
Josh Coalsona7b673c2002-05-25 02:13:38 +00002999 if(errno)
3000 return 1;
Josh Coalsonea25cb32004-07-10 00:39:01 +00003001 else if(n != 4)
3002 return 3;
Josh Coalsoncae58732002-05-04 17:30:32 +00003003 }
Josh Coalsona7b673c2002-05-25 02:13:38 +00003004
3005 /* check for the fLaC signature */
3006 if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH))
3007 return 0;
3008 else
Josh Coalsonea25cb32004-07-10 00:39:01 +00003009 return 3;
3010}
3011
3012unsigned seek_to_first_metadata_block_(FILE *f)
3013{
3014 return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_);
Josh Coalsoncae58732002-05-04 17:30:32 +00003015}
3016
Josh Coalsoncc682512002-06-08 04:53:42 +00003017FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append)
Josh Coalsoncae58732002-05-04 17:30:32 +00003018{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003019 const FLAC__off_t offset_end = append? iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length : iterator->offset[iterator->depth];
Josh Coalsoncae58732002-05-04 17:30:32 +00003020
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003021 if(0 != fseeko(iterator->file, 0, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003022 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3023 return false;
3024 }
3025 if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) {
3026 cleanup_tempfile_(tempfile, tempfilename);
3027 return false;
3028 }
3029 if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) {
3030 cleanup_tempfile_(tempfile, tempfilename);
3031 return false;
3032 }
3033
3034 return true;
3035}
3036
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003037FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup)
Josh Coalsoncae58732002-05-04 17:30:32 +00003038{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003039 FLAC__off_t save_offset = iterator->offset[iterator->depth];
Josh Coalsoncae58732002-05-04 17:30:32 +00003040 FLAC__ASSERT(0 != *tempfile);
3041
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003042 if(0 != fseeko(iterator->file, save_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003043 cleanup_tempfile_(tempfile, tempfilename);
3044 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3045 return false;
3046 }
3047 if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) {
3048 cleanup_tempfile_(tempfile, tempfilename);
3049 return false;
3050 }
3051
Josh Coalson6b02a752002-05-10 06:42:02 +00003052 if(fixup_is_last_code != 0) {
3053 /*
3054 * if code == 1, it means a block was appended to the end so
3055 * we have to clear the is_last flag of the previous block
3056 * if code == -1, it means the last block was deleted so
3057 * we have to set the is_last flag of the previous block
3058 */
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00003059 /* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */
Josh Coalsoncae58732002-05-04 17:30:32 +00003060 FLAC__byte x;
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003061 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003062 cleanup_tempfile_(tempfile, tempfilename);
3063 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3064 return false;
3065 }
3066 if(fread(&x, 1, 1, *tempfile) != 1) {
3067 cleanup_tempfile_(tempfile, tempfilename);
3068 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3069 return false;
3070 }
Josh Coalson6b02a752002-05-10 06:42:02 +00003071 if(fixup_is_last_code > 0) {
3072 FLAC__ASSERT(x & 0x80);
3073 x &= 0x7f;
3074 }
3075 else {
3076 FLAC__ASSERT(!(x & 0x80));
3077 x |= 0x80;
3078 }
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003079 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003080 cleanup_tempfile_(tempfile, tempfilename);
3081 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3082 return false;
3083 }
Josh Coalsonf91251d2002-12-28 07:04:49 +00003084 if(local__fwrite(&x, 1, 1, *tempfile) != 1) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003085 cleanup_tempfile_(tempfile, tempfilename);
3086 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3087 return false;
3088 }
3089 }
3090
3091 (void)fclose(iterator->file);
3092
3093 if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status))
3094 return false;
3095
3096 if(iterator->has_stats)
3097 set_file_stats_(iterator->filename, &iterator->stats);
3098
Josh Coalson3ac66932002-08-30 05:41:31 +00003099 if(!simple_iterator_prime_input_(iterator, !iterator->is_writable))
Josh Coalsoncae58732002-05-04 17:30:32 +00003100 return false;
3101 if(backup) {
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003102 while(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length < save_offset)
Josh Coalsoncae58732002-05-04 17:30:32 +00003103 if(!FLAC__metadata_simple_iterator_next(iterator))
3104 return false;
3105 return true;
3106 }
3107 else {
3108 /* move the iterator to it's original block faster by faking a push, then doing a pop_ */
3109 FLAC__ASSERT(iterator->depth == 0);
3110 iterator->offset[0] = save_offset;
3111 iterator->depth++;
3112 return simple_iterator_pop_(iterator);
3113 }
3114}
3115
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003116FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003117{
3118 FLAC__byte buffer[8192];
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003119 size_t n;
Josh Coalsoncae58732002-05-04 17:30:32 +00003120
Josh Coalsonacd4a432006-11-11 22:43:25 +00003121 FLAC__ASSERT(bytes >= 0);
Josh Coalsoncae58732002-05-04 17:30:32 +00003122 while(bytes > 0) {
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003123 n = flac_min(sizeof(buffer), (size_t)bytes);
Josh Coalsoncae58732002-05-04 17:30:32 +00003124 if(fread(buffer, 1, n, file) != n) {
3125 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3126 return false;
3127 }
Josh Coalsonf91251d2002-12-28 07:04:49 +00003128 if(local__fwrite(buffer, 1, n, tempfile) != n) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003129 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3130 return false;
3131 }
3132 bytes -= n;
3133 }
3134
3135 return true;
3136}
3137
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003138FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
Josh Coalson463a22c2004-07-11 06:41:00 +00003139{
3140 FLAC__byte buffer[8192];
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003141 size_t n;
Josh Coalson463a22c2004-07-11 06:41:00 +00003142
Josh Coalsonacd4a432006-11-11 22:43:25 +00003143 FLAC__ASSERT(bytes >= 0);
Josh Coalson463a22c2004-07-11 06:41:00 +00003144 while(bytes > 0) {
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003145 n = flac_min(sizeof(buffer), (size_t)bytes);
Josh Coalson463a22c2004-07-11 06:41:00 +00003146 if(read_cb(buffer, 1, n, handle) != n) {
3147 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3148 return false;
3149 }
3150 if(temp_write_cb(buffer, 1, n, temp_handle) != n) {
3151 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3152 return false;
3153 }
3154 bytes -= n;
3155 }
3156
3157 return true;
3158}
3159
Josh Coalsoncc682512002-06-08 04:53:42 +00003160FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003161{
3162 FLAC__byte buffer[8192];
3163 size_t n;
3164
3165 while(!feof(file)) {
3166 n = fread(buffer, 1, sizeof(buffer), file);
3167 if(n == 0 && !feof(file)) {
3168 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3169 return false;
3170 }
Josh Coalsonf91251d2002-12-28 07:04:49 +00003171 if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003172 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3173 return false;
3174 }
3175 }
3176
3177 return true;
3178}
3179
Josh Coalson463a22c2004-07-11 06:41:00 +00003180FLAC__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)
3181{
3182 FLAC__byte buffer[8192];
3183 size_t n;
3184
3185 while(!eof_cb(handle)) {
3186 n = read_cb(buffer, 1, sizeof(buffer), handle);
3187 if(n == 0 && !eof_cb(handle)) {
3188 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3189 return false;
3190 }
3191 if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) {
3192 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3193 return false;
3194 }
3195 }
3196
3197 return true;
3198}
3199
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003200static int
3201local_snprintf(char *str, size_t size, const char *fmt, ...)
3202{
3203 va_list va;
3204 int rc ;
3205
3206 va_start (va, fmt);
3207
3208#ifdef _MSC_VER
3209 rc = vsnprintf_s (str, size, _TRUNCATE, fmt, va);
3210 rc = (rc > 0) ? rc : (size == 0 ? 1024 : size * 2);
3211#else
3212 rc = vsnprintf (str, size, fmt, va);
3213#endif
3214 va_end (va);
3215
3216 return rc;
3217}
3218
Josh Coalsoncc682512002-06-08 04:53:42 +00003219FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003220{
3221 static const char *tempfile_suffix = ".metadata_edit";
3222 if(0 == tempfile_path_prefix) {
Erik de Castro Lopo2d6354f2013-03-19 20:23:44 +11003223 size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1;
3224 if(0 == (*tempfilename = safe_malloc_(dest_len))) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003225 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
3226 return false;
3227 }
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003228 local_snprintf(*tempfilename, dest_len, "%s%s", filename, tempfile_suffix);
Josh Coalsoncae58732002-05-04 17:30:32 +00003229 }
3230 else {
3231 const char *p = strrchr(filename, '/');
Erik de Castro Lopo2d6354f2013-03-19 20:23:44 +11003232 size_t dest_len;
Josh Coalsoncae58732002-05-04 17:30:32 +00003233 if(0 == p)
3234 p = filename;
3235 else
3236 p++;
3237
Erik de Castro Lopo2d6354f2013-03-19 20:23:44 +11003238 dest_len = strlen(tempfile_path_prefix) + strlen(p) + strlen(tempfile_suffix) + 2;
3239
3240 if(0 == (*tempfilename = safe_malloc_(dest_len))) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003241 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
3242 return false;
3243 }
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003244 local_snprintf(*tempfilename, dest_len, "%s/%s%s", tempfile_path_prefix, p, tempfile_suffix);
Josh Coalsoncae58732002-05-04 17:30:32 +00003245 }
3246
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003247 if(0 == (*tempfile = flac_fopen(*tempfilename, "w+b"))) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003248 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
3249 return false;
3250 }
3251
3252 return true;
3253}
3254
Josh Coalsoncc682512002-06-08 04:53:42 +00003255FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003256{
3257 FLAC__ASSERT(0 != filename);
3258 FLAC__ASSERT(0 != tempfile);
3259 FLAC__ASSERT(0 != *tempfile);
3260 FLAC__ASSERT(0 != tempfilename);
3261 FLAC__ASSERT(0 != *tempfilename);
3262 FLAC__ASSERT(0 != status);
3263
3264 (void)fclose(*tempfile);
3265 *tempfile = 0;
Josh Coalsond57c8d32002-06-11 06:15:28 +00003266
Josh Coalson7581d122006-11-20 07:19:15 +00003267#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003268 /* on some flavors of windows, flac_rename() will fail if the destination already exists */
3269 if(flac_unlink(filename) < 0) {
Josh Coalsond57c8d32002-06-11 06:15:28 +00003270 cleanup_tempfile_(tempfile, tempfilename);
3271 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR;
3272 return false;
3273 }
3274#endif
3275
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003276 /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just flac_rename(): */
3277 if(0 != flac_rename(*tempfilename, filename)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003278 cleanup_tempfile_(tempfile, tempfilename);
3279 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR;
3280 return false;
3281 }
3282
3283 cleanup_tempfile_(tempfile, tempfilename);
3284
3285 return true;
3286}
3287
3288void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
3289{
3290 if(0 != *tempfile) {
3291 (void)fclose(*tempfile);
3292 *tempfile = 0;
3293 }
3294
3295 if(0 != *tempfilename) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003296 (void)flac_unlink(*tempfilename);
Josh Coalsoncae58732002-05-04 17:30:32 +00003297 free(*tempfilename);
3298 *tempfilename = 0;
3299 }
3300}
3301
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +11003302FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats)
Josh Coalsoncae58732002-05-04 17:30:32 +00003303{
3304 FLAC__ASSERT(0 != filename);
3305 FLAC__ASSERT(0 != stats);
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003306 return (0 == flac_stat(filename, stats));
Josh Coalsoncae58732002-05-04 17:30:32 +00003307}
3308
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +11003309void set_file_stats_(const char *filename, struct flac_stat_s *stats)
Josh Coalsoncae58732002-05-04 17:30:32 +00003310{
3311 struct utimbuf srctime;
3312
3313 FLAC__ASSERT(0 != filename);
3314 FLAC__ASSERT(0 != stats);
3315
3316 srctime.actime = stats->st_atime;
3317 srctime.modtime = stats->st_mtime;
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003318 (void)flac_chmod(filename, stats->st_mode);
3319 (void)flac_utime(filename, &srctime);
Erik de Castro Lopodac99fa2012-02-08 21:07:08 +11003320#if !defined _MSC_VER && !defined __BORLANDC__ && !defined __MINGW32__
Erik de Castro Lopo91790ef2013-03-05 22:19:27 +11003321 FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1));
3322 FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid));
Josh Coalsoncae58732002-05-04 17:30:32 +00003323#endif
3324}
3325
Josh Coalsonea25cb32004-07-10 00:39:01 +00003326int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
3327{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003328 return fseeko((FILE*)handle, (FLAC__off_t)offset, whence);
Josh Coalsonea25cb32004-07-10 00:39:01 +00003329}
3330
3331FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle)
3332{
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003333 return ftello((FILE*)handle);
Josh Coalsonea25cb32004-07-10 00:39:01 +00003334}
3335
Josh Coalsoncc682512002-06-08 04:53:42 +00003336FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003337{
3338 switch(status) {
3339 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK:
3340 return FLAC__METADATA_CHAIN_STATUS_OK;
3341 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
3342 return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT;
3343 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
3344 return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
3345 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
3346 return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
3347 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE:
3348 return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE;
Josh Coalsona7b673c2002-05-25 02:13:38 +00003349 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA:
3350 return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
Josh Coalsoncae58732002-05-04 17:30:32 +00003351 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR:
3352 return FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
3353 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR:
3354 return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
3355 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR:
3356 return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
3357 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR:
3358 return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR;
3359 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR:
3360 return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR;
3361 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR:
3362 return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
3363 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR:
3364 default:
3365 return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
3366 }
3367}