Coverage Report

Created: 2024-07-23 06:08

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