-
Notifications
You must be signed in to change notification settings - Fork 67
/
metalist.cpp
2164 lines (1912 loc) · 78.3 KB
/
metalist.cpp
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
//==================================================================//
/*
AtomicParsley - metalist.cpp
AtomicParsley is GPL software; you can freely distribute,
redistribute, modify & use under the terms of the GNU General
Public License; either version 2 or its successor.
AtomicParsley is distributed under the GPL "AS IS", without
any warranty; without the implied warranty of merchantability
or fitness for either an expressed or implied particular purpose.
Please see the included GNU General Public License (GPL) for
your rights and further details; see the file COPYING. If you
cannot, write to the Free Software Foundation, 59 Temple Place
Suite 330, Boston, MA 02111-1307, USA. Or www.fsf.org
Copyright ©2006-2007 puck_lock
with contributions from others; see the CREDITS file
*/
//==================================================================//
#include "AtomicParsley.h"
bool BOM_printed = false;
uint32_t APar_ProvideTallyForAtom(const char *atom_name) {
uint32_t tally_for_atom = 0;
short iter = parsedAtoms[0].NextAtomNumber;
while (true) {
if (memcmp(parsedAtoms[iter].AtomicName, atom_name, 4) == 0) {
if (parsedAtoms[iter].AtomicLength == 0) {
tally_for_atom += file_size - parsedAtoms[iter].AtomicStart;
} else if (parsedAtoms[iter].AtomicLength == 1) {
tally_for_atom += parsedAtoms[iter].AtomicLengthExtended;
} else {
tally_for_atom += parsedAtoms[iter].AtomicLength;
}
}
if (iter == 0) {
break;
} else {
iter = parsedAtoms[iter].NextAtomNumber;
}
}
return tally_for_atom;
}
void printBOM() {
if (BOM_printed)
return;
#if defined(_WIN32) && !defined(__CYGWIN__)
if (UnicodeOutputStatus == WIN32_UTF16) {
APar_unicode_win32Printout(L"\xEF\xBB\xBF", "\xEF\xBB\xBF");
}
#else
fprintf(stdout, "\xEF\xBB\xBF"); // Default to output of a UTF-8 BOM
#endif
BOM_printed = true;
return;
}
#if defined(_WIN32) && !defined(__CYGWIN__)
void APar_unicode_win32Printout(
wchar_t *unicode_out,
char *
utf8_out) { // based on
// http://blogs.msdn.com/junfeng/archive/2004/02/25/79621.aspx
// its possible that this isn't even available on windows95
DWORD dwBytesWritten;
DWORD fdwMode;
HANDLE outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
// ThreadLocale adjustment, resource loading, etc. is skipped
if ((GetFileType(outHandle) & FILE_TYPE_CHAR) &&
GetConsoleMode(outHandle, &fdwMode)) {
if (wcsncmp(unicode_out, L"\xEF\xBB\xBF", 3) !=
0) { // skip BOM when writing directly to the console
WriteConsoleW(
outHandle, unicode_out, wcslen(unicode_out), &dwBytesWritten, 0);
}
} else {
// writing out to a file. Everything will be written out in utf8 to the
// file.
fprintf(stdout, "%s", utf8_out);
}
return;
}
#endif
void APar_fprintf_UTF8_data(const char *utf8_encoded_data) {
#if defined(_WIN32) && !defined(__CYGWIN__)
if (GetVersion() & 0x80000000 || UnicodeOutputStatus == UNIVERSAL_UTF8) {
fprintf(stdout,
"%s",
utf8_encoded_data); // just printout the raw utf8 bytes (not
// characters) under pre-NT windows
} else {
wchar_t *utf16_data = Convert_multibyteUTF8_to_wchar(utf8_encoded_data);
fflush(stdout);
APar_unicode_win32Printout(utf16_data, (char *)utf8_encoded_data);
fflush(stdout);
free(utf16_data);
utf16_data = NULL;
}
#else
fprintf(stdout, "%s", utf8_encoded_data);
#endif
return;
}
void APar_Mark_UserData_area(uint8_t track_num,
short userdata_atom,
bool quantum_listing) {
if (quantum_listing && track_num > 0) {
fprintf(stdout,
"User data; level: track=%u; atom \"%s\" ",
track_num,
parsedAtoms[userdata_atom].AtomicName);
} else if (quantum_listing && track_num == 0) {
fprintf(stdout,
"User data; level: movie; atom \"%s\" ",
parsedAtoms[userdata_atom].AtomicName);
} else {
fprintf(stdout, "User data \"%s\" ", parsedAtoms[userdata_atom].AtomicName);
}
return;
}
// the difference between APar_PrintUnicodeAssest above and
// APar_SimplePrintUnicodeAssest below is: APar_PrintUnicodeAssest contains the
// entire contents of the atom, NULL bytes and all APar_SimplePrintUnicodeAssest
// contains a purely unicode string (either utf8 or utf16 with BOM) and slight
// output formatting differences
void APar_SimplePrintUnicodeAssest(char *unicode_string,
int asset_length,
bool print_encoding) { // 3gp files
if (strncmp(unicode_string, "\xFE\xFF", 2) == 0) { // utf16
if (print_encoding) {
fprintf(stdout, " (utf16): ");
}
unsigned char *utf8_data = Convert_multibyteUTF16_to_UTF8(
unicode_string, asset_length * 6, asset_length);
#if defined(_WIN32) && !defined(__CYGWIN__)
if (GetVersion() & 0x80000000 ||
UnicodeOutputStatus ==
UNIVERSAL_UTF8) { // pre-NT or AP-utf8.exe (pish, thats my win98se,
// and without unicows support convert utf16toutf8
// and output raw bytes)
unsigned char *utf8_data = Convert_multibyteUTF16_to_UTF8(
unicode_string, asset_length * 6, asset_length - 14);
fprintf(stdout, "%s", utf8_data);
free(utf8_data);
utf8_data = NULL;
} else {
wchar_t *utf16_data = Convert_multibyteUTF16_to_wchar(
unicode_string, asset_length / 2, true);
// wchar_t* utf16_data = Convert_multibyteUTF16_to_wchar(unicode_string,
// (asset_length / 2) + 1, true);
APar_unicode_win32Printout(utf16_data, (char *)utf8_data);
free(utf16_data);
utf16_data = NULL;
}
#else
fprintf(stdout, "%s", utf8_data);
#endif
free(utf8_data);
utf8_data = NULL;
} else { // utf8
if (print_encoding) {
fprintf(stdout, " (utf8): ");
}
APar_fprintf_UTF8_data(unicode_string);
}
return;
}
///////////////////////////////////////////////////////////////////////////////////////
// embedded file extraction //
///////////////////////////////////////////////////////////////////////////////////////
/*----------------------
APar_Extract_uuid_binary_file
uuid_atom - pointer to the struct holding the information describing the
target atom originating_file - the full file path string to the parsed file
output_path - a (possibly null) string where the embedded file will be
extracted to
If the output path is a null pointer, create a new path derived from
originating file name & path - strip off the extension and use that as the base.
Read into memory the contents of that particular uuid atom. Glob onto the base
path the atom name and then the suffix that was embedded along with the file.
Write out the file to the now fully formed uuid_outfile path.
----------------------*/
void APar_Extract_uuid_binary_file(AtomicInfo *uuid_atom,
const char *originating_file,
char *output_path) {
uint32_t path_len = 0;
uint64_t atom_offsets = 0;
char *uuid_outfile = (char *)calloc(
1,
sizeof(char) * MAXPATHLEN + 1); // malloc a new string because it may be a
// cli arg for a specific output path
if (output_path == NULL) {
const char *orig_suffix = strrchr(originating_file, '.');
if (orig_suffix == NULL) {
fprintf(stdout,
"AP warning: a file extension for the input file was not "
"found.\n\tGlobbing onto original filename...\n");
path_len = strlen(originating_file);
memcpy(uuid_outfile, originating_file, path_len);
} else {
path_len = orig_suffix - originating_file;
memcpy(uuid_outfile, originating_file, path_len);
}
} else {
path_len = strlen(output_path);
memcpy(uuid_outfile, output_path, path_len);
}
char *uuid_payload =
(char *)calloc(1, sizeof(char) * (uuid_atom->AtomicLength - 36 + 1));
APar_readX(uuid_payload,
source_file,
uuid_atom->AtomicStart + 36,
uuid_atom->AtomicLength - 36);
uint32_t descrip_len = UInt32FromBigEndian(uuid_payload);
atom_offsets += 4 + descrip_len;
uint8_t suffix_len = (uint8_t)uuid_payload[atom_offsets];
char *file_suffix = (char *)calloc(1, sizeof(char) * suffix_len + 16);
memcpy(file_suffix, uuid_payload + atom_offsets + 1, suffix_len);
atom_offsets += 1 + suffix_len;
uint8_t mime_len = (uint8_t)uuid_payload[atom_offsets];
uint64_t mimetype_string = atom_offsets + 1;
atom_offsets += 1 + mime_len;
uint64_t bin_len = UInt32FromBigEndian(uuid_payload + atom_offsets);
atom_offsets += 4;
sprintf(uuid_outfile + path_len,
"-%s-uuid%s",
uuid_atom->uuid_ap_atomname,
file_suffix);
FILE *outfile = APar_OpenFile(uuid_outfile, "wb");
if (outfile != NULL) {
fwrite(uuid_payload + atom_offsets, (size_t)bin_len, 1, outfile);
fclose(outfile);
fprintf(stdout,
"Extracted uuid=%s attachment (mime-type=%s) to file: ",
uuid_atom->uuid_ap_atomname,
uuid_payload + mimetype_string);
APar_fprintf_UTF8_data(uuid_outfile);
fprintf(stdout, "\n");
}
free(uuid_payload);
uuid_payload = NULL;
free(uuid_outfile);
uuid_outfile = NULL;
free(file_suffix);
file_suffix = NULL;
return;
}
void APar_ExtractAAC_Artwork(short this_atom_num,
char *pic_output_path,
short artwork_count) {
char *base_outpath = (char *)malloc(sizeof(char) * MAXPATHLEN + 1);
if (snprintf(base_outpath,
MAXPATHLEN + 1,
"%s_artwork_%d",
pic_output_path,
artwork_count) > MAXPATHLEN) {
free(base_outpath);
return;
}
char *art_payload = (char *)malloc(
sizeof(char) * (parsedAtoms[this_atom_num].AtomicLength - 16) + 1);
memset(art_payload, 0, (parsedAtoms[this_atom_num].AtomicLength - 16) + 1);
APar_readX(art_payload,
source_file,
parsedAtoms[this_atom_num].AtomicStart + 16,
parsedAtoms[this_atom_num].AtomicLength - 16);
char *suffix = (char *)malloc(sizeof(char) * 5);
memset(suffix, 0, sizeof(char) * 5);
if (memcmp(art_payload, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) {
strcpy(suffix, ".png");
} else if (memcmp(art_payload, "\xFF\xD8\xFF\xE0", 4) == 0 ||
memcmp(art_payload, "\xFF\xD8\xFF\xE1", 4) == 0) {
strcpy(suffix, ".jpg");
}
strcat(base_outpath, suffix);
FILE *outfile = APar_OpenFile(base_outpath, "wb");
if (outfile != NULL) {
fwrite(art_payload,
(size_t)(parsedAtoms[this_atom_num].AtomicLength - 16),
1,
outfile);
fclose(outfile);
fprintf(stdout, "Extracted artwork to file: ");
APar_fprintf_UTF8_data(base_outpath);
fprintf(stdout, "\n");
}
free(base_outpath);
free(art_payload);
free(suffix);
return;
}
/*----------------------
APar_ImageExtractTest
buffer - pointer to raw image data
id3args - *currently unused* when testing raw image data from an image
file, results like mimetype & imagetype will be placed here
Loop through the ImageList array and see if the first few bytes in the image
data in buffer match any of the known image_binaryheader types listed. If it
does, and its png, do a further test to see if its type 0x01 which requires it
to be 32x32
----------------------*/
ImageFileFormatDefinition *APar_ImageExtractTest(char *buffer,
AdjunctArgs *id3args) {
ImageFileFormatDefinition *thisImage = NULL;
uint8_t total_image_tests = ImageListMembers();
for (uint8_t itest = 0; itest < total_image_tests; itest++) {
if (ImageList[itest].image_testbytes == 0) {
if (id3args != NULL) {
id3args->mimeArg = ImageList[itest].image_mimetype;
}
return &ImageList[itest];
} else if (memcmp(buffer,
ImageList[itest].image_binaryheader,
ImageList[itest].image_testbytes) == 0) {
if (id3args != NULL) {
id3args->mimeArg = ImageList[itest].image_mimetype;
if (id3args->pictype_uint8 == 0x01) {
if (memcmp(buffer + 16, "\x00\x00\x00\x20\x00\x00\x00\x20", 8) != 0 &&
itest != 2) {
id3args->pictype_uint8 = 0x02;
}
}
}
thisImage = &ImageList[itest];
break;
}
}
return thisImage;
}
/*----------------------
APar_Extract_ID3v2_file
id32_atom - pointer to the AtomicInfo ID32 atom that contains this while
ID3 tag (containing all the frames like APIC) frame_str - either APIC or GEOB
originfile - the originating mpeg-4 file that contains the ID32 atom
destination_folder - *currently not used* TODO: extract to this folder
id3args - *currently not used* TODO: extract by mimetype or imagetype or
description
Extracts (all) files of a particular frame type (APIC or GEOB - GEOB is
currently not implemented) out to a file next to the originating mpeg-4 file.
First, match frame_str to get the internal frameID number for APIC/GEOB frame.
Locate the .ext of the origin file, duplicate the path including the basename
(excluding the extension. Loop through the linked list of ID3v2Frame and search
for the internal frameID number. When an image is found, test the data that the
image contains and determine file extension from the ImageFileFormatDefinition
structure (containing some popular image format/extension definitions). In
combination with the file extension, use the image description and image type to
create the name of the output file. The image (which if was compressed on disc
was expanded when read in) and simply write out its data (stored in the 5th
member of the frame's field strings.
----------------------*/
void APar_Extract_ID3v2_file(AtomicInfo *id32_atom,
const char *frame_str,
const char *originfile,
const char *destination_folder,
AdjunctArgs *id3args) {
uint16_t iter = 0;
ID3v2Frame *eval_frame = NULL;
uint32_t basepath_len = 0;
char *extract_filename = NULL;
int frameID = MatchID3FrameIDstr(
frame_str, id32_atom->ID32_TagInfo->ID3v2Tag_MajorVersion);
int frameType = KnownFrames[frameID + 1].ID3v2_FrameType;
if (destination_folder == NULL) {
basepath_len = (strrchr(originfile, '.') - originfile);
}
if (frameType == ID3_ATTACHED_PICTURE_FRAME ||
frameType == ID3_ATTACHED_OBJECT_FRAME) {
if (id32_atom->ID32_TagInfo->ID3v2_FirstFrame == NULL)
return;
eval_frame = id32_atom->ID32_TagInfo->ID3v2_FirstFrame;
extract_filename = (char *)malloc(sizeof(char *) * MAXPATHLEN + 1);
while (eval_frame != NULL) {
if (frameType == eval_frame->ID3v2_FrameType) {
memset(extract_filename, 0, sizeof(char *) * MAXPATHLEN + 1);
memcpy(extract_filename, originfile, basepath_len);
iter++;
if (eval_frame->ID3v2_FrameType == ID3_ATTACHED_PICTURE_FRAME) {
ImageFileFormatDefinition *thisimage = APar_ImageExtractTest(
(eval_frame->ID3v2_Frame_Fields + 4)->field_string, NULL);
char *img_description =
APar_ConvertField_to_UTF8(eval_frame, ID3_DESCRIPTION_FIELD);
sprintf(
extract_filename + basepath_len,
"-img#%u-(desc=%s)-0x%02X%s",
iter,
img_description,
(uint8_t)((eval_frame->ID3v2_Frame_Fields + 2)->field_string[0]),
thisimage->image_fileextn);
if (img_description != NULL) {
free(img_description);
img_description = NULL;
}
} else {
char *obj_description =
APar_ConvertField_to_UTF8(eval_frame, ID3_DESCRIPTION_FIELD);
char *obj_filename =
APar_ConvertField_to_UTF8(eval_frame, ID3_FILENAME_FIELD);
sprintf(extract_filename + basepath_len,
"-obj#%u-(desc=%s)-%s",
iter,
obj_description,
obj_filename);
if (obj_description != NULL) {
free(obj_description);
obj_description = NULL;
}
if (obj_filename != NULL) {
free(obj_filename);
obj_filename = NULL;
}
}
FILE *extractfile = APar_OpenFile(extract_filename, "wb");
if (extractfile != NULL) {
fwrite((eval_frame->ID3v2_Frame_Fields + 4)->field_string,
(size_t)((eval_frame->ID3v2_Frame_Fields + 4)->field_length),
1,
extractfile);
fclose(extractfile);
fprintf(
stdout,
"Extracted %s to file: %s\n",
(frameType == ID3_ATTACHED_PICTURE_FRAME ? "artwork" : "object"),
extract_filename);
}
}
eval_frame = eval_frame->ID3v2_NextFrame;
}
}
if (extract_filename != NULL) {
free(extract_filename);
extract_filename = NULL;
}
return;
}
///////////////////////////////////////////////////////////////////////////////////////
// iTunes-style metadata listings //
///////////////////////////////////////////////////////////////////////////////////////
void APar_ExtractDataAtom(int this_atom_number) {
if (source_file != NULL) {
AtomicInfo *thisAtom = &parsedAtoms[this_atom_number];
char *parent_atom_name;
AtomicInfo parent_atom_stats = parsedAtoms[this_atom_number - 1];
parent_atom_name = parent_atom_stats.AtomicName;
uint32_t min_atom_datasize = 12;
uint32_t atom_header_size = 16;
if (thisAtom->AtomicClassification == EXTENDED_ATOM) {
if (thisAtom->uuid_style == UUID_DEPRECATED_FORM) {
min_atom_datasize += 4;
atom_header_size += 4;
} else {
min_atom_datasize = 36;
atom_header_size = 36;
}
}
if (thisAtom->AtomicLength > min_atom_datasize) {
char *data_payload = (char *)malloc(
sizeof(char) * (thisAtom->AtomicLength - atom_header_size + 1));
memset(data_payload,
0,
sizeof(char) * (thisAtom->AtomicLength - atom_header_size + 1));
APar_readX(data_payload,
source_file,
thisAtom->AtomicStart + atom_header_size,
thisAtom->AtomicLength - atom_header_size);
if (thisAtom->AtomicVerFlags == AtomFlags_Data_Text) {
if (thisAtom->AtomicLength < (atom_header_size + 4)) {
// tvnn was showing up with 4 chars instead of 3; easier to null it
// out for now
data_payload[thisAtom->AtomicLength - atom_header_size] = '\00';
}
APar_fprintf_UTF8_data(data_payload);
fprintf(stdout, "\n");
} else {
if ((memcmp(parent_atom_name, "trkn", 4) == 0) ||
(memcmp(parent_atom_name, "disk", 4) == 0)) {
if (UInt16FromBigEndian(data_payload + 4) != 0) {
fprintf(stdout,
"%u of %u\n",
UInt16FromBigEndian(data_payload + 2),
UInt16FromBigEndian(data_payload + 4));
} else {
fprintf(stdout, "%u\n", UInt16FromBigEndian(data_payload + 2));
}
} else if (strncmp(parent_atom_name, "gnre", 4) == 0) {
if (thisAtom->AtomicLength - atom_header_size <
3) { // oh, a 1byte int for genre number
char *genre_string =
GenreIntToString(UInt16FromBigEndian(data_payload));
if (genre_string != NULL) {
fprintf(stdout, "%s\n", genre_string);
} else {
fprintf(stdout,
" out of bound value - %u\n",
UInt16FromBigEndian(data_payload));
}
} else {
fprintf(stdout,
" out of bound value - %u\n",
UInt16FromBigEndian(data_payload));
}
} else if ((strncmp(parent_atom_name, "purl", 4) == 0) ||
(strncmp(parent_atom_name, "egid", 4) == 0)) {
fprintf(stdout, "%s\n", data_payload);
} else {
if (thisAtom->AtomicVerFlags == AtomFlags_Data_UInt &&
(thisAtom->AtomicLength <= 20 || thisAtom->AtomicLength == 24)) {
uint8_t bytes_rep =
(uint8_t)(thisAtom->AtomicLength - atom_header_size);
switch (bytes_rep) {
case 1: {
if ((memcmp(parent_atom_name, "cpil", 4) == 0) ||
(memcmp(parent_atom_name, "pcst", 4) == 0) ||
(memcmp(parent_atom_name, "pgap", 4) == 0)) {
if (data_payload[0] == 1) {
fprintf(stdout, "true\n");
} else {
fprintf(stdout, "false\n");
}
} else if (strncmp(parent_atom_name, "stik", 4) == 0) {
stiks *returned_stik =
MatchStikNumber((uint8_t)data_payload[0]);
if (returned_stik != NULL) {
fprintf(stdout, "%s\n", returned_stik->stik_string);
} else {
fprintf(
stdout, "Unknown value: %u\n", (uint8_t)data_payload[0]);
}
} else if (strncmp(parent_atom_name, "rtng", 4) ==
0) { // okay, this is definitely an 8-bit number
if (data_payload[0] == 2) {
fprintf(stdout, "Clean Content\n");
} else if (data_payload[0] != 0) {
fprintf(stdout, "Explicit Content\n");
} else {
fprintf(stdout, "Inoffensive\n");
}
} else {
fprintf(stdout, "%u\n", (uint8_t)data_payload[0]);
}
break;
}
case 2: { // tmpo
fprintf(stdout, "%u\n", UInt16FromBigEndian(data_payload));
break;
}
case 4: { // tves, tvsn
if (memcmp(parent_atom_name, "sfID", 4) == 0) {
sfIDs *this_store =
MatchStoreFrontNumber(UInt32FromBigEndian(data_payload));
if (this_store != NULL) {
fprintf(stdout,
"%s (%" PRIu32 ")\n",
this_store->storefront_string,
this_store->storefront_number);
} else {
fprintf(stdout,
"Unknown (%" PRIu32 ")\n",
UInt32FromBigEndian(data_payload));
}
} else {
fprintf(
stdout, "%" PRIu32 "\n", UInt32FromBigEndian(data_payload));
}
break;
}
case 8: {
fprintf(
stdout, "%" PRIu64 "\n", UInt64FromBigEndian(data_payload));
break;
}
}
} else if (thisAtom->AtomicClassification == EXTENDED_ATOM &&
thisAtom->AtomicVerFlags == AtomFlags_Data_uuid_binary &&
thisAtom->uuid_style == UUID_AP_SHA1_NAMESPACE) {
uint64_t offset_into_uuiddata = 0;
uint64_t descrip_len = UInt32FromBigEndian(data_payload);
offset_into_uuiddata += 4;
char *uuid_description =
(char *)calloc(1,
sizeof(char) * descrip_len +
16); // char uuid_description[descrip_len+1];
memcpy(uuid_description,
data_payload + offset_into_uuiddata,
descrip_len);
offset_into_uuiddata += descrip_len;
uint8_t suffix_len = (uint8_t)data_payload[offset_into_uuiddata];
offset_into_uuiddata += 1;
char *file_suffix =
(char *)calloc(1,
sizeof(char) * suffix_len +
16); // char file_suffix[suffix_len+1];
memcpy(
file_suffix, data_payload + offset_into_uuiddata, suffix_len);
offset_into_uuiddata += suffix_len;
uint8_t mime_len = (uint8_t)data_payload[offset_into_uuiddata];
offset_into_uuiddata += 1;
char *uuid_mimetype =
(char *)calloc(1,
sizeof(char) * mime_len +
16); // char uuid_mimetype[mime_len+1];
memcpy(
uuid_mimetype, data_payload + offset_into_uuiddata, mime_len);
fprintf(stdout,
"FILE%s; mime-type=%s; description=%s\n",
file_suffix,
uuid_mimetype,
uuid_description);
free(uuid_description);
uuid_description = NULL;
free(file_suffix);
file_suffix = NULL;
free(uuid_mimetype);
uuid_description = NULL;
} else { // purl & egid would end up here too, but Apple switched it
// to a text string (0x00), so gets taken care above
// explicitly
fprintf(stdout, "hex 0x");
for (int hexx = 1;
hexx <= (int)(thisAtom->AtomicLength - atom_header_size);
++hexx) {
fprintf(stdout, "%02X", (uint8_t)data_payload[hexx - 1]);
if ((hexx % 4) == 0 && hexx >= 4) {
fprintf(stdout, " ");
}
if ((hexx % 16) == 0 && hexx > 16) {
fprintf(stdout, "\n\t\t\t");
}
if (hexx == (int)(thisAtom->AtomicLength - atom_header_size)) {
fprintf(stdout, "\n");
}
}
} // end if AtomFlags_Data_UInt
}
free(data_payload);
data_payload = NULL;
}
}
}
return;
}
void APar_Print_iTunesData(const char *path,
char *output_path,
uint8_t supplemental_info,
uint8_t target_information,
AtomicInfo *ilstAtom) {
printBOM();
short artwork_count = 0;
if (ilstAtom == NULL) {
ilstAtom = APar_FindAtom("moov.udta.meta.ilst", false, SIMPLE_ATOM, 0);
if (ilstAtom == NULL)
return;
}
for (int i = ilstAtom->AtomicNumber; i < atom_number; i++) {
AtomicInfo *thisAtom = &parsedAtoms[i];
if (strncmp(thisAtom->AtomicName, "data", 4) ==
0) { // thisAtom->AtomicClassification == VERSIONED_ATOM) {
AtomicInfo *parent =
&parsedAtoms[APar_FindParentAtom(i, thisAtom->AtomicLevel)];
if ((thisAtom->AtomicVerFlags == AtomFlags_Data_Binary ||
thisAtom->AtomicVerFlags == AtomFlags_Data_Text ||
thisAtom->AtomicVerFlags == AtomFlags_Data_UInt) &&
target_information == PRINT_DATA) {
if (strncmp(parent->AtomicName, "----", 4) == 0) {
if (memcmp(parsedAtoms[parent->AtomicNumber + 2].AtomicName,
"name",
4) == 0) {
fprintf(stdout,
"Atom \"%s\" [%s;%s] contains: ",
parent->AtomicName,
parsedAtoms[parent->AtomicNumber + 1].ReverseDNSdomain,
parsedAtoms[parent->AtomicNumber + 2].ReverseDNSname);
APar_ExtractDataAtom(i);
}
} else if (memcmp(parent->AtomicName, "covr", 4) ==
0) { // libmp4v2 doesn't properly set artwork with the right
// flags (its all 0x00)
artwork_count++;
} else {
// converts iso8859 © in '©ART' to a 2byte utf8 © glyph; replaces
// libiconv conversion
memset(twenty_byte_buffer, 0, sizeof(char) * 20);
isolat1ToUTF8((unsigned char *)twenty_byte_buffer,
10,
(unsigned char *)parent->AtomicName,
4);
if (UnicodeOutputStatus == WIN32_UTF16) {
fprintf(stdout, "Atom \"");
APar_fprintf_UTF8_data(twenty_byte_buffer);
fprintf(stdout, "\" contains: ");
} else {
fprintf(stdout, "Atom \"%s\" contains: ", twenty_byte_buffer);
}
APar_ExtractDataAtom(i);
}
} else if (memcmp(parent->AtomicName, "covr", 4) == 0) {
artwork_count++;
if (target_information == EXTRACT_ARTWORK) {
APar_ExtractAAC_Artwork(
thisAtom->AtomicNumber, output_path, artwork_count);
}
}
if (thisAtom->AtomicLength <= 12) {
fprintf(
stdout,
"\n"); // (corrupted atom); libmp4v2 touching a file with copyright
}
}
}
if (artwork_count != 0 && target_information == PRINT_DATA) {
if (artwork_count == 1) {
fprintf(stdout,
"Atom \"covr\" contains: %i piece of artwork\n",
artwork_count);
} else {
fprintf(stdout,
"Atom \"covr\" contains: %i pieces of artwork\n",
artwork_count);
}
}
if (supplemental_info) {
fprintf(stdout, "---------------------------\n");
dynUpd.updage_by_padding = false;
// APar_DetermineDynamicUpdate(true); //gets the size of the padding
APar_Optimize(
true); // just to know if 'free' atoms can be considered padding, or (in
// the case of say a faac file) it's *just* 'free'
if (supplemental_info & 0x02) { // PRINT_FREE_SPACE
fprintf(stdout,
"free atom space: %" PRIu32 "\n",
APar_ProvideTallyForAtom("free"));
}
if (supplemental_info & 0x04) { // PRINT_PADDING_SPACE
if (!moov_atom_was_mooved) {
fprintf(stdout,
"padding available: %" PRIu64 " bytes\n",
dynUpd.padding_bytes);
} else {
fprintf(stdout, "padding available: 0 (reorg)\n");
}
}
if (supplemental_info & 0x08 &&
dynUpd.moov_udta_atom != NULL) { // PRINT_USER_DATA_SPACE
fprintf(stdout,
"user data space: %" PRIu64 "\n",
dynUpd.moov_udta_atom->AtomicLength);
}
if (supplemental_info & 0x10) { // PRINT_USER_DATA_SPACE
fprintf(stdout,
"media data space: %" PRIu32 "\n",
APar_ProvideTallyForAtom("mdat"));
}
}
return;
}
///////////////////////////////////////////////////////////////////////////////////////
// AP uuid metadata listings //
///////////////////////////////////////////////////////////////////////////////////////
void APar_Print_APuuidv5_contents(AtomicInfo *thisAtom) {
memset(twenty_byte_buffer, 0, sizeof(char) * 20);
isolat1ToUTF8((unsigned char *)twenty_byte_buffer,
10,
(unsigned char *)thisAtom->uuid_ap_atomname,
4);
fprintf(stdout, "Atom uuid=");
APar_print_uuid((ap_uuid_t *)thisAtom->AtomicName, false);
fprintf(stdout, " (AP uuid for \"");
APar_fprintf_UTF8_data(twenty_byte_buffer);
fprintf(stdout, "\") contains: ");
APar_ExtractDataAtom(thisAtom->AtomicNumber);
return;
}
void APar_Print_APuuid_deprecated_contents(AtomicInfo *thisAtom) {
memset(twenty_byte_buffer, 0, sizeof(char) * 20);
isolat1ToUTF8((unsigned char *)twenty_byte_buffer,
10,
(unsigned char *)thisAtom->AtomicName,
4);
if (UnicodeOutputStatus == WIN32_UTF16) {
fprintf(stdout, "Atom uuid=\"");
APar_fprintf_UTF8_data(twenty_byte_buffer);
fprintf(stdout, "\" contains: ");
} else {
fprintf(stdout, "Atom uuid=\"%s\" contains: ", twenty_byte_buffer);
}
APar_ExtractDataAtom(thisAtom->AtomicNumber);
return;
}
void APar_Print_APuuid_atoms(const char *path,
char *output_path,
uint8_t target_information) {
AtomicInfo *thisAtom = NULL;
printBOM();
AtomicInfo *metaAtom =
APar_FindAtom("moov.udta.meta", false, VERSIONED_ATOM, 0);
if (metaAtom == NULL)
return;
for (int i = metaAtom->NextAtomNumber; i < atom_number; i++) {
thisAtom = &parsedAtoms[i];
if (thisAtom->AtomicLevel <= metaAtom->AtomicLevel)
break; // we've gone too far
if (thisAtom->AtomicClassification == EXTENDED_ATOM) {
if (thisAtom->uuid_style == UUID_AP_SHA1_NAMESPACE) {
if (target_information == PRINT_DATA)
APar_Print_APuuidv5_contents(thisAtom);
if (target_information == EXTRACT_ALL_UUID_BINARYS &&
thisAtom->AtomicVerFlags == AtomFlags_Data_uuid_binary) {
APar_Extract_uuid_binary_file(thisAtom, path, output_path);
}
}
if (thisAtom->uuid_style == UUID_DEPRECATED_FORM &&
target_information == PRINT_DATA)
APar_Print_APuuid_deprecated_contents(thisAtom);
}
}
return;
}
///////////////////////////////////////////////////////////////////////////////////////
// 3GP asset metadata listings //
///////////////////////////////////////////////////////////////////////////////////////
void APar_PrintUnicodeAssest(char *unicode_string,
int asset_length) { // 3gp files
if (strncmp(unicode_string, "\xFE\xFF", 2) == 0) { // utf16
fprintf(stdout, " (utf16)] : ");
unsigned char *utf8_data = Convert_multibyteUTF16_to_UTF8(
unicode_string, (asset_length - 13) * 6, asset_length - 14);
#if defined(_WIN32) && !defined(__CYGWIN__)
if (GetVersion() & 0x80000000 ||
UnicodeOutputStatus ==
UNIVERSAL_UTF8) { // pre-NT or AP-utf8.exe (pish, thats my win98se,
// and without unicows support convert utf16toutf8
// and output raw bytes)
unsigned char *utf8_data = Convert_multibyteUTF16_to_UTF8(
unicode_string, (asset_length - 13) * 6, asset_length - 14);
fprintf(stdout, "%s", utf8_data);
free(utf8_data);
utf8_data = NULL;
} else {
wchar_t *utf16_data = Convert_multibyteUTF16_to_wchar(
unicode_string, (asset_length - 16) / 2, true);
APar_unicode_win32Printout(utf16_data, (char *)utf8_data);
free(utf16_data);
utf16_data = NULL;
}
#else
fprintf(stdout, "%s", utf8_data);
#endif
free(utf8_data);
utf8_data = NULL;
} else { // utf8
fprintf(stdout, " (utf8)] : ");
APar_fprintf_UTF8_data(unicode_string);
}
return;
}
void APar_Print_single_userdata_atomcontents(uint8_t track_num,
short userdata_atom,
bool quantum_listing) {
uint32_t box = UInt32FromBigEndian(parsedAtoms[userdata_atom].AtomicName);
char bitpacked_lang[3];
memset(bitpacked_lang, 0, 3);
unsigned char unpacked_lang[3];
uint32_t box_length = parsedAtoms[userdata_atom].AtomicLength;
char *box_data = (char *)malloc(sizeof(char) * box_length);
memset(box_data, 0, sizeof(char) * box_length);
switch (box) {
case 0x7469746C: //'titl'
case 0x64736370: //'dscp'
case 0x63707274: //'cprt'
case 0x70657266: //'perf'
case 0x61757468: //'auth'
case 0x676E7265: //'gnre'
case 0x616C626D: //'albm'
{
APar_Mark_UserData_area(track_num, userdata_atom, quantum_listing);
uint16_t packed_lang =
APar_read16(bitpacked_lang,