added prefix swf_ to action enumeration routines.
[swftools.git] / lib / modules / swfaction.c
index b0b7390..c751fd3 100644 (file)
@@ -26,11 +26,12 @@ f: frame (word)
 u: url (string)
 t: target (string)
 l: label (string)
-c: constant pool (string)
+C: constant pool header (byte)
+c: constant pool entry (string)
 s: skip (byte) (number of actions)
-m: method (byte) url:(0=none, 1=get, 2=post)/gf2:(1=play)
+m: method (byte) swf_GetUrl2:(0=none, 1=get, 2=post)/GotoFrame2:(1=play)
 b: branch (word) (number of bytes)
-p: type(byte), type=0:string, type=1:double
+p (push): type(byte), type=0:string, type=1:double
 {: define function (name (string), num (word), params (num strings), codesize (word)
 o: object (string)
 r: register (byte)
@@ -38,8 +39,8 @@ r: register (byte)
 {3,"End", 0x00, ""},
 {3,"GotoFrame", 0x81, "f"},
 {4,"GotoFrame2", 0x9f, "m"}, // -1 (/Movieclip:3)
-{3,"GetUrl", 0x83, "ul"},
-{4,"GetUrl2", 0x9a, "m"}, //-2
+{3,"swf_GetUrl", 0x83, "ul"},
+{4,"swf_GetUrl2", 0x9a, "m"}, //-2
 {3,"NextFrame", 0x04, ""},
 {3,"PreviousFrame", 0x05, ""},
 {3,"Play", 0x06, ""},
@@ -88,7 +89,6 @@ r: register (byte)
 {4,"Trace", 0x26, ""}, //-1
 {4,"GetTime", 0x34, ""}, //+1
 {4,"RandomNumber", 0x30, ""}, //-1,+1
-
 {5,"Modulo", 0x3f,""},
 {5,"BitAnd", 0x60,""},
 {5,"BitLShift", 0x63,""},
@@ -107,7 +107,7 @@ r: register (byte)
 {5,"GetMember", 0x4e,""},
 {5,"SetMember", 0x4f,""},
 {5,"CallMethod", 0x52,""},
-{5,"Constantpool", 0x88, "c"},
+{5,"Constantpool", 0x88, "Cc"},
 {5,"DefineLocal", 0x3c,""},
 {5,"DefineLocal2", 0x41,""},
 {5,"Makehash", 0x43, ""}, //??
@@ -128,44 +128,106 @@ r: register (byte)
 };
 static int definedactions = sizeof(actions)/sizeof(struct Action);
 
-ActionTAG* GetActions(TAG*tag) 
+ActionTAG* swf_GetActions(TAG*tag) 
 {
     U8 op = 1;
     int length;
     ActionTAG tmp;
-    ActionTAG*atag = &tmp;
+    ActionTAG*action = &tmp;
     U8*data;
     while(op)
     {
        int pos;
-       atag->next = (ActionTAG*)malloc(sizeof(ActionTAG));
-       atag->next->prev = atag;
-       atag->next->next = 0;
-       atag = atag->next;
+       action->next = (ActionTAG*)malloc(sizeof(ActionTAG));
+       action->next->prev = action;
+       action->next->next = 0;
+       action = action->next;
 
-       op = GetU8(tag);
+       op = swf_GetU8(tag);
        if(op<0x80)
            length = 0;
        else
-           length = GetU16(tag);
+           length = swf_GetU16(tag);
 
        if(length) {
            int t;
            data = malloc(length);
            for(t=0;t<length;t++)
-               data[t] = GetU8(tag);
+               data[t] = swf_GetU8(tag);
        } else {
          data = 0;
        }
-       atag->op = op;
-       atag->len = length;
-       atag->data = data;
+       action->op = op;
+       action->len = length;
+       action->data = data;
+       action->parent = tag;
     }
     return tmp.next;
 }
 
+void swf_SetActions(TAG*tag, ActionTAG*action)
+{
+    while(action)
+    {
+       swf_SetU8(tag, action->op);
+       if(action->op & 128)
+         swf_SetU16(tag, action->len);
+
+       swf_SetBlock(tag, action->data, action->len);
+
+       action = action->next;
+    }
+}
+
+int OpAdvance(char c, char*data)
+{
+    switch (c)
+    {
+       case 'f':
+           return 2;
+       case 'u':
+           return strlen(data)+1;
+       case 't':
+           return strlen(data)+1;
+       case 'l': 
+           return strlen(data)+1;
+       case 'c': 
+           return strlen(data)+1;
+       case 'C': 
+           return 2;
+       case 's':
+           return 1;
+       case 'm':
+           return 1;
+       case 'b':
+           return 2;
+       case 'p': {
+           U8 type = *data++;
+           if(type == 0) {
+               return 1+strlen(data)+1; //string
+           } else if (type == 1) {
+               return 1+4; //float
+           } else if (type == 2) {
+               return 1+0; //NULL
+           } else if (type == 4) {
+               return 1+1; //register
+           } else if (type == 5) {
+               return 1+1; //bool
+           } else if (type == 6) {
+               return 1+8; //double
+           } else if (type == 7) {
+               return 1+4; //int
+           } else if (type == 8) {
+               return 1+1; //lookup
+           }
+           break;
+       }
+    }
+    return 0;
+}
+
 /* TODO: this should be in swfdump.c */
-void DumpActions(ActionTAG*atag, char*prefix) 
+void swf_DumpActions(ActionTAG*atag, char*prefix) 
 {
     U8 op;
     int t;
@@ -175,6 +237,7 @@ void DumpActions(ActionTAG*atag, char*prefix)
        prefix="";
     while(atag)
     {
+       U8 poollen = 0;
        for(t=0;t<definedactions;t++)
            if(actions[t].op == atag->op)
                break;
@@ -193,53 +256,61 @@ void DumpActions(ActionTAG*atag, char*prefix)
            switch(*cp)
            {
                case 'f': {
-                   printf(" %d", *(U16*)data);data+=2; //FIXME: le/be
+                   printf(" %d", *(U16*)data); //FIXME: le/be
                } break;
                case 'u': {
-                   printf(" URL:\"%s\"", data);data+=strlen(data)+1;
+                   printf(" URL:\"%s\"", data);
                } break;
                case 't': {
-                   printf(" Target:\"%s\"", data);data+=strlen(data)+1;
+                   printf(" Target:\"%s\"", data);
                } break;
                case 'l': {
-                   printf(" Label:\"%s\"", data);data+=strlen(data)+1;
+                   printf(" Label:\"%s\"", data);
                } break;
                case 'c': {
-                   printf(" Constant Pool:\"%s\"", data);data+=strlen(data)+1;
+                   printf(" String:\"%s\"", data);
+               } break;
+               case 'C': {
+                   poollen = *data;
+                   printf("(%d entries)", poollen);
                } break;
                case 's': {
-                   printf(" +%d", data);data++;
+                   printf(" +%d", data);
                } break;
                case 'm': {
-//m: method (byte) url:(0=none, 1=get, 2=datat)/gf2:(1=play)
-                   printf(" %d", data);data++;
+                   //m: method (byte) url:(0=none, 1=get, 2=datat)/gf2:(1=play)
+                   printf(" %d", data);
                } break;
                case 'b': {
-                   printf(" %d", *(U16*)data);data+=2;
+                   printf(" %d", *(U16*)data);
                } break;
                case 'p': {
-                   U8 type = *data++;
+                   U8 type = *data;
+                   char*value = data+1;
                    if(type == 0) {
-                       printf(" String:\"%s\"", data);data+=strlen(data)+1;
+                       printf(" String:\"%s\"", value);
                    } else if (type == 1) {
-                       printf(" Double:\"%f\"", *(float*)data);data+=4;
+                       printf(" Float:\"%f\"", *(float*)value);
                    } else if (type == 2) {
                        printf(" NULL");
                    } else if (type == 4) {
-                       printf(" register:%d", data++);
+                       printf(" register:%d", value);
                    } else if (type == 5) {
-                       printf(" %s", data++?"true":"false");
+                       printf(" %s", *value?"true":"false");
                    } else if (type == 6) {
-                       printf(" %f", *(double*)data);  data+=8;
+                       printf(" %f", *(double*)value);
                    } else if (type == 7) {
-                       printf(" %d", *(int*)data); data+=4;
+                       printf(" %d", *(int*)value);
                    } else if (type == 8) {
-                       printf(" Lookup:%d", data++);
+                       printf(" Lookup:%d", *value);
                    }
                } break;
-           
            }
-           cp++;
+           data += OpAdvance(*cp, data);
+           if(*cp!='c' || !poollen)
+               cp++;
+           if(poollen)
+               poollen--;
        }
 
        if(data < atag->data + atag->len)
@@ -260,3 +331,116 @@ void DumpActions(ActionTAG*atag, char*prefix)
     }
 }
 
