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__ASSERT(num_comments > 0);
116 return calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
119 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
123 FLAC__ASSERT(0 != object_array && num_comments > 0);
125 for(i = 0; i < num_comments; i++)
126 if(0 != object_array[i].entry)
127 free(object_array[i].entry);
129 if(0 != object_array)
133 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
135 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
137 FLAC__ASSERT(0 != object_array);
138 FLAC__ASSERT(num_comments > 0);
140 return_array = vorbiscomment_entry_array_new_(num_comments);
142 if(0 != return_array) {
145 /* Need to do this to set the pointers inside the comments to 0.
146 * In case of an error in the following loop, the object will be
147 * deleted and we don't want the destructor freeing uninitialized
150 memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
152 for(i = 0; i < num_comments; i++) {
153 if(!copy_vcentry_(return_array+i, object_array+i)) {
154 vorbiscomment_entry_array_delete_(return_array, num_comments);
163 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
167 FLAC__ASSERT(0 != object);
168 FLAC__ASSERT(0 != dest);
169 FLAC__ASSERT(0 != src);
170 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
171 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0 && copy == false));
175 /* do the copy first so that if we fail we leave the object untouched */
177 if(!copy_vcentry_(dest, src))
187 vorbiscomment_calculate_length_(object);
192 /****************************************************************************
194 * Metadata object routines
196 ***************************************************************************/
198 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
200 FLAC__StreamMetadata *object = calloc(1, sizeof(FLAC__StreamMetadata));
202 object->is_last = false;
205 case FLAC__METADATA_TYPE_STREAMINFO:
206 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
208 case FLAC__METADATA_TYPE_PADDING:
210 case FLAC__METADATA_TYPE_APPLICATION:
211 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
213 case FLAC__METADATA_TYPE_SEEKTABLE:
215 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
216 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
219 /* double protection: */
229 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
231 FLAC__StreamMetadata *to;
233 FLAC__ASSERT(0 != object);
235 if(0 != (to = FLAC__metadata_object_new(object->type))) {
236 to->is_last = object->is_last;
237 to->type = object->type;
238 to->length = object->length;
240 case FLAC__METADATA_TYPE_STREAMINFO:
241 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
243 case FLAC__METADATA_TYPE_PADDING:
245 case FLAC__METADATA_TYPE_APPLICATION:
246 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
247 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
248 FLAC__metadata_object_delete(to);
252 case FLAC__METADATA_TYPE_SEEKTABLE:
253 to->data.seek_table.num_points = object->data.seek_table.num_points;
254 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))) {
255 FLAC__metadata_object_delete(to);
259 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
260 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
261 FLAC__metadata_object_delete(to);
264 if(object->data.vorbis_comment.num_comments == 0) {
265 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
266 to->data.vorbis_comment.comments = 0;
269 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
270 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
271 if(0 == to->data.vorbis_comment.comments) {
272 FLAC__metadata_object_delete(to);
276 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
279 /* double protection: */
289 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
291 FLAC__ASSERT(0 != object);
293 switch(object->type) {
294 case FLAC__METADATA_TYPE_STREAMINFO:
295 case FLAC__METADATA_TYPE_PADDING:
297 case FLAC__METADATA_TYPE_APPLICATION:
298 if(0 != object->data.application.data)
299 free(object->data.application.data);
301 case FLAC__METADATA_TYPE_SEEKTABLE:
302 if(0 != object->data.seek_table.points)
303 free(object->data.seek_table.points);
305 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
306 if(0 != object->data.vorbis_comment.vendor_string.entry)
307 free(object->data.vorbis_comment.vendor_string.entry);
308 if(0 != object->data.vorbis_comment.comments) {
309 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
310 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
318 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
320 FLAC__metadata_object_delete_data(object);
324 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
326 if(block1->min_blocksize != block2->min_blocksize)
328 if(block1->max_blocksize != block2->max_blocksize)
330 if(block1->min_framesize != block2->min_framesize)
332 if(block1->max_framesize != block2->max_framesize)
334 if(block1->sample_rate != block2->sample_rate)
336 if(block1->channels != block2->channels)
338 if(block1->bits_per_sample != block2->bits_per_sample)
340 if(block1->total_samples != block2->total_samples)
342 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
347 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
349 FLAC__ASSERT(0 != block1);
350 FLAC__ASSERT(0 != block2);
351 FLAC__ASSERT(block_length >= sizeof(block1->id));
353 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
355 if(0 != block1->data && 0 != block2->data)
356 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
358 return block1->data == block2->data;
361 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
365 FLAC__ASSERT(0 != block1);
366 FLAC__ASSERT(0 != block2);
368 if(block1->num_points != block2->num_points)
371 if(0 != block1->points && 0 != block2->points) {
372 for(i = 0; i < block1->num_points; i++) {
373 if(block1->points[i].sample_number != block2->points[i].sample_number)
375 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
377 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
383 return block1->points == block2->points;
386 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
390 if(block1->vendor_string.length != block2->vendor_string.length)
393 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
394 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
397 else if(block1->vendor_string.entry != block2->vendor_string.entry)
400 if(block1->num_comments != block2->num_comments)
403 for(i = 0; i < block1->num_comments; i++) {
404 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
405 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
408 else if(block1->comments[i].entry != block2->comments[i].entry)
414 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
416 FLAC__ASSERT(0 != block1);
417 FLAC__ASSERT(0 != block2);
419 if(block1->type != block2->type) {
422 if(block1->is_last != block2->is_last) {
425 if(block1->length != block2->length) {
428 switch(block1->type) {
429 case FLAC__METADATA_TYPE_STREAMINFO:
430 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
431 case FLAC__METADATA_TYPE_PADDING:
432 return true; /* we don't compare the padding guts */
433 case FLAC__METADATA_TYPE_APPLICATION:
434 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
435 case FLAC__METADATA_TYPE_SEEKTABLE:
436 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
437 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
438 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
445 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
449 FLAC__ASSERT(0 != object);
450 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
451 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
453 save = object->data.application.data;
455 /* do the copy first so that if we fail we leave the object untouched */
457 if(!copy_bytes_(&object->data.application.data, data, length))
461 object->data.application.data = data;
467 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
471 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
473 FLAC__ASSERT(0 != object);
474 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
476 if(0 == object->data.seek_table.points) {
477 FLAC__ASSERT(object->data.seek_table.num_points == 0);
478 if(0 == new_num_points)
480 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
484 const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
485 const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
487 FLAC__ASSERT(object->data.seek_table.num_points > 0);
490 free(object->data.seek_table.points);
491 object->data.seek_table.points = 0;
493 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
496 /* if growing, set new elements to placeholders */
497 if(new_size > old_size) {
499 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
500 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
501 object->data.seek_table.points[i].stream_offset = 0;
502 object->data.seek_table.points[i].frame_samples = 0;
507 object->data.seek_table.num_points = new_num_points;
509 seektable_calculate_length_(object);
513 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
515 FLAC__ASSERT(0 != object);
516 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
517 FLAC__ASSERT(object->data.seek_table.num_points > point_num);
519 object->data.seek_table.points[point_num] = point;
522 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
526 FLAC__ASSERT(0 != object);
527 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
528 FLAC__ASSERT(object->data.seek_table.num_points >= point_num);
530 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
533 /* move all points >= point_num forward one space */
534 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
535 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
537 FLAC__metadata_object_seektable_set_point(object, point_num, point);
538 seektable_calculate_length_(object);
542 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
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 /* move all points > point_num backward one space */
551 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
552 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
554 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
557 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
559 FLAC__ASSERT(0 != object);
560 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
562 return FLAC__format_seektable_is_legal(&object->data.seek_table);
565 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
567 FLAC__ASSERT(0 != object);
568 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
571 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
572 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
577 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
579 FLAC__StreamMetadata_SeekTable *seek_table;
581 FLAC__ASSERT(0 != object);
582 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
584 seek_table = &object->data.seek_table;
586 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
589 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
590 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
591 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
596 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
598 FLAC__ASSERT(0 != object);
599 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
600 FLAC__ASSERT(0 != sample_numbers || num == 0);
603 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
606 i = seek_table->num_points;
608 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
611 for(j = 0; j < num; i++, j++) {
612 seek_table->points[i].sample_number = sample_numbers[j];
613 seek_table->points[i].stream_offset = 0;
614 seek_table->points[i].frame_samples = 0;
621 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
623 FLAC__ASSERT(0 != object);
624 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
625 FLAC__ASSERT(total_samples > 0);
628 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
631 i = seek_table->num_points;
633 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
636 for(j = 0; j < num; i++, j++) {
637 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
638 seek_table->points[i].stream_offset = 0;
639 seek_table->points[i].frame_samples = 0;
646 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
650 FLAC__ASSERT(0 != object);
651 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
653 unique = FLAC__format_seektable_sort(&object->data.seek_table);
655 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
658 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
660 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
663 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
665 FLAC__ASSERT(0 != object);
666 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
668 if(0 == object->data.vorbis_comment.comments) {
669 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
670 if(0 == new_num_comments)
672 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
676 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
677 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
679 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
681 /* if shrinking, free the truncated entries */
682 if(new_num_comments < object->data.vorbis_comment.num_comments) {
684 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
685 if(0 != object->data.vorbis_comment.comments[i].entry)
686 free(object->data.vorbis_comment.comments[i].entry);
690 free(object->data.vorbis_comment.comments);
691 object->data.vorbis_comment.comments = 0;
693 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
696 /* if growing, zero all the length/pointers of new elements */
697 if(new_size > old_size)
698 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
701 object->data.vorbis_comment.num_comments = new_num_comments;
703 vorbiscomment_calculate_length_(object);
707 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
709 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
712 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
714 FLAC__StreamMetadata_VorbisComment *vc;
716 FLAC__ASSERT(0 != object);
717 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
718 FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num);
720 vc = &object->data.vorbis_comment;
722 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
725 /* move all comments >= comment_num forward one space */
726 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
727 vc->comments[comment_num].length = 0;
728 vc->comments[comment_num].entry = 0;
730 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
733 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
735 FLAC__StreamMetadata_VorbisComment *vc;
737 FLAC__ASSERT(0 != object);
738 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
739 FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num);
741 vc = &object->data.vorbis_comment;
743 /* free the comment at comment_num */
744 if(0 != vc->comments[comment_num].entry)
745 free(vc->comments[comment_num].entry);
747 /* move all comments > comment_num backward one space */
748 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
749 vc->comments[vc->num_comments-1].length = 0;
750 vc->comments[vc->num_comments-1].entry = 0;
752 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);