minor changes in anticipation of release
[flac.git] / src / libFLAC / metadata_object.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2001,2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "private/metadata.h"
24
25 #include "FLAC/assert.h"
26
27
28 /****************************************************************************
29  *
30  * Local routines
31  *
32  ***************************************************************************/
33
34 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
35 {
36         if(bytes > 0 && 0 != from) {
37                 FLAC__byte *x;
38                 if(0 == (x = malloc(bytes)))
39                         return false;
40                 memcpy(x, from, bytes);
41                 *to = x;
42         }
43         else {
44                 FLAC__ASSERT(0 == from);
45                 FLAC__ASSERT(bytes == 0);
46                 *to = 0;
47         }
48         return true;
49 }
50
51 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
52 {
53         to->length = from->length;
54         if(0 == from->entry) {
55                 FLAC__ASSERT(from->length == 0);
56                 to->entry = 0;
57         }
58         else {
59                 FLAC__byte *x;
60                 FLAC__ASSERT(from->length > 0);
61                 if(0 == (x = malloc(from->length)))
62                         return false;
63                 memcpy(x, from->entry, from->length);
64                 to->entry = x;
65         }
66         return true;
67 }
68
69 static void seektable_calculate_length_(FLAC__StreamMetadata *object)
70 {
71         FLAC__ASSERT(0 != object);
72         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
73
74         object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
75 }
76
77 static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
78 {
79         FLAC__StreamMetadata_SeekPoint *object_array;
80
81         FLAC__ASSERT(num_points > 0);
82
83         object_array = malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
84
85         if(0 != object_array) {
86                 unsigned i;
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;
91                 }
92         }
93
94         return object_array;
95 }
96
97 static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
98 {
99         unsigned i;
100
101         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
102
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;
109         }
110 }
111
112 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
113 {
114         FLAC__StreamMetadata_VorbisComment_Entry *object_array;
115
116         FLAC__ASSERT(num_comments > 0);
117
118         object_array = malloc(num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
119
120         if(0 != object_array)
121                 memset(object_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
122
123         return object_array;
124 }
125
126 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
127 {
128         unsigned i;
129
130         FLAC__ASSERT(0 != object_array && num_comments > 0);
131
132         for(i = 0; i < num_comments; i++)
133                 if(0 != object_array[i].entry)
134                         free(object_array[i].entry);
135
136         if(0 != object_array)
137                 free(object_array);
138 }
139
140 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
141 {
142         FLAC__StreamMetadata_VorbisComment_Entry *return_array;
143
144         FLAC__ASSERT(0 != object_array);
145         FLAC__ASSERT(num_comments > 0);
146
147         return_array = vorbiscomment_entry_array_new_(num_comments);
148
149         if(0 != return_array) {
150                 unsigned i;
151
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
155                  * pointers.
156                  */
157                 memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
158
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);
162                                 return 0;
163                         }
164                 }
165         }
166
167         return return_array;
168 }
169
170 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
171 {
172         FLAC__byte *save;
173
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));
179
180         save = dest->entry;
181
182         /* do the copy first so that if we fail we leave the object untouched */
183         if(copy) {
184                 if(!copy_vcentry_(dest, src))
185                         return false;
186         }
187         else {
188                 *dest = *src;
189         }
190
191         if(0 != save)
192                 free(save);
193
194         vorbiscomment_calculate_length_(object);
195         return true;
196 }
197
198
199 /****************************************************************************
200  *
201  * Metadata object routines
202  *
203  ***************************************************************************/
204
205 /*@@@move
206 will return pointer to new empty object of type 'type', or 0 if malloc failed
207 type is valid type
208 */
209 FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
210 {
211         FLAC__StreamMetadata *object = malloc(sizeof(FLAC__StreamMetadata));
212         if(0 != object) {
213                 memset(object, 0, sizeof(FLAC__StreamMetadata));
214                 object->is_last = false;
215                 object->type = type;
216                 switch(type) {
217                         case FLAC__METADATA_TYPE_STREAMINFO:
218                                 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
219                                 break;
220                         case FLAC__METADATA_TYPE_PADDING:
221                                 break;
222                         case FLAC__METADATA_TYPE_APPLICATION:
223                                 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
224                                 break;
225                         case FLAC__METADATA_TYPE_SEEKTABLE:
226                                 break;
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;
229                                 break;
230                         default:
231                                 /* double protection: */
232                                 FLAC__ASSERT(0);
233                                 free(object);
234                                 return 0;
235                 }
236         }
237
238         return object;
239 }
240
241 /*@@@move
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);
244 */
245 FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
246 {
247         FLAC__StreamMetadata *to;
248
249         FLAC__ASSERT(0 != object);
250
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;
255                 switch(to->type) {
256                         case FLAC__METADATA_TYPE_STREAMINFO:
257                                 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
258                                 break;
259                         case FLAC__METADATA_TYPE_PADDING:
260                                 break;
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);
265                                         return 0;
266                                 }
267                                 break;
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);
272                                         return 0;
273                                 }
274                                 break;
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);
278                                         return 0;
279                                 }
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;
283                                 }
284                                 else {
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);
289                                                 return 0;
290                                         }
291                                 }
292                                 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
293                                 break;
294                         default:
295                                 /* double protection: */
296                                 FLAC__ASSERT(0);
297                                 free(to);
298                                 return 0;
299                 }
300         }
301
302         return to;
303 }
304
305 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
306 {
307         FLAC__ASSERT(0 != object);
308
309         switch(object->type) {
310                 case FLAC__METADATA_TYPE_STREAMINFO:
311                 case FLAC__METADATA_TYPE_PADDING:
312                         break;
313                 case FLAC__METADATA_TYPE_APPLICATION:
314                         if(0 != object->data.application.data)
315                                 free(object->data.application.data);
316                         break;
317                 case FLAC__METADATA_TYPE_SEEKTABLE:
318                         if(0 != object->data.seek_table.points)
319                                 free(object->data.seek_table.points);
320                         break;
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);
327                         }
328                         break;
329                 default:
330                         FLAC__ASSERT(0);
331         }
332 }
333
334 /*@@@move
335 frees 'object'.  does a deep delete.
336 */
337 void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
338 {
339         FLAC__metadata_object_delete_data(object);
340         free(object);
341 }
342
343 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
344 {
345         if(block1->min_blocksize != block2->min_blocksize)
346                 return false;
347         if(block1->max_blocksize != block2->max_blocksize)
348                 return false;
349         if(block1->min_framesize != block2->min_framesize)
350                 return false;
351         if(block1->max_framesize != block2->max_framesize)
352                 return false;
353         if(block1->sample_rate != block2->sample_rate)
354                 return false;
355         if(block1->channels != block2->channels)
356                 return false;
357         if(block1->bits_per_sample != block2->bits_per_sample)
358                 return false;
359         if(block1->total_samples != block2->total_samples)
360                 return false;
361         if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
362                 return false;
363         return true;
364 }
365
366 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
367 {
368         FLAC__ASSERT(0 != block1);
369         FLAC__ASSERT(0 != block2);
370         FLAC__ASSERT(block_length >= sizeof(block1->id));
371
372         if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
373                 return false;
374         if(0 != block1->data && 0 != block2->data)
375                 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
376         else
377                 return block1->data == block2->data;
378 }
379
380 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
381 {
382         unsigned i;
383
384         FLAC__ASSERT(0 != block1);
385         FLAC__ASSERT(0 != block2);
386
387         if(block1->num_points != block2->num_points)
388                 return false;
389
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)
393                                 return false;
394                         if(block1->points[i].stream_offset != block2->points[i].stream_offset)
395                                 return false;
396                         if(block1->points[i].frame_samples != block2->points[i].frame_samples)
397                                 return false;
398                 }
399                 return true;
400         }
401         else
402                 return block1->points == block2->points;
403 }
404
405 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
406 {
407         unsigned i;
408
409         if(block1->vendor_string.length != block2->vendor_string.length)
410                 return false;
411
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))
414                         return false;
415         }
416         else if(block1->vendor_string.entry != block2->vendor_string.entry)
417                 return false;
418
419         if(block1->num_comments != block2->num_comments)
420                 return false;
421
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))
425                                 return false;
426                 }
427                 else if(block1->comments[i].entry != block2->comments[i].entry)
428                         return false;
429         }
430         return true;
431 }
432
433 FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
434 {
435         if(block1->type != block2->type) {
436                 return false;
437     }
438         if(block1->is_last != block2->is_last) {
439                 return false;
440     }
441         if(block1->length != block2->length) {
442                 return false;
443     }
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);
455                 default:
456                         FLAC__ASSERT(0);
457                         return false;
458         }
459 }
460
461 /*@@@move
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));
465 */
466 FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
467 {
468         FLAC__byte *save;
469
470         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
471         FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
472
473         save = object->data.application.data;
474
475         /* do the copy first so that if we fail we leave the object untouched */
476         if(copy) {
477                 if(!copy_bytes_(&object->data.application.data, data, length))
478                         return false;
479         }
480         else {
481                 object->data.application.data = data;
482         }
483
484         if(0 != save)
485                 free(save);
486
487         object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
488         return true;
489 }
490
491 FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
492 {
493         FLAC__ASSERT(0 != object);
494         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
495
496         if(0 == object->data.seek_table.points) {
497                 FLAC__ASSERT(object->data.seek_table.num_points == 0);
498                 if(0 == new_num_points)
499                         return true;
500                 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
501                         return false;
502         }
503         else {
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);
506
507                 FLAC__ASSERT(object->data.seek_table.num_points > 0);
508
509                 if(new_size == 0) {
510                         free(object->data.seek_table.points);
511                         object->data.seek_table.points = 0;
512                 }
513                 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
514                         return false;
515
516                 /* if growing, set new elements to placeholders */
517                 if(new_size > old_size) {
518                         unsigned i;
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;
523                         }
524                 }
525         }
526
527         object->data.seek_table.num_points = new_num_points;
528
529         seektable_calculate_length_(object);
530         return true;
531 }
532
533 void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
534 {
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);
538
539         object->data.seek_table.points[point_num] = point;
540 }
541
542 FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
543 {
544         int i;
545
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);
549
550         if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
551                 return false;
552
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];
556
557         FLAC__metadata_object_seektable_set_point(object, point_num, point);
558         seektable_calculate_length_(object);
559         return true;
560 }
561
562 FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
563 {
564         unsigned i;
565
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);
569
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];
573
574         return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
575 }
576
577 FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
578 {
579         unsigned i;
580         FLAC__uint64 prev_sample_number = 0;
581         FLAC__bool got_prev = false;
582
583         FLAC__ASSERT(0 != object);
584         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
585
586         {
587                 const FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
588
589                 for(i = 0; i < seek_table->num_points; i++) {
590                         if(got_prev) {
591                                 if(seek_table->points[i].sample_number <= prev_sample_number)
592                                         return false;
593                         }
594                         prev_sample_number = seek_table->points[i].sample_number;
595                         got_prev = true;
596                 }
597         }
598
599         return true;
600 }
601
602 FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
603 {
604         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
605 }
606
607 FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
608 {
609         FLAC__ASSERT(0 != object);
610         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
611
612         if(0 == object->data.vorbis_comment.comments) {
613                 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
614                 if(0 == new_num_comments)
615                         return true;
616                 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
617                         return false;
618         }
619         else {
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);
622
623                 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
624
625                 /* if shrinking, free the truncated entries */
626                 if(new_num_comments < object->data.vorbis_comment.num_comments) {
627                         unsigned i;
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);
631                 }
632
633                 if(new_size == 0) {
634                         free(object->data.vorbis_comment.comments);
635                         object->data.vorbis_comment.comments = 0;
636                 }
637                 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
638                         return false;
639
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);
643         }
644
645         object->data.vorbis_comment.num_comments = new_num_comments;
646
647         vorbiscomment_calculate_length_(object);
648         return true;
649 }
650
651 FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
652 {
653         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
654 }
655
656 FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
657 {
658         FLAC__StreamMetadata_VorbisComment *vc;
659
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);
663
664         vc = &object->data.vorbis_comment;
665
666         if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
667                 return false;
668
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;
673
674         return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
675 }
676
677 FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
678 {
679         FLAC__StreamMetadata_VorbisComment *vc;
680
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);
684
685         vc = &object->data.vorbis_comment;
686
687         /* free the comment at comment_num */
688         if(0 != vc->comments[comment_num].entry)
689                 free(vc->comments[comment_num].entry);
690
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;
695
696         return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
697 }