blob: 6846c3bcfc2c84ea5ab854c1a091df1e4812c27d [file] [log] [blame]
Josh Coalson90ced912002-05-30 05:23:38 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson0395dac2006-04-25 06:59:33 +00002 * Copyright (C) 2001,2002,2003,2004,2005,2006 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"
42
43
44/****************************************************************************
45 *
46 * Local routines
47 *
48 ***************************************************************************/
49
50static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
51{
52 if(bytes > 0 && 0 != from) {
53 FLAC__byte *x;
Josh Coalson5e3fcb22002-11-06 07:11:26 +000054 if(0 == (x = (FLAC__byte*)malloc(bytes)))
Josh Coalson90ced912002-05-30 05:23:38 +000055 return false;
56 memcpy(x, from, bytes);
57 *to = x;
58 }
59 else {
60 FLAC__ASSERT(0 == from);
61 FLAC__ASSERT(bytes == 0);
62 *to = 0;
63 }
64 return true;
65}
66
Josh Coalsondef597e2004-12-30 00:59:30 +000067static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
68{
69 FLAC__byte *x = (FLAC__byte*)realloc(*entry, length+1);
70 if(0 != x) {
71 x[length] = '\0';
72 *entry = x;
73 return true;
74 }
75 else
76 return false;
77}
78
Josh Coalsoncc682512002-06-08 04:53:42 +000079static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
Josh Coalson90ced912002-05-30 05:23:38 +000080{
81 to->length = from->length;
82 if(0 == from->entry) {
83 FLAC__ASSERT(from->length == 0);
84 to->entry = 0;
85 }
86 else {
87 FLAC__byte *x;
88 FLAC__ASSERT(from->length > 0);
Josh Coalsondef597e2004-12-30 00:59:30 +000089 if(0 == (x = (FLAC__byte*)malloc(from->length+1)))
Josh Coalson90ced912002-05-30 05:23:38 +000090 return false;
91 memcpy(x, from->entry, from->length);
Josh Coalsondef597e2004-12-30 00:59:30 +000092 x[from->length] = '\0';
Josh Coalson90ced912002-05-30 05:23:38 +000093 to->entry = x;
94 }
95 return true;
96}
97
Josh Coalson8e9c4512002-11-14 05:00:24 +000098static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
99{
100 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
101 if(0 == from->indices) {
102 FLAC__ASSERT(from->num_indices == 0);
103 }
104 else {
105 FLAC__StreamMetadata_CueSheet_Index *x;
106 FLAC__ASSERT(from->num_indices > 0);
107 if(0 == (x = (FLAC__StreamMetadata_CueSheet_Index*)malloc(from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index))))
108 return false;
109 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
110 to->indices = x;
111 }
112 return true;
113}
114
Josh Coalsoncc682512002-06-08 04:53:42 +0000115static void seektable_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000116{
117 FLAC__ASSERT(0 != object);
118 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
119
120 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
121}
122
Josh Coalsoncc682512002-06-08 04:53:42 +0000123static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000124{
Josh Coalsoncc682512002-06-08 04:53:42 +0000125 FLAC__StreamMetadata_SeekPoint *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000126
127 FLAC__ASSERT(num_points > 0);
128
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000129 object_array = (FLAC__StreamMetadata_SeekPoint*)malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
Josh Coalson90ced912002-05-30 05:23:38 +0000130
131 if(0 != object_array) {
132 unsigned i;
133 for(i = 0; i < num_points; i++) {
134 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
135 object_array[i].stream_offset = 0;
136 object_array[i].frame_samples = 0;
137 }
138 }
139
140 return object_array;
141}
142
Josh Coalsoncc682512002-06-08 04:53:42 +0000143static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000144{
145 unsigned i;
146
147 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
148
149 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
150 object->length += object->data.vorbis_comment.vendor_string.length;
151 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
152 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
153 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
154 object->length += object->data.vorbis_comment.comments[i].length;
155 }
156}
157
Josh Coalsoncc682512002-06-08 04:53:42 +0000158static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000159{
Josh Coalson90ced912002-05-30 05:23:38 +0000160 FLAC__ASSERT(num_comments > 0);
161
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000162 return (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000163}
164
Josh Coalsoncc682512002-06-08 04:53:42 +0000165static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000166{
167 unsigned i;
168
169 FLAC__ASSERT(0 != object_array && num_comments > 0);
170
171 for(i = 0; i < num_comments; i++)
172 if(0 != object_array[i].entry)
173 free(object_array[i].entry);
174
175 if(0 != object_array)
176 free(object_array);
177}
178
Josh Coalsoncc682512002-06-08 04:53:42 +0000179static 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 +0000180{
Josh Coalsoncc682512002-06-08 04:53:42 +0000181 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000182
183 FLAC__ASSERT(0 != object_array);
184 FLAC__ASSERT(num_comments > 0);
185
186 return_array = vorbiscomment_entry_array_new_(num_comments);
187
188 if(0 != return_array) {
189 unsigned i;
190
Josh Coalson90ced912002-05-30 05:23:38 +0000191 for(i = 0; i < num_comments; i++) {
192 if(!copy_vcentry_(return_array+i, object_array+i)) {
193 vorbiscomment_entry_array_delete_(return_array, num_comments);
194 return 0;
195 }
196 }
197 }
198
199 return return_array;
200}
201
Josh Coalsoncc682512002-06-08 04:53:42 +0000202static 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 +0000203{
204 FLAC__byte *save;
205
206 FLAC__ASSERT(0 != object);
207 FLAC__ASSERT(0 != dest);
208 FLAC__ASSERT(0 != src);
209 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson748459c2004-09-08 00:49:30 +0000210 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
Josh Coalson90ced912002-05-30 05:23:38 +0000211
Josh Coalson6b8e5302002-05-31 06:23:09 +0000212 save = dest->entry;
Josh Coalson90ced912002-05-30 05:23:38 +0000213
Josh Coalsondef597e2004-12-30 00:59:30 +0000214 if(0 != src->entry && src->length > 0) {
215 if(copy) {
216 /* do the copy first so that if we fail we leave the dest object untouched */
217 if(!copy_vcentry_(dest, src))
218 return false;
219 }
220 else {
221 /* we have to make sure that the string we're taking over is null-terminated */
222
223 /*
224 * Stripping the const from src->entry is OK since we're taking
225 * ownership of the pointer. This is a hack around a deficiency
226 * in the API where the same function is used for 'copy' and
227 * 'own', but the source entry is a const pointer. If we were
228 * precise, the 'own' flavor would be a separate function with a
229 * non-const source pointer. But it's not, so we hack away.
230 */
231 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
232 return false;
233 *dest = *src;
234 }
Josh Coalson90ced912002-05-30 05:23:38 +0000235 }
236 else {
Josh Coalsondef597e2004-12-30 00:59:30 +0000237 /* the src is null */
Josh Coalson90ced912002-05-30 05:23:38 +0000238 *dest = *src;
239 }
240
241 if(0 != save)
242 free(save);
243
244 vorbiscomment_calculate_length_(object);
245 return true;
246}
247
Josh Coalsondef597e2004-12-30 00:59:30 +0000248static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
249{
250 unsigned i;
251
252 FLAC__ASSERT(0 != object);
253 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
254 FLAC__ASSERT(0 != field_name);
255
256 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
257 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
258 return (int)i;
259 }
260
261 return -1;
262}
263
Josh Coalson8e9c4512002-11-14 05:00:24 +0000264static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
265{
266 unsigned i;
267
268 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
269
270 object->length = (
271 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
272 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000273 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
Josh Coalsondf7240a2002-11-16 06:32:30 +0000274 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
Josh Coalson8e9c4512002-11-14 05:00:24 +0000275 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
276 ) / 8;
277
278 object->length += object->data.cue_sheet.num_tracks * (
279 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
280 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
281 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
282 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
283 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
284 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
285 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
286 ) / 8;
287
288 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
289 object->length += object->data.cue_sheet.tracks[i].num_indices * (
290 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
291 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
292 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
293 ) / 8;
294 }
295}
296
297static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
298{
299 FLAC__ASSERT(num_indices > 0);
300
301 return (FLAC__StreamMetadata_CueSheet_Index*)calloc(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
302}
303
304static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
305{
306 FLAC__ASSERT(num_tracks > 0);
307
308 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
309}
310
311static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
312{
313 unsigned i;
314
315 FLAC__ASSERT(0 != object_array && num_tracks > 0);
316
317 for(i = 0; i < num_tracks; i++) {
318 if(0 != object_array[i].indices) {
319 FLAC__ASSERT(object_array[i].num_indices > 0);
320 free(object_array[i].indices);
321 }
322 }
323
324 if(0 != object_array)
325 free(object_array);
326}
327
328static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
329{
330 FLAC__StreamMetadata_CueSheet_Track *return_array;
331
332 FLAC__ASSERT(0 != object_array);
333 FLAC__ASSERT(num_tracks > 0);
334
335 return_array = cuesheet_track_array_new_(num_tracks);
336
337 if(0 != return_array) {
338 unsigned i;
339
340 for(i = 0; i < num_tracks; i++) {
341 if(!copy_track_(return_array+i, object_array+i)) {
342 cuesheet_track_array_delete_(return_array, num_tracks);
343 return 0;
344 }
345 }
346 }
347
348 return return_array;
349}
350
351static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
352{
353 FLAC__StreamMetadata_CueSheet_Index *save;
354
355 FLAC__ASSERT(0 != object);
356 FLAC__ASSERT(0 != dest);
357 FLAC__ASSERT(0 != src);
358 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
359 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000360
361 save = dest->indices;
362
363 /* do the copy first so that if we fail we leave the object untouched */
364 if(copy) {
365 if(!copy_track_(dest, src))
366 return false;
367 }
368 else {
369 *dest = *src;
370 }
371
372 if(0 != save)
373 free(save);
374
375 cuesheet_calculate_length_(object);
376 return true;
377}
378
Josh Coalson90ced912002-05-30 05:23:38 +0000379
380/****************************************************************************
381 *
382 * Metadata object routines
383 *
384 ***************************************************************************/
385
Josh Coalson6afed9f2002-10-16 22:29:47 +0000386FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
Josh Coalson90ced912002-05-30 05:23:38 +0000387{
Josh Coalson1cb23412004-07-22 01:32:00 +0000388 FLAC__StreamMetadata *object;
389
390 if(type > FLAC__MAX_METADATA_TYPE_CODE)
391 return 0;
392
393 object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000394 if(0 != object) {
Josh Coalson90ced912002-05-30 05:23:38 +0000395 object->is_last = false;
396 object->type = type;
397 switch(type) {
398 case FLAC__METADATA_TYPE_STREAMINFO:
399 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
400 break;
401 case FLAC__METADATA_TYPE_PADDING:
Josh Coalsond0609472003-01-10 05:37:13 +0000402 /* calloc() took care of this for us:
403 object->length = 0;
404 */
Josh Coalson90ced912002-05-30 05:23:38 +0000405 break;
406 case FLAC__METADATA_TYPE_APPLICATION:
407 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
Josh Coalsond0609472003-01-10 05:37:13 +0000408 /* calloc() took care of this for us:
409 object->data.application.data = 0;
410 */
Josh Coalson90ced912002-05-30 05:23:38 +0000411 break;
412 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsond0609472003-01-10 05:37:13 +0000413 /* calloc() took care of this for us:
414 object->length = 0;
415 object->data.seek_table.num_points = 0;
416 object->data.seek_table.points = 0;
417 */
Josh Coalson90ced912002-05-30 05:23:38 +0000418 break;
419 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson45bb9882002-10-26 04:34:16 +0000420 {
421 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
Josh Coalsondef597e2004-12-30 00:59:30 +0000422 if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
Josh Coalson45bb9882002-10-26 04:34:16 +0000423 free(object);
424 return 0;
425 }
Josh Coalson8e9c4512002-11-14 05:00:24 +0000426 vorbiscomment_calculate_length_(object);
Josh Coalson45bb9882002-10-26 04:34:16 +0000427 }
Josh Coalson90ced912002-05-30 05:23:38 +0000428 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000429 case FLAC__METADATA_TYPE_CUESHEET:
430 cuesheet_calculate_length_(object);
431 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000432 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000433 /* calloc() took care of this for us:
434 object->length = 0;
435 object->data.unknown.data = 0;
436 */
437 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000438 }
439 }
440
441 return object;
442}
443
Josh Coalson6afed9f2002-10-16 22:29:47 +0000444FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000445{
Josh Coalsoncc682512002-06-08 04:53:42 +0000446 FLAC__StreamMetadata *to;
Josh Coalson90ced912002-05-30 05:23:38 +0000447
448 FLAC__ASSERT(0 != object);
449
450 if(0 != (to = FLAC__metadata_object_new(object->type))) {
451 to->is_last = object->is_last;
452 to->type = object->type;
453 to->length = object->length;
454 switch(to->type) {
455 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalsoncc682512002-06-08 04:53:42 +0000456 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
Josh Coalson90ced912002-05-30 05:23:38 +0000457 break;
458 case FLAC__METADATA_TYPE_PADDING:
459 break;
460 case FLAC__METADATA_TYPE_APPLICATION:
461 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
462 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
463 FLAC__metadata_object_delete(to);
464 return 0;
465 }
466 break;
467 case FLAC__METADATA_TYPE_SEEKTABLE:
468 to->data.seek_table.num_points = object->data.seek_table.num_points;
Josh Coalsoncc682512002-06-08 04:53:42 +0000469 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 +0000470 FLAC__metadata_object_delete(to);
471 return 0;
472 }
473 break;
474 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000475 if(0 != to->data.vorbis_comment.vendor_string.entry) {
Josh Coalson45bb9882002-10-26 04:34:16 +0000476 free(to->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000477 to->data.vorbis_comment.vendor_string.entry = 0;
478 }
Josh Coalson90ced912002-05-30 05:23:38 +0000479 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
480 FLAC__metadata_object_delete(to);
481 return 0;
482 }
483 if(object->data.vorbis_comment.num_comments == 0) {
484 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
485 to->data.vorbis_comment.comments = 0;
486 }
487 else {
488 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
489 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
490 if(0 == to->data.vorbis_comment.comments) {
491 FLAC__metadata_object_delete(to);
492 return 0;
493 }
494 }
495 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
496 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000497 case FLAC__METADATA_TYPE_CUESHEET:
498 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
499 if(object->data.cue_sheet.num_tracks == 0) {
500 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
501 }
502 else {
503 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
504 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
505 if(0 == to->data.cue_sheet.tracks) {
506 FLAC__metadata_object_delete(to);
507 return 0;
508 }
509 }
510 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000511 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000512 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
513 FLAC__metadata_object_delete(to);
514 return 0;
515 }
516 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000517 }
518 }
519
520 return to;
521}
522
Josh Coalsoncc682512002-06-08 04:53:42 +0000523void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000524{
525 FLAC__ASSERT(0 != object);
526
527 switch(object->type) {
528 case FLAC__METADATA_TYPE_STREAMINFO:
529 case FLAC__METADATA_TYPE_PADDING:
530 break;
531 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson4fa90592002-12-04 07:01:37 +0000532 if(0 != object->data.application.data) {
Josh Coalson90ced912002-05-30 05:23:38 +0000533 free(object->data.application.data);
Josh Coalson4fa90592002-12-04 07:01:37 +0000534 object->data.application.data = 0;
535 }
Josh Coalson90ced912002-05-30 05:23:38 +0000536 break;
537 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalson4fa90592002-12-04 07:01:37 +0000538 if(0 != object->data.seek_table.points) {
Josh Coalson90ced912002-05-30 05:23:38 +0000539 free(object->data.seek_table.points);
Josh Coalson4fa90592002-12-04 07:01:37 +0000540 object->data.seek_table.points = 0;
541 }
Josh Coalson90ced912002-05-30 05:23:38 +0000542 break;
543 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000544 if(0 != object->data.vorbis_comment.vendor_string.entry) {
Josh Coalson90ced912002-05-30 05:23:38 +0000545 free(object->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000546 object->data.vorbis_comment.vendor_string.entry = 0;
547 }
Josh Coalson90ced912002-05-30 05:23:38 +0000548 if(0 != object->data.vorbis_comment.comments) {
549 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
550 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
551 }
552 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000553 case FLAC__METADATA_TYPE_CUESHEET:
554 if(0 != object->data.cue_sheet.tracks) {
555 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
556 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
557 }
558 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000559 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000560 if(0 != object->data.unknown.data) {
561 free(object->data.unknown.data);
562 object->data.unknown.data = 0;
563 }
564 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000565 }
566}
567
Josh Coalson6afed9f2002-10-16 22:29:47 +0000568FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000569{
570 FLAC__metadata_object_delete_data(object);
571 free(object);
572}
573
Josh Coalsoncc682512002-06-08 04:53:42 +0000574static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000575{
576 if(block1->min_blocksize != block2->min_blocksize)
577 return false;
578 if(block1->max_blocksize != block2->max_blocksize)
579 return false;
580 if(block1->min_framesize != block2->min_framesize)
581 return false;
582 if(block1->max_framesize != block2->max_framesize)
583 return false;
584 if(block1->sample_rate != block2->sample_rate)
585 return false;
586 if(block1->channels != block2->channels)
587 return false;
588 if(block1->bits_per_sample != block2->bits_per_sample)
589 return false;
590 if(block1->total_samples != block2->total_samples)
591 return false;
592 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
593 return false;
594 return true;
595}
596
Josh Coalsoncc682512002-06-08 04:53:42 +0000597static 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 +0000598{
599 FLAC__ASSERT(0 != block1);
600 FLAC__ASSERT(0 != block2);
601 FLAC__ASSERT(block_length >= sizeof(block1->id));
602
603 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
604 return false;
605 if(0 != block1->data && 0 != block2->data)
606 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
607 else
608 return block1->data == block2->data;
609}
610
Josh Coalsoncc682512002-06-08 04:53:42 +0000611static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000612{
613 unsigned i;
614
615 FLAC__ASSERT(0 != block1);
616 FLAC__ASSERT(0 != block2);
617
618 if(block1->num_points != block2->num_points)
619 return false;
620
621 if(0 != block1->points && 0 != block2->points) {
622 for(i = 0; i < block1->num_points; i++) {
623 if(block1->points[i].sample_number != block2->points[i].sample_number)
624 return false;
625 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
626 return false;
627 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
628 return false;
629 }
630 return true;
631 }
632 else
633 return block1->points == block2->points;
634}
635
Josh Coalsoncc682512002-06-08 04:53:42 +0000636static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000637{
638 unsigned i;
639
640 if(block1->vendor_string.length != block2->vendor_string.length)
641 return false;
642
643 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
644 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
645 return false;
646 }
647 else if(block1->vendor_string.entry != block2->vendor_string.entry)
648 return false;
649
650 if(block1->num_comments != block2->num_comments)
651 return false;
652
653 for(i = 0; i < block1->num_comments; i++) {
654 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
655 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
656 return false;
657 }
658 else if(block1->comments[i].entry != block2->comments[i].entry)
659 return false;
660 }
661 return true;
662}
663
Josh Coalson8e9c4512002-11-14 05:00:24 +0000664static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
665{
666 unsigned i, j;
667
668 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
669 return false;
670
671 if(block1->lead_in != block2->lead_in)
672 return false;
673
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000674 if(block1->is_cd != block2->is_cd)
675 return false;
676
Josh Coalson8e9c4512002-11-14 05:00:24 +0000677 if(block1->num_tracks != block2->num_tracks)
678 return false;
679
680 if(0 != block1->tracks && 0 != block2->tracks) {
681 FLAC__ASSERT(block1->num_tracks > 0);
682 for(i = 0; i < block1->num_tracks; i++) {
683 if(block1->tracks[i].offset != block2->tracks[i].offset)
684 return false;
685 if(block1->tracks[i].number != block2->tracks[i].number)
686 return false;
687 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
688 return false;
689 if(block1->tracks[i].type != block2->tracks[i].type)
690 return false;
691 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
692 return false;
693 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
694 return false;
695 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
696 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
697 for(j = 0; j < block1->tracks[i].num_indices; j++) {
698 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
699 return false;
700 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
701 return false;
702 }
703 }
704 else if(block1->tracks[i].indices != block2->tracks[i].indices)
705 return false;
706 }
707 }
708 else if(block1->tracks != block2->tracks)
709 return false;
710 return true;
711}
712
Josh Coalsond0609472003-01-10 05:37:13 +0000713static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
714{
715 FLAC__ASSERT(0 != block1);
716 FLAC__ASSERT(0 != block2);
717
718 if(0 != block1->data && 0 != block2->data)
719 return 0 == memcmp(block1->data, block2->data, block_length);
720 else
721 return block1->data == block2->data;
722}
723
Josh Coalson6afed9f2002-10-16 22:29:47 +0000724FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000725{
Josh Coalsond8ab3462002-07-11 05:54:05 +0000726 FLAC__ASSERT(0 != block1);
727 FLAC__ASSERT(0 != block2);
728
Josh Coalson57ba6f42002-06-07 05:27:37 +0000729 if(block1->type != block2->type) {
730 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000731 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000732 if(block1->is_last != block2->is_last) {
733 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000734 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000735 if(block1->length != block2->length) {
736 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000737 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000738 switch(block1->type) {
739 case FLAC__METADATA_TYPE_STREAMINFO:
740 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
741 case FLAC__METADATA_TYPE_PADDING:
742 return true; /* we don't compare the padding guts */
743 case FLAC__METADATA_TYPE_APPLICATION:
744 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
745 case FLAC__METADATA_TYPE_SEEKTABLE:
746 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
747 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
748 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000749 case FLAC__METADATA_TYPE_CUESHEET:
750 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000751 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000752 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000753 }
754}
755
Josh Coalson6afed9f2002-10-16 22:29:47 +0000756FLAC_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 +0000757{
758 FLAC__byte *save;
759
Josh Coalsond8ab3462002-07-11 05:54:05 +0000760 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000761 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
762 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
763
764 save = object->data.application.data;
765
766 /* do the copy first so that if we fail we leave the object untouched */
767 if(copy) {
768 if(!copy_bytes_(&object->data.application.data, data, length))
769 return false;
770 }
771 else {
772 object->data.application.data = data;
773 }
774
775 if(0 != save)
776 free(save);
777
778 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
779 return true;
780}
781
Josh Coalson6afed9f2002-10-16 22:29:47 +0000782FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000783{
784 FLAC__ASSERT(0 != object);
785 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
786
787 if(0 == object->data.seek_table.points) {
788 FLAC__ASSERT(object->data.seek_table.num_points == 0);
789 if(0 == new_num_points)
790 return true;
791 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
792 return false;
793 }
794 else {
Josh Coalsoncc682512002-06-08 04:53:42 +0000795 const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
796 const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
Josh Coalson90ced912002-05-30 05:23:38 +0000797
798 FLAC__ASSERT(object->data.seek_table.num_points > 0);
799
800 if(new_size == 0) {
801 free(object->data.seek_table.points);
802 object->data.seek_table.points = 0;
803 }
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000804 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 +0000805 return false;
806
807 /* if growing, set new elements to placeholders */
808 if(new_size > old_size) {
809 unsigned i;
810 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
811 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
812 object->data.seek_table.points[i].stream_offset = 0;
813 object->data.seek_table.points[i].frame_samples = 0;
814 }
815 }
816 }
817
818 object->data.seek_table.num_points = new_num_points;
819
820 seektable_calculate_length_(object);
821 return true;
822}
823
Josh Coalson6afed9f2002-10-16 22:29:47 +0000824FLAC_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 +0000825{
826 FLAC__ASSERT(0 != object);
827 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000828 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000829
830 object->data.seek_table.points[point_num] = point;
831}
832
Josh Coalson6afed9f2002-10-16 22:29:47 +0000833FLAC_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 +0000834{
835 int i;
836
837 FLAC__ASSERT(0 != object);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000838 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000839 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000840
841 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
842 return false;
843
844 /* move all points >= point_num forward one space */
845 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
846 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
847
848 FLAC__metadata_object_seektable_set_point(object, point_num, point);
849 seektable_calculate_length_(object);
850 return true;
851}
852
Josh Coalson6afed9f2002-10-16 22:29:47 +0000853FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
Josh Coalson90ced912002-05-30 05:23:38 +0000854{
855 unsigned i;
856
857 FLAC__ASSERT(0 != object);
858 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000859 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000860
861 /* move all points > point_num backward one space */
862 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
863 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
864
865 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
866}
867
Josh Coalson6afed9f2002-10-16 22:29:47 +0000868FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
Josh Coalson28e08d82002-06-05 05:56:41 +0000869{
Josh Coalson28e08d82002-06-05 05:56:41 +0000870 FLAC__ASSERT(0 != object);
871 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
872
Josh Coalson0833f342002-07-15 05:31:55 +0000873 return FLAC__format_seektable_is_legal(&object->data.seek_table);
Josh Coalson28e08d82002-06-05 05:56:41 +0000874}
875
Josh Coalson6afed9f2002-10-16 22:29:47 +0000876FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
Josh Coalson5a5de732002-08-01 06:37:11 +0000877{
878 FLAC__ASSERT(0 != object);
879 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
880
881 if(num > 0)
882 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
883 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
884 else
885 return true;
886}
887
Josh Coalson6afed9f2002-10-16 22:29:47 +0000888FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
Josh Coalson5a5de732002-08-01 06:37:11 +0000889{
890 FLAC__StreamMetadata_SeekTable *seek_table;
891
892 FLAC__ASSERT(0 != object);
893 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
894
895 seek_table = &object->data.seek_table;
896
897 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
898 return false;
899
900 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
901 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
902 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
903
904 return true;
905}
906
Josh Coalson6afed9f2002-10-16 22:29:47 +0000907FLAC_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 +0000908{
909 FLAC__ASSERT(0 != object);
910 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
911 FLAC__ASSERT(0 != sample_numbers || num == 0);
912
913 if(num > 0) {
914 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
915 unsigned i, j;
916
917 i = seek_table->num_points;
918
919 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
920 return false;
921
922 for(j = 0; j < num; i++, j++) {
923 seek_table->points[i].sample_number = sample_numbers[j];
924 seek_table->points[i].stream_offset = 0;
925 seek_table->points[i].frame_samples = 0;
926 }
927 }
928
929 return true;
930}
931
Josh Coalson6afed9f2002-10-16 22:29:47 +0000932FLAC_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 +0000933{
934 FLAC__ASSERT(0 != object);
935 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
936 FLAC__ASSERT(total_samples > 0);
937
Josh Coalson6b21f662006-09-13 01:42:27 +0000938 if(num > 0 && total_samples > 0) {
Josh Coalson5a5de732002-08-01 06:37:11 +0000939 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
940 unsigned i, j;
941
942 i = seek_table->num_points;
943
944 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
945 return false;
946
947 for(j = 0; j < num; i++, j++) {
948 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
949 seek_table->points[i].stream_offset = 0;
950 seek_table->points[i].frame_samples = 0;
951 }
952 }
953
954 return true;
955}
956
Josh Coalson6b21f662006-09-13 01:42:27 +0000957FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
958{
959 FLAC__ASSERT(0 != object);
960 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
961 FLAC__ASSERT(samples > 0);
962 FLAC__ASSERT(total_samples > 0);
963
964 if(samples > 0 && total_samples > 0) {
965 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
966 unsigned i, j;
967 FLAC__uint64 num, sample;
968
969 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
970 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
971 if(total_samples % samples == 0)
972 num--;
973
974 i = seek_table->num_points;
975
976 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
977 return false;
978
979 sample = 0;
980 for(j = 0; j < num; i++, j++, sample += samples) {
981 seek_table->points[i].sample_number = sample;
982 seek_table->points[i].stream_offset = 0;
983 seek_table->points[i].frame_samples = 0;
984 }
985 }
986
987 return true;
988}
989
Josh Coalson6afed9f2002-10-16 22:29:47 +0000990FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +0000991{
992 unsigned unique;
993
994 FLAC__ASSERT(0 != object);
995 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
996
997 unique = FLAC__format_seektable_sort(&object->data.seek_table);
998
999 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1000}
1001
Josh Coalson6afed9f2002-10-16 22:29:47 +00001002FLAC_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 +00001003{
Josh Coalson2de11242004-12-30 03:41:19 +00001004 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1005 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001006 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001007}
1008
Josh Coalson6afed9f2002-10-16 22:29:47 +00001009FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +00001010{
1011 FLAC__ASSERT(0 != object);
1012 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1013
1014 if(0 == object->data.vorbis_comment.comments) {
1015 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1016 if(0 == new_num_comments)
1017 return true;
1018 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1019 return false;
1020 }
1021 else {
Josh Coalsoncc682512002-06-08 04:53:42 +00001022 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1023 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001024
1025 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1026
1027 /* if shrinking, free the truncated entries */
1028 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1029 unsigned i;
1030 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1031 if(0 != object->data.vorbis_comment.comments[i].entry)
1032 free(object->data.vorbis_comment.comments[i].entry);
1033 }
1034
1035 if(new_size == 0) {
1036 free(object->data.vorbis_comment.comments);
1037 object->data.vorbis_comment.comments = 0;
1038 }
Josh Coalson5e3fcb22002-11-06 07:11:26 +00001039 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 +00001040 return false;
1041
1042 /* if growing, zero all the length/pointers of new elements */
1043 if(new_size > old_size)
1044 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1045 }
1046
1047 object->data.vorbis_comment.num_comments = new_num_comments;
1048
1049 vorbiscomment_calculate_length_(object);
1050 return true;
1051}
1052
Josh Coalson6afed9f2002-10-16 22:29:47 +00001053FLAC_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 +00001054{
Josh Coalsonfb993862003-01-15 03:18:07 +00001055 FLAC__ASSERT(0 != object);
1056 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1057
Josh Coalson2de11242004-12-30 03:41:19 +00001058 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1059 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001060 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001061}
1062
Josh Coalson6afed9f2002-10-16 22:29:47 +00001063FLAC_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 +00001064{
Josh Coalsoncc682512002-06-08 04:53:42 +00001065 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001066
1067 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001068 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001069 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001070
Josh Coalson2de11242004-12-30 03:41:19 +00001071 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1072 return false;
1073
Josh Coalson6b8e5302002-05-31 06:23:09 +00001074 vc = &object->data.vorbis_comment;
1075
1076 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001077 return false;
1078
1079 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001080 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 +00001081 vc->comments[comment_num].length = 0;
1082 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001083
1084 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1085}
1086
Josh Coalsondef597e2004-12-30 00:59:30 +00001087FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1088{
1089 FLAC__ASSERT(0 != object);
1090 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1091 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1092}
1093
1094FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1095{
1096 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
Josh Coalson2de11242004-12-30 03:41:19 +00001097
1098 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1099 return false;
1100
Josh Coalsondef597e2004-12-30 00:59:30 +00001101 {
1102 int i;
1103 unsigned field_name_length;
1104 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1105
1106 FLAC__ASSERT(0 != eq);
1107
1108 if(0 == eq)
1109 return false; /* double protection */
1110
1111 field_name_length = eq-entry.entry;
1112
Josh Coalsonb4de1692005-08-24 07:39:25 +00001113 if((i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length)) >= 0) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001114 unsigned index = (unsigned)i;
1115 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
1116 return false;
1117 if(all && (index+1 < object->data.vorbis_comment.num_comments)) {
Josh Coalsonb4de1692005-08-24 07:39:25 +00001118 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 +00001119 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
1120 return false;
1121 if((unsigned)i < object->data.vorbis_comment.num_comments)
Josh Coalsonb4de1692005-08-24 07:39:25 +00001122 i = vorbiscomment_find_entry_from_(object, (unsigned)i, (const char *)entry.entry, field_name_length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001123 else
1124 i = -1;
1125 }
1126 }
1127 return true;
1128 }
1129 else
1130 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1131 }
1132}
1133
Josh Coalson6afed9f2002-10-16 22:29:47 +00001134FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001135{
Josh Coalsoncc682512002-06-08 04:53:42 +00001136 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001137
1138 FLAC__ASSERT(0 != object);
1139 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001140 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001141
Josh Coalson6b8e5302002-05-31 06:23:09 +00001142 vc = &object->data.vorbis_comment;
1143
Josh Coalson90ced912002-05-30 05:23:38 +00001144 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +00001145 if(0 != vc->comments[comment_num].entry)
1146 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001147
1148 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001149 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 +00001150 vc->comments[vc->num_comments-1].length = 0;
1151 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001152
Josh Coalson6b8e5302002-05-31 06:23:09 +00001153 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +00001154}
Josh Coalson45bb9882002-10-26 04:34:16 +00001155
Josh Coalsondef597e2004-12-30 00:59:30 +00001156FLAC_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 +00001157{
Josh Coalsondef597e2004-12-30 00:59:30 +00001158 FLAC__ASSERT(0 != entry);
1159 FLAC__ASSERT(0 != field_name);
1160 FLAC__ASSERT(0 != field_value);
1161
Josh Coalson2de11242004-12-30 03:41:19 +00001162 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1163 return false;
Josh Coalsonb4de1692005-08-24 07:39:25 +00001164 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
Josh Coalson2de11242004-12-30 03:41:19 +00001165 return false;
1166
Josh Coalsondef597e2004-12-30 00:59:30 +00001167 {
1168 const size_t nn = strlen(field_name);
1169 const size_t nv = strlen(field_value);
1170 entry->length = nn + 1 /*=*/ + nv;
Josh Coalson80171dc2004-12-30 03:57:13 +00001171 if(0 == (entry->entry = (FLAC__byte*)malloc(entry->length+1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001172 return false;
1173 memcpy(entry->entry, field_name, nn);
1174 entry->entry[nn] = '=';
1175 memcpy(entry->entry+nn+1, field_value, nv);
1176 entry->entry[entry->length] = '\0';
1177 }
1178
1179 return true;
1180}
1181
1182FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1183{
1184 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1185 FLAC__ASSERT(0 != field_name);
1186 FLAC__ASSERT(0 != field_value);
Josh Coalson2de11242004-12-30 03:41:19 +00001187
1188 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1189 return false;
1190
Josh Coalsondef597e2004-12-30 00:59:30 +00001191 {
1192 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1193 const size_t nn = eq-entry.entry;
1194 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1195 FLAC__ASSERT(0 != eq);
1196 if(0 == eq)
1197 return false; /* double protection */
Josh Coalson80171dc2004-12-30 03:57:13 +00001198 if(0 == (*field_name = (char*)malloc(nn+1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001199 return false;
Josh Coalson80171dc2004-12-30 03:57:13 +00001200 if(0 == (*field_value = (char*)malloc(nv+1))) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001201 free(*field_name);
1202 return false;
1203 }
1204 memcpy(*field_name, entry.entry, nn);
1205 memcpy(*field_value, entry.entry+nn+1, nv);
1206 (*field_name)[nn] = '\0';
1207 (*field_value)[nv] = '\0';
1208 }
1209
1210 return true;
1211}
1212
Josh Coalson44623112005-01-30 18:15:36 +00001213FLAC_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 +00001214{
1215 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1216 {
1217 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
Josh Coalsone9a638d2005-09-03 03:54:16 +00001218#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
Josh Coalson45bb9882002-10-26 04:34:16 +00001219#define FLAC__STRNCASECMP strnicmp
1220#else
1221#define FLAC__STRNCASECMP strncasecmp
1222#endif
Josh Coalsondef597e2004-12-30 00:59:30 +00001223 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 +00001224#undef FLAC__STRNCASECMP
Josh Coalsondef597e2004-12-30 00:59:30 +00001225 }
Josh Coalson45bb9882002-10-26 04:34:16 +00001226}
1227
Josh Coalsonb667e702002-11-05 07:24:33 +00001228FLAC_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 +00001229{
Josh Coalsondef597e2004-12-30 00:59:30 +00001230 FLAC__ASSERT(0 != field_name);
Josh Coalson45bb9882002-10-26 04:34:16 +00001231
Josh Coalsondef597e2004-12-30 00:59:30 +00001232 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
Josh Coalson45bb9882002-10-26 04:34:16 +00001233}
1234
1235FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1236{
1237 const unsigned field_name_length = strlen(field_name);
1238 unsigned i;
1239
1240 FLAC__ASSERT(0 != object);
1241 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1242
1243 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001244 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 +00001245 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1246 return -1;
1247 else
1248 return 1;
1249 }
1250 }
1251
1252 return 0;
1253}
1254
1255FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1256{
1257 FLAC__bool ok = true;
1258 unsigned matching = 0;
1259 const unsigned field_name_length = strlen(field_name);
1260 int i;
1261
1262 FLAC__ASSERT(0 != object);
1263 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1264
1265 /* must delete from end to start otherwise it will interfere with our iteration */
1266 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001267 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 +00001268 matching++;
1269 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1270 }
1271 }
1272
1273 return ok? (int)matching : -1;
1274}
Josh Coalson8e9c4512002-11-14 05:00:24 +00001275
Josh Coalsondf7240a2002-11-16 06:32:30 +00001276FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new()
1277{
1278 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1279}
1280
1281FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1282{
1283 FLAC__StreamMetadata_CueSheet_Track *to;
1284
1285 FLAC__ASSERT(0 != object);
1286
1287 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1288 if(!copy_track_(to, object)) {
1289 FLAC__metadata_object_cuesheet_track_delete(to);
1290 return 0;
1291 }
1292 }
1293
1294 return to;
1295}
1296
1297void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1298{
1299 FLAC__ASSERT(0 != object);
1300
1301 if(0 != object->indices) {
1302 FLAC__ASSERT(object->num_indices > 0);
1303 free(object->indices);
1304 }
1305}
1306
1307FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1308{
1309 FLAC__metadata_object_cuesheet_track_delete_data(object);
1310 free(object);
1311}
1312
Josh Coalson8e9c4512002-11-14 05:00:24 +00001313FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1314{
1315 FLAC__StreamMetadata_CueSheet_Track *track;
1316 FLAC__ASSERT(0 != object);
1317 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1318 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1319
1320 track = &object->data.cue_sheet.tracks[track_num];
1321
1322 if(0 == track->indices) {
1323 FLAC__ASSERT(track->num_indices == 0);
1324 if(0 == new_num_indices)
1325 return true;
1326 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1327 return false;
1328 }
1329 else {
1330 const unsigned old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1331 const unsigned new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1332
1333 FLAC__ASSERT(track->num_indices > 0);
1334
1335 if(new_size == 0) {
1336 free(track->indices);
1337 track->indices = 0;
1338 }
1339 else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
1340 return false;
1341
1342 /* if growing, zero all the lengths/pointers of new elements */
1343 if(new_size > old_size)
1344 memset(track->indices + track->num_indices, 0, new_size - old_size);
1345 }
1346
1347 track->num_indices = new_num_indices;
1348
1349 cuesheet_calculate_length_(object);
1350 return true;
1351}
1352
1353FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index)
1354{
1355 FLAC__StreamMetadata_CueSheet_Track *track;
1356
1357 FLAC__ASSERT(0 != object);
1358 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1359 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1360 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1361
1362 track = &object->data.cue_sheet.tracks[track_num];
1363
1364 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1365 return false;
1366
1367 /* move all indices >= index_num forward one space */
1368 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1369
1370 track->indices[index_num] = index;
1371 cuesheet_calculate_length_(object);
1372 return true;
1373}
1374
Josh Coalson3b026e82002-11-21 06:41:01 +00001375FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1376{
1377 FLAC__StreamMetadata_CueSheet_Index index;
1378 memset(&index, 0, sizeof(index));
Josh Coalsonbfc8e312002-11-21 09:00:25 +00001379 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
Josh Coalson3b026e82002-11-21 06:41:01 +00001380}
1381
Josh Coalson8e9c4512002-11-14 05:00:24 +00001382FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1383{
1384 FLAC__StreamMetadata_CueSheet_Track *track;
1385
1386 FLAC__ASSERT(0 != object);
1387 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1388 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1389 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1390
1391 track = &object->data.cue_sheet.tracks[track_num];
1392
1393 /* move all indices > index_num backward one space */
Josh Coalson4fa90592002-12-04 07:01:37 +00001394 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 +00001395
1396 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1397 cuesheet_calculate_length_(object);
1398 return true;
1399}
1400
1401FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1402{
1403 FLAC__ASSERT(0 != object);
1404 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1405
1406 if(0 == object->data.cue_sheet.tracks) {
1407 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1408 if(0 == new_num_tracks)
1409 return true;
1410 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1411 return false;
1412 }
1413 else {
1414 const unsigned old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1415 const unsigned new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1416
1417 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1418
1419 /* if shrinking, free the truncated entries */
1420 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1421 unsigned i;
1422 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1423 if(0 != object->data.cue_sheet.tracks[i].indices)
1424 free(object->data.cue_sheet.tracks[i].indices);
1425 }
1426
1427 if(new_size == 0) {
1428 free(object->data.cue_sheet.tracks);
1429 object->data.cue_sheet.tracks = 0;
1430 }
1431 else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
1432 return false;
1433
1434 /* if growing, zero all the lengths/pointers of new elements */
1435 if(new_size > old_size)
1436 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1437 }
1438
1439 object->data.cue_sheet.num_tracks = new_num_tracks;
1440
1441 cuesheet_calculate_length_(object);
1442 return true;
1443}
1444
Josh Coalsondf7240a2002-11-16 06:32:30 +00001445FLAC_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 +00001446{
Josh Coalsonfb993862003-01-15 03:18:07 +00001447 FLAC__ASSERT(0 != object);
1448 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1449
Josh Coalsondf7240a2002-11-16 06:32:30 +00001450 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001451}
1452
Josh Coalsondf7240a2002-11-16 06:32:30 +00001453FLAC_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 +00001454{
1455 FLAC__StreamMetadata_CueSheet *cs;
1456
1457 FLAC__ASSERT(0 != object);
1458 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1459 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1460
1461 cs = &object->data.cue_sheet;
1462
1463 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1464 return false;
1465
1466 /* move all tracks >= track_num forward one space */
1467 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1468 cs->tracks[track_num].num_indices = 0;
1469 cs->tracks[track_num].indices = 0;
1470
1471 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1472}
1473
Josh Coalson3b026e82002-11-21 06:41:01 +00001474FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1475{
1476 FLAC__StreamMetadata_CueSheet_Track track;
1477 memset(&track, 0, sizeof(track));
1478 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1479}
1480
Josh Coalson8e9c4512002-11-14 05:00:24 +00001481FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1482{
1483 FLAC__StreamMetadata_CueSheet *cs;
1484
1485 FLAC__ASSERT(0 != object);
1486 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1487 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1488
1489 cs = &object->data.cue_sheet;
1490
1491 /* free the track at track_num */
1492 if(0 != cs->tracks[track_num].indices)
1493 free(cs->tracks[track_num].indices);
1494
1495 /* move all tracks > track_num backward one space */
1496 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1497 cs->tracks[cs->num_tracks-1].num_indices = 0;
1498 cs->tracks[cs->num_tracks-1].indices = 0;
1499
1500 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1501}
1502
1503FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1504{
1505 FLAC__ASSERT(0 != object);
1506 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1507
1508 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1509}
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001510
1511static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1512{
1513 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1514 return 0;
1515 else if (cs->tracks[track].indices[0].number == 1)
1516 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1517 else if (cs->tracks[track].num_indices < 2)
1518 return 0;
1519 else if (cs->tracks[track].indices[1].number == 1)
1520 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1521 else
1522 return 0;
1523}
1524
1525static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1526{
1527 FLAC__uint32 n = 0;
1528 while (x) {
1529 n += (x%10);
1530 x /= 10;
1531 }
1532 return n;
1533}
1534
1535FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1536{
1537 const FLAC__StreamMetadata_CueSheet *cs;
1538
1539 FLAC__ASSERT(0 != object);
1540 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1541
1542 cs = &object->data.cue_sheet;
1543
1544 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1545 return 0;
1546
1547 {
1548 FLAC__uint32 i, length, sum = 0;
1549 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1550 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1551 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1552
1553 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1554 }
1555}