1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "private/metadata.h"
25 #include "FLAC/assert.h"
28 /****************************************************************************
32 ***************************************************************************/
34 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
36 if(bytes > 0 && 0 != from) {
38 if(0 == (x = malloc(bytes)))
40 memcpy(x, from, bytes);
44 FLAC__ASSERT(0 == from);
45 FLAC__ASSERT(bytes == 0);
51 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
53 to->length = from->length;
54 if(0 == from->entry) {
55 FLAC__ASSERT(from->length == 0);
60 FLAC__ASSERT(from->length > 0);
61 if(0 == (x = malloc(from->length)))
63 memcpy(x, from->entry, from->length);
69 static void seektable_calculate_length_(FLAC__StreamMetadata *object)
71 FLAC__ASSERT(0 != object);
72 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
74 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
77 static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
79 FLAC__StreamMetadata_SeekPoint *object_array;
81 FLAC__ASSERT(num_points > 0);
83 object_array = malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
85 if(0 != object_array) {
87 for(i = 0; i < num_points; i++) {
88 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
89 object_array[i].stream_offset = 0;
90 object_array[i].frame_samples = 0;
97 static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
101 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
103 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
104 object->length += object->data.vorbis_comment.vendor_string.length;
105 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
106 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
107 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
108 object->length += object->data.vorbis_comment.comments[i].length;
112 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
114 FLAC__StreamMetadata_VorbisComment_Entry *object_array;
116 FLAC__ASSERT(num_comments > 0);
118 object_array = malloc(num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
120 if(0 != object_array)
121 memset(object_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
126 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
130 FLAC__ASSERT(0 != object_array && num_comments > 0);
132 for(i = 0; i < num_comments; i++)
133 if(0 != object_array[i].entry)
134 free(object_array[i].entry);
136 if(0 != object_array)
140 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
142 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
144 FLAC__ASSERT(0 != object_array);
145 FLAC__ASSERT(num_comments > 0);
147 return_array = vorbiscomment_entry_array_new_(num_comments);
149 if(0 != return_array) {
152 /* Need to do this to set the pointers inside the comments to 0.
153 * In case of an error in the following loop, the object will be
154 * deleted and we don't want the destructor freeing uninitialized
157 memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
159 for(i = 0; i < num_comments; i++) {
160 if(!copy_vcentry_(return_array+i, object_array+i)) {
161 vorbiscomment_entry_array_delete_(return_array, num_comments);
170 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
174 FLAC__ASSERT(0 != object);
175 FLAC__ASSERT(0 != dest);
176 FLAC__ASSERT(0 != src);
177 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
178 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0 && copy == false));
182 /* do the copy first so that if we fail we leave the object untouched */
184 if(!copy_vcentry_(dest, src))
194 vorbiscomment_calculate_length_(object);
199 /****************************************************************************
201 * Metadata object routines
203 ***************************************************************************/
206 will return pointer to new empty object of type 'type', or 0 if malloc failed
209 FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
211 FLAC__StreamMetadata *object = malloc(sizeof(FLAC__StreamMetadata));
213 memset(object, 0, sizeof(FLAC__StreamMetadata));
214 object->is_last = false;
217 case FLAC__METADATA_TYPE_STREAMINFO:
218 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
220 case FLAC__METADATA_TYPE_PADDING:
222 case FLAC__METADATA_TYPE_APPLICATION:
223 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
225 case FLAC__METADATA_TYPE_SEEKTABLE:
227 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
228 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
231 /* double protection: */
242 return a pointer to a copy of 'object', or 0 if any malloc failed. does a deep copy. user gets ownership of object.
243 FLAC__ASSERT(0 != object);
245 FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
247 FLAC__StreamMetadata *to;
249 FLAC__ASSERT(0 != object);
251 if(0 != (to = FLAC__metadata_object_new(object->type))) {
252 to->is_last = object->is_last;
253 to->type = object->type;
254 to->length = object->length;
256 case FLAC__METADATA_TYPE_STREAMINFO:
257 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
259 case FLAC__METADATA_TYPE_PADDING:
261 case FLAC__METADATA_TYPE_APPLICATION:
262 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
263 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
264 FLAC__metadata_object_delete(to);
268 case FLAC__METADATA_TYPE_SEEKTABLE:
269 to->data.seek_table.num_points = object->data.seek_table.num_points;
270 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))) {
271 FLAC__metadata_object_delete(to);
275 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
276 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
277 FLAC__metadata_object_delete(to);
280 if(object->data.vorbis_comment.num_comments == 0) {
281 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
282 to->data.vorbis_comment.comments = 0;
285 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
286 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
287 if(0 == to->data.vorbis_comment.comments) {
288 FLAC__metadata_object_delete(to);
292 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
295 /* double protection: */
305 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
307 FLAC__ASSERT(0 != object);
309 switch(object->type) {
310 case FLAC__METADATA_TYPE_STREAMINFO:
311 case FLAC__METADATA_TYPE_PADDING:
313 case FLAC__METADATA_TYPE_APPLICATION:
314 if(0 != object->data.application.data)
315 free(object->data.application.data);
317 case FLAC__METADATA_TYPE_SEEKTABLE:
318 if(0 != object->data.seek_table.points)
319 free(object->data.seek_table.points);
321 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
322 if(0 != object->data.vorbis_comment.vendor_string.entry)
323 free(object->data.vorbis_comment.vendor_string.entry);
324 if(0 != object->data.vorbis_comment.comments) {
325 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
326 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
335 frees 'object'. does a deep delete.
337 void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
339 FLAC__metadata_object_delete_data(object);
343 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
345 if(block1->min_blocksize != block2->min_blocksize)
347 if(block1->max_blocksize != block2->max_blocksize)
349 if(block1->min_framesize != block2->min_framesize)
351 if(block1->max_framesize != block2->max_framesize)
353 if(block1->sample_rate != block2->sample_rate)
355 if(block1->channels != block2->channels)
357 if(block1->bits_per_sample != block2->bits_per_sample)
359 if(block1->total_samples != block2->total_samples)
361 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
366 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
368 FLAC__ASSERT(0 != block1);
369 FLAC__ASSERT(0 != block2);
370 FLAC__ASSERT(block_length >= sizeof(block1->id));
372 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
374 if(0 != block1->data && 0 != block2->data)
375 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
377 return block1->data == block2->data;
380 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
384 FLAC__ASSERT(0 != block1);
385 FLAC__ASSERT(0 != block2);
387 if(block1->num_points != block2->num_points)
390 if(0 != block1->points && 0 != block2->points) {
391 for(i = 0; i < block1->num_points; i++) {
392 if(block1->points[i].sample_number != block2->points[i].sample_number)
394 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
396 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
402 return block1->points == block2->points;
405 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
409 if(block1->vendor_string.length != block2->vendor_string.length)
412 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
413 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
416 else if(block1->vendor_string.entry != block2->vendor_string.entry)
419 if(block1->num_comments != block2->num_comments)
422 for(i = 0; i < block1->num_comments; i++) {
423 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
424 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
427 else if(block1->comments[i].entry != block2->comments[i].entry)
433 FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
435 if(block1->type != block2->type) {
438 if(block1->is_last != block2->is_last) {
441 if(block1->length != block2->length) {
444 switch(block1->type) {
445 case FLAC__METADATA_TYPE_STREAMINFO:
446 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
447 case FLAC__METADATA_TYPE_PADDING:
448 return true; /* we don't compare the padding guts */
449 case FLAC__METADATA_TYPE_APPLICATION:
450 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
451 case FLAC__METADATA_TYPE_SEEKTABLE:
452 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
453 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
454 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
462 sets the application data to 'data'. if 'copy' is true, makes, copy, else takes ownership of pointer. returns false if copy==true and malloc fails.
463 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
464 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
466 FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
470 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
471 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
473 save = object->data.application.data;
475 /* do the copy first so that if we fail we leave the object untouched */
477 if(!copy_bytes_(&object->data.application.data, data, length))
481 object->data.application.data = data;
487 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
491 FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
493 FLAC__ASSERT(0 != object);
494 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
496 if(0 == object->data.seek_table.points) {
497 FLAC__ASSERT(object->data.seek_table.num_points == 0);
498 if(0 == new_num_points)
500 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
504 const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
505 const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
507 FLAC__ASSERT(object->data.seek_table.num_points > 0);
510 free(object->data.seek_table.points);
511 object->data.seek_table.points = 0;
513 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
516 /* if growing, set new elements to placeholders */
517 if(new_size > old_size) {
519 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
520 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
521 object->data.seek_table.points[i].stream_offset = 0;
522 object->data.seek_table.points[i].frame_samples = 0;
527 object->data.seek_table.num_points = new_num_points;
529 seektable_calculate_length_(object);
533 void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
535 FLAC__ASSERT(0 != object);
536 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
537 FLAC__ASSERT(object->data.seek_table.num_points > point_num);
539 object->data.seek_table.points[point_num] = point;
542 FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
546 FLAC__ASSERT(0 != object);
547 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
548 FLAC__ASSERT(object->data.seek_table.num_points >= point_num);
550 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
553 /* move all points >= point_num forward one space */
554 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
555 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
557 FLAC__metadata_object_seektable_set_point(object, point_num, point);
558 seektable_calculate_length_(object);
562 FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
566 FLAC__ASSERT(0 != object);
567 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
568 FLAC__ASSERT(object->data.seek_table.num_points > point_num);
570 /* move all points > point_num backward one space */
571 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
572 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
574 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
577 FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
580 FLAC__uint64 prev_sample_number = 0;
581 FLAC__bool got_prev = false;
583 FLAC__ASSERT(0 != object);
584 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
587 const FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
589 for(i = 0; i < seek_table->num_points; i++) {
591 if(seek_table->points[i].sample_number <= prev_sample_number)
594 prev_sample_number = seek_table->points[i].sample_number;
602 FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
604 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
607 FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
609 FLAC__ASSERT(0 != object);
610 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
612 if(0 == object->data.vorbis_comment.comments) {
613 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
614 if(0 == new_num_comments)
616 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
620 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
621 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
623 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
625 /* if shrinking, free the truncated entries */
626 if(new_num_comments < object->data.vorbis_comment.num_comments) {
628 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
629 if(0 != object->data.vorbis_comment.comments[i].entry)
630 free(object->data.vorbis_comment.comments[i].entry);
634 free(object->data.vorbis_comment.comments);
635 object->data.vorbis_comment.comments = 0;
637 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
640 /* if growing, zero all the length/pointers of new elements */
641 if(new_size > old_size)
642 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
645 object->data.vorbis_comment.num_comments = new_num_comments;
647 vorbiscomment_calculate_length_(object);
651 FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
653 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
656 FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
658 FLAC__StreamMetadata_VorbisComment *vc;
660 FLAC__ASSERT(0 != object);
661 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
662 FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num);
664 vc = &object->data.vorbis_comment;
666 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
669 /* move all comments >= comment_num forward one space */
670 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
671 vc->comments[comment_num].length = 0;
672 vc->comments[comment_num].entry = 0;
674 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
677 FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
679 FLAC__StreamMetadata_VorbisComment *vc;
681 FLAC__ASSERT(0 != object);
682 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
683 FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num);
685 vc = &object->data.vorbis_comment;
687 /* free the comment at comment_num */
688 if(0 != vc->comments[comment_num].entry)
689 free(vc->comments[comment_num].entry);
691 /* move all comments > comment_num backward one space */
692 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
693 vc->comments[vc->num_comments-1].length = 0;
694 vc->comments[vc->num_comments-1].entry = 0;
696 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);