moved memory handling to mem.c, mem.h
[swftools.git] / lib / rfxswf.c
index dbf0cd6..d817479 100644 (file)
@@ -24,6 +24,7 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
+#include "mem.h"
 #include "rfxswf.h"
 
 #ifdef HAVE_JPEGLIB
 #include "./bitio.h"
 #include "./MD5.h"
 
-// memory allocation
-
-void* rfx_alloc(int size)
-{
-  void*ptr;
-  if(size == 0) {
-    //*(int*)0 = 0xdead;
-    //fprintf(stderr, "Warning: Zero alloc\n");
-    return 0;
-  }
-
-  ptr = malloc(size);
-  if(!ptr) {
-    fprintf(stderr, "FATAL: Out of memory\n");
-    /* TODO: we should send a signal, so that the debugger kicks in */
-    exit(1);
-  }
-  return ptr;
-}
-void* rfx_realloc(void*data, int size)
-{
-  void*ptr;
-  if(size == 0) {
-    //*(int*)0 = 0xdead;
-    //fprintf(stderr, "Warning: Zero realloc\n");
-    rfx_free(data);
-    return 0;
-  }
-  if(!data) {
-    ptr = malloc(size);
-  } else {
-    ptr = realloc(data, size);
-  }
-
-  if(!ptr) {
-    fprintf(stderr, "FATAL: Out of memory\n");
-    /* TODO: we should send a signal, so that the debugger kicks in */
-    exit(1);
-  }
-  return ptr;
-}
-void* rfx_calloc(int size)
-{
-  void*ptr;
-  if(size == 0) {
-    //*(int*)0 = 0xdead;
-    //fprintf(stderr, "Warning: Zero alloc\n");
-    return 0;
-  }
-#ifdef HAVE_CALLOC
-  ptr = calloc(size);
-#else
-  ptr = malloc(size);
-#endif
-  if(!ptr) {
-    fprintf(stderr, "FATAL: Out of memory\n");
-    /* TODO: we should send a signal, so that the debugger kicks in */
-    exit(1);
-  }
-#ifndef HAVE_CALLOC
-  memset(ptr, 0, size);
-#endif
-  return ptr;
-}
-
-void rfx_free(void*ptr)
-{
-  if(!ptr)
-    return;
-  free(ptr);
-}
-
-#ifdef MEMORY_INFO
-long rfx_memory_used()
-{
-}
-
-char* rfx_memory_used_str()
-{
-}
-#endif
-
 // internal constants
 
 #define MALLOC_SIZE     128
@@ -633,6 +552,7 @@ int swf_SetMatrix(TAG * t,MATRIX * m)
     nbits = swf_CountBits(m->sx,0);
     nbits = swf_CountBits(m->sy,nbits);
     if(nbits>=32) {
+        /* TODO: happens on AMD64 systems for normal values? */
        fprintf(stderr,"rfxswf: Error: matrix values too large\n");
        nbits = 31;
     }
@@ -668,7 +588,7 @@ int swf_SetMatrix(TAG * t,MATRIX * m)
   return 0;
 }
 
-int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bool
+int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha)
 { CXFORM cxf;
   int hasadd;
   int hasmul;
@@ -782,6 +702,7 @@ void  swf_SetPassword(TAG * t, const char * password)
     fprintf(stderr, "rfxswf: Warning- no usable random generator found\n");
     fprintf(stderr, "Your password will be vulnerable to dictionary attacks\n");
 #endif
+    salt[2] = 0;
     
     md5string = crypt_md5(password, salt);
 
@@ -1314,9 +1235,9 @@ int  swf_WriteSWF2(struct writer_t*writer, SWF * swf)     // Writes SWF to file,
 
 #ifdef INSERT_RFX_TAG
 
-  if (swf->firstTag && swf->firstTag->next &&
-      (swf->firstTag->id != ST_REFLEX || swf->firstTag->next->id != ST_REFLEX)
-     ) {
+  if ((swf->firstTag && swf->firstTag->id != ST_REFLEX) &&
+      (!swf->firstTag->next || swf->firstTag->next->id != ST_REFLEX)) 
+  {
       swf_SetBlock(swf_InsertTagBefore(swf, swf->firstTag,ST_REFLEX),"rfx",3);
   }
 
@@ -1436,6 +1357,7 @@ int  swf_WriteSWF(int handle, SWF * swf)     // Writes SWF to file, returns leng
   if(handle<0) {
     writer_init_nullwriter(&writer);
     len = swf_WriteSWF2(&writer, swf);
+    return len;
   }
   writer_init_filewriter(&writer, handle);
   len = swf_WriteSWF2(&writer, swf);