+
+#ifdef MAX_LOOKUP
+ for (t=0;t<MAX_LOOKUP;t++) if (lookup[t]) rfx_free(lookup[t]);
+#endif
+}
+
+static const char TYPE_URL = 1;
+static const char TYPE_TARGET = 2;
+static const char TYPE_STRING = 4;
+
+int swf_ActionEnumerate(ActionTAG*atag, char*(*callback)(char*), int type)
+{
+ int t;
+ U8*data;
+ char* cp;
+ int count = 0;
+ while(atag)
+ {
+ U16 poollen = 0;
+ for(t=0;t<definedactions;t++)
+ if(actions[t].op == atag->op)
+ break;
+
+ if(t==definedactions) {
+ // unknown actiontag
+ atag = atag->next;
+ count++;
+ continue;
+ }
+ cp = actions[t].flags;
+ data = atag->data;
+ if(atag->len) {
+ while(*cp) {
+ U8 * replacepos = 0;
+ int replacelen = 0;
+ U8 * replacement = 0;
+ switch(*cp)
+ {
+ case 'u': {
+ if(type&TYPE_URL)
+ {
+ replacelen = strlen((const char*)data);
+ replacepos = data;
+ replacement = (U8*)callback((char*)data); // may be null
+ }
+ } break;
+ case 't': {
+ if(type&TYPE_TARGET)
+ {
+ replacelen = strlen((const char*)data);
+ replacepos = data;
+ replacement = (U8*)callback((char*)data); // may be null
+ }
+ } break;
+ case 'c': {
+ if(type&TYPE_STRING)
+ {
+ replacelen = strlen((const char*)data);
+ replacepos = data;
+ replacement = (U8*)callback((char*)data); // may be null
+ }
+ } break;
+ case 'C': {
+ poollen = (data[0]+256*data[1]);
+ } break;
+ case 'o': {
+ } break;
+ case 'p': {
+ U8 datatype = *data;
+ char*value = (char*)&data[1];
+ if(datatype == 0) { //string
+ if(type&TYPE_STRING)
+ {
+ replacelen = strlen(value);
+ replacepos = (U8*)value;
+ replacement = (U8*)callback(value); // may be null
+ }
+ } else if (datatype == 8) { //lookup
+ }
+ } break;
+ }
+ data += OpAdvance(*cp, data);
+ if(*cp!='c' || !poollen)
+ cp++;
+ if(poollen)
+ poollen--;
+
+ if(replacement)
+ {
+ int newlen = strlen((const char *)replacement);
+ char * newdata = (char*)rfx_alloc(atag->len - replacelen + newlen);
+ int rpos = replacepos - atag->data;
+ memcpy(newdata, atag->data, rpos);
+ memcpy(&newdata[rpos], replacement, newlen);
+ memcpy(&newdata[rpos+newlen], &replacepos[replacelen],
+ &data[atag->len] - &replacepos[replacelen]);
+ rfx_free(atag->data);
+ atag->data = (U8*)newdata;
+ data = &atag->data[rpos+newlen+1];
+ }
+ }
+ }
+ atag = atag->next;
+ count ++;
+ }
+ return count;
+}
+
+void swf_ActionEnumerateTargets(ActionTAG*atag, char*(*callback)(char*))
+{
+ swf_ActionEnumerate(atag, callback, TYPE_TARGET);
+}
+void swf_ActionEnumerateStrings(ActionTAG*atag, char*(*callback)(char*))
+{
+ swf_ActionEnumerate(atag, callback, TYPE_STRING);
+}
+void swf_ActionEnumerateURLs(ActionTAG*atag, char*(*callback)(char*))
+{
+ swf_ActionEnumerate(atag, callback, TYPE_URL);
+}
+
+/*static ActionTAG* swf_ActionStart()
+{
+ ActionTAG*atag;
+ atag = (ActionTAG*)rfx_alloc(sizeof(ActionTAG));
+ atag->prev = 0;
+ atag->next = 0;
+ atag->parent = 0;
+ atag->data = 0;
+ atag->len = 0;
+ return atag;
+}
+
+void swf_ActionEnd(ActionTAG* atag)
+{
+ ActionTAG*last;
+ while(atag) {
+ last = atag;
+ atag=atag->next;
+ }
+
+ last->prev->next = 0;
+ rfx_free(last);
+}*/
+
+static ActionTAG*lastATAG(ActionTAG*atag)
+{
+ ActionTAG*last = 0;
+ while(atag) {
+ last = atag;
+ atag=atag->next;
+ }
+ return last;
+}
+
+ActionTAG* swf_AddActionTAG(ActionTAG*atag, U8 op, U8*data, U16 len)
+{
+ ActionTAG*tmp;
+ tmp = (ActionTAG*)rfx_alloc(sizeof(ActionTAG));
+ tmp->next = 0;
+ if(atag) {
+ tmp->prev = atag;
+ atag->next = tmp;
+ tmp->parent = atag->parent;
+ } else {
+ tmp->prev = 0;
+ tmp->parent = tmp;
+ }
+ if(data || !len) {
+ tmp->data = data;
+ } else {
+ tmp->data = tmp->tmp;
+ }
+
+ tmp->len = len;
+ tmp->op = op;
+ return tmp;
+}
+
+ActionMarker action_setMarker(ActionTAG*atag)
+{
+ ActionMarker m;
+ m.atag = atag;
+ return m;
+}
+
+int inline ActionTagSize(ActionTAG*atag)
+{
+ return (atag->op&0x80)?3+(atag->len):1+0;
+}
+
+
+#define ACTION_END 0x00
+#define ACTION_NEXTFRAME 0x04
+#define ACTION_PREVIOUSFRAME 0x05
+#define ACTION_PLAY 0x06
+#define ACTION_STOP 0x07
+#define ACTION_TOGGLEQUALITY 0x08
+#define ACTION_STOPSOUNDS 0x09
+#define ACTION_ADD 0x0a
+#define ACTION_SUBTRACT 0x0b
+#define ACTION_MULTIPLY 0x0c
+#define ACTION_DIVIDE 0x0d
+#define ACTION_EQUALS 0x0e
+#define ACTION_LESS 0x0f
+#define ACTION_AND 0x10
+#define ACTION_OR 0x11
+#define ACTION_NOT 0x12
+#define ACTION_STRINGEQUALS 0x13
+#define ACTION_STRINGLENGTH 0x14
+#define ACTION_STRINGEXTRACT 0x15
+#define ACTION_POP 0x17
+#define ACTION_TOINTEGER 0x18
+#define ACTION_GETVARIABLE 0x1c
+#define ACTION_SETVARIABLE 0x1d
+#define ACTION_SETTARGET2 0x20
+#define ACTION_STRINGADD 0x21
+#define ACTION_GETPROPERTY 0x22
+#define ACTION_SETPROPERTY 0x23
+#define ACTION_CLONESPRITE 0x24
+#define ACTION_REMOVESPRITE 0x25
+#define ACTION_TRACE 0x26
+#define ACTION_STARTDRAG 0x27
+#define ACTION_ENDDRAG 0x28
+#define ACTION_STRINGLESS 0x29
+#define ACTION_RANDOMNUMBER 0x30
+#define ACTION_MBSTRINGLENGTH 0x31
+#define ACTION_CHARTOASCII 0x32
+#define ACTION_ASCIITOCHAR 0x33
+#define ACTION_GETTIME 0x34
+#define ACTION_MBSTRINGEXTRACT 0x35
+#define ACTION_MBCHARTOASCII 0x36
+#define ACTION_MBASCIITOCHAR 0x37
+#define ACTION_DELETE 0x3a
+#define ACTION_DELETE2 0x3b
+#define ACTION_DEFINELOCAL 0x3c
+#define ACTION_CALLFUNCTION 0x3d
+#define ACTION_RETURN 0x3e
+#define ACTION_MODULO 0x3f
+#define ACTION_NEWOBJECT 0x40
+#define ACTION_DEFINELOCAL2 0x41
+#define ACTION_INITARRAY 0x42
+#define ACTION_MAKEHASH 0x43
+#define ACTION_TYPEOF 0x44
+#define ACTION_TARGETPATH 0x45
+#define ACTION_ENUMERATE 0x46
+#define ACTION_ADD2 0x47
+#define ACTION_LESS2 0x48
+#define ACTION_EQUALS2 0x49
+#define ACTION_TONUMBER 0x4a
+#define ACTION_TOSTRING 0x4b
+#define ACTION_PUSHDUPLICATE 0x4c
+#define ACTION_STACKSWAP 0x4d
+#define ACTION_GETMEMBER 0x4e
+#define ACTION_SETMEMBER 0x4f
+#define ACTION_INCREMENT 0x50
+#define ACTION_DECREMENT 0x51
+#define ACTION_CALLMETHOD 0x52
+#define ACTION_NEWMETHOD 0x53
+#define ACTION_BITAND 0x60
+#define ACTION_BITOR 0x61
+#define ACTION_BITXOR 0x62
+#define ACTION_BITLSHIFT 0x63
+#define ACTION_BITRSHIFT 0x64
+#define ACTION_BITURSHIFT 0x65
+#define ACTION_GOTOFRAME 0x81
+#define ACTION_GETURL 0x83
+#define ACTION_STOREREGISTER 0x87
+#define ACTION_CONSTANTPOOL 0x88
+#define ACTION_WAITFORFRAME 0x8a
+#define ACTION_SETTARGET 0x8b
+#define ACTION_GOTOLABEL 0x8c
+#define ACTION_WAITFORFRAME2 0x8d
+#define ACTION_WITH 0x94
+#define ACTION_PUSH 0x96
+#define ACTION_JUMP 0x99
+#define ACTION_GETURL2 0x9a
+#define ACTION_DEFINEFUNCTION 0x9b
+#define ACTION_IF 0x9d
+#define ACTION_CALL 0x9e
+#define ACTION_GOTOFRAME2 0x9f
+
+void action_fixjump(ActionMarker m1, ActionMarker m2)
+{
+ ActionTAG* a1 = m1.atag;
+ ActionTAG* a2 = m2.atag;
+ ActionTAG* a;
+ int len = 0;
+ int oplen = 0;
+ a = a1;
+
+ a = a->next; //first one is free
+ while(a && a!=a2)
+ {
+ len += ActionTagSize(a);
+ oplen ++;
+ a = a->next;
+ }
+ if(!a)
+ { len = 0;
+ oplen = 0;
+ a = a2;
+ while(a && a!=a1) {
+ len -= ActionTagSize(a);
+ oplen --;
+ a = a->next;
+ }
+ if(!a) {
+ fprintf(stderr, "action_fixjump: couldn't find second tag\n");
+ return;
+ }
+ len -= ActionTagSize(a);
+ oplen --;
+ }
+
+ if (a1->op == ACTION_IF || a1->op == ACTION_JUMP)
+ {
+ *(U16*)(a1->data) = SWAP16(len);
+ }
+ else if(a1->op == ACTION_WAITFORFRAME)
+ {
+ ((U8*)(a1->data))[2] = oplen;
+ }
+ else if(a1->op == ACTION_WAITFORFRAME2)
+ {
+ ((U8*)(a1->data))[0] = oplen;
+ }
+
+}
+
+ActionTAG* action_NextFrame(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_NEXTFRAME, 0, 0);}
+ActionTAG* action_PreviousFrame(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_PREVIOUSFRAME, 0, 0);}
+ActionTAG* action_Play(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_PLAY, 0, 0);}
+ActionTAG* action_Stop(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STOP, 0, 0);}
+ActionTAG* action_ToggleQuality(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_TOGGLEQUALITY, 0, 0);}
+ActionTAG* action_StopSounds(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STOPSOUNDS, 0, 0);}
+ActionTAG* action_Add(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_ADD, 0, 0);}
+ActionTAG* action_Subtract(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_SUBTRACT, 0, 0);}
+ActionTAG* action_Multiply(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_MULTIPLY, 0, 0);}
+ActionTAG* action_Divide(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_DIVIDE, 0, 0);}
+ActionTAG* action_Equals(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_EQUALS, 0, 0);}
+ActionTAG* action_Less(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_LESS, 0, 0);}
+ActionTAG* action_And(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_AND, 0, 0);}
+ActionTAG* action_Or(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_OR, 0, 0);}
+ActionTAG* action_Not(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_NOT, 0, 0);}
+ActionTAG* action_StringEquals(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STRINGEQUALS, 0, 0);}
+ActionTAG* action_StringLength(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STRINGLENGTH, 0, 0);}
+ActionTAG* action_StringExtract(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STRINGEXTRACT, 0, 0);}
+ActionTAG* action_Pop(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_POP, 0, 0);}
+ActionTAG* action_ToInteger(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_TOINTEGER, 0, 0);}
+ActionTAG* action_GetVariable(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_GETVARIABLE, 0, 0);}
+ActionTAG* action_SetVariable(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_SETVARIABLE, 0, 0);}
+ActionTAG* action_SetTarget2(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_SETTARGET2, 0, 0);}
+ActionTAG* action_StringAdd(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STRINGADD, 0, 0);}
+ActionTAG* action_GetProperty(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_GETPROPERTY, 0, 0);}
+ActionTAG* action_SetProperty(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_SETPROPERTY, 0, 0);}
+ActionTAG* action_CloneSprite(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_CLONESPRITE, 0, 0);}
+ActionTAG* action_RemoveSprite(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_REMOVESPRITE, 0, 0);}
+ActionTAG* action_Trace(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_TRACE, 0, 0);}
+ActionTAG* action_StartDrag(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STARTDRAG, 0, 0);}
+ActionTAG* action_EndDrag(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_ENDDRAG, 0, 0);}
+ActionTAG* action_StringLess(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STRINGLESS, 0, 0);}
+ActionTAG* action_RandomNumber(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_RANDOMNUMBER, 0, 0);}
+ActionTAG* action_MBStringLength(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_MBSTRINGLENGTH, 0, 0);}
+ActionTAG* action_CharToAscii(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_CHARTOASCII, 0, 0);}
+ActionTAG* action_AsciiToChar(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_ASCIITOCHAR, 0, 0);}
+ActionTAG* action_GetTime(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_GETTIME, 0, 0);}
+ActionTAG* action_MBStringExtract(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_MBSTRINGEXTRACT, 0, 0);}
+ActionTAG* action_MBCharToAscii(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_MBCHARTOASCII, 0, 0);}
+ActionTAG* action_MBAsciiToChar(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_MBASCIITOCHAR, 0, 0);}
+ActionTAG* action_Delete(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_DELETE, 0, 0);}
+ActionTAG* action_Delete2(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_DELETE2, 0, 0);}
+ActionTAG* action_DefineLocal(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_DEFINELOCAL, 0, 0);}
+ActionTAG* action_CallFunction(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_CALLFUNCTION, 0, 0);}
+ActionTAG* action_Return(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_RETURN, 0, 0);}
+ActionTAG* action_Modulo(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_MODULO, 0, 0);}
+ActionTAG* action_NewObject(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_NEWOBJECT, 0, 0);}
+ActionTAG* action_DefineLocal2(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_DEFINELOCAL2, 0, 0);}
+ActionTAG* action_InitArray(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_INITARRAY, 0, 0);}
+ActionTAG* action_Makehash(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_MAKEHASH, 0, 0);}
+ActionTAG* action_TypeOf(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_TYPEOF, 0, 0);}
+ActionTAG* action_TargetPath(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_TARGETPATH, 0, 0);}
+ActionTAG* action_Enumerate(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_ENUMERATE, 0, 0);}
+ActionTAG* action_Add2(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_ADD2, 0, 0);}
+ActionTAG* action_Less2(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_LESS2, 0, 0);}
+ActionTAG* action_Equals2(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_EQUALS2, 0, 0);}
+ActionTAG* action_ToNumber(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_TONUMBER, 0, 0);}
+ActionTAG* action_ToString(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_TOSTRING, 0, 0);}
+ActionTAG* action_PushDuplicate(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_PUSHDUPLICATE, 0, 0);}
+ActionTAG* action_StackSwap(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_STACKSWAP, 0, 0);}
+ActionTAG* action_GetMember(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_GETMEMBER, 0, 0);}
+ActionTAG* action_SetMember(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_SETMEMBER, 0, 0);}
+ActionTAG* action_Increment(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_INCREMENT, 0, 0);}
+ActionTAG* action_Decrement(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_DECREMENT, 0, 0);}
+ActionTAG* action_CallMethod(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_CALLMETHOD, 0, 0);}
+ActionTAG* action_NewMethod(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_NEWMETHOD, 0, 0);}
+ActionTAG* action_BitAnd(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_BITAND, 0, 0);}
+ActionTAG* action_BitOr(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_BITOR, 0, 0);}
+ActionTAG* action_BitXor(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_BITXOR, 0, 0);}
+ActionTAG* action_BitLShift(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_BITLSHIFT, 0, 0);}
+ActionTAG* action_BitRShift(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_BITRSHIFT, 0, 0);}
+ActionTAG* action_BitURShift(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_BITURSHIFT, 0, 0);}
+ActionTAG* action_Call(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_CALL, 0, 0);}
+ActionTAG* action_End(ActionTAG*atag) {return swf_AddActionTAG(atag, ACTION_END, 0, 0);}
+ActionTAG* action_GotoFrame(ActionTAG*atag, U16 frame)
+{
+ atag = swf_AddActionTAG(atag, ACTION_GOTOFRAME, 0, 2);
+ *(U16*)atag->tmp = SWAP16(frame);
+ return atag;
+}
+
+ActionTAG* action_Jump(ActionTAG*atag, U16 branch)
+{
+ atag = swf_AddActionTAG(atag, ACTION_JUMP, 0, 2);
+ *(U16*)atag->tmp = SWAP16(branch);
+ return atag;
+}
+ActionTAG* action_If(ActionTAG*atag, U16 branch)
+{
+ atag = swf_AddActionTAG(atag, ACTION_IF, 0, 2);
+ *(U16*)atag->tmp = SWAP16(branch);
+ return atag;
+}
+ActionTAG* action_StoreRegister(ActionTAG*atag, U8 reg)
+{
+ atag = swf_AddActionTAG(atag, ACTION_STOREREGISTER, 0, 1);
+ *(U8*)atag->tmp = reg;
+ return atag;
+}
+ActionTAG* action_GotoFrame2(ActionTAG*atag, U8 method)
+{
+ atag = swf_AddActionTAG(atag, ACTION_GOTOFRAME2, 0, 1);
+ *(U8*)atag->tmp = method;
+ return atag;