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