blob: d9ed7809bf7a2d99a438e0a14b8f3a4684c989e2 [file] [log] [blame]
Josh Coalson90ced912002-05-30 05:23:38 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalsondea0f5a2009-01-07 07:31:28 +00002 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
Josh Coalson90ced912002-05-30 05:23:38 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalson90ced912002-05-30 05:23:38 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalson90ced912002-05-30 05:23:38 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson90ced912002-05-30 05:23:38 +000030 */
31
Josh Coalsonb1ec7962006-05-24 04:41:36 +000032#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
Josh Coalson90ced912002-05-30 05:23:38 +000036#include <stdlib.h>
37#include <string.h>
38
39#include "private/metadata.h"
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +100040#include "private/memory.h"
Josh Coalson90ced912002-05-30 05:23:38 +000041
42#include "FLAC/assert.h"
Josh Coalson0f008d22007-09-11 04:49:56 +000043#include "share/alloc.h"
Erik de Castro Lopoa7e37052012-06-22 16:03:04 +100044#include "share/compat.h"
Josh Coalson90ced912002-05-30 05:23:38 +000045
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +100046/* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
47#define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
48
Josh Coalson90ced912002-05-30 05:23:38 +000049
50/****************************************************************************
51 *
52 * Local routines
53 *
54 ***************************************************************************/
55
Josh Coalsone343ab22006-09-23 19:21:19 +000056/* copy bytes:
57 * from = NULL && bytes = 0
58 * to <- NULL
59 * from != NULL && bytes > 0
60 * to <- copy of from
61 * else ASSERT
Josh Coalson0f008d22007-09-11 04:49:56 +000062 * malloc error leaves 'to' unchanged
Josh Coalsone343ab22006-09-23 19:21:19 +000063 */
Josh Coalson90ced912002-05-30 05:23:38 +000064static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
65{
Josh Coalsone343ab22006-09-23 19:21:19 +000066 FLAC__ASSERT(0 != to);
Josh Coalson90ced912002-05-30 05:23:38 +000067 if(bytes > 0 && 0 != from) {
68 FLAC__byte *x;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +100069 if(0 == (x = safe_malloc_(bytes)))
Josh Coalson90ced912002-05-30 05:23:38 +000070 return false;
71 memcpy(x, from, bytes);
72 *to = x;
73 }
74 else {
75 FLAC__ASSERT(0 == from);
76 FLAC__ASSERT(bytes == 0);
77 *to = 0;
78 }
79 return true;
80}
81
Josh Coalsone343ab22006-09-23 19:21:19 +000082#if 0 /* UNUSED */
83/* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */
84static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
85{
86 FLAC__byte *copy;
87 FLAC__ASSERT(0 != to);
88 if(copy_bytes_(&copy, from, bytes)) {
89 if(*to)
90 free(*to);
91 *to = copy;
92 return true;
93 }
94 else
95 return false;
96}
97#endif
98
99/* reallocate entry to 1 byte larger and add a terminating NUL */
100/* realloc() failure leaves entry unchanged */
Josh Coalsondef597e2004-12-30 00:59:30 +0000101static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
102{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000103 FLAC__byte *x = safe_realloc_add_2op_(*entry, length, /*+*/1);
Josh Coalsondef597e2004-12-30 00:59:30 +0000104 if(0 != x) {
105 x[length] = '\0';
106 *entry = x;
107 return true;
108 }
109 else
110 return false;
111}
112
Josh Coalsone343ab22006-09-23 19:21:19 +0000113/* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
114 * unchanged if malloc fails, free()ing the original '*to' if it
115 * succeeds and the original '*to' was not NULL
116 */
117static FLAC__bool copy_cstring_(char **to, const char *from)
118{
Josh Coalsone343ab22006-09-23 19:21:19 +0000119 char *copy = strdup(from);
Josh Coalsona65fd932006-10-03 01:02:44 +0000120 FLAC__ASSERT(to);
Josh Coalsone343ab22006-09-23 19:21:19 +0000121 if(copy) {
122 if(*to)
123 free(*to);
124 *to = copy;
125 return true;
126 }
127 else
128 return false;
129}
130
Josh Coalsoncc682512002-06-08 04:53:42 +0000131static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
Josh Coalson90ced912002-05-30 05:23:38 +0000132{
133 to->length = from->length;
134 if(0 == from->entry) {
135 FLAC__ASSERT(from->length == 0);
136 to->entry = 0;
137 }
138 else {
139 FLAC__byte *x;
140 FLAC__ASSERT(from->length > 0);
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000141 if(0 == (x = safe_malloc_add_2op_(from->length, /*+*/1)))
Josh Coalson90ced912002-05-30 05:23:38 +0000142 return false;
143 memcpy(x, from->entry, from->length);
Josh Coalsondef597e2004-12-30 00:59:30 +0000144 x[from->length] = '\0';
Josh Coalson90ced912002-05-30 05:23:38 +0000145 to->entry = x;
146 }
147 return true;
148}
149
Josh Coalson8e9c4512002-11-14 05:00:24 +0000150static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
151{
152 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
153 if(0 == from->indices) {
154 FLAC__ASSERT(from->num_indices == 0);
155 }
156 else {
157 FLAC__StreamMetadata_CueSheet_Index *x;
158 FLAC__ASSERT(from->num_indices > 0);
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +1000159 if(0 == (x = safe_malloc_mul_2op_p(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))))
Josh Coalson8e9c4512002-11-14 05:00:24 +0000160 return false;
161 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
162 to->indices = x;
163 }
164 return true;
165}
166
Josh Coalsoncc682512002-06-08 04:53:42 +0000167static void seektable_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000168{
169 FLAC__ASSERT(0 != object);
170 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
171
172 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
173}
174
Josh Coalsoncc682512002-06-08 04:53:42 +0000175static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000176{
Josh Coalsoncc682512002-06-08 04:53:42 +0000177 FLAC__StreamMetadata_SeekPoint *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000178
179 FLAC__ASSERT(num_points > 0);
180
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +1000181 object_array = safe_malloc_mul_2op_p(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
Josh Coalson90ced912002-05-30 05:23:38 +0000182
183 if(0 != object_array) {
184 unsigned i;
185 for(i = 0; i < num_points; i++) {
186 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
187 object_array[i].stream_offset = 0;
188 object_array[i].frame_samples = 0;
189 }
190 }
191
192 return object_array;
193}
194
Josh Coalsoncc682512002-06-08 04:53:42 +0000195static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000196{
197 unsigned i;
198
199 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
200
201 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
202 object->length += object->data.vorbis_comment.vendor_string.length;
203 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
204 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
205 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
206 object->length += object->data.vorbis_comment.comments[i].length;
207 }
208}
209
Josh Coalsoncc682512002-06-08 04:53:42 +0000210static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000211{
Josh Coalson90ced912002-05-30 05:23:38 +0000212 FLAC__ASSERT(num_comments > 0);
213
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000214 return safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000215}
216
Josh Coalsoncc682512002-06-08 04:53:42 +0000217static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000218{
219 unsigned i;
220
221 FLAC__ASSERT(0 != object_array && num_comments > 0);
222
223 for(i = 0; i < num_comments; i++)
224 if(0 != object_array[i].entry)
225 free(object_array[i].entry);
226
227 if(0 != object_array)
228 free(object_array);
229}
230
Josh Coalsoncc682512002-06-08 04:53:42 +0000231static 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 +0000232{
Josh Coalsoncc682512002-06-08 04:53:42 +0000233 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000234
235 FLAC__ASSERT(0 != object_array);
236 FLAC__ASSERT(num_comments > 0);
237
238 return_array = vorbiscomment_entry_array_new_(num_comments);
239
240 if(0 != return_array) {
241 unsigned i;
242
Josh Coalson90ced912002-05-30 05:23:38 +0000243 for(i = 0; i < num_comments; i++) {
244 if(!copy_vcentry_(return_array+i, object_array+i)) {
245 vorbiscomment_entry_array_delete_(return_array, num_comments);
246 return 0;
247 }
248 }
249 }
250
251 return return_array;
252}
253
Josh Coalsoncc682512002-06-08 04:53:42 +0000254static 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 +0000255{
256 FLAC__byte *save;
257
258 FLAC__ASSERT(0 != object);
259 FLAC__ASSERT(0 != dest);
260 FLAC__ASSERT(0 != src);
261 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson748459c2004-09-08 00:49:30 +0000262 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
Josh Coalson90ced912002-05-30 05:23:38 +0000263
Josh Coalson6b8e5302002-05-31 06:23:09 +0000264 save = dest->entry;
Josh Coalson90ced912002-05-30 05:23:38 +0000265
Josh Coalsondef597e2004-12-30 00:59:30 +0000266 if(0 != src->entry && src->length > 0) {
267 if(copy) {
268 /* do the copy first so that if we fail we leave the dest object untouched */
269 if(!copy_vcentry_(dest, src))
270 return false;
271 }
272 else {
273 /* we have to make sure that the string we're taking over is null-terminated */
274
275 /*
276 * Stripping the const from src->entry is OK since we're taking
277 * ownership of the pointer. This is a hack around a deficiency
278 * in the API where the same function is used for 'copy' and
279 * 'own', but the source entry is a const pointer. If we were
280 * precise, the 'own' flavor would be a separate function with a
281 * non-const source pointer. But it's not, so we hack away.
282 */
283 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
284 return false;
285 *dest = *src;
286 }
Josh Coalson90ced912002-05-30 05:23:38 +0000287 }
288 else {
Josh Coalsondef597e2004-12-30 00:59:30 +0000289 /* the src is null */
Josh Coalson90ced912002-05-30 05:23:38 +0000290 *dest = *src;
291 }
292
293 if(0 != save)
294 free(save);
295
296 vorbiscomment_calculate_length_(object);
297 return true;
298}
299
Josh Coalsondef597e2004-12-30 00:59:30 +0000300static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
301{
302 unsigned i;
303
304 FLAC__ASSERT(0 != object);
305 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
306 FLAC__ASSERT(0 != field_name);
307
308 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
309 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
310 return (int)i;
311 }
312
313 return -1;
314}
315
Josh Coalson8e9c4512002-11-14 05:00:24 +0000316static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
317{
318 unsigned i;
319
320 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
321
322 object->length = (
323 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
324 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000325 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
Josh Coalsondf7240a2002-11-16 06:32:30 +0000326 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
Josh Coalson8e9c4512002-11-14 05:00:24 +0000327 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
328 ) / 8;
329
330 object->length += object->data.cue_sheet.num_tracks * (
331 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
332 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
333 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
334 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
335 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
336 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
337 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
338 ) / 8;
339
340 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
341 object->length += object->data.cue_sheet.tracks[i].num_indices * (
342 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
343 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
344 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
345 ) / 8;
346 }
347}
348
349static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
350{
351 FLAC__ASSERT(num_indices > 0);
352
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000353 return safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000354}
355
356static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
357{
358 FLAC__ASSERT(num_tracks > 0);
359
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000360 return safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000361}
362
363static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
364{
365 unsigned i;
366
367 FLAC__ASSERT(0 != object_array && num_tracks > 0);
368
369 for(i = 0; i < num_tracks; i++) {
370 if(0 != object_array[i].indices) {
371 FLAC__ASSERT(object_array[i].num_indices > 0);
372 free(object_array[i].indices);
373 }
374 }
375
376 if(0 != object_array)
377 free(object_array);
378}
379
380static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
381{
382 FLAC__StreamMetadata_CueSheet_Track *return_array;
383
384 FLAC__ASSERT(0 != object_array);
385 FLAC__ASSERT(num_tracks > 0);
386
387 return_array = cuesheet_track_array_new_(num_tracks);
388
389 if(0 != return_array) {
390 unsigned i;
391
392 for(i = 0; i < num_tracks; i++) {
393 if(!copy_track_(return_array+i, object_array+i)) {
394 cuesheet_track_array_delete_(return_array, num_tracks);
395 return 0;
396 }
397 }
398 }
399
400 return return_array;
401}
402
403static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
404{
405 FLAC__StreamMetadata_CueSheet_Index *save;
406
407 FLAC__ASSERT(0 != object);
408 FLAC__ASSERT(0 != dest);
409 FLAC__ASSERT(0 != src);
410 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
411 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000412
413 save = dest->indices;
414
415 /* do the copy first so that if we fail we leave the object untouched */
416 if(copy) {
417 if(!copy_track_(dest, src))
418 return false;
419 }
420 else {
421 *dest = *src;
422 }
423
424 if(0 != save)
425 free(save);
426
427 cuesheet_calculate_length_(object);
428 return true;
429}
430
Josh Coalson90ced912002-05-30 05:23:38 +0000431
432/****************************************************************************
433 *
434 * Metadata object routines
435 *
436 ***************************************************************************/
437
Josh Coalson6afed9f2002-10-16 22:29:47 +0000438FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
Josh Coalson90ced912002-05-30 05:23:38 +0000439{
Josh Coalson1cb23412004-07-22 01:32:00 +0000440 FLAC__StreamMetadata *object;
441
442 if(type > FLAC__MAX_METADATA_TYPE_CODE)
443 return 0;
444
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000445 object = calloc(1, sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000446 if(0 != object) {
Josh Coalson90ced912002-05-30 05:23:38 +0000447 object->is_last = false;
448 object->type = type;
449 switch(type) {
450 case FLAC__METADATA_TYPE_STREAMINFO:
451 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
452 break;
453 case FLAC__METADATA_TYPE_PADDING:
Josh Coalsond0609472003-01-10 05:37:13 +0000454 /* calloc() took care of this for us:
455 object->length = 0;
456 */
Josh Coalson90ced912002-05-30 05:23:38 +0000457 break;
458 case FLAC__METADATA_TYPE_APPLICATION:
459 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
Josh Coalsond0609472003-01-10 05:37:13 +0000460 /* calloc() took care of this for us:
461 object->data.application.data = 0;
462 */
Josh Coalson90ced912002-05-30 05:23:38 +0000463 break;
464 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsond0609472003-01-10 05:37:13 +0000465 /* calloc() took care of this for us:
466 object->length = 0;
467 object->data.seek_table.num_points = 0;
468 object->data.seek_table.points = 0;
469 */
Josh Coalson90ced912002-05-30 05:23:38 +0000470 break;
471 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalsone343ab22006-09-23 19:21:19 +0000472 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
473 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
474 free(object);
475 return 0;
Josh Coalson45bb9882002-10-26 04:34:16 +0000476 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000477 vorbiscomment_calculate_length_(object);
Josh Coalson90ced912002-05-30 05:23:38 +0000478 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000479 case FLAC__METADATA_TYPE_CUESHEET:
480 cuesheet_calculate_length_(object);
481 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000482 case FLAC__METADATA_TYPE_PICTURE:
483 object->length = (
484 FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
485 FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
486 FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
487 FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
488 FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
489 FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
Josh Coalson74ed2942006-09-23 23:15:05 +0000490 FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
Josh Coalsone343ab22006-09-23 19:21:19 +0000491 FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
492 0 /* no data */
493 ) / 8;
494 object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
495 object->data.picture.mime_type = 0;
496 object->data.picture.description = 0;
497 /* calloc() took care of this for us:
498 object->data.picture.width = 0;
499 object->data.picture.height = 0;
500 object->data.picture.depth = 0;
Josh Coalson74ed2942006-09-23 23:15:05 +0000501 object->data.picture.colors = 0;
Josh Coalsone343ab22006-09-23 19:21:19 +0000502 object->data.picture.data_length = 0;
503 object->data.picture.data = 0;
504 */
505 /* now initialize mime_type and description with empty strings to make things easier on the client */
506 if(!copy_cstring_(&object->data.picture.mime_type, "")) {
507 free(object);
508 return 0;
509 }
510 if(!copy_cstring_((char**)(&object->data.picture.description), "")) {
511 if(object->data.picture.mime_type)
512 free(object->data.picture.mime_type);
513 free(object);
514 return 0;
515 }
516 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000517 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000518 /* calloc() took care of this for us:
519 object->length = 0;
520 object->data.unknown.data = 0;
521 */
522 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000523 }
524 }
525
526 return object;
527}
528
Josh Coalson6afed9f2002-10-16 22:29:47 +0000529FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000530{
Josh Coalsoncc682512002-06-08 04:53:42 +0000531 FLAC__StreamMetadata *to;
Josh Coalson90ced912002-05-30 05:23:38 +0000532
533 FLAC__ASSERT(0 != object);
534
535 if(0 != (to = FLAC__metadata_object_new(object->type))) {
536 to->is_last = object->is_last;
537 to->type = object->type;
538 to->length = object->length;
539 switch(to->type) {
540 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalsoncc682512002-06-08 04:53:42 +0000541 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
Josh Coalson90ced912002-05-30 05:23:38 +0000542 break;
543 case FLAC__METADATA_TYPE_PADDING:
544 break;
545 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson0f008d22007-09-11 04:49:56 +0000546 if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
547 FLAC__metadata_object_delete(to);
548 return 0;
549 }
Josh Coalson90ced912002-05-30 05:23:38 +0000550 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
551 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
552 FLAC__metadata_object_delete(to);
553 return 0;
554 }
555 break;
556 case FLAC__METADATA_TYPE_SEEKTABLE:
557 to->data.seek_table.num_points = object->data.seek_table.num_points;
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100558 if(to->data.seek_table.num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +0000559 FLAC__metadata_object_delete(to);
560 return 0;
561 }
Josh Coalsoncc682512002-06-08 04:53:42 +0000562 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 +0000563 FLAC__metadata_object_delete(to);
564 return 0;
565 }
566 break;
567 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000568 if(0 != to->data.vorbis_comment.vendor_string.entry) {
Josh Coalson45bb9882002-10-26 04:34:16 +0000569 free(to->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000570 to->data.vorbis_comment.vendor_string.entry = 0;
571 }
Josh Coalson90ced912002-05-30 05:23:38 +0000572 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
573 FLAC__metadata_object_delete(to);
574 return 0;
575 }
576 if(object->data.vorbis_comment.num_comments == 0) {
577 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
578 to->data.vorbis_comment.comments = 0;
579 }
580 else {
581 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
582 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
583 if(0 == to->data.vorbis_comment.comments) {
584 FLAC__metadata_object_delete(to);
585 return 0;
586 }
587 }
588 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
589 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000590 case FLAC__METADATA_TYPE_CUESHEET:
591 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
592 if(object->data.cue_sheet.num_tracks == 0) {
593 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
594 }
595 else {
596 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
597 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
598 if(0 == to->data.cue_sheet.tracks) {
599 FLAC__metadata_object_delete(to);
600 return 0;
601 }
602 }
603 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000604 case FLAC__METADATA_TYPE_PICTURE:
605 to->data.picture.type = object->data.picture.type;
606 if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
607 FLAC__metadata_object_delete(to);
608 return 0;
609 }
610 if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
611 FLAC__metadata_object_delete(to);
612 return 0;
613 }
614 to->data.picture.width = object->data.picture.width;
615 to->data.picture.height = object->data.picture.height;
616 to->data.picture.depth = object->data.picture.depth;
Josh Coalson74ed2942006-09-23 23:15:05 +0000617 to->data.picture.colors = object->data.picture.colors;
Josh Coalsone343ab22006-09-23 19:21:19 +0000618 to->data.picture.data_length = object->data.picture.data_length;
619 if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
620 FLAC__metadata_object_delete(to);
621 return 0;
622 }
623 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000624 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000625 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
626 FLAC__metadata_object_delete(to);
627 return 0;
628 }
629 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000630 }
631 }
632
633 return to;
634}
635
Josh Coalsoncc682512002-06-08 04:53:42 +0000636void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000637{
638 FLAC__ASSERT(0 != object);
639
640 switch(object->type) {
641 case FLAC__METADATA_TYPE_STREAMINFO:
642 case FLAC__METADATA_TYPE_PADDING:
643 break;
644 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson4fa90592002-12-04 07:01:37 +0000645 if(0 != object->data.application.data) {
Josh Coalson90ced912002-05-30 05:23:38 +0000646 free(object->data.application.data);
Josh Coalson4fa90592002-12-04 07:01:37 +0000647 object->data.application.data = 0;
648 }
Josh Coalson90ced912002-05-30 05:23:38 +0000649 break;
650 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalson4fa90592002-12-04 07:01:37 +0000651 if(0 != object->data.seek_table.points) {
Josh Coalson90ced912002-05-30 05:23:38 +0000652 free(object->data.seek_table.points);
Josh Coalson4fa90592002-12-04 07:01:37 +0000653 object->data.seek_table.points = 0;
654 }
Josh Coalson90ced912002-05-30 05:23:38 +0000655 break;
656 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000657 if(0 != object->data.vorbis_comment.vendor_string.entry) {
Josh Coalson90ced912002-05-30 05:23:38 +0000658 free(object->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000659 object->data.vorbis_comment.vendor_string.entry = 0;
660 }
Josh Coalson90ced912002-05-30 05:23:38 +0000661 if(0 != object->data.vorbis_comment.comments) {
662 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
663 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
664 }
665 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000666 case FLAC__METADATA_TYPE_CUESHEET:
667 if(0 != object->data.cue_sheet.tracks) {
668 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
669 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
670 }
671 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000672 case FLAC__METADATA_TYPE_PICTURE:
673 if(0 != object->data.picture.mime_type) {
674 free(object->data.picture.mime_type);
675 object->data.picture.mime_type = 0;
676 }
677 if(0 != object->data.picture.description) {
678 free(object->data.picture.description);
679 object->data.picture.description = 0;
680 }
681 if(0 != object->data.picture.data) {
682 free(object->data.picture.data);
683 object->data.picture.data = 0;
684 }
685 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000686 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000687 if(0 != object->data.unknown.data) {
688 free(object->data.unknown.data);
689 object->data.unknown.data = 0;
690 }
691 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000692 }
693}
694
Josh Coalson6afed9f2002-10-16 22:29:47 +0000695FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000696{
697 FLAC__metadata_object_delete_data(object);
698 free(object);
699}
700
Josh Coalsoncc682512002-06-08 04:53:42 +0000701static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000702{
703 if(block1->min_blocksize != block2->min_blocksize)
704 return false;
705 if(block1->max_blocksize != block2->max_blocksize)
706 return false;
707 if(block1->min_framesize != block2->min_framesize)
708 return false;
709 if(block1->max_framesize != block2->max_framesize)
710 return false;
711 if(block1->sample_rate != block2->sample_rate)
712 return false;
713 if(block1->channels != block2->channels)
714 return false;
715 if(block1->bits_per_sample != block2->bits_per_sample)
716 return false;
717 if(block1->total_samples != block2->total_samples)
718 return false;
719 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
720 return false;
721 return true;
722}
723
Josh Coalsoncc682512002-06-08 04:53:42 +0000724static 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 +0000725{
726 FLAC__ASSERT(0 != block1);
727 FLAC__ASSERT(0 != block2);
728 FLAC__ASSERT(block_length >= sizeof(block1->id));
729
730 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
731 return false;
732 if(0 != block1->data && 0 != block2->data)
733 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
734 else
735 return block1->data == block2->data;
736}
737
Josh Coalsoncc682512002-06-08 04:53:42 +0000738static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000739{
740 unsigned i;
741
742 FLAC__ASSERT(0 != block1);
743 FLAC__ASSERT(0 != block2);
744
745 if(block1->num_points != block2->num_points)
746 return false;
747
748 if(0 != block1->points && 0 != block2->points) {
749 for(i = 0; i < block1->num_points; i++) {
750 if(block1->points[i].sample_number != block2->points[i].sample_number)
751 return false;
752 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
753 return false;
754 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
755 return false;
756 }
757 return true;
758 }
759 else
760 return block1->points == block2->points;
761}
762
Josh Coalsoncc682512002-06-08 04:53:42 +0000763static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000764{
765 unsigned i;
766
767 if(block1->vendor_string.length != block2->vendor_string.length)
768 return false;
769
770 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
771 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
772 return false;
773 }
774 else if(block1->vendor_string.entry != block2->vendor_string.entry)
775 return false;
776
777 if(block1->num_comments != block2->num_comments)
778 return false;
779
780 for(i = 0; i < block1->num_comments; i++) {
781 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
782 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
783 return false;
784 }
785 else if(block1->comments[i].entry != block2->comments[i].entry)
786 return false;
787 }
788 return true;
789}
790
Josh Coalson8e9c4512002-11-14 05:00:24 +0000791static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
792{
793 unsigned i, j;
794
795 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
796 return false;
797
798 if(block1->lead_in != block2->lead_in)
799 return false;
800
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000801 if(block1->is_cd != block2->is_cd)
802 return false;
803
Josh Coalson8e9c4512002-11-14 05:00:24 +0000804 if(block1->num_tracks != block2->num_tracks)
805 return false;
806
807 if(0 != block1->tracks && 0 != block2->tracks) {
808 FLAC__ASSERT(block1->num_tracks > 0);
809 for(i = 0; i < block1->num_tracks; i++) {
810 if(block1->tracks[i].offset != block2->tracks[i].offset)
811 return false;
812 if(block1->tracks[i].number != block2->tracks[i].number)
813 return false;
814 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
815 return false;
816 if(block1->tracks[i].type != block2->tracks[i].type)
817 return false;
818 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
819 return false;
820 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
821 return false;
822 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
823 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
824 for(j = 0; j < block1->tracks[i].num_indices; j++) {
825 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
826 return false;
827 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
828 return false;
829 }
830 }
831 else if(block1->tracks[i].indices != block2->tracks[i].indices)
832 return false;
833 }
834 }
835 else if(block1->tracks != block2->tracks)
836 return false;
837 return true;
838}
839
Josh Coalsone343ab22006-09-23 19:21:19 +0000840static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
841{
842 if(block1->type != block2->type)
843 return false;
844 if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type)))
845 return false;
846 if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description)))
847 return false;
848 if(block1->width != block2->width)
849 return false;
850 if(block1->height != block2->height)
851 return false;
852 if(block1->depth != block2->depth)
853 return false;
Josh Coalson74ed2942006-09-23 23:15:05 +0000854 if(block1->colors != block2->colors)
855 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +0000856 if(block1->data_length != block2->data_length)
857 return false;
858 if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length)))
859 return false;
860 return true;
861}
862
Josh Coalsond0609472003-01-10 05:37:13 +0000863static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
864{
865 FLAC__ASSERT(0 != block1);
866 FLAC__ASSERT(0 != block2);
867
868 if(0 != block1->data && 0 != block2->data)
869 return 0 == memcmp(block1->data, block2->data, block_length);
870 else
871 return block1->data == block2->data;
872}
873
Josh Coalson6afed9f2002-10-16 22:29:47 +0000874FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000875{
Josh Coalsond8ab3462002-07-11 05:54:05 +0000876 FLAC__ASSERT(0 != block1);
877 FLAC__ASSERT(0 != block2);
878
Josh Coalson57ba6f42002-06-07 05:27:37 +0000879 if(block1->type != block2->type) {
880 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000881 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000882 if(block1->is_last != block2->is_last) {
883 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000884 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000885 if(block1->length != block2->length) {
886 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000887 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000888 switch(block1->type) {
889 case FLAC__METADATA_TYPE_STREAMINFO:
890 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
891 case FLAC__METADATA_TYPE_PADDING:
892 return true; /* we don't compare the padding guts */
893 case FLAC__METADATA_TYPE_APPLICATION:
894 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
895 case FLAC__METADATA_TYPE_SEEKTABLE:
896 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
897 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
898 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000899 case FLAC__METADATA_TYPE_CUESHEET:
900 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +0000901 case FLAC__METADATA_TYPE_PICTURE:
902 return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000903 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000904 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000905 }
906}
907
Josh Coalson6afed9f2002-10-16 22:29:47 +0000908FLAC_API FLAC__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 +0000909{
910 FLAC__byte *save;
911
Josh Coalsond8ab3462002-07-11 05:54:05 +0000912 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000913 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
914 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
915
916 save = object->data.application.data;
917
918 /* do the copy first so that if we fail we leave the object untouched */
919 if(copy) {
920 if(!copy_bytes_(&object->data.application.data, data, length))
921 return false;
922 }
923 else {
924 object->data.application.data = data;
925 }
926
927 if(0 != save)
928 free(save);
929
930 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
931 return true;
932}
933
Josh Coalson6afed9f2002-10-16 22:29:47 +0000934FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000935{
936 FLAC__ASSERT(0 != object);
937 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
938
939 if(0 == object->data.seek_table.points) {
940 FLAC__ASSERT(object->data.seek_table.num_points == 0);
941 if(0 == new_num_points)
942 return true;
943 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
944 return false;
945 }
946 else {
Josh Coalson0f008d22007-09-11 04:49:56 +0000947 const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
948 const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
949
950 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100951 if(new_num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
Josh Coalson0f008d22007-09-11 04:49:56 +0000952 return false;
Josh Coalson90ced912002-05-30 05:23:38 +0000953
954 FLAC__ASSERT(object->data.seek_table.num_points > 0);
955
956 if(new_size == 0) {
957 free(object->data.seek_table.points);
958 object->data.seek_table.points = 0;
959 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000960 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +0000961 return false;
962
963 /* if growing, set new elements to placeholders */
964 if(new_size > old_size) {
965 unsigned i;
966 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
967 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
968 object->data.seek_table.points[i].stream_offset = 0;
969 object->data.seek_table.points[i].frame_samples = 0;
970 }
971 }
972 }
973
974 object->data.seek_table.num_points = new_num_points;
975
976 seektable_calculate_length_(object);
977 return true;
978}
979
Josh Coalson6afed9f2002-10-16 22:29:47 +0000980FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
Josh Coalson90ced912002-05-30 05:23:38 +0000981{
982 FLAC__ASSERT(0 != object);
983 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000984 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000985
986 object->data.seek_table.points[point_num] = point;
987}
988
Josh Coalson6afed9f2002-10-16 22:29:47 +0000989FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
Josh Coalson90ced912002-05-30 05:23:38 +0000990{
991 int i;
992
993 FLAC__ASSERT(0 != object);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000994 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000995 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000996
997 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
998 return false;
999
1000 /* move all points >= point_num forward one space */
1001 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
1002 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
1003
1004 FLAC__metadata_object_seektable_set_point(object, point_num, point);
1005 seektable_calculate_length_(object);
1006 return true;
1007}
1008
Josh Coalson6afed9f2002-10-16 22:29:47 +00001009FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001010{
1011 unsigned i;
1012
1013 FLAC__ASSERT(0 != object);
1014 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001015 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +00001016
1017 /* move all points > point_num backward one space */
1018 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
1019 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
1020
1021 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
1022}
1023
Josh Coalson6afed9f2002-10-16 22:29:47 +00001024FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
Josh Coalson28e08d82002-06-05 05:56:41 +00001025{
Josh Coalson28e08d82002-06-05 05:56:41 +00001026 FLAC__ASSERT(0 != object);
1027 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1028
Josh Coalson0833f342002-07-15 05:31:55 +00001029 return FLAC__format_seektable_is_legal(&object->data.seek_table);
Josh Coalson28e08d82002-06-05 05:56:41 +00001030}
1031
Josh Coalson6afed9f2002-10-16 22:29:47 +00001032FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
Josh Coalson5a5de732002-08-01 06:37:11 +00001033{
1034 FLAC__ASSERT(0 != object);
1035 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1036
1037 if(num > 0)
1038 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
1039 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
1040 else
1041 return true;
1042}
1043
Josh Coalson6afed9f2002-10-16 22:29:47 +00001044FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
Josh Coalson5a5de732002-08-01 06:37:11 +00001045{
1046 FLAC__StreamMetadata_SeekTable *seek_table;
1047
1048 FLAC__ASSERT(0 != object);
1049 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1050
1051 seek_table = &object->data.seek_table;
1052
1053 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
1054 return false;
1055
1056 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
1057 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
1058 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
1059
1060 return true;
1061}
1062
Josh Coalson6afed9f2002-10-16 22:29:47 +00001063FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
Josh Coalson5a5de732002-08-01 06:37:11 +00001064{
1065 FLAC__ASSERT(0 != object);
1066 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1067 FLAC__ASSERT(0 != sample_numbers || num == 0);
1068
1069 if(num > 0) {
1070 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1071 unsigned i, j;
1072
1073 i = seek_table->num_points;
1074
1075 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1076 return false;
1077
1078 for(j = 0; j < num; i++, j++) {
1079 seek_table->points[i].sample_number = sample_numbers[j];
1080 seek_table->points[i].stream_offset = 0;
1081 seek_table->points[i].frame_samples = 0;
1082 }
1083 }
1084
1085 return true;
1086}
1087
Josh Coalson6afed9f2002-10-16 22:29:47 +00001088FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
Josh Coalson5a5de732002-08-01 06:37:11 +00001089{
1090 FLAC__ASSERT(0 != object);
1091 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1092 FLAC__ASSERT(total_samples > 0);
1093
Josh Coalson6b21f662006-09-13 01:42:27 +00001094 if(num > 0 && total_samples > 0) {
Josh Coalson5a5de732002-08-01 06:37:11 +00001095 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1096 unsigned i, j;
1097
1098 i = seek_table->num_points;
1099
1100 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1101 return false;
1102
1103 for(j = 0; j < num; i++, j++) {
1104 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
1105 seek_table->points[i].stream_offset = 0;
1106 seek_table->points[i].frame_samples = 0;
1107 }
1108 }
1109
1110 return true;
1111}
1112
Josh Coalson6b21f662006-09-13 01:42:27 +00001113FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
1114{
1115 FLAC__ASSERT(0 != object);
1116 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1117 FLAC__ASSERT(samples > 0);
1118 FLAC__ASSERT(total_samples > 0);
1119
1120 if(samples > 0 && total_samples > 0) {
1121 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1122 unsigned i, j;
1123 FLAC__uint64 num, sample;
1124
1125 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
1126 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
1127 if(total_samples % samples == 0)
1128 num--;
1129
1130 i = seek_table->num_points;
1131
Josh Coalsona65fd932006-10-03 01:02:44 +00001132 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
Josh Coalson6b21f662006-09-13 01:42:27 +00001133 return false;
1134
1135 sample = 0;
1136 for(j = 0; j < num; i++, j++, sample += samples) {
1137 seek_table->points[i].sample_number = sample;
1138 seek_table->points[i].stream_offset = 0;
1139 seek_table->points[i].frame_samples = 0;
1140 }
1141 }
1142
1143 return true;
1144}
1145
Josh Coalson6afed9f2002-10-16 22:29:47 +00001146FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +00001147{
1148 unsigned unique;
1149
1150 FLAC__ASSERT(0 != object);
1151 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1152
1153 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1154
1155 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1156}
1157
Josh Coalson6afed9f2002-10-16 22:29:47 +00001158FLAC_API FLAC__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 +00001159{
Josh Coalson2de11242004-12-30 03:41:19 +00001160 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1161 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001162 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001163}
1164
Josh Coalson6afed9f2002-10-16 22:29:47 +00001165FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +00001166{
1167 FLAC__ASSERT(0 != object);
1168 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1169
1170 if(0 == object->data.vorbis_comment.comments) {
1171 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1172 if(0 == new_num_comments)
1173 return true;
1174 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1175 return false;
1176 }
1177 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001178 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1179 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1180
1181 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001182 if(new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
Josh Coalson0f008d22007-09-11 04:49:56 +00001183 return false;
Josh Coalson90ced912002-05-30 05:23:38 +00001184
1185 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1186
1187 /* if shrinking, free the truncated entries */
1188 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1189 unsigned i;
1190 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1191 if(0 != object->data.vorbis_comment.comments[i].entry)
1192 free(object->data.vorbis_comment.comments[i].entry);
1193 }
1194
1195 if(new_size == 0) {
1196 free(object->data.vorbis_comment.comments);
1197 object->data.vorbis_comment.comments = 0;
1198 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001199 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +00001200 return false;
1201
1202 /* if growing, zero all the length/pointers of new elements */
1203 if(new_size > old_size)
1204 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1205 }
1206
1207 object->data.vorbis_comment.num_comments = new_num_comments;
1208
1209 vorbiscomment_calculate_length_(object);
1210 return true;
1211}
1212
Josh Coalson6afed9f2002-10-16 22:29:47 +00001213FLAC_API FLAC__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 +00001214{
Josh Coalsonfb993862003-01-15 03:18:07 +00001215 FLAC__ASSERT(0 != object);
1216 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1217
Josh Coalson2de11242004-12-30 03:41:19 +00001218 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1219 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001220 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001221}
1222
Josh Coalson6afed9f2002-10-16 22:29:47 +00001223FLAC_API FLAC__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 +00001224{
Josh Coalsoncc682512002-06-08 04:53:42 +00001225 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001226
1227 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001228 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001229 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001230
Josh Coalson2de11242004-12-30 03:41:19 +00001231 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1232 return false;
1233
Josh Coalson6b8e5302002-05-31 06:23:09 +00001234 vc = &object->data.vorbis_comment;
1235
1236 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001237 return false;
1238
1239 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001240 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 +00001241 vc->comments[comment_num].length = 0;
1242 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001243
1244 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1245}
1246
Josh Coalsondef597e2004-12-30 00:59:30 +00001247FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1248{
1249 FLAC__ASSERT(0 != object);
1250 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1251 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1252}
1253
1254FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1255{
1256 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
Josh Coalson2de11242004-12-30 03:41:19 +00001257
1258 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1259 return false;
1260
Josh Coalsondef597e2004-12-30 00:59:30 +00001261 {
1262 int i;
Josh Coalson9bedd782007-02-22 01:37:33 +00001263 size_t field_name_length;
Josh Coalsondef597e2004-12-30 00:59:30 +00001264 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1265
1266 FLAC__ASSERT(0 != eq);
1267
1268 if(0 == eq)
1269 return false; /* double protection */
1270
1271 field_name_length = eq-entry.entry;
1272
Josh Coalsone95399c2008-09-15 05:37:27 +00001273 i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
1274 if(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001275 unsigned indx = (unsigned)i;
1276 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, indx, entry, copy))
Josh Coalsondef597e2004-12-30 00:59:30 +00001277 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001278 entry = object->data.vorbis_comment.comments[indx];
1279 indx++; /* skip over replaced comment */
1280 if(all && indx < object->data.vorbis_comment.num_comments) {
1281 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsone95399c2008-09-15 05:37:27 +00001282 while(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001283 indx = (unsigned)i;
1284 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, indx))
Josh Coalsondef597e2004-12-30 00:59:30 +00001285 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001286 if(indx < object->data.vorbis_comment.num_comments)
1287 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001288 else
1289 i = -1;
1290 }
1291 }
1292 return true;
1293 }
1294 else
1295 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1296 }
1297}
1298
Josh Coalson6afed9f2002-10-16 22:29:47 +00001299FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001300{
Josh Coalsoncc682512002-06-08 04:53:42 +00001301 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001302
1303 FLAC__ASSERT(0 != object);
1304 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001305 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001306
Josh Coalson6b8e5302002-05-31 06:23:09 +00001307 vc = &object->data.vorbis_comment;
1308
Josh Coalson90ced912002-05-30 05:23:38 +00001309 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +00001310 if(0 != vc->comments[comment_num].entry)
1311 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001312
1313 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001314 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 +00001315 vc->comments[vc->num_comments-1].length = 0;
1316 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001317
Josh Coalson6b8e5302002-05-31 06:23:09 +00001318 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +00001319}
Josh Coalson45bb9882002-10-26 04:34:16 +00001320
Josh Coalsondef597e2004-12-30 00:59:30 +00001321FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, const char *field_value)
Josh Coalson45bb9882002-10-26 04:34:16 +00001322{
Josh Coalsondef597e2004-12-30 00:59:30 +00001323 FLAC__ASSERT(0 != entry);
1324 FLAC__ASSERT(0 != field_name);
1325 FLAC__ASSERT(0 != field_value);
1326
Josh Coalson2de11242004-12-30 03:41:19 +00001327 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1328 return false;
Josh Coalsonb4de1692005-08-24 07:39:25 +00001329 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
Josh Coalson2de11242004-12-30 03:41:19 +00001330 return false;
1331
Josh Coalsondef597e2004-12-30 00:59:30 +00001332 {
1333 const size_t nn = strlen(field_name);
1334 const size_t nv = strlen(field_value);
1335 entry->length = nn + 1 /*=*/ + nv;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001336 if(0 == (entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001337 return false;
1338 memcpy(entry->entry, field_name, nn);
1339 entry->entry[nn] = '=';
1340 memcpy(entry->entry+nn+1, field_value, nv);
1341 entry->entry[entry->length] = '\0';
1342 }
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001343
Josh Coalsondef597e2004-12-30 00:59:30 +00001344 return true;
1345}
1346
1347FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1348{
1349 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1350 FLAC__ASSERT(0 != field_name);
1351 FLAC__ASSERT(0 != field_value);
Josh Coalson2de11242004-12-30 03:41:19 +00001352
1353 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1354 return false;
1355
Josh Coalsondef597e2004-12-30 00:59:30 +00001356 {
1357 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1358 const size_t nn = eq-entry.entry;
1359 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1360 FLAC__ASSERT(0 != eq);
1361 if(0 == eq)
1362 return false; /* double protection */
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001363 if(0 == (*field_name = safe_malloc_add_2op_(nn, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001364 return false;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001365 if(0 == (*field_value = safe_malloc_add_2op_(nv, /*+*/1))) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001366 free(*field_name);
1367 return false;
1368 }
1369 memcpy(*field_name, entry.entry, nn);
1370 memcpy(*field_value, entry.entry+nn+1, nv);
1371 (*field_name)[nn] = '\0';
1372 (*field_value)[nv] = '\0';
1373 }
1374
1375 return true;
1376}
1377
Josh Coalson44623112005-01-30 18:15:36 +00001378FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
Josh Coalsondef597e2004-12-30 00:59:30 +00001379{
1380 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1381 {
1382 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001383 return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
Josh Coalsondef597e2004-12-30 00:59:30 +00001384 }
Josh Coalson45bb9882002-10-26 04:34:16 +00001385}
1386
Josh Coalsonb667e702002-11-05 07:24:33 +00001387FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
Josh Coalson45bb9882002-10-26 04:34:16 +00001388{
Josh Coalsondef597e2004-12-30 00:59:30 +00001389 FLAC__ASSERT(0 != field_name);
Josh Coalson45bb9882002-10-26 04:34:16 +00001390
Josh Coalsondef597e2004-12-30 00:59:30 +00001391 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
Josh Coalson45bb9882002-10-26 04:34:16 +00001392}
1393
1394FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1395{
1396 const unsigned field_name_length = strlen(field_name);
1397 unsigned i;
1398
1399 FLAC__ASSERT(0 != object);
1400 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1401
1402 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001403 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
Josh Coalson45bb9882002-10-26 04:34:16 +00001404 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1405 return -1;
1406 else
1407 return 1;
1408 }
1409 }
1410
1411 return 0;
1412}
1413
1414FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1415{
1416 FLAC__bool ok = true;
1417 unsigned matching = 0;
1418 const unsigned field_name_length = strlen(field_name);
1419 int i;
1420
1421 FLAC__ASSERT(0 != object);
1422 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1423
1424 /* must delete from end to start otherwise it will interfere with our iteration */
1425 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001426 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
Josh Coalson45bb9882002-10-26 04:34:16 +00001427 matching++;
1428 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1429 }
1430 }
1431
1432 return ok? (int)matching : -1;
1433}
Josh Coalson8e9c4512002-11-14 05:00:24 +00001434
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001435FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
Josh Coalsondf7240a2002-11-16 06:32:30 +00001436{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001437 return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
Josh Coalsondf7240a2002-11-16 06:32:30 +00001438}
1439
1440FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1441{
1442 FLAC__StreamMetadata_CueSheet_Track *to;
1443
1444 FLAC__ASSERT(0 != object);
1445
1446 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1447 if(!copy_track_(to, object)) {
1448 FLAC__metadata_object_cuesheet_track_delete(to);
1449 return 0;
1450 }
1451 }
1452
1453 return to;
1454}
1455
1456void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1457{
1458 FLAC__ASSERT(0 != object);
1459
1460 if(0 != object->indices) {
1461 FLAC__ASSERT(object->num_indices > 0);
1462 free(object->indices);
1463 }
1464}
1465
1466FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1467{
1468 FLAC__metadata_object_cuesheet_track_delete_data(object);
1469 free(object);
1470}
1471
Josh Coalson8e9c4512002-11-14 05:00:24 +00001472FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1473{
1474 FLAC__StreamMetadata_CueSheet_Track *track;
1475 FLAC__ASSERT(0 != object);
1476 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1477 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1478
1479 track = &object->data.cue_sheet.tracks[track_num];
1480
1481 if(0 == track->indices) {
1482 FLAC__ASSERT(track->num_indices == 0);
1483 if(0 == new_num_indices)
1484 return true;
1485 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1486 return false;
1487 }
1488 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001489 const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1490 const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1491
1492 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001493 if(new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
Josh Coalson0f008d22007-09-11 04:49:56 +00001494 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001495
1496 FLAC__ASSERT(track->num_indices > 0);
1497
1498 if(new_size == 0) {
1499 free(track->indices);
1500 track->indices = 0;
1501 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001502 else if(0 == (track->indices = realloc(track->indices, new_size)))
Josh Coalson8e9c4512002-11-14 05:00:24 +00001503 return false;
1504
1505 /* if growing, zero all the lengths/pointers of new elements */
1506 if(new_size > old_size)
1507 memset(track->indices + track->num_indices, 0, new_size - old_size);
1508 }
1509
1510 track->num_indices = new_num_indices;
1511
1512 cuesheet_calculate_length_(object);
1513 return true;
1514}
1515
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001516FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index indx)
Josh Coalson8e9c4512002-11-14 05:00:24 +00001517{
1518 FLAC__StreamMetadata_CueSheet_Track *track;
1519
1520 FLAC__ASSERT(0 != object);
1521 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1522 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1523 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1524
1525 track = &object->data.cue_sheet.tracks[track_num];
1526
1527 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1528 return false;
1529
1530 /* move all indices >= index_num forward one space */
1531 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1532
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001533 track->indices[index_num] = indx;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001534 cuesheet_calculate_length_(object);
1535 return true;
1536}
1537
Josh Coalson3b026e82002-11-21 06:41:01 +00001538FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1539{
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001540 FLAC__StreamMetadata_CueSheet_Index indx;
1541 memset(&indx, 0, sizeof(indx));
1542 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, indx);
Josh Coalson3b026e82002-11-21 06:41:01 +00001543}
1544
Josh Coalson8e9c4512002-11-14 05:00:24 +00001545FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1546{
1547 FLAC__StreamMetadata_CueSheet_Track *track;
1548
1549 FLAC__ASSERT(0 != object);
1550 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1551 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1552 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1553
1554 track = &object->data.cue_sheet.tracks[track_num];
1555
1556 /* move all indices > index_num backward one space */
Josh Coalson4fa90592002-12-04 07:01:37 +00001557 memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
Josh Coalson8e9c4512002-11-14 05:00:24 +00001558
1559 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1560 cuesheet_calculate_length_(object);
1561 return true;
1562}
1563
1564FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1565{
1566 FLAC__ASSERT(0 != object);
1567 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1568
1569 if(0 == object->data.cue_sheet.tracks) {
1570 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1571 if(0 == new_num_tracks)
1572 return true;
1573 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1574 return false;
1575 }
1576 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001577 const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1578 const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1579
1580 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001581 if(new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
Josh Coalson0f008d22007-09-11 04:49:56 +00001582 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001583
1584 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1585
1586 /* if shrinking, free the truncated entries */
1587 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1588 unsigned i;
1589 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1590 if(0 != object->data.cue_sheet.tracks[i].indices)
1591 free(object->data.cue_sheet.tracks[i].indices);
1592 }
1593
1594 if(new_size == 0) {
1595 free(object->data.cue_sheet.tracks);
1596 object->data.cue_sheet.tracks = 0;
1597 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001598 else if(0 == (object->data.cue_sheet.tracks = realloc(object->data.cue_sheet.tracks, new_size)))
Josh Coalson8e9c4512002-11-14 05:00:24 +00001599 return false;
1600
1601 /* if growing, zero all the lengths/pointers of new elements */
1602 if(new_size > old_size)
1603 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1604 }
1605
1606 object->data.cue_sheet.num_tracks = new_num_tracks;
1607
1608 cuesheet_calculate_length_(object);
1609 return true;
1610}
1611
Josh Coalsondf7240a2002-11-16 06:32:30 +00001612FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
Josh Coalson8e9c4512002-11-14 05:00:24 +00001613{
Josh Coalsonfb993862003-01-15 03:18:07 +00001614 FLAC__ASSERT(0 != object);
1615 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1616
Josh Coalsondf7240a2002-11-16 06:32:30 +00001617 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001618}
1619
Josh Coalsondf7240a2002-11-16 06:32:30 +00001620FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
Josh Coalson8e9c4512002-11-14 05:00:24 +00001621{
1622 FLAC__StreamMetadata_CueSheet *cs;
1623
1624 FLAC__ASSERT(0 != object);
1625 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1626 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1627
1628 cs = &object->data.cue_sheet;
1629
1630 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1631 return false;
1632
1633 /* move all tracks >= track_num forward one space */
1634 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1635 cs->tracks[track_num].num_indices = 0;
1636 cs->tracks[track_num].indices = 0;
1637
1638 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1639}
1640
Josh Coalson3b026e82002-11-21 06:41:01 +00001641FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1642{
1643 FLAC__StreamMetadata_CueSheet_Track track;
1644 memset(&track, 0, sizeof(track));
1645 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1646}
1647
Josh Coalson8e9c4512002-11-14 05:00:24 +00001648FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1649{
1650 FLAC__StreamMetadata_CueSheet *cs;
1651
1652 FLAC__ASSERT(0 != object);
1653 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1654 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1655
1656 cs = &object->data.cue_sheet;
1657
1658 /* free the track at track_num */
1659 if(0 != cs->tracks[track_num].indices)
1660 free(cs->tracks[track_num].indices);
1661
1662 /* move all tracks > track_num backward one space */
1663 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1664 cs->tracks[cs->num_tracks-1].num_indices = 0;
1665 cs->tracks[cs->num_tracks-1].indices = 0;
1666
1667 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1668}
1669
1670FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1671{
1672 FLAC__ASSERT(0 != object);
1673 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1674
1675 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1676}
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001677
1678static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1679{
1680 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1681 return 0;
1682 else if (cs->tracks[track].indices[0].number == 1)
1683 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1684 else if (cs->tracks[track].num_indices < 2)
1685 return 0;
1686 else if (cs->tracks[track].indices[1].number == 1)
1687 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1688 else
1689 return 0;
1690}
1691
1692static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1693{
1694 FLAC__uint32 n = 0;
1695 while (x) {
1696 n += (x%10);
1697 x /= 10;
1698 }
1699 return n;
1700}
1701
Josh Coalson504dcaf2007-09-13 15:42:47 +00001702/*@@@@add to tests*/
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001703FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1704{
1705 const FLAC__StreamMetadata_CueSheet *cs;
1706
1707 FLAC__ASSERT(0 != object);
1708 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1709
1710 cs = &object->data.cue_sheet;
1711
1712 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1713 return 0;
1714
1715 {
1716 FLAC__uint32 i, length, sum = 0;
1717 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1718 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1719 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1720
1721 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1722 }
1723}
Josh Coalsone343ab22006-09-23 19:21:19 +00001724
1725FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1726{
1727 char *old;
1728 size_t old_length, new_length;
1729
1730 FLAC__ASSERT(0 != object);
1731 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1732 FLAC__ASSERT(0 != mime_type);
1733
1734 old = object->data.picture.mime_type;
1735 old_length = old? strlen(old) : 0;
1736 new_length = strlen(mime_type);
1737
1738 /* do the copy first so that if we fail we leave the object untouched */
1739 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001740 if(new_length >= SIZE_MAX) /* overflow check */
1741 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001742 if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1743 return false;
1744 }
1745 else {
1746 object->data.picture.mime_type = mime_type;
1747 }
1748
1749 if(0 != old)
1750 free(old);
1751
1752 object->length -= old_length;
1753 object->length += new_length;
1754 return true;
1755}
1756
1757FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1758{
1759 FLAC__byte *old;
1760 size_t old_length, new_length;
1761
1762 FLAC__ASSERT(0 != object);
1763 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1764 FLAC__ASSERT(0 != description);
1765
1766 old = object->data.picture.description;
1767 old_length = old? strlen((const char *)old) : 0;
1768 new_length = strlen((const char *)description);
1769
1770 /* do the copy first so that if we fail we leave the object untouched */
1771 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001772 if(new_length >= SIZE_MAX) /* overflow check */
1773 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001774 if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
1775 return false;
1776 }
1777 else {
1778 object->data.picture.description = description;
1779 }
1780
1781 if(0 != old)
1782 free(old);
1783
1784 object->length -= old_length;
1785 object->length += new_length;
1786 return true;
1787}
1788
1789FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1790{
1791 FLAC__byte *old;
1792
1793 FLAC__ASSERT(0 != object);
1794 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1795 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
1796
1797 old = object->data.picture.data;
1798
1799 /* do the copy first so that if we fail we leave the object untouched */
1800 if(copy) {
1801 if(!copy_bytes_(&object->data.picture.data, data, length))
1802 return false;
1803 }
1804 else {
1805 object->data.picture.data = data;
1806 }
1807
1808 if(0 != old)
1809 free(old);
1810
1811 object->length -= object->data.picture.data_length;
1812 object->data.picture.data_length = length;
1813 object->length += length;
1814 return true;
1815}
1816
1817FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1818{
1819 FLAC__ASSERT(0 != object);
1820 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1821
1822 return FLAC__format_picture_is_legal(&object->data.picture, violation);
1823}