1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002,2003,2004,2005,2006 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"
41 #include "FLAC/assert.h"
44 /****************************************************************************
48 ***************************************************************************/
50 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
52 if(bytes > 0 && 0 != from) {
54 if(0 == (x = (FLAC__byte*)malloc(bytes)))
56 memcpy(x, from, bytes);
60 FLAC__ASSERT(0 == from);
61 FLAC__ASSERT(bytes == 0);
67 static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
69 FLAC__byte *x = (FLAC__byte*)realloc(*entry, length+1);
79 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
81 to->length = from->length;
82 if(0 == from->entry) {
83 FLAC__ASSERT(from->length == 0);
88 FLAC__ASSERT(from->length > 0);
89 if(0 == (x = (FLAC__byte*)malloc(from->length+1)))
91 memcpy(x, from->entry, from->length);
92 x[from->length] = '\0';
98 static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
100 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
101 if(0 == from->indices) {
102 FLAC__ASSERT(from->num_indices == 0);
105 FLAC__StreamMetadata_CueSheet_Index *x;
106 FLAC__ASSERT(from->num_indices > 0);
107 if(0 == (x = (FLAC__StreamMetadata_CueSheet_Index*)malloc(from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index))))
109 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
115 static void seektable_calculate_length_(FLAC__StreamMetadata *object)
117 FLAC__ASSERT(0 != object);
118 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
120 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
123 static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
125 FLAC__StreamMetadata_SeekPoint *object_array;
127 FLAC__ASSERT(num_points > 0);
129 object_array = (FLAC__StreamMetadata_SeekPoint*)malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
131 if(0 != object_array) {
133 for(i = 0; i < num_points; i++) {
134 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
135 object_array[i].stream_offset = 0;
136 object_array[i].frame_samples = 0;
143 static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
147 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
149 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
150 object->length += object->data.vorbis_comment.vendor_string.length;
151 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
152 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
153 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
154 object->length += object->data.vorbis_comment.comments[i].length;
158 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
160 FLAC__ASSERT(num_comments > 0);
162 return (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
165 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
169 FLAC__ASSERT(0 != object_array && num_comments > 0);
171 for(i = 0; i < num_comments; i++)
172 if(0 != object_array[i].entry)
173 free(object_array[i].entry);
175 if(0 != object_array)
179 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
181 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
183 FLAC__ASSERT(0 != object_array);
184 FLAC__ASSERT(num_comments > 0);
186 return_array = vorbiscomment_entry_array_new_(num_comments);
188 if(0 != return_array) {
191 for(i = 0; i < num_comments; i++) {
192 if(!copy_vcentry_(return_array+i, object_array+i)) {
193 vorbiscomment_entry_array_delete_(return_array, num_comments);
202 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
206 FLAC__ASSERT(0 != object);
207 FLAC__ASSERT(0 != dest);
208 FLAC__ASSERT(0 != src);
209 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
210 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
214 if(0 != src->entry && src->length > 0) {
216 /* do the copy first so that if we fail we leave the dest object untouched */
217 if(!copy_vcentry_(dest, src))
221 /* we have to make sure that the string we're taking over is null-terminated */
224 * Stripping the const from src->entry is OK since we're taking
225 * ownership of the pointer. This is a hack around a deficiency
226 * in the API where the same function is used for 'copy' and
227 * 'own', but the source entry is a const pointer. If we were
228 * precise, the 'own' flavor would be a separate function with a
229 * non-const source pointer. But it's not, so we hack away.
231 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
237 /* the src is null */
244 vorbiscomment_calculate_length_(object);
248 static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
252 FLAC__ASSERT(0 != object);
253 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
254 FLAC__ASSERT(0 != field_name);
256 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
257 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
264 static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
268 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
271 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
272 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
273 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
274 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
275 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
278 object->length += object->data.cue_sheet.num_tracks * (
279 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
280 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
281 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
282 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
283 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
284 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
285 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
288 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
289 object->length += object->data.cue_sheet.tracks[i].num_indices * (
290 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
291 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
292 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
297 static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
299 FLAC__ASSERT(num_indices > 0);
301 return (FLAC__StreamMetadata_CueSheet_Index*)calloc(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
304 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
306 FLAC__ASSERT(num_tracks > 0);
308 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
311 static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
315 FLAC__ASSERT(0 != object_array && num_tracks > 0);
317 for(i = 0; i < num_tracks; i++) {
318 if(0 != object_array[i].indices) {
319 FLAC__ASSERT(object_array[i].num_indices > 0);
320 free(object_array[i].indices);
324 if(0 != object_array)
328 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
330 FLAC__StreamMetadata_CueSheet_Track *return_array;
332 FLAC__ASSERT(0 != object_array);
333 FLAC__ASSERT(num_tracks > 0);
335 return_array = cuesheet_track_array_new_(num_tracks);
337 if(0 != return_array) {
340 for(i = 0; i < num_tracks; i++) {
341 if(!copy_track_(return_array+i, object_array+i)) {
342 cuesheet_track_array_delete_(return_array, num_tracks);
351 static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
353 FLAC__StreamMetadata_CueSheet_Index *save;
355 FLAC__ASSERT(0 != object);
356 FLAC__ASSERT(0 != dest);
357 FLAC__ASSERT(0 != src);
358 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
359 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
361 save = dest->indices;
363 /* do the copy first so that if we fail we leave the object untouched */
365 if(!copy_track_(dest, src))
375 cuesheet_calculate_length_(object);
380 /****************************************************************************
382 * Metadata object routines
384 ***************************************************************************/
386 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
388 FLAC__StreamMetadata *object;
390 if(type > FLAC__MAX_METADATA_TYPE_CODE)
393 object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata));
395 object->is_last = false;
398 case FLAC__METADATA_TYPE_STREAMINFO:
399 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
401 case FLAC__METADATA_TYPE_PADDING:
402 /* calloc() took care of this for us:
406 case FLAC__METADATA_TYPE_APPLICATION:
407 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
408 /* calloc() took care of this for us:
409 object->data.application.data = 0;
412 case FLAC__METADATA_TYPE_SEEKTABLE:
413 /* calloc() took care of this for us:
415 object->data.seek_table.num_points = 0;
416 object->data.seek_table.points = 0;
419 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
421 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
422 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
426 vorbiscomment_calculate_length_(object);
429 case FLAC__METADATA_TYPE_CUESHEET:
430 cuesheet_calculate_length_(object);
433 /* calloc() took care of this for us:
435 object->data.unknown.data = 0;
444 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
446 FLAC__StreamMetadata *to;
448 FLAC__ASSERT(0 != object);
450 if(0 != (to = FLAC__metadata_object_new(object->type))) {
451 to->is_last = object->is_last;
452 to->type = object->type;
453 to->length = object->length;
455 case FLAC__METADATA_TYPE_STREAMINFO:
456 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
458 case FLAC__METADATA_TYPE_PADDING:
460 case FLAC__METADATA_TYPE_APPLICATION:
461 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
462 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
463 FLAC__metadata_object_delete(to);
467 case FLAC__METADATA_TYPE_SEEKTABLE:
468 to->data.seek_table.num_points = object->data.seek_table.num_points;
469 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))) {
470 FLAC__metadata_object_delete(to);
474 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
475 if(0 != to->data.vorbis_comment.vendor_string.entry) {
476 free(to->data.vorbis_comment.vendor_string.entry);
477 to->data.vorbis_comment.vendor_string.entry = 0;
479 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
480 FLAC__metadata_object_delete(to);
483 if(object->data.vorbis_comment.num_comments == 0) {
484 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
485 to->data.vorbis_comment.comments = 0;
488 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
489 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
490 if(0 == to->data.vorbis_comment.comments) {
491 FLAC__metadata_object_delete(to);
495 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
497 case FLAC__METADATA_TYPE_CUESHEET:
498 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
499 if(object->data.cue_sheet.num_tracks == 0) {
500 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
503 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
504 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
505 if(0 == to->data.cue_sheet.tracks) {
506 FLAC__metadata_object_delete(to);
512 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
513 FLAC__metadata_object_delete(to);
523 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
525 FLAC__ASSERT(0 != object);
527 switch(object->type) {
528 case FLAC__METADATA_TYPE_STREAMINFO:
529 case FLAC__METADATA_TYPE_PADDING:
531 case FLAC__METADATA_TYPE_APPLICATION:
532 if(0 != object->data.application.data) {
533 free(object->data.application.data);
534 object->data.application.data = 0;
537 case FLAC__METADATA_TYPE_SEEKTABLE:
538 if(0 != object->data.seek_table.points) {
539 free(object->data.seek_table.points);
540 object->data.seek_table.points = 0;
543 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
544 if(0 != object->data.vorbis_comment.vendor_string.entry) {
545 free(object->data.vorbis_comment.vendor_string.entry);
546 object->data.vorbis_comment.vendor_string.entry = 0;
548 if(0 != object->data.vorbis_comment.comments) {
549 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
550 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
553 case FLAC__METADATA_TYPE_CUESHEET:
554 if(0 != object->data.cue_sheet.tracks) {
555 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
556 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
560 if(0 != object->data.unknown.data) {
561 free(object->data.unknown.data);
562 object->data.unknown.data = 0;
568 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
570 FLAC__metadata_object_delete_data(object);
574 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
576 if(block1->min_blocksize != block2->min_blocksize)
578 if(block1->max_blocksize != block2->max_blocksize)
580 if(block1->min_framesize != block2->min_framesize)
582 if(block1->max_framesize != block2->max_framesize)
584 if(block1->sample_rate != block2->sample_rate)
586 if(block1->channels != block2->channels)
588 if(block1->bits_per_sample != block2->bits_per_sample)
590 if(block1->total_samples != block2->total_samples)
592 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
597 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
599 FLAC__ASSERT(0 != block1);
600 FLAC__ASSERT(0 != block2);
601 FLAC__ASSERT(block_length >= sizeof(block1->id));
603 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
605 if(0 != block1->data && 0 != block2->data)
606 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
608 return block1->data == block2->data;
611 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
615 FLAC__ASSERT(0 != block1);
616 FLAC__ASSERT(0 != block2);
618 if(block1->num_points != block2->num_points)
621 if(0 != block1->points && 0 != block2->points) {
622 for(i = 0; i < block1->num_points; i++) {
623 if(block1->points[i].sample_number != block2->points[i].sample_number)
625 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
627 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
633 return block1->points == block2->points;
636 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
640 if(block1->vendor_string.length != block2->vendor_string.length)
643 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
644 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
647 else if(block1->vendor_string.entry != block2->vendor_string.entry)
650 if(block1->num_comments != block2->num_comments)
653 for(i = 0; i < block1->num_comments; i++) {
654 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
655 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
658 else if(block1->comments[i].entry != block2->comments[i].entry)
664 static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
668 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
671 if(block1->lead_in != block2->lead_in)
674 if(block1->is_cd != block2->is_cd)
677 if(block1->num_tracks != block2->num_tracks)
680 if(0 != block1->tracks && 0 != block2->tracks) {
681 FLAC__ASSERT(block1->num_tracks > 0);
682 for(i = 0; i < block1->num_tracks; i++) {
683 if(block1->tracks[i].offset != block2->tracks[i].offset)
685 if(block1->tracks[i].number != block2->tracks[i].number)
687 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
689 if(block1->tracks[i].type != block2->tracks[i].type)
691 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
693 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
695 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
696 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
697 for(j = 0; j < block1->tracks[i].num_indices; j++) {
698 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
700 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
704 else if(block1->tracks[i].indices != block2->tracks[i].indices)
708 else if(block1->tracks != block2->tracks)
713 static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
715 FLAC__ASSERT(0 != block1);
716 FLAC__ASSERT(0 != block2);
718 if(0 != block1->data && 0 != block2->data)
719 return 0 == memcmp(block1->data, block2->data, block_length);
721 return block1->data == block2->data;
724 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
726 FLAC__ASSERT(0 != block1);
727 FLAC__ASSERT(0 != block2);
729 if(block1->type != block2->type) {
732 if(block1->is_last != block2->is_last) {
735 if(block1->length != block2->length) {
738 switch(block1->type) {
739 case FLAC__METADATA_TYPE_STREAMINFO:
740 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
741 case FLAC__METADATA_TYPE_PADDING:
742 return true; /* we don't compare the padding guts */
743 case FLAC__METADATA_TYPE_APPLICATION:
744 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
745 case FLAC__METADATA_TYPE_SEEKTABLE:
746 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
747 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
748 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
749 case FLAC__METADATA_TYPE_CUESHEET:
750 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
752 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
756 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
760 FLAC__ASSERT(0 != object);
761 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
762 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
764 save = object->data.application.data;
766 /* do the copy first so that if we fail we leave the object untouched */
768 if(!copy_bytes_(&object->data.application.data, data, length))
772 object->data.application.data = data;
778 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
782 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
784 FLAC__ASSERT(0 != object);
785 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
787 if(0 == object->data.seek_table.points) {
788 FLAC__ASSERT(object->data.seek_table.num_points == 0);
789 if(0 == new_num_points)
791 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
795 const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
796 const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
798 FLAC__ASSERT(object->data.seek_table.num_points > 0);
801 free(object->data.seek_table.points);
802 object->data.seek_table.points = 0;
804 else if(0 == (object->data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(object->data.seek_table.points, new_size)))
807 /* if growing, set new elements to placeholders */
808 if(new_size > old_size) {
810 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
811 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
812 object->data.seek_table.points[i].stream_offset = 0;
813 object->data.seek_table.points[i].frame_samples = 0;
818 object->data.seek_table.num_points = new_num_points;
820 seektable_calculate_length_(object);
824 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
826 FLAC__ASSERT(0 != object);
827 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
828 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
830 object->data.seek_table.points[point_num] = point;
833 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
837 FLAC__ASSERT(0 != object);
838 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
839 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
841 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
844 /* move all points >= point_num forward one space */
845 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
846 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
848 FLAC__metadata_object_seektable_set_point(object, point_num, point);
849 seektable_calculate_length_(object);
853 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
857 FLAC__ASSERT(0 != object);
858 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
859 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
861 /* move all points > point_num backward one space */
862 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
863 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
865 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
868 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
870 FLAC__ASSERT(0 != object);
871 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
873 return FLAC__format_seektable_is_legal(&object->data.seek_table);
876 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
878 FLAC__ASSERT(0 != object);
879 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
882 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
883 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
888 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
890 FLAC__StreamMetadata_SeekTable *seek_table;
892 FLAC__ASSERT(0 != object);
893 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
895 seek_table = &object->data.seek_table;
897 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
900 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
901 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
902 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
907 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
909 FLAC__ASSERT(0 != object);
910 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
911 FLAC__ASSERT(0 != sample_numbers || num == 0);
914 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
917 i = seek_table->num_points;
919 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
922 for(j = 0; j < num; i++, j++) {
923 seek_table->points[i].sample_number = sample_numbers[j];
924 seek_table->points[i].stream_offset = 0;
925 seek_table->points[i].frame_samples = 0;
932 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
934 FLAC__ASSERT(0 != object);
935 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
936 FLAC__ASSERT(total_samples > 0);
938 if(num > 0 && total_samples > 0) {
939 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
942 i = seek_table->num_points;
944 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
947 for(j = 0; j < num; i++, j++) {
948 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
949 seek_table->points[i].stream_offset = 0;
950 seek_table->points[i].frame_samples = 0;
957 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
959 FLAC__ASSERT(0 != object);
960 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
961 FLAC__ASSERT(samples > 0);
962 FLAC__ASSERT(total_samples > 0);
964 if(samples > 0 && total_samples > 0) {
965 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
967 FLAC__uint64 num, sample;
969 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
970 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
971 if(total_samples % samples == 0)
974 i = seek_table->num_points;
976 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
980 for(j = 0; j < num; i++, j++, sample += samples) {
981 seek_table->points[i].sample_number = sample;
982 seek_table->points[i].stream_offset = 0;
983 seek_table->points[i].frame_samples = 0;
990 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
994 FLAC__ASSERT(0 != object);
995 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
997 unique = FLAC__format_seektable_sort(&object->data.seek_table);
999 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1002 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1004 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1006 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
1009 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
1011 FLAC__ASSERT(0 != object);
1012 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1014 if(0 == object->data.vorbis_comment.comments) {
1015 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1016 if(0 == new_num_comments)
1018 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1022 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1023 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1025 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1027 /* if shrinking, free the truncated entries */
1028 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1030 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1031 if(0 != object->data.vorbis_comment.comments[i].entry)
1032 free(object->data.vorbis_comment.comments[i].entry);
1036 free(object->data.vorbis_comment.comments);
1037 object->data.vorbis_comment.comments = 0;
1039 else if(0 == (object->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(object->data.vorbis_comment.comments, new_size)))
1042 /* if growing, zero all the length/pointers of new elements */
1043 if(new_size > old_size)
1044 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1047 object->data.vorbis_comment.num_comments = new_num_comments;
1049 vorbiscomment_calculate_length_(object);
1053 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1055 FLAC__ASSERT(0 != object);
1056 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1058 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1060 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
1063 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1065 FLAC__StreamMetadata_VorbisComment *vc;
1067 FLAC__ASSERT(0 != object);
1068 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1069 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
1071 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1074 vc = &object->data.vorbis_comment;
1076 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
1079 /* move all comments >= comment_num forward one space */
1080 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
1081 vc->comments[comment_num].length = 0;
1082 vc->comments[comment_num].entry = 0;
1084 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1087 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1089 FLAC__ASSERT(0 != object);
1090 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1091 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1094 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1096 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1098 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1103 unsigned field_name_length;
1104 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1106 FLAC__ASSERT(0 != eq);
1109 return false; /* double protection */
1111 field_name_length = eq-entry.entry;
1113 if((i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length)) >= 0) {
1114 unsigned index = (unsigned)i;
1115 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
1117 if(all && (index+1 < object->data.vorbis_comment.num_comments)) {
1118 for(i = vorbiscomment_find_entry_from_(object, index+1, (const char *)entry.entry, field_name_length); i >= 0; ) {
1119 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
1121 if((unsigned)i < object->data.vorbis_comment.num_comments)
1122 i = vorbiscomment_find_entry_from_(object, (unsigned)i, (const char *)entry.entry, field_name_length);
1130 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1134 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
1136 FLAC__StreamMetadata_VorbisComment *vc;
1138 FLAC__ASSERT(0 != object);
1139 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1140 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1142 vc = &object->data.vorbis_comment;
1144 /* free the comment at comment_num */
1145 if(0 != vc->comments[comment_num].entry)
1146 free(vc->comments[comment_num].entry);
1148 /* move all comments > comment_num backward one space */
1149 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
1150 vc->comments[vc->num_comments-1].length = 0;
1151 vc->comments[vc->num_comments-1].entry = 0;
1153 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
1156 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)
1158 FLAC__ASSERT(0 != entry);
1159 FLAC__ASSERT(0 != field_name);
1160 FLAC__ASSERT(0 != field_value);
1162 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1164 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
1168 const size_t nn = strlen(field_name);
1169 const size_t nv = strlen(field_value);
1170 entry->length = nn + 1 /*=*/ + nv;
1171 if(0 == (entry->entry = (FLAC__byte*)malloc(entry->length+1)))
1173 memcpy(entry->entry, field_name, nn);
1174 entry->entry[nn] = '=';
1175 memcpy(entry->entry+nn+1, field_value, nv);
1176 entry->entry[entry->length] = '\0';
1182 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)
1184 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1185 FLAC__ASSERT(0 != field_name);
1186 FLAC__ASSERT(0 != field_value);
1188 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1192 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1193 const size_t nn = eq-entry.entry;
1194 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1195 FLAC__ASSERT(0 != eq);
1197 return false; /* double protection */
1198 if(0 == (*field_name = (char*)malloc(nn+1)))
1200 if(0 == (*field_value = (char*)malloc(nv+1))) {
1204 memcpy(*field_name, entry.entry, nn);
1205 memcpy(*field_value, entry.entry+nn+1, nv);
1206 (*field_name)[nn] = '\0';
1207 (*field_value)[nv] = '\0';
1213 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
1215 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1217 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1218 #if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
1219 #define FLAC__STRNCASECMP strnicmp
1221 #define FLAC__STRNCASECMP strncasecmp
1223 return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
1224 #undef FLAC__STRNCASECMP
1228 FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
1230 FLAC__ASSERT(0 != field_name);
1232 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
1235 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1237 const unsigned field_name_length = strlen(field_name);
1240 FLAC__ASSERT(0 != object);
1241 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1243 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
1244 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1245 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1255 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1257 FLAC__bool ok = true;
1258 unsigned matching = 0;
1259 const unsigned field_name_length = strlen(field_name);
1262 FLAC__ASSERT(0 != object);
1263 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1265 /* must delete from end to start otherwise it will interfere with our iteration */
1266 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
1267 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1269 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1273 return ok? (int)matching : -1;
1276 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new()
1278 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1281 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1283 FLAC__StreamMetadata_CueSheet_Track *to;
1285 FLAC__ASSERT(0 != object);
1287 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1288 if(!copy_track_(to, object)) {
1289 FLAC__metadata_object_cuesheet_track_delete(to);
1297 void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1299 FLAC__ASSERT(0 != object);
1301 if(0 != object->indices) {
1302 FLAC__ASSERT(object->num_indices > 0);
1303 free(object->indices);
1307 FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1309 FLAC__metadata_object_cuesheet_track_delete_data(object);
1313 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1315 FLAC__StreamMetadata_CueSheet_Track *track;
1316 FLAC__ASSERT(0 != object);
1317 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1318 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1320 track = &object->data.cue_sheet.tracks[track_num];
1322 if(0 == track->indices) {
1323 FLAC__ASSERT(track->num_indices == 0);
1324 if(0 == new_num_indices)
1326 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1330 const unsigned old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1331 const unsigned new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1333 FLAC__ASSERT(track->num_indices > 0);
1336 free(track->indices);
1339 else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
1342 /* if growing, zero all the lengths/pointers of new elements */
1343 if(new_size > old_size)
1344 memset(track->indices + track->num_indices, 0, new_size - old_size);
1347 track->num_indices = new_num_indices;
1349 cuesheet_calculate_length_(object);
1353 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)
1355 FLAC__StreamMetadata_CueSheet_Track *track;
1357 FLAC__ASSERT(0 != object);
1358 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1359 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1360 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1362 track = &object->data.cue_sheet.tracks[track_num];
1364 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1367 /* move all indices >= index_num forward one space */
1368 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1370 track->indices[index_num] = index;
1371 cuesheet_calculate_length_(object);
1375 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1377 FLAC__StreamMetadata_CueSheet_Index index;
1378 memset(&index, 0, sizeof(index));
1379 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
1382 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1384 FLAC__StreamMetadata_CueSheet_Track *track;
1386 FLAC__ASSERT(0 != object);
1387 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1388 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1389 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1391 track = &object->data.cue_sheet.tracks[track_num];
1393 /* move all indices > index_num backward one space */
1394 memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
1396 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1397 cuesheet_calculate_length_(object);
1401 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1403 FLAC__ASSERT(0 != object);
1404 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1406 if(0 == object->data.cue_sheet.tracks) {
1407 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1408 if(0 == new_num_tracks)
1410 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1414 const unsigned old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1415 const unsigned new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1417 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1419 /* if shrinking, free the truncated entries */
1420 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1422 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1423 if(0 != object->data.cue_sheet.tracks[i].indices)
1424 free(object->data.cue_sheet.tracks[i].indices);
1428 free(object->data.cue_sheet.tracks);
1429 object->data.cue_sheet.tracks = 0;
1431 else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
1434 /* if growing, zero all the lengths/pointers of new elements */
1435 if(new_size > old_size)
1436 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1439 object->data.cue_sheet.num_tracks = new_num_tracks;
1441 cuesheet_calculate_length_(object);
1445 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1447 FLAC__ASSERT(0 != object);
1448 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1450 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
1453 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1455 FLAC__StreamMetadata_CueSheet *cs;
1457 FLAC__ASSERT(0 != object);
1458 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1459 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1461 cs = &object->data.cue_sheet;
1463 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1466 /* move all tracks >= track_num forward one space */
1467 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1468 cs->tracks[track_num].num_indices = 0;
1469 cs->tracks[track_num].indices = 0;
1471 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1474 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1476 FLAC__StreamMetadata_CueSheet_Track track;
1477 memset(&track, 0, sizeof(track));
1478 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1481 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1483 FLAC__StreamMetadata_CueSheet *cs;
1485 FLAC__ASSERT(0 != object);
1486 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1487 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1489 cs = &object->data.cue_sheet;
1491 /* free the track at track_num */
1492 if(0 != cs->tracks[track_num].indices)
1493 free(cs->tracks[track_num].indices);
1495 /* move all tracks > track_num backward one space */
1496 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1497 cs->tracks[cs->num_tracks-1].num_indices = 0;
1498 cs->tracks[cs->num_tracks-1].indices = 0;
1500 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1503 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1505 FLAC__ASSERT(0 != object);
1506 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1508 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1511 static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1513 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1515 else if (cs->tracks[track].indices[0].number == 1)
1516 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1517 else if (cs->tracks[track].num_indices < 2)
1519 else if (cs->tracks[track].indices[1].number == 1)
1520 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1525 static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1535 FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1537 const FLAC__StreamMetadata_CueSheet *cs;
1539 FLAC__ASSERT(0 != object);
1540 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1542 cs = &object->data.cue_sheet;
1544 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1548 FLAC__uint32 i, length, sum = 0;
1549 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1550 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1551 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1553 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);