fixed bug in swf_SetFixed(8)
[swftools.git] / lib / rfxswf.c
index 0301f29..05f6ad9 100644 (file)
 
 #include "mem.h"
 #include "rfxswf.h"
-
-#ifdef HAVE_JPEGLIB
-#define HAVE_BOOLEAN
-#include <jpeglib.h>
-#endif // HAVE_JPEGLIB
-
 #ifdef HAVE_ZLIB
 #include <zlib.h>
 #endif // HAVE_ZLIB
 #include <time.h>
 #endif
 
+#ifdef HAVE_IO_H
+#include <io.h>
+#endif
+
 #include "./bitio.h"
 #include "./MD5.h"
 
@@ -65,11 +63,6 @@ U32   swf_GetTagLen(TAG * t) { return t->len; }
 U8*   swf_GetTagLenPtr(TAG * t) { return &(t->data[t->len]); }
 U32   swf_GetTagPos(TAG * t)   { return t->pos; }
 
-// for future purpose: avoid high level lib functions to change tagpos/bitpos
-
-#define swf_SaveTagPos(tag)
-#define swf_RestoreTagPos(tag)
-
 void swf_SetTagPos(TAG * t,U32 pos)
 { swf_ResetReadBits(t);
   if (pos<=t->len) t->pos = pos;
@@ -268,7 +261,7 @@ double swf_GetFixed(TAG * t)
 }
 void swf_SetFixed(TAG * t, double f)
 {
-  U16 = (f-(int)f)*65536;
+  U16 fr = (U16)((f-(int)f)*65536);
   swf_SetU16(t, fr);
   swf_SetU16(t, (U16)f - (f<0 && fr!=0));
 }
@@ -276,11 +269,11 @@ float swf_GetFixed8(TAG * t)
 {
   U8 low =  swf_GetU8(t);
   U8 high = swf_GetU8(t);
-  return high + low*(1/256.0);
+  return (float)(high + low*(1/256.0));
 }
 void swf_SetFixed8(TAG * t, float f)
 {
-  U8 = (f-(int)f)*256;
+  U8 fr = (U8)((f-(int)f)*256);
   swf_SetU8(t, fr);
   swf_SetU8(t, (U8)f - (f<0 && fr!=0));
 }
@@ -328,7 +321,6 @@ void swf_GetRGBA(TAG * t, RGBA * col)
 
 void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
 {
-    GRADIENT dummy;
     int t;
     if(!tag) {
       memset(gradient, 0, sizeof(GRADIENT));
@@ -337,8 +329,8 @@ void swf_GetGradient(TAG * tag, GRADIENT * gradient, char alpha)
     U8 num = swf_GetU8(tag) & 15;
     if(gradient) {
        gradient->num = num;
-       gradient->rgba = rfx_calloc(sizeof(RGBA)*gradient->num);
-       gradient->ratios = rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
+       gradient->rgba = (RGBA*)rfx_calloc(sizeof(RGBA)*gradient->num);
+       gradient->ratios = (U8*)rfx_calloc(sizeof(gradient->ratios[0])*gradient->num);
     }
     for(t=0;t<num;t++)
     {
@@ -462,6 +454,20 @@ int swf_SetRect(TAG * t,SRECT * r)
   return 0;
 }
 
+SRECT swf_ClipRect(SRECT border, SRECT r)
+{
+    if(r.xmax > border.xmax) r.xmax = border.xmax;
+    if(r.ymax > border.ymax) r.ymax = border.ymax;
+    if(r.xmax < border.xmin) r.xmax = border.xmin;
+    if(r.ymax < border.ymin) r.ymax = border.ymin;
+    
+    if(r.xmin > border.xmax) r.xmin = border.xmax;
+    if(r.ymin > border.ymax) r.ymin = border.ymax;
+    if(r.xmin < border.xmin) r.xmin = border.xmin;
+    if(r.ymin < border.ymin) r.ymin = border.ymin;
+    return r;
+}
+
 void swf_ExpandRect(SRECT*src, SPOINT add)
 {
     if((src->xmin | src->ymin | src->xmax | src->ymax)==0) {
@@ -755,14 +761,14 @@ void  swf_SetPassword(TAG * t, const char * password)
     md5string = crypt_md5(password, salt);
 
     swf_SetU16(t,0);
-    swf_SetString(t, md5string);
+    swf_SetString(t, (U8*)md5string);
 }
 
 int swf_VerifyPassword(TAG * t, const char * password)
 {
     char*md5string1, *md5string2;
     char*x;
-    char*md5, *salt;
+    char*salt;
     int n;
 
     if(t->len >= 5 && t->pos==0 && 
@@ -1062,7 +1068,7 @@ void swf_UnFoldSprite(TAG * t)
 void swf_FoldSprite(TAG * t)
 {
   TAG*sprtag=t,*tmp;
-  U16 id,frames,tmpid;
+  U16 id,frames;
   int level;
   if(t->id!=ST_DEFINESPRITE)
       return;
@@ -1245,6 +1251,7 @@ int swf_ReadSWF2(reader_t*reader, SWF * swf)   // Reads SWF to memory (malloc'ed
        reader_init_zlibinflate(&zreader, reader);
        reader = &zreader;
     }
+    swf->compressed = 0; // derive from version number from now on
 
     reader_GetRect(reader, &swf->movieSize);
     reader->read(reader, &swf->frameRate, 2);
@@ -1289,9 +1296,10 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
 #ifdef INSERT_RFX_TAG
 
   if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
-      (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX)) 
+      (!swf->firstTag->next || (swf->firstTag->next->id != ST_REFLEX &&
+      (!swf->firstTag->next->next || (swf->firstTag->next->next->id!=ST_REFLEX)))))
   {
-      swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
+      swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),(U8*)"rfx",3);
   }
 
 #endif // INSERT_RFX_TAG
@@ -1306,7 +1314,7 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
     {
        TAG*scene = swf_InsertTagBefore(swf, swf->firstTag,ST_SCENEDESCRIPTION);
        swf_SetU16(scene, 1);
-       swf_SetString(scene, "Scene 1");
+       swf_SetString(scene, (U8*)"Scene 1");
        swf_SetU8(scene, 0);
     }
   }
