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 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 51 | static FLAC__bool copy_vcentry_(FLAC__StreamMetadata_VorbisComment_Entry *to, const FLAC__StreamMetadata_VorbisComment_Entry *from) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 69 | static void seektable_calculate_length_(FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 77 | static FLAC__StreamMetadata_SeekPoint *seekpoint_array_new_(unsigned num_points) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 78 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 79 | FLAC__StreamMetadata_SeekPoint *object_array; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 80 | |
| 81 | FLAC__ASSERT(num_points > 0); |
| 82 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 83 | object_array = malloc(num_points * sizeof(FLAC__StreamMetadata_SeekPoint)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 97 | static void vorbiscomment_calculate_length_(FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 112 | static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_new_(unsigned num_comments) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 113 | { |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 114 | FLAC__ASSERT(num_comments > 0); |
| 115 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 116 | return calloc(num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 119 | static void vorbiscomment_entry_array_delete_(FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 120 | { |
| 121 | unsigned i; |
| 122 | |
| 123 | FLAC__ASSERT(0 != object_array && num_comments > 0); |
| 124 | |
| 125 | for(i = 0; i < num_comments; i++) |
| 126 | if(0 != object_array[i].entry) |
| 127 | free(object_array[i].entry); |
| 128 | |
| 129 | if(0 != object_array) |
| 130 | free(object_array); |
| 131 | } |
| 132 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 133 | static FLAC__StreamMetadata_VorbisComment_Entry *vorbiscomment_entry_array_copy_(const FLAC__StreamMetadata_VorbisComment_Entry *object_array, unsigned num_comments) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 134 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 135 | FLAC__StreamMetadata_VorbisComment_Entry *return_array; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 136 | |
| 137 | FLAC__ASSERT(0 != object_array); |
| 138 | FLAC__ASSERT(num_comments > 0); |
| 139 | |
| 140 | return_array = vorbiscomment_entry_array_new_(num_comments); |
| 141 | |
| 142 | if(0 != return_array) { |
| 143 | unsigned i; |
| 144 | |
| 145 | /* Need to do this to set the pointers inside the comments to 0. |
| 146 | * In case of an error in the following loop, the object will be |
| 147 | * deleted and we don't want the destructor freeing uninitialized |
| 148 | * pointers. |
| 149 | */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 150 | memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 151 | |
| 152 | for(i = 0; i < num_comments; i++) { |
| 153 | if(!copy_vcentry_(return_array+i, object_array+i)) { |
| 154 | vorbiscomment_entry_array_delete_(return_array, num_comments); |
| 155 | return 0; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return return_array; |
| 161 | } |
| 162 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 163 | 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] | 164 | { |
| 165 | FLAC__byte *save; |
| 166 | |
| 167 | FLAC__ASSERT(0 != object); |
| 168 | FLAC__ASSERT(0 != dest); |
| 169 | FLAC__ASSERT(0 != src); |
| 170 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 171 | FLAC__ASSERT((0 != src->entry && src->length > 0) || (0 == src->entry && src->length == 0 && copy == false)); |
| 172 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 173 | save = dest->entry; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 174 | |
| 175 | /* do the copy first so that if we fail we leave the object untouched */ |
| 176 | if(copy) { |
| 177 | if(!copy_vcentry_(dest, src)) |
| 178 | return false; |
| 179 | } |
| 180 | else { |
| 181 | *dest = *src; |
| 182 | } |
| 183 | |
| 184 | if(0 != save) |
| 185 | free(save); |
| 186 | |
| 187 | vorbiscomment_calculate_length_(object); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /**************************************************************************** |
| 193 | * |
| 194 | * Metadata object routines |
| 195 | * |
| 196 | ***************************************************************************/ |
| 197 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 198 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 199 | { |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 200 | FLAC__StreamMetadata *object = calloc(1, sizeof(FLAC__StreamMetadata)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 201 | if(0 != object) { |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 202 | object->is_last = false; |
| 203 | object->type = type; |
| 204 | switch(type) { |
| 205 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 206 | object->length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 207 | break; |
| 208 | case FLAC__METADATA_TYPE_PADDING: |
| 209 | break; |
| 210 | case FLAC__METADATA_TYPE_APPLICATION: |
| 211 | object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 212 | break; |
| 213 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 214 | break; |
| 215 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
Josh Coalson | 45bb988 | 2002-10-26 04:34:16 +0000 | [diff] [blame] | 216 | { |
| 217 | object->data.vorbis_comment.vendor_string.length = (unsigned)strlen(FLAC__VENDOR_STRING); |
| 218 | if(!copy_bytes_(&object->data.vorbis_comment.vendor_string.entry, (const FLAC__byte*)FLAC__VENDOR_STRING, object->data.vorbis_comment.vendor_string.length)) { |
| 219 | free(object); |
| 220 | return 0; |
| 221 | } |
| 222 | object->length = |
| 223 | (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8) + |
| 224 | object->data.vorbis_comment.vendor_string.length + |
| 225 | (FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN/8) |
| 226 | ; |
| 227 | } |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 228 | break; |
| 229 | default: |
| 230 | /* double protection: */ |
| 231 | FLAC__ASSERT(0); |
| 232 | free(object); |
| 233 | return 0; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return object; |
| 238 | } |
| 239 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 240 | FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 241 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 242 | FLAC__StreamMetadata *to; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 243 | |
| 244 | FLAC__ASSERT(0 != object); |
| 245 | |
| 246 | if(0 != (to = FLAC__metadata_object_new(object->type))) { |
| 247 | to->is_last = object->is_last; |
| 248 | to->type = object->type; |
| 249 | to->length = object->length; |
| 250 | switch(to->type) { |
| 251 | case FLAC__METADATA_TYPE_STREAMINFO: |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 252 | memcpy(&to->data.stream_info, &object->data.stream_info, sizeof(FLAC__StreamMetadata_StreamInfo)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 253 | break; |
| 254 | case FLAC__METADATA_TYPE_PADDING: |
| 255 | break; |
| 256 | case FLAC__METADATA_TYPE_APPLICATION: |
| 257 | memcpy(&to->data.application.id, &object->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8); |
| 258 | if(!copy_bytes_(&to->data.application.data, object->data.application.data, object->length - FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)) { |
| 259 | FLAC__metadata_object_delete(to); |
| 260 | return 0; |
| 261 | } |
| 262 | break; |
| 263 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 264 | to->data.seek_table.num_points = object->data.seek_table.num_points; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 265 | 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))) { |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 266 | FLAC__metadata_object_delete(to); |
| 267 | return 0; |
| 268 | } |
| 269 | break; |
| 270 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
Josh Coalson | 45bb988 | 2002-10-26 04:34:16 +0000 | [diff] [blame] | 271 | if(0 != to->data.vorbis_comment.vendor_string.entry) |
| 272 | free(to->data.vorbis_comment.vendor_string.entry); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 273 | if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) { |
| 274 | FLAC__metadata_object_delete(to); |
| 275 | return 0; |
| 276 | } |
| 277 | if(object->data.vorbis_comment.num_comments == 0) { |
| 278 | FLAC__ASSERT(0 == object->data.vorbis_comment.comments); |
| 279 | to->data.vorbis_comment.comments = 0; |
| 280 | } |
| 281 | else { |
| 282 | FLAC__ASSERT(0 != object->data.vorbis_comment.comments); |
| 283 | to->data.vorbis_comment.comments = vorbiscomment_entry_array_copy_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments); |
| 284 | if(0 == to->data.vorbis_comment.comments) { |
| 285 | FLAC__metadata_object_delete(to); |
| 286 | return 0; |
| 287 | } |
| 288 | } |
| 289 | to->data.vorbis_comment.num_comments = object->data.vorbis_comment.num_comments; |
| 290 | break; |
| 291 | default: |
| 292 | /* double protection: */ |
| 293 | FLAC__ASSERT(0); |
| 294 | free(to); |
| 295 | return 0; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return to; |
| 300 | } |
| 301 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 302 | void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 303 | { |
| 304 | FLAC__ASSERT(0 != object); |
| 305 | |
| 306 | switch(object->type) { |
| 307 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 308 | case FLAC__METADATA_TYPE_PADDING: |
| 309 | break; |
| 310 | case FLAC__METADATA_TYPE_APPLICATION: |
| 311 | if(0 != object->data.application.data) |
| 312 | free(object->data.application.data); |
| 313 | break; |
| 314 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 315 | if(0 != object->data.seek_table.points) |
| 316 | free(object->data.seek_table.points); |
| 317 | break; |
| 318 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 319 | if(0 != object->data.vorbis_comment.vendor_string.entry) |
| 320 | free(object->data.vorbis_comment.vendor_string.entry); |
| 321 | if(0 != object->data.vorbis_comment.comments) { |
| 322 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 323 | vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments); |
| 324 | } |
| 325 | break; |
| 326 | default: |
| 327 | FLAC__ASSERT(0); |
| 328 | } |
| 329 | } |
| 330 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 331 | FLAC_API void FLAC__metadata_object_delete(FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 332 | { |
| 333 | FLAC__metadata_object_delete_data(object); |
| 334 | free(object); |
| 335 | } |
| 336 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 337 | static FLAC__bool compare_block_data_streaminfo_(const FLAC__StreamMetadata_StreamInfo *block1, const FLAC__StreamMetadata_StreamInfo *block2) |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 338 | { |
| 339 | if(block1->min_blocksize != block2->min_blocksize) |
| 340 | return false; |
| 341 | if(block1->max_blocksize != block2->max_blocksize) |
| 342 | return false; |
| 343 | if(block1->min_framesize != block2->min_framesize) |
| 344 | return false; |
| 345 | if(block1->max_framesize != block2->max_framesize) |
| 346 | return false; |
| 347 | if(block1->sample_rate != block2->sample_rate) |
| 348 | return false; |
| 349 | if(block1->channels != block2->channels) |
| 350 | return false; |
| 351 | if(block1->bits_per_sample != block2->bits_per_sample) |
| 352 | return false; |
| 353 | if(block1->total_samples != block2->total_samples) |
| 354 | return false; |
| 355 | if(0 != memcmp(block1->md5sum, block2->md5sum, 16)) |
| 356 | return false; |
| 357 | return true; |
| 358 | } |
| 359 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 360 | static FLAC__bool compare_block_data_application_(const FLAC__StreamMetadata_Application *block1, const FLAC__StreamMetadata_Application *block2, unsigned block_length) |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 361 | { |
| 362 | FLAC__ASSERT(0 != block1); |
| 363 | FLAC__ASSERT(0 != block2); |
| 364 | FLAC__ASSERT(block_length >= sizeof(block1->id)); |
| 365 | |
| 366 | if(0 != memcmp(block1->id, block2->id, sizeof(block1->id))) |
| 367 | return false; |
| 368 | if(0 != block1->data && 0 != block2->data) |
| 369 | return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id)); |
| 370 | else |
| 371 | return block1->data == block2->data; |
| 372 | } |
| 373 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 374 | static FLAC__bool compare_block_data_seektable_(const FLAC__StreamMetadata_SeekTable *block1, const FLAC__StreamMetadata_SeekTable *block2) |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 375 | { |
| 376 | unsigned i; |
| 377 | |
| 378 | FLAC__ASSERT(0 != block1); |
| 379 | FLAC__ASSERT(0 != block2); |
| 380 | |
| 381 | if(block1->num_points != block2->num_points) |
| 382 | return false; |
| 383 | |
| 384 | if(0 != block1->points && 0 != block2->points) { |
| 385 | for(i = 0; i < block1->num_points; i++) { |
| 386 | if(block1->points[i].sample_number != block2->points[i].sample_number) |
| 387 | return false; |
| 388 | if(block1->points[i].stream_offset != block2->points[i].stream_offset) |
| 389 | return false; |
| 390 | if(block1->points[i].frame_samples != block2->points[i].frame_samples) |
| 391 | return false; |
| 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | else |
| 396 | return block1->points == block2->points; |
| 397 | } |
| 398 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 399 | static FLAC__bool compare_block_data_vorbiscomment_(const FLAC__StreamMetadata_VorbisComment *block1, const FLAC__StreamMetadata_VorbisComment *block2) |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 400 | { |
| 401 | unsigned i; |
| 402 | |
| 403 | if(block1->vendor_string.length != block2->vendor_string.length) |
| 404 | return false; |
| 405 | |
| 406 | if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) { |
| 407 | if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length)) |
| 408 | return false; |
| 409 | } |
| 410 | else if(block1->vendor_string.entry != block2->vendor_string.entry) |
| 411 | return false; |
| 412 | |
| 413 | if(block1->num_comments != block2->num_comments) |
| 414 | return false; |
| 415 | |
| 416 | for(i = 0; i < block1->num_comments; i++) { |
| 417 | if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) { |
| 418 | if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length)) |
| 419 | return false; |
| 420 | } |
| 421 | else if(block1->comments[i].entry != block2->comments[i].entry) |
| 422 | return false; |
| 423 | } |
| 424 | return true; |
| 425 | } |
| 426 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 427 | FLAC_API FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2) |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 428 | { |
Josh Coalson | d8ab346 | 2002-07-11 05:54:05 +0000 | [diff] [blame] | 429 | FLAC__ASSERT(0 != block1); |
| 430 | FLAC__ASSERT(0 != block2); |
| 431 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 432 | if(block1->type != block2->type) { |
| 433 | return false; |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 434 | } |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 435 | if(block1->is_last != block2->is_last) { |
| 436 | return false; |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 437 | } |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 438 | if(block1->length != block2->length) { |
| 439 | return false; |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 440 | } |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 441 | switch(block1->type) { |
| 442 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 443 | return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info); |
| 444 | case FLAC__METADATA_TYPE_PADDING: |
| 445 | return true; /* we don't compare the padding guts */ |
| 446 | case FLAC__METADATA_TYPE_APPLICATION: |
| 447 | return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length); |
| 448 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 449 | return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table); |
| 450 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 451 | return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment); |
| 452 | default: |
| 453 | FLAC__ASSERT(0); |
| 454 | return false; |
| 455 | } |
| 456 | } |
| 457 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 458 | FLAC_API FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 459 | { |
| 460 | FLAC__byte *save; |
| 461 | |
Josh Coalson | d8ab346 | 2002-07-11 05:54:05 +0000 | [diff] [blame] | 462 | FLAC__ASSERT(0 != object); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 463 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION); |
| 464 | FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false)); |
| 465 | |
| 466 | save = object->data.application.data; |
| 467 | |
| 468 | /* do the copy first so that if we fail we leave the object untouched */ |
| 469 | if(copy) { |
| 470 | if(!copy_bytes_(&object->data.application.data, data, length)) |
| 471 | return false; |
| 472 | } |
| 473 | else { |
| 474 | object->data.application.data = data; |
| 475 | } |
| 476 | |
| 477 | if(0 != save) |
| 478 | free(save); |
| 479 | |
| 480 | object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length; |
| 481 | return true; |
| 482 | } |
| 483 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 484 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 485 | { |
| 486 | FLAC__ASSERT(0 != object); |
| 487 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 488 | |
| 489 | if(0 == object->data.seek_table.points) { |
| 490 | FLAC__ASSERT(object->data.seek_table.num_points == 0); |
| 491 | if(0 == new_num_points) |
| 492 | return true; |
| 493 | else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points))) |
| 494 | return false; |
| 495 | } |
| 496 | else { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 497 | const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint); |
| 498 | const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 499 | |
| 500 | FLAC__ASSERT(object->data.seek_table.num_points > 0); |
| 501 | |
| 502 | if(new_size == 0) { |
| 503 | free(object->data.seek_table.points); |
| 504 | object->data.seek_table.points = 0; |
| 505 | } |
| 506 | else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size))) |
| 507 | return false; |
| 508 | |
| 509 | /* if growing, set new elements to placeholders */ |
| 510 | if(new_size > old_size) { |
| 511 | unsigned i; |
| 512 | for(i = object->data.seek_table.num_points; i < new_num_points; i++) { |
| 513 | object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 514 | object->data.seek_table.points[i].stream_offset = 0; |
| 515 | object->data.seek_table.points[i].frame_samples = 0; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | object->data.seek_table.num_points = new_num_points; |
| 521 | |
| 522 | seektable_calculate_length_(object); |
| 523 | return true; |
| 524 | } |
| 525 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 526 | FLAC_API void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 527 | { |
| 528 | FLAC__ASSERT(0 != object); |
| 529 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 530 | FLAC__ASSERT(object->data.seek_table.num_points > point_num); |
| 531 | |
| 532 | object->data.seek_table.points[point_num] = point; |
| 533 | } |
| 534 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 535 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 536 | { |
| 537 | int i; |
| 538 | |
| 539 | FLAC__ASSERT(0 != object); |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 540 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 541 | FLAC__ASSERT(object->data.seek_table.num_points >= point_num); |
| 542 | |
| 543 | if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1)) |
| 544 | return false; |
| 545 | |
| 546 | /* move all points >= point_num forward one space */ |
| 547 | for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--) |
| 548 | object->data.seek_table.points[i] = object->data.seek_table.points[i-1]; |
| 549 | |
| 550 | FLAC__metadata_object_seektable_set_point(object, point_num, point); |
| 551 | seektable_calculate_length_(object); |
| 552 | return true; |
| 553 | } |
| 554 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 555 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 556 | { |
| 557 | unsigned i; |
| 558 | |
| 559 | FLAC__ASSERT(0 != object); |
| 560 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 561 | FLAC__ASSERT(object->data.seek_table.num_points > point_num); |
| 562 | |
| 563 | /* move all points > point_num backward one space */ |
| 564 | for(i = point_num; i < object->data.seek_table.num_points-1; i++) |
| 565 | object->data.seek_table.points[i] = object->data.seek_table.points[i+1]; |
| 566 | |
| 567 | return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1); |
| 568 | } |
| 569 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 570 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object) |
Josh Coalson | 28e08d8 | 2002-06-05 05:56:41 +0000 | [diff] [blame] | 571 | { |
Josh Coalson | 28e08d8 | 2002-06-05 05:56:41 +0000 | [diff] [blame] | 572 | FLAC__ASSERT(0 != object); |
| 573 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 574 | |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 575 | return FLAC__format_seektable_is_legal(&object->data.seek_table); |
Josh Coalson | 28e08d8 | 2002-06-05 05:56:41 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 578 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num) |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 579 | { |
| 580 | FLAC__ASSERT(0 != object); |
| 581 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 582 | |
| 583 | if(num > 0) |
| 584 | /* WATCHOUT: we rely on the fact that growing the array adds PLACEHOLDERS at the end */ |
| 585 | return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points + num); |
| 586 | else |
| 587 | return true; |
| 588 | } |
| 589 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 590 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number) |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 591 | { |
| 592 | FLAC__StreamMetadata_SeekTable *seek_table; |
| 593 | |
| 594 | FLAC__ASSERT(0 != object); |
| 595 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 596 | |
| 597 | seek_table = &object->data.seek_table; |
| 598 | |
| 599 | if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + 1)) |
| 600 | return false; |
| 601 | |
| 602 | seek_table->points[seek_table->num_points - 1].sample_number = sample_number; |
| 603 | seek_table->points[seek_table->num_points - 1].stream_offset = 0; |
| 604 | seek_table->points[seek_table->num_points - 1].frame_samples = 0; |
| 605 | |
| 606 | return true; |
| 607 | } |
| 608 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 609 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num) |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 610 | { |
| 611 | FLAC__ASSERT(0 != object); |
| 612 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 613 | FLAC__ASSERT(0 != sample_numbers || num == 0); |
| 614 | |
| 615 | if(num > 0) { |
| 616 | FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table; |
| 617 | unsigned i, j; |
| 618 | |
| 619 | i = seek_table->num_points; |
| 620 | |
| 621 | if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num)) |
| 622 | return false; |
| 623 | |
| 624 | for(j = 0; j < num; i++, j++) { |
| 625 | seek_table->points[i].sample_number = sample_numbers[j]; |
| 626 | seek_table->points[i].stream_offset = 0; |
| 627 | seek_table->points[i].frame_samples = 0; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return true; |
| 632 | } |
| 633 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 634 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples) |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 635 | { |
| 636 | FLAC__ASSERT(0 != object); |
| 637 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 638 | FLAC__ASSERT(total_samples > 0); |
| 639 | |
| 640 | if(num > 0) { |
| 641 | FLAC__StreamMetadata_SeekTable *seek_table = &object->data.seek_table; |
| 642 | unsigned i, j; |
| 643 | |
| 644 | i = seek_table->num_points; |
| 645 | |
| 646 | if(!FLAC__metadata_object_seektable_resize_points(object, seek_table->num_points + num)) |
| 647 | return false; |
| 648 | |
| 649 | for(j = 0; j < num; i++, j++) { |
| 650 | seek_table->points[i].sample_number = total_samples * (FLAC__uint64)j / (FLAC__uint64)num; |
| 651 | seek_table->points[i].stream_offset = 0; |
| 652 | seek_table->points[i].frame_samples = 0; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | return true; |
| 657 | } |
| 658 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 659 | FLAC_API FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact) |
Josh Coalson | 5a5de73 | 2002-08-01 06:37:11 +0000 | [diff] [blame] | 660 | { |
| 661 | unsigned unique; |
| 662 | |
| 663 | FLAC__ASSERT(0 != object); |
| 664 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 665 | |
| 666 | unique = FLAC__format_seektable_sort(&object->data.seek_table); |
| 667 | |
| 668 | return !compact || FLAC__metadata_object_seektable_resize_points(object, unique); |
| 669 | } |
| 670 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 671 | FLAC_API 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] | 672 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 673 | 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] | 674 | } |
| 675 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 676 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 677 | { |
| 678 | FLAC__ASSERT(0 != object); |
| 679 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 680 | |
| 681 | if(0 == object->data.vorbis_comment.comments) { |
| 682 | FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0); |
| 683 | if(0 == new_num_comments) |
| 684 | return true; |
| 685 | else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments))) |
| 686 | return false; |
| 687 | } |
| 688 | else { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 689 | const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
| 690 | const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 691 | |
| 692 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 693 | |
| 694 | /* if shrinking, free the truncated entries */ |
| 695 | if(new_num_comments < object->data.vorbis_comment.num_comments) { |
| 696 | unsigned i; |
| 697 | for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++) |
| 698 | if(0 != object->data.vorbis_comment.comments[i].entry) |
| 699 | free(object->data.vorbis_comment.comments[i].entry); |
| 700 | } |
| 701 | |
| 702 | if(new_size == 0) { |
| 703 | free(object->data.vorbis_comment.comments); |
| 704 | object->data.vorbis_comment.comments = 0; |
| 705 | } |
| 706 | else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size))) |
| 707 | return false; |
| 708 | |
| 709 | /* if growing, zero all the length/pointers of new elements */ |
| 710 | if(new_size > old_size) |
| 711 | memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size); |
| 712 | } |
| 713 | |
| 714 | object->data.vorbis_comment.num_comments = new_num_comments; |
| 715 | |
| 716 | vorbiscomment_calculate_length_(object); |
| 717 | return true; |
| 718 | } |
| 719 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 720 | FLAC_API 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] | 721 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 722 | 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] | 723 | } |
| 724 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 725 | FLAC_API 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] | 726 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 727 | FLAC__StreamMetadata_VorbisComment *vc; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 728 | |
| 729 | FLAC__ASSERT(0 != object); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 730 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 731 | FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num); |
| 732 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 733 | vc = &object->data.vorbis_comment; |
| 734 | |
| 735 | if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1)) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 736 | return false; |
| 737 | |
| 738 | /* move all comments >= comment_num forward one space */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 739 | memmove(&vc->comments[comment_num+1], &vc->comments[comment_num], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-1-comment_num)); |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 740 | vc->comments[comment_num].length = 0; |
| 741 | vc->comments[comment_num].entry = 0; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 742 | |
| 743 | return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy); |
| 744 | } |
| 745 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 746 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 747 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 748 | FLAC__StreamMetadata_VorbisComment *vc; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 749 | |
| 750 | FLAC__ASSERT(0 != object); |
| 751 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 752 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num); |
| 753 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 754 | vc = &object->data.vorbis_comment; |
| 755 | |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 756 | /* free the comment at comment_num */ |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 757 | if(0 != vc->comments[comment_num].entry) |
| 758 | free(vc->comments[comment_num].entry); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 759 | |
| 760 | /* move all comments > comment_num backward one space */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 761 | memmove(&vc->comments[comment_num], &vc->comments[comment_num+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(vc->num_comments-comment_num-1)); |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 762 | vc->comments[vc->num_comments-1].length = 0; |
| 763 | vc->comments[vc->num_comments-1].entry = 0; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 764 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 765 | return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 766 | } |
Josh Coalson | 45bb988 | 2002-10-26 04:34:16 +0000 | [diff] [blame] | 767 | |
| 768 | FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_entry_matches(const FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field_name, unsigned field_name_length) |
| 769 | { |
| 770 | const FLAC__byte *eq = memchr(entry->entry, '=', entry->length); |
| 771 | #if defined _MSC_VER || defined __MINGW32__ |
| 772 | #define FLAC__STRNCASECMP strnicmp |
| 773 | #else |
| 774 | #define FLAC__STRNCASECMP strncasecmp |
| 775 | #endif |
| 776 | return (0 != eq && (unsigned)(eq-entry->entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, entry->entry, field_name_length)); |
| 777 | #undef FLAC__STRNCASECMP |
| 778 | } |
| 779 | |
Josh Coalson | b667e70 | 2002-11-05 07:24:33 +0000 | [diff] [blame^] | 780 | FLAC_API int FLAC__metadata_object_vorbiscomment_find_entry_from(const FLAC__StreamMetadata *object, unsigned offset, const char *field_name) |
Josh Coalson | 45bb988 | 2002-10-26 04:34:16 +0000 | [diff] [blame] | 781 | { |
| 782 | const unsigned field_name_length = strlen(field_name); |
| 783 | unsigned i; |
| 784 | |
| 785 | FLAC__ASSERT(0 != object); |
| 786 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 787 | |
| 788 | for(i = offset; i < object->data.vorbis_comment.num_comments; i++) { |
| 789 | if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments + i, field_name, field_name_length)) |
| 790 | return (int)i; |
| 791 | } |
| 792 | |
| 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entry_matching(FLAC__StreamMetadata *object, const char *field_name) |
| 797 | { |
| 798 | const unsigned field_name_length = strlen(field_name); |
| 799 | unsigned i; |
| 800 | |
| 801 | FLAC__ASSERT(0 != object); |
| 802 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 803 | |
| 804 | for(i = 0; i < object->data.vorbis_comment.num_comments; i++) { |
| 805 | if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments + i, field_name, field_name_length)) { |
| 806 | if(!FLAC__metadata_object_vorbiscomment_delete_comment(object, i)) |
| 807 | return -1; |
| 808 | else |
| 809 | return 1; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__StreamMetadata *object, const char *field_name) |
| 817 | { |
| 818 | FLAC__bool ok = true; |
| 819 | unsigned matching = 0; |
| 820 | const unsigned field_name_length = strlen(field_name); |
| 821 | int i; |
| 822 | |
| 823 | FLAC__ASSERT(0 != object); |
| 824 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 825 | |
| 826 | /* must delete from end to start otherwise it will interfere with our iteration */ |
| 827 | for(i = (int)object->data.vorbis_comment.num_comments - 1; ok && i >= 0; i--) { |
| 828 | if(FLAC__metadata_object_vorbiscomment_entry_matches(object->data.vorbis_comment.comments + i, field_name, field_name_length)) { |
| 829 | matching++; |
| 830 | ok &= FLAC__metadata_object_vorbiscomment_delete_comment(object, (unsigned)i); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | return ok? (int)matching : -1; |
| 835 | } |