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