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