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