switch malloc+memset(0) to calloc()
[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__ASSERT(num_comments > 0);
115
116         return calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
117 }
118
119 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
120 {
121         unsigned i;
122
123         FLAC__ASSERT(0 != object_array && num_comments > 0);
124
125         for(i = 0; i < num_comments; i++)
126                 if(0 != object_array[i].entry)
127                         free(object_array[i].entry);
128
129         if(0 != object_array)
130                 free(object_array);
131 }
132
133 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
134 {
135         FLAC__StreamMetadata_VorbisComment_Entry *return_array;
136
137         FLAC__ASSERT(0 != object_array);
138         FLAC__ASSERT(num_comments > 0);
139
140         return_array = vorbiscomment_entry_array_new_(num_comments);
141
142         if(0 != return_array) {
143                 unsigned i;
144
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
148                  * pointers.
149                  */
150                 memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
151
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);
155                                 return 0;
156                         }
157                 }
158         }
159
160         return return_array;
161 }
162
163 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
164 {
165         FLAC__byte *save;
166
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));
172
173         save = dest->entry;
174
175         /* do the copy first so that if we fail we leave the object untouched */
176         if(copy) {
177                 if(!copy_vcentry_(dest, src))
178                         return false;
179         }
180         else {
181                 *dest = *src;
182         }
183
184         if(0 != save)
185                 free(save);
186
187         vorbiscomment_calculate_length_(object);
188         return true;
189 }
190
191
192 /****************************************************************************
193  *
194  * Metadata object routines
195  *
196  ***************************************************************************/
197
198 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
199 {
200         FLAC__StreamMetadata *object = calloc(1, sizeof(FLAC__StreamMetadata));
201         if(0 != object) {
202                 object->is_last = false;
203                 object->type = type;
204                 switch(type) {
205                         case FLAC__METADATA_TYPE_STREAMINFO:
206                                 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
207                                 break;
208                         case FLAC__METADATA_TYPE_PADDING:
209                                 break;
210                         case FLAC__METADATA_TYPE_APPLICATION:
211                                 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
212                                 break;
213                         case FLAC__METADATA_TYPE_SEEKTABLE:
214                                 break;
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;
217                                 break;
218                         default:
219                                 /* double protection: */
220                                 FLAC__ASSERT(0);
221                                 free(object);
222                                 return 0;
223                 }
224         }
225
226         return object;
227 }
228
229 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
230 {
231         FLAC__StreamMetadata *to;
232
233         FLAC__ASSERT(0 != object);
234
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;
239                 switch(to->type) {
240                         case FLAC__METADATA_TYPE_STREAMINFO:
241                                 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
242                                 break;
243                         case FLAC__METADATA_TYPE_PADDING:
244                                 break;
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);
249                                         return 0;
250                                 }
251                                 break;
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);
256                                         return 0;
257                                 }
258                                 break;
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);
262                                         return 0;
263                                 }
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;
267                                 }
268                                 else {
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);
273                                                 return 0;
274                                         }
275                                 }
276                                 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
277                                 break;
278                         default:
279                                 /* double protection: */
280                                 FLAC__ASSERT(0);
281                                 free(to);
282                                 return 0;
283                 }
284         }
285
286         return to;
287 }
288
289 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
290 {
291         FLAC__ASSERT(0 != object);
292
293         switch(object->type) {
294                 case FLAC__METADATA_TYPE_STREAMINFO:
295                 case FLAC__METADATA_TYPE_PADDING:
296                         break;
297                 case FLAC__METADATA_TYPE_APPLICATION:
298                         if(0 != object->data.application.data)
299                                 free(object->data.application.data);
300                         break;
301                 case FLAC__METADATA_TYPE_SEEKTABLE:
302                         if(0 != object->data.seek_table.points)
303                                 free(object->data.seek_table.points);
304                         break;
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);
311                         }
312                         break;
313                 default:
314                         FLAC__ASSERT(0);
315         }
316 }
317
318 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
319 {
320         FLAC__metadata_object_delete_data(object);
321         free(object);
322 }
323
324 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
325 {
326         if(block1->min_blocksize != block2->min_blocksize)
327                 return false;
328         if(block1->max_blocksize != block2->max_blocksize)
329                 return false;
330         if(block1->min_framesize != block2->min_framesize)
331                 return false;
332         if(block1->max_framesize != block2->max_framesize)
333                 return false;
334         if(block1->sample_rate != block2->sample_rate)
335                 return false;
336         if(block1->channels != block2->channels)
337                 return false;
338         if(block1->bits_per_sample != block2->bits_per_sample)
339                 return false;
340         if(block1->total_samples != block2->total_samples)
341                 return false;
342         if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
343                 return false;
344         return true;
345 }
346
347 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
348 {
349         FLAC__ASSERT(0 != block1);
350         FLAC__ASSERT(0 != block2);
351         FLAC__ASSERT(block_length >= sizeof(block1->id));
352
353         if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
354                 return false;
355         if(0 != block1->data && 0 != block2->data)
356                 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
357         else
358                 return block1->data == block2->data;
359 }
360
361 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
362 {
363         unsigned i;
364
365         FLAC__ASSERT(0 != block1);
366         FLAC__ASSERT(0 != block2);
367
368         if(block1->num_points != block2->num_points)
369                 return false;
370
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)
374                                 return false;
375                         if(block1->points[i].stream_offset != block2->points[i].stream_offset)
376                                 return false;
377                         if(block1->points[i].frame_samples != block2->points[i].frame_samples)
378                                 return false;
379                 }
380                 return true;
381         }
382         else
383                 return block1->points == block2->points;
384 }
385
386 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
387 {
388         unsigned i;
389
390         if(block1->vendor_string.length != block2->vendor_string.length)
391                 return false;
392
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))
395                         return false;
396         }
397         else if(block1->vendor_string.entry != block2->vendor_string.entry)
398                 return false;
399
400         if(block1->num_comments != block2->num_comments)
401                 return false;
402
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))
406                                 return false;
407                 }
408                 else if(block1->comments[i].entry != block2->comments[i].entry)
409                         return false;
410         }
411         return true;
412 }
413
414 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
415 {
416         FLAC__ASSERT(0 != block1);
417         FLAC__ASSERT(0 != block2);
418
419         if(block1->type != block2->type) {
420                 return false;
421         }
422         if(block1->is_last != block2->is_last) {
423                 return false;
424         }
425         if(block1->length != block2->length) {
426                 return false;
427         }
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);
439                 default:
440                         FLAC__ASSERT(0);
441                         return false;
442         }
443 }
444
445 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
446 {
447         FLAC__byte *save;
448
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));
452
453         save = object->data.application.data;
454
455         /* do the copy first so that if we fail we leave the object untouched */
456         if(copy) {
457                 if(!copy_bytes_(&object->data.application.data, data, length))
458                         return false;
459         }
460         else {
461                 object->data.application.data = data;
462         }
463
464         if(0 != save)
465                 free(save);
466
467         object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
468         return true;
469 }
470
471 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
472 {
473         FLAC__ASSERT(0 != object);
474         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
475
476         if(0 == object->data.seek_table.points) {
477                 FLAC__ASSERT(object->data.seek_table.num_points == 0);
478                 if(0 == new_num_points)
479                         return true;
480                 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
481                         return false;
482         }
483         else {
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);
486
487                 FLAC__ASSERT(object->data.seek_table.num_points > 0);
488
489                 if(new_size == 0) {
490                         free(object->data.seek_table.points);
491                         object->data.seek_table.points = 0;
492                 }
493                 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
494                         return false;
495
496                 /* if growing, set new elements to placeholders */
497                 if(new_size > old_size) {
498                         unsigned i;
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;
503                         }
504                 }
505         }
506
507         object->data.seek_table.num_points = new_num_points;
508
509         seektable_calculate_length_(object);
510         return true;
511 }
512
513 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
514 {
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);
518
519         object->data.seek_table.points[point_num] = point;
520 }
521
522 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
523 {
524         int i;
525
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);
529
530         if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
531                 return false;
532
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];
536
537         FLAC__metadata_object_seektable_set_point(object, point_num, point);
538         seektable_calculate_length_(object);
539         return true;
540 }
541
542 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
543 {
544         unsigned 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         /* 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];
553
554         return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
555 }
556
557 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
558 {
559         FLAC__ASSERT(0 != object);
560         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
561
562         return FLAC__format_seektable_is_legal(&object->data.seek_table);
563 }
564
565 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
566 {
567         FLAC__ASSERT(0 != object);
568         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
569
570         if(num > 0)
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);
573         else
574                 return true;
575 }
576
577 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
578 {
579         FLAC__StreamMetadata_SeekTable *seek_table;
580
581         FLAC__ASSERT(0 != object);
582         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
583
584         seek_table = &object->data.seek_table;
585
586         if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
587                 return false;
588
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;
592
593         return true;
594 }
595
596 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
597 {
598         FLAC__ASSERT(0 != object);
599         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
600         FLAC__ASSERT(0 != sample_numbers || num == 0);
601
602         if(num > 0) {
603                 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
604                 unsigned i, j;
605
606                 i = seek_table->num_points;
607
608                 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
609                         return false;
610
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;
615                 }
616         }
617
618         return true;
619 }
620
621 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
622 {
623         FLAC__ASSERT(0 != object);
624         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
625         FLAC__ASSERT(total_samples > 0);
626
627         if(num > 0) {
628                 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
629                 unsigned i, j;
630
631                 i = seek_table->num_points;
632
633                 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
634                         return false;
635
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;
640                 }
641         }
642
643         return true;
644 }
645
646 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
647 {
648         unsigned unique;
649
650         FLAC__ASSERT(0 != object);
651         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
652
653         unique = FLAC__format_seektable_sort(&object->data.seek_table);
654
655         return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
656 }
657
658 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
659 {
660         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
661 }
662
663 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
664 {
665         FLAC__ASSERT(0 != object);
666         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
667
668         if(0 == object->data.vorbis_comment.comments) {
669                 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
670                 if(0 == new_num_comments)
671                         return true;
672                 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
673                         return false;
674         }
675         else {
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);
678
679                 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
680
681                 /* if shrinking, free the truncated entries */
682                 if(new_num_comments < object->data.vorbis_comment.num_comments) {
683                         unsigned i;
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);
687                 }
688
689                 if(new_size == 0) {
690                         free(object->data.vorbis_comment.comments);
691                         object->data.vorbis_comment.comments = 0;
692                 }
693                 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
694                         return false;
695
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);
699         }
700
701         object->data.vorbis_comment.num_comments = new_num_comments;
702
703         vorbiscomment_calculate_length_(object);
704         return true;
705 }
706
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)
708 {
709         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
710 }
711
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)
713 {
714         FLAC__StreamMetadata_VorbisComment *vc;
715
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);
719
720         vc = &object->data.vorbis_comment;
721
722         if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
723                 return false;
724
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;
729
730         return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
731 }
732
733 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
734 {
735         FLAC__StreamMetadata_VorbisComment *vc;
736
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);
740
741         vc = &object->data.vorbis_comment;
742
743         /* free the comment at comment_num */
744         if(0 != vc->comments[comment_num].entry)
745                 free(vc->comments[comment_num].entry);
746
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;
751
752         return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
753 }