filesize check works now.
[swftools.git] / src / swfdump.c
index b898f62..e70a425 100644 (file)
@@ -7,7 +7,7 @@
 
    This file is distributed under the GPL, see file COPYING for details */
 
-#define HAVE_STAT
+#include "../config.h"
 
 #ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
@@ -24,6 +24,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <stdarg.h>
 #include "../lib/rfxswf.h"
 #include "../lib/args.h"
 
@@ -37,16 +38,20 @@ char idtab[65536];
 int action = 0;
 int html = 0;
 int xy = 0;
+int showtext = 0;
+int hex = 0;
 
 struct options_t options[] =
 {
  {"a","action"},
+ {"t","text"},
  {"X","width"},
  {"Y","height"},
  {"r","rate"},
  {"e","html"},
  {"v","verbose"},
  {"V","version"},
+ {"d","hex"},
  {0,0}
 };
 
@@ -61,6 +66,10 @@ int args_callback_option(char*name,char*val)
         action = 1;
         return 0;
     }
+    else if(name[0]=='t') {
+        showtext = 1;
+        return 0;
+    }
     else if(name[0]=='e') {
         html = 1;
         return 0;
@@ -77,6 +86,10 @@ int args_callback_option(char*name,char*val)
        xy |= 4;
        return 0;
     }
+    else if(name[0]=='d') {
+       hex = 1;
+       return 0;
+    }
     else {
         printf("Unknown option: -%s\n", name);
     }
