added function name fix from Marcus Doemling.
[swftools.git] / lib / modules / swfaction.c
index 135d026..d897c10 100644 (file)
@@ -7,9 +7,19 @@
 
    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
  
-   This file is distributed under the GPL, see file COPYING for details 
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
 
-*/
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
 #include "../rfxswf.h"
 
@@ -28,7 +38,7 @@ f: frame (word)
 u: url (string)
 t: target (string)
 l: label (string)
-C: constant pool header (byte)
+C: constant pool header (word)
 c: constant pool entry (string)
 s: skip (byte) (number of actions)
 m: method (byte) swf_GetUrl2:(0=none, 1=get, 2=post)/GotoFrame2:(1=play)
@@ -129,8 +139,8 @@ r: register (byte)
 {5,"Less2", 0x48,""},
 {6,"Greater", 0x67,""},
 {6,"StringGreater", 0x68,""},
-{6,"Enumerate2", 0x55,""}
-{6,"InstanceOf", 0x54,""}
+{6,"Enumerate2", 0x55,""},
+{6,"InstanceOf", 0x54,""},
 {6,"StrictEquals", 0x66,""}
 };
 static int definedactions = sizeof(actions)/sizeof(struct Action);
@@ -145,8 +155,10 @@ ActionTAG* swf_ActionGet(TAG*tag)
     while(op)
     {
        action->next = (ActionTAG*)malloc(sizeof(ActionTAG));
+       memset(action->next, 0, sizeof(ActionTAG));
        action->next->prev = action;
        action->next->next = 0;
+       action->next->parent = tmp.next;
        action = action->next;
 
        op = swf_GetU8(tag);
@@ -170,6 +182,16 @@ ActionTAG* swf_ActionGet(TAG*tag)
 
 void swf_ActionFree(ActionTAG*action)
 {
+    if(!action) {
+       fprintf(stderr, "Warning: freeing zero action");
+       return;
+    }
+    action = action->parent;
+    if(!action) {
+       fprintf(stderr, "Warning: freeing zero action (no parent)");
+       return;
+    }
+
     while(action)
     {
        ActionTAG*tmp;
@@ -229,6 +251,8 @@ int OpAdvance(char c, U8*data)
                return 1+4; //float
            } else if (type == 2) {
                return 1+0; //NULL
+           } else if (type == 3) {
+               return 1+0; //Undefined
            } else if (type == 4) {
                return 1+1; //register
            } else if (type == 5) {
@@ -239,6 +263,8 @@ int OpAdvance(char c, U8*data)
                return 1+4; //int
            } else if (type == 8) {
                return 1+1; //lookup
+           } else if (type == 9) {
+               return 1+2; //lookup 16
            } else return 1;
            break;
        }
@@ -290,7 +316,7 @@ void swf_DumpActions(ActionTAG*atag, char*prefix)
     while(atag)
     {
        char*indent = &spaces[sizeof(spaces)-1-countpos*4];
-       U8 poollen = 0;
+       U16 poollen = 0;
        for(t=0;t<definedactions;t++)
            if(actions[t].op == atag->op)
                break;
@@ -328,7 +354,7 @@ void swf_DumpActions(ActionTAG*atag, char*prefix)
 #endif
                  } break;
                  case 'C': {
-                     poollen = *data;
+                     poollen = data[0]+256*data[1];
                      entry = 0;
                      printf("(%d entries)", poollen);
                  } break;
@@ -349,7 +375,7 @@ void swf_DumpActions(ActionTAG*atag, char*prefix)
                      num = (data[s++]); //num
                      num += (data[s++])*256;
                      for(t=0;t<num;t++) {
-                         printf("%s",data);
+                         printf("%s",data+s);  // 10/22/04 MD: added +s to
                          if(t<num-1)
                              printf(", ");
                          while(data[s++]); //param
@@ -404,6 +430,8 @@ void swf_DumpActions(ActionTAG*atag, char*prefix)
                          printf(" Float:%f", *(float*)&f);
                      } else if (type == 2) {
                          printf(" NULL");
+                     } else if (type == 3) {
+                         printf(" Undefined");
                      } else if (type == 4) {
                          printf(" register:%d", *value);
                      } else if (type == 5) {
@@ -430,6 +458,13 @@ void swf_DumpActions(ActionTAG*atag, char*prefix)
                          if (lookup[*value])
                            printf(" (\"%s\")",lookup[*value]);
 #endif
+                     } else if (type == 9) {
+                         U32 offset = value[0]+(value[1]<<8);
+                         printf(" Lookup16:%d", offset);
+#ifdef MAX_LOOKUP
+                         if (lookup[offset])
+                           printf(" (\"%s\")",lookup[offset]);
+#endif
                      } else {
                          printf(" UNKNOWN[%02x]",type);
                      }
@@ -496,7 +531,7 @@ int swf_ActionEnumerate(ActionTAG*atag, char*(*callback)(char*), int type)
     int count = 0;
     while(atag)
     {
-       U8 poollen = 0;
+       U16 poollen = 0;
        for(t=0;t<definedactions;t++)
            if(actions[t].op == atag->op)
                break;
@@ -541,7 +576,7 @@ int swf_ActionEnumerate(ActionTAG*atag, char*(*callback)(char*), int type)
                        }
                    } break;
                    case 'C': {
-                       poollen = (*data);
+                       poollen = (data[0]+256*data[1]);
                    } break;
                    case 'o': {
                    } break;
