1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2023 Xiph.Org Foundation
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "private/metadata.h"
41 #include "private/memory.h"
42 #include "private/stream_encoder_framing.h"
43
44 #include "FLAC/assert.h"
45 #include "FLAC/stream_decoder.h"
46 #include "share/alloc.h"
47 #include "share/compat.h"
48
49 /* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
50 #define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
51
52
53 /****************************************************************************
54 *
55 * Local routines
56 *
57 ***************************************************************************/
58
59 /* copy bytes:
60 * from = NULL && bytes = 0
61 * to <- NULL
62 * from != NULL && bytes > 0
63 * to <- copy of from
64 * else ASSERT
65 * malloc error leaves 'to' unchanged
66 */
copy_bytes_(FLAC__byte ** to,const FLAC__byte * from,uint32_t bytes)67 static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, uint32_t bytes)
68 {
69 FLAC__ASSERT(to != NULL);
70 if (bytes > 0 && from != NULL) {
71 FLAC__byte *x;
72 if ((x = safe_malloc_(bytes)) == NULL)
73 return false;
74 memcpy(x, from, bytes);
75 *to = x;
76 }
77 else {
78 *to = 0;
79 }
80 return true;
81 }
82
83 #if 0 /* UNUSED */
84 /* like copy_bytes_(), but free()s the original '*to' if the copy succeeds and the original '*to' is non-NULL */
85 static FLAC__bool free_copy_bytes_(FLAC__byte **to, const FLAC__byte *from, uint32_t bytes)
86 {
87 FLAC__byte *copy;
88 FLAC__ASSERT(to != NULL);
89 if (copy_bytes_(©, from, bytes)) {
90 free(*to);
91 *to = copy;
92 return true;
93 }
94 else
95 return false;
96 }
97 #endif
98
99 /* reallocate entry to 1 byte larger and add a terminating NUL */
100 /* realloc() failure leaves entry unchanged */
ensure_null_terminated_(FLAC__byte ** entry,uint32_t length)101 static FLAC__bool ensure_null_terminated_(FLAC__byte **entry, uint32_t length)
102 {
103 FLAC__byte *x = safe_realloc_nofree_add_2op_(*entry, length, /*+*/1);
104 if (x != NULL) {
105 x[length] = '\0';
106 *entry = x;
107 return true;
108 }
109 else
110 return false;
111 }
112
113 /* copies the NUL-terminated C-string 'from' to '*to', leaving '*to'
114 * unchanged if malloc fails, free()ing the original '*to' if it
115 * succeeds and the original '*to' was not NULL
116 */
copy_cstring_(char ** to,const char * from)117 static FLAC__bool copy_cstring_(char **to, const char *from)
118 {
119 char *copy = strdup(from);
120 FLAC__ASSERT(to != NULL);
121 if (copy) {
122 free(*to);
123 *to = copy;
124 return true;
125 }
126 else
127 return false;
128 }
129
copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry * to,const FLAC__StreamMetadata_VorbisComment_Entry * from)130 static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from)
131 {
132 to->length = from->length;
133 if (from->entry == 0) {
134 FLAC__ASSERT(from->length == 0);
135 if ((to->entry = safe_malloc_(1)) == NULL)
136 return false;
137 to->entry[0] = '\0';
138 }
139 else {
140 FLAC__byte *x;
141 if ((x = safe_malloc_add_2op_(from->length, /*+*/1)) == NULL)
142 return false;
143 memcpy(x, from->entry, from->length);
144 x[from->length] = '\0';
145 to->entry = x;
146 }
147 return true;
148 }
149
copy_track_(FLAC__StreamMetadata_CueSheet_Track * to,const FLAC__StreamMetadata_CueSheet_Track * from)150 static FLAC__bool copy_track_(FLAC__StreamMetadata_CueSheet_Track *to, const FLAC__StreamMetadata_CueSheet_Track *from)
151 {
152 memcpy(to, from, sizeof(FLAC__StreamMetadata_CueSheet_Track));
153 if (from->indices == 0) {
154 FLAC__ASSERT(from->num_indices == 0);
155 }
156 else {
157 FLAC__StreamMetadata_CueSheet_Index *x;
158 FLAC__ASSERT(from->num_indices > 0);
159 if ((x = safe_malloc_mul_2op_p(from->num_indices, /*times*/sizeof(FLAC__StreamMetadata_CueSheet_Index))) == NULL)
160 return false;
161 memcpy(x, from->indices, from->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
162 to->indices = x;
163 }
164 return true;
165 }
166
seektable_calculate_length_(FLAC__StreamMetadata * object)167 static void seektable_calculate_length_(FLAC__StreamMetadata *object)
168 {
169 FLAC__ASSERT(object != NULL);
170 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
171
172 object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
173 }
174
seekpoint_array_new_(uint32_t num_points)175 static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(uint32_t num_points)
176 {
177 FLAC__StreamMetadata_SeekPoint *object_array;
178
179 FLAC__ASSERT(num_points > 0);
180
181 object_array = safe_malloc_mul_2op_p(num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint));
182
183 if (object_array != NULL) {
184 uint32_t i;
185 for (i = 0; i < num_points; i++) {
186 object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
187 object_array[i].stream_offset = 0;
188 object_array[i].frame_samples = 0;
189 }
190 }
191
192 return object_array;
193 }
194
vorbiscomment_calculate_length_(FLAC__StreamMetadata * object)195 static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object)
196 {
197 uint32_t i;
198
199 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
200
201 object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8;
202 object->length += object->data.vorbis_comment.vendor_string.length;
203 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8;
204 for (i = 0; i < object->data.vorbis_comment.num_comments; i++) {
205 object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8);
206 object->length += object->data.vorbis_comment.comments[i].length;
207 }
208 }
209
vorbiscomment_entry_array_new_(uint32_t num_comments)210 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(uint32_t num_comments)
211 {
212 FLAC__ASSERT(num_comments > 0);
213
214 return safe_calloc_(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
215 }
216
vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry * object_array,uint32_t num_comments)217 static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, uint32_t num_comments)
218 {
219 uint32_t i;
220
221 FLAC__ASSERT(object_array != NULL);
222
223 for (i = 0; i < num_comments; i++)
224 free(object_array[i].entry);
225
226 free(object_array);
227 }
228
vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry * object_array,uint32_t num_comments)229 static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, uint32_t num_comments)
230 {
231 FLAC__StreamMetadata_VorbisComment_Entry *return_array;
232
233 FLAC__ASSERT(object_array != NULL);
234 FLAC__ASSERT(num_comments > 0);
235
236 return_array = vorbiscomment_entry_array_new_(num_comments);
237
238 if (return_array != NULL) {
239 uint32_t i;
240
241 for (i = 0; i < num_comments; i++) {
242 if (!copy_vcentry_(return_array+i, object_array+i)) {
243 vorbiscomment_entry_array_delete_(return_array, num_comments);
244 return 0;
245 }
246 }
247 }
248
249 return return_array;
250 }
251
vorbiscomment_set_entry_(FLAC__StreamMetadata * object,FLAC__StreamMetadata_VorbisComment_Entry * dest,const FLAC__StreamMetadata_VorbisComment_Entry * src,FLAC__bool copy)252 static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry *dest, const FLAC__StreamMetadata_VorbisComment_Entry *src, FLAC__bool copy)
253 {
254 FLAC__byte *save;
255
256 FLAC__ASSERT(object != NULL);
257 FLAC__ASSERT(dest != NULL);
258 FLAC__ASSERT(src != NULL);
259 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
260 FLAC__ASSERT((src->entry != NULL && src->length > 0) || (src->entry == NULL && src->length == 0));
261
262 save = dest->entry;
263
264 if (src->entry != NULL) {
265 if (copy) {
266 /* do the copy first so that if we fail we leave the dest object untouched */
267 if (!copy_vcentry_(dest, src))
268 return false;
269 }
270 else {
271 /* we have to make sure that the string we're taking over is null-terminated */
272
273 /*
274 * Stripping the const from src->entry is OK since we're taking
275 * ownership of the pointer. This is a hack around a deficiency
276 * in the API where the same function is used for 'copy' and
277 * 'own', but the source entry is a const pointer. If we were
278 * precise, the 'own' flavor would be a separate function with a
279 * non-const source pointer. But it's not, so we hack away.
280 */
281 if (!ensure_null_terminated_((FLAC__byte**)(&src->entry), src->length))
282 return false;
283 *dest = *src;
284 }
285 }
286 else {
287 /* the src is null */
288 *dest = *src;
289 }
290
291 free(save);
292
293 vorbiscomment_calculate_length_(object);
294 return true;
295 }
296
vorbiscomment_find_entry_from_(const FLAC__StreamMetadata * object,uint32_t offset,const char * field_name,uint32_t field_name_length)297 static int vorbiscomment_find_entry_from_(const FLAC__StreamMetadata *object, uint32_t offset, const char *field_name, uint32_t field_name_length)
298 {
299 uint32_t i;
300
301 FLAC__ASSERT(object != NULL);
302 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
303 FLAC__ASSERT(field_name != NULL);
304
305 for (i = offset; i < object->data.vorbis_comment.num_comments; i++) {
306 if (FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length))
307 return (int)i;
308 }
309
310 return -1;
311 }
312
cuesheet_calculate_length_(FLAC__StreamMetadata * object)313 static void cuesheet_calculate_length_(FLAC__StreamMetadata *object)
314 {
315 uint32_t i;
316
317 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
318
319 object->length = (
320 FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
321 FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
322 FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
323 FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
324 FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
325 ) / 8;
326
327 object->length += object->data.cue_sheet.num_tracks * (
328 FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
329 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
330 FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
331 FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
332 FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
333 FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
334 FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
335 ) / 8;
336
337 for (i = 0; i < object->data.cue_sheet.num_tracks; i++) {
338 object->length += object->data.cue_sheet.tracks[i].num_indices * (
339 FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
340 FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
341 FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
342 ) / 8;
343 }
344 }
345
cuesheet_track_index_array_new_(uint32_t num_indices)346 static FLAC__StreamMetadata_CueSheet_Index *cuesheet_track_index_array_new_(uint32_t num_indices)
347 {
348 FLAC__ASSERT(num_indices > 0);
349
350 return safe_calloc_(num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index));
351 }
352
cuesheet_track_array_new_(uint32_t num_tracks)353 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_new_(uint32_t num_tracks)
354 {
355 FLAC__ASSERT(num_tracks > 0);
356
357 return safe_calloc_(num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track));
358 }
359
cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track * object_array,uint32_t num_tracks)360 static void cuesheet_track_array_delete_(FLAC__StreamMetadata_CueSheet_Track *object_array, uint32_t num_tracks)
361 {
362 uint32_t i;
363
364 FLAC__ASSERT(object_array != NULL && num_tracks > 0);
365
366 for (i = 0; i < num_tracks; i++) {
367 if (object_array[i].indices != 0) {
368 FLAC__ASSERT(object_array[i].num_indices > 0);
369 free(object_array[i].indices);
370 }
371 }
372
373 free(object_array);
374 }
375
cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track * object_array,uint32_t num_tracks)376 static FLAC__StreamMetadata_CueSheet_Track *cuesheet_track_array_copy_(const FLAC__StreamMetadata_CueSheet_Track *object_array, uint32_t num_tracks)
377 {
378 FLAC__StreamMetadata_CueSheet_Track *return_array;
379
380 FLAC__ASSERT(object_array != NULL);
381 FLAC__ASSERT(num_tracks > 0);
382
383 return_array = cuesheet_track_array_new_(num_tracks);
384
385 if (return_array != NULL) {
386 uint32_t i;
387
388 for (i = 0; i < num_tracks; i++) {
389 if (!copy_track_(return_array+i, object_array+i)) {
390 cuesheet_track_array_delete_(return_array, num_tracks);
391 return 0;
392 }
393 }
394 }
395
396 return return_array;
397 }
398
cuesheet_set_track_(FLAC__StreamMetadata * object,FLAC__StreamMetadata_CueSheet_Track * dest,const FLAC__StreamMetadata_CueSheet_Track * src,FLAC__bool copy)399 static FLAC__bool cuesheet_set_track_(FLAC__StreamMetadata *object, FLAC__StreamMetadata_CueSheet_Track *dest, const FLAC__StreamMetadata_CueSheet_Track *src, FLAC__bool copy)
400 {
401 FLAC__StreamMetadata_CueSheet_Index *save;
402
403 FLAC__ASSERT(object != NULL);
404 FLAC__ASSERT(dest != NULL);
405 FLAC__ASSERT(src != NULL);
406 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
407 FLAC__ASSERT((src->indices != NULL && src->num_indices > 0) || (src->indices == NULL && src->num_indices == 0));
408
409 save = dest->indices;
410
411 /* do the copy first so that if we fail we leave the object untouched */
412 if (copy) {
413 if (!copy_track_(dest, src))
414 return false;
415 }
416 else {
417 *dest = *src;
418 }
419
420 free(save);
421
422 cuesheet_calculate_length_(object);
423 return true;
424 }
425
426
427 /****************************************************************************
428 *
429 * Metadata object routines
430 *
431 ***************************************************************************/
432
FLAC__metadata_object_new(FLAC__MetadataType type)433 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type)
434 {
435 FLAC__StreamMetadata *object;
436
437 if (type > FLAC__MAX_METADATA_TYPE)
438 return 0;
439
440 object = calloc(1, sizeof(FLAC__StreamMetadata));
441 if (object != NULL) {
442 object->is_last = false;
443 object->type = type;
444 switch(type) {
445 case FLAC__METADATA_TYPE_STREAMINFO:
446 object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
447 break;
448 case FLAC__METADATA_TYPE_PADDING:
449 /* calloc() took care of this for us:
450 object->length = 0;
451 */
452 break;
453 case FLAC__METADATA_TYPE_APPLICATION:
454 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
455 /* calloc() took care of this for us:
456 object->data.application.data = 0;
457 */
458 break;
459 case FLAC__METADATA_TYPE_SEEKTABLE:
460 /* calloc() took care of this for us:
461 object->length = 0;
462 object->data.seek_table.num_points = 0;
463 object->data.seek_table.points = 0;
464 */
465 break;
466 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
467 object->data.vorbis_comment.vendor_string.length = (uint32_t)strlen(FLAC__VENDOR_STRING);
468 if (!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length+1)) {
469 free(object);
470 return 0;
471 }
472 vorbiscomment_calculate_length_(object);
473 break;
474 case FLAC__METADATA_TYPE_CUESHEET:
475 cuesheet_calculate_length_(object);
476 break;
477 case FLAC__METADATA_TYPE_PICTURE:
478 object->length = (
479 FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
480 FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* empty mime_type string */
481 FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* empty description string */
482 FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
483 FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
484 FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
485 FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
486 FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN +
487 0 /* no data */
488 ) / 8;
489 object->data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
490 object->data.picture.mime_type = 0;
491 object->data.picture.description = 0;
492 /* calloc() took care of this for us:
493 object->data.picture.width = 0;
494 object->data.picture.height = 0;
495 object->data.picture.depth = 0;
496 object->data.picture.colors = 0;
497 object->data.picture.data_length = 0;
498 object->data.picture.data = 0;
499 */
500 /* now initialize mime_type and description with empty strings to make things easier on the client */
501 if (!copy_cstring_(&object->data.picture.mime_type, "")) {
502 free(object);
503 return 0;
504 }
505 if (!copy_cstring_((char**)(&object->data.picture.description), "")) {
506 free(object->data.picture.mime_type);
507 free(object);
508 return 0;
509 }
510 break;
511 default:
512 /* calloc() took care of this for us:
513 object->length = 0;
514 object->data.unknown.data = 0;
515 */
516 break;
517 }
518 }
519
520 return object;
521 }
522
FLAC__metadata_object_clone(const FLAC__StreamMetadata * object)523 FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object)
524 {
525 FLAC__StreamMetadata *to;
526
527 FLAC__ASSERT(object != NULL);
528
529 if ((to = FLAC__metadata_object_new(object->type)) != NULL) {
530 to->is_last = object->is_last;
531 to->type = object->type;
532 to->length = object->length;
533 switch(to->type) {
534 case FLAC__METADATA_TYPE_STREAMINFO:
535 memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo));
536 break;
537 case FLAC__METADATA_TYPE_PADDING:
538 break;
539 case FLAC__METADATA_TYPE_APPLICATION:
540 if (to->length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) { /* underflow check */
541 FLAC__metadata_object_delete(to);
542 return 0;
543 }
544 memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8);
545 if (!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) {
546 FLAC__metadata_object_delete(to);
547 return 0;
548 }
549 break;
550 case FLAC__METADATA_TYPE_SEEKTABLE:
551 to->data.seek_table.num_points = object->data.seek_table.num_points;
552 if (to->data.seek_table.num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint)) { /* overflow check */
553 FLAC__metadata_object_delete(to);
554 return 0;
555 }
556 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))) {
557 FLAC__metadata_object_delete(to);
558 return 0;
559 }
560 break;
561 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
562 if (to->data.vorbis_comment.vendor_string.entry != NULL) {
563 free(to->data.vorbis_comment.vendor_string.entry);
564 to->data.vorbis_comment.vendor_string.entry = 0;
565 }
566 if (!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
567 FLAC__metadata_object_delete(to);
568 return 0;
569 }
570 if (object->data.vorbis_comment.num_comments == 0) {
571 to->data.vorbis_comment.comments = 0;
572 }
573 else {
574 to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
575 if (to->data.vorbis_comment.comments == NULL) {
576 to->data.vorbis_comment.num_comments = 0;
577 FLAC__metadata_object_delete(to);
578 return 0;
579 }
580 }
581 to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments;
582 break;
583 case FLAC__METADATA_TYPE_CUESHEET:
584 memcpy(&to->data.cue_sheet, &object->data.cue_sheet, sizeof(FLAC__StreamMetadata_CueSheet));
585 if (object->data.cue_sheet.num_tracks == 0) {
586 FLAC__ASSERT(object->data.cue_sheet.tracks == NULL);
587 }
588 else {
589 FLAC__ASSERT(object->data.cue_sheet.tracks != 0);
590 to->data.cue_sheet.tracks = cuesheet_track_array_copy_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
591 if (to->data.cue_sheet.tracks == NULL) {
592 FLAC__metadata_object_delete(to);
593 return 0;
594 }
595 }
596 break;
597 case FLAC__METADATA_TYPE_PICTURE:
598 to->data.picture.type = object->data.picture.type;
599 if (!copy_cstring_(&to->data.picture.mime_type, object->data.picture.mime_type)) {
600 FLAC__metadata_object_delete(to);
601 return 0;
602 }
603 if (!copy_cstring_((char**)(&to->data.picture.description), (const char*)object->data.picture.description)) {
604 FLAC__metadata_object_delete(to);
605 return 0;
606 }
607 to->data.picture.width = object->data.picture.width;
608 to->data.picture.height = object->data.picture.height;
609 to->data.picture.depth = object->data.picture.depth;
610 to->data.picture.colors = object->data.picture.colors;
611 to->data.picture.data_length = object->data.picture.data_length;
612 if (!copy_bytes_((&to->data.picture.data), object->data.picture.data, object->data.picture.data_length)) {
613 FLAC__metadata_object_delete(to);
614 return 0;
615 }
616 break;
617 default:
618 if (!copy_bytes_(&to->data.unknown.data, object->data.unknown.data, object->length)) {
619 FLAC__metadata_object_delete(to);
620 return 0;
621 }
622 break;
623 }
624 }
625
626 return to;
627 }
628
FLAC__metadata_object_delete_data(FLAC__StreamMetadata * object)629 void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
630 {
631 FLAC__ASSERT(object != NULL);
632
633 switch(object->type) {
634 case FLAC__METADATA_TYPE_STREAMINFO:
635 case FLAC__METADATA_TYPE_PADDING:
636 break;
637 case FLAC__METADATA_TYPE_APPLICATION:
638 if (object->data.application.data != NULL) {
639 free(object->data.application.data);
640 object->data.application.data = NULL;
641 }
642 break;
643 case FLAC__METADATA_TYPE_SEEKTABLE:
644 if (object->data.seek_table.points != NULL) {
645 free(object->data.seek_table.points);
646 object->data.seek_table.points = NULL;
647 }
648 break;
649 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
650 if (object->data.vorbis_comment.vendor_string.entry != NULL) {
651 free(object->data.vorbis_comment.vendor_string.entry);
652 object->data.vorbis_comment.vendor_string.entry = 0;
653 }
654 if (object->data.vorbis_comment.comments != NULL) {
655 vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
656 object->data.vorbis_comment.comments = NULL;
657 object->data.vorbis_comment.num_comments = 0;
658 }
659 break;
660 case FLAC__METADATA_TYPE_CUESHEET:
661 if (object->data.cue_sheet.tracks != NULL) {
662 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
663 cuesheet_track_array_delete_(object->data.cue_sheet.tracks, object->data.cue_sheet.num_tracks);
664 object->data.cue_sheet.tracks = NULL;
665 object->data.cue_sheet.num_tracks = 0;
666 }
667 break;
668 case FLAC__METADATA_TYPE_PICTURE:
669 if (object->data.picture.mime_type != NULL) {
670 free(object->data.picture.mime_type);
671 object->data.picture.mime_type = NULL;
672 }
673 if (object->data.picture.description != NULL) {
674 free(object->data.picture.description);
675 object->data.picture.description = NULL;
676 }
677 if (object->data.picture.data != NULL) {
678 free(object->data.picture.data);
679 object->data.picture.data = NULL;
680 }
681 break;
682 default:
683 if (object->data.unknown.data != NULL) {
684 free(object->data.unknown.data);
685 object->data.unknown.data = NULL;
686 }
687 break;
688 }
689 }
690
FLAC__metadata_object_delete(FLAC__StreamMetadata * object)691 FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object)
692 {
693 FLAC__metadata_object_delete_data(object);
694 free(object);
695 }
696
compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo * block1,const FLAC__StreamMetadata_StreamInfo * block2)697 static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2)
698 {
699 if (block1->min_blocksize != block2->min_blocksize)
700 return false;
701 if (block1->max_blocksize != block2->max_blocksize)
702 return false;
703 if (block1->min_framesize != block2->min_framesize)
704 return false;
705 if (block1->max_framesize != block2->max_framesize)
706 return false;
707 if (block1->sample_rate != block2->sample_rate)
708 return false;
709 if (block1->channels != block2->channels)
710 return false;
711 if (block1->bits_per_sample != block2->bits_per_sample)
712 return false;
713 if (block1->total_samples != block2->total_samples)
714 return false;
715 if (memcmp(block1->md5sum, block2->md5sum, 16) != 0)
716 return false;
717 return true;
718 }
719
compare_block_data_application_(const FLAC__StreamMetadata_Application * block1,const FLAC__StreamMetadata_Application * block2,uint32_t block_length)720 static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, uint32_t block_length)
721 {
722 FLAC__ASSERT(block1 != NULL);
723 FLAC__ASSERT(block2 != NULL);
724 FLAC__ASSERT(block_length >= sizeof(block1->id));
725
726 if (memcmp(block1->id, block2->id, sizeof(block1->id)) != 0)
727 return false;
728 if (block1->data != NULL && block2->data != NULL)
729 return memcmp(block1->data, block2->data, block_length - sizeof(block1->id)) == 0;
730 else
731 return block1->data == block2->data;
732 }
733
compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable * block1,const FLAC__StreamMetadata_SeekTable * block2)734 static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2)
735 {
736 uint32_t i;
737
738 FLAC__ASSERT(block1 != NULL);
739 FLAC__ASSERT(block2 != NULL);
740
741 if (block1->num_points != block2->num_points)
742 return false;
743
744 if (block1->points != NULL && block2->points != NULL) {
745 for (i = 0; i < block1->num_points; i++) {
746 if (block1->points[i].sample_number != block2->points[i].sample_number)
747 return false;
748 if (block1->points[i].stream_offset != block2->points[i].stream_offset)
749 return false;
750 if (block1->points[i].frame_samples != block2->points[i].frame_samples)
751 return false;
752 }
753 return true;
754 }
755 else
756 return block1->points == block2->points;
757 }
758
compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment * block1,const FLAC__StreamMetadata_VorbisComment * block2)759 static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2)
760 {
761 uint32_t i;
762
763 if (block1->vendor_string.length != block2->vendor_string.length)
764 return false;
765
766 if (block1->vendor_string.entry != NULL && block2->vendor_string.entry != NULL) {
767 if (memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length) != 0)
768 return false;
769 }
770 else if (block1->vendor_string.entry != block2->vendor_string.entry)
771 return false;
772
773 if (block1->num_comments != block2->num_comments)
774 return false;
775
776 for (i = 0; i < block1->num_comments; i++) {
777 if (block1->comments[i].entry != NULL && block2->comments[i].entry != NULL) {
778 if (memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length) != 0)
779 return false;
780 }
781 else if (block1->comments[i].entry != block2->comments[i].entry)
782 return false;
783 }
784 return true;
785 }
786
compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet * block1,const FLAC__StreamMetadata_CueSheet * block2)787 static FLAC__bool compare_block_data_cuesheet_(const FLAC__StreamMetadata_CueSheet *block1, const FLAC__StreamMetadata_CueSheet *block2)
788 {
789 uint32_t i, j;
790
791 if (strcmp(block1->media_catalog_number, block2->media_catalog_number) != 0)
792 return false;
793
794 if (block1->lead_in != block2->lead_in)
795 return false;
796
797 if (block1->is_cd != block2->is_cd)
798 return false;
799
800 if (block1->num_tracks != block2->num_tracks)
801 return false;
802
803 if (block1->tracks != NULL && block2->tracks != NULL) {
804 FLAC__ASSERT(block1->num_tracks > 0);
805 for (i = 0; i < block1->num_tracks; i++) {
806 if (block1->tracks[i].offset != block2->tracks[i].offset)
807 return false;
808 if (block1->tracks[i].number != block2->tracks[i].number)
809 return false;
810 if (memcmp(block1->tracks[i].isrc, block2->tracks[i].isrc, sizeof(block1->tracks[i].isrc)) != 0)
811 return false;
812 if (block1->tracks[i].type != block2->tracks[i].type)
813 return false;
814 if (block1->tracks[i].pre_emphasis != block2->tracks[i].pre_emphasis)
815 return false;
816 if (block1->tracks[i].num_indices != block2->tracks[i].num_indices)
817 return false;
818 if (block1->tracks[i].indices != NULL && block2->tracks[i].indices != NULL) {
819 FLAC__ASSERT(block1->tracks[i].num_indices > 0);
820 for (j = 0; j < block1->tracks[i].num_indices; j++) {
821 if (block1->tracks[i].indices[j].offset != block2->tracks[i].indices[j].offset)
822 return false;
823 if (block1->tracks[i].indices[j].number != block2->tracks[i].indices[j].number)
824 return false;
825 }
826 }
827 else if (block1->tracks[i].indices != block2->tracks[i].indices)
828 return false;
829 }
830 }
831 else if (block1->tracks != block2->tracks)
832 return false;
833 return true;
834 }
835
compare_block_data_picture_(const FLAC__StreamMetadata_Picture * block1,const FLAC__StreamMetadata_Picture * block2)836 static FLAC__bool compare_block_data_picture_(const FLAC__StreamMetadata_Picture *block1, const FLAC__StreamMetadata_Picture *block2)
837 {
838 if (block1->type != block2->type)
839 return false;
840 if (block1->mime_type != block2->mime_type && (block1->mime_type == 0 || block2->mime_type == 0 || strcmp(block1->mime_type, block2->mime_type)))
841 return false;
842 if (block1->description != block2->description && (block1->description == 0 || block2->description == 0 || strcmp((const char *)block1->description, (const char *)block2->description)))
843 return false;
844 if (block1->width != block2->width)
845 return false;
846 if (block1->height != block2->height)
847 return false;
848 if (block1->depth != block2->depth)
849 return false;
850 if (block1->colors != block2->colors)
851 return false;
852 if (block1->data_length != block2->data_length)
853 return false;
854 if (block1->data != block2->data && (block1->data == NULL || block2->data == NULL || memcmp(block1->data, block2->data, block1->data_length)))
855 return false;
856 return true;
857 }
858
compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown * block1,const FLAC__StreamMetadata_Unknown * block2,uint32_t block_length)859 static FLAC__bool compare_block_data_unknown_(const FLAC__StreamMetadata_Unknown *block1, const FLAC__StreamMetadata_Unknown *block2, uint32_t block_length)
860 {
861 FLAC__ASSERT(block1 != NULL);
862 FLAC__ASSERT(block2 != NULL);
863
864 if (block1->data != NULL && block2->data != NULL)
865 return memcmp(block1->data, block2->data, block_length) == 0;
866 else
867 return block1->data == block2->data;
868 }
869
FLAC__metadata_object_is_equal(const FLAC__StreamMetadata * block1,const FLAC__StreamMetadata * block2)870 FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2)
871 {
872 FLAC__ASSERT(block1 != NULL);
873 FLAC__ASSERT(block2 != NULL);
874
875 if (block1->type != block2->type) {
876 return false;
877 }
878 if (block1->is_last != block2->is_last) {
879 return false;
880 }
881 if (block1->length != block2->length) {
882 return false;
883 }
884 switch(block1->type) {
885 case FLAC__METADATA_TYPE_STREAMINFO:
886 return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info);
887 case FLAC__METADATA_TYPE_PADDING:
888 return true; /* we don't compare the padding guts */
889 case FLAC__METADATA_TYPE_APPLICATION:
890 return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length);
891 case FLAC__METADATA_TYPE_SEEKTABLE:
892 return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table);
893 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
894 return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment);
895 case FLAC__METADATA_TYPE_CUESHEET:
896 return compare_block_data_cuesheet_(&block1->data.cue_sheet, &block2->data.cue_sheet);
897 case FLAC__METADATA_TYPE_PICTURE:
898 return compare_block_data_picture_(&block1->data.picture, &block2->data.picture);
899 default:
900 return compare_block_data_unknown_(&block1->data.unknown, &block2->data.unknown, block1->length);
901 }
902 }
903
FLAC__metadata_object_application_set_data(FLAC__StreamMetadata * object,FLAC__byte * data,uint32_t length,FLAC__bool copy)904 FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, uint32_t length, FLAC__bool copy)
905 {
906 FLAC__byte *save;
907
908 FLAC__ASSERT(object != NULL);
909 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION);
910 FLAC__ASSERT((data != NULL && length > 0) || (data == NULL && length == 0 && copy == false));
911
912 save = object->data.application.data;
913
914 /* do the copy first so that if we fail we leave the object untouched */
915 if (copy) {
916 if (!copy_bytes_(&object->data.application.data, data, length))
917 return false;
918 }
919 else {
920 object->data.application.data = data;
921 }
922
923 free(save);
924
925 object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length;
926 return true;
927 }
928
FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata * object,uint32_t new_num_points)929 FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, uint32_t new_num_points)
930 {
931 FLAC__ASSERT(object != NULL);
932 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
933
934 if((FLAC__uint64)(new_num_points) * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
935 return false;
936
937 if (object->data.seek_table.points == 0) {
938 FLAC__ASSERT(object->data.seek_table.num_points == 0);
939 if (new_num_points == 0)
940 return true;
941 else if ((object->data.seek_table.points = seekpoint_array_new_(new_num_points)) == 0)
942 return false;
943 }
944 else {
945 const size_t old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
946 const size_t new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint);
947
948 /* overflow check */
949 if (new_num_points > UINT32_MAX / sizeof(FLAC__StreamMetadata_SeekPoint))
950 return false;
951
952 FLAC__ASSERT(object->data.seek_table.num_points > 0);
953
954 if (new_size == 0) {
955 free(object->data.seek_table.points);
956 object->data.seek_table.points = 0;
957 }
958 else {
959 /* Leave object->data.seek_table.points untouched if realloc fails */
960 FLAC__StreamMetadata_SeekPoint *tmpptr;
961 if ((tmpptr = realloc(object->data.seek_table.points, new_size)) == NULL)
962 return false;
963 object->data.seek_table.points = tmpptr;
964 }
965
966 /* if growing, set new elements to placeholders */
967 if (new_size > old_size) {
968 uint32_t i;
969 for (i = object->data.seek_table.num_points; i < new_num_points; i++) {
970 object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
971 object->data.seek_table.points[i].stream_offset = 0;
972 object->data.seek_table.points[i].frame_samples = 0;
973 }
974 }
975 }
976
977 object->data.seek_table.num_points = new_num_points;
978
979 seektable_calculate_length_(object);
980 return true;
981 }
982
FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata * object,uint32_t point_num,FLAC__StreamMetadata_SeekPoint point)983 FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, uint32_t point_num, FLAC__StreamMetadata_SeekPoint point)
984 {
985 FLAC__ASSERT(object != NULL);
986 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
987 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
988
989 object->data.seek_table.points[point_num] = point;
990 }
991
FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata * object,uint32_t point_num,FLAC__StreamMetadata_SeekPoint point)992 FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, uint32_t point_num, FLAC__StreamMetadata_SeekPoint point)
993 {
994 int i;
995
996 FLAC__ASSERT(object != NULL);
997 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
998 FLAC__ASSERT(point_num <= object->data.seek_table.num_points);
999
1000 if (!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1))
1001 return false;
1002
1003 /* move all points >= point_num forward one space */
1004 for (i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--)
1005 object->data.seek_table.points[i] = object->data.seek_table.points[i-1];
1006
1007 FLAC__metadata_object_seektable_set_point(object, point_num, point);
1008 seektable_calculate_length_(object);
1009 return true;
1010 }
1011
FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata * object,uint32_t point_num)1012 FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, uint32_t point_num)
1013 {
1014 uint32_t i;
1015
1016 FLAC__ASSERT(object != NULL);
1017 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1018 FLAC__ASSERT(point_num < object->data.seek_table.num_points);
1019
1020 /* move all points > point_num backward one space */
1021 for (i = point_num; i < object->data.seek_table.num_points-1; i++)
1022 object->data.seek_table.points[i] = object->data.seek_table.points[i+1];
1023
1024 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1);
1025 }
1026
FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata * object)1027 FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object)
1028 {
1029 FLAC__ASSERT(object != NULL);
1030 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1031
1032 return FLAC__format_seektable_is_legal(&object->data.seek_table);
1033 }
1034
FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata * object,uint32_t num)1035 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, uint32_t num)
1036 {
1037 FLAC__ASSERT(object != NULL);
1038 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1039
1040 if (num > 0)
1041 /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */
1042 return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num);
1043 else
1044 return true;
1045 }
1046
FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata * object,FLAC__uint64 sample_number)1047 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number)
1048 {
1049 FLAC__StreamMetadata_SeekTable *seek_table;
1050
1051 FLAC__ASSERT(object != NULL);
1052 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1053
1054 seek_table = &object->data.seek_table;
1055
1056 if (!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1))
1057 return false;
1058
1059 seek_table->points[seek_table->num_points - 1].sample_number = sample_number;
1060 seek_table->points[seek_table->num_points - 1].stream_offset = 0;
1061 seek_table->points[seek_table->num_points - 1].frame_samples = 0;
1062
1063 return true;
1064 }
1065
FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata * object,FLAC__uint64 sample_numbers[],uint32_t num)1066 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], uint32_t num)
1067 {
1068 FLAC__ASSERT(object != NULL);
1069 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1070 FLAC__ASSERT(sample_numbers != 0 || num == 0);
1071
1072 if (num > 0) {
1073 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1074 uint32_t i, j;
1075
1076 i = seek_table->num_points;
1077
1078 if (!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1079 return false;
1080
1081 for (j = 0; j < num; i++, j++) {
1082 seek_table->points[i].sample_number = sample_numbers[j];
1083 seek_table->points[i].stream_offset = 0;
1084 seek_table->points[i].frame_samples = 0;
1085 }
1086 }
1087
1088 return true;
1089 }
1090
FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata * object,uint32_t num,FLAC__uint64 total_samples)1091 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, uint32_t num, FLAC__uint64 total_samples)
1092 {
1093 FLAC__ASSERT(object != NULL);
1094 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1095
1096 if (num > 0 && total_samples > 0) {
1097 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1098 uint32_t i, j;
1099
1100 i = seek_table->num_points;
1101
1102 if (!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num))
1103 return false;
1104
1105 for (j = 0; j < num; i++, j++) {
1106 seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num;
1107 seek_table->points[i].stream_offset = 0;
1108 seek_table->points[i].frame_samples = 0;
1109 }
1110 }
1111
1112 return true;
1113 }
1114
FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata * object,uint32_t samples,FLAC__uint64 total_samples)1115 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, uint32_t samples, FLAC__uint64 total_samples)
1116 {
1117 FLAC__ASSERT(object != NULL);
1118 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1119
1120 if (samples > 0 && total_samples > 0) {
1121 FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table;
1122 uint32_t i, j;
1123 FLAC__uint64 num, sample;
1124
1125 num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */
1126 /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */
1127 if (total_samples % samples == 0)
1128 num--;
1129
1130 /* Put a strict upper bound on the number of allowed seek points. */
1131 if (num > 32768) {
1132 /* Set the bound and recalculate samples accordingly. */
1133 num = 32768;
1134 samples = (uint32_t)(total_samples / num);
1135 }
1136
1137 i = seek_table->num_points;
1138
1139 if (!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (uint32_t)num))
1140 return false;
1141
1142 sample = 0;
1143 for (j = 0; j < num; i++, j++, sample += samples) {
1144 seek_table->points[i].sample_number = sample;
1145 seek_table->points[i].stream_offset = 0;
1146 seek_table->points[i].frame_samples = 0;
1147 }
1148 }
1149
1150 return true;
1151 }
1152
FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata * object,FLAC__bool compact)1153 FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact)
1154 {
1155 uint32_t unique;
1156
1157 FLAC__ASSERT(object != NULL);
1158 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE);
1159
1160 unique = FLAC__format_seektable_sort(&object->data.seek_table);
1161
1162 return !compact || FLAC__metadata_object_seektable_resize_points(object, unique);
1163 }
1164
FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata * object,FLAC__StreamMetadata_VorbisComment_Entry entry,FLAC__bool copy)1165 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1166 {
1167 if (!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length))
1168 return false;
1169 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy);
1170 }
1171
FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata * object,uint32_t new_num_comments)1172 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, uint32_t new_num_comments)
1173 {
1174 FLAC__ASSERT(object != NULL);
1175 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1176
1177 if (object->data.vorbis_comment.comments == NULL) {
1178 FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0);
1179 if (new_num_comments == 0)
1180 return true;
1181 else {
1182 uint32_t i;
1183 if ((object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)) == NULL)
1184 return false;
1185 for (i = 0; i < new_num_comments; i++) {
1186 object->data.vorbis_comment.comments[i].length = 0;
1187 if ((object->data.vorbis_comment.comments[i].entry = safe_malloc_(1)) == NULL) {
1188 object->data.vorbis_comment.num_comments = i+1;
1189 return false;
1190 }
1191 object->data.vorbis_comment.comments[i].entry[0] = '\0';
1192 }
1193 }
1194 }
1195 else {
1196 const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1197 const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry);
1198
1199 /* overflow check */
1200 if (new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry))
1201 return false;
1202
1203 FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
1204
1205 /* if shrinking, free the truncated entries */
1206 if (new_num_comments < object->data.vorbis_comment.num_comments) {
1207 uint32_t i;
1208 for (i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++)
1209 if (object->data.vorbis_comment.comments[i].entry != NULL)
1210 free(object->data.vorbis_comment.comments[i].entry);
1211 }
1212
1213 if (new_size == 0) {
1214 free(object->data.vorbis_comment.comments);
1215 object->data.vorbis_comment.comments = 0;
1216 }
1217 else {
1218 /* Leave object->data.vorbis_comment.comments untouched if realloc fails */
1219 FLAC__StreamMetadata_VorbisComment_Entry *tmpptr;
1220 if ((tmpptr = realloc(object->data.vorbis_comment.comments, new_size)) == NULL)
1221 return false;
1222 object->data.vorbis_comment.comments = tmpptr;
1223 }
1224
1225 /* if growing, zero all the length/pointers of new elements */
1226 if (new_size > old_size) {
1227 uint32_t i;
1228 for (i = object->data.vorbis_comment.num_comments; i < new_num_comments; i++) {
1229 object->data.vorbis_comment.comments[i].length = 0;
1230 if ((object->data.vorbis_comment.comments[i].entry = safe_malloc_(1)) == NULL) {
1231 object->data.vorbis_comment.num_comments = i+1;
1232 return false;
1233 }
1234 object->data.vorbis_comment.comments[i].entry[0] = '\0';
1235 }
1236 }
1237 }
1238
1239 object->data.vorbis_comment.num_comments = new_num_comments;
1240
1241 vorbiscomment_calculate_length_(object);
1242 return true;
1243 }
1244
FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata * object,uint32_t comment_num,FLAC__StreamMetadata_VorbisComment_Entry entry,FLAC__bool copy)1245 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, uint32_t comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1246 {
1247 FLAC__ASSERT(object != NULL);
1248 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1249
1250 if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1251 return false;
1252 return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy);
1253 }
1254
FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata * object,uint32_t comment_num,FLAC__StreamMetadata_VorbisComment_Entry entry,FLAC__bool copy)1255 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, uint32_t comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1256 {
1257 FLAC__StreamMetadata_VorbisComment *vc;
1258 FLAC__StreamMetadata_VorbisComment_Entry temp;
1259
1260 FLAC__ASSERT(object != NULL);
1261 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1262 FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments);
1263
1264 if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1265 return false;
1266
1267 vc = &object->data.vorbis_comment;
1268
1269 if (!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1))
1270 return false;
1271
1272 /* move all comments >= comment_num forward one space */
1273 /* reuse newly added empty comment */
1274 temp = vc->comments[vc->num_comments-1];
1275 memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num));
1276 vc->comments[comment_num] = temp;
1277
1278 return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy);
1279 }
1280
FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata * object,FLAC__StreamMetadata_VorbisComment_Entry entry,FLAC__bool copy)1281 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy)
1282 {
1283 FLAC__ASSERT(object != NULL);
1284 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1285 return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy);
1286 }
1287
FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata * object,FLAC__StreamMetadata_VorbisComment_Entry entry,FLAC__bool all,FLAC__bool copy)1288 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy)
1289 {
1290 FLAC__ASSERT(entry.entry != NULL);
1291
1292 if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1293 return false;
1294
1295 {
1296 int i;
1297 size_t field_name_length;
1298 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1299
1300 if (eq == NULL)
1301 return false; /* double protection */
1302
1303 field_name_length = eq-entry.entry;
1304
1305 i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length);
1306 if (i >= 0) {
1307 uint32_t indx = (uint32_t)i;
1308 if (!FLAC__metadata_object_vorbiscomment_set_comment(object, indx, entry, copy))
1309 return false;
1310 entry = object->data.vorbis_comment.comments[indx];
1311 indx++; /* skip over replaced comment */
1312 if (all && indx < object->data.vorbis_comment.num_comments) {
1313 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
1314 while (i >= 0) {
1315 indx = (uint32_t)i;
1316 if (!FLAC__metadata_object_vorbiscomment_delete_comment(object, indx))
1317 return false;
1318 if (indx < object->data.vorbis_comment.num_comments)
1319 i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length);
1320 else
1321 i = -1;
1322 }
1323 }
1324 return true;
1325 }
1326 else
1327 return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy);
1328 }
1329 }
1330
FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata * object,uint32_t comment_num)1331 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, uint32_t comment_num)
1332 {
1333 FLAC__StreamMetadata_VorbisComment *vc;
1334
1335 FLAC__ASSERT(object != NULL);
1336 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1337 FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments);
1338
1339 vc = &object->data.vorbis_comment;
1340
1341 /* free the comment at comment_num */
1342 free(vc->comments[comment_num].entry);
1343
1344 /* move all comments > comment_num backward one space */
1345 memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1));
1346 vc->comments[vc->num_comments-1].length = 0;
1347 vc->comments[vc->num_comments-1].entry = 0;
1348
1349 return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1);
1350 }
1351
FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(FLAC__StreamMetadata_VorbisComment_Entry * entry,const char * field_name,const char * field_value)1352 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)
1353 {
1354 FLAC__ASSERT(entry != NULL);
1355 FLAC__ASSERT(field_name != NULL);
1356 FLAC__ASSERT(field_value != NULL);
1357
1358 if (!FLAC__format_vorbiscomment_entry_name_is_legal(field_name))
1359 return false;
1360 if (!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (uint32_t)(-1)))
1361 return false;
1362
1363 {
1364 const size_t nn = strlen(field_name);
1365 const size_t nv = strlen(field_value);
1366 entry->length = nn + 1 /*=*/ + nv;
1367 if ((entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)) == NULL)
1368 return false;
1369 memcpy(entry->entry, field_name, nn);
1370 entry->entry[nn] = '=';
1371 memcpy(entry->entry+nn+1, field_value, nv);
1372 entry->entry[entry->length] = '\0';
1373 }
1374
1375 return true;
1376 }
1377
FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(const FLAC__StreamMetadata_VorbisComment_Entry entry,char ** field_name,char ** field_value)1378 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)
1379 {
1380 FLAC__ASSERT(entry.entry != NULL);
1381 FLAC__ASSERT(field_name != NULL);
1382 FLAC__ASSERT(field_value != NULL);
1383
1384 if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length))
1385 return false;
1386
1387 {
1388 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1389 const size_t nn = eq-entry.entry;
1390 const size_t nv = entry.length-nn-1; /* -1 for the '=' */
1391
1392 if (eq == NULL)
1393 return false; /* double protection */
1394 if ((*field_name = safe_malloc_add_2op_(nn, /*+*/1)) == NULL)
1395 return false;
1396 if ((*field_value = safe_malloc_add_2op_(nv, /*+*/1)) == NULL) {
1397 free(*field_name);
1398 return false;
1399 }
1400 memcpy(*field_name, entry.entry, nn);
1401 memcpy(*field_value, entry.entry+nn+1, nv);
1402 (*field_name)[nn] = '\0';
1403 (*field_value)[nv] = '\0';
1404 }
1405
1406 return true;
1407 }
1408
FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry,const char * field_name,uint32_t field_name_length)1409 FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry entry, const char *field_name, uint32_t field_name_length)
1410 {
1411 FLAC__ASSERT(entry.entry != NULL);
1412 {
1413 const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length);
1414 return (eq != NULL && (uint32_t)(eq-entry.entry) == field_name_length && FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length) == 0);
1415 }
1416 }
1417
FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata * object,uint32_t offset,const char * field_name)1418 FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, uint32_t offset, const char *field_name)
1419 {
1420 FLAC__ASSERT(field_name != NULL);
1421
1422 return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name));
1423 }
1424
FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata * object,const char * field_name)1425 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name)
1426 {
1427 const uint32_t field_name_length = strlen(field_name);
1428 uint32_t i;
1429
1430 FLAC__ASSERT(object != NULL);
1431 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1432
1433 for (i = 0; i < object->data.vorbis_comment.num_comments; i++) {
1434 if (FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1435 if (!FLAC__metadata_object_vorbiscomment_delete_comment(object, i))
1436 return -1;
1437 else
1438 return 1;
1439 }
1440 }
1441
1442 return 0;
1443 }
1444
FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata * object,const char * field_name)1445 FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name)
1446 {
1447 FLAC__bool ok = true;
1448 uint32_t matching = 0;
1449 const uint32_t field_name_length = strlen(field_name);
1450 int i;
1451
1452 FLAC__ASSERT(object != NULL);
1453 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
1454
1455 /* must delete from end to start otherwise it will interfere with our iteration */
1456 for (i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) {
1457 if (FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) {
1458 matching++;
1459 ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (uint32_t)i);
1460 }
1461 }
1462
1463 return ok? (int)matching : -1;
1464 }
1465
FLAC__metadata_object_cuesheet_track_new(void)1466 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
1467 {
1468 return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1469 }
1470
FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track * object)1471 FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
1472 {
1473 FLAC__StreamMetadata_CueSheet_Track *to;
1474
1475 FLAC__ASSERT(object != NULL);
1476
1477 if ((to = FLAC__metadata_object_cuesheet_track_new()) != NULL) {
1478 if (!copy_track_(to, object)) {
1479 FLAC__metadata_object_cuesheet_track_delete(to);
1480 return 0;
1481 }
1482 }
1483
1484 return to;
1485 }
1486
FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track * object)1487 void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object)
1488 {
1489 FLAC__ASSERT(object != NULL);
1490
1491 if (object->indices != NULL) {
1492 FLAC__ASSERT(object->num_indices > 0);
1493 free(object->indices);
1494 }
1495 }
1496
FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track * object)1497 FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object)
1498 {
1499 FLAC__metadata_object_cuesheet_track_delete_data(object);
1500 free(object);
1501 }
1502
FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata * object,uint32_t track_num,uint32_t new_num_indices)1503 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, uint32_t track_num, uint32_t new_num_indices)
1504 {
1505 FLAC__StreamMetadata_CueSheet_Track *track;
1506 FLAC__ASSERT(object != NULL);
1507 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1508 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1509
1510 track = &object->data.cue_sheet.tracks[track_num];
1511
1512 if (track->indices == NULL) {
1513 FLAC__ASSERT(track->num_indices == 0);
1514 if (new_num_indices == 0)
1515 return true;
1516 else if ((track->indices = cuesheet_track_index_array_new_(new_num_indices)) == NULL)
1517 return false;
1518 }
1519 else {
1520 const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1521 const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index);
1522
1523 /* overflow check */
1524 if (new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index))
1525 return false;
1526
1527 FLAC__ASSERT(track->num_indices > 0);
1528
1529 if (new_size == 0) {
1530 free(track->indices);
1531 track->indices = 0;
1532 }
1533 else {
1534 /* Leave track->indices untouched if realloc fails */
1535 FLAC__StreamMetadata_CueSheet_Index *tmpptr;
1536 if ((tmpptr = realloc(track->indices, new_size)) == NULL)
1537 return false;
1538 track->indices = tmpptr;
1539 }
1540
1541 /* if growing, zero all the lengths/pointers of new elements */
1542 if (new_size > old_size)
1543 memset(track->indices + track->num_indices, 0, new_size - old_size);
1544 }
1545
1546 track->num_indices = new_num_indices;
1547
1548 cuesheet_calculate_length_(object);
1549 return true;
1550 }
1551
FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata * object,uint32_t track_num,uint32_t index_num,FLAC__StreamMetadata_CueSheet_Index indx)1552 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_index(FLAC__StreamMetadata *object, uint32_t track_num, uint32_t index_num, FLAC__StreamMetadata_CueSheet_Index indx)
1553 {
1554 FLAC__StreamMetadata_CueSheet_Track *track;
1555
1556 FLAC__ASSERT(object != NULL);
1557 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1558 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1559 FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices);
1560
1561 track = &object->data.cue_sheet.tracks[track_num];
1562
1563 if (!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1))
1564 return false;
1565
1566 /* move all indices >= index_num forward one space */
1567 memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num));
1568
1569 track->indices[index_num] = indx;
1570 cuesheet_calculate_length_(object);
1571 return true;
1572 }
1573
FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata * object,uint32_t track_num,uint32_t index_num)1574 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, uint32_t track_num, uint32_t index_num)
1575 {
1576 FLAC__StreamMetadata_CueSheet_Index indx;
1577 memset(&indx, 0, sizeof(indx));
1578 return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, indx);
1579 }
1580
FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata * object,uint32_t track_num,uint32_t index_num)1581 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, uint32_t track_num, uint32_t index_num)
1582 {
1583 FLAC__StreamMetadata_CueSheet_Track *track;
1584
1585 FLAC__ASSERT(object != NULL);
1586 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1587 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1588 FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices);
1589
1590 track = &object->data.cue_sheet.tracks[track_num];
1591
1592 /* move all indices > index_num backward one space */
1593 memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
1594
1595 FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
1596 cuesheet_calculate_length_(object);
1597 return true;
1598 }
1599
FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata * object,uint32_t new_num_tracks)1600 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, uint32_t new_num_tracks)
1601 {
1602 FLAC__ASSERT(object != NULL);
1603 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1604
1605 if (object->data.cue_sheet.tracks == NULL) {
1606 FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0);
1607 if (new_num_tracks == 0)
1608 return true;
1609 else if ((object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)) == NULL)
1610 return false;
1611 }
1612 else {
1613 const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1614 const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track);
1615
1616 /* overflow check */
1617 if (new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track))
1618 return false;
1619
1620 FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0);
1621
1622 /* if shrinking, free the truncated entries */
1623 if (new_num_tracks < object->data.cue_sheet.num_tracks) {
1624 uint32_t i;
1625 for (i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++)
1626 free(object->data.cue_sheet.tracks[i].indices);
1627 }
1628
1629 if (new_size == 0) {
1630 free(object->data.cue_sheet.tracks);
1631 object->data.cue_sheet.tracks = 0;
1632 }
1633 else {
1634 /* Leave object->data.cue_sheet.tracks untouched if realloc fails */
1635 FLAC__StreamMetadata_CueSheet_Track *tmpptr;
1636 if ((tmpptr = realloc(object->data.cue_sheet.tracks, new_size)) == NULL)
1637 return false;
1638 object->data.cue_sheet.tracks = tmpptr;
1639 }
1640
1641 /* if growing, zero all the lengths/pointers of new elements */
1642 if (new_size > old_size)
1643 memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size);
1644 }
1645
1646 object->data.cue_sheet.num_tracks = new_num_tracks;
1647
1648 cuesheet_calculate_length_(object);
1649 return true;
1650 }
1651
FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata * object,uint32_t track_num,FLAC__StreamMetadata_CueSheet_Track * track,FLAC__bool copy)1652 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_set_track(FLAC__StreamMetadata *object, uint32_t track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1653 {
1654 FLAC__ASSERT(object != NULL);
1655 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1656
1657 return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy);
1658 }
1659
FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata * object,uint32_t track_num,FLAC__StreamMetadata_CueSheet_Track * track,FLAC__bool copy)1660 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_track(FLAC__StreamMetadata *object, uint32_t track_num, FLAC__StreamMetadata_CueSheet_Track *track, FLAC__bool copy)
1661 {
1662 FLAC__StreamMetadata_CueSheet *cs;
1663
1664 FLAC__ASSERT(object != NULL);
1665 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1666 FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks);
1667
1668 cs = &object->data.cue_sheet;
1669
1670 if (!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1))
1671 return false;
1672
1673 /* move all tracks >= track_num forward one space */
1674 memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num));
1675 cs->tracks[track_num].num_indices = 0;
1676 cs->tracks[track_num].indices = 0;
1677
1678 return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy);
1679 }
1680
FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata * object,uint32_t track_num)1681 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, uint32_t track_num)
1682 {
1683 FLAC__StreamMetadata_CueSheet_Track track;
1684 memset(&track, 0, sizeof(track));
1685 return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false);
1686 }
1687
FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata * object,uint32_t track_num)1688 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, uint32_t track_num)
1689 {
1690 FLAC__StreamMetadata_CueSheet *cs;
1691
1692 FLAC__ASSERT(object != NULL);
1693 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1694 FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks);
1695
1696 cs = &object->data.cue_sheet;
1697
1698 /* free the track at track_num */
1699 free(cs->tracks[track_num].indices);
1700
1701 /* move all tracks > track_num backward one space */
1702 memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1));
1703 cs->tracks[cs->num_tracks-1].num_indices = 0;
1704 cs->tracks[cs->num_tracks-1].indices = 0;
1705
1706 return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1);
1707 }
1708
FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata * object,FLAC__bool check_cd_da_subset,const char ** violation)1709 FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation)
1710 {
1711 FLAC__ASSERT(object != NULL);
1712 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1713
1714 return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation);
1715 }
1716
get_index_01_offset_(const FLAC__StreamMetadata_CueSheet * cs,uint32_t track)1717 static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, uint32_t track)
1718 {
1719 if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1)
1720 return 0;
1721 else if (cs->tracks[track].indices[0].number == 1)
1722 return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in;
1723 else if (cs->tracks[track].num_indices < 2)
1724 return 0;
1725 else if (cs->tracks[track].indices[1].number == 1)
1726 return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in;
1727 else
1728 return 0;
1729 }
1730
cddb_add_digits_(FLAC__uint32 x)1731 static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x)
1732 {
1733 FLAC__uint32 n = 0;
1734 while (x) {
1735 n += (x%10);
1736 x /= 10;
1737 }
1738 return n;
1739 }
1740
1741 /*@@@@add to tests*/
FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata * object)1742 FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object)
1743 {
1744 const FLAC__StreamMetadata_CueSheet *cs;
1745
1746 FLAC__ASSERT(object != NULL);
1747 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET);
1748
1749 cs = &object->data.cue_sheet;
1750
1751 if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */
1752 return 0;
1753
1754 {
1755 FLAC__uint32 i, length, sum = 0;
1756 for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */
1757 sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100));
1758 length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100);
1759
1760 return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1);
1761 }
1762 }
1763
FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata * object,char * mime_type,FLAC__bool copy)1764 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy)
1765 {
1766 char *old;
1767 size_t old_length, new_length;
1768
1769 FLAC__ASSERT(object != NULL);
1770 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1771 FLAC__ASSERT(mime_type != NULL);
1772
1773 old = object->data.picture.mime_type;
1774 old_length = old? strlen(old) : 0;
1775 new_length = strlen(mime_type);
1776
1777 /* do the copy first so that if we fail we leave the object untouched */
1778 if (copy) {
1779 if (new_length >= SIZE_MAX) /* overflow check */
1780 return false;
1781 if (!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1))
1782 return false;
1783 }
1784 else {
1785 object->data.picture.mime_type = mime_type;
1786 }
1787
1788 free(old);
1789
1790 object->length -= old_length;
1791 object->length += new_length;
1792 return true;
1793 }
1794
FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata * object,FLAC__byte * description,FLAC__bool copy)1795 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy)
1796 {
1797 FLAC__byte *old;
1798 size_t old_length, new_length;
1799
1800 FLAC__ASSERT(object != NULL);
1801 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1802 FLAC__ASSERT(description != NULL);
1803
1804 old = object->data.picture.description;
1805 old_length = old? strlen((const char *)old) : 0;
1806 new_length = strlen((const char *)description);
1807
1808 /* do the copy first so that if we fail we leave the object untouched */
1809 if (copy) {
1810 if (new_length >= SIZE_MAX) /* overflow check */
1811 return false;
1812 if (!copy_bytes_(&object->data.picture.description, description, new_length+1))
1813 return false;
1814 }
1815 else {
1816 object->data.picture.description = description;
1817 }
1818
1819 free(old);
1820
1821 object->length -= old_length;
1822 object->length += new_length;
1823 return true;
1824 }
1825
FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata * object,FLAC__byte * data,FLAC__uint32 length,FLAC__bool copy)1826 FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy)
1827 {
1828 FLAC__byte *old;
1829
1830 FLAC__ASSERT(object != NULL);
1831 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1832 FLAC__ASSERT((data != NULL && length > 0) || (data == NULL && length == 0 && copy == false));
1833
1834 old = object->data.picture.data;
1835
1836 /* do the copy first so that if we fail we leave the object untouched */
1837 if (copy) {
1838 if (!copy_bytes_(&object->data.picture.data, data, length))
1839 return false;
1840 }
1841 else {
1842 object->data.picture.data = data;
1843 }
1844
1845 free(old);
1846
1847 object->length -= object->data.picture.data_length;
1848 object->data.picture.data_length = length;
1849 object->length += length;
1850 return true;
1851 }
1852
FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata * object,const char ** violation)1853 FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation)
1854 {
1855 FLAC__ASSERT(object != NULL);
1856 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE);
1857
1858 return FLAC__format_picture_is_legal(&object->data.picture, violation);
1859 }
1860
FLAC__metadata_object_get_raw(const FLAC__StreamMetadata * object)1861 FLAC_API FLAC__byte * FLAC__metadata_object_get_raw(const FLAC__StreamMetadata *object)
1862 {
1863 FLAC__BitWriter *bw;
1864 const FLAC__byte * buffer;
1865 FLAC__byte * output;
1866 size_t bytes;
1867
1868 FLAC__ASSERT(object != NULL);
1869
1870 if((bw = FLAC__bitwriter_new()) == NULL)
1871 return 0;
1872 if(!FLAC__bitwriter_init(bw)) {
1873 FLAC__bitwriter_delete(bw);
1874 return 0;
1875 }
1876 if(!FLAC__add_metadata_block(object, bw, false)) {
1877 FLAC__bitwriter_delete(bw);
1878 return 0;
1879 }
1880
1881 if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes)) {
1882 FLAC__bitwriter_delete(bw);
1883 return 0;
1884 }
1885
1886 /* Extra check whether length of bitwriter agrees with length of metadata block */
1887 if(bytes != (object->length+FLAC__STREAM_METADATA_HEADER_LENGTH)) {
1888 FLAC__bitwriter_delete(bw);
1889 return 0;
1890 }
1891
1892 output = safe_malloc_(bytes);
1893 if(output == 0) {
1894 FLAC__bitwriter_delete(bw);
1895 return 0;
1896 }
1897
1898 memcpy(output,buffer,bytes);
1899 FLAC__bitwriter_delete(bw);
1900 return output;
1901 }
1902
1903 /* The following callbacks are for FLAC__metadata_object_set_raw */
1904
1905 static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte *buffer, size_t *bytes, void *client_data);
1906 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
1907 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
1908 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
1909
1910 typedef struct {
1911 FLAC__StreamMetadata *object;
1912 FLAC__bool got_error;
1913 FLAC__byte *buffer;
1914 FLAC__int32 length;
1915 FLAC__int32 tell;
1916 } set_raw_client_data;
1917
FLAC__metadata_object_set_raw(FLAC__byte * buffer,FLAC__uint32 length)1918 FLAC_API FLAC__StreamMetadata * FLAC__metadata_object_set_raw(FLAC__byte *buffer, FLAC__uint32 length)
1919 {
1920 set_raw_client_data cd;
1921 FLAC__StreamDecoder * decoder;
1922
1923 cd.buffer = buffer;
1924 cd.length = length;
1925 cd.got_error = false;
1926 cd.object = 0;
1927 cd.tell = -4;
1928
1929 decoder = FLAC__stream_decoder_new();
1930
1931 if(0 == decoder)
1932 return 0;
1933
1934 FLAC__stream_decoder_set_md5_checking(decoder, false);
1935 FLAC__stream_decoder_set_metadata_respond_all(decoder);
1936
1937 if(FLAC__stream_decoder_init_stream(decoder, read_callback_, NULL, NULL, NULL, NULL, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) {
1938 (void)FLAC__stream_decoder_finish(decoder);
1939 FLAC__stream_decoder_delete(decoder);
1940 return 0;
1941 }
1942
1943 if((!FLAC__stream_decoder_process_until_end_of_metadata(decoder) && FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_END_OF_STREAM) || cd.got_error) {
1944 (void)FLAC__stream_decoder_finish(decoder);
1945 FLAC__stream_decoder_delete(decoder);
1946 if(0 != cd.object)
1947 FLAC__metadata_object_delete(cd.object);
1948 return 0;
1949 }
1950
1951 (void)FLAC__stream_decoder_finish(decoder);
1952 FLAC__stream_decoder_delete(decoder);
1953
1954 return cd.object;
1955
1956 }
1957
read_callback_(const FLAC__StreamDecoder * decoder,FLAC__byte * buffer,size_t * bytes,void * client_data)1958 FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte *buffer, size_t *bytes, void *client_data)
1959 {
1960 set_raw_client_data *cd = (set_raw_client_data *)client_data;
1961 (void)decoder;
1962
1963 if(cd->tell == -4) {
1964 if(*bytes < 4)
1965 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
1966 buffer[0] = 'f';
1967 buffer[1] = 'L';
1968 buffer[2] = 'a';
1969 buffer[3] = 'C';
1970 *bytes = 4;
1971 cd->tell = 0;
1972 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
1973 }
1974 else if(cd->tell < 0)
1975 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
1976 else if(cd->tell == cd->length) {
1977 *bytes = 0;
1978 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
1979 }
1980 else {
1981 if((FLAC__int32)(*bytes) > (cd->length - cd->tell))
1982 *bytes = cd->length - cd->tell;
1983 memcpy(buffer, cd->buffer+cd->tell, *bytes);
1984 cd->tell += *bytes;
1985 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
1986 }
1987 }
1988
write_callback_(const FLAC__StreamDecoder * decoder,const FLAC__Frame * frame,const FLAC__int32 * const buffer[],void * client_data)1989 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
1990 {
1991 (void)decoder, (void)frame, (void)buffer, (void)client_data;
1992
1993 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
1994 }
1995
metadata_callback_(const FLAC__StreamDecoder * decoder,const FLAC__StreamMetadata * metadata,void * client_data)1996 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
1997 {
1998 set_raw_client_data *cd = (set_raw_client_data *)client_data;
1999 (void)decoder;
2000
2001 /*
2002 * we assume we only get here when the one metadata block we were
2003 * looking for was passed to us
2004 */
2005 if(!cd->got_error && 0 == cd->object) {
2006 if(0 == (cd->object = FLAC__metadata_object_clone(metadata)))
2007 cd->got_error = true;
2008 }
2009 }
2010
error_callback_(const FLAC__StreamDecoder * decoder,FLAC__StreamDecoderErrorStatus status,void * client_data)2011 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
2012 {
2013 set_raw_client_data *cd = (set_raw_client_data *)client_data;
2014 (void)decoder;
2015
2016 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
2017 cd->got_error = true;
2018 }
2019