@@ -89,14 +102,16 @@ int args_callback_longoption(char*name,char*val)
 }
 void args_callback_usage(char*name)
 {    
-    printf("Usage: %s [-a] file.swf\n", name);
-    printf("-h , --help\t\t\t Print help and exit\n");
-    printf("-e , --html\t\t\t Create a html embedding the file (simple, but useful)\n");
-    printf("-X , --width\t\t\t Prints out a string of the form \"-X width\"\n");
-    printf("-Y , --height\t\t\t Prints out a string of the form \"-Y height\"\n");
-    printf("-r , --rate\t\t\t Prints out a string of the form \"-r rate\"\n");
-    printf("-a , --action\t\t\t Disassemble action tags\n");
-    printf("-V , --version\t\t\t Print program version and exit\n");
+    printf("Usage: %s [-at] file.swf\n", name);
+    printf("\t-h , --help\t\t Print help and exit\n");
+    printf("\t-e , --html\t\t Create a html embedding the file (simple, but useful)\n");
+    printf("\t-X , --width\t\t Prints out a string of the form \"-X width\"\n");
+    printf("\t-Y , --height\t\t Prints out a string of the form \"-Y height\"\n");
+    printf("\t-r , --rate\t\t Prints out a string of the form \"-r rate\"\n");
+    printf("\t-a , --action\t\t Disassemble action tags\n");
+    printf("\t-t , --text\t\t Show text data\n");
+    printf("\t-d , --hex\t\t Print hex output of tag data, too\n");
+    printf("\t-V , --version\t\t Print program version and exit\n");
 }
 int args_callback_command(char*name,char*val)
 {
@@ -135,12 +150,15 @@ void dumpButton2Actions(TAG*tag, char*prefix)
     { swf_GetU16(tag);          // id
       swf_GetU16(tag);          // layer
       swf_GetMatrix(tag,NULL);  // matrix
-      swf_GetCXForm(tag,NULL,0);  // matrix
+      swf_GetCXForm(tag,NULL,1);  // cxform
     }
 
     while(offsetpos)
     { U8 a;
       ActionTAG*actions;
+
+      if(tag->pos >= tag->len)
+         break;
         
       offsetpos = swf_GetU16(tag);
       condition = swf_GetU16(tag);                // condition
@@ -184,6 +202,46 @@ void dumpButtonActions(TAG*tag, char*prefix)
 #define ET_X0 2
 #define ET_USEOUTLINES 1
 
+SWF swf;
+int fontnum = 0;
+SWFFONT**fonts;
+
+void textcallback(int*glyphs, int nr, int fontid) 
+{
+    int font=-1,t;
+    printf("                <%2d glyphs in font %2d> ",nr, fontid);
+    for(t=0;t<fontnum;t++)
+    {
+       if(fonts[t]->id == fontid) {
+           font = t;
+           break;
+       }
+    }
+
+    for(t=0;t<nr;t++)
+    {
+       unsigned char a; 
+       if(font>=0) {
+           if(glyphs[t] >= fonts[font]->numchars)
+               continue;
+           a = fonts[font]->glyph2ascii[glyphs[t]];
+       } else {
+           a = glyphs[t];
+       }
+       if(a>=32)
+           printf("%c", a,a);
+       else
+           printf("\\x%x", (int)a);
+    }
+    printf("\n");
+}
+
+void handleText(TAG*tag) 
+{
+  printf("\n");
+  swf_FontExtract_DefineTextCallback(-1,0,tag,4, textcallback);
+}
+
 void handleEditText(TAG*tag)
 {
     U16 id ;
@@ -233,16 +291,102 @@ void handleEditText(TAG*tag)
    //  printf(" text \"%s\"\n", &tag->data[tag->pos])
        ;
 }
+void printhandlerflags(U16 handlerflags) 
+{
+    if(handlerflags&1) printf("[on load]");
+    if(handlerflags&2) printf("[enter frame]");
+    if(handlerflags&4) printf("[unload]");
+    if(handlerflags&8) printf("[mouse move]");
+    if(handlerflags&16) printf("[mouse down]");
+    if(handlerflags&32) printf("[mouse up]");
+    if(handlerflags&64) printf("[key down]");
+    if(handlerflags&128) printf("[key up]");
+    if(handlerflags&256) printf("[data]");
+    if(handlerflags&0xfe00) printf("[???]");
+}
+void handlePlaceObject2(TAG*tag, char*prefix)
+{
+    U8 flags = swf_GetU8(tag);
+    swf_GetU16(tag); //depth
+    //flags&1: move
+    if(flags&2) swf_GetU16(tag); //id
+    if(flags&4) swf_GetMatrix(tag,0);
+    if(flags&8) swf_GetCXForm(tag,0,0);
+    if(flags&16) swf_GetU16(tag); //ratio
+    if(flags&32) { 
+       while(swf_GetU8(tag));
+    }
+    if(flags&64) swf_GetU16(tag); //clip
+    if(flags&128) {
+      if (action) {
+       U16 globalflags;
+       U16 unknown;
+       printf("\n");
+       unknown = swf_GetU16(tag);
+       globalflags = swf_GetU16(tag);
+       if(unknown)
+           printf("Unknown parameter field not zero: %04x\n", unknown);
+       while(1)  {
+           int length;
+           int t;
+           U16 handlerflags;
+           ActionTAG*a;
+           handlerflags = swf_GetU16(tag);
+           if(!handlerflags)
+               break;
+           globalflags &= ~handlerflags;
+           printf("%s flags %04x ",prefix, handlerflags);
+           printhandlerflags(handlerflags);
+
+           length = swf_GetU32(tag);
+           printf(", %d bytes actioncode\n",length);
+           a = swf_ActionGet(tag);
+           swf_DumpActions(a,prefix);
+           swf_ActionFree(a);
+       }
+       if(globalflags) // should go to sterr.
+           printf("ERROR: unsatisfied handlerflags: %02x\n", globalflags);
+    } else {
+      printf(" has action code\n");
+    }
+    } else printf("\n");
+}
+    
+void fontcallback1(U16 id,U8 * name)
+{ fontnum++;
+}
+
+void fontcallback2(U16 id,U8 * name)
+{ 
+  swf_FontExtract(&swf,id,&fonts[fontnum]);
+  fontnum++;
+}
+
+void dumperror(const char* format, ...)
+{
+    char buf[1024];
+    va_list arglist;
+
+    va_start(arglist, format);
+    vsprintf(buf, format, arglist);
+    va_end(arglist);
+
+    printf("==== Error: %s ====\n", buf);
+}
 
 int main (int argc,char ** argv)
 { 
-    SWF swf;
     TAG*tag;
 #ifdef HAVE_STAT
     struct stat statbuf;
 #endif
     int f;
     int xsize,ysize;
+    char issprite = 0; // are we inside a sprite definition?
+    int spriteframe;
+    int mainframe=0;
+    char* spriteframelabel;
+    char* framelabel = 0;
     char prefix[128];
     prefix[0] = 0;
     memset(idtab,0,65536);
@@ -271,9 +415,9 @@ int main (int argc,char ** argv)
 
 #ifdef HAVE_STAT
     fstat(f, &statbuf);
-    if(statbuf.st_size != swf.FileSize)
-        fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)",
-                statbuf.st_size, swf.FileSize);
+    if(statbuf.st_size != swf.fileSize)
+        dumperror("Real Filesize (%d) doesn't match header Filesize (%d)",
+                statbuf.st_size, swf.fileSize);
 #endif
 
     close(f);
@@ -326,53 +470,137 @@ int main (int argc,char ** argv)
     printf("[HEADER]        Movie height: %.3f\n",(swf.movieSize.ymax-swf.movieSize.ymin)/20.0);
 
     tag = swf.firstTag;
+   
+    if(showtext) {
+       fontnum = 0;
+       swf_FontEnumerate(&swf,&fontcallback1);
+       fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
+       fontnum = 0;
+       swf_FontEnumerate(&swf,&fontcallback2);
+    }
 
     while(tag) {
         char*name = swf_TagGetName(tag);
         char myprefix[128];
         if(!name) {
-            fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id);
+            dumperror("Unknown tag:0x%03x", tag->id);
             tag = tag->next;
             continue;
         }
         printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag));
+       
+       if(tag->id == ST_FREECHARACTER) {
+           U16 id = swf_GetU16(tag);
+           idtab[id] = 0;
+       }
 
         if(swf_isDefiningTag(tag)) {
             U16 id = swf_GetDefineID(tag);
-            printf(" defines id %04x", id);
+            printf(" defines id %04d", id);
             if(idtab[id])
-                fprintf(stderr, "Error: Id %04x is defined more than once.\n", id);
+                dumperror("Id %04d is defined more than once.", id);
             idtab[id] = 1;
         }
