blob: df1cb5ec24699b8450eb343df5ca9b1867a69696 [file] [log] [blame]
Josh Coalson90ced912002-05-30 05:23:38 +00001/* libFLAC - Free Lossless Audio Codec library
Erik de Castro Lopob1982fb2013-05-25 17:11:19 +10002 * Copyright (C) 2001-2009 Josh Coalson
Erik de Castro Lopo14373912014-11-24 22:07:15 +11003 * Copyright (C) 2011-2014 Xiph.Org Foundation
Josh Coalson90ced912002-05-30 05:23:38 +00004 *
Josh Coalsonafd81072003-01-31 23:34:56 +00005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
Josh Coalson90ced912002-05-30 05:23:38 +00008 *
Josh Coalsonafd81072003-01-31 23:34:56 +00009 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
Josh Coalson90ced912002-05-30 05:23:38 +000011 *
Josh Coalsonafd81072003-01-31 23:34:56 +000012 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson90ced912002-05-30 05:23:38 +000031 */
32
Erik de Castro Lopo006b8352014-03-23 21:59:46 +110033#ifdef HAVE_CONFIG_H
Josh Coalsonb1ec7962006-05-24 04:41:36 +000034# include <config.h>
35#endif
36
Josh Coalson90ced912002-05-30 05:23:38 +000037#include <stdlib.h>
38#include <string.h>
39
40#include "private/metadata.h"
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +100041#include "private/memory.h"
Josh Coalson90ced912002-05-30 05:23:38 +000042
43#include "FLAC/assert.h"
Josh Coalson0f008d22007-09-11 04:49:56 +000044#include "share/alloc.h"
Erik de Castro Lopoa7e37052012-06-22 16:03:04 +100045#include "share/compat.h"
Josh Coalson90ced912002-05-30 05:23:38 +000046
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +100047/* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
48#define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
49
Josh Coalson90ced912002-05-30 05:23:38 +000050
51/****************************************************************************
52 *
53 * Local routines
54 *
55 ***************************************************************************/
56
Josh Coalsone343ab22006-09-23 19:21:19 +000057/* copy bytes:
58 * from = NULL && bytes = 0
59 * to <- NULL
60 * from != NULL && bytes > 0
61 * to <- copy of from
62 * else ASSERT
Josh Coalson0f008d22007-09-11 04:49:56 +000063 * malloc error leaves 'to' unchanged
Josh Coalsone343ab22006-09-23 19:21:19 +000064 */
Josh Coalson90ced912002-05-30 05:23:38 +000065static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
66{
Josh Coalsone343ab22006-09-23 19:21:19 +000067 FLAC__ASSERT(0 != to);
Josh Coalson90ced912002-05-30 05:23:38 +000068 if(bytes > 0 && 0 != from) {
69 FLAC__byte *x;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +100070 if(0 == (x = safe_malloc_(bytes)))
Josh Coalson90ced912002-05-30 05:23:38 +000071 return false;
72 memcpy(x, from, bytes);
73 *to = x;
74 }
75 else {
76 FLAC__ASSERT(0 == from);
77 FLAC__ASSERT(bytes == 0);
78 *to = 0;
79 }
80 return true;
81}
82
Josh Coalsone343ab22006-09-23 19:21:19 +000083#if 0 /* UNUSED */
84/* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */
85static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
86{
87 FLAC__byte *copy;
88 FLAC__ASSERT(0 != to);
89 if(copy_bytes_(&copy, from, bytes)) {
90 if(*to)
91 free(*to);
92 *to = copy;
93 return true;
94 }
95 else
96 return false;
97}
98#endif
99
100/* reallocate entry to 1 byte larger and add a terminating NUL */
101/* realloc() failure leaves entry unchanged */
Josh Coalsondef597e2004-12-30 00:59:30 +0000102static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
103{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000104 FLAC__byte *x = safe_realloc_add_2op_(*entry, length, /*+*/1);
Josh Coalsondef597e2004-12-30 00:59:30 +0000105 if(0 != x) {
106 x[length] = '\0';
107 *entry = x;
108 return true;
109 }
110 else
111 return false;
112}
113
Josh Coalsone343ab22006-09-23 19:21:19 +0000114/* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
115 * unchanged if malloc fails, free()ing the original '*to' if it
116 * succeeds and the original '*to' was not NULL
117 */
118static FLAC__bool copy_cstring_(char **to, const char *from)
119{
Josh Coalsone343ab22006-09-23 19:21:19 +0000120 char *copy = strdup(from);
Josh Coalsona65fd932006-10-03 01:02:44 +0000121 FLAC__ASSERT(to);
Josh Coalsone343ab22006-09-23 19:21:19 +0000122 if(copy) {
123 if(*to)
124 free(*to);
125 *to = copy;
126 return true;
127 }
128 else
129 return false;
130}
131
Josh Coalsoncc682512002-06-08 04:53:42 +0000132static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
Josh Coalson90ced912002-05-30 05:23:38 +0000133{
134 to->length = from->length;
135 if(0 == from->entry) {
136 FLAC__ASSERT(from->length == 0);
137 to->entry = 0;
138 }
139 else {
140 FLAC__byte *x;
141 FLAC__ASSERT(from->length > 0);
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000142 if(0 == (x = safe_malloc_add_2op_(from->length, /*+*/1)))
Josh Coalson90ced912002-05-30 05:23:38 +0000143 return false;
144 memcpy(x, from->entry, from->length);
Josh Coalsondef597e2004-12-30 00:59:30 +0000145 x[from->length] = '\0';
Josh Coalson90ced912002-05-30 05:23:38 +0000146 to->entry = x;
147 }
148 return true;
149}
150
Josh Coalson8e9c4512002-11-14 05:00:24 +0000151static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
152{
153 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
154 if(0 == from->indices) {
155 FLAC__ASSERT(from->num_indices == 0);
156 }
157 else {
158 FLAC__StreamMetadata_CueSheet_Index *x;
159 FLAC__ASSERT(from->num_indices > 0);
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +1000160 if(0 == (x = safe_malloc_mul_2op_p(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))))
Josh Coalson8e9c4512002-11-14 05:00:24 +0000161 return false;
162 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
163 to->indices = x;
164 }
165 return true;
166}
167
Josh Coalsoncc682512002-06-08 04:53:42 +0000168static void seektable_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000169{
170 FLAC__ASSERT(0 != object);
171 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
172
173 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
174}
175
Josh Coalsoncc682512002-06-08 04:53:42 +0000176static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000177{
Josh Coalsoncc682512002-06-08 04:53:42 +0000178 FLAC__StreamMetadata_SeekPoint *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000179
180 FLAC__ASSERT(num_points > 0);
181
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +1000182 object_array = safe_malloc_mul_2op_p(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
Josh Coalson90ced912002-05-30 05:23:38 +0000183
184 if(0 != object_array) {
185 unsigned i;
186 for(i = 0; i < num_points; i++) {
187 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
188 object_array[i].stream_offset = 0;
189 object_array[i].frame_samples = 0;
190 }
191 }
192
193 return object_array;
194}
195
Josh Coalsoncc682512002-06-08 04:53:42 +0000196static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000197{
198 unsigned i;
199
200 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
201
202 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
203 object->length += object->data.vorbis_comment.vendor_string.length;
204 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
205 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
206 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
207 object->length += object->data.vorbis_comment.comments[i].length;
208 }
209}
210
Josh Coalsoncc682512002-06-08 04:53:42 +0000211static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000212{
Josh Coalson90ced912002-05-30 05:23:38 +0000213 FLAC__ASSERT(num_comments > 0);
214
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000215 return safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000216}
217
Josh Coalsoncc682512002-06-08 04:53:42 +0000218static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000219{
220 unsigned i;
221
222 FLAC__ASSERT(0 != object_array && num_comments > 0);
223
224 for(i = 0; i < num_comments; i++)
225 if(0 != object_array[i].entry)
226 free(object_array[i].entry);
227
228 if(0 != object_array)
229 free(object_array);
230}
231
Josh Coalsoncc682512002-06-08 04:53:42 +0000232static 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 +0000233{
Josh Coalsoncc682512002-06-08 04:53:42 +0000234 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000235
236 FLAC__ASSERT(0 != object_array);
237 FLAC__ASSERT(num_comments > 0);
238
239 return_array = vorbiscomment_entry_array_new_(num_comments);
240
241 if(0 != return_array) {
242 unsigned i;
243
Josh Coalson90ced912002-05-30 05:23:38 +0000244 for(i = 0; i < num_comments; i++) {
245 if(!copy_vcentry_(return_array+i, object_array+i)) {
246 vorbiscomment_entry_array_delete_(return_array, num_comments);
247 return 0;
248 }
249 }
250 }
251
252 return return_array;
253}
254
Josh Coalsoncc682512002-06-08 04:53:42 +0000255static 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 +0000256{
257 FLAC__byte *save;
258
259 FLAC__ASSERT(0 != object);
260 FLAC__ASSERT(0 != dest);
261 FLAC__ASSERT(0 != src);
262 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson748459c2004-09-08 00:49:30 +0000263 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
Josh Coalson90ced912002-05-30 05:23:38 +0000264
Josh Coalson6b8e5302002-05-31 06:23:09 +0000265 save = dest->entry;
Josh Coalson90ced912002-05-30 05:23:38 +0000266
Erik de Castro Lopo49d9d742014-03-23 21:40:54 +1100267 if(0 != src->entry) {
Josh Coalsondef597e2004-12-30 00:59:30 +0000268 if(copy) {
269 /* do the copy first so that if we fail we leave the dest object untouched */
270 if(!copy_vcentry_(dest, src))
271 return false;
272 }
273 else {
274 /* we have to make sure that the string we're taking over is null-terminated */
275
276 /*
277 * Stripping the const from src->entry is OK since we're taking
278 * ownership of the pointer. This is a hack around a deficiency
279 * in the API where the same function is used for 'copy' and
280 * 'own', but the source entry is a const pointer. If we were
281 * precise, the 'own' flavor would be a separate function with a
282 * non-const source pointer. But it's not, so we hack away.
283 */
284 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
285 return false;
286 *dest = *src;
287 }
Josh Coalson90ced912002-05-30 05:23:38 +0000288 }
289 else {
Josh Coalsondef597e2004-12-30 00:59:30 +0000290 /* the src is null */
Josh Coalson90ced912002-05-30 05:23:38 +0000291 *dest = *src;
292 }
293
294 if(0 != save)
295 free(save);
296
297 vorbiscomment_calculate_length_(object);
298 return true;
299}
300
Josh Coalsondef597e2004-12-30 00:59:30 +0000301static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
302{
303 unsigned i;
304
305 FLAC__ASSERT(0 != object);
306 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
307 FLAC__ASSERT(0 != field_name);
308
309 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
310 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
311 return (int)i;
312 }
313
314 return -1;
315}
316
Josh Coalson8e9c4512002-11-14 05:00:24 +0000317static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
318{
319 unsigned i;
320
321 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
322
323 object->length = (
324 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
325 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000326 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
Josh Coalsondf7240a2002-11-16 06:32:30 +0000327 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
Josh Coalson8e9c4512002-11-14 05:00:24 +0000328 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
329 ) / 8;
330
331 object->length += object->data.cue_sheet.num_tracks * (
332 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
333 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
334 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
335 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
336 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
337 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
338 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
339 ) / 8;
340
341 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
342 object->length += object->data.cue_sheet.tracks[i].num_indices * (
343 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
344 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
345 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
346 ) / 8;
347 }
348}
349
350static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
351{
352 FLAC__ASSERT(num_indices > 0);
353
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000354 return safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000355}
356
357static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
358{
359 FLAC__ASSERT(num_tracks > 0);
360
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000361 return safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000362}
363
364static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
365{
366 unsigned i;
367
368 FLAC__ASSERT(0 != object_array && num_tracks > 0);
369
370 for(i = 0; i < num_tracks; i++) {
371 if(0 != object_array[i].indices) {
372 FLAC__ASSERT(object_array[i].num_indices > 0);
373 free(object_array[i].indices);
374 }
375 }
376
377 if(0 != object_array)
378 free(object_array);
379}
380
381static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
382{
383 FLAC__StreamMetadata_CueSheet_Track *return_array;
384
385 FLAC__ASSERT(0 != object_array);
386 FLAC__ASSERT(num_tracks > 0);
387
388 return_array = cuesheet_track_array_new_(num_tracks);
389
390 if(0 != return_array) {
391 unsigned i;
392
393 for(i = 0; i < num_tracks; i++) {
394 if(!copy_track_(return_array+i, object_array+i)) {
395 cuesheet_track_array_delete_(return_array, num_tracks);
396 return 0;
397 }
398 }
399 }
400
401 return return_array;
402}
403
404static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
405{
406 FLAC__StreamMetadata_CueSheet_Index *save;
407
408 FLAC__ASSERT(0 != object);
409 FLAC__ASSERT(0 != dest);
410 FLAC__ASSERT(0 != src);
411 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
412 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000413
414 save = dest->indices;
415
416 /* do the copy first so that if we fail we leave the object untouched */
417 if(copy) {
418 if(!copy_track_(dest, src))
419 return false;
420 }
421 else {
422 *dest = *src;
423 }
424
425 if(0 != save)
426 free(save);
427
428 cuesheet_calculate_length_(object);
429 return true;
430}
431
Josh Coalson90ced912002-05-30 05:23:38 +0000432
433/****************************************************************************
434 *
435 * Metadata object routines
436 *
437 ***************************************************************************/
438
Josh Coalson6afed9f2002-10-16 22:29:47 +0000439FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
Josh Coalson90ced912002-05-30 05:23:38 +0000440{
Josh Coalson1cb23412004-07-22 01:32:00 +0000441 FLAC__StreamMetadata *object;
442
Erik de Castro Lopo3f5208c2014-04-09 17:56:13 +1000443 if(type > FLAC__MAX_METADATA_TYPE)
Josh Coalson1cb23412004-07-22 01:32:00 +0000444 return 0;
445
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000446 object = calloc(1, sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000447 if(0 != object) {
Josh Coalson90ced912002-05-30 05:23:38 +0000448 object->is_last = false;
449 object->type = type;
450 switch(type) {
451 case FLAC__METADATA_TYPE_STREAMINFO:
452 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
453 break;
454 case FLAC__METADATA_TYPE_PADDING:
Josh Coalsond0609472003-01-10 05:37:13 +0000455 /* calloc() took care of this for us:
456 object->length = 0;
457 */
Josh Coalson90ced912002-05-30 05:23:38 +0000458 break;
459 case FLAC__METADATA_TYPE_APPLICATION:
460 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
Josh Coalsond0609472003-01-10 05:37:13 +0000461 /* calloc() took care of this for us:
462 object->data.application.data = 0;
463 */
Josh Coalson90ced912002-05-30 05:23:38 +0000464 break;
465 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsond0609472003-01-10 05:37:13 +0000466 /* calloc() took care of this for us:
467 object->length = 0;
468 object->data.seek_table.num_points = 0;
469 object->data.seek_table.points = 0;
470 */
Josh Coalson90ced912002-05-30 05:23:38 +0000471 break;
472 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalsone343ab22006-09-23 19:21:19 +0000473 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
474 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
475 free(object);
476 return 0;
Josh Coalson45bb9882002-10-26 04:34:16 +0000477 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000478 vorbiscomment_calculate_length_(object);
Josh Coalson90ced912002-05-30 05:23:38 +0000479 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000480 case FLAC__METADATA_TYPE_CUESHEET:
481 cuesheet_calculate_length_(object);
482 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000483 case FLAC__METADATA_TYPE_PICTURE:
484 object->length = (
485 FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
486 FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
487 FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
488 FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
489 FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
490 FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
Josh Coalson74ed2942006-09-23 23:15:05 +0000491 FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
Josh Coalsone343ab22006-09-23 19:21:19 +0000492 FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
493 0 /* no data */
494 ) / 8;
495 object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
496 object->data.picture.mime_type = 0;
497 object->data.picture.description = 0;
498 /* calloc() took care of this for us:
499 object->data.picture.width = 0;
500 object->data.picture.height = 0;
501 object->data.picture.depth = 0;
Josh Coalson74ed2942006-09-23 23:15:05 +0000502 object->data.picture.colors = 0;
Josh Coalsone343ab22006-09-23 19:21:19 +0000503 object->data.picture.data_length = 0;
504 object->data.picture.data = 0;
505 */
506 /* now initialize mime_type and description with empty strings to make things easier on the client */
507 if(!copy_cstring_(&object->data.picture.mime_type, "")) {
508 free(object);
509 return 0;
510 }
511 if(!copy_cstring_((char**)(&object->data.picture.description), "")) {
512 if(object->data.picture.mime_type)
513 free(object->data.picture.mime_type);
514 free(object);
515 return 0;
516 }
517 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000518 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000519 /* calloc() took care of this for us:
520 object->length = 0;
521 object->data.unknown.data = 0;
522 */
523 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000524 }
525 }
526
527 return object;
528}
529
Josh Coalson6afed9f2002-10-16 22:29:47 +0000530FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000531{
Josh Coalsoncc682512002-06-08 04:53:42 +0000532 FLAC__StreamMetadata *to;
Josh Coalson90ced912002-05-30 05:23:38 +0000533
534 FLAC__ASSERT(0 != object);
535
536 if(0 != (to = FLAC__metadata_object_new(object->type))) {
537 to->is_last = object->is_last;
538 to->type = object->type;
539 to->length = object->length;
540 switch(to->type) {
541 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalsoncc682512002-06-08 04:53:42 +0000542 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
Josh Coalson90ced912002-05-30 05:23:38 +0000543 break;
544 case FLAC__METADATA_TYPE_PADDING:
545 break;
546 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson0f008d22007-09-11 04:49:56 +0000547 if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
548 FLAC__metadata_object_delete(to);
549 return 0;
550 }
Josh Coalson90ced912002-05-30 05:23:38 +0000551 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
552 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
553 FLAC__metadata_object_delete(to);
554 return 0;
555 }
556 break;
557 case FLAC__METADATA_TYPE_SEEKTABLE:
558 to->data.seek_table.num_points = object->data.seek_table.num_points;
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100559 if(to->data.seek_table.num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +0000560 FLAC__metadata_object_delete(to);
561 return 0;
562 }
Josh Coalsoncc682512002-06-08 04:53:42 +0000563 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 +0000564 FLAC__metadata_object_delete(to);
565 return 0;
566 }
567 break;
568 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000569 if(0 != to->data.vorbis_comment.vendor_string.entry) {
Josh Coalson45bb9882002-10-26 04:34:16 +0000570 free(to->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000571 to->data.vorbis_comment.vendor_string.entry = 0;
572 }
Josh Coalson90ced912002-05-30 05:23:38 +0000573 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
574 FLAC__metadata_object_delete(to);
575 return 0;
576 }
577 if(object->data.vorbis_comment.num_comments == 0) {
578 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
579 to->data.vorbis_comment.comments = 0;
580 }
581 else {
582 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
583 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
584 if(0 == to->data.vorbis_comment.comments) {
585 FLAC__metadata_object_delete(to);
586 return 0;
587 }
588 }
589 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
590 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000591 case FLAC__METADATA_TYPE_CUESHEET:
592 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
593 if(object->data.cue_sheet.num_tracks == 0) {
594 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
595 }
596 else {
597 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
598 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
599 if(0 == to->data.cue_sheet.tracks) {
600 FLAC__metadata_object_delete(to);
601 return 0;
602 }
603 }
604 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000605 case FLAC__METADATA_TYPE_PICTURE:
606 to->data.picture.type = object->data.picture.type;
607 if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
608 FLAC__metadata_object_delete(to);
609 return 0;
610 }
611 if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
612 FLAC__metadata_object_delete(to);
613 return 0;
614 }
615 to->data.picture.width = object->data.picture.width;
616 to->data.picture.height = object->data.picture.height;
617 to->data.picture.depth = object->data.picture.depth;
Josh Coalson74ed2942006-09-23 23:15:05 +0000618 to->data.picture.colors = object->data.picture.colors;
Josh Coalsone343ab22006-09-23 19:21:19 +0000619 to->data.picture.data_length = object->data.picture.data_length;
620 if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
621 FLAC__metadata_object_delete(to);
622 return 0;
623 }
624 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000625 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000626 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
627 FLAC__metadata_object_delete(to);
628 return 0;
629 }
630 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000631 }
632 }
633
634 return to;
635}
636
Josh Coalsoncc682512002-06-08 04:53:42 +0000637void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000638{
639 FLAC__ASSERT(0 != object);
640
641 switch(object->type) {
642 case FLAC__METADATA_TYPE_STREAMINFO:
643 case FLAC__METADATA_TYPE_PADDING:
644 break;
645 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson4fa90592002-12-04 07:01:37 +0000646 if(0 != object->data.application.data) {
Josh Coalson90ced912002-05-30 05:23:38 +0000647 free(object->data.application.data);
Josh Coalson4fa90592002-12-04 07:01:37 +0000648 object->data.application.data = 0;
649 }
Josh Coalson90ced912002-05-30 05:23:38 +0000650 break;
651 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalson4fa90592002-12-04 07:01:37 +0000652 if(0 != object->data.seek_table.points) {
Josh Coalson90ced912002-05-30 05:23:38 +0000653 free(object->data.seek_table.points);
Josh Coalson4fa90592002-12-04 07:01:37 +0000654 object->data.seek_table.points = 0;
655 }
Josh Coalson90ced912002-05-30 05:23:38 +0000656 break;
657 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000658 if(0 != object->data.vorbis_comment.vendor_string.entry) {
Josh Coalson90ced912002-05-30 05:23:38 +0000659 free(object->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000660 object->data.vorbis_comment.vendor_string.entry = 0;
661 }
Josh Coalson90ced912002-05-30 05:23:38 +0000662 if(0 != object->data.vorbis_comment.comments) {
663 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
664 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
665 }
666 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000667 case FLAC__METADATA_TYPE_CUESHEET:
668 if(0 != object->data.cue_sheet.tracks) {
669 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
670 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
671 }
672 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000673 case FLAC__METADATA_TYPE_PICTURE:
674 if(0 != object->data.picture.mime_type) {
675 free(object->data.picture.mime_type);
676 object->data.picture.mime_type = 0;
677 }
678 if(0 != object->data.picture.description) {
679 free(object->data.picture.description);
680 object->data.picture.description = 0;
681 }
682 if(0 != object->data.picture.data) {
683 free(object->data.picture.data);
684 object->data.picture.data = 0;
685 }
686 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000687 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000688 if(0 != object->data.unknown.data) {
689 free(object->data.unknown.data);
690 object->data.unknown.data = 0;
691 }
692 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000693 }
694}
695
Josh Coalson6afed9f2002-10-16 22:29:47 +0000696FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000697{
698 FLAC__metadata_object_delete_data(object);
699 free(object);
700}
701
Josh Coalsoncc682512002-06-08 04:53:42 +0000702static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000703{
704 if(block1->min_blocksize != block2->min_blocksize)
705 return false;
706 if(block1->max_blocksize != block2->max_blocksize)
707 return false;
708 if(block1->min_framesize != block2->min_framesize)
709 return false;
710 if(block1->max_framesize != block2->max_framesize)
711 return false;
712 if(block1->sample_rate != block2->sample_rate)
713 return false;
714 if(block1->channels != block2->channels)
715 return false;
716 if(block1->bits_per_sample != block2->bits_per_sample)
717 return false;
718 if(block1->total_samples != block2->total_samples)
719 return false;
720 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
721 return false;
722 return true;
723}
724
Josh Coalsoncc682512002-06-08 04:53:42 +0000725static 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 +0000726{
727 FLAC__ASSERT(0 != block1);
728 FLAC__ASSERT(0 != block2);
729 FLAC__ASSERT(block_length >= sizeof(block1->id));
730
731 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
732 return false;
733 if(0 != block1->data && 0 != block2->data)
734 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
735 else
736 return block1->data == block2->data;
737}
738
Josh Coalsoncc682512002-06-08 04:53:42 +0000739static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000740{
741 unsigned i;
742
743 FLAC__ASSERT(0 != block1);
744 FLAC__ASSERT(0 != block2);
745
746 if(block1->num_points != block2->num_points)
747 return false;
748
749 if(0 != block1->points && 0 != block2->points) {
750 for(i = 0; i < block1->num_points; i++) {
751 if(block1->points[i].sample_number != block2->points[i].sample_number)
752 return false;
753 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
754 return false;
755 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
756 return false;
757 }
758 return true;
759 }
760 else
761 return block1->points == block2->points;
762}
763
Josh Coalsoncc682512002-06-08 04:53:42 +0000764static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000765{
766 unsigned i;
767
768 if(block1->vendor_string.length != block2->vendor_string.length)
769 return false;
770
771 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
772 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
773 return false;
774 }
775 else if(block1->vendor_string.entry != block2->vendor_string.entry)
776 return false;
777
778 if(block1->num_comments != block2->num_comments)
779 return false;
780
781 for(i = 0; i < block1->num_comments; i++) {
782 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
783 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
784 return false;
785 }
786 else if(block1->comments[i].entry != block2->comments[i].entry)
787 return false;
788 }
789 return true;
790}
791
Josh Coalson8e9c4512002-11-14 05:00:24 +0000792static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
793{
794 unsigned i, j;
795
796 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
797 return false;
798
799 if(block1->lead_in != block2->lead_in)
800 return false;
801
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000802 if(block1->is_cd != block2->is_cd)
803 return false;
804
Josh Coalson8e9c4512002-11-14 05:00:24 +0000805 if(block1->num_tracks != block2->num_tracks)
806 return false;
807
808 if(0 != block1->tracks && 0 != block2->tracks) {
809 FLAC__ASSERT(block1->num_tracks > 0);
810 for(i = 0; i < block1->num_tracks; i++) {
811 if(block1->tracks[i].offset != block2->tracks[i].offset)
812 return false;
813 if(block1->tracks[i].number != block2->tracks[i].number)
814 return false;
815 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
816 return false;
817 if(block1->tracks[i].type != block2->tracks[i].type)
818 return false;
819 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
820 return false;
821 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
822 return false;
823 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
824 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
825 for(j = 0; j < block1->tracks[i].num_indices; j++) {
826 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
827 return false;
828 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
829 return false;
830 }
831 }
832 else if(block1->tracks[i].indices != block2->tracks[i].indices)
833 return false;
834 }
835 }
836 else if(block1->tracks != block2->tracks)
837 return false;
838 return true;
839}
840
Josh Coalsone343ab22006-09-23 19:21:19 +0000841static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
842{
843 if(block1->type != block2->type)
844 return false;
845 if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type)))
846 return false;
847 if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description)))
848 return false;
849 if(block1->width != block2->width)
850 return false;
851 if(block1->height != block2->height)
852 return false;
853 if(block1->depth != block2->depth)
854 return false;
Josh Coalson74ed2942006-09-23 23:15:05 +0000855 if(block1->colors != block2->colors)
856 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +0000857 if(block1->data_length != block2->data_length)
858 return false;
859 if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length)))
860 return false;
861 return true;
862}
863
Josh Coalsond0609472003-01-10 05:37:13 +0000864static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
865{
866 FLAC__ASSERT(0 != block1);
867 FLAC__ASSERT(0 != block2);
868
869 if(0 != block1->data && 0 != block2->data)
870 return 0 == memcmp(block1->data, block2->data, block_length);
871 else
872 return block1->data == block2->data;
873}
874
Josh Coalson6afed9f2002-10-16 22:29:47 +0000875FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000876{
Josh Coalsond8ab3462002-07-11 05:54:05 +0000877 FLAC__ASSERT(0 != block1);
878 FLAC__ASSERT(0 != block2);
879
Josh Coalson57ba6f42002-06-07 05:27:37 +0000880 if(block1->type != block2->type) {
881 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000882 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000883 if(block1->is_last != block2->is_last) {
884 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000885 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000886 if(block1->length != block2->length) {
887 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000888 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000889 switch(block1->type) {
890 case FLAC__METADATA_TYPE_STREAMINFO:
891 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
892 case FLAC__METADATA_TYPE_PADDING:
893 return true; /* we don't compare the padding guts */
894 case FLAC__METADATA_TYPE_APPLICATION:
895 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
896 case FLAC__METADATA_TYPE_SEEKTABLE:
897 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
898 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
899 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000900 case FLAC__METADATA_TYPE_CUESHEET:
901 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +0000902 case FLAC__METADATA_TYPE_PICTURE:
903 return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000904 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000905 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000906 }
907}
908
Josh Coalson6afed9f2002-10-16 22:29:47 +0000909FLAC_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 +0000910{
911 FLAC__byte *save;
912
Josh Coalsond8ab3462002-07-11 05:54:05 +0000913 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000914 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
915 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
916
917 save = object->data.application.data;
918
919 /* do the copy first so that if we fail we leave the object untouched */
920 if(copy) {
921 if(!copy_bytes_(&object->data.application.data, data, length))
922 return false;
923 }
924 else {
925 object->data.application.data = data;
926 }
927
928 if(0 != save)
929 free(save);
930
931 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
932 return true;
933}
934
Josh Coalson6afed9f2002-10-16 22:29:47 +0000935FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000936{
937 FLAC__ASSERT(0 != object);
938 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
939
940 if(0 == object->data.seek_table.points) {
941 FLAC__ASSERT(object->data.seek_table.num_points == 0);
942 if(0 == new_num_points)
943 return true;
944 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
945 return false;
946 }
947 else {
Josh Coalson0f008d22007-09-11 04:49:56 +0000948 const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
949 const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
950
951 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100952 if(new_num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
Josh Coalson0f008d22007-09-11 04:49:56 +0000953 return false;
Josh Coalson90ced912002-05-30 05:23:38 +0000954
955 FLAC__ASSERT(object->data.seek_table.num_points > 0);
956
957 if(new_size == 0) {
958 free(object->data.seek_table.points);
959 object->data.seek_table.points = 0;
960 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000961 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +0000962 return false;
963
964 /* if growing, set new elements to placeholders */
965 if(new_size > old_size) {
966 unsigned i;
967 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
968 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
969 object->data.seek_table.points[i].stream_offset = 0;
970 object->data.seek_table.points[i].frame_samples = 0;
971 }
972 }
973 }
974
975 object->data.seek_table.num_points = new_num_points;
976
977 seektable_calculate_length_(object);
978 return true;
979}
980
Josh Coalson6afed9f2002-10-16 22:29:47 +0000981FLAC_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 +0000982{
983 FLAC__ASSERT(0 != object);
984 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000985 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000986
987 object->data.seek_table.points[point_num] = point;
988}
989
Josh Coalson6afed9f2002-10-16 22:29:47 +0000990FLAC_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 +0000991{
992 int i;
993
994 FLAC__ASSERT(0 != object);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000995 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000996 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000997
998 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
999 return false;
1000
1001 /* move all points >= point_num forward one space */
1002 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
1003 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
1004
1005 FLAC__metadata_object_seektable_set_point(object, point_num, point);
1006 seektable_calculate_length_(object);
1007 return true;
1008}
1009
Josh Coalson6afed9f2002-10-16 22:29:47 +00001010FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001011{
1012 unsigned i;
1013
1014 FLAC__ASSERT(0 != object);
1015 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001016 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +00001017
1018 /* move all points > point_num backward one space */
1019 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
1020 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
1021
1022 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
1023}
1024
Josh Coalson6afed9f2002-10-16 22:29:47 +00001025FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
Josh Coalson28e08d82002-06-05 05:56:41 +00001026{
Josh Coalson28e08d82002-06-05 05:56:41 +00001027 FLAC__ASSERT(0 != object);
1028 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1029
Josh Coalson0833f342002-07-15 05:31:55 +00001030 return FLAC__format_seektable_is_legal(&object->data.seek_table);
Josh Coalson28e08d82002-06-05 05:56:41 +00001031}
1032
Josh Coalson6afed9f2002-10-16 22:29:47 +00001033FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
Josh Coalson5a5de732002-08-01 06:37:11 +00001034{
1035 FLAC__ASSERT(0 != object);
1036 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1037
1038 if(num > 0)
1039 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
1040 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
1041 else
1042 return true;
1043}
1044
Josh Coalson6afed9f2002-10-16 22:29:47 +00001045FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
Josh Coalson5a5de732002-08-01 06:37:11 +00001046{
1047 FLAC__StreamMetadata_SeekTable *seek_table;
1048
1049 FLAC__ASSERT(0 != object);
1050 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1051
1052 seek_table = &object->data.seek_table;
1053
1054 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
1055 return false;
1056
1057 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
1058 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
1059 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
1060
1061 return true;
1062}
1063
Josh Coalson6afed9f2002-10-16 22:29:47 +00001064FLAC_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 +00001065{
1066 FLAC__ASSERT(0 != object);
1067 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1068 FLAC__ASSERT(0 != sample_numbers || num == 0);
1069
1070 if(num > 0) {
1071 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1072 unsigned i, j;
1073
1074 i = seek_table->num_points;
1075
1076 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1077 return false;
1078
1079 for(j = 0; j < num; i++, j++) {
1080 seek_table->points[i].sample_number = sample_numbers[j];
1081 seek_table->points[i].stream_offset = 0;
1082 seek_table->points[i].frame_samples = 0;
1083 }
1084 }
1085
1086 return true;
1087}
1088
Josh Coalson6afed9f2002-10-16 22:29:47 +00001089FLAC_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 +00001090{
1091 FLAC__ASSERT(0 != object);
1092 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1093 FLAC__ASSERT(total_samples > 0);
1094
Josh Coalson6b21f662006-09-13 01:42:27 +00001095 if(num > 0 && total_samples > 0) {
Josh Coalson5a5de732002-08-01 06:37:11 +00001096 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1097 unsigned i, j;
1098
1099 i = seek_table->num_points;
1100
1101 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1102 return false;
1103
1104 for(j = 0; j < num; i++, j++) {
1105 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
1106 seek_table->points[i].stream_offset = 0;
1107 seek_table->points[i].frame_samples = 0;
1108 }
1109 }
1110
1111 return true;
1112}
1113
Josh Coalson6b21f662006-09-13 01:42:27 +00001114FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
1115{
1116 FLAC__ASSERT(0 != object);
1117 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1118 FLAC__ASSERT(samples > 0);
1119 FLAC__ASSERT(total_samples > 0);
1120
1121 if(samples > 0 && total_samples > 0) {
1122 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1123 unsigned i, j;
1124 FLAC__uint64 num, sample;
1125
1126 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
1127 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
1128 if(total_samples % samples == 0)
1129 num--;
1130
Erik de Castro Lopoc431a6c2015-02-21 07:05:21 +11001131 /* Put a strict upper bound on the number of allowed seek points. */
Erik de Castro Lopo033af7b2015-02-18 17:55:52 +11001132 if (num > 32768) {
1133 /* Set the bound and recalculate samples accordingly. */
1134 num = 32786;
1135 samples = total_samples / num;
1136 }
1137
Josh Coalson6b21f662006-09-13 01:42:27 +00001138 i = seek_table->num_points;
1139
Josh Coalsona65fd932006-10-03 01:02:44 +00001140 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
Josh Coalson6b21f662006-09-13 01:42:27 +00001141 return false;
1142
1143 sample = 0;
1144 for(j = 0; j < num; i++, j++, sample += samples) {
1145 seek_table->points[i].sample_number = sample;
1146 seek_table->points[i].stream_offset = 0;
1147 seek_table->points[i].frame_samples = 0;
1148 }
1149 }
1150
1151 return true;
1152}
1153
Josh Coalson6afed9f2002-10-16 22:29:47 +00001154FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +00001155{
1156 unsigned unique;
1157
1158 FLAC__ASSERT(0 != object);
1159 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1160
1161 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1162
1163 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1164}
1165
Josh Coalson6afed9f2002-10-16 22:29:47 +00001166FLAC_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 +00001167{
Josh Coalson2de11242004-12-30 03:41:19 +00001168 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1169 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001170 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001171}
1172
Josh Coalson6afed9f2002-10-16 22:29:47 +00001173FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +00001174{
1175 FLAC__ASSERT(0 != object);
1176 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1177
1178 if(0 == object->data.vorbis_comment.comments) {
1179 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1180 if(0 == new_num_comments)
1181 return true;
1182 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1183 return false;
1184 }
1185 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001186 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1187 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1188
1189 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001190 if(new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
Josh Coalson0f008d22007-09-11 04:49:56 +00001191 return false;
Josh Coalson90ced912002-05-30 05:23:38 +00001192
1193 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1194
1195 /* if shrinking, free the truncated entries */
1196 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1197 unsigned i;
1198 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1199 if(0 != object->data.vorbis_comment.comments[i].entry)
1200 free(object->data.vorbis_comment.comments[i].entry);
1201 }
1202
1203 if(new_size == 0) {
1204 free(object->data.vorbis_comment.comments);
1205 object->data.vorbis_comment.comments = 0;
1206 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001207 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +00001208 return false;
1209
1210 /* if growing, zero all the length/pointers of new elements */
1211 if(new_size > old_size)
1212 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1213 }
1214
1215 object->data.vorbis_comment.num_comments = new_num_comments;
1216
1217 vorbiscomment_calculate_length_(object);
1218 return true;
1219}
1220
Josh Coalson6afed9f2002-10-16 22:29:47 +00001221FLAC_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 +00001222{
Josh Coalsonfb993862003-01-15 03:18:07 +00001223 FLAC__ASSERT(0 != object);
1224 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1225
Josh Coalson2de11242004-12-30 03:41:19 +00001226 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1227 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001228 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001229}
1230
Josh Coalson6afed9f2002-10-16 22:29:47 +00001231FLAC_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 +00001232{
Josh Coalsoncc682512002-06-08 04:53:42 +00001233 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001234
1235 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001236 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001237 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001238
Josh Coalson2de11242004-12-30 03:41:19 +00001239 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1240 return false;
1241
Josh Coalson6b8e5302002-05-31 06:23:09 +00001242 vc = &object->data.vorbis_comment;
1243
1244 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001245 return false;
1246
1247 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001248 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 +00001249 vc->comments[comment_num].length = 0;
1250 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001251
1252 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1253}
1254
Josh Coalsondef597e2004-12-30 00:59:30 +00001255FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1256{
1257 FLAC__ASSERT(0 != object);
1258 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1259 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1260}
1261
1262FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1263{
1264 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
Josh Coalson2de11242004-12-30 03:41:19 +00001265
1266 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1267 return false;
1268
Josh Coalsondef597e2004-12-30 00:59:30 +00001269 {
1270 int i;
Josh Coalson9bedd782007-02-22 01:37:33 +00001271 size_t field_name_length;
Josh Coalsondef597e2004-12-30 00:59:30 +00001272 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1273
1274 FLAC__ASSERT(0 != eq);
1275
1276 if(0 == eq)
1277 return false; /* double protection */
1278
1279 field_name_length = eq-entry.entry;
1280
Josh Coalsone95399c2008-09-15 05:37:27 +00001281 i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
1282 if(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001283 unsigned indx = (unsigned)i;
1284 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, indx, entry, copy))
Josh Coalsondef597e2004-12-30 00:59:30 +00001285 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001286 entry = object->data.vorbis_comment.comments[indx];
1287 indx++; /* skip over replaced comment */
1288 if(all && indx < object->data.vorbis_comment.num_comments) {
1289 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsone95399c2008-09-15 05:37:27 +00001290 while(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001291 indx = (unsigned)i;
1292 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, indx))
Josh Coalsondef597e2004-12-30 00:59:30 +00001293 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001294 if(indx < object->data.vorbis_comment.num_comments)
1295 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001296 else
1297 i = -1;
1298 }
1299 }
1300 return true;
1301 }
1302 else
1303 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1304 }
1305}
1306
Josh Coalson6afed9f2002-10-16 22:29:47 +00001307FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001308{
Josh Coalsoncc682512002-06-08 04:53:42 +00001309 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001310
1311 FLAC__ASSERT(0 != object);
1312 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001313 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001314
Josh Coalson6b8e5302002-05-31 06:23:09 +00001315 vc = &object->data.vorbis_comment;
1316
Josh Coalson90ced912002-05-30 05:23:38 +00001317 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +00001318 if(0 != vc->comments[comment_num].entry)
1319 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001320
1321 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001322 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 +00001323 vc->comments[vc->num_comments-1].length = 0;
1324 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001325
Josh Coalson6b8e5302002-05-31 06:23:09 +00001326 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +00001327}
Josh Coalson45bb9882002-10-26 04:34:16 +00001328
Josh Coalsondef597e2004-12-30 00:59:30 +00001329FLAC_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 +00001330{
Josh Coalsondef597e2004-12-30 00:59:30 +00001331 FLAC__ASSERT(0 != entry);
1332 FLAC__ASSERT(0 != field_name);
1333 FLAC__ASSERT(0 != field_value);
1334
Josh Coalson2de11242004-12-30 03:41:19 +00001335 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1336 return false;
Josh Coalsonb4de1692005-08-24 07:39:25 +00001337 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
Josh Coalson2de11242004-12-30 03:41:19 +00001338 return false;
1339
Josh Coalsondef597e2004-12-30 00:59:30 +00001340 {
1341 const size_t nn = strlen(field_name);
1342 const size_t nv = strlen(field_value);
1343 entry->length = nn + 1 /*=*/ + nv;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001344 if(0 == (entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001345 return false;
1346 memcpy(entry->entry, field_name, nn);
1347 entry->entry[nn] = '=';
1348 memcpy(entry->entry+nn+1, field_value, nv);
1349 entry->entry[entry->length] = '\0';
1350 }
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001351
Josh Coalsondef597e2004-12-30 00:59:30 +00001352 return true;
1353}
1354
1355FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1356{
1357 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1358 FLAC__ASSERT(0 != field_name);
1359 FLAC__ASSERT(0 != field_value);
Josh Coalson2de11242004-12-30 03:41:19 +00001360
1361 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1362 return false;
1363
Josh Coalsondef597e2004-12-30 00:59:30 +00001364 {
1365 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1366 const size_t nn = eq-entry.entry;
1367 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1368 FLAC__ASSERT(0 != eq);
1369 if(0 == eq)
1370 return false; /* double protection */
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001371 if(0 == (*field_name = safe_malloc_add_2op_(nn, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001372 return false;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001373 if(0 == (*field_value = safe_malloc_add_2op_(nv, /*+*/1))) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001374 free(*field_name);
1375 return false;
1376 }
1377 memcpy(*field_name, entry.entry, nn);
1378 memcpy(*field_value, entry.entry+nn+1, nv);
1379 (*field_name)[nn] = '\0';
1380 (*field_value)[nv] = '\0';
1381 }
1382
1383 return true;
1384}
1385
Josh Coalson44623112005-01-30 18:15:36 +00001386FLAC_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 +00001387{
1388 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1389 {
1390 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001391 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 +00001392 }
Josh Coalson45bb9882002-10-26 04:34:16 +00001393}
1394
Josh Coalsonb667e702002-11-05 07:24:33 +00001395FLAC_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 +00001396{
Josh Coalsondef597e2004-12-30 00:59:30 +00001397 FLAC__ASSERT(0 != field_name);
Josh Coalson45bb9882002-10-26 04:34:16 +00001398
Josh Coalsondef597e2004-12-30 00:59:30 +00001399 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
Josh Coalson45bb9882002-10-26 04:34:16 +00001400}
1401
1402FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1403{
1404 const unsigned field_name_length = strlen(field_name);
1405 unsigned i;
1406
1407 FLAC__ASSERT(0 != object);
1408 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1409
1410 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001411 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 +00001412 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1413 return -1;
1414 else
1415 return 1;
1416 }
1417 }
1418
1419 return 0;
1420}
1421
1422FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1423{
1424 FLAC__bool ok = true;
1425 unsigned matching = 0;
1426 const unsigned field_name_length = strlen(field_name);
1427 int i;
1428
1429 FLAC__ASSERT(0 != object);
1430 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1431
1432 /* must delete from end to start otherwise it will interfere with our iteration */
1433 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001434 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 +00001435 matching++;
1436 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1437 }
1438 }
1439
1440 return ok? (int)matching : -1;
1441}
Josh Coalson8e9c4512002-11-14 05:00:24 +00001442
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001443FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
Josh Coalsondf7240a2002-11-16 06:32:30 +00001444{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001445 return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
Josh Coalsondf7240a2002-11-16 06:32:30 +00001446}
1447
1448FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1449{
1450 FLAC__StreamMetadata_CueSheet_Track *to;
1451
1452 FLAC__ASSERT(0 != object);
1453
1454 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1455 if(!copy_track_(to, object)) {
1456 FLAC__metadata_object_cuesheet_track_delete(to);
1457 return 0;
1458 }
1459 }
1460
1461 return to;
1462}
1463
1464void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1465{
1466 FLAC__ASSERT(0 != object);
1467
1468 if(0 != object->indices) {
1469 FLAC__ASSERT(object->num_indices > 0);
1470 free(object->indices);
1471 }
1472}
1473
1474FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1475{
1476 FLAC__metadata_object_cuesheet_track_delete_data(object);
1477 free(object);
1478}
1479
Josh Coalson8e9c4512002-11-14 05:00:24 +00001480FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1481{
1482 FLAC__StreamMetadata_CueSheet_Track *track;
1483 FLAC__ASSERT(0 != object);
1484 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1485 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1486
1487 track = &object->data.cue_sheet.tracks[track_num];
1488
1489 if(0 == track->indices) {
1490 FLAC__ASSERT(track->num_indices == 0);
1491 if(0 == new_num_indices)
1492 return true;
1493 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1494 return false;
1495 }
1496 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001497 const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1498 const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1499
1500 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001501 if(new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
Josh Coalson0f008d22007-09-11 04:49:56 +00001502 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001503
1504 FLAC__ASSERT(track->num_indices > 0);
1505
1506 if(new_size == 0) {
1507 free(track->indices);
1508 track->indices = 0;
1509 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001510 else if(0 == (track->indices = realloc(track->indices, new_size)))
Josh Coalson8e9c4512002-11-14 05:00:24 +00001511 return false;
1512
1513 /* if growing, zero all the lengths/pointers of new elements */
1514 if(new_size > old_size)
1515 memset(track->indices + track->num_indices, 0, new_size - old_size);
1516 }
1517
1518 track->num_indices = new_num_indices;
1519
1520 cuesheet_calculate_length_(object);
1521 return true;
1522}
1523
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001524FLAC_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 +00001525{
1526 FLAC__StreamMetadata_CueSheet_Track *track;
1527
1528 FLAC__ASSERT(0 != object);
1529 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1530 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1531 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1532
1533 track = &object->data.cue_sheet.tracks[track_num];
1534
1535 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1536 return false;
1537
1538 /* move all indices >= index_num forward one space */
1539 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1540
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001541 track->indices[index_num] = indx;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001542 cuesheet_calculate_length_(object);
1543 return true;
1544}
1545
Josh Coalson3b026e82002-11-21 06:41:01 +00001546FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1547{
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001548 FLAC__StreamMetadata_CueSheet_Index indx;
1549 memset(&indx, 0, sizeof(indx));
1550 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, indx);
Josh Coalson3b026e82002-11-21 06:41:01 +00001551}
1552
Josh Coalson8e9c4512002-11-14 05:00:24 +00001553FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1554{
1555 FLAC__StreamMetadata_CueSheet_Track *track;
1556
1557 FLAC__ASSERT(0 != object);
1558 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1559 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1560 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1561
1562 track = &object->data.cue_sheet.tracks[track_num];
1563
1564 /* move all indices > index_num backward one space */
Josh Coalson4fa90592002-12-04 07:01:37 +00001565 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 +00001566
1567 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1568 cuesheet_calculate_length_(object);
1569 return true;
1570}
1571
1572FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1573{
1574 FLAC__ASSERT(0 != object);
1575 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1576
1577 if(0 == object->data.cue_sheet.tracks) {
1578 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1579 if(0 == new_num_tracks)
1580 return true;
1581 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1582 return false;
1583 }
1584 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001585 const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1586 const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1587
1588 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001589 if(new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
Josh Coalson0f008d22007-09-11 04:49:56 +00001590 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001591
1592 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1593
1594 /* if shrinking, free the truncated entries */
1595 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1596 unsigned i;
1597 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1598 if(0 != object->data.cue_sheet.tracks[i].indices)
1599 free(object->data.cue_sheet.tracks[i].indices);
1600 }
1601
1602 if(new_size == 0) {
1603 free(object->data.cue_sheet.tracks);
1604 object->data.cue_sheet.tracks = 0;
1605 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001606 else if(0 == (object->data.cue_sheet.tracks = realloc(object->data.cue_sheet.tracks, new_size)))
Josh Coalson8e9c4512002-11-14 05:00:24 +00001607 return false;
1608
1609 /* if growing, zero all the lengths/pointers of new elements */
1610 if(new_size > old_size)
1611 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1612 }
1613
1614 object->data.cue_sheet.num_tracks = new_num_tracks;
1615
1616 cuesheet_calculate_length_(object);
1617 return true;
1618}
1619
Josh Coalsondf7240a2002-11-16 06:32:30 +00001620FLAC_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 +00001621{
Josh Coalsonfb993862003-01-15 03:18:07 +00001622 FLAC__ASSERT(0 != object);
1623 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1624
Josh Coalsondf7240a2002-11-16 06:32:30 +00001625 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001626}
1627
Josh Coalsondf7240a2002-11-16 06:32:30 +00001628FLAC_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 +00001629{
1630 FLAC__StreamMetadata_CueSheet *cs;
1631
1632 FLAC__ASSERT(0 != object);
1633 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1634 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1635
1636 cs = &object->data.cue_sheet;
1637
1638 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1639 return false;
1640
1641 /* move all tracks >= track_num forward one space */
1642 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1643 cs->tracks[track_num].num_indices = 0;
1644 cs->tracks[track_num].indices = 0;
1645
1646 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1647}
1648
Josh Coalson3b026e82002-11-21 06:41:01 +00001649FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1650{
1651 FLAC__StreamMetadata_CueSheet_Track track;
1652 memset(&track, 0, sizeof(track));
1653 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1654}
1655
Josh Coalson8e9c4512002-11-14 05:00:24 +00001656FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1657{
1658 FLAC__StreamMetadata_CueSheet *cs;
1659
1660 FLAC__ASSERT(0 != object);
1661 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1662 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1663
1664 cs = &object->data.cue_sheet;
1665
1666 /* free the track at track_num */
1667 if(0 != cs->tracks[track_num].indices)
1668 free(cs->tracks[track_num].indices);
1669
1670 /* move all tracks > track_num backward one space */
1671 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1672 cs->tracks[cs->num_tracks-1].num_indices = 0;
1673 cs->tracks[cs->num_tracks-1].indices = 0;
1674
1675 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1676}
1677
1678FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1679{
1680 FLAC__ASSERT(0 != object);
1681 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1682
1683 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1684}
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001685
1686static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1687{
1688 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1689 return 0;
1690 else if (cs->tracks[track].indices[0].number == 1)
1691 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1692 else if (cs->tracks[track].num_indices < 2)
1693 return 0;
1694 else if (cs->tracks[track].indices[1].number == 1)
1695 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1696 else
1697 return 0;
1698}
1699
1700static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1701{
1702 FLAC__uint32 n = 0;
1703 while (x) {
1704 n += (x%10);
1705 x /= 10;
1706 }
1707 return n;
1708}
1709
Josh Coalson504dcaf2007-09-13 15:42:47 +00001710/*@@@@add to tests*/
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001711FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1712{
1713 const FLAC__StreamMetadata_CueSheet *cs;
1714
1715 FLAC__ASSERT(0 != object);
1716 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1717
1718 cs = &object->data.cue_sheet;
1719
1720 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1721 return 0;
1722
1723 {
1724 FLAC__uint32 i, length, sum = 0;
1725 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1726 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1727 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1728
1729 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1730 }
1731}
Josh Coalsone343ab22006-09-23 19:21:19 +00001732
1733FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1734{
1735 char *old;
1736 size_t old_length, new_length;
1737
1738 FLAC__ASSERT(0 != object);
1739 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1740 FLAC__ASSERT(0 != mime_type);
1741
1742 old = object->data.picture.mime_type;
1743 old_length = old? strlen(old) : 0;
1744 new_length = strlen(mime_type);
1745
1746 /* do the copy first so that if we fail we leave the object untouched */
1747 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001748 if(new_length >= SIZE_MAX) /* overflow check */
1749 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001750 if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1751 return false;
1752 }
1753 else {
1754 object->data.picture.mime_type = mime_type;
1755 }
1756
1757 if(0 != old)
1758 free(old);
1759
1760 object->length -= old_length;
1761 object->length += new_length;
1762 return true;
1763}
1764
1765FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1766{
1767 FLAC__byte *old;
1768 size_t old_length, new_length;
1769
1770 FLAC__ASSERT(0 != object);
1771 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1772 FLAC__ASSERT(0 != description);
1773
1774 old = object->data.picture.description;
1775 old_length = old? strlen((const char *)old) : 0;
1776 new_length = strlen((const char *)description);
1777
1778 /* do the copy first so that if we fail we leave the object untouched */
1779 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001780 if(new_length >= SIZE_MAX) /* overflow check */
1781 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001782 if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
1783 return false;
1784 }
1785 else {
1786 object->data.picture.description = description;
1787 }
1788
1789 if(0 != old)
1790 free(old);
1791
1792 object->length -= old_length;
1793 object->length += new_length;
1794 return true;
1795}
1796
1797FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1798{
1799 FLAC__byte *old;
1800
1801 FLAC__ASSERT(0 != object);
1802 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1803 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
1804
1805 old = object->data.picture.data;
1806
1807 /* do the copy first so that if we fail we leave the object untouched */
1808 if(copy) {
1809 if(!copy_bytes_(&object->data.picture.data, data, length))
1810 return false;
1811 }
1812 else {
1813 object->data.picture.data = data;
1814 }
1815
1816 if(0 != old)
1817 free(old);
1818
1819 object->length -= object->data.picture.data_length;
1820 object->data.picture.data_length = length;
1821 object->length += length;
1822 return true;
1823}
1824
1825FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1826{
1827 FLAC__ASSERT(0 != object);
1828 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1829
1830 return FLAC__format_picture_is_legal(&object->data.picture, violation);
1831}