bugfix: doubles weren't parsed right.
[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 #define MAX_LOOKUP 1024   // make cross references in dumps
17
18 struct Action
19 {
20     int version;
21     char*name;
22     U8 op;
23     char*flags;
24 } static actions[] =
25 {
26 /*
27 f: frame (word)
28 u: url (string)
29 t: target (string)
30 l: label (string)
31 C: constant pool header (byte)
32 c: constant pool entry (string)
33 s: skip (byte) (number of actions)
34 m: method (byte) swf_GetUrl2:(0=none, 1=get, 2=post)/GotoFrame2:(1=play)
35 b: branch (word) (number of bytes)
36 p (push): type(byte), type=0:string, type=1:double
37 {: define function (name (string), num (word), params (num strings), codesize (word)
38 o: object (string)
39 r: register (byte)
40  */
41 {3,"End", 0x00, ""},
42 {3,"GotoFrame", 0x81, "f"},
43 {4,"GotoFrame2", 0x9f, "m"}, // -1 (/Movieclip:3)
44 {3,"GetUrl", 0x83, "ul"},
45 {4,"GetUrl2", 0x9a, "m"}, //-2
46 {3,"NextFrame", 0x04, ""},
47 {3,"PreviousFrame", 0x05, ""},
48 {3,"Play", 0x06, ""},
49 {3,"Stop", 0x07, ""},
50 {3,"ToggleQuality", 0x08, ""},
51 {3,"StopSounds", 0x09, ""},
52 {3,"WaitForFrame", 0x8a, "fs"},
53 {4,"WaitForFrame2", 0x8d, "s"}, // -1
54 {3,"SetTarget", 0x8b, "t"},
55 {4,"SetTarget2", 0x20, ""}, //-1
56 {3,"GotoLabel", 0x8c, "l"},
57 {4,"Add", 0x0a, ""}, //  -2, +1
58 {4,"Multiply", 0x0c, ""}, //  -2, +1
59 {4,"Divide", 0x0d, ""}, //  -2, +1
60 {4,"Subtract", 0x0b, ""}, //  -2, +1
61 {4,"Less", 0x0f, ""}, //  -2, +1
62 {4,"Equals", 0x0e, ""}, //  -2, +1
63 {4,"And", 0x10, ""}, //  -2, +1
64 {4,"Or", 0x11, ""}, //  -2, +1
65 {4,"Not", 0x12, ""}, //  -1, +1
66 {4,"StringAdd", 0x21, ""}, // -2,+1
67 {4,"StringLength", 0x14, ""}, // -1, +1
68 {4,"MBStringLength", 0x31, ""}, // -1, +1
69 {4,"StringEquals", 0x13, ""}, // -2, +1
70 {4,"StringLess", 0x29, ""}, //-2, +1
71 {4,"StringExtract", 0x15, ""}, // -3, +1
72 {4,"MBStringExtract", 0x35, ""}, //-3 +1
73 {4,"Push", 0x96, "p"}, //  +1
74 {4,"Pop", 0x17, ""}, //  -1
75 {4,"ToInteger", 0x18, ""}, // -1, +1
76 {4,"CharToAscii", 0x32, ""}, // -1, +1
77 {4,"AsciiToChar", 0x33, ""}, // -1, +1
78 {4,"MBCharToAscii", 0x36, ""}, // -1, +1
79 {4,"MBAsciiToChar", 0x37, ""}, // -1, +1
80 {4,"Jump", 0x99, "b"},
81 {4,"If", 0x9d, "b"}, // -1
82 {4,"Call", 0x9e, ""}, //-1 (frame label/number) (high bit is wrong.)
83 {4,"GetVariable", 0x1c,""}, // -1, +1
84 {4,"SetVariable", 0x1d,""}, // -2
85 {4,"GetProperty", 0x22,""}, //-2, +1
86 {4,"SetProperty", 0x23, ""}, // -3
87 {4,"RemoveSprite", 0x25, ""}, //-1
88 {4,"StartDrag", 0x27, ""}, // -2, -1, (-4)
89 {4,"EndDrag", 0x28, ""}, 
90 {4,"CloneSprite", 0x24, ""}, // -3
91 {4,"Trace", 0x26, ""}, //-1
92 {4,"GetTime", 0x34, ""}, //+1
93 {4,"RandomNumber", 0x30, ""}, //-1,+1
94 {5,"Modulo", 0x3f,""},
95 {5,"BitAnd", 0x60,""},
96 {5,"BitLShift", 0x63,""},
97 {5,"BitOr", 0x61,""},
98 {5,"BitRShift", 0x64,""},
99 {5,"BitURShift", 0x65,""},
100 {5,"BitXor", 0x62,""},//66?
101 {5,"Decrement", 0x51,""},
102 {5,"Increment", 0x50,""},
103 {5,"PushDuplicate", 0x4c,""},
104 {5,"StackSwap", 0x4d,""}, //?
105 {5,"StoreRegister", 0x87,"r"},
106 {5,"CallFunction", 0x3d,""},
107 {5,"DefineFunction", 0x9b, "{"},
108 {5,"Return", 0x3e,""},
109 {5,"GetMember", 0x4e,""},
110 {5,"SetMember", 0x4f,""},
111 {5,"CallMethod", 0x52,""},
112 {5,"Constantpool", 0x88, "Cc"},
113 {5,"DefineLocal", 0x3c,""},
114 {5,"DefineLocal2", 0x41,""},
115 {5,"Makehash", 0x43, ""}, //??
116 {5,"Delete", 0x3a,""}, //?
117 {5,"Delete2", 0x3b,""},
118 {5,"Enumerate", 0x46,""},
119 {5,"Equals2", 0x49,""},
120 {5,"InitArray", 0x42,""}, // InitObject?
121 {5,"NewMethod", 0x53,""}, //?
122 {5,"NewObject", 0x40,""},
123 {5,"TargetPath", 0x45,""}, //?
124 {5,"With", 0x94, "o"},
125 {5,"ToNumber", 0x4a,""}, //?
126 {5,"ToString", 0x4b,""}, //?
127 {5,"TypeOf", 0x44,""},
128 {5,"Add2", 0x47,""},
129 {5,"Less2", 0x48,""}
130 };
131 static int definedactions = sizeof(actions)/sizeof(struct Action);
132
133 ActionTAG* swf_ActionGet(TAG*tag) 
134 {
135     U8 op = 1;
136     int length;
137     ActionTAG tmp;
138     ActionTAG*action = &tmp;
139     U8*data;
140     while(op)
141     {
142         action->next = (ActionTAG*)malloc(sizeof(ActionTAG));
143         action->next->prev = action;
144         action->next->next = 0;
145         action = action->next;
146
147         op = swf_GetU8(tag);
148         if(op<0x80)
149             length = 0;
150         else
151             length = swf_GetU16(tag);
152
153         if(length) {
154             data = malloc(length);
155             swf_GetBlock(tag, data, length);
156         } else {
157           data = 0;
158         }
159         action->op = op;
160         action->len = length;
161         action->data = data;
162         action->parent = tag;
163     }
164     return tmp.next;
165 }
166
167 void swf_ActionFree(ActionTAG*action)
168 {
169     while(action)
170     {
171         ActionTAG*tmp;
172         if(action->data && action->data != action->tmp)
173             free(action->data);
174         tmp = action;
175         action=action->next;
176         free(tmp);
177     }
178 }
179
180 void swf_ActionSet(TAG*tag, ActionTAG*action)
181 {
182     while(action)
183     {
184         swf_SetU8(tag, action->op);
185         if(action->op & 128)
186           swf_SetU16(tag, action->len);
187
188         swf_SetBlock(tag, action->data, action->len);
189
190         action = action->next;
191     }
192 }
193
194 int OpAdvance(char c, char*data)
195 {
196     switch (c)
197     {
198         case 'f':
199             return 2;
200         case 'u':
201             return strlen(data)+1;
202         case 't':
203             return strlen(data)+1;
204         case 'l': 
205             return strlen(data)+1;
206         case 'c': 
207             return strlen(data)+1;
208         case 'C': 
209             return 2;
210         case 's':
211             return 1;
212         case 'm':
213             return 1;
214         case 'b':
215             return 2;
216         case 'p': {
217             U8 type = *data++;
218             if(type == 0) {
219                 return 1+strlen(data)+1; //string
220             } else if (type == 1) {
221                 return 1+4; //float
222             } else if (type == 2) {
223                 return 1+0; //NULL
224             } else if (type == 4) {
225                 return 1+1; //register
226             } else if (type == 5) {
227                 return 1+1; //bool
228             } else if (type == 6) {
229                 return 1+8; //double
230             } else if (type == 7) {
231                 return 1+4; //int
232             } else if (type == 8) {
233                 return 1+1; //lookup
234             } else return 1;
235             break;
236         }
237     }
238     return 0;
239 }
240
241 /* TODO: * this should be in swfdump.c */
242 void swf_DumpActions(ActionTAG*atag, char*prefix) 
243 {
244     int t;
245     U8*data;
246     char* cp;
247     int entry = 0;
248
249 #ifdef MAX_LOOKUP
250
251     char * lookup[MAX_LOOKUP];
252     memset(lookup,0x00,sizeof(lookup));
253
254 #endif
255
256    if (!prefix)
257         prefix="";
258
259     while(atag)
260     {
261         U8 poollen = 0;
262         for(t=0;t<definedactions;t++)
263             if(actions[t].op == atag->op)
264                 break;
265
266         if(t==definedactions) {
267             printf("%s (%5d bytes) action: %02x\n", prefix, atag->len, atag->op);
268             atag = atag->next;
269             continue;
270         }
271         printf("%s (%5d bytes) action: %s", prefix, atag->len, actions[t].name);
272         cp = actions[t].flags;
273         data = atag->data;
274         if(atag->len) //TODO: check for consistency: should we have a length?
275         while(*cp)
276         {
277             switch(*cp)
278             {
279                 case 'f': { //frame
280                     printf(" %d", data[0]+256*data[1]);
281                 } break;
282                 case 'u': {
283                     printf(" URL:\"%s\"", data);
284                 } break;
285                 case 't': {
286                     printf(" Target:\"%s\"", data);
287                 } break;
288                 case 'l': {
289                     printf(" Label:\"%s\"", data);
290                 } break;
291                 case 'c': {
292                     printf(" String:\"%s\"", data);
293 #ifdef MAX_LOOKUP
294                     if (entry<MAX_LOOKUP)
295                       lookup[entry++] = strdup(data);
296 #endif
297                 } break;
298                 case 'C': {
299                     poollen = *data;
300                     entry = 0;
301                     printf("(%d entries)", poollen);
302                 } break;
303                 case 's': {
304                     printf(" +%d", *data);
305                 } break;
306                 case 'm': {
307                     //m: method (byte) url:(0=none, 1=get, 2=datat)/gf2:(1=play)
308                     printf(" %d", *data);
309                 } break;
310                 case 'b': {
311                     printf(" %d", data[0]+256*(signed char)data[1]);
312                 } break;
313                 case 'p': {
314                     U8 type = *data;
315                     unsigned char*value = data+1;
316                     if(type == 0) {
317                         printf(" String:\"%s\"", value);
318                     } else if (type == 1) {
319                         U32 f = value[0]+(value[1]<<8)+
320                                 (value[2]<<16)+(value[3]<<24);
321                         printf(" Float:%f", *(float*)&f);
322                     } else if (type == 2) {
323                         printf(" NULL");
324                     } else if (type == 4) {
325                         printf(" register:%d", *value);
326                     } else if (type == 5) {
327                         printf(" bool:%s", *value?"true":"false");
328                     } else if (type == 6) {
329                         U8 a[8];
330                         int t;
331                         memcpy(&a[4],value,4);
332                         memcpy(a,&value[4],4);
333 #ifdef WORDS_BIGENDIAN
334                         for(t=0;t<4;t++) {
335                             U8 tmp = a[t];
336                             a[t]=a[7-t];
337                             a[7-t] = tmp;
338                         }
339 #endif
340                         printf(" double:%f", *(double*)a);
341                     } else if (type == 7) {
342                         printf(" int:%d", value[0]+(value[1]<<8)+
343                                           (value[2]<<16)+(value[3]<<24));
344                     } else if (type == 8) {
345                         printf(" Lookup:%d", *value);
346 #ifdef MAX_LOOKUP
347                         if (lookup[*value])
348                           printf(" (\"%s\")",lookup[*value]);
349 #endif
350                     } else {
351                         printf(" UNKNOWN[%02x]",type);
352                     }
353                 } break;
354             }
355             data += OpAdvance(*cp, data);
356             if((*cp!='c' || !poollen) &&
357                (*cp!='p' || !(data<&atag->data[atag->len])))
358                 cp++;
359             if(poollen)
360                 poollen--;
361         }
362
363         if(data < atag->data + atag->len)
364         {
365             int nl = ((atag->data+atag->len)-data);
366             int t;
367             printf(" (remainder of %d bytes:\"", nl);
368             for(t=0;t<nl;t++) {
369                 if(data[t]<32)
370                     printf("\\%d",data[t]);
371                 else
372                     printf("%c", data[t]);
373             }
374             printf("\")");
375         }
376         printf("\n");
377         atag = atag->next;
378     }
379
380 #ifdef MAX_LOOKUP
381   for (t=0;t<MAX_LOOKUP;t++) if (lookup[t]) free(lookup[t]);
382 #endif
383 }
384
385 static const char TYPE_URL = 1;
386 static const char TYPE_TARGET = 2;
387 static const char TYPE_STRING = 4;
388
389 int swf_ActionEnumerate(ActionTAG*atag, char*(*callback)(char*), int type)
390 {
391     int t;
392     U8*data;
393     char* cp;
394     int count = 0;
395     while(atag)
396     {
397         U8 poollen = 0;
398         for(t=0;t<definedactions;t++)
399             if(actions[t].op == atag->op)
400                 break;
401
402         if(t==definedactions) {
403             // unknown actiontag
404             atag = atag->next;
405             count++;
406             continue;
407         }
408         cp = actions[t].flags;
409         data = atag->data;
410         if(atag->len) {
411             while(*cp) {
412                 U8 * replacepos = 0;
413                 int replacelen = 0;
414                 U8 * replacement = 0;
415                 switch(*cp)
416                 {
417                     case 'u': {
418                         if(type&TYPE_URL)
419                         {
420                             replacelen = strlen(data);
421                             replacepos = data;
422                             replacement = callback(data); // may be null
423                         }
424                     } break;
425                     case 't': {
426                         if(type&TYPE_TARGET)
427                         {
428                             replacelen = strlen(data);
429                             replacepos = data;
430                             replacement = callback(data); // may be null
431                         }
432                     } break;
433                     case 'c': {
434                         if(type&TYPE_STRING)
435                         {
436                             replacelen = strlen(data);
437                             replacepos = data;
438                             replacement = callback(data); // may be null
439                         }
440                     } break;
441                     case 'C': {
442                         poollen = (*data);
443                     } break;
444                     case 'o': {
445                     } break;
446                     case 'p': {
447                         U8 datatype = *data;
448                         char*value = &data[1];
449                         if(datatype == 0) { //string
450                             if(type&TYPE_STRING)
451                             {
452                                 replacelen = strlen(value);
453                                 replacepos = value;
454                                 replacement = callback(value); // may be null
455                             }
456                         } else if (datatype == 8) { //lookup
457                         }
458                     } break;
459                 }
460                 data += OpAdvance(*cp, data);
461                 if(*cp!='c' || !poollen)
462                     cp++;
463                 if(poollen)
464                     poollen--;
465
466                 if(replacement)
467                 {
468                     int newlen = strlen(replacement);
469                     char * newdata = malloc(atag->len - replacelen + newlen);
470                     int rpos = replacepos - atag->data;
471                     memcpy(newdata, atag->data, rpos);
472                     memcpy(&newdata[rpos], replacement, newlen);
473                     memcpy(&newdata[rpos+newlen], &replacepos[replacelen],
474                             &data[atag->len] - &replacepos[replacelen]);
475                     free(atag->data);
476                     atag->data = newdata;
477                     data = &atag->data[rpos+newlen+1];
478                 }
479             }
480         }
481         atag = atag->next;
482         count ++;
483     }
484     return count;
485 }
486
487 void swf_ActionEnumerateTargets(ActionTAG*atag, char*(*callback)(char*))
488 {
489     swf_ActionEnumerate(atag, callback, TYPE_TARGET);
490 }
491 void swf_ActionEnumerateStrings(ActionTAG*atag, char*(*callback)(char*))
492 {
493     swf_ActionEnumerate(atag, callback, TYPE_STRING);
494 }
495 void swf_ActionEnumerateURLs(ActionTAG*atag, char*(*callback)(char*))
496 {
497     swf_ActionEnumerate(atag, callback, TYPE_URL);
498 }
499
500 static ActionTAG * currentatag;
501
502 ActionTAG* swf_ActionStart()
503 {
504     currentatag = (ActionTAG*)malloc(sizeof(ActionTAG));
505     currentatag->prev = 0;
506     currentatag->parent = 0;
507     currentatag->data = 0;
508     currentatag->len = 0;
509     return currentatag;
510 }
511
512 void swf_ActionEnd()
513 {
514     currentatag->prev->next = 0;
515     free(currentatag);
516 }
517
518 void swf_AddActionTAG(U8 op, U8*data, U16 len)
519 {
520     currentatag->next = (ActionTAG*)malloc(sizeof(ActionTAG));
521     currentatag->next->prev = currentatag;
522     currentatag->parent = 0;
523     currentatag->data = data;
524     currentatag->len = len;
525     currentatag->op = op;
526     currentatag = currentatag->next;
527 }
528
529 ActionMarker action_setMarker()
530 {
531     ActionMarker m;
532     m.atag = currentatag;
533     return m;
534 }
535
536 int inline ActionTagSize(ActionTAG*atag)
537 {
538     return (atag->op&0x80)?3+(atag->len):1+0;
539 }
540
541
542 #define ACTION_END            0x00
543 #define ACTION_NEXTFRAME      0x04
544 #define ACTION_PREVIOUSFRAME  0x05
545 #define ACTION_PLAY           0x06
546 #define ACTION_STOP           0x07
547 #define ACTION_TOGGLEQUALITY  0x08
548 #define ACTION_STOPSOUNDS     0x09
549 #define ACTION_ADD            0x0a
550 #define ACTION_SUBTRACT       0x0b
551 #define ACTION_MULTIPLY       0x0c
552 #define ACTION_DIVIDE         0x0d
553 #define ACTION_EQUALS         0x0e
554 #define ACTION_LESS           0x0f
555 #define ACTION_AND            0x10
556 #define ACTION_OR             0x11
557 #define ACTION_NOT            0x12
558 #define ACTION_STRINGEQUALS   0x13
559 #define ACTION_STRINGLENGTH   0x14
560 #define ACTION_STRINGEXTRACT  0x15
561 #define ACTION_POP            0x17
562 #define ACTION_TOINTEGER      0x18
563 #define ACTION_GETVARIABLE    0x1c
564 #define ACTION_SETVARIABLE    0x1d
565 #define ACTION_SETTARGET2     0x20
566 #define ACTION_STRINGADD      0x21
567 #define ACTION_GETPROPERTY    0x22
568 #define ACTION_SETPROPERTY    0x23
569 #define ACTION_CLONESPRITE    0x24
570 #define ACTION_REMOVESPRITE   0x25
571 #define ACTION_TRACE          0x26
572 #define ACTION_STARTDRAG      0x27
573 #define ACTION_ENDDRAG        0x28
574 #define ACTION_STRINGLESS     0x29
575 #define ACTION_RANDOMNUMBER   0x30
576 #define ACTION_MBSTRINGLENGTH 0x31
577 #define ACTION_CHARTOASCII    0x32
578 #define ACTION_ASCIITOCHAR    0x33
579 #define ACTION_GETTIME        0x34
580 #define ACTION_MBSTRINGEXTRACT 0x35
581 #define ACTION_MBCHARTOASCII  0x36
582 #define ACTION_MBASCIITOCHAR  0x37
583 #define ACTION_DELETE         0x3a
584 #define ACTION_DELETE2        0x3b
585 #define ACTION_DEFINELOCAL    0x3c
586 #define ACTION_CALLFUNCTION   0x3d
587 #define ACTION_RETURN         0x3e
588 #define ACTION_MODULO         0x3f
589 #define ACTION_NEWOBJECT      0x40
590 #define ACTION_DEFINELOCAL2   0x41
591 #define ACTION_INITARRAY      0x42
592 #define ACTION_MAKEHASH       0x43
593 #define ACTION_TYPEOF         0x44
594 #define ACTION_TARGETPATH     0x45
595 #define ACTION_ENUMERATE      0x46
596 #define ACTION_ADD2           0x47
597 #define ACTION_LESS2          0x48
598 #define ACTION_EQUALS2        0x49
599 #define ACTION_TONUMBER       0x4a
600 #define ACTION_TOSTRING       0x4b
601 #define ACTION_PUSHDUPLICATE  0x4c
602 #define ACTION_STACKSWAP      0x4d
603 #define ACTION_GETMEMBER      0x4e
604 #define ACTION_SETMEMBER      0x4f
605 #define ACTION_INCREMENT      0x50
606 #define ACTION_DECREMENT      0x51
607 #define ACTION_CALLMETHOD     0x52
608 #define ACTION_NEWMETHOD      0x53
609 #define ACTION_BITAND         0x60
610 #define ACTION_BITOR          0x61
611 #define ACTION_BITXOR         0x62
612 #define ACTION_BITLSHIFT      0x63
613 #define ACTION_BITRSHIFT      0x64
614 #define ACTION_BITURSHIFT     0x65
615 #define ACTION_GOTOFRAME      0x81
616 #define ACTION_GETURL         0x83
617 #define ACTION_STOREREGISTER  0x87
618 #define ACTION_CONSTANTPOOL   0x88
619 #define ACTION_WAITFORFRAME   0x8a
620 #define ACTION_SETTARGET      0x8b
621 #define ACTION_GOTOLABEL      0x8c
622 #define ACTION_WAITFORFRAME2  0x8d
623 #define ACTION_WITH           0x94
624 #define ACTION_PUSH           0x96
625 #define ACTION_JUMP           0x99
626 #define ACTION_GETURL2        0x9a
627 #define ACTION_DEFINEFUNCTION 0x9b
628 #define ACTION_IF             0x9d
629 #define ACTION_CALL           0x9e
630 #define ACTION_GOTOFRAME2     0x9f
631
632 void action_fixjump(ActionMarker m1, ActionMarker m2)
633 {
634     ActionTAG* a1 = m1.atag;
635     ActionTAG* a2 = m2.atag;
636     ActionTAG* a;
637     int len = 0;
638     int oplen = 0;
639     a = a1;
640     
641     a = a->next; //first one is free
642     while(a && a!=a2)
643     {
644         len += ActionTagSize(a);
645         oplen ++;
646         a = a->next;
647     }
648     if(!a)
649     { len = 0;
650       oplen = 0;
651       a = a2;
652       while(a && a!=a1) {
653           len -= ActionTagSize(a);
654           oplen --;
655           a = a->next;
656       }
657       if(!a) {
658           fprintf(stderr, "action_fixjump: couldn't find second tag\n");
659           return;
660       }
661       len -= ActionTagSize(a);
662       oplen --;
663     }
664
665     if (a1->op == ACTION_IF || a1->op == ACTION_JUMP) 
666     {
667         *(U16*)(a1->data) = SWAP16(len);
668     }
669     else if(a1->op == ACTION_WAITFORFRAME)
670     {
671         ((U8*)(a1->data))[2] = oplen;
672     }
673     else if(a1->op == ACTION_WAITFORFRAME2)
674     {
675         ((U8*)(a1->data))[0] = oplen;
676     }
677     
678 }
679
680
681 void action_NextFrame() {swf_AddActionTAG(ACTION_NEXTFRAME, 0, 0);}
682 void action_PreviousFrame() {swf_AddActionTAG(ACTION_PREVIOUSFRAME, 0, 0);}
683 void action_Play() {swf_AddActionTAG(ACTION_PLAY, 0, 0);}
684 void action_Stop() {swf_AddActionTAG(ACTION_STOP, 0, 0);}
685 void action_ToggleQuality() {swf_AddActionTAG(ACTION_TOGGLEQUALITY, 0, 0);}
686 void action_StopSounds() {swf_AddActionTAG(ACTION_STOPSOUNDS, 0, 0);}
687 void action_Add() {swf_AddActionTAG(ACTION_ADD, 0, 0);}
688 void action_Subtract() {swf_AddActionTAG(ACTION_SUBTRACT, 0, 0);}
689 void action_Multiply() {swf_AddActionTAG(ACTION_MULTIPLY, 0, 0);}
690 void action_Divide() {swf_AddActionTAG(ACTION_DIVIDE, 0, 0);}
691 void action_Equals() {swf_AddActionTAG(ACTION_EQUALS, 0, 0);}
692 void action_Less() {swf_AddActionTAG(ACTION_LESS, 0, 0);}
693 void action_And() {swf_AddActionTAG(ACTION_AND, 0, 0);}
694 void action_Or() {swf_AddActionTAG(ACTION_OR, 0, 0);}
695 void action_Not() {swf_AddActionTAG(ACTION_NOT, 0, 0);}
696 void action_StringEquals() {swf_AddActionTAG(ACTION_STRINGEQUALS, 0, 0);}
697 void action_StringLength() {swf_AddActionTAG(ACTION_STRINGLENGTH, 0, 0);}
698 void action_StringExtract() {swf_AddActionTAG(ACTION_STRINGEXTRACT, 0, 0);}
699 void action_Pop() {swf_AddActionTAG(ACTION_POP, 0, 0);}
700 void action_ToInteger() {swf_AddActionTAG(ACTION_TOINTEGER, 0, 0);}
701 void action_GetVariable() {swf_AddActionTAG(ACTION_GETVARIABLE, 0, 0);}
702 void action_SetVariable() {swf_AddActionTAG(ACTION_SETVARIABLE, 0, 0);}
703 void action_SetTarget2() {swf_AddActionTAG(ACTION_SETTARGET2, 0, 0);}
704 void action_StringAdd() {swf_AddActionTAG(ACTION_STRINGADD, 0, 0);}
705 void action_GetProperty() {swf_AddActionTAG(ACTION_GETPROPERTY, 0, 0);}
706 void action_SetProperty() {swf_AddActionTAG(ACTION_SETPROPERTY, 0, 0);}
707 void action_CloneSprite() {swf_AddActionTAG(ACTION_CLONESPRITE, 0, 0);}
708 void action_RemoveSprite() {swf_AddActionTAG(ACTION_REMOVESPRITE, 0, 0);}
709 void action_Trace() {swf_AddActionTAG(ACTION_TRACE, 0, 0);}
710 void action_StartDrag() {swf_AddActionTAG(ACTION_STARTDRAG, 0, 0);}
711 void action_EndDrag() {swf_AddActionTAG(ACTION_ENDDRAG, 0, 0);}
712 void action_StringLess() {swf_AddActionTAG(ACTION_STRINGLESS, 0, 0);}
713 void action_RandomNumber() {swf_AddActionTAG(ACTION_RANDOMNUMBER, 0, 0);}
714 void action_MBStringLength() {swf_AddActionTAG(ACTION_MBSTRINGLENGTH, 0, 0);}
715 void action_CharToAscii() {swf_AddActionTAG(ACTION_CHARTOASCII, 0, 0);}
716 void action_AsciiToChar() {swf_AddActionTAG(ACTION_ASCIITOCHAR, 0, 0);}
717 void action_GetTime() {swf_AddActionTAG(ACTION_GETTIME, 0, 0);}
718 void action_MBStringExtract() {swf_AddActionTAG(ACTION_MBSTRINGEXTRACT, 0, 0);}
719 void action_MBCharToAscii() {swf_AddActionTAG(ACTION_MBCHARTOASCII, 0, 0);}
720 void action_MBAsciiToChar() {swf_AddActionTAG(ACTION_MBASCIITOCHAR, 0, 0);}
721 void action_Delete() {swf_AddActionTAG(ACTION_DELETE, 0, 0);}
722 void action_Delete2() {swf_AddActionTAG(ACTION_DELETE2, 0, 0);}
723 void action_DefineLocal() {swf_AddActionTAG(ACTION_DEFINELOCAL, 0, 0);}
724 void action_CallFunction() {swf_AddActionTAG(ACTION_CALLFUNCTION, 0, 0);}
725 void action_Return() {swf_AddActionTAG(ACTION_RETURN, 0, 0);}
726 void action_Modulo() {swf_AddActionTAG(ACTION_MODULO, 0, 0);}
727 void action_NewObject() {swf_AddActionTAG(ACTION_NEWOBJECT, 0, 0);}
728 void action_DefineLocal2() {swf_AddActionTAG(ACTION_DEFINELOCAL2, 0, 0);}
729 void action_InitArray() {swf_AddActionTAG(ACTION_INITARRAY, 0, 0);}
730 void action_Makehash() {swf_AddActionTAG(ACTION_MAKEHASH, 0, 0);}
731 void action_TypeOf() {swf_AddActionTAG(ACTION_TYPEOF, 0, 0);}
732 void action_TargetPath() {swf_AddActionTAG(ACTION_TARGETPATH, 0, 0);}
733 void action_Enumerate() {swf_AddActionTAG(ACTION_ENUMERATE, 0, 0);}
734 void action_Add2() {swf_AddActionTAG(ACTION_ADD2, 0, 0);}
735 void action_Less2() {swf_AddActionTAG(ACTION_LESS2, 0, 0);}
736 void action_Equals2() {swf_AddActionTAG(ACTION_EQUALS2, 0, 0);}
737 void action_ToNumber() {swf_AddActionTAG(ACTION_TONUMBER, 0, 0);}
738 void action_ToString() {swf_AddActionTAG(ACTION_TOSTRING, 0, 0);}
739 void action_PushDuplicate() {swf_AddActionTAG(ACTION_PUSHDUPLICATE, 0, 0);}
740 void action_StackSwap() {swf_AddActionTAG(ACTION_STACKSWAP, 0, 0);}
741 void action_GetMember() {swf_AddActionTAG(ACTION_GETMEMBER, 0, 0);}
742 void action_SetMember() {swf_AddActionTAG(ACTION_SETMEMBER, 0, 0);}
743 void action_Increment() {swf_AddActionTAG(ACTION_INCREMENT, 0, 0);}
744 void action_Decrement() {swf_AddActionTAG(ACTION_DECREMENT, 0, 0);}
745 void action_CallMethod() {swf_AddActionTAG(ACTION_CALLMETHOD, 0, 0);}
746 void action_NewMethod() {swf_AddActionTAG(ACTION_NEWMETHOD, 0, 0);}
747 void action_BitAnd() {swf_AddActionTAG(ACTION_BITAND, 0, 0);}
748 void action_BitOr() {swf_AddActionTAG(ACTION_BITOR, 0, 0);}
749 void action_BitXor() {swf_AddActionTAG(ACTION_BITXOR, 0, 0);}
750 void action_BitLShift() {swf_AddActionTAG(ACTION_BITLSHIFT, 0, 0);}
751 void action_BitRShift() {swf_AddActionTAG(ACTION_BITRSHIFT, 0, 0);}
752 void action_BitURShift() {swf_AddActionTAG(ACTION_BITURSHIFT, 0, 0);}
753 void action_Call() {swf_AddActionTAG(ACTION_CALL, 0, 0);}
754 void action_End() {swf_AddActionTAG(ACTION_END, 0, 0);}
755 void action_GotoFrame(U16 frame) 
756 {
757     *(U16*)currentatag->tmp = SWAP16(frame);
758     swf_AddActionTAG(ACTION_GOTOFRAME, (U8*)currentatag->tmp, 2);
759 }
760 void action_Jump(U16 branch) 
761 {
762     *(U16*)currentatag->tmp = SWAP16(branch);
763     swf_AddActionTAG(ACTION_JUMP, (U8*)currentatag->tmp, 2);
764 }
765 void action_If(U16 branch) 
766 {
767     *(U16*)currentatag->tmp = SWAP16(branch);
768     swf_AddActionTAG(ACTION_IF, (U8*)currentatag->tmp, 2);
769 }
770 void action_StoreRegister(U8 reg) 
771 {
772     *(U8*)currentatag->tmp = reg;
773     swf_AddActionTAG(ACTION_STOREREGISTER, (U8*)currentatag->tmp, 1);
774 }
775 void action_GotoFrame2(U8 method) 
776 {
777     *(U8*)currentatag->tmp = method;
778     swf_AddActionTAG(ACTION_GOTOFRAME2, (U8*)currentatag->tmp, 1);
779 }
780 void action_GetUrl2(U8 method) 
781 {
782     *(U8*)currentatag->tmp = method;
783     swf_AddActionTAG(ACTION_GETURL2, (U8*)currentatag->tmp, 1);
784 }
785 void action_WaitForFrame2(U8 skip) 
786 {
787     *(U8*)currentatag->tmp = skip;
788     swf_AddActionTAG(ACTION_WAITFORFRAME2, (U8*)currentatag->tmp, 1);
789 }
790 void action_WaitForFrame(U16 frame, U8 skip) 
791 {
792     *(U16*)currentatag->tmp = SWAP16(frame);
793     *(U8*)&currentatag->tmp[2] = skip;
794     swf_AddActionTAG(ACTION_WAITFORFRAME, (U8*)currentatag->tmp, 3);
795 }
796 void action_SetTarget(char* target)
797 {
798     char*ptr = strdup(target);
799     swf_AddActionTAG(ACTION_SETTARGET, (U8*)ptr, strlen(ptr)+1);
800 }
801 void action_PushNULL() 
802 {
803     *(U8*)currentatag->tmp = 2; //NULL
804     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 1);
805 }
806 void action_PushBoolean(char c) 
807 {
808     *(U8*)currentatag->tmp = 5; //bool
809     *(U8*)&currentatag->tmp[1] = c;
810     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 2);
811 }
812 void action_PushRegister(U8 reg) 
813 {
814     *(U8*)currentatag->tmp = 4; //register
815     *(U8*)&currentatag->tmp[1] = reg;
816     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 2);
817 }
818 void action_PushLookup(U8 index) 
819 {
820     *(U8*)currentatag->tmp = 8; //lookup
821     *(U8*)&currentatag->tmp[1] = index;
822     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 2);
823 }
824 void action_PushString(char*str) 
825 {
826     int l = strlen(str);
827     char*ptr = (char*)malloc(l+2);
828     ptr[0] = 0; // string
829     strcpy(&ptr[1], str);
830     swf_AddActionTAG(ACTION_PUSH, (U8*)ptr, l+2);
831 }
832 void action_PushFloat(float f)
833 {
834     char*ptr = (char*)malloc(5);
835     U32 fd = *(U32*)&f;
836     ptr[0] = 1; //float
837     ptr[1]  = fd;
838     ptr[2]  = fd>>8;
839     ptr[3]  = fd>>16;
840     ptr[4]  = fd>>24;
841     swf_AddActionTAG(ACTION_PUSH, (U8*)ptr, 5);
842 }
843 void action_PushDouble(double d) 
844 {
845     char*ptr = (char*)malloc(9);
846     U8*dd = (U8*)&d;
847     ptr[0] = 6; //double
848 #ifdef WORDS_BIGENDIAN
849     ptr[1] = dd[7];ptr[2] = dd[6];
850     ptr[3] = dd[5];ptr[4] = dd[4];
851     ptr[5] = dd[3];ptr[6] = dd[2];
852     ptr[7] = dd[1];ptr[8] = dd[0];
853 #else
854     ptr[1] = dd[0];ptr[2] = dd[1];
855     ptr[3] = dd[2];ptr[4] = dd[3];
856     ptr[5] = dd[4];ptr[6] = dd[5];
857     ptr[7] = dd[6];ptr[8] = dd[7];
858 #endif
859     swf_AddActionTAG(ACTION_PUSH, (U8*)ptr, 9);
860 }
861 void action_PushInt(int i)
862 {
863     *(U8*)currentatag->tmp = 7; //int
864     currentatag->tmp[1] = i;
865     currentatag->tmp[2] = i>>8;
866     currentatag->tmp[3] = i>>16;
867     currentatag->tmp[4] = i>>24;
868     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 5);
869 }
870 void action_GotoLabel(char* label)
871 {
872     char*ptr = strdup(label);
873     swf_AddActionTAG(ACTION_GOTOLABEL, (U8*)ptr, strlen(ptr));
874 }
875 void action_GetUrl(char* url, char* label) 
876 {
877     int l1= strlen(url);
878     int l2= strlen(label);
879     char*ptr = malloc(l1+l2+2);
880     strcpy(ptr, url);
881     strcpy(&ptr[l1+1], label);
882     swf_AddActionTAG(ACTION_GETURL, ptr, l1+l2+2);
883 }
884 //TODO:
885 void action_DefineFunction(U8*data, int len) {}
886 void action_Constantpool(char* constantpool) {}
887 void action_With(char*object) {}
888
889 /*
890   Properties:
891
892   _X 0
893   _Y 1
894   _xscale 2
895   _yscale 3
896   _currentframe 4
897   _totalframes 5
898   _alpha 6
899   _visible 7
900   _width 8
901   _height 9
902   _rotation 10
903   _target 11
904   _framesloaded 12
905   _name 13
906   _droptarget 14
907   _url 15
908   _highquality 16
909   _focusrect 17
910   _soundbuftime 18
911   _quality* 19
912   _xmouse* 20
913   _ymouse* 21
914 */