| 1 | package IO::SWF::Type::Action; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'IO::SWF::Type'; | ||
| 7 | |||
| 8 | use IO::SWF::Bit; | ||
| 9 | use IO::SWF::Type::String; | ||
| 10 | use IO::SWF::Type::Float; | ||
| 11 | use IO::SWF::Type::Double; | ||
| 12 | |||
| 13 | our %action_code_table = ( | ||
| 14 | # | ||
| 15 | # Opecode only | ||
| 16 | # | ||
| 17 | 0x04 => 'NextFrame', | ||
| 18 | 0x05 => 'PreviousFrame', | ||
| 19 | 0x06 => 'Play', | ||
| 20 | 0x07 => 'Stop', | ||
| 21 | 0x08 => 'ToggleQuality', | ||
| 22 | 0x09 => 'StopSounds', | ||
| 23 | # | ||
| 24 | 0x0A => 'Add', | ||
| 25 | 0x0B => 'Substract', | ||
| 26 | 0x0C => 'Multiply', | ||
| 27 | 0x0D => 'Divide', | ||
| 28 | 0x0E => 'Equals', | ||
| 29 | 0x0F => 'Less', | ||
| 30 | 0x10 => 'And', | ||
| 31 | 0x11 => 'Or', | ||
| 32 | 0x12 => 'Not', | ||
| 33 | 0x13 => 'StringEquals', | ||
| 34 | 0x14 => 'StringLength', | ||
| 35 | 0x15 => 'StringExtract', | ||
| 36 | # | ||
| 37 | 0x17 => 'Pop', | ||
| 38 | 0x18 => 'ToInteger', | ||
| 39 | # | ||
| 40 | 0x1C => 'GetVariable', | ||
| 41 | 0x1D => 'SetVariable', | ||
| 42 | # | ||
| 43 | 0x20 => 'SetTarget2', | ||
| 44 | 0x21 => 'StringAdd', | ||
| 45 | 0x22 => 'GetProperty', | ||
| 46 | 0x23 => 'SetProperty', | ||
| 47 | 0x24 => 'CloneSprite', | ||
| 48 | 0x25 => 'RemoveSprite', | ||
| 49 | 0x26 => 'Trace', | ||
| 50 | 0x2D => 'FSCommand2', # Flash Lite | ||
| 51 | # | ||
| 52 | |||
| 53 | # | ||
| 54 | 0x27 => 'StartDrag', | ||
| 55 | 0x28 => 'EndDrag', | ||
| 56 | 0x29 => 'StringLess', | ||
| 57 | # | ||
| 58 | 0x30 => 'RandomNumber', | ||
| 59 | 0x31 => 'MBStringLength', | ||
| 60 | 0x32 => 'CharToAscii', | ||
| 61 | 0x33 => 'AsciiToChar', | ||
| 62 | 0x34 => 'GetTime', | ||
| 63 | 0x35 => 'MBStringExtract', | ||
| 64 | 0x36 => 'MBCharToAscii', | ||
| 65 | 0x37 => 'MBAsciiToChar', | ||
| 66 | |||
| 67 | # | ||
| 68 | 0x3A => 'Delete', # SWF 5 | ||
| 69 | 0x3B => 'Delete2', # SWF 5 | ||
| 70 | 0x3C => 'DefineLocal', # SWF 5 | ||
| 71 | 0x3D => 'CallFunction', # SWF 5 | ||
| 72 | 0x3E => 'Return', # SWF 5 | ||
| 73 | 0x3F => 'Modulo', # SWF 5 | ||
| 74 | 0x40 => 'NewObject', # SWF 5 | ||
| 75 | 0x41 => 'DefineLocal2', # SWF 5 | ||
| 76 | 0x42 => 'InitArray', # SWF 5 | ||
| 77 | 0x43 => 'InitObject', # SWF 5 | ||
| 78 | 0x44 => 'TypeOf', # SWF 5 | ||
| 79 | 0x45 => 'TargetPath', # SWF 5 | ||
| 80 | 0x46 => 'Enumerate', # SWF 5 | ||
| 81 | 0x47 => 'Add2', # SWF 5 | ||
| 82 | 0x48 => 'Less2', # SWF 5 | ||
| 83 | 0x49 => 'Equals2', # SWF 5 | ||
| 84 | 0x4A => 'ToNumber', # SWF 5 | ||
| 85 | 0x4B => 'ToString', # SWF 5 | ||
| 86 | 0x4C => 'PushDuplicate', # SWF 5 | ||
| 87 | 0x4D => 'StackSwap', # SWF 5 | ||
| 88 | 0x4E => 'GetMember', # SWF 5 | ||
| 89 | 0x4F => 'SetMember', # SWF 5 | ||
| 90 | 0x50 => 'Increment', # SWF 5 | ||
| 91 | 0x51 => 'Decrement', # SWF 5 | ||
| 92 | 0x52 => 'CallMethod', # SWF 5 | ||
| 93 | 0x53 => 'NewMethod', # SWF 5 | ||
| 94 | 0x54 => 'InstanceOf', # SWF 6 | ||
| 95 | 0x55 => 'Enumerate2', # SWF 6 | ||
| 96 | # | ||
| 97 | 0x60 => 'BitAnd', # SWF 5 | ||
| 98 | 0x61 => 'BitOr', # SWF 5 | ||
| 99 | 0x62 => 'BitXOr', # SWF 5 | ||
| 100 | 0x63 => 'BitShift', # SWF 5 | ||
| 101 | 0x64 => 'BitURShift', # SWF 5 | ||
| 102 | # | ||
| 103 | 0x66 => 'StrictEquals', # SWF 6 | ||
| 104 | 0x67 => 'Greater', # SWF 6 | ||
| 105 | 0x68 => 'StringGreater', # SWF 6 | ||
| 106 | |||
| 107 | # | ||
| 108 | # has Data Payload | ||
| 109 | 0x81 => 'GotoFrame', | ||
| 110 | 0x83 => 'GetURL', | ||
| 111 | 0x87 => 'StoreRegister', # SWF 5 | ||
| 112 | 0x88 => 'ConstantPool', # SWF 5 | ||
| 113 | 0x8A => 'WaitForFrame', | ||
| 114 | 0x8B => 'SetTarget', | ||
| 115 | 0x8C => 'GoToLabel', | ||
| 116 | 0x8D => 'WaitForFrame2', | ||
| 117 | |||
| 118 | # | ||
| 119 | 0x94 => 'With', # SWF 5 | ||
| 120 | 0x96 => 'Push', | ||
| 121 | # | ||
| 122 | 0x99 => 'Jump', | ||
| 123 | 0x9A => 'GetURL2', | ||
| 124 | 0x9B => 'DefineFunction', # SWF 5 | ||
| 125 | # | ||
| 126 | 0x9D => 'If', | ||
| 127 | 0x9E => 'Call', # why it >=0x80 ? | ||
| 128 | 0x9E => 'GotoFrame2', | ||
| 129 | ); | ||
| 130 | |||
| 131 | sub getCodeName { | ||
| 132 | my $code = shift; | ||
| 133 | if (defined $action_code_table{$code}) { | ||
| 134 | return $action_code_table{$code}; | ||
| 135 | } else { | ||
| 136 | return "Unknown"; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | sub parse { | ||
| 141 | my ($reader, $opts_href) = @_; | ||
| 142 | my (%action, $data, @strs); | ||
| 143 | my $code = $reader->getUI8(); | ||
| 144 | $action{'Code'} = $code; | ||
| 145 | if ($code >= 0x80) { | ||
| 146 | my $length = $reader->getUI16LE(); | ||
| 147 | $action{'Length'} = $length; | ||
| 148 | if ($code == 0x81) { | ||
| 149 | # 0x81: // ActionGotoFrame | ||
| 150 | $action{'Frame'} = $reader->getUI16LE(); | ||
| 151 | } | ||
| 152 | elsif ($code == 0x83) { | ||
| 153 | # 0x83: // ActionGetURL | ||
| 154 | $data = $reader->getData($length); | ||
| 155 | @strs = split("\0", $data, 2+1); | ||
| 156 | $action{'UrlString'} = $strs[0]; | ||
| 157 | $action{'TargetString'} = $strs[1]; | ||
| 158 | } | ||
| 159 | elsif ($code == 0x88) { | ||
| 160 | # 0x88: // ActionConstantPool | ||
| 161 | my $count = $reader->getUI16LE(); | ||
| 162 | $action{'Count'} = $count; | ||
| 163 | $data = $reader->getData($length - 2); | ||
| 164 | @strs = split("\0", $data, $count+1); | ||
| 165 | my @ConstantPool = @strs[0 .. $count-1]; | ||
| 166 | $action{'ConstantPool'} = \@ConstantPool; | ||
| 167 | } | ||
| 168 | elsif ($code == 0x8A) { | ||
| 169 | # 0x8A: // ActionWaitForFrame | ||
| 170 | $action{'Frame'} = $reader->getUI16LE(); | ||
| 171 | $action{'SkipCount'} = $reader->getUI8(); | ||
| 172 | } | ||
| 173 | elsif ($code == 0x8B) { | ||
| 174 | # 0x8B: // ActionSetTarget | ||
| 175 | $data = $reader->getData($length); | ||
| 176 | @strs = split("\0", $data, 1+1); | ||
| 177 | $action{'TargetName'} = $strs[0]; | ||
| 178 | } | ||
| 179 | elsif ($code == 0x8C) { | ||
| 180 | # 0x8C: // ActionSetTarget | ||
| 181 | $data = $reader->getData($length); | ||
| 182 | @strs = split("\0", $data, 1+1); | ||
| 183 | $action{'Label'} = $strs[0]; | ||
| 184 | } | ||
| 185 | elsif ($code == 0x8D) { | ||
| 186 | # 0x8D: // ActionWaitForFrame2 | ||
| 187 | $action{'Frame'} = $reader->getUI16LE(); | ||
| 188 | $action{'SkipCount'} = $reader->getUI8(); | ||
| 189 | } | ||
| 190 | elsif ($code == 0x96) { | ||
| 191 | # 0x96: // ActionPush | ||
| 192 | my $data = $reader->getData($length); | ||
| 193 | my @values = (); | ||
| 194 | my $values_reader = IO::SWF::Bit->new(); | ||
| 195 | $values_reader->input($data); | ||
| 196 | while ($values_reader->hasNextData()) { | ||
| 197 | my %value = (); | ||
| 198 | my $type = $values_reader->getUI8(); | ||
| 199 | $value{'Type'} = $type; | ||
| 200 | if ($type == 0) { | ||
| 201 | # 0: // STRING | ||
| 202 | $value{'String'} = IO::SWF::Type::String::parse($values_reader); | ||
| 203 | } | ||
| 204 | elsif ($type == 1) { | ||
| 205 | # 1: // Float | ||
| 206 | $value{'Float'} = IO::SWF::Type::Float::parse($values_reader); | ||
| 207 | } | ||
| 208 | elsif ($type == 2) { | ||
| 209 | # 2: // null | ||
| 210 | $value{'null'} = undef(); | ||
| 211 | } | ||
| 212 | elsif ($type == 3) { | ||
| 213 | # 3: // undefined | ||
| 214 | $value{'undefined'} = undef(); | ||
| 215 | } | ||
| 216 | elsif ($type == 4) { | ||
| 217 | # 4: // RegisterNumber | ||
| 218 | $value{'RegisterNumber'} = $values_reader->getUI8(); | ||
| 219 | } | ||
| 220 | elsif ($type == 5) { | ||
| 221 | # 5: // Boolean | ||
| 222 | $value{'Boolean'} = $values_reader->getUI8(); | ||
| 223 | } | ||
| 224 | elsif ($type == 6) { | ||
| 225 | # 6: // Double | ||
| 226 | $value{'Double'} = IO::SWF::Type::Double::parse($values_reader); | ||
| 227 | } | ||
| 228 | elsif ($type == 7) { | ||
| 229 | # 7: // Integer | ||
| 230 | $value{'Integer'} = $values_reader->getUI32LE(); | ||
| 231 | } | ||
| 232 | elsif ($type == 8) { | ||
| 233 | # 8: // Constant8 | ||
| 234 | $value{'Constant8'} = $values_reader->getUI8(); | ||
| 235 | } | ||
| 236 | elsif ($type == 9) { | ||
| 237 | # 9: // Constant16 | ||
| 238 | $value{'Constant16'} = $values_reader->getUI16LE(); | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | die "Illegal ActionPush value's type($type)"; | ||
| 242 | } | ||
| 243 | push @values, \%value; | ||
| 244 | } | ||
| 245 | $action{'Values'} = \@values; | ||
| 246 | } | ||
| 247 | elsif ($code == 0x99) { | ||
| 248 | # 0x99: // ActionJump | ||
| 249 | $action{'BranchOffset'} = $reader->getSI16LE(); | ||
| 250 | } | ||
| 251 | elsif ($code == 0x9A) { | ||
| 252 | # 0x9A: // ActionGetURL2 | ||
| 253 | $action{'SendVarsMethod'} = $reader->getUIBits(2); | ||
| 254 | $action{'(Reserved)'} = $reader->getUIBits(4); | ||
| 255 | $action{'LoadTargetFlag'} = $reader->getUIBit(); | ||
| 256 | $action{'LoadVariablesFlag'} = $reader->getUIBit(); | ||
| 257 | } | ||
| 258 | elsif ($code == 0x9D) { | ||
| 259 | # 0x9D: // ActionIf | ||
| 260 | $action{'Offset'} = $reader->getSI16LE(); | ||
| 261 | } | ||
| 262 | elsif ($code == 0x9F) { | ||
| 263 | # 0x9F: // ActionGotoFrame2 | ||
| 264 | $action{'(Reserved)'} = $reader->getUIBits(6); | ||
| 265 | my $sceneBlasFlag = $reader->getUIBit(); | ||
| 266 | $action{'SceneBlasFlag'} = $sceneBlasFlag; | ||
| 267 | $action{'PlayFlag'} = $reader->getUIBit(); | ||
| 268 | if ($sceneBlasFlag == 1) { | ||
| 269 | $action{'SceneBias'} = $reader->getUI16LE(); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | else { | ||
| 273 | $action{'Data'} = $reader->getData($length); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | return \%action; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub build { | ||
| 280 | my ($writer, $action_href, $opts_href) = @_; | ||
| 281 | my %action = ref($action_href) ? %{$action_href} : (); | ||
| 282 | my ($data, $count); | ||
| 283 | |||
| 284 | my $code = $action{'Code'}; | ||
| 285 | $writer->putUI8($code); | ||
| 286 | if (0x80 <= $code) { | ||
| 287 | if ($code == 0x81) { | ||
| 288 | # 0x81: // ActionGotoFrame | ||
| 289 | $writer->putUI16LE(2); | ||
| 290 | $writer->putUI16LE($action{'Frame'}); | ||
| 291 | } | ||
| 292 | elsif ($code == 0x83) { | ||
| 293 | # 0x83: // ActionGetURL | ||
| 294 | $data = $action{'UrlString'}."\0".$action{'TargetString'}."\0"; | ||
| 295 | $writer->putUI16LE(length($data)); | ||
| 296 | $writer->putData($data); | ||
| 297 | } | ||
| 298 | elsif ($code == 0x88) { | ||
| 299 | # 0x88: // ActionConstantPool | ||
| 300 | my @ConstantPool = @{$action{'ConstantPool'}}; | ||
| 301 | $count = scalar(@ConstantPool); | ||
| 302 | $data = join("\0", @ConstantPool) . "\0"; | ||
| 303 | $writer->putUI16LE(length($data) + 2); | ||
| 304 | $writer->putUI16LE($count); | ||
| 305 | $writer->putData($data); | ||
| 306 | } | ||
| 307 | elsif ($code == 0x8A) { | ||
| 308 | # 0x8A: // ActionWaitForFrame | ||
| 309 | $writer->putUI16LE($action{'Frame'}); | ||
| 310 | $writer->putUI8($action{'SkipCount'}); | ||
| 311 | } | ||
| 312 | elsif ($code == 0x8B) { | ||
| 313 | # 0x8B: // ActionSetTarget | ||
| 314 | $data = $action{'TargetName'}."\0"; | ||
| 315 | $writer->putUI16LE(length($data)); | ||
| 316 | $writer->putData($data); | ||
| 317 | } | ||
| 318 | elsif ($code == 0x8C) { | ||
| 319 | # 0x8C: // ActionGoToLabel | ||
| 320 | $data = $action{'Label'}."\0"; | ||
| 321 | $writer->putUI16LE(length($data)); | ||
| 322 | $writer->putData($data); | ||
| 323 | } | ||
| 324 | elsif ($code == 0x8D) { | ||
| 325 | # 0x8D: // ActionWaitForFrame2 | ||
| 326 | $writer->putUI16LE($action{'Frame'}); | ||
| 327 | $writer->putUI8($action{'SkipCount'}); | ||
| 328 | } | ||
| 329 | elsif ($code == 0x96) { | ||
| 330 | # 0x96: // ActionPush | ||
| 331 | my $values_writer = IO::SWF::Bit->new(); | ||
| 332 | foreach my $value (@{$action{'Values'}}) { | ||
| 333 | my $type = $value->{'Type'}; | ||
| 334 | $values_writer->putUI8($type); | ||
| 335 | if ($type == 0) { | ||
| 336 | # 0: // STRING | ||
| 337 | my $str = $value->{'String'}; | ||
| 338 | my $pos = index($str, "\0"); | ||
| 339 | if ($pos < 0) { | ||
| 340 | $str .= "\0"; | ||
| 341 | } else { | ||
| 342 | $str = substr($str, 0, $pos + 1); | ||
| 343 | } | ||
| 344 | $values_writer->putData($str); | ||
| 345 | } | ||
| 346 | elsif ($type == 1) { | ||
| 347 | # 1: // Float | ||
| 348 | IO::SWF::Type::Float::build($values_writer, $value->{'Float'}); | ||
| 349 | } | ||
| 350 | elsif ($type == 2) { | ||
| 351 | # 2: // null | ||
| 352 | # nothing to do. | ||
| 353 | } | ||
| 354 | elsif ($type == 3) { | ||
| 355 | # 3: // undefined | ||
| 356 | # nothing to do. | ||
| 357 | } | ||
| 358 | elsif ($type == 4) { | ||
| 359 | # 4: // RegisterNumber | ||
| 360 | $values_writer->putUI8($value->{'RegisterNumber'}); | ||
| 361 | } | ||
| 362 | elsif ($type == 5) { | ||
| 363 | # 5: // Boolean | ||
| 364 | $values_writer->putUI8($value->{'Boolean'}); | ||
| 365 | } | ||
| 366 | elsif ($type == 6) { | ||
| 367 | # 6: // Double | ||
| 368 | IO::SWF::Type::Double::build($values_writer, $value->{'Double'}); | ||
| 369 | } | ||
| 370 | elsif ($type == 7) { | ||
| 371 | # 7: // Integer | ||
| 372 | $values_writer->putUI32LE($value->{'Integer'}); | ||
| 373 | } | ||
| 374 | elsif ($type == 8) { | ||
| 375 | # 8: // Constant8 | ||
| 376 | $values_writer->putUI8($value->{'Constant8'}); | ||
| 377 | } | ||
| 378 | elsif ($type == 9) { | ||
| 379 | # 9: // Constant16 | ||
| 380 | $values_writer->putUI16LE($value->{'Constant16'}); | ||
| 381 | } | ||
| 382 | else { | ||
| 383 | die "Illegal ActionPush value's type($type)"; | ||
| 384 | } | ||
| 385 | } | ||
| 386 | my $values_data = $values_writer->output(); | ||
| 387 | $writer->putUI16LE(length($values_data)); | ||
| 388 | $writer->putData($values_data); | ||
| 389 | } | ||
| 390 | elsif ($code == 0x99) { | ||
| 391 | # 0x99: // ActionJump | ||
| 392 | $writer->putUI16LE(2); | ||
| 393 | $writer->putSI16LE($action{'BranchOffset'}); | ||
| 394 | } | ||
| 395 | elsif ($code == 0x9A) { | ||
| 396 | # 0x9A: // ActionGetURL2 | ||
| 397 | $writer->putUI16LE(1); | ||
| 398 | $writer->putUIBits($action{'SendVarsMethod'}, 2); | ||
| 399 | $writer->putUIBits(0, 4); # Reserved | ||
| 400 | $writer->putUIBit($action{'LoadTargetFlag'}); | ||
| 401 | $writer->putUIBit($action{'LoadVariablesFlag'}); | ||
| 402 | } | ||
| 403 | elsif ($code == 0x9D) { | ||
| 404 | # 0x9D: // ActionIf | ||
| 405 | $writer->putUI16LE(2); | ||
| 406 | $writer->putSI16LE($action{'Offset'}); | ||
| 407 | } | ||
| 408 | elsif ($code == 0x9F) { | ||
| 409 | # 0x9F: // ActionGotoFrame2 | ||
| 410 | my $sceneBlasFlag; | ||
| 411 | if (defined ($action{'SceneBias'})) { | ||
| 412 | $sceneBlasFlag = 1; | ||
| 413 | $writer->putUI16LE(3); | ||
| 414 | } else { | ||
| 415 | $sceneBlasFlag = 0; | ||
| 416 | $writer->putUI16LE(1); | ||
| 417 | } | ||
| 418 | $writer->putUIBits(0, 6); # Reserved | ||
| 419 | $writer->putUIBit($sceneBlasFlag); | ||
| 420 | $writer->putUIBit($action{'PlayFlag'}); | ||
| 421 | if ($sceneBlasFlag) { | ||
| 422 | $writer->putUI16LE($action{'SceneBias'}); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | else { | ||
| 426 | $data = $action{'Data'}; | ||
| 427 | $writer->putUI16LE(length($data)); | ||
| 428 | $writer->putData($data); | ||
| 429 | } | ||
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | sub string { | ||
| 434 | my ($action_href, $opts_href) = @_; | ||
| 435 | my %action = ref($action_href) ? %{$action_href} : (); | ||
| 436 | |||
| 437 | my $code = $action{'Code'}; | ||
| 438 | my $str = sprintf('%s(Code=0x%02X)', getCodeName($code), $code); | ||
| 439 | if (defined ($action{'Length'})) { | ||
| 440 | $str .= sprintf(" (Length=%d):", $action{'Length'}); | ||
| 441 | $str .= "\n\t"; | ||
| 442 | if ($code == 0x88) { | ||
| 443 | # 0x88: // ActonConstantPool | ||
| 444 | $str .= " Count=".$action{'Count'}."\n"; | ||
| 445 | my @ConstantPool = @{$action{'ConstantPool'}}; | ||
| 446 | for(my $idx = 0; $idx < @ConstantPool;$idx++) { | ||
| 447 | $str .= "\t[$idx] " . $ConstantPool[$idx] . "\n"; | ||
| 448 | } | ||
| 449 | } | ||
| 450 | elsif ($code == 0x96) { | ||
| 451 | $str .= " "; | ||
| 452 | foreach my $value (@{$action{'Values'}}) { | ||
| 453 | foreach my $key (keys %{$value}) { | ||
| 454 | $str .= " ($key)".$value->{$key} if $key ne 'Type'; | ||
| 455 | } | ||
| 456 | } | ||
| 457 | } | ||
| 458 | else { | ||
| 459 | my @data_keys = keys %action; | ||
| 460 | foreach my $key (keys %action) { | ||
| 461 | next if ($key eq 'Code' || $key eq 'Length'); | ||
| 462 | my $value = $action{$key}; | ||
| 463 | if (ref($value) eq 'HASH') { | ||
| 464 | my @new_value = (); | ||
| 465 | foreach my $l_key (keys %{$value}) { | ||
| 466 | push @new_value, $l_key . ":" . $value->{$l_key}; | ||
| 467 | } | ||
| 468 | $value = join(' ', @new_value); | ||
| 469 | } | ||
| 470 | $str .= " " ."$key=$value"; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | } | ||
| 474 | return $str; | ||
| 475 | } | ||
| 476 | |||
| 477 | 1; |