74a00f9cbc48d373b781d4800a149057aeffc397
[swftools.git] / lib / modules / swfabc.c
1 /* swfbits.c
2
3    Bitmap functions (needs libjpeg) 
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2007 Alessandro Molina
9    Copyright (c) 2007 Matthias Kramm <kramm@quiss.org>
10  
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
24
25 #include "../rfxswf.h"
26
27 static unsigned AVM2_uint32toU30(unsigned val, char * out)
28 {
29     unsigned len = 0;
30
31     while(val) {
32           out[len++] = val & 0xFF;
33           val >>= 7;
34     }
35
36     return len;
37 }
38
39 void AVM2_InsertStops(SWF*swf)
40 {
41     TAG * t;
42
43     //MAIN AS3 CLASS INIT
44     unsigned frame_nums = 0;
45
46     /* Count number of pages to set up stop callback for every frame */
47     t = swf->firstTag;
48     while(t) {
49         if (t->id == ST_SHOWFRAME) ++frame_nums;
50         t = t->next;
51     }
52
53     char pool2[26] = {0x01,0x00,0x00,0x00,0x73,0x74,0x6f,0x70,0x5f,0x66,0x6c,0x61,0x2e,0x4d,0x61,0x69
54     ,0x6e,0x54,0x69,0x6d,0x65,0x6c,0x69,0x6e,0x65,0x00};
55     TAG * classCall = swf_InsertTagBefore(swf, swf->firstTag, 0x04c);
56     swf_SetBlock(classCall, (U8*)pool2, sizeof(pool2));
57
58     //0x52 is the Flash9 equivalent to DoAction
59     char init_pool[322] = { 0x01,0x00,0x00,0x00, /* Flag Parameter to 0x52 */
60                        0x00,                /* 0 terminated class name */
61                        0x10,0x00,0x2e,0x00, /* ABC file magic number */
62                        0x00,0x00,0x00,0x11, /* Constant Pool dec:
63     0 Integers, 0 Unsigned Integers, 0 Doubles, 17 Strings */
64
65       /* HERE FOLLOW THE 16 STRINGS OF THE CPOOL */
66       0x08,                            /* Len of 'stop_fla' (8) */
67       's','t','o','p','_','f','l','a', 
68       0x0c,                            /* Len of 'MainTimeline' */
69       'M','a','i','n','T','i','m','e','l','i','n','e',
70       0x0d,                            /* Len of 'flash.display' (13) */
71       'f','l','a','s','h','.','d','i','s','p','l','a','y', 
72       0x09,                                                    /* Len of 'MovieClip' (9) */
73       'M','o','v','i','e','C','l','i','p',
74       0x15,                                                    /* Len of 'stop_fla:MainTimeLine' (21) */
75       's','t','o','p','_','f','l','a',':','M','a','i','n','T','i','m','e','l','i','n','e',
76       0x06,                                                    /* Len of 'dostop' (6) */
77       'd','o','s','t','o','p',
78       0x00,   /* Empty string: ref. to the global namespace */
79       0x04,                                                    /* Len of 'stop' (4) */
80       's','t','o','p',
81       0x0e,                                                    /* Len of 'addFrameScript' (14) */
82       'a','d','d','F','r','a','m','e','S','c','r','i','p','t',
83       0x06,                                                    /* Len of 'Object' (6) */
84       'O','b','j','e','c','t',
85       0x0c,                                                    /* Len of 'flash.events' (12) */
86       'f','l','a','s','h','.','e','v','e','n','t','s',
87       0x0f,                                                    /* Len of 'EventDispatcher' (15) */
88       'E','v','e','n','t','D','i','s','p','a','t','c','h','e','r',
89       0x0d,                                                    /* Len of 'DisplayObject' (13) */
90       'D','i','s','p','l','a','y','O','b','j','e','c','t',
91       0x11,                                                    /* Len of 'InteractiveObject' (17) */
92       'I','n','t','e','r','a','c','t','i','v','e','O','b','j','e','c','t',
93       0x16,                                                    /* Len of 'DisplayObjectContainer' (22) */
94       'D','i','s','p','l','a','y','O','b','j','e','c','t','C','o','n','t','a','i','n','e','r',
95       0x06,                                                    /* Len of 'Sprite' (6) */
96       'S','p','r','i','t','e',
97
98       0x07, /* No. of NameSpaces (+1) */
99       /* HERE FOLLOW THE 6 NAMESPACES OF THE CPOOL*/
100       0x16,0x01, /* kind: CONSTANT_PackageNamespace, ref. to: stop_fla */
101       0x16,0x03, /* kind: CONSTANT_PackageNamespace, ref. to: flash.display */
102       0x18,0x05, /* kind: CONSTANT_ProtectedNamespace, ref. to: stop_fla:MainTimeline */
103       0x17,0x01, /* kind: CONSTANT_PackageInternalNs, ref. to: MainTimeline */
104       0x16,0x07, /* kind: CONSTANT_PackageNamespace, ref. to the global namespace */
105       0x16,0x0b, /* kind: CONSTANT_PackageNamespace, ref. to: flash.events */
106                               
107       0x00, /* NO NAMESPACE_SET */
108                               
109       0x0c, /* No. of MultiNames (+1) */
110       /* HERE FOLLOW THE 11 MULTINAMES OF THE CPOOL */
111       /*      1st value: Multiname kind (0x07 = Qualified Name, a multiname with 1 namespace, refers to a method)
112                       2nd value: refers to namespace name index in the namespace table
113                       3rd value: refers to method name index in the string table */
114       0x07,0x01,0x02, /* Ref. to method: stop_fla:MainTimeLine */
115       0x07,0x02,0x04, /* Ref. to method: flash.display:MovieClip */
116       0x07,0x04,0x06, /* Ref. to method: <4th namespace>:dostop */
117       0x07,0x05,0x08, /* Ref. to method: <global>:stop */
118       0x07,0x05,0x09, /* Ref. to method: <global>:addFrameScript */
119       0x07,0x05,0x0a, /* Ref. to method: <global>:Object */
120       0x07,0x06,0x0c, /* Ref. to method: flash.events:EventDispatcher */
121       0x07,0x02,0x0d, /* Ref. to method: flash.display:DisplayObject */
122       0x07,0x02,0x0e, /* Ref. to method: flash.display:InteractiveObject */
123       0x07,0x02,0x0f, /* Ref. to method: flash.display:DisplayObjectContainer */
124       0x07,0x02,0x10, /* Ref. to method: flash.display:Sprite */
125       /* END OF THE CPOOL */  
126                               
127       0x04,   /* Method count: there are 4 methods */
128       /* HERE FOLLOW THE 4 METHODS SIGNATURES */              
129       /* METHOD 1 */
130       0x00, /* No params (nor params types)*/
131       0x00, /* Any return type */
132       0x00, /* No method name */
133       0x00, /* Flags */
134       /* SAME FOR THE OTHER METHODS */
135       0x00,0x00,0x00,0x00,
136       0x00,0x00,0x00,0x00,
137       0x00,0x00,0x00,0x00,
138
139       0x00,   /* Metadata count: there is no metadata (nor metadata_info) */
140
141       0x01,   /* Class count: there is 1 class */
142       /* HERE FOLLOW THE INSTANCE DATA */
143       0x01, /* Name: ref. to multiname no. 1 (MainTimeline) */
144       0x02, /* Super_name (base class): ref. to multiname no. 2 (flash.display) */
145       0x08, /* Flags: 0x08 value indicates that it uses its protected namespace (and make next field exists) */
146       0x03, /* Ref. to namespace no. 3 (MainTimeline, protected namespace for this class) */
147       0x00, /* No. of interfaces: there are no interfaces (nor interface definition) */
148       0x02, /* Initializer index: ref. to method no. 2 */
149       0x01, /* No. of traits */
150       /* Trait section */     
151               0x03, /* Name: ref. to multiname no. 3 (stop) */
152               0x01, /* 0000: no attributes (nor metadata in the next subsection);
153                                then 1: type is Trait_Method */
154               /* Trait_method section */
155                       0x00, /* NO optimization for virtual calls */
156                       0x01, /* Ref. to method no. 1 */
157       /* HERE FOLLOW THE CLASS DATA */
158       0x00, /* Ref. to method no. 0 as static initializer for this class */
159       0x00, /* No. of traits for this class (no ones) */
160
161       0x01, /* Script count: there is 1 script */
162       /* HERE FOLLOW THE SCRIPT DATA */
163       0x03, /* Ref. to method no. 3, invoked prior to any other code in the script  */
164       0x01, /* No. of traits */
165       /* Trait section */
166       0x01, /* Name: ref. to multiname no. 1 (flash.display:MovieClip) */
167       0x04,  /* 0000: no attributes (nor metadata in the next subsection);
168                                 then 4: type is Trait_Class*/
169                       /* Trait_class section */
170                       0x01, /* Slot ID where the trait resides */
171                       0x00, /* Class index (there is only one class)*/
172
173       0x04, /* Method body count: there are 4 method bodies */
174       /* HERE FOLLOW THE METHOD BODY DATA */
175       0x00, /* Method index, ref. to method no. 0 */
176       0x01, /* Max stack slots the method can use */
177       0x01, /* No. of registers +1 the method can use: this one cannot use registers */
178       0x09,0x0a, /* Min and max scope depth the method can access*/
179       0x03, /* Code length (in bytes) */
180       /* The actual method code:
181                       this is the function stop_fla::MainTimeline$cinit()*/
182                       0xd0,0x30,0x47,
183       0x00, /* No. of exceptions (no exceptions) */
184       0x00, /* No. of traits (no traits) */
185
186       0x01, /* Method index, ref. to method no. 1 */
187       0x01, /* Max stack slots the method can use */
188       0x01, /* No. of registers +1 the method can use: this one cannot use registers */
189       0x0a,0x0b, /* Min and max scope depth the method can access*/
190       0x08, /* Code length (in bytes) */
191       /* The actual method code:
192                       this is the function stop_fla::dostop(), 
193                       the one that actually executes the stop() */
194       0xd0,0x30,0x5d,0x04,0x4f,0x04,0x00,0x47,
195       0x00, /* No. of exceptions (no exceptions) */
196       0x00, /* No. of traits (no traits) */
197     };
198
199     /* Header of stop_fla::MainTimeline() method */
200     char constructor_header[5] = {
201       0x02, /* Method index, ref. to method no. 2 */
202       0x03, /* Max stack slots the method can use */
203       0x01, /* No. of registers +1 the method can use: this one cannot use registers */
204       0x0a,0x0b /* Min and max scope depth the method can access*/
205     };
206
207     char constructor_first[5] = {
208       /* The actual method code:
209     This is the function stop_fla::MainTimeline()
210     Does: calls the super constructor for class #0 (MainTimeline),
211             then puts on the stack the object, namespace and name of the arguments
212             needed by the addFrameScripts, then calls the addFrameScripts
213              */
214       0xd0,   /* getlocal0 */
215       0x30,   /* pushscope */
216       0xd0,   /* getlocal0 */
217       0x49,   /* constructsuper */
218       0x00   /*              0 */
219     };
220
221     char constructor_frame_register[10] = {
222       0x5d,   /* findpropstrict */
223       0x05,   /*              'addFrameScript' */
224       0x25,   /* pushshort */
225       0x00,   /*              frame number for pushshort */
226   0x02,   /* NOP (hack to write u30 value of frame number when > 127) */
227       0x60,   /* getlex */
228       0x03,   /*              'stop_fla::dostop' */
229       0x4f,   /* callpropvoid */
230       0x05,   /*              'addFrameScript' */
231       0x02   /*              2*/
232     };
233
234     char constructor_return[3] = { 
235       0x47, /* returnvoid */
236       0x00, /* No. of exceptions (no exceptions) */
237       0x00, /* No. of traits (no traits) */
238     };
239
240     char script_init_pool[47] = {
241       0x03, /* Method index, ref. to method no. 3 */
242       0x02, /* Max stack slots the method can use */
243       0x01, /* No. of registers +1 the method can use: this one cannot use registers */
244       0x01,0x09, /* Min and max scope depth the method can access*/
245       0x27, /* Code length (in bytes) */
246       /* The actual method code:
247                       this is the function script0$init() */
248       0xd0,0x30,0x65,0x00,0x60,0x06,0x30,0x60,0x07,0x30,0x60,0x08,0x30,0x60,0x09,0x30,0x60,0x0a,0x30,0x60,
249       0x0b,0x30,0x60,0x02,0x30,0x60,0x02,0x58,0x00,0x1d,0x1d,0x1d,0x1d,0x1d,0x1d,0x1d,0x68,0x01,0x47,
250       0x00, /* No. of exceptions (no exceptions) */
251       0x00  /* No. of traits (no traits) */
252     };
253     /* END OF THE ABCFILE (Actionscript Byte Code) */
254
255     char code_len[5] = {0, };
256     unsigned clen_len = AVM2_uint32toU30((6 + sizeof(constructor_frame_register) * frame_nums), code_len);
257     unsigned i, offset = 0;
258
259     TAG *classInit = swf_InsertTagBefore(swf, swf->firstTag, 0x052);
260
261     /* Copy the environment Initialization code */
262     swf_SetBlock(classInit, (U8*)init_pool,sizeof(init_pool));
263     /* Copy Constructor Method header */
264     swf_SetBlock(classInit, (U8*)constructor_header, sizeof(constructor_header));
265     /* Add Code block size (u30) to the method header */
266     swf_SetBlock(classInit, (U8*)code_len, clen_len);
267
268     /* Copy Constructor Method body first part */
269     swf_SetBlock(classInit, (U8*)constructor_first, sizeof(constructor_first));
270
271     /* Register the callback for every frame */
272     for (i = 0; i < frame_nums; ++i) {
273         AVM2_uint32toU30(i,constructor_frame_register + 3); // Write current frame number
274         swf_SetBlock(classInit, (U8*)constructor_frame_register, sizeof(constructor_frame_register));
275     }
276
277     /* Copy Constructor method body ending, just a return */
278     swf_SetBlock(classInit, (U8*)constructor_return, sizeof(constructor_return));
279
280     /* Copy the script init code */
281     swf_SetBlock(classInit, (U8*)script_init_pool, sizeof(script_init_pool));
282 }
283