2013年5月10日金曜日

SWF AS3 string replacing

SWF AS3 string replacing


AS3 constant string is be defined by Constant Pool that is at the head of the ABC binary.
so I think string replacing is easy, and I implemented it powered by PHP.
IO_SWF,  IO_Bit is not used this time.

Program

Process

  • SWF => SWFHeader(8byte) + SWFMovieHeader + SWFMovie
    •  If sig = CWS then zlib uncompress SWFMovie
  • SWFMovie => TAG & TAG + ...
  • TAG (DoABC) => TagHeader + flag(4byte) + name(string) + ABC
  • ABC => version(4 byte) + intList + uintList + doubleList + stringList + etc.
  • stringList => strlen(u30) + string(strlen byte) + strlen ...
  • replace string!
  • overwrite each length field by re-calculated value.
  • contatinate all data.

u30 type

ABC primitive type u30 has little complex data structure.
  • 0 ~ 2^7-1 => (1byte) 0XXXXXXX
  • 2^7 ~ 2^(7+7)-1 => (2byte) 1XXXXXXX 0XXXXXXXX
  • 2^(7+7) ~ 2^(7+7+7)-1 => (3byte) 1XXXXXXX 1XXXXXXX 0XXXXXXX
  • ...
With MSB as extended bit, variable length field is expressed. (UTF-8 like format)

Usage

You can replace AS3 string by swfas3strreplace.php
yoya@sakura:~/git/flashswf$ php swfas3strreplace.php
Usage: swfas3strreplace.php <swf> # listing
Usage: swfas3strreplace.php <swf> <from> <to> [<from2> <to2> [...]]
 The list of the AS3 character string can be acquired by passing only SWF.
yoya@sakura:~/git/flashswf$ php swfas3strreplace.php topbg.swf
string_count: 34
[0]: (null string)
[1]: madoka_fla
[2]: OP_1
[3]: flash.display
[4]: MovieClip
[5]: madoka_fla:OP_1
[6]:
[7]: mami
[8]: hmhm
[9]: anko
[10]: syk
<略>
 Suitable character string (hmhm) to be listing is suitably rewritten.
yoya@sakura:~/git/flashswf$ php swfas3strreplace.php topbg.swf hmhm yoyayoya  > output.swf
 Result
yoya@sakura:~/git/flashswf$ php swfas3strreplace.php output.swf | grep -C 1 yoya
[7]: mami
[8]: yoyayoya
[9]: anko

Reference

You can see strings of Constant Pool also wish swfmill.

yoya@sakura:~/git/flashswf$ swfmill swf2xml topbg.swf | grep -A 42 "<DoABCDefine"
      <DoABCDefine flags="1" name="">
        <actions>
          <Action3 minorVersion="16" majorVersion="46">
            <constants>
              <Constants>
                <ints/>
                <uints/>
                <doubles/>
                <strings>
                  <String2 value="madoka_fla"/>
                  <String2 value="OP_1"/>
                  <String2 value="flash.display"/>
                  <String2 value="MovieClip"/>
                  <String2 value="madoka_fla:OP_1"/>
                  <String2 value=""/>
< omit ...>


2011年8月18日木曜日

OpenGL/GLUT for PHP5.3 exe tarball v0.0.1 (experimental)

OpenGL/GLUT for PHP5.3 exe tarball v0.0.1 (experimental)

i release zip archive of OpenGL PHP for Windows, it runnable so
http://diary.awm.jp/~yoya/data/2011/08/17/php53opengl-0.0.1.zip

i checked it on Microsoft Windows Vista and 7 32bits version.

i reported sourceforge.net の phpopengl forum



Dear all.
Windows binary PHP5.3 with OpenGL/GLUT extension for you.

source repos.


↓original source, it runnable on php4
- http://phpopengl.sourceforge.net/

↓my php5 migration and costomize code
- https://github.com/yoya/phpopengl/

usage



php -c php.ini samples\contrib\gears.php

[f:id:yoya:20110818025231p:image]

problem


  • OpenGL 1.2 version because Windows SDK 7.1 's default.

  • GLUT callback is not stable so maybe not runnable sample under.
    • samples\examples\movelight.php
    • samples\glut\glut_example.php
    • samples\redbook\bezmesh.php
  • msvcr100.dll
    • attached in zip. I'm trying to enbeded into exe file.

2011年2月13日日曜日

SWF Shape reducer

* SWF Shape reducer


** code
** size compare

- before
magic=CWS  version=8  file_length=3166474
 rect=(0, 0)-(550, 300) (f_size=15)

- after
magic=CWS  version=8  file_length=3166366
 rect=(0, 0)-(550, 300) (f_size=15)

** detail


swf defineshape vector data has length field at first, and following data has  concrete value.


- sample

+------------------------------+
| numBits |  deltaX  | deltaY  |
+------------------------------+
<4or5 bits><-numBits-><-numBits->


i can reduce size for rebuild swf binary.


** notes

- numBits 0 is OK
- styleChangeRecord x, y is not delta value.
- new style not add table but replace it.

** TODO

- deforme vector.

SWF DefineShape parser

SWF DefineShape parser in PHP.

