blob: 3a613a1a8dfcefa366314e212629cce33b78b34a [file] [log] [blame]
Josh Coalson90ced912002-05-30 05:23:38 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson95643902004-01-17 04:14:43 +00002 * Copyright (C) 2001,2002,2003,2004 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
32#include <stdlib.h>
33#include <string.h>
34
35#include "private/metadata.h"
36
37#include "FLAC/assert.h"
38
39
40/****************************************************************************
41 *
42 * Local routines
43 *
44 ***************************************************************************/
45
46static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
47{
48 if(bytes > 0 && 0 != from) {
49 FLAC__byte *x;
Josh Coalson5e3fcb22002-11-06 07:11:26 +000050 if(0 == (x = (FLAC__byte*)malloc(bytes)))
Josh Coalson90ced912002-05-30 05:23:38 +000051 return false;
52 memcpy(x, from, bytes);
53 *to = x;
54 }
55 else {
56 FLAC__ASSERT(0 == from);
57 FLAC__ASSERT(bytes == 0);
58 *to = 0;
59 }
60 return true;
61}
62
Josh Coalsondef597e2004-12-30 00:59:30 +000063static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, unsigned length)
64{
65 FLAC__byte *x = (FLAC__byte*)realloc(*entry, length+1);
66 if(0 != x) {
67 x[length] = '\0';
68 *entry = x;
69 return true;
70 }
71 else
72 return false;
73}
74
Josh Coalsoncc682512002-06-08 04:53:42 +000075static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
Josh Coalson90ced912002-05-30 05:23:38 +000076{
77 to->length = from->length;
78 if(0 == from->entry) {
79 FLAC__ASSERT(from->length == 0);
80 to->entry = 0;
81 }
82 else {
83 FLAC__byte *x;
84 FLAC__ASSERT(from->length > 0);
Josh Coalsondef597e2004-12-30 00:59:30 +000085 if(0 == (x = (FLAC__byte*)malloc(from->length+1)))
Josh Coalson90ced912002-05-30 05:23:38 +000086 return false;
87 memcpy(x, from->entry, from->length);
Josh Coalsondef597e2004-12-30 00:59:30 +000088 x[from->length] = '\0';
Josh Coalson90ced912002-05-30 05:23:38 +000089 to->entry = x;
90 }
91 return true;
92}
93
Josh Coalson8e9c4512002-11-14 05:00:24 +000094static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
95{
96 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
97 if(0 == from->indices) {
98 FLAC__ASSERT(from->num_indices == 0);
99 }
100 else {
101 FLAC__StreamMetadata_CueSheet_Index *x;
102 FLAC__ASSERT(from->num_indices > 0);
103 if(0 == (x = (FLAC__StreamMetadata_CueSheet_Index*)malloc(from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index))))
104 return false;
105 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
106 to->indices = x;
107 }
108 return true;
109}
110
Josh Coalsoncc682512002-06-08 04:53:42 +0000111static void seektable_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000112{
113 FLAC__ASSERT(0 != object);
114 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
115
116 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
117}
118
Josh Coalsoncc682512002-06-08 04:53:42 +0000119static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000120{
Josh Coalsoncc682512002-06-08 04:53:42 +0000121 FLAC__StreamMetadata_SeekPoint *object_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000122
123 FLAC__ASSERT(num_points > 0);
124
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000125 object_array = (FLAC__StreamMetadata_SeekPoint*)malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
Josh Coalson90ced912002-05-30 05:23:38 +0000126
127 if(0 != object_array) {
128 unsigned i;
129 for(i = 0; i < num_points; i++) {
130 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
131 object_array[i].stream_offset = 0;
132 object_array[i].frame_samples = 0;
133 }
134 }
135
136 return object_array;
137}
138
Josh Coalsoncc682512002-06-08 04:53:42 +0000139static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000140{
141 unsigned i;
142
143 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
144
145 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
146 object->length += object->data.vorbis_comment.vendor_string.length;
147 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
148 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
149 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
150 object->length += object->data.vorbis_comment.comments[i].length;
151 }
152}
153
Josh Coalsoncc682512002-06-08 04:53:42 +0000154static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000155{
Josh Coalson90ced912002-05-30 05:23:38 +0000156 FLAC__ASSERT(num_comments > 0);
157
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000158 return (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
Josh Coalson90ced912002-05-30 05:23:38 +0000159}
160
Josh Coalsoncc682512002-06-08 04:53:42 +0000161static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000162{
163 unsigned i;
164
165 FLAC__ASSERT(0 != object_array && num_comments > 0);
166
167 for(i = 0; i < num_comments; i++)
168 if(0 != object_array[i].entry)
169 free(object_array[i].entry);
170
171 if(0 != object_array)
172 free(object_array);
173}
174
Josh Coalsoncc682512002-06-08 04:53:42 +0000175static 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 +0000176{
Josh Coalsoncc682512002-06-08 04:53:42 +0000177 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
Josh Coalson90ced912002-05-30 05:23:38 +0000178
179 FLAC__ASSERT(0 != object_array);
180 FLAC__ASSERT(num_comments > 0);
181
182 return_array = vorbiscomment_entry_array_new_(num_comments);
183
184 if(0 != return_array) {
185 unsigned i;
186
Josh Coalson90ced912002-05-30 05:23:38 +0000187 for(i = 0; i < num_comments; i++) {
188 if(!copy_vcentry_(return_array+i, object_array+i)) {
189 vorbiscomment_entry_array_delete_(return_array, num_comments);
190 return 0;
191 }
192 }
193 }
194
195 return return_array;
196}
197
Josh Coalsoncc682512002-06-08 04:53:42 +0000198static 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 +0000199{
200 FLAC__byte *save;
201
202 FLAC__ASSERT(0 != object);
203 FLAC__ASSERT(0 != dest);
204 FLAC__ASSERT(0 != src);
205 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson748459c2004-09-08 00:49:30 +0000206 FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
Josh Coalson90ced912002-05-30 05:23:38 +0000207
Josh Coalson6b8e5302002-05-31 06:23:09 +0000208 save = dest->entry;
Josh Coalson90ced912002-05-30 05:23:38 +0000209
Josh Coalsondef597e2004-12-30 00:59:30 +0000210 if(0 != src->entry && src->length > 0) {
211 if(copy) {
212 /* do the copy first so that if we fail we leave the dest object untouched */
213 if(!copy_vcentry_(dest, src))
214 return false;
215 }
216 else {
217 /* we have to make sure that the string we're taking over is null-terminated */
218
219 /*
220 * Stripping the const from src->entry is OK since we're taking
221 * ownership of the pointer. This is a hack around a deficiency
222 * in the API where the same function is used for 'copy' and
223 * 'own', but the source entry is a const pointer. If we were
224 * precise, the 'own' flavor would be a separate function with a
225 * non-const source pointer. But it's not, so we hack away.
226 */
227 if(!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
228 return false;
229 *dest = *src;
230 }
Josh Coalson90ced912002-05-30 05:23:38 +0000231 }
232 else {
Josh Coalsondef597e2004-12-30 00:59:30 +0000233 /* the src is null */
Josh Coalson90ced912002-05-30 05:23:38 +0000234 *dest = *src;
235 }
236
237 if(0 != save)
238 free(save);
239
240 vorbiscomment_calculate_length_(object);
241 return true;
242}
243
Josh Coalsondef597e2004-12-30 00:59:30 +0000244static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name, unsigned field_name_length)
245{
246 unsigned i;
247
248 FLAC__ASSERT(0 != object);
249 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
250 FLAC__ASSERT(0 != field_name);
251
252 for(i = offset; i < object->data.vorbis_comment.num_comments; i++) {
253 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
254 return (int)i;
255 }
256
257 return -1;
258}
259
Josh Coalson8e9c4512002-11-14 05:00:24 +0000260static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
261{
262 unsigned i;
263
264 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
265
266 object->length = (
267 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
268 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000269 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
Josh Coalsondf7240a2002-11-16 06:32:30 +0000270 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
Josh Coalson8e9c4512002-11-14 05:00:24 +0000271 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
272 ) / 8;
273
274 object->length += object->data.cue_sheet.num_tracks * (
275 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
276 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
277 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
278 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
279 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
280 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
281 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
282 ) / 8;
283
284 for(i = 0; i < object->data.cue_sheet.num_tracks; i++) {
285 object->length += object->data.cue_sheet.tracks[i].num_indices * (
286 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
287 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
288 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
289 ) / 8;
290 }
291}
292
293static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(unsigned num_indices)
294{
295 FLAC__ASSERT(num_indices > 0);
296
297 return (FLAC__StreamMetadata_CueSheet_Index*)calloc(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
298}
299
300static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(unsigned num_tracks)
301{
302 FLAC__ASSERT(num_tracks > 0);
303
304 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
305}
306
307static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
308{
309 unsigned i;
310
311 FLAC__ASSERT(0 != object_array && num_tracks > 0);
312
313 for(i = 0; i < num_tracks; i++) {
314 if(0 != object_array[i].indices) {
315 FLAC__ASSERT(object_array[i].num_indices > 0);
316 free(object_array[i].indices);
317 }
318 }
319
320 if(0 != object_array)
321 free(object_array);
322}
323
324static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, unsigned num_tracks)
325{
326 FLAC__StreamMetadata_CueSheet_Track *return_array;
327
328 FLAC__ASSERT(0 != object_array);
329 FLAC__ASSERT(num_tracks > 0);
330
331 return_array = cuesheet_track_array_new_(num_tracks);
332
333 if(0 != return_array) {
334 unsigned i;
335
336 for(i = 0; i < num_tracks; i++) {
337 if(!copy_track_(return_array+i, object_array+i)) {
338 cuesheet_track_array_delete_(return_array, num_tracks);
339 return 0;
340 }
341 }
342 }
343
344 return return_array;
345}
346
347static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
348{
349 FLAC__StreamMetadata_CueSheet_Index *save;
350
351 FLAC__ASSERT(0 != object);
352 FLAC__ASSERT(0 != dest);
353 FLAC__ASSERT(0 != src);
354 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
355 FLAC__ASSERT((0 != src->indices && src->num_indices > 0) || (0 == src->indices && src->num_indices == 0));
Josh Coalson8e9c4512002-11-14 05:00:24 +0000356
357 save = dest->indices;
358
359 /* do the copy first so that if we fail we leave the object untouched */
360 if(copy) {
361 if(!copy_track_(dest, src))
362 return false;
363 }
364 else {
365 *dest = *src;
366 }
367
368 if(0 != save)
369 free(save);
370
371 cuesheet_calculate_length_(object);
372 return true;
373}
374
Josh Coalson90ced912002-05-30 05:23:38 +0000375
376/****************************************************************************
377 *
378 * Metadata object routines
379 *
380 ***************************************************************************/
381
Josh Coalson6afed9f2002-10-16 22:29:47 +0000382FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
Josh Coalson90ced912002-05-30 05:23:38 +0000383{
Josh Coalson1cb23412004-07-22 01:32:00 +0000384 FLAC__StreamMetadata *object;
385
386 if(type > FLAC__MAX_METADATA_TYPE_CODE)
387 return 0;
388
389 object = (FLAC__StreamMetadata*)calloc(1, sizeof(FLAC__StreamMetadata));
Josh Coalson90ced912002-05-30 05:23:38 +0000390 if(0 != object) {
Josh Coalson90ced912002-05-30 05:23:38 +0000391 object->is_last = false;
392 object->type = type;
393 switch(type) {
394 case FLAC__METADATA_TYPE_STREAMINFO:
395 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
396 break;
397 case FLAC__METADATA_TYPE_PADDING:
Josh Coalsond0609472003-01-10 05:37:13 +0000398 /* calloc() took care of this for us:
399 object->length = 0;
400 */
Josh Coalson90ced912002-05-30 05:23:38 +0000401 break;
402 case FLAC__METADATA_TYPE_APPLICATION:
403 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
Josh Coalsond0609472003-01-10 05:37:13 +0000404 /* calloc() took care of this for us:
405 object->data.application.data = 0;
406 */
Josh Coalson90ced912002-05-30 05:23:38 +0000407 break;
408 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsond0609472003-01-10 05:37:13 +0000409 /* calloc() took care of this for us:
410 object->length = 0;
411 object->data.seek_table.num_points = 0;
412 object->data.seek_table.points = 0;
413 */
Josh Coalson90ced912002-05-30 05:23:38 +0000414 break;
415 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson45bb9882002-10-26 04:34:16 +0000416 {
417 object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
Josh Coalsondef597e2004-12-30 00:59:30 +0000418 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 +0000419 free(object);
420 return 0;
421 }
Josh Coalson8e9c4512002-11-14 05:00:24 +0000422 vorbiscomment_calculate_length_(object);
Josh Coalson45bb9882002-10-26 04:34:16 +0000423 }
Josh Coalson90ced912002-05-30 05:23:38 +0000424 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000425 case FLAC__METADATA_TYPE_CUESHEET:
426 cuesheet_calculate_length_(object);
427 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000428 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000429 /* calloc() took care of this for us:
430 object->length = 0;
431 object->data.unknown.data = 0;
432 */
433 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000434 }
435 }
436
437 return object;
438}
439
Josh Coalson6afed9f2002-10-16 22:29:47 +0000440FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000441{
Josh Coalsoncc682512002-06-08 04:53:42 +0000442 FLAC__StreamMetadata *to;
Josh Coalson90ced912002-05-30 05:23:38 +0000443
444 FLAC__ASSERT(0 != object);
445
446 if(0 != (to = FLAC__metadata_object_new(object->type))) {
447 to->is_last = object->is_last;
448 to->type = object->type;
449 to->length = object->length;
450 switch(to->type) {
451 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalsoncc682512002-06-08 04:53:42 +0000452 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
Josh Coalson90ced912002-05-30 05:23:38 +0000453 break;
454 case FLAC__METADATA_TYPE_PADDING:
455 break;
456 case FLAC__METADATA_TYPE_APPLICATION:
457 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
458 if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
459 FLAC__metadata_object_delete(to);
460 return 0;
461 }
462 break;
463 case FLAC__METADATA_TYPE_SEEKTABLE:
464 to->data.seek_table.num_points = object->data.seek_table.num_points;
Josh Coalsoncc682512002-06-08 04:53:42 +0000465 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 +0000466 FLAC__metadata_object_delete(to);
467 return 0;
468 }
469 break;
470 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000471 if(0 != to->data.vorbis_comment.vendor_string.entry) {
Josh Coalson45bb9882002-10-26 04:34:16 +0000472 free(to->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000473 to->data.vorbis_comment.vendor_string.entry = 0;
474 }
Josh Coalson90ced912002-05-30 05:23:38 +0000475 if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
476 FLAC__metadata_object_delete(to);
477 return 0;
478 }
479 if(object->data.vorbis_comment.num_comments == 0) {
480 FLAC__ASSERT(0 == object->data.vorbis_comment.comments);
481 to->data.vorbis_comment.comments = 0;
482 }
483 else {
484 FLAC__ASSERT(0 != object->data.vorbis_comment.comments);
485 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
486 if(0 == to->data.vorbis_comment.comments) {
487 FLAC__metadata_object_delete(to);
488 return 0;
489 }
490 }
491 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
492 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000493 case FLAC__METADATA_TYPE_CUESHEET:
494 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
495 if(object->data.cue_sheet.num_tracks == 0) {
496 FLAC__ASSERT(0 == object->data.cue_sheet.tracks);
497 }
498 else {
499 FLAC__ASSERT(0 != object->data.cue_sheet.tracks);
500 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
501 if(0 == to->data.cue_sheet.tracks) {
502 FLAC__metadata_object_delete(to);
503 return 0;
504 }
505 }
506 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000507 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000508 if(!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
509 FLAC__metadata_object_delete(to);
510 return 0;
511 }
512 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000513 }
514 }
515
516 return to;
517}
518
Josh Coalsoncc682512002-06-08 04:53:42 +0000519void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000520{
521 FLAC__ASSERT(0 != object);
522
523 switch(object->type) {
524 case FLAC__METADATA_TYPE_STREAMINFO:
525 case FLAC__METADATA_TYPE_PADDING:
526 break;
527 case FLAC__METADATA_TYPE_APPLICATION:
Josh Coalson4fa90592002-12-04 07:01:37 +0000528 if(0 != object->data.application.data) {
Josh Coalson90ced912002-05-30 05:23:38 +0000529 free(object->data.application.data);
Josh Coalson4fa90592002-12-04 07:01:37 +0000530 object->data.application.data = 0;
531 }
Josh Coalson90ced912002-05-30 05:23:38 +0000532 break;
533 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalson4fa90592002-12-04 07:01:37 +0000534 if(0 != object->data.seek_table.points) {
Josh Coalson90ced912002-05-30 05:23:38 +0000535 free(object->data.seek_table.points);
Josh Coalson4fa90592002-12-04 07:01:37 +0000536 object->data.seek_table.points = 0;
537 }
Josh Coalson90ced912002-05-30 05:23:38 +0000538 break;
539 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson4fa90592002-12-04 07:01:37 +0000540 if(0 != object->data.vorbis_comment.vendor_string.entry) {
Josh Coalson90ced912002-05-30 05:23:38 +0000541 free(object->data.vorbis_comment.vendor_string.entry);
Josh Coalson4fa90592002-12-04 07:01:37 +0000542 object->data.vorbis_comment.vendor_string.entry = 0;
543 }
Josh Coalson90ced912002-05-30 05:23:38 +0000544 if(0 != object->data.vorbis_comment.comments) {
545 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
546 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
547 }
548 break;
Josh Coalson8e9c4512002-11-14 05:00:24 +0000549 case FLAC__METADATA_TYPE_CUESHEET:
550 if(0 != object->data.cue_sheet.tracks) {
551 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
552 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
553 }
554 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000555 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000556 if(0 != object->data.unknown.data) {
557 free(object->data.unknown.data);
558 object->data.unknown.data = 0;
559 }
560 break;
Josh Coalson90ced912002-05-30 05:23:38 +0000561 }
562}
563
Josh Coalson6afed9f2002-10-16 22:29:47 +0000564FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
Josh Coalson90ced912002-05-30 05:23:38 +0000565{
566 FLAC__metadata_object_delete_data(object);
567 free(object);
568}
569
Josh Coalsoncc682512002-06-08 04:53:42 +0000570static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000571{
572 if(block1->min_blocksize != block2->min_blocksize)
573 return false;
574 if(block1->max_blocksize != block2->max_blocksize)
575 return false;
576 if(block1->min_framesize != block2->min_framesize)
577 return false;
578 if(block1->max_framesize != block2->max_framesize)
579 return false;
580 if(block1->sample_rate != block2->sample_rate)
581 return false;
582 if(block1->channels != block2->channels)
583 return false;
584 if(block1->bits_per_sample != block2->bits_per_sample)
585 return false;
586 if(block1->total_samples != block2->total_samples)
587 return false;
588 if(0 != memcmp(block1->md5sum, block2->md5sum, 16))
589 return false;
590 return true;
591}
592
Josh Coalsoncc682512002-06-08 04:53:42 +0000593static 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 +0000594{
595 FLAC__ASSERT(0 != block1);
596 FLAC__ASSERT(0 != block2);
597 FLAC__ASSERT(block_length >= sizeof(block1->id));
598
599 if(0 != memcmp(block1->id, block2->id, sizeof(block1->id)))
600 return false;
601 if(0 != block1->data && 0 != block2->data)
602 return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id));
603 else
604 return block1->data == block2->data;
605}
606
Josh Coalsoncc682512002-06-08 04:53:42 +0000607static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000608{
609 unsigned i;
610
611 FLAC__ASSERT(0 != block1);
612 FLAC__ASSERT(0 != block2);
613
614 if(block1->num_points != block2->num_points)
615 return false;
616
617 if(0 != block1->points && 0 != block2->points) {
618 for(i = 0; i < block1->num_points; i++) {
619 if(block1->points[i].sample_number != block2->points[i].sample_number)
620 return false;
621 if(block1->points[i].stream_offset != block2->points[i].stream_offset)
622 return false;
623 if(block1->points[i].frame_samples != block2->points[i].frame_samples)
624 return false;
625 }
626 return true;
627 }
628 else
629 return block1->points == block2->points;
630}
631
Josh Coalsoncc682512002-06-08 04:53:42 +0000632static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000633{
634 unsigned i;
635
636 if(block1->vendor_string.length != block2->vendor_string.length)
637 return false;
638
639 if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) {
640 if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length))
641 return false;
642 }
643 else if(block1->vendor_string.entry != block2->vendor_string.entry)
644 return false;
645
646 if(block1->num_comments != block2->num_comments)
647 return false;
648
649 for(i = 0; i < block1->num_comments; i++) {
650 if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) {
651 if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length))
652 return false;
653 }
654 else if(block1->comments[i].entry != block2->comments[i].entry)
655 return false;
656 }
657 return true;
658}
659
Josh Coalson8e9c4512002-11-14 05:00:24 +0000660static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
661{
662 unsigned i, j;
663
664 if(0 != strcmp(block1->media_catalog_number, block2->media_catalog_number))
665 return false;
666
667 if(block1->lead_in != block2->lead_in)
668 return false;
669
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000670 if(block1->is_cd != block2->is_cd)
671 return false;
672
Josh Coalson8e9c4512002-11-14 05:00:24 +0000673 if(block1->num_tracks != block2->num_tracks)
674 return false;
675
676 if(0 != block1->tracks && 0 != block2->tracks) {
677 FLAC__ASSERT(block1->num_tracks > 0);
678 for(i = 0; i < block1->num_tracks; i++) {
679 if(block1->tracks[i].offset != block2->tracks[i].offset)
680 return false;
681 if(block1->tracks[i].number != block2->tracks[i].number)
682 return false;
683 if(0 != memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)))
684 return false;
685 if(block1->tracks[i].type != block2->tracks[i].type)
686 return false;
687 if(block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
688 return false;
689 if(block1->tracks[i].num_indices != block2->tracks[i].num_indices)
690 return false;
691 if(0 != block1->tracks[i].indices && 0 != block2->tracks[i].indices) {
692 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
693 for(j = 0; j < block1->tracks[i].num_indices; j++) {
694 if(block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
695 return false;
696 if(block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
697 return false;
698 }
699 }
700 else if(block1->tracks[i].indices != block2->tracks[i].indices)
701 return false;
702 }
703 }
704 else if(block1->tracks != block2->tracks)
705 return false;
706 return true;
707}
708
Josh Coalsond0609472003-01-10 05:37:13 +0000709static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, unsigned block_length)
710{
711 FLAC__ASSERT(0 != block1);
712 FLAC__ASSERT(0 != block2);
713
714 if(0 != block1->data && 0 != block2->data)
715 return 0 == memcmp(block1->data, block2->data, block_length);
716 else
717 return block1->data == block2->data;
718}
719
Josh Coalson6afed9f2002-10-16 22:29:47 +0000720FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
Josh Coalson57ba6f42002-06-07 05:27:37 +0000721{
Josh Coalsond8ab3462002-07-11 05:54:05 +0000722 FLAC__ASSERT(0 != block1);
723 FLAC__ASSERT(0 != block2);
724
Josh Coalson57ba6f42002-06-07 05:27:37 +0000725 if(block1->type != block2->type) {
726 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000727 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000728 if(block1->is_last != block2->is_last) {
729 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000730 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000731 if(block1->length != block2->length) {
732 return false;
Josh Coalson5a5de732002-08-01 06:37:11 +0000733 }
Josh Coalson57ba6f42002-06-07 05:27:37 +0000734 switch(block1->type) {
735 case FLAC__METADATA_TYPE_STREAMINFO:
736 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
737 case FLAC__METADATA_TYPE_PADDING:
738 return true; /* we don't compare the padding guts */
739 case FLAC__METADATA_TYPE_APPLICATION:
740 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
741 case FLAC__METADATA_TYPE_SEEKTABLE:
742 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
743 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
744 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000745 case FLAC__METADATA_TYPE_CUESHEET:
746 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000747 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000748 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
Josh Coalson57ba6f42002-06-07 05:27:37 +0000749 }
750}
751
Josh Coalson6afed9f2002-10-16 22:29:47 +0000752FLAC_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 +0000753{
754 FLAC__byte *save;
755
Josh Coalsond8ab3462002-07-11 05:54:05 +0000756 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +0000757 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
758 FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false));
759
760 save = object->data.application.data;
761
762 /* do the copy first so that if we fail we leave the object untouched */
763 if(copy) {
764 if(!copy_bytes_(&object->data.application.data, data, length))
765 return false;
766 }
767 else {
768 object->data.application.data = data;
769 }
770
771 if(0 != save)
772 free(save);
773
774 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
775 return true;
776}
777
Josh Coalson6afed9f2002-10-16 22:29:47 +0000778FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
Josh Coalson90ced912002-05-30 05:23:38 +0000779{
780 FLAC__ASSERT(0 != object);
781 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
782
783 if(0 == object->data.seek_table.points) {
784 FLAC__ASSERT(object->data.seek_table.num_points == 0);
785 if(0 == new_num_points)
786 return true;
787 else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points)))
788 return false;
789 }
790 else {
Josh Coalsoncc682512002-06-08 04:53:42 +0000791 const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
792 const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
Josh Coalson90ced912002-05-30 05:23:38 +0000793
794 FLAC__ASSERT(object->data.seek_table.num_points > 0);
795
796 if(new_size == 0) {
797 free(object->data.seek_table.points);
798 object->data.seek_table.points = 0;
799 }
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000800 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 +0000801 return false;
802
803 /* if growing, set new elements to placeholders */
804 if(new_size > old_size) {
805 unsigned i;
806 for(i = object->data.seek_table.num_points; i < new_num_points; i++) {
807 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
808 object->data.seek_table.points[i].stream_offset = 0;
809 object->data.seek_table.points[i].frame_samples = 0;
810 }
811 }
812 }
813
814 object->data.seek_table.num_points = new_num_points;
815
816 seektable_calculate_length_(object);
817 return true;
818}
819
Josh Coalson6afed9f2002-10-16 22:29:47 +0000820FLAC_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 +0000821{
822 FLAC__ASSERT(0 != object);
823 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000824 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000825
826 object->data.seek_table.points[point_num] = point;
827}
828
Josh Coalson6afed9f2002-10-16 22:29:47 +0000829FLAC_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 +0000830{
831 int i;
832
833 FLAC__ASSERT(0 != object);
Josh Coalson6b8e5302002-05-31 06:23:09 +0000834 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000835 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000836
837 if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
838 return false;
839
840 /* move all points >= point_num forward one space */
841 for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
842 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
843
844 FLAC__metadata_object_seektable_set_point(object, point_num, point);
845 seektable_calculate_length_(object);
846 return true;
847}
848
Josh Coalson6afed9f2002-10-16 22:29:47 +0000849FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
Josh Coalson90ced912002-05-30 05:23:38 +0000850{
851 unsigned i;
852
853 FLAC__ASSERT(0 != object);
854 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
Josh Coalson8e9c4512002-11-14 05:00:24 +0000855 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
Josh Coalson90ced912002-05-30 05:23:38 +0000856
857 /* move all points > point_num backward one space */
858 for(i = point_num; i < object->data.seek_table.num_points-1; i++)
859 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
860
861 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
862}
863
Josh Coalson6afed9f2002-10-16 22:29:47 +0000864FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
Josh Coalson28e08d82002-06-05 05:56:41 +0000865{
Josh Coalson28e08d82002-06-05 05:56:41 +0000866 FLAC__ASSERT(0 != object);
867 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
868
Josh Coalson0833f342002-07-15 05:31:55 +0000869 return FLAC__format_seektable_is_legal(&object->data.seek_table);
Josh Coalson28e08d82002-06-05 05:56:41 +0000870}
871
Josh Coalson6afed9f2002-10-16 22:29:47 +0000872FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
Josh Coalson5a5de732002-08-01 06:37:11 +0000873{
874 FLAC__ASSERT(0 != object);
875 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
876
877 if(num > 0)
878 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
879 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
880 else
881 return true;
882}
883
Josh Coalson6afed9f2002-10-16 22:29:47 +0000884FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
Josh Coalson5a5de732002-08-01 06:37:11 +0000885{
886 FLAC__StreamMetadata_SeekTable *seek_table;
887
888 FLAC__ASSERT(0 != object);
889 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
890
891 seek_table = &object->data.seek_table;
892
893 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
894 return false;
895
896 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
897 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
898 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
899
900 return true;
901}
902
Josh Coalson6afed9f2002-10-16 22:29:47 +0000903FLAC_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 +0000904{
905 FLAC__ASSERT(0 != object);
906 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
907 FLAC__ASSERT(0 != sample_numbers || num == 0);
908
909 if(num > 0) {
910 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
911 unsigned i, j;
912
913 i = seek_table->num_points;
914
915 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
916 return false;
917
918 for(j = 0; j < num; i++, j++) {
919 seek_table->points[i].sample_number = sample_numbers[j];
920 seek_table->points[i].stream_offset = 0;
921 seek_table->points[i].frame_samples = 0;
922 }
923 }
924
925 return true;
926}
927
Josh Coalson6afed9f2002-10-16 22:29:47 +0000928FLAC_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 +0000929{
930 FLAC__ASSERT(0 != object);
931 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
932 FLAC__ASSERT(total_samples > 0);
933
934 if(num > 0) {
935 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
936 unsigned i, j;
937
938 i = seek_table->num_points;
939
940 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
941 return false;
942
943 for(j = 0; j < num; i++, j++) {
944 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
945 seek_table->points[i].stream_offset = 0;
946 seek_table->points[i].frame_samples = 0;
947 }
948 }
949
950 return true;
951}
952
Josh Coalson6afed9f2002-10-16 22:29:47 +0000953FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
Josh Coalson5a5de732002-08-01 06:37:11 +0000954{
955 unsigned unique;
956
957 FLAC__ASSERT(0 != object);
958 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
959
960 unique = FLAC__format_seektable_sort(&object->data.seek_table);
961
962 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
963}
964
Josh Coalson6afed9f2002-10-16 22:29:47 +0000965FLAC_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 +0000966{
Josh Coalson6b8e5302002-05-31 06:23:09 +0000967 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +0000968}
969
Josh Coalson6afed9f2002-10-16 22:29:47 +0000970FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
Josh Coalson90ced912002-05-30 05:23:38 +0000971{
972 FLAC__ASSERT(0 != object);
973 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
974
975 if(0 == object->data.vorbis_comment.comments) {
976 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
977 if(0 == new_num_comments)
978 return true;
979 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
980 return false;
981 }
982 else {
Josh Coalsoncc682512002-06-08 04:53:42 +0000983 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
984 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
Josh Coalson90ced912002-05-30 05:23:38 +0000985
986 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
987
988 /* if shrinking, free the truncated entries */
989 if(new_num_comments < object->data.vorbis_comment.num_comments) {
990 unsigned i;
991 for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
992 if(0 != object->data.vorbis_comment.comments[i].entry)
993 free(object->data.vorbis_comment.comments[i].entry);
994 }
995
996 if(new_size == 0) {
997 free(object->data.vorbis_comment.comments);
998 object->data.vorbis_comment.comments = 0;
999 }
Josh Coalson5e3fcb22002-11-06 07:11:26 +00001000 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 +00001001 return false;
1002
1003 /* if growing, zero all the length/pointers of new elements */
1004 if(new_size > old_size)
1005 memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1006 }
1007
1008 object->data.vorbis_comment.num_comments = new_num_comments;
1009
1010 vorbiscomment_calculate_length_(object);
1011 return true;
1012}
1013
Josh Coalson6afed9f2002-10-16 22:29:47 +00001014FLAC_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 +00001015{
Josh Coalsonfb993862003-01-15 03:18:07 +00001016 FLAC__ASSERT(0 != object);
1017 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1018
Josh Coalson6b8e5302002-05-31 06:23:09 +00001019 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
Josh Coalson90ced912002-05-30 05:23:38 +00001020}
1021
Josh Coalson6afed9f2002-10-16 22:29:47 +00001022FLAC_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 +00001023{
Josh Coalsoncc682512002-06-08 04:53:42 +00001024 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001025
1026 FLAC__ASSERT(0 != object);
Josh Coalson90ced912002-05-30 05:23:38 +00001027 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001028 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001029
Josh Coalson6b8e5302002-05-31 06:23:09 +00001030 vc = &object->data.vorbis_comment;
1031
1032 if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
Josh Coalson90ced912002-05-30 05:23:38 +00001033 return false;
1034
1035 /* move all comments >= comment_num forward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001036 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 +00001037 vc->comments[comment_num].length = 0;
1038 vc->comments[comment_num].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001039
1040 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1041}
1042
Josh Coalsondef597e2004-12-30 00:59:30 +00001043FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1044{
1045 FLAC__ASSERT(0 != object);
1046 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1047 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1048}
1049
1050FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1051{
1052 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1053 {
1054 int i;
1055 unsigned field_name_length;
1056 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1057
1058 FLAC__ASSERT(0 != eq);
1059
1060 if(0 == eq)
1061 return false; /* double protection */
1062
1063 field_name_length = eq-entry.entry;
1064
1065 if((i = vorbiscomment_find_entry_from_(object, 0, entry.entry, field_name_length)) >= 0) {
1066 unsigned index = (unsigned)i;
1067 if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
1068 return false;
1069 if(all && (index+1 < object->data.vorbis_comment.num_comments)) {
1070 for(i = vorbiscomment_find_entry_from_(object, index+1, entry.entry, field_name_length); i >= 0; ) {
1071 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
1072 return false;
1073 if((unsigned)i < object->data.vorbis_comment.num_comments)
1074 i = vorbiscomment_find_entry_from_(object, (unsigned)i, entry.entry, field_name_length);
1075 else
1076 i = -1;
1077 }
1078 }
1079 return true;
1080 }
1081 else
1082 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1083 }
1084}
1085
Josh Coalson6afed9f2002-10-16 22:29:47 +00001086FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
Josh Coalson90ced912002-05-30 05:23:38 +00001087{
Josh Coalsoncc682512002-06-08 04:53:42 +00001088 FLAC__StreamMetadata_VorbisComment *vc;
Josh Coalson90ced912002-05-30 05:23:38 +00001089
1090 FLAC__ASSERT(0 != object);
1091 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001092 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
Josh Coalson90ced912002-05-30 05:23:38 +00001093
Josh Coalson6b8e5302002-05-31 06:23:09 +00001094 vc = &object->data.vorbis_comment;
1095
Josh Coalson90ced912002-05-30 05:23:38 +00001096 /* free the comment at comment_num */
Josh Coalson6b8e5302002-05-31 06:23:09 +00001097 if(0 != vc->comments[comment_num].entry)
1098 free(vc->comments[comment_num].entry);
Josh Coalson90ced912002-05-30 05:23:38 +00001099
1100 /* move all comments > comment_num backward one space */
Josh Coalsoncc682512002-06-08 04:53:42 +00001101 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 +00001102 vc->comments[vc->num_comments-1].length = 0;
1103 vc->comments[vc->num_comments-1].entry = 0;
Josh Coalson90ced912002-05-30 05:23:38 +00001104
Josh Coalson6b8e5302002-05-31 06:23:09 +00001105 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
Josh Coalson90ced912002-05-30 05:23:38 +00001106}
Josh Coalson45bb9882002-10-26 04:34:16 +00001107
Josh Coalsondef597e2004-12-30 00:59:30 +00001108FLAC_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 +00001109{
Josh Coalsondef597e2004-12-30 00:59:30 +00001110 FLAC__ASSERT(0 != entry);
1111 FLAC__ASSERT(0 != field_name);
1112 FLAC__ASSERT(0 != field_value);
1113
1114 {
1115 const size_t nn = strlen(field_name);
1116 const size_t nv = strlen(field_value);
1117 entry->length = nn + 1 /*=*/ + nv;
1118 if(0 == (entry->entry = malloc(entry->length+1)))
1119 return false;
1120 memcpy(entry->entry, field_name, nn);
1121 entry->entry[nn] = '=';
1122 memcpy(entry->entry+nn+1, field_value, nv);
1123 entry->entry[entry->length] = '\0';
1124 }
1125
1126 return true;
1127}
1128
1129FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry, char **field_name, char **field_value)
1130{
1131 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1132 FLAC__ASSERT(0 != field_name);
1133 FLAC__ASSERT(0 != field_value);
1134 {
1135 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1136 const size_t nn = eq-entry.entry;
1137 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1138 FLAC__ASSERT(0 != eq);
1139 if(0 == eq)
1140 return false; /* double protection */
1141 if(0 == (*field_name = malloc(nn+1)))
1142 return false;
1143 if(0 == (*field_value = malloc(nv+1))) {
1144 free(*field_name);
1145 return false;
1146 }
1147 memcpy(*field_name, entry.entry, nn);
1148 memcpy(*field_value, entry.entry+nn+1, nv);
1149 (*field_name)[nn] = '\0';
1150 (*field_value)[nv] = '\0';
1151 }
1152
1153 return true;
1154}
1155
1156FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
1157{
1158 FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1159 {
1160 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
Josh Coalson45bb9882002-10-26 04:34:16 +00001161#if defined _MSC_VER || defined __MINGW32__
1162#define FLAC__STRNCASECMP strnicmp
1163#else
1164#define FLAC__STRNCASECMP strncasecmp
1165#endif
Josh Coalsondef597e2004-12-30 00:59:30 +00001166 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 +00001167#undef FLAC__STRNCASECMP
Josh Coalsondef597e2004-12-30 00:59:30 +00001168 }
Josh Coalson45bb9882002-10-26 04:34:16 +00001169}
1170
Josh Coalsonb667e702002-11-05 07:24:33 +00001171FLAC_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 +00001172{
Josh Coalsondef597e2004-12-30 00:59:30 +00001173 FLAC__ASSERT(0 != field_name);
Josh Coalson45bb9882002-10-26 04:34:16 +00001174
Josh Coalsondef597e2004-12-30 00:59:30 +00001175 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
Josh Coalson45bb9882002-10-26 04:34:16 +00001176}
1177
1178FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1179{
1180 const unsigned field_name_length = strlen(field_name);
1181 unsigned i;
1182
1183 FLAC__ASSERT(0 != object);
1184 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1185
1186 for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001187 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 +00001188 if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1189 return -1;
1190 else
1191 return 1;
1192 }
1193 }
1194
1195 return 0;
1196}
1197
1198FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1199{
1200 FLAC__bool ok = true;
1201 unsigned matching = 0;
1202 const unsigned field_name_length = strlen(field_name);
1203 int i;
1204
1205 FLAC__ASSERT(0 != object);
1206 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1207
1208 /* must delete from end to start otherwise it will interfere with our iteration */
1209 for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001210 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 +00001211 matching++;
1212 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1213 }
1214 }
1215
1216 return ok? (int)matching : -1;
1217}
Josh Coalson8e9c4512002-11-14 05:00:24 +00001218
Josh Coalsondf7240a2002-11-16 06:32:30 +00001219FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new()
1220{
1221 return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1222}
1223
1224FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1225{
1226 FLAC__StreamMetadata_CueSheet_Track *to;
1227
1228 FLAC__ASSERT(0 != object);
1229
1230 if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1231 if(!copy_track_(to, object)) {
1232 FLAC__metadata_object_cuesheet_track_delete(to);
1233 return 0;
1234 }
1235 }
1236
1237 return to;
1238}
1239
1240void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1241{
1242 FLAC__ASSERT(0 != object);
1243
1244 if(0 != object->indices) {
1245 FLAC__ASSERT(object->num_indices > 0);
1246 free(object->indices);
1247 }
1248}
1249
1250FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1251{
1252 FLAC__metadata_object_cuesheet_track_delete_data(object);
1253 free(object);
1254}
1255
Josh Coalson8e9c4512002-11-14 05:00:24 +00001256FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1257{
1258 FLAC__StreamMetadata_CueSheet_Track *track;
1259 FLAC__ASSERT(0 != object);
1260 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1261 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1262
1263 track = &object->data.cue_sheet.tracks[track_num];
1264
1265 if(0 == track->indices) {
1266 FLAC__ASSERT(track->num_indices == 0);
1267 if(0 == new_num_indices)
1268 return true;
1269 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1270 return false;
1271 }
1272 else {
1273 const unsigned old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1274 const unsigned new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1275
1276 FLAC__ASSERT(track->num_indices > 0);
1277
1278 if(new_size == 0) {
1279 free(track->indices);
1280 track->indices = 0;
1281 }
1282 else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
1283 return false;
1284
1285 /* if growing, zero all the lengths/pointers of new elements */
1286 if(new_size > old_size)
1287 memset(track->indices + track->num_indices, 0, new_size - old_size);
1288 }
1289
1290 track->num_indices = new_num_indices;
1291
1292 cuesheet_calculate_length_(object);
1293 return true;
1294}
1295
1296FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num, FLAC__StreamMetadata_CueSheet_Index index)
1297{
1298 FLAC__StreamMetadata_CueSheet_Track *track;
1299
1300 FLAC__ASSERT(0 != object);
1301 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1302 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1303 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1304
1305 track = &object->data.cue_sheet.tracks[track_num];
1306
1307 if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1308 return false;
1309
1310 /* move all indices >= index_num forward one space */
1311 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1312
1313 track->indices[index_num] = index;
1314 cuesheet_calculate_length_(object);
1315 return true;
1316}
1317
Josh Coalson3b026e82002-11-21 06:41:01 +00001318FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1319{
1320 FLAC__StreamMetadata_CueSheet_Index index;
1321 memset(&index, 0, sizeof(index));
Josh Coalsonbfc8e312002-11-21 09:00:25 +00001322 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
Josh Coalson3b026e82002-11-21 06:41:01 +00001323}
1324
Josh Coalson8e9c4512002-11-14 05:00:24 +00001325FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1326{
1327 FLAC__StreamMetadata_CueSheet_Track *track;
1328
1329 FLAC__ASSERT(0 != object);
1330 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1331 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1332 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1333
1334 track = &object->data.cue_sheet.tracks[track_num];
1335
1336 /* move all indices > index_num backward one space */
Josh Coalson4fa90592002-12-04 07:01:37 +00001337 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 +00001338
1339 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1340 cuesheet_calculate_length_(object);
1341 return true;
1342}
1343
1344FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1345{
1346 FLAC__ASSERT(0 != object);
1347 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1348
1349 if(0 == object->data.cue_sheet.tracks) {
1350 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1351 if(0 == new_num_tracks)
1352 return true;
1353 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1354 return false;
1355 }
1356 else {
1357 const unsigned old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1358 const unsigned new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1359
1360 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1361
1362 /* if shrinking, free the truncated entries */
1363 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1364 unsigned i;
1365 for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1366 if(0 != object->data.cue_sheet.tracks[i].indices)
1367 free(object->data.cue_sheet.tracks[i].indices);
1368 }
1369
1370 if(new_size == 0) {
1371 free(object->data.cue_sheet.tracks);
1372 object->data.cue_sheet.tracks = 0;
1373 }
1374 else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
1375 return false;
1376
1377 /* if growing, zero all the lengths/pointers of new elements */
1378 if(new_size > old_size)
1379 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1380 }
1381
1382 object->data.cue_sheet.num_tracks = new_num_tracks;
1383
1384 cuesheet_calculate_length_(object);
1385 return true;
1386}
1387
Josh Coalsondf7240a2002-11-16 06:32:30 +00001388FLAC_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 +00001389{
Josh Coalsonfb993862003-01-15 03:18:07 +00001390 FLAC__ASSERT(0 != object);
1391 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1392
Josh Coalsondf7240a2002-11-16 06:32:30 +00001393 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
Josh Coalson8e9c4512002-11-14 05:00:24 +00001394}
1395
Josh Coalsondf7240a2002-11-16 06:32:30 +00001396FLAC_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 +00001397{
1398 FLAC__StreamMetadata_CueSheet *cs;
1399
1400 FLAC__ASSERT(0 != object);
1401 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1402 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1403
1404 cs = &object->data.cue_sheet;
1405
1406 if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1407 return false;
1408
1409 /* move all tracks >= track_num forward one space */
1410 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1411 cs->tracks[track_num].num_indices = 0;
1412 cs->tracks[track_num].indices = 0;
1413
1414 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1415}
1416
Josh Coalson3b026e82002-11-21 06:41:01 +00001417FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1418{
1419 FLAC__StreamMetadata_CueSheet_Track track;
1420 memset(&track, 0, sizeof(track));
1421 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1422}
1423
Josh Coalson8e9c4512002-11-14 05:00:24 +00001424FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1425{
1426 FLAC__StreamMetadata_CueSheet *cs;
1427
1428 FLAC__ASSERT(0 != object);
1429 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1430 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1431
1432 cs = &object->data.cue_sheet;
1433
1434 /* free the track at track_num */
1435 if(0 != cs->tracks[track_num].indices)
1436 free(cs->tracks[track_num].indices);
1437
1438 /* move all tracks > track_num backward one space */
1439 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1440 cs->tracks[cs->num_tracks-1].num_indices = 0;
1441 cs->tracks[cs->num_tracks-1].indices = 0;
1442
1443 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1444}
1445
1446FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1447{
1448 FLAC__ASSERT(0 != object);
1449 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1450
1451 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1452}