SWF/Tag.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 250 CC 18 SCAR 26.9
CC
18
Cov
0%
dump
30.0
parseTagContent
20.0
bitmapSize
20.0
CRAP 14.7
1 package IO::SWF::Tag;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Class::Accessor::Fast';
7  
8 use UNIVERSAL::require;
9  
10 __PACKAGE__->mk_accessors( qw(
11 code
12 content
13 tag
14 byte_offset
15 byte_size
16 length
17 swfInfo
18  
19 characterId
20 referenceId
21 placeFlag
22 depth
23 ));
24  
25 sub new {
26 my ($class, $swfInfo) = @_;
27 my $self = $class->SUPER::new;
28 $self->swfInfo($swfInfo) if $swfInfo;
29 return $self;
30 }
31  
32 sub getTagInfo {
33 my ($self, $tagCode, $label) = @_;
34 my %tagMap = (
35 # code => { name , klass )
36 0 => {'name' => 'End'},
37 1 => {'name' => 'ShowFrame'},
38 2 => {'name' => 'DefineShape', 'klass' => 'Shape'},
39 # 3 => {'name' => 'FreeCharacter'}, # ???
40 4 => {'name' => 'PlaceObject', 'klass' => 'Place'},
41 5 => {'name' => 'RemoveObject'},
42 6 => {'name' => 'DefineBits', 'klass' => 'Jpeg'},
43 7 => {'name' => 'DefineButton'},
44 8 => {'name' => 'JPEGTables', 'klass' => 'Jpeg'},
45 9 => {'name' => 'SetBackgroundColor', 'klass' => 'BGColor'},
46 10 => {'name' => 'DefineFont'},
47 11 => {'name' => 'DefineText'},
48 12 => {'name' => 'DoAction', 'klass' => 'Action'},
49 13 => {'name' => 'DefineFontInfo'},
50 14 => {'name' => 'DefineSound'},
51 15 => {'name' => 'StartSound'},
52 # 16 missing
53 17 => {'name' => 'DefineButtonSound'},
54 18 => {'name' => 'SoundStreamHead'},
55 19 => {'name' => 'SoundStreamBlock'},
56 20 => {'name' => 'DefineBitsLossless', 'klass' => 'Lossless'},
57 21 => {'name' => 'DefineBitsJPEG2', 'klass' => 'Jpeg'},
58 22 => {'name' => 'DefineShape2', 'klass' => 'Shape'},
59 24 => {'name' => 'Protect'},
60 # 25 missing
61 26 => {'name' => 'PlaceObject2', 'klass' => 'Place'},
62 # 27 missing
63 28 => {'name' => 'RemoveObject2'},
64 # 29,30,31 missing
65 32 => {'name' => 'DefineShape3', 'klass' => 'Shape'},
66 33 => {'name' => 'DefineText2'},
67 34 => {'name' => 'DefineButton2'},
68 35 => {'name' => 'DefineBitsJPEG3', 'klass' => 'Jpeg'},
69 36 => {'name' => 'DefineBitsLossless2', 'klass' => 'Lossless'},
70 37 => {'name' => 'DefineEditText'},
71 # 38 missing
72 39 => {'name' => 'DefineSprite', 'klass' => 'Sprite'},
73 # 40,41,42 missing
74 43 => {'name' => 'FrameLabel'},
75 # 44 missing
76 45 => {'name' => 'SoundStreamHead2'},
77 46 => {'name' => 'DefineMorphShape', 'klass' => 'Shape'},
78 48 => {'name' => 'DefineFont2'},
79 56 => {'name' => 'Export'},
80 57 => {'name' => ''},
81 58 => {'name' => ''},
82 59 => {'name' => 'DoInitAction', 'klass' => 'Action'},
83 60 => {'name' => 'DefineVideoStream'},
84 61 => {'name' => 'videoFrame'},
85 62 => {'name' => 'DefineFontInfo2'},
86 # 63 missing
87 64 => {'name' => 'EnableDebugger2'},
88 65 => {'name' => 'ScriptLimits'},
89 66 => {'name' => 'SetTabIndex'},
90 # 67,68 missing
91 69 => {'name' => 'FileAttributes'},
92 70 => {'name' => 'PlaceObject3'},
93 71 => {'name' => 'ImportAssets2'},
94 # 72 missing
95 73 => {'name' => 'DefineFontAlignZones'},
96 74 => {'name' => 'CSMTextSettings'},
97 75 => {'name' => 'DefineFont3'},
98 76 => {'name' => 'SymbolClass'},
99 77 => {'name' => 'MetaData'},
100 78 => {'name' => 'DefineScalingGrid'},
101 # 79,80,81 missing
102 82 => {'name' => 'DoABC'},
103 83 => {'name' => 'DefineShape4'},
104 84 => {'name' => 'DefineMorphShape2'},
105 # 85 missing
106 86 => {'name' => 'DefineSceneAndFrameLabelData'},
107 87 => {'name' => 'DefineBinaryData'},
108 88 => {'name' => 'DefineFontName'},
109 89 => {'name' => 'StartSound2'},
110 90 => {'name' => 'DefineBitsJPEG4'},
111 91 => {'name' => 'DefineFont4'},
112 777 => {'name' => 'Reflex'}, # swftools ?
113 );
114 if (defined $tagMap{$tagCode}{$label}) {
115 return $tagMap{$tagCode}{$label};
116 }
117 return undef();
118 }
119  
120 sub parse {
121 my ($self, $reader, $opts_href) = @_;
122 my ($byte_offset, $dummy) = $reader->getOffset();
123 $self->byte_offset($byte_offset);
124 my $tagAndLength = $reader->getUI16LE();
125 $self->code($tagAndLength >> 6);
126 my $length = $tagAndLength & 0x3f;
127 if ($length == 0x3f) { # long format
128 $length = $reader->getUI32LE();
129 }
130 $self->content($reader->getData($length));
131 ($byte_offset, $dummy) = $reader->getOffset();
132 $self->byte_size($byte_offset - $self->byte_offset);
133 }
134  
135 sub dump {
136 my ($self, $opts_href) = @_;
137 my %opts = ref($opts_href) ? %{$opts_href} : ();
138 my $code = $self->code;
139 my $name = $self->getTagInfo($code, 'name');
140 if (!$name) {
141 $name = 'unknown';
142 }
143 my $length = length($self->content);
144 print "Code: $code($name) Length: $length\n";
145 $opts{'Version'} = $self->swfInfo->{'Version'};
146 if ($self->parseTagContent(\%opts)) {
147 $self->tag->dumpContent($code, \%opts);
148 }
149 if (defined($opts{'hexdump'})) {
150 my $bitio = $opts{'bitio'};
151 $bitio->hexdump($self->byte_offset, $self->byte_size);
152 }
153 }
154  
155 sub build {
156 my ($self, $opts_href) = @_;
157 my $code = $self->code;
158 my $content = $self->content;
159 my $length = length($self->content);
160 my $writer = IO::SWF::Bit->new();
161 my $longFormat = 0;
162 if ($code == 6 || # DefineBitsJPEG
163 $code == 21 || # DefineBitsJPEG2
164 $code == 35 || # DefineBitsJPEG3
165 $code == 20 || # DefineBitsLossless
166 $code == 36 || # DefineBitsLossless2
167 $code == 19 # SoundStreamBlock
168 ) {
169 $longFormat = 1;
170 }
171 if (!$longFormat && ($length < 0x3f)) {
172 my $tagAndLength = ($code << 6) | $length;
173 $writer->putUI16LE($tagAndLength);
174 } else {
175 my $tagAndLength = ($code << 6) | 0x3f;
176 $writer->putUI16LE($tagAndLength);
177 $writer->putUI32LE($length);
178 }
179 return $writer->output() . $self->buildTagContent();
180 }
181  
182 sub parseTagContent {
183 my ($self, $opts_href) = @_;
184 my %opts = ref($opts_href) ? %{$opts_href} : ();
185 if ($self->tag) {
186 return 1;
187 }
188 my $code = $self->code;
189 my $klass = $self->getTagInfo($code, 'klass');
190 if (!defined $klass) {
191 return 0; # no parse
192 }
193  
194 my $module = "IO::SWF::Tag::$klass";
195 $module->require or die $@;
196 my $obj = $module->new($self->swfInfo);
197  
198 $opts{'Version'} = $self->swfInfo->{'Version'};
199 $obj->parseContent($code, $self->content, \%opts);
200 $self->tag($obj);
201 return 1;
202 }
203  
204 sub buildTagContent {
205 my $self = shift;
206 if (length($self->content)) {
207 return $self->content;
208 }
209 if (!$self->tag) {
210 return '';
211 }
212 my $code = $self->code;
213 my %opts = ( 'Version' => $self->swfInfo->{'Version'} );
214 $self->content($self->tag->buildContent($code, \%opts));
215 return $self->content;
216 }
217  
218 sub bitmapSize {
219 my $self = shift;
220 my $code = $self->code;
221 if (!$self->parseTagContent()) {
222 die "failed to parseTagContent";
223 }
224 if ($code == 6 || # 21: // DefineBitsJPEG2
225 $code == 35 # 35: // DefineBitsJPEG3
226 ) {
227 # dummy
228 }
229 elsif ($code == 20 || # 20: // DefineBitsLossless
230 $code == 36 # 36: // DefineBitsLossless2
231 ) {
232 # dummy
233 }
234 else {
235 # dummy
236 }
237 }
238  
239 1;