merge down from merged-API-layer branch: cvs -q up -dP -j API_LAYER_MERGING_BASELINE...
[platform/upstream/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 && total_samples > 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_append_spaced_points_by_samples(FLAC__StreamMetadata *object, unsigned samples, FLAC__uint64 total_samples)
958 {
959         FLAC__ASSERT(0 != object);
960         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
961         FLAC__ASSERT(samples > 0);
962         FLAC__ASSERT(total_samples > 0);
963
964         if(samples > 0 && total_samples > 0) {
965                 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
966                 unsigned i, j;
967                 FLAC__uint64 num, sample;
968
969                 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
970                 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
971                 if(total_samples % samples == 0)
972                         num--;
973
974                 i = seek_table->num_points;
975
976                 if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
977                         return false;
978
979                 sample = 0;
980                 for(j = 0; j < num; i++, j++, sample += samples) {
981                         seek_table->points[i].sample_number = sample;
982                         seek_table->points[i].stream_offset = 0;
983                         seek_table->points[i].frame_samples = 0;
984                 }
985         }
986
987         return true;
988 }
989
990 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
991 {
992         unsigned unique;
993
994         FLAC__ASSERT(0 != object);
995         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
996
997         unique = FLAC__format_seektable_sort(&object->data.seek_table);
998
999         return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1000 }
1001
1002 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1003 {
1004         if(!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1005                 return false;
1006         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
1007 }
1008
1009 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments)
1010 {
1011         FLAC__ASSERT(0 != object);
1012         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1013
1014         if(0 == object->data.vorbis_comment.comments) {
1015                 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1016                 if(0 == new_num_comments)
1017                         return true;
1018                 else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)))
1019                         return false;
1020         }
1021         else {
1022                 const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1023                 const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1024
1025                 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1026
1027                 /* if shrinking, free the truncated entries */
1028                 if(new_num_comments < object->data.vorbis_comment.num_comments) {
1029                         unsigned i;
1030                         for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1031                                 if(0 != object->data.vorbis_comment.comments[i].entry)
1032                                         free(object->data.vorbis_comment.comments[i].entry);
1033                 }
1034
1035                 if(new_size == 0) {
1036                         free(object->data.vorbis_comment.comments);
1037                         object->data.vorbis_comment.comments = 0;
1038                 }
1039                 else if(0 == (object->data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)realloc(object->data.vorbis_comment.comments, new_size)))
1040                         return false;
1041
1042                 /* if growing, zero all the length/pointers of new elements */
1043                 if(new_size > old_size)
1044                         memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size);
1045         }
1046
1047         object->data.vorbis_comment.num_comments = new_num_comments;
1048
1049         vorbiscomment_calculate_length_(object);
1050         return true;
1051 }
1052
1053 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1054 {
1055         FLAC__ASSERT(0 != object);
1056         FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1057
1058         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1059                 return false;
1060         return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
1061 }
1062
1063 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1064 {
1065         FLAC__StreamMetadata_VorbisComment *vc;
1066
1067         FLAC__ASSERT(0 != object);
1068         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1069         FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
1070
1071         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1072                 return false;
1073
1074         vc = &object->data.vorbis_comment;
1075
1076         if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
1077                 return false;
1078
1079         /* move all comments >= comment_num forward one space */
1080         memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
1081         vc->comments[comment_num].length = 0;
1082         vc->comments[comment_num].entry = 0;
1083
1084         return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1085 }
1086
1087 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1088 {
1089         FLAC__ASSERT(0 != object);
1090         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1091         return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1092 }
1093
1094 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1095 {
1096         FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1097
1098         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1099                 return false;
1100
1101         {
1102                 int i;
1103                 unsigned field_name_length;
1104                 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1105
1106                 FLAC__ASSERT(0 != eq);
1107
1108                 if(0 == eq)
1109                         return false; /* double protection */
1110
1111                 field_name_length = eq-entry.entry;
1112
1113                 if((i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length)) >= 0) {
1114                         unsigned index = (unsigned)i;
1115                         if(!FLAC__metadata_object_vorbiscomment_set_comment(object, index, entry, copy))
1116                                 return false;
1117                         if(all && (index+1 < object->data.vorbis_comment.num_comments)) {
1118                                 for(i = vorbiscomment_find_entry_from_(object, index+1, (const char *)entry.entry, field_name_length); i >= 0; ) {
1119                                         if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i))
1120                                                 return false;
1121                                         if((unsigned)i < object->data.vorbis_comment.num_comments)
1122                                                 i = vorbiscomment_find_entry_from_(object, (unsigned)i, (const char *)entry.entry, field_name_length);
1123                                         else
1124                                                 i = -1;
1125                                 }
1126                         }
1127                         return true;
1128                 }
1129                 else
1130                         return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1131         }
1132 }
1133
1134 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num)
1135 {
1136         FLAC__StreamMetadata_VorbisComment *vc;
1137
1138         FLAC__ASSERT(0 != object);
1139         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1140         FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1141
1142         vc = &object->data.vorbis_comment;
1143
1144         /* free the comment at comment_num */
1145         if(0 != vc->comments[comment_num].entry)
1146                 free(vc->comments[comment_num].entry);
1147
1148         /* move all comments > comment_num backward one space */
1149         memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
1150         vc->comments[vc->num_comments-1].length = 0;
1151         vc->comments[vc->num_comments-1].entry = 0;
1152
1153         return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
1154 }
1155
1156 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)
1157 {
1158         FLAC__ASSERT(0 != entry);
1159         FLAC__ASSERT(0 != field_name);
1160         FLAC__ASSERT(0 != field_value);
1161
1162         if(!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1163                 return false;
1164         if(!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (unsigned)(-1)))
1165                 return false;
1166
1167         {
1168                 const size_t nn = strlen(field_name);
1169                 const size_t nv = strlen(field_value);
1170                 entry->length = nn + 1 /*=*/ + nv;
1171                 if(0 == (entry->entry = (FLAC__byte*)malloc(entry->length+1)))
1172                         return false;
1173                 memcpy(entry->entry, field_name, nn);
1174                 entry->entry[nn] = '=';
1175                 memcpy(entry->entry+nn+1, field_value, nv);
1176                 entry->entry[entry->length] = '\0';
1177         }
1178         
1179         return true;
1180 }
1181
1182 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)
1183 {
1184         FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1185         FLAC__ASSERT(0 != field_name);
1186         FLAC__ASSERT(0 != field_value);
1187
1188         if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1189                 return false;
1190
1191         {
1192                 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1193                 const size_t nn = eq-entry.entry;
1194                 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1195                 FLAC__ASSERT(0 != eq);
1196                 if(0 == eq)
1197                         return false; /* double protection */
1198                 if(0 == (*field_name = (char*)malloc(nn+1)))
1199                         return false;
1200                 if(0 == (*field_value = (char*)malloc(nv+1))) {
1201                         free(*field_name);
1202                         return false;
1203                 }
1204                 memcpy(*field_name, entry.entry, nn);
1205                 memcpy(*field_value, entry.entry+nn+1, nv);
1206                 (*field_name)[nn] = '\0';
1207                 (*field_value)[nv] = '\0';
1208         }
1209
1210         return true;
1211 }
1212
1213 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, unsigned field_name_length)
1214 {
1215         FLAC__ASSERT(0 != entry.entry && entry.length > 0);
1216         {
1217                 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1218 #if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
1219 #define FLAC__STRNCASECMP strnicmp
1220 #else
1221 #define FLAC__STRNCASECMP strncasecmp
1222 #endif
1223                 return (0 != eq && (unsigned)(eq-entry.entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length));
1224 #undef FLAC__STRNCASECMP
1225         }
1226 }
1227
1228 FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name)
1229 {
1230         FLAC__ASSERT(0 != field_name);
1231
1232         return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
1233 }
1234
1235 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1236 {
1237         const unsigned field_name_length = strlen(field_name);
1238         unsigned i;
1239
1240         FLAC__ASSERT(0 != object);
1241         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1242
1243         for(i = 0; i < object->data.vorbis_comment.num_comments; i++) {
1244                 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1245                         if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1246                                 return -1;
1247                         else
1248                                 return 1;
1249                 }
1250         }
1251
1252         return 0;
1253 }
1254
1255 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1256 {
1257         FLAC__bool ok = true;
1258         unsigned matching = 0;
1259         const unsigned field_name_length = strlen(field_name);
1260         int i;
1261
1262         FLAC__ASSERT(0 != object);
1263         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1264
1265         /* must delete from end to start otherwise it will interfere with our iteration */
1266         for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
1267                 if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1268                         matching++;
1269                         ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i);
1270                 }
1271         }
1272
1273         return ok? (int)matching : -1;
1274 }
1275
1276 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new()
1277 {
1278         return (FLAC__StreamMetadata_CueSheet_Track*)calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1279 }
1280
1281 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1282 {
1283         FLAC__StreamMetadata_CueSheet_Track *to;
1284
1285         FLAC__ASSERT(0 != object);
1286
1287         if(0 != (to = FLAC__metadata_object_cuesheet_track_new())) {
1288                 if(!copy_track_(to, object)) {
1289                         FLAC__metadata_object_cuesheet_track_delete(to);
1290                         return 0;
1291                 }
1292         }
1293
1294         return to;
1295 }
1296
1297 void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1298 {
1299         FLAC__ASSERT(0 != object);
1300
1301         if(0 != object->indices) {
1302                 FLAC__ASSERT(object->num_indices > 0);
1303                 free(object->indices);
1304         }
1305 }
1306
1307 FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1308 {
1309         FLAC__metadata_object_cuesheet_track_delete_data(object);
1310         free(object);
1311 }
1312
1313 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, unsigned track_num, unsigned new_num_indices)
1314 {
1315         FLAC__StreamMetadata_CueSheet_Track *track;
1316         FLAC__ASSERT(0 != object);
1317         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1318         FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1319
1320         track = &object->data.cue_sheet.tracks[track_num];
1321
1322         if(0 == track->indices) {
1323                 FLAC__ASSERT(track->num_indices == 0);
1324                 if(0 == new_num_indices)
1325                         return true;
1326                 else if(0 == (track->indices = cuesheet_track_index_array_new_(new_num_indices)))
1327                         return false;
1328         }
1329         else {
1330                 const unsigned old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1331                 const unsigned new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1332
1333                 FLAC__ASSERT(track->num_indices > 0);
1334
1335                 if(new_size == 0) {
1336                         free(track->indices);
1337                         track->indices = 0;
1338                 }
1339                 else if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)realloc(track->indices, new_size)))
1340                         return false;
1341
1342                 /* if growing, zero all the lengths/pointers of new elements */
1343                 if(new_size > old_size)
1344                         memset(track->indices + track->num_indices, 0, new_size - old_size);
1345         }
1346
1347         track->num_indices = new_num_indices;
1348
1349         cuesheet_calculate_length_(object);
1350         return true;
1351 }
1352
1353 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)
1354 {
1355         FLAC__StreamMetadata_CueSheet_Track *track;
1356
1357         FLAC__ASSERT(0 != object);
1358         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1359         FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1360         FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1361
1362         track = &object->data.cue_sheet.tracks[track_num];
1363
1364         if(!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1365                 return false;
1366
1367         /* move all indices >= index_num forward one space */
1368         memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1369
1370         track->indices[index_num] = index;
1371         cuesheet_calculate_length_(object);
1372         return true;
1373 }
1374
1375 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1376 {
1377         FLAC__StreamMetadata_CueSheet_Index index;
1378         memset(&index, 0, sizeof(index));
1379         return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, index);
1380 }
1381
1382 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, unsigned track_num, unsigned index_num)
1383 {
1384         FLAC__StreamMetadata_CueSheet_Track *track;
1385
1386         FLAC__ASSERT(0 != object);
1387         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1388         FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1389         FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1390
1391         track = &object->data.cue_sheet.tracks[track_num];
1392
1393         /* move all indices > index_num backward one space */
1394         memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
1395
1396         FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1397         cuesheet_calculate_length_(object);
1398         return true;
1399 }
1400
1401 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, unsigned new_num_tracks)
1402 {
1403         FLAC__ASSERT(0 != object);
1404         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1405
1406         if(0 == object->data.cue_sheet.tracks) {
1407                 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1408                 if(0 == new_num_tracks)
1409                         return true;
1410                 else if(0 == (object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)))
1411                         return false;
1412         }
1413         else {
1414                 const unsigned old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1415                 const unsigned new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1416
1417                 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1418
1419                 /* if shrinking, free the truncated entries */
1420                 if(new_num_tracks < object->data.cue_sheet.num_tracks) {
1421                         unsigned i;
1422                         for(i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1423                                 if(0 != object->data.cue_sheet.tracks[i].indices)
1424                                         free(object->data.cue_sheet.tracks[i].indices);
1425                 }
1426
1427                 if(new_size == 0) {
1428                         free(object->data.cue_sheet.tracks);
1429                         object->data.cue_sheet.tracks = 0;
1430                 }
1431                 else if(0 == (object->data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)realloc(object->data.cue_sheet.tracks, new_size)))
1432                         return false;
1433
1434                 /* if growing, zero all the lengths/pointers of new elements */
1435                 if(new_size > old_size)
1436                         memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1437         }
1438
1439         object->data.cue_sheet.num_tracks = new_num_tracks;
1440
1441         cuesheet_calculate_length_(object);
1442         return true;
1443 }
1444
1445 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1446 {
1447         FLAC__ASSERT(0 != object);
1448         FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1449
1450         return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
1451 }
1452
1453 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, unsigned track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1454 {
1455         FLAC__StreamMetadata_CueSheet *cs;
1456
1457         FLAC__ASSERT(0 != object);
1458         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1459         FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1460
1461         cs = &object->data.cue_sheet;
1462
1463         if(!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1464                 return false;
1465
1466         /* move all tracks >= track_num forward one space */
1467         memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1468         cs->tracks[track_num].num_indices = 0;
1469         cs->tracks[track_num].indices = 0;
1470
1471         return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1472 }
1473
1474 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, unsigned track_num)
1475 {
1476         FLAC__StreamMetadata_CueSheet_Track track;
1477         memset(&track, 0, sizeof(track));
1478         return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1479 }
1480
1481 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, unsigned track_num)
1482 {
1483         FLAC__StreamMetadata_CueSheet *cs;
1484
1485         FLAC__ASSERT(0 != object);
1486         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1487         FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1488
1489         cs = &object->data.cue_sheet;
1490
1491         /* free the track at track_num */
1492         if(0 != cs->tracks[track_num].indices)
1493                 free(cs->tracks[track_num].indices);
1494
1495         /* move all tracks > track_num backward one space */
1496         memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1497         cs->tracks[cs->num_tracks-1].num_indices = 0;
1498         cs->tracks[cs->num_tracks-1].indices = 0;
1499
1500         return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1501 }
1502
1503 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1504 {
1505         FLAC__ASSERT(0 != object);
1506         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1507
1508         return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1509 }
1510
1511 static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, unsigned track)
1512 {
1513         if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1514                 return 0;
1515         else if (cs->tracks[track].indices[0].number == 1)
1516                 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1517         else if (cs->tracks[track].num_indices < 2)
1518                 return 0;
1519         else if (cs->tracks[track].indices[1].number == 1)
1520                 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1521         else
1522                 return 0;
1523 }
1524
1525 static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1526 {
1527         FLAC__uint32 n = 0;
1528         while (x) {
1529                 n += (x%10);
1530                 x /= 10;
1531         }
1532         return n;
1533 }
1534
1535 FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1536 {
1537         const FLAC__StreamMetadata_CueSheet *cs;
1538
1539         FLAC__ASSERT(0 != object);
1540         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1541
1542         cs = &object->data.cue_sheet;
1543
1544         if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1545                 return 0;
1546
1547         {
1548                 FLAC__uint32 i, length, sum = 0;
1549                 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1550                         sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1551                 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1552
1553                 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1554         }
1555 }