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