blob: 82abe4179fabbdc02e9a2d1a0cb4282a94bb1b1e [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)) {
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +100090 free(*to);
Josh Coalsone343ab22006-09-23 19:21:19 +000091 *to = copy;
92 return true;
93 }
94 else
95 return false;
96}
97#endif
98
99/* reallocate entry to 1 byte larger and add a terminating NUL */
100/* realloc() failure leaves entry unchanged */
Josh Coalsondef597e2004-12-30 00:59:30 +0000101static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
102{
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000103 FLAC__byte *x = safe_realloc_add_2op_(*entry, length, /*+*/1);
Josh Coalsondef597e2004-12-30 00:59:30 +0000104 if(0 != x) {
105 x[length] = '\0';
106 *entry = x;
107 return true;
108 }
109 else
110 return false;
111}
112
Josh Coalsone343ab22006-09-23 19:21:19 +0000113/* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
114 * unchanged if malloc fails, free()ing the original '*to' if it
115 * succeeds and the original '*to' was not NULL
116 */
117static FLAC__bool copy_cstring_(char **to, const char *from)
118{
Josh Coalsone343ab22006-09-23 19:21:19 +0000119 char *copy = strdup(from);
Josh Coalsona65fd932006-10-03 01:02:44 +0000120 FLAC__ASSERT(to);
Josh Coalsone343ab22006-09-23 19:21:19 +0000121 if(copy) {
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000122 free(*to);
Josh Coalsone343ab22006-09-23 19:21:19 +0000123 *to = copy;
124 return true;
125 }
126 else
127 return false;
128}
129
Josh Coalsoncc682512002-06-08 04:53:42 +0000130static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
Josh Coalson90ced912002-05-30 05:23:38 +0000131{
132 to->length = from->length;
133 if(0 == from->entry) {
134 FLAC__ASSERT(from->length == 0);
135 to->entry = 0;
136 }
137 else {
138 FLAC__byte *x;
139 FLAC__ASSERT(from->length > 0);
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000140 if(0 == (x = safe_malloc_add_2op_(from->length, /*+*/1)))
Josh Coalson90ced912002-05-30 05:23:38 +0000141 return false;
142 memcpy(x, from->entry, from->length);
Josh Coalsondef597e2004-12-30 00:59:30 +0000143 x[from->length] = '\0';
Josh Coalson90ced912002-05-30 05:23:38 +0000144 to->entry = x;
145 }
146 return true;
147}
148
Josh Coalson8e9c4512002-11-14 05:00:24 +0000149static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
150{
151 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
152 if(0 == from->indices) {
153 FLAC__ASSERT(from->num_indices == 0);
154 }
155 else {
156 FLAC__StreamMetadata_CueSheet_Index *x;
157 FLAC__ASSERT(from->num_indices > 0);
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +1000158 if(0 == (x = safe_malloc_mul_2op_p(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))))
Josh Coalson8e9c4512002-11-14 05:00:24 +0000159 return false;
160 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
161 to->indices = x;
162 }
163 return true;
164}
165
Josh Coalsoncc682512002-06-08 04:53:42 +0000166static void seektable_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000167{
168 FLAC__ASSERT(0 != object);
169 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
170
171 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
172}
173
Josh Coalsoncc682512002-06-08 04:53:42 +0000174static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000175{
Josh Coalsoncc682512002-06-08 04:53:42 +0000176 FLAC__StreamMetadata_SeekPoint *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000177
178 FLAC__ASSERT(num_points > 0);
179
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +1000180 object_array = safe_malloc_mul_2op_p(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
Josh Coalson90ced912002-05-30 05:23:38 +0000181
182 if(0 != object_array) {
183 unsigned i;
184 for(i = 0; i < num_points; i++) {
185 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
186 object_array[i].stream_offset = 0;
187 object_array[i].frame_samples = 0;
188 }
189 }
190
191 return object_array;
192}
193
Josh Coalsoncc682512002-06-08 04:53:42 +0000194static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000195{
196 unsigned i;
197
198 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
199
200 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
201 object->length += object->data.vorbis_comment.vendor_string.length;
202 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
203 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
204 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
205 object->length += object->data.vorbis_comment.comments[i].length;
206 }
207}
208
Josh Coalsoncc682512002-06-08 04:53:42 +0000209static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000210{
Josh Coalson90ced912002-05-30 05:23:38 +0000211 FLAC__ASSERT(num_comments > 0);
212
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000213 return safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000214}
215
Josh Coalsoncc682512002-06-08 04:53:42 +0000216static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000217{
218 unsigned i;
219
220 FLAC__ASSERT(0 != object_array && num_comments > 0);
221
222 for(i = 0; i < num_comments; i++)
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000223 free(object_array[i].entry);
Josh Coalson90ced912002-05-30 05:23:38 +0000224
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000225 free(object_array);
Josh Coalson90ced912002-05-30 05:23:38 +0000226}
227
Josh Coalsoncc682512002-06-08 04:53:42 +0000228static 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 +0000229{
Josh Coalsoncc682512002-06-08 04:53:42 +0000230 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000231
232 FLAC__ASSERT(0 != object_array);
233 FLAC__ASSERT(num_comments > 0);
234
235 return_array = vorbiscomment_entry_array_new_(num_comments);
236
237 if(0 != return_array) {
238 unsigned i;
239
Josh Coalson90ced912002-05-30 05:23:38 +0000240 for(i = 0; i < num_comments; i++) {
241 if(!copy_vcentry_(return_array+i, object_array+i)) {
242 vorbiscomment_entry_array_delete_(return_array, num_comments);
243 return 0;
244 }
245 }
246 }
247
248 return return_array;
249}
250
Josh Coalsoncc682512002-06-08 04:53:42 +0000251static 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 +0000252{
253 FLAC__byte *save;
254
255 FLAC__ASSERT(0 != object);
256 FLAC__ASSERT(0 != dest);
257 FLAC__ASSERT(0 != src);
258 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson748459c2004-09-08 00:49:30 +0000259 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
Josh Coalson90ced912002-05-30 05:23:38 +0000260
Josh Coalson6b8e5302002-05-31 06:23:09 +0000261 save = dest->entry;
Josh Coalson90ced912002-05-30 05:23:38 +0000262
Erik de Castro Lopo49d9d742014-03-23 21:40:54 +1100263 if(0 != src->entry) {
Josh Coalsondef597e2004-12-30 00:59:30 +0000264 if(copy) {
265 /* do the copy first so that if we fail we leave the dest object untouched */
266 if(!copy_vcentry_(dest, src))
267 return false;
268 }
269 else {
270 /* we have to make sure that the string we're taking over is null-terminated */
271
272 /*
273 * Stripping the const from src->entry is OK since we're taking
274 * ownership of the pointer. This is a hack around a deficiency
275 * in the API where the same function is used for 'copy' and
276 * 'own', but the source entry is a const pointer. If we were
277 * precise, the 'own' flavor would be a separate function with a
278 * non-const source pointer. But it's not, so we hack away.
279 */
280 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
281 return false;
282 *dest = *src;
283 }
Josh Coalson90ced912002-05-30 05:23:38 +0000284 }
285 else {
Josh Coalsondef597e2004-12-30 00:59:30 +0000286 /* the src is null */
Josh Coalson90ced912002-05-30 05:23:38 +0000287 *dest = *src;
288 }
289
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000290 free(save);
Josh Coalson90ced912002-05-30 05:23:38 +0000291
292 vorbiscomment_calculate_length_(object);
293 return true;
294}
295
Josh Coalsondef597e2004-12-30 00:59:30 +0000296static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
297{
298 unsigned i;
299
300 FLAC__ASSERT(0 != object);
301 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
302 FLAC__ASSERT(0 != field_name);
303
304 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
305 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
306 return (int)i;
307 }
308
309 return -1;
310}
311
Josh Coalson8e9c4512002-11-14 05:00:24 +0000312static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
313{
314 unsigned i;
315
316 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
317
318 object->length = (
319 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
320 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000321 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
Josh Coalsondf7240a2002-11-16 06:32:30 +0000322 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
Josh Coalson8e9c4512002-11-14 05:00:24 +0000323 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
324 ) / 8;
325
326 object->length += object->data.cue_sheet.num_tracks * (
327 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
328 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
329 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
330 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
331 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
332 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
333 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
334 ) / 8;
335
336 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
337 object->length += object->data.cue_sheet.tracks[i].num_indices * (
338 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
339 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
340 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
341 ) / 8;
342 }
343}
344
345static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
346{
347 FLAC__ASSERT(num_indices > 0);
348
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000349 return safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000350}
351
352static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
353{
354 FLAC__ASSERT(num_tracks > 0);
355
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000356 return safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000357}
358
359static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
360{
361 unsigned i;
362
363 FLAC__ASSERT(0 != object_array && num_tracks > 0);
364
365 for(i = 0; i < num_tracks; i++) {
366 if(0 != object_array[i].indices) {
367 FLAC__ASSERT(object_array[i].num_indices > 0);
368 free(object_array[i].indices);
369 }
370 }
371
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000372 free(object_array);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000373}
374
375static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
376{
377 FLAC__StreamMetadata_CueSheet_Track *return_array;
378
379 FLAC__ASSERT(0 != object_array);
380 FLAC__ASSERT(num_tracks > 0);
381
382 return_array = cuesheet_track_array_new_(num_tracks);
383
384 if(0 != return_array) {
385 unsigned i;
386
387 for(i = 0; i < num_tracks; i++) {
388 if(!copy_track_(return_array+i, object_array+i)) {
389 cuesheet_track_array_delete_(return_array, num_tracks);
390 return 0;
391 }
392 }
393 }
394
395 return return_array;
396}
397
398static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
399{
400 FLAC__StreamMetadata_CueSheet_Index *save;
401
402 FLAC__ASSERT(0 != object);
403 FLAC__ASSERT(0 != dest);
404 FLAC__ASSERT(0 != src);
405 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
406 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000407
408 save = dest->indices;
409
410 /* do the copy first so that if we fail we leave the object untouched */
411 if(copy) {
412 if(!copy_track_(dest, src))
413 return false;
414 }
415 else {
416 *dest = *src;
417 }
418
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000419 free(save);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000420
421 cuesheet_calculate_length_(object);
422 return true;
423}
424
Josh Coalson90ced912002-05-30 05:23:38 +0000425
426/****************************************************************************
427 *
428 * Metadata object routines
429 *
430 ***************************************************************************/
431
Josh Coalson6afed9f2002-10-16 22:29:47 +0000432FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
Josh Coalson90ced912002-05-30 05:23:38 +0000433{
Josh Coalson1cb23412004-07-22 01:32:00 +0000434 FLAC__StreamMetadata *object;
435
Erik de Castro Lopo3f5208c2014-04-09 17:56:13 +1000436 if(type > FLAC__MAX_METADATA_TYPE)
Josh Coalson1cb23412004-07-22 01:32:00 +0000437 return 0;
438
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000439 object = calloc(1, sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000440 if(0 != object) {
Josh Coalson90ced912002-05-30 05:23:38 +0000441 object->is_last = false;
442 object->type = type;
443 switch(type) {
444 case FLAC__METADATA_TYPE_STREAMINFO:
445 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
446 break;
447 case FLAC__METADATA_TYPE_PADDING:
Josh Coalsond0609472003-01-10 05:37:13 +0000448 /* calloc() took care of this for us:
449 object->length = 0;
450 */
Josh Coalson90ced912002-05-30 05:23:38 +0000451 break;
452 case FLAC__METADATA_TYPE_APPLICATION:
453 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
Josh Coalsond0609472003-01-10 05:37:13 +0000454 /* calloc() took care of this for us:
455 object->data.application.data = 0;
456 */
Josh Coalson90ced912002-05-30 05:23:38 +0000457 break;
458 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsond0609472003-01-10 05:37:13 +0000459 /* calloc() took care of this for us:
460 object->length = 0;
461 object->data.seek_table.num_points = 0;
462 object->data.seek_table.points = 0;
463 */
Josh Coalson90ced912002-05-30 05:23:38 +0000464 break;
465 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalsone343ab22006-09-23 19:21:19 +0000466 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
467 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
468 free(object);
469 return 0;
Josh Coalson45bb9882002-10-26 04:34:16 +0000470 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000471 vorbiscomment_calculate_length_(object);
Josh Coalson90ced912002-05-30 05:23:38 +0000472 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000473 case FLAC__METADATA_TYPE_CUESHEET:
474 cuesheet_calculate_length_(object);
475 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000476 case FLAC__METADATA_TYPE_PICTURE:
477 object->length = (
478 FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
479 FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
480 FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
481 FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
482 FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
483 FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
Josh Coalson74ed2942006-09-23 23:15:05 +0000484 FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
Josh Coalsone343ab22006-09-23 19:21:19 +0000485 FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
486 0 /* no data */
487 ) / 8;
488 object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
489 object->data.picture.mime_type = 0;
490 object->data.picture.description = 0;
491 /* calloc() took care of this for us:
492 object->data.picture.width = 0;
493 object->data.picture.height = 0;
494 object->data.picture.depth = 0;
Josh Coalson74ed2942006-09-23 23:15:05 +0000495 object->data.picture.colors = 0;
Josh Coalsone343ab22006-09-23 19:21:19 +0000496 object->data.picture.data_length = 0;
497 object->data.picture.data = 0;
498 */
499 /* now initialize mime_type and description with empty strings to make things easier on the client */
500 if(!copy_cstring_(&object->data.picture.mime_type, "")) {
501 free(object);
502 return 0;
503 }
504 if(!copy_cstring_((char**)(&object->data.picture.description), "")) {
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000505 free(object->data.picture.mime_type);
Josh Coalsone343ab22006-09-23 19:21:19 +0000506 free(object);
507 return 0;
508 }
509 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000510 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000511 /* calloc() took care of this for us:
512 object->length = 0;
513 object->data.unknown.data = 0;
514 */
515 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000516 }
517 }
518
519 return object;
520}
521
Josh Coalson6afed9f2002-10-16 22:29:47 +0000522FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000523{
Josh Coalsoncc682512002-06-08 04:53:42 +0000524 FLAC__StreamMetadata *to;
Josh Coalson90ced912002-05-30 05:23:38 +0000525
526 FLAC__ASSERT(0 != object);
527
528 if(0 != (to = FLAC__metadata_object_new(object->type))) {
529 to->is_last = object->is_last;
530 to->type = object->type;
531 to->length = object->length;
532 switch(to->type) {
533 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalsoncc682512002-06-08 04:53:42 +0000534 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
Josh Coalson90ced912002-05-30 05:23:38 +0000535 break;
536 case FLAC__METADATA_TYPE_PADDING:
537 break;
538 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson0f008d22007-09-11 04:49:56 +0000539 if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
540 FLAC__metadata_object_delete(to);
541 return 0;
542 }
Josh Coalson90ced912002-05-30 05:23:38 +0000543 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
544 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
545 FLAC__metadata_object_delete(to);
546 return 0;
547 }
548 break;
549 case FLAC__METADATA_TYPE_SEEKTABLE:
550 to->data.seek_table.num_points = object->data.seek_table.num_points;
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100551 if(to->data.seek_table.num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
Josh Coalson0f008d22007-09-11 04:49:56 +0000552 FLAC__metadata_object_delete(to);
553 return 0;
554 }
Josh Coalsoncc682512002-06-08 04:53:42 +0000555 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 +0000556 FLAC__metadata_object_delete(to);
557 return 0;
558 }
559 break;
560 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000561 if(0 != to->data.vorbis_comment.vendor_string.entry) {
Josh Coalson45bb9882002-10-26 04:34:16 +0000562 free(to->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000563 to->data.vorbis_comment.vendor_string.entry = 0;
564 }
Josh Coalson90ced912002-05-30 05:23:38 +0000565 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
566 FLAC__metadata_object_delete(to);
567 return 0;
568 }
569 if(object->data.vorbis_comment.num_comments == 0) {
Erik de Castro Lopod939b442015-07-05 20:54:28 +1000570 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
Josh Coalson90ced912002-05-30 05:23:38 +0000571 to->data.vorbis_comment.comments = 0;
572 }
573 else {
574 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
575 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
576 if(0 == to->data.vorbis_comment.comments) {
Erik de Castro Lopoff507792015-07-05 21:21:44 +1000577 to->data.vorbis_comment.num_comments = 0;
Josh Coalson90ced912002-05-30 05:23:38 +0000578 FLAC__metadata_object_delete(to);
579 return 0;
580 }
581 }
582 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
583 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000584 case FLAC__METADATA_TYPE_CUESHEET:
585 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
586 if(object->data.cue_sheet.num_tracks == 0) {
587 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
588 }
589 else {
590 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
591 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
592 if(0 == to->data.cue_sheet.tracks) {
593 FLAC__metadata_object_delete(to);
594 return 0;
595 }
596 }
597 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000598 case FLAC__METADATA_TYPE_PICTURE:
599 to->data.picture.type = object->data.picture.type;
600 if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
601 FLAC__metadata_object_delete(to);
602 return 0;
603 }
604 if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
605 FLAC__metadata_object_delete(to);
606 return 0;
607 }
608 to->data.picture.width = object->data.picture.width;
609 to->data.picture.height = object->data.picture.height;
610 to->data.picture.depth = object->data.picture.depth;
Josh Coalson74ed2942006-09-23 23:15:05 +0000611 to->data.picture.colors = object->data.picture.colors;
Josh Coalsone343ab22006-09-23 19:21:19 +0000612 to->data.picture.data_length = object->data.picture.data_length;
613 if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
614 FLAC__metadata_object_delete(to);
615 return 0;
616 }
617 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000618 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000619 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
620 FLAC__metadata_object_delete(to);
621 return 0;
622 }
623 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000624 }
625 }
626
627 return to;
628}
629
Josh Coalsoncc682512002-06-08 04:53:42 +0000630void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000631{
632 FLAC__ASSERT(0 != object);
633
634 switch(object->type) {
635 case FLAC__METADATA_TYPE_STREAMINFO:
636 case FLAC__METADATA_TYPE_PADDING:
637 break;
638 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson4fa90592002-12-04 07:01:37 +0000639 if(0 != object->data.application.data) {
Josh Coalson90ced912002-05-30 05:23:38 +0000640 free(object->data.application.data);
Josh Coalson4fa90592002-12-04 07:01:37 +0000641 object->data.application.data = 0;
642 }
Josh Coalson90ced912002-05-30 05:23:38 +0000643 break;
644 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalson4fa90592002-12-04 07:01:37 +0000645 if(0 != object->data.seek_table.points) {
Josh Coalson90ced912002-05-30 05:23:38 +0000646 free(object->data.seek_table.points);
Josh Coalson4fa90592002-12-04 07:01:37 +0000647 object->data.seek_table.points = 0;
648 }
Josh Coalson90ced912002-05-30 05:23:38 +0000649 break;
650 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000651 if(0 != object->data.vorbis_comment.vendor_string.entry) {
Josh Coalson90ced912002-05-30 05:23:38 +0000652 free(object->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000653 object->data.vorbis_comment.vendor_string.entry = 0;
654 }
Josh Coalson90ced912002-05-30 05:23:38 +0000655 if(0 != object->data.vorbis_comment.comments) {
656 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
657 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
658 }
659 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000660 case FLAC__METADATA_TYPE_CUESHEET:
661 if(0 != object->data.cue_sheet.tracks) {
662 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
663 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
664 }
665 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000666 case FLAC__METADATA_TYPE_PICTURE:
667 if(0 != object->data.picture.mime_type) {
668 free(object->data.picture.mime_type);
669 object->data.picture.mime_type = 0;
670 }
671 if(0 != object->data.picture.description) {
672 free(object->data.picture.description);
673 object->data.picture.description = 0;
674 }
675 if(0 != object->data.picture.data) {
676 free(object->data.picture.data);
677 object->data.picture.data = 0;
678 }
679 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000680 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000681 if(0 != object->data.unknown.data) {
682 free(object->data.unknown.data);
683 object->data.unknown.data = 0;
684 }
685 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000686 }
687}
688
Josh Coalson6afed9f2002-10-16 22:29:47 +0000689FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000690{
691 FLAC__metadata_object_delete_data(object);
692 free(object);
693}
694
Josh Coalsoncc682512002-06-08 04:53:42 +0000695static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000696{
697 if(block1->min_blocksize != block2->min_blocksize)
698 return false;
699 if(block1->max_blocksize != block2->max_blocksize)
700 return false;
701 if(block1->min_framesize != block2->min_framesize)
702 return false;
703 if(block1->max_framesize != block2->max_framesize)
704 return false;
705 if(block1->sample_rate != block2->sample_rate)
706 return false;
707 if(block1->channels != block2->channels)
708 return false;
709 if(block1->bits_per_sample != block2->bits_per_sample)
710 return false;
711 if(block1->total_samples != block2->total_samples)
712 return false;
713 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
714 return false;
715 return true;
716}
717
Josh Coalsoncc682512002-06-08 04:53:42 +0000718static 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 +0000719{
720 FLAC__ASSERT(0 != block1);
721 FLAC__ASSERT(0 != block2);
722 FLAC__ASSERT(block_length >= sizeof(block1->id));
723
724 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
725 return false;
726 if(0 != block1->data && 0 != block2->data)
727 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
728 else
729 return block1->data == block2->data;
730}
731
Josh Coalsoncc682512002-06-08 04:53:42 +0000732static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000733{
734 unsigned i;
735
736 FLAC__ASSERT(0 != block1);
737 FLAC__ASSERT(0 != block2);
738
739 if(block1->num_points != block2->num_points)
740 return false;
741
742 if(0 != block1->points && 0 != block2->points) {
743 for(i = 0; i < block1->num_points; i++) {
744 if(block1->points[i].sample_number != block2->points[i].sample_number)
745 return false;
746 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
747 return false;
748 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
749 return false;
750 }
751 return true;
752 }
753 else
754 return block1->points == block2->points;
755}
756
Josh Coalsoncc682512002-06-08 04:53:42 +0000757static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000758{
759 unsigned i;
760
761 if(block1->vendor_string.length != block2->vendor_string.length)
762 return false;
763
764 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
765 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
766 return false;
767 }
768 else if(block1->vendor_string.entry != block2->vendor_string.entry)
769 return false;
770
771 if(block1->num_comments != block2->num_comments)
772 return false;
773
774 for(i = 0; i < block1->num_comments; i++) {
775 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
776 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
777 return false;
778 }
779 else if(block1->comments[i].entry != block2->comments[i].entry)
780 return false;
781 }
782 return true;
783}
784
Josh Coalson8e9c4512002-11-14 05:00:24 +0000785static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
786{
787 unsigned i, j;
788
789 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
790 return false;
791
792 if(block1->lead_in != block2->lead_in)
793 return false;
794
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000795 if(block1->is_cd != block2->is_cd)
796 return false;
797
Josh Coalson8e9c4512002-11-14 05:00:24 +0000798 if(block1->num_tracks != block2->num_tracks)
799 return false;
800
801 if(0 != block1->tracks && 0 != block2->tracks) {
802 FLAC__ASSERT(block1->num_tracks > 0);
803 for(i = 0; i < block1->num_tracks; i++) {
804 if(block1->tracks[i].offset != block2->tracks[i].offset)
805 return false;
806 if(block1->tracks[i].number != block2->tracks[i].number)
807 return false;
808 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
809 return false;
810 if(block1->tracks[i].type != block2->tracks[i].type)
811 return false;
812 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
813 return false;
814 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
815 return false;
816 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
817 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
818 for(j = 0; j < block1->tracks[i].num_indices; j++) {
819 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
820 return false;
821 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
822 return false;
823 }
824 }
825 else if(block1->tracks[i].indices != block2->tracks[i].indices)
826 return false;
827 }
828 }
829 else if(block1->tracks != block2->tracks)
830 return false;
831 return true;
832}
833
Josh Coalsone343ab22006-09-23 19:21:19 +0000834static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
835{
836 if(block1->type != block2->type)
837 return false;
838 if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type)))
839 return false;
840 if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description)))
841 return false;
842 if(block1->width != block2->width)
843 return false;
844 if(block1->height != block2->height)
845 return false;
846 if(block1->depth != block2->depth)
847 return false;
Josh Coalson74ed2942006-09-23 23:15:05 +0000848 if(block1->colors != block2->colors)
849 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +0000850 if(block1->data_length != block2->data_length)
851 return false;
852 if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length)))
853 return false;
854 return true;
855}
856
Josh Coalsond0609472003-01-10 05:37:13 +0000857static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
858{
859 FLAC__ASSERT(0 != block1);
860 FLAC__ASSERT(0 != block2);
861
862 if(0 != block1->data && 0 != block2->data)
863 return 0 == memcmp(block1->data, block2->data, block_length);
864 else
865 return block1->data == block2->data;
866}
867
Josh Coalson6afed9f2002-10-16 22:29:47 +0000868FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000869{
Josh Coalsond8ab3462002-07-11 05:54:05 +0000870 FLAC__ASSERT(0 != block1);
871 FLAC__ASSERT(0 != block2);
872
Josh Coalson57ba6f42002-06-07 05:27:37 +0000873 if(block1->type != block2->type) {
874 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000875 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000876 if(block1->is_last != block2->is_last) {
877 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000878 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000879 if(block1->length != block2->length) {
880 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000881 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000882 switch(block1->type) {
883 case FLAC__METADATA_TYPE_STREAMINFO:
884 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
885 case FLAC__METADATA_TYPE_PADDING:
886 return true; /* we don't compare the padding guts */
887 case FLAC__METADATA_TYPE_APPLICATION:
888 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
889 case FLAC__METADATA_TYPE_SEEKTABLE:
890 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
891 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
892 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000893 case FLAC__METADATA_TYPE_CUESHEET:
894 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +0000895 case FLAC__METADATA_TYPE_PICTURE:
896 return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000897 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000898 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000899 }
900}
901
Josh Coalson6afed9f2002-10-16 22:29:47 +0000902FLAC_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 +0000903{
904 FLAC__byte *save;
905
Josh Coalsond8ab3462002-07-11 05:54:05 +0000906 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000907 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
908 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
909
910 save = object->data.application.data;
911
912 /* do the copy first so that if we fail we leave the object untouched */
913 if(copy) {
914 if(!copy_bytes_(&object->data.application.data, data, length))
915 return false;
916 }
917 else {
918 object->data.application.data = data;
919 }
920
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +1000921 free(save);
Josh Coalson90ced912002-05-30 05:23:38 +0000922
923 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
924 return true;
925}
926
Josh Coalson6afed9f2002-10-16 22:29:47 +0000927FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000928{
929 FLAC__ASSERT(0 != object);
930 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
931
932 if(0 == object->data.seek_table.points) {
933 FLAC__ASSERT(object->data.seek_table.num_points == 0);
934 if(0 == new_num_points)
935 return true;
936 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
937 return false;
938 }
939 else {
Josh Coalson0f008d22007-09-11 04:49:56 +0000940 const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
941 const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
942
943 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +1100944 if(new_num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
Josh Coalson0f008d22007-09-11 04:49:56 +0000945 return false;
Josh Coalson90ced912002-05-30 05:23:38 +0000946
947 FLAC__ASSERT(object->data.seek_table.num_points > 0);
948
949 if(new_size == 0) {
950 free(object->data.seek_table.points);
951 object->data.seek_table.points = 0;
952 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000953 else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +0000954 return false;
955
956 /* if growing, set new elements to placeholders */
957 if(new_size > old_size) {
958 unsigned i;
959 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
960 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
961 object->data.seek_table.points[i].stream_offset = 0;
962 object->data.seek_table.points[i].frame_samples = 0;
963 }
964 }
965 }
966
967 object->data.seek_table.num_points = new_num_points;
968
969 seektable_calculate_length_(object);
970 return true;
971}
972
Josh Coalson6afed9f2002-10-16 22:29:47 +0000973FLAC_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 +0000974{
975 FLAC__ASSERT(0 != object);
976 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000977 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000978
979 object->data.seek_table.points[point_num] = point;
980}
981
Josh Coalson6afed9f2002-10-16 22:29:47 +0000982FLAC_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 +0000983{
984 int i;
985
986 FLAC__ASSERT(0 != object);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000987 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000988 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000989
990 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
991 return false;
992
993 /* move all points >= point_num forward one space */
994 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
995 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
996
997 FLAC__metadata_object_seektable_set_point(object, point_num, point);
998 seektable_calculate_length_(object);
999 return true;
1000}
1001
Josh Coalson6afed9f2002-10-16 22:29:47 +00001002FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001003{
1004 unsigned i;
1005
1006 FLAC__ASSERT(0 != object);
1007 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001008 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +00001009
1010 /* move all points > point_num backward one space */
1011 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
1012 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
1013
1014 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
1015}
1016
Josh Coalson6afed9f2002-10-16 22:29:47 +00001017FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
Josh Coalson28e08d82002-06-05 05:56:41 +00001018{
Josh Coalson28e08d82002-06-05 05:56:41 +00001019 FLAC__ASSERT(0 != object);
1020 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1021
Josh Coalson0833f342002-07-15 05:31:55 +00001022 return FLAC__format_seektable_is_legal(&object->data.seek_table);
Josh Coalson28e08d82002-06-05 05:56:41 +00001023}
1024
Josh Coalson6afed9f2002-10-16 22:29:47 +00001025FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
Josh Coalson5a5de732002-08-01 06:37:11 +00001026{
1027 FLAC__ASSERT(0 != object);
1028 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1029
1030 if(num > 0)
1031 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
1032 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
1033 else
1034 return true;
1035}
1036
Josh Coalson6afed9f2002-10-16 22:29:47 +00001037FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
Josh Coalson5a5de732002-08-01 06:37:11 +00001038{
1039 FLAC__StreamMetadata_SeekTable *seek_table;
1040
1041 FLAC__ASSERT(0 != object);
1042 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1043
1044 seek_table = &object->data.seek_table;
1045
1046 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
1047 return false;
1048
1049 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
1050 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
1051 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
1052
1053 return true;
1054}
1055
Josh Coalson6afed9f2002-10-16 22:29:47 +00001056FLAC_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 +00001057{
1058 FLAC__ASSERT(0 != object);
1059 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1060 FLAC__ASSERT(0 != sample_numbers || num == 0);
1061
1062 if(num > 0) {
1063 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1064 unsigned i, j;
1065
1066 i = seek_table->num_points;
1067
1068 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1069 return false;
1070
1071 for(j = 0; j < num; i++, j++) {
1072 seek_table->points[i].sample_number = sample_numbers[j];
1073 seek_table->points[i].stream_offset = 0;
1074 seek_table->points[i].frame_samples = 0;
1075 }
1076 }
1077
1078 return true;
1079}
1080
Josh Coalson6afed9f2002-10-16 22:29:47 +00001081FLAC_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 +00001082{
1083 FLAC__ASSERT(0 != object);
1084 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1085 FLAC__ASSERT(total_samples > 0);
1086
Josh Coalson6b21f662006-09-13 01:42:27 +00001087 if(num > 0 && total_samples > 0) {
Josh Coalson5a5de732002-08-01 06:37:11 +00001088 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1089 unsigned i, j;
1090
1091 i = seek_table->num_points;
1092
1093 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1094 return false;
1095
1096 for(j = 0; j < num; i++, j++) {
1097 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
1098 seek_table->points[i].stream_offset = 0;
1099 seek_table->points[i].frame_samples = 0;
1100 }
1101 }
1102
1103 return true;
1104}
1105
Josh Coalson6b21f662006-09-13 01:42:27 +00001106FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
1107{
1108 FLAC__ASSERT(0 != object);
1109 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1110 FLAC__ASSERT(samples > 0);
1111 FLAC__ASSERT(total_samples > 0);
1112
1113 if(samples > 0 && total_samples > 0) {
1114 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1115 unsigned i, j;
1116 FLAC__uint64 num, sample;
1117
1118 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
1119 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
1120 if(total_samples % samples == 0)
1121 num--;
1122
Erik de Castro Lopoc431a6c2015-02-21 07:05:21 +11001123 /* Put a strict upper bound on the number of allowed seek points. */
Erik de Castro Lopo033af7b2015-02-18 17:55:52 +11001124 if (num > 32768) {
1125 /* Set the bound and recalculate samples accordingly. */
1126 num = 32786;
1127 samples = total_samples / num;
1128 }
1129
Josh Coalson6b21f662006-09-13 01:42:27 +00001130 i = seek_table->num_points;
1131
Josh Coalsona65fd932006-10-03 01:02:44 +00001132 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
Josh Coalson6b21f662006-09-13 01:42:27 +00001133 return false;
1134
1135 sample = 0;
1136 for(j = 0; j < num; i++, j++, sample += samples) {
1137 seek_table->points[i].sample_number = sample;
1138 seek_table->points[i].stream_offset = 0;
1139 seek_table->points[i].frame_samples = 0;
1140 }
1141 }
1142
1143 return true;
1144}
1145
Josh Coalson6afed9f2002-10-16 22:29:47 +00001146FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +00001147{
1148 unsigned unique;
1149
1150 FLAC__ASSERT(0 != object);
1151 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1152
1153 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1154
1155 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1156}
1157
Josh Coalson6afed9f2002-10-16 22:29:47 +00001158FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
Josh Coalson90ced912002-05-30 05:23:38 +00001159{
Josh Coalson2de11242004-12-30 03:41:19 +00001160 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1161 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001162 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001163}
1164
Josh Coalson6afed9f2002-10-16 22:29:47 +00001165FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +00001166{
1167 FLAC__ASSERT(0 != object);
1168 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1169
1170 if(0 == object->data.vorbis_comment.comments) {
1171 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1172 if(0 == new_num_comments)
1173 return true;
1174 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1175 return false;
1176 }
1177 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001178 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1179 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1180
1181 /* overflow check */
Erik de Castro Lopo587e1182012-02-17 17:52:12 +11001182 if(new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
Josh Coalson0f008d22007-09-11 04:49:56 +00001183 return false;
Josh Coalson90ced912002-05-30 05:23:38 +00001184
1185 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1186
1187 /* if shrinking, free the truncated entries */
1188 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1189 unsigned i;
1190 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1191 if(0 != object->data.vorbis_comment.comments[i].entry)
1192 free(object->data.vorbis_comment.comments[i].entry);
1193 }
1194
1195 if(new_size == 0) {
1196 free(object->data.vorbis_comment.comments);
1197 object->data.vorbis_comment.comments = 0;
1198 }
Erik de Castro Lopoff507792015-07-05 21:21:44 +10001199 else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size))) {
1200 object->data.vorbis_comment.num_comments = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001201 return false;
Erik de Castro Lopoff507792015-07-05 21:21:44 +10001202 }
Josh Coalson90ced912002-05-30 05:23:38 +00001203
1204 /* if growing, zero all the length/pointers of new elements */
1205 if(new_size > old_size)
1206 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1207 }
1208
1209 object->data.vorbis_comment.num_comments = new_num_comments;
1210
1211 vorbiscomment_calculate_length_(object);
1212 return true;
1213}
1214
Josh Coalson6afed9f2002-10-16 22:29:47 +00001215FLAC_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 +00001216{
Josh Coalsonfb993862003-01-15 03:18:07 +00001217 FLAC__ASSERT(0 != object);
1218 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1219
Josh Coalson2de11242004-12-30 03:41:19 +00001220 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1221 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001222 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001223}
1224
Josh Coalson6afed9f2002-10-16 22:29:47 +00001225FLAC_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 +00001226{
Josh Coalsoncc682512002-06-08 04:53:42 +00001227 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001228
1229 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001230 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001231 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001232
Josh Coalson2de11242004-12-30 03:41:19 +00001233 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1234 return false;
1235
Josh Coalson6b8e5302002-05-31 06:23:09 +00001236 vc = &object->data.vorbis_comment;
1237
1238 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001239 return false;
1240
1241 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001242 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 +00001243 vc->comments[comment_num].length = 0;
1244 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001245
1246 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1247}
1248
Josh Coalsondef597e2004-12-30 00:59:30 +00001249FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1250{
1251 FLAC__ASSERT(0 != object);
1252 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1253 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1254}
1255
1256FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1257{
1258 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
Josh Coalson2de11242004-12-30 03:41:19 +00001259
1260 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1261 return false;
1262
Josh Coalsondef597e2004-12-30 00:59:30 +00001263 {
1264 int i;
Josh Coalson9bedd782007-02-22 01:37:33 +00001265 size_t field_name_length;
Josh Coalsondef597e2004-12-30 00:59:30 +00001266 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1267
1268 FLAC__ASSERT(0 != eq);
1269
1270 if(0 == eq)
1271 return false; /* double protection */
1272
1273 field_name_length = eq-entry.entry;
1274
Josh Coalsone95399c2008-09-15 05:37:27 +00001275 i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
1276 if(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001277 unsigned indx = (unsigned)i;
1278 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, indx, entry, copy))
Josh Coalsondef597e2004-12-30 00:59:30 +00001279 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001280 entry = object->data.vorbis_comment.comments[indx];
1281 indx++; /* skip over replaced comment */
1282 if(all && indx < object->data.vorbis_comment.num_comments) {
1283 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsone95399c2008-09-15 05:37:27 +00001284 while(i >= 0) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001285 indx = (unsigned)i;
1286 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, indx))
Josh Coalsondef597e2004-12-30 00:59:30 +00001287 return false;
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +11001288 if(indx < object->data.vorbis_comment.num_comments)
1289 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001290 else
1291 i = -1;
1292 }
1293 }
1294 return true;
1295 }
1296 else
1297 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1298 }
1299}
1300
Josh Coalson6afed9f2002-10-16 22:29:47 +00001301FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001302{
Josh Coalsoncc682512002-06-08 04:53:42 +00001303 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001304
1305 FLAC__ASSERT(0 != object);
1306 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001307 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001308
Josh Coalson6b8e5302002-05-31 06:23:09 +00001309 vc = &object->data.vorbis_comment;
1310
Josh Coalson90ced912002-05-30 05:23:38 +00001311 /* free the comment at comment_num */
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +10001312 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++)
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +10001591 free(object->data.cue_sheet.tracks[i].indices);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001592 }
1593
1594 if(new_size == 0) {
1595 free(object->data.cue_sheet.tracks);
1596 object->data.cue_sheet.tracks = 0;
1597 }
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +10001598 else if(0 == (object->data.cue_sheet.tracks = realloc(object->data.cue_sheet.tracks, new_size)))
Josh Coalson8e9c4512002-11-14 05:00:24 +00001599 return false;
1600
1601 /* if growing, zero all the lengths/pointers of new elements */
1602 if(new_size > old_size)
1603 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1604 }
1605
1606 object->data.cue_sheet.num_tracks = new_num_tracks;
1607
1608 cuesheet_calculate_length_(object);
1609 return true;
1610}
1611
Josh Coalsondf7240a2002-11-16 06:32:30 +00001612FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
Josh Coalson8e9c4512002-11-14 05:00:24 +00001613{
Josh Coalsonfb993862003-01-15 03:18:07 +00001614 FLAC__ASSERT(0 != object);
1615 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1616
Josh Coalsondf7240a2002-11-16 06:32:30 +00001617 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001618}
1619
Josh Coalsondf7240a2002-11-16 06:32:30 +00001620FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
Josh Coalson8e9c4512002-11-14 05:00:24 +00001621{
1622 FLAC__StreamMetadata_CueSheet *cs;
1623
1624 FLAC__ASSERT(0 != object);
1625 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1626 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1627
1628 cs = &object->data.cue_sheet;
1629
1630 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1631 return false;
1632
1633 /* move all tracks >= track_num forward one space */
1634 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1635 cs->tracks[track_num].num_indices = 0;
1636 cs->tracks[track_num].indices = 0;
1637
1638 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1639}
1640
Josh Coalson3b026e82002-11-21 06:41:01 +00001641FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1642{
1643 FLAC__StreamMetadata_CueSheet_Track track;
1644 memset(&track, 0, sizeof(track));
1645 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1646}
1647
Josh Coalson8e9c4512002-11-14 05:00:24 +00001648FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1649{
1650 FLAC__StreamMetadata_CueSheet *cs;
1651
1652 FLAC__ASSERT(0 != object);
1653 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1654 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1655
1656 cs = &object->data.cue_sheet;
1657
1658 /* free the track at track_num */
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +10001659 free(cs->tracks[track_num].indices);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001660
1661 /* move all tracks > track_num backward one space */
1662 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1663 cs->tracks[cs->num_tracks-1].num_indices = 0;
1664 cs->tracks[cs->num_tracks-1].indices = 0;
1665
1666 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1667}
1668
1669FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1670{
1671 FLAC__ASSERT(0 != object);
1672 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1673
1674 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1675}
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001676
1677static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1678{
1679 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1680 return 0;
1681 else if (cs->tracks[track].indices[0].number == 1)
1682 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1683 else if (cs->tracks[track].num_indices < 2)
1684 return 0;
1685 else if (cs->tracks[track].indices[1].number == 1)
1686 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1687 else
1688 return 0;
1689}
1690
1691static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1692{
1693 FLAC__uint32 n = 0;
1694 while (x) {
1695 n += (x%10);
1696 x /= 10;
1697 }
1698 return n;
1699}
1700
Josh Coalson504dcaf2007-09-13 15:42:47 +00001701/*@@@@add to tests*/
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001702FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1703{
1704 const FLAC__StreamMetadata_CueSheet *cs;
1705
1706 FLAC__ASSERT(0 != object);
1707 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1708
1709 cs = &object->data.cue_sheet;
1710
1711 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1712 return 0;
1713
1714 {
1715 FLAC__uint32 i, length, sum = 0;
1716 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1717 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1718 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1719
1720 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1721 }
1722}
Josh Coalsone343ab22006-09-23 19:21:19 +00001723
1724FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1725{
1726 char *old;
1727 size_t old_length, new_length;
1728
1729 FLAC__ASSERT(0 != object);
1730 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1731 FLAC__ASSERT(0 != mime_type);
1732
1733 old = object->data.picture.mime_type;
1734 old_length = old? strlen(old) : 0;
1735 new_length = strlen(mime_type);
1736
1737 /* do the copy first so that if we fail we leave the object untouched */
1738 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001739 if(new_length >= SIZE_MAX) /* overflow check */
1740 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001741 if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1742 return false;
1743 }
1744 else {
1745 object->data.picture.mime_type = mime_type;
1746 }
1747
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +10001748 free(old);
Josh Coalsone343ab22006-09-23 19:21:19 +00001749
1750 object->length -= old_length;
1751 object->length += new_length;
1752 return true;
1753}
1754
1755FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1756{
1757 FLAC__byte *old;
1758 size_t old_length, new_length;
1759
1760 FLAC__ASSERT(0 != object);
1761 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1762 FLAC__ASSERT(0 != description);
1763
1764 old = object->data.picture.description;
1765 old_length = old? strlen((const char *)old) : 0;
1766 new_length = strlen((const char *)description);
1767
1768 /* do the copy first so that if we fail we leave the object untouched */
1769 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001770 if(new_length >= SIZE_MAX) /* overflow check */
1771 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001772 if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
1773 return false;
1774 }
1775 else {
1776 object->data.picture.description = description;
1777 }
1778
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +10001779 free(old);
Josh Coalsone343ab22006-09-23 19:21:19 +00001780
1781 object->length -= old_length;
1782 object->length += new_length;
1783 return true;
1784}
1785
1786FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1787{
1788 FLAC__byte *old;
1789
1790 FLAC__ASSERT(0 != object);
1791 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1792 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
1793
1794 old = object->data.picture.data;
1795
1796 /* do the copy first so that if we fail we leave the object untouched */
1797 if(copy) {
1798 if(!copy_bytes_(&object->data.picture.data, data, length))
1799 return false;
1800 }
1801 else {
1802 object->data.picture.data = data;
1803 }
1804
Erik de Castro Lopob105f9a2015-07-04 12:19:29 +10001805 free(old);
Josh Coalsone343ab22006-09-23 19:21:19 +00001806
1807 object->length -= object->data.picture.data_length;
1808 object->data.picture.data_length = length;
1809 object->length += length;
1810 return true;
1811}
1812
1813FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1814{
1815 FLAC__ASSERT(0 != object);
1816 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1817
1818 return FLAC__format_picture_is_legal(&object->data.picture, violation);
1819}