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