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 | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 114 | FLAC__StreamMetadata_VorbisComment_Entry *object_array; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 115 | |
| 116 | FLAC__ASSERT(num_comments > 0); |
| 117 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 118 | object_array = malloc(num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 119 | |
| 120 | if(0 != object_array) |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 121 | memset(object_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 122 | |
| 123 | return object_array; |
| 124 | } |
| 125 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 126 | 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] | 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 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 140 | 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] | 141 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 142 | FLAC__StreamMetadata_VorbisComment_Entry *return_array; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 157 | memset(return_array, 0, num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | cc68251 | 2002-06-08 04:53:42 +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 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 205 | /*@@@move |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 206 | will return pointer to new empty object of type 'type', or 0 if malloc failed |
| 207 | type is valid type |
| 208 | */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 209 | FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 210 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 211 | FLAC__StreamMetadata *object = malloc(sizeof(FLAC__StreamMetadata)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 212 | if(0 != object) { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 213 | memset(object, 0, sizeof(FLAC__StreamMetadata)); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 241 | /*@@@move |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 245 | FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 246 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 247 | FLAC__StreamMetadata *to; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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: |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 257 | 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] | 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; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 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))) { |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 305 | void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 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 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 334 | /*@@@move |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 335 | frees 'object'. does a deep delete. |
| 336 | */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 337 | void FLAC__metadata_object_delete(FLAC__StreamMetadata *object) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 338 | { |
| 339 | FLAC__metadata_object_delete_data(object); |
| 340 | free(object); |
| 341 | } |
| 342 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 343 | 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] | 344 | { |
| 345 | if(block1->min_blocksize != block2->min_blocksize) |
| 346 | return false; |
| 347 | if(block1->max_blocksize != block2->max_blocksize) |
| 348 | return false; |
| 349 | if(block1->min_framesize != block2->min_framesize) |
| 350 | return false; |
| 351 | if(block1->max_framesize != block2->max_framesize) |
| 352 | return false; |
| 353 | if(block1->sample_rate != block2->sample_rate) |
| 354 | return false; |
| 355 | if(block1->channels != block2->channels) |
| 356 | return false; |
| 357 | if(block1->bits_per_sample != block2->bits_per_sample) |
| 358 | return false; |
| 359 | if(block1->total_samples != block2->total_samples) |
| 360 | return false; |
| 361 | if(0 != memcmp(block1->md5sum, block2->md5sum, 16)) |
| 362 | return false; |
| 363 | return true; |
| 364 | } |
| 365 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 366 | 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] | 367 | { |
| 368 | FLAC__ASSERT(0 != block1); |
| 369 | FLAC__ASSERT(0 != block2); |
| 370 | FLAC__ASSERT(block_length >= sizeof(block1->id)); |
| 371 | |
| 372 | if(0 != memcmp(block1->id, block2->id, sizeof(block1->id))) |
| 373 | return false; |
| 374 | if(0 != block1->data && 0 != block2->data) |
| 375 | return 0 == memcmp(block1->data, block2->data, block_length - sizeof(block1->id)); |
| 376 | else |
| 377 | return block1->data == block2->data; |
| 378 | } |
| 379 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 380 | 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] | 381 | { |
| 382 | unsigned i; |
| 383 | |
| 384 | FLAC__ASSERT(0 != block1); |
| 385 | FLAC__ASSERT(0 != block2); |
| 386 | |
| 387 | if(block1->num_points != block2->num_points) |
| 388 | return false; |
| 389 | |
| 390 | if(0 != block1->points && 0 != block2->points) { |
| 391 | for(i = 0; i < block1->num_points; i++) { |
| 392 | if(block1->points[i].sample_number != block2->points[i].sample_number) |
| 393 | return false; |
| 394 | if(block1->points[i].stream_offset != block2->points[i].stream_offset) |
| 395 | return false; |
| 396 | if(block1->points[i].frame_samples != block2->points[i].frame_samples) |
| 397 | return false; |
| 398 | } |
| 399 | return true; |
| 400 | } |
| 401 | else |
| 402 | return block1->points == block2->points; |
| 403 | } |
| 404 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 405 | 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] | 406 | { |
| 407 | unsigned i; |
| 408 | |
| 409 | if(block1->vendor_string.length != block2->vendor_string.length) |
| 410 | return false; |
| 411 | |
| 412 | if(0 != block1->vendor_string.entry && 0 != block2->vendor_string.entry) { |
| 413 | if(0 != memcmp(block1->vendor_string.entry, block2->vendor_string.entry, block1->vendor_string.length)) |
| 414 | return false; |
| 415 | } |
| 416 | else if(block1->vendor_string.entry != block2->vendor_string.entry) |
| 417 | return false; |
| 418 | |
| 419 | if(block1->num_comments != block2->num_comments) |
| 420 | return false; |
| 421 | |
| 422 | for(i = 0; i < block1->num_comments; i++) { |
| 423 | if(0 != block1->comments[i].entry && 0 != block2->comments[i].entry) { |
| 424 | if(0 != memcmp(block1->comments[i].entry, block2->comments[i].entry, block1->comments[i].length)) |
| 425 | return false; |
| 426 | } |
| 427 | else if(block1->comments[i].entry != block2->comments[i].entry) |
| 428 | return false; |
| 429 | } |
| 430 | return true; |
| 431 | } |
| 432 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 433 | 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] | 434 | { |
Josh Coalson | d8ab346 | 2002-07-11 05:54:05 +0000 | [diff] [blame] | 435 | FLAC__ASSERT(0 != block1); |
| 436 | FLAC__ASSERT(0 != block2); |
| 437 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 438 | if(block1->type != block2->type) { |
| 439 | return false; |
| 440 | } |
| 441 | if(block1->is_last != block2->is_last) { |
| 442 | return false; |
| 443 | } |
| 444 | if(block1->length != block2->length) { |
| 445 | return false; |
| 446 | } |
| 447 | switch(block1->type) { |
| 448 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 449 | return compare_block_data_streaminfo_(&block1->data.stream_info, &block2->data.stream_info); |
| 450 | case FLAC__METADATA_TYPE_PADDING: |
| 451 | return true; /* we don't compare the padding guts */ |
| 452 | case FLAC__METADATA_TYPE_APPLICATION: |
| 453 | return compare_block_data_application_(&block1->data.application, &block2->data.application, block1->length); |
| 454 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 455 | return compare_block_data_seektable_(&block1->data.seek_table, &block2->data.seek_table); |
| 456 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 457 | return compare_block_data_vorbiscomment_(&block1->data.vorbis_comment, &block2->data.vorbis_comment); |
| 458 | default: |
| 459 | FLAC__ASSERT(0); |
| 460 | return false; |
| 461 | } |
| 462 | } |
| 463 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 464 | 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] | 465 | { |
| 466 | FLAC__byte *save; |
| 467 | |
Josh Coalson | d8ab346 | 2002-07-11 05:54:05 +0000 | [diff] [blame] | 468 | FLAC__ASSERT(0 != object); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 469 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_APPLICATION); |
| 470 | FLAC__ASSERT((0 != data && length > 0) || (0 == data && length == 0 && copy == false)); |
| 471 | |
| 472 | save = object->data.application.data; |
| 473 | |
| 474 | /* do the copy first so that if we fail we leave the object untouched */ |
| 475 | if(copy) { |
| 476 | if(!copy_bytes_(&object->data.application.data, data, length)) |
| 477 | return false; |
| 478 | } |
| 479 | else { |
| 480 | object->data.application.data = data; |
| 481 | } |
| 482 | |
| 483 | if(0 != save) |
| 484 | free(save); |
| 485 | |
| 486 | object->length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8 + length; |
| 487 | return true; |
| 488 | } |
| 489 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 490 | 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] | 491 | { |
| 492 | FLAC__ASSERT(0 != object); |
| 493 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 494 | |
| 495 | if(0 == object->data.seek_table.points) { |
| 496 | FLAC__ASSERT(object->data.seek_table.num_points == 0); |
| 497 | if(0 == new_num_points) |
| 498 | return true; |
| 499 | else if(0 == (object->data.seek_table.points = seekpoint_array_new_(new_num_points))) |
| 500 | return false; |
| 501 | } |
| 502 | else { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 503 | const unsigned old_size = object->data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint); |
| 504 | const unsigned new_size = new_num_points * sizeof(FLAC__StreamMetadata_SeekPoint); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 505 | |
| 506 | FLAC__ASSERT(object->data.seek_table.num_points > 0); |
| 507 | |
| 508 | if(new_size == 0) { |
| 509 | free(object->data.seek_table.points); |
| 510 | object->data.seek_table.points = 0; |
| 511 | } |
| 512 | else if(0 == (object->data.seek_table.points = realloc(object->data.seek_table.points, new_size))) |
| 513 | return false; |
| 514 | |
| 515 | /* if growing, set new elements to placeholders */ |
| 516 | if(new_size > old_size) { |
| 517 | unsigned i; |
| 518 | for(i = object->data.seek_table.num_points; i < new_num_points; i++) { |
| 519 | object->data.seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; |
| 520 | object->data.seek_table.points[i].stream_offset = 0; |
| 521 | object->data.seek_table.points[i].frame_samples = 0; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | object->data.seek_table.num_points = new_num_points; |
| 527 | |
| 528 | seektable_calculate_length_(object); |
| 529 | return true; |
| 530 | } |
| 531 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 532 | 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] | 533 | { |
| 534 | FLAC__ASSERT(0 != object); |
| 535 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 536 | FLAC__ASSERT(object->data.seek_table.num_points > point_num); |
| 537 | |
| 538 | object->data.seek_table.points[point_num] = point; |
| 539 | } |
| 540 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 541 | 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] | 542 | { |
| 543 | int i; |
| 544 | |
| 545 | FLAC__ASSERT(0 != object); |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 546 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 547 | FLAC__ASSERT(object->data.seek_table.num_points >= point_num); |
| 548 | |
| 549 | if(!FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points+1)) |
| 550 | return false; |
| 551 | |
| 552 | /* move all points >= point_num forward one space */ |
| 553 | for(i = (int)object->data.seek_table.num_points-1; i > (int)point_num; i--) |
| 554 | object->data.seek_table.points[i] = object->data.seek_table.points[i-1]; |
| 555 | |
| 556 | FLAC__metadata_object_seektable_set_point(object, point_num, point); |
| 557 | seektable_calculate_length_(object); |
| 558 | return true; |
| 559 | } |
| 560 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 561 | 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] | 562 | { |
| 563 | unsigned i; |
| 564 | |
| 565 | FLAC__ASSERT(0 != object); |
| 566 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 567 | FLAC__ASSERT(object->data.seek_table.num_points > point_num); |
| 568 | |
| 569 | /* move all points > point_num backward one space */ |
| 570 | for(i = point_num; i < object->data.seek_table.num_points-1; i++) |
| 571 | object->data.seek_table.points[i] = object->data.seek_table.points[i+1]; |
| 572 | |
| 573 | return FLAC__metadata_object_seektable_resize_points(object, object->data.seek_table.num_points-1); |
| 574 | } |
| 575 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 576 | FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object) |
Josh Coalson | 28e08d8 | 2002-06-05 05:56:41 +0000 | [diff] [blame] | 577 | { |
Josh Coalson | 28e08d8 | 2002-06-05 05:56:41 +0000 | [diff] [blame] | 578 | FLAC__ASSERT(0 != object); |
| 579 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_SEEKTABLE); |
| 580 | |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame^] | 581 | return FLAC__format_seektable_is_legal(&object->data.seek_table); |
Josh Coalson | 28e08d8 | 2002-06-05 05:56:41 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 584 | 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] | 585 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 586 | 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] | 587 | } |
| 588 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 589 | 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] | 590 | { |
| 591 | FLAC__ASSERT(0 != object); |
| 592 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 593 | |
| 594 | if(0 == object->data.vorbis_comment.comments) { |
| 595 | FLAC__ASSERT(object->data.vorbis_comment.num_comments == 0); |
| 596 | if(0 == new_num_comments) |
| 597 | return true; |
| 598 | else if(0 == (object->data.vorbis_comment.comments = vorbiscomment_entry_array_new_(new_num_comments))) |
| 599 | return false; |
| 600 | } |
| 601 | else { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 602 | const unsigned old_size = object->data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
| 603 | const unsigned new_size = new_num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 604 | |
| 605 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); |
| 606 | |
| 607 | /* if shrinking, free the truncated entries */ |
| 608 | if(new_num_comments < object->data.vorbis_comment.num_comments) { |
| 609 | unsigned i; |
| 610 | for(i = new_num_comments; i < object->data.vorbis_comment.num_comments; i++) |
| 611 | if(0 != object->data.vorbis_comment.comments[i].entry) |
| 612 | free(object->data.vorbis_comment.comments[i].entry); |
| 613 | } |
| 614 | |
| 615 | if(new_size == 0) { |
| 616 | free(object->data.vorbis_comment.comments); |
| 617 | object->data.vorbis_comment.comments = 0; |
| 618 | } |
| 619 | else if(0 == (object->data.vorbis_comment.comments = realloc(object->data.vorbis_comment.comments, new_size))) |
| 620 | return false; |
| 621 | |
| 622 | /* if growing, zero all the length/pointers of new elements */ |
| 623 | if(new_size > old_size) |
| 624 | memset(object->data.vorbis_comment.comments + object->data.vorbis_comment.num_comments, 0, new_size - old_size); |
| 625 | } |
| 626 | |
| 627 | object->data.vorbis_comment.num_comments = new_num_comments; |
| 628 | |
| 629 | vorbiscomment_calculate_length_(object); |
| 630 | return true; |
| 631 | } |
| 632 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 633 | 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] | 634 | { |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 635 | 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] | 636 | } |
| 637 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 638 | 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] | 639 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 640 | FLAC__StreamMetadata_VorbisComment *vc; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 641 | |
| 642 | FLAC__ASSERT(0 != object); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 643 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 644 | FLAC__ASSERT(object->data.vorbis_comment.num_comments >= comment_num); |
| 645 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 646 | vc = &object->data.vorbis_comment; |
| 647 | |
| 648 | if(!FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments+1)) |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 649 | return false; |
| 650 | |
| 651 | /* move all comments >= comment_num forward one space */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 652 | 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] | 653 | vc->comments[comment_num].length = 0; |
| 654 | vc->comments[comment_num].entry = 0; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 655 | |
| 656 | return FLAC__metadata_object_vorbiscomment_set_comment(object, comment_num, entry, copy); |
| 657 | } |
| 658 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 659 | 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] | 660 | { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 661 | FLAC__StreamMetadata_VorbisComment *vc; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 662 | |
| 663 | FLAC__ASSERT(0 != object); |
| 664 | FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 665 | FLAC__ASSERT(object->data.vorbis_comment.num_comments > comment_num); |
| 666 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 667 | vc = &object->data.vorbis_comment; |
| 668 | |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 669 | /* free the comment at comment_num */ |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 670 | if(0 != vc->comments[comment_num].entry) |
| 671 | free(vc->comments[comment_num].entry); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 672 | |
| 673 | /* move all comments > comment_num backward one space */ |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 674 | 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] | 675 | vc->comments[vc->num_comments-1].length = 0; |
| 676 | vc->comments[vc->num_comments-1].entry = 0; |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 677 | |
Josh Coalson | 6b8e530 | 2002-05-31 06:23:09 +0000 | [diff] [blame] | 678 | return FLAC__metadata_object_vorbiscomment_resize_comments(object, vc->num_comments-1); |
Josh Coalson | 90ced91 | 2002-05-30 05:23:38 +0000 | [diff] [blame] | 679 | } |