-        else if(tag->id == ST_PLACEOBJECT || 
-                tag->id == ST_PLACEOBJECT2) {
-            printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
+       else if(swf_isPseudoDefiningTag(tag)) {
+            U16 id = swf_GetDefineID(tag);
+            printf(" adds information to id %04d", id);
+            if(!idtab[id])
+                dumperror("Id %04d is not yet defined.\n", id);
+       }
+        else if(tag->id == ST_PLACEOBJECT) {
+            printf(" places id %04d at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
             if(swf_GetName(tag))
                 printf(" name \"%s\"",swf_GetName(tag));
         }
+       else if(tag->id == ST_PLACEOBJECT2) {
+           if(tag->data[0]&1)
+               printf(" moves");
+           else
+               printf(" places");
+           
+           if(tag->data[0]&2)
+               printf(" id %04d",swf_GetPlaceID(tag));
+           else
+               printf(" object");
+
+            printf(" at depth %04d", swf_GetDepth(tag));
+            if(swf_GetName(tag))
+                printf(" name \"%s\"",swf_GetName(tag));
+       }
         else if(tag->id == ST_REMOVEOBJECT) {
-            printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag));
+            printf(" removes id %04d from depth %04d", swf_GetPlaceID(tag), swf_GetDepth(tag));
         }
         else if(tag->id == ST_REMOVEOBJECT2) {
-            printf(" removes object from depth %04x", swf_GetDepth(tag));
+            printf(" removes object from depth %04d", swf_GetDepth(tag));
         }
+       else if(tag->id == ST_STARTSOUND) {
+           printf(" starts id %04d", swf_GetPlaceID(tag));
+       }
        else if(tag->id == ST_FRAMELABEL) {
            printf(" \"%s\"", tag->data);
+           if((framelabel && !issprite) ||
+              (spriteframelabel && issprite)) {
+               dumperror("Frame %d has more than one label", 
+                       issprite?spriteframe:mainframe);
+           }
+           if(issprite) spriteframelabel = tag->data;
+           else framelabel = tag->data;
+       }
+       else if(tag->id == ST_SHOWFRAME) {
+           char*label = issprite?spriteframelabel:framelabel;
+           int frame = issprite?spriteframe:mainframe;
+           int nframe = frame;
+           if(!label) {
+               while(tag->next && tag->next->id == ST_SHOWFRAME && tag->next->len == 0) {
+                   tag = tag->next;
+                   if(issprite) spriteframe++;
+                   else mainframe++;
+                   nframe++;
+               }
+           }
+           if(nframe == frame)
+               printf(" %d", frame);
+           else
+               printf(" %d-%d", frame, nframe);
+           if(label)
+               printf(" (label \"%s\")", label);
+           if(issprite) {spriteframe++; spriteframelabel = 0;}
+           if(!issprite) {mainframe++; framelabel = 0;}
        }
+
        if(tag->id == ST_DEFINEEDITTEXT) {
            handleEditText(tag);
+           printf("\n");
+       }
+       else if(tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
+           if(showtext)
+               handleText(tag);
+           else
+               printf("\n");
+       }
+       else if(tag->id == ST_PLACEOBJECT2) {
+       }
+       else {
+           printf("\n");
        }
         
-        printf("\n");
         sprintf(myprefix, "                %s", prefix);
 
         if(tag->id == ST_DEFINESPRITE) {
             sprintf(prefix, "         ");
+           if(issprite) {
+               dumperror("Sprite definition inside a sprite definition");
+           }
+           issprite = 1;
+           spriteframe = 0;
+           spriteframelabel = 0;
         }
         else if(tag->id == ST_END) {
             *prefix = 0;
+           issprite = 0;
+           spriteframelabel = 0;
            if(tag->len)
-               fprintf(stderr, "Error: End Tag not empty");
+               dumperror("End Tag not empty");
         }
         else if(tag->id == ST_DOACTION && action) {
             ActionTAG*actions;
@@ -385,10 +613,32 @@ int main (int argc,char ** argv)
        else if(tag->id == ST_DEFINEBUTTON2 && action) {
            dumpButton2Actions(tag, myprefix);
        }
+       else if(tag->id == ST_PLACEOBJECT2) {
+           if((*(U8*)tag->data)&0x80)
+               handlePlaceObject2(tag, myprefix);
+           else
+               printf("\n");
+       }
+       if(tag->len && hex) {
+           int t;
+           printf("                %s-=> ",prefix);
+           for(t=0;t<tag->len;t++) {
+               printf("%02x ", tag->data[t]);
+               if((t && !(t&15)) || (t==tag->len-1))
+               {
+                   if(t==tag->len-1)
+                       printf("\n");
+                   else
+                       printf("\n                %s-=> ",prefix);
+               }
+           }
+       }
         tag = tag->next;
+       fflush(stdout);
     }
 
     swf_FreeTags(&swf);
     return 0;
 }
 
+