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 ***************************************************************************/
205 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
207 FLAC__StreamMetadata *object = malloc(sizeof(FLAC__StreamMetadata));
209 memset(object, 0, sizeof(FLAC__StreamMetadata));
210 object->is_last = false;
213 case FLAC__METADATA_TYPE_STREAMINFO:
214 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
216 case FLAC__METADATA_TYPE_PADDING:
218 case FLAC__METADATA_TYPE_APPLICATION:
219 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
221 case FLAC__METADATA_TYPE_SEEKTABLE:
223 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
224 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
227 /* double protection: */
237 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
239 FLAC__StreamMetadata *to;
241 FLAC__ASSERT(0 != object);
243 if(0 != (to = FLAC__metadata_object_new(object->type))) {
244 to->is_last = object->is_last;
245 to->type = object->type;
246 to->length = object->length;
248 case FLAC__METADATA_TYPE_STREAMINFO:
249 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
251 case FLAC__METADATA_TYPE_PADDING:
253 case FLAC__METADATA_TYPE_APPLICATION:
254 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
255 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
256 FLAC__metadata_object_delete(to);
260 case FLAC__METADATA_TYPE_SEEKTABLE:
261 to->data.seek_table.num_points = object->data.seek_table.num_points;
262 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))) {
263 FLAC__metadata_object_delete(to);
267 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
268 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
269 FLAC__metadata_object_delete(to);
272 if(object->data.vorbis_comment.num_comments == 0) {
273 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
274 to->data.vorbis_comment.comments = 0;
277 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
278 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
279 if(0 == to->data.vorbis_comment.comments) {
280 FLAC__metadata_object_delete(to);
284 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
287 /* double protection: */
297 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
299 FLAC__ASSERT(0 != object);
301 switch(object->type) {
302 case FLAC__METADATA_TYPE_STREAMINFO:
303 case FLAC__METADATA_TYPE_PADDING:
305 case FLAC__METADATA_TYPE_APPLICATION:
306 if(0 != object->data.application.data)
307 free(object->data.application.data);
309 case FLAC__METADATA_TYPE_SEEKTABLE:
310 if(0 != object->data.seek_table.points)
311 free(object->data.seek_table.points);
313 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
314 if(0 != object->data.vorbis_comment.vendor_string.entry)
315 free(object->data.vorbis_comment.vendor_string.entry);
316 if(0 != object->data.vorbis_comment.comments) {
317 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
318 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
326 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
328 FLAC__metadata_object_delete_data(object);
332 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
334 if(block1->min_blocksize != block2->min_blocksize)
336 if(block1->max_blocksize != block2->max_blocksize)
338 if(block1->min_framesize != block2->min_framesize)
340 if(block1->max_framesize != block2->max_framesize)
342 if(block1->sample_rate != block2->sample_rate)
344 if(block1->channels != block2->channels)
346 if(block1->bits_per_sample != block2->bits_per_sample)
348 if(block1->total_samples != block2->total_samples)
350 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
355 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
357 FLAC__ASSERT(0 != block1);
358 FLAC__ASSERT(0 != block2);
359 FLAC__ASSERT(block_length >= sizeof(block1->id));
361 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
363 if(0 != block1->data && 0 != block2->data)
364 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
366 return block1->data == block2->data;
369 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
373 FLAC__ASSERT(0 != block1);
374 FLAC__ASSERT(0 != block2);
376 if(block1->num_points != block2->num_points)
379 if(0 != block1->points && 0 != block2->points) {
380 for(i = 0; i < block1->num_points; i++) {
381 if(block1->points[i].sample_number != block2->points[i].sample_number)
383 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
385 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
391 return block1->points == block2->points;
394 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
398 if(block1->vendor_string.length != block2->vendor_string.length)
401 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
402 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
405 else if(block1->vendor_string.entry != block2->vendor_string.entry)
408 if(block1->num_comments != block2->num_comments)
411 for(i = 0; i < block1->num_comments; i++) {
412 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
413 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
416 else if(block1->comments[i].entry != block2->comments[i].entry)
422 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
424 FLAC__ASSERT(0 != block1);
425 FLAC__ASSERT(0 != block2);
427 if(block1->type != block2->type) {
430 if(block1->is_last != block2->is_last) {
433 if(block1->length != block2->length) {
436 switch(block1->type) {
437 case FLAC__METADATA_TYPE_STREAMINFO:
438 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
439 case FLAC__METADATA_TYPE_PADDING:
440 return true; /* we don't compare the padding guts */
441 case FLAC__METADATA_TYPE_APPLICATION:
442 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
443 case FLAC__METADATA_TYPE_SEEKTABLE:
444 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
445 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
446 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
453 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
457 FLAC__ASSERT(0 != object);
458 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
459 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
461 save = object->data.application.data;
463 /* do the copy first so that if we fail we leave the object untouched */
465 if(!copy_bytes_(&object->data.application.data, data, length))
469 object->data.application.data = data;
475 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
479 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
481 FLAC__ASSERT(0 != object);
482 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
484 if(0 == object->data.seek_table.points) {
485 FLAC__ASSERT(object->data.seek_table.num_points == 0);
486 if(0 == new_num_points)
488 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
492 const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
493 const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
495 FLAC__ASSERT(object->data.seek_table.num_points > 0);
498 free(object->data.seek_table.points);
499 object->data.seek_table.points = 0;
501 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
504 /* if growing, set new elements to placeholders */
505 if(new_size > old_size) {
507 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
508 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
509 object->data.seek_table.points[i].stream_offset = 0;
510 object->data.seek_table.points[i].frame_samples = 0;
515 object->data.seek_table.num_points = new_num_points;
517 seektable_calculate_length_(object);
521 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
523 FLAC__ASSERT(0 != object);
524 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
525 FLAC__ASSERT(object->data.seek_table.num_points > point_num);
527 object->data.seek_table.points[point_num] = point;
530 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
534 FLAC__ASSERT(0 != object);
535 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
536 FLAC__ASSERT(object->data.seek_table.num_points >= point_num);
538 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
541 /* move all points >= point_num forward one space */
542 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
543 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
545 FLAC__metadata_object_seektable_set_point(object, point_num, point);
546 seektable_calculate_length_(object);
550 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
554 FLAC__ASSERT(0 != object);
555 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
556 FLAC__ASSERT(object->data.seek_table.num_points > point_num);
558 /* move all points > point_num backward one space */
559 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
560 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
562 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
565 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
567 FLAC__ASSERT(0 != object);
568 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
570 return FLAC__format_seektable_is_legal(&object->data.seek_table);
573 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
575 FLAC__ASSERT(0 != object);
576 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
579 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
580 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
585 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
587 FLAC__StreamMetadata_SeekTable *seek_table;
589 FLAC__ASSERT(0 != object);
590 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
592 seek_table = &object->data.seek_table;
594 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
597 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
598 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
599 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
604 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
606 FLAC__ASSERT(0 != object);
607 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
608 FLAC__ASSERT(0 != sample_numbers || num == 0);
611 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
614 i = seek_table->num_points;
616 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
619 for(j = 0; j < num; i++, j++) {
620 seek_table->points[i].sample_number = sample_numbers[j];
621 seek_table->points[i].stream_offset = 0;
622 seek_table->points[i].frame_samples = 0;
629 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
631 FLAC__ASSERT(0 != object);
632 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
633 FLAC__ASSERT(total_samples > 0);
636 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
639 i = seek_table->num_points;
641 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
644 for(j = 0; j < num; i++, j++) {
645 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
646 seek_table->points[i].stream_offset = 0;
647 seek_table->points[i].frame_samples = 0;
654 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
658 FLAC__ASSERT(0 != object);
659 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
661 unique = FLAC__format_seektable_sort(&object->data.seek_table);
663 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
666 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
668 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
671 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
673 FLAC__ASSERT(0 != object);
674 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
676 if(0 == object->data.vorbis_comment.comments) {
677 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
678 if(0 == new_num_comments)
680 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
684 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
685 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
687 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
689 /* if shrinking, free the truncated entries */
690 if(new_num_comments < object->data.vorbis_comment.num_comments) {
692 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
693 if(0 != object->data.vorbis_comment.comments[i].entry)
694 free(object->data.vorbis_comment.comments[i].entry);
698 free(object->data.vorbis_comment.comments);
699 object->data.vorbis_comment.comments = 0;
701 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
704 /* if growing, zero all the length/pointers of new elements */
705 if(new_size > old_size)
706 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
709 object->data.vorbis_comment.num_comments = new_num_comments;
711 vorbiscomment_calculate_length_(object);
715 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
717 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
720 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
722 FLAC__StreamMetadata_VorbisComment *vc;
724 FLAC__ASSERT(0 != object);
725 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
726 FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num);
728 vc = &object->data.vorbis_comment;
730 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
733 /* move all comments >= comment_num forward one space */
734 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
735 vc->comments[comment_num].length = 0;
736 vc->comments[comment_num].entry = 0;
738 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
741 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
743 FLAC__StreamMetadata_VorbisComment *vc;
745 FLAC__ASSERT(0 != object);
746 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
747 FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num);
749 vc = &object->data.vorbis_comment;
751 /* free the comment at comment_num */
752 if(0 != vc->comments[comment_num].entry)
753 free(vc->comments[comment_num].entry);
755 /* move all comments > comment_num backward one space */
756 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
757 vc->comments[vc->num_comments-1].length = 0;
758 vc->comments[vc->num_comments-1].entry = 0;
760 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);