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