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