blob: 9a6b5d2d75e85931cb80bab7c560510de339e3a7 [file] [log] [blame]
Josh Coalson90ced912002-05-30 05:23:38 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalsone74bd952007-02-02 06:58:19 +00002 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson
Josh Coalson90ced912002-05-30 05:23:38 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalson90ced912002-05-30 05:23:38 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalson90ced912002-05-30 05:23:38 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson90ced912002-05-30 05:23:38 +000030 */
31
Josh Coalsonb1ec7962006-05-24 04:41:36 +000032#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
Josh Coalson90ced912002-05-30 05:23:38 +000036#include <stdlib.h>
37#include <string.h>
38
39#include "private/metadata.h"
40
41#include "FLAC/assert.h"
Josh Coalson0f008d22007-09-11 04:49:56 +000042#include "share/alloc.h"
Josh Coalson90ced912002-05-30 05:23:38 +000043
44
45/****************************************************************************
46 *
47 * Local routines
48 *
49 ***************************************************************************/
50
Josh Coalsone343ab22006-09-23 19:21:19 +000051/* copy bytes:
52 * from = NULL && bytes = 0
53 * to <- NULL
54 * from != NULL && bytes > 0
55 * to <- copy of from
56 * else ASSERT
Josh Coalson0f008d22007-09-11 04:49:56 +000057 * malloc error leaves 'to' unchanged
Josh Coalsone343ab22006-09-23 19:21:19 +000058 */
Josh Coalson90ced912002-05-30 05:23:38 +000059static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
60{
Josh Coalsone343ab22006-09-23 19:21:19 +000061 FLAC__ASSERT(0 != to);
Josh Coalson90ced912002-05-30 05:23:38 +000062 if(bytes > 0 && 0 != from) {
63 FLAC__byte *x;
Josh Coalson0f008d22007-09-11 04:49:56 +000064 if(0 == (x = (FLAC__byte*)safe_malloc_(bytes)))
Josh Coalson90ced912002-05-30 05:23:38 +000065 return false;
66 memcpy(x, from, bytes);
67 *to = x;
68 }
69 else {
70 FLAC__ASSERT(0 == from);
71 FLAC__ASSERT(bytes == 0);
72 *to = 0;
73 }
74 return true;
75}
76
Josh Coalsone343ab22006-09-23 19:21:19 +000077#if 0 /* UNUSED */
78/* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */
79static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
80{
81 FLAC__byte *copy;
82 FLAC__ASSERT(0 != to);
83 if(copy_bytes_(&copy, from, bytes)) {
84 if(*to)
85 free(*to);
86 *to = copy;
87 return true;
88 }
89 else
90 return false;
91}
92#endif
93
94/* reallocate entry to 1 byte larger and add a terminating NUL */
95/* realloc() failure leaves entry unchanged */
Josh Coalsondef597e2004-12-30 00:59:30 +000096static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
97{
Josh Coalson0f008d22007-09-11 04:49:56 +000098 FLAC__byte *x = (FLAC__byte*)safe_realloc_add_2op_(*entry, length, /*+*/1);
Josh Coalsondef597e2004-12-30 00:59:30 +000099 if(0 != x) {
100 x[length] = '\0';
101 *entry = x;
102 return true;
103 }
104 else
105 return false;
106}
107
Josh Coalsone343ab22006-09-23 19:21:19 +0000108/* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
109 * unchanged if malloc fails, free()ing the original '*to' if it
110 * succeeds and the original '*to' was not NULL
111 */
112static FLAC__bool copy_cstring_(char **to, const char *from)
113{
Josh Coalsone343ab22006-09-23 19:21:19 +0000114 char *copy = strdup(from);
Josh Coalsona65fd932006-10-03 01:02:44 +0000115 FLAC__ASSERT(to);
Josh Coalsone343ab22006-09-23 19:21:19 +0000116 if(copy) {
117 if(*to)
118 free(*to);
119 *to = copy;
120 return true;
121 }
122 else
123 return false;
124}
125
Josh Coalsoncc682512002-06-08 04:53:42 +0000126static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
Josh Coalson90ced912002-05-30 05:23:38 +0000127{
128 to->length = from->length;
129 if(0 == from->entry) {
130 FLAC__ASSERT(from->length == 0);
131 to->entry = 0;
132 }
133 else {
134 FLAC__byte *x;
135 FLAC__ASSERT(from->length > 0);
Josh Coalson0f008d22007-09-11 04:49:56 +0000136 if(0 == (x = (FLAC__byte*)safe_malloc_add_2op_(from->length, /*+*/1)))
Josh Coalson90ced912002-05-30 05:23:38 +0000137 return false;
138 memcpy(x, from->entry, from->length);
Josh Coalsondef597e2004-12-30 00:59:30 +0000139 x[from->length] = '\0';
Josh Coalson90ced912002-05-30 05:23:38 +0000140 to->entry = x;
141 }
142 return true;
143}
144
Josh Coalson8e9c4512002-11-14 05:00:24 +0000145static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
146{
147 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
148 if(0 == from->indices) {
149 FLAC__ASSERT(from->num_indices == 0);
150 }
151 else {
152 FLAC__StreamMetadata_CueSheet_Index *x;
153 FLAC__ASSERT(from->num_indices > 0);
Josh Coalson0f008d22007-09-11 04:49:56 +0000154 if(0 == (x = (FLAC__StreamMetadata_CueSheet_Index*)safe_malloc_mul_2op_(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))))
Josh Coalson8e9c4512002-11-14 05:00:24 +0000155 return false;
156 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
157 to->indices = x;
158 }
159 return true;
160}
161
Josh Coalsoncc682512002-06-08 04:53:42 +0000162static void seektable_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000163{
164 FLAC__ASSERT(0 != object);
165 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
166
167 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
168}
169
Josh Coalsoncc682512002-06-08 04:53:42 +0000170static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000171{
Josh Coalsoncc682512002-06-08 04:53:42 +0000172 FLAC__StreamMetadata_SeekPoint *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000173
174 FLAC__ASSERT(num_points > 0);
175
Josh Coalson0f008d22007-09-11 04:49:56 +0000176 object_array = (FLAC__StreamMetadata_SeekPoint*)safe_malloc_mul_2op_(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
Josh Coalson90ced912002-05-30 05:23:38 +0000177
178 if(0 != object_array) {
179 unsigned i;
180 for(i = 0; i < num_points; i++) {
181 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
182 object_array[i].stream_offset = 0;
183 object_array[i].frame_samples = 0;
184 }
185 }
186
187 return object_array;
188}
189
Josh Coalsoncc682512002-06-08 04:53:42 +0000190static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000191{
192 unsigned i;
193
194 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
195
196 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
197 object->length += object->data.vorbis_comment.vendor_string.length;
198 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
199 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
200 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
201 object->length += object->data.vorbis_comment.comments[i].length;
202 }
203}
204
Josh Coalsoncc682512002-06-08 04:53:42 +0000205static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000206{
Josh Coalson90ced912002-05-30 05:23:38 +0000207 FLAC__ASSERT(num_comments > 0);
208
Josh Coalson0f008d22007-09-11 04:49:56 +0000209 return (FLAC__StreamMetadata_VorbisComment_Entry*)safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000210}
211
Josh Coalsoncc682512002-06-08 04:53:42 +0000212static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000213{
214 unsigned i;
215
216 FLAC__ASSERT(0 != object_array && num_comments > 0);
217
218 for(i = 0; i < num_comments; i++)
219 if(0 != object_array[i].entry)
220 free(object_array[i].entry);
221
222 if(0 != object_array)
223 free(object_array);
224}
225
Josh Coalsoncc682512002-06-08 04:53:42 +0000226static 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 +0000227{
Josh Coalsoncc682512002-06-08 04:53:42 +0000228 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000229
230 FLAC__ASSERT(0 != object_array);
231 FLAC__ASSERT(num_comments > 0);
232
233 return_array = vorbiscomment_entry_array_new_(num_comments);
234
235 if(0 != return_array) {
236 unsigned i;
237
Josh Coalson90ced912002-05-30 05:23:38 +0000238 for(i = 0; i < num_comments; i++) {
239 if(!copy_vcentry_(return_array+i, object_array+i)) {
240 vorbiscomment_entry_array_delete_(return_array, num_comments);
241 return 0;
242 }
243 }
244 }
245
246 return return_array;
247}
248
Josh Coalsoncc682512002-06-08 04:53:42 +0000249static 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 +0000250{
251 FLAC__byte *save;
252
253 FLAC__ASSERT(0 != object);
254 FLAC__ASSERT(0 != dest);
255 FLAC__ASSERT(0 != src);
256 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson748459c2004-09-08 00:49:30 +0000257 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
Josh Coalson90ced912002-05-30 05:23:38 +0000258
Josh Coalson6b8e5302002-05-31 06:23:09 +0000259 save = dest->entry;
Josh Coalson90ced912002-05-30 05:23:38 +0000260
Josh Coalsondef597e2004-12-30 00:59:30 +0000261 if(0 != src->entry && src->length > 0) {
262 if(copy) {
263 /* do the copy first so that if we fail we leave the dest object untouched */
264 if(!copy_vcentry_(dest, src))
265 return false;
266 }
267 else {
268 /* we have to make sure that the string we're taking over is null-terminated */
269
270 /*
271 * Stripping the const from src->entry is OK since we're taking
272 * ownership of the pointer. This is a hack around a deficiency
273 * in the API where the same function is used for 'copy' and
274 * 'own', but the source entry is a const pointer. If we were
275 * precise, the 'own' flavor would be a separate function with a
276 * non-const source pointer. But it's not, so we hack away.
277 */
278 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
279 return false;
280 *dest = *src;
281 }
Josh Coalson90ced912002-05-30 05:23:38 +0000282 }
283 else {
Josh Coalsondef597e2004-12-30 00:59:30 +0000284 /* the src is null */
Josh Coalson90ced912002-05-30 05:23:38 +0000285 *dest = *src;
286 }
287
288 if(0 != save)
289 free(save);
290
291 vorbiscomment_calculate_length_(object);
292 return true;
293}
294
Josh Coalsondef597e2004-12-30 00:59:30 +0000295static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
296{
297 unsigned i;
298
299 FLAC__ASSERT(0 != object);
300 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
301 FLAC__ASSERT(0 != field_name);
302
303 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
304 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
305 return (int)i;
306 }
307
308 return -1;
309}
310
Josh Coalson8e9c4512002-11-14 05:00:24 +0000311static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
312{
313 unsigned i;
314
315 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
316
317 object->length = (
318 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
319 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000320 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
Josh Coalsondf7240a2002-11-16 06:32:30 +0000321 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
Josh Coalson8e9c4512002-11-14 05:00:24 +0000322 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
323 ) / 8;
324
325 object->length += object->data.cue_sheet.num_tracks * (
326 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
327 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
328 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
329 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
330 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
331 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
332 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
333 ) / 8;
334
335 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
336 object->length += object->data.cue_sheet.tracks[i].num_indices * (
337 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
338 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
339 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
340 ) / 8;
341 }
342}
343
344static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
345{
346 FLAC__ASSERT(num_indices > 0);
347
Josh Coalson0f008d22007-09-11 04:49:56 +0000348 return (FLAC__StreamMetadata_CueSheet_Index*)safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000349}
350
351static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
352{
353 FLAC__ASSERT(num_tracks > 0);
354
Josh Coalson0f008d22007-09-11 04:49:56 +0000355 return (FLAC__StreamMetadata_CueSheet_Track*)safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000356}
357
358static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
359{
360 unsigned i;
361
362 FLAC__ASSERT(0 != object_array && num_tracks > 0);
363
364 for(i = 0; i < num_tracks; i++) {
365 if(0 != object_array[i].indices) {
366 FLAC__ASSERT(object_array[i].num_indices > 0);
367 free(object_array[i].indices);
368 }
369 }
370
371 if(0 != object_array)
372 free(object_array);
373}
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
419 if(0 != save)
420 free(save);
421
422 cuesheet_calculate_length_(object);
423 return true;
424}
425
Josh Coalson90ced912002-05-30 05:23:38 +0000426
427/****************************************************************************
428 *
429 * Metadata object routines
430 *
431 ***************************************************************************/
432
Josh Coalson6afed9f2002-10-16 22:29:47 +0000433FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
Josh Coalson90ced912002-05-30 05:23:38 +0000434{
Josh Coalson1cb23412004-07-22 01:32:00 +0000435 FLAC__StreamMetadata *object;
436
437 if(type > FLAC__MAX_METADATA_TYPE_CODE)
438 return 0;
439
440 object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000441 if(0 != object) {
Josh Coalson90ced912002-05-30 05:23:38 +0000442 object->is_last = false;
443 object->type = type;
444 switch(type) {
445 case FLAC__METADATA_TYPE_STREAMINFO:
446 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
447 break;
448 case FLAC__METADATA_TYPE_PADDING:
Josh Coalsond0609472003-01-10 05:37:13 +0000449 /* calloc() took care of this for us:
450 object->length = 0;
451 */
Josh Coalson90ced912002-05-30 05:23:38 +0000452 break;
453 case FLAC__METADATA_TYPE_APPLICATION:
454 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
Josh Coalsond0609472003-01-10 05:37:13 +0000455 /* calloc() took care of this for us:
456 object->data.application.data = 0;
457 */
Josh Coalson90ced912002-05-30 05:23:38 +0000458 break;
459 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsond0609472003-01-10 05:37:13 +0000460 /* calloc() took care of this for us:
461 object->length = 0;
462 object->data.seek_table.num_points = 0;
463 object->data.seek_table.points = 0;
464 */
Josh Coalson90ced912002-05-30 05:23:38 +0000465 break;
466 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalsone343ab22006-09-23 19:21:19 +0000467 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
468 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
469 free(object);
470 return 0;
Josh Coalson45bb9882002-10-26 04:34:16 +0000471 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000472 vorbiscomment_calculate_length_(object);
Josh Coalson90ced912002-05-30 05:23:38 +0000473 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000474 case FLAC__METADATA_TYPE_CUESHEET:
475 cuesheet_calculate_length_(object);
476 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000477 case FLAC__METADATA_TYPE_PICTURE:
478 object->length = (
479 FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
480 FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
481 FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
482 FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
483 FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
484 FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
Josh Coalson74ed2942006-09-23 23:15:05 +0000485 FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
Josh Coalsone343ab22006-09-23 19:21:19 +0000486 FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
487 0 /* no data */
488 ) / 8;
489 object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
490 object->data.picture.mime_type = 0;
491 object->data.picture.description = 0;
492 /* calloc() took care of this for us:
493 object->data.picture.width = 0;
494 object->data.picture.height = 0;
495 object->data.picture.depth = 0;
Josh Coalson74ed2942006-09-23 23:15:05 +0000496 object->data.picture.colors = 0;
Josh Coalsone343ab22006-09-23 19:21:19 +0000497 object->data.picture.data_length = 0;
498 object->data.picture.data = 0;
499 */
500 /* now initialize mime_type and description with empty strings to make things easier on the client */
501 if(!copy_cstring_(&object->data.picture.mime_type, "")) {
502 free(object);
503 return 0;
504 }
505 if(!copy_cstring_((char**)(&object->data.picture.description), "")) {
506 if(object->data.picture.mime_type)
507 free(object->data.picture.mime_type);
508 free(object);
509 return 0;
510 }
511 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000512 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000513 /* calloc() took care of this for us:
514 object->length = 0;
515 object->data.unknown.data = 0;
516 */
517 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000518 }
519 }
520
521 return object;
522}
523
Josh Coalson6afed9f2002-10-16 22:29:47 +0000524FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000525{
Josh Coalsoncc682512002-06-08 04:53:42 +0000526 FLAC__StreamMetadata *to;
Josh Coalson90ced912002-05-30 05:23:38 +0000527
528 FLAC__ASSERT(0 != object);
529
530 if(0 != (to = FLAC__metadata_object_new(object->type))) {
531 to->is_last = object->is_last;
532 to->type = object->type;
533 to->length = object->length;
534 switch(to->type) {
535 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalsoncc682512002-06-08 04:53:42 +0000536 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
Josh Coalson90ced912002-05-30 05:23:38 +0000537 break;
538 case FLAC__METADATA_TYPE_PADDING:
539 break;
540 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson0f008d22007-09-11 04:49:56 +0000541 if(to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
542 FLAC__metadata_object_delete(to);
543 return 0;
544 }
Josh Coalson90ced912002-05-30 05:23:38 +0000545 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
546 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
547 FLAC__metadata_object_delete(to);
548 return 0;
549 }
550 break;
551 case FLAC__METADATA_TYPE_SEEKTABLE:
552 to->data.seek_table.num_points = object->data.seek_table.num_points;
Josh Coalson0f008d22007-09-11 04:49:56 +0000553 if(to->data.seek_table.num_points > SIZE_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
554 FLAC__metadata_object_delete(to);
555 return 0;
556 }
Josh Coalsoncc682512002-06-08 04:53:42 +0000557 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 +0000558 FLAC__metadata_object_delete(to);
559 return 0;
560 }
561 break;
562 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000563 if(0 != to->data.vorbis_comment.vendor_string.entry) {
Josh Coalson45bb9882002-10-26 04:34:16 +0000564 free(to->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000565 to->data.vorbis_comment.vendor_string.entry = 0;
566 }
Josh Coalson90ced912002-05-30 05:23:38 +0000567 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
568 FLAC__metadata_object_delete(to);
569 return 0;
570 }
571 if(object->data.vorbis_comment.num_comments == 0) {
572 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
573 to->data.vorbis_comment.comments = 0;
574 }
575 else {
576 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
577 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
578 if(0 == to->data.vorbis_comment.comments) {
579 FLAC__metadata_object_delete(to);
580 return 0;
581 }
582 }
583 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
584 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000585 case FLAC__METADATA_TYPE_CUESHEET:
586 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
587 if(object->data.cue_sheet.num_tracks == 0) {
588 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
589 }
590 else {
591 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
592 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
593 if(0 == to->data.cue_sheet.tracks) {
594 FLAC__metadata_object_delete(to);
595 return 0;
596 }
597 }
598 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000599 case FLAC__METADATA_TYPE_PICTURE:
600 to->data.picture.type = object->data.picture.type;
601 if(!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
602 FLAC__metadata_object_delete(to);
603 return 0;
604 }
605 if(!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
606 FLAC__metadata_object_delete(to);
607 return 0;
608 }
609 to->data.picture.width = object->data.picture.width;
610 to->data.picture.height = object->data.picture.height;
611 to->data.picture.depth = object->data.picture.depth;
Josh Coalson74ed2942006-09-23 23:15:05 +0000612 to->data.picture.colors = object->data.picture.colors;
Josh Coalsone343ab22006-09-23 19:21:19 +0000613 to->data.picture.data_length = object->data.picture.data_length;
614 if(!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
615 FLAC__metadata_object_delete(to);
616 return 0;
617 }
618 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000619 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000620 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
621 FLAC__metadata_object_delete(to);
622 return 0;
623 }
624 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000625 }
626 }
627
628 return to;
629}
630
Josh Coalsoncc682512002-06-08 04:53:42 +0000631void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000632{
633 FLAC__ASSERT(0 != object);
634
635 switch(object->type) {
636 case FLAC__METADATA_TYPE_STREAMINFO:
637 case FLAC__METADATA_TYPE_PADDING:
638 break;
639 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson4fa90592002-12-04 07:01:37 +0000640 if(0 != object->data.application.data) {
Josh Coalson90ced912002-05-30 05:23:38 +0000641 free(object->data.application.data);
Josh Coalson4fa90592002-12-04 07:01:37 +0000642 object->data.application.data = 0;
643 }
Josh Coalson90ced912002-05-30 05:23:38 +0000644 break;
645 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalson4fa90592002-12-04 07:01:37 +0000646 if(0 != object->data.seek_table.points) {
Josh Coalson90ced912002-05-30 05:23:38 +0000647 free(object->data.seek_table.points);
Josh Coalson4fa90592002-12-04 07:01:37 +0000648 object->data.seek_table.points = 0;
649 }
Josh Coalson90ced912002-05-30 05:23:38 +0000650 break;
651 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000652 if(0 != object->data.vorbis_comment.vendor_string.entry) {
Josh Coalson90ced912002-05-30 05:23:38 +0000653 free(object->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000654 object->data.vorbis_comment.vendor_string.entry = 0;
655 }
Josh Coalson90ced912002-05-30 05:23:38 +0000656 if(0 != object->data.vorbis_comment.comments) {
657 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
658 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
659 }
660 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000661 case FLAC__METADATA_TYPE_CUESHEET:
662 if(0 != object->data.cue_sheet.tracks) {
663 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
664 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
665 }
666 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000667 case FLAC__METADATA_TYPE_PICTURE:
668 if(0 != object->data.picture.mime_type) {
669 free(object->data.picture.mime_type);
670 object->data.picture.mime_type = 0;
671 }
672 if(0 != object->data.picture.description) {
673 free(object->data.picture.description);
674 object->data.picture.description = 0;
675 }
676 if(0 != object->data.picture.data) {
677 free(object->data.picture.data);
678 object->data.picture.data = 0;
679 }
680 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000681 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000682 if(0 != object->data.unknown.data) {
683 free(object->data.unknown.data);
684 object->data.unknown.data = 0;
685 }
686 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000687 }
688}
689
Josh Coalson6afed9f2002-10-16 22:29:47 +0000690FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000691{
692 FLAC__metadata_object_delete_data(object);
693 free(object);
694}
695
Josh Coalsoncc682512002-06-08 04:53:42 +0000696static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000697{
698 if(block1->min_blocksize != block2->min_blocksize)
699 return false;
700 if(block1->max_blocksize != block2->max_blocksize)
701 return false;
702 if(block1->min_framesize != block2->min_framesize)
703 return false;
704 if(block1->max_framesize != block2->max_framesize)
705 return false;
706 if(block1->sample_rate != block2->sample_rate)
707 return false;
708 if(block1->channels != block2->channels)
709 return false;
710 if(block1->bits_per_sample != block2->bits_per_sample)
711 return false;
712 if(block1->total_samples != block2->total_samples)
713 return false;
714 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
715 return false;
716 return true;
717}
718
Josh Coalsoncc682512002-06-08 04:53:42 +0000719static 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 +0000720{
721 FLAC__ASSERT(0 != block1);
722 FLAC__ASSERT(0 != block2);
723 FLAC__ASSERT(block_length >= sizeof(block1->id));
724
725 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
726 return false;
727 if(0 != block1->data && 0 != block2->data)
728 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
729 else
730 return block1->data == block2->data;
731}
732
Josh Coalsoncc682512002-06-08 04:53:42 +0000733static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000734{
735 unsigned i;
736
737 FLAC__ASSERT(0 != block1);
738 FLAC__ASSERT(0 != block2);
739
740 if(block1->num_points != block2->num_points)
741 return false;
742
743 if(0 != block1->points && 0 != block2->points) {
744 for(i = 0; i < block1->num_points; i++) {
745 if(block1->points[i].sample_number != block2->points[i].sample_number)
746 return false;
747 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
748 return false;
749 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
750 return false;
751 }
752 return true;
753 }
754 else
755 return block1->points == block2->points;
756}
757
Josh Coalsoncc682512002-06-08 04:53:42 +0000758static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000759{
760 unsigned i;
761
762 if(block1->vendor_string.length != block2->vendor_string.length)
763 return false;
764
765 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
766 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
767 return false;
768 }
769 else if(block1->vendor_string.entry != block2->vendor_string.entry)
770 return false;
771
772 if(block1->num_comments != block2->num_comments)
773 return false;
774
775 for(i = 0; i < block1->num_comments; i++) {
776 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
777 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
778 return false;
779 }
780 else if(block1->comments[i].entry != block2->comments[i].entry)
781 return false;
782 }
783 return true;
784}
785
Josh Coalson8e9c4512002-11-14 05:00:24 +0000786static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
787{
788 unsigned i, j;
789
790 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
791 return false;
792
793 if(block1->lead_in != block2->lead_in)
794 return false;
795
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000796 if(block1->is_cd != block2->is_cd)
797 return false;
798
Josh Coalson8e9c4512002-11-14 05:00:24 +0000799 if(block1->num_tracks != block2->num_tracks)
800 return false;
801
802 if(0 != block1->tracks && 0 != block2->tracks) {
803 FLAC__ASSERT(block1->num_tracks > 0);
804 for(i = 0; i < block1->num_tracks; i++) {
805 if(block1->tracks[i].offset != block2->tracks[i].offset)
806 return false;
807 if(block1->tracks[i].number != block2->tracks[i].number)
808 return false;
809 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
810 return false;
811 if(block1->tracks[i].type != block2->tracks[i].type)
812 return false;
813 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
814 return false;
815 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
816 return false;
817 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
818 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
819 for(j = 0; j < block1->tracks[i].num_indices; j++) {
820 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
821 return false;
822 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
823 return false;
824 }
825 }
826 else if(block1->tracks[i].indices != block2->tracks[i].indices)
827 return false;
828 }
829 }
830 else if(block1->tracks != block2->tracks)
831 return false;
832 return true;
833}
834
Josh Coalsone343ab22006-09-23 19:21:19 +0000835static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
836{
837 if(block1->type != block2->type)
838 return false;
839 if(block1->mime_type != block2->mime_type && (0 == block1->mime_type || 0 == block2->mime_type || strcmp(block1->mime_type, block2->mime_type)))
840 return false;
841 if(block1->description != block2->description && (0 == block1->description || 0 == block2->description || strcmp((const char *)block1->description, (const char *)block2->description)))
842 return false;
843 if(block1->width != block2->width)
844 return false;
845 if(block1->height != block2->height)
846 return false;
847 if(block1->depth != block2->depth)
848 return false;
Josh Coalson74ed2942006-09-23 23:15:05 +0000849 if(block1->colors != block2->colors)
850 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +0000851 if(block1->data_length != block2->data_length)
852 return false;
853 if(block1->data != block2->data && (0 == block1->data || 0 == block2->data || memcmp(block1->data, block2->data, block1->data_length)))
854 return false;
855 return true;
856}
857
Josh Coalsond0609472003-01-10 05:37:13 +0000858static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
859{
860 FLAC__ASSERT(0 != block1);
861 FLAC__ASSERT(0 != block2);
862
863 if(0 != block1->data && 0 != block2->data)
864 return 0 == memcmp(block1->data, block2->data, block_length);
865 else
866 return block1->data == block2->data;
867}
868
Josh Coalson6afed9f2002-10-16 22:29:47 +0000869FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000870{
Josh Coalsond8ab3462002-07-11 05:54:05 +0000871 FLAC__ASSERT(0 != block1);
872 FLAC__ASSERT(0 != block2);
873
Josh Coalson57ba6f42002-06-07 05:27:37 +0000874 if(block1->type != block2->type) {
875 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000876 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000877 if(block1->is_last != block2->is_last) {
878 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000879 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000880 if(block1->length != block2->length) {
881 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000882 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000883 switch(block1->type) {
884 case FLAC__METADATA_TYPE_STREAMINFO:
885 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
886 case FLAC__METADATA_TYPE_PADDING:
887 return true; /* we don't compare the padding guts */
888 case FLAC__METADATA_TYPE_APPLICATION:
889 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
890 case FLAC__METADATA_TYPE_SEEKTABLE:
891 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
892 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
893 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000894 case FLAC__METADATA_TYPE_CUESHEET:
895 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
Josh Coalsone343ab22006-09-23 19:21:19 +0000896 case FLAC__METADATA_TYPE_PICTURE:
897 return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000898 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000899 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000900 }
901}
902
Josh Coalson6afed9f2002-10-16 22:29:47 +0000903FLAC_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 +0000904{
905 FLAC__byte *save;
906
Josh Coalsond8ab3462002-07-11 05:54:05 +0000907 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000908 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
909 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
910
911 save = object->data.application.data;
912
913 /* do the copy first so that if we fail we leave the object untouched */
914 if(copy) {
915 if(!copy_bytes_(&object->data.application.data, data, length))
916 return false;
917 }
918 else {
919 object->data.application.data = data;
920 }
921
922 if(0 != save)
923 free(save);
924
925 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
926 return true;
927}
928
Josh Coalson6afed9f2002-10-16 22:29:47 +0000929FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000930{
931 FLAC__ASSERT(0 != object);
932 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
933
934 if(0 == object->data.seek_table.points) {
935 FLAC__ASSERT(object->data.seek_table.num_points == 0);
936 if(0 == new_num_points)
937 return true;
938 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
939 return false;
940 }
941 else {
Josh Coalson0f008d22007-09-11 04:49:56 +0000942 const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
943 const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
944
945 /* overflow check */
946 if((size_t)new_num_points > SIZE_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
947 return false;
Josh Coalson90ced912002-05-30 05:23:38 +0000948
949 FLAC__ASSERT(object->data.seek_table.num_points > 0);
950
951 if(new_size == 0) {
952 free(object->data.seek_table.points);
953 object->data.seek_table.points = 0;
954 }
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000955 else if(0 == (object->data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(object->data.seek_table.points, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +0000956 return false;
957
958 /* if growing, set new elements to placeholders */
959 if(new_size > old_size) {
960 unsigned i;
961 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
962 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
963 object->data.seek_table.points[i].stream_offset = 0;
964 object->data.seek_table.points[i].frame_samples = 0;
965 }
966 }
967 }
968
969 object->data.seek_table.num_points = new_num_points;
970
971 seektable_calculate_length_(object);
972 return true;
973}
974
Josh Coalson6afed9f2002-10-16 22:29:47 +0000975FLAC_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 +0000976{
977 FLAC__ASSERT(0 != object);
978 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000979 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000980
981 object->data.seek_table.points[point_num] = point;
982}
983
Josh Coalson6afed9f2002-10-16 22:29:47 +0000984FLAC_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 +0000985{
986 int i;
987
988 FLAC__ASSERT(0 != object);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000989 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000990 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000991
992 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
993 return false;
994
995 /* move all points >= point_num forward one space */
996 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
997 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
998
999 FLAC__metadata_object_seektable_set_point(object, point_num, point);
1000 seektable_calculate_length_(object);
1001 return true;
1002}
1003
Josh Coalson6afed9f2002-10-16 22:29:47 +00001004FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001005{
1006 unsigned i;
1007
1008 FLAC__ASSERT(0 != object);
1009 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001010 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +00001011
1012 /* move all points > point_num backward one space */
1013 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
1014 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
1015
1016 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
1017}
1018
Josh Coalson6afed9f2002-10-16 22:29:47 +00001019FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
Josh Coalson28e08d82002-06-05 05:56:41 +00001020{
Josh Coalson28e08d82002-06-05 05:56:41 +00001021 FLAC__ASSERT(0 != object);
1022 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1023
Josh Coalson0833f342002-07-15 05:31:55 +00001024 return FLAC__format_seektable_is_legal(&object->data.seek_table);
Josh Coalson28e08d82002-06-05 05:56:41 +00001025}
1026
Josh Coalson6afed9f2002-10-16 22:29:47 +00001027FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
Josh Coalson5a5de732002-08-01 06:37:11 +00001028{
1029 FLAC__ASSERT(0 != object);
1030 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1031
1032 if(num > 0)
1033 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
1034 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
1035 else
1036 return true;
1037}
1038
Josh Coalson6afed9f2002-10-16 22:29:47 +00001039FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
Josh Coalson5a5de732002-08-01 06:37:11 +00001040{
1041 FLAC__StreamMetadata_SeekTable *seek_table;
1042
1043 FLAC__ASSERT(0 != object);
1044 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1045
1046 seek_table = &object->data.seek_table;
1047
1048 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
1049 return false;
1050
1051 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
1052 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
1053 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
1054
1055 return true;
1056}
1057
Josh Coalson6afed9f2002-10-16 22:29:47 +00001058FLAC_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 +00001059{
1060 FLAC__ASSERT(0 != object);
1061 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1062 FLAC__ASSERT(0 != sample_numbers || num == 0);
1063
1064 if(num > 0) {
1065 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1066 unsigned i, j;
1067
1068 i = seek_table->num_points;
1069
1070 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1071 return false;
1072
1073 for(j = 0; j < num; i++, j++) {
1074 seek_table->points[i].sample_number = sample_numbers[j];
1075 seek_table->points[i].stream_offset = 0;
1076 seek_table->points[i].frame_samples = 0;
1077 }
1078 }
1079
1080 return true;
1081}
1082
Josh Coalson6afed9f2002-10-16 22:29:47 +00001083FLAC_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 +00001084{
1085 FLAC__ASSERT(0 != object);
1086 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1087 FLAC__ASSERT(total_samples > 0);
1088
Josh Coalson6b21f662006-09-13 01:42:27 +00001089 if(num > 0 && total_samples > 0) {
Josh Coalson5a5de732002-08-01 06:37:11 +00001090 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1091 unsigned i, j;
1092
1093 i = seek_table->num_points;
1094
1095 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1096 return false;
1097
1098 for(j = 0; j < num; i++, j++) {
1099 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
1100 seek_table->points[i].stream_offset = 0;
1101 seek_table->points[i].frame_samples = 0;
1102 }
1103 }
1104
1105 return true;
1106}
1107
Josh Coalson6b21f662006-09-13 01:42:27 +00001108FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
1109{
1110 FLAC__ASSERT(0 != object);
1111 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1112 FLAC__ASSERT(samples > 0);
1113 FLAC__ASSERT(total_samples > 0);
1114
1115 if(samples > 0 && total_samples > 0) {
1116 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1117 unsigned i, j;
1118 FLAC__uint64 num, sample;
1119
1120 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
1121 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
1122 if(total_samples % samples == 0)
1123 num--;
1124
1125 i = seek_table->num_points;
1126
Josh Coalsona65fd932006-10-03 01:02:44 +00001127 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (unsigned)num))
Josh Coalson6b21f662006-09-13 01:42:27 +00001128 return false;
1129
1130 sample = 0;
1131 for(j = 0; j < num; i++, j++, sample += samples) {
1132 seek_table->points[i].sample_number = sample;
1133 seek_table->points[i].stream_offset = 0;
1134 seek_table->points[i].frame_samples = 0;
1135 }
1136 }
1137
1138 return true;
1139}
1140
Josh Coalson6afed9f2002-10-16 22:29:47 +00001141FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +00001142{
1143 unsigned unique;
1144
1145 FLAC__ASSERT(0 != object);
1146 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1147
1148 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1149
1150 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1151}
1152
Josh Coalson6afed9f2002-10-16 22:29:47 +00001153FLAC_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 +00001154{
Josh Coalson2de11242004-12-30 03:41:19 +00001155 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1156 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001157 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001158}
1159
Josh Coalson6afed9f2002-10-16 22:29:47 +00001160FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +00001161{
1162 FLAC__ASSERT(0 != object);
1163 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1164
1165 if(0 == object->data.vorbis_comment.comments) {
1166 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1167 if(0 == new_num_comments)
1168 return true;
1169 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1170 return false;
1171 }
1172 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001173 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1174 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1175
1176 /* overflow check */
1177 if((size_t)new_num_comments > SIZE_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
1178 return false;
Josh Coalson90ced912002-05-30 05:23:38 +00001179
1180 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1181
1182 /* if shrinking, free the truncated entries */
1183 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1184 unsigned i;
1185 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1186 if(0 != object->data.vorbis_comment.comments[i].entry)
1187 free(object->data.vorbis_comment.comments[i].entry);
1188 }
1189
1190 if(new_size == 0) {
1191 free(object->data.vorbis_comment.comments);
1192 object->data.vorbis_comment.comments = 0;
1193 }
Josh Coalson5e3fcb22002-11-06 07:11:26 +00001194 else if(0 == (object->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(object->data.vorbis_comment.comments, new_size)))
Josh Coalson90ced912002-05-30 05:23:38 +00001195 return false;
1196
1197 /* if growing, zero all the length/pointers of new elements */
1198 if(new_size > old_size)
1199 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1200 }
1201
1202 object->data.vorbis_comment.num_comments = new_num_comments;
1203
1204 vorbiscomment_calculate_length_(object);
1205 return true;
1206}
1207
Josh Coalson6afed9f2002-10-16 22:29:47 +00001208FLAC_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 +00001209{
Josh Coalsonfb993862003-01-15 03:18:07 +00001210 FLAC__ASSERT(0 != object);
1211 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1212
Josh Coalson2de11242004-12-30 03:41:19 +00001213 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1214 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001215 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001216}
1217
Josh Coalson6afed9f2002-10-16 22:29:47 +00001218FLAC_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 +00001219{
Josh Coalsoncc682512002-06-08 04:53:42 +00001220 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001221
1222 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001223 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001224 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001225
Josh Coalson2de11242004-12-30 03:41:19 +00001226 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1227 return false;
1228
Josh Coalson6b8e5302002-05-31 06:23:09 +00001229 vc = &object->data.vorbis_comment;
1230
1231 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001232 return false;
1233
1234 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001235 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 +00001236 vc->comments[comment_num].length = 0;
1237 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001238
1239 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1240}
1241
Josh Coalsondef597e2004-12-30 00:59:30 +00001242FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1243{
1244 FLAC__ASSERT(0 != object);
1245 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1246 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1247}
1248
1249FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1250{
1251 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
Josh Coalson2de11242004-12-30 03:41:19 +00001252
1253 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1254 return false;
1255
Josh Coalsondef597e2004-12-30 00:59:30 +00001256 {
1257 int i;
Josh Coalson9bedd782007-02-22 01:37:33 +00001258 size_t field_name_length;
Josh Coalsondef597e2004-12-30 00:59:30 +00001259 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1260
1261 FLAC__ASSERT(0 != eq);
1262
1263 if(0 == eq)
1264 return false; /* double protection */
1265
1266 field_name_length = eq-entry.entry;
1267
Josh Coalsonb4de1692005-08-24 07:39:25 +00001268 if((i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length)) >= 0) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001269 unsigned index = (unsigned)i;
1270 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
1271 return false;
1272 if(all && (index+1 < object->data.vorbis_comment.num_comments)) {
Josh Coalsonb4de1692005-08-24 07:39:25 +00001273 for(i = vorbiscomment_find_entry_from_(object, index+1, (const char *)entry.entry, field_name_length); i >= 0; ) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001274 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
1275 return false;
1276 if((unsigned)i < object->data.vorbis_comment.num_comments)
Josh Coalsonb4de1692005-08-24 07:39:25 +00001277 i = vorbiscomment_find_entry_from_(object, (unsigned)i, (const char *)entry.entry, field_name_length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001278 else
1279 i = -1;
1280 }
1281 }
1282 return true;
1283 }
1284 else
1285 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1286 }
1287}
1288
Josh Coalson6afed9f2002-10-16 22:29:47 +00001289FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001290{
Josh Coalsoncc682512002-06-08 04:53:42 +00001291 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001292
1293 FLAC__ASSERT(0 != object);
1294 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001295 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001296
Josh Coalson6b8e5302002-05-31 06:23:09 +00001297 vc = &object->data.vorbis_comment;
1298
Josh Coalson90ced912002-05-30 05:23:38 +00001299 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +00001300 if(0 != vc->comments[comment_num].entry)
1301 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001302
1303 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001304 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 +00001305 vc->comments[vc->num_comments-1].length = 0;
1306 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001307
Josh Coalson6b8e5302002-05-31 06:23:09 +00001308 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +00001309}
Josh Coalson45bb9882002-10-26 04:34:16 +00001310
Josh Coalsondef597e2004-12-30 00:59:30 +00001311FLAC_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 +00001312{
Josh Coalsondef597e2004-12-30 00:59:30 +00001313 FLAC__ASSERT(0 != entry);
1314 FLAC__ASSERT(0 != field_name);
1315 FLAC__ASSERT(0 != field_value);
1316
Josh Coalson2de11242004-12-30 03:41:19 +00001317 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1318 return false;
Josh Coalsonb4de1692005-08-24 07:39:25 +00001319 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
Josh Coalson2de11242004-12-30 03:41:19 +00001320 return false;
1321
Josh Coalsondef597e2004-12-30 00:59:30 +00001322 {
1323 const size_t nn = strlen(field_name);
1324 const size_t nv = strlen(field_value);
1325 entry->length = nn + 1 /*=*/ + nv;
Josh Coalson0f008d22007-09-11 04:49:56 +00001326 if(0 == (entry->entry = (FLAC__byte*)safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001327 return false;
1328 memcpy(entry->entry, field_name, nn);
1329 entry->entry[nn] = '=';
1330 memcpy(entry->entry+nn+1, field_value, nv);
1331 entry->entry[entry->length] = '\0';
1332 }
1333
1334 return true;
1335}
1336
1337FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1338{
1339 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1340 FLAC__ASSERT(0 != field_name);
1341 FLAC__ASSERT(0 != field_value);
Josh Coalson2de11242004-12-30 03:41:19 +00001342
1343 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1344 return false;
1345
Josh Coalsondef597e2004-12-30 00:59:30 +00001346 {
1347 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1348 const size_t nn = eq-entry.entry;
1349 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1350 FLAC__ASSERT(0 != eq);
1351 if(0 == eq)
1352 return false; /* double protection */
Josh Coalson0f008d22007-09-11 04:49:56 +00001353 if(0 == (*field_name = (char*)safe_malloc_add_2op_(nn, /*+*/1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001354 return false;
Josh Coalson0f008d22007-09-11 04:49:56 +00001355 if(0 == (*field_value = (char*)safe_malloc_add_2op_(nv, /*+*/1))) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001356 free(*field_name);
1357 return false;
1358 }
1359 memcpy(*field_name, entry.entry, nn);
1360 memcpy(*field_value, entry.entry+nn+1, nv);
1361 (*field_name)[nn] = '\0';
1362 (*field_value)[nv] = '\0';
1363 }
1364
1365 return true;
1366}
1367
Josh Coalson44623112005-01-30 18:15:36 +00001368FLAC_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 +00001369{
1370 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1371 {
1372 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
Josh Coalson2beca732006-11-21 01:51:58 +00001373#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__
Josh Coalson45bb9882002-10-26 04:34:16 +00001374#define FLAC__STRNCASECMP strnicmp
1375#else
1376#define FLAC__STRNCASECMP strncasecmp
1377#endif
Josh Coalsondef597e2004-12-30 00:59:30 +00001378 return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
Josh Coalson45bb9882002-10-26 04:34:16 +00001379#undef FLAC__STRNCASECMP
Josh Coalsondef597e2004-12-30 00:59:30 +00001380 }
Josh Coalson45bb9882002-10-26 04:34:16 +00001381}
1382
Josh Coalsonb667e702002-11-05 07:24:33 +00001383FLAC_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 +00001384{
Josh Coalsondef597e2004-12-30 00:59:30 +00001385 FLAC__ASSERT(0 != field_name);
Josh Coalson45bb9882002-10-26 04:34:16 +00001386
Josh Coalsondef597e2004-12-30 00:59:30 +00001387 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
Josh Coalson45bb9882002-10-26 04:34:16 +00001388}
1389
1390FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1391{
1392 const unsigned field_name_length = strlen(field_name);
1393 unsigned i;
1394
1395 FLAC__ASSERT(0 != object);
1396 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1397
1398 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001399 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 +00001400 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1401 return -1;
1402 else
1403 return 1;
1404 }
1405 }
1406
1407 return 0;
1408}
1409
1410FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1411{
1412 FLAC__bool ok = true;
1413 unsigned matching = 0;
1414 const unsigned field_name_length = strlen(field_name);
1415 int i;
1416
1417 FLAC__ASSERT(0 != object);
1418 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1419
1420 /* must delete from end to start otherwise it will interfere with our iteration */
1421 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001422 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 +00001423 matching++;
1424 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1425 }
1426 }
1427
1428 return ok? (int)matching : -1;
1429}
Josh Coalson8e9c4512002-11-14 05:00:24 +00001430
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00001431FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
Josh Coalsondf7240a2002-11-16 06:32:30 +00001432{
1433 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1434}
1435
1436FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1437{
1438 FLAC__StreamMetadata_CueSheet_Track *to;
1439
1440 FLAC__ASSERT(0 != object);
1441
1442 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1443 if(!copy_track_(to, object)) {
1444 FLAC__metadata_object_cuesheet_track_delete(to);
1445 return 0;
1446 }
1447 }
1448
1449 return to;
1450}
1451
1452void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1453{
1454 FLAC__ASSERT(0 != object);
1455
1456 if(0 != object->indices) {
1457 FLAC__ASSERT(object->num_indices > 0);
1458 free(object->indices);
1459 }
1460}
1461
1462FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1463{
1464 FLAC__metadata_object_cuesheet_track_delete_data(object);
1465 free(object);
1466}
1467
Josh Coalson8e9c4512002-11-14 05:00:24 +00001468FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1469{
1470 FLAC__StreamMetadata_CueSheet_Track *track;
1471 FLAC__ASSERT(0 != object);
1472 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1473 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1474
1475 track = &object->data.cue_sheet.tracks[track_num];
1476
1477 if(0 == track->indices) {
1478 FLAC__ASSERT(track->num_indices == 0);
1479 if(0 == new_num_indices)
1480 return true;
1481 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1482 return false;
1483 }
1484 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001485 const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1486 const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1487
1488 /* overflow check */
1489 if((size_t)new_num_indices > SIZE_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
1490 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001491
1492 FLAC__ASSERT(track->num_indices > 0);
1493
1494 if(new_size == 0) {
1495 free(track->indices);
1496 track->indices = 0;
1497 }
1498 else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
1499 return false;
1500
1501 /* if growing, zero all the lengths/pointers of new elements */
1502 if(new_size > old_size)
1503 memset(track->indices + track->num_indices, 0, new_size - old_size);
1504 }
1505
1506 track->num_indices = new_num_indices;
1507
1508 cuesheet_calculate_length_(object);
1509 return true;
1510}
1511
1512FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index)
1513{
1514 FLAC__StreamMetadata_CueSheet_Track *track;
1515
1516 FLAC__ASSERT(0 != object);
1517 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1518 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1519 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1520
1521 track = &object->data.cue_sheet.tracks[track_num];
1522
1523 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1524 return false;
1525
1526 /* move all indices >= index_num forward one space */
1527 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1528
1529 track->indices[index_num] = index;
1530 cuesheet_calculate_length_(object);
1531 return true;
1532}
1533
Josh Coalson3b026e82002-11-21 06:41:01 +00001534FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1535{
1536 FLAC__StreamMetadata_CueSheet_Index index;
1537 memset(&index, 0, sizeof(index));
Josh Coalsonbfc8e312002-11-21 09:00:25 +00001538 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
Josh Coalson3b026e82002-11-21 06:41:01 +00001539}
1540
Josh Coalson8e9c4512002-11-14 05:00:24 +00001541FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1542{
1543 FLAC__StreamMetadata_CueSheet_Track *track;
1544
1545 FLAC__ASSERT(0 != object);
1546 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1547 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1548 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1549
1550 track = &object->data.cue_sheet.tracks[track_num];
1551
1552 /* move all indices > index_num backward one space */
Josh Coalson4fa90592002-12-04 07:01:37 +00001553 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 +00001554
1555 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1556 cuesheet_calculate_length_(object);
1557 return true;
1558}
1559
1560FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1561{
1562 FLAC__ASSERT(0 != object);
1563 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1564
1565 if(0 == object->data.cue_sheet.tracks) {
1566 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1567 if(0 == new_num_tracks)
1568 return true;
1569 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1570 return false;
1571 }
1572 else {
Josh Coalson0f008d22007-09-11 04:49:56 +00001573 const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1574 const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1575
1576 /* overflow check */
1577 if((size_t)new_num_tracks > SIZE_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
1578 return false;
Josh Coalson8e9c4512002-11-14 05:00:24 +00001579
1580 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1581
1582 /* if shrinking, free the truncated entries */
1583 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1584 unsigned i;
1585 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1586 if(0 != object->data.cue_sheet.tracks[i].indices)
1587 free(object->data.cue_sheet.tracks[i].indices);
1588 }
1589
1590 if(new_size == 0) {
1591 free(object->data.cue_sheet.tracks);
1592 object->data.cue_sheet.tracks = 0;
1593 }
1594 else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
1595 return false;
1596
1597 /* if growing, zero all the lengths/pointers of new elements */
1598 if(new_size > old_size)
1599 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1600 }
1601
1602 object->data.cue_sheet.num_tracks = new_num_tracks;
1603
1604 cuesheet_calculate_length_(object);
1605 return true;
1606}
1607
Josh Coalsondf7240a2002-11-16 06:32:30 +00001608FLAC_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 +00001609{
Josh Coalsonfb993862003-01-15 03:18:07 +00001610 FLAC__ASSERT(0 != object);
1611 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1612
Josh Coalsondf7240a2002-11-16 06:32:30 +00001613 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001614}
1615
Josh Coalsondf7240a2002-11-16 06:32:30 +00001616FLAC_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 +00001617{
1618 FLAC__StreamMetadata_CueSheet *cs;
1619
1620 FLAC__ASSERT(0 != object);
1621 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1622 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1623
1624 cs = &object->data.cue_sheet;
1625
1626 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1627 return false;
1628
1629 /* move all tracks >= track_num forward one space */
1630 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1631 cs->tracks[track_num].num_indices = 0;
1632 cs->tracks[track_num].indices = 0;
1633
1634 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1635}
1636
Josh Coalson3b026e82002-11-21 06:41:01 +00001637FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1638{
1639 FLAC__StreamMetadata_CueSheet_Track track;
1640 memset(&track, 0, sizeof(track));
1641 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1642}
1643
Josh Coalson8e9c4512002-11-14 05:00:24 +00001644FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1645{
1646 FLAC__StreamMetadata_CueSheet *cs;
1647
1648 FLAC__ASSERT(0 != object);
1649 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1650 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1651
1652 cs = &object->data.cue_sheet;
1653
1654 /* free the track at track_num */
1655 if(0 != cs->tracks[track_num].indices)
1656 free(cs->tracks[track_num].indices);
1657
1658 /* move all tracks > track_num backward one space */
1659 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1660 cs->tracks[cs->num_tracks-1].num_indices = 0;
1661 cs->tracks[cs->num_tracks-1].indices = 0;
1662
1663 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1664}
1665
1666FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1667{
1668 FLAC__ASSERT(0 != object);
1669 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1670
1671 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1672}
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001673
1674static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1675{
1676 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1677 return 0;
1678 else if (cs->tracks[track].indices[0].number == 1)
1679 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1680 else if (cs->tracks[track].num_indices < 2)
1681 return 0;
1682 else if (cs->tracks[track].indices[1].number == 1)
1683 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1684 else
1685 return 0;
1686}
1687
1688static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1689{
1690 FLAC__uint32 n = 0;
1691 while (x) {
1692 n += (x%10);
1693 x /= 10;
1694 }
1695 return n;
1696}
1697
1698FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1699{
1700 const FLAC__StreamMetadata_CueSheet *cs;
1701
1702 FLAC__ASSERT(0 != object);
1703 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1704
1705 cs = &object->data.cue_sheet;
1706
1707 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1708 return 0;
1709
1710 {
1711 FLAC__uint32 i, length, sum = 0;
1712 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1713 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1714 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1715
1716 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1717 }
1718}
Josh Coalsone343ab22006-09-23 19:21:19 +00001719
1720FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1721{
1722 char *old;
1723 size_t old_length, new_length;
1724
1725 FLAC__ASSERT(0 != object);
1726 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1727 FLAC__ASSERT(0 != mime_type);
1728
1729 old = object->data.picture.mime_type;
1730 old_length = old? strlen(old) : 0;
1731 new_length = strlen(mime_type);
1732
1733 /* do the copy first so that if we fail we leave the object untouched */
1734 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001735 if(new_length >= SIZE_MAX) /* overflow check */
1736 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001737 if(!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1738 return false;
1739 }
1740 else {
1741 object->data.picture.mime_type = mime_type;
1742 }
1743
1744 if(0 != old)
1745 free(old);
1746
1747 object->length -= old_length;
1748 object->length += new_length;
1749 return true;
1750}
1751
1752FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1753{
1754 FLAC__byte *old;
1755 size_t old_length, new_length;
1756
1757 FLAC__ASSERT(0 != object);
1758 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1759 FLAC__ASSERT(0 != description);
1760
1761 old = object->data.picture.description;
1762 old_length = old? strlen((const char *)old) : 0;
1763 new_length = strlen((const char *)description);
1764
1765 /* do the copy first so that if we fail we leave the object untouched */
1766 if(copy) {
Josh Coalson0f008d22007-09-11 04:49:56 +00001767 if(new_length >= SIZE_MAX) /* overflow check */
1768 return false;
Josh Coalsone343ab22006-09-23 19:21:19 +00001769 if(!copy_bytes_(&object->data.picture.description, description, new_length+1))
1770 return false;
1771 }
1772 else {
1773 object->data.picture.description = description;
1774 }
1775
1776 if(0 != old)
1777 free(old);
1778
1779 object->length -= old_length;
1780 object->length += new_length;
1781 return true;
1782}
1783
1784FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1785{
1786 FLAC__byte *old;
1787
1788 FLAC__ASSERT(0 != object);
1789 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1790 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
1791
1792 old = object->data.picture.data;
1793
1794 /* do the copy first so that if we fail we leave the object untouched */
1795 if(copy) {
1796 if(!copy_bytes_(&object->data.picture.data, data, length))
1797 return false;
1798 }
1799 else {
1800 object->data.picture.data = data;
1801 }
1802
1803 if(0 != old)
1804 free(old);
1805
1806 object->length -= object->data.picture.data_length;
1807 object->data.picture.data_length = length;
1808 object->length += length;
1809 return true;
1810}
1811
1812FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1813{
1814 FLAC__ASSERT(0 != object);
1815 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1816
1817 return FLAC__format_picture_is_legal(&object->data.picture, violation);
1818}