+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)
+{
+    U8 op;
+    int t;
+    U8*data;
+    char* cp;
+    
+    while(atag)
+    {
+       U8 poollen = 0;
+       for(t=0;t<definedactions;t++)
+           if(actions[t].op == atag->op)
+               break;
+
+       if(t==definedactions) {
+           // unknown actiontag
+           atag = atag->next;
+           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(data);
+                           replacepos = data;
+                           replacement = callback(data); // may be null
+                       }
+                   } break;
+                   case 't': {
+                       if(type&TYPE_TARGET)
+                       {
+                           replacelen = strlen(data);
+                           replacepos = data;
+                           replacement = callback(data); // may be null
+                       }
+                   } break;
+                   case 'c': {
+                       if(type&TYPE_STRING)
+                       {
+                           replacelen = strlen(data);
+                           replacepos = data;
+                           replacement = callback(data); // may be null
+                       }
+                   } break;
+                   case 'C': {
+                       poollen = (*data);
+                   } break;
+                   case 'o': {
+                   } break;
+                   case 'p': {
+                       U8 datatype = *data;
+                       char*value = &data[1];
+                       if(datatype == 0) { //string
+                           if(type&TYPE_STRING)
+                           {
+                               replacelen = strlen(value);
+                               replacepos = value;
+                               replacement = 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(replacement);
+                   char * newdata = malloc(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]);
+                   free(atag->data);
+                   atag->data = newdata;
+                   data = &atag->data[rpos+newlen+1];
+               }
+           }
+       }
+       atag = atag->next;
+    }
+}
+
+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);
+}
+