Coverage Report

Created: 2023-11-19 06:20

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