blob: 0e2edd5b5e31060e45daf1b17e1ea9eae24b58be [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
Erik de Castro Lopo14373912014-11-24 22:07:15 +11003 * Copyright (C) 2011-2014 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);
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +100080static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry, unsigned max_length);
81static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, unsigned block_length);
Josh Coalsonea25cb32004-07-10 00:39:01 +000082static 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
Erik de Castro Lopo7efe8f32014-11-14 23:35:37 -0800766 if(!iterator->is_writable) {
767 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
Josh Coalsoncae58732002-05-04 17:30:32 +0000768 return false;
Erik de Castro Lopo7efe8f32014-11-14 23:35:37 -0800769 }
Josh Coalsoncae58732002-05-04 17:30:32 +0000770
771 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
772 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
773 return false;
774 }
775
Josh Coalson6b02a752002-05-10 06:42:02 +0000776 block->is_last = iterator->is_last;
Josh Coalson51ee4652002-05-09 05:46:23 +0000777
Josh Coalsoncae58732002-05-04 17:30:32 +0000778 if(use_padding) {
779 /* first see if we can even use padding */
780 if(iterator->is_last) {
781 use_padding = false;
782 }
783 else {
784 simple_iterator_push_(iterator);
785 if(!FLAC__metadata_simple_iterator_next(iterator)) {
786 (void)simple_iterator_pop_(iterator);
787 return false;
788 }
789 if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
790 use_padding = false;
791 }
792 else {
Josh Coalsoncae58732002-05-04 17:30:32 +0000793 if(iterator->length == block->length) {
794 padding_leftover = 0;
795 block->is_last = iterator->is_last;
796 }
Josh Coalsoneb643a22002-05-20 05:58:50 +0000797 else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length)
Josh Coalsoncae58732002-05-04 17:30:32 +0000798 use_padding = false;
799 else {
Josh Coalson6b02a752002-05-10 06:42:02 +0000800 padding_leftover = iterator->length - block->length;
Josh Coalsoncae58732002-05-04 17:30:32 +0000801 padding_is_last = iterator->is_last;
802 block->is_last = false;
803 }
804 }
805 if(!simple_iterator_pop_(iterator))
806 return false;
807 }
808 }
809 if(use_padding) {
810 /* move to the next block, which is suitable padding */
811 if(!FLAC__metadata_simple_iterator_next(iterator))
812 return false;
Josh Coalson51ee4652002-05-09 05:46:23 +0000813 if(padding_leftover == 0) {
814 ret = write_metadata_block_stationary_(iterator, block);
Josh Coalson51ee4652002-05-09 05:46:23 +0000815 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100816 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000817 return ret;
818 }
819 else {
Josh Coalsoneb643a22002-05-20 05:58:50 +0000820 FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
821 ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
Josh Coalson51ee4652002-05-09 05:46:23 +0000822 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100823 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000824 return ret;
825 }
Josh Coalsoncae58732002-05-04 17:30:32 +0000826 }
827 else {
Josh Coalson51ee4652002-05-09 05:46:23 +0000828 ret = rewrite_whole_file_(iterator, block, /*append=*/true);
Josh Coalson51ee4652002-05-09 05:46:23 +0000829 FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100830 FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
Josh Coalson51ee4652002-05-09 05:46:23 +0000831 return ret;
Josh Coalsoncae58732002-05-04 17:30:32 +0000832 }
833}
834
Josh Coalson6afed9f2002-10-16 22:29:47 +0000835FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)
Josh Coalsoncae58732002-05-04 17:30:32 +0000836{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100837 FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
Josh Coalson51ee4652002-05-09 05:46:23 +0000838 FLAC__bool ret;
839
Erik de Castro Lopo46846082016-01-16 07:57:26 +1100840 if(!iterator->is_writable) {
841 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
842 return false;
843 }
844
Josh Coalsoncae58732002-05-04 17:30:32 +0000845 if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
846 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
847 return false;
848 }
849
850 if(use_padding) {
Josh Coalsoncc682512002-06-08 04:53:42 +0000851 FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
Josh Coalsoncae58732002-05-04 17:30:32 +0000852 if(0 == padding) {
853 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
854 return false;
855 }
856 padding->length = iterator->length;
857 if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) {
858 FLAC__metadata_object_delete(padding);
859 return false;
860 }
861 FLAC__metadata_object_delete(padding);
Josh Coalson6b02a752002-05-10 06:42:02 +0000862 if(!FLAC__metadata_simple_iterator_prev(iterator))
863 return false;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100864 FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
865 FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
Josh Coalsoncae58732002-05-04 17:30:32 +0000866 return true;
867 }
868 else {
Josh Coalson51ee4652002-05-09 05:46:23 +0000869 ret = rewrite_whole_file_(iterator, 0, /*append=*/false);
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100870 FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
871 FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
Josh Coalson51ee4652002-05-09 05:46:23 +0000872 return ret;
Josh Coalsoncae58732002-05-04 17:30:32 +0000873 }
874}
875
876
877
878/****************************************************************************
879 *
880 * Level 2 implementation
881 *
882 ***************************************************************************/
883
884
Josh Coalsoncc682512002-06-08 04:53:42 +0000885typedef struct FLAC__Metadata_Node {
886 FLAC__StreamMetadata *data;
887 struct FLAC__Metadata_Node *prev, *next;
888} FLAC__Metadata_Node;
Josh Coalsoncae58732002-05-04 17:30:32 +0000889
Josh Coalsoncc682512002-06-08 04:53:42 +0000890struct FLAC__Metadata_Chain {
Josh Coalson91748be2004-07-10 22:28:03 +0000891 char *filename; /* will be NULL if using callbacks */
Josh Coalson7d273b42006-11-15 06:12:30 +0000892 FLAC__bool is_ogg;
Josh Coalsoncc682512002-06-08 04:53:42 +0000893 FLAC__Metadata_Node *head;
894 FLAC__Metadata_Node *tail;
Josh Coalsoncae58732002-05-04 17:30:32 +0000895 unsigned nodes;
Josh Coalsoncc682512002-06-08 04:53:42 +0000896 FLAC__Metadata_ChainStatus status;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100897 FLAC__off_t first_offset, last_offset;
Josh Coalsoncae58732002-05-04 17:30:32 +0000898 /*
899 * This is the length of the chain initially read from the FLAC file.
Josh Coalson3ecb1172002-06-15 05:09:47 +0000900 * it is used to compare against the current length to decide whether
Josh Coalsoncae58732002-05-04 17:30:32 +0000901 * or not the whole file has to be rewritten.
902 */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +1100903 FLAC__off_t initial_length;
Josh Coalson7d273b42006-11-15 06:12:30 +0000904 /* @@@ hacky, these are currently only needed by ogg reader */
905 FLAC__IOHandle handle;
906 FLAC__IOCallback_Read read_cb;
Josh Coalsoncae58732002-05-04 17:30:32 +0000907};
908
Josh Coalsoncc682512002-06-08 04:53:42 +0000909struct FLAC__Metadata_Iterator {
910 FLAC__Metadata_Chain *chain;
911 FLAC__Metadata_Node *current;
Josh Coalsoncae58732002-05-04 17:30:32 +0000912};
913
Josh Coalson6afed9f2002-10-16 22:29:47 +0000914FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = {
Josh Coalsoncae58732002-05-04 17:30:32 +0000915 "FLAC__METADATA_CHAIN_STATUS_OK",
916 "FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT",
917 "FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE",
918 "FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE",
919 "FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE",
Josh Coalsona7b673c2002-05-25 02:13:38 +0000920 "FLAC__METADATA_CHAIN_STATUS_BAD_METADATA",
Josh Coalsoncae58732002-05-04 17:30:32 +0000921 "FLAC__METADATA_CHAIN_STATUS_READ_ERROR",
922 "FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR",
923 "FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR",
924 "FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR",
925 "FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR",
926 "FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR",
Josh Coalson91748be2004-07-10 22:28:03 +0000927 "FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR",
Josh Coalson463a22c2004-07-11 06:41:00 +0000928 "FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS",
929 "FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH",
930 "FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL"
Josh Coalsoncae58732002-05-04 17:30:32 +0000931};
932
933
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000934static FLAC__Metadata_Node *node_new_(void)
Josh Coalsoncae58732002-05-04 17:30:32 +0000935{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000936 return calloc(1, sizeof(FLAC__Metadata_Node));
Josh Coalsoncae58732002-05-04 17:30:32 +0000937}
938
Josh Coalsoncc682512002-06-08 04:53:42 +0000939static void node_delete_(FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +0000940{
941 FLAC__ASSERT(0 != node);
942 if(0 != node->data)
943 FLAC__metadata_object_delete(node->data);
944 free(node);
945}
946
Josh Coalson91748be2004-07-10 22:28:03 +0000947static void chain_init_(FLAC__Metadata_Chain *chain)
948{
949 FLAC__ASSERT(0 != chain);
950
951 chain->filename = 0;
Josh Coalson7d273b42006-11-15 06:12:30 +0000952 chain->is_ogg = false;
Josh Coalson91748be2004-07-10 22:28:03 +0000953 chain->head = chain->tail = 0;
954 chain->nodes = 0;
955 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
956 chain->initial_length = 0;
Josh Coalson7d273b42006-11-15 06:12:30 +0000957 chain->read_cb = 0;
Josh Coalson91748be2004-07-10 22:28:03 +0000958}
959
960static void chain_clear_(FLAC__Metadata_Chain *chain)
961{
962 FLAC__Metadata_Node *node, *next;
963
964 FLAC__ASSERT(0 != chain);
965
966 for(node = chain->head; node; ) {
967 next = node->next;
968 node_delete_(node);
969 node = next;
970 }
971
972 if(0 != chain->filename)
973 free(chain->filename);
974
975 chain_init_(chain);
976}
977
Josh Coalsoncc682512002-06-08 04:53:42 +0000978static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +0000979{
980 FLAC__ASSERT(0 != chain);
981 FLAC__ASSERT(0 != node);
Josh Coalson6b02a752002-05-10 06:42:02 +0000982 FLAC__ASSERT(0 != node->data);
Josh Coalsoncae58732002-05-04 17:30:32 +0000983
Josh Coalson6b02a752002-05-10 06:42:02 +0000984 node->next = node->prev = 0;
Josh Coalsoncae58732002-05-04 17:30:32 +0000985 node->data->is_last = true;
986 if(0 != chain->tail)
987 chain->tail->data->is_last = false;
988
989 if(0 == chain->head)
990 chain->head = node;
991 else {
Josh Coalson6b02a752002-05-10 06:42:02 +0000992 FLAC__ASSERT(0 != chain->tail);
Josh Coalsoncae58732002-05-04 17:30:32 +0000993 chain->tail->next = node;
994 node->prev = chain->tail;
995 }
996 chain->tail = node;
997 chain->nodes++;
Josh Coalsoncae58732002-05-04 17:30:32 +0000998}
999
Josh Coalsoncc682512002-06-08 04:53:42 +00001000static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +00001001{
1002 FLAC__ASSERT(0 != chain);
1003 FLAC__ASSERT(0 != node);
1004
1005 if(node == chain->head)
1006 chain->head = node->next;
1007 else
1008 node->prev->next = node->next;
1009
1010 if(node == chain->tail)
1011 chain->tail = node->prev;
1012 else
1013 node->next->prev = node->prev;
1014
Josh Coalson6b02a752002-05-10 06:42:02 +00001015 if(0 != chain->tail)
1016 chain->tail->data->is_last = true;
1017
Josh Coalsoncae58732002-05-04 17:30:32 +00001018 chain->nodes--;
Josh Coalsoncae58732002-05-04 17:30:32 +00001019}
1020
Josh Coalsoncc682512002-06-08 04:53:42 +00001021static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +00001022{
1023 chain_remove_node_(chain, node);
1024 node_delete_(node);
1025}
1026
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001027static FLAC__off_t chain_calculate_length_(FLAC__Metadata_Chain *chain)
Josh Coalson3ecb1172002-06-15 05:09:47 +00001028{
1029 const FLAC__Metadata_Node *node;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001030 FLAC__off_t length = 0;
Josh Coalson3ecb1172002-06-15 05:09:47 +00001031 for(node = chain->head; node; node = node->next)
1032 length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1033 return length;
1034}
1035
Josh Coalsoncc682512002-06-08 04:53:42 +00001036static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +00001037{
1038 FLAC__ASSERT(0 != node);
Josh Coalson6b02a752002-05-10 06:42:02 +00001039 FLAC__ASSERT(0 != node->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001040 FLAC__ASSERT(0 != iterator);
1041 FLAC__ASSERT(0 != iterator->current);
1042 FLAC__ASSERT(0 != iterator->chain);
1043 FLAC__ASSERT(0 != iterator->chain->head);
1044 FLAC__ASSERT(0 != iterator->chain->tail);
1045
Josh Coalson6b02a752002-05-10 06:42:02 +00001046 node->data->is_last = false;
Josh Coalson2745fe72002-05-11 05:31:56 +00001047
Josh Coalsoncae58732002-05-04 17:30:32 +00001048 node->prev = iterator->current->prev;
Josh Coalson2745fe72002-05-11 05:31:56 +00001049 node->next = iterator->current;
1050
Josh Coalsoncae58732002-05-04 17:30:32 +00001051 if(0 == node->prev)
1052 iterator->chain->head = node;
1053 else
1054 node->prev->next = node;
1055
Josh Coalsoncae58732002-05-04 17:30:32 +00001056 iterator->current->prev = node;
1057
1058 iterator->chain->nodes++;
Josh Coalsoncae58732002-05-04 17:30:32 +00001059}
1060
Josh Coalsoncc682512002-06-08 04:53:42 +00001061static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
Josh Coalsoncae58732002-05-04 17:30:32 +00001062{
1063 FLAC__ASSERT(0 != node);
Josh Coalson6b02a752002-05-10 06:42:02 +00001064 FLAC__ASSERT(0 != node->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001065 FLAC__ASSERT(0 != iterator);
1066 FLAC__ASSERT(0 != iterator->current);
1067 FLAC__ASSERT(0 != iterator->chain);
1068 FLAC__ASSERT(0 != iterator->chain->head);
1069 FLAC__ASSERT(0 != iterator->chain->tail);
1070
1071 iterator->current->data->is_last = false;
1072
1073 node->prev = iterator->current;
Josh Coalsoncae58732002-05-04 17:30:32 +00001074 node->next = iterator->current->next;
Josh Coalson2745fe72002-05-11 05:31:56 +00001075
Josh Coalsoncae58732002-05-04 17:30:32 +00001076 if(0 == node->next)
1077 iterator->chain->tail = node;
1078 else
1079 node->next->prev = node;
1080
Josh Coalson2745fe72002-05-11 05:31:56 +00001081 node->prev->next = node;
1082
Josh Coalsoncae58732002-05-04 17:30:32 +00001083 iterator->chain->tail->data->is_last = true;
1084
1085 iterator->chain->nodes++;
Josh Coalsoncae58732002-05-04 17:30:32 +00001086}
1087
Josh Coalson2745fe72002-05-11 05:31:56 +00001088/* return true iff node and node->next are both padding */
Josh Coalsoncc682512002-06-08 04:53:42 +00001089static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
Josh Coalson2745fe72002-05-11 05:31:56 +00001090{
1091 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 +00001092 const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
Erik de Castro Lopo387992b2016-05-01 20:57:15 +10001093 node->data->length += growth; /* new block size can be greater than max metadata block size, but it'll be fixed later in chain_prepare_for_write_() */
Josh Coalson2745fe72002-05-11 05:31:56 +00001094
1095 chain_delete_node_(chain, node->next);
1096 return true;
1097 }
1098 else
1099 return false;
1100}
1101
Josh Coalson463a22c2004-07-11 06:41:00 +00001102/* Returns the new length of the chain, or 0 if there was an error. */
1103/* WATCHOUT: This can get called multiple times before a write, so
1104 * it should still work when this happens.
1105 */
1106/* WATCHOUT: Make sure to also update the logic in
1107 * FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes.
1108 */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001109static FLAC__off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
Josh Coalson463a22c2004-07-11 06:41:00 +00001110{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001111 FLAC__off_t current_length = chain_calculate_length_(chain);
Josh Coalson463a22c2004-07-11 06:41:00 +00001112
1113 if(use_padding) {
1114 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1115 if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001116 const FLAC__off_t delta = chain->initial_length - current_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001117 chain->tail->data->length += delta;
1118 current_length += delta;
1119 FLAC__ASSERT(current_length == chain->initial_length);
1120 }
1121 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001122 else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001123 FLAC__StreamMetadata *padding;
1124 FLAC__Metadata_Node *node;
1125 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) {
1126 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1127 return 0;
1128 }
1129 padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
1130 if(0 == (node = node_new_())) {
1131 FLAC__metadata_object_delete(padding);
1132 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1133 return 0;
1134 }
1135 node->data = padding;
1136 chain_append_node_(chain, node);
1137 current_length = chain_calculate_length_(chain);
1138 FLAC__ASSERT(current_length == chain->initial_length);
1139 }
1140 /* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
1141 else if(current_length > chain->initial_length) {
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001142 const FLAC__off_t delta = current_length - chain->initial_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001143 if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
1144 /* if the delta is exactly the size of the last padding block, remove the padding block */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001145 if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001146 chain_delete_node_(chain, chain->tail);
1147 current_length = chain_calculate_length_(chain);
1148 FLAC__ASSERT(current_length == chain->initial_length);
1149 }
1150 /* if there is at least 'delta' bytes of padding, trim the padding down */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001151 else if((FLAC__off_t)chain->tail->data->length >= delta) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001152 chain->tail->data->length -= delta;
1153 current_length -= delta;
1154 FLAC__ASSERT(current_length == chain->initial_length);
1155 }
1156 }
1157 }
1158 }
1159
Erik de Castro Lopo387992b2016-05-01 20:57:15 +10001160 /* check sizes of all metadata blocks; reduce padding size if necessary */
1161 {
1162 FLAC__Metadata_Node *node;
1163 for (node = chain->head; node; node = node->next) {
1164 if(node->data->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
1165 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1166 node->data->length = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
1167 current_length = chain_calculate_length_(chain);
1168 } else {
1169 chain->status = FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
1170 return 0;
1171 }
1172 }
1173 }
1174 }
1175
Josh Coalson463a22c2004-07-11 06:41:00 +00001176 return current_length;
1177}
1178
Josh Coalson0e46bf52004-07-15 00:04:46 +00001179static 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 +00001180{
1181 FLAC__Metadata_Node *node;
1182
1183 FLAC__ASSERT(0 != chain);
1184
1185 /* we assume we're already at the beginning of the file */
1186
1187 switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) {
1188 case 0:
1189 break;
1190 case 1:
1191 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1192 return false;
1193 case 2:
1194 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1195 return false;
1196 case 3:
1197 chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
1198 return false;
1199 default:
1200 FLAC__ASSERT(0);
1201 return false;
1202 }
1203
1204 {
1205 FLAC__int64 pos = tell_cb(handle);
1206 if(pos < 0) {
1207 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1208 return false;
1209 }
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001210 chain->first_offset = (FLAC__off_t)pos;
Josh Coalsonea25cb32004-07-10 00:39:01 +00001211 }
1212
1213 {
1214 FLAC__bool is_last;
1215 FLAC__MetadataType type;
1216 unsigned length;
1217
1218 do {
1219 node = node_new_();
1220 if(0 == node) {
1221 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1222 return false;
1223 }
1224
1225 if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) {
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001226 node_delete_(node);
Josh Coalsonea25cb32004-07-10 00:39:01 +00001227 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1228 return false;
1229 }
1230
1231 node->data = FLAC__metadata_object_new(type);
1232 if(0 == node->data) {
1233 node_delete_(node);
1234 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1235 return false;
1236 }
1237
1238 node->data->is_last = is_last;
1239 node->data->length = length;
1240
1241 chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data));
1242 if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
1243 node_delete_(node);
1244 return false;
1245 }
1246 chain_append_node_(chain, node);
1247 } while(!is_last);
1248 }
1249
1250 {
1251 FLAC__int64 pos = tell_cb(handle);
1252 if(pos < 0) {
1253 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
1254 return false;
1255 }
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001256 chain->last_offset = (FLAC__off_t)pos;
Josh Coalsonea25cb32004-07-10 00:39:01 +00001257 }
1258
Josh Coalsonea25cb32004-07-10 00:39:01 +00001259 chain->initial_length = chain_calculate_length_(chain);
1260
1261 return true;
1262}
1263
Josh Coalsonf9cf27f2007-07-25 03:11:00 +00001264static 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 +00001265{
1266 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
1267 (void)decoder;
1268 if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) {
1269 *bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle);
1270 if(*bytes == 0)
1271 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
1272 else
1273 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
1274 }
1275 else
1276 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
1277}
1278
1279static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
1280{
1281 (void)decoder, (void)frame, (void)buffer, (void)client_data;
1282 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
1283}
1284
1285static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
1286{
1287 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
Josh Coalsonb0627a42006-11-17 06:05:02 +00001288 FLAC__Metadata_Node *node;
Josh Coalson7d273b42006-11-15 06:12:30 +00001289
1290 (void)decoder;
1291
Josh Coalsonb0627a42006-11-17 06:05:02 +00001292 node = node_new_();
Josh Coalson7d273b42006-11-15 06:12:30 +00001293 if(0 == node) {
1294 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1295 return;
1296 }
1297
1298 node->data = FLAC__metadata_object_clone(metadata);
1299 if(0 == node->data) {
1300 node_delete_(node);
1301 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1302 return;
1303 }
1304
1305 chain_append_node_(chain, node);
1306}
1307
1308static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
1309{
1310 FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
1311 (void)decoder, (void)status;
1312 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1313}
1314
1315static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb)
1316{
1317 FLAC__StreamDecoder *decoder;
1318
1319 FLAC__ASSERT(0 != chain);
1320
1321 /* we assume we're already at the beginning of the file */
1322
1323 chain->handle = handle;
1324 chain->read_cb = read_cb;
1325 if(0 == (decoder = FLAC__stream_decoder_new())) {
1326 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1327 return false;
1328 }
1329 FLAC__stream_decoder_set_metadata_respond_all(decoder);
1330 if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
1331 FLAC__stream_decoder_delete(decoder);
1332 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1333 return false;
1334 }
1335
1336 chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
1337
1338 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
1339 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
1340 if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
1341 FLAC__stream_decoder_delete(decoder);
1342 return false;
1343 }
1344
1345 FLAC__stream_decoder_delete(decoder);
1346
1347 chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
1348
1349 chain->initial_length = chain_calculate_length_(chain);
1350
1351 return true;
1352}
1353
Josh Coalson0e46bf52004-07-15 00:04:46 +00001354static 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 +00001355{
1356 FLAC__Metadata_Node *node;
1357
1358 FLAC__ASSERT(0 != chain);
1359 FLAC__ASSERT(0 != chain->head);
1360
1361 if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001362 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1363 return false;
1364 }
1365
1366 for(node = chain->head; node; node = node->next) {
1367 if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001368 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1369 return false;
1370 }
1371 if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001372 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1373 return false;
1374 }
1375 }
1376
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001377 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
Josh Coalson463a22c2004-07-11 06:41:00 +00001378
Josh Coalson463a22c2004-07-11 06:41:00 +00001379 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1380 return true;
1381}
1382
1383static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain)
1384{
1385 FILE *file;
Josh Coalson0e46bf52004-07-15 00:04:46 +00001386 FLAC__bool ret;
Josh Coalson463a22c2004-07-11 06:41:00 +00001387
1388 FLAC__ASSERT(0 != chain->filename);
1389
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11001390 if(0 == (file = flac_fopen(chain->filename, "r+b"))) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001391 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1392 return false;
1393 }
1394
Josh Coalson0e46bf52004-07-15 00:04:46 +00001395 /* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */
1396 ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_);
1397
1398 fclose(file);
1399
1400 return ret;
Josh Coalson463a22c2004-07-11 06:41:00 +00001401}
1402
1403static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
1404{
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11001405 FILE *f, *tempfile = NULL;
Josh Coalson463a22c2004-07-11 06:41:00 +00001406 char *tempfilename;
1407 FLAC__Metadata_SimpleIteratorStatus status;
1408 const FLAC__Metadata_Node *node;
1409
1410 FLAC__ASSERT(0 != chain);
1411 FLAC__ASSERT(0 != chain->filename);
1412 FLAC__ASSERT(0 != chain->head);
1413
1414 /* copy the file prefix (data up to first metadata block */
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11001415 if(0 == (f = flac_fopen(chain->filename, "rb"))) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001416 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
1417 return false;
1418 }
1419 if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) {
1420 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001421 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001422 }
1423 if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) {
1424 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 /* write the metadata */
1429 for(node = chain->head; node; node = node->next) {
1430 if(!write_metadata_block_header_(tempfile, &status, node->data)) {
1431 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001432 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001433 }
1434 if(!write_metadata_block_data_(tempfile, &status, node->data)) {
1435 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001436 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001437 }
1438 }
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001439 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
Josh Coalson463a22c2004-07-11 06:41:00 +00001440
1441 /* copy the file postfix (everything after the metadata) */
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001442 if(0 != fseeko(f, chain->last_offset, SEEK_SET)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001443 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001444 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001445 }
1446 if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001447 chain->status = get_equivalent_status_(status);
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001448 goto err;
Josh Coalson463a22c2004-07-11 06:41:00 +00001449 }
1450
1451 /* move the tempfile on top of the original */
1452 (void)fclose(f);
1453 if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status))
1454 return false;
1455
1456 return true;
Felipe Contreras6c8d7402011-04-28 20:43:21 +03001457
1458err:
1459 (void)fclose(f);
1460 cleanup_tempfile_(&tempfile, &tempfilename);
1461 return false;
Josh Coalson463a22c2004-07-11 06:41:00 +00001462}
1463
Josh Coalson9aac6702004-09-08 00:50:21 +00001464/* assumes 'handle' is already at beginning of file */
Josh Coalson0e46bf52004-07-15 00:04:46 +00001465static 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 +00001466{
1467 FLAC__Metadata_SimpleIteratorStatus status;
1468 const FLAC__Metadata_Node *node;
1469
1470 FLAC__ASSERT(0 != chain);
1471 FLAC__ASSERT(0 == chain->filename);
1472 FLAC__ASSERT(0 != chain->head);
1473
1474 /* copy the file prefix (data up to first metadata block */
1475 if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001476 chain->status = get_equivalent_status_(status);
1477 return false;
1478 }
1479
1480 /* write the metadata */
1481 for(node = chain->head; node; node = node->next) {
1482 if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001483 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1484 return false;
1485 }
1486 if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001487 chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
1488 return false;
1489 }
1490 }
Josh Coalsonb1ec7962006-05-24 04:41:36 +00001491 /*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
Josh Coalson463a22c2004-07-11 06:41:00 +00001492
1493 /* copy the file postfix (everything after the metadata) */
1494 if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001495 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1496 return false;
1497 }
1498 if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001499 chain->status = get_equivalent_status_(status);
1500 return false;
1501 }
1502
Josh Coalson463a22c2004-07-11 06:41:00 +00001503 return true;
1504}
1505
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001506FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void)
Josh Coalsoncae58732002-05-04 17:30:32 +00001507{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001508 FLAC__Metadata_Chain *chain = calloc(1, sizeof(FLAC__Metadata_Chain));
Josh Coalsoncae58732002-05-04 17:30:32 +00001509
Josh Coalson91748be2004-07-10 22:28:03 +00001510 if(0 != chain)
1511 chain_init_(chain);
Josh Coalsoncae58732002-05-04 17:30:32 +00001512
1513 return chain;
1514}
1515
Josh Coalson6afed9f2002-10-16 22:29:47 +00001516FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001517{
Josh Coalsoncae58732002-05-04 17:30:32 +00001518 FLAC__ASSERT(0 != chain);
1519
Josh Coalson91748be2004-07-10 22:28:03 +00001520 chain_clear_(chain);
Josh Coalsoncae58732002-05-04 17:30:32 +00001521
1522 free(chain);
1523}
1524
Josh Coalson6afed9f2002-10-16 22:29:47 +00001525FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001526{
Josh Coalsoncc682512002-06-08 04:53:42 +00001527 FLAC__Metadata_ChainStatus status;
Josh Coalsoncae58732002-05-04 17:30:32 +00001528
1529 FLAC__ASSERT(0 != chain);
1530
1531 status = chain->status;
1532 chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
1533 return status;
1534}
1535
Josh Coalson7d273b42006-11-15 06:12:30 +00001536static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg)
Josh Coalsoncae58732002-05-04 17:30:32 +00001537{
Josh Coalsonea25cb32004-07-10 00:39:01 +00001538 FILE *file;
Josh Coalson0e46bf52004-07-15 00:04:46 +00001539 FLAC__bool ret;
Josh Coalsoncae58732002-05-04 17:30:32 +00001540
1541 FLAC__ASSERT(0 != chain);
1542 FLAC__ASSERT(0 != filename);
1543
Josh Coalson91748be2004-07-10 22:28:03 +00001544 chain_clear_(chain);
1545
Josh Coalsoncae58732002-05-04 17:30:32 +00001546 if(0 == (chain->filename = strdup(filename))) {
1547 chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
1548 return false;
1549 }
1550
Josh Coalson7d273b42006-11-15 06:12:30 +00001551 chain->is_ogg = is_ogg;
1552
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11001553 if(0 == (file = flac_fopen(filename, "rb"))) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00001554 chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
Josh Coalsoncae58732002-05-04 17:30:32 +00001555 return false;
1556 }
1557
Josh Coalson7d273b42006-11-15 06:12:30 +00001558 /* the function also sets chain->status for us */
1559 ret = is_ogg?
1560 chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) :
1561 chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_)
1562 ;
Josh Coalsoncae58732002-05-04 17:30:32 +00001563
Josh Coalson0e46bf52004-07-15 00:04:46 +00001564 fclose(file);
Josh Coalson91748be2004-07-10 22:28:03 +00001565
Josh Coalson0e46bf52004-07-15 00:04:46 +00001566 return ret;
Josh Coalson91748be2004-07-10 22:28:03 +00001567}
1568
Josh Coalson7d273b42006-11-15 06:12:30 +00001569FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
Josh Coalson91748be2004-07-10 22:28:03 +00001570{
Josh Coalson7d273b42006-11-15 06:12:30 +00001571 return chain_read_(chain, filename, /*is_ogg=*/false);
1572}
1573
Josh Coalson504dcaf2007-09-13 15:42:47 +00001574/*@@@@add to tests*/
Josh Coalson7d273b42006-11-15 06:12:30 +00001575FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename)
1576{
1577 return chain_read_(chain, filename, /*is_ogg=*/true);
1578}
1579
1580static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg)
1581{
1582 FLAC__bool ret;
1583
Josh Coalson91748be2004-07-10 22:28:03 +00001584 FLAC__ASSERT(0 != chain);
1585
1586 chain_clear_(chain);
1587
Josh Coalson0e46bf52004-07-15 00:04:46 +00001588 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) {
Josh Coalson91748be2004-07-10 22:28:03 +00001589 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1590 return false;
1591 }
1592
Josh Coalson7d273b42006-11-15 06:12:30 +00001593 chain->is_ogg = is_ogg;
1594
Josh Coalson9aac6702004-09-08 00:50:21 +00001595 /* rewind */
1596 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1597 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1598 return false;
1599 }
1600
Josh Coalson7d273b42006-11-15 06:12:30 +00001601 /* the function also sets chain->status for us */
1602 ret = is_ogg?
1603 chain_read_ogg_cb_(chain, handle, callbacks.read) :
1604 chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell)
1605 ;
Josh Coalson91748be2004-07-10 22:28:03 +00001606
Josh Coalson7d273b42006-11-15 06:12:30 +00001607 return ret;
1608}
1609
1610FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1611{
1612 return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false);
1613}
1614
Josh Coalson504dcaf2007-09-13 15:42:47 +00001615/*@@@@add to tests*/
Josh Coalson7d273b42006-11-15 06:12:30 +00001616FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1617{
1618 return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true);
Josh Coalsoncae58732002-05-04 17:30:32 +00001619}
1620
Josh Coalson463a22c2004-07-11 06:41:00 +00001621FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
1622{
1623 /* This does all the same checks that are in chain_prepare_for_write_()
1624 * but doesn't actually alter the chain. Make sure to update the logic
1625 * here if chain_prepare_for_write_() changes.
1626 */
Erik de Castro Lopo387992b2016-05-01 20:57:15 +10001627 FLAC__off_t current_length;
1628 FLAC__Metadata_Node *node;
Josh Coalson463a22c2004-07-11 06:41:00 +00001629
1630 FLAC__ASSERT(0 != chain);
1631
Erik de Castro Lopo387992b2016-05-01 20:57:15 +10001632 current_length = chain_calculate_length_(chain);
1633
1634 for (node = chain->head; node; node = node->next) {
1635 if(node != chain->tail || !use_padding) {
1636 if(node->data->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
1637 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1638 current_length -= node->data->length;
1639 current_length += (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
1640 } else {
1641 return false /* the return value doesn't matter */;
1642 }
1643 }
1644 } else {
1645 /* use_padding == true, node == chain->tail */
1646 unsigned block_len = node->data->length;
1647
1648 /* if the metadata shrank and the last block is padding, we just extend the last padding block */
1649 if(current_length < chain->initial_length && node->data->type == FLAC__METADATA_TYPE_PADDING) {
1650 const FLAC__off_t delta = chain->initial_length - current_length;
1651 block_len += delta;
1652 current_length += delta;
1653 /* test extended block */
1654 if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
1655 current_length -= block_len;
1656 current_length += (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
1657 }
1658 }
1659 /* if the metadata shrank more than 4 bytes then there's room to add another padding block */
1660 else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
1661 /* test current last block */
1662 if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
1663 current_length -= block_len;
1664 current_length += (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
1665 }
1666 block_len = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
1667 current_length += FLAC__STREAM_METADATA_HEADER_LENGTH + block_len;
1668 /* test added block */
1669 if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
1670 current_length -= block_len;
1671 current_length += (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
1672 }
1673 }
1674 /* 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 */
1675 else if(current_length > chain->initial_length) {
1676 const FLAC__off_t delta = current_length - chain->initial_length;
1677 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1678 /* if the delta is exactly the size of the last padding block, remove the padding block */
1679 if((FLAC__off_t)node->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
1680 block_len = 0;
1681 current_length -= FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length;
1682 }
1683 /* if there is at least 'delta' bytes of padding, trim the padding down */
1684 else if((FLAC__off_t)node->data->length >= delta) {
1685 block_len -= delta;
1686 current_length -= delta;
1687 /* test shrinked block */
1688 if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
1689 current_length -= block_len;
1690 current_length += (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
1691 }
1692 }
1693 }
Josh Coalson463a22c2004-07-11 06:41:00 +00001694 }
1695 }
1696 }
1697
Josh Coalson028d97e2004-07-14 00:49:56 +00001698 return (current_length != chain->initial_length);
Josh Coalson463a22c2004-07-11 06:41:00 +00001699}
1700
Josh Coalson6afed9f2002-10-16 22:29:47 +00001701FLAC_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 +00001702{
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +11001703 struct flac_stat_s stats;
Josh Coalsoncae58732002-05-04 17:30:32 +00001704 const char *tempfile_path_prefix = 0;
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001705 FLAC__off_t current_length;
Josh Coalson3ecb1172002-06-15 05:09:47 +00001706
1707 FLAC__ASSERT(0 != chain);
1708
Josh Coalson7d273b42006-11-15 06:12:30 +00001709 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1710 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1711 return false;
1712 }
1713
Josh Coalson463a22c2004-07-11 06:41:00 +00001714 if (0 == chain->filename) {
1715 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1716 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00001717 }
1718
Josh Coalson463a22c2004-07-11 06:41:00 +00001719 current_length = chain_prepare_for_write_(chain, use_padding);
1720
1721 /* a return value of 0 means there was an error; chain->status is already set */
1722 if (0 == current_length)
1723 return false;
1724
Josh Coalsoncae58732002-05-04 17:30:32 +00001725 if(preserve_file_stats)
1726 get_file_stats_(chain->filename, &stats);
1727
Josh Coalson3ecb1172002-06-15 05:09:47 +00001728 if(current_length == chain->initial_length) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001729 if(!chain_rewrite_metadata_in_place_(chain))
Josh Coalsoncae58732002-05-04 17:30:32 +00001730 return false;
1731 }
1732 else {
1733 if(!chain_rewrite_file_(chain, tempfile_path_prefix))
1734 return false;
Josh Coalson463a22c2004-07-11 06:41:00 +00001735
1736 /* recompute lengths and offsets */
1737 {
1738 const FLAC__Metadata_Node *node;
1739 chain->initial_length = current_length;
1740 chain->last_offset = chain->first_offset;
1741 for(node = chain->head; node; node = node->next)
1742 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
1743 }
Josh Coalsoncae58732002-05-04 17:30:32 +00001744 }
1745
1746 if(preserve_file_stats)
1747 set_file_stats_(chain->filename, &stats);
1748
Josh Coalson463a22c2004-07-11 06:41:00 +00001749 return true;
1750}
1751
1752FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
1753{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001754 FLAC__off_t current_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001755
1756 FLAC__ASSERT(0 != chain);
1757
Josh Coalson7d273b42006-11-15 06:12:30 +00001758 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1759 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1760 return false;
1761 }
1762
Josh Coalson463a22c2004-07-11 06:41:00 +00001763 if (0 != chain->filename) {
1764 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1765 return false;
1766 }
1767
Josh Coalson0e46bf52004-07-15 00:04:46 +00001768 if (0 == callbacks.write || 0 == callbacks.seek) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001769 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1770 return false;
1771 }
1772
1773 if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1774 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1775 return false;
1776 }
1777
1778 current_length = chain_prepare_for_write_(chain, use_padding);
1779
1780 /* a return value of 0 means there was an error; chain->status is already set */
1781 if (0 == current_length)
1782 return false;
1783
1784 FLAC__ASSERT(current_length == chain->initial_length);
1785
Josh Coalson0e46bf52004-07-15 00:04:46 +00001786 return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek);
Josh Coalson463a22c2004-07-11 06:41:00 +00001787}
1788
1789FLAC_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)
1790{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11001791 FLAC__off_t current_length;
Josh Coalson463a22c2004-07-11 06:41:00 +00001792
1793 FLAC__ASSERT(0 != chain);
1794
Josh Coalson7d273b42006-11-15 06:12:30 +00001795 if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
1796 chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
1797 return false;
1798 }
1799
Josh Coalson463a22c2004-07-11 06:41:00 +00001800 if (0 != chain->filename) {
1801 chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
1802 return false;
1803 }
1804
Josh Coalson0e46bf52004-07-15 00:04:46 +00001805 if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001806 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1807 return false;
1808 }
Josh Coalson0e46bf52004-07-15 00:04:46 +00001809 if (0 == temp_callbacks.write) {
Josh Coalson463a22c2004-07-11 06:41:00 +00001810 chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
1811 return false;
1812 }
1813
1814 if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
1815 chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
1816 return false;
1817 }
1818
1819 current_length = chain_prepare_for_write_(chain, use_padding);
1820
1821 /* a return value of 0 means there was an error; chain->status is already set */
1822 if (0 == current_length)
1823 return false;
1824
1825 FLAC__ASSERT(current_length != chain->initial_length);
1826
Josh Coalson9aac6702004-09-08 00:50:21 +00001827 /* rewind */
1828 if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
1829 chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
1830 return false;
1831 }
1832
Josh Coalson0e46bf52004-07-15 00:04:46 +00001833 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 +00001834 return false;
1835
1836 /* recompute lengths and offsets */
1837 {
Josh Coalsoncc682512002-06-08 04:53:42 +00001838 const FLAC__Metadata_Node *node;
Josh Coalson3ecb1172002-06-15 05:09:47 +00001839 chain->initial_length = current_length;
Josh Coalsone2ba10f2002-05-14 06:03:17 +00001840 chain->last_offset = chain->first_offset;
1841 for(node = chain->head; node; node = node->next)
Josh Coalsoneb643a22002-05-20 05:58:50 +00001842 chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
Josh Coalsone2ba10f2002-05-14 06:03:17 +00001843 }
1844
Josh Coalsoncae58732002-05-04 17:30:32 +00001845 return true;
1846}
1847
Josh Coalson6afed9f2002-10-16 22:29:47 +00001848FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001849{
Josh Coalsoncc682512002-06-08 04:53:42 +00001850 FLAC__Metadata_Node *node;
Josh Coalsoncae58732002-05-04 17:30:32 +00001851
1852 FLAC__ASSERT(0 != chain);
1853
1854 for(node = chain->head; node; ) {
Josh Coalson2745fe72002-05-11 05:31:56 +00001855 if(!chain_merge_adjacent_padding_(chain, node))
Josh Coalsoncae58732002-05-04 17:30:32 +00001856 node = node->next;
Josh Coalsoncae58732002-05-04 17:30:32 +00001857 }
Josh Coalsoncae58732002-05-04 17:30:32 +00001858}
1859
Josh Coalson6afed9f2002-10-16 22:29:47 +00001860FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001861{
Josh Coalsoncc682512002-06-08 04:53:42 +00001862 FLAC__Metadata_Node *node, *save;
Josh Coalsoncae58732002-05-04 17:30:32 +00001863 unsigned i;
1864
1865 FLAC__ASSERT(0 != chain);
1866
1867 /*
1868 * Don't try and be too smart... this simple algo is good enough for
1869 * the small number of nodes that we deal with.
1870 */
1871 for(i = 0, node = chain->head; i < chain->nodes; i++) {
Josh Coalsoncae58732002-05-04 17:30:32 +00001872 if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
1873 save = node->next;
1874 chain_remove_node_(chain, node);
1875 chain_append_node_(chain, node);
1876 node = save;
1877 }
1878 else {
1879 node = node->next;
1880 }
1881 }
Josh Coalsoncae58732002-05-04 17:30:32 +00001882
1883 FLAC__metadata_chain_merge_padding(chain);
1884}
1885
1886
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001887FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void)
Josh Coalsoncae58732002-05-04 17:30:32 +00001888{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001889 FLAC__Metadata_Iterator *iterator = calloc(1, sizeof(FLAC__Metadata_Iterator));
Josh Coalsoncae58732002-05-04 17:30:32 +00001890
Josh Coalsonea7155f2002-10-18 05:49:19 +00001891 /* calloc() implies:
Josh Coalsoncae58732002-05-04 17:30:32 +00001892 iterator->current = 0;
1893 iterator->chain = 0;
Josh Coalsonea7155f2002-10-18 05:49:19 +00001894 */
Josh Coalsoncae58732002-05-04 17:30:32 +00001895
1896 return iterator;
1897}
1898
Josh Coalson6afed9f2002-10-16 22:29:47 +00001899FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001900{
1901 FLAC__ASSERT(0 != iterator);
1902
1903 free(iterator);
1904}
1905
Josh Coalson6afed9f2002-10-16 22:29:47 +00001906FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain)
Josh Coalsoncae58732002-05-04 17:30:32 +00001907{
1908 FLAC__ASSERT(0 != iterator);
1909 FLAC__ASSERT(0 != chain);
1910 FLAC__ASSERT(0 != chain->head);
1911
1912 iterator->chain = chain;
1913 iterator->current = chain->head;
1914}
1915
Josh Coalson6afed9f2002-10-16 22:29:47 +00001916FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001917{
1918 FLAC__ASSERT(0 != iterator);
1919
1920 if(0 == iterator->current || 0 == iterator->current->next)
1921 return false;
1922
1923 iterator->current = iterator->current->next;
1924 return true;
1925}
1926
Josh Coalson6afed9f2002-10-16 22:29:47 +00001927FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001928{
1929 FLAC__ASSERT(0 != iterator);
1930
1931 if(0 == iterator->current || 0 == iterator->current->prev)
1932 return false;
1933
1934 iterator->current = iterator->current->prev;
1935 return true;
1936}
1937
Josh Coalson6afed9f2002-10-16 22:29:47 +00001938FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001939{
1940 FLAC__ASSERT(0 != iterator);
1941 FLAC__ASSERT(0 != iterator->current);
Josh Coalson2745fe72002-05-11 05:31:56 +00001942 FLAC__ASSERT(0 != iterator->current->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001943
1944 return iterator->current->data->type;
1945}
1946
Josh Coalson6afed9f2002-10-16 22:29:47 +00001947FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00001948{
1949 FLAC__ASSERT(0 != iterator);
1950 FLAC__ASSERT(0 != iterator->current);
1951
1952 return iterator->current->data;
1953}
1954
Josh Coalson6afed9f2002-10-16 22:29:47 +00001955FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
Josh Coalson2745fe72002-05-11 05:31:56 +00001956{
Josh Coalson9b86c962002-07-10 07:11:15 +00001957 FLAC__ASSERT(0 != iterator);
1958 FLAC__ASSERT(0 != block);
Josh Coalson2745fe72002-05-11 05:31:56 +00001959 return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block);
1960}
1961
Josh Coalson6afed9f2002-10-16 22:29:47 +00001962FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding)
Josh Coalsoncae58732002-05-04 17:30:32 +00001963{
Josh Coalsoncc682512002-06-08 04:53:42 +00001964 FLAC__Metadata_Node *save;
Josh Coalsoncae58732002-05-04 17:30:32 +00001965
1966 FLAC__ASSERT(0 != iterator);
1967 FLAC__ASSERT(0 != iterator->current);
1968
1969 if(0 == iterator->current->prev) {
1970 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
1971 return false;
1972 }
1973
1974 save = iterator->current->prev;
1975
1976 if(replace_with_padding) {
Josh Coalson90ced912002-05-30 05:23:38 +00001977 FLAC__metadata_object_delete_data(iterator->current->data);
Josh Coalsoncae58732002-05-04 17:30:32 +00001978 iterator->current->data->type = FLAC__METADATA_TYPE_PADDING;
1979 }
1980 else {
1981 chain_delete_node_(iterator->chain, iterator->current);
1982 }
1983
1984 iterator->current = save;
1985 return true;
1986}
1987
Josh Coalson6afed9f2002-10-16 22:29:47 +00001988FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00001989{
Josh Coalsoncc682512002-06-08 04:53:42 +00001990 FLAC__Metadata_Node *node;
Josh Coalsoncae58732002-05-04 17:30:32 +00001991
1992 FLAC__ASSERT(0 != iterator);
1993 FLAC__ASSERT(0 != iterator->current);
Josh Coalson9b86c962002-07-10 07:11:15 +00001994 FLAC__ASSERT(0 != block);
Josh Coalsoncae58732002-05-04 17:30:32 +00001995
1996 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
1997 return false;
1998
1999 if(0 == iterator->current->prev) {
2000 FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
2001 return false;
2002 }
2003
2004 if(0 == (node = node_new_()))
2005 return false;
2006
2007 node->data = block;
2008 iterator_insert_node_(iterator, node);
2009 iterator->current = node;
2010 return true;
2011}
2012
Josh Coalson6afed9f2002-10-16 22:29:47 +00002013FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002014{
Josh Coalsoncc682512002-06-08 04:53:42 +00002015 FLAC__Metadata_Node *node;
Josh Coalsoncae58732002-05-04 17:30:32 +00002016
2017 FLAC__ASSERT(0 != iterator);
2018 FLAC__ASSERT(0 != iterator->current);
Josh Coalson9b86c962002-07-10 07:11:15 +00002019 FLAC__ASSERT(0 != block);
Josh Coalsoncae58732002-05-04 17:30:32 +00002020
2021 if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
2022 return false;
2023
2024 if(0 == (node = node_new_()))
2025 return false;
2026
2027 node->data = block;
2028 iterator_insert_node_after_(iterator, node);
2029 iterator->current = node;
2030 return true;
2031}
2032
2033
2034/****************************************************************************
2035 *
Josh Coalsoncae58732002-05-04 17:30:32 +00002036 * Local function definitions
2037 *
2038 ***************************************************************************/
2039
2040void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
2041{
2042 unsigned i;
2043
2044 b += bytes;
2045
2046 for(i = 0; i < bytes; i++) {
Josh Coalsoneb643a22002-05-20 05:58:50 +00002047 *(--b) = (FLAC__byte)(val & 0xff);
Josh Coalsoncae58732002-05-04 17:30:32 +00002048 val >>= 8;
2049 }
2050}
2051
2052void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
2053{
2054 unsigned i;
2055
2056 for(i = 0; i < bytes; i++) {
Josh Coalsoneb643a22002-05-20 05:58:50 +00002057 *(b++) = (FLAC__byte)(val & 0xff);
Josh Coalsoncae58732002-05-04 17:30:32 +00002058 val >>= 8;
2059 }
2060}
2061
2062void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes)
2063{
2064 unsigned i;
2065
2066 b += bytes;
2067
2068 for(i = 0; i < bytes; i++) {
Josh Coalson72943092002-05-22 05:35:43 +00002069 *(--b) = (FLAC__byte)(val & 0xff);
Josh Coalsoncae58732002-05-04 17:30:32 +00002070 val >>= 8;
2071 }
2072}
2073
2074FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes)
2075{
2076 FLAC__uint32 ret = 0;
2077 unsigned i;
2078
2079 for(i = 0; i < bytes; i++)
2080 ret = (ret << 8) | (FLAC__uint32)(*b++);
2081
2082 return ret;
2083}
2084
2085FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes)
2086{
2087 FLAC__uint32 ret = 0;
2088 unsigned i;
2089
2090 b += bytes;
2091
2092 for(i = 0; i < bytes; i++)
2093 ret = (ret << 8) | (FLAC__uint32)(*--b);
2094
2095 return ret;
2096}
2097
2098FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes)
2099{
2100 FLAC__uint64 ret = 0;
2101 unsigned i;
2102
2103 for(i = 0; i < bytes; i++)
2104 ret = (ret << 8) | (FLAC__uint64)(*b++);
2105
2106 return ret;
2107}
2108
Josh Coalsoncc682512002-06-08 04:53:42 +00002109FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00002110{
Josh Coalsoncae58732002-05-04 17:30:32 +00002111 FLAC__ASSERT(0 != iterator);
2112 FLAC__ASSERT(0 != iterator->file);
2113
Josh Coalsonea25cb32004-07-10 00:39:01 +00002114 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 +00002115 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2116 return false;
2117 }
2118
Josh Coalsoncae58732002-05-04 17:30:32 +00002119 return true;
2120}
2121
Josh Coalsoncc682512002-06-08 04:53:42 +00002122FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002123{
2124 FLAC__ASSERT(0 != iterator);
2125 FLAC__ASSERT(0 != iterator->file);
2126
Josh Coalsonea25cb32004-07-10 00:39:01 +00002127 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 +00002128
2129 return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
2130}
2131
Josh Coalsonea25cb32004-07-10 00:39:01 +00002132FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length)
2133{
2134 FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH];
2135
2136 if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
2137 return false;
2138
2139 *is_last = raw_header[0] & 0x80? true : false;
2140 *type = (FLAC__MetadataType)(raw_header[0] & 0x7f);
2141 *length = unpack_uint32_(raw_header + 1, 3);
2142
2143 /* Note that we don't check:
2144 * if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED)
2145 * we just will read in an opaque block
2146 */
2147
2148 return true;
2149}
2150
2151FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block)
2152{
2153 switch(block->type) {
2154 case FLAC__METADATA_TYPE_STREAMINFO:
2155 return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info);
2156 case FLAC__METADATA_TYPE_PADDING:
2157 return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length);
2158 case FLAC__METADATA_TYPE_APPLICATION:
2159 return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length);
2160 case FLAC__METADATA_TYPE_SEEKTABLE:
2161 return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length);
2162 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002163 return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, seek_cb, &block->data.vorbis_comment, block->length);
Josh Coalsonea25cb32004-07-10 00:39:01 +00002164 case FLAC__METADATA_TYPE_CUESHEET:
2165 return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +00002166 case FLAC__METADATA_TYPE_PICTURE:
2167 return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture);
Josh Coalsonea25cb32004-07-10 00:39:01 +00002168 default:
2169 return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length);
2170 }
2171}
2172
2173FLAC__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 +00002174{
2175 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b;
2176
Josh Coalsonea25cb32004-07-10 00:39:01 +00002177 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
Josh Coalsoncae58732002-05-04 17:30:32 +00002178 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2179
2180 b = buffer;
2181
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002182 /* we are using hardcoded numbers for simplicity but we should
Josh Coalsoncae58732002-05-04 17:30:32 +00002183 * probably eventually write a bit-level unpacker and use the
2184 * _STREAMINFO_ constants.
2185 */
2186 block->min_blocksize = unpack_uint32_(b, 2); b += 2;
2187 block->max_blocksize = unpack_uint32_(b, 2); b += 2;
2188 block->min_framesize = unpack_uint32_(b, 3); b += 3;
2189 block->max_framesize = unpack_uint32_(b, 3); b += 3;
2190 block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
2191 block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
Josh Coalsonb4e186a2002-08-12 21:35:43 +00002192 block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
Josh Coalsoncae58732002-05-04 17:30:32 +00002193 block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4);
2194 memcpy(block->md5sum, b+8, 16);
2195
2196 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2197}
2198
Josh Coalsonea25cb32004-07-10 00:39:01 +00002199FLAC__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 +00002200{
Josh Coalsoncae58732002-05-04 17:30:32 +00002201 (void)block; /* nothing to do; we don't care about reading the padding bytes */
2202
Josh Coalsonea25cb32004-07-10 00:39:01 +00002203 if(0 != seek_cb(handle, block_length, SEEK_CUR))
Josh Coalsoncae58732002-05-04 17:30:32 +00002204 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2205
2206 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2207}
2208
Josh Coalsonea25cb32004-07-10 00:39:01 +00002209FLAC__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 +00002210{
2211 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
2212
Josh Coalsonea25cb32004-07-10 00:39:01 +00002213 if(read_cb(block->id, 1, id_bytes, handle) != id_bytes)
Josh Coalsoncae58732002-05-04 17:30:32 +00002214 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2215
Josh Coalson0f008d22007-09-11 04:49:56 +00002216 if(block_length < id_bytes)
2217 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2218
Josh Coalsoncae58732002-05-04 17:30:32 +00002219 block_length -= id_bytes;
2220
Josh Coalson72943092002-05-22 05:35:43 +00002221 if(block_length == 0) {
2222 block->data = 0;
2223 }
2224 else {
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002225 if(0 == (block->data = malloc(block_length)))
Josh Coalson72943092002-05-22 05:35:43 +00002226 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncae58732002-05-04 17:30:32 +00002227
Josh Coalsonea25cb32004-07-10 00:39:01 +00002228 if(read_cb(block->data, 1, block_length, handle) != block_length)
Josh Coalson72943092002-05-22 05:35:43 +00002229 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2230 }
Josh Coalsoncae58732002-05-04 17:30:32 +00002231
2232 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2233}
2234
Josh Coalsonea25cb32004-07-10 00:39:01 +00002235FLAC__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 +00002236{
2237 unsigned i;
2238 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
2239
Josh Coalsoncae58732002-05-04 17:30:32 +00002240 FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0);
2241
2242 block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
2243
Josh Coalson72943092002-05-22 05:35:43 +00002244 if(block->num_points == 0)
2245 block->points = 0;
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +10002246 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 +00002247 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2248
2249 for(i = 0; i < block->num_points; i++) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00002250 if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
Josh Coalsoncae58732002-05-04 17:30:32 +00002251 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002252 /* some MAGIC NUMBERs here */
Josh Coalsoncae58732002-05-04 17:30:32 +00002253 block->points[i].sample_number = unpack_uint64_(buffer, 8);
2254 block->points[i].stream_offset = unpack_uint64_(buffer+8, 8);
2255 block->points[i].frame_samples = unpack_uint32_(buffer+16, 2);
2256 }
2257
2258 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2259}
2260
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002261FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry, unsigned max_length)
Josh Coalsoncae58732002-05-04 17:30:32 +00002262{
2263 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
Josh Coalson7ee4d172002-06-01 06:31:27 +00002264 FLAC__byte buffer[4]; /* magic number is asserted below */
Josh Coalsoncae58732002-05-04 17:30:32 +00002265
Josh Coalsone343ab22006-09-23 19:21:19 +00002266 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer));
Josh Coalsoncae58732002-05-04 17:30:32 +00002267
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002268 if(max_length < entry_length_len)
2269 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA;
2270
2271 max_length -= entry_length_len;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002272 if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
Josh Coalsoncae58732002-05-04 17:30:32 +00002273 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalsoneb643a22002-05-20 05:58:50 +00002274 entry->length = unpack_uint32_little_endian_(buffer, entry_length_len);
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002275 if(max_length < entry->length) {
2276 entry->length = 0;
2277 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA;
2278 } else max_length -= entry->length;
Josh Coalsoncae58732002-05-04 17:30:32 +00002279
Josh Coalson6ffe8da2002-10-26 04:35:00 +00002280 if(0 != entry->entry)
2281 free(entry->entry);
2282
Josh Coalson72943092002-05-22 05:35:43 +00002283 if(entry->length == 0) {
2284 entry->entry = 0;
2285 }
2286 else {
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002287 if(0 == (entry->entry = safe_malloc_add_2op_(entry->length, /*+*/1)))
Josh Coalson72943092002-05-22 05:35:43 +00002288 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
Josh Coalsoncae58732002-05-04 17:30:32 +00002289
Josh Coalsonea25cb32004-07-10 00:39:01 +00002290 if(read_cb(entry->entry, 1, entry->length, handle) != entry->length)
Josh Coalson72943092002-05-22 05:35:43 +00002291 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalsondef597e2004-12-30 00:59:30 +00002292
2293 entry->entry[entry->length] = '\0';
Josh Coalson72943092002-05-22 05:35:43 +00002294 }
Josh Coalsoncae58732002-05-04 17:30:32 +00002295
2296 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2297}
2298
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002299FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, unsigned block_length)
Josh Coalsoncae58732002-05-04 17:30:32 +00002300{
2301 unsigned i;
Josh Coalsoncc682512002-06-08 04:53:42 +00002302 FLAC__Metadata_SimpleIteratorStatus status;
Josh Coalsoncae58732002-05-04 17:30:32 +00002303 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
Josh Coalson7ee4d172002-06-01 06:31:27 +00002304 FLAC__byte buffer[4]; /* magic number is asserted below */
Josh Coalsoncae58732002-05-04 17:30:32 +00002305
Josh Coalsone343ab22006-09-23 19:21:19 +00002306 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer));
Josh Coalsoncae58732002-05-04 17:30:32 +00002307
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002308 status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string), block_length);
2309 if(block_length >= 4)
2310 block_length -= 4;
2311 if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA)
2312 goto skip;
2313 else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
Josh Coalsoncae58732002-05-04 17:30:32 +00002314 return status;
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002315 block_length -= block->vendor_string.length;
Josh Coalsoncae58732002-05-04 17:30:32 +00002316
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002317 if(block_length < num_comments_len) goto skip; else block_length -= num_comments_len;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002318 if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
Josh Coalsoncae58732002-05-04 17:30:32 +00002319 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2320 block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len);
2321
Josh Coalson72943092002-05-22 05:35:43 +00002322 if(block->num_comments == 0) {
2323 block->comments = 0;
2324 }
Erik de Castro Lopoff507792015-07-05 21:21:44 +10002325 else if(0 == (block->comments = calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
2326 block->num_comments = 0;
Josh Coalsoncae58732002-05-04 17:30:32 +00002327 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
Erik de Castro Lopoff507792015-07-05 21:21:44 +10002328 }
Josh Coalsoncae58732002-05-04 17:30:32 +00002329
2330 for(i = 0; i < block->num_comments; i++) {
Erik de Castro Lopo25db9b22014-09-27 08:52:27 +10002331 status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i, block_length);
2332 if(block_length >= 4) block_length -= 4;
2333 if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA) {
2334 block->num_comments = i;
2335 goto skip;
2336 }
2337 else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) return status;
2338 block_length -= block->comments[i].length;
2339 }
2340
2341 skip:
2342 if(block_length > 0) {
2343 /* bad metadata */
2344 if(0 != seek_cb(handle, block_length, SEEK_CUR))
2345 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
Josh Coalsoncae58732002-05-04 17:30:32 +00002346 }
2347
2348 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2349}
2350
Josh Coalsonea25cb32004-07-10 00:39:01 +00002351FLAC__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 +00002352{
2353 unsigned i, len;
2354 FLAC__byte buffer[32]; /* asserted below that this is big enough */
2355
2356 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
2357 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2358 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);
2359
Josh Coalsone4869382002-11-15 05:41:48 +00002360 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2361 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002362 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002363 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2364 track->offset = unpack_uint64_(buffer, len);
2365
2366 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2367 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002368 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002369 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2370 track->number = (FLAC__byte)unpack_uint32_(buffer, len);
2371
2372 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2373 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002374 if(read_cb(track->isrc, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002375 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2376
2377 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);
2378 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 +00002379 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002380 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2381 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1);
2382 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1);
2383 track->type = buffer[0] >> 7;
2384 track->pre_emphasis = (buffer[0] >> 6) & 1;
2385
2386 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2387 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002388 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002389 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2390 track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len);
2391
2392 if(track->num_indices == 0) {
2393 track->indices = 0;
2394 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002395 else if(0 == (track->indices = calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
Josh Coalsone4869382002-11-15 05:41:48 +00002396 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2397
2398 for(i = 0; i < track->num_indices; i++) {
2399 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2400 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002401 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002402 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2403 track->indices[i].offset = unpack_uint64_(buffer, len);
2404
2405 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2406 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002407 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002408 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2409 track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len);
2410
2411 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2412 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002413 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002414 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2415 }
2416
2417 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2418}
2419
Josh Coalsonea25cb32004-07-10 00:39:01 +00002420FLAC__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 +00002421{
2422 unsigned i, len;
2423 FLAC__Metadata_SimpleIteratorStatus status;
Josh Coalsonbfc8e312002-11-21 09:00:25 +00002424 FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */
Josh Coalsone4869382002-11-15 05:41:48 +00002425
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002426 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 +00002427 FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer));
Josh Coalsone4869382002-11-15 05:41:48 +00002428
2429 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2430 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002431 if(read_cb(block->media_catalog_number, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002432 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2433
2434 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2435 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002436 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002437 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2438 block->lead_in = unpack_uint64_(buffer, len);
2439
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002440 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2441 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002442 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalson38162d52002-11-16 06:31:54 +00002443 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002444 block->is_cd = buffer[0]&0x80? true : false;
Josh Coalson38162d52002-11-16 06:31:54 +00002445
Josh Coalsone4869382002-11-15 05:41:48 +00002446 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2447 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
Josh Coalsonea25cb32004-07-10 00:39:01 +00002448 if(read_cb(buffer, 1, len, handle) != len)
Josh Coalsone4869382002-11-15 05:41:48 +00002449 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2450 block->num_tracks = unpack_uint32_(buffer, len);
2451
2452 if(block->num_tracks == 0) {
2453 block->tracks = 0;
2454 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002455 else if(0 == (block->tracks = calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
Josh Coalsone4869382002-11-15 05:41:48 +00002456 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2457
2458 for(i = 0; i < block->num_tracks; i++) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00002459 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 +00002460 return status;
2461 }
2462
2463 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2464}
2465
Josh Coalsonf9cf27f2007-07-25 03:11:00 +00002466static 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 +00002467{
2468 FLAC__byte buffer[sizeof(FLAC__uint32)];
2469
2470 FLAC__ASSERT(0 != data);
2471 FLAC__ASSERT(length_len%8 == 0);
2472
2473 length_len /= 8; /* convert to bytes */
2474
2475 FLAC__ASSERT(sizeof(buffer) >= length_len);
2476
2477 if(read_cb(buffer, 1, length_len, handle) != length_len)
2478 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2479 *length = unpack_uint32_(buffer, length_len);
2480
2481 if(0 != *data)
2482 free(*data);
2483
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002484 if(0 == (*data = safe_malloc_add_2op_(*length, /*+*/1)))
Josh Coalsone343ab22006-09-23 19:21:19 +00002485 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2486
2487 if(*length > 0) {
2488 if(read_cb(*data, 1, *length, handle) != *length)
2489 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2490 }
2491
2492 (*data)[*length] = '\0';
2493
2494 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2495}
2496
2497FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block)
2498{
2499 FLAC__Metadata_SimpleIteratorStatus status;
2500 FLAC__byte buffer[4]; /* asserted below that this is big enough */
2501 FLAC__uint32 len;
2502
2503 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2504 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2505 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2506 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
Josh Coalson74ed2942006-09-23 23:15:05 +00002507 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
Josh Coalsone343ab22006-09-23 19:21:19 +00002508
2509 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0);
2510 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8;
2511 if(read_cb(buffer, 1, len, handle) != len)
2512 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2513 block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len);
2514
2515 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)
2516 return status;
2517
2518 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)
2519 return status;
2520
2521 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0);
2522 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8;
2523 if(read_cb(buffer, 1, len, handle) != len)
2524 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2525 block->width = unpack_uint32_(buffer, len);
2526
2527 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0);
2528 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8;
2529 if(read_cb(buffer, 1, len, handle) != len)
2530 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2531 block->height = unpack_uint32_(buffer, len);
2532
2533 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0);
2534 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8;
2535 if(read_cb(buffer, 1, len, handle) != len)
2536 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2537 block->depth = unpack_uint32_(buffer, len);
2538
Josh Coalson74ed2942006-09-23 23:15:05 +00002539 FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0);
2540 len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8;
2541 if(read_cb(buffer, 1, len, handle) != len)
2542 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2543 block->colors = unpack_uint32_(buffer, len);
2544
Josh Coalsone343ab22006-09-23 19:21:19 +00002545 /* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */
2546 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)
2547 return status;
2548
2549 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2550}
2551
Josh Coalsonea25cb32004-07-10 00:39:01 +00002552FLAC__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 +00002553{
Josh Coalsond0609472003-01-10 05:37:13 +00002554 if(block_length == 0) {
2555 block->data = 0;
2556 }
2557 else {
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10002558 if(0 == (block->data = malloc(block_length)))
Josh Coalsond0609472003-01-10 05:37:13 +00002559 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2560
Josh Coalsonea25cb32004-07-10 00:39:01 +00002561 if(read_cb(block->data, 1, block_length, handle) != block_length)
Josh Coalsond0609472003-01-10 05:37:13 +00002562 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
2563 }
2564
2565 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2566}
2567
Josh Coalsoncc682512002-06-08 04:53:42 +00002568FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002569{
Josh Coalsoncae58732002-05-04 17:30:32 +00002570 FLAC__ASSERT(0 != file);
2571 FLAC__ASSERT(0 != status);
Josh Coalsoncae58732002-05-04 17:30:32 +00002572
Josh Coalson463a22c2004-07-11 06:41:00 +00002573 if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002574 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2575 return false;
2576 }
2577
2578 return true;
2579}
2580
Josh Coalsoncc682512002-06-08 04:53:42 +00002581FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002582{
2583 FLAC__ASSERT(0 != file);
2584 FLAC__ASSERT(0 != status);
2585
Josh Coalson463a22c2004-07-11 06:41:00 +00002586 if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
2587 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
2588 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002589 }
Josh Coalson463a22c2004-07-11 06:41:00 +00002590 else {
2591 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
2592 return false;
2593 }
Josh Coalsoncae58732002-05-04 17:30:32 +00002594}
2595
Josh Coalson463a22c2004-07-11 06:41:00 +00002596FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2597{
2598 FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];
2599
2600 FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
Erik de Castro Lopo94386fd2016-05-01 20:34:26 +10002601 /* double protection */
2602 if(block->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
2603 return false;
Josh Coalson463a22c2004-07-11 06:41:00 +00002604
2605 buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
2606 pack_uint32_(block->length, buffer + 1, 3);
2607
2608 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
2609 return false;
2610
2611 return true;
2612}
2613
2614FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
2615{
2616 FLAC__ASSERT(0 != block);
2617
2618 switch(block->type) {
2619 case FLAC__METADATA_TYPE_STREAMINFO:
2620 return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info);
2621 case FLAC__METADATA_TYPE_PADDING:
2622 return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length);
2623 case FLAC__METADATA_TYPE_APPLICATION:
2624 return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length);
2625 case FLAC__METADATA_TYPE_SEEKTABLE:
2626 return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table);
2627 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
2628 return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment);
2629 case FLAC__METADATA_TYPE_CUESHEET:
2630 return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +00002631 case FLAC__METADATA_TYPE_PICTURE:
2632 return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture);
Josh Coalson463a22c2004-07-11 06:41:00 +00002633 default:
2634 return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length);
2635 }
2636}
2637
2638FLAC__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 +00002639{
2640 FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
2641 const unsigned channels1 = block->channels - 1;
2642 const unsigned bps1 = block->bits_per_sample - 1;
2643
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002644 /* we are using hardcoded numbers for simplicity but we should
Josh Coalsoncae58732002-05-04 17:30:32 +00002645 * probably eventually write a bit-level packer and use the
2646 * _STREAMINFO_ constants.
2647 */
2648 pack_uint32_(block->min_blocksize, buffer, 2);
2649 pack_uint32_(block->max_blocksize, buffer+2, 2);
2650 pack_uint32_(block->min_framesize, buffer+4, 3);
2651 pack_uint32_(block->max_framesize, buffer+7, 3);
2652 buffer[10] = (block->sample_rate >> 12) & 0xff;
2653 buffer[11] = (block->sample_rate >> 4) & 0xff;
2654 buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4);
Josh Coalson7ee4d172002-06-01 06:31:27 +00002655 buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f));
Josh Coalsoncae58732002-05-04 17:30:32 +00002656 pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
2657 memcpy(buffer+18, block->md5sum, 16);
2658
Josh Coalson463a22c2004-07-11 06:41:00 +00002659 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
2660 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002661
Josh Coalson463a22c2004-07-11 06:41:00 +00002662 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002663}
2664
Josh Coalson463a22c2004-07-11 06:41:00 +00002665FLAC__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 +00002666{
2667 unsigned i, n = block_length;
2668 FLAC__byte buffer[1024];
2669
Josh Coalsoncae58732002-05-04 17:30:32 +00002670 (void)block;
2671
2672 memset(buffer, 0, 1024);
2673
2674 for(i = 0; i < n/1024; i++)
Josh Coalson463a22c2004-07-11 06:41:00 +00002675 if(write_cb(buffer, 1, 1024, handle) != 1024)
2676 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002677
2678 n %= 1024;
Josh Coalson765ff502002-08-27 05:46:11 +00002679
Josh Coalson463a22c2004-07-11 06:41:00 +00002680 if(write_cb(buffer, 1, n, handle) != n)
2681 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002682
Josh Coalson463a22c2004-07-11 06:41:00 +00002683 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002684}
2685
Josh Coalson463a22c2004-07-11 06:41:00 +00002686FLAC__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 +00002687{
2688 const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
2689
Josh Coalson463a22c2004-07-11 06:41:00 +00002690 if(write_cb(block->id, 1, id_bytes, handle) != id_bytes)
2691 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002692
2693 block_length -= id_bytes;
2694
Josh Coalson463a22c2004-07-11 06:41:00 +00002695 if(write_cb(block->data, 1, block_length, handle) != block_length)
2696 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002697
Josh Coalson463a22c2004-07-11 06:41:00 +00002698 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002699}
2700
Josh Coalson463a22c2004-07-11 06:41:00 +00002701FLAC__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 +00002702{
2703 unsigned i;
2704 FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
2705
Josh Coalsoncae58732002-05-04 17:30:32 +00002706 for(i = 0; i < block->num_points; i++) {
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00002707 /* some MAGIC NUMBERs here */
Josh Coalsoncae58732002-05-04 17:30:32 +00002708 pack_uint64_(block->points[i].sample_number, buffer, 8);
2709 pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
2710 pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
Josh Coalson463a22c2004-07-11 06:41:00 +00002711 if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
2712 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002713 }
2714
Josh Coalson463a22c2004-07-11 06:41:00 +00002715 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002716}
2717
Josh Coalson463a22c2004-07-11 06:41:00 +00002718FLAC__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 +00002719{
2720 unsigned i;
2721 const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
2722 const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
Josh Coalson7ee4d172002-06-01 06:31:27 +00002723 FLAC__byte buffer[4]; /* magic number is asserted below */
Josh Coalsoncae58732002-05-04 17:30:32 +00002724
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002725 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 +00002726
2727 pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002728 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2729 return false;
2730 if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length)
2731 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002732
2733 pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002734 if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
2735 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002736
2737 for(i = 0; i < block->num_comments; i++) {
2738 pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002739 if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
2740 return false;
2741 if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length)
2742 return false;
Josh Coalsoncae58732002-05-04 17:30:32 +00002743 }
2744
Josh Coalson463a22c2004-07-11 06:41:00 +00002745 return true;
Josh Coalsoncae58732002-05-04 17:30:32 +00002746}
2747
Josh Coalson463a22c2004-07-11 06:41:00 +00002748FLAC__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 +00002749{
2750 unsigned i, j, len;
Josh Coalson38162d52002-11-16 06:31:54 +00002751 FLAC__byte buffer[1024]; /* asserted below that this is big enough */
Josh Coalsone4869382002-11-15 05:41:48 +00002752
2753 FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
Josh Coalson38162d52002-11-16 06:31:54 +00002754 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8);
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002755 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 +00002756 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
2757
Josh Coalsone4869382002-11-15 05:41:48 +00002758 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2759 len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
Josh Coalson463a22c2004-07-11 06:41:00 +00002760 if(write_cb(block->media_catalog_number, 1, len, handle) != len)
2761 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002762
2763 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
2764 len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
2765 pack_uint64_(block->lead_in, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002766 if(write_cb(buffer, 1, len, handle) != len)
2767 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002768
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002769 FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
2770 len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
Josh Coalson38162d52002-11-16 06:31:54 +00002771 memset(buffer, 0, len);
Josh Coalson8f0c71b2002-12-05 06:37:46 +00002772 if(block->is_cd)
2773 buffer[0] |= 0x80;
Josh Coalson463a22c2004-07-11 06:41:00 +00002774 if(write_cb(buffer, 1, len, handle) != len)
2775 return false;
Josh Coalson38162d52002-11-16 06:31:54 +00002776
Josh Coalsone4869382002-11-15 05:41:48 +00002777 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
2778 len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
2779 pack_uint32_(block->num_tracks, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002780 if(write_cb(buffer, 1, len, handle) != len)
2781 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002782
2783 for(i = 0; i < block->num_tracks; i++) {
2784 FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i;
2785
2786 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
2787 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
2788 pack_uint64_(track->offset, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002789 if(write_cb(buffer, 1, len, handle) != len)
2790 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002791
2792 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
2793 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
2794 pack_uint32_(track->number, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002795 if(write_cb(buffer, 1, len, handle) != len)
2796 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002797
2798 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2799 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
Josh Coalson463a22c2004-07-11 06:41:00 +00002800 if(write_cb(track->isrc, 1, len, handle) != len)
2801 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002802
2803 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);
2804 len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
2805 memset(buffer, 0, len);
2806 buffer[0] = (track->type << 7) | (track->pre_emphasis << 6);
Josh Coalson463a22c2004-07-11 06:41:00 +00002807 if(write_cb(buffer, 1, len, handle) != len)
2808 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002809
2810 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
2811 len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
2812 pack_uint32_(track->num_indices, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002813 if(write_cb(buffer, 1, len, handle) != len)
2814 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002815
2816 for(j = 0; j < track->num_indices; j++) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11002817 FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + j;
Josh Coalsone4869382002-11-15 05:41:48 +00002818
2819 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
2820 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11002821 pack_uint64_(indx->offset, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002822 if(write_cb(buffer, 1, len, handle) != len)
2823 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002824
2825 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
2826 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11002827 pack_uint32_(indx->number, buffer, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002828 if(write_cb(buffer, 1, len, handle) != len)
2829 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002830
2831 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
2832 len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
2833 memset(buffer, 0, len);
Josh Coalson463a22c2004-07-11 06:41:00 +00002834 if(write_cb(buffer, 1, len, handle) != len)
2835 return false;
Josh Coalsone4869382002-11-15 05:41:48 +00002836 }
2837 }
2838
Josh Coalson463a22c2004-07-11 06:41:00 +00002839 return true;
Josh Coalsone4869382002-11-15 05:41:48 +00002840}
2841
Josh Coalsone343ab22006-09-23 19:21:19 +00002842FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block)
2843{
2844 unsigned len;
2845 size_t slen;
2846 FLAC__byte buffer[4]; /* magic number is asserted below */
2847
2848 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8);
2849 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8);
2850 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8);
2851 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8);
2852 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8);
2853 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8);
Josh Coalson74ed2942006-09-23 23:15:05 +00002854 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8);
Josh Coalsone343ab22006-09-23 19:21:19 +00002855 FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8);
2856 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
2857 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8);
2858 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8);
2859 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
2860 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
2861 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
Josh Coalson74ed2942006-09-23 23:15:05 +00002862 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
Josh Coalsone343ab22006-09-23 19:21:19 +00002863 FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8);
2864
2865 len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8;
2866 pack_uint32_(block->type, buffer, len);
2867 if(write_cb(buffer, 1, len, handle) != len)
2868 return false;
2869
2870 len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8;
2871 slen = strlen(block->mime_type);
2872 pack_uint32_(slen, buffer, len);
2873 if(write_cb(buffer, 1, len, handle) != len)
2874 return false;
2875 if(write_cb(block->mime_type, 1, slen, handle) != slen)
2876 return false;
2877
2878 len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8;
2879 slen = strlen((const char *)block->description);
2880 pack_uint32_(slen, buffer, len);
2881 if(write_cb(buffer, 1, len, handle) != len)
2882 return false;
2883 if(write_cb(block->description, 1, slen, handle) != slen)
2884 return false;
2885
2886 len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8;
2887 pack_uint32_(block->width, buffer, len);
2888 if(write_cb(buffer, 1, len, handle) != len)
2889 return false;
2890
2891 len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8;
2892 pack_uint32_(block->height, buffer, len);
2893 if(write_cb(buffer, 1, len, handle) != len)
2894 return false;
2895
2896 len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8;
2897 pack_uint32_(block->depth, buffer, len);
2898 if(write_cb(buffer, 1, len, handle) != len)
2899 return false;
2900
Josh Coalson74ed2942006-09-23 23:15:05 +00002901 len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8;
2902 pack_uint32_(block->colors, buffer, len);
2903 if(write_cb(buffer, 1, len, handle) != len)
2904 return false;
2905
Josh Coalsone343ab22006-09-23 19:21:19 +00002906 len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8;
2907 pack_uint32_(block->data_length, buffer, len);
2908 if(write_cb(buffer, 1, len, handle) != len)
2909 return false;
2910 if(write_cb(block->data, 1, block->data_length, handle) != block->data_length)
2911 return false;
2912
2913 return true;
2914}
2915
Josh Coalson463a22c2004-07-11 06:41:00 +00002916FLAC__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 +00002917{
Josh Coalson463a22c2004-07-11 06:41:00 +00002918 if(write_cb(block->data, 1, block_length, handle) != block_length)
2919 return false;
Josh Coalsond0609472003-01-10 05:37:13 +00002920
Josh Coalson463a22c2004-07-11 06:41:00 +00002921 return true;
Josh Coalsond0609472003-01-10 05:37:13 +00002922}
2923
Josh Coalsoncc682512002-06-08 04:53:42 +00002924FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block)
Josh Coalsoncae58732002-05-04 17:30:32 +00002925{
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002926 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002927 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2928 return false;
2929 }
2930
2931 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2932 return false;
2933
2934 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2935 return false;
2936
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002937 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002938 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2939 return false;
2940 }
2941
2942 return read_metadata_block_header_(iterator);
2943}
2944
Josh Coalsoncc682512002-06-08 04:53:42 +00002945FLAC__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 +00002946{
Josh Coalsoncc682512002-06-08 04:53:42 +00002947 FLAC__StreamMetadata *padding;
Josh Coalsoncae58732002-05-04 17:30:32 +00002948
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002949 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002950 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2951 return false;
2952 }
2953
2954 block->is_last = false;
2955
2956 if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
2957 return false;
2958
2959 if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
2960 return false;
2961
2962 if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
2963 return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
2964
2965 padding->is_last = padding_is_last;
2966 padding->length = padding_length;
2967
2968 if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) {
2969 FLAC__metadata_object_delete(padding);
2970 return false;
2971 }
2972
2973 if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) {
2974 FLAC__metadata_object_delete(padding);
2975 return false;
2976 }
2977
2978 FLAC__metadata_object_delete(padding);
2979
Josh Coalsonb1ec7962006-05-24 04:41:36 +00002980 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00002981 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
2982 return false;
2983 }
2984
2985 return read_metadata_block_header_(iterator);
2986}
2987
Josh Coalsoncc682512002-06-08 04:53:42 +00002988FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append)
Josh Coalsoncae58732002-05-04 17:30:32 +00002989{
Erik de Castro Lopo5de949f2012-04-05 21:24:06 +10002990 FILE *tempfile = NULL;
2991 char *tempfilename = NULL;
Josh Coalson6b02a752002-05-10 06:42:02 +00002992 int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11002993 FLAC__off_t fixup_is_last_flag_offset = -1;
Josh Coalsoncae58732002-05-04 17:30:32 +00002994
Josh Coalson51ee4652002-05-09 05:46:23 +00002995 FLAC__ASSERT(0 != block || append == false);
2996
Josh Coalson6b02a752002-05-10 06:42:02 +00002997 if(iterator->is_last) {
2998 if(append) {
2999 fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */
3000 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
3001 }
3002 else if(0 == block) {
3003 simple_iterator_push_(iterator);
3004 if(!FLAC__metadata_simple_iterator_prev(iterator)) {
3005 (void)simple_iterator_pop_(iterator);
3006 return false;
3007 }
3008 fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */
3009 fixup_is_last_flag_offset = iterator->offset[iterator->depth];
3010 if(!simple_iterator_pop_(iterator))
3011 return false;
3012 }
3013 }
3014
Josh Coalsoncae58732002-05-04 17:30:32 +00003015 if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append))
3016 return false;
3017
3018 if(0 != block) {
3019 if(!write_metadata_block_header_(tempfile, &iterator->status, block)) {
3020 cleanup_tempfile_(&tempfile, &tempfilename);
3021 return false;
3022 }
3023
3024 if(!write_metadata_block_data_(tempfile, &iterator->status, block)) {
3025 cleanup_tempfile_(&tempfile, &tempfilename);
3026 return false;
3027 }
3028 }
3029
Josh Coalson6b02a752002-05-10 06:42:02 +00003030 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 +00003031 return false;
3032
Josh Coalson51ee4652002-05-09 05:46:23 +00003033 if(append)
3034 return FLAC__metadata_simple_iterator_next(iterator);
3035
Josh Coalsoncae58732002-05-04 17:30:32 +00003036 return true;
3037}
3038
Josh Coalsoncc682512002-06-08 04:53:42 +00003039void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00003040{
3041 FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH);
3042 iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth];
3043 iterator->depth++;
3044}
3045
Josh Coalsoncc682512002-06-08 04:53:42 +00003046FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator)
Josh Coalsoncae58732002-05-04 17:30:32 +00003047{
3048 FLAC__ASSERT(iterator->depth > 0);
3049 iterator->depth--;
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003050 if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003051 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3052 return false;
3053 }
3054
3055 return read_metadata_block_header_(iterator);
3056}
3057
Josh Coalsonea25cb32004-07-10 00:39:01 +00003058/* return meanings:
3059 * 0: ok
3060 * 1: read error
3061 * 2: seek error
3062 * 3: not a FLAC file
3063 */
3064unsigned 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 +00003065{
Josh Coalsona7b673c2002-05-25 02:13:38 +00003066 FLAC__byte buffer[4];
3067 size_t n;
3068 unsigned i;
Josh Coalsoncae58732002-05-04 17:30:32 +00003069
Josh Coalsone343ab22006-09-23 19:21:19 +00003070 FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer));
Josh Coalsoncae58732002-05-04 17:30:32 +00003071
Josh Coalsona7b673c2002-05-25 02:13:38 +00003072 /* skip any id3v2 tag */
3073 errno = 0;
Josh Coalsonea25cb32004-07-10 00:39:01 +00003074 n = read_cb(buffer, 1, 4, handle);
Josh Coalsona7b673c2002-05-25 02:13:38 +00003075 if(errno)
Josh Coalsoncae58732002-05-04 17:30:32 +00003076 return 1;
Josh Coalsonea25cb32004-07-10 00:39:01 +00003077 else if(n != 4)
3078 return 3;
Josh Coalsona7b673c2002-05-25 02:13:38 +00003079 else if(0 == memcmp(buffer, "ID3", 3)) {
3080 unsigned tag_length = 0;
3081
3082 /* skip to the tag length */
Josh Coalsonea25cb32004-07-10 00:39:01 +00003083 if(seek_cb(handle, 2, SEEK_CUR) < 0)
3084 return 2;
Josh Coalsona7b673c2002-05-25 02:13:38 +00003085
3086 /* read the length */
3087 for(i = 0; i < 4; i++) {
Josh Coalsonea25cb32004-07-10 00:39:01 +00003088 if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80)
Josh Coalsona7b673c2002-05-25 02:13:38 +00003089 return 1;
3090 tag_length <<= 7;
3091 tag_length |= (buffer[0] & 0x7f);
3092 }
3093
3094 /* skip the rest of the tag */
Josh Coalsonea25cb32004-07-10 00:39:01 +00003095 if(seek_cb(handle, tag_length, SEEK_CUR) < 0)
3096 return 2;
Josh Coalsona7b673c2002-05-25 02:13:38 +00003097
3098 /* read the stream sync code */
3099 errno = 0;
Josh Coalsonea25cb32004-07-10 00:39:01 +00003100 n = read_cb(buffer, 1, 4, handle);
Josh Coalsona7b673c2002-05-25 02:13:38 +00003101 if(errno)
3102 return 1;
Josh Coalsonea25cb32004-07-10 00:39:01 +00003103 else if(n != 4)
3104 return 3;
Josh Coalsoncae58732002-05-04 17:30:32 +00003105 }
Josh Coalsona7b673c2002-05-25 02:13:38 +00003106
3107 /* check for the fLaC signature */
3108 if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH))
3109 return 0;
3110 else
Josh Coalsonea25cb32004-07-10 00:39:01 +00003111 return 3;
3112}
3113
3114unsigned seek_to_first_metadata_block_(FILE *f)
3115{
3116 return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_);
Josh Coalsoncae58732002-05-04 17:30:32 +00003117}
3118
Josh Coalsoncc682512002-06-08 04:53:42 +00003119FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append)
Josh Coalsoncae58732002-05-04 17:30:32 +00003120{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003121 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 +00003122
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003123 if(0 != fseeko(iterator->file, 0, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003124 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3125 return false;
3126 }
3127 if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) {
3128 cleanup_tempfile_(tempfile, tempfilename);
3129 return false;
3130 }
3131 if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) {
3132 cleanup_tempfile_(tempfile, tempfilename);
3133 return false;
3134 }
3135
3136 return true;
3137}
3138
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003139FLAC__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 +00003140{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003141 FLAC__off_t save_offset = iterator->offset[iterator->depth];
Josh Coalsoncae58732002-05-04 17:30:32 +00003142 FLAC__ASSERT(0 != *tempfile);
3143
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003144 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 +00003145 cleanup_tempfile_(tempfile, tempfilename);
3146 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3147 return false;
3148 }
3149 if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) {
3150 cleanup_tempfile_(tempfile, tempfilename);
3151 return false;
3152 }
3153
Josh Coalson6b02a752002-05-10 06:42:02 +00003154 if(fixup_is_last_code != 0) {
3155 /*
3156 * if code == 1, it means a block was appended to the end so
3157 * we have to clear the is_last flag of the previous block
3158 * if code == -1, it means the last block was deleted so
3159 * we have to set the is_last flag of the previous block
3160 */
Josh Coalsonf0a8c4f2003-01-15 03:17:51 +00003161 /* 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 +00003162 FLAC__byte x;
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003163 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003164 cleanup_tempfile_(tempfile, tempfilename);
3165 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3166 return false;
3167 }
3168 if(fread(&x, 1, 1, *tempfile) != 1) {
3169 cleanup_tempfile_(tempfile, tempfilename);
3170 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3171 return false;
3172 }
Josh Coalson6b02a752002-05-10 06:42:02 +00003173 if(fixup_is_last_code > 0) {
3174 FLAC__ASSERT(x & 0x80);
3175 x &= 0x7f;
3176 }
3177 else {
3178 FLAC__ASSERT(!(x & 0x80));
3179 x |= 0x80;
3180 }
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003181 if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003182 cleanup_tempfile_(tempfile, tempfilename);
3183 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
3184 return false;
3185 }
Josh Coalsonf91251d2002-12-28 07:04:49 +00003186 if(local__fwrite(&x, 1, 1, *tempfile) != 1) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003187 cleanup_tempfile_(tempfile, tempfilename);
3188 iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3189 return false;
3190 }
3191 }
3192
3193 (void)fclose(iterator->file);
3194
3195 if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status))
3196 return false;
3197
3198 if(iterator->has_stats)
3199 set_file_stats_(iterator->filename, &iterator->stats);
3200
Josh Coalson3ac66932002-08-30 05:41:31 +00003201 if(!simple_iterator_prime_input_(iterator, !iterator->is_writable))
Josh Coalsoncae58732002-05-04 17:30:32 +00003202 return false;
3203 if(backup) {
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003204 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 +00003205 if(!FLAC__metadata_simple_iterator_next(iterator))
3206 return false;
3207 return true;
3208 }
3209 else {
3210 /* move the iterator to it's original block faster by faking a push, then doing a pop_ */
3211 FLAC__ASSERT(iterator->depth == 0);
3212 iterator->offset[0] = save_offset;
3213 iterator->depth++;
3214 return simple_iterator_pop_(iterator);
3215 }
3216}
3217
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003218FLAC__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 +00003219{
3220 FLAC__byte buffer[8192];
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003221 size_t n;
Josh Coalsoncae58732002-05-04 17:30:32 +00003222
Josh Coalsonacd4a432006-11-11 22:43:25 +00003223 FLAC__ASSERT(bytes >= 0);
Josh Coalsoncae58732002-05-04 17:30:32 +00003224 while(bytes > 0) {
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003225 n = flac_min(sizeof(buffer), (size_t)bytes);
Josh Coalsoncae58732002-05-04 17:30:32 +00003226 if(fread(buffer, 1, n, file) != n) {
3227 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3228 return false;
3229 }
Josh Coalsonf91251d2002-12-28 07:04:49 +00003230 if(local__fwrite(buffer, 1, n, tempfile) != n) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003231 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3232 return false;
3233 }
3234 bytes -= n;
3235 }
3236
3237 return true;
3238}
3239
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003240FLAC__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 +00003241{
3242 FLAC__byte buffer[8192];
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003243 size_t n;
Josh Coalson463a22c2004-07-11 06:41:00 +00003244
Josh Coalsonacd4a432006-11-11 22:43:25 +00003245 FLAC__ASSERT(bytes >= 0);
Josh Coalson463a22c2004-07-11 06:41:00 +00003246 while(bytes > 0) {
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003247 n = flac_min(sizeof(buffer), (size_t)bytes);
Josh Coalson463a22c2004-07-11 06:41:00 +00003248 if(read_cb(buffer, 1, n, handle) != n) {
3249 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3250 return false;
3251 }
3252 if(temp_write_cb(buffer, 1, n, temp_handle) != n) {
3253 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3254 return false;
3255 }
3256 bytes -= n;
3257 }
3258
3259 return true;
3260}
3261
Josh Coalsoncc682512002-06-08 04:53:42 +00003262FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003263{
3264 FLAC__byte buffer[8192];
3265 size_t n;
3266
3267 while(!feof(file)) {
3268 n = fread(buffer, 1, sizeof(buffer), file);
3269 if(n == 0 && !feof(file)) {
3270 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3271 return false;
3272 }
Josh Coalsonf91251d2002-12-28 07:04:49 +00003273 if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003274 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3275 return false;
3276 }
3277 }
3278
3279 return true;
3280}
3281
Josh Coalson463a22c2004-07-11 06:41:00 +00003282FLAC__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)
3283{
3284 FLAC__byte buffer[8192];
3285 size_t n;
3286
3287 while(!eof_cb(handle)) {
3288 n = read_cb(buffer, 1, sizeof(buffer), handle);
3289 if(n == 0 && !eof_cb(handle)) {
3290 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
3291 return false;
3292 }
3293 if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) {
3294 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
3295 return false;
3296 }
3297 }
3298
3299 return true;
3300}
3301
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003302static int
3303local_snprintf(char *str, size_t size, const char *fmt, ...)
3304{
3305 va_list va;
Erik de Castro Lopo1c7e3f12014-09-22 18:29:32 +10003306 int rc;
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003307
Erik de Castro Lopo1c7e3f12014-09-22 18:29:32 +10003308#if defined _MSC_VER
3309 if (size == 0)
3310 return 1024;
Julien Nabetd908b292016-01-22 20:57:24 +01003311#endif
3312
3313 va_start (va, fmt);
3314
3315#if defined _MSC_VER
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003316 rc = vsnprintf_s (str, size, _TRUNCATE, fmt, va);
Erik de Castro Lopo1c7e3f12014-09-22 18:29:32 +10003317 if (rc < 0)
3318 rc = size - 1;
3319#elif defined __MINGW32__
3320 rc = __mingw_vsnprintf (str, size, fmt, va);
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003321#else
3322 rc = vsnprintf (str, size, fmt, va);
3323#endif
3324 va_end (va);
3325
3326 return rc;
3327}
3328
Josh Coalsoncc682512002-06-08 04:53:42 +00003329FLAC__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 +00003330{
3331 static const char *tempfile_suffix = ".metadata_edit";
3332 if(0 == tempfile_path_prefix) {
Erik de Castro Lopo2d6354f2013-03-19 20:23:44 +11003333 size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1;
3334 if(0 == (*tempfilename = safe_malloc_(dest_len))) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003335 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
3336 return false;
3337 }
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003338 local_snprintf(*tempfilename, dest_len, "%s%s", filename, tempfile_suffix);
Josh Coalsoncae58732002-05-04 17:30:32 +00003339 }
3340 else {
3341 const char *p = strrchr(filename, '/');
Erik de Castro Lopo2d6354f2013-03-19 20:23:44 +11003342 size_t dest_len;
Josh Coalsoncae58732002-05-04 17:30:32 +00003343 if(0 == p)
3344 p = filename;
3345 else
3346 p++;
3347
Erik de Castro Lopo2d6354f2013-03-19 20:23:44 +11003348 dest_len = strlen(tempfile_path_prefix) + strlen(p) + strlen(tempfile_suffix) + 2;
3349
3350 if(0 == (*tempfilename = safe_malloc_(dest_len))) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003351 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
3352 return false;
3353 }
Erik de Castro Lopo14c28ae2013-03-21 17:28:23 +11003354 local_snprintf(*tempfilename, dest_len, "%s/%s%s", tempfile_path_prefix, p, tempfile_suffix);
Josh Coalsoncae58732002-05-04 17:30:32 +00003355 }
3356
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003357 if(0 == (*tempfile = flac_fopen(*tempfilename, "w+b"))) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003358 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
3359 return false;
3360 }
3361
3362 return true;
3363}
3364
Josh Coalsoncc682512002-06-08 04:53:42 +00003365FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003366{
3367 FLAC__ASSERT(0 != filename);
3368 FLAC__ASSERT(0 != tempfile);
3369 FLAC__ASSERT(0 != *tempfile);
3370 FLAC__ASSERT(0 != tempfilename);
3371 FLAC__ASSERT(0 != *tempfilename);
3372 FLAC__ASSERT(0 != status);
3373
3374 (void)fclose(*tempfile);
3375 *tempfile = 0;
Josh Coalsond57c8d32002-06-11 06:15:28 +00003376
Josh Coalson7581d122006-11-20 07:19:15 +00003377#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003378 /* on some flavors of windows, flac_rename() will fail if the destination already exists */
3379 if(flac_unlink(filename) < 0) {
Josh Coalsond57c8d32002-06-11 06:15:28 +00003380 cleanup_tempfile_(tempfile, tempfilename);
3381 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR;
3382 return false;
3383 }
3384#endif
3385
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003386 /*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just flac_rename(): */
3387 if(0 != flac_rename(*tempfilename, filename)) {
Josh Coalsoncae58732002-05-04 17:30:32 +00003388 cleanup_tempfile_(tempfile, tempfilename);
3389 *status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR;
3390 return false;
3391 }
3392
3393 cleanup_tempfile_(tempfile, tempfilename);
3394
3395 return true;
3396}
3397
3398void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
3399{
3400 if(0 != *tempfile) {
3401 (void)fclose(*tempfile);
3402 *tempfile = 0;
3403 }
3404
3405 if(0 != *tempfilename) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003406 (void)flac_unlink(*tempfilename);
Josh Coalsoncae58732002-05-04 17:30:32 +00003407 free(*tempfilename);
3408 *tempfilename = 0;
3409 }
3410}
3411
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +11003412FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats)
Josh Coalsoncae58732002-05-04 17:30:32 +00003413{
3414 FLAC__ASSERT(0 != filename);
3415 FLAC__ASSERT(0 != stats);
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003416 return (0 == flac_stat(filename, stats));
Josh Coalsoncae58732002-05-04 17:30:32 +00003417}
3418
Erik de Castro Lopo7dc5e3a2013-03-22 18:22:57 +11003419void set_file_stats_(const char *filename, struct flac_stat_s *stats)
Josh Coalsoncae58732002-05-04 17:30:32 +00003420{
3421 struct utimbuf srctime;
3422
3423 FLAC__ASSERT(0 != filename);
3424 FLAC__ASSERT(0 != stats);
3425
3426 srctime.actime = stats->st_atime;
3427 srctime.modtime = stats->st_mtime;
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11003428 (void)flac_chmod(filename, stats->st_mode);
3429 (void)flac_utime(filename, &srctime);
Erik de Castro Lopodac99fa2012-02-08 21:07:08 +11003430#if !defined _MSC_VER && !defined __BORLANDC__ && !defined __MINGW32__
Erik de Castro Lopo91790ef2013-03-05 22:19:27 +11003431 FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1));
3432 FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid));
Josh Coalsoncae58732002-05-04 17:30:32 +00003433#endif
3434}
3435
Josh Coalsonea25cb32004-07-10 00:39:01 +00003436int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
3437{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11003438 return fseeko((FILE*)handle, (FLAC__off_t)offset, whence);
Josh Coalsonea25cb32004-07-10 00:39:01 +00003439}
3440
3441FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle)
3442{
Josh Coalsonb1ec7962006-05-24 04:41:36 +00003443 return ftello((FILE*)handle);
Josh Coalsonea25cb32004-07-10 00:39:01 +00003444}
3445
Josh Coalsoncc682512002-06-08 04:53:42 +00003446FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status)
Josh Coalsoncae58732002-05-04 17:30:32 +00003447{
3448 switch(status) {
3449 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK:
3450 return FLAC__METADATA_CHAIN_STATUS_OK;
3451 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
3452 return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT;
3453 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
3454 return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
3455 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
3456 return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
3457 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE:
3458 return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE;
Josh Coalsona7b673c2002-05-25 02:13:38 +00003459 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA:
3460 return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
Josh Coalsoncae58732002-05-04 17:30:32 +00003461 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR:
3462 return FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
3463 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR:
3464 return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
3465 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR:
3466 return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
3467 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR:
3468 return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR;
3469 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR:
3470 return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR;
3471 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR:
3472 return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
3473 case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR:
3474 default:
3475 return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
3476 }
3477}