-
Notifications
You must be signed in to change notification settings - Fork 4
/
zzos
executable file
·2138 lines (1770 loc) · 62.4 KB
/
zzos
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
#!/usr/bin/perl
# zzos - decompile Android resources compiled with aapt
# Copyright (C) 2011 Lingnu Open Source Consulting Ltd.
# Written by Shachar Shemesh
#
# 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.
#
use Getopt::Long;
use File::Basename;
use File::Path qw(make_path);
use POSIX;
use feature "switch";
# Hashes for tracking the cross references
# The main hash. Key is integer resource number. Value is "package:class/name" string
my %references, %framework_references;
# Flags of resources. Key is same as above. Value is integer
my %references_flags, %framework_references_flags;
# Bags of attribs. Key is string "package:name". Value is reference to hash:
# Key is name of resource (as in %attrib_references). Value is integer, except for "flags", where
# value is a hash reference:
# Key is integer bit field or value. Value is the name of field.
my %attrib_bags, %framework_attrib_bags;
# Reverse hash of %references. Key is %references value, value is %references key.
my %rev_references, %framework_rev_references;
my $package_name, $apk_package_name, $package_ext;
my %class_map=(
raw=>undef,
layout=>undef,
xml=>undef,
anim=>undef,
menu=>undef,
animator=>undef,
accelerate_quad=>undef,
interpolator=>undef,
mipmap=>undef,
id=>\&parse_id_value,
string=>\&parse_string_value,
dimen=>\&parse_dimen_value,
color=>\&parse_color_value,
drawable=>\&parse_color_value,
bool=>\&parse_bool_value,
integer=>\&parse_int_value,
fraction=>\&parse_fraction_value,
);
$REF_INVALID= 0x08000000;
$REF_PUBLIC= 0x40000000;
$REF_HARDCODE= 0x80000000;
$REF_INTERNAL= 0x00000001;
$REF_SPEC= 0x00000002;
$REF_USED= 0x00000004;
$REF_RENAMED= 0x00000010;
$REF_DUPLICATE= 0x00000020;
# Attribute type bits
%attrib_types=(
0x00000001 => [ "reference", 0x01 ],
0x00000002 => [ "string", 0x03 ],
0x00000004 => [ "integer", 0x10 ],
0x00000008 => [ "boolean", 0x12 ],
0x00000010 => [ "color", 0x1c ],
0x00000020 => [ "float", 0x04 ],
0x00000040 => [ "dimension", 0x05 ],
0x00000080 => [ "fraction", 0x06 ],
0x00010000 => [ "enum", undef ],
0x00020000 => [ "flags", undef ],
);
%attrib_references=(
0x01000000 => "format",
0x01000001 => "min",
0x01000002 => "max",
0x01000003 => "localization", # 0 - not required, 1 - suggested
);
%plural_references=(
0x01000004 => "other",
0x01000005 => "zero",
0x01000006 => "one",
0x01000007 => "two",
0x01000008 => "few",
0x01000009 => "many",
);
# Hard code null
$framework_references{0}="null";
$framework_references_flags{0}=$REF_PUBLIC|$REF_HARDCODE;
# Bit counting cache for num_set_bits in 4 bit values.
my @bitcount=( 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 );
# Resource type open file descriptors
my %type_files;
sub help
{
print STDERR <<EOM;
zzos [options] apk...
Options:
--framework Given APK provides framework for this APK
--output directory where to create the extracted APK dirs. Default is current directory.
--nosubdir "output" specifies the actual directory to create.
EOM
exit 1;
}
my (@framework_names, $outdir, $nosubdir);
$outdir=".";
GetOptions("framework=s" => \@framework_names, "output=s" => \$outdir, "nosubdir" => \$nosubdir )
or help();
for( @framework_names ) {
read_resources( $_, $outdir, 1 ) or die "Failed to read framework resources for $_";
%framework_references = %references;
%framework_references_flags = %references_flags;
%framework_attrib_bags = %attrib_bags;
%framework_rev_references = %rev_references;
}
if( $#ARGV==-1 ) {
help();
exit 0;
}
my ($apkpath, $delayed_fail);
my $delayed_fail=0;
while( $apkpath = shift @ARGV ) {
# For each APK to be processed
my ($apkname, $apkdir ) = fileparse( $apkpath, ( ".apk" ) );
my $real_outdir="$outdir";
$real_outdir.="/$apkname" if ! $nosubdir;
if( process_apk( $apkpath, "$real_outdir" ) ) {
$delayed_fail = 1;
print STDERR "process_apk failed for $apkname\n";
}
}
exit $delayed_fail;
sub process_apk
{
my ( $apkpath, $outdir ) = @_;
if( !mkdir "$outdir" ) {
print STDERR "Failed to create directory $outdir: $!\n";
return 1;
}
set_package_name( $apkpath );
# List of files to be unzipped as they are
my @ziplist, @patch9list, %rename_list;
print STDOUT "Processing $apkpath\n";
if( ! read_resources( $apkpath, $outdir, 0 ) ) {
return 1;
}
# Get a list of files in the package
if( ! open( APKLIST, "-|", "aapt", ("l", $apkpath)) ) {
print STDERR "Failed to open $apkpath: $!\n";
return 1;
}
# Scan all the files in the APK
while( <APKLIST> ) {
chomp;
my $file=$_;
if( m/^resources.arsc$/ ) {
# Do nothing. We handle this file through aapt dump resources.
} elsif( m'^res/raw.*\.xml$' ) {
# .xml files under raw are not compiled.
push @ziplist, $_;
} elsif( m'^AndroidManifest.xml$' ) {
# AndroidManifest.xml is, obviously, an XML file.
if( !xml_decompile( $apkpath, $_, $outdir ) ) {
return 1;
}
} elsif( m'^res/.*\.xml$' ) {
# All other .xml files under res are compiled
if( !xml_decompile( $apkpath, $_, $outdir ) ) {
return 1;
}
} elsif( m'^res/(xml|anim|layout|menu)' ) {
# ALL files under those directories are xmls, regardless of extension
if( !xml_decompile( $apkpath, $_, $outdir ) ) {
return 1;
}
} elsif( m'^res/drawable.*\.9\.png$' ) {
# 9Patch png's need to be decompiled after being extracted
push @ziplist, $_;
push @patch9list, $_;
} else {
push @ziplist, $_;
}
# lang-country combination get their "r" removed by aapt
if( $file=~'^res/([^/]+)-([a-z]{2})-([A-Z]{2})(-[^/]*)?/' ) {
my $name="res/$1-$2-$3$4";
if( ! exists $rename_list{$name} ) {
$rename_list{$name}="res/$1-$2-r$3$4";
}
}
}
close APKLIST;
# Extract all of the files that need simple extraction
system "unzip", ( "-q", $apkpath, @ziplist, "-d", $outdir );
# De9patch all relevant files
de9patch( $outdir, \@patch9list );
# Rename incorrectly named directories
for( keys %rename_list ) {
rename "$outdir/$_", "$outdir/$rename_list{$_}";
}
return dump_values( $apkpath, $outdir );
}
sub de9patch
{
my( $outdir, $filelist )=@_;
for( @$filelist ) {
print " Decompiling $_\n";
(system "de9patch", ( "$outdir/$_", "$outdir/$_.tmp" ))==0
or die "Failed to decompile $_";
rename "$outdir/$_.tmp", "$outdir/$_"
or die "Failed to rename: $!";
}
}
sub read_resources
{
my ( $apkpath, $outdir, $framework ) = @_;
# Inherit all of the framework references
%references = %framework_references;
%references_flags = %framework_references_flags;
%attrib_bags = %framework_attrib_bags;
%rev_references = %framework_rev_references;
open RESFILE, "-|", "aapt", ( "d", "--values", "resources", $apkpath ) or
die "Failed to extract resources";
my $state=0;
my ($counter, $bag_index);
my $bag, $bag_flags;
my $lastclass;
while( <RESFILE> ) {
chomp;
my ($index, $package, $str);
my $flags=$framework ? 0 : $REF_INTERNAL;
$index=undef;
if( $state == 0 ) {
# Base state - usual processing
if( m'Package Group \d+ id=\d+ packageCount=\d+ name=(.+)$' ) {
if( !$framework ) {
# If this is not a framework APK read, set the package name
$package_name=$1;
} else {
$package_name=undef;
}
} elsif( m'^ *(spec )?resource 0x([0-9a-f]{8}) ([^:/]+):([^:/]+)/([^:]+): flags=0x([0-9a-f]+)' )
{
($index, $package, $str, $lastclass)=( hex($2), $3, "$4/$5", $4 );
if( $1 ) {
$flags|=$REF_SPEC;
}
if( (hex($6)&0x40000000)!=0 ) {
$flags|=$REF_PUBLIC;
}
if( exists( $references{$index} ) ) {
# A package sometimes defines attributes belonging to the android: package through plus
if( $framework || ($references_flags{$index} & $REF_INTERNAL) != 0 ) {
print STDERR "$package_name $package\n";
die "Reference 0x$2 refers once to $references{$index} and once to $package:$str";
}
}
my $process=1;
if( exists $rev_references{"$package:$str"} ) {
if( $rev_references{"$package:$str"} ne $index ) {
printf STDERR "Symbol 0x%08x has the same name as 0x%08x: $package:$str\n",
$index, $rev_references{"$package:$str"};
# Search for a free name to use
my( $ext, $i )=("___COPY", 1);
while( exists $rev_references{"$package:$str$ext$i"} ) {
++$i;
}
$str="$str$ext$i";
$flags|=$REF_RENAMED|$REF_DUPLICATE;
} else {
# Symbol defined twice with same everything - ignore second time
$process=0;
}
}
if( $process ) {
$references{$index}="$package:$str";
$rev_references{"$package:$str"}=$index;
$references_flags{$index}=$flags;
}
} elsif( m"(spec )?resource 0x([0-9a-f]{8}) ([-a-zA-Z\.]+):([^:/]+)/([^:]+): <bag>( \(PUBLIC\))?" )
{
($index, $package, $type, $str)=( hex($2), $3, $4, $5 );
if( $1 ) {
$flags|=$REF_SPEC;
}
if( $6 ) {
$flags|=$REF_PUBLIC;
}
if( $type eq "attr" ) {
# We don't care about style bags at this point
$state=1; # Start of bag
$bag_index = "$package:$str";
}
} elsif( m'^ INVALID TYPE CONFIG FOR RESOURCE 0x([0-9a-f]+)$' ) {
print STDERR "ERROR: Invalid type config for resource 0x$1: creating place holder\n";
# Just put something, and we'll most likely pad this later on
$index=hex($1);
$references{$index}=sprintf("%s:%s/INVALID%08x", $package_name, $lastclass, $index);
$references_flags{$index}=$REF_INVALID;
# Do not define the reverse mapping - we made up the name anyways
} else {
}
} elsif( $state == 1 ) {
# Start of bag
if( m'Parent=0x([0-9a-f]{8}), Count=(\d+)' ) {
die "Attrib style with parent 0x$1" if hex($1)!=0;
$state = 2; # Cleanly inside the bag
$counter = $2+0;
$bag = { };
$bag_flags = {};
die "Zero counter on bag 0x$1" if $counter==0;
} else {
die "Line \"$_\" inside bag";
$state = 0;
}
} elsif( $state == 2 ) {
# Inside bag
if( m'#(\d+) \(Key=0x([0-9a-f]{8})\): \(color\) #([0-9a-f]{8})' ) {
if( exists $attrib_references{hex($2)} ) {
# Hardcoded reference
$bag->{$attrib_references{hex($2)}}=hex($3);
} else {
$$bag_flags{hex($2)}=hex($3);
}
$counter--;
} else {
die "Inside bag, got unparsable \"$_\"" if( $counter>0 );
}
if( $counter == 0 ) {
$state = 0;
if( scalar keys %$bag_flags ) {
$bag->{flags}=$bag_flags;
}
$attrib_bags{$bag_index} = $bag;
}
} else {
die "Unable to parse resource line \"$_\"";
}
}
close RESFILE;
return 1;
}
sub set_package_name
{
my ($apkpath)=@_;
if( !open XMLFILE, "-|", "aapt", ("d", "xmltree", $apkpath, "AndroidManifest.xml") ) {
print STDERR "Failed to dump XML tree $filename\n";
return 0;
}
while(<XMLFILE>) {
chomp;
if( m'^ A: package="(.*)" \(Raw: ".*"\)$' ) {
$flag = 0;
$apk_package_name = $1;
last;
}
}
close XMLFILE;
}
sub xml_decompile
{
my( $apkpath, $filename, $outdir ) = @_;
my $error;
print STDOUT " Generating $filename\n";
make_path( dirname("$outdir/$filename"), { error => \$error } );
if( @$error ) {
print STDERR "Error creating ".dirname("$outdir/$filename").": $!\n";
return 0;
}
if( !open XMLFILE, "-|", "aapt", ("d", "xmltree", $apkpath, $filename) ) {
print STDERR "Failed to dump XML tree $filename\n";
return 0;
}
if( ! open OUTFILE, ">", "$outdir/$filename" ) {
print STDERR "Failed to create XML file $outdir/$filename: $!\n";
close XMLFILE;
return 0;
}
print OUTFILE '<?xml version="1.0" encoding="utf-8"?>'."\n";
my @elements=();
my @schemas;
my %namespaces, %rev_namespaces;
my $current_level=-1;
my $element_open=0;
my $line_number=0;
while(<XMLFILE>) {
chomp;
$line_number++;
given ($_) {
when(m'^( *)N: ([^=]+)=(http://schemas.android.com/apk/res/)(.*)$') {
$namespaces{$2}=$4;
$rev_namespaces{$4}=$2;
local $new_level=length $1;
$new_level/=2;
local $firstline=1;
while( $current_level>=$new_level ) {
# Close all pending open Elements
if( $elements[$current_level] ) {
print OUTFILE " "x($current_level*2) if !$firstline;
if( $element_open ) {
print OUTFILE " />\n";
$element_open=0;
} else {
print OUTFILE "</$elements[$current_level]>\n";
}
$firstline=0;
}
--$current_level;
}
if( $element_open ) {
# Close pending elements
print OUTFILE ">\n";
$element_open=0;
}
$new_level<=$current_level+1 or
die("Jumped for level $current_level to $new_level in one go at $filename:$line_number");
$current_level=$new_level;
$elements[$new_level]="";
push( @schemas, "xmlns:$2=\"$3$4\"");
}
when(m'^( *)N: ([^=]+)=(.*)$') {
$namespaces{$2}=$3;
$rev_namespaces{$3}=$2;
local $new_level=length $1;
$new_level/=2;
local $firstline=1;
while( $current_level>=$new_level ) {
# Close all pending open Elements
if( $elements[$current_level] ) {
print OUTFILE " "x($current_level*2) if !$firstline;
if( $element_open ) {
print OUTFILE " />\n";
$element_open=0;
} else {
print OUTFILE "</$elements[$current_level]>\n";
}
$firstline=0;
}
--$current_level;
}
if( $element_open ) {
# Close pending elements
print OUTFILE ">\n";
$element_open=0;
}
$new_level<=$current_level+1 or
die("Jumped for level $current_level to $new_level in one go at $filename:$line_number");
$current_level=$new_level;
$elements[$new_level]="";
push( @schemas, "xmlns:$2=\"$3\"");
}
when(/^( *)E: ([^ ]+)/) {
# Element
local $new_level=length $1;
$new_level/=2;
local $firstline=1;
while( $current_level>=$new_level ) {
# Close all pending open Elements
if( $elements[$current_level] ) {
print OUTFILE " "x($current_level*2) if !$firstline;
if( $element_open ) {
print OUTFILE " />\n";
$element_open=0;
} else {
print OUTFILE "</$elements[$current_level]>\n";
}
$firstline=0;
}
--$current_level;
}
$nospace=0;
if( $element_open ) {
# Close pending elements
print OUTFILE ">\n";
$element_open=0;
}
$new_level<=$current_level+1 or
die("Jumped for level $current_level to $new_level in one go at $filename:$line_number");
$current_level=$new_level;
$elements[$current_level]=$2;
print OUTFILE "$1<$2";
for( @schemas ) {
print OUTFILE " $_";
}
@schemas=(); # Empty the array
$element_open=1;
}
when(m'^( *)A: (([^:/]+):)?([^)]+)(\(0x[0-9a-f]+\))?=(.*)$') {
# An attribute - we need to parse to find out which
# Attribute with value
$element_open or die "Attribute with no open element at $filename:$line_number";
my ($indent, $attr, $val)=($1, $4, $6);
if( $2 ) {
die "Attribute from undeclared namespace $3: \"$_\""
if ! exists $namespaces{$3};
$attr="$3:$4";
$val=parse_attribute($6, "$namespaces{$3}:$4", 1);
} elsif( $val=~'^@0x([0-9a-f]+)( .*)$' ) {
$val="@".format_reference(hex($1));
} elsif( $val=~'^\?0x([0-9a-f]+)( .*)$' ) {
$val="?".format_xml_styleref(hex($1), \%rev_namespaces);
} elsif( $val=~'\(Raw: "(.*)"\)$' ) {
$val=escape_string($1, 1);
} else {
die "Unparsable attribute";
}
print OUTFILE "\n$indent $attr=\"$val\"";
}
when(/^( *)C: "(.*)"$/) {
# Tag content
$element_open or die "Content with no open element at $filename:$line_number";
# Unescape the string, and escape it differently
my $content=$2;
$content=~s/\\n/\n/g;
$content=~s/\\t/\t/g;
$content=~s/\\\\/\\/g;
$content=~s/&/&/g;
print OUTFILE ">$content";
$element_open=0;
$nospace=1;
}
when(/^Adding resources to ResTable:/) {
# Administrative messages
}
default {
die "Unknown line at $filename:$line_number: \"$_\"";
}
}
}
if( $element_open ) {
# Close pending elements
print OUTFILE ">\n";
$element_open=0;
}
while( $current_level>=0 ) {
# Close all pending open Elements
if( $elements[$current_level] ) {
print OUTFILE " "x($current_level*2) if !$nospace;
$nospace=0;
print OUTFILE "</$elements[$current_level]>\n";
}
--$current_level;
}
close XMLFILE;
close OUTFILE;
return 1;
}
sub num_set_bits
{
my ($val)=@_;
my $ret=0, $i=0;
# Use a translation table for each nibble. There are 8 nibbles in 32 bit
while( $i<8 && $val!=0 ) {
$ret+=$bitcount[$val&0x0f];
$val>>=4;
$i++;
}
return $ret;
}
sub reparse_attrib
{
my ( $attrib, $val ) = @_;
$val = $val * 1;
# Is there anything to translate?
if( !exists( $attrib_bags{$attrib} ) ) {
return undef;
}
my $bag = $attrib_bags{$attrib};
die "Attrib $attrib with no format" if ! exists $bag->{format};
my $ret=undef;
my $bag_flags=$bag->{format};
if( ($bag_flags&0x00030000) != 0 ) {
die "Attrib $attrib should have flags, but doesn't" if ! exists $bag->{flags};
# Attrib has either enum or flags, either way this code will handle it
$ret=reparse_attrib_enum_flags($bag->{flags}, $attrib, $val);
if( ($bag_flags&0x00010000) == 0 and $val==0 and not defined $ret ) {
# These are flags, not enums, and the data is zero
$ret="";
}
$bag_flags&= ~0x00030000;
}
return $ret if defined $ret;
if( $bag_flags & 0x0004 ) {
# Integer is one of the accepted types
return sprintf "%d", $val;
}
my $strval=sprintf "%08x", $val;
for( keys %attrib_types ) {
if( ($_ & 0xfff8) ) {
if( ($bag_flags&$_)!=0 ) {
$ret = format_value( $strval, ${$attrib_types{$_}}[1] );
$bag_flags &= ~$_;
}
}
}
die sprintf "Unhandled flags in bag $attrib 0x%08x", $bag_flags
if $bag_flags!=0 && ! defined $ret;
die sprintf "Failed to format attrib $attrib with value #%08x", $val
if ! defined $ret;
return $ret;
}
sub reparse_attrib_enum_flags
{
my ($bag, $attrib, $val)=@_;
my ($ret, $data, $zero )=(undef, 0, undef);
my $rev = ~$val;
my @selected;
my @bag_keys=keys( %$bag );
@bag_keys = sort { num_set_bits($bag->{$b}) <=> num_set_bits($bag->{$a}) } @bag_keys;
for( @bag_keys ) {
if( ($rev & $bag->{$_})==0 ) {
die sprintf "Attrib $attrib contains key 0x%08x which is undefined"
if ! exists( $references{$_} );
if( ($data | $bag->{$_}) != $data ) {
$data |= $bag->{$_};
push @selected, $_;
} elsif( $bag->{$_}==0 ) {
$references{$_} =~ '/(.*)$';
$zero = $1;
}
}
last if( $data!=0 && $data==$val ); # We are done anyways
}
return $zero if $val==0 && $zero;
return undef if $val!=$data || $#selected==-1;
# We now need to eliminate overlapping values
$val=0;
while( $#selected > -1 ) {
my $att = shift @selected;
my $att_val=$bag->{$att};
my $flag=0;
# This value is not contained by any values that came before, but it might be contained
# by values later on.
FUTURE: for( @selected ) {
# Does the current attribute change the $i attribute?
if( ($bag->{$_}|$att_val|$val)==($bag->{$_}|$val) ) {
$flag=1;
last FUTURE;
}
}
if( !$flag ) {
$references{$att} =~ '/(.*)$';
$ret .= "|" if length($ret)>0;
$ret .= $1;
$val |= $att_val;
}
}
die sprintf("Internal logic error val=#%08x data=#%08x", $val, $data) if $val != $data;
return $ret;
}
# Parse a floating point type
sub parse_type_4
{
my $val=hex($_[0]);
my $value=0.0;
# Zero is a special case in FP
if( $val!=0 ) {
# 1 bit of sign, 8 bits of exponent, 23 bits of mantissa
my $man=$val % 0x800000;
$man/=2**23;
$man+=1;
$val>>=23;
my $exp=$val % 0x100;
$exp-=0x7f;
$val>>=8;
$value=$man*2**$exp;
if( $val ) {
$value= -$value;
}
}
return $value;
}
# Parse a dimension type
sub parse_type_5
{
my $val=signedhex($_[0]);
# Extract the value type
my $value=$val/256;
$value+=0.5;
$value=floor($value);
my $unit;
# Check which unit to use
given( $val & 0x0f ) {
when(0) { $unit='px'; }
when(1) { $unit='dip'; }
when(2) { $unit='sp'; }
when(3) { $unit='pt'; }
when(4) { $unit='in'; }
when(5) { $unit='mm'; }
default { die "$filename:$line_number: Unknown unit for type: \"$_[0]\"\n"; }
}
# Check what split between fraction and whole
given( ($val & 0x30)>>4 ) {
when(0) { }; # Integer
when(1) { $value/=128 }; # Fix point 7 bit shift
when(2) { $value/=32768 }; # Fix point 15 bit shift
when(3) { $value/=8388608 }; # Fix point 23 bit shift
}
return "$value$unit";
}
# Parse fraction type
sub parse_type_6
{
my $val=signedhex($_[0]);
# 4 bit relation
my $relation=$val&0x7;
$val >>= 4;
# 4 bit fraction type
my $type=$val&0x7;
$val >>= 4;
# 23 bit mantissa
my $mantissa=$val&((1<<23)-1);
$val >>= 23;
# $val is zero if the number is positive
if( $val ) {
# Number is negative - perform two's complement on it to turn it positive
$mantissa ^= (1<<23)-1;
$mantissa ++;
$val="-";
} else {
$val="";
}
given( $type ) {
when( 0x0 ) {
# Number is whole - do nothing
}
when( 0x1 ) {
# 16 bit whole, 7 bit fraction
$mantissa /= 2.0 ** 7;
}
when( 0x2 ) {
# 8 bit whole, 15 bit fraction
$mantissa /= 2.0 ** 15;
}
when( 0x3 ) {
# 23 bits of fraction
$mantissa /= 2.0 ** 23;
}
default {
die "While parsing fraction type, unknown type $type";
}
}
$mantissa *= 100; # Turn it into percents
given( $relation ) {
when( 0 ) {
$relation="";
}
when( 1 ) {
$relation="p";
}
default {
die "While parsing fraction type, unknown relation $relation at $filename:$line_number";
}
}
return "$val$mantissa%$relation";
}
# Parse type boolean
sub parse_type_12
{
my $val=signedhex($_[0]);
if( $val==0 ) {
return "false";
} elsif( $val==-1 ) {
return "true";
} else {
die sprintf "Boolean is 0x%08x, neither 0 nor -1", $val;
}
}
# Parse type ARGB8
sub parse_type_1c
{
my $val=hex($_[0]);
return sprintf "#%08x", $val;
}
# Parse type RGB8
sub parse_type_1d
{
my $val=hex($_[0]);
# Mask out the Alpha channel
$val&=0x00ffffff;
return sprintf "#%06x", $val;
}
# Parse type ARGB4
sub parse_type_1e
{
my $val=hex($_[0]);
# Mask out the Alpha channel
my $value=0;
$val>>=4;
$value=$val&0xf;
$val>>=4;
$value|=$val&0xf0;
$val>>=4;
$value|=$val&0xf00;
$val>>=4;
$value|=$val&0xf000;
return sprintf "#%04x", $value;
}
# Parse type RGB4
sub parse_type_1f
{
my $val=hex($_[0]);
# Mask out the Alpha channel
my $value=0;
$val>>=4;
$value=$val&0xf;
$val>>=4;
$value|=$val&0xf0;
$val>>=4;
$value|=$val&0xf00;
return sprintf "#%03x", $value;
}
sub parse_attribute
{
my ($value, $attrib, $known_string)=@_;
given( $value ) {
when(/^.*\(Raw: "(.*)"\)$/) {
return escape_string($1, $known_string);
}
when(/^\(type 0x4\)0x(.*)$/) {
return parse_type_4($1);
}
when(/^\(type 0x5\)0x(.*)$/) {
return parse_type_5($1);
}
when(/^\(type 0x6\)0x(.*)$/) {
return parse_type_6($1);
}
when(/^\(type 0x10\)0x(.*)$/) {
my $val=reparse_attrib( $attrib, hex($1) );
if( !defined($val) ) {