1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "private/metadata.h"
40 #include "private/memory.h"
42 #include "FLAC/assert.h"
43 #include "share/alloc.h"
44 #include "share/compat.h"
46 /* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
47 #define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
50 /****************************************************************************
54 ***************************************************************************/
57 * from = NULL && bytes = 0
59 * from != NULL && bytes > 0
62 * malloc error leaves 'to' unchanged
64 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
66 FLAC__ASSERT(0 != to);
67 if(bytes > 0 && 0 != from) {
69 if(0 == (x = safe_malloc_(bytes)))
71 memcpy(x, from, bytes);
75 FLAC__ASSERT(0 == from);
76 FLAC__ASSERT(bytes == 0);
83 /* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */
84 static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
87 FLAC__ASSERT(0 != to);
88 if(copy_bytes_(©, from, bytes)) {
99 /* reallocate entry to 1 byte larger and add a terminating NUL */
100 /* realloc() failure leaves entry unchanged */
101 static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
103 FLAC__byte *x = safe_realloc_add_2op_(*entry, length, /*+*/1);
113 /* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
114 * unchanged if malloc fails, free()ing the original '*to' if it
115 * succeeds and the original '*to' was not NULL
117 static FLAC__bool copy_cstring_(char **to, const char *from)
119 char *copy = strdup(from);
131 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
133 to->length = from->length;
134 if(0 == from->entry) {
135 FLAC__ASSERT(from->length == 0);
140 FLAC__ASSERT(from->length > 0);
141 if(0 == (x = safe_malloc_add_2op_(from->length, /*+*/1)))
143 memcpy(x, from->entry, from->length);
144 x[from->length] = '\0';
150 static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
152 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
153 if(0 == from->indices) {
154 FLAC__ASSERT(from->num_indices == 0);
157 FLAC__StreamMetadata_CueSheet_Index *x;
158 FLAC__ASSERT(from->num_indices > 0);
159 if(0 == (x = safe_malloc_mul_2op_p(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))))
161 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
167 static void seektable_calculate_length_(FLAC__StreamMetadata *object)
169 FLAC__ASSERT(0 != object);
170 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
172 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
175 static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
177 FLAC__StreamMetadata_SeekPoint *object_array;
179 FLAC__ASSERT(num_points > 0);
181 object_array = safe_malloc_mul_2op_p(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
183 if(0 != object_array) {
185 for(i = 0; i < num_points; i++) {
186 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
187 object_array[i].stream_offset = 0;
188 object_array[i].frame_samples = 0;
195 static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
199 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
201 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
202 object->length += object->data.vorbis_comment.vendor_string.length;
203 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
204 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
205 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
206 object->length += object->data.vorbis_comment.comments[i].length;
210 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
212 FLAC__ASSERT(num_comments > 0);
214 return safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
217 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
221 FLAC__ASSERT(0 != object_array && num_comments > 0);
223 for(i = 0; i < num_comments; i++)
224 if(0 != object_array[i].entry)
225 free(object_array[i].entry);
227 if(0 != object_array)
231 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
233 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
235 FLAC__ASSERT(0 != object_array);
236 FLAC__ASSERT(num_comments > 0);
238 return_array = vorbiscomment_entry_array_new_(num_comments);
240 if(0 != return_array) {
243 for(i = 0; i < num_comments; i++) {
244 if(!copy_vcentry_(return_array+i, object_array+i)) {
245 vorbiscomment_entry_array_delete_(return_array, num_comments);
254 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
258 FLAC__ASSERT(0 != object);
259 FLAC__ASSERT(0 != dest);
260 FLAC__ASSERT(0 != src);
261 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
262 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
266 if(0 != src->entry && src->length > 0) {
268 /* do the copy first so that if we fail we leave the dest object untouched */
269 if(!copy_vcentry_(dest, src))
273 /* we have to make sure that the string we're taking over is null-terminated */
276 * Stripping the const from src->entry is OK since we're taking
277 * ownership of the pointer. This is a hack around a deficiency
278 * in the API where the same function is used for 'copy' and
279 * 'own', but the source entry is a const pointer. If we were
280 * precise, the 'own' flavor would be a separate function with a
281 * non-const source pointer. But it's not, so we hack away.
283 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
289 /* the src is null */
296 vorbiscomment_calculate_length_(object);
300 static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
304 FLAC__ASSERT(0 != object);
305 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
306 FLAC__ASSERT(0 != field_name);
308 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
309 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
316 static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
320 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
323 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
324 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
325 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
326 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
327 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
330 object->length += object->data.cue_sheet.num_tracks * (
331 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
332 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
333 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
334 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
335 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
336 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
337 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
340 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
341 object->length += object->data.cue_sheet.tracks[i].num_indices * (
342 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
343 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
344 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
349 static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
351 FLAC__ASSERT(num_indices > 0);
353 return safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
356 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
358 FLAC__ASSERT(num_tracks > 0);
360 return safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
363 static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
367 FLAC__ASSERT(0 != object_array && num_tracks > 0);
369 for(i = 0; i < num_tracks; i++) {
370 if(0 != object_array[i].indices) {
371 FLAC__ASSERT(object_array[i].num_indices > 0);
372 free(object_array[i].indices);
376 if(0 != object_array)
380 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
382 FLAC__StreamMetadata_CueSheet_Track *return_array;
384 FLAC__ASSERT(0 != object_array);
385 FLAC__ASSERT(num_tracks > 0);
387 return_array = cuesheet_track_array_new_(num_tracks);
389 if(0 != return_array) {
392 for(i = 0; i < num_tracks; i++) {
393 if(!copy_track_(return_array+i, object_array+i)) {
394 cuesheet_track_array_delete_(return_array, num_tracks);
403 static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
405 FLAC__StreamMetadata_CueSheet_Index *save;
407 FLAC__ASSERT(0 != object);
408 FLAC__ASSERT(0 != dest);
409 FLAC__ASSERT(0 != src);
410 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
411 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
413 save = dest->indices;
415 /* do the copy first so that if we fail we leave the object untouched */
417 if(!copy_track_(dest, src))
427 cuesheet_calculate_length_(object);
432 /****************************************************************************
434 * Metadata object routines
436 ***************************************************************************/
438 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
440 FLAC__StreamMetadata *object;
442 if(type > FLAC__MAX_METADATA_TYPE_CODE)
445 object = calloc(1, sizeof(FLAC__StreamMetadata));
447 object->is_last = false;
450 case FLAC__METADATA_TYPE_STREAMINFO:
451 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
453 case FLAC__METADATA_TYPE_PADDING:
454 /* calloc() took care of this for us:
458 case FLAC__METADATA_TYPE_APPLICATION:
459 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
460 /* calloc() took care of this for us:
461 object->data.application.data = 0;
464 case FLAC__METADATA_TYPE_SEEKTABLE:
465 /* calloc() took care of this for us:
467 object->data.seek_table.num_points = 0;
468 object->data.seek_table.points = 0;
471 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
472 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
473 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
477 vorbiscomment_calculate_length_(object);
479 case FLAC__METADATA_TYPE_CUESHEET:
480 cuesheet_calculate_length_(object);
482 case FLAC__METADATA_TYPE_PICTURE:
484 FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
485 FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
486 FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
487 FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
488 FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
489 FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
490 FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
491 FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
494 object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
495 object->data.picture.mime_type = 0;
496 object->data.picture.description = 0;
497 /* calloc() took care of this for us:
498 object->data.picture.width = 0;
499 object->data.picture.height = 0;
500 object->data.picture.depth = 0;
501 object->data.picture.colors = 0;
502 object->data.picture.data_length = 0;
503 object->data.picture.data = 0;
505 /* now initialize mime_type and description with empty strings to make things easier on the client */
506 if(!copy_cstring_(&object->data.picture.mime_type, "")) {
510 if(!copy_cstring_((char**)(&object->data.picture.description), "")) {
511 if(object->data.picture.mime_type)
512 free(object->data.picture.mime_type);
518 /* calloc() took care of this for us:
520 object->data.unknown.data = 0;
529 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
531 FLAC__StreamMetadata *to;
533 FLAC__ASSERT(0 != object);
535 if(0 != (to = FLAC__metadata_object_new(object->type))) {
536 to->is_last = object->is_last;
537 to->type = object->type;
538 to->length = object->length;
540 case FLAC__METADATA_TYPE_STREAMINFO:
541 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
543 case FLAC__METADATA_TYPE_PADDING:
545 case FLAC__METADATA_TYPE_APPLICATION:
546 if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
547 FLAC__metadata_object_delete(to);
550 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
551 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
552 FLAC__metadata_object_delete(to);
556 case FLAC__METADATA_TYPE_SEEKTABLE:
557 to->data.seek_table.num_points = object->data.seek_table.num_points;
558 if(to->data.seek_table.num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
559 FLAC__metadata_object_delete(to);
562 if(!copy_bytes_((FLAC__byte**)&to->data.seek_table.points, (FLAC__byte*)object->data.seek_table.points, object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint))) {
563 FLAC__metadata_object_delete(to);
567 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
568 if(0 != to->data.vorbis_comment.vendor_string.entry) {
569 free(to->data.vorbis_comment.vendor_string.entry);
570 to->data.vorbis_comment.vendor_string.entry = 0;
572 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
573 FLAC__metadata_object_delete(to);
576 if(object->data.vorbis_comment.num_comments == 0) {
577 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
578 to->data.vorbis_comment.comments = 0;
581 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
582 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
583 if(0 == to->data.vorbis_comment.comments) {
584 FLAC__metadata_object_delete(to);
588 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
590 case FLAC__METADATA_TYPE_CUESHEET:
591 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
592 if(object->data.cue_sheet.num_tracks == 0) {
593 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
596 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
597 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
598 if(0 == to->data.cue_sheet.tracks) {
599 FLAC__metadata_object_delete(to);
604 case FLAC__METADATA_TYPE_PICTURE:
605 to->data.picture.type = object->data.picture.type;
606 if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
607 FLAC__metadata_object_delete(to);
610 if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
611 FLAC__metadata_object_delete(to);
614 to->data.picture.width = object->data.picture.width;
615 to->data.picture.height = object->data.picture.height;
616 to->data.picture.depth = object->data.picture.depth;
617 to->data.picture.colors = object->data.picture.colors;
618 to->data.picture.data_length = object->data.picture.data_length;
619 if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
620 FLAC__metadata_object_delete(to);
625 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
626 FLAC__metadata_object_delete(to);
636 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
638 FLAC__ASSERT(0 != object);
640 switch(object->type) {
641 case FLAC__METADATA_TYPE_STREAMINFO:
642 case FLAC__METADATA_TYPE_PADDING:
644 case FLAC__METADATA_TYPE_APPLICATION:
645 if(0 != object->data.application.data) {
646 free(object->data.application.data);
647 object->data.application.data = 0;
650 case FLAC__METADATA_TYPE_SEEKTABLE:
651 if(0 != object->data.seek_table.points) {
652 free(object->data.seek_table.points);
653 object->data.seek_table.points = 0;
656 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
657 if(0 != object->data.vorbis_comment.vendor_string.entry) {
658 free(object->data.vorbis_comment.vendor_string.entry);
659 object->data.vorbis_comment.vendor_string.entry = 0;
661 if(0 != object->data.vorbis_comment.comments) {
662 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
663 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
666 case FLAC__METADATA_TYPE_CUESHEET:
667 if(0 != object->data.cue_sheet.tracks) {
668 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
669 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
672 case FLAC__METADATA_TYPE_PICTURE:
673 if(0 != object->data.picture.mime_type) {
674 free(object->data.picture.mime_type);
675 object->data.picture.mime_type = 0;
677 if(0 != object->data.picture.description) {
678 free(object->data.picture.description);
679 object->data.picture.description = 0;
681 if(0 != object->data.picture.data) {
682 free(object->data.picture.data);
683 object->data.picture.data = 0;
687 if(0 != object->data.unknown.data) {
688 free(object->data.unknown.data);
689 object->data.unknown.data = 0;
695 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
697 FLAC__metadata_object_delete_data(object);
701 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
703 if(block1->min_blocksize != block2->min_blocksize)
705 if(block1->max_blocksize != block2->max_blocksize)
707 if(block1->min_framesize != block2->min_framesize)
709 if(block1->max_framesize != block2->max_framesize)
711 if(block1->sample_rate != block2->sample_rate)
713 if(block1->channels != block2->channels)
715 if(block1->bits_per_sample != block2->bits_per_sample)
717 if(block1->total_samples != block2->total_samples)
719 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
724 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
726 FLAC__ASSERT(0 != block1);
727 FLAC__ASSERT(0 != block2);
728 FLAC__ASSERT(block_length >= sizeof(block1->id));
730 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
732 if(0 != block1->data && 0 != block2->data)
733 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
735 return block1->data == block2->data;
738 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
742 FLAC__ASSERT(0 != block1);
743 FLAC__ASSERT(0 != block2);
745 if(block1->num_points != block2->num_points)
748 if(0 != block1->points && 0 != block2->points) {
749 for(i = 0; i < block1->num_points; i++) {
750 if(block1->points[i].sample_number != block2->points[i].sample_number)
752 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
754 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
760 return block1->points == block2->points;
763 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
767 if(block1->vendor_string.length != block2->vendor_string.length)
770 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
771 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
774 else if(block1->vendor_string.entry != block2->vendor_string.entry)
777 if(block1->num_comments != block2->num_comments)
780 for(i = 0; i < block1->num_comments; i++) {
781 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
782 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
785 else if(block1->comments[i].entry != block2->comments[i].entry)
791 static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
795 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
798 if(block1->lead_in != block2->lead_in)
801 if(block1->is_cd != block2->is_cd)
804 if(block1->num_tracks != block2->num_tracks)
807 if(0 != block1->tracks && 0 != block2->tracks) {
808 FLAC__ASSERT(block1->num_tracks > 0);
809 for(i = 0; i < block1->num_tracks; i++) {
810 if(block1->tracks[i].offset != block2->tracks[i].offset)
812 if(block1->tracks[i].number != block2->tracks[i].number)
814 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
816 if(block1->tracks[i].type != block2->tracks[i].type)
818 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
820 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
822 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
823 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
824 for(j = 0; j < block1->tracks[i].num_indices; j++) {
825 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
827 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
831 else if(block1->tracks[i].indices != block2->tracks[i].indices)
835 else if(block1->tracks != block2->tracks)
840 static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
842 if(block1->type != block2->type)
844 if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type)))
846 if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description)))
848 if(block1->width != block2->width)
850 if(block1->height != block2->height)
852 if(block1->depth != block2->depth)
854 if(block1->colors != block2->colors)
856 if(block1->data_length != block2->data_length)
858 if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length)))
863 static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
865 FLAC__ASSERT(0 != block1);
866 FLAC__ASSERT(0 != block2);
868 if(0 != block1->data && 0 != block2->data)
869 return 0 == memcmp(block1->data, block2->data, block_length);
871 return block1->data == block2->data;
874 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
876 FLAC__ASSERT(0 != block1);
877 FLAC__ASSERT(0 != block2);
879 if(block1->type != block2->type) {
882 if(block1->is_last != block2->is_last) {
885 if(block1->length != block2->length) {
888 switch(block1->type) {
889 case FLAC__METADATA_TYPE_STREAMINFO:
890 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
891 case FLAC__METADATA_TYPE_PADDING:
892 return true; /* we don't compare the padding guts */
893 case FLAC__METADATA_TYPE_APPLICATION:
894 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
895 case FLAC__METADATA_TYPE_SEEKTABLE:
896 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
897 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
898 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
899 case FLAC__METADATA_TYPE_CUESHEET:
900 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
901 case FLAC__METADATA_TYPE_PICTURE:
902 return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
904 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
908 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
912 FLAC__ASSERT(0 != object);
913 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
914 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
916 save = object->data.application.data;
918 /* do the copy first so that if we fail we leave the object untouched */
920 if(!copy_bytes_(&object->data.application.data, data, length))
924 object->data.application.data = data;
930 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
934 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
936 FLAC__ASSERT(0 != object);
937 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
939 if(0 == object->data.seek_table.points) {
940 FLAC__ASSERT(object->data.seek_table.num_points == 0);
941 if(0 == new_num_points)
943 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
947 const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
948 const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
951 if(new_num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
954 FLAC__ASSERT(object->data.seek_table.num_points > 0);
957 free(object->data.seek_table.points);
958 object->data.seek_table.points = 0;
960 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
963 /* if growing, set new elements to placeholders */
964 if(new_size > old_size) {
966 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
967 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
968 object->data.seek_table.points[i].stream_offset = 0;
969 object->data.seek_table.points[i].frame_samples = 0;
974 object->data.seek_table.num_points = new_num_points;
976 seektable_calculate_length_(object);
980 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
982 FLAC__ASSERT(0 != object);
983 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
984 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
986 object->data.seek_table.points[point_num] = point;
989 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
993 FLAC__ASSERT(0 != object);
994 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
995 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
997 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
1000 /* move all points >= point_num forward one space */
1001 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
1002 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
1004 FLAC__metadata_object_seektable_set_point(object, point_num, point);
1005 seektable_calculate_length_(object);
1009 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
1013 FLAC__ASSERT(0 != object);
1014 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1015 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
1017 /* move all points > point_num backward one space */
1018 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
1019 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
1021 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
1024 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
1026 FLAC__ASSERT(0 != object);
1027 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1029 return FLAC__format_seektable_is_legal(&object->data.seek_table);
1032 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
1034 FLAC__ASSERT(0 != object);
1035 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1038 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
1039 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
1044 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
1046 FLAC__StreamMetadata_SeekTable *seek_table;
1048 FLAC__ASSERT(0 != object);
1049 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1051 seek_table = &object->data.seek_table;
1053 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
1056 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
1057 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
1058 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
1063 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
1065 FLAC__ASSERT(0 != object);
1066 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1067 FLAC__ASSERT(0 != sample_numbers || num == 0);
1070 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1073 i = seek_table->num_points;
1075 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1078 for(j = 0; j < num; i++, j++) {
1079 seek_table->points[i].sample_number = sample_numbers[j];
1080 seek_table->points[i].stream_offset = 0;
1081 seek_table->points[i].frame_samples = 0;
1088 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
1090 FLAC__ASSERT(0 != object);
1091 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1092 FLAC__ASSERT(total_samples > 0);
1094 if(num > 0 && total_samples > 0) {
1095 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1098 i = seek_table->num_points;
1100 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1103 for(j = 0; j < num; i++, j++) {
1104 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
1105 seek_table->points[i].stream_offset = 0;
1106 seek_table->points[i].frame_samples = 0;
1113 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
1115 FLAC__ASSERT(0 != object);
1116 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1117 FLAC__ASSERT(samples > 0);
1118 FLAC__ASSERT(total_samples > 0);
1120 if(samples > 0 && total_samples > 0) {
1121 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1123 FLAC__uint64 num, sample;
1125 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
1126 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
1127 if(total_samples % samples == 0)
1130 i = seek_table->num_points;
1132 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
1136 for(j = 0; j < num; i++, j++, sample += samples) {
1137 seek_table->points[i].sample_number = sample;
1138 seek_table->points[i].stream_offset = 0;
1139 seek_table->points[i].frame_samples = 0;
1146 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
1150 FLAC__ASSERT(0 != object);
1151 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1153 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1155 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1158 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1160 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1162 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
1165 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
1167 FLAC__ASSERT(0 != object);
1168 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1170 if(0 == object->data.vorbis_comment.comments) {
1171 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1172 if(0 == new_num_comments)
1174 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1178 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1179 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1181 /* overflow check */
1182 if(new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
1185 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1187 /* if shrinking, free the truncated entries */
1188 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1190 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1191 if(0 != object->data.vorbis_comment.comments[i].entry)
1192 free(object->data.vorbis_comment.comments[i].entry);
1196 free(object->data.vorbis_comment.comments);
1197 object->data.vorbis_comment.comments = 0;
1199 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
1202 /* if growing, zero all the length/pointers of new elements */
1203 if(new_size > old_size)
1204 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1207 object->data.vorbis_comment.num_comments = new_num_comments;
1209 vorbiscomment_calculate_length_(object);
1213 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1215 FLAC__ASSERT(0 != object);
1216 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1218 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1220 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
1223 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1225 FLAC__StreamMetadata_VorbisComment *vc;
1227 FLAC__ASSERT(0 != object);
1228 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1229 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
1231 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1234 vc = &object->data.vorbis_comment;
1236 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
1239 /* move all comments >= comment_num forward one space */
1240 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
1241 vc->comments[comment_num].length = 0;
1242 vc->comments[comment_num].entry = 0;
1244 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1247 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1249 FLAC__ASSERT(0 != object);
1250 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1251 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1254 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1256 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1258 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1263 size_t field_name_length;
1264 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1266 FLAC__ASSERT(0 != eq);
1269 return false; /* double protection */
1271 field_name_length = eq-entry.entry;
1273 i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
1275 unsigned index = (unsigned)i;
1276 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
1278 entry = object->data.vorbis_comment.comments[index];
1279 index++; /* skip over replaced comment */
1280 if(all && index < object->data.vorbis_comment.num_comments) {
1281 i = vorbiscomment_find_entry_from_(object, index, (const char *)entry.entry, field_name_length);
1283 index = (unsigned)i;
1284 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, index))
1286 if(index < object->data.vorbis_comment.num_comments)
1287 i = vorbiscomment_find_entry_from_(object, index, (const char *)entry.entry, field_name_length);
1295 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1299 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
1301 FLAC__StreamMetadata_VorbisComment *vc;
1303 FLAC__ASSERT(0 != object);
1304 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1305 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1307 vc = &object->data.vorbis_comment;
1309 /* free the comment at comment_num */
1310 if(0 != vc->comments[comment_num].entry)
1311 free(vc->comments[comment_num].entry);
1313 /* move all comments > comment_num backward one space */
1314 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
1315 vc->comments[vc->num_comments-1].length = 0;
1316 vc->comments[vc->num_comments-1].entry = 0;
1318 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
1321 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, const char *field_value)
1323 FLAC__ASSERT(0 != entry);
1324 FLAC__ASSERT(0 != field_name);
1325 FLAC__ASSERT(0 != field_value);
1327 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1329 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
1333 const size_t nn = strlen(field_name);
1334 const size_t nv = strlen(field_value);
1335 entry->length = nn + 1 /*=*/ + nv;
1336 if(0 == (entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)))
1338 memcpy(entry->entry, field_name, nn);
1339 entry->entry[nn] = '=';
1340 memcpy(entry->entry+nn+1, field_value, nv);
1341 entry->entry[entry->length] = '\0';
1347 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1349 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1350 FLAC__ASSERT(0 != field_name);
1351 FLAC__ASSERT(0 != field_value);
1353 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1357 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1358 const size_t nn = eq-entry.entry;
1359 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1360 FLAC__ASSERT(0 != eq);
1362 return false; /* double protection */
1363 if(0 == (*field_name = safe_malloc_add_2op_(nn, /*+*/1)))
1365 if(0 == (*field_value = safe_malloc_add_2op_(nv, /*+*/1))) {
1369 memcpy(*field_name, entry.entry, nn);
1370 memcpy(*field_value, entry.entry+nn+1, nv);
1371 (*field_name)[nn] = '\0';
1372 (*field_value)[nv] = '\0';
1378 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
1380 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1382 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1383 return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
1387 FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
1389 FLAC__ASSERT(0 != field_name);
1391 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
1394 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1396 const unsigned field_name_length = strlen(field_name);
1399 FLAC__ASSERT(0 != object);
1400 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1402 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
1403 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1404 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1414 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1416 FLAC__bool ok = true;
1417 unsigned matching = 0;
1418 const unsigned field_name_length = strlen(field_name);
1421 FLAC__ASSERT(0 != object);
1422 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1424 /* must delete from end to start otherwise it will interfere with our iteration */
1425 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
1426 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1428 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1432 return ok? (int)matching : -1;
1435 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
1437 return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1440 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1442 FLAC__StreamMetadata_CueSheet_Track *to;
1444 FLAC__ASSERT(0 != object);
1446 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1447 if(!copy_track_(to, object)) {
1448 FLAC__metadata_object_cuesheet_track_delete(to);
1456 void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1458 FLAC__ASSERT(0 != object);
1460 if(0 != object->indices) {
1461 FLAC__ASSERT(object->num_indices > 0);
1462 free(object->indices);
1466 FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1468 FLAC__metadata_object_cuesheet_track_delete_data(object);
1472 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1474 FLAC__StreamMetadata_CueSheet_Track *track;
1475 FLAC__ASSERT(0 != object);
1476 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1477 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1479 track = &object->data.cue_sheet.tracks[track_num];
1481 if(0 == track->indices) {
1482 FLAC__ASSERT(track->num_indices == 0);
1483 if(0 == new_num_indices)
1485 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1489 const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1490 const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1492 /* overflow check */
1493 if(new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
1496 FLAC__ASSERT(track->num_indices > 0);
1499 free(track->indices);
1502 else if(0 == (track->indices = realloc(track->indices, new_size)))
1505 /* if growing, zero all the lengths/pointers of new elements */
1506 if(new_size > old_size)
1507 memset(track->indices + track->num_indices, 0, new_size - old_size);
1510 track->num_indices = new_num_indices;
1512 cuesheet_calculate_length_(object);
1516 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index)
1518 FLAC__StreamMetadata_CueSheet_Track *track;
1520 FLAC__ASSERT(0 != object);
1521 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1522 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1523 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1525 track = &object->data.cue_sheet.tracks[track_num];
1527 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1530 /* move all indices >= index_num forward one space */
1531 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1533 track->indices[index_num] = index;
1534 cuesheet_calculate_length_(object);
1538 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1540 FLAC__StreamMetadata_CueSheet_Index index;
1541 memset(&index, 0, sizeof(index));
1542 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
1545 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1547 FLAC__StreamMetadata_CueSheet_Track *track;
1549 FLAC__ASSERT(0 != object);
1550 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1551 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1552 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1554 track = &object->data.cue_sheet.tracks[track_num];
1556 /* move all indices > index_num backward one space */
1557 memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
1559 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1560 cuesheet_calculate_length_(object);
1564 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1566 FLAC__ASSERT(0 != object);
1567 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1569 if(0 == object->data.cue_sheet.tracks) {
1570 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1571 if(0 == new_num_tracks)
1573 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1577 const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1578 const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1580 /* overflow check */
1581 if(new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
1584 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1586 /* if shrinking, free the truncated entries */
1587 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1589 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1590 if(0 != object->data.cue_sheet.tracks[i].indices)
1591 free(object->data.cue_sheet.tracks[i].indices);
1595 free(object->data.cue_sheet.tracks);
1596 object->data.cue_sheet.tracks = 0;
1598 else if(0 == (object->data.cue_sheet.tracks = realloc(object->data.cue_sheet.tracks, new_size)))
1601 /* if growing, zero all the lengths/pointers of new elements */
1602 if(new_size > old_size)
1603 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1606 object->data.cue_sheet.num_tracks = new_num_tracks;
1608 cuesheet_calculate_length_(object);
1612 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1614 FLAC__ASSERT(0 != object);
1615 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1617 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
1620 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1622 FLAC__StreamMetadata_CueSheet *cs;
1624 FLAC__ASSERT(0 != object);
1625 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1626 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1628 cs = &object->data.cue_sheet;
1630 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1633 /* move all tracks >= track_num forward one space */
1634 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1635 cs->tracks[track_num].num_indices = 0;
1636 cs->tracks[track_num].indices = 0;
1638 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1641 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1643 FLAC__StreamMetadata_CueSheet_Track track;
1644 memset(&track, 0, sizeof(track));
1645 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1648 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1650 FLAC__StreamMetadata_CueSheet *cs;
1652 FLAC__ASSERT(0 != object);
1653 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1654 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1656 cs = &object->data.cue_sheet;
1658 /* free the track at track_num */
1659 if(0 != cs->tracks[track_num].indices)
1660 free(cs->tracks[track_num].indices);
1662 /* move all tracks > track_num backward one space */
1663 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1664 cs->tracks[cs->num_tracks-1].num_indices = 0;
1665 cs->tracks[cs->num_tracks-1].indices = 0;
1667 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1670 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1672 FLAC__ASSERT(0 != object);
1673 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1675 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1678 static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1680 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1682 else if (cs->tracks[track].indices[0].number == 1)
1683 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1684 else if (cs->tracks[track].num_indices < 2)
1686 else if (cs->tracks[track].indices[1].number == 1)
1687 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1692 static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1702 /*@@@@add to tests*/
1703 FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1705 const FLAC__StreamMetadata_CueSheet *cs;
1707 FLAC__ASSERT(0 != object);
1708 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1710 cs = &object->data.cue_sheet;
1712 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1716 FLAC__uint32 i, length, sum = 0;
1717 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1718 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1719 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1721 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1725 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1728 size_t old_length, new_length;
1730 FLAC__ASSERT(0 != object);
1731 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1732 FLAC__ASSERT(0 != mime_type);
1734 old = object->data.picture.mime_type;
1735 old_length = old? strlen(old) : 0;
1736 new_length = strlen(mime_type);
1738 /* do the copy first so that if we fail we leave the object untouched */
1740 if(new_length >= SIZE_MAX) /* overflow check */
1742 if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1746 object->data.picture.mime_type = mime_type;
1752 object->length -= old_length;
1753 object->length += new_length;
1757 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1760 size_t old_length, new_length;
1762 FLAC__ASSERT(0 != object);
1763 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1764 FLAC__ASSERT(0 != description);
1766 old = object->data.picture.description;
1767 old_length = old? strlen((const char *)old) : 0;
1768 new_length = strlen((const char *)description);
1770 /* do the copy first so that if we fail we leave the object untouched */
1772 if(new_length >= SIZE_MAX) /* overflow check */
1774 if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
1778 object->data.picture.description = description;
1784 object->length -= old_length;
1785 object->length += new_length;
1789 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1793 FLAC__ASSERT(0 != object);
1794 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1795 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
1797 old = object->data.picture.data;
1799 /* do the copy first so that if we fail we leave the object untouched */
1801 if(!copy_bytes_(&object->data.picture.data, data, length))
1805 object->data.picture.data = data;
1811 object->length -= object->data.picture.data_length;
1812 object->data.picture.data_length = length;
1813 object->length += length;
1817 FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1819 FLAC__ASSERT(0 != object);
1820 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1822 return FLAC__format_picture_is_legal(&object->data.picture, violation);