/
metadata_manip.c
2136 lines (1730 loc) · 82.5 KB
/
metadata_manip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* test_libFLAC - Unit tester for libFLAC
* Copyright (C) 2002-2009 Josh Coalson
* Copyright (C) 2011-2016 Xiph.Org Foundation
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy()/memset() */
#if defined _MSC_VER || defined __MINGW32__
#include <sys/utime.h> /* for utime() */
#include <io.h> /* for chmod() */
#else
#include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
#include <utime.h> /* for utime() */
#include <unistd.h> /* for chown(), unlink() */
#endif
#include <sys/stat.h> /* for stat(), maybe chmod() */
#include "FLAC/assert.h"
#include "FLAC/stream_decoder.h"
#include "FLAC/metadata.h"
#include "share/grabbag.h"
#include "share/compat.h"
#include "share/macros.h"
#include "share/safe_str.h"
#include "test_libs_common/file_utils_flac.h"
#include "test_libs_common/metadata_utils.h"
#include "metadata.h"
/******************************************************************************
The general strategy of these tests (for interface levels 1 and 2) is
to create a dummy FLAC file with a known set of initial metadata
blocks, then keep a mirror locally of what we expect the metadata to be
after each operation. Then testing becomes a simple matter of running
a FLAC__StreamDecoder over the dummy file after each operation, comparing
the decoded metadata to what's in our local copy. If there are any
differences in the metadata, or the actual audio data is corrupted, we
will catch it while decoding.
******************************************************************************/
typedef struct {
FLAC__bool error_occurred;
} decoder_client_struct;
typedef struct {
FLAC__StreamMetadata *blocks[64];
uint32_t num_blocks;
} our_metadata_struct;
/* our copy of the metadata in flacfilename() */
static our_metadata_struct our_metadata_;
/* the current block number that corresponds to the position of the iterator we are testing */
static uint32_t mc_our_block_number_ = 0;
static const char *flacfilename(FLAC__bool is_ogg)
{
return is_ogg? "metadata.oga" : "metadata.flac";
}
static FLAC__bool die_(const char *msg)
{
printf("ERROR: %s\n", msg);
return false;
}
static FLAC__bool die_c_(const char *msg, FLAC__Metadata_ChainStatus status)
{
printf("ERROR: %s\n", msg);
printf(" status=%s\n", FLAC__Metadata_ChainStatusString[status]);
return false;
}
static FLAC__bool die_ss_(const char *msg, FLAC__Metadata_SimpleIterator *iterator)
{
printf("ERROR: %s\n", msg);
printf(" status=%s\n", FLAC__Metadata_SimpleIteratorStatusString[FLAC__metadata_simple_iterator_status(iterator)]);
return false;
}
static void *malloc_or_die_(size_t size)
{
void *x = malloc(size);
if(0 == x) {
fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (uint32_t)size);
exit(1);
}
return x;
}
static char *strdup_or_die_(const char *s)
{
char *x = strdup(s);
if(0 == x) {
fprintf(stderr, "ERROR: out of memory copying string \"%s\"\n", s);
exit(1);
}
return x;
}
/* functions for working with our metadata copy */
static FLAC__bool replace_in_our_metadata_(FLAC__StreamMetadata *block, uint32_t position, FLAC__bool copy)
{
uint32_t i;
FLAC__StreamMetadata *obj = block;
FLAC__ASSERT(position < our_metadata_.num_blocks);
if(copy) {
if(0 == (obj = FLAC__metadata_object_clone(block)))
return die_("during FLAC__metadata_object_clone()");
}
FLAC__metadata_object_delete(our_metadata_.blocks[position]);
our_metadata_.blocks[position] = obj;
/* set the is_last flags */
for(i = 0; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i]->is_last = false;
our_metadata_.blocks[i]->is_last = true;
return true;
}
static FLAC__bool insert_to_our_metadata_(FLAC__StreamMetadata *block, uint32_t position, FLAC__bool copy)
{
uint32_t i;
FLAC__StreamMetadata *obj = block;
if(copy) {
if(0 == (obj = FLAC__metadata_object_clone(block)))
return die_("during FLAC__metadata_object_clone()");
}
if(position > our_metadata_.num_blocks) {
position = our_metadata_.num_blocks;
}
else {
for(i = our_metadata_.num_blocks; i > position; i--)
our_metadata_.blocks[i] = our_metadata_.blocks[i-1];
}
our_metadata_.blocks[position] = obj;
our_metadata_.num_blocks++;
/* set the is_last flags */
for(i = 0; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i]->is_last = false;
our_metadata_.blocks[i]->is_last = true;
return true;
}
static void delete_from_our_metadata_(uint32_t position)
{
uint32_t i;
FLAC__ASSERT(position < our_metadata_.num_blocks);
FLAC__metadata_object_delete(our_metadata_.blocks[position]);
for(i = position; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i] = our_metadata_.blocks[i+1];
our_metadata_.num_blocks--;
/* set the is_last flags */
if(our_metadata_.num_blocks > 0) {
for(i = 0; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i]->is_last = false;
our_metadata_.blocks[i]->is_last = true;
}
}
/*
* This wad of functions supports filename- and callback-based chain reading/writing.
* Everything up to set_file_stats_() is copied from libFLAC/metadata_iterators.c
*/
static FLAC__bool open_tempfile_(const char *filename, FILE **tempfile, char **tempfilename)
{
static const char *tempfile_suffix = ".metadata_edit";
size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1;
*tempfilename = malloc(dest_len);
if (*tempfilename == NULL)
return false;
safe_strncpy(*tempfilename, filename, dest_len);
safe_strncat(*tempfilename, tempfile_suffix, dest_len);
*tempfile = flac_fopen(*tempfilename, "wb");
if (*tempfile == NULL)
return false;
return true;
}
static void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
{
if (*tempfile != NULL) {
(void)fclose(*tempfile);
*tempfile = 0;
}
if (*tempfilename != NULL) {
(void)flac_unlink(*tempfilename);
free(*tempfilename);
*tempfilename = 0;
}
}
static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != tempfile);
FLAC__ASSERT(0 != tempfilename);
FLAC__ASSERT(0 != *tempfilename);
if(0 != *tempfile) {
(void)fclose(*tempfile);
*tempfile = 0;
}
#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
/* on some flavors of windows, flac_rename() will fail if the destination already exists */
if(flac_unlink(filename) < 0) {
cleanup_tempfile_(tempfile, tempfilename);
return false;
}
#endif
if(0 != flac_rename(*tempfilename, filename)) {
cleanup_tempfile_(tempfile, tempfilename);
return false;
}
cleanup_tempfile_(tempfile, tempfilename);
return true;
}
static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != stats);
return (0 == flac_stat(filename, stats));
}
static void set_file_stats_(const char *filename, struct flac_stat_s *stats)
{
struct utimbuf srctime;
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != stats);
srctime.actime = stats->st_atime;
srctime.modtime = stats->st_mtime;
(void)flac_chmod(filename, stats->st_mode);
(void)flac_utime(filename, &srctime);
#if !defined _MSC_VER && !defined __MINGW32__
FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1));
FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid));
#endif
}
#ifdef FLAC__VALGRIND_TESTING
static size_t chain_write_cb_(const void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle)
{
FILE *stream = (FILE*)handle;
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#endif
static int chain_seek_cb_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
{
FLAC__off_t o = (FLAC__off_t)offset;
FLAC__ASSERT(offset == o);
return fseeko((FILE*)handle, o, whence);
}
static FLAC__int64 chain_tell_cb_(FLAC__IOHandle handle)
{
return ftello((FILE*)handle);
}
static int chain_eof_cb_(FLAC__IOHandle handle)
{
return feof((FILE*)handle);
}
static FLAC__bool write_chain_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats, FLAC__bool filename_based, const char *filename)
{
if(filename_based)
return FLAC__metadata_chain_write(chain, use_padding, preserve_file_stats);
else {
FLAC__IOCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.read = (FLAC__IOCallback_Read)fread;
#ifdef FLAC__VALGRIND_TESTING
callbacks.write = chain_write_cb_;
#else
callbacks.write = (FLAC__IOCallback_Write)fwrite;
#endif
callbacks.seek = chain_seek_cb_;
callbacks.eof = chain_eof_cb_;
if(FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
struct flac_stat_s stats;
FILE *file, *tempfile = 0;
char *tempfilename;
if(preserve_file_stats) {
if(!get_file_stats_(filename, &stats))
return false;
}
if(0 == (file = flac_fopen(filename, "rb")))
return false; /*@@@@ chain status still says OK though */
if(!open_tempfile_(filename, &tempfile, &tempfilename)) {
fclose(file);
cleanup_tempfile_(&tempfile, &tempfilename);
return false; /*@@@@ chain status still says OK though */
}
if(!FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain, use_padding, (FLAC__IOHandle)file, callbacks, (FLAC__IOHandle)tempfile, callbacks)) {
fclose(file);
fclose(tempfile);
return false;
}
fclose(file);
fclose(tempfile);
file = tempfile = 0;
if(!transport_tempfile_(filename, &tempfile, &tempfilename))
return false;
if(preserve_file_stats)
set_file_stats_(filename, &stats);
}
else {
FILE *file = flac_fopen(filename, "r+b");
if(0 == file)
return false; /*@@@@ chain status still says OK though */
if(!FLAC__metadata_chain_write_with_callbacks(chain, use_padding, (FLAC__IOHandle)file, callbacks))
return false;
fclose(file);
}
}
return true;
}
static FLAC__bool read_chain_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool filename_based, FLAC__bool is_ogg)
{
if(filename_based)
return is_ogg?
FLAC__metadata_chain_read_ogg(chain, flacfilename(is_ogg)) :
FLAC__metadata_chain_read(chain, flacfilename(is_ogg))
;
else {
FLAC__IOCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.read = (FLAC__IOCallback_Read)fread;
callbacks.seek = chain_seek_cb_;
callbacks.tell = chain_tell_cb_;
{
FLAC__bool ret;
FILE *file = flac_fopen(filename, "rb");
if(0 == file)
return false; /*@@@@ chain status still says OK though */
ret = is_ogg?
FLAC__metadata_chain_read_ogg_with_callbacks(chain, (FLAC__IOHandle)file, callbacks) :
FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)
;
fclose(file);
return ret;
}
}
}
/* function for comparing our metadata to a FLAC__Metadata_Chain */
static FLAC__bool compare_chain_(FLAC__Metadata_Chain *chain, uint32_t current_position, FLAC__StreamMetadata *current_block)
{
uint32_t i;
FLAC__Metadata_Iterator *iterator;
FLAC__StreamMetadata *block;
FLAC__bool next_ok = true;
FLAC__ASSERT(0 != chain);
printf("\tcomparing chain... ");
fflush(stdout);
if(0 == (iterator = FLAC__metadata_iterator_new()))
return die_("allocating memory for iterator");
FLAC__metadata_iterator_init(iterator, chain);
i = 0;
do {
printf("%u... ", i);
fflush(stdout);
if(0 == (block = FLAC__metadata_iterator_get_block(iterator))) {
FLAC__metadata_iterator_delete(iterator);
return die_("getting block from iterator");
}
if(!mutils__compare_block(our_metadata_.blocks[i], block)) {
FLAC__metadata_iterator_delete(iterator);
return die_("metadata block mismatch");
}
i++;
next_ok = FLAC__metadata_iterator_next(iterator);
} while(i < our_metadata_.num_blocks && next_ok);
FLAC__metadata_iterator_delete(iterator);
if(next_ok)
return die_("chain has more blocks than expected");
if(i < our_metadata_.num_blocks)
return die_("short block count in chain");
if(0 != current_block) {
printf("CURRENT_POSITION... ");
fflush(stdout);
if(!mutils__compare_block(our_metadata_.blocks[current_position], current_block))
return die_("metadata block mismatch");
}
printf("PASSED\n");
return true;
}
/* decoder callbacks for checking the file */
static FLAC__StreamDecoderWriteStatus decoder_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
(void)decoder, (void)buffer, (void)client_data;
if(
(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) ||
(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0)
) {
printf("content... ");
fflush(stdout);
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
/* this version pays no attention to the metadata */
static void decoder_metadata_callback_null_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
(void)decoder, (void)metadata, (void)client_data;
printf("%d... ", mc_our_block_number_);
fflush(stdout);
mc_our_block_number_++;
}
/* this version is used when we want to compare to our metadata copy */
static void decoder_metadata_callback_compare_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
decoder_client_struct *dcd = (decoder_client_struct*)client_data;
(void)decoder;
/* don't bother checking if we've already hit an error */
if(dcd->error_occurred)
return;
printf("%d... ", mc_our_block_number_);
fflush(stdout);
if(mc_our_block_number_ >= our_metadata_.num_blocks) {
(void)die_("got more metadata blocks than expected");
dcd->error_occurred = true;
}
else {
if(!mutils__compare_block(our_metadata_.blocks[mc_our_block_number_], metadata)) {
(void)die_("metadata block mismatch");
dcd->error_occurred = true;
}
}
mc_our_block_number_++;
}
static void decoder_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
decoder_client_struct *dcd = (decoder_client_struct*)client_data;
(void)decoder;
dcd->error_occurred = true;
printf("ERROR: got error callback, status = %s (%u)\n", FLAC__StreamDecoderErrorStatusString[status], (uint32_t)status);
}
static FLAC__bool generate_file_(FLAC__bool include_extras, FLAC__bool is_ogg)
{
FLAC__StreamMetadata streaminfo, vorbiscomment, *cuesheet, picture, padding;
FLAC__StreamMetadata *metadata[4];
uint32_t i = 0, n = 0;
printf("generating %sFLAC file for test\n", is_ogg? "Ogg " : "");
while(our_metadata_.num_blocks > 0)
delete_from_our_metadata_(0);
streaminfo.is_last = false;
streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
streaminfo.data.stream_info.min_blocksize = 576;
streaminfo.data.stream_info.max_blocksize = 576;
streaminfo.data.stream_info.min_framesize = 0;
streaminfo.data.stream_info.max_framesize = 0;
streaminfo.data.stream_info.sample_rate = 44100;
streaminfo.data.stream_info.channels = 1;
streaminfo.data.stream_info.bits_per_sample = 8;
streaminfo.data.stream_info.total_samples = 0;
memset(streaminfo.data.stream_info.md5sum, 0, 16);
{
const uint32_t vendor_string_length = (uint32_t)strlen(FLAC__VENDOR_STRING);
vorbiscomment.is_last = false;
vorbiscomment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
vorbiscomment.length = (4 + vendor_string_length) + 4;
vorbiscomment.data.vorbis_comment.vendor_string.length = vendor_string_length;
vorbiscomment.data.vorbis_comment.vendor_string.entry = malloc_or_die_(vendor_string_length+1);
memcpy(vorbiscomment.data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length+1);
vorbiscomment.data.vorbis_comment.num_comments = 0;
vorbiscomment.data.vorbis_comment.comments = 0;
}
{
if (0 == (cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET)))
return die_("priming our metadata");
cuesheet->is_last = false;
safe_strncpy(cuesheet->data.cue_sheet.media_catalog_number, "bogo-MCN", sizeof(cuesheet->data.cue_sheet.media_catalog_number));
cuesheet->data.cue_sheet.lead_in = 123;
cuesheet->data.cue_sheet.is_cd = false;
if (!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, 0))
return die_("priming our metadata");
cuesheet->data.cue_sheet.tracks[0].number = 1;
if (!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, 0, 0))
return die_("priming our metadata");
}
{
picture.is_last = false;
picture.type = FLAC__METADATA_TYPE_PICTURE;
picture.length =
(
FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* will add the length for the string later */
FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* will add the length for the string later */
FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN /* will add the length for the data later */
) / 8
;
picture.data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER;
picture.data.picture.mime_type = strdup_or_die_("image/jpeg");
picture.length += strlen(picture.data.picture.mime_type);
picture.data.picture.description = (FLAC__byte*)strdup_or_die_("desc");
picture.length += strlen((const char *)picture.data.picture.description);
picture.data.picture.width = 300;
picture.data.picture.height = 300;
picture.data.picture.depth = 24;
picture.data.picture.colors = 0;
picture.data.picture.data = (FLAC__byte*)strdup_or_die_("SOMEJPEGDATA");
picture.data.picture.data_length = strlen((const char *)picture.data.picture.data);
picture.length += picture.data.picture.data_length;
}
padding.is_last = true;
padding.type = FLAC__METADATA_TYPE_PADDING;
padding.length = 1234;
metadata[n++] = &vorbiscomment;
if(include_extras) {
metadata[n++] = cuesheet;
metadata[n++] = &picture;
}
metadata[n++] = &padding;
if(
!insert_to_our_metadata_(&streaminfo, i++, /*copy=*/true) ||
!insert_to_our_metadata_(&vorbiscomment, i++, /*copy=*/true) ||
(include_extras && !insert_to_our_metadata_(cuesheet, i++, /*copy=*/false)) ||
(include_extras && !insert_to_our_metadata_(&picture, i++, /*copy=*/true)) ||
!insert_to_our_metadata_(&padding, i++, /*copy=*/true)
)
return die_("priming our metadata");
if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), 0, 512 * 1024, &streaminfo, metadata, n))
return die_("creating the encoded file");
free(vorbiscomment.data.vorbis_comment.vendor_string.entry);
free(picture.data.picture.mime_type);
free(picture.data.picture.description);
free(picture.data.picture.data);
if(!include_extras)
FLAC__metadata_object_delete(cuesheet);
return true;
}
static FLAC__bool test_file_(FLAC__bool is_ogg, FLAC__StreamDecoderMetadataCallback metadata_callback)
{
const char *filename = flacfilename(is_ogg);
FLAC__StreamDecoder *decoder;
decoder_client_struct decoder_client_data;
FLAC__ASSERT(0 != metadata_callback);
mc_our_block_number_ = 0;
decoder_client_data.error_occurred = false;
printf("\ttesting '%s'... ", filename);
fflush(stdout);
if(0 == (decoder = FLAC__stream_decoder_new()))
return die_("couldn't allocate decoder instance");
FLAC__stream_decoder_set_md5_checking(decoder, true);
FLAC__stream_decoder_set_metadata_respond_all(decoder);
if(
(is_ogg?
FLAC__stream_decoder_init_ogg_file(decoder, filename, decoder_write_callback_, metadata_callback, decoder_error_callback_, &decoder_client_data) :
FLAC__stream_decoder_init_file(decoder, filename, decoder_write_callback_, metadata_callback, decoder_error_callback_, &decoder_client_data)
) != FLAC__STREAM_DECODER_INIT_STATUS_OK
) {
(void)FLAC__stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder);
return die_("initializing decoder\n");
}
if(!FLAC__stream_decoder_process_until_end_of_stream(decoder)) {
(void)FLAC__stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder);
return die_("decoding file\n");
}
(void)FLAC__stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder);
if(decoder_client_data.error_occurred)
return false;
if(mc_our_block_number_ != our_metadata_.num_blocks)
return die_("short metadata block count");
printf("PASSED\n");
return true;
}
static FLAC__bool change_stats_(const char *filename, FLAC__bool read_only)
{
if(!grabbag__file_change_stats(filename, read_only))
return die_("during grabbag__file_change_stats()");
return true;
}
static FLAC__bool remove_file_(const char *filename)
{
while(our_metadata_.num_blocks > 0)
delete_from_our_metadata_(0);
if(!grabbag__file_remove_file(filename))
return die_("removing file");
return true;
}
static FLAC__bool test_level_0_(void)
{
FLAC__StreamMetadata streaminfo;
FLAC__StreamMetadata *tags = 0;
FLAC__StreamMetadata *cuesheet = 0;
FLAC__StreamMetadata *picture = 0;
printf("\n\n++++++ testing level 0 interface\n");
if(!generate_file_(/*include_extras=*/true, /*is_ogg=*/false))
return false;
if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_null_))
return false;
printf("testing FLAC__metadata_get_streaminfo()... ");
if(!FLAC__metadata_get_streaminfo(flacfilename(/*is_ogg=*/false), &streaminfo))
return die_("during FLAC__metadata_get_streaminfo()");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(streaminfo.data.stream_info.channels != 1)
return die_("mismatch in streaminfo.data.stream_info.channels");
if(streaminfo.data.stream_info.bits_per_sample != 8)
return die_("mismatch in streaminfo.data.stream_info.bits_per_sample");
if(streaminfo.data.stream_info.sample_rate != 44100)
return die_("mismatch in streaminfo.data.stream_info.sample_rate");
if(streaminfo.data.stream_info.min_blocksize != 576)
return die_("mismatch in streaminfo.data.stream_info.min_blocksize");
if(streaminfo.data.stream_info.max_blocksize != 576)
return die_("mismatch in streaminfo.data.stream_info.max_blocksize");
printf("OK\n");
printf("testing FLAC__metadata_get_tags()... ");
if(!FLAC__metadata_get_tags(flacfilename(/*is_ogg=*/false), &tags))
return die_("during FLAC__metadata_get_tags()");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(tags->data.vorbis_comment.num_comments != 0)
return die_("mismatch in tags->data.vorbis_comment.num_comments");
printf("OK\n");
FLAC__metadata_object_delete(tags);
printf("testing FLAC__metadata_get_cuesheet()... ");
if(!FLAC__metadata_get_cuesheet(flacfilename(/*is_ogg=*/false), &cuesheet))
return die_("during FLAC__metadata_get_cuesheet()");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(cuesheet->data.cue_sheet.lead_in != 123)
return die_("mismatch in cuesheet->data.cue_sheet.lead_in");
printf("OK\n");
FLAC__metadata_object_delete(cuesheet);
printf("testing FLAC__metadata_get_picture()... ");
if(!FLAC__metadata_get_picture(flacfilename(/*is_ogg=*/false), &picture, /*type=*/(FLAC__StreamMetadata_Picture_Type)(-1), /*mime_type=*/0, /*description=*/0, /*max_width=*/(uint32_t)(-1), /*max_height=*/(uint32_t)(-1), /*max_depth=*/(uint32_t)(-1), /*max_colors=*/(uint32_t)(-1)))
return die_("during FLAC__metadata_get_picture()");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(picture->data.picture.type != FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER)
return die_("mismatch in picture->data.picture.type");
printf("OK\n");
FLAC__metadata_object_delete(picture);
if(!remove_file_(flacfilename(/*is_ogg=*/false)))
return false;
return true;
}
static FLAC__bool test_level_1_(void)
{
FLAC__Metadata_SimpleIterator *iterator;
FLAC__StreamMetadata *block, *app, *padding;
FLAC__byte data[1000];
uint32_t our_current_position = 0;
/* initialize 'data' to avoid Valgrind errors */
memset(data, 0, sizeof(data));
printf("\n\n++++++ testing level 1 interface\n");
/************************************************************/
printf("simple iterator on read-only file\n");
if(!generate_file_(/*include_extras=*/false, /*is_ogg=*/false))
return false;
if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read_only=*/true))
return false;
if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_null_))
return false;
if(0 == (iterator = FLAC__metadata_simple_iterator_new()))
return die_("FLAC__metadata_simple_iterator_new()");
if(!FLAC__metadata_simple_iterator_init(iterator, flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false))
return die_("FLAC__metadata_simple_iterator_init() returned false");
printf("is writable = %u\n", (uint32_t)FLAC__metadata_simple_iterator_is_writable(iterator));
if(FLAC__metadata_simple_iterator_is_writable(iterator))
return die_("iterator claims file is writable when tester thinks it should not be; are you running as root?\n");
printf("iterate forwards\n");
if(FLAC__metadata_simple_iterator_get_block_type(iterator) != FLAC__METADATA_TYPE_STREAMINFO)
return die_("expected STREAMINFO type from FLAC__metadata_simple_iterator_get_block_type()");
if(0 == (block = FLAC__metadata_simple_iterator_get_block(iterator)))
return die_("getting block 0");
if(block->type != FLAC__METADATA_TYPE_STREAMINFO)
return die_("expected STREAMINFO type");
if(block->is_last)
return die_("expected is_last to be false");
if(block->length != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
return die_("bad STREAMINFO length");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(block->data.stream_info.channels != 1)
return die_("mismatch in channels");
if(block->data.stream_info.bits_per_sample != 8)
return die_("mismatch in bits_per_sample");
if(block->data.stream_info.sample_rate != 44100)
return die_("mismatch in sample_rate");
if(block->data.stream_info.min_blocksize != 576)
return die_("mismatch in min_blocksize");
if(block->data.stream_info.max_blocksize != 576)
return die_("mismatch in max_blocksize");
FLAC__metadata_object_delete(block);
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("forward iterator ended early");
our_current_position++;
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("forward iterator ended early");
our_current_position++;
if(FLAC__metadata_simple_iterator_get_block_type(iterator) != FLAC__METADATA_TYPE_PADDING)
return die_("expected PADDING type from FLAC__metadata_simple_iterator_get_block_type()");
if(0 == (block = FLAC__metadata_simple_iterator_get_block(iterator)))
return die_("getting block 2");
if(block->type != FLAC__METADATA_TYPE_PADDING)
return die_("expected PADDING type");
if(!block->is_last)
return die_("expected is_last to be true");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(block->length != 1234)
return die_("bad PADDING length");
FLAC__metadata_object_delete(block);
if(FLAC__metadata_simple_iterator_next(iterator))
return die_("forward iterator returned true but should have returned false");
printf("iterate backwards\n");
if(!FLAC__metadata_simple_iterator_prev(iterator))
return die_("reverse iterator ended early");
if(!FLAC__metadata_simple_iterator_prev(iterator))
return die_("reverse iterator ended early");
if(FLAC__metadata_simple_iterator_prev(iterator))
return die_("reverse iterator returned true but should have returned false");
printf("testing FLAC__metadata_simple_iterator_set_block() on read-only file...\n");
if(!FLAC__metadata_simple_iterator_set_block(iterator, (FLAC__StreamMetadata*)99, false))
printf("OK: FLAC__metadata_simple_iterator_set_block() returned false like it should\n");
else
return die_("FLAC__metadata_simple_iterator_set_block() returned true but shouldn't have");
FLAC__metadata_simple_iterator_delete(iterator);
/************************************************************/
printf("simple iterator on writable file\n");
if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read-only=*/false))
return false;
printf("creating APPLICATION block\n");
if(0 == (app = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)))
return die_("FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)");
memcpy(app->data.application.id, "duh", (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
printf("creating PADDING block\n");
if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
return die_("FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)");
padding->length = 20;
if(0 == (iterator = FLAC__metadata_simple_iterator_new()))
return die_("FLAC__metadata_simple_iterator_new()");
if(!FLAC__metadata_simple_iterator_init(iterator, flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false))
return die_("FLAC__metadata_simple_iterator_init() returned false");
our_current_position = 0;
printf("is writable = %u\n", (uint32_t)FLAC__metadata_simple_iterator_is_writable(iterator));
printf("[S]VP\ttry to write over STREAMINFO block...\n");
if(!FLAC__metadata_simple_iterator_set_block(iterator, app, false))
printf("\tFLAC__metadata_simple_iterator_set_block() returned false like it should\n");
else
return die_("FLAC__metadata_simple_iterator_set_block() returned true but shouldn't have");
printf("[S]VP\tnext\n");
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("iterator ended early\n");
our_current_position++;
printf("S[V]P\tnext\n");
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("iterator ended early\n");
our_current_position++;
printf("SV[P]\tinsert PADDING after, don't expand into padding\n");
padding->length = 25;
if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false))
return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)", iterator);
if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true))
return false;
printf("SVP[P]\tprev\n");
if(!FLAC__metadata_simple_iterator_prev(iterator))
return die_("iterator ended early\n");
our_current_position--;
printf("SV[P]P\tprev\n");
if(!FLAC__metadata_simple_iterator_prev(iterator))
return die_("iterator ended early\n");
our_current_position--;
printf("S[V]PP\tinsert PADDING after, don't expand into padding\n");
padding->length = 30;
if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false))
return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)", iterator);
if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true))
return false;
if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_))
return false;
printf("SV[P]PP\tprev\n");
if(!FLAC__metadata_simple_iterator_prev(iterator))
return die_("iterator ended early\n");
our_current_position--;
printf("S[V]PPP\tprev\n");
if(!FLAC__metadata_simple_iterator_prev(iterator))
return die_("iterator ended early\n");
our_current_position--;
printf("[S]VPPP\tdelete (STREAMINFO block), must fail\n");
if(FLAC__metadata_simple_iterator_delete_block(iterator, false))
return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false) should have returned false", iterator);
if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_))
return false;
printf("[S]VPPP\tnext\n");
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("iterator ended early\n");
our_current_position++;
printf("S[V]PPP\tnext\n");
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("iterator ended early\n");
our_current_position++;
printf("SV[P]PP\tdelete (middle block), replace with padding\n");
if(!FLAC__metadata_simple_iterator_delete_block(iterator, true))
return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, true)", iterator);
our_current_position--;
printf("S[V]PPP\tnext\n");
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("iterator ended early\n");
our_current_position++;
printf("SV[P]PP\tdelete (middle block), don't replace with padding\n");
if(!FLAC__metadata_simple_iterator_delete_block(iterator, false))
return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator);
delete_from_our_metadata_(our_current_position--);
if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_))
return false;
printf("S[V]PP\tnext\n");
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("iterator ended early\n");
our_current_position++;
printf("SV[P]P\tnext\n");
if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("iterator ended early\n");
our_current_position++;
printf("SVP[P]\tdelete (last block), replace with padding\n");
if(!FLAC__metadata_simple_iterator_delete_block(iterator, true))
return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator);