add HAVE_SYS_BSDTYPES_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 #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: codesize (word) 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 {5/*6?*/,"Less3?", 0x67,""},
131 {5/*6?*/,"GetMembers?", 0x55,""}
132 };
133 static int definedactions = sizeof(actions)/sizeof(struct Action);
134
135 ActionTAG* swf_ActionGet(TAG*tag) 
136 {
137     U8 op = 1;
138     int length;
139     ActionTAG tmp;
140     ActionTAG*action = &tmp;
141     U8*data;
142     while(op)
143     {
144         action->next = (ActionTAG*)malloc(sizeof(ActionTAG));
145         action->next->prev = action;
146         action->next->next = 0;
147         action = action->next;
148
149         op = swf_GetU8(tag);
150         if(op<0x80)
151             length = 0;
152         else
153             length = swf_GetU16(tag);
154
155         if(length) {
156             data = malloc(length);
157             swf_GetBlock(tag, data, length);
158         } else {
159           data = 0;
160         }
161         action->op = op;
162         action->len = length;
163         action->data = data;
164         action->parent = tag;
165     }
166     return tmp.next;
167 }
168
169 void swf_ActionFree(ActionTAG*action)
170 {
171     while(action)
172     {
173         ActionTAG*tmp;
174         if(action->data && action->data != action->tmp)
175             free(action->data);
176         tmp = action;
177         action=action->next;
178         free(tmp);
179     }
180 }
181
182 void swf_ActionSet(TAG*tag, ActionTAG*action)
183 {
184     while(action)
185     {
186         swf_SetU8(tag, action->op);
187         if(action->op & 128)
188           swf_SetU16(tag, action->len);
189
190         swf_SetBlock(tag, action->data, action->len);
191
192         action = action->next;
193     }
194 }
195
196 int OpAdvance(char c, U8*data)
197 {
198     switch (c)
199     {
200         case 'f':
201             return 2;
202         case 'u':
203             return strlen(data)+1;
204         case 't':
205             return strlen(data)+1;
206         case 'l': 
207             return strlen(data)+1;
208         case 'c': 
209             return strlen(data)+1;
210         case 'C': 
211             return 2;
212         case 's':
213             return 1;
214         case 'm':
215             return 1;
216         case 'b':
217             return 2;
218         case 'r':
219             return 1;
220         case 'p': {
221             U8 type = *data++;
222             if(type == 0) {
223                 return 1+strlen(data)+1; //string
224             } else if (type == 1) {
225                 return 1+4; //float
226             } else if (type == 2) {
227                 return 1+0; //NULL
228             } else if (type == 4) {
229                 return 1+1; //register
230             } else if (type == 5) {
231                 return 1+1; //bool
232             } else if (type == 6) {
233                 return 1+8; //double
234             } else if (type == 7) {
235                 return 1+4; //int
236             } else if (type == 8) {
237                 return 1+1; //lookup
238             } else return 1;
239             break;
240         }
241         case 'o': {
242             return 2;
243         }
244         case '{': {
245             U16 num;
246             U16 codesize;
247             U8* odata = data;
248             int t;
249             while(*data++); //name
250             num = (*data++)*256; //num
251             num += (*data++);
252             for(t=0;t<num;t++)
253                 while(*data++); //param
254             codesize = (*data++)*256; //num
255             codesize += (*data++);
256             return data-odata;
257         }
258     }
259     return 0;
260 }
261 #define ATAG_FULLLENGTH(atag) ((atag)->len + 1 + ((atag)->op&0x80?2:0))
262 #define MAX_LEVELS 16
263 /* TODO: * this should be in swfdump.c */
264 void swf_DumpActions(ActionTAG*atag, char*prefix) 
265 {
266     int t;
267     U8*data;
268     char* cp;
269     int entry = 0;
270     char spaces[MAX_LEVELS*4+1];
271     struct {
272         char*text;
273         int count;
274     } counter[MAX_LEVELS];
275     int countpos = 0;
276 #ifdef MAX_LOOKUP
277     char * lookup[MAX_LOOKUP];
278     memset(lookup,0x00,sizeof(lookup));
279 #endif
280     memset(spaces, 32, sizeof(spaces));
281     spaces[sizeof(spaces)-1] = 0;
282
283    if (!prefix)
284         prefix="";
285
286     while(atag)
287     {
288         char*indent = &spaces[sizeof(spaces)-1-countpos*4];
289         U8 poollen = 0;
290         for(t=0;t<definedactions;t++)
291             if(actions[t].op == atag->op)
292                 break;
293
294         if(t==definedactions) {
295             printf("%s (%5d bytes) action:%s unknown[%02x]", prefix, atag->len, indent, atag->op);
296         } else {
297             printf("%s (%5d bytes) action:%s %s", prefix, atag->len, indent, actions[t].name);
298         }
299         data = atag->data;
300         if(atag->len && t!=definedactions) //TODO: check for consistency: should we have a length?
301         {
302           cp = actions[t].flags;
303           while(*cp)
304           {
305               switch(*cp)
306               {
307                   case 'f': { //frame
308                       printf(" %d", data[0]+256*data[1]);
309                   } break;
310                   case 'u': {
311                       printf(" URL:\"%s\"", data);
312                   } break;
313                   case 't': {
314                       printf(" Target:\"%s\"", data);
315                   } break;
316                   case 'l': {
317                       printf(" Label:\"%s\"", data);
318                   } break;
319                   case 'c': {
320                       printf(" String:\"%s\"", data);
321 #ifdef MAX_LOOKUP
322                       if (entry<MAX_LOOKUP)
323                         lookup[entry++] = strdup(data);
324 #endif
325                   } break;
326                   case 'C': {
327                       poollen = *data;
328                       entry = 0;
329                       printf("(%d entries)", poollen);
330                   } break;
331                   case 's': {
332                       printf(" +%d", *data);
333                   } break;
334                   case 'm': {
335                       //m: method (byte) url:(0=none, 1=get, 2=datat)/gf2:(1=play)
336                       printf(" %d", *data);
337                   } break;
338                   case '{': {
339                       U16 num;
340                       U16 codesize;
341                       int s = 0;
342                       int t;
343                       printf(" %s(", data);
344                       while(data[s++]); //name
345                       num = (data[s++]); //num
346                       num += (data[s++])*256;
347                       for(t=0;t<num;t++) {
348                           printf("%s",data);
349                           if(t<num-1)
350                               printf(", ");
351                           while(data[s++]); //param
352                       }
353                       printf(")");
354                       codesize = (data[s++]); //num
355                       codesize += (data[s++])*256;
356                       printf(" codesize:%d ",codesize);
357                       printf("\n%s                       %s{", prefix, indent);
358                       if(countpos>=15) {
359                           printf("Error: nested too deep\n");
360                           continue;
361                       }
362                       counter[countpos].text = "}";
363                       counter[countpos].count = codesize + ATAG_FULLLENGTH(atag);
364                       countpos++;
365                   } break;
366                   case 'o': {
367                       int t;
368                       U16 codesize = data[0]+256*data[1];
369                       printf(" codesize:%d ", codesize);
370
371                       /* the following tries to find the "string"
372                          the flash documentation speaks of- I've
373                          never actually seen one yet. -mk */
374                       for(t=2;t<atag->len;t++)
375                           printf("[%02x]", atag->data[t]);
376
377                       printf("\n%s                       %s{", prefix, indent);
378                       if(countpos>=15) {
379                           printf("Error: nested too deep\n");
380                           continue;
381                       }
382                       counter[countpos].text = "}";
383                       counter[countpos].count = codesize + ATAG_FULLLENGTH(atag);
384                       countpos++;
385                   } break;
386                   case 'b': {
387                       printf(" %d", data[0]+256*(signed char)data[1]);
388                   } break;
389                   case 'r': {
390                       printf(" %d", data[0]);
391                   } break;
392                   case 'p': {
393                       U8 type = *data;
394                       unsigned char*value = data+1;
395                       if(type == 0) {
396                           printf(" String:\"%s\"", value);
397                       } else if (type == 1) {
398                           U32 f = value[0]+(value[1]<<8)+
399                                   (value[2]<<16)+(value[3]<<24);
400                           printf(" Float:%f", *(float*)&f);
401                       } else if (type == 2) {
402                           printf(" NULL");
403                       } else if (type == 4) {
404                           printf(" register:%d", *value);
405                       } else if (type == 5) {
406                           printf(" bool:%s", *value?"true":"false");
407                       } else if (type == 6) {
408                           U8 a[8];
409                           int t;
410                           memcpy(&a[4],value,4);
411                           memcpy(a,&value[4],4);
412 #ifdef WORDS_BIGENDIAN
413                           for(t=0;t<4;t++) {
414                               U8 tmp = a[t];
415                               a[t]=a[7-t];
416                               a[7-t] = tmp;
417                           }
418 #endif
419                           printf(" double:%f", *(double*)a);
420                       } else if (type == 7) {
421                           printf(" int:%d", value[0]+(value[1]<<8)+
422                                             (value[2]<<16)+(value[3]<<24));
423                       } else if (type == 8) {
424                           printf(" Lookup:%d", *value);
425 #ifdef MAX_LOOKUP
426                           if (lookup[*value])
427                             printf(" (\"%s\")",lookup[*value]);
428 #endif
429                       } else {
430                           printf(" UNKNOWN[%02x]",type);
431                       }
432                   } break;
433               }
434               data += OpAdvance(*cp, data);
435               if((*cp!='c' || !poollen) &&
436                  (*cp!='p' || !(data<&atag->data[atag->len])))
437                   cp++;
438               if(poollen)
439                   poollen--;
440           }
441         }
442
443         if(data < atag->data + atag->len)
444         {
445             int nl = ((atag->data+atag->len)-data);
446             int t;
447             printf(" (remainder of %d bytes:\"", nl);
448             for(t=0;t<nl;t++) {
449                 if(data[t]<32)
450                     printf("\\%d",data[t]);
451                 else
452                     printf("%c", data[t]);
453             }
454             printf("\")");
455         }
456         printf("\n");
457
458         for(t=0;t<countpos;t++) {
459             counter[t].count -= ATAG_FULLLENGTH(atag);
460             if(counter[t].count < 0) {
461                 printf("===== Error: Oplength errors =====\n");
462                 countpos = 0;
463                 break;
464             }
465         }
466
467         while(countpos && !counter[countpos-1].count)
468         {
469             printf("%s                   %s%s\n", 
470                 prefix, indent, counter[countpos-1].text);
471             indent += 4;
472             countpos--;
473         }
474
475         atag = atag->next;
476     }
477
478 #ifdef MAX_LOOKUP
479   for (t=0;t<MAX_LOOKUP;t++) if (lookup[t]) free(lookup[t]);
480 #endif
481 }
482
483 static const char TYPE_URL = 1;
484 static const char TYPE_TARGET = 2;
485 static const char TYPE_STRING = 4;
486
487 int swf_ActionEnumerate(ActionTAG*atag, char*(*callback)(char*), int type)
488 {
489     int t;
490     U8*data;
491     char* cp;
492     int count = 0;
493     while(atag)
494     {
495         U8 poollen = 0;
496         for(t=0;t<definedactions;t++)
497             if(actions[t].op == atag->op)
498                 break;
499
500         if(t==definedactions) {
501             // unknown actiontag
502             atag = atag->next;
503             count++;
504             continue;
505         }
506         cp = actions[t].flags;
507         data = atag->data;
508         if(atag->len) {
509             while(*cp) {
510                 U8 * replacepos = 0;
511                 int replacelen = 0;
512                 U8 * replacement = 0;
513                 switch(*cp)
514                 {
515                     case 'u': {
516                         if(type&TYPE_URL)
517                         {
518                             replacelen = strlen(data);
519                             replacepos = data;
520                             replacement = callback(data); // may be null
521                         }
522                     } break;
523                     case 't': {
524                         if(type&TYPE_TARGET)
525                         {
526                             replacelen = strlen(data);
527                             replacepos = data;
528                             replacement = callback(data); // may be null
529                         }
530                     } break;
531                     case 'c': {
532                         if(type&TYPE_STRING)
533                         {
534                             replacelen = strlen(data);
535                             replacepos = data;
536                             replacement = callback(data); // may be null
537                         }
538                     } break;
539                     case 'C': {
540                         poollen = (*data);
541                     } break;
542                     case 'o': {
543                     } break;
544                     case 'p': {
545                         U8 datatype = *data;
546                         char*value = &data[1];
547                         if(datatype == 0) { //string
548                             if(type&TYPE_STRING)
549                             {
550                                 replacelen = strlen(value);
551                                 replacepos = value;
552                                 replacement = callback(value); // may be null
553                             }
554                         } else if (datatype == 8) { //lookup
555                         }
556                     } break;
557                 }
558                 data += OpAdvance(*cp, data);
559                 if(*cp!='c' || !poollen)
560                     cp++;
561                 if(poollen)
562                     poollen--;
563
564                 if(replacement)
565                 {
566                     int newlen = strlen(replacement);
567                     char * newdata = malloc(atag->len - replacelen + newlen);
568                     int rpos = replacepos - atag->data;
569                     memcpy(newdata, atag->data, rpos);
570                     memcpy(&newdata[rpos], replacement, newlen);
571                     memcpy(&newdata[rpos+newlen], &replacepos[replacelen],
572                             &data[atag->len] - &replacepos[replacelen]);
573                     free(atag->data);
574                     atag->data = newdata;
575                     data = &atag->data[rpos+newlen+1];
576                 }
577             }
578         }
579         atag = atag->next;
580         count ++;
581     }
582     return count;
583 }
584
585 void swf_ActionEnumerateTargets(ActionTAG*atag, char*(*callback)(char*))
586 {
587     swf_ActionEnumerate(atag, callback, TYPE_TARGET);
588 }
589 void swf_ActionEnumerateStrings(ActionTAG*atag, char*(*callback)(char*))
590 {
591     swf_ActionEnumerate(atag, callback, TYPE_STRING);
592 }
593 void swf_ActionEnumerateURLs(ActionTAG*atag, char*(*callback)(char*))
594 {
595     swf_ActionEnumerate(atag, callback, TYPE_URL);
596 }
597
598 static ActionTAG * currentatag;
599
600 ActionTAG* swf_ActionStart()
601 {
602     currentatag = (ActionTAG*)malloc(sizeof(ActionTAG));
603     currentatag->prev = 0;
604     currentatag->parent = 0;
605     currentatag->data = 0;
606     currentatag->len = 0;
607     return currentatag;
608 }
609
610 void swf_ActionEnd()
611 {
612     currentatag->prev->next = 0;
613     free(currentatag);
614 }
615
616 void swf_AddActionTAG(U8 op, U8*data, U16 len)
617 {
618     currentatag->next = (ActionTAG*)malloc(sizeof(ActionTAG));
619     currentatag->next->prev = currentatag;
620     currentatag->parent = 0;
621     currentatag->data = data;
622     currentatag->len = len;
623     currentatag->op = op;
624     currentatag = currentatag->next;
625 }
626
627 ActionMarker action_setMarker()
628 {
629     ActionMarker m;
630     m.atag = currentatag;
631     return m;
632 }
633
634 int inline ActionTagSize(ActionTAG*atag)
635 {
636     return (atag->op&0x80)?3+(atag->len):1+0;
637 }
638
639
640 #define ACTION_END            0x00
641 #define ACTION_NEXTFRAME      0x04
642 #define ACTION_PREVIOUSFRAME  0x05
643 #define ACTION_PLAY           0x06
644 #define ACTION_STOP           0x07
645 #define ACTION_TOGGLEQUALITY  0x08
646 #define ACTION_STOPSOUNDS     0x09
647 #define ACTION_ADD            0x0a
648 #define ACTION_SUBTRACT       0x0b
649 #define ACTION_MULTIPLY       0x0c
650 #define ACTION_DIVIDE         0x0d
651 #define ACTION_EQUALS         0x0e
652 #define ACTION_LESS           0x0f
653 #define ACTION_AND            0x10
654 #define ACTION_OR             0x11
655 #define ACTION_NOT            0x12
656 #define ACTION_STRINGEQUALS   0x13
657 #define ACTION_STRINGLENGTH   0x14
658 #define ACTION_STRINGEXTRACT  0x15
659 #define ACTION_POP            0x17
660 #define ACTION_TOINTEGER      0x18
661 #define ACTION_GETVARIABLE    0x1c
662 #define ACTION_SETVARIABLE    0x1d
663 #define ACTION_SETTARGET2     0x20
664 #define ACTION_STRINGADD      0x21
665 #define ACTION_GETPROPERTY    0x22
666 #define ACTION_SETPROPERTY    0x23
667 #define ACTION_CLONESPRITE    0x24
668 #define ACTION_REMOVESPRITE   0x25
669 #define ACTION_TRACE          0x26
670 #define ACTION_STARTDRAG      0x27
671 #define ACTION_ENDDRAG        0x28
672 #define ACTION_STRINGLESS     0x29
673 #define ACTION_RANDOMNUMBER   0x30
674 #define ACTION_MBSTRINGLENGTH 0x31
675 #define ACTION_CHARTOASCII    0x32
676 #define ACTION_ASCIITOCHAR    0x33
677 #define ACTION_GETTIME        0x34
678 #define ACTION_MBSTRINGEXTRACT 0x35
679 #define ACTION_MBCHARTOASCII  0x36
680 #define ACTION_MBASCIITOCHAR  0x37
681 #define ACTION_DELETE         0x3a
682 #define ACTION_DELETE2        0x3b
683 #define ACTION_DEFINELOCAL    0x3c
684 #define ACTION_CALLFUNCTION   0x3d
685 #define ACTION_RETURN         0x3e
686 #define ACTION_MODULO         0x3f
687 #define ACTION_NEWOBJECT      0x40
688 #define ACTION_DEFINELOCAL2   0x41
689 #define ACTION_INITARRAY      0x42
690 #define ACTION_MAKEHASH       0x43
691 #define ACTION_TYPEOF         0x44
692 #define ACTION_TARGETPATH     0x45
693 #define ACTION_ENUMERATE      0x46
694 #define ACTION_ADD2           0x47
695 #define ACTION_LESS2          0x48
696 #define ACTION_EQUALS2        0x49
697 #define ACTION_TONUMBER       0x4a
698 #define ACTION_TOSTRING       0x4b
699 #define ACTION_PUSHDUPLICATE  0x4c
700 #define ACTION_STACKSWAP      0x4d
701 #define ACTION_GETMEMBER      0x4e
702 #define ACTION_SETMEMBER      0x4f
703 #define ACTION_INCREMENT      0x50
704 #define ACTION_DECREMENT      0x51
705 #define ACTION_CALLMETHOD     0x52
706 #define ACTION_NEWMETHOD      0x53
707 #define ACTION_BITAND         0x60
708 #define ACTION_BITOR          0x61
709 #define ACTION_BITXOR         0x62
710 #define ACTION_BITLSHIFT      0x63
711 #define ACTION_BITRSHIFT      0x64
712 #define ACTION_BITURSHIFT     0x65
713 #define ACTION_GOTOFRAME      0x81
714 #define ACTION_GETURL         0x83
715 #define ACTION_STOREREGISTER  0x87
716 #define ACTION_CONSTANTPOOL   0x88
717 #define ACTION_WAITFORFRAME   0x8a
718 #define ACTION_SETTARGET      0x8b
719 #define ACTION_GOTOLABEL      0x8c
720 #define ACTION_WAITFORFRAME2  0x8d
721 #define ACTION_WITH           0x94
722 #define ACTION_PUSH           0x96
723 #define ACTION_JUMP           0x99
724 #define ACTION_GETURL2        0x9a
725 #define ACTION_DEFINEFUNCTION 0x9b
726 #define ACTION_IF             0x9d
727 #define ACTION_CALL           0x9e
728 #define ACTION_GOTOFRAME2     0x9f
729
730 void action_fixjump(ActionMarker m1, ActionMarker m2)
731 {
732     ActionTAG* a1 = m1.atag;
733     ActionTAG* a2 = m2.atag;
734     ActionTAG* a;
735     int len = 0;
736     int oplen = 0;
737     a = a1;
738     
739     a = a->next; //first one is free
740     while(a && a!=a2)
741     {
742         len += ActionTagSize(a);
743         oplen ++;
744         a = a->next;
745     }
746     if(!a)
747     { len = 0;
748       oplen = 0;
749       a = a2;
750       while(a && a!=a1) {
751           len -= ActionTagSize(a);
752           oplen --;
753           a = a->next;
754       }
755       if(!a) {
756           fprintf(stderr, "action_fixjump: couldn't find second tag\n");
757           return;
758       }
759       len -= ActionTagSize(a);
760       oplen --;
761     }
762
763     if (a1->op == ACTION_IF || a1->op == ACTION_JUMP) 
764     {
765         *(U16*)(a1->data) = SWAP16(len);
766     }
767     else if(a1->op == ACTION_WAITFORFRAME)
768     {
769         ((U8*)(a1->data))[2] = oplen;
770     }
771     else if(a1->op == ACTION_WAITFORFRAME2)
772     {
773         ((U8*)(a1->data))[0] = oplen;
774     }
775     
776 }
777
778
779 void action_NextFrame() {swf_AddActionTAG(ACTION_NEXTFRAME, 0, 0);}
780 void action_PreviousFrame() {swf_AddActionTAG(ACTION_PREVIOUSFRAME, 0, 0);}
781 void action_Play() {swf_AddActionTAG(ACTION_PLAY, 0, 0);}
782 void action_Stop() {swf_AddActionTAG(ACTION_STOP, 0, 0);}
783 void action_ToggleQuality() {swf_AddActionTAG(ACTION_TOGGLEQUALITY, 0, 0);}
784 void action_StopSounds() {swf_AddActionTAG(ACTION_STOPSOUNDS, 0, 0);}
785 void action_Add() {swf_AddActionTAG(ACTION_ADD, 0, 0);}
786 void action_Subtract() {swf_AddActionTAG(ACTION_SUBTRACT, 0, 0);}
787 void action_Multiply() {swf_AddActionTAG(ACTION_MULTIPLY, 0, 0);}
788 void action_Divide() {swf_AddActionTAG(ACTION_DIVIDE, 0, 0);}
789 void action_Equals() {swf_AddActionTAG(ACTION_EQUALS, 0, 0);}
790 void action_Less() {swf_AddActionTAG(ACTION_LESS, 0, 0);}
791 void action_And() {swf_AddActionTAG(ACTION_AND, 0, 0);}
792 void action_Or() {swf_AddActionTAG(ACTION_OR, 0, 0);}
793 void action_Not() {swf_AddActionTAG(ACTION_NOT, 0, 0);}
794 void action_StringEquals() {swf_AddActionTAG(ACTION_STRINGEQUALS, 0, 0);}
795 void action_StringLength() {swf_AddActionTAG(ACTION_STRINGLENGTH, 0, 0);}
796 void action_StringExtract() {swf_AddActionTAG(ACTION_STRINGEXTRACT, 0, 0);}
797 void action_Pop() {swf_AddActionTAG(ACTION_POP, 0, 0);}
798 void action_ToInteger() {swf_AddActionTAG(ACTION_TOINTEGER, 0, 0);}
799 void action_GetVariable() {swf_AddActionTAG(ACTION_GETVARIABLE, 0, 0);}
800 void action_SetVariable() {swf_AddActionTAG(ACTION_SETVARIABLE, 0, 0);}
801 void action_SetTarget2() {swf_AddActionTAG(ACTION_SETTARGET2, 0, 0);}
802 void action_StringAdd() {swf_AddActionTAG(ACTION_STRINGADD, 0, 0);}
803 void action_GetProperty() {swf_AddActionTAG(ACTION_GETPROPERTY, 0, 0);}
804 void action_SetProperty() {swf_AddActionTAG(ACTION_SETPROPERTY, 0, 0);}
805 void action_CloneSprite() {swf_AddActionTAG(ACTION_CLONESPRITE, 0, 0);}
806 void action_RemoveSprite() {swf_AddActionTAG(ACTION_REMOVESPRITE, 0, 0);}
807 void action_Trace() {swf_AddActionTAG(ACTION_TRACE, 0, 0);}
808 void action_StartDrag() {swf_AddActionTAG(ACTION_STARTDRAG, 0, 0);}
809 void action_EndDrag() {swf_AddActionTAG(ACTION_ENDDRAG, 0, 0);}
810 void action_StringLess() {swf_AddActionTAG(ACTION_STRINGLESS, 0, 0);}
811 void action_RandomNumber() {swf_AddActionTAG(ACTION_RANDOMNUMBER, 0, 0);}
812 void action_MBStringLength() {swf_AddActionTAG(ACTION_MBSTRINGLENGTH, 0, 0);}
813 void action_CharToAscii() {swf_AddActionTAG(ACTION_CHARTOASCII, 0, 0);}
814 void action_AsciiToChar() {swf_AddActionTAG(ACTION_ASCIITOCHAR, 0, 0);}
815 void action_GetTime() {swf_AddActionTAG(ACTION_GETTIME, 0, 0);}
816 void action_MBStringExtract() {swf_AddActionTAG(ACTION_MBSTRINGEXTRACT, 0, 0);}
817 void action_MBCharToAscii() {swf_AddActionTAG(ACTION_MBCHARTOASCII, 0, 0);}
818 void action_MBAsciiToChar() {swf_AddActionTAG(ACTION_MBASCIITOCHAR, 0, 0);}
819 void action_Delete() {swf_AddActionTAG(ACTION_DELETE, 0, 0);}
820 void action_Delete2() {swf_AddActionTAG(ACTION_DELETE2, 0, 0);}
821 void action_DefineLocal() {swf_AddActionTAG(ACTION_DEFINELOCAL, 0, 0);}
822 void action_CallFunction() {swf_AddActionTAG(ACTION_CALLFUNCTION, 0, 0);}
823 void action_Return() {swf_AddActionTAG(ACTION_RETURN, 0, 0);}
824 void action_Modulo() {swf_AddActionTAG(ACTION_MODULO, 0, 0);}
825 void action_NewObject() {swf_AddActionTAG(ACTION_NEWOBJECT, 0, 0);}
826 void action_DefineLocal2() {swf_AddActionTAG(ACTION_DEFINELOCAL2, 0, 0);}
827 void action_InitArray() {swf_AddActionTAG(ACTION_INITARRAY, 0, 0);}
828 void action_Makehash() {swf_AddActionTAG(ACTION_MAKEHASH, 0, 0);}
829 void action_TypeOf() {swf_AddActionTAG(ACTION_TYPEOF, 0, 0);}
830 void action_TargetPath() {swf_AddActionTAG(ACTION_TARGETPATH, 0, 0);}
831 void action_Enumerate() {swf_AddActionTAG(ACTION_ENUMERATE, 0, 0);}
832 void action_Add2() {swf_AddActionTAG(ACTION_ADD2, 0, 0);}
833 void action_Less2() {swf_AddActionTAG(ACTION_LESS2, 0, 0);}
834 void action_Equals2() {swf_AddActionTAG(ACTION_EQUALS2, 0, 0);}
835 void action_ToNumber() {swf_AddActionTAG(ACTION_TONUMBER, 0, 0);}
836 void action_ToString() {swf_AddActionTAG(ACTION_TOSTRING, 0, 0);}
837 void action_PushDuplicate() {swf_AddActionTAG(ACTION_PUSHDUPLICATE, 0, 0);}
838 void action_StackSwap() {swf_AddActionTAG(ACTION_STACKSWAP, 0, 0);}
839 void action_GetMember() {swf_AddActionTAG(ACTION_GETMEMBER, 0, 0);}
840 void action_SetMember() {swf_AddActionTAG(ACTION_SETMEMBER, 0, 0);}
841 void action_Increment() {swf_AddActionTAG(ACTION_INCREMENT, 0, 0);}
842 void action_Decrement() {swf_AddActionTAG(ACTION_DECREMENT, 0, 0);}
843 void action_CallMethod() {swf_AddActionTAG(ACTION_CALLMETHOD, 0, 0);}
844 void action_NewMethod() {swf_AddActionTAG(ACTION_NEWMETHOD, 0, 0);}
845 void action_BitAnd() {swf_AddActionTAG(ACTION_BITAND, 0, 0);}
846 void action_BitOr() {swf_AddActionTAG(ACTION_BITOR, 0, 0);}
847 void action_BitXor() {swf_AddActionTAG(ACTION_BITXOR, 0, 0);}
848 void action_BitLShift() {swf_AddActionTAG(ACTION_BITLSHIFT, 0, 0);}
849 void action_BitRShift() {swf_AddActionTAG(ACTION_BITRSHIFT, 0, 0);}
850 void action_BitURShift() {swf_AddActionTAG(ACTION_BITURSHIFT, 0, 0);}
851 void action_Call() {swf_AddActionTAG(ACTION_CALL, 0, 0);}
852 void action_End() {swf_AddActionTAG(ACTION_END, 0, 0);}
853 void action_GotoFrame(U16 frame) 
854 {
855     *(U16*)currentatag->tmp = SWAP16(frame);
856     swf_AddActionTAG(ACTION_GOTOFRAME, (U8*)currentatag->tmp, 2);
857 }
858 void action_Jump(U16 branch) 
859 {
860     *(U16*)currentatag->tmp = SWAP16(branch);
861     swf_AddActionTAG(ACTION_JUMP, (U8*)currentatag->tmp, 2);
862 }
863 void action_If(U16 branch) 
864 {
865     *(U16*)currentatag->tmp = SWAP16(branch);
866     swf_AddActionTAG(ACTION_IF, (U8*)currentatag->tmp, 2);
867 }
868 void action_StoreRegister(U8 reg) 
869 {
870     *(U8*)currentatag->tmp = reg;
871     swf_AddActionTAG(ACTION_STOREREGISTER, (U8*)currentatag->tmp, 1);
872 }
873 void action_GotoFrame2(U8 method) 
874 {
875     *(U8*)currentatag->tmp = method;
876     swf_AddActionTAG(ACTION_GOTOFRAME2, (U8*)currentatag->tmp, 1);
877 }
878 void action_GetUrl2(U8 method) 
879 {
880     *(U8*)currentatag->tmp = method;
881     swf_AddActionTAG(ACTION_GETURL2, (U8*)currentatag->tmp, 1);
882 }
883 void action_WaitForFrame2(U8 skip) 
884 {
885     *(U8*)currentatag->tmp = skip;
886     swf_AddActionTAG(ACTION_WAITFORFRAME2, (U8*)currentatag->tmp, 1);
887 }
888 void action_WaitForFrame(U16 frame, U8 skip) 
889 {
890     *(U16*)currentatag->tmp = SWAP16(frame);
891     *(U8*)&currentatag->tmp[2] = skip;
892     swf_AddActionTAG(ACTION_WAITFORFRAME, (U8*)currentatag->tmp, 3);
893 }
894 void action_SetTarget(char* target)
895 {
896     char*ptr = strdup(target);
897     swf_AddActionTAG(ACTION_SETTARGET, (U8*)ptr, strlen(ptr)+1);
898 }
899 void action_PushNULL() 
900 {
901     *(U8*)currentatag->tmp = 2; //NULL
902     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 1);
903 }
904 void action_PushBoolean(char c) 
905 {
906     *(U8*)currentatag->tmp = 5; //bool
907     *(U8*)&currentatag->tmp[1] = c;
908     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 2);
909 }
910 void action_PushRegister(U8 reg) 
911 {
912     *(U8*)currentatag->tmp = 4; //register
913     *(U8*)&currentatag->tmp[1] = reg;
914     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 2);
915 }
916 void action_PushLookup(U8 index) 
917 {
918     *(U8*)currentatag->tmp = 8; //lookup
919     *(U8*)&currentatag->tmp[1] = index;
920     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 2);
921 }
922 void action_PushString(char*str) 
923 {
924     int l = strlen(str);
925     char*ptr = (char*)malloc(l+2);
926     ptr[0] = 0; // string
927     strcpy(&ptr[1], str);
928     swf_AddActionTAG(ACTION_PUSH, (U8*)ptr, l+2);
929 }
930 void action_PushFloat(float f)
931 {
932     char*ptr = (char*)malloc(5);
933     U32 fd = *(U32*)&f;
934     ptr[0] = 1; //float
935     ptr[1]  = fd;
936     ptr[2]  = fd>>8;
937     ptr[3]  = fd>>16;
938     ptr[4]  = fd>>24;
939     swf_AddActionTAG(ACTION_PUSH, (U8*)ptr, 5);
940 }
941 void action_PushDouble(double d) 
942 {
943     char*ptr = (char*)malloc(9);
944     U8*dd = (U8*)&d;
945     ptr[0] = 6; //double
946 #ifdef WORDS_BIGENDIAN
947     ptr[1] = dd[7];ptr[2] = dd[6];
948     ptr[3] = dd[5];ptr[4] = dd[4];
949     ptr[5] = dd[3];ptr[6] = dd[2];
950     ptr[7] = dd[1];ptr[8] = dd[0];
951 #else
952     ptr[1] = dd[0];ptr[2] = dd[1];
953     ptr[3] = dd[2];ptr[4] = dd[3];
954     ptr[5] = dd[4];ptr[6] = dd[5];
955     ptr[7] = dd[6];ptr[8] = dd[7];
956 #endif
957     swf_AddActionTAG(ACTION_PUSH, (U8*)ptr, 9);
958 }
959 void action_PushInt(int i)
960 {
961     *(U8*)currentatag->tmp = 7; //int
962     currentatag->tmp[1] = i;
963     currentatag->tmp[2] = i>>8;
964     currentatag->tmp[3] = i>>16;
965     currentatag->tmp[4] = i>>24;
966     swf_AddActionTAG(ACTION_PUSH, (U8*)currentatag->tmp, 5);
967 }
968 void action_GotoLabel(char* label)
969 {
970     char*ptr = strdup(label);
971     swf_AddActionTAG(ACTION_GOTOLABEL, (U8*)ptr, strlen(ptr));
972 }
973 void action_GetUrl(char* url, char* label) 
974 {
975     int l1= strlen(url);
976     int l2= strlen(label);
977     char*ptr = malloc(l1+l2+2);
978     strcpy(ptr, url);
979     strcpy(&ptr[l1+1], label);
980     swf_AddActionTAG(ACTION_GETURL, ptr, l1+l2+2);
981 }
982 //TODO:
983 void action_DefineFunction(U8*data, int len) {}
984 void action_Constantpool(char* constantpool) {}
985 void action_With(char*object) {}
986
987 /*
988   Properties:
989
990   _X 0
991   _Y 1
992   _xscale 2
993   _yscale 3
994   _currentframe 4
995   _totalframes 5
996   _alpha 6
997   _visible 7
998   _width 8
999   _height 9
1000   _rotation 10
1001   _target 11
1002   _framesloaded 12
1003   _name 13
1004   _droptarget 14
1005   _url 15
1006   _highquality 16
1007   _focusrect 17
1008   _soundbuftime 18
1009   _quality* 19
1010   _xmouse* 20
1011   _ymouse* 21
1012 */