@@ -625,7 +660,7 @@ void swf_ActionEnd(ActionTAG* atag)
 
 static ActionTAG*lastATAG(ActionTAG*atag)
 {
-    ActionTAG*last;
+    ActionTAG*last = 0;
     while(atag) {
        last = atag;
        atag=atag->next;
@@ -942,6 +977,12 @@ ActionTAG* action_PushNULL(ActionTAG*atag)
     *(U8*)atag->tmp = 2; //NULL
     return atag;
 }
+ActionTAG* action_PushUndefined(ActionTAG*atag) 
+{
+    atag = swf_AddActionTAG(atag, ACTION_PUSH, 0, 1);
+    *(U8*)atag->tmp = 3; //Undefined
+    return atag;
+}
 ActionTAG* action_PushBoolean(ActionTAG*atag, char c) 
 {
     atag = swf_AddActionTAG(atag, ACTION_PUSH, 0, 2);
@@ -963,6 +1004,14 @@ ActionTAG* action_PushLookup(ActionTAG*atag, U8 index)
     *(U8*)&atag->tmp[1] = index;
     return atag;
 }
+ActionTAG* action_PushLookup16(ActionTAG*atag, U16 index) 
+{
+    atag = swf_AddActionTAG(atag, ACTION_PUSH, 0, 3);
+    *(U8*)atag->tmp = 9; //lookup
+    *(U8*)&atag->tmp[1] = index;
+    *(U8*)&atag->tmp[2] = index>>8;
+    return atag;
+}
 ActionTAG* action_PushString(ActionTAG*atag, char*str) 
 {
     int l = strlen(str);
@@ -1029,6 +1078,32 @@ ActionTAG* action_DefineFunction(ActionTAG*atag, U8*data, int len) {return atag;
 ActionTAG* action_Constantpool(ActionTAG*atag, char* constantpool) {return atag;}
 ActionTAG*  action_With(ActionTAG*atag, char*object) {return atag;}
 
+#include "../action/actioncompiler.h"
+
+ActionTAG* swf_ActionCompile(const char* source, int version)
+{
+    TAG* tag;
+    ActionTAG* a = 0;
+    void*buffer = 0;
+    int len = 0;
+    int ret;
+    
+    tag = swf_InsertTag(NULL, ST_DOACTION);
+    ret = compileSWFActionCode(source, version, &buffer, &len);
+    if(!ret || buffer==0 || len == 0)
+       return 0;
+
+    swf_SetBlock(tag, buffer, len);
+    swf_SetU8(tag, 0);
+
+    free(buffer);
+
+    a = swf_ActionGet(tag);
+    swf_DeleteTag(tag);
+    return a;
+}
+
+
 /*
   Properties: