2630648fda16a892f278a57187ab5a5599603e2d
[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 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
206 {
207         FLAC__StreamMetadata *object = malloc(sizeof(FLAC__StreamMetadata));
208         if(0 != object) {
209                 memset(object, 0, sizeof(FLAC__StreamMetadata));
210                 object->is_last = false;
211                 object->type = type;
212                 switch(type) {
213                         case FLAC__METADATA_TYPE_STREAMINFO:
214                                 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
215                                 break;
216                         case FLAC__METADATA_TYPE_PADDING:
217                                 break;
218                         case FLAC__METADATA_TYPE_APPLICATION:
219                                 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
220                                 break;
221                         case FLAC__METADATA_TYPE_SEEKTABLE:
222                                 break;
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;
225                                 break;
226                         default:
227                                 /* double protection: */
228                                 FLAC__ASSERT(0);
229                                 free(object);
230                                 return 0;
231                 }
232         }
233
234         return object;
235 }
236
237 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
238 {
239         FLAC__StreamMetadata *to;
240
241         FLAC__ASSERT(0 != object);
242
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;
247                 switch(to->type) {
248                         case FLAC__METADATA_TYPE_STREAMINFO:
249                                 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
250                                 break;
251                         case FLAC__METADATA_TYPE_PADDING:
252                                 break;
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);
257                                         return 0;
258                                 }
259                                 break;
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);
264                                         return 0;
265                                 }
266                                 break;
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);
270                                         return 0;
271                                 }
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;
275                                 }
276                                 else {
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);
281                                                 return 0;
282                                         }
283                                 }
284                                 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
285                                 break;
286                         default:
287                                 /* double protection: */
288                                 FLAC__ASSERT(0);
289                                 free(to);
290                                 return 0;
291                 }
292         }
293
294         return to;
295 }
296
297 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
298 {
299         FLAC__ASSERT(0 != object);
300
301         switch(object->type) {
302                 case FLAC__METADATA_TYPE_STREAMINFO:
303                 case FLAC__METADATA_TYPE_PADDING:
304                         break;
305                 case FLAC__METADATA_TYPE_APPLICATION:
306                         if(0 != object->data.application.data)
307                                 free(object->data.application.data);
308                         break;
309                 case FLAC__METADATA_TYPE_SEEKTABLE:
310                         if(0 != object->data.seek_table.points)
311                                 free(object->data.seek_table.points);
312                         break;
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);
319                         }
320                         break;
321                 default:
322                         FLAC__ASSERT(0);
323         }
324 }
325
326 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
327 {
328         FLAC__metadata_object_delete_data(object);
329         free(object);
330 }
331
332 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
333 {
334         if(block1->min_blocksize != block2->min_blocksize)
335                 return false;
336         if(block1->max_blocksize != block2->max_blocksize)
337                 return false;
338         if(block1->min_framesize != block2->min_framesize)
339                 return false;
340         if(block1->max_framesize != block2->max_framesize)
341                 return false;
342         if(block1->sample_rate != block2->sample_rate)
343                 return false;
344         if(block1->channels != block2->channels)
345                 return false;
346         if(block1->bits_per_sample != block2->bits_per_sample)
347                 return false;
348         if(block1->total_samples != block2->total_samples)
349                 return false;
350         if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
351                 return false;
352         return true;
353 }
354
355 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
356 {
357         FLAC__ASSERT(0 != block1);
358         FLAC__ASSERT(0 != block2);
359         FLAC__ASSERT(block_length >= sizeof(block1->id));
360
361         if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
362                 return false;
363         if(0 != block1->data && 0 != block2->data)
364                 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
365         else
366                 return block1->data == block2->data;
367 }
368
369 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
370 {
371         unsigned i;
372
373         FLAC__ASSERT(0 != block1);
374         FLAC__ASSERT(0 != block2);
375
376         if(block1->num_points != block2->num_points)
377                 return false;
378
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)
382                                 return false;
383                         if(block1->points[i].stream_offset != block2->points[i].stream_offset)
384                                 return false;
385                         if(block1->points[i].frame_samples != block2->points[i].frame_samples)
386                                 return false;
387                 }
388                 return true;
389         }
390         else
391                 return block1->points == block2->points;
392 }
393
394 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
395 {
396         unsigned i;
397
398         if(block1->vendor_string.length != block2->vendor_string.length)
399                 return false;
400
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))
403                         return false;
404         }
405         else if(block1->vendor_string.entry != block2->vendor_string.entry)
406                 return false;
407
408         if(block1->num_comments != block2->num_comments)
409                 return false;
410
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))
414                                 return false;
415                 }
416                 else if(block1->comments[i].entry != block2->comments[i].entry)
417                         return false;
418         }
419         return true;
420 }
421
422 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
423 {
424         FLAC__ASSERT(0 != block1);
425         FLAC__ASSERT(0 != block2);
426
427         if(block1->type != block2->type) {
428                 return false;
429         }
430         if(block1->is_last != block2->is_last) {
431                 return false;
432         }
433         if(block1->length != block2->length) {
434                 return false;
435         }
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);
447                 default:
448                         FLAC__ASSERT(0);
449                         return false;
450         }
451 }
452
453 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
454 {
455         FLAC__byte *save;
456
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));
460
461         save = object->data.application.data;
462
463         /* do the copy first so that if we fail we leave the object untouched */
464         if(copy) {
465                 if(!copy_bytes_(&object->data.application.data, data, length))
466                         return false;
467         }
468         else {
469                 object->data.application.data = data;
470         }
471
472         if(0 != save)
473                 free(save);
474
475         object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
476         return true;
477 }
478
479 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
480 {
481         FLAC__ASSERT(0 != object);
482         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
483
484         if(0 == object->data.seek_table.points) {
485                 FLAC__ASSERT(object->data.seek_table.num_points == 0);
486                 if(0 == new_num_points)
487                         return true;
488                 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
489                         return false;
490         }
491         else {
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);
494
495                 FLAC__ASSERT(object->data.seek_table.num_points > 0);
496
497                 if(new_size == 0) {
498                         free(object->data.seek_table.points);
499                         object->data.seek_table.points = 0;
500                 }
501                 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
502                         return false;
503
504                 /* if growing, set new elements to placeholders */
505                 if(new_size > old_size) {
506                         unsigned i;
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;
511                         }
512                 }
513         }
514
515         object->data.seek_table.num_points = new_num_points;
516
517         seektable_calculate_length_(object);
518         return true;
519 }
520
521 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
522 {
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);
526
527         object->data.seek_table.points[point_num] = point;
528 }
529
530 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
531 {
532         int i;
533
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);
537
538         if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
539                 return false;
540
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];
544
545         FLAC__metadata_object_seektable_set_point(object, point_num, point);
546         seektable_calculate_length_(object);
547         return true;
548 }
549
550 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
551 {
552         unsigned i;
553
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);
557
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];
561
562         return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
563 }
564
565 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
566 {
567         FLAC__ASSERT(0 != object);
568         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
569
570         return FLAC__format_seektable_is_legal(&object->data.seek_table);
571 }
572
573 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
574 {
575         FLAC__ASSERT(0 != object);
576         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
577
578         if(num > 0)
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);
581         else
582                 return true;
583 }
584
585 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
586 {
587         FLAC__StreamMetadata_SeekTable *seek_table;
588
589         FLAC__ASSERT(0 != object);
590         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
591
592         seek_table = &object->data.seek_table;
593
594         if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
595                 return false;
596
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;
600
601         return true;
602 }
603
604 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
605 {
606         FLAC__ASSERT(0 != object);
607         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
608         FLAC__ASSERT(0 != sample_numbers || num == 0);
609
610         if(num > 0) {
611                 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
612                 unsigned i, j;
613
614                 i = seek_table->num_points;
615
616                 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
617                         return false;
618
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;
623                 }
624         }
625
626         return true;
627 }
628
629 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
630 {
631         FLAC__ASSERT(0 != object);
632         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
633         FLAC__ASSERT(total_samples > 0);
634
635         if(num > 0) {
636                 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
637                 unsigned i, j;
638
639                 i = seek_table->num_points;
640
641                 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
642                         return false;
643
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;
648                 }
649         }
650
651         return true;
652 }
653
654 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
655 {
656         unsigned unique;
657
658         FLAC__ASSERT(0 != object);
659         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
660
661         unique = FLAC__format_seektable_sort(&object->data.seek_table);
662
663         return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
664 }
665
666 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
667 {
668         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
669 }
670
671 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
672 {
673         FLAC__ASSERT(0 != object);
674         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
675
676         if(0 == object->data.vorbis_comment.comments) {
677                 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
678                 if(0 == new_num_comments)
679                         return true;
680                 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
681                         return false;
682         }
683         else {
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);
686
687                 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
688
689                 /* if shrinking, free the truncated entries */
690                 if(new_num_comments < object->data.vorbis_comment.num_comments) {
691                         unsigned i;
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);
695                 }
696
697                 if(new_size == 0) {
698                         free(object->data.vorbis_comment.comments);
699                         object->data.vorbis_comment.comments = 0;
700                 }
701                 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
702                         return false;
703
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);
707         }
708
709         object->data.vorbis_comment.num_comments = new_num_comments;
710
711         vorbiscomment_calculate_length_(object);
712         return true;
713 }
714
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)
716 {
717         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
718 }
719
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)
721 {
722         FLAC__StreamMetadata_VorbisComment *vc;
723
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);
727
728         vc = &object->data.vorbis_comment;
729
730         if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
731                 return false;
732
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;
737
738         return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
739 }
740
741 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
742 {
743         FLAC__StreamMetadata_VorbisComment *vc;
744
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);
748
749         vc = &object->data.vorbis_comment;
750
751         /* free the comment at comment_num */
752         if(0 != vc->comments[comment_num].entry)
753                 free(vc->comments[comment_num].entry);
754
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;
759
760         return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
761 }