| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2001-2009 Josh Coalson |
| 3 | * Copyright (C) 2011-2025 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 | //ppgb #ifdef HAVE_CONFIG_H |
| 34 | #include "flac_config.h" |
| 35 | //ppgb #endif |
| 36 | |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "flac_private_metadata.h" |
| 41 | #include "flac_private_memory.h" |
| 42 | #include "flac_private_stream_encoder_framing.h" |
| 43 | |
| 44 | #include "flac_FLAC_assert.h" |
| 45 | #include "flac_FLAC_stream_decoder.h" |
| 46 | #include "flac_share_alloc.h" |
| 47 | #include "flac_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 | */ |
| 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 */ |
| 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 | */ |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 691 | FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object) |
| 692 | { |
| 693 | FLAC__metadata_object_delete_data(object); |
| 694 | free(object); |
| 695 | } |
| 696 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | if(total_samples < UINT64_MAX / num) { |
| 1106 | /* No risk of overflow */ |
| 1107 | for (j = 0; j < num; i++, j++) { |
| 1108 | seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num; |
| 1109 | seek_table->points[i].stream_offset = 0; |
| 1110 | seek_table->points[i].frame_samples = 0; |
| 1111 | } |
| 1112 | } |
| 1113 | else { |
| 1114 | /* Less precise, but can handle total_samples near UINT64_MAX */ |
| 1115 | for (j = 0; j < num; i++, j++) { |
| 1116 | seek_table->points[i].sample_number = total_samples / (FLAC__uint64)num * (FLAC__uint64)j; |
| 1117 | seek_table->points[i].stream_offset = 0; |
| 1118 | seek_table->points[i].frame_samples = 0; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | return true; |
| 1124 | } |
| 1125 | |
| 1126 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(FLAC__StreamMetadata *object, uint32_t samples, FLAC__uint64 total_samples) |
| 1127 | { |
| 1128 | FLAC__ASSERT(object != NULL); |
| 1129 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1130 | |
| 1131 | if (samples > 0 && total_samples > 0) { |
| 1132 | FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table; |
| 1133 | uint32_t i, j; |
| 1134 | FLAC__uint64 num, sample; |
| 1135 | |
| 1136 | num = 1 + total_samples / samples; /* 1+ for the first sample at 0 */ |
| 1137 | /* now account for the fact that we don't place a seekpoint at "total_samples" since samples are number from 0: */ |
| 1138 | if (total_samples % samples == 0) |
| 1139 | num--; |
| 1140 | |
| 1141 | /* Put a strict upper bound on the number of allowed seek points. */ |
| 1142 | if (num > 32768) { |
| 1143 | /* Set the bound and recalculate samples accordingly. */ |
| 1144 | num = 32768; |
| 1145 | samples = (uint32_t)(total_samples / num); |
| 1146 | } |
| 1147 | |
| 1148 | i = seek_table->num_points; |
| 1149 | |
| 1150 | if (!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + (uint32_t)num)) |
| 1151 | return false; |
| 1152 | |
| 1153 | sample = 0; |
| 1154 | for (j = 0; j < num; i++, j++, sample += samples) { |
| 1155 | seek_table->points[i].sample_number = sample; |
| 1156 | seek_table->points[i].stream_offset = 0; |
| 1157 | seek_table->points[i].frame_samples = 0; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | return true; |
| 1162 | } |
| 1163 | |
| 1164 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact) |
| 1165 | { |
| 1166 | uint32_t unique; |
| 1167 | |
| 1168 | FLAC__ASSERT(object != NULL); |
| 1169 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 1170 | |
| 1171 | unique = FLAC__format_seektable_sort(&object->data.seek_table); |
| 1172 | |
| 1173 | return !compact || FLAC__metadata_object_seektable_resize_points(object, unique); |
| 1174 | } |
| 1175 | |
| 1176 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy) |
| 1177 | { |
| 1178 | if (!FLAC__format_vorbiscomment_entry_value_is_legal(entry.entry, entry.length)) |
| 1179 | return false; |
| 1180 | return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy); |
| 1181 | } |
| 1182 | |
| 1183 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, uint32_t new_num_comments) |
| 1184 | { |
| 1185 | FLAC__ASSERT(object != NULL); |
| 1186 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1187 | |
| 1188 | if (object->data.vorbis_comment.comments == NULL) { |
| 1189 | FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0); |
| 1190 | if (new_num_comments == 0) |
| 1191 | return true; |
| 1192 | else { |
| 1193 | uint32_t i; |
| 1194 | if ((object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments)) == NULL) |
| 1195 | return false; |
| 1196 | for (i = 0; i < new_num_comments; i++) { |
| 1197 | object->data.vorbis_comment.comments[i].length = 0; |
| 1198 | if ((object->data.vorbis_comment.comments[i].entry = safe_malloc_(1)) == NULL) { |
| 1199 | object->data.vorbis_comment.num_comments = i+1; |
| 1200 | return false; |
| 1201 | } |
| 1202 | object->data.vorbis_comment.comments[i].entry[0] = '\0'; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | else { |
| 1207 | const size_t old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
| 1208 | const size_t new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
| 1209 | |
| 1210 | /* overflow check */ |
| 1211 | if (new_num_comments > UINT32_MAX / sizeof(FLAC__StreamMetadata_VorbisComment_Entry)) |
| 1212 | return false; |
| 1213 | |
| 1214 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 1215 | |
| 1216 | /* if shrinking, free the truncated entries */ |
| 1217 | if (new_num_comments < object->data.vorbis_comment.num_comments) { |
| 1218 | uint32_t i; |
| 1219 | for (i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++) |
| 1220 | if (object->data.vorbis_comment.comments[i].entry != NULL) |
| 1221 | free(object->data.vorbis_comment.comments[i].entry); |
| 1222 | } |
| 1223 | |
| 1224 | if (new_size == 0) { |
| 1225 | free(object->data.vorbis_comment.comments); |
| 1226 | object->data.vorbis_comment.comments = 0; |
| 1227 | } |
| 1228 | else { |
| 1229 | /* Leave object->data.vorbis_comment.comments untouched if realloc fails */ |
| 1230 | FLAC__StreamMetadata_VorbisComment_Entry *tmpptr; |
| 1231 | if ((tmpptr = realloc(object->data.vorbis_comment.comments, new_size)) == NULL) |
| 1232 | return false; |
| 1233 | object->data.vorbis_comment.comments = tmpptr; |
| 1234 | } |
| 1235 | |
| 1236 | /* if growing, zero all the length/pointers of new elements */ |
| 1237 | if (new_size > old_size) { |
| 1238 | uint32_t i; |
| 1239 | for (i = object->data.vorbis_comment.num_comments; i < new_num_comments; i++) { |
| 1240 | object->data.vorbis_comment.comments[i].length = 0; |
| 1241 | if ((object->data.vorbis_comment.comments[i].entry = safe_malloc_(1)) == NULL) { |
| 1242 | object->data.vorbis_comment.num_comments = i+1; |
| 1243 | return false; |
| 1244 | } |
| 1245 | object->data.vorbis_comment.comments[i].entry[0] = '\0'; |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | object->data.vorbis_comment.num_comments = new_num_comments; |
| 1251 | |
| 1252 | vorbiscomment_calculate_length_(object); |
| 1253 | return true; |
| 1254 | } |
| 1255 | |
| 1256 | 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) |
| 1257 | { |
| 1258 | FLAC__ASSERT(object != NULL); |
| 1259 | FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments); |
| 1260 | |
| 1261 | if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1262 | return false; |
| 1263 | return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy); |
| 1264 | } |
| 1265 | |
| 1266 | 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) |
| 1267 | { |
| 1268 | FLAC__StreamMetadata_VorbisComment *vc; |
| 1269 | FLAC__StreamMetadata_VorbisComment_Entry temp; |
| 1270 | |
| 1271 | FLAC__ASSERT(object != NULL); |
| 1272 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1273 | FLAC__ASSERT(comment_num <= object->data.vorbis_comment.num_comments); |
| 1274 | |
| 1275 | if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1276 | return false; |
| 1277 | |
| 1278 | vc = &object->data.vorbis_comment; |
| 1279 | |
| 1280 | if (!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1)) |
| 1281 | return false; |
| 1282 | |
| 1283 | /* move all comments >= comment_num forward one space */ |
| 1284 | /* reuse newly added empty comment */ |
| 1285 | temp = vc->comments[vc->num_comments-1]; |
| 1286 | memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num)); |
| 1287 | vc->comments[comment_num] = temp; |
| 1288 | |
| 1289 | return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy); |
| 1290 | } |
| 1291 | |
| 1292 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_append_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy) |
| 1293 | { |
| 1294 | FLAC__ASSERT(object != NULL); |
| 1295 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1296 | return FLAC__metadata_object_vorbiscomment_insert_comment(object, object->data.vorbis_comment.num_comments, entry, copy); |
| 1297 | } |
| 1298 | |
| 1299 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_replace_comment(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool all, FLAC__bool copy) |
| 1300 | { |
| 1301 | FLAC__ASSERT(entry.entry != NULL); |
| 1302 | |
| 1303 | if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1304 | return false; |
| 1305 | |
| 1306 | { |
| 1307 | int i; |
| 1308 | size_t field_name_length; |
| 1309 | const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length); |
| 1310 | |
| 1311 | if (eq == NULL) |
| 1312 | return false; /* double protection */ |
| 1313 | |
| 1314 | field_name_length = eq-entry.entry; |
| 1315 | |
| 1316 | i = vorbiscomment_find_entry_from_(object, 0, (const char *)entry.entry, field_name_length); |
| 1317 | if (i >= 0) { |
| 1318 | uint32_t indx = (uint32_t)i; |
| 1319 | if (!FLAC__metadata_object_vorbiscomment_set_comment(object, indx, entry, copy)) |
| 1320 | return false; |
| 1321 | entry = object->data.vorbis_comment.comments[indx]; |
| 1322 | indx++; /* skip over replaced comment */ |
| 1323 | if (all && indx < object->data.vorbis_comment.num_comments) { |
| 1324 | i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length); |
| 1325 | while (i >= 0) { |
| 1326 | indx = (uint32_t)i; |
| 1327 | if (!FLAC__metadata_object_vorbiscomment_delete_comment(object, indx)) |
| 1328 | return false; |
| 1329 | if (indx < object->data.vorbis_comment.num_comments) |
| 1330 | i = vorbiscomment_find_entry_from_(object, indx, (const char *)entry.entry, field_name_length); |
| 1331 | else |
| 1332 | i = -1; |
| 1333 | } |
| 1334 | } |
| 1335 | return true; |
| 1336 | } |
| 1337 | else |
| 1338 | return FLAC__metadata_object_vorbiscomment_append_comment(object, entry, copy); |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, uint32_t comment_num) |
| 1343 | { |
| 1344 | FLAC__StreamMetadata_VorbisComment *vc; |
| 1345 | |
| 1346 | FLAC__ASSERT(object != NULL); |
| 1347 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1348 | FLAC__ASSERT(comment_num < object->data.vorbis_comment.num_comments); |
| 1349 | |
| 1350 | vc = &object->data.vorbis_comment; |
| 1351 | |
| 1352 | /* free the comment at comment_num */ |
| 1353 | free(vc->comments[comment_num].entry); |
| 1354 | |
| 1355 | /* move all comments > comment_num backward one space */ |
| 1356 | memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1)); |
| 1357 | vc->comments[vc->num_comments-1].length = 0; |
| 1358 | vc->comments[vc->num_comments-1].entry = 0; |
| 1359 | |
| 1360 | return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1); |
| 1361 | } |
| 1362 | |
| 1363 | 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) |
| 1364 | { |
| 1365 | FLAC__ASSERT(entry != NULL); |
| 1366 | FLAC__ASSERT(field_name != NULL); |
| 1367 | FLAC__ASSERT(field_value != NULL); |
| 1368 | |
| 1369 | if (!FLAC__format_vorbiscomment_entry_name_is_legal(field_name)) |
| 1370 | return false; |
| 1371 | if (!FLAC__format_vorbiscomment_entry_value_is_legal((const FLAC__byte *)field_value, (uint32_t)(-1))) |
| 1372 | return false; |
| 1373 | |
| 1374 | { |
| 1375 | const size_t nn = strlen(field_name); |
| 1376 | const size_t nv = strlen(field_value); |
| 1377 | entry->length = nn + 1 /*=*/ + nv; |
| 1378 | if ((entry->entry = safe_malloc_add_4op_(nn, /*+*/1, /*+*/nv, /*+*/1)) == NULL) |
| 1379 | return false; |
| 1380 | memcpy(entry->entry, field_name, nn); |
| 1381 | entry->entry[nn] = '='; |
| 1382 | memcpy(entry->entry+nn+1, field_value, nv); |
| 1383 | entry->entry[entry->length] = '\0'; |
| 1384 | } |
| 1385 | |
| 1386 | return true; |
| 1387 | } |
| 1388 | |
| 1389 | 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) |
| 1390 | { |
| 1391 | FLAC__ASSERT(entry.entry != NULL); |
| 1392 | FLAC__ASSERT(field_name != NULL); |
| 1393 | FLAC__ASSERT(field_value != NULL); |
| 1394 | |
| 1395 | if (!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) |
| 1396 | return false; |
| 1397 | |
| 1398 | { |
| 1399 | const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length); |
| 1400 | const size_t nn = eq-entry.entry; |
| 1401 | const size_t nv = entry.length-nn-1; /* -1 for the '=' */ |
| 1402 | |
| 1403 | if (eq == NULL) |
| 1404 | return false; /* double protection */ |
| 1405 | if ((*field_name = safe_malloc_add_2op_(nn, /*+*/1)) == NULL) |
| 1406 | return false; |
| 1407 | if ((*field_value = safe_malloc_add_2op_(nv, /*+*/1)) == NULL) { |
| 1408 | free(*field_name); |
| 1409 | return false; |
| 1410 | } |
| 1411 | memcpy(*field_name, entry.entry, nn); |
| 1412 | memcpy(*field_value, entry.entry+nn+1, nv); |
| 1413 | (*field_name)[nn] = '\0'; |
| 1414 | (*field_value)[nv] = '\0'; |
| 1415 | } |
| 1416 | |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | 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) |
| 1421 | { |
| 1422 | FLAC__ASSERT(entry.entry != NULL); |
| 1423 | { |
| 1424 | const FLAC__byte *eq = (FLAC__byte*)memchr(entry.entry, '=', entry.length); |
| 1425 | return (eq != NULL && (uint32_t)(eq-entry.entry) == field_name_length && FLAC__STRNCASECMP(field_name, (const char *)entry.entry, field_name_length) == 0); |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, uint32_t offset, const char *field_name) |
| 1430 | { |
| 1431 | FLAC__ASSERT(field_name != NULL); |
| 1432 | |
| 1433 | return vorbiscomment_find_entry_from_(object, offset, field_name, strlen(field_name)); |
| 1434 | } |
| 1435 | |
| 1436 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name) |
| 1437 | { |
| 1438 | const uint32_t field_name_length = strlen(field_name); |
| 1439 | uint32_t i; |
| 1440 | |
| 1441 | FLAC__ASSERT(object != NULL); |
| 1442 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1443 | |
| 1444 | for (i = 0; i < object->data.vorbis_comment.num_comments; i++) { |
| 1445 | if (FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) { |
| 1446 | if (!FLAC__metadata_object_vorbiscomment_delete_comment(object, i)) |
| 1447 | return -1; |
| 1448 | else |
| 1449 | return 1; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name) |
| 1457 | { |
| 1458 | FLAC__bool ok = true; |
| 1459 | uint32_t matching = 0; |
| 1460 | const uint32_t field_name_length = strlen(field_name); |
| 1461 | int i; |
| 1462 | |
| 1463 | FLAC__ASSERT(object != NULL); |
| 1464 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 1465 | |
| 1466 | /* must delete from end to start otherwise it will interfere with our iteration */ |
| 1467 | for (i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) { |
| 1468 | if (FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments[i], field_name, field_name_length)) { |
| 1469 | matching++; |
| 1470 | ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (uint32_t)i); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | return ok? (int)matching : -1; |
| 1475 | } |
| 1476 | |
| 1477 | FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void) |
| 1478 | { |
| 1479 | return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track)); |
| 1480 | } |
| 1481 | |
| 1482 | FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object) |
| 1483 | { |
| 1484 | FLAC__StreamMetadata_CueSheet_Track *to; |
| 1485 | |
| 1486 | FLAC__ASSERT(object != NULL); |
| 1487 | |
| 1488 | if ((to = FLAC__metadata_object_cuesheet_track_new()) != NULL) { |
| 1489 | if (!copy_track_(to, object)) { |
| 1490 | FLAC__metadata_object_cuesheet_track_delete(to); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | return to; |
| 1496 | } |
| 1497 | |
| 1498 | void FLAC__metadata_object_cuesheet_track_delete_data(FLAC__StreamMetadata_CueSheet_Track *object) |
| 1499 | { |
| 1500 | FLAC__ASSERT(object != NULL); |
| 1501 | |
| 1502 | if (object->indices != NULL) { |
| 1503 | FLAC__ASSERT(object->num_indices > 0); |
| 1504 | free(object->indices); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | FLAC_API void FLAC__metadata_object_cuesheet_track_delete(FLAC__StreamMetadata_CueSheet_Track *object) |
| 1509 | { |
| 1510 | FLAC__metadata_object_cuesheet_track_delete_data(object); |
| 1511 | free(object); |
| 1512 | } |
| 1513 | |
| 1514 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__StreamMetadata *object, uint32_t track_num, uint32_t new_num_indices) |
| 1515 | { |
| 1516 | FLAC__StreamMetadata_CueSheet_Track *track; |
| 1517 | FLAC__ASSERT(object != NULL); |
| 1518 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1519 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1520 | |
| 1521 | track = &object->data.cue_sheet.tracks[track_num]; |
| 1522 | |
| 1523 | if (track->indices == NULL) { |
| 1524 | FLAC__ASSERT(track->num_indices == 0); |
| 1525 | if (new_num_indices == 0) |
| 1526 | return true; |
| 1527 | else if ((track->indices = cuesheet_track_index_array_new_(new_num_indices)) == NULL) |
| 1528 | return false; |
| 1529 | } |
| 1530 | else { |
| 1531 | const size_t old_size = track->num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index); |
| 1532 | const size_t new_size = new_num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index); |
| 1533 | |
| 1534 | /* overflow check */ |
| 1535 | if (new_num_indices > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Index)) |
| 1536 | return false; |
| 1537 | |
| 1538 | FLAC__ASSERT(track->num_indices > 0); |
| 1539 | |
| 1540 | if (new_size == 0) { |
| 1541 | free(track->indices); |
| 1542 | track->indices = 0; |
| 1543 | } |
| 1544 | else { |
| 1545 | /* Leave track->indices untouched if realloc fails */ |
| 1546 | FLAC__StreamMetadata_CueSheet_Index *tmpptr; |
| 1547 | if ((tmpptr = realloc(track->indices, new_size)) == NULL) |
| 1548 | return false; |
| 1549 | track->indices = tmpptr; |
| 1550 | } |
| 1551 | |
| 1552 | /* if growing, zero all the lengths/pointers of new elements */ |
| 1553 | if (new_size > old_size) |
| 1554 | memset(track->indices + track->num_indices, 0, new_size - old_size); |
| 1555 | } |
| 1556 | |
| 1557 | track->num_indices = new_num_indices; |
| 1558 | |
| 1559 | cuesheet_calculate_length_(object); |
| 1560 | return true; |
| 1561 | } |
| 1562 | |
| 1563 | 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) |
| 1564 | { |
| 1565 | FLAC__StreamMetadata_CueSheet_Track *track; |
| 1566 | |
| 1567 | FLAC__ASSERT(object != NULL); |
| 1568 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1569 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1570 | FLAC__ASSERT(index_num <= object->data.cue_sheet.tracks[track_num].num_indices); |
| 1571 | |
| 1572 | track = &object->data.cue_sheet.tracks[track_num]; |
| 1573 | |
| 1574 | if (!FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices+1)) |
| 1575 | return false; |
| 1576 | |
| 1577 | /* move all indices >= index_num forward one space */ |
| 1578 | memmove(&track->indices[index_num+1], &track->indices[index_num], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-1-index_num)); |
| 1579 | |
| 1580 | track->indices[index_num] = indx; |
| 1581 | cuesheet_calculate_length_(object); |
| 1582 | return true; |
| 1583 | } |
| 1584 | |
| 1585 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_insert_blank_index(FLAC__StreamMetadata *object, uint32_t track_num, uint32_t index_num) |
| 1586 | { |
| 1587 | FLAC__StreamMetadata_CueSheet_Index indx; |
| 1588 | memset(&indx, 0, sizeof(indx)); |
| 1589 | return FLAC__metadata_object_cuesheet_track_insert_index(object, track_num, index_num, indx); |
| 1590 | } |
| 1591 | |
| 1592 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__StreamMetadata *object, uint32_t track_num, uint32_t index_num) |
| 1593 | { |
| 1594 | FLAC__StreamMetadata_CueSheet_Track *track; |
| 1595 | |
| 1596 | FLAC__ASSERT(object != NULL); |
| 1597 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1598 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1599 | FLAC__ASSERT(index_num < object->data.cue_sheet.tracks[track_num].num_indices); |
| 1600 | |
| 1601 | track = &object->data.cue_sheet.tracks[track_num]; |
| 1602 | |
| 1603 | /* move all indices > index_num backward one space */ |
| 1604 | memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1)); |
| 1605 | |
| 1606 | FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1); |
| 1607 | cuesheet_calculate_length_(object); |
| 1608 | return true; |
| 1609 | } |
| 1610 | |
| 1611 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMetadata *object, uint32_t new_num_tracks) |
| 1612 | { |
| 1613 | FLAC__ASSERT(object != NULL); |
| 1614 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1615 | |
| 1616 | if (object->data.cue_sheet.tracks == NULL) { |
| 1617 | FLAC__ASSERT(object->data.cue_sheet.num_tracks == 0); |
| 1618 | if (new_num_tracks == 0) |
| 1619 | return true; |
| 1620 | else if ((object->data.cue_sheet.tracks = cuesheet_track_array_new_(new_num_tracks)) == NULL) |
| 1621 | return false; |
| 1622 | } |
| 1623 | else { |
| 1624 | const size_t old_size = object->data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track); |
| 1625 | const size_t new_size = new_num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track); |
| 1626 | |
| 1627 | /* overflow check */ |
| 1628 | if (new_num_tracks > UINT32_MAX / sizeof(FLAC__StreamMetadata_CueSheet_Track)) |
| 1629 | return false; |
| 1630 | |
| 1631 | FLAC__ASSERT(object->data.cue_sheet.num_tracks > 0); |
| 1632 | |
| 1633 | /* if shrinking, free the truncated entries */ |
| 1634 | if (new_num_tracks < object->data.cue_sheet.num_tracks) { |
| 1635 | uint32_t i; |
| 1636 | for (i = new_num_tracks; i < object->data.cue_sheet.num_tracks; i++) |
| 1637 | free(object->data.cue_sheet.tracks[i].indices); |
| 1638 | } |
| 1639 | |
| 1640 | if (new_size == 0) { |
| 1641 | free(object->data.cue_sheet.tracks); |
| 1642 | object->data.cue_sheet.tracks = 0; |
| 1643 | } |
| 1644 | else { |
| 1645 | /* Leave object->data.cue_sheet.tracks untouched if realloc fails */ |
| 1646 | FLAC__StreamMetadata_CueSheet_Track *tmpptr; |
| 1647 | if ((tmpptr = realloc(object->data.cue_sheet.tracks, new_size)) == NULL) |
| 1648 | return false; |
| 1649 | object->data.cue_sheet.tracks = tmpptr; |
| 1650 | } |
| 1651 | |
| 1652 | /* if growing, zero all the lengths/pointers of new elements */ |
| 1653 | if (new_size > old_size) |
| 1654 | memset(object->data.cue_sheet.tracks + object->data.cue_sheet.num_tracks, 0, new_size - old_size); |
| 1655 | } |
| 1656 | |
| 1657 | object->data.cue_sheet.num_tracks = new_num_tracks; |
| 1658 | |
| 1659 | cuesheet_calculate_length_(object); |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
| 1663 | 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) |
| 1664 | { |
| 1665 | FLAC__ASSERT(object != NULL); |
| 1666 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1667 | |
| 1668 | return cuesheet_set_track_(object, object->data.cue_sheet.tracks + track_num, track, copy); |
| 1669 | } |
| 1670 | |
| 1671 | 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) |
| 1672 | { |
| 1673 | FLAC__StreamMetadata_CueSheet *cs; |
| 1674 | |
| 1675 | FLAC__ASSERT(object != NULL); |
| 1676 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1677 | FLAC__ASSERT(track_num <= object->data.cue_sheet.num_tracks); |
| 1678 | |
| 1679 | cs = &object->data.cue_sheet; |
| 1680 | |
| 1681 | if (!FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks+1)) |
| 1682 | return false; |
| 1683 | |
| 1684 | /* move all tracks >= track_num forward one space */ |
| 1685 | memmove(&cs->tracks[track_num+1], &cs->tracks[track_num], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-1-track_num)); |
| 1686 | cs->tracks[track_num].num_indices = 0; |
| 1687 | cs->tracks[track_num].indices = 0; |
| 1688 | |
| 1689 | return FLAC__metadata_object_cuesheet_set_track(object, track_num, track, copy); |
| 1690 | } |
| 1691 | |
| 1692 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_insert_blank_track(FLAC__StreamMetadata *object, uint32_t track_num) |
| 1693 | { |
| 1694 | FLAC__StreamMetadata_CueSheet_Track track; |
| 1695 | memset(&track, 0, sizeof(track)); |
| 1696 | return FLAC__metadata_object_cuesheet_insert_track(object, track_num, &track, /*copy=*/false); |
| 1697 | } |
| 1698 | |
| 1699 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_delete_track(FLAC__StreamMetadata *object, uint32_t track_num) |
| 1700 | { |
| 1701 | FLAC__StreamMetadata_CueSheet *cs; |
| 1702 | |
| 1703 | FLAC__ASSERT(object != NULL); |
| 1704 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1705 | FLAC__ASSERT(track_num < object->data.cue_sheet.num_tracks); |
| 1706 | |
| 1707 | cs = &object->data.cue_sheet; |
| 1708 | |
| 1709 | /* free the track at track_num */ |
| 1710 | free(cs->tracks[track_num].indices); |
| 1711 | |
| 1712 | /* move all tracks > track_num backward one space */ |
| 1713 | memmove(&cs->tracks[track_num], &cs->tracks[track_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(cs->num_tracks-track_num-1)); |
| 1714 | cs->tracks[cs->num_tracks-1].num_indices = 0; |
| 1715 | cs->tracks[cs->num_tracks-1].indices = 0; |
| 1716 | |
| 1717 | return FLAC__metadata_object_cuesheet_resize_tracks(object, cs->num_tracks-1); |
| 1718 | } |
| 1719 | |
| 1720 | FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_is_legal(const FLAC__StreamMetadata *object, FLAC__bool check_cd_da_subset, const char **violation) |
| 1721 | { |
| 1722 | FLAC__ASSERT(object != NULL); |
| 1723 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1724 | |
| 1725 | return FLAC__format_cuesheet_is_legal(&object->data.cue_sheet, check_cd_da_subset, violation); |
| 1726 | } |
| 1727 | |
| 1728 | static FLAC__uint64 get_index_01_offset_(const FLAC__StreamMetadata_CueSheet *cs, uint32_t track) |
| 1729 | { |
| 1730 | if (track >= (cs->num_tracks-1) || cs->tracks[track].num_indices < 1) |
| 1731 | return 0; |
| 1732 | else if (cs->tracks[track].indices[0].number == 1) |
| 1733 | return cs->tracks[track].indices[0].offset + cs->tracks[track].offset + cs->lead_in; |
| 1734 | else if (cs->tracks[track].num_indices < 2) |
| 1735 | return 0; |
| 1736 | else if (cs->tracks[track].indices[1].number == 1) |
| 1737 | return cs->tracks[track].indices[1].offset + cs->tracks[track].offset + cs->lead_in; |
| 1738 | else |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | static FLAC__uint32 cddb_add_digits_(FLAC__uint32 x) |
| 1743 | { |
| 1744 | FLAC__uint32 n = 0; |
| 1745 | while (x) { |
| 1746 | n += (x%10); |
| 1747 | x /= 10; |
| 1748 | } |
| 1749 | return n; |
| 1750 | } |
| 1751 | |
| 1752 | /*@@@@add to tests*/ |
| 1753 | FLAC_API FLAC__uint32 FLAC__metadata_object_cuesheet_calculate_cddb_id(const FLAC__StreamMetadata *object) |
| 1754 | { |
| 1755 | const FLAC__StreamMetadata_CueSheet *cs; |
| 1756 | |
| 1757 | FLAC__ASSERT(object != NULL); |
| 1758 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_CUESHEET); |
| 1759 | |
| 1760 | cs = &object->data.cue_sheet; |
| 1761 | |
| 1762 | if (cs->num_tracks < 2) /* need at least one real track and the lead-out track */ |
| 1763 | return 0; |
| 1764 | |
| 1765 | { |
| 1766 | FLAC__uint32 i, length, sum = 0; |
| 1767 | for (i = 0; i < (cs->num_tracks-1); i++) /* -1 to avoid counting the lead-out */ |
| 1768 | sum += cddb_add_digits_((FLAC__uint32)(get_index_01_offset_(cs, i) / 44100)); |
| 1769 | length = (FLAC__uint32)((cs->tracks[cs->num_tracks-1].offset+cs->lead_in) / 44100) - (FLAC__uint32)(get_index_01_offset_(cs, 0) / 44100); |
| 1770 | |
| 1771 | return (sum % 0xFF) << 24 | length << 8 | (FLAC__uint32)(cs->num_tracks-1); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_mime_type(FLAC__StreamMetadata *object, char *mime_type, FLAC__bool copy) |
| 1776 | { |
| 1777 | char *old; |
| 1778 | size_t old_length, new_length; |
| 1779 | |
| 1780 | FLAC__ASSERT(object != NULL); |
| 1781 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1782 | FLAC__ASSERT(mime_type != NULL); |
| 1783 | |
| 1784 | old = object->data.picture.mime_type; |
| 1785 | old_length = old? strlen(old) : 0; |
| 1786 | new_length = strlen(mime_type); |
| 1787 | |
| 1788 | /* do the copy first so that if we fail we leave the object untouched */ |
| 1789 | if (copy) { |
| 1790 | if (new_length >= SIZE_MAX) /* overflow check */ |
| 1791 | return false; |
| 1792 | if (!copy_bytes_((FLAC__byte**)(&object->data.picture.mime_type), (FLAC__byte*)mime_type, new_length+1)) |
| 1793 | return false; |
| 1794 | } |
| 1795 | else { |
| 1796 | object->data.picture.mime_type = mime_type; |
| 1797 | } |
| 1798 | |
| 1799 | free(old); |
| 1800 | |
| 1801 | object->length -= old_length; |
| 1802 | object->length += new_length; |
| 1803 | return true; |
| 1804 | } |
| 1805 | |
| 1806 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_description(FLAC__StreamMetadata *object, FLAC__byte *description, FLAC__bool copy) |
| 1807 | { |
| 1808 | FLAC__byte *old; |
| 1809 | size_t old_length, new_length; |
| 1810 | |
| 1811 | FLAC__ASSERT(object != NULL); |
| 1812 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1813 | FLAC__ASSERT(description != NULL); |
| 1814 | |
| 1815 | old = object->data.picture.description; |
| 1816 | old_length = old? strlen((const char *)old) : 0; |
| 1817 | new_length = strlen((const char *)description); |
| 1818 | |
| 1819 | /* do the copy first so that if we fail we leave the object untouched */ |
| 1820 | if (copy) { |
| 1821 | if (new_length >= SIZE_MAX) /* overflow check */ |
| 1822 | return false; |
| 1823 | if (!copy_bytes_(&object->data.picture.description, description, new_length+1)) |
| 1824 | return false; |
| 1825 | } |
| 1826 | else { |
| 1827 | object->data.picture.description = description; |
| 1828 | } |
| 1829 | |
| 1830 | free(old); |
| 1831 | |
| 1832 | object->length -= old_length; |
| 1833 | object->length += new_length; |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | FLAC_API FLAC__bool FLAC__metadata_object_picture_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, FLAC__uint32 length, FLAC__bool copy) |
| 1838 | { |
| 1839 | FLAC__byte *old; |
| 1840 | |
| 1841 | FLAC__ASSERT(object != NULL); |
| 1842 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1843 | FLAC__ASSERT((data != NULL && length > 0) || (data == NULL && length == 0 && copy == false)); |
| 1844 | |
| 1845 | old = object->data.picture.data; |
| 1846 | |
| 1847 | /* do the copy first so that if we fail we leave the object untouched */ |
| 1848 | if (copy) { |
| 1849 | if (!copy_bytes_(&object->data.picture.data, data, length)) |
| 1850 | return false; |
| 1851 | } |
| 1852 | else { |
| 1853 | object->data.picture.data = data; |
| 1854 | } |
| 1855 | |
| 1856 | free(old); |
| 1857 | |
| 1858 | object->length -= object->data.picture.data_length; |
| 1859 | object->data.picture.data_length = length; |
| 1860 | object->length += length; |
| 1861 | return true; |
| 1862 | } |
| 1863 | |
| 1864 | FLAC_API FLAC__bool FLAC__metadata_object_picture_is_legal(const FLAC__StreamMetadata *object, const char **violation) |
| 1865 | { |
| 1866 | FLAC__ASSERT(object != NULL); |
| 1867 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_PICTURE); |
| 1868 | |
| 1869 | return FLAC__format_picture_is_legal(&object->data.picture, violation); |
| 1870 | } |
| 1871 | |
| 1872 | FLAC_API FLAC__byte * FLAC__metadata_object_get_raw(const FLAC__StreamMetadata *object) |
| 1873 | { |
| 1874 | FLAC__BitWriter *bw; |
| 1875 | const FLAC__byte * buffer; |
| 1876 | FLAC__byte * output; |
| 1877 | size_t bytes; |
| 1878 | |
| 1879 | FLAC__ASSERT(object != NULL); |
| 1880 | |
| 1881 | if((bw = FLAC__bitwriter_new()) == NULL) |
| 1882 | return 0; |
| 1883 | if(!FLAC__bitwriter_init(bw)) { |
| 1884 | FLAC__bitwriter_delete(bw); |
| 1885 | return 0; |
| 1886 | } |
| 1887 | if(!FLAC__add_metadata_block(object, bw, false)) { |
| 1888 | FLAC__bitwriter_delete(bw); |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
| 1892 | if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes)) { |
| 1893 | FLAC__bitwriter_delete(bw); |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | /* Extra check whether length of bitwriter agrees with length of metadata block */ |
| 1898 | if(bytes != (object->length+FLAC__STREAM_METADATA_HEADER_LENGTH)) { |
| 1899 | FLAC__bitwriter_delete(bw); |
| 1900 | return 0; |
| 1901 | } |
| 1902 | |
| 1903 | output = safe_malloc_(bytes); |
| 1904 | if(output == 0) { |
| 1905 | FLAC__bitwriter_delete(bw); |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | memcpy(output,buffer,bytes); |
| 1910 | FLAC__bitwriter_delete(bw); |
| 1911 | return output; |
| 1912 | } |
| 1913 | |
| 1914 | /* The following callbacks are for FLAC__metadata_object_set_raw */ |
| 1915 | |
| 1916 | static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte *buffer, size_t *bytes, void *client_data); |
| 1917 | static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 1918 | static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 1919 | static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
| 1920 | |
| 1921 | typedef struct { |
| 1922 | FLAC__StreamMetadata *object; |
| 1923 | FLAC__bool got_error; |
| 1924 | FLAC__byte *buffer; |
| 1925 | FLAC__int32 length; |
| 1926 | FLAC__int32 tell; |
| 1927 | } set_raw_client_data; |
| 1928 | |
| 1929 | FLAC_API FLAC__StreamMetadata * FLAC__metadata_object_set_raw(FLAC__byte *buffer, FLAC__uint32 length) |
| 1930 | { |
| 1931 | set_raw_client_data cd; |
| 1932 | FLAC__StreamDecoder * decoder; |
| 1933 | |
| 1934 | cd.buffer = buffer; |
| 1935 | cd.length = length; |
| 1936 | cd.got_error = false; |
| 1937 | cd.object = 0; |
| 1938 | cd.tell = -4; |
| 1939 | |
| 1940 | decoder = FLAC__stream_decoder_new(); |
| 1941 | |
| 1942 | if(0 == decoder) |
| 1943 | return 0; |
| 1944 | |
| 1945 | FLAC__stream_decoder_set_md5_checking(decoder, false); |
| 1946 | FLAC__stream_decoder_set_metadata_respond_all(decoder); |
| 1947 | |
| 1948 | 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) { |
| 1949 | (void)FLAC__stream_decoder_finish(decoder); |
| 1950 | FLAC__stream_decoder_delete(decoder); |
| 1951 | return 0; |
| 1952 | } |
| 1953 | |
| 1954 | 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) { |
| 1955 | (void)FLAC__stream_decoder_finish(decoder); |
| 1956 | FLAC__stream_decoder_delete(decoder); |
| 1957 | if(0 != cd.object) |
| 1958 | FLAC__metadata_object_delete(cd.object); |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | (void)FLAC__stream_decoder_finish(decoder); |
| 1963 | FLAC__stream_decoder_delete(decoder); |
| 1964 | |
| 1965 | return cd.object; |
| 1966 | |
| 1967 | } |
| 1968 | |
| 1969 | FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte *buffer, size_t *bytes, void *client_data) |
| 1970 | { |
| 1971 | set_raw_client_data *cd = (set_raw_client_data *)client_data; |
| 1972 | (void)decoder; |
| 1973 | |
| 1974 | if(cd->tell == -4) { |
| 1975 | if(*bytes < 4) |
| 1976 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 1977 | buffer[0] = 'f'; |
| 1978 | buffer[1] = 'L'; |
| 1979 | buffer[2] = 'a'; |
| 1980 | buffer[3] = 'C'; |
| 1981 | *bytes = 4; |
| 1982 | cd->tell = 0; |
| 1983 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 1984 | } |
| 1985 | else if(cd->tell < 0) |
| 1986 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 1987 | else if(cd->tell == cd->length) { |
| 1988 | *bytes = 0; |
| 1989 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 1990 | } |
| 1991 | else { |
| 1992 | if((FLAC__int32)(*bytes) > (cd->length - cd->tell)) |
| 1993 | *bytes = cd->length - cd->tell; |
| 1994 | memcpy(buffer, cd->buffer+cd->tell, *bytes); |
| 1995 | cd->tell += *bytes; |
| 1996 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 2001 | { |
| 2002 | (void)decoder, (void)frame, (void)buffer, (void)client_data; |
| 2003 | |
| 2004 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 2005 | } |
| 2006 | |
| 2007 | void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 2008 | { |
| 2009 | set_raw_client_data *cd = (set_raw_client_data *)client_data; |
| 2010 | (void)decoder; |
| 2011 | |
| 2012 | /* |
| 2013 | * we assume we only get here when the one metadata block we were |
| 2014 | * looking for was passed to us |
| 2015 | */ |
| 2016 | if(!cd->got_error && 0 == cd->object) { |
| 2017 | if(0 == (cd->object = FLAC__metadata_object_clone(metadata))) |
| 2018 | cd->got_error = true; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 2023 | { |
| 2024 | set_raw_client_data *cd = (set_raw_client_data *)client_data; |
| 2025 | (void)decoder; |
| 2026 | |
| 2027 | if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC) |
| 2028 | cd->got_error = true; |
| 2029 | } |