| 1 |
/* libFLAC - Free Lossless Audio Codec library |
| 2 |
* Copyright (C) 2001-2009 Josh Coalson |
| 3 |
* Copyright (C) 2011-2025 Xiph.Org Foundation |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* - Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* - Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
//ppgb #ifdef HAVE_CONFIG_H |
| 34 |
#include "flac_config.h" |
| 35 |
//ppgb #endif |
| 36 |
|
| 37 |
#include <errno.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <stdarg.h> |
| 42 |
|
| 43 |
#include <sys/stat.h> /* for stat(), maybe chmod() */ |
| 44 |
|
| 45 |
#include "flac_private_metadata.h" |
| 46 |
|
| 47 |
#include "flac_FLAC_assert.h" |
| 48 |
#include "flac_FLAC_stream_decoder.h" |
| 49 |
#include "flac_share_alloc.h" |
| 50 |
#include "flac_share_compat.h" |
| 51 |
#include "flac_share_macros.h" |
| 52 |
#include "flac_private_macros.h" |
| 53 |
#include "flac_private_memory.h" |
| 54 |
|
| 55 |
/* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */ |
| 56 |
#define safe_malloc_mul_2op_ safe_malloc_mul_2op_p |
| 57 |
|
| 58 |
/**************************************************************************** |
| 59 |
* |
| 60 |
* Local function declarations |
| 61 |
* |
| 62 |
***************************************************************************/ |
| 63 |
|
| 64 |
static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, uint32_t bytes); |
| 65 |
static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, uint32_t bytes); |
| 66 |
static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, uint32_t bytes); |
| 67 |
static FLAC__uint32 unpack_uint32_(FLAC__byte *b, uint32_t bytes); |
| 68 |
static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, uint32_t bytes); |
| 69 |
static FLAC__uint64 unpack_uint64_(FLAC__byte *b, uint32_t bytes); |
| 70 |
|
| 71 |
static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator); |
| 72 |
static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block); |
| 73 |
static FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, uint32_t *length); |
| 74 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block); |
| 75 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block); |
| 76 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, uint32_t block_length); |
| 77 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, uint32_t block_length); |
| 78 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, uint32_t block_length); |
| 79 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry, uint32_t max_length); |
| 80 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, uint32_t block_length); |
| 81 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track); |
| 82 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block); |
| 83 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block); |
| 84 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, uint32_t block_length); |
| 85 |
|
| 86 |
static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block); |
| 87 |
static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block); |
| 88 |
static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block); |
| 89 |
static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block); |
| 90 |
static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block); |
| 91 |
static FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, uint32_t block_length); |
| 92 |
static FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, uint32_t block_length); |
| 93 |
static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block); |
| 94 |
static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block); |
| 95 |
static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block); |
| 96 |
static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block); |
| 97 |
static FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, uint32_t block_length); |
| 98 |
|
| 99 |
static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block); |
| 100 |
static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, uint32_t padding_length, FLAC__bool padding_is_last); |
| 101 |
static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append); |
| 102 |
|
| 103 |
static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator); |
| 104 |
static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator); |
| 105 |
|
| 106 |
static uint32_t seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb); |
| 107 |
static uint32_t seek_to_first_metadata_block_(FILE *f); |
| 108 |
|
| 109 |
static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append); |
| 110 |
static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup); |
| 111 |
|
| 112 |
static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status); |
| 113 |
static FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status); |
| 114 |
static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status); |
| 115 |
static FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status); |
| 116 |
|
| 117 |
static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status); |
| 118 |
static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status); |
| 119 |
static void cleanup_tempfile_(FILE **tempfile, char **tempfilename); |
| 120 |
|
| 121 |
static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats); |
| 122 |
static void set_file_stats_(const char *filename, struct flac_stat_s *stats); |
| 123 |
|
| 124 |
static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence); |
| 125 |
static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle); |
| 126 |
|
| 127 |
static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status); |
| 128 |
|
| 129 |
|
| 130 |
#ifdef FLAC__VALGRIND_TESTING |
| 131 |
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) |
| 132 |
{ |
| 133 |
size_t ret = fwrite(ptr, size, nmemb, stream); |
| 134 |
if(!ferror(stream)) |
| 135 |
fflush(stream); |
| 136 |
return ret; |
| 137 |
} |
| 138 |
#else |
| 139 |
#define local__fwrite fwrite |
| 140 |
#endif |
| 141 |
|
| 142 |
/**************************************************************************** |
| 143 |
* |
| 144 |
* Level 0 implementation |
| 145 |
* |
| 146 |
***************************************************************************/ |
| 147 |
|
| 148 |
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 149 |
static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 150 |
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
| 151 |
|
| 152 |
typedef struct { |
| 153 |
FLAC__bool got_error; |
| 154 |
FLAC__StreamMetadata *object; |
| 155 |
} level0_client_data; |
| 156 |
|
| 157 |
static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type) |
| 158 |
{ |
| 159 |
level0_client_data cd; |
| 160 |
FLAC__StreamDecoder *decoder; |
| 161 |
FILE *file; |
| 162 |
FLAC__bool open_as_ogg = false; |
| 163 |
|
| 164 |
FLAC__ASSERT(0 != filename); |
| 165 |
|
| 166 |
cd.got_error = false; |
| 167 |
cd.object = 0; |
| 168 |
|
| 169 |
decoder = FLAC__stream_decoder_new(); |
| 170 |
|
| 171 |
if(0 == decoder) |
| 172 |
return 0; |
| 173 |
|
| 174 |
FLAC__stream_decoder_set_md5_checking(decoder, false); |
| 175 |
FLAC__stream_decoder_set_metadata_ignore_all(decoder); |
| 176 |
FLAC__stream_decoder_set_metadata_respond(decoder, type); |
| 177 |
|
| 178 |
/* Try to open the file and determine whether it is Ogg */ |
| 179 |
if(0 != (file = flac_fopen(filename, "rb"))) { |
| 180 |
FLAC__byte signature[4]; |
| 181 |
if(fread(&signature, 1, 4, file) == 4) { |
| 182 |
if(memcmp(&signature,"OggS",4) == 0) |
| 183 |
open_as_ogg = true; |
| 184 |
} |
| 185 |
fclose(file); |
| 186 |
} |
| 187 |
|
| 188 |
if(!open_as_ogg) { |
| 189 |
if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) { |
| 190 |
(void)FLAC__stream_decoder_finish(decoder); |
| 191 |
FLAC__stream_decoder_delete(decoder); |
| 192 |
return 0; |
| 193 |
} |
| 194 |
} |
| 195 |
else { |
| 196 |
if(FLAC__stream_decoder_init_ogg_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) { |
| 197 |
(void)FLAC__stream_decoder_finish(decoder); |
| 198 |
FLAC__stream_decoder_delete(decoder); |
| 199 |
return 0; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) { |
| 204 |
(void)FLAC__stream_decoder_finish(decoder); |
| 205 |
FLAC__stream_decoder_delete(decoder); |
| 206 |
if(0 != cd.object) |
| 207 |
FLAC__metadata_object_delete(cd.object); |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
(void)FLAC__stream_decoder_finish(decoder); |
| 212 |
FLAC__stream_decoder_delete(decoder); |
| 213 |
|
| 214 |
return cd.object; |
| 215 |
} |
| 216 |
|
| 217 |
FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo) |
| 218 |
{ |
| 219 |
FLAC__StreamMetadata *object; |
| 220 |
|
| 221 |
FLAC__ASSERT(0 != filename); |
| 222 |
FLAC__ASSERT(0 != streaminfo); |
| 223 |
|
| 224 |
object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO); |
| 225 |
|
| 226 |
if (object) { |
| 227 |
/* can just copy the contents since STREAMINFO has no internal structure */ |
| 228 |
*streaminfo = *object; |
| 229 |
FLAC__metadata_object_delete(object); |
| 230 |
return true; |
| 231 |
} |
| 232 |
else { |
| 233 |
return false; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags) |
| 238 |
{ |
| 239 |
FLAC__ASSERT(0 != filename); |
| 240 |
FLAC__ASSERT(0 != tags); |
| 241 |
|
| 242 |
*tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 243 |
|
| 244 |
return 0 != *tags; |
| 245 |
} |
| 246 |
|
| 247 |
FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet) |
| 248 |
{ |
| 249 |
FLAC__ASSERT(0 != filename); |
| 250 |
FLAC__ASSERT(0 != cuesheet); |
| 251 |
|
| 252 |
*cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET); |
| 253 |
|
| 254 |
return 0 != *cuesheet; |
| 255 |
} |
| 256 |
|
| 257 |
FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 258 |
{ |
| 259 |
(void)decoder, (void)frame, (void)buffer, (void)client_data; |
| 260 |
|
| 261 |
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 262 |
} |
| 263 |
|
| 264 |
void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 265 |
{ |
| 266 |
level0_client_data *cd = (level0_client_data *)client_data; |
| 267 |
(void)decoder; |
| 268 |
|
| 269 |
/* |
| 270 |
* we assume we only get here when the one metadata block we were |
| 271 |
* looking for was passed to us |
| 272 |
*/ |
| 273 |
if(!cd->got_error && 0 == cd->object) { |
| 274 |
if(0 == (cd->object = FLAC__metadata_object_clone(metadata))) |
| 275 |
cd->got_error = true; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 280 |
{ |
| 281 |
level0_client_data *cd = (level0_client_data *)client_data; |
| 282 |
(void)decoder; |
| 283 |
|
| 284 |
if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC) |
| 285 |
cd->got_error = true; |
| 286 |
} |
| 287 |
|
| 288 |
FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, uint32_t max_width, uint32_t max_height, uint32_t max_depth, uint32_t max_colors) |
| 289 |
{ |
| 290 |
FLAC__Metadata_SimpleIterator *it; |
| 291 |
FLAC__uint64 max_area_seen = 0; |
| 292 |
FLAC__uint64 max_depth_seen = 0; |
| 293 |
|
| 294 |
FLAC__ASSERT(0 != filename); |
| 295 |
FLAC__ASSERT(0 != picture); |
| 296 |
|
| 297 |
*picture = 0; |
| 298 |
|
| 299 |
it = FLAC__metadata_simple_iterator_new(); |
| 300 |
if(0 == it) |
| 301 |
return false; |
| 302 |
if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) { |
| 303 |
FLAC__metadata_simple_iterator_delete(it); |
| 304 |
return false; |
| 305 |
} |
| 306 |
do { |
| 307 |
if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) { |
| 308 |
FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it); |
| 309 |
if(0 != obj) { |
| 310 |
FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height; |
| 311 |
|
| 312 |
/* check constraints */ |
| 313 |
if( |
| 314 |
(type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) && |
| 315 |
(mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) && |
| 316 |
(description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) && |
| 317 |
obj->data.picture.width <= max_width && |
| 318 |
obj->data.picture.height <= max_height && |
| 319 |
obj->data.picture.depth <= max_depth && |
| 320 |
obj->data.picture.colors <= max_colors && |
| 321 |
(area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen)) |
| 322 |
) { |
| 323 |
if(*picture) |
| 324 |
FLAC__metadata_object_delete(*picture); |
| 325 |
*picture = obj; |
| 326 |
max_area_seen = area; |
| 327 |
max_depth_seen = obj->data.picture.depth; |
| 328 |
} |
| 329 |
else { |
| 330 |
FLAC__metadata_object_delete(obj); |
| 331 |
} |
| 332 |
} |
| 333 |
else |
| 334 |
break; |
| 335 |
} |
| 336 |
} while(FLAC__metadata_simple_iterator_next(it)); |
| 337 |
|
| 338 |
FLAC__metadata_simple_iterator_delete(it); |
| 339 |
|
| 340 |
return (0 != *picture); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
/**************************************************************************** |
| 345 |
* |
| 346 |
* Level 1 implementation |
| 347 |
* |
| 348 |
***************************************************************************/ |
| 349 |
|
| 350 |
#define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4) |
| 351 |
/* 1 for initial offset, +4 for our own personal use */ |
| 352 |
|
| 353 |
struct FLAC__Metadata_SimpleIterator { |
| 354 |
FILE *file; |
| 355 |
char *filename, *tempfile_path_prefix; |
| 356 |
struct flac_stat_s stats; |
| 357 |
FLAC__bool has_stats; |
| 358 |
FLAC__bool is_writable; |
| 359 |
FLAC__Metadata_SimpleIteratorStatus status; |
| 360 |
FLAC__off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH]; |
| 361 |
FLAC__off_t first_offset; /* this is the offset to the STREAMINFO block */ |
| 362 |
uint32_t depth; |
| 363 |
/* this is the metadata block header of the current block we are pointing to: */ |
| 364 |
FLAC__bool is_last; |
| 365 |
FLAC__MetadataType type; |
| 366 |
uint32_t length; |
| 367 |
}; |
| 368 |
|
| 369 |
FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = { |
| 370 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK", |
| 371 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT", |
| 372 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE", |
| 373 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE", |
| 374 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE", |
| 375 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA", |
| 376 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR", |
| 377 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR", |
| 378 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR", |
| 379 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR", |
| 380 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR", |
| 381 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR", |
| 382 |
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR" |
| 383 |
}; |
| 384 |
|
| 385 |
|
| 386 |
FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void) |
| 387 |
{ |
| 388 |
FLAC__Metadata_SimpleIterator *iterator = calloc(1, sizeof(FLAC__Metadata_SimpleIterator)); |
| 389 |
|
| 390 |
if(0 != iterator) { |
| 391 |
iterator->file = 0; |
| 392 |
iterator->filename = 0; |
| 393 |
iterator->tempfile_path_prefix = 0; |
| 394 |
iterator->has_stats = false; |
| 395 |
iterator->is_writable = false; |
| 396 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 397 |
iterator->first_offset = iterator->offset[0] = -1; |
| 398 |
iterator->depth = 0; |
| 399 |
} |
| 400 |
|
| 401 |
return iterator; |
| 402 |
} |
| 403 |
|
| 404 |
static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator) |
| 405 |
{ |
| 406 |
FLAC__ASSERT(0 != iterator); |
| 407 |
|
| 408 |
if(0 != iterator->file) { |
| 409 |
fclose(iterator->file); |
| 410 |
iterator->file = 0; |
| 411 |
if(iterator->has_stats) |
| 412 |
set_file_stats_(iterator->filename, &iterator->stats); |
| 413 |
} |
| 414 |
if(0 != iterator->filename) { |
| 415 |
free(iterator->filename); |
| 416 |
iterator->filename = 0; |
| 417 |
} |
| 418 |
if(0 != iterator->tempfile_path_prefix) { |
| 419 |
free(iterator->tempfile_path_prefix); |
| 420 |
iterator->tempfile_path_prefix = 0; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator) |
| 425 |
{ |
| 426 |
FLAC__ASSERT(0 != iterator); |
| 427 |
|
| 428 |
simple_iterator_free_guts_(iterator); |
| 429 |
free(iterator); |
| 430 |
} |
| 431 |
|
| 432 |
FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator) |
| 433 |
{ |
| 434 |
FLAC__Metadata_SimpleIteratorStatus status; |
| 435 |
|
| 436 |
FLAC__ASSERT(0 != iterator); |
| 437 |
|
| 438 |
status = iterator->status; |
| 439 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 440 |
return status; |
| 441 |
} |
| 442 |
|
| 443 |
static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only) |
| 444 |
{ |
| 445 |
uint32_t ret; |
| 446 |
|
| 447 |
FLAC__ASSERT(0 != iterator); |
| 448 |
|
| 449 |
if(read_only || 0 == (iterator->file = flac_fopen(iterator->filename, "r+b"))) { |
| 450 |
iterator->is_writable = false; |
| 451 |
if(read_only || errno == EACCES) { |
| 452 |
if(0 == (iterator->file = flac_fopen(iterator->filename, "rb"))) { |
| 453 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 454 |
return false; |
| 455 |
} |
| 456 |
} |
| 457 |
else { |
| 458 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 459 |
return false; |
| 460 |
} |
| 461 |
} |
| 462 |
else { |
| 463 |
iterator->is_writable = true; |
| 464 |
} |
| 465 |
|
| 466 |
ret = seek_to_first_metadata_block_(iterator->file); |
| 467 |
switch(ret) { |
| 468 |
case 0: |
| 469 |
iterator->depth = 0; |
| 470 |
iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file); |
| 471 |
ret = read_metadata_block_header_(iterator); |
| 472 |
/* The first metadata block must be a streaminfo. If this is not the |
| 473 |
* case, the file is invalid and assumptions made elsewhere in the |
| 474 |
* code are invalid */ |
| 475 |
if(iterator->type != FLAC__METADATA_TYPE_STREAMINFO) { |
| 476 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 477 |
return false; |
| 478 |
} |
| 479 |
return ret; |
| 480 |
case 1: |
| 481 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 482 |
return false; |
| 483 |
case 2: |
| 484 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 485 |
return false; |
| 486 |
case 3: |
| 487 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE; |
| 488 |
return false; |
| 489 |
default: |
| 490 |
FLAC__ASSERT(0); |
| 491 |
return false; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
#if 0 |
| 496 |
@@@ If we decide to finish implementing this, put this comment back in metadata.h |
| 497 |
/* |
| 498 |
* The 'tempfile_path_prefix' allows you to specify a directory where |
| 499 |
* tempfiles should go. Remember that if your metadata edits cause the |
| 500 |
* FLAC file to grow, the entire file will have to be rewritten. If |
| 501 |
* 'tempfile_path_prefix' is NULL, the temp file will be written in the |
| 502 |
* same directory as the original FLAC file. This makes replacing the |
| 503 |
* original with the tempfile fast but requires extra space in the same |
| 504 |
* partition for the tempfile. If space is a problem, you can pass a |
| 505 |
* directory name belonging to a different partition in |
| 506 |
* 'tempfile_path_prefix'. Note that you should use the forward slash |
| 507 |
* '/' as the directory separator. A trailing slash is not needed; it |
| 508 |
* will be added automatically. |
| 509 |
*/ |
| 510 |
FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix); |
| 511 |
#endif |
| 512 |
|
| 513 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats) |
| 514 |
{ |
| 515 |
const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'flac_rename(...)' for what it will take to finish implementing this */ |
| 516 |
|
| 517 |
FLAC__ASSERT(0 != iterator); |
| 518 |
FLAC__ASSERT(0 != filename); |
| 519 |
|
| 520 |
simple_iterator_free_guts_(iterator); |
| 521 |
|
| 522 |
if(!read_only && preserve_file_stats) |
| 523 |
iterator->has_stats = get_file_stats_(filename, &iterator->stats); |
| 524 |
|
| 525 |
if(0 == (iterator->filename = strdup(filename))) { |
| 526 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 527 |
return false; |
| 528 |
} |
| 529 |
if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) { |
| 530 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 531 |
return false; |
| 532 |
} |
| 533 |
|
| 534 |
return simple_iterator_prime_input_(iterator, read_only); |
| 535 |
} |
| 536 |
|
| 537 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator) |
| 538 |
{ |
| 539 |
FLAC__ASSERT(0 != iterator); |
| 540 |
FLAC__ASSERT(0 != iterator->file); |
| 541 |
|
| 542 |
return iterator->is_writable; |
| 543 |
} |
| 544 |
|
| 545 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator) |
| 546 |
{ |
| 547 |
FLAC__ASSERT(0 != iterator); |
| 548 |
FLAC__ASSERT(0 != iterator->file); |
| 549 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 550 |
|
| 551 |
if(iterator->is_last) |
| 552 |
return false; |
| 553 |
|
| 554 |
if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) { |
| 555 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 556 |
return false; |
| 557 |
} |
| 558 |
|
| 559 |
iterator->offset[iterator->depth] = ftello(iterator->file); |
| 560 |
|
| 561 |
return read_metadata_block_header_(iterator); |
| 562 |
} |
| 563 |
|
| 564 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator) |
| 565 |
{ |
| 566 |
FLAC__off_t this_offset; |
| 567 |
|
| 568 |
FLAC__ASSERT(0 != iterator); |
| 569 |
FLAC__ASSERT(0 != iterator->file); |
| 570 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 571 |
|
| 572 |
if(iterator->offset[iterator->depth] == iterator->first_offset) |
| 573 |
return false; |
| 574 |
|
| 575 |
if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) { |
| 576 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 577 |
return false; |
| 578 |
} |
| 579 |
this_offset = iterator->first_offset; |
| 580 |
if(!read_metadata_block_header_(iterator)) |
| 581 |
return false; |
| 582 |
|
| 583 |
/* we ignore any error from ftello() and catch it in fseeko() */ |
| 584 |
while(ftello(iterator->file) + (FLAC__off_t)iterator->length < iterator->offset[iterator->depth]) { |
| 585 |
if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) { |
| 586 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 587 |
return false; |
| 588 |
} |
| 589 |
this_offset = ftello(iterator->file); |
| 590 |
if(!read_metadata_block_header_(iterator)) |
| 591 |
return false; |
| 592 |
} |
| 593 |
|
| 594 |
iterator->offset[iterator->depth] = this_offset; |
| 595 |
|
| 596 |
return true; |
| 597 |
} |
| 598 |
|
| 599 |
/*@@@@add to tests*/ |
| 600 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator) |
| 601 |
{ |
| 602 |
FLAC__ASSERT(0 != iterator); |
| 603 |
FLAC__ASSERT(0 != iterator->file); |
| 604 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 605 |
|
| 606 |
return iterator->is_last; |
| 607 |
} |
| 608 |
|
| 609 |
/*@@@@add to tests*/ |
| 610 |
FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator) |
| 611 |
{ |
| 612 |
FLAC__ASSERT(0 != iterator); |
| 613 |
FLAC__ASSERT(0 != iterator->file); |
| 614 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 615 |
|
| 616 |
return (off_t)iterator->offset[iterator->depth]; |
| 617 |
} |
| 618 |
|
| 619 |
FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator) |
| 620 |
{ |
| 621 |
FLAC__ASSERT(0 != iterator); |
| 622 |
FLAC__ASSERT(0 != iterator->file); |
| 623 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 624 |
|
| 625 |
return iterator->type; |
| 626 |
} |
| 627 |
|
| 628 |
/*@@@@add to tests*/ |
| 629 |
FLAC_API uint32_t FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator) |
| 630 |
{ |
| 631 |
FLAC__ASSERT(0 != iterator); |
| 632 |
FLAC__ASSERT(0 != iterator->file); |
| 633 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 634 |
|
| 635 |
return iterator->length; |
| 636 |
} |
| 637 |
|
| 638 |
/*@@@@add to tests*/ |
| 639 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id) |
| 640 |
{ |
| 641 |
const uint32_t id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 642 |
|
| 643 |
FLAC__ASSERT(0 != iterator); |
| 644 |
FLAC__ASSERT(0 != iterator->file); |
| 645 |
FLAC__ASSERT(0 != id); |
| 646 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 647 |
|
| 648 |
if(iterator->type != FLAC__METADATA_TYPE_APPLICATION) { |
| 649 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 650 |
return false; |
| 651 |
} |
| 652 |
|
| 653 |
if(fread(id, 1, id_bytes, iterator->file) != id_bytes) { |
| 654 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 655 |
return false; |
| 656 |
} |
| 657 |
|
| 658 |
/* back up */ |
| 659 |
if(0 != fseeko(iterator->file, -((int)id_bytes), SEEK_CUR)) { |
| 660 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 661 |
return false; |
| 662 |
} |
| 663 |
|
| 664 |
return true; |
| 665 |
} |
| 666 |
|
| 667 |
FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator) |
| 668 |
{ |
| 669 |
FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type); |
| 670 |
|
| 671 |
FLAC__ASSERT(0 != iterator); |
| 672 |
FLAC__ASSERT(0 != iterator->file); |
| 673 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 674 |
|
| 675 |
if(0 != block) { |
| 676 |
block->is_last = iterator->is_last; |
| 677 |
block->length = iterator->length; |
| 678 |
|
| 679 |
if(!read_metadata_block_data_(iterator, block)) { |
| 680 |
FLAC__metadata_object_delete(block); |
| 681 |
return 0; |
| 682 |
} |
| 683 |
|
| 684 |
/* back up to the beginning of the block data to stay consistent */ |
| 685 |
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) { |
| 686 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 687 |
FLAC__metadata_object_delete(block); |
| 688 |
return 0; |
| 689 |
} |
| 690 |
} |
| 691 |
else |
| 692 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 693 |
|
| 694 |
return block; |
| 695 |
} |
| 696 |
|
| 697 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding) |
| 698 |
{ |
| 699 |
FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];) |
| 700 |
FLAC__bool ret; |
| 701 |
|
| 702 |
FLAC__ASSERT(0 != iterator); |
| 703 |
FLAC__ASSERT(0 != iterator->file); |
| 704 |
FLAC__ASSERT(0 != block); |
| 705 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 706 |
|
| 707 |
if(!iterator->is_writable) { |
| 708 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE; |
| 709 |
return false; |
| 710 |
} |
| 711 |
|
| 712 |
if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 713 |
if(iterator->type != block->type) { |
| 714 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 715 |
return false; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
block->is_last = iterator->is_last; |
| 720 |
|
| 721 |
if(iterator->length == block->length) |
| 722 |
return write_metadata_block_stationary_(iterator, block); |
| 723 |
else if(iterator->length > block->length) { |
| 724 |
if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) { |
| 725 |
ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last); |
| 726 |
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 727 |
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 728 |
return ret; |
| 729 |
} |
| 730 |
else { |
| 731 |
if((ret = rewrite_whole_file_(iterator, block, /*append=*/false))) { |
| 732 |
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 733 |
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 734 |
} |
| 735 |
return ret; |
| 736 |
} |
| 737 |
} |
| 738 |
else /* iterator->length < block->length */ { |
| 739 |
uint32_t padding_leftover = 0; |
| 740 |
FLAC__bool padding_is_last = false; |
| 741 |
if(use_padding) { |
| 742 |
/* first see if we can even use padding */ |
| 743 |
if(iterator->is_last) { |
| 744 |
use_padding = false; |
| 745 |
} |
| 746 |
else { |
| 747 |
const uint32_t extra_padding_bytes_required = block->length - iterator->length; |
| 748 |
simple_iterator_push_(iterator); |
| 749 |
if(!FLAC__metadata_simple_iterator_next(iterator)) { |
| 750 |
(void)simple_iterator_pop_(iterator); |
| 751 |
return false; |
| 752 |
} |
| 753 |
if(iterator->type != FLAC__METADATA_TYPE_PADDING) { |
| 754 |
use_padding = false; |
| 755 |
} |
| 756 |
else { |
| 757 |
if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) { |
| 758 |
padding_leftover = 0; |
| 759 |
block->is_last = iterator->is_last; |
| 760 |
} |
| 761 |
else if(iterator->length < extra_padding_bytes_required) |
| 762 |
use_padding = false; |
| 763 |
else { |
| 764 |
padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required; |
| 765 |
padding_is_last = iterator->is_last; |
| 766 |
block->is_last = false; |
| 767 |
} |
| 768 |
} |
| 769 |
if(!simple_iterator_pop_(iterator)) |
| 770 |
return false; |
| 771 |
} |
| 772 |
} |
| 773 |
if(use_padding) { |
| 774 |
if(padding_leftover == 0) { |
| 775 |
ret = write_metadata_block_stationary_(iterator, block); |
| 776 |
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 777 |
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 778 |
return ret; |
| 779 |
} |
| 780 |
else { |
| 781 |
FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 782 |
ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last); |
| 783 |
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 784 |
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 785 |
return ret; |
| 786 |
} |
| 787 |
} |
| 788 |
else { |
| 789 |
if((ret = rewrite_whole_file_(iterator, block, /*append=*/false))) { |
| 790 |
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset); |
| 791 |
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 792 |
} |
| 793 |
return ret; |
| 794 |
} |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding) |
| 799 |
{ |
| 800 |
uint32_t padding_leftover = 0; |
| 801 |
FLAC__bool padding_is_last = false; |
| 802 |
|
| 803 |
FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;) |
| 804 |
FLAC__bool ret; |
| 805 |
|
| 806 |
FLAC__ASSERT(0 != iterator); |
| 807 |
FLAC__ASSERT(0 != iterator->file); |
| 808 |
FLAC__ASSERT(0 != block); |
| 809 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 810 |
|
| 811 |
if(!iterator->is_writable) { |
| 812 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE; |
| 813 |
return false; |
| 814 |
} |
| 815 |
|
| 816 |
if(block->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 817 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 818 |
return false; |
| 819 |
} |
| 820 |
|
| 821 |
block->is_last = iterator->is_last; |
| 822 |
|
| 823 |
if(use_padding) { |
| 824 |
/* first see if we can even use padding */ |
| 825 |
if(iterator->is_last) { |
| 826 |
use_padding = false; |
| 827 |
} |
| 828 |
else { |
| 829 |
simple_iterator_push_(iterator); |
| 830 |
if(!FLAC__metadata_simple_iterator_next(iterator)) { |
| 831 |
(void)simple_iterator_pop_(iterator); |
| 832 |
return false; |
| 833 |
} |
| 834 |
if(iterator->type != FLAC__METADATA_TYPE_PADDING) { |
| 835 |
use_padding = false; |
| 836 |
} |
| 837 |
else { |
| 838 |
if(iterator->length == block->length) { |
| 839 |
padding_leftover = 0; |
| 840 |
block->is_last = iterator->is_last; |
| 841 |
} |
| 842 |
else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) |
| 843 |
use_padding = false; |
| 844 |
else { |
| 845 |
padding_leftover = iterator->length - block->length; |
| 846 |
padding_is_last = iterator->is_last; |
| 847 |
block->is_last = false; |
| 848 |
} |
| 849 |
} |
| 850 |
if(!simple_iterator_pop_(iterator)) |
| 851 |
return false; |
| 852 |
} |
| 853 |
} |
| 854 |
if(use_padding) { |
| 855 |
/* move to the next block, which is suitable padding */ |
| 856 |
if(!FLAC__metadata_simple_iterator_next(iterator)) |
| 857 |
return false; |
| 858 |
if(padding_leftover == 0) { |
| 859 |
ret = write_metadata_block_stationary_(iterator, block); |
| 860 |
FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
| 861 |
FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 862 |
return ret; |
| 863 |
} |
| 864 |
else { |
| 865 |
FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 866 |
ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last); |
| 867 |
FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
| 868 |
FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 869 |
return ret; |
| 870 |
} |
| 871 |
} |
| 872 |
else { |
| 873 |
if((ret = rewrite_whole_file_(iterator, block, /*append=*/true))) { |
| 874 |
FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset); |
| 875 |
FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 876 |
} |
| 877 |
return ret; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding) |
| 882 |
{ |
| 883 |
FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];) |
| 884 |
FLAC__bool ret; |
| 885 |
|
| 886 |
FLAC__ASSERT(iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 887 |
|
| 888 |
if(!iterator->is_writable) { |
| 889 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE; |
| 890 |
return false; |
| 891 |
} |
| 892 |
|
| 893 |
if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) { |
| 894 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT; |
| 895 |
return false; |
| 896 |
} |
| 897 |
|
| 898 |
if(use_padding) { |
| 899 |
FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING); |
| 900 |
if(0 == padding) { |
| 901 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 902 |
return false; |
| 903 |
} |
| 904 |
padding->length = iterator->length; |
| 905 |
if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) { |
| 906 |
FLAC__metadata_object_delete(padding); |
| 907 |
return false; |
| 908 |
} |
| 909 |
FLAC__metadata_object_delete(padding); |
| 910 |
if(!FLAC__metadata_simple_iterator_prev(iterator)) |
| 911 |
return false; |
| 912 |
FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset); |
| 913 |
FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset); |
| 914 |
return true; |
| 915 |
} |
| 916 |
else { |
| 917 |
if((ret = rewrite_whole_file_(iterator, 0, /*append=*/false))) { |
| 918 |
FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset); |
| 919 |
FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset); |
| 920 |
} |
| 921 |
return ret; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
/**************************************************************************** |
| 928 |
* |
| 929 |
* Level 2 implementation |
| 930 |
* |
| 931 |
***************************************************************************/ |
| 932 |
|
| 933 |
|
| 934 |
typedef struct FLAC__Metadata_Node { |
| 935 |
FLAC__StreamMetadata *data; |
| 936 |
struct FLAC__Metadata_Node *prev, *next; |
| 937 |
} FLAC__Metadata_Node; |
| 938 |
|
| 939 |
struct FLAC__Metadata_Chain { |
| 940 |
char *filename; /* will be NULL if using callbacks */ |
| 941 |
FLAC__bool is_ogg; |
| 942 |
FLAC__Metadata_Node *head; |
| 943 |
FLAC__Metadata_Node *tail; |
| 944 |
uint32_t nodes; |
| 945 |
FLAC__Metadata_ChainStatus status; |
| 946 |
FLAC__off_t first_offset, last_offset; |
| 947 |
/* |
| 948 |
* This is the length of the chain initially read from the FLAC file. |
| 949 |
* it is used to compare against the current length to decide whether |
| 950 |
* or not the whole file has to be rewritten. |
| 951 |
*/ |
| 952 |
FLAC__off_t initial_length; |
| 953 |
/* @@@ hacky, these are currently only needed by ogg reader */ |
| 954 |
FLAC__IOHandle handle; |
| 955 |
FLAC__IOCallback_Read read_cb; |
| 956 |
}; |
| 957 |
|
| 958 |
struct FLAC__Metadata_Iterator { |
| 959 |
FLAC__Metadata_Chain *chain; |
| 960 |
FLAC__Metadata_Node *current; |
| 961 |
}; |
| 962 |
|
| 963 |
FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = { |
| 964 |
"FLAC__METADATA_CHAIN_STATUS_OK", |
| 965 |
"FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT", |
| 966 |
"FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE", |
| 967 |
"FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE", |
| 968 |
"FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE", |
| 969 |
"FLAC__METADATA_CHAIN_STATUS_BAD_METADATA", |
| 970 |
"FLAC__METADATA_CHAIN_STATUS_READ_ERROR", |
| 971 |
"FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR", |
| 972 |
"FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR", |
| 973 |
"FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR", |
| 974 |
"FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR", |
| 975 |
"FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR", |
| 976 |
"FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR", |
| 977 |
"FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS", |
| 978 |
"FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", |
| 979 |
"FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL" |
| 980 |
}; |
| 981 |
|
| 982 |
|
| 983 |
static FLAC__Metadata_Node *node_new_(void) |
| 984 |
{ |
| 985 |
return calloc(1, sizeof(FLAC__Metadata_Node)); |
| 986 |
} |
| 987 |
|
| 988 |
static void node_delete_(FLAC__Metadata_Node *node) |
| 989 |
{ |
| 990 |
FLAC__ASSERT(0 != node); |
| 991 |
if(0 != node->data) |
| 992 |
FLAC__metadata_object_delete(node->data); |
| 993 |
free(node); |
| 994 |
} |
| 995 |
|
| 996 |
static void chain_init_(FLAC__Metadata_Chain *chain) |
| 997 |
{ |
| 998 |
FLAC__ASSERT(0 != chain); |
| 999 |
|
| 1000 |
chain->filename = 0; |
| 1001 |
chain->is_ogg = false; |
| 1002 |
chain->head = chain->tail = 0; |
| 1003 |
chain->nodes = 0; |
| 1004 |
chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 1005 |
chain->initial_length = 0; |
| 1006 |
chain->read_cb = 0; |
| 1007 |
} |
| 1008 |
|
| 1009 |
static void chain_clear_(FLAC__Metadata_Chain *chain) |
| 1010 |
{ |
| 1011 |
FLAC__Metadata_Node *node, *next; |
| 1012 |
|
| 1013 |
FLAC__ASSERT(0 != chain); |
| 1014 |
|
| 1015 |
for(node = chain->head; node; ) { |
| 1016 |
next = node->next; |
| 1017 |
node_delete_(node); |
| 1018 |
node = next; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if(0 != chain->filename) |
| 1022 |
free(chain->filename); |
| 1023 |
|
| 1024 |
chain_init_(chain); |
| 1025 |
} |
| 1026 |
|
| 1027 |
static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 1028 |
{ |
| 1029 |
FLAC__ASSERT(0 != chain); |
| 1030 |
FLAC__ASSERT(0 != node); |
| 1031 |
FLAC__ASSERT(0 != node->data); |
| 1032 |
|
| 1033 |
node->next = node->prev = 0; |
| 1034 |
node->data->is_last = true; |
| 1035 |
if(0 != chain->tail) |
| 1036 |
chain->tail->data->is_last = false; |
| 1037 |
|
| 1038 |
if(0 == chain->head) |
| 1039 |
chain->head = node; |
| 1040 |
else { |
| 1041 |
FLAC__ASSERT(0 != chain->tail); |
| 1042 |
chain->tail->next = node; |
| 1043 |
node->prev = chain->tail; |
| 1044 |
} |
| 1045 |
chain->tail = node; |
| 1046 |
chain->nodes++; |
| 1047 |
} |
| 1048 |
|
| 1049 |
static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 1050 |
{ |
| 1051 |
FLAC__ASSERT(0 != chain); |
| 1052 |
FLAC__ASSERT(0 != node); |
| 1053 |
|
| 1054 |
if(node == chain->head) |
| 1055 |
chain->head = node->next; |
| 1056 |
else |
| 1057 |
node->prev->next = node->next; |
| 1058 |
|
| 1059 |
if(node == chain->tail) |
| 1060 |
chain->tail = node->prev; |
| 1061 |
else |
| 1062 |
node->next->prev = node->prev; |
| 1063 |
|
| 1064 |
if(0 != chain->tail) |
| 1065 |
chain->tail->data->is_last = true; |
| 1066 |
|
| 1067 |
chain->nodes--; |
| 1068 |
} |
| 1069 |
|
| 1070 |
static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 1071 |
{ |
| 1072 |
chain_remove_node_(chain, node); |
| 1073 |
node_delete_(node); |
| 1074 |
} |
| 1075 |
|
| 1076 |
static FLAC__off_t chain_calculate_length_(FLAC__Metadata_Chain *chain) |
| 1077 |
{ |
| 1078 |
const FLAC__Metadata_Node *node; |
| 1079 |
FLAC__off_t length = 0; |
| 1080 |
for(node = chain->head; node; node = node->next) |
| 1081 |
length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1082 |
return length; |
| 1083 |
} |
| 1084 |
|
| 1085 |
static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node) |
| 1086 |
{ |
| 1087 |
FLAC__ASSERT(0 != node); |
| 1088 |
FLAC__ASSERT(0 != node->data); |
| 1089 |
FLAC__ASSERT(0 != iterator); |
| 1090 |
FLAC__ASSERT(0 != iterator->current); |
| 1091 |
FLAC__ASSERT(0 != iterator->chain); |
| 1092 |
FLAC__ASSERT(0 != iterator->chain->head); |
| 1093 |
FLAC__ASSERT(0 != iterator->chain->tail); |
| 1094 |
|
| 1095 |
node->data->is_last = false; |
| 1096 |
|
| 1097 |
node->prev = iterator->current->prev; |
| 1098 |
node->next = iterator->current; |
| 1099 |
|
| 1100 |
if(0 == node->prev) |
| 1101 |
iterator->chain->head = node; |
| 1102 |
else |
| 1103 |
node->prev->next = node; |
| 1104 |
|
| 1105 |
iterator->current->prev = node; |
| 1106 |
|
| 1107 |
iterator->chain->nodes++; |
| 1108 |
} |
| 1109 |
|
| 1110 |
static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node) |
| 1111 |
{ |
| 1112 |
FLAC__ASSERT(0 != node); |
| 1113 |
FLAC__ASSERT(0 != node->data); |
| 1114 |
FLAC__ASSERT(0 != iterator); |
| 1115 |
FLAC__ASSERT(0 != iterator->current); |
| 1116 |
FLAC__ASSERT(0 != iterator->chain); |
| 1117 |
FLAC__ASSERT(0 != iterator->chain->head); |
| 1118 |
FLAC__ASSERT(0 != iterator->chain->tail); |
| 1119 |
|
| 1120 |
iterator->current->data->is_last = false; |
| 1121 |
|
| 1122 |
node->prev = iterator->current; |
| 1123 |
node->next = iterator->current->next; |
| 1124 |
|
| 1125 |
if(0 == node->next) |
| 1126 |
iterator->chain->tail = node; |
| 1127 |
else |
| 1128 |
node->next->prev = node; |
| 1129 |
|
| 1130 |
node->prev->next = node; |
| 1131 |
|
| 1132 |
iterator->chain->tail->data->is_last = true; |
| 1133 |
|
| 1134 |
iterator->chain->nodes++; |
| 1135 |
} |
| 1136 |
|
| 1137 |
/* return true iff node and node->next are both padding */ |
| 1138 |
static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node) |
| 1139 |
{ |
| 1140 |
if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1141 |
const uint32_t growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length; |
| 1142 |
node->data->length += growth; /* new block size can be greater than max metadata block size, but it'll be fixed later in chain_prepare_for_write_() */ |
| 1143 |
|
| 1144 |
chain_delete_node_(chain, node->next); |
| 1145 |
return true; |
| 1146 |
} |
| 1147 |
else |
| 1148 |
return false; |
| 1149 |
} |
| 1150 |
|
| 1151 |
#if defined(_MSC_VER) |
| 1152 |
// silence three MSVC warnings 'conversion from 'conversion from 'const __int64' to 'uint32_t', possible loss of data' |
| 1153 |
#pragma warning ( disable : 4244 ) |
| 1154 |
#endif |
| 1155 |
|
| 1156 |
/* Returns the new length of the chain, or 0 if there was an error. */ |
| 1157 |
/* WATCHOUT: This can get called multiple times before a write, so |
| 1158 |
* it should still work when this happens. |
| 1159 |
*/ |
| 1160 |
/* WATCHOUT: Make sure to also update the logic in |
| 1161 |
* FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes. |
| 1162 |
*/ |
| 1163 |
static FLAC__off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding) |
| 1164 |
{ |
| 1165 |
FLAC__off_t current_length = chain_calculate_length_(chain); |
| 1166 |
FLAC__Metadata_Node * i; |
| 1167 |
|
| 1168 |
/* Check all is_last settings on the blocks */ |
| 1169 |
for(i = chain->head; i->next != NULL; i = i->next) |
| 1170 |
i->data->is_last = 0; |
| 1171 |
chain->tail->data->is_last = 1; |
| 1172 |
|
| 1173 |
if(use_padding) { |
| 1174 |
/* if the metadata shrank and the last block is padding, we just extend the last padding block */ |
| 1175 |
if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1176 |
const FLAC__off_t delta = chain->initial_length - current_length; |
| 1177 |
chain->tail->data->length += delta; |
| 1178 |
current_length += delta; |
| 1179 |
FLAC__ASSERT(current_length == chain->initial_length); |
| 1180 |
} |
| 1181 |
/* if the metadata shrank more than 4 bytes then there's room to add another padding block */ |
| 1182 |
else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) { |
| 1183 |
FLAC__StreamMetadata *padding; |
| 1184 |
FLAC__Metadata_Node *node; |
| 1185 |
if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) { |
| 1186 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1187 |
return 0; |
| 1188 |
} |
| 1189 |
padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length); |
| 1190 |
if(0 == (node = node_new_())) { |
| 1191 |
FLAC__metadata_object_delete(padding); |
| 1192 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1193 |
return 0; |
| 1194 |
} |
| 1195 |
node->data = padding; |
| 1196 |
chain_append_node_(chain, node); |
| 1197 |
current_length = chain_calculate_length_(chain); |
| 1198 |
FLAC__ASSERT(current_length == chain->initial_length); |
| 1199 |
} |
| 1200 |
/* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */ |
| 1201 |
else if(current_length > chain->initial_length) { |
| 1202 |
const FLAC__off_t delta = current_length - chain->initial_length; |
| 1203 |
if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1204 |
/* if the delta is exactly the size of the last padding block, remove the padding block */ |
| 1205 |
if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) { |
| 1206 |
chain_delete_node_(chain, chain->tail); |
| 1207 |
current_length = chain_calculate_length_(chain); |
| 1208 |
FLAC__ASSERT(current_length == chain->initial_length); |
| 1209 |
} |
| 1210 |
/* if there is at least 'delta' bytes of padding, trim the padding down */ |
| 1211 |
else if((FLAC__off_t)chain->tail->data->length >= delta) { |
| 1212 |
chain->tail->data->length -= delta; |
| 1213 |
current_length -= delta; |
| 1214 |
FLAC__ASSERT(current_length == chain->initial_length); |
| 1215 |
} |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
/* check sizes of all metadata blocks; reduce padding size if necessary */ |
| 1221 |
{ |
| 1222 |
FLAC__Metadata_Node *node; |
| 1223 |
for (node = chain->head; node; node = node->next) { |
| 1224 |
if(node->data->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) { |
| 1225 |
if(node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1226 |
node->data->length = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1; |
| 1227 |
current_length = chain_calculate_length_(chain); |
| 1228 |
} else { |
| 1229 |
chain->status = FLAC__METADATA_CHAIN_STATUS_BAD_METADATA; |
| 1230 |
return 0; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
return current_length; |
| 1237 |
} |
| 1238 |
|
| 1239 |
#if defined(_MSC_VER) |
| 1240 |
#pragma warning ( default : 4244 ) |
| 1241 |
#endif |
| 1242 |
|
| 1243 |
static FLAC__bool chain_read_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Tell tell_cb) |
| 1244 |
{ |
| 1245 |
FLAC__Metadata_Node *node; |
| 1246 |
|
| 1247 |
FLAC__ASSERT(0 != chain); |
| 1248 |
|
| 1249 |
/* we assume we're already at the beginning of the file */ |
| 1250 |
|
| 1251 |
switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) { |
| 1252 |
case 0: |
| 1253 |
break; |
| 1254 |
case 1: |
| 1255 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1256 |
return false; |
| 1257 |
case 2: |
| 1258 |
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1259 |
return false; |
| 1260 |
case 3: |
| 1261 |
chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE; |
| 1262 |
return false; |
| 1263 |
default: |
| 1264 |
FLAC__ASSERT(0); |
| 1265 |
return false; |
| 1266 |
} |
| 1267 |
|
| 1268 |
{ |
| 1269 |
FLAC__int64 pos = tell_cb(handle); |
| 1270 |
if(pos < 0) { |
| 1271 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1272 |
return false; |
| 1273 |
} |
| 1274 |
chain->first_offset = (FLAC__off_t)pos; |
| 1275 |
} |
| 1276 |
|
| 1277 |
{ |
| 1278 |
FLAC__bool is_last; |
| 1279 |
FLAC__MetadataType type; |
| 1280 |
uint32_t length; |
| 1281 |
|
| 1282 |
do { |
| 1283 |
node = node_new_(); |
| 1284 |
if(0 == node) { |
| 1285 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1286 |
return false; |
| 1287 |
} |
| 1288 |
|
| 1289 |
if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) { |
| 1290 |
node_delete_(node); |
| 1291 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1292 |
return false; |
| 1293 |
} |
| 1294 |
|
| 1295 |
node->data = FLAC__metadata_object_new(type); |
| 1296 |
if(0 == node->data) { |
| 1297 |
node_delete_(node); |
| 1298 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1299 |
return false; |
| 1300 |
} |
| 1301 |
|
| 1302 |
node->data->is_last = is_last; |
| 1303 |
node->data->length = length; |
| 1304 |
|
| 1305 |
chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data)); |
| 1306 |
if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1307 |
node_delete_(node); |
| 1308 |
return false; |
| 1309 |
} |
| 1310 |
chain_append_node_(chain, node); |
| 1311 |
} while(!is_last); |
| 1312 |
} |
| 1313 |
|
| 1314 |
{ |
| 1315 |
FLAC__int64 pos = tell_cb(handle); |
| 1316 |
if(pos < 0) { |
| 1317 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 1318 |
return false; |
| 1319 |
} |
| 1320 |
chain->last_offset = (FLAC__off_t)pos; |
| 1321 |
} |
| 1322 |
|
| 1323 |
if(chain->head->data->type != FLAC__METADATA_TYPE_STREAMINFO) { |
| 1324 |
chain->status = FLAC__METADATA_CHAIN_STATUS_BAD_METADATA; |
| 1325 |
return false; |
| 1326 |
} |
| 1327 |
|
| 1328 |
chain->initial_length = chain_calculate_length_(chain); |
| 1329 |
|
| 1330 |
return true; |
| 1331 |
} |
| 1332 |
|
| 1333 |
static FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
| 1334 |
{ |
| 1335 |
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1336 |
(void)decoder; |
| 1337 |
if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1338 |
*bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle); |
| 1339 |
if(*bytes == 0) |
| 1340 |
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
| 1341 |
else |
| 1342 |
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 1343 |
} |
| 1344 |
else |
| 1345 |
return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 1346 |
} |
| 1347 |
|
| 1348 |
static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 1349 |
{ |
| 1350 |
(void)decoder, (void)frame, (void)buffer, (void)client_data; |
| 1351 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 1352 |
} |
| 1353 |
|
| 1354 |
static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 1355 |
{ |
| 1356 |
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1357 |
FLAC__Metadata_Node *node; |
| 1358 |
|
| 1359 |
(void)decoder; |
| 1360 |
|
| 1361 |
node = node_new_(); |
| 1362 |
if(0 == node) { |
| 1363 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1364 |
return; |
| 1365 |
} |
| 1366 |
|
| 1367 |
node->data = FLAC__metadata_object_clone(metadata); |
| 1368 |
if(0 == node->data) { |
| 1369 |
node_delete_(node); |
| 1370 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1371 |
return; |
| 1372 |
} |
| 1373 |
|
| 1374 |
chain_append_node_(chain, node); |
| 1375 |
} |
| 1376 |
|
| 1377 |
static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 1378 |
{ |
| 1379 |
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data; |
| 1380 |
(void)decoder, (void)status; |
| 1381 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1382 |
} |
| 1383 |
|
| 1384 |
static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb) |
| 1385 |
{ |
| 1386 |
FLAC__StreamDecoder *decoder; |
| 1387 |
|
| 1388 |
FLAC__ASSERT(0 != chain); |
| 1389 |
|
| 1390 |
/* we assume we're already at the beginning of the file */ |
| 1391 |
|
| 1392 |
chain->handle = handle; |
| 1393 |
chain->read_cb = read_cb; |
| 1394 |
if(0 == (decoder = FLAC__stream_decoder_new())) { |
| 1395 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1396 |
return false; |
| 1397 |
} |
| 1398 |
FLAC__stream_decoder_set_metadata_respond_all(decoder); |
| 1399 |
if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { |
| 1400 |
FLAC__stream_decoder_delete(decoder); |
| 1401 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1402 |
return false; |
| 1403 |
} |
| 1404 |
|
| 1405 |
chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */ |
| 1406 |
|
| 1407 |
if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) |
| 1408 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */ |
| 1409 |
if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) { |
| 1410 |
FLAC__stream_decoder_delete(decoder); |
| 1411 |
return false; |
| 1412 |
} |
| 1413 |
|
| 1414 |
FLAC__stream_decoder_delete(decoder); |
| 1415 |
|
| 1416 |
chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */ |
| 1417 |
|
| 1418 |
chain->initial_length = chain_calculate_length_(chain); |
| 1419 |
|
| 1420 |
if(chain->initial_length == 0 || chain->head->data->type != FLAC__METADATA_TYPE_STREAMINFO) { |
| 1421 |
/* Ogg FLAC file must have at least streaminfo and vorbis comment */ |
| 1422 |
chain->status = FLAC__METADATA_CHAIN_STATUS_BAD_METADATA; |
| 1423 |
return false; |
| 1424 |
} |
| 1425 |
|
| 1426 |
return true; |
| 1427 |
} |
| 1428 |
|
| 1429 |
static FLAC__bool chain_rewrite_metadata_in_place_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, FLAC__IOCallback_Seek seek_cb) |
| 1430 |
{ |
| 1431 |
FLAC__Metadata_Node *node; |
| 1432 |
|
| 1433 |
FLAC__ASSERT(0 != chain); |
| 1434 |
FLAC__ASSERT(0 != chain->head); |
| 1435 |
|
| 1436 |
if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) { |
| 1437 |
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1438 |
return false; |
| 1439 |
} |
| 1440 |
|
| 1441 |
for(node = chain->head; node; node = node->next) { |
| 1442 |
if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) { |
| 1443 |
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1444 |
return false; |
| 1445 |
} |
| 1446 |
if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) { |
| 1447 |
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1448 |
return false; |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
/*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
| 1453 |
|
| 1454 |
chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 1455 |
return true; |
| 1456 |
} |
| 1457 |
|
| 1458 |
static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain) |
| 1459 |
{ |
| 1460 |
FILE *file; |
| 1461 |
FLAC__bool ret; |
| 1462 |
|
| 1463 |
FLAC__ASSERT(0 != chain->filename); |
| 1464 |
|
| 1465 |
if(0 == (file = flac_fopen(chain->filename, "r+b"))) { |
| 1466 |
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1467 |
return false; |
| 1468 |
} |
| 1469 |
|
| 1470 |
/* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */ |
| 1471 |
ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_); |
| 1472 |
|
| 1473 |
fclose(file); |
| 1474 |
|
| 1475 |
return ret; |
| 1476 |
} |
| 1477 |
|
| 1478 |
static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix, const char *filename) |
| 1479 |
{ |
| 1480 |
FILE *f, *tempfile = NULL; |
| 1481 |
char *tempfilename = 0; |
| 1482 |
FLAC__Metadata_SimpleIteratorStatus status; |
| 1483 |
const FLAC__Metadata_Node *node; |
| 1484 |
|
| 1485 |
FLAC__ASSERT(0 != chain); |
| 1486 |
FLAC__ASSERT(0 != chain->filename); |
| 1487 |
FLAC__ASSERT(0 != chain->head); |
| 1488 |
|
| 1489 |
/* copy the file prefix (data up to first metadata block */ |
| 1490 |
if(0 == (f = flac_fopen(chain->filename, "rb"))) { |
| 1491 |
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1492 |
return false; |
| 1493 |
} |
| 1494 |
if(filename == NULL) { |
| 1495 |
if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) { |
| 1496 |
chain->status = get_equivalent_status_(status); |
| 1497 |
goto err; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
else { |
| 1501 |
if(0 == (tempfile = flac_fopen(filename, "wb"))) { |
| 1502 |
(void)fclose(f); |
| 1503 |
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1504 |
return false; |
| 1505 |
} |
| 1506 |
|
| 1507 |
} |
| 1508 |
if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) { |
| 1509 |
chain->status = get_equivalent_status_(status); |
| 1510 |
goto err; |
| 1511 |
} |
| 1512 |
|
| 1513 |
/* write the metadata */ |
| 1514 |
for(node = chain->head; node; node = node->next) { |
| 1515 |
if(!write_metadata_block_header_(tempfile, &status, node->data)) { |
| 1516 |
chain->status = get_equivalent_status_(status); |
| 1517 |
goto err; |
| 1518 |
} |
| 1519 |
if(!write_metadata_block_data_(tempfile, &status, node->data)) { |
| 1520 |
chain->status = get_equivalent_status_(status); |
| 1521 |
goto err; |
| 1522 |
} |
| 1523 |
} |
| 1524 |
/*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
| 1525 |
|
| 1526 |
/* copy the file postfix (everything after the metadata) */ |
| 1527 |
if(0 != fseeko(f, chain->last_offset, SEEK_SET)) { |
| 1528 |
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1529 |
goto err; |
| 1530 |
} |
| 1531 |
if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) { |
| 1532 |
chain->status = get_equivalent_status_(status); |
| 1533 |
goto err; |
| 1534 |
} |
| 1535 |
|
| 1536 |
/* move the tempfile on top of the original */ |
| 1537 |
(void)fclose(f); |
| 1538 |
if(filename == NULL) { |
| 1539 |
if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status)) { |
| 1540 |
chain->status = get_equivalent_status_(status); |
| 1541 |
return false; |
| 1542 |
} |
| 1543 |
} |
| 1544 |
else |
| 1545 |
(void)fclose(tempfile); |
| 1546 |
|
| 1547 |
return true; |
| 1548 |
|
| 1549 |
err: |
| 1550 |
(void)fclose(f); |
| 1551 |
if(filename == NULL) |
| 1552 |
cleanup_tempfile_(&tempfile, &tempfilename); |
| 1553 |
else |
| 1554 |
(void)fclose(tempfile); |
| 1555 |
return false; |
| 1556 |
} |
| 1557 |
|
| 1558 |
/* assumes 'handle' is already at beginning of file */ |
| 1559 |
static FLAC__bool chain_rewrite_file_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb) |
| 1560 |
{ |
| 1561 |
FLAC__Metadata_SimpleIteratorStatus status; |
| 1562 |
const FLAC__Metadata_Node *node; |
| 1563 |
|
| 1564 |
FLAC__ASSERT(0 != chain); |
| 1565 |
FLAC__ASSERT(0 == chain->filename); |
| 1566 |
FLAC__ASSERT(0 != chain->head); |
| 1567 |
|
| 1568 |
/* copy the file prefix (data up to first metadata block */ |
| 1569 |
if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) { |
| 1570 |
chain->status = get_equivalent_status_(status); |
| 1571 |
return false; |
| 1572 |
} |
| 1573 |
|
| 1574 |
/* write the metadata */ |
| 1575 |
for(node = chain->head; node; node = node->next) { |
| 1576 |
if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) { |
| 1577 |
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1578 |
return false; |
| 1579 |
} |
| 1580 |
if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) { |
| 1581 |
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 1582 |
return false; |
| 1583 |
} |
| 1584 |
} |
| 1585 |
/*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/ |
| 1586 |
|
| 1587 |
/* copy the file postfix (everything after the metadata) */ |
| 1588 |
if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) { |
| 1589 |
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1590 |
return false; |
| 1591 |
} |
| 1592 |
if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) { |
| 1593 |
chain->status = get_equivalent_status_(status); |
| 1594 |
return false; |
| 1595 |
} |
| 1596 |
|
| 1597 |
return true; |
| 1598 |
} |
| 1599 |
|
| 1600 |
FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void) |
| 1601 |
{ |
| 1602 |
FLAC__Metadata_Chain *chain = calloc(1, sizeof(FLAC__Metadata_Chain)); |
| 1603 |
|
| 1604 |
if(0 != chain) |
| 1605 |
chain_init_(chain); |
| 1606 |
|
| 1607 |
return chain; |
| 1608 |
} |
| 1609 |
|
| 1610 |
FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain) |
| 1611 |
{ |
| 1612 |
FLAC__ASSERT(0 != chain); |
| 1613 |
|
| 1614 |
chain_clear_(chain); |
| 1615 |
|
| 1616 |
free(chain); |
| 1617 |
} |
| 1618 |
|
| 1619 |
FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain) |
| 1620 |
{ |
| 1621 |
FLAC__Metadata_ChainStatus status; |
| 1622 |
|
| 1623 |
FLAC__ASSERT(0 != chain); |
| 1624 |
|
| 1625 |
status = chain->status; |
| 1626 |
chain->status = FLAC__METADATA_CHAIN_STATUS_OK; |
| 1627 |
return status; |
| 1628 |
} |
| 1629 |
|
| 1630 |
static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg) |
| 1631 |
{ |
| 1632 |
FILE *file; |
| 1633 |
FLAC__bool ret; |
| 1634 |
|
| 1635 |
FLAC__ASSERT(0 != chain); |
| 1636 |
FLAC__ASSERT(0 != filename); |
| 1637 |
|
| 1638 |
chain_clear_(chain); |
| 1639 |
|
| 1640 |
if(0 == (chain->filename = strdup(filename))) { |
| 1641 |
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 1642 |
return false; |
| 1643 |
} |
| 1644 |
|
| 1645 |
chain->is_ogg = is_ogg; |
| 1646 |
|
| 1647 |
if(0 == (file = flac_fopen(filename, "rb"))) { |
| 1648 |
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 1649 |
return false; |
| 1650 |
} |
| 1651 |
|
| 1652 |
/* the function also sets chain->status for us */ |
| 1653 |
ret = is_ogg? |
| 1654 |
chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) : |
| 1655 |
chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_) |
| 1656 |
; |
| 1657 |
|
| 1658 |
fclose(file); |
| 1659 |
|
| 1660 |
return ret; |
| 1661 |
} |
| 1662 |
|
| 1663 |
FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename) |
| 1664 |
{ |
| 1665 |
return chain_read_(chain, filename, /*is_ogg=*/false); |
| 1666 |
} |
| 1667 |
|
| 1668 |
/*@@@@add to tests*/ |
| 1669 |
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename) |
| 1670 |
{ |
| 1671 |
return chain_read_(chain, filename, /*is_ogg=*/true); |
| 1672 |
} |
| 1673 |
|
| 1674 |
static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg) |
| 1675 |
{ |
| 1676 |
FLAC__bool ret; |
| 1677 |
|
| 1678 |
FLAC__ASSERT(0 != chain); |
| 1679 |
|
| 1680 |
chain_clear_(chain); |
| 1681 |
|
| 1682 |
if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) { |
| 1683 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1684 |
return false; |
| 1685 |
} |
| 1686 |
|
| 1687 |
chain->is_ogg = is_ogg; |
| 1688 |
|
| 1689 |
/* rewind */ |
| 1690 |
if(0 != callbacks.seek(handle, 0, SEEK_SET)) { |
| 1691 |
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1692 |
return false; |
| 1693 |
} |
| 1694 |
|
| 1695 |
/* the function also sets chain->status for us */ |
| 1696 |
ret = is_ogg? |
| 1697 |
chain_read_ogg_cb_(chain, handle, callbacks.read) : |
| 1698 |
chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell) |
| 1699 |
; |
| 1700 |
|
| 1701 |
return ret; |
| 1702 |
} |
| 1703 |
|
| 1704 |
FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1705 |
{ |
| 1706 |
return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false); |
| 1707 |
} |
| 1708 |
|
| 1709 |
/*@@@@add to tests*/ |
| 1710 |
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1711 |
{ |
| 1712 |
return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true); |
| 1713 |
} |
| 1714 |
|
| 1715 |
typedef enum { |
| 1716 |
LBS_NONE = 0, |
| 1717 |
LBS_SIZE_CHANGED, |
| 1718 |
LBS_BLOCK_ADDED, |
| 1719 |
LBS_BLOCK_REMOVED |
| 1720 |
} LastBlockState; |
| 1721 |
|
| 1722 |
#if defined(_MSC_VER) |
| 1723 |
// silence three MSVC warnings 'conversion from 'conversion from 'const __int64' to 'uint32_t', possible loss of data' |
| 1724 |
#pragma warning ( disable : 4244 ) |
| 1725 |
#endif |
| 1726 |
|
| 1727 |
FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding) |
| 1728 |
{ |
| 1729 |
/* This does all the same checks that are in chain_prepare_for_write_() |
| 1730 |
* but doesn't actually alter the chain. Make sure to update the logic |
| 1731 |
* here if chain_prepare_for_write_() changes. |
| 1732 |
*/ |
| 1733 |
FLAC__off_t current_length; |
| 1734 |
LastBlockState lbs_state = LBS_NONE; |
| 1735 |
uint32_t lbs_size = 0; |
| 1736 |
|
| 1737 |
FLAC__ASSERT(0 != chain); |
| 1738 |
|
| 1739 |
current_length = chain_calculate_length_(chain); |
| 1740 |
|
| 1741 |
if(use_padding) { |
| 1742 |
const FLAC__Metadata_Node * const node = chain->tail; |
| 1743 |
/* if the metadata shrank and the last block is padding, we just extend the last padding block */ |
| 1744 |
if(current_length < chain->initial_length && node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1745 |
lbs_state = LBS_SIZE_CHANGED; |
| 1746 |
lbs_size = node->data->length + (chain->initial_length - current_length); |
| 1747 |
} |
| 1748 |
/* if the metadata shrank more than 4 bytes then there's room to add another padding block */ |
| 1749 |
else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) { |
| 1750 |
lbs_state = LBS_BLOCK_ADDED; |
| 1751 |
lbs_size = chain->initial_length - (current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH); |
| 1752 |
} |
| 1753 |
/* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */ |
| 1754 |
else if(current_length > chain->initial_length) { |
| 1755 |
const FLAC__off_t delta = current_length - chain->initial_length; |
| 1756 |
if(node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 1757 |
/* if the delta is exactly the size of the last padding block, remove the padding block */ |
| 1758 |
if((FLAC__off_t)node->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) { |
| 1759 |
lbs_state = LBS_BLOCK_REMOVED; |
| 1760 |
lbs_size = 0; |
| 1761 |
} |
| 1762 |
/* if there is at least 'delta' bytes of padding, trim the padding down */ |
| 1763 |
else if((FLAC__off_t)node->data->length >= delta) { |
| 1764 |
lbs_state = LBS_SIZE_CHANGED; |
| 1765 |
lbs_size = node->data->length - delta; |
| 1766 |
} |
| 1767 |
} |
| 1768 |
} |
| 1769 |
} |
| 1770 |
|
| 1771 |
current_length = 0; |
| 1772 |
/* check sizes of all metadata blocks; reduce padding size if necessary */ |
| 1773 |
{ |
| 1774 |
const FLAC__Metadata_Node *node; |
| 1775 |
for(node = chain->head; node; node = node->next) { |
| 1776 |
uint32_t block_len = node->data->length; |
| 1777 |
if(node == chain->tail) { |
| 1778 |
if(lbs_state == LBS_BLOCK_REMOVED) |
| 1779 |
continue; |
| 1780 |
else if(lbs_state == LBS_SIZE_CHANGED) |
| 1781 |
block_len = lbs_size; |
| 1782 |
} |
| 1783 |
if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) { |
| 1784 |
if(node->data->type == FLAC__METADATA_TYPE_PADDING) |
| 1785 |
block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1; |
| 1786 |
else |
| 1787 |
return false /* the return value doesn't matter */; |
| 1788 |
} |
| 1789 |
current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len); |
| 1790 |
} |
| 1791 |
|
| 1792 |
if(lbs_state == LBS_BLOCK_ADDED) { |
| 1793 |
/* test added padding block */ |
| 1794 |
uint32_t block_len = lbs_size; |
| 1795 |
if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) |
| 1796 |
block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1; |
| 1797 |
current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len); |
| 1798 |
} |
| 1799 |
} |
| 1800 |
|
| 1801 |
return (current_length != chain->initial_length); |
| 1802 |
} |
| 1803 |
|
| 1804 |
#if defined(_MSC_VER) |
| 1805 |
#pragma warning ( default : 4244 ) |
| 1806 |
#endif |
| 1807 |
|
| 1808 |
FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats) |
| 1809 |
{ |
| 1810 |
struct flac_stat_s stats; |
| 1811 |
const char *tempfile_path_prefix = 0; |
| 1812 |
FLAC__off_t current_length; |
| 1813 |
|
| 1814 |
FLAC__ASSERT(0 != chain); |
| 1815 |
|
| 1816 |
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1817 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1818 |
return false; |
| 1819 |
} |
| 1820 |
|
| 1821 |
if (0 == chain->filename) { |
| 1822 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1823 |
return false; |
| 1824 |
} |
| 1825 |
|
| 1826 |
current_length = chain_prepare_for_write_(chain, use_padding); |
| 1827 |
|
| 1828 |
/* a return value of 0 means there was an error; chain->status is already set */ |
| 1829 |
if (0 == current_length) |
| 1830 |
return false; |
| 1831 |
|
| 1832 |
if(preserve_file_stats) |
| 1833 |
get_file_stats_(chain->filename, &stats); |
| 1834 |
|
| 1835 |
if(current_length == chain->initial_length) { |
| 1836 |
if(!chain_rewrite_metadata_in_place_(chain)) |
| 1837 |
return false; |
| 1838 |
} |
| 1839 |
else { |
| 1840 |
if(!chain_rewrite_file_(chain, tempfile_path_prefix, NULL)) |
| 1841 |
return false; |
| 1842 |
|
| 1843 |
/* recompute lengths and offsets */ |
| 1844 |
{ |
| 1845 |
const FLAC__Metadata_Node *node; |
| 1846 |
chain->initial_length = current_length; |
| 1847 |
chain->last_offset = chain->first_offset; |
| 1848 |
for(node = chain->head; node; node = node->next) |
| 1849 |
chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1850 |
} |
| 1851 |
} |
| 1852 |
|
| 1853 |
if(preserve_file_stats) |
| 1854 |
set_file_stats_(chain->filename, &stats); |
| 1855 |
|
| 1856 |
return true; |
| 1857 |
} |
| 1858 |
|
| 1859 |
FLAC_API FLAC__bool FLAC__metadata_chain_write_new_file(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool use_padding) |
| 1860 |
{ |
| 1861 |
FLAC__off_t current_length; |
| 1862 |
|
| 1863 |
FLAC__ASSERT(0 != chain); |
| 1864 |
|
| 1865 |
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1866 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1867 |
return false; |
| 1868 |
} |
| 1869 |
|
| 1870 |
if (0 == chain->filename) { |
| 1871 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1872 |
return false; |
| 1873 |
} |
| 1874 |
|
| 1875 |
if (0 == filename) { |
| 1876 |
chain->status = FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT; |
| 1877 |
return false; |
| 1878 |
} |
| 1879 |
|
| 1880 |
current_length = chain_prepare_for_write_(chain, use_padding); |
| 1881 |
|
| 1882 |
/* a return value of 0 means there was an error; chain->status is already set */ |
| 1883 |
if (0 == current_length) |
| 1884 |
return false; |
| 1885 |
|
| 1886 |
if(!chain_rewrite_file_(chain, NULL, filename)) |
| 1887 |
return false; |
| 1888 |
|
| 1889 |
/* recompute lengths and offsets */ |
| 1890 |
{ |
| 1891 |
const FLAC__Metadata_Node *node; |
| 1892 |
chain->initial_length = current_length; |
| 1893 |
chain->last_offset = chain->first_offset; |
| 1894 |
for(node = chain->head; node; node = node->next) |
| 1895 |
chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1896 |
} |
| 1897 |
|
| 1898 |
return true; |
| 1899 |
} |
| 1900 |
|
| 1901 |
FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks) |
| 1902 |
{ |
| 1903 |
FLAC__off_t current_length; |
| 1904 |
|
| 1905 |
FLAC__ASSERT(0 != chain); |
| 1906 |
|
| 1907 |
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1908 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1909 |
return false; |
| 1910 |
} |
| 1911 |
|
| 1912 |
if (0 != chain->filename) { |
| 1913 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1914 |
return false; |
| 1915 |
} |
| 1916 |
|
| 1917 |
if (0 == callbacks.write || 0 == callbacks.seek) { |
| 1918 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1919 |
return false; |
| 1920 |
} |
| 1921 |
|
| 1922 |
if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) { |
| 1923 |
chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL; |
| 1924 |
return false; |
| 1925 |
} |
| 1926 |
|
| 1927 |
current_length = chain_prepare_for_write_(chain, use_padding); |
| 1928 |
|
| 1929 |
/* a return value of 0 means there was an error; chain->status is already set */ |
| 1930 |
if (0 == current_length) |
| 1931 |
return false; |
| 1932 |
|
| 1933 |
FLAC__ASSERT(current_length == chain->initial_length); |
| 1934 |
|
| 1935 |
return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek); |
| 1936 |
} |
| 1937 |
|
| 1938 |
FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks) |
| 1939 |
{ |
| 1940 |
FLAC__off_t current_length; |
| 1941 |
|
| 1942 |
FLAC__ASSERT(0 != chain); |
| 1943 |
|
| 1944 |
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */ |
| 1945 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 1946 |
return false; |
| 1947 |
} |
| 1948 |
|
| 1949 |
if (0 != chain->filename) { |
| 1950 |
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH; |
| 1951 |
return false; |
| 1952 |
} |
| 1953 |
|
| 1954 |
if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) { |
| 1955 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1956 |
return false; |
| 1957 |
} |
| 1958 |
if (0 == temp_callbacks.write) { |
| 1959 |
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS; |
| 1960 |
return false; |
| 1961 |
} |
| 1962 |
|
| 1963 |
current_length = chain_prepare_for_write_(chain, use_padding); |
| 1964 |
|
| 1965 |
/* a return value of 0 means there was an error; chain->status is already set */ |
| 1966 |
if (0 == current_length) |
| 1967 |
return false; |
| 1968 |
|
| 1969 |
/* rewind */ |
| 1970 |
if(0 != callbacks.seek(handle, 0, SEEK_SET)) { |
| 1971 |
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 1972 |
return false; |
| 1973 |
} |
| 1974 |
|
| 1975 |
if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write)) |
| 1976 |
return false; |
| 1977 |
|
| 1978 |
/* recompute lengths and offsets */ |
| 1979 |
{ |
| 1980 |
const FLAC__Metadata_Node *node; |
| 1981 |
chain->initial_length = current_length; |
| 1982 |
chain->last_offset = chain->first_offset; |
| 1983 |
for(node = chain->head; node; node = node->next) |
| 1984 |
chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length); |
| 1985 |
} |
| 1986 |
|
| 1987 |
return true; |
| 1988 |
} |
| 1989 |
|
| 1990 |
FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain) |
| 1991 |
{ |
| 1992 |
FLAC__Metadata_Node *node; |
| 1993 |
|
| 1994 |
FLAC__ASSERT(0 != chain); |
| 1995 |
|
| 1996 |
for(node = chain->head; node; ) { |
| 1997 |
if(!chain_merge_adjacent_padding_(chain, node)) |
| 1998 |
node = node->next; |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain) |
| 2003 |
{ |
| 2004 |
FLAC__Metadata_Node *node, *save; |
| 2005 |
uint32_t i; |
| 2006 |
|
| 2007 |
FLAC__ASSERT(0 != chain); |
| 2008 |
|
| 2009 |
/* |
| 2010 |
* Don't try and be too smart... this simple algo is good enough for |
| 2011 |
* the small number of nodes that we deal with. |
| 2012 |
*/ |
| 2013 |
for(i = 0, node = chain->head; i < chain->nodes; i++) { |
| 2014 |
if(node->data->type == FLAC__METADATA_TYPE_PADDING) { |
| 2015 |
save = node->next; |
| 2016 |
chain_remove_node_(chain, node); |
| 2017 |
chain_append_node_(chain, node); |
| 2018 |
node = save; |
| 2019 |
} |
| 2020 |
else { |
| 2021 |
node = node->next; |
| 2022 |
} |
| 2023 |
} |
| 2024 |
|
| 2025 |
FLAC__metadata_chain_merge_padding(chain); |
| 2026 |
} |
| 2027 |
|
| 2028 |
|
| 2029 |
FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void) |
| 2030 |
{ |
| 2031 |
FLAC__Metadata_Iterator *iterator = calloc(1, sizeof(FLAC__Metadata_Iterator)); |
| 2032 |
|
| 2033 |
/* calloc() implies: |
| 2034 |
iterator->current = 0; |
| 2035 |
iterator->chain = 0; |
| 2036 |
*/ |
| 2037 |
|
| 2038 |
return iterator; |
| 2039 |
} |
| 2040 |
|
| 2041 |
FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator) |
| 2042 |
{ |
| 2043 |
FLAC__ASSERT(0 != iterator); |
| 2044 |
|
| 2045 |
free(iterator); |
| 2046 |
} |
| 2047 |
|
| 2048 |
FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain) |
| 2049 |
{ |
| 2050 |
FLAC__ASSERT(0 != iterator); |
| 2051 |
FLAC__ASSERT(0 != chain); |
| 2052 |
FLAC__ASSERT(0 != chain->head); |
| 2053 |
|
| 2054 |
iterator->chain = chain; |
| 2055 |
iterator->current = chain->head; |
| 2056 |
} |
| 2057 |
|
| 2058 |
FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator) |
| 2059 |
{ |
| 2060 |
FLAC__ASSERT(0 != iterator); |
| 2061 |
|
| 2062 |
if(0 == iterator->current || 0 == iterator->current->next) |
| 2063 |
return false; |
| 2064 |
|
| 2065 |
iterator->current = iterator->current->next; |
| 2066 |
return true; |
| 2067 |
} |
| 2068 |
|
| 2069 |
FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator) |
| 2070 |
{ |
| 2071 |
FLAC__ASSERT(0 != iterator); |
| 2072 |
|
| 2073 |
if(0 == iterator->current || 0 == iterator->current->prev) |
| 2074 |
return false; |
| 2075 |
|
| 2076 |
iterator->current = iterator->current->prev; |
| 2077 |
return true; |
| 2078 |
} |
| 2079 |
|
| 2080 |
FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator) |
| 2081 |
{ |
| 2082 |
FLAC__ASSERT(0 != iterator); |
| 2083 |
FLAC__ASSERT(0 != iterator->current); |
| 2084 |
FLAC__ASSERT(0 != iterator->current->data); |
| 2085 |
|
| 2086 |
return iterator->current->data->type; |
| 2087 |
} |
| 2088 |
|
| 2089 |
FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator) |
| 2090 |
{ |
| 2091 |
FLAC__ASSERT(0 != iterator); |
| 2092 |
FLAC__ASSERT(0 != iterator->current); |
| 2093 |
|
| 2094 |
return iterator->current->data; |
| 2095 |
} |
| 2096 |
|
| 2097 |
FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
| 2098 |
{ |
| 2099 |
FLAC__ASSERT(0 != iterator); |
| 2100 |
FLAC__ASSERT(0 != block); |
| 2101 |
return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block); |
| 2102 |
} |
| 2103 |
|
| 2104 |
FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding) |
| 2105 |
{ |
| 2106 |
FLAC__Metadata_Node *save; |
| 2107 |
|
| 2108 |
FLAC__ASSERT(0 != iterator); |
| 2109 |
FLAC__ASSERT(0 != iterator->current); |
| 2110 |
|
| 2111 |
if(0 == iterator->current->prev) { |
| 2112 |
FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 2113 |
return false; |
| 2114 |
} |
| 2115 |
|
| 2116 |
save = iterator->current->prev; |
| 2117 |
|
| 2118 |
if(replace_with_padding) { |
| 2119 |
FLAC__metadata_object_delete_data(iterator->current->data); |
| 2120 |
iterator->current->data->type = FLAC__METADATA_TYPE_PADDING; |
| 2121 |
} |
| 2122 |
else { |
| 2123 |
chain_delete_node_(iterator->chain, iterator->current); |
| 2124 |
} |
| 2125 |
|
| 2126 |
iterator->current = save; |
| 2127 |
return true; |
| 2128 |
} |
| 2129 |
|
| 2130 |
FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
| 2131 |
{ |
| 2132 |
FLAC__Metadata_Node *node; |
| 2133 |
|
| 2134 |
FLAC__ASSERT(0 != iterator); |
| 2135 |
FLAC__ASSERT(0 != iterator->current); |
| 2136 |
FLAC__ASSERT(0 != block); |
| 2137 |
|
| 2138 |
if(block->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 2139 |
return false; |
| 2140 |
|
| 2141 |
if(0 == iterator->current->prev) { |
| 2142 |
FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO); |
| 2143 |
return false; |
| 2144 |
} |
| 2145 |
|
| 2146 |
if(0 == (node = node_new_())) |
| 2147 |
return false; |
| 2148 |
|
| 2149 |
node->data = block; |
| 2150 |
iterator_insert_node_(iterator, node); |
| 2151 |
iterator->current = node; |
| 2152 |
return true; |
| 2153 |
} |
| 2154 |
|
| 2155 |
FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block) |
| 2156 |
{ |
| 2157 |
FLAC__Metadata_Node *node; |
| 2158 |
|
| 2159 |
FLAC__ASSERT(0 != iterator); |
| 2160 |
FLAC__ASSERT(0 != iterator->current); |
| 2161 |
FLAC__ASSERT(0 != block); |
| 2162 |
|
| 2163 |
if(block->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 2164 |
return false; |
| 2165 |
|
| 2166 |
if(0 == (node = node_new_())) |
| 2167 |
return false; |
| 2168 |
|
| 2169 |
node->data = block; |
| 2170 |
iterator_insert_node_after_(iterator, node); |
| 2171 |
iterator->current = node; |
| 2172 |
return true; |
| 2173 |
} |
| 2174 |
|
| 2175 |
|
| 2176 |
/**************************************************************************** |
| 2177 |
* |
| 2178 |
* Local function definitions |
| 2179 |
* |
| 2180 |
***************************************************************************/ |
| 2181 |
|
| 2182 |
void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, uint32_t bytes) |
| 2183 |
{ |
| 2184 |
uint32_t i; |
| 2185 |
|
| 2186 |
b += bytes; |
| 2187 |
|
| 2188 |
for(i = 0; i < bytes; i++) { |
| 2189 |
*(--b) = (FLAC__byte)(val & 0xff); |
| 2190 |
val >>= 8; |
| 2191 |
} |
| 2192 |
} |
| 2193 |
|
| 2194 |
void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, uint32_t bytes) |
| 2195 |
{ |
| 2196 |
uint32_t i; |
| 2197 |
|
| 2198 |
for(i = 0; i < bytes; i++) { |
| 2199 |
*(b++) = (FLAC__byte)(val & 0xff); |
| 2200 |
val >>= 8; |
| 2201 |
} |
| 2202 |
} |
| 2203 |
|
| 2204 |
void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, uint32_t bytes) |
| 2205 |
{ |
| 2206 |
uint32_t i; |
| 2207 |
|
| 2208 |
b += bytes; |
| 2209 |
|
| 2210 |
for(i = 0; i < bytes; i++) { |
| 2211 |
*(--b) = (FLAC__byte)(val & 0xff); |
| 2212 |
val >>= 8; |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
FLAC__uint32 unpack_uint32_(FLAC__byte *b, uint32_t bytes) |
| 2217 |
{ |
| 2218 |
FLAC__uint32 ret = 0; |
| 2219 |
uint32_t i; |
| 2220 |
|
| 2221 |
for(i = 0; i < bytes; i++) |
| 2222 |
ret = (ret << 8) | (FLAC__uint32)(*b++); |
| 2223 |
|
| 2224 |
return ret; |
| 2225 |
} |
| 2226 |
|
| 2227 |
FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, uint32_t bytes) |
| 2228 |
{ |
| 2229 |
FLAC__uint32 ret = 0; |
| 2230 |
uint32_t i; |
| 2231 |
|
| 2232 |
b += bytes; |
| 2233 |
|
| 2234 |
for(i = 0; i < bytes; i++) |
| 2235 |
ret = (ret << 8) | (FLAC__uint32)(*--b); |
| 2236 |
|
| 2237 |
return ret; |
| 2238 |
} |
| 2239 |
|
| 2240 |
FLAC__uint64 unpack_uint64_(FLAC__byte *b, uint32_t bytes) |
| 2241 |
{ |
| 2242 |
FLAC__uint64 ret = 0; |
| 2243 |
uint32_t i; |
| 2244 |
|
| 2245 |
for(i = 0; i < bytes; i++) |
| 2246 |
ret = (ret << 8) | (FLAC__uint64)(*b++); |
| 2247 |
|
| 2248 |
return ret; |
| 2249 |
} |
| 2250 |
|
| 2251 |
FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator) |
| 2252 |
{ |
| 2253 |
FLAC__ASSERT(0 != iterator); |
| 2254 |
FLAC__ASSERT(0 != iterator->file); |
| 2255 |
|
| 2256 |
if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) { |
| 2257 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2258 |
return false; |
| 2259 |
} |
| 2260 |
|
| 2261 |
return true; |
| 2262 |
} |
| 2263 |
|
| 2264 |
FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block) |
| 2265 |
{ |
| 2266 |
FLAC__ASSERT(0 != iterator); |
| 2267 |
FLAC__ASSERT(0 != iterator->file); |
| 2268 |
|
| 2269 |
iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block); |
| 2270 |
|
| 2271 |
return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK); |
| 2272 |
} |
| 2273 |
|
| 2274 |
FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, uint32_t *length) |
| 2275 |
{ |
| 2276 |
FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH]; |
| 2277 |
|
| 2278 |
if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH) |
| 2279 |
return false; |
| 2280 |
|
| 2281 |
*is_last = raw_header[0] & 0x80? true : false; |
| 2282 |
*type = (FLAC__MetadataType)(raw_header[0] & 0x7f); |
| 2283 |
*length = unpack_uint32_(raw_header + 1, 3); |
| 2284 |
|
| 2285 |
/* Note that we don't check: |
| 2286 |
* if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED) |
| 2287 |
* we just will read in an opaque block |
| 2288 |
*/ |
| 2289 |
|
| 2290 |
return true; |
| 2291 |
} |
| 2292 |
|
| 2293 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block) |
| 2294 |
{ |
| 2295 |
switch(block->type) { |
| 2296 |
case FLAC__METADATA_TYPE_STREAMINFO: |
| 2297 |
return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info); |
| 2298 |
case FLAC__METADATA_TYPE_PADDING: |
| 2299 |
return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length); |
| 2300 |
case FLAC__METADATA_TYPE_APPLICATION: |
| 2301 |
return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length); |
| 2302 |
case FLAC__METADATA_TYPE_SEEKTABLE: |
| 2303 |
return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length); |
| 2304 |
case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 2305 |
return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, seek_cb, &block->data.vorbis_comment, block->length); |
| 2306 |
case FLAC__METADATA_TYPE_CUESHEET: |
| 2307 |
return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet); |
| 2308 |
case FLAC__METADATA_TYPE_PICTURE: |
| 2309 |
return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture); |
| 2310 |
default: |
| 2311 |
return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length); |
| 2312 |
} |
| 2313 |
} |
| 2314 |
|
| 2315 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block) |
| 2316 |
{ |
| 2317 |
FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b; |
| 2318 |
|
| 2319 |
if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) |
| 2320 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2321 |
|
| 2322 |
b = buffer; |
| 2323 |
|
| 2324 |
/* we are using hardcoded numbers for simplicity but we should |
| 2325 |
* probably eventually write a bit-level unpacker and use the |
| 2326 |
* _STREAMINFO_ constants. |
| 2327 |
*/ |
| 2328 |
block->min_blocksize = unpack_uint32_(b, 2); b += 2; |
| 2329 |
block->max_blocksize = unpack_uint32_(b, 2); b += 2; |
| 2330 |
block->min_framesize = unpack_uint32_(b, 3); b += 3; |
| 2331 |
block->max_framesize = unpack_uint32_(b, 3); b += 3; |
| 2332 |
block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((uint32_t)(b[2] & 0xf0) >> 4); |
| 2333 |
block->channels = (uint32_t)((b[2] & 0x0e) >> 1) + 1; |
| 2334 |
block->bits_per_sample = ((((uint32_t)(b[2] & 0x01)) << 4) | (((uint32_t)(b[3] & 0xf0)) >> 4)) + 1; |
| 2335 |
block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4); |
| 2336 |
memcpy(block->md5sum, b+8, 16); |
| 2337 |
|
| 2338 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2339 |
} |
| 2340 |
|
| 2341 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, uint32_t block_length) |
| 2342 |
{ |
| 2343 |
(void)block; /* nothing to do; we don't care about reading the padding bytes */ |
| 2344 |
|
| 2345 |
if(0 != seek_cb(handle, block_length, SEEK_CUR)) |
| 2346 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2347 |
|
| 2348 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2349 |
} |
| 2350 |
|
| 2351 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, uint32_t block_length) |
| 2352 |
{ |
| 2353 |
const uint32_t id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 2354 |
|
| 2355 |
if(read_cb(block->id, 1, id_bytes, handle) != id_bytes) |
| 2356 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2357 |
|
| 2358 |
if(block_length < id_bytes) |
| 2359 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2360 |
|
| 2361 |
block_length -= id_bytes; |
| 2362 |
|
| 2363 |
if(block_length == 0) { |
| 2364 |
block->data = 0; |
| 2365 |
} |
| 2366 |
else { |
| 2367 |
if(0 == (block->data = malloc(block_length))) |
| 2368 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2369 |
|
| 2370 |
if(read_cb(block->data, 1, block_length, handle) != block_length) |
| 2371 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2372 |
} |
| 2373 |
|
| 2374 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2375 |
} |
| 2376 |
|
| 2377 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, uint32_t block_length) |
| 2378 |
{ |
| 2379 |
uint32_t i; |
| 2380 |
FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH]; |
| 2381 |
|
| 2382 |
if(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH != 0) |
| 2383 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2384 |
|
| 2385 |
block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 2386 |
|
| 2387 |
if(block->num_points == 0) |
| 2388 |
block->points = 0; |
| 2389 |
else if(0 == (block->points = safe_malloc_mul_2op_p(block->num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) |
| 2390 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2391 |
|
| 2392 |
for(i = 0; i < block->num_points; i++) { |
| 2393 |
if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) |
| 2394 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2395 |
/* some MAGIC NUMBERs here */ |
| 2396 |
block->points[i].sample_number = unpack_uint64_(buffer, 8); |
| 2397 |
block->points[i].stream_offset = unpack_uint64_(buffer+8, 8); |
| 2398 |
block->points[i].frame_samples = unpack_uint32_(buffer+16, 2); |
| 2399 |
} |
| 2400 |
|
| 2401 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2402 |
} |
| 2403 |
|
| 2404 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry, uint32_t max_length) |
| 2405 |
{ |
| 2406 |
const uint32_t entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; |
| 2407 |
FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2408 |
|
| 2409 |
FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer)); |
| 2410 |
|
| 2411 |
if(max_length < entry_length_len) |
| 2412 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2413 |
|
| 2414 |
max_length -= entry_length_len; |
| 2415 |
if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2416 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2417 |
entry->length = unpack_uint32_little_endian_(buffer, entry_length_len); |
| 2418 |
if(max_length < entry->length) { |
| 2419 |
entry->length = 0; |
| 2420 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2421 |
} |
| 2422 |
|
| 2423 |
if(0 != entry->entry) |
| 2424 |
free(entry->entry); |
| 2425 |
|
| 2426 |
if(0 == (entry->entry = safe_malloc_add_2op_(entry->length, /*+*/1))) |
| 2427 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2428 |
|
| 2429 |
if(entry->length > 0 && read_cb(entry->entry, 1, entry->length, handle) != entry->length) |
| 2430 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2431 |
|
| 2432 |
entry->entry[entry->length] = '\0'; |
| 2433 |
|
| 2434 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2435 |
} |
| 2436 |
|
| 2437 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, uint32_t block_length) |
| 2438 |
{ |
| 2439 |
uint32_t i; |
| 2440 |
FLAC__Metadata_SimpleIteratorStatus status; |
| 2441 |
const uint32_t num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8; |
| 2442 |
FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2443 |
|
| 2444 |
FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer)); |
| 2445 |
|
| 2446 |
status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string), block_length); |
| 2447 |
if(block_length >= 4) |
| 2448 |
block_length -= 4; |
| 2449 |
if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA) |
| 2450 |
goto skip; |
| 2451 |
else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
| 2452 |
return status; |
| 2453 |
block_length -= block->vendor_string.length; |
| 2454 |
|
| 2455 |
if(block_length < num_comments_len) goto skip; else block_length -= num_comments_len; |
| 2456 |
if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len) |
| 2457 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2458 |
block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len); |
| 2459 |
|
| 2460 |
if(block->num_comments == 0) { |
| 2461 |
block->comments = 0; |
| 2462 |
} |
| 2463 |
else if(block->num_comments > (block_length >> 2)) { /* each comment needs at least 4 byte */ |
| 2464 |
block->num_comments = 0; |
| 2465 |
status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2466 |
goto skip; |
| 2467 |
} |
| 2468 |
else if(0 == (block->comments = calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { |
| 2469 |
block->num_comments = 0; |
| 2470 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2471 |
} |
| 2472 |
|
| 2473 |
for(i = 0; i < block->num_comments; i++) { |
| 2474 |
status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i, block_length); |
| 2475 |
if(block_length >= 4) block_length -= 4; |
| 2476 |
if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA) { |
| 2477 |
block->num_comments = i; |
| 2478 |
goto skip; |
| 2479 |
} |
| 2480 |
else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) return status; |
| 2481 |
block_length -= block->comments[i].length; |
| 2482 |
} |
| 2483 |
|
| 2484 |
skip: |
| 2485 |
if(block_length > 0) { |
| 2486 |
/* bad metadata */ |
| 2487 |
if(0 != seek_cb(handle, block_length, SEEK_CUR)) |
| 2488 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 2489 |
} |
| 2490 |
|
| 2491 |
return status; |
| 2492 |
} |
| 2493 |
|
| 2494 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track) |
| 2495 |
{ |
| 2496 |
uint32_t i, len; |
| 2497 |
FLAC__byte buffer[32]; /* asserted below that this is big enough */ |
| 2498 |
|
| 2499 |
FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64)); |
| 2500 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8); |
| 2501 |
FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8); |
| 2502 |
|
| 2503 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0); |
| 2504 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8; |
| 2505 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2506 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2507 |
track->offset = unpack_uint64_(buffer, len); |
| 2508 |
|
| 2509 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0); |
| 2510 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8; |
| 2511 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2512 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2513 |
track->number = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2514 |
|
| 2515 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 2516 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8; |
| 2517 |
if(read_cb(track->isrc, 1, len, handle) != len) |
| 2518 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2519 |
|
| 2520 |
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0); |
| 2521 |
len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8; |
| 2522 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2523 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2524 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1); |
| 2525 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1); |
| 2526 |
track->type = buffer[0] >> 7; |
| 2527 |
track->pre_emphasis = (buffer[0] >> 6) & 1; |
| 2528 |
|
| 2529 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0); |
| 2530 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8; |
| 2531 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2532 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2533 |
track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2534 |
|
| 2535 |
if(track->num_indices == 0) { |
| 2536 |
track->indices = 0; |
| 2537 |
} |
| 2538 |
else if(0 == (track->indices = calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) |
| 2539 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2540 |
|
| 2541 |
for(i = 0; i < track->num_indices; i++) { |
| 2542 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0); |
| 2543 |
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8; |
| 2544 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2545 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2546 |
track->indices[i].offset = unpack_uint64_(buffer, len); |
| 2547 |
|
| 2548 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0); |
| 2549 |
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8; |
| 2550 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2551 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2552 |
track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len); |
| 2553 |
|
| 2554 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0); |
| 2555 |
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8; |
| 2556 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2557 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2558 |
} |
| 2559 |
|
| 2560 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2561 |
} |
| 2562 |
|
| 2563 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block) |
| 2564 |
{ |
| 2565 |
uint32_t i, len; |
| 2566 |
FLAC__Metadata_SimpleIteratorStatus status; |
| 2567 |
FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */ |
| 2568 |
|
| 2569 |
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer)); |
| 2570 |
FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer)); |
| 2571 |
|
| 2572 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 2573 |
len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8; |
| 2574 |
if(read_cb(block->media_catalog_number, 1, len, handle) != len) |
| 2575 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2576 |
|
| 2577 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0); |
| 2578 |
len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8; |
| 2579 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2580 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2581 |
block->lead_in = unpack_uint64_(buffer, len); |
| 2582 |
|
| 2583 |
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0); |
| 2584 |
len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8; |
| 2585 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2586 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2587 |
block->is_cd = buffer[0]&0x80? true : false; |
| 2588 |
|
| 2589 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0); |
| 2590 |
len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8; |
| 2591 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2592 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2593 |
block->num_tracks = unpack_uint32_(buffer, len); |
| 2594 |
|
| 2595 |
if(block->num_tracks == 0) { |
| 2596 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2597 |
} |
| 2598 |
else if(0 == (block->tracks = calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) |
| 2599 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2600 |
|
| 2601 |
for(i = 0; i < block->num_tracks; i++) { |
| 2602 |
if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i))) |
| 2603 |
return status; |
| 2604 |
} |
| 2605 |
|
| 2606 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2607 |
} |
| 2608 |
|
| 2609 |
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cstring_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__byte **data, FLAC__uint32 *length, FLAC__uint32 length_len) |
| 2610 |
{ |
| 2611 |
FLAC__byte buffer[sizeof(FLAC__uint32)]; |
| 2612 |
|
| 2613 |
FLAC__ASSERT(0 != data); |
| 2614 |
FLAC__ASSERT(length_len%8 == 0); |
| 2615 |
|
| 2616 |
length_len /= 8; /* convert to bytes */ |
| 2617 |
|
| 2618 |
FLAC__ASSERT(sizeof(buffer) >= length_len); |
| 2619 |
|
| 2620 |
if(read_cb(buffer, 1, length_len, handle) != length_len) |
| 2621 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2622 |
*length = unpack_uint32_(buffer, length_len); |
| 2623 |
|
| 2624 |
if(*length > (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) /* data cannot be larger than FLAC metadata block */ |
| 2625 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA; |
| 2626 |
|
| 2627 |
if(0 != *data) |
| 2628 |
free(*data); |
| 2629 |
|
| 2630 |
if(0 == (*data = safe_malloc_add_2op_(*length, /*+*/1))) |
| 2631 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2632 |
|
| 2633 |
if(*length > 0) { |
| 2634 |
if(read_cb(*data, 1, *length, handle) != *length) |
| 2635 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2636 |
} |
| 2637 |
|
| 2638 |
(*data)[*length] = '\0'; |
| 2639 |
|
| 2640 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2641 |
} |
| 2642 |
|
| 2643 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block) |
| 2644 |
{ |
| 2645 |
FLAC__Metadata_SimpleIteratorStatus status; |
| 2646 |
FLAC__byte buffer[4]; /* asserted below that this is big enough */ |
| 2647 |
FLAC__uint32 len; |
| 2648 |
|
| 2649 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8); |
| 2650 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8); |
| 2651 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8); |
| 2652 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8); |
| 2653 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8); |
| 2654 |
|
| 2655 |
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0); |
| 2656 |
len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8; |
| 2657 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2658 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2659 |
block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len); |
| 2660 |
|
| 2661 |
if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, (FLAC__byte**)(&(block->mime_type)), &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
| 2662 |
return status; |
| 2663 |
|
| 2664 |
if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->description), &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
| 2665 |
return status; |
| 2666 |
|
| 2667 |
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0); |
| 2668 |
len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8; |
| 2669 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2670 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2671 |
block->width = unpack_uint32_(buffer, len); |
| 2672 |
|
| 2673 |
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0); |
| 2674 |
len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8; |
| 2675 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2676 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2677 |
block->height = unpack_uint32_(buffer, len); |
| 2678 |
|
| 2679 |
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0); |
| 2680 |
len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8; |
| 2681 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2682 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2683 |
block->depth = unpack_uint32_(buffer, len); |
| 2684 |
|
| 2685 |
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0); |
| 2686 |
len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8; |
| 2687 |
if(read_cb(buffer, 1, len, handle) != len) |
| 2688 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2689 |
block->colors = unpack_uint32_(buffer, len); |
| 2690 |
|
| 2691 |
/* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */ |
| 2692 |
if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->data), &(block->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) |
| 2693 |
return status; |
| 2694 |
|
| 2695 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2696 |
} |
| 2697 |
|
| 2698 |
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, uint32_t block_length) |
| 2699 |
{ |
| 2700 |
if(block_length == 0) { |
| 2701 |
block->data = 0; |
| 2702 |
} |
| 2703 |
else { |
| 2704 |
if(0 == (block->data = malloc(block_length))) |
| 2705 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 2706 |
|
| 2707 |
if(read_cb(block->data, 1, block_length, handle) != block_length) |
| 2708 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 2709 |
} |
| 2710 |
|
| 2711 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2712 |
} |
| 2713 |
|
| 2714 |
FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block) |
| 2715 |
{ |
| 2716 |
FLAC__ASSERT(0 != file); |
| 2717 |
FLAC__ASSERT(0 != status); |
| 2718 |
|
| 2719 |
if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) { |
| 2720 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 2721 |
return false; |
| 2722 |
} |
| 2723 |
|
| 2724 |
return true; |
| 2725 |
} |
| 2726 |
|
| 2727 |
FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block) |
| 2728 |
{ |
| 2729 |
FLAC__ASSERT(0 != file); |
| 2730 |
FLAC__ASSERT(0 != status); |
| 2731 |
|
| 2732 |
if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) { |
| 2733 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK; |
| 2734 |
return true; |
| 2735 |
} |
| 2736 |
else { |
| 2737 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 2738 |
return false; |
| 2739 |
} |
| 2740 |
} |
| 2741 |
|
| 2742 |
FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block) |
| 2743 |
{ |
| 2744 |
FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH]; |
| 2745 |
|
| 2746 |
FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN)); |
| 2747 |
/* double protection */ |
| 2748 |
if(block->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) |
| 2749 |
return false; |
| 2750 |
|
| 2751 |
buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type; |
| 2752 |
pack_uint32_(block->length, buffer + 1, 3); |
| 2753 |
|
| 2754 |
if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH) |
| 2755 |
return false; |
| 2756 |
|
| 2757 |
return true; |
| 2758 |
} |
| 2759 |
|
| 2760 |
FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block) |
| 2761 |
{ |
| 2762 |
FLAC__ASSERT(0 != block); |
| 2763 |
|
| 2764 |
switch(block->type) { |
| 2765 |
case FLAC__METADATA_TYPE_STREAMINFO: |
| 2766 |
return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info); |
| 2767 |
case FLAC__METADATA_TYPE_PADDING: |
| 2768 |
return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length); |
| 2769 |
case FLAC__METADATA_TYPE_APPLICATION: |
| 2770 |
return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length); |
| 2771 |
case FLAC__METADATA_TYPE_SEEKTABLE: |
| 2772 |
return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table); |
| 2773 |
case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 2774 |
return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment); |
| 2775 |
case FLAC__METADATA_TYPE_CUESHEET: |
| 2776 |
return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet); |
| 2777 |
case FLAC__METADATA_TYPE_PICTURE: |
| 2778 |
return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture); |
| 2779 |
default: |
| 2780 |
return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length); |
| 2781 |
} |
| 2782 |
} |
| 2783 |
|
| 2784 |
FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block) |
| 2785 |
{ |
| 2786 |
FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH]; |
| 2787 |
const uint32_t channels1 = block->channels - 1; |
| 2788 |
const uint32_t bps1 = block->bits_per_sample - 1; |
| 2789 |
|
| 2790 |
/* we are using hardcoded numbers for simplicity but we should |
| 2791 |
* probably eventually write a bit-level packer and use the |
| 2792 |
* _STREAMINFO_ constants. |
| 2793 |
*/ |
| 2794 |
pack_uint32_(block->min_blocksize, buffer, 2); |
| 2795 |
pack_uint32_(block->max_blocksize, buffer+2, 2); |
| 2796 |
pack_uint32_(block->min_framesize, buffer+4, 3); |
| 2797 |
pack_uint32_(block->max_framesize, buffer+7, 3); |
| 2798 |
buffer[10] = (block->sample_rate >> 12) & 0xff; |
| 2799 |
buffer[11] = (block->sample_rate >> 4) & 0xff; |
| 2800 |
buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4); |
| 2801 |
buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f)); |
| 2802 |
pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4); |
| 2803 |
memcpy(buffer+18, block->md5sum, 16); |
| 2804 |
|
| 2805 |
if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) |
| 2806 |
return false; |
| 2807 |
|
| 2808 |
return true; |
| 2809 |
} |
| 2810 |
|
| 2811 |
FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, uint32_t block_length) |
| 2812 |
{ |
| 2813 |
uint32_t i, n = block_length; |
| 2814 |
FLAC__byte buffer[1024]; |
| 2815 |
|
| 2816 |
(void)block; |
| 2817 |
|
| 2818 |
memset(buffer, 0, 1024); |
| 2819 |
|
| 2820 |
for(i = 0; i < n/1024; i++) |
| 2821 |
if(write_cb(buffer, 1, 1024, handle) != 1024) |
| 2822 |
return false; |
| 2823 |
|
| 2824 |
n %= 1024; |
| 2825 |
|
| 2826 |
if(write_cb(buffer, 1, n, handle) != n) |
| 2827 |
return false; |
| 2828 |
|
| 2829 |
return true; |
| 2830 |
} |
| 2831 |
|
| 2832 |
FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, uint32_t block_length) |
| 2833 |
{ |
| 2834 |
const uint32_t id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; |
| 2835 |
|
| 2836 |
if(write_cb(block->id, 1, id_bytes, handle) != id_bytes) |
| 2837 |
return false; |
| 2838 |
|
| 2839 |
block_length -= id_bytes; |
| 2840 |
|
| 2841 |
if(write_cb(block->data, 1, block_length, handle) != block_length) |
| 2842 |
return false; |
| 2843 |
|
| 2844 |
return true; |
| 2845 |
} |
| 2846 |
|
| 2847 |
FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block) |
| 2848 |
{ |
| 2849 |
uint32_t i; |
| 2850 |
FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH]; |
| 2851 |
|
| 2852 |
for(i = 0; i < block->num_points; i++) { |
| 2853 |
/* some MAGIC NUMBERs here */ |
| 2854 |
pack_uint64_(block->points[i].sample_number, buffer, 8); |
| 2855 |
pack_uint64_(block->points[i].stream_offset, buffer+8, 8); |
| 2856 |
pack_uint32_(block->points[i].frame_samples, buffer+16, 2); |
| 2857 |
if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) |
| 2858 |
return false; |
| 2859 |
} |
| 2860 |
|
| 2861 |
return true; |
| 2862 |
} |
| 2863 |
|
| 2864 |
FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block) |
| 2865 |
{ |
| 2866 |
uint32_t i; |
| 2867 |
const uint32_t entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; |
| 2868 |
const uint32_t num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8; |
| 2869 |
FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2870 |
|
| 2871 |
FLAC__ASSERT(flac_max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer)); |
| 2872 |
|
| 2873 |
pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len); |
| 2874 |
if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2875 |
return false; |
| 2876 |
if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length) |
| 2877 |
return false; |
| 2878 |
|
| 2879 |
pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len); |
| 2880 |
if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len) |
| 2881 |
return false; |
| 2882 |
|
| 2883 |
for(i = 0; i < block->num_comments; i++) { |
| 2884 |
pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len); |
| 2885 |
if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len) |
| 2886 |
return false; |
| 2887 |
if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length) |
| 2888 |
return false; |
| 2889 |
} |
| 2890 |
|
| 2891 |
return true; |
| 2892 |
} |
| 2893 |
|
| 2894 |
FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block) |
| 2895 |
{ |
| 2896 |
uint32_t i, j, len; |
| 2897 |
FLAC__byte buffer[1024]; /* asserted below that this is big enough */ |
| 2898 |
|
| 2899 |
FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64)); |
| 2900 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8); |
| 2901 |
FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)/8); |
| 2902 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8); |
| 2903 |
|
| 2904 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
| 2905 |
len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8; |
| 2906 |
if(write_cb(block->media_catalog_number, 1, len, handle) != len) |
| 2907 |
return false; |
| 2908 |
|
| 2909 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0); |
| 2910 |
len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8; |
| 2911 |
pack_uint64_(block->lead_in, buffer, len); |
| 2912 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2913 |
return false; |
| 2914 |
|
| 2915 |
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0); |
| 2916 |
len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8; |
| 2917 |
memset(buffer, 0, len); |
| 2918 |
if(block->is_cd) |
| 2919 |
buffer[0] |= 0x80; |
| 2920 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2921 |
return false; |
| 2922 |
|
| 2923 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0); |
| 2924 |
len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8; |
| 2925 |
pack_uint32_(block->num_tracks, buffer, len); |
| 2926 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2927 |
return false; |
| 2928 |
|
| 2929 |
for(i = 0; i < block->num_tracks; i++) { |
| 2930 |
FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i; |
| 2931 |
|
| 2932 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0); |
| 2933 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8; |
| 2934 |
pack_uint64_(track->offset, buffer, len); |
| 2935 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2936 |
return false; |
| 2937 |
|
| 2938 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0); |
| 2939 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8; |
| 2940 |
pack_uint32_(track->number, buffer, len); |
| 2941 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2942 |
return false; |
| 2943 |
|
| 2944 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
| 2945 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8; |
| 2946 |
if(write_cb(track->isrc, 1, len, handle) != len) |
| 2947 |
return false; |
| 2948 |
|
| 2949 |
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0); |
| 2950 |
len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8; |
| 2951 |
memset(buffer, 0, len); |
| 2952 |
buffer[0] = (track->type << 7) | (track->pre_emphasis << 6); |
| 2953 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2954 |
return false; |
| 2955 |
|
| 2956 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0); |
| 2957 |
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8; |
| 2958 |
pack_uint32_(track->num_indices, buffer, len); |
| 2959 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2960 |
return false; |
| 2961 |
|
| 2962 |
for(j = 0; j < track->num_indices; j++) { |
| 2963 |
FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + j; |
| 2964 |
|
| 2965 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0); |
| 2966 |
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8; |
| 2967 |
pack_uint64_(indx->offset, buffer, len); |
| 2968 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2969 |
return false; |
| 2970 |
|
| 2971 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0); |
| 2972 |
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8; |
| 2973 |
pack_uint32_(indx->number, buffer, len); |
| 2974 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2975 |
return false; |
| 2976 |
|
| 2977 |
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0); |
| 2978 |
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8; |
| 2979 |
memset(buffer, 0, len); |
| 2980 |
if(write_cb(buffer, 1, len, handle) != len) |
| 2981 |
return false; |
| 2982 |
} |
| 2983 |
} |
| 2984 |
|
| 2985 |
return true; |
| 2986 |
} |
| 2987 |
|
| 2988 |
FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block) |
| 2989 |
{ |
| 2990 |
uint32_t len; |
| 2991 |
size_t slen; |
| 2992 |
FLAC__byte buffer[4]; /* magic number is asserted below */ |
| 2993 |
|
| 2994 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8); |
| 2995 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8); |
| 2996 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8); |
| 2997 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8); |
| 2998 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8); |
| 2999 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8); |
| 3000 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8); |
| 3001 |
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8); |
| 3002 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8); |
| 3003 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8); |
| 3004 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8); |
| 3005 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8); |
| 3006 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8); |
| 3007 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8); |
| 3008 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8); |
| 3009 |
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8); |
| 3010 |
|
| 3011 |
len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8; |
| 3012 |
pack_uint32_(block->type, buffer, len); |
| 3013 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3014 |
return false; |
| 3015 |
|
| 3016 |
len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8; |
| 3017 |
slen = strlen(block->mime_type); |
| 3018 |
pack_uint32_(slen, buffer, len); |
| 3019 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3020 |
return false; |
| 3021 |
if(write_cb(block->mime_type, 1, slen, handle) != slen) |
| 3022 |
return false; |
| 3023 |
|
| 3024 |
len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8; |
| 3025 |
slen = strlen((const char *)block->description); |
| 3026 |
pack_uint32_(slen, buffer, len); |
| 3027 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3028 |
return false; |
| 3029 |
if(write_cb(block->description, 1, slen, handle) != slen) |
| 3030 |
return false; |
| 3031 |
|
| 3032 |
len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8; |
| 3033 |
pack_uint32_(block->width, buffer, len); |
| 3034 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3035 |
return false; |
| 3036 |
|
| 3037 |
len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8; |
| 3038 |
pack_uint32_(block->height, buffer, len); |
| 3039 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3040 |
return false; |
| 3041 |
|
| 3042 |
len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8; |
| 3043 |
pack_uint32_(block->depth, buffer, len); |
| 3044 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3045 |
return false; |
| 3046 |
|
| 3047 |
len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8; |
| 3048 |
pack_uint32_(block->colors, buffer, len); |
| 3049 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3050 |
return false; |
| 3051 |
|
| 3052 |
len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8; |
| 3053 |
pack_uint32_(block->data_length, buffer, len); |
| 3054 |
if(write_cb(buffer, 1, len, handle) != len) |
| 3055 |
return false; |
| 3056 |
if(write_cb(block->data, 1, block->data_length, handle) != block->data_length) |
| 3057 |
return false; |
| 3058 |
|
| 3059 |
return true; |
| 3060 |
} |
| 3061 |
|
| 3062 |
FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, uint32_t block_length) |
| 3063 |
{ |
| 3064 |
if(write_cb(block->data, 1, block_length, handle) != block_length) |
| 3065 |
return false; |
| 3066 |
|
| 3067 |
return true; |
| 3068 |
} |
| 3069 |
|
| 3070 |
FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block) |
| 3071 |
{ |
| 3072 |
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 3073 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3074 |
return false; |
| 3075 |
} |
| 3076 |
|
| 3077 |
if(!write_metadata_block_header_(iterator->file, &iterator->status, block)) |
| 3078 |
return false; |
| 3079 |
|
| 3080 |
if(!write_metadata_block_data_(iterator->file, &iterator->status, block)) |
| 3081 |
return false; |
| 3082 |
|
| 3083 |
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 3084 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3085 |
return false; |
| 3086 |
} |
| 3087 |
|
| 3088 |
return read_metadata_block_header_(iterator); |
| 3089 |
} |
| 3090 |
|
| 3091 |
FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, uint32_t padding_length, FLAC__bool padding_is_last) |
| 3092 |
{ |
| 3093 |
FLAC__StreamMetadata *padding; |
| 3094 |
|
| 3095 |
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 3096 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3097 |
return false; |
| 3098 |
} |
| 3099 |
|
| 3100 |
block->is_last = false; |
| 3101 |
|
| 3102 |
if(!write_metadata_block_header_(iterator->file, &iterator->status, block)) |
| 3103 |
return false; |
| 3104 |
|
| 3105 |
if(!write_metadata_block_data_(iterator->file, &iterator->status, block)) |
| 3106 |
return false; |
| 3107 |
|
| 3108 |
if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) |
| 3109 |
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3110 |
|
| 3111 |
padding->is_last = padding_is_last; |
| 3112 |
padding->length = padding_length; |
| 3113 |
|
| 3114 |
if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) { |
| 3115 |
FLAC__metadata_object_delete(padding); |
| 3116 |
return false; |
| 3117 |
} |
| 3118 |
|
| 3119 |
if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) { |
| 3120 |
FLAC__metadata_object_delete(padding); |
| 3121 |
return false; |
| 3122 |
} |
| 3123 |
|
| 3124 |
FLAC__metadata_object_delete(padding); |
| 3125 |
|
| 3126 |
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 3127 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3128 |
return false; |
| 3129 |
} |
| 3130 |
|
| 3131 |
return read_metadata_block_header_(iterator); |
| 3132 |
} |
| 3133 |
|
| 3134 |
FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append) |
| 3135 |
{ |
| 3136 |
FILE *tempfile = NULL; |
| 3137 |
char *tempfilename = NULL; |
| 3138 |
int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */ |
| 3139 |
FLAC__off_t fixup_is_last_flag_offset = -1; |
| 3140 |
|
| 3141 |
FLAC__ASSERT(0 != block || append == false); |
| 3142 |
|
| 3143 |
if(iterator->is_last) { |
| 3144 |
if(append) { |
| 3145 |
fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */ |
| 3146 |
fixup_is_last_flag_offset = iterator->offset[iterator->depth]; |
| 3147 |
} |
| 3148 |
else if(0 == block) { |
| 3149 |
simple_iterator_push_(iterator); |
| 3150 |
if(!FLAC__metadata_simple_iterator_prev(iterator)) { |
| 3151 |
(void)simple_iterator_pop_(iterator); |
| 3152 |
return false; |
| 3153 |
} |
| 3154 |
fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */ |
| 3155 |
fixup_is_last_flag_offset = iterator->offset[iterator->depth]; |
| 3156 |
if(!simple_iterator_pop_(iterator)) |
| 3157 |
return false; |
| 3158 |
} |
| 3159 |
} |
| 3160 |
|
| 3161 |
if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append)) |
| 3162 |
return false; |
| 3163 |
|
| 3164 |
if(0 != block) { |
| 3165 |
if(!write_metadata_block_header_(tempfile, &iterator->status, block)) { |
| 3166 |
cleanup_tempfile_(&tempfile, &tempfilename); |
| 3167 |
return false; |
| 3168 |
} |
| 3169 |
|
| 3170 |
if(!write_metadata_block_data_(tempfile, &iterator->status, block)) { |
| 3171 |
cleanup_tempfile_(&tempfile, &tempfilename); |
| 3172 |
return false; |
| 3173 |
} |
| 3174 |
} |
| 3175 |
|
| 3176 |
if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0)) |
| 3177 |
return false; |
| 3178 |
|
| 3179 |
if(append) |
| 3180 |
return FLAC__metadata_simple_iterator_next(iterator); |
| 3181 |
|
| 3182 |
return true; |
| 3183 |
} |
| 3184 |
|
| 3185 |
void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator) |
| 3186 |
{ |
| 3187 |
FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH); |
| 3188 |
iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth]; |
| 3189 |
iterator->depth++; |
| 3190 |
} |
| 3191 |
|
| 3192 |
FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator) |
| 3193 |
{ |
| 3194 |
FLAC__ASSERT(iterator->depth > 0); |
| 3195 |
iterator->depth--; |
| 3196 |
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) { |
| 3197 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3198 |
return false; |
| 3199 |
} |
| 3200 |
|
| 3201 |
return read_metadata_block_header_(iterator); |
| 3202 |
} |
| 3203 |
|
| 3204 |
/* return meanings: |
| 3205 |
* 0: ok |
| 3206 |
* 1: read error |
| 3207 |
* 2: seek error |
| 3208 |
* 3: not a FLAC file |
| 3209 |
*/ |
| 3210 |
uint32_t seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb) |
| 3211 |
{ |
| 3212 |
FLAC__byte buffer[4]; |
| 3213 |
size_t n; |
| 3214 |
uint32_t i; |
| 3215 |
|
| 3216 |
FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer)); |
| 3217 |
|
| 3218 |
/* skip any id3v2 tag */ |
| 3219 |
errno = 0; |
| 3220 |
n = read_cb(buffer, 1, 4, handle); |
| 3221 |
if(errno) |
| 3222 |
return 1; |
| 3223 |
else if(n != 4) |
| 3224 |
return 3; |
| 3225 |
else if(0 == memcmp(buffer, "ID3", 3)) { |
| 3226 |
uint32_t tag_length = 0; |
| 3227 |
|
| 3228 |
/* skip to the tag length */ |
| 3229 |
if(seek_cb(handle, 2, SEEK_CUR) < 0) |
| 3230 |
return 2; |
| 3231 |
|
| 3232 |
/* read the length */ |
| 3233 |
for(i = 0; i < 4; i++) { |
| 3234 |
if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80) |
| 3235 |
return 1; |
| 3236 |
tag_length <<= 7; |
| 3237 |
tag_length |= (buffer[0] & 0x7f); |
| 3238 |
} |
| 3239 |
|
| 3240 |
/* skip the rest of the tag */ |
| 3241 |
if(seek_cb(handle, tag_length, SEEK_CUR) < 0) |
| 3242 |
return 2; |
| 3243 |
|
| 3244 |
/* read the stream sync code */ |
| 3245 |
errno = 0; |
| 3246 |
n = read_cb(buffer, 1, 4, handle); |
| 3247 |
if(errno) |
| 3248 |
return 1; |
| 3249 |
else if(n != 4) |
| 3250 |
return 3; |
| 3251 |
} |
| 3252 |
|
| 3253 |
/* check for the fLaC signature */ |
| 3254 |
if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH)) |
| 3255 |
return 0; |
| 3256 |
else |
| 3257 |
return 3; |
| 3258 |
} |
| 3259 |
|
| 3260 |
uint32_t seek_to_first_metadata_block_(FILE *f) |
| 3261 |
{ |
| 3262 |
return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_); |
| 3263 |
} |
| 3264 |
|
| 3265 |
FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append) |
| 3266 |
{ |
| 3267 |
const FLAC__off_t offset_end = append? iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length : iterator->offset[iterator->depth]; |
| 3268 |
|
| 3269 |
if(0 != fseeko(iterator->file, 0, SEEK_SET)) { |
| 3270 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3271 |
return false; |
| 3272 |
} |
| 3273 |
if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) { |
| 3274 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3275 |
return false; |
| 3276 |
} |
| 3277 |
if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) { |
| 3278 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3279 |
return false; |
| 3280 |
} |
| 3281 |
|
| 3282 |
return true; |
| 3283 |
} |
| 3284 |
|
| 3285 |
FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup) |
| 3286 |
{ |
| 3287 |
FLAC__off_t save_offset = iterator->offset[iterator->depth]; |
| 3288 |
FLAC__ASSERT(0 != *tempfile); |
| 3289 |
|
| 3290 |
if(0 != fseeko(iterator->file, save_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length, SEEK_SET)) { |
| 3291 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3292 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3293 |
return false; |
| 3294 |
} |
| 3295 |
if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) { |
| 3296 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3297 |
return false; |
| 3298 |
} |
| 3299 |
|
| 3300 |
if(fixup_is_last_code != 0) { |
| 3301 |
/* |
| 3302 |
* if code == 1, it means a block was appended to the end so |
| 3303 |
* we have to clear the is_last flag of the previous block |
| 3304 |
* if code == -1, it means the last block was deleted so |
| 3305 |
* we have to set the is_last flag of the previous block |
| 3306 |
*/ |
| 3307 |
/* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */ |
| 3308 |
FLAC__byte x; |
| 3309 |
if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) { |
| 3310 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3311 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3312 |
return false; |
| 3313 |
} |
| 3314 |
if(fread(&x, 1, 1, *tempfile) != 1) { |
| 3315 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3316 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3317 |
return false; |
| 3318 |
} |
| 3319 |
if(fixup_is_last_code > 0) { |
| 3320 |
FLAC__ASSERT(x & 0x80); |
| 3321 |
x &= 0x7f; |
| 3322 |
} |
| 3323 |
else { |
| 3324 |
FLAC__ASSERT(!(x & 0x80)); |
| 3325 |
x |= 0x80; |
| 3326 |
} |
| 3327 |
if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) { |
| 3328 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3329 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR; |
| 3330 |
return false; |
| 3331 |
} |
| 3332 |
if(local__fwrite(&x, 1, 1, *tempfile) != 1) { |
| 3333 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3334 |
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3335 |
return false; |
| 3336 |
} |
| 3337 |
} |
| 3338 |
|
| 3339 |
(void)fclose(iterator->file); |
| 3340 |
|
| 3341 |
if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status)) |
| 3342 |
return false; |
| 3343 |
|
| 3344 |
if(iterator->has_stats) |
| 3345 |
set_file_stats_(iterator->filename, &iterator->stats); |
| 3346 |
|
| 3347 |
if(!simple_iterator_prime_input_(iterator, !iterator->is_writable)) |
| 3348 |
return false; |
| 3349 |
if(backup) { |
| 3350 |
while(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length < save_offset) |
| 3351 |
if(!FLAC__metadata_simple_iterator_next(iterator)) |
| 3352 |
return false; |
| 3353 |
return true; |
| 3354 |
} |
| 3355 |
else { |
| 3356 |
/* move the iterator to it's original block faster by faking a push, then doing a pop_ */ |
| 3357 |
FLAC__ASSERT(iterator->depth == 0); |
| 3358 |
iterator->offset[0] = save_offset; |
| 3359 |
iterator->depth++; |
| 3360 |
return simple_iterator_pop_(iterator); |
| 3361 |
} |
| 3362 |
} |
| 3363 |
|
| 3364 |
FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3365 |
{ |
| 3366 |
FLAC__byte buffer[8192]; |
| 3367 |
size_t n; |
| 3368 |
|
| 3369 |
FLAC__ASSERT(bytes >= 0); |
| 3370 |
while(bytes > 0) { |
| 3371 |
n = flac_min(sizeof(buffer), (size_t)bytes); |
| 3372 |
if(fread(buffer, 1, n, file) != n) { |
| 3373 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3374 |
return false; |
| 3375 |
} |
| 3376 |
if(local__fwrite(buffer, 1, n, tempfile) != n) { |
| 3377 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3378 |
return false; |
| 3379 |
} |
| 3380 |
bytes -= n; |
| 3381 |
} |
| 3382 |
|
| 3383 |
return true; |
| 3384 |
} |
| 3385 |
|
| 3386 |
FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3387 |
{ |
| 3388 |
FLAC__byte buffer[8192]; |
| 3389 |
size_t n; |
| 3390 |
|
| 3391 |
FLAC__ASSERT(bytes >= 0); |
| 3392 |
while(bytes > 0) { |
| 3393 |
n = flac_min(sizeof(buffer), (size_t)bytes); |
| 3394 |
if(read_cb(buffer, 1, n, handle) != n) { |
| 3395 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3396 |
return false; |
| 3397 |
} |
| 3398 |
if(temp_write_cb(buffer, 1, n, temp_handle) != n) { |
| 3399 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3400 |
return false; |
| 3401 |
} |
| 3402 |
bytes -= n; |
| 3403 |
} |
| 3404 |
|
| 3405 |
return true; |
| 3406 |
} |
| 3407 |
|
| 3408 |
FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3409 |
{ |
| 3410 |
FLAC__byte buffer[8192]; |
| 3411 |
size_t n; |
| 3412 |
|
| 3413 |
while(!feof(file)) { |
| 3414 |
n = fread(buffer, 1, sizeof(buffer), file); |
| 3415 |
if(n == 0 && !feof(file)) { |
| 3416 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3417 |
return false; |
| 3418 |
} |
| 3419 |
if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) { |
| 3420 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3421 |
return false; |
| 3422 |
} |
| 3423 |
} |
| 3424 |
|
| 3425 |
return true; |
| 3426 |
} |
| 3427 |
|
| 3428 |
FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3429 |
{ |
| 3430 |
FLAC__byte buffer[8192]; |
| 3431 |
size_t n; |
| 3432 |
|
| 3433 |
while(!eof_cb(handle)) { |
| 3434 |
n = read_cb(buffer, 1, sizeof(buffer), handle); |
| 3435 |
if(n == 0 && !eof_cb(handle)) { |
| 3436 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR; |
| 3437 |
return false; |
| 3438 |
} |
| 3439 |
if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) { |
| 3440 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR; |
| 3441 |
return false; |
| 3442 |
} |
| 3443 |
} |
| 3444 |
|
| 3445 |
return true; |
| 3446 |
} |
| 3447 |
|
| 3448 |
static int |
| 3449 |
local_snprintf(char *str, size_t size, const char *fmt, ...) |
| 3450 |
{ |
| 3451 |
va_list va; |
| 3452 |
int rc; |
| 3453 |
|
| 3454 |
#if defined _MSC_VER |
| 3455 |
if (size == 0) |
| 3456 |
return 1024; |
| 3457 |
#endif |
| 3458 |
|
| 3459 |
va_start (va, fmt); |
| 3460 |
|
| 3461 |
#if defined _MSC_VER |
| 3462 |
rc = vsnprintf_s (str, size, _TRUNCATE, fmt, va); |
| 3463 |
if (rc < 0) |
| 3464 |
rc = size - 1; |
| 3465 |
#elif defined __MINGW32__ |
| 3466 |
rc = __mingw_vsnprintf (str, size, fmt, va); |
| 3467 |
#else |
| 3468 |
rc = vsnprintf (str, size, fmt, va); |
| 3469 |
#endif |
| 3470 |
va_end (va); |
| 3471 |
|
| 3472 |
return rc; |
| 3473 |
} |
| 3474 |
|
| 3475 |
FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3476 |
{ |
| 3477 |
static const char *tempfile_suffix = ".metadata_edit"; |
| 3478 |
if(0 == tempfile_path_prefix) { |
| 3479 |
size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1; |
| 3480 |
if(0 == (*tempfilename = safe_malloc_(dest_len))) { |
| 3481 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3482 |
return false; |
| 3483 |
} |
| 3484 |
local_snprintf(*tempfilename, dest_len, "%s%s", filename, tempfile_suffix); |
| 3485 |
} |
| 3486 |
else { |
| 3487 |
const char *p = strrchr(filename, '/'); |
| 3488 |
size_t dest_len; |
| 3489 |
if(0 == p) |
| 3490 |
p = filename; |
| 3491 |
else |
| 3492 |
p++; |
| 3493 |
|
| 3494 |
dest_len = strlen(tempfile_path_prefix) + strlen(p) + strlen(tempfile_suffix) + 2; |
| 3495 |
|
| 3496 |
if(0 == (*tempfilename = safe_malloc_(dest_len))) { |
| 3497 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3498 |
return false; |
| 3499 |
} |
| 3500 |
local_snprintf(*tempfilename, dest_len, "%s/%s%s", tempfile_path_prefix, p, tempfile_suffix); |
| 3501 |
} |
| 3502 |
|
| 3503 |
if(0 == (*tempfile = flac_fopen(*tempfilename, "w+b"))) { |
| 3504 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE; |
| 3505 |
return false; |
| 3506 |
} |
| 3507 |
|
| 3508 |
return true; |
| 3509 |
} |
| 3510 |
|
| 3511 |
FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status) |
| 3512 |
{ |
| 3513 |
FLAC__ASSERT(0 != filename); |
| 3514 |
FLAC__ASSERT(0 != tempfile); |
| 3515 |
FLAC__ASSERT(0 != *tempfile); |
| 3516 |
FLAC__ASSERT(0 != tempfilename); |
| 3517 |
FLAC__ASSERT(0 != *tempfilename); |
| 3518 |
FLAC__ASSERT(0 != status); |
| 3519 |
|
| 3520 |
(void)fclose(*tempfile); |
| 3521 |
*tempfile = 0; |
| 3522 |
|
| 3523 |
#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__ |
| 3524 |
/* on some flavors of windows, flac_rename() will fail if the destination already exists */ |
| 3525 |
if(flac_unlink(filename) < 0) { |
| 3526 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3527 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR; |
| 3528 |
return false; |
| 3529 |
} |
| 3530 |
#endif |
| 3531 |
|
| 3532 |
#ifndef _WIN32 |
| 3533 |
/* Need to check whether filename refers to a symlink or not */ |
| 3534 |
{ |
| 3535 |
struct stat filestat; |
| 3536 |
if(lstat(filename, &filestat) != 0) { |
| 3537 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3538 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR; |
| 3539 |
return false; |
| 3540 |
} |
| 3541 |
if(S_ISLNK(filestat.st_mode)) { |
| 3542 |
/* file is symlink, find out name of symlinked file */ |
| 3543 |
char linked_name[1024]; |
| 3544 |
ssize_t len = readlink(filename, linked_name, sizeof(linked_name) - 1); |
| 3545 |
if (len == sizeof(linked_name) - 1) { |
| 3546 |
/* name too long */ |
| 3547 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3548 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR; |
| 3549 |
return false; |
| 3550 |
} |
| 3551 |
linked_name[len] = '\0'; |
| 3552 |
|
| 3553 |
if(0 != flac_rename(*tempfilename, linked_name)) { |
| 3554 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3555 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR; |
| 3556 |
return false; |
| 3557 |
} |
| 3558 |
return true; |
| 3559 |
} |
| 3560 |
/* else, continue with normale code */ |
| 3561 |
} |
| 3562 |
|
| 3563 |
#endif |
| 3564 |
|
| 3565 |
|
| 3566 |
|
| 3567 |
/*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just flac_rename(): */ |
| 3568 |
if(0 != flac_rename(*tempfilename, filename)) { |
| 3569 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3570 |
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR; |
| 3571 |
return false; |
| 3572 |
} |
| 3573 |
|
| 3574 |
cleanup_tempfile_(tempfile, tempfilename); |
| 3575 |
|
| 3576 |
return true; |
| 3577 |
} |
| 3578 |
|
| 3579 |
void cleanup_tempfile_(FILE **tempfile, char **tempfilename) |
| 3580 |
{ |
| 3581 |
if(0 != *tempfile) { |
| 3582 |
(void)fclose(*tempfile); |
| 3583 |
*tempfile = 0; |
| 3584 |
} |
| 3585 |
|
| 3586 |
if(0 != *tempfilename) { |
| 3587 |
(void)flac_unlink(*tempfilename); |
| 3588 |
free(*tempfilename); |
| 3589 |
*tempfilename = 0; |
| 3590 |
} |
| 3591 |
} |
| 3592 |
|
| 3593 |
FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats) |
| 3594 |
{ |
| 3595 |
FLAC__ASSERT(0 != filename); |
| 3596 |
FLAC__ASSERT(0 != stats); |
| 3597 |
return (0 == flac_stat(filename, stats)); |
| 3598 |
} |
| 3599 |
|
| 3600 |
void set_file_stats_(const char *filename, struct flac_stat_s *stats) |
| 3601 |
{ |
| 3602 |
#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) && !defined(_WIN32) |
| 3603 |
struct timespec srctime[2] = {}; |
| 3604 |
srctime[0].tv_sec = stats->st_atime; |
| 3605 |
srctime[1].tv_sec = stats->st_mtime; |
| 3606 |
#else |
| 3607 |
struct utimbuf srctime; |
| 3608 |
srctime.actime = stats->st_atime; |
| 3609 |
srctime.modtime = stats->st_mtime; |
| 3610 |
#endif |
| 3611 |
|
| 3612 |
FLAC__ASSERT(0 != filename); |
| 3613 |
FLAC__ASSERT(0 != stats); |
| 3614 |
|
| 3615 |
(void)flac_chmod(filename, stats->st_mode); |
| 3616 |
(void)flac_utime(filename, &srctime); |
| 3617 |
#if !defined _MSC_VER && !defined __BORLANDC__ && !defined __MINGW32__ |
| 3618 |
FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1)); |
| 3619 |
FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid)); |
| 3620 |
#endif |
| 3621 |
} |
| 3622 |
|
| 3623 |
int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence) |
| 3624 |
{ |
| 3625 |
return fseeko((FILE*)handle, (FLAC__off_t)offset, whence); |
| 3626 |
} |
| 3627 |
|
| 3628 |
FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle) |
| 3629 |
{ |
| 3630 |
return ftello((FILE*)handle); |
| 3631 |
} |
| 3632 |
|
| 3633 |
FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status) |
| 3634 |
{ |
| 3635 |
switch(status) { |
| 3636 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK: |
| 3637 |
return FLAC__METADATA_CHAIN_STATUS_OK; |
| 3638 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT: |
| 3639 |
return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT; |
| 3640 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE: |
| 3641 |
return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE; |
| 3642 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE: |
| 3643 |
return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE; |
| 3644 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE: |
| 3645 |
return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE; |
| 3646 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA: |
| 3647 |
return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA; |
| 3648 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR: |
| 3649 |
return FLAC__METADATA_CHAIN_STATUS_READ_ERROR; |
| 3650 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR: |
| 3651 |
return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR; |
| 3652 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR: |
| 3653 |
return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR; |
| 3654 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR: |
| 3655 |
return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR; |
| 3656 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR: |
| 3657 |
return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR; |
| 3658 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR: |
| 3659 |
return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR; |
| 3660 |
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR: |
| 3661 |
default: |
| 3662 |
return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; |
| 3663 |
} |
| 3664 |
} |