blob: 0f822b094282c95f65986a420189b66c93995fd7 [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
Josh Coalsoncc682512002-06-08 04:53:42 +000051static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
Josh Coalson90ced912002-05-30 05:23:38 +000052{
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
Josh Coalsoncc682512002-06-08 04:53:42 +000069static void seektable_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +000070{
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
Josh Coalsoncc682512002-06-08 04:53:42 +000077static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
Josh Coalson90ced912002-05-30 05:23:38 +000078{
Josh Coalsoncc682512002-06-08 04:53:42 +000079 FLAC__StreamMetadata_SeekPoint *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +000080
81 FLAC__ASSERT(num_points > 0);
82
Josh Coalsoncc682512002-06-08 04:53:42 +000083 object_array = malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
Josh Coalson90ced912002-05-30 05:23:38 +000084
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
Josh Coalsoncc682512002-06-08 04:53:42 +000097static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +000098{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000112static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000113{
Josh Coalsoncc682512002-06-08 04:53:42 +0000114 FLAC__StreamMetadata_VorbisComment_Entry *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000115
116 FLAC__ASSERT(num_comments > 0);
117
Josh Coalsoncc682512002-06-08 04:53:42 +0000118 object_array = malloc(num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000119
120 if(0 != object_array)
Josh Coalsoncc682512002-06-08 04:53:42 +0000121 memset(object_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000122
123 return object_array;
124}
125
Josh Coalsoncc682512002-06-08 04:53:42 +0000126static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000127{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000140static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000141{
Josh Coalsoncc682512002-06-08 04:53:42 +0000142 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000143
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 */
Josh Coalsoncc682512002-06-08 04:53:42 +0000157 memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000158
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 Coalsoncc682512002-06-08 04:53:42 +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
Josh Coalsoncc682512002-06-08 04:53:42 +0000205FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
Josh Coalson90ced912002-05-30 05:23:38 +0000206{
Josh Coalsoncc682512002-06-08 04:53:42 +0000207 FLAC__StreamMetadata *object = malloc(sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000208 if(0 != object) {
Josh Coalsoncc682512002-06-08 04:53:42 +0000209 memset(object, 0, sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000210 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
Josh Coalsoncc682512002-06-08 04:53:42 +0000237FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000238{
Josh Coalsoncc682512002-06-08 04:53:42 +0000239 FLAC__StreamMetadata *to;
Josh Coalson90ced912002-05-30 05:23:38 +0000240
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:
Josh Coalsoncc682512002-06-08 04:53:42 +0000249 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
Josh Coalson90ced912002-05-30 05:23:38 +0000250 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;
Josh Coalsoncc682512002-06-08 04:53:42 +0000262 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))) {
Josh Coalson90ced912002-05-30 05:23:38 +0000263 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
Josh Coalsoncc682512002-06-08 04:53:42 +0000297void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000298{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000326void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000327{
328 FLAC__metadata_object_delete_data(object);
329 free(object);
330}
331
Josh Coalsoncc682512002-06-08 04:53:42 +0000332static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000333{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000355static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000356{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000369static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000370{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000394static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000395{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000422FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000423{
Josh Coalsond8ab3462002-07-11 05:54:05 +0000424 FLAC__ASSERT(0 != block1);
425 FLAC__ASSERT(0 != block2);
426
Josh Coalson57ba6f42002-06-07 05:27:37 +0000427 if(block1->type != block2->type) {
428 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000429 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000430 if(block1->is_last != block2->is_last) {
431 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000432 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000433 if(block1->length != block2->length) {
434 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000435 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000436 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
Josh Coalsoncc682512002-06-08 04:53:42 +0000453FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
Josh Coalson90ced912002-05-30 05:23:38 +0000454{
455 FLAC__byte *save;
456
Josh Coalsond8ab3462002-07-11 05:54:05 +0000457 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000458 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
Josh Coalsoncc682512002-06-08 04:53:42 +0000479FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000480{
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 {
Josh Coalsoncc682512002-06-08 04:53:42 +0000492 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);
Josh Coalson90ced912002-05-30 05:23:38 +0000494
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000521void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
Josh Coalson90ced912002-05-30 05:23:38 +0000522{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000530FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
Josh Coalson90ced912002-05-30 05:23:38 +0000531{
532 int i;
533
534 FLAC__ASSERT(0 != object);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000535 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson90ced912002-05-30 05:23:38 +0000536 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
Josh Coalsoncc682512002-06-08 04:53:42 +0000550FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
Josh Coalson90ced912002-05-30 05:23:38 +0000551{
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000565FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
Josh Coalson28e08d82002-06-05 05:56:41 +0000566{
Josh Coalson28e08d82002-06-05 05:56:41 +0000567 FLAC__ASSERT(0 != object);
568 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
569
Josh Coalson0833f342002-07-15 05:31:55 +0000570 return FLAC__format_seektable_is_legal(&object->data.seek_table);
Josh Coalson28e08d82002-06-05 05:56:41 +0000571}
572
Josh Coalson5a5de732002-08-01 06:37:11 +0000573FLAC__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
585FLAC__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
604FLAC__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
629FLAC__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
654FLAC__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
Josh Coalsoncc682512002-06-08 04:53:42 +0000666FLAC__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 +0000667{
Josh Coalson6b8e5302002-05-31 06:23:09 +0000668 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +0000669}
670
Josh Coalsoncc682512002-06-08 04:53:42 +0000671FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000672{
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 {
Josh Coalsoncc682512002-06-08 04:53:42 +0000684 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);
Josh Coalson90ced912002-05-30 05:23:38 +0000686
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
Josh Coalsoncc682512002-06-08 04:53:42 +0000715FLAC__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 +0000716{
Josh Coalson6b8e5302002-05-31 06:23:09 +0000717 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +0000718}
719
Josh Coalsoncc682512002-06-08 04:53:42 +0000720FLAC__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 +0000721{
Josh Coalsoncc682512002-06-08 04:53:42 +0000722 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +0000723
724 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000725 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
726 FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num);
727
Josh Coalson6b8e5302002-05-31 06:23:09 +0000728 vc = &object->data.vorbis_comment;
729
730 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +0000731 return false;
732
733 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +0000734 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
Josh Coalson6b8e5302002-05-31 06:23:09 +0000735 vc->comments[comment_num].length = 0;
736 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +0000737
738 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
739}
740
Josh Coalsoncc682512002-06-08 04:53:42 +0000741FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +0000742{
Josh Coalsoncc682512002-06-08 04:53:42 +0000743 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +0000744
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
Josh Coalson6b8e5302002-05-31 06:23:09 +0000749 vc = &object->data.vorbis_comment;
750
Josh Coalson90ced912002-05-30 05:23:38 +0000751 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +0000752 if(0 != vc->comments[comment_num].entry)
753 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +0000754
755 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +0000756 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
Josh Coalson6b8e5302002-05-31 06:23:09 +0000757 vc->comments[vc->num_comments-1].length = 0;
758 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +0000759
Josh Coalson6b8e5302002-05-31 06:23:09 +0000760 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +0000761}