Module for handling Flash 3/4/5 Actionscript.
[swftools.git] / lib / modules / swfaction.c
1 /* swfaction.c
2
3    Actionscript generation and parsing routines
4    
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
9  
10    This file is distributed under the GPL, see file COPYING for details 
11
12 */
13
14 #include "../rfxswf.h"
15 #include "../args.h"
16
17 struct Action
18 {
19     int version;
20     char*name;
21     U8 op;
22     char*flags;
23 } static actions[] =
24 {
25 /*
26 f: frame (word)
27 u: url (string)
28 t: target (string)
29 l: label (string)
30 c: constant pool (string)
31 s: skip (byte) (number of actions)
32 m: method (byte) url:(0=none, 1=get, 2=post)/gf2:(1=play)
33 b: branch (word) (number of bytes)
34 p: type(byte), type=0:string, type=1:double
35 {: define function (name (string), num (word), params (num strings), codesize (word)
36 o: object (string)
37 r: register (byte)
38  */
39 {3,"End", 0x00, ""},
40 {3,"GotoFrame", 0x81, "f"},
41 {4,"GotoFrame2", 0x9f, "m"}, // -1 (/Movieclip:3)
42 {3,"GetUrl", 0x83, "ul"},
43 {4,"GetUrl2", 0x9a, "m"}, //-2
44 {3,"NextFrame", 0x04, ""},
45 {3,"PreviousFrame", 0x05, ""},
46 {3,"Play", 0x06, ""},
47 {3,"Stop", 0x07, ""},
48 {3,"ToggleQuality", 0x08, ""},
49 {3,"StopSounds", 0x09, ""},
50 {3,"WaitForFrame", 0x8a, "fs"},
51 {4,"WaitForFrame2", 0x8d, "s"}, // -1
52 {3,"SetTarget", 0x8b, "t"},
53 {4,"SetTarget2", 0x20, ""}, //-1
54 {3,"GotoLabel", 0x8c, "l"},
55 {4,"Add", 0x0a, ""}, //  -2, +1
56 {4,"Multiply", 0x0c, ""}, //  -2, +1
57 {4,"Divide", 0x0d, ""}, //  -2, +1
58 {4,"Subtract", 0x0b, ""}, //  -2, +1
59 {4,"Less", 0x0f, ""}, //  -2, +1
60 {4,"Equals", 0x0e, ""}, //  -2, +1
61 {4,"And", 0x10, ""}, //  -2, +1
62 {4,"Or", 0x11, ""}, //  -2, +1
63 {4,"Not", 0x12, ""}, //  -1, +1
64 {4,"StringAdd", 0x21, ""}, // -2,+1
65 {4,"StringLength", 0x14, ""}, // -1, +1
66 {4,"MBStringLength", 0x31}, // -1, +1
67 {4,"StringEquals", 0x13, ""}, // -2, +1
68 {4,"StringLess", 0x29, ""}, //-2, +1
69 {4,"StringExtract", 0x15, ""}, // -3, +1
70 {4,"MBStringExtract", 0x35, ""}, //-3 +1
71 {4,"Push", 0x96, "p"}, //  +1
72 {4,"Pop", 0x17, ""}, //  -1
73 {4,"ToInteger", 0x18, ""}, // -1, +1
74 {4,"CharToAscii", 0x32, ""}, // -1, +1
75 {4,"AsciiToChar", 0x33, ""}, // -1, +1
76 {4,"MBCharToAscii", 0x36, ""}, // -1, +1
77 {4,"MBAsciiToChar", 0x37, ""}, // -1, +1
78 {4,"Jump", 0x99, "b"},
79 {4,"If", 0x9d, "b"}, // -1
80 {4,"Call", 0x9e, ""}, //-1 (frame label/number)
81 {4,"GetVariable", 0x1c,""}, // -1, +1
82 {4,"SetVariable", 0x1d,""}, // -2
83 {4,"GetProperty", 0x22,""}, //-2, +1
84 {4,"SetProperty", 0x23, ""}, // -3
85 {4,"RemoveSprite", 0x25, ""}, //-1
86 {4,"StartDrag", 0x27, ""}, // -2, -1, (-4)
87 {4,"EndDrag", 0x28, ""}, 
88 {4,"CloneSprite", 0x24, ""}, // -3
89 {4,"Trace", 0x26, ""}, //-1
90 {4,"GetTime", 0x34, ""}, //+1
91 {4,"RandomNumber", 0x30, ""}, //-1,+1
92
93 {5,"Modulo", 0x3f,""},
94 {5,"BitAnd", 0x60,""},
95 {5,"BitLShift", 0x63,""},
96 {5,"BitOr", 0x61,""},
97 {5,"BitRShift", 0x64,""},
98 {5,"BitURShift", 0x65,""},
99 {5,"BitXor", 0x62,""},//66?
100 {5,"Decrement", 0x51,""},
101 {5,"Increment", 0x50,""},
102 {5,"PushDuplicate", 0x4c,""},
103 {5,"StackSwap", 0x4d,""}, //?
104 {5,"StoreRegister", 0x87,"r"},
105 {5,"CallFunction", 0x3d,""},
106 {5,"DefineFunction", 0x9b, "{"},
107 {5,"Return", 0x3e,""},
108 {5,"GetMember", 0x4e,""},
109 {5,"SetMember", 0x4f,""},
110 {5,"CallMethod", 0x52,""},
111 {5,"Constantpool", 0x88, "c"},
112 {5,"DefineLocal", 0x3c,""},
113 {5,"DefineLocal2", 0x41,""},
114 {5,"Makehash", 0x43, ""}, //??
115 {5,"Delete", 0x3a,""}, //?
116 {5,"Delete2", 0x3b,""},
117 {5,"Enumerate", 0x46,""},
118 {5,"Equals2", 0x49,""},
119 {5,"InitArray", 0x42,""}, // InitObject?
120 {5,"NewMethod", 0x53,""}, //?
121 {5,"NewObject", 0x40,""},
122 {5,"TargetPath", 0x45,""}, //?
123 {5,"With", 0x94, "o"},
124 {5,"ToNumber", 0x4a,""}, //?
125 {5,"ToString", 0x4b,""}, //?
126 {5,"TypeOf", 0x44,""},
127 {5,"Add2", 0x47,""},
128 {5,"Less2", 0x48,""}
129 };
130 static int definedactions = sizeof(actions)/sizeof(struct Action);
131
132 ActionTAG* GetActions(TAG*tag) 
133 {
134     U8 op = 1;
135     int length;
136     ActionTAG tmp;
137     ActionTAG*atag = &tmp;
138     U8*data;
139     while(op)
140     {
141         int pos;
142         atag->next = (ActionTAG*)malloc(sizeof(ActionTAG));
143         atag->next->prev = atag;
144         atag->next->next = 0;
145         atag = atag->next;
146
147         op = GetU8(tag);
148         if(op<0x80)
149             length = 0;
150         else
151             length = GetU16(tag);
152
153         if(length) {
154             int t;
155             data = malloc(length);
156             for(t=0;t<length;t++)
157                 data[t] = GetU8(tag);
158         } else {
159           data = 0;
160         }
161         atag->op = op;
162         atag->len = length;
163         atag->data = data;
164     }
165     return tmp.next;
166 }
167
168 /* TODO: this should be in swfdump.c */
169 void DumpActions(ActionTAG*atag, char*prefix) 
170 {
171     U8 op;
172     int t;
173     U8*data;
174     char* cp;
175     if(!prefix) 
176         prefix="";
177     while(atag)
178     {
179         for(t=0;t<definedactions;t++)
180             if(actions[t].op == atag->op)
181                 break;
182
183         if(t==definedactions) {
184             printf("%s (%5d bytes) action: %02x\n", prefix, atag->len, op);
185             atag = atag->next;
186             continue;
187         }
188         printf("%s (%5d bytes) action: %s", prefix, atag->len, actions[t].name);
189         cp = actions[t].flags;
190         data = atag->data;
191         if(atag->len) //TODO: check for consistency: should we have a length?
192         while(*cp)
193         {
194             switch(*cp)
195             {
196                 case 'f': {
197                     printf(" %d", *(U16*)data);data+=2; //FIXME: le/be
198                 } break;
199                 case 'u': {
200                     printf(" URL:\"%s\"", data);data+=strlen(data)+1;
201                 } break;
202                 case 't': {
203                     printf(" Target:\"%s\"", data);data+=strlen(data)+1;
204                 } break;
205                 case 'l': {
206                     printf(" Label:\"%s\"", data);data+=strlen(data)+1;
207                 } break;
208                 case 'c': {
209                     printf(" Constant Pool:\"%s\"", data);data+=strlen(data)+1;
210                 } break;
211                 case 's': {
212                     printf(" +%d", data);data++;
213                 } break;
214                 case 'm': {
215 //m: method (byte) url:(0=none, 1=get, 2=datat)/gf2:(1=play)
216                     printf(" %d", data);data++;
217                 } break;
218                 case 'b': {
219                     printf(" %d", *(U16*)data);data+=2;
220                 } break;
221                 case 'p': {
222                     U8 type = *data++;
223                     if(type == 0) {
224                         printf(" String:\"%s\"", data);data+=strlen(data)+1;
225                     } else if (type == 1) {
226                         printf(" Double:\"%f\"", *(float*)data);data+=4;
227                     } else if (type == 2) {
228                         printf(" NULL");
229                     } else if (type == 4) {
230                         printf(" register:%d", data++);
231                     } else if (type == 5) {
232                         printf(" %s", data++?"true":"false");
233                     } else if (type == 6) {
234                         printf(" %f", *(double*)data);  data+=8;
235                     } else if (type == 7) {
236                         printf(" %d", *(int*)data); data+=4;
237                     } else if (type == 8) {
238                         printf(" Lookup:%d", data++);
239                     }
240                 } break;
241             
242             }
243             cp++;
244         }
245
246         if(data < atag->data + atag->len)
247         {
248             int nl = ((atag->data+atag->len)-data);
249             int t;
250             printf(" remainder of %d bytes:\"", nl);
251             for(t=0;t<nl;t++) {
252                 if(data[t]<32)
253                     printf("\\%d",data[t]);
254                 else
255                     printf("%c", data[t]);
256             }
257             printf("\"");
258         }
259         printf("\n");
260         atag = atag->next;
261     }
262 }
263