blob: 1af0b0de395961deff6007d3ccec6910712baec2 [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
3 * Copyright (C) 2011-2013 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
443 if(type > FLAC__MAX_METADATA_TYPE_CODE)
444 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
1131 i = seek_table->num_points;
1132
Josh Coalsona65fd932006-10-03 01:02:44 +00001133 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
Josh Coalson6b21f662006-09-13 01:42:27 +00001134 return false;
1135
1136 sample = 0;
1137 for(j = 0; j < num; i++, j++, sample += samples) {
1138 seek_table->points[i].sample_number = sample;
1139 seek_table->points[i].stream_offset = 0;
1140 seek_table->points[i].frame_samples = 0;
1141 }
1142 }
1143
1144 return true;
1145}
1146
Josh Coalson6afed9f2002-10-16 22:29:47 +00001147FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +00001148{
1149 unsigned unique;
1150
1151 FLAC__ASSERT(0 != object);
1152 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1153
1154 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1155
1156 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1157}
1158
Josh Coalson6afed9f2002-10-16 22:29:47 +00001159FLAC_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 +00001160{
Josh Coalson2de11242004-12-30 03:41:19 +00001161 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1162 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001163 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001164}
1165
Josh Coalson6afed9f2002-10-16 22:29:47 +00001166FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +00001167{
1168 FLAC__ASSERT(0 != object);
1169 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1170
1171 if(0 == object->data.vorbis_comment.comments) {
1172 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1173 if(0 == new_num_comments)
1174 return true;
1175 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1176 return false;
1177 }
1178 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001179 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1180 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1181
1182 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001183 if(new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
Josh Coalson0f008d22007-09-11 04:49:56 +00001184 return false;
Josh Coalson90ced912002-05-30 05:23:38 +00001185
1186 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1187
1188 /* if shrinking, free the truncated entries */
1189 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1190 unsigned i;
1191 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1192 if(0 != object->data.vorbis_comment.comments[i].entry)
1193 free(object->data.vorbis_comment.comments[i].entry);
1194 }
1195
1196 if(new_size == 0) {
1197 free(object->data.vorbis_comment.comments);
1198 object->data.vorbis_comment.comments = 0;
1199 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001200 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +00001201 return false;
1202
1203 /* if growing, zero all the length/pointers of new elements */
1204 if(new_size > old_size)
1205 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1206 }
1207
1208 object->data.vorbis_comment.num_comments = new_num_comments;
1209
1210 vorbiscomment_calculate_length_(object);
1211 return true;
1212}
1213
Josh Coalson6afed9f2002-10-16 22:29:47 +00001214FLAC_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 +00001215{
Josh Coalsonfb993862003-01-15 03:18:07 +00001216 FLAC__ASSERT(0 != object);
1217 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1218
Josh Coalson2de11242004-12-30 03:41:19 +00001219 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1220 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001221 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001222}
1223
Josh Coalson6afed9f2002-10-16 22:29:47 +00001224FLAC_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 +00001225{
Josh Coalsoncc682512002-06-08 04:53:42 +00001226 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001227
1228 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001229 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001230 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001231
Josh Coalson2de11242004-12-30 03:41:19 +00001232 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1233 return false;
1234
Josh Coalson6b8e5302002-05-31 06:23:09 +00001235 vc = &object->data.vorbis_comment;
1236
1237 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001238 return false;
1239
1240 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001241 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 +00001242 vc->comments[comment_num].length = 0;
1243 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001244
1245 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1246}
1247
Josh Coalsondef597e2004-12-30 00:59:30 +00001248FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1249{
1250 FLAC__ASSERT(0 != object);
1251 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1252 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1253}
1254
1255FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1256{
1257 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
Josh Coalson2de11242004-12-30 03:41:19 +00001258
1259 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1260 return false;
1261
Josh Coalsondef597e2004-12-30 00:59:30 +00001262 {
1263 int i;
Josh Coalson9bedd782007-02-22 01:37:33 +00001264 size_t field_name_length;
Josh Coalsondef597e2004-12-30 00:59:30 +00001265 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1266
1267 FLAC__ASSERT(0 != eq);
1268
1269 if(0 == eq)
1270 return false; /* double protection */
1271
1272 field_name_length = eq-entry.entry;
1273
Josh Coalsone95399c2008-09-15 05:37:27 +00001274 i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
1275 if(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001276 unsigned indx = (unsigned)i;
1277 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, indx, entry, copy))
Josh Coalsondef597e2004-12-30 00:59:30 +00001278 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001279 entry = object->data.vorbis_comment.comments[indx];
1280 indx++; /* skip over replaced comment */
1281 if(all && indx < object->data.vorbis_comment.num_comments) {
1282 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsone95399c2008-09-15 05:37:27 +00001283 while(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001284 indx = (unsigned)i;
1285 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, indx))
Josh Coalsondef597e2004-12-30 00:59:30 +00001286 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001287 if(indx < object->data.vorbis_comment.num_comments)
1288 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001289 else
1290 i = -1;
1291 }
1292 }
1293 return true;
1294 }
1295 else
1296 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1297 }
1298}
1299
Josh Coalson6afed9f2002-10-16 22:29:47 +00001300FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001301{
Josh Coalsoncc682512002-06-08 04:53:42 +00001302 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001303
1304 FLAC__ASSERT(0 != object);
1305 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001306 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001307
Josh Coalson6b8e5302002-05-31 06:23:09 +00001308 vc = &object->data.vorbis_comment;
1309
Josh Coalson90ced912002-05-30 05:23:38 +00001310 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +00001311 if(0 != vc->comments[comment_num].entry)
1312 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001313
1314 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001315 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 +00001316 vc->comments[vc->num_comments-1].length = 0;
1317 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001318
Josh Coalson6b8e5302002-05-31 06:23:09 +00001319 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +00001320}
Josh Coalson45bb9882002-10-26 04:34:16 +00001321
Josh Coalsondef597e2004-12-30 00:59:30 +00001322FLAC_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 +00001323{
Josh Coalsondef597e2004-12-30 00:59:30 +00001324 FLAC__ASSERT(0 != entry);
1325 FLAC__ASSERT(0 != field_name);
1326 FLAC__ASSERT(0 != field_value);
1327
Josh Coalson2de11242004-12-30 03:41:19 +00001328 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1329 return false;
Josh Coalsonb4de1692005-08-24 07:39:25 +00001330 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
Josh Coalson2de11242004-12-30 03:41:19 +00001331 return false;
1332
Josh Coalsondef597e2004-12-30 00:59:30 +00001333 {
1334 const size_t nn = strlen(field_name);
1335 const size_t nv = strlen(field_value);
1336 entry->length = nn + 1 /*=*/ + nv;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001337 if(0 == (entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001338 return false;
1339 memcpy(entry->entry, field_name, nn);
1340 entry->entry[nn] = '=';
1341 memcpy(entry->entry+nn+1, field_value, nv);
1342 entry->entry[entry->length] = '\0';
1343 }
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001344
Josh Coalsondef597e2004-12-30 00:59:30 +00001345 return true;
1346}
1347
1348FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1349{
1350 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1351 FLAC__ASSERT(0 != field_name);
1352 FLAC__ASSERT(0 != field_value);
Josh Coalson2de11242004-12-30 03:41:19 +00001353
1354 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1355 return false;
1356
Josh Coalsondef597e2004-12-30 00:59:30 +00001357 {
1358 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1359 const size_t nn = eq-entry.entry;
1360 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1361 FLAC__ASSERT(0 != eq);
1362 if(0 == eq)
1363 return false; /* double protection */
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001364 if(0 == (*field_name = safe_malloc_add_2op_(nn, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001365 return false;
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001366 if(0 == (*field_value = safe_malloc_add_2op_(nv, /*+*/1))) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001367 free(*field_name);
1368 return false;
1369 }
1370 memcpy(*field_name, entry.entry, nn);
1371 memcpy(*field_value, entry.entry+nn+1, nv);
1372 (*field_name)[nn] = '\0';
1373 (*field_value)[nv] = '\0';
1374 }
1375
1376 return true;
1377}
1378
Josh Coalson44623112005-01-30 18:15:36 +00001379FLAC_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 +00001380{
1381 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1382 {
1383 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001384 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 +00001385 }
Josh Coalson45bb9882002-10-26 04:34:16 +00001386}
1387
Josh Coalsonb667e702002-11-05 07:24:33 +00001388FLAC_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 +00001389{
Josh Coalsondef597e2004-12-30 00:59:30 +00001390 FLAC__ASSERT(0 != field_name);
Josh Coalson45bb9882002-10-26 04:34:16 +00001391
Josh Coalsondef597e2004-12-30 00:59:30 +00001392 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
Josh Coalson45bb9882002-10-26 04:34:16 +00001393}
1394
1395FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1396{
1397 const unsigned field_name_length = strlen(field_name);
1398 unsigned i;
1399
1400 FLAC__ASSERT(0 != object);
1401 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1402
1403 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001404 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 +00001405 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1406 return -1;
1407 else
1408 return 1;
1409 }
1410 }
1411
1412 return 0;
1413}
1414
1415FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1416{
1417 FLAC__bool ok = true;
1418 unsigned matching = 0;
1419 const unsigned field_name_length = strlen(field_name);
1420 int i;
1421
1422 FLAC__ASSERT(0 != object);
1423 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1424
1425 /* must delete from end to start otherwise it will interfere with our iteration */
1426 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001427 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 +00001428 matching++;
1429 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1430 }
1431 }
1432
1433 return ok? (int)matching : -1;
1434}
Josh Coalson8e9c4512002-11-14 05:00:24 +00001435
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001436FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
Josh Coalsondf7240a2002-11-16 06:32:30 +00001437{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001438 return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
Josh Coalsondf7240a2002-11-16 06:32:30 +00001439}
1440
1441FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1442{
1443 FLAC__StreamMetadata_CueSheet_Track *to;
1444
1445 FLAC__ASSERT(0 != object);
1446
1447 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1448 if(!copy_track_(to, object)) {
1449 FLAC__metadata_object_cuesheet_track_delete(to);
1450 return 0;
1451 }
1452 }
1453
1454 return to;
1455}
1456
1457void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1458{
1459 FLAC__ASSERT(0 != object);
1460
1461 if(0 != object->indices) {
1462 FLAC__ASSERT(object->num_indices > 0);
1463 free(object->indices);
1464 }
1465}
1466
1467FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1468{
1469 FLAC__metadata_object_cuesheet_track_delete_data(object);
1470 free(object);
1471}
1472
Josh Coalson8e9c4512002-11-14 05:00:24 +00001473FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1474{
1475 FLAC__StreamMetadata_CueSheet_Track *track;
1476 FLAC__ASSERT(0 != object);
1477 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1478 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1479
1480 track = &object->data.cue_sheet.tracks[track_num];
1481
1482 if(0 == track->indices) {
1483 FLAC__ASSERT(track->num_indices == 0);
1484 if(0 == new_num_indices)
1485 return true;
1486 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1487 return false;
1488 }
1489 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001490 const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1491 const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1492
1493 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001494 if(new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
Josh Coalson0f008d22007-09-11 04:49:56 +00001495 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001496
1497 FLAC__ASSERT(track->num_indices > 0);
1498
1499 if(new_size == 0) {
1500 free(track->indices);
1501 track->indices = 0;
1502 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001503 else if(0 == (track->indices = realloc(track->indices, new_size)))
Josh Coalson8e9c4512002-11-14 05:00:24 +00001504 return false;
1505
1506 /* if growing, zero all the lengths/pointers of new elements */
1507 if(new_size > old_size)
1508 memset(track->indices + track->num_indices, 0, new_size - old_size);
1509 }
1510
1511 track->num_indices = new_num_indices;
1512
1513 cuesheet_calculate_length_(object);
1514 return true;
1515}
1516
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001517FLAC_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 +00001518{
1519 FLAC__StreamMetadata_CueSheet_Track *track;
1520
1521 FLAC__ASSERT(0 != object);
1522 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1523 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1524 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1525
1526 track = &object->data.cue_sheet.tracks[track_num];
1527
1528 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1529 return false;
1530
1531 /* move all indices >= index_num forward one space */
1532 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1533
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001534 track->indices[index_num] = indx;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001535 cuesheet_calculate_length_(object);
1536 return true;
1537}
1538
Josh Coalson3b026e82002-11-21 06:41:01 +00001539FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1540{
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001541 FLAC__StreamMetadata_CueSheet_Index indx;
1542 memset(&indx, 0, sizeof(indx));
1543 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, indx);
Josh Coalson3b026e82002-11-21 06:41:01 +00001544}
1545
Josh Coalson8e9c4512002-11-14 05:00:24 +00001546FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1547{
1548 FLAC__StreamMetadata_CueSheet_Track *track;
1549
1550 FLAC__ASSERT(0 != object);
1551 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1552 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1553 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1554
1555 track = &object->data.cue_sheet.tracks[track_num];
1556
1557 /* move all indices > index_num backward one space */
Josh Coalson4fa90592002-12-04 07:01:37 +00001558 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 +00001559
1560 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1561 cuesheet_calculate_length_(object);
1562 return true;
1563}
1564
1565FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1566{
1567 FLAC__ASSERT(0 != object);
1568 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1569
1570 if(0 == object->data.cue_sheet.tracks) {
1571 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1572 if(0 == new_num_tracks)
1573 return true;
1574 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1575 return false;
1576 }
1577 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001578 const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1579 const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1580
1581 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001582 if(new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
Josh Coalson0f008d22007-09-11 04:49:56 +00001583 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001584
1585 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1586
1587 /* if shrinking, free the truncated entries */
1588 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1589 unsigned i;
1590 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1591 if(0 != object->data.cue_sheet.tracks[i].indices)
1592 free(object->data.cue_sheet.tracks[i].indices);
1593 }
1594
1595 if(new_size == 0) {
1596 free(object->data.cue_sheet.tracks);
1597 object->data.cue_sheet.tracks = 0;
1598 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001599 else if(0 == (object->data.cue_sheet.tracks = realloc(object->data.cue_sheet.tracks, new_size)))
Josh Coalson8e9c4512002-11-14 05:00:24 +00001600 return false;
1601
1602 /* if growing, zero all the lengths/pointers of new elements */
1603 if(new_size > old_size)
1604 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1605 }
1606
1607 object->data.cue_sheet.num_tracks = new_num_tracks;
1608
1609 cuesheet_calculate_length_(object);
1610 return true;
1611}
1612
Josh Coalsondf7240a2002-11-16 06:32:30 +00001613FLAC_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 +00001614{
Josh Coalsonfb993862003-01-15 03:18:07 +00001615 FLAC__ASSERT(0 != object);
1616 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1617
Josh Coalsondf7240a2002-11-16 06:32:30 +00001618 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001619}
1620
Josh Coalsondf7240a2002-11-16 06:32:30 +00001621FLAC_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 +00001622{
1623 FLAC__StreamMetadata_CueSheet *cs;
1624
1625 FLAC__ASSERT(0 != object);
1626 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1627 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1628
1629 cs = &object->data.cue_sheet;
1630
1631 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1632 return false;
1633
1634 /* move all tracks >= track_num forward one space */
1635 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1636 cs->tracks[track_num].num_indices = 0;
1637 cs->tracks[track_num].indices = 0;
1638
1639 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1640}
1641
Josh Coalson3b026e82002-11-21 06:41:01 +00001642FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1643{
1644 FLAC__StreamMetadata_CueSheet_Track track;
1645 memset(&track, 0, sizeof(track));
1646 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1647}
1648
Josh Coalson8e9c4512002-11-14 05:00:24 +00001649FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1650{
1651 FLAC__StreamMetadata_CueSheet *cs;
1652
1653 FLAC__ASSERT(0 != object);
1654 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1655 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1656
1657 cs = &object->data.cue_sheet;
1658
1659 /* free the track at track_num */
1660 if(0 != cs->tracks[track_num].indices)
1661 free(cs->tracks[track_num].indices);
1662
1663 /* move all tracks > track_num backward one space */
1664 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1665 cs->tracks[cs->num_tracks-1].num_indices = 0;
1666 cs->tracks[cs->num_tracks-1].indices = 0;
1667
1668 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1669}
1670
1671FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1672{
1673 FLAC__ASSERT(0 != object);
1674 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1675
1676 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1677}
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001678
1679static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1680{
1681 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1682 return 0;
1683 else if (cs->tracks[track].indices[0].number == 1)
1684 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1685 else if (cs->tracks[track].num_indices < 2)
1686 return 0;
1687 else if (cs->tracks[track].indices[1].number == 1)
1688 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1689 else
1690 return 0;
1691}
1692
1693static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1694{
1695 FLAC__uint32 n = 0;
1696 while (x) {
1697 n += (x%10);
1698 x /= 10;
1699 }
1700 return n;
1701}
1702
Josh Coalson504dcaf2007-09-13 15:42:47 +00001703/*@@@@add to tests*/
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001704FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1705{
1706 const FLAC__StreamMetadata_CueSheet *cs;
1707
1708 FLAC__ASSERT(0 != object);
1709 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1710
1711 cs = &object->data.cue_sheet;
1712
1713 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1714 return 0;
1715
1716 {
1717 FLAC__uint32 i, length, sum = 0;
1718 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1719 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1720 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1721
1722 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1723 }
1724}
Josh Coalsone343ab22006-09-23 19:21:19 +00001725
1726FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1727{
1728 char *old;
1729 size_t old_length, new_length;
1730
1731 FLAC__ASSERT(0 != object);
1732 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1733 FLAC__ASSERT(0 != mime_type);
1734
1735 old = object->data.picture.mime_type;
1736 old_length = old? strlen(old) : 0;
1737 new_length = strlen(mime_type);
1738
1739 /* do the copy first so that if we fail we leave the object untouched */
1740 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001741 if(new_length >= SIZE_MAX) /* overflow check */
1742 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001743 if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1744 return false;
1745 }
1746 else {
1747 object->data.picture.mime_type = mime_type;
1748 }
1749
1750 if(0 != old)
1751 free(old);
1752
1753 object->length -= old_length;
1754 object->length += new_length;
1755 return true;
1756}
1757
1758FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1759{
1760 FLAC__byte *old;
1761 size_t old_length, new_length;
1762
1763 FLAC__ASSERT(0 != object);
1764 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1765 FLAC__ASSERT(0 != description);
1766
1767 old = object->data.picture.description;
1768 old_length = old? strlen((const char *)old) : 0;
1769 new_length = strlen((const char *)description);
1770
1771 /* do the copy first so that if we fail we leave the object untouched */
1772 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001773 if(new_length >= SIZE_MAX) /* overflow check */
1774 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001775 if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
1776 return false;
1777 }
1778 else {
1779 object->data.picture.description = description;
1780 }
1781
1782 if(0 != old)
1783 free(old);
1784
1785 object->length -= old_length;
1786 object->length += new_length;
1787 return true;
1788}
1789
1790FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1791{
1792 FLAC__byte *old;
1793
1794 FLAC__ASSERT(0 != object);
1795 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1796 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
1797
1798 old = object->data.picture.data;
1799
1800 /* do the copy first so that if we fail we leave the object untouched */
1801 if(copy) {
1802 if(!copy_bytes_(&object->data.picture.data, data, length))
1803 return false;
1804 }
1805 else {
1806 object->data.picture.data = data;
1807 }
1808
1809 if(0 != old)
1810 free(old);
1811
1812 object->length -= object->data.picture.data_length;
1813 object->data.picture.data_length = length;
1814 object->length += length;
1815 return true;
1816}
1817
1818FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1819{
1820 FLAC__ASSERT(0 != object);
1821 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1822
1823 return FLAC__format_picture_is_legal(&object->data.picture, violation);
1824}