Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2001,2002 Josh Coalson |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "private/metadata.h" |
| 24 | |
| 25 | #include "FLAC/assert.h" |
| 26 | |
| 27 | |
| 28 | /**************************************************************************** |
| 29 | * |
| 30 | * Local routines |
| 31 | * |
| 32 | ***************************************************************************/ |
| 33 | |
| 34 | static FLAC__bool copy_bytes_(FLAC__byte **to, const FLAC__byte *from, unsigned bytes) |
| 35 | { |
| 36 | if(bytes > 0 && 0 != from) { |
| 37 | FLAC__byte *x; |
| 38 | if(0 == (x = malloc(bytes))) |
| 39 | return false; |
| 40 | memcpy(x, from, bytes); |
| 41 | *to = x; |
| 42 | } |
| 43 | else { |
| 44 | FLAC__ASSERT(0 == from); |
| 45 | FLAC__ASSERT(bytes == 0); |
| 46 | *to = 0; |
| 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | static FLAC__bool copy_vcentry_(FLAC__StreamMetaData_VorbisComment_Entry *to, const FLAC__StreamMetaData_VorbisComment_Entry *from) |
| 52 | { |
| 53 | to->length = from->length; |
| 54 | if(0 == from->entry) { |
| 55 | FLAC__ASSERT(from->length == 0); |
| 56 | to->entry = 0; |
| 57 | } |
| 58 | else { |
| 59 | FLAC__byte *x; |
| 60 | FLAC__ASSERT(from->length > 0); |
| 61 | if(0 == (x = malloc(from->length))) |
| 62 | return false; |
| 63 | memcpy(x, from->entry, from->length); |
| 64 | to->entry = x; |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | static void seektable_calculate_length_(FLAC__StreamMetaData *object) |
| 70 | { |
| 71 | FLAC__ASSERT(0 != object); |
| 72 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 73 | |
| 74 | object->length = object->data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 75 | } |
| 76 | |
| 77 | static FLAC__StreamMetaData_SeekPoint *seekpoint_array_new_(unsigned num_points) |
| 78 | { |
| 79 | FLAC__StreamMetaData_SeekPoint *object_array; |
| 80 | |
| 81 | FLAC__ASSERT(num_points > 0); |
| 82 | |
| 83 | object_array = malloc(num_points * sizeof(FLAC__StreamMetaData_SeekPoint)); |
| 84 | |
| 85 | if(0 != object_array) { |
| 86 | unsigned i; |
| 87 | for(i = 0; i < num_points; i++) { |
| 88 | object_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 89 | object_array[i].stream_offset = 0; |
| 90 | object_array[i].frame_samples = 0; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return object_array; |
| 95 | } |
| 96 | |
| 97 | static void vorbiscomment_calculate_length_(FLAC__StreamMetaData *object) |
| 98 | { |
| 99 | unsigned i; |
| 100 | |
| 101 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 102 | |
| 103 | object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN) / 8; |
| 104 | object->length += object->data.vorbis_comment.vendor_string.length; |
| 105 | object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8; |
| 106 | for(i = 0; i < object->data.vorbis_comment.num_comments; i++) { |
| 107 | object->length += (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8); |
| 108 | object->length += object->data.vorbis_comment.comments[i].length; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static FLAC__StreamMetaData_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments) |
| 113 | { |
| 114 | FLAC__StreamMetaData_VorbisComment_Entry *object_array; |
| 115 | |
| 116 | FLAC__ASSERT(num_comments > 0); |
| 117 | |
| 118 | object_array = malloc(num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry)); |
| 119 | |
| 120 | if(0 != object_array) |
| 121 | memset(object_array, 0, num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry)); |
| 122 | |
| 123 | return object_array; |
| 124 | } |
| 125 | |
| 126 | static void vorbiscomment_entry_array_delete_(FLAC__StreamMetaData_VorbisComment_Entry *object_array, unsigned num_comments) |
| 127 | { |
| 128 | unsigned i; |
| 129 | |
| 130 | FLAC__ASSERT(0 != object_array && num_comments > 0); |
| 131 | |
| 132 | for(i = 0; i < num_comments; i++) |
| 133 | if(0 != object_array[i].entry) |
| 134 | free(object_array[i].entry); |
| 135 | |
| 136 | if(0 != object_array) |
| 137 | free(object_array); |
| 138 | } |
| 139 | |
| 140 | static FLAC__StreamMetaData_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetaData_VorbisComment_Entry *object_array, unsigned num_comments) |
| 141 | { |
| 142 | FLAC__StreamMetaData_VorbisComment_Entry *return_array; |
| 143 | |
| 144 | FLAC__ASSERT(0 != object_array); |
| 145 | FLAC__ASSERT(num_comments > 0); |
| 146 | |
| 147 | return_array = vorbiscomment_entry_array_new_(num_comments); |
| 148 | |
| 149 | if(0 != return_array) { |
| 150 | unsigned i; |
| 151 | |
| 152 | /* Need to do this to set the pointers inside the comments to 0. |
| 153 | * In case of an error in the following loop, the object will be |
| 154 | * deleted and we don't want the destructor freeing uninitialized |
| 155 | * pointers. |
| 156 | */ |
| 157 | memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry)); |
| 158 | |
| 159 | for(i = 0; i < num_comments; i++) { |
| 160 | if(!copy_vcentry_(return_array+i, object_array+i)) { |
| 161 | vorbiscomment_entry_array_delete_(return_array, num_comments); |
| 162 | return 0; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return return_array; |
| 168 | } |
| 169 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 170 | static FLAC__bool vorbiscomment_set_entry_(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry *dest, const FLAC__StreamMetaData_VorbisComment_Entry *src, FLAC__bool copy) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 171 | { |
| 172 | FLAC__byte *save; |
| 173 | |
| 174 | FLAC__ASSERT(0 != object); |
| 175 | FLAC__ASSERT(0 != dest); |
| 176 | FLAC__ASSERT(0 != src); |
| 177 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 178 | FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0 && copy == false)); |
| 179 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 180 | save = dest->entry; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 181 | |
| 182 | /* do the copy first so that if we fail we leave the object untouched */ |
| 183 | if(copy) { |
| 184 | if(!copy_vcentry_(dest, src)) |
| 185 | return false; |
| 186 | } |
| 187 | else { |
| 188 | *dest = *src; |
| 189 | } |
| 190 | |
| 191 | if(0 != save) |
| 192 | free(save); |
| 193 | |
| 194 | vorbiscomment_calculate_length_(object); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /**************************************************************************** |
| 200 | * |
| 201 | * Metadata object routines |
| 202 | * |
| 203 | ***************************************************************************/ |
| 204 | |
| 205 | /*@@@@move |
| 206 | will return pointer to new empty object of type 'type', or 0 if malloc failed |
| 207 | type is valid type |
| 208 | */ |
| 209 | FLAC__StreamMetaData *FLAC__metadata_object_new(FLAC__MetaDataType type) |
| 210 | { |
| 211 | FLAC__StreamMetaData *object = malloc(sizeof(FLAC__StreamMetaData)); |
| 212 | if(0 != object) { |
| 213 | memset(object, 0, sizeof(FLAC__StreamMetaData)); |
| 214 | object->is_last = false; |
| 215 | object->type = type; |
| 216 | switch(type) { |
| 217 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 218 | object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 219 | break; |
| 220 | case FLAC__METADATA_TYPE_PADDING: |
| 221 | break; |
| 222 | case FLAC__METADATA_TYPE_APPLICATION: |
| 223 | object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 224 | break; |
| 225 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 226 | break; |
| 227 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 228 | object->length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8; |
| 229 | break; |
| 230 | default: |
| 231 | /* double protection: */ |
| 232 | FLAC__ASSERT(0); |
| 233 | free(object); |
| 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return object; |
| 239 | } |
| 240 | |
| 241 | /*@@@@move |
| 242 | return a pointer to a copy of 'object', or 0 if any malloc failed. does a deep copy. user gets ownership of object. |
| 243 | FLAC__ASSERT(0 != object); |
| 244 | */ |
| 245 | FLAC__StreamMetaData *FLAC__metadata_object_copy(const FLAC__StreamMetaData *object) |
| 246 | { |
| 247 | FLAC__StreamMetaData *to; |
| 248 | |
| 249 | FLAC__ASSERT(0 != object); |
| 250 | |
| 251 | if(0 != (to = FLAC__metadata_object_new(object->type))) { |
| 252 | to->is_last = object->is_last; |
| 253 | to->type = object->type; |
| 254 | to->length = object->length; |
| 255 | switch(to->type) { |
| 256 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 257 | memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetaData_StreamInfo)); |
| 258 | break; |
| 259 | case FLAC__METADATA_TYPE_PADDING: |
| 260 | break; |
| 261 | case FLAC__METADATA_TYPE_APPLICATION: |
| 262 | memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8); |
| 263 | if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) { |
| 264 | FLAC__metadata_object_delete(to); |
| 265 | return 0; |
| 266 | } |
| 267 | break; |
| 268 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 269 | to->data.seek_table.num_points = object->data.seek_table.num_points; |
| 270 | 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))) { |
| 271 | FLAC__metadata_object_delete(to); |
| 272 | return 0; |
| 273 | } |
| 274 | break; |
| 275 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 276 | if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) { |
| 277 | FLAC__metadata_object_delete(to); |
| 278 | return 0; |
| 279 | } |
| 280 | if(object->data.vorbis_comment.num_comments == 0) { |
| 281 | FLAC__ASSERT(0 == object->data.vorbis_comment.comments); |
| 282 | to->data.vorbis_comment.comments = 0; |
| 283 | } |
| 284 | else { |
| 285 | FLAC__ASSERT(0 != object->data.vorbis_comment.comments); |
| 286 | to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments); |
| 287 | if(0 == to->data.vorbis_comment.comments) { |
| 288 | FLAC__metadata_object_delete(to); |
| 289 | return 0; |
| 290 | } |
| 291 | } |
| 292 | to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments; |
| 293 | break; |
| 294 | default: |
| 295 | /* double protection: */ |
| 296 | FLAC__ASSERT(0); |
| 297 | free(to); |
| 298 | return 0; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return to; |
| 303 | } |
| 304 | |
| 305 | void FLAC__metadata_object_delete_data(FLAC__StreamMetaData *object) |
| 306 | { |
| 307 | FLAC__ASSERT(0 != object); |
| 308 | |
| 309 | switch(object->type) { |
| 310 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 311 | case FLAC__METADATA_TYPE_PADDING: |
| 312 | break; |
| 313 | case FLAC__METADATA_TYPE_APPLICATION: |
| 314 | if(0 != object->data.application.data) |
| 315 | free(object->data.application.data); |
| 316 | break; |
| 317 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 318 | if(0 != object->data.seek_table.points) |
| 319 | free(object->data.seek_table.points); |
| 320 | break; |
| 321 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 322 | if(0 != object->data.vorbis_comment.vendor_string.entry) |
| 323 | free(object->data.vorbis_comment.vendor_string.entry); |
| 324 | if(0 != object->data.vorbis_comment.comments) { |
| 325 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 326 | vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments); |
| 327 | } |
| 328 | break; |
| 329 | default: |
| 330 | FLAC__ASSERT(0); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /*@@@@move |
| 335 | frees 'object'. does a deep delete. |
| 336 | */ |
| 337 | void FLAC__metadata_object_delete(FLAC__StreamMetaData *object) |
| 338 | { |
| 339 | FLAC__metadata_object_delete_data(object); |
| 340 | free(object); |
| 341 | } |
| 342 | |
| 343 | /*@@@@move |
| 344 | sets the application data to 'data'. if 'copy' is true, makes, copy, else takes ownership of pointer. returns false if copy==true and malloc fails. |
| 345 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION); |
| 346 | FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false)); |
| 347 | */ |
| 348 | FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetaData *object, FLAC__byte *data, unsigned length, FLAC__bool copy) |
| 349 | { |
| 350 | FLAC__byte *save; |
| 351 | |
| 352 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION); |
| 353 | FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false)); |
| 354 | |
| 355 | save = object->data.application.data; |
| 356 | |
| 357 | /* do the copy first so that if we fail we leave the object untouched */ |
| 358 | if(copy) { |
| 359 | if(!copy_bytes_(&object->data.application.data, data, length)) |
| 360 | return false; |
| 361 | } |
| 362 | else { |
| 363 | object->data.application.data = data; |
| 364 | } |
| 365 | |
| 366 | if(0 != save) |
| 367 | free(save); |
| 368 | |
| 369 | object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length; |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetaData *object, unsigned new_num_points) |
| 374 | { |
| 375 | FLAC__ASSERT(0 != object); |
| 376 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 377 | |
| 378 | if(0 == object->data.seek_table.points) { |
| 379 | FLAC__ASSERT(object->data.seek_table.num_points == 0); |
| 380 | if(0 == new_num_points) |
| 381 | return true; |
| 382 | else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points))) |
| 383 | return false; |
| 384 | } |
| 385 | else { |
| 386 | const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint); |
| 387 | const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetaData_SeekPoint); |
| 388 | |
| 389 | FLAC__ASSERT(object->data.seek_table.num_points > 0); |
| 390 | |
| 391 | if(new_size == 0) { |
| 392 | free(object->data.seek_table.points); |
| 393 | object->data.seek_table.points = 0; |
| 394 | } |
| 395 | else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size))) |
| 396 | return false; |
| 397 | |
| 398 | /* if growing, set new elements to placeholders */ |
| 399 | if(new_size > old_size) { |
| 400 | unsigned i; |
| 401 | for(i = object->data.seek_table.num_points; i < new_num_points; i++) { |
| 402 | object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 403 | object->data.seek_table.points[i].stream_offset = 0; |
| 404 | object->data.seek_table.points[i].frame_samples = 0; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | object->data.seek_table.num_points = new_num_points; |
| 410 | |
| 411 | seektable_calculate_length_(object); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetaData *object, unsigned point_num, FLAC__StreamMetaData_SeekPoint point) |
| 416 | { |
| 417 | FLAC__ASSERT(0 != object); |
| 418 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 419 | FLAC__ASSERT(object->data.seek_table.num_points > point_num); |
| 420 | |
| 421 | object->data.seek_table.points[point_num] = point; |
| 422 | } |
| 423 | |
| 424 | FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetaData *object, unsigned point_num, FLAC__StreamMetaData_SeekPoint point) |
| 425 | { |
| 426 | int i; |
| 427 | |
| 428 | FLAC__ASSERT(0 != object); |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 429 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 430 | FLAC__ASSERT(object->data.seek_table.num_points >= point_num); |
| 431 | |
| 432 | if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1)) |
| 433 | return false; |
| 434 | |
| 435 | /* move all points >= point_num forward one space */ |
| 436 | for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--) |
| 437 | object->data.seek_table.points[i] = object->data.seek_table.points[i-1]; |
| 438 | |
| 439 | FLAC__metadata_object_seektable_set_point(object, point_num, point); |
| 440 | seektable_calculate_length_(object); |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetaData *object, unsigned point_num) |
| 445 | { |
| 446 | unsigned i; |
| 447 | |
| 448 | FLAC__ASSERT(0 != object); |
| 449 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 450 | FLAC__ASSERT(object->data.seek_table.num_points > point_num); |
| 451 | |
| 452 | /* move all points > point_num backward one space */ |
| 453 | for(i = point_num; i < object->data.seek_table.num_points-1; i++) |
| 454 | object->data.seek_table.points[i] = object->data.seek_table.points[i+1]; |
| 455 | |
| 456 | return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1); |
| 457 | } |
| 458 | |
Josh Coalson | 28e08d8 | 2002-06-05 05:56:41 +0000 | [diff] [blame^] | 459 | FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetaData *object) |
| 460 | { |
| 461 | unsigned i; |
| 462 | FLAC__uint64 last_sample_number = 0; |
| 463 | FLAC__bool got_last = false; |
| 464 | |
| 465 | FLAC__ASSERT(0 != object); |
| 466 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 467 | |
| 468 | { |
| 469 | const FLAC__StreamMetaData_SeekTable *seek_table = &object->data.seek_table; |
| 470 | |
| 471 | for(i = 0; i < seek_table->num_points; i++) { |
| 472 | if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) { |
| 473 | if(got_last) { |
| 474 | if(seek_table->points[i].sample_number <= last_sample_number) |
| 475 | return false; |
| 476 | } |
| 477 | last_sample_number = seek_table->points[i].sample_number; |
| 478 | got_last = true; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return true; |
| 484 | } |
| 485 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 486 | FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetaData *object, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 487 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 488 | return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.vendor_string, &entry, copy); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetaData *object, unsigned new_num_comments) |
| 492 | { |
| 493 | FLAC__ASSERT(0 != object); |
| 494 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 495 | |
| 496 | if(0 == object->data.vorbis_comment.comments) { |
| 497 | FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0); |
| 498 | if(0 == new_num_comments) |
| 499 | return true; |
| 500 | else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments))) |
| 501 | return false; |
| 502 | } |
| 503 | else { |
| 504 | const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry); |
| 505 | const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry); |
| 506 | |
| 507 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 508 | |
| 509 | /* if shrinking, free the truncated entries */ |
| 510 | if(new_num_comments < object->data.vorbis_comment.num_comments) { |
| 511 | unsigned i; |
| 512 | for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++) |
| 513 | if(0 != object->data.vorbis_comment.comments[i].entry) |
| 514 | free(object->data.vorbis_comment.comments[i].entry); |
| 515 | } |
| 516 | |
| 517 | if(new_size == 0) { |
| 518 | free(object->data.vorbis_comment.comments); |
| 519 | object->data.vorbis_comment.comments = 0; |
| 520 | } |
| 521 | else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size))) |
| 522 | return false; |
| 523 | |
| 524 | /* if growing, zero all the length/pointers of new elements */ |
| 525 | if(new_size > old_size) |
| 526 | memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size); |
| 527 | } |
| 528 | |
| 529 | object->data.vorbis_comment.num_comments = new_num_comments; |
| 530 | |
| 531 | vorbiscomment_calculate_length_(object); |
| 532 | return true; |
| 533 | } |
| 534 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 535 | FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 536 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 537 | return vorbiscomment_set_entry_(object, &object->data.vorbis_comment.comments[comment_num], &entry, copy); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 540 | FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetaData *object, unsigned comment_num, FLAC__StreamMetaData_VorbisComment_Entry entry, FLAC__bool copy) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 541 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 542 | FLAC__StreamMetaData_VorbisComment *vc; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 543 | |
| 544 | FLAC__ASSERT(0 != object); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 545 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 546 | FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num); |
| 547 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 548 | vc = &object->data.vorbis_comment; |
| 549 | |
| 550 | if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1)) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 551 | return false; |
| 552 | |
| 553 | /* move all comments >= comment_num forward one space */ |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 554 | memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(vc->num_comments-1-comment_num)); |
| 555 | vc->comments[comment_num].length = 0; |
| 556 | vc->comments[comment_num].entry = 0; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 557 | |
| 558 | return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy); |
| 559 | } |
| 560 | |
| 561 | FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetaData *object, unsigned comment_num) |
| 562 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 563 | FLAC__StreamMetaData_VorbisComment *vc; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 564 | |
| 565 | FLAC__ASSERT(0 != object); |
| 566 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 567 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num); |
| 568 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 569 | vc = &object->data.vorbis_comment; |
| 570 | |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 571 | /* free the comment at comment_num */ |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 572 | if(0 != vc->comments[comment_num].entry) |
| 573 | free(vc->comments[comment_num].entry); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 574 | |
| 575 | /* move all comments > comment_num backward one space */ |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 576 | memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetaData_VorbisComment_Entry)*(vc->num_comments-comment_num-1)); |
| 577 | vc->comments[vc->num_comments-1].length = 0; |
| 578 | vc->comments[vc->num_comments-1].entry = 0; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 579 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 580 | return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 581 | } |