@@ -1314,7 +1322,7 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
   if(swf->fileVersion >= 9) {
       if (swf->firstTag && swf->firstTag->id != ST_FILEATTRIBUTES)
       {
-         U32 flags = 0x8; // | 128 = usenetwork, | 8 = hasmetadata
+         U32 flags = 0x8; // | 128 = usenetwork, | 16 = Actionscript3 | 8 = hasmetadata
          swf_SetU32(swf_InsertTagBefore(swf, swf->firstTag,ST_FILEATTRIBUTES),flags);
       }
   }
@@ -1373,11 +1381,10 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
        It also means that we don't initialize our own zlib
        writer, but assume the caller provided one.
      */
-      if(swf->compressed) {
+      if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
        char*id = "CWS";
        writer->write(writer, id, 3);
-      }
-      else {
+      } else {
        char*id = "FWS";
        writer->write(writer, id, 3);
       }
@@ -1386,7 +1393,7 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
       PUT32(b4, swf->fileSize);
       writer->write(writer, b4, 4);
       
-      if(swf->compressed) {
+      if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6)) {
        writer_init_zlibdeflate(&zwriter, writer);
        writer = &zwriter;
       }
@@ -1412,7 +1419,7 @@ int  swf_WriteSWF2(writer_t*writer, SWF * swf)     // Writes SWF to file, return
     { if (swf_WriteTag2(writer, t)<0) return -1;
       t = swf_NextTag(t);
     }
-    if(swf->compressed) {
+    if(swf->compressed==1 || (swf->compressed==0 && swf->fileVersion>=6) || swf->compressed==8) {
       if(swf->compressed != 8) {
        zwriter.finish(&zwriter);
        return writer->pos - writer_lastpos;
@@ -1428,7 +1435,6 @@ int  swf_WriteSWF(int handle, SWF * swf)     // Writes SWF to file, returns leng
 {
   writer_t writer;
   int len = 0;
-  swf->compressed = 0;
   
   if(handle<0) {
     writer_init_nullwriter(&writer);
@@ -1441,22 +1447,6 @@ int  swf_WriteSWF(int handle, SWF * swf)     // Writes SWF to file, returns leng
   return len;
 }
 
-int  swf_WriteSWC(int handle, SWF * swf)     // Writes SWF to file, returns length or <0 if fails
-{
-  writer_t writer;
-  int len = 0;
-  swf->compressed = 1;
-
-  if(handle<0) {
-    writer_init_nullwriter(&writer);
-    len = swf_WriteSWF2(&writer, swf);
-  }
-  writer_init_filewriter(&writer, handle);
-  len = swf_WriteSWF2(&writer, swf);
-  writer.finish(&writer);
-  return len;
-}
-
 int swf_WriteHeader2(writer_t*writer,SWF * swf)
 {
   SWF myswf;
@@ -1493,7 +1483,7 @@ int swf_WriteCGI(SWF * swf)
 
 SWF* swf_CopySWF(SWF*swf)
 {
-    SWF*nswf = rfx_alloc(sizeof(SWF));
+    SWF*nswf = (SWF*)rfx_alloc(sizeof(SWF));
     TAG*tag, *ntag;
     memcpy(nswf, swf, sizeof(SWF));
     nswf->firstTag = 0;
@@ -1522,17 +1512,18 @@ void swf_FreeTags(SWF * swf)                 // Frees all malloc'ed memory for t
 
 // include advanced functions
 
-#include "modules/swfdump.c"
-#include "modules/swfshape.c"
-#include "modules/swftext.c"
-#include "modules/swffont.c"
-#include "modules/swfobject.c"
-#include "modules/swfbutton.c"
-#include "modules/swftools.c"
-#include "modules/swfcgi.c"
-#include "modules/swfbits.c"
-#include "modules/swfaction.c"
-#include "modules/swfsound.c"
-#include "modules/swfdraw.c"
-#include "modules/swfrender.c"
-#include "modules/swffilter.c"
+//#include "modules/swfdump.c"
+//#include "modules/swfshape.c"
+//#include "modules/swftext.c"
+//#include "modules/swffont.c"
+//#include "modules/swfobject.c"
+//#include "modules/swfbutton.c"
+//#include "modules/swftools.c"
+//#include "modules/swfcgi.c"
+//#include "modules/swfbits.c"
+//#include "modules/swfaction.c"
+//#include "modules/swfabc.c"
+//#include "modules/swfsound.c"
+//#include "modules/swfdraw.c"
+//#include "modules/swfrender.c"
+//#include "modules/swffilter.c"