** essence




  • http://openpear.org/package/IO_SWF/src/trunk/IO/SWF/Shape.php






  • $this->_shapeBounds = IO_SWF_Type::parseRECT($reader);
    $this->_parseFILLSTYLEARRAY($reader);
    $this->_parseLINESTYLEARRAY($reader);
    $reader->byteAlign();
    $numFillBits = $reader->getUIBits(4);
    $numLineBits = $reader->getUIBits(4);
    while ($done === false) {
        $typeFlag = $reader->getUIBit();
        if ($typeFlag == 0) {
            $endOfShape = $reader->getUIBits(5);
            if ($endOfShape == 0) {
                $done = true;
            } else {
                // StyleChangeRecord
                ...
            }
        } else {
            $straightFlag = $reader->getUIBit();
            if ($straightFlag) {
                 // StraightEdgeRecord
                 ...
                $stateNewStyles = $reader->getUIBit();
                if ($stateNewStyles) {
                    $this->_parseFILLSTYLEARRAY($reader);
                    $this->_parseLINESTYLEARRAY($reader);
                    $reader->byteAlign();
    $numFillBits = $reader->getUIBits(4);
                    $numLineBits = $reader->getUIBits(4);
                }
            } else {
                 // CurvedEdgeRecord
                 ...
            }
        }
    }
    
    ** ready

    pear channel-discover openpear.org
    pear install openpear/IO_Bit
    pear install openpear/IO_SWF
    

    ** go

    ShapeId: 1
    ShapeBounds:
            (-7.75, -7.75) - (7.75, 7.75)
    FillStyles:
            solid fill: #0066ff
    LineStyles:
    ShapeRecords:
            ChangeStyle: MoveTo: (-7.75, -7.75)  FillStyle: 0|1  LineStyle: 0
            StraightEdge: MoveTo: (7.75, -7.75)
            StraightEdge: MoveTo: (7.75, 7.75)
            StraightEdge: MoveTo: (-7.75, 7.75)
            StraightEdge: MoveTo: (-7.75, -7.75)
    

    ** TODO

    i want convert to SVG for shape display.




    2011年2月12日土曜日

    Challenge to FizzBuzz

    I challenged to fizzbuzz because i had watched "fizzbuzz" in Twitter timeline.
    It takes 5 minutes for take1,2,3. so good.

    - reference
    --    http://www.aoky.net/articles/jeff_atwood/why_cant_programmers_program.htm

    * subject

    Let's programming 1 to 100 number print out.
    but if number is multiple of three then print "Fizz",
    ,multiple of five then print "Buzz", and
    multiple of three and file then print "FizzBuzz".

    * take1

    simple and  stupidly honest.

    <?php
    foreach (range(1, 100) as $n) {
        if ($n % 3 == 0)  {
            if ($n % 5 == 0)  {
              echo "FizzBuzz\n";
            } else {
              echo "Fizz\n";
            }
        } elseif ($n % 5 == 0)  {
              echo "Buzz\n";
        } else {
            echo "$n\n";
        }
    }
    

    * take2

    aggregate %5 routine.

    <?php
    foreach (range(1, 100) as $n) {
        $d = '';
        if ($n % 3 == 0)  {
            $d .= "Fizz";
        }
        if ($n % 5 == 0)  {
            $d .= "Buzz";
        }
        if ($d === '') {
            $d = $n;
        }
        echo "$d\n";
    }

    * take2'

    ternary operand

    <?php
    foreach (range(1, 100) as $n) {
        $d = ($n % 3)?'':"Fizz";
        $d .= ($n % 5)?'':"Buzz";
        echo ($d?$d:$n)."\n";
    }
    

    i don't want to use ternary operand nesting.

    * take3

    flag programming. clearly but long code.

    <?php
    foreach (range(1, 100) as $n) {
        $b3 = ($n % 3)?0:1;
        $b5 = ($n % 5)?0:2;
        switch ($b3 | $b5) {
            case 0:
                 echo "$n\n";
                 break;
            case 1:
                 echo "Fizz\n";
                 break;
            case 2:
                 echo "Buzz\n";
                 break;
            case 3:
                 echo "FizzBuzz\n";
                 break;
        }
    }
    

    * take3

    printf programming.

    $fmt = array('%d', 'Fizz', 'Buzz', 'FizzBuzz');
    foreach (range(1, 100) as $n) {
        $b3 = ($n % 3)?0:1;
        $b5 = ($n % 5)?0:2;
        printf($fmt[$b3 | $b5]."\n", $n);
    }
    
    * perl

    foreach (1 .. 100) {
        $_ = ($_, 'Fizz', 'Buzz', 'FizzBuzz')[2 * !($_ % 3) + !($_ % 5)]."\n"; print
    }
    

    very simple....


    2011年1月25日火曜日

    What spoken at Zynga Japan

    I've spoken at Zynga Japan.
    I can speak about Flash SWF only, and wrote down blog.

    ** SWF review

    - header structure and control tag and display list.
    - base acknowledgement for gordon.js leaning.

    ** about gordon.js

    - flush v1 player, powered by javascript and SVG.
    - animation by SVG node addttion and removal to DOM.
    - Javascript mouse, button event handle to flush event.
    - multi-timeline by Worker thread. but not works.

    ** gordon.js remix

    - contents repository
    - deformer shape edge.
    - image pre filter (reverse pre-mutilipried alpha channel)
    - actionbytecode to javascript converter.

    ** SWFEditor

    - setActionVariables
    - setShapeAdjustMode
      -  http://d.hatena.ne.jp/yoya/20101027/swfed
    - applyShapeMatrixFactor

    ** etc...

    - Flush Lite Study. Im waiting it enjoyment.
    - ZendEngine Study. Thank you for your presentation.
    - I want to come again.