1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
40 #include "private/metadata.h"
41 #include "private/memory.h"
43 #include "FLAC/assert.h"
44 #include "share/alloc.h"
45 #include "share/compat.h"
47 /* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
48 #define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
51 /****************************************************************************
55 ***************************************************************************/
58 * from = NULL && bytes = 0
60 * from != NULL && bytes > 0
63 * malloc error leaves 'to' unchanged
65 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
67 FLAC__ASSERT(0 != to);
68 if(bytes > 0 && 0 != from) {
70 if(0 == (x = safe_malloc_(bytes)))
72 memcpy(x, from, bytes);
76 FLAC__ASSERT(0 == from);
77 FLAC__ASSERT(bytes == 0);
84 /* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */
85 static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
88 FLAC__ASSERT(0 != to);
89 if(copy_bytes_(©, from, bytes)) {
100 /* reallocate entry to 1 byte larger and add a terminating NUL */
101 /* realloc() failure leaves entry unchanged */
102 static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
104 FLAC__byte *x = safe_realloc_add_2op_(*entry, length, /*+*/1);
114 /* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
115 * unchanged if malloc fails, free()ing the original '*to' if it
116 * succeeds and the original '*to' was not NULL
118 static FLAC__bool copy_cstring_(char **to, const char *from)
120 char *copy = strdup(from);
132 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
134 to->length = from->length;
135 if(0 == from->entry) {
136 FLAC__ASSERT(from->length == 0);
141 FLAC__ASSERT(from->length > 0);
142 if(0 == (x = safe_malloc_add_2op_(from->length, /*+*/1)))
144 memcpy(x, from->entry, from->length);
145 x[from->length] = '\0';
151 static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
153 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
154 if(0 == from->indices) {
155 FLAC__ASSERT(from->num_indices == 0);
158 FLAC__StreamMetadata_CueSheet_Index *x;
159 FLAC__ASSERT(from->num_indices > 0);
160 if(0 == (x = safe_malloc_mul_2op_p(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))))
162 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
168 static void seektable_calculate_length_(FLAC__StreamMetadata *object)
170 FLAC__ASSERT(0 != object);
171 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
173 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
176 static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
178 FLAC__StreamMetadata_SeekPoint *object_array;
180 FLAC__ASSERT(num_points > 0);
182 object_array = safe_malloc_mul_2op_p(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
184 if(0 != object_array) {
186 for(i = 0; i < num_points; i++) {
187 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
188 object_array[i].stream_offset = 0;
189 object_array[i].frame_samples = 0;
196 static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
200 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
202 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
203 object->length += object->data.vorbis_comment.vendor_string.length;
204 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
205 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
206 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
207 object->length += object->data.vorbis_comment.comments[i].length;
211 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
213 FLAC__ASSERT(num_comments > 0);
215 return safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
218 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
222 FLAC__ASSERT(0 != object_array && num_comments > 0);
224 for(i = 0; i < num_comments; i++)
225 if(0 != object_array[i].entry)
226 free(object_array[i].entry);
228 if(0 != object_array)
232 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
234 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
236 FLAC__ASSERT(0 != object_array);
237 FLAC__ASSERT(num_comments > 0);
239 return_array = vorbiscomment_entry_array_new_(num_comments);
241 if(0 != return_array) {
244 for(i = 0; i < num_comments; i++) {
245 if(!copy_vcentry_(return_array+i, object_array+i)) {
246 vorbiscomment_entry_array_delete_(return_array, num_comments);
255 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
259 FLAC__ASSERT(0 != object);
260 FLAC__ASSERT(0 != dest);
261 FLAC__ASSERT(0 != src);
262 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
263 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
267 if(0 != src->entry && src->length > 0) {
269 /* do the copy first so that if we fail we leave the dest object untouched */
270 if(!copy_vcentry_(dest, src))
274 /* we have to make sure that the string we're taking over is null-terminated */
277 * Stripping the const from src->entry is OK since we're taking
278 * ownership of the pointer. This is a hack around a deficiency
279 * in the API where the same function is used for 'copy' and
280 * 'own', but the source entry is a const pointer. If we were
281 * precise, the 'own' flavor would be a separate function with a
282 * non-const source pointer. But it's not, so we hack away.
284 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
290 /* the src is null */
297 vorbiscomment_calculate_length_(object);
301 static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
305 FLAC__ASSERT(0 != object);
306 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
307 FLAC__ASSERT(0 != field_name);
309 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
310 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
317 static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
321 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
324 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
325 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
326 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
327 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
328 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
331 object->length += object->data.cue_sheet.num_tracks * (
332 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
333 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
334 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
335 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
336 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
337 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
338 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
341 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
342 object->length += object->data.cue_sheet.tracks[i].num_indices * (
343 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
344 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
345 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
350 static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
352 FLAC__ASSERT(num_indices > 0);
354 return safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
357 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
359 FLAC__ASSERT(num_tracks > 0);
361 return safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
364 static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
368 FLAC__ASSERT(0 != object_array && num_tracks > 0);
370 for(i = 0; i < num_tracks; i++) {
371 if(0 != object_array[i].indices) {
372 FLAC__ASSERT(object_array[i].num_indices > 0);
373 free(object_array[i].indices);
377 if(0 != object_array)
381 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
383 FLAC__StreamMetadata_CueSheet_Track *return_array;
385 FLAC__ASSERT(0 != object_array);
386 FLAC__ASSERT(num_tracks > 0);
388 return_array = cuesheet_track_array_new_(num_tracks);
390 if(0 != return_array) {
393 for(i = 0; i < num_tracks; i++) {
394 if(!copy_track_(return_array+i, object_array+i)) {
395 cuesheet_track_array_delete_(return_array, num_tracks);
404 static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
406 FLAC__StreamMetadata_CueSheet_Index *save;
408 FLAC__ASSERT(0 != object);
409 FLAC__ASSERT(0 != dest);
410 FLAC__ASSERT(0 != src);
411 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
412 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
414 save = dest->indices;
416 /* do the copy first so that if we fail we leave the object untouched */
418 if(!copy_track_(dest, src))
428 cuesheet_calculate_length_(object);
433 /****************************************************************************
435 * Metadata object routines
437 ***************************************************************************/
439 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
441 FLAC__StreamMetadata *object;
443 if(type > FLAC__MAX_METADATA_TYPE_CODE)
446 object = calloc(1, sizeof(FLAC__StreamMetadata));
448 object->is_last = false;
451 case FLAC__METADATA_TYPE_STREAMINFO:
452 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
454 case FLAC__METADATA_TYPE_PADDING:
455 /* calloc() took care of this for us:
459 case FLAC__METADATA_TYPE_APPLICATION:
460 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
461 /* calloc() took care of this for us:
462 object->data.application.data = 0;
465 case FLAC__METADATA_TYPE_SEEKTABLE:
466 /* calloc() took care of this for us:
468 object->data.seek_table.num_points = 0;
469 object->data.seek_table.points = 0;
472 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
473 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
474 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
478 vorbiscomment_calculate_length_(object);
480 case FLAC__METADATA_TYPE_CUESHEET:
481 cuesheet_calculate_length_(object);
483 case FLAC__METADATA_TYPE_PICTURE:
485 FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
486 FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
487 FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
488 FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
489 FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
490 FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
491 FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
492 FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
495 object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
496 object->data.picture.mime_type = 0;
497 object->data.picture.description = 0;
498 /* calloc() took care of this for us:
499 object->data.picture.width = 0;
500 object->data.picture.height = 0;
501 object->data.picture.depth = 0;
502 object->data.picture.colors = 0;
503 object->data.picture.data_length = 0;
504 object->data.picture.data = 0;
506 /* now initialize mime_type and description with empty strings to make things easier on the client */
507 if(!copy_cstring_(&object->data.picture.mime_type, "")) {
511 if(!copy_cstring_((char**)(&object->data.picture.description), "")) {
512 if(object->data.picture.mime_type)
513 free(object->data.picture.mime_type);
519 /* calloc() took care of this for us:
521 object->data.unknown.data = 0;
530 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
532 FLAC__StreamMetadata *to;
534 FLAC__ASSERT(0 != object);
536 if(0 != (to = FLAC__metadata_object_new(object->type))) {
537 to->is_last = object->is_last;
538 to->type = object->type;
539 to->length = object->length;
541 case FLAC__METADATA_TYPE_STREAMINFO:
542 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
544 case FLAC__METADATA_TYPE_PADDING:
546 case FLAC__METADATA_TYPE_APPLICATION:
547 if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
548 FLAC__metadata_object_delete(to);
551 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
552 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
553 FLAC__metadata_object_delete(to);
557 case FLAC__METADATA_TYPE_SEEKTABLE:
558 to->data.seek_table.num_points = object->data.seek_table.num_points;
559 if(to->data.seek_table.num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
560 FLAC__metadata_object_delete(to);
563 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))) {
564 FLAC__metadata_object_delete(to);
568 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
569 if(0 != to->data.vorbis_comment.vendor_string.entry) {
570 free(to->data.vorbis_comment.vendor_string.entry);
571 to->data.vorbis_comment.vendor_string.entry = 0;
573 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
574 FLAC__metadata_object_delete(to);
577 if(object->data.vorbis_comment.num_comments == 0) {
578 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
579 to->data.vorbis_comment.comments = 0;
582 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
583 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
584 if(0 == to->data.vorbis_comment.comments) {
585 FLAC__metadata_object_delete(to);
589 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
591 case FLAC__METADATA_TYPE_CUESHEET:
592 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
593 if(object->data.cue_sheet.num_tracks == 0) {
594 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
597 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
598 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
599 if(0 == to->data.cue_sheet.tracks) {
600 FLAC__metadata_object_delete(to);
605 case FLAC__METADATA_TYPE_PICTURE:
606 to->data.picture.type = object->data.picture.type;
607 if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
608 FLAC__metadata_object_delete(to);
611 if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
612 FLAC__metadata_object_delete(to);
615 to->data.picture.width = object->data.picture.width;
616 to->data.picture.height = object->data.picture.height;
617 to->data.picture.depth = object->data.picture.depth;
618 to->data.picture.colors = object->data.picture.colors;
619 to->data.picture.data_length = object->data.picture.data_length;
620 if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
621 FLAC__metadata_object_delete(to);
626 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
627 FLAC__metadata_object_delete(to);
637 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
639 FLAC__ASSERT(0 != object);
641 switch(object->type) {
642 case FLAC__METADATA_TYPE_STREAMINFO:
643 case FLAC__METADATA_TYPE_PADDING:
645 case FLAC__METADATA_TYPE_APPLICATION:
646 if(0 != object->data.application.data) {
647 free(object->data.application.data);
648 object->data.application.data = 0;
651 case FLAC__METADATA_TYPE_SEEKTABLE:
652 if(0 != object->data.seek_table.points) {
653 free(object->data.seek_table.points);
654 object->data.seek_table.points = 0;
657 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
658 if(0 != object->data.vorbis_comment.vendor_string.entry) {
659 free(object->data.vorbis_comment.vendor_string.entry);
660 object->data.vorbis_comment.vendor_string.entry = 0;
662 if(0 != object->data.vorbis_comment.comments) {
663 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
664 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
667 case FLAC__METADATA_TYPE_CUESHEET:
668 if(0 != object->data.cue_sheet.tracks) {
669 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
670 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
673 case FLAC__METADATA_TYPE_PICTURE:
674 if(0 != object->data.picture.mime_type) {
675 free(object->data.picture.mime_type);
676 object->data.picture.mime_type = 0;
678 if(0 != object->data.picture.description) {
679 free(object->data.picture.description);
680 object->data.picture.description = 0;
682 if(0 != object->data.picture.data) {
683 free(object->data.picture.data);
684 object->data.picture.data = 0;
688 if(0 != object->data.unknown.data) {
689 free(object->data.unknown.data);
690 object->data.unknown.data = 0;
696 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
698 FLAC__metadata_object_delete_data(object);
702 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
704 if(block1->min_blocksize != block2->min_blocksize)
706 if(block1->max_blocksize != block2->max_blocksize)
708 if(block1->min_framesize != block2->min_framesize)
710 if(block1->max_framesize != block2->max_framesize)
712 if(block1->sample_rate != block2->sample_rate)
714 if(block1->channels != block2->channels)
716 if(block1->bits_per_sample != block2->bits_per_sample)
718 if(block1->total_samples != block2->total_samples)
720 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
725 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
727 FLAC__ASSERT(0 != block1);
728 FLAC__ASSERT(0 != block2);
729 FLAC__ASSERT(block_length >= sizeof(block1->id));
731 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
733 if(0 != block1->data && 0 != block2->data)
734 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
736 return block1->data == block2->data;
739 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
743 FLAC__ASSERT(0 != block1);
744 FLAC__ASSERT(0 != block2);
746 if(block1->num_points != block2->num_points)
749 if(0 != block1->points && 0 != block2->points) {
750 for(i = 0; i < block1->num_points; i++) {
751 if(block1->points[i].sample_number != block2->points[i].sample_number)
753 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
755 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
761 return block1->points == block2->points;
764 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
768 if(block1->vendor_string.length != block2->vendor_string.length)
771 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
772 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
775 else if(block1->vendor_string.entry != block2->vendor_string.entry)
778 if(block1->num_comments != block2->num_comments)
781 for(i = 0; i < block1->num_comments; i++) {
782 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
783 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
786 else if(block1->comments[i].entry != block2->comments[i].entry)
792 static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
796 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
799 if(block1->lead_in != block2->lead_in)
802 if(block1->is_cd != block2->is_cd)
805 if(block1->num_tracks != block2->num_tracks)
808 if(0 != block1->tracks && 0 != block2->tracks) {
809 FLAC__ASSERT(block1->num_tracks > 0);
810 for(i = 0; i < block1->num_tracks; i++) {
811 if(block1->tracks[i].offset != block2->tracks[i].offset)
813 if(block1->tracks[i].number != block2->tracks[i].number)
815 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
817 if(block1->tracks[i].type != block2->tracks[i].type)
819 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
821 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
823 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
824 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
825 for(j = 0; j < block1->tracks[i].num_indices; j++) {
826 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
828 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
832 else if(block1->tracks[i].indices != block2->tracks[i].indices)
836 else if(block1->tracks != block2->tracks)
841 static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
843 if(block1->type != block2->type)
845 if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type)))
847 if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description)))
849 if(block1->width != block2->width)
851 if(block1->height != block2->height)
853 if(block1->depth != block2->depth)
855 if(block1->colors != block2->colors)
857 if(block1->data_length != block2->data_length)
859 if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length)))
864 static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
866 FLAC__ASSERT(0 != block1);
867 FLAC__ASSERT(0 != block2);
869 if(0 != block1->data && 0 != block2->data)
870 return 0 == memcmp(block1->data, block2->data, block_length);
872 return block1->data == block2->data;
875 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
877 FLAC__ASSERT(0 != block1);
878 FLAC__ASSERT(0 != block2);
880 if(block1->type != block2->type) {
883 if(block1->is_last != block2->is_last) {
886 if(block1->length != block2->length) {
889 switch(block1->type) {
890 case FLAC__METADATA_TYPE_STREAMINFO:
891 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
892 case FLAC__METADATA_TYPE_PADDING:
893 return true; /* we don't compare the padding guts */
894 case FLAC__METADATA_TYPE_APPLICATION:
895 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
896 case FLAC__METADATA_TYPE_SEEKTABLE:
897 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
898 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
899 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
900 case FLAC__METADATA_TYPE_CUESHEET:
901 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
902 case FLAC__METADATA_TYPE_PICTURE:
903 return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
905 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
909 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
913 FLAC__ASSERT(0 != object);
914 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
915 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
917 save = object->data.application.data;
919 /* do the copy first so that if we fail we leave the object untouched */
921 if(!copy_bytes_(&object->data.application.data, data, length))
925 object->data.application.data = data;
931 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
935 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
937 FLAC__ASSERT(0 != object);
938 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
940 if(0 == object->data.seek_table.points) {
941 FLAC__ASSERT(object->data.seek_table.num_points == 0);
942 if(0 == new_num_points)
944 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
948 const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
949 const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
952 if(new_num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
955 FLAC__ASSERT(object->data.seek_table.num_points > 0);
958 free(object->data.seek_table.points);
959 object->data.seek_table.points = 0;
961 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
964 /* if growing, set new elements to placeholders */
965 if(new_size > old_size) {
967 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
968 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
969 object->data.seek_table.points[i].stream_offset = 0;
970 object->data.seek_table.points[i].frame_samples = 0;
975 object->data.seek_table.num_points = new_num_points;
977 seektable_calculate_length_(object);
981 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
983 FLAC__ASSERT(0 != object);
984 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
985 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
987 object->data.seek_table.points[point_num] = point;
990 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
994 FLAC__ASSERT(0 != object);
995 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
996 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
998 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
1001 /* move all points >= point_num forward one space */
1002 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
1003 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
1005 FLAC__metadata_object_seektable_set_point(object, point_num, point);
1006 seektable_calculate_length_(object);
1010 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
1014 FLAC__ASSERT(0 != object);
1015 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1016 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
1018 /* move all points > point_num backward one space */
1019 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
1020 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
1022 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
1025 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
1027 FLAC__ASSERT(0 != object);
1028 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1030 return FLAC__format_seektable_is_legal(&object->data.seek_table);
1033 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
1035 FLAC__ASSERT(0 != object);
1036 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1039 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
1040 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
1045 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
1047 FLAC__StreamMetadata_SeekTable *seek_table;
1049 FLAC__ASSERT(0 != object);
1050 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1052 seek_table = &object->data.seek_table;
1054 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
1057 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
1058 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
1059 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
1064 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
1066 FLAC__ASSERT(0 != object);
1067 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1068 FLAC__ASSERT(0 != sample_numbers || num == 0);
1071 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1074 i = seek_table->num_points;
1076 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1079 for(j = 0; j < num; i++, j++) {
1080 seek_table->points[i].sample_number = sample_numbers[j];
1081 seek_table->points[i].stream_offset = 0;
1082 seek_table->points[i].frame_samples = 0;
1089 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
1091 FLAC__ASSERT(0 != object);
1092 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1093 FLAC__ASSERT(total_samples > 0);
1095 if(num > 0 && total_samples > 0) {
1096 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1099 i = seek_table->num_points;
1101 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1104 for(j = 0; j < num; i++, j++) {
1105 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
1106 seek_table->points[i].stream_offset = 0;
1107 seek_table->points[i].frame_samples = 0;
1114 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
1116 FLAC__ASSERT(0 != object);
1117 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1118 FLAC__ASSERT(samples > 0);
1119 FLAC__ASSERT(total_samples > 0);
1121 if(samples > 0 && total_samples > 0) {
1122 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1124 FLAC__uint64 num, sample;
1126 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
1127 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
1128 if(total_samples % samples == 0)
1131 i = seek_table->num_points;
1133 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
1137 for(j = 0; j < num; i++, j++, sample += samples) {
1138 seek_table->points[i].sample_number = sample;
1139 seek_table->points[i].stream_offset = 0;
1140 seek_table->points[i].frame_samples = 0;
1147 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
1151 FLAC__ASSERT(0 != object);
1152 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1154 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1156 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1159 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1161 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1163 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
1166 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
1168 FLAC__ASSERT(0 != object);
1169 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1171 if(0 == object->data.vorbis_comment.comments) {
1172 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1173 if(0 == new_num_comments)
1175 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1179 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1180 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1182 /* overflow check */
1183 if(new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
1186 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1188 /* if shrinking, free the truncated entries */
1189 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1191 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1192 if(0 != object->data.vorbis_comment.comments[i].entry)
1193 free(object->data.vorbis_comment.comments[i].entry);
1197 free(object->data.vorbis_comment.comments);
1198 object->data.vorbis_comment.comments = 0;
1200 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
1203 /* if growing, zero all the length/pointers of new elements */
1204 if(new_size > old_size)
1205 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1208 object->data.vorbis_comment.num_comments = new_num_comments;
1210 vorbiscomment_calculate_length_(object);
1214 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1216 FLAC__ASSERT(0 != object);
1217 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1219 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1221 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
1224 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1226 FLAC__StreamMetadata_VorbisComment *vc;
1228 FLAC__ASSERT(0 != object);
1229 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1230 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
1232 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1235 vc = &object->data.vorbis_comment;
1237 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
1240 /* move all comments >= comment_num forward one space */
1241 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
1242 vc->comments[comment_num].length = 0;
1243 vc->comments[comment_num].entry = 0;
1245 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1248 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1250 FLAC__ASSERT(0 != object);
1251 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1252 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1255 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1257 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1259 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1264 size_t field_name_length;
1265 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1267 FLAC__ASSERT(0 != eq);
1270 return false; /* double protection */
1272 field_name_length = eq-entry.entry;
1274 i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
1276 unsigned indx = (unsigned)i;
1277 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, indx, entry, copy))
1279 entry = object->data.vorbis_comment.comments[indx];
1280 indx++; /* skip over replaced comment */
1281 if(all && indx < object->data.vorbis_comment.num_comments) {
1282 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
1285 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, indx))
1287 if(indx < object->data.vorbis_comment.num_comments)
1288 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
1296 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1300 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
1302 FLAC__StreamMetadata_VorbisComment *vc;
1304 FLAC__ASSERT(0 != object);
1305 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1306 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1308 vc = &object->data.vorbis_comment;
1310 /* free the comment at comment_num */
1311 if(0 != vc->comments[comment_num].entry)
1312 free(vc->comments[comment_num].entry);
1314 /* move all comments > comment_num backward one space */
1315 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
1316 vc->comments[vc->num_comments-1].length = 0;
1317 vc->comments[vc->num_comments-1].entry = 0;
1319 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
1322 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)
1324 FLAC__ASSERT(0 != entry);
1325 FLAC__ASSERT(0 != field_name);
1326 FLAC__ASSERT(0 != field_value);
1328 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1330 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
1334 const size_t nn = strlen(field_name);
1335 const size_t nv = strlen(field_value);
1336 entry->length = nn + 1 /*=*/ + nv;
1337 if(0 == (entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)))
1339 memcpy(entry->entry, field_name, nn);
1340 entry->entry[nn] = '=';
1341 memcpy(entry->entry+nn+1, field_value, nv);
1342 entry->entry[entry->length] = '\0';
1348 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)
1350 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1351 FLAC__ASSERT(0 != field_name);
1352 FLAC__ASSERT(0 != field_value);
1354 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1358 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1359 const size_t nn = eq-entry.entry;
1360 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1361 FLAC__ASSERT(0 != eq);
1363 return false; /* double protection */
1364 if(0 == (*field_name = safe_malloc_add_2op_(nn, /*+*/1)))
1366 if(0 == (*field_value = safe_malloc_add_2op_(nv, /*+*/1))) {
1370 memcpy(*field_name, entry.entry, nn);
1371 memcpy(*field_value, entry.entry+nn+1, nv);
1372 (*field_name)[nn] = '\0';
1373 (*field_value)[nv] = '\0';
1379 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
1381 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1383 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1384 return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
1388 FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
1390 FLAC__ASSERT(0 != field_name);
1392 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
1395 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1397 const unsigned field_name_length = strlen(field_name);
1400 FLAC__ASSERT(0 != object);
1401 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1403 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
1404 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1405 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1415 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1417 FLAC__bool ok = true;
1418 unsigned matching = 0;
1419 const unsigned field_name_length = strlen(field_name);
1422 FLAC__ASSERT(0 != object);
1423 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1425 /* must delete from end to start otherwise it will interfere with our iteration */
1426 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
1427 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1429 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1433 return ok? (int)matching : -1;
1436 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
1438 return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1441 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1443 FLAC__StreamMetadata_CueSheet_Track *to;
1445 FLAC__ASSERT(0 != object);
1447 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1448 if(!copy_track_(to, object)) {
1449 FLAC__metadata_object_cuesheet_track_delete(to);
1457 void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1459 FLAC__ASSERT(0 != object);
1461 if(0 != object->indices) {
1462 FLAC__ASSERT(object->num_indices > 0);
1463 free(object->indices);
1467 FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1469 FLAC__metadata_object_cuesheet_track_delete_data(object);
1473 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1475 FLAC__StreamMetadata_CueSheet_Track *track;
1476 FLAC__ASSERT(0 != object);
1477 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1478 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1480 track = &object->data.cue_sheet.tracks[track_num];
1482 if(0 == track->indices) {
1483 FLAC__ASSERT(track->num_indices == 0);
1484 if(0 == new_num_indices)
1486 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1490 const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1491 const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1493 /* overflow check */
1494 if(new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
1497 FLAC__ASSERT(track->num_indices > 0);
1500 free(track->indices);
1503 else if(0 == (track->indices = realloc(track->indices, new_size)))
1506 /* if growing, zero all the lengths/pointers of new elements */
1507 if(new_size > old_size)
1508 memset(track->indices + track->num_indices, 0, new_size - old_size);
1511 track->num_indices = new_num_indices;
1513 cuesheet_calculate_length_(object);
1517 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index indx)
1519 FLAC__StreamMetadata_CueSheet_Track *track;
1521 FLAC__ASSERT(0 != object);
1522 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1523 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1524 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1526 track = &object->data.cue_sheet.tracks[track_num];
1528 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1531 /* move all indices >= index_num forward one space */
1532 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1534 track->indices[index_num] = indx;
1535 cuesheet_calculate_length_(object);
1539 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1541 FLAC__StreamMetadata_CueSheet_Index indx;
1542 memset(&indx, 0, sizeof(indx));
1543 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, indx);
1546 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1548 FLAC__StreamMetadata_CueSheet_Track *track;
1550 FLAC__ASSERT(0 != object);
1551 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1552 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1553 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1555 track = &object->data.cue_sheet.tracks[track_num];
1557 /* move all indices > index_num backward one space */
1558 memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
1560 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1561 cuesheet_calculate_length_(object);
1565 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1567 FLAC__ASSERT(0 != object);
1568 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1570 if(0 == object->data.cue_sheet.tracks) {
1571 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1572 if(0 == new_num_tracks)
1574 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1578 const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1579 const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1581 /* overflow check */
1582 if(new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
1585 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1587 /* if shrinking, free the truncated entries */
1588 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1590 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1591 if(0 != object->data.cue_sheet.tracks[i].indices)
1592 free(object->data.cue_sheet.tracks[i].indices);
1596 free(object->data.cue_sheet.tracks);
1597 object->data.cue_sheet.tracks = 0;
1599 else if(0 == (object->data.cue_sheet.tracks = realloc(object->data.cue_sheet.tracks, new_size)))
1602 /* if growing, zero all the lengths/pointers of new elements */
1603 if(new_size > old_size)
1604 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1607 object->data.cue_sheet.num_tracks = new_num_tracks;
1609 cuesheet_calculate_length_(object);
1613 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1615 FLAC__ASSERT(0 != object);
1616 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1618 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
1621 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1623 FLAC__StreamMetadata_CueSheet *cs;
1625 FLAC__ASSERT(0 != object);
1626 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1627 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1629 cs = &object->data.cue_sheet;
1631 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1634 /* move all tracks >= track_num forward one space */
1635 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1636 cs->tracks[track_num].num_indices = 0;
1637 cs->tracks[track_num].indices = 0;
1639 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1642 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1644 FLAC__StreamMetadata_CueSheet_Track track;
1645 memset(&track, 0, sizeof(track));
1646 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1649 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1651 FLAC__StreamMetadata_CueSheet *cs;
1653 FLAC__ASSERT(0 != object);
1654 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1655 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1657 cs = &object->data.cue_sheet;
1659 /* free the track at track_num */
1660 if(0 != cs->tracks[track_num].indices)
1661 free(cs->tracks[track_num].indices);
1663 /* move all tracks > track_num backward one space */
1664 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1665 cs->tracks[cs->num_tracks-1].num_indices = 0;
1666 cs->tracks[cs->num_tracks-1].indices = 0;
1668 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1671 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1673 FLAC__ASSERT(0 != object);
1674 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1676 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1679 static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1681 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1683 else if (cs->tracks[track].indices[0].number == 1)
1684 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1685 else if (cs->tracks[track].num_indices < 2)
1687 else if (cs->tracks[track].indices[1].number == 1)
1688 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1693 static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1703 /*@@@@add to tests*/
1704 FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1706 const FLAC__StreamMetadata_CueSheet *cs;
1708 FLAC__ASSERT(0 != object);
1709 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1711 cs = &object->data.cue_sheet;
1713 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1717 FLAC__uint32 i, length, sum = 0;
1718 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1719 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1720 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1722 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1726 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1729 size_t old_length, new_length;
1731 FLAC__ASSERT(0 != object);
1732 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1733 FLAC__ASSERT(0 != mime_type);
1735 old = object->data.picture.mime_type;
1736 old_length = old? strlen(old) : 0;
1737 new_length = strlen(mime_type);
1739 /* do the copy first so that if we fail we leave the object untouched */
1741 if(new_length >= SIZE_MAX) /* overflow check */
1743 if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1747 object->data.picture.mime_type = mime_type;
1753 object->length -= old_length;
1754 object->length += new_length;
1758 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1761 size_t old_length, new_length;
1763 FLAC__ASSERT(0 != object);
1764 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1765 FLAC__ASSERT(0 != description);
1767 old = object->data.picture.description;
1768 old_length = old? strlen((const char *)old) : 0;
1769 new_length = strlen((const char *)description);
1771 /* do the copy first so that if we fail we leave the object untouched */
1773 if(new_length >= SIZE_MAX) /* overflow check */
1775 if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
1779 object->data.picture.description = description;
1785 object->length -= old_length;
1786 object->length += new_length;
1790 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1794 FLAC__ASSERT(0 != object);
1795 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1796 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
1798 old = object->data.picture.data;
1800 /* do the copy first so that if we fail we leave the object untouched */
1802 if(!copy_bytes_(&object->data.picture.data, data, length))
1806 object->data.picture.data = data;
1812 object->length -= object->data.picture.data_length;
1813 object->data.picture.data_length = length;
1814 object->length += length;
1818 FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1820 FLAC__ASSERT(0 != object);
1821 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1823 return FLAC__format_picture_is_legal(&object->data.picture, violation);