bump version number to 1.1.3
[flac.git] / src / libFLAC / metadata_object.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2001,2002,2003,2004,2005,2006  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - 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.
30  */
31
32 #if HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #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
50 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes)
51 {
52         if(bytes > 0 && 0 != from) {
53                 FLAC__byte *x;
54                 if(0 == (x = (FLAC__byte*)malloc(bytes)))
55                         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
67 static 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
79 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
80 {
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);
89                 if(0 == (x = (FLAC__byte*)malloc(from->length+1)))
90                         return false;
91                 memcpy(x, from->entry, from->length);
92                 x[from->length] = '\0';
93                 to->entry = x;
94         }
95         return true;
96 }
97
98 static 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
115 static void seektable_calculate_length_(FLAC__StreamMetadata *object)
116 {
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
123 static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points)
124 {
125         FLAC__StreamMetadata_SeekPoint *object_array;
126
127         FLAC__ASSERT(num_points > 0);
128
129         object_array = (FLAC__StreamMetadata_SeekPoint*)malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
130
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
143 static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
144 {
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
158 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments)
159 {
160         FLAC__ASSERT(num_comments > 0);
161
162         return (FLAC__StreamMetadata_VorbisComment_Entry*)calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
163 }
164
165 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
166 {
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
179 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments)
180 {
181         FLAC__StreamMetadata_VorbisComment_Entry *return_array;
182
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
191                 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
202 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
203 {
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);
210         FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0));
211
212         save = dest->entry;
213
214         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                 }
235         }
236         else {
237                 /* the src is null */
238                 *dest = *src;
239         }
240
241         if(0 != save)
242                 free(save);
243
244         vorbiscomment_calculate_length_(object);
245         return true;
246 }
247
248 static 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
264 static 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 +
273                 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
274                 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
275                 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
297 static 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
304 static 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
311 static 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
328 static 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
351 static 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));
360
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
379
380 /****************************************************************************
381  *
382  * Metadata object routines
383  *
384  ***************************************************************************/
385
386 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
387 {
388         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));
394         if(0 != object) {
395                 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:
402                                 /* calloc() took care of this for us:
403                                 object->length = 0;
404                                 */
405                                 break;
406                         case FLAC__METADATA_TYPE_APPLICATION:
407                                 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
408                                 /* calloc() took care of this for us:
409                                 object->data.application.data = 0;
410                                 */
411                                 break;
412                         case FLAC__METADATA_TYPE_SEEKTABLE:
413                                 /* 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                                 */
418                                 break;
419                         case FLAC__METADATA_TYPE_VORBIS_COMMENT:
420                                 {
421                                         object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING);
422                                         if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
423                                                 free(object);
424                                                 return 0;
425                                         }
426                                         vorbiscomment_calculate_length_(object);
427                                 }
428                                 break;
429                         case FLAC__METADATA_TYPE_CUESHEET:
430                                 cuesheet_calculate_length_(object);
431                                 break;
432                         default:
433                                 /* calloc() took care of this for us:
434                                 object->length = 0;
435                                 object->data.unknown.data = 0;
436                                 */
437                                 break;
438                 }
439         }
440
441         return object;
442 }
443
444 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
445 {
446         FLAC__StreamMetadata *to;
447
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:
456                                 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
457                                 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;
469                                 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))) {
470                                         FLAC__metadata_object_delete(to);
471                                         return 0;
472                                 }
473                                 break;
474                         case FLAC__METADATA_TYPE_VORBIS_COMMENT:
475                                 if(0 != to->data.vorbis_comment.vendor_string.entry) {
476                                         free(to->data.vorbis_comment.vendor_string.entry);
477                                         to->data.vorbis_comment.vendor_string.entry = 0;
478                                 }
479                                 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;
497                         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;
511                         default:
512                                 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;
517                 }
518         }
519
520         return to;
521 }
522
523 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
524 {
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:
532                         if(0 != object->data.application.data) {
533                                 free(object->data.application.data);
534                                 object->data.application.data = 0;
535                         }
536                         break;
537                 case FLAC__METADATA_TYPE_SEEKTABLE:
538                         if(0 != object->data.seek_table.points) {
539                                 free(object->data.seek_table.points);
540                                 object->data.seek_table.points = 0;
541                         }
542                         break;
543                 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
544                         if(0 != object->data.vorbis_comment.vendor_string.entry) {
545                                 free(object->data.vorbis_comment.vendor_string.entry);
546                                 object->data.vorbis_comment.vendor_string.entry = 0;
547                         }
548                         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;
553                 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;
559                 default:
560                         if(0 != object->data.unknown.data) {
561                                 free(object->data.unknown.data);
562                                 object->data.unknown.data = 0;
563                         }
564                         break;
565         }
566 }
567
568 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
569 {
570         FLAC__metadata_object_delete_data(object);
571         free(object);
572 }
573
574 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
575 {
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
597 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length)
598 {
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
611 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
612 {
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
636 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
637 {
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
664 static 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
674         if(block1->is_cd != block2->is_cd)
675                 return false;
676
677         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
713 static 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
724 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
725 {
726         FLAC__ASSERT(0 != block1);
727         FLAC__ASSERT(0 != block2);
728
729         if(block1->type != block2->type) {
730                 return false;
731         }
732         if(block1->is_last != block2->is_last) {
733                 return false;
734         }
735         if(block1->length != block2->length) {
736                 return false;
737         }
738         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);
749                 case FLAC__METADATA_TYPE_CUESHEET:
750                         return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
751                 default:
752                         return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
753         }
754 }
755
756 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy)
757 {
758         FLAC__byte *save;
759
760         FLAC__ASSERT(0 != object);
761         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
782 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points)
783 {
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 {
795                 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);
797
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                 }
804                 else if(0 == (object->data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(object->data.seek_table.points, new_size)))
805                         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
824 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
825 {
826         FLAC__ASSERT(0 != object);
827         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
828         FLAC__ASSERT(point_num < object->data.seek_table.num_points);
829
830         object->data.seek_table.points[point_num] = point;
831 }
832
833 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point)
834 {
835         int i;
836
837         FLAC__ASSERT(0 != object);
838         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
839         FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
840
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
853 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num)
854 {
855         unsigned i;
856
857         FLAC__ASSERT(0 != object);
858         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
859         FLAC__ASSERT(point_num < object->data.seek_table.num_points);
860
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
868 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
869 {
870         FLAC__ASSERT(0 != object);
871         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
872
873         return FLAC__format_seektable_is_legal(&object->data.seek_table);
874 }
875
876 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num)
877 {
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
888 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
889 {
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
907 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num)
908 {
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
932 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples)
933 {
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
957 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
958 {
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
969 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
970 {
971         if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
972                 return false;
973         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
974 }
975
976 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
977 {
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 {
989                 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);
991
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                 }
1006                 else if(0 == (object->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(object->data.vorbis_comment.comments, new_size)))
1007                         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
1020 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1021 {
1022         FLAC__ASSERT(0 != object);
1023         FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1024
1025         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1026                 return false;
1027         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
1028 }
1029
1030 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1031 {
1032         FLAC__StreamMetadata_VorbisComment *vc;
1033
1034         FLAC__ASSERT(0 != object);
1035         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1036         FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
1037
1038         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1039                 return false;
1040
1041         vc = &object->data.vorbis_comment;
1042
1043         if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
1044                 return false;
1045
1046         /* move all comments >= comment_num forward one space */
1047         memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
1048         vc->comments[comment_num].length = 0;
1049         vc->comments[comment_num].entry = 0;
1050
1051         return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1052 }
1053
1054 FLAC_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
1061 FLAC_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);
1064
1065         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1066                 return false;
1067
1068         {
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
1080                 if((i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length)) >= 0) {
1081                         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)) {
1085                                 for(i = vorbiscomment_find_entry_from_(object, index+1, (const char *)entry.entry, field_name_length); i >= 0; ) {
1086                                         if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
1087                                                 return false;
1088                                         if((unsigned)i < object->data.vorbis_comment.num_comments)
1089                                                 i = vorbiscomment_find_entry_from_(object, (unsigned)i, (const char *)entry.entry, field_name_length);
1090                                         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
1101 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
1102 {
1103         FLAC__StreamMetadata_VorbisComment *vc;
1104
1105         FLAC__ASSERT(0 != object);
1106         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1107         FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1108
1109         vc = &object->data.vorbis_comment;
1110
1111         /* free the comment at comment_num */
1112         if(0 != vc->comments[comment_num].entry)
1113                 free(vc->comments[comment_num].entry);
1114
1115         /* move all comments > comment_num backward one space */
1116         memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
1117         vc->comments[vc->num_comments-1].length = 0;
1118         vc->comments[vc->num_comments-1].entry = 0;
1119
1120         return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
1121 }
1122
1123 FLAC_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)
1124 {
1125         FLAC__ASSERT(0 != entry);
1126         FLAC__ASSERT(0 != field_name);
1127         FLAC__ASSERT(0 != field_value);
1128
1129         if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1130                 return false;
1131         if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
1132                 return false;
1133
1134         {
1135                 const size_t nn = strlen(field_name);
1136                 const size_t nv = strlen(field_value);
1137                 entry->length = nn + 1 /*=*/ + nv;
1138                 if(0 == (entry->entry = (FLAC__byte*)malloc(entry->length+1)))
1139                         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
1149 FLAC_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);
1154
1155         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1156                 return false;
1157
1158         {
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 */
1165                 if(0 == (*field_name = (char*)malloc(nn+1)))
1166                         return false;
1167                 if(0 == (*field_value = (char*)malloc(nv+1))) {
1168                         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
1180 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
1181 {
1182         FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1183         {
1184                 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1185 #if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
1186 #define FLAC__STRNCASECMP strnicmp
1187 #else
1188 #define FLAC__STRNCASECMP strncasecmp
1189 #endif
1190                 return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
1191 #undef FLAC__STRNCASECMP
1192         }
1193 }
1194
1195 FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
1196 {
1197         FLAC__ASSERT(0 != field_name);
1198
1199         return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
1200 }
1201
1202 FLAC_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++) {
1211                 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1212                         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
1222 FLAC_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--) {
1234                 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1235                         matching++;
1236                         ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1237                 }
1238         }
1239
1240         return ok? (int)matching : -1;
1241 }
1242
1243 FLAC_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
1248 FLAC_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
1264 void 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
1274 FLAC_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
1280 FLAC_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
1320 FLAC_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
1342 FLAC_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));
1346         return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
1347 }
1348
1349 FLAC_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 */
1361         memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
1362
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
1368 FLAC_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
1412 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1413 {
1414         FLAC__ASSERT(0 != object);
1415         FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1416
1417         return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
1418 }
1419
1420 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1421 {
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
1441 FLAC_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
1448 FLAC_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
1470 FLAC_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 }
1477
1478 static 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
1492 static 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
1502 FLAC_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 }