blob: 21f81c45a77e5ecab0431496b3eb897cc7a121a5 [file] [log] [blame]
Josh Coalson90ced912002-05-30 05:23:38 +00001/* 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
34static 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
51static 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
69static 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
77static 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
97static 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
112static 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
126static 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
140static 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
Josh Coalson6b8e5302002-05-31 06:23:09 +0000170static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry *dest, const FLAC__StreamMetaData_VorbisComment_Entry *src, FLAC__bool copy)
Josh Coalson90ced912002-05-30 05:23:38 +0000171{
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
Josh Coalson6b8e5302002-05-31 06:23:09 +0000180 save = dest->entry;
Josh Coalson90ced912002-05-30 05:23:38 +0000181
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
206will return pointer to new empty object of type 'type', or 0 if malloc failed
207type is valid type
208*/
209FLAC__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
242return 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*/
245FLAC__StreamMetaData *FLAC__metadata_object_copy(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
305void 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
335frees 'object'. does a deep delete.
336*/
337void FLAC__metadata_object_delete(FLAC__StreamMetaData *object)
338{
339 FLAC__metadata_object_delete_data(object);
340 free(object);
341}
342
Josh Coalson57ba6f42002-06-07 05:27:37 +0000343static 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
366static 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
380static 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
405static 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
433FLAC__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
Josh Coalson90ced912002-05-30 05:23:38 +0000461/*@@@@move
462sets 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*/
466FLAC__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
491FLAC__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
533void 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
542FLAC__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);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000547 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson90ced912002-05-30 05:23:38 +0000548 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
562FLAC__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
Josh Coalson28e08d82002-06-05 05:56:41 +0000577FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetaData *object)
578{
579 unsigned i;
580 FLAC__uint64 last_sample_number = 0;
581 FLAC__bool got_last = 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(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
591 if(got_last) {
592 if(seek_table->points[i].sample_number <= last_sample_number)
593 return false;
594 }
595 last_sample_number = seek_table->points[i].sample_number;
596 got_last = true;
597 }
598 }
599 }
600
601 return true;
602}
603
Josh Coalson6b8e5302002-05-31 06:23:09 +0000604FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy)
Josh Coalson90ced912002-05-30 05:23:38 +0000605{
Josh Coalson6b8e5302002-05-31 06:23:09 +0000606 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +0000607}
608
609FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetaData *object, unsigned new_num_comments)
610{
611 FLAC__ASSERT(0 != object);
612 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
613
614 if(0 == object->data.vorbis_comment.comments) {
615 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
616 if(0 == new_num_comments)
617 return true;
618 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
619 return false;
620 }
621 else {
622 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry);
623 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry);
624
625 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
626
627 /* if shrinking, free the truncated entries */
628 if(new_num_comments < object->data.vorbis_comment.num_comments) {
629 unsigned i;
630 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
631 if(0 != object->data.vorbis_comment.comments[i].entry)
632 free(object->data.vorbis_comment.comments[i].entry);
633 }
634
635 if(new_size == 0) {
636 free(object->data.vorbis_comment.comments);
637 object->data.vorbis_comment.comments = 0;
638 }
639 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
640 return false;
641
642 /* if growing, zero all the length/pointers of new elements */
643 if(new_size > old_size)
644 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
645 }
646
647 object->data.vorbis_comment.num_comments = new_num_comments;
648
649 vorbiscomment_calculate_length_(object);
650 return true;
651}
652
Josh Coalson6b8e5302002-05-31 06:23:09 +0000653FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy)
Josh Coalson90ced912002-05-30 05:23:38 +0000654{
Josh Coalson6b8e5302002-05-31 06:23:09 +0000655 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +0000656}
657
Josh Coalson6b8e5302002-05-31 06:23:09 +0000658FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy)
Josh Coalson90ced912002-05-30 05:23:38 +0000659{
Josh Coalson6b8e5302002-05-31 06:23:09 +0000660 FLAC__StreamMetaData_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +0000661
662 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000663 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
664 FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num);
665
Josh Coalson6b8e5302002-05-31 06:23:09 +0000666 vc = &object->data.vorbis_comment;
667
668 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +0000669 return false;
670
671 /* move all comments >= comment_num forward one space */
Josh Coalson6b8e5302002-05-31 06:23:09 +0000672 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
673 vc->comments[comment_num].length = 0;
674 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +0000675
676 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
677}
678
679FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetaData *object, unsigned comment_num)
680{
Josh Coalson6b8e5302002-05-31 06:23:09 +0000681 FLAC__StreamMetaData_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +0000682
683 FLAC__ASSERT(0 != object);
684 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
685 FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num);
686
Josh Coalson6b8e5302002-05-31 06:23:09 +0000687 vc = &object->data.vorbis_comment;
688
Josh Coalson90ced912002-05-30 05:23:38 +0000689 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +0000690 if(0 != vc->comments[comment_num].entry)
691 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +0000692
693 /* move all comments > comment_num backward one space */
Josh Coalson6b8e5302002-05-31 06:23:09 +0000694 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
695 vc->comments[vc->num_comments-1].length = 0;
696 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +0000697
Josh Coalson6b8e5302002-05-31 06:23:09 +0000698 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +0000699}