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