blob: 521a49b689886009b77add144c23d098496624f5 [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
938 if(num > 0) {
939 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 Coalson6afed9f2002-10-16 22:29:47 +0000957FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +0000958{
959 unsigned unique;
960
961 FLAC__ASSERT(0 != object);
962 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
963
964 unique = FLAC__format_seektable_sort(&object->data.seek_table);
965
966 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
967}
968
Josh Coalson6afed9f2002-10-16 22:29:47 +0000969FLAC_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 +0000970{
Josh Coalson2de11242004-12-30 03:41:19 +0000971 if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
972 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +0000973 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +0000974}
975
Josh Coalson6afed9f2002-10-16 22:29:47 +0000976FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000977{
978 FLAC__ASSERT(0 != object);
979 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
980
981 if(0 == object->data.vorbis_comment.comments) {
982 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
983 if(0 == new_num_comments)
984 return true;
985 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
986 return false;
987 }
988 else {
Josh Coalsoncc682512002-06-08 04:53:42 +0000989 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
990 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
Josh Coalson90ced912002-05-30 05:23:38 +0000991
992 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
993
994 /* if shrinking, free the truncated entries */
995 if(new_num_comments < object->data.vorbis_comment.num_comments) {
996 unsigned i;
997 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
998 if(0 != object->data.vorbis_comment.comments[i].entry)
999 free(object->data.vorbis_comment.comments[i].entry);
1000 }
1001
1002 if(new_size == 0) {
1003 free(object->data.vorbis_comment.comments);
1004 object->data.vorbis_comment.comments = 0;
1005 }
Josh Coalson5e3fcb22002-11-06 07:11:26 +00001006 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 +00001007 return false;
1008
1009 /* if growing, zero all the length/pointers of new elements */
1010 if(new_size > old_size)
1011 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1012 }
1013
1014 object->data.vorbis_comment.num_comments = new_num_comments;
1015
1016 vorbiscomment_calculate_length_(object);
1017 return true;
1018}
1019
Josh Coalson6afed9f2002-10-16 22:29:47 +00001020FLAC_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 +00001021{
Josh Coalsonfb993862003-01-15 03:18:07 +00001022 FLAC__ASSERT(0 != object);
1023 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1024
Josh Coalson2de11242004-12-30 03:41:19 +00001025 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1026 return false;
Josh Coalson6b8e5302002-05-31 06:23:09 +00001027 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001028}
1029
Josh Coalson6afed9f2002-10-16 22:29:47 +00001030FLAC_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 +00001031{
Josh Coalsoncc682512002-06-08 04:53:42 +00001032 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001033
1034 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001035 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001036 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001037
Josh Coalson2de11242004-12-30 03:41:19 +00001038 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1039 return false;
1040
Josh Coalson6b8e5302002-05-31 06:23:09 +00001041 vc = &object->data.vorbis_comment;
1042
1043 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001044 return false;
1045
1046 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001047 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 +00001048 vc->comments[comment_num].length = 0;
1049 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001050
1051 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1052}
1053
Josh Coalsondef597e2004-12-30 00:59:30 +00001054FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1055{
1056 FLAC__ASSERT(0 != object);
1057 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1058 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1059}
1060
1061FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1062{
1063 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
Josh Coalson2de11242004-12-30 03:41:19 +00001064
1065 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1066 return false;
1067
Josh Coalsondef597e2004-12-30 00:59:30 +00001068 {
1069 int i;
1070 unsigned field_name_length;
1071 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1072
1073 FLAC__ASSERT(0 != eq);
1074
1075 if(0 == eq)
1076 return false; /* double protection */
1077
1078 field_name_length = eq-entry.entry;
1079
Josh Coalsonb4de1692005-08-24 07:39:25 +00001080 if((i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length)) >= 0) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001081 unsigned index = (unsigned)i;
1082 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
1083 return false;
1084 if(all && (index+1 < object->data.vorbis_comment.num_comments)) {
Josh Coalsonb4de1692005-08-24 07:39:25 +00001085 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 +00001086 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
1087 return false;
1088 if((unsigned)i < object->data.vorbis_comment.num_comments)
Josh Coalsonb4de1692005-08-24 07:39:25 +00001089 i = vorbiscomment_find_entry_from_(object, (unsigned)i, (const char *)entry.entry, field_name_length);
Josh Coalsondef597e2004-12-30 00:59:30 +00001090 else
1091 i = -1;
1092 }
1093 }
1094 return true;
1095 }
1096 else
1097 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1098 }
1099}
1100
Josh Coalson6afed9f2002-10-16 22:29:47 +00001101FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001102{
Josh Coalsoncc682512002-06-08 04:53:42 +00001103 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001104
1105 FLAC__ASSERT(0 != object);
1106 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001107 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001108
Josh Coalson6b8e5302002-05-31 06:23:09 +00001109 vc = &object->data.vorbis_comment;
1110
Josh Coalson90ced912002-05-30 05:23:38 +00001111 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +00001112 if(0 != vc->comments[comment_num].entry)
1113 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001114
1115 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001116 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 +00001117 vc->comments[vc->num_comments-1].length = 0;
1118 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001119
Josh Coalson6b8e5302002-05-31 06:23:09 +00001120 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +00001121}
Josh Coalson45bb9882002-10-26 04:34:16 +00001122
Josh Coalsondef597e2004-12-30 00:59:30 +00001123FLAC_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 +00001124{
Josh Coalsondef597e2004-12-30 00:59:30 +00001125 FLAC__ASSERT(0 != entry);
1126 FLAC__ASSERT(0 != field_name);
1127 FLAC__ASSERT(0 != field_value);
1128
Josh Coalson2de11242004-12-30 03:41:19 +00001129 if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1130 return false;
Josh Coalsonb4de1692005-08-24 07:39:25 +00001131 if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
Josh Coalson2de11242004-12-30 03:41:19 +00001132 return false;
1133
Josh Coalsondef597e2004-12-30 00:59:30 +00001134 {
1135 const size_t nn = strlen(field_name);
1136 const size_t nv = strlen(field_value);
1137 entry->length = nn + 1 /*=*/ + nv;
Josh Coalson80171dc2004-12-30 03:57:13 +00001138 if(0 == (entry->entry = (FLAC__byte*)malloc(entry->length+1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001139 return false;
1140 memcpy(entry->entry, field_name, nn);
1141 entry->entry[nn] = '=';
1142 memcpy(entry->entry+nn+1, field_value, nv);
1143 entry->entry[entry->length] = '\0';
1144 }
1145
1146 return true;
1147}
1148
1149FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1150{
1151 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1152 FLAC__ASSERT(0 != field_name);
1153 FLAC__ASSERT(0 != field_value);
Josh Coalson2de11242004-12-30 03:41:19 +00001154
1155 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1156 return false;
1157
Josh Coalsondef597e2004-12-30 00:59:30 +00001158 {
1159 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1160 const size_t nn = eq-entry.entry;
1161 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1162 FLAC__ASSERT(0 != eq);
1163 if(0 == eq)
1164 return false; /* double protection */
Josh Coalson80171dc2004-12-30 03:57:13 +00001165 if(0 == (*field_name = (char*)malloc(nn+1)))
Josh Coalsondef597e2004-12-30 00:59:30 +00001166 return false;
Josh Coalson80171dc2004-12-30 03:57:13 +00001167 if(0 == (*field_value = (char*)malloc(nv+1))) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001168 free(*field_name);
1169 return false;
1170 }
1171 memcpy(*field_name, entry.entry, nn);
1172 memcpy(*field_value, entry.entry+nn+1, nv);
1173 (*field_name)[nn] = '\0';
1174 (*field_value)[nv] = '\0';
1175 }
1176
1177 return true;
1178}
1179
Josh Coalson44623112005-01-30 18:15:36 +00001180FLAC_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 +00001181{
1182 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1183 {
1184 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
Josh Coalsone9a638d2005-09-03 03:54:16 +00001185#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
Josh Coalson45bb9882002-10-26 04:34:16 +00001186#define FLAC__STRNCASECMP strnicmp
1187#else
1188#define FLAC__STRNCASECMP strncasecmp
1189#endif
Josh Coalsondef597e2004-12-30 00:59:30 +00001190 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 +00001191#undef FLAC__STRNCASECMP
Josh Coalsondef597e2004-12-30 00:59:30 +00001192 }
Josh Coalson45bb9882002-10-26 04:34:16 +00001193}
1194
Josh Coalsonb667e702002-11-05 07:24:33 +00001195FLAC_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 +00001196{
Josh Coalsondef597e2004-12-30 00:59:30 +00001197 FLAC__ASSERT(0 != field_name);
Josh Coalson45bb9882002-10-26 04:34:16 +00001198
Josh Coalsondef597e2004-12-30 00:59:30 +00001199 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
Josh Coalson45bb9882002-10-26 04:34:16 +00001200}
1201
1202FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1203{
1204 const unsigned field_name_length = strlen(field_name);
1205 unsigned i;
1206
1207 FLAC__ASSERT(0 != object);
1208 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1209
1210 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001211 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 +00001212 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1213 return -1;
1214 else
1215 return 1;
1216 }
1217 }
1218
1219 return 0;
1220}
1221
1222FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1223{
1224 FLAC__bool ok = true;
1225 unsigned matching = 0;
1226 const unsigned field_name_length = strlen(field_name);
1227 int i;
1228
1229 FLAC__ASSERT(0 != object);
1230 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1231
1232 /* must delete from end to start otherwise it will interfere with our iteration */
1233 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001234 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 +00001235 matching++;
1236 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1237 }
1238 }
1239
1240 return ok? (int)matching : -1;
1241}
Josh Coalson8e9c4512002-11-14 05:00:24 +00001242
Josh Coalsondf7240a2002-11-16 06:32:30 +00001243FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new()
1244{
1245 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1246}
1247
1248FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1249{
1250 FLAC__StreamMetadata_CueSheet_Track *to;
1251
1252 FLAC__ASSERT(0 != object);
1253
1254 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1255 if(!copy_track_(to, object)) {
1256 FLAC__metadata_object_cuesheet_track_delete(to);
1257 return 0;
1258 }
1259 }
1260
1261 return to;
1262}
1263
1264void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1265{
1266 FLAC__ASSERT(0 != object);
1267
1268 if(0 != object->indices) {
1269 FLAC__ASSERT(object->num_indices > 0);
1270 free(object->indices);
1271 }
1272}
1273
1274FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1275{
1276 FLAC__metadata_object_cuesheet_track_delete_data(object);
1277 free(object);
1278}
1279
Josh Coalson8e9c4512002-11-14 05:00:24 +00001280FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1281{
1282 FLAC__StreamMetadata_CueSheet_Track *track;
1283 FLAC__ASSERT(0 != object);
1284 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1285 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1286
1287 track = &object->data.cue_sheet.tracks[track_num];
1288
1289 if(0 == track->indices) {
1290 FLAC__ASSERT(track->num_indices == 0);
1291 if(0 == new_num_indices)
1292 return true;
1293 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1294 return false;
1295 }
1296 else {
1297 const unsigned old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1298 const unsigned new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1299
1300 FLAC__ASSERT(track->num_indices > 0);
1301
1302 if(new_size == 0) {
1303 free(track->indices);
1304 track->indices = 0;
1305 }
1306 else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
1307 return false;
1308
1309 /* if growing, zero all the lengths/pointers of new elements */
1310 if(new_size > old_size)
1311 memset(track->indices + track->num_indices, 0, new_size - old_size);
1312 }
1313
1314 track->num_indices = new_num_indices;
1315
1316 cuesheet_calculate_length_(object);
1317 return true;
1318}
1319
1320FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index)
1321{
1322 FLAC__StreamMetadata_CueSheet_Track *track;
1323
1324 FLAC__ASSERT(0 != object);
1325 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1326 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1327 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1328
1329 track = &object->data.cue_sheet.tracks[track_num];
1330
1331 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1332 return false;
1333
1334 /* move all indices >= index_num forward one space */
1335 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1336
1337 track->indices[index_num] = index;
1338 cuesheet_calculate_length_(object);
1339 return true;
1340}
1341
Josh Coalson3b026e82002-11-21 06:41:01 +00001342FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1343{
1344 FLAC__StreamMetadata_CueSheet_Index index;
1345 memset(&index, 0, sizeof(index));
Josh Coalsonbfc8e312002-11-21 09:00:25 +00001346 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
Josh Coalson3b026e82002-11-21 06:41:01 +00001347}
1348
Josh Coalson8e9c4512002-11-14 05:00:24 +00001349FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1350{
1351 FLAC__StreamMetadata_CueSheet_Track *track;
1352
1353 FLAC__ASSERT(0 != object);
1354 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1355 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1356 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1357
1358 track = &object->data.cue_sheet.tracks[track_num];
1359
1360 /* move all indices > index_num backward one space */
Josh Coalson4fa90592002-12-04 07:01:37 +00001361 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 +00001362
1363 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1364 cuesheet_calculate_length_(object);
1365 return true;
1366}
1367
1368FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1369{
1370 FLAC__ASSERT(0 != object);
1371 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1372
1373 if(0 == object->data.cue_sheet.tracks) {
1374 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1375 if(0 == new_num_tracks)
1376 return true;
1377 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1378 return false;
1379 }
1380 else {
1381 const unsigned old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1382 const unsigned new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1383
1384 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1385
1386 /* if shrinking, free the truncated entries */
1387 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1388 unsigned i;
1389 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1390 if(0 != object->data.cue_sheet.tracks[i].indices)
1391 free(object->data.cue_sheet.tracks[i].indices);
1392 }
1393
1394 if(new_size == 0) {
1395 free(object->data.cue_sheet.tracks);
1396 object->data.cue_sheet.tracks = 0;
1397 }
1398 else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
1399 return false;
1400
1401 /* if growing, zero all the lengths/pointers of new elements */
1402 if(new_size > old_size)
1403 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1404 }
1405
1406 object->data.cue_sheet.num_tracks = new_num_tracks;
1407
1408 cuesheet_calculate_length_(object);
1409 return true;
1410}
1411
Josh Coalsondf7240a2002-11-16 06:32:30 +00001412FLAC_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 +00001413{
Josh Coalsonfb993862003-01-15 03:18:07 +00001414 FLAC__ASSERT(0 != object);
1415 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1416
Josh Coalsondf7240a2002-11-16 06:32:30 +00001417 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001418}
1419
Josh Coalsondf7240a2002-11-16 06:32:30 +00001420FLAC_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 +00001421{
1422 FLAC__StreamMetadata_CueSheet *cs;
1423
1424 FLAC__ASSERT(0 != object);
1425 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1426 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1427
1428 cs = &object->data.cue_sheet;
1429
1430 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1431 return false;
1432
1433 /* move all tracks >= track_num forward one space */
1434 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1435 cs->tracks[track_num].num_indices = 0;
1436 cs->tracks[track_num].indices = 0;
1437
1438 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1439}
1440
Josh Coalson3b026e82002-11-21 06:41:01 +00001441FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1442{
1443 FLAC__StreamMetadata_CueSheet_Track track;
1444 memset(&track, 0, sizeof(track));
1445 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1446}
1447
Josh Coalson8e9c4512002-11-14 05:00:24 +00001448FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1449{
1450 FLAC__StreamMetadata_CueSheet *cs;
1451
1452 FLAC__ASSERT(0 != object);
1453 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1454 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1455
1456 cs = &object->data.cue_sheet;
1457
1458 /* free the track at track_num */
1459 if(0 != cs->tracks[track_num].indices)
1460 free(cs->tracks[track_num].indices);
1461
1462 /* move all tracks > track_num backward one space */
1463 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1464 cs->tracks[cs->num_tracks-1].num_indices = 0;
1465 cs->tracks[cs->num_tracks-1].indices = 0;
1466
1467 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1468}
1469
1470FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1471{
1472 FLAC__ASSERT(0 != object);
1473 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1474
1475 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1476}
Josh Coalson7cfac0b2006-04-10 05:37:34 +00001477
1478static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1479{
1480 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1481 return 0;
1482 else if (cs->tracks[track].indices[0].number == 1)
1483 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1484 else if (cs->tracks[track].num_indices < 2)
1485 return 0;
1486 else if (cs->tracks[track].indices[1].number == 1)
1487 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1488 else
1489 return 0;
1490}
1491
1492static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1493{
1494 FLAC__uint32 n = 0;
1495 while (x) {
1496 n += (x%10);
1497 x /= 10;
1498 }
1499 return n;
1500}
1501
1502FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1503{
1504 const FLAC__StreamMetadata_CueSheet *cs;
1505
1506 FLAC__ASSERT(0 != object);
1507 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1508
1509 cs = &object->data.cue_sheet;
1510
1511 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1512 return 0;
1513
1514 {
1515 FLAC__uint32 i, length, sum = 0;
1516 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1517 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1518 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1519
1520 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1521 }
1522}