changed from malloc to rfx_alloc.
authorkramm <kramm>
Fri, 5 Nov 2004 17:29:52 +0000 (17:29 +0000)
committerkramm <kramm>
Fri, 5 Nov 2004 17:29:52 +0000 (17:29 +0000)
12 files changed:
lib/h.263/swfvideo.c
lib/modules/swfaction.c
lib/modules/swfbits.c
lib/modules/swfcgi.c
lib/modules/swfdraw.c
lib/modules/swffont.c
lib/modules/swfobject.c
lib/modules/swfshape.c
lib/modules/swfsound.c
lib/modules/swftext.c
lib/modules/swftools.c
lib/rfxswf.c

index 05a1df8..678d578 100644 (file)
@@ -51,15 +51,12 @@ void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width
     stream->height = height;
     stream->bbx = width/16;
     stream->bby = height/16;
-    stream->current = (YUV*)malloc(width*height*sizeof(YUV));
-    stream->oldpic = (YUV*)malloc(width*height*sizeof(YUV));
-    stream->mvdx = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
-    stream->mvdy = (int*)malloc(stream->bbx*stream->bby*sizeof(int));
+    stream->current = (YUV*)rfx_calloc(width*height*sizeof(YUV));
+    stream->oldpic = (YUV*)rfx_calloc(width*height*sizeof(YUV));
+    stream->mvdx = (int*)rfx_alloc(stream->bbx*stream->bby*sizeof(int));
+    stream->mvdy = (int*)rfx_alloc(stream->bbx*stream->bby*sizeof(int));
     stream->do_motion = 0;
 
-    memset(stream->oldpic, 0, width*height*sizeof(YUV));
-    memset(stream->current, 0, width*height*sizeof(YUV));
-
     assert((stream->width&15) == 0);
     assert((stream->height&15) == 0);
     assert((stream->bbx*16) == stream->width);
@@ -75,10 +72,10 @@ void swf_SetVideoStreamDefine(TAG*tag, VIDEOSTREAM*stream, U16 frames, U16 width
 }
 void swf_VideoStreamClear(VIDEOSTREAM*stream)
 {
-    free(stream->oldpic);stream->oldpic = 0;
-    free(stream->current);stream->current = 0;
-    free(stream->mvdx);stream->mvdx=0;
-    free(stream->mvdy);stream->mvdy=0;
+    rfx_free(stream->oldpic);stream->oldpic = 0;
+    rfx_free(stream->current);stream->current = 0;
+    rfx_free(stream->mvdx);stream->mvdx=0;
+    rfx_free(stream->mvdy);stream->mvdy=0;
 }
 
 typedef struct _block_t
@@ -1396,7 +1393,7 @@ void test_copy_diff()
     VIDEOSTREAM stream;
     VIDEOSTREAM* s = &stream;
     TAG*tag;
-    RGBA*pic = malloc(256*256*sizeof(RGBA));
+    RGBA*pic = rfx_alloc(256*256*sizeof(RGBA));
     block_t fb;
     int x,y;
     int bx,by;
@@ -1451,8 +1448,7 @@ void mkblack()
     RGBA* pic = 0;
     VIDEOSTREAM stream;
    
-    pic = malloc(width*height*4);
-    memset(pic, 0, width*height*4);
+    pic = rfx_calloc(width*height*4);
 
     memset(&swf,0,sizeof(SWF));
     memset(&obj,0,sizeof(obj));
@@ -1537,10 +1533,10 @@ int main(int argn, char*argv[])
     memset(&stream, 0, sizeof(stream));
 
     getPNG(fname, &width, &height, &data);
-    pic = (RGBA*)malloc(width*height*sizeof(RGBA));
-    pic2 = (RGBA*)malloc(width*height*sizeof(RGBA));
+    pic = (RGBA*)rfx_alloc(width*height*sizeof(RGBA));
+    pic2 = (RGBA*)rfx_alloc(width*height*sizeof(RGBA));
     memcpy(pic, data, width*height*sizeof(RGBA));
-    free(data);
+    rfx_free(data);
 
     printf("Compressing %s, size %dx%d\n", fname, width, height);
 
index d897c10..33964c1 100644 (file)
@@ -154,8 +154,7 @@ ActionTAG* swf_ActionGet(TAG*tag)
     U8*data;
     while(op)
     {
-       action->next = (ActionTAG*)malloc(sizeof(ActionTAG));
-       memset(action->next, 0, sizeof(ActionTAG));
+       action->next = (ActionTAG*)rfx_calloc(sizeof(ActionTAG));
        action->next->prev = action;
        action->next->next = 0;
        action->next->parent = tmp.next;
@@ -168,7 +167,7 @@ ActionTAG* swf_ActionGet(TAG*tag)
            length = swf_GetU16(tag);
 
        if(length) {
-           data = malloc(length);
+           data = rfx_alloc(length);
            swf_GetBlock(tag, data, length);
        } else {
          data = 0;
@@ -196,11 +195,11 @@ void swf_ActionFree(ActionTAG*action)
     {
        ActionTAG*tmp;
        if(action->data && action->data != action->tmp)
-           free(action->data);
+           rfx_free(action->data);
        
        tmp = action;
        action=action->next;
-       free(tmp);
+       rfx_free(tmp);
     }
 }
 
@@ -515,7 +514,7 @@ void swf_DumpActions(ActionTAG*atag, char*prefix)
     }
 
 #ifdef MAX_LOOKUP
-  for (t=0;t<MAX_LOOKUP;t++) if (lookup[t]) free(lookup[t]);
+  for (t=0;t<MAX_LOOKUP;t++) if (lookup[t]) rfx_free(lookup[t]);
 #endif
 }
 
@@ -603,13 +602,13 @@ int swf_ActionEnumerate(ActionTAG*atag, char*(*callback)(char*), int type)
                if(replacement)
                {
                    int newlen = strlen(replacement);
-                   char * newdata = malloc(atag->len - replacelen + newlen);
+                   char * newdata = rfx_alloc(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);
+                   rfx_free(atag->data);
                    atag->data = newdata;
                    data = &atag->data[rpos+newlen+1];
                }
@@ -637,7 +636,7 @@ void swf_ActionEnumerateURLs(ActionTAG*atag, char*(*callback)(char*))
 /*static ActionTAG* swf_ActionStart()
 {
     ActionTAG*atag;
-    atag = (ActionTAG*)malloc(sizeof(ActionTAG));
+    atag = (ActionTAG*)rfx_alloc(sizeof(ActionTAG));
     atag->prev = 0;
     atag->next = 0;
     atag->parent = 0;
@@ -655,7 +654,7 @@ void swf_ActionEnd(ActionTAG* atag)
     } 
 
     last->prev->next = 0;
-    free(last);
+    rfx_free(last);
 }*/
 
 static ActionTAG*lastATAG(ActionTAG*atag)
@@ -671,7 +670,7 @@ static ActionTAG*lastATAG(ActionTAG*atag)
 ActionTAG* swf_AddActionTAG(ActionTAG*atag, U8 op, U8*data, U16 len)
 {
     ActionTAG*tmp;
-    tmp = (ActionTAG*)malloc(sizeof(ActionTAG));
+    tmp = (ActionTAG*)rfx_alloc(sizeof(ActionTAG));
     tmp->next = 0;
     if(atag) {
        tmp->prev = atag;
@@ -1015,14 +1014,14 @@ ActionTAG* action_PushLookup16(ActionTAG*atag, U16 index)
 ActionTAG* action_PushString(ActionTAG*atag, char*str) 
 {
     int l = strlen(str);
-    char*ptr = (char*)malloc(l+2);
+    char*ptr = (char*)rfx_alloc(l+2);
     ptr[0] = 0; // string
     strcpy(&ptr[1], str);
     return swf_AddActionTAG(atag, ACTION_PUSH, (U8*)ptr, l+2);
 }
 ActionTAG* action_PushFloat(ActionTAG*atag, float f)
 {
-    char*ptr = (char*)malloc(5);
+    char*ptr = (char*)rfx_alloc(5);
     U32 fd = *(U32*)&f;
     ptr[0] = 1; //float
     ptr[1]  = fd;
@@ -1033,7 +1032,7 @@ ActionTAG* action_PushFloat(ActionTAG*atag, float f)
 }
 ActionTAG* action_PushDouble(ActionTAG*atag, double d) 
 {
-    char*ptr = (char*)malloc(9);
+    char*ptr = (char*)rfx_alloc(9);
     U8*dd = (U8*)&d;
     ptr[0] = 6; //double
 #ifdef WORDS_BIGENDIAN
@@ -1068,7 +1067,7 @@ ActionTAG* action_GetUrl(ActionTAG*atag, char* url, char* label)
 {
     int l1= strlen(url);
     int l2= strlen(label);
-    char*ptr = malloc(l1+l2+2);
+    char*ptr = rfx_alloc(l1+l2+2);
     strcpy(ptr, url);
     strcpy(&ptr[l1+1], label);
     return swf_AddActionTAG(atag, ACTION_GETURL, ptr, l1+l2+2);
@@ -1096,7 +1095,7 @@ ActionTAG* swf_ActionCompile(const char* source, int version)
     swf_SetBlock(tag, buffer, len);
     swf_SetU8(tag, 0);
 
-    free(buffer);
+    rfx_free(buffer);
 
     a = swf_ActionGet(tag);
     swf_DeleteTag(tag);
index b92b06b..b85eb58 100644 (file)
@@ -37,7 +37,7 @@ typedef struct _JPEGDESTMGR
 
 static void RFXSWF_init_destination(j_compress_ptr cinfo) 
 { JPEGDESTMGR * dmgr = (JPEGDESTMGR *)cinfo->dest;
-  dmgr->buffer = (JOCTET*)malloc(OUTBUFFER_SIZE);
+  dmgr->buffer = (JOCTET*)rfx_alloc(OUTBUFFER_SIZE);
   dmgr->mgr.next_output_byte = dmgr->buffer;
   dmgr->mgr.free_in_buffer = OUTBUFFER_SIZE;
 }
@@ -53,7 +53,7 @@ static boolean RFXSWF_empty_output_buffer(j_compress_ptr cinfo)
 static void RFXSWF_term_destination(j_compress_ptr cinfo) 
 { JPEGDESTMGR * dmgr = (JPEGDESTMGR *)cinfo->dest;
   swf_SetBlock(dmgr->t,(U8*)dmgr->buffer,OUTBUFFER_SIZE-dmgr->mgr.free_in_buffer);
-  free(dmgr->buffer);
+  rfx_free(dmgr->buffer);
   dmgr->mgr.free_in_buffer = 0;
 }
 
@@ -63,10 +63,8 @@ JPEGBITS * swf_SetJPEGBitsStart(TAG * t,int width,int height,int quality)
         
   // redirect compression lib output to local SWF Tag structure
   
-  jpeg = (JPEGDESTMGR *)malloc(sizeof(JPEGDESTMGR));
-  if (!jpeg) return NULL;
+  jpeg = (JPEGDESTMGR *)rfx_calloc(sizeof(JPEGDESTMGR));
   
-  memset(jpeg,0x00,sizeof(JPEGDESTMGR));
   jpeg->cinfo.err = jpeg_std_error(&jpeg->jerr);
 
   jpeg_create_compress(&jpeg->cinfo);
@@ -116,7 +114,7 @@ int swf_SetJPEGBitsFinish(JPEGBITS * jpegbits)
 { JPEGDESTMGR * jpeg = (JPEGDESTMGR *)jpegbits;
   if (!jpeg) return -1;
   jpeg_finish_compress(&jpeg->cinfo);
-  free(jpeg);
+  rfx_free(jpeg);
   return 0;
 }
 
@@ -178,7 +176,7 @@ int swf_SetJPEGBits(TAG * t,char * fname,int quality)
   jpeg_start_decompress(&cinfo);
 
   out = swf_SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
-  scanline = (U8*)malloc(4*cinfo.output_width);
+  scanline = (U8*)rfx_alloc(4*cinfo.output_width);
   
   if (scanline) 
   { int y;
@@ -322,7 +320,7 @@ RGBA* swf_JPEG2TagToImage(TAG*tag, int*width, int*height)
     jpeg_read_header(&cinfo, TRUE);
     *width = cinfo.image_width;
     *height = cinfo.image_height;
-    dest = malloc(sizeof(RGBA)*cinfo.image_width*cinfo.image_height);
+    dest = rfx_alloc(sizeof(RGBA)*cinfo.image_width*cinfo.image_height);
     
     jpeg_start_decompress(&cinfo);
     int y;
@@ -332,9 +330,9 @@ RGBA* swf_JPEG2TagToImage(TAG*tag, int*width, int*height)
         int x;
         jpeg_read_scanlines(&cinfo,&to,1);
         for(x=cinfo.output_width-1;x>=0;--x) {
-            int b = to[x*3+0];
+            int r = to[x*3+0];
             int g = to[x*3+1];
-            int r = to[x*3+2];
+            int b = to[x*3+2];
             line[x].r = r;
             line[x].g = g;
             line[x].b = b;
@@ -357,7 +355,7 @@ RGBA* swf_JPEG2TagToImage(TAG*tag, int*width, int*height)
 
 int RFXSWF_deflate_wraper(TAG * t,z_stream * zs,boolean finish)
 { 
-  U8*data=malloc(OUTBUFFER_SIZE);
+  U8*data=rfx_alloc(OUTBUFFER_SIZE);
   zs->next_out = data;
   zs->avail_out = OUTBUFFER_SIZE;
   while (1)
@@ -368,7 +366,7 @@ int RFXSWF_deflate_wraper(TAG * t,z_stream * zs,boolean finish)
 #ifdef DEBUG_RFXSWF
       fprintf(stderr,"rfxswf: zlib compression error (%i)\n",status);
 #endif
-      free(data);
+      rfx_free(data);
       return status;
     }
 
@@ -383,7 +381,7 @@ int RFXSWF_deflate_wraper(TAG * t,z_stream * zs,boolean finish)
   }
 
   if(!finish) {
-      free(data);
+      rfx_free(data);
       return 0;
   }
 
@@ -394,7 +392,7 @@ int RFXSWF_deflate_wraper(TAG * t,z_stream * zs,boolean finish)
 #ifdef DEBUG_RFXSWF
       fprintf(stderr,"rfxswf: zlib compression error (%i)\n",status);
 #endif
-      free(data);
+      rfx_free(data);
       return status;
     }
 
@@ -408,7 +406,7 @@ int RFXSWF_deflate_wraper(TAG * t,z_stream * zs,boolean finish)
     if(status == Z_STREAM_END)
        break;
   }
-  free(data);
+  rfx_free(data);
   return 0;
 }
 
@@ -460,7 +458,7 @@ int swf_SetLosslessBitsIndexed(TAG * t,U16 width,U16 height,U8 * bitmap,RGBA * p
     
   if (!pal)     // create default palette for grayscale images
   { int i;
-    pal = malloc(256*sizeof(RGBA));
+    pal = rfx_alloc(256*sizeof(RGBA));
     for (i=0;i<256;i++) { pal[i].r = pal[i].g = pal[i].b = i; pal[i].a = 0xff;}
     ncolors = 256;
   }
@@ -483,7 +481,7 @@ int swf_SetLosslessBitsIndexed(TAG * t,U16 width,U16 height,U8 * bitmap,RGBA * p
 
     if (deflateInit(&zs,Z_DEFAULT_COMPRESSION)==Z_OK)
     { U8 * zpal;                    // compress palette
-      if ((zpal = malloc(ncolors*4)))
+      if ((zpal = rfx_alloc(ncolors*4)))
       { U8 * pp = zpal;
         int i;
 
@@ -530,12 +528,12 @@ int swf_SetLosslessBitsIndexed(TAG * t,U16 width,U16 height,U8 * bitmap,RGBA * p
 
         deflateEnd(&zs);
 
-        free(zpal);
+        rfx_free(zpal);
       } else res = -2; // memory error
     } else res = -3; // zlib error
   }
   
-  if (!palette) free(pal);
+  if (!palette) rfx_free(pal);
 
   return res;
 }
@@ -554,7 +552,7 @@ RGBA* swf_DefineLosslessBitsTagToImage(TAG*tag, int*dwidth, int*dheight)
     int pos2=0;
     char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
     int t,x,y;
-    RGBA*palette;
+    RGBA*palette = 0;
     U8*data,*data2;
     RGBA*dest;
     if(tag->id != ST_DEFINEBITSLOSSLESS &&
@@ -578,7 +576,7 @@ RGBA* swf_DefineLosslessBitsTagToImage(TAG*tag, int*dwidth, int*dheight)
     *dwidth = width = swf_GetU16(tag);
     *dheight = height = swf_GetU16(tag);
     
-    dest = malloc(sizeof(RGBA)*width*height);
+    dest = rfx_alloc(sizeof(RGBA)*width*height);
 
     if(format == 3) cols = swf_GetU8(tag) + 1;
     else            cols = 0;
@@ -587,9 +585,9 @@ RGBA* swf_DefineLosslessBitsTagToImage(TAG*tag, int*dwidth, int*dheight)
     datalen = (width*height*bpp/8+cols*8);
     do {
        if(data)
-           free(data);
+           rfx_free(data);
        datalen+=4096;
-       data = malloc(datalen);
+       data = rfx_alloc(datalen);
        error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
     } while(error == Z_BUF_ERROR);
     if(error != Z_OK) {
@@ -597,15 +595,17 @@ RGBA* swf_DefineLosslessBitsTagToImage(TAG*tag, int*dwidth, int*dheight)
        return 0;
     }
     pos = 0;
-    
-    palette = (RGBA*)malloc(cols*sizeof(RGBA));
-    for(t=0;t<cols;t++) {
-       palette[t].r = data[pos++];
-       palette[t].g = data[pos++];
-       palette[t].b = data[pos++];
-       if(alpha) {
-           palette[t].a = data[pos++];
-       }
+   
+    if(cols) {
+        palette = (RGBA*)rfx_alloc(cols*sizeof(RGBA));
+        for(t=0;t<cols;t++) {
+            palette[t].r = data[pos++];
+            palette[t].g = data[pos++];
+            palette[t].b = data[pos++];
+            if(alpha) {
+                palette[t].a = data[pos++];
+            }
+        }
     }
 
     for(y=0;y<height;y++) {
@@ -640,8 +640,8 @@ RGBA* swf_DefineLosslessBitsTagToImage(TAG*tag, int*dwidth, int*dheight)
        pos+=((srcwidth+3)&~3)-srcwidth; //align
     }
     if(palette)
-        free(palette);
-    free(data);
+        rfx_free(palette);
+    rfx_free(data);
     return dest;
 }
 
@@ -673,7 +673,7 @@ int swf_SetJPEGBits3(TAG * tag,U16 width,U16 height,RGBA* bitmap, int quality)
   swf_SetJPEGBitsFinish(jpeg);
   PUT32(&tag->data[pos], tag->len - pos - 4);
 
-  data=malloc(OUTBUFFER_SIZE);
+  data=rfx_alloc(OUTBUFFER_SIZE);
   memset(&zs,0x00,sizeof(z_stream));
 
   if (deflateInit(&zs,Z_DEFAULT_COMPRESSION)!=Z_OK) {
@@ -727,7 +727,7 @@ int swf_SetJPEGBits3(TAG * tag,U16 width,U16 height,RGBA* bitmap, int quality)
   }
 
   deflateEnd(&zs);
-  free(data);
+  rfx_free(data);
   return 0;
 }
 #endif
index a1656e5..fec09e3 100644 (file)
@@ -69,7 +69,7 @@ static void swf_cgienv(unsigned char * var)
   // fprintf(stderr,"%s\n",var);
 
   
-  buf = (unsigned char*)malloc(strlen(var) + sizeof(PREFIX) + 2);
+  buf = (unsigned char*)rfx_alloc(strlen(var) + sizeof(PREFIX) + 2);
   if (!buf) return;
 
   strcpy(buf, PREFIX);
@@ -107,7 +107,7 @@ static void swf_cgienv(unsigned char * var)
   }
 
   if ((oldval = getenv(buf)))
-  { newval = (unsigned char*)malloc(strlen(oldval) + strlen(buf) + strlen(&c[1]) + 3);
+  { newval = (unsigned char*)rfx_alloc(strlen(oldval) + strlen(buf) + strlen(&c[1]) + 3);
     if (!newval) return;
 
     c[0] = '=';
@@ -124,8 +124,8 @@ static void swf_cgienv(unsigned char * var)
   putenv(newval);
         
   if (oldval)
-  { free(oldval);
-    free(buf);
+  { rfx_free(oldval);
+    rfx_free(buf);
   }
 }
 
@@ -155,7 +155,7 @@ char * swf_postread()
   if (!buf) return NULL;
         
   size = atoi(buf);
-  buf = (unsigned char*)malloc(size + 1);
+  buf = (unsigned char*)rfx_alloc(size + 1);
   if (buf)
   { do
     { got = fread(buf + sofar, 1, size - sofar, stdin);
@@ -174,14 +174,14 @@ void swf_uncgi()
   if ((query) && strlen(query))
   { dupquery = strdup(query);
     swf_scanquery(dupquery);
-    free(dupquery);
+    rfx_free(dupquery);
   }
 
   method = getenv("REQUEST_METHOD");
   if ((method) && ! strcmp(method, "POST"))
   { query = swf_postread();
     if ((query)&&(query[0]!=0)) swf_scanquery(query);
-    free(query);
+    rfx_free(query);
   }
   
 }
index 723b253..66f169c 100644 (file)
@@ -23,8 +23,7 @@ static void swf_ShapeDrawerClear(drawer_t*draw);
 
 static void swf_ShapeDrawerInit(drawer_t*draw, TAG*tag, int fillstylebits, int linestylebits)
 {
-    SWFSHAPEDRAWER*sdraw = malloc(sizeof(SWFSHAPEDRAWER));
-    memset(sdraw, 0, sizeof(SWFSHAPEDRAWER));
+    SWFSHAPEDRAWER*sdraw = rfx_calloc(sizeof(SWFSHAPEDRAWER));
     draw->internal = sdraw;
 
     draw->setLineStyle = swf_ShapeDrawerSetLineStyle;
@@ -184,7 +183,7 @@ static void swf_ShapeDrawerClear(drawer_t*draw)
     swf_ShapeFree(sdraw->shape);
     sdraw->shape = 0;
 
-    free(draw->internal);
+    rfx_free(draw->internal);
     draw->internal = 0;
 }
 
@@ -197,14 +196,14 @@ SRECT swf_ShapeDrawerGetBBox(drawer_t*draw)
 SHAPE* swf_ShapeDrawerToShape(drawer_t*draw)
 {
     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
-    SHAPE* shape = malloc(sizeof(SHAPE));
+    SHAPE* shape = rfx_alloc(sizeof(SHAPE));
     if(!sdraw->isfinished) {
        fprintf(stderr, "Warning: you should Finish() your drawer before calling DrawerToShape");
        swf_ShapeDrawerFinish(draw);
     }
     memcpy(shape, sdraw->shape, sizeof(SHAPE));
     shape->bitlen = (sdraw->tag->len-1)*8;
-    shape->data = (U8*)malloc(sdraw->tag->len-1);
+    shape->data = (U8*)rfx_alloc(sdraw->tag->len-1);
     memcpy(shape->data, &sdraw->tag->data[1], sdraw->tag->len-1);
     return shape;
 }
index 8254374..6344fd3 100644 (file)
@@ -139,24 +139,19 @@ SWFFONT* swf_LoadTrueTypeFont(char*filename)
        return 0;
     }
 
-    font = malloc(sizeof(SWFFONT));
-    memset(font, 0, sizeof(SWFFONT));
+    font = rfx_calloc(sizeof(SWFFONT));
     font->id = -1;
     font->version = 2;
-    font->layout = malloc(sizeof(SWFLAYOUT));
-    memset(font->layout, 0, sizeof(SWFLAYOUT));
-    font->layout->bounds = malloc(face->num_glyphs*sizeof(SRECT));
+    font->layout = rfx_calloc(sizeof(SWFLAYOUT));
+    font->layout->bounds = rfx_calloc(face->num_glyphs*sizeof(SRECT));
     font->style =  ((face->style_flags&FT_STYLE_FLAG_ITALIC)?FONT_STYLE_ITALIC:0)
                  |((face->style_flags&FT_STYLE_FLAG_BOLD)?FONT_STYLE_BOLD:0);
     font->encoding = FONT_ENCODING_UNICODE;
-    font->glyph2ascii = malloc(face->num_glyphs*sizeof(U16));
-    memset(font->glyph2ascii, 0, face->num_glyphs*sizeof(U16));
+    font->glyph2ascii = rfx_calloc(face->num_glyphs*sizeof(U16));
     font->maxascii = 0;
-    font->glyph = malloc(face->num_glyphs*sizeof(SWFGLYPH));
-    memset(font->glyph, 0, face->num_glyphs*sizeof(SWFGLYPH));
+    font->glyph = rfx_calloc(face->num_glyphs*sizeof(SWFGLYPH));
     if(FT_HAS_GLYPH_NAMES(face)) {
-       font->glyphnames = malloc(face->num_glyphs*sizeof(char*));
-       memset(font->glyphnames,0,face->num_glyphs*sizeof(char*));
+       font->glyphnames = rfx_calloc(face->num_glyphs*sizeof(char*));
     }
 
     font->layout->ascent = abs(face->ascender)*FT_SCALE*loadfont_scale*20/FT_SUBPIXELS/2; //face->bbox.xMin;
@@ -214,7 +209,7 @@ SWFFONT* swf_LoadTrueTypeFont(char*filename)
     if(full_unicode)
        font->maxascii = 65535;
     
-    font->ascii2glyph = malloc(font->maxascii*sizeof(int));
+    font->ascii2glyph = rfx_calloc(font->maxascii*sizeof(int));
     
     for(t=0;t<font->maxascii;t++) {
        int g = FT_Get_Char_Index(face, t);
@@ -232,7 +227,7 @@ SWFFONT* swf_LoadTrueTypeFont(char*filename)
 
     font->numchars = 0;
 
-    glyph2glyph = (int*)malloc(face->num_glyphs*sizeof(int));
+    glyph2glyph = (int*)rfx_calloc(face->num_glyphs*sizeof(int));
 
     for(t=0; t < face->num_glyphs; t++) {
        FT_Glyph glyph;
@@ -325,7 +320,7 @@ SWFFONT* swf_LoadTrueTypeFont(char*filename)
            font->ascii2glyph[t] = glyph2glyph[font->ascii2glyph[t]];
        }
     }
-    free(glyph2glyph);
+    rfx_free(glyph2glyph);
 
     FT_Done_Face(face);
     FT_Done_FreeType(ftlibrary);ftlibrary=0;
@@ -388,16 +383,14 @@ SWFFONT* swf_LoadT1Font(char*filename)
     underline = T1_GetUnderlinePosition(nr);
     bbox = T1_GetFontBBox(nr);
 
-    font = (SWFFONT*)malloc(sizeof(SWFFONT));
-    memset(font, 0, sizeof(SWFFONT));
+    font = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
 
     font->version = 2;
     if(fontname) 
        font->name = (U8*)strdup(fontname);
     else 
        font->name = 0;
-    font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
-    memset(font->layout, 0, sizeof(SWFLAYOUT));
+    font->layout = (SWFLAYOUT*)rfx_calloc(sizeof(SWFLAYOUT));
 
     num = 0;
     charname = charnames;
@@ -419,23 +412,18 @@ SWFFONT* swf_LoadT1Font(char*filename)
     
     font->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
 
-    font->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
-    memset(font->glyph, 0, num*sizeof(SWFGLYPH));
-    font->glyph2ascii = (U16*)malloc(num*sizeof(U16));
-    memset(font->glyph2ascii, 0, num*sizeof(U16));
-    font->ascii2glyph = (int*)malloc(font->maxascii*sizeof(int));
-    memset(font->ascii2glyph, -1, font->maxascii*sizeof(int));
+    font->glyph = (SWFGLYPH*)rfx_calloc(num*sizeof(SWFGLYPH));
+    font->glyph2ascii = (U16*)rfx_calloc(num*sizeof(U16));
+    font->ascii2glyph = (int*)rfx_calloc(font->maxascii*sizeof(int));
     font->layout->ascent = (U16)(underline - bbox.lly);
     font->layout->descent = (U16)(bbox.ury - underline);
     font->layout->leading = (U16)(font->layout->ascent - 
                             font->layout->descent -
                             (bbox.lly - bbox.ury));
-    font->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
-    memset(font->layout->bounds, 0, sizeof(SRECT)*num);
+    font->layout->bounds = (SRECT*)rfx_calloc(sizeof(SRECT)*num);
     font->layout->kerningcount = 0;
     font->layout->kerning = 0;
-    font->glyphnames = malloc(num*sizeof(char*));
-    memset(font->glyphnames, 0, num*sizeof(char*));
+    font->glyphnames = rfx_calloc(num*sizeof(char*));
   
     num = 0;
 
@@ -503,7 +491,7 @@ SWFFONT* swf_LoadT1Font(char*filename)
     T1_DeleteFont(nr);
 
     for(t=0;t<256;t++)
-       free(encoding[t]);
+       rfx_free(encoding[t]);
     return font;
 }
 
@@ -519,8 +507,7 @@ SWFFONT* swf_LoadT1Font(char*filename)
 
 SWFFONT* swf_DummyFont()
 {
-    SWFFONT*font = (SWFFONT*)malloc(sizeof(SWFFONT));
-    memset(font, 0, sizeof(SWFFONT));
+    SWFFONT*font = (SWFFONT*)rfx_calloc(sizeof(SWFFONT));
     return font;
 }
 
index 5789fef..d93a928 100644 (file)
@@ -185,7 +185,7 @@ void swf_GetPlaceObject(TAG * tag,SWFPLACEOBJECT* obj)
             swf_ResetReadBits(tag);
             l = strlen(&tag->data[tag->pos]);
             t = 0;
-            data = malloc(l+1);
+            data = rfx_alloc(l+1);
             obj->name = data;
             while((data[t++] = swf_GetU8(tag))); 
         }
@@ -200,7 +200,6 @@ void swf_GetPlaceObject(TAG * tag,SWFPLACEOBJECT* obj)
 void swf_PlaceObjectFree(SWFPLACEOBJECT* obj)
 {
     if(obj->name)
-       free(obj->name);
-    free(obj);
+       rfx_free(obj->name);
 }
 
index cbc1389..fe4d3a2 100644 (file)
@@ -31,24 +31,23 @@ void swf_ShapeFree(SHAPE * s)
 { 
     if(!s)
         return;
-    if (s->linestyle.data) free(s->linestyle.data);
+    if (s->linestyle.data) rfx_free(s->linestyle.data);
     s->linestyle.data = NULL;
     s->linestyle.n    = 0;
-    if (s->fillstyle.data) free(s->fillstyle.data);
+    if (s->fillstyle.data) rfx_free(s->fillstyle.data);
     s->fillstyle.data = NULL;
     s->fillstyle.n    = 0;
-    if (s->data) free(s->data);
+    if (s->data) rfx_free(s->data);
     s->data = NULL;
-    free(s);
+    rfx_free(s);
 }
 
 int swf_ShapeNew(SHAPE * * s)
 { 
     SHAPE * sh;
     if (!s) return -1;
-    sh = (SHAPE *)malloc(sizeof(SHAPE)); 
+    sh = (SHAPE *)rfx_calloc(sizeof(SHAPE)); 
     *s = sh;
-    memset(sh,0,sizeof(SHAPE));
     return 0;
 }
 
@@ -132,8 +131,8 @@ int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Recor
   swf_SetTagPos(t,pos);
   len = (bitl+7)/8;
   
-  if (sh->data) free(sh->data);
-  sh->data = (U8*)malloc(len);
+  if (sh->data) rfx_free(sh->data);
+  sh->data = (U8*)rfx_alloc(len);
   
   if (sh->data)
   { sh->bitlen = bitl;
@@ -278,12 +277,12 @@ int swf_ShapeAddFillStyle(SHAPE * s,U8 type,MATRIX * m,RGBA * color,U16 id_bitma
   // handle memory
   
   if (s->fillstyle.data)
-  { FILLSTYLE * new = (FILLSTYLE *)realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
+  { FILLSTYLE * new = (FILLSTYLE *)rfx_realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
     if (!new) return -1;
     s->fillstyle.data = new;
   }
   else
-  { s->fillstyle.data = (FILLSTYLE *)malloc(sizeof(FILLSTYLE));
+  { s->fillstyle.data = (FILLSTYLE *)rfx_alloc(sizeof(FILLSTYLE));
     s->fillstyle.n = 0;
     if (!s->fillstyle.data) return -1;
   }
@@ -320,12 +319,12 @@ int swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color)
     def.r = def.g = def.b = 0; 
   }
   if (s->linestyle.data)
-  { LINESTYLE * new = (LINESTYLE *)realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
+  { LINESTYLE * new = (LINESTYLE *)rfx_realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
     if (!new) return -1;
     s->linestyle.data = new;
   }
   else
-  { s->linestyle.data = (LINESTYLE *)malloc(sizeof(LINESTYLE));
+  { s->linestyle.data = (LINESTYLE *)rfx_alloc(sizeof(LINESTYLE));
     s->linestyle.n = 0;
     if (!s->linestyle.data) return -1;
   }
@@ -541,7 +540,7 @@ SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits)
                linebits = swf_GetBits(tag, 4);
            }
            if(flags&1) { //move
-               lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
+               lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
                lines = lines->next;
                lines->type = moveTo;
                lines->x = x; 
@@ -566,7 +565,7 @@ SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits)
                    if(v) y += d;
                    else  x += d;
                }
-               lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
+               lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
                lines = lines->next;
                lines->type = lineTo;
                lines->x = x; 
@@ -586,7 +585,7 @@ SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits)
                x += swf_GetSBits(tag, n);
                y += swf_GetSBits(tag, n);
 
-               lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
+               lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
                lines = lines->next;
                lines->type = splineTo;
                lines->sx = x1; 
@@ -651,27 +650,27 @@ void swf_Shape2Free(SHAPE2 * s)
     SHAPELINE*line = s->lines;
     while(line) {
        SHAPELINE*next = line->next;
-       free(line);
+       rfx_free(line);
        line = next;
     }
     if(s->linestyles)
-       free(s->linestyles);
+       rfx_free(s->linestyles);
     if(s->fillstyles)
-       free(s->fillstyles);
+       rfx_free(s->fillstyles);
     if(s->bbox)
-       free(s->bbox);
+       rfx_free(s->bbox);
 }
 
 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
 
-    SHAPE2*shape2 = (SHAPE2*)malloc(sizeof(SHAPE2));
+    SHAPE2*shape2 = (SHAPE2*)rfx_alloc(sizeof(SHAPE2));
     
     shape2->numlinestyles = shape->linestyle.n;
-    shape2->linestyles = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
+    shape2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
     memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
     
     shape2->numfillstyles = shape->fillstyle.n;
-    shape2->fillstyles = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
+    shape2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
     memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
 
     shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line);
@@ -725,11 +724,11 @@ void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
     memset(shape, 0, sizeof(SHAPE));
 
     shape->linestyle.n = shape2->numlinestyles;
-    shape->linestyle.data = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
+    shape->linestyle.data = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
     memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
     
     shape->fillstyle.n =  shape2->numfillstyles;
-    shape->fillstyle.data = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
+    shape->fillstyle.data = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
     memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
 
     swf_ShapeCountBits(shape,NULL,NULL);
@@ -800,42 +799,46 @@ static void parseFillStyleArray(TAG*tag, SHAPE2*shape)
        count = swf_GetU16(tag);
 
     shape->numfillstyles = count;
-    shape->fillstyles = malloc(sizeof(FILLSTYLE)*count);
-
-    for(t=0;t<count;t++)
-    {
-       int type;
-       U8*pos;
-       FILLSTYLE*dest = &shape->fillstyles[t];
-       type = swf_GetU8(tag); //type
-       shape->fillstyles[t].type = type;
-       if(type == 0) {
-           /* plain color */
-           if(num == 3)
-               swf_GetRGBA(tag, &dest->color);
-           else 
-               swf_GetRGB(tag, &dest->color);
-       }
-       else if(type == 0x10 || type == 0x12)
-       {
-           /* linear/radial gradient fill */
-           swf_ResetReadBits(tag);
-           swf_GetMatrix(tag, &dest->m);
-           swf_ResetReadBits(tag);
-           swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
-       }
-       else if(type == 0x40 || type == 0x41)
-       {
-           /* bitmap fill */
-           swf_ResetReadBits(tag);
-           dest->id_bitmap = swf_GetU16(tag); //id
-           swf_ResetReadBits(tag); //?
-           swf_GetMatrix(tag, &dest->m);
-       }
-       else {
-           fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
-       }
+
+    if(shape->numfillstyles) {
+        shape->fillstyles = rfx_alloc(sizeof(FILLSTYLE)*count);
+
+        for(t=0;t<count;t++)
+        {
+            int type;
+            U8*pos;
+            FILLSTYLE*dest = &shape->fillstyles[t];
+            type = swf_GetU8(tag); //type
+            shape->fillstyles[t].type = type;
+            if(type == 0) {
+                /* plain color */
+                if(num == 3)
+                    swf_GetRGBA(tag, &dest->color);
+                else 
+                    swf_GetRGB(tag, &dest->color);
+            }
+            else if(type == 0x10 || type == 0x12)
+            {
+                /* linear/radial gradient fill */
+                swf_ResetReadBits(tag);
+                swf_GetMatrix(tag, &dest->m);
+                swf_ResetReadBits(tag);
+                swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
+            }
+            else if(type == 0x40 || type == 0x41)
+            {
+                /* bitmap fill */
+                swf_ResetReadBits(tag);
+                dest->id_bitmap = swf_GetU16(tag); //id
+                swf_ResetReadBits(tag); //?
+                swf_GetMatrix(tag, &dest->m);
+            }
+            else {
+                fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
+            }
+        }
     }
+
     swf_ResetReadBits(tag);
     count = swf_GetU8(tag); // line style array
     if(count == 0xff)
@@ -844,16 +847,18 @@ static void parseFillStyleArray(TAG*tag, SHAPE2*shape)
     //if(verbose) printf("lnum: %d\n", count);
 
     shape->numlinestyles = count;
-    shape->linestyles = malloc(sizeof(LINESTYLE)*count);
-    /* TODO: should we start with 1 and insert a correct definition of the
-       "built in" linestyle 0? */
-    for(t=0;t<count;t++) 
-    {
-       shape->linestyles[t].width = swf_GetU16(tag);
-       if(num == 3)
-           swf_GetRGBA(tag, &shape->linestyles[t].color);
-       else
-           swf_GetRGB(tag, &shape->linestyles[t].color);
+    if(count) {
+        shape->linestyles = rfx_alloc(sizeof(LINESTYLE)*count);
+        /* TODO: should we start with 1 and insert a correct definition of the
+           "built in" linestyle 0? */
+        for(t=0;t<count;t++) 
+        {
+            shape->linestyles[t].width = swf_GetU16(tag);
+            if(num == 3)
+                swf_GetRGBA(tag, &shape->linestyles[t].color);
+            else
+                swf_GetRGB(tag, &shape->linestyles[t].color);
+        }
     }
     return;
 }
@@ -877,7 +882,7 @@ void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
 
     id = swf_GetU16(tag); //id
     memset(shape, 0, sizeof(SHAPE2));
-    shape->bbox = malloc(sizeof(SRECT));
+    shape->bbox = rfx_alloc(sizeof(SRECT));
     swf_GetRect(tag, &r);
 
     memcpy(shape->bbox, &r, sizeof(SRECT));
index 935814c..3e5652b 100644 (file)
@@ -60,7 +60,7 @@ void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int numsamples, char first)
     char*buf;
     int len = 0;
 
-    buf = malloc(init->bufferSize);
+    buf = rfx_alloc(init->bufferSize);
     if(!buf)
        return;
     
@@ -73,7 +73,7 @@ void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int numsamples, char first)
        swf_SetU16(tag, 0); // seek
     }
     swf_SetBlock(tag, buf, len);
-    free(buf);
+    rfx_free(buf);
 }
 #endif
 
@@ -164,7 +164,7 @@ void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int seek, char first)
     int numsamples = (int)(((swf_mp3_out_samplerate > 22050) ? 1152 : 576) * ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate));
     int fs = 0;
 
-    buf = malloc(bufsize);
+    buf = rfx_alloc(bufsize);
     if(!buf)
        return;
 
@@ -184,7 +184,7 @@ void swf_SetSoundStreamBlock(TAG*tag, S16*samples, int seek, char first)
        fprintf(stderr, "ok: mp3 nonempty block, %d samples, first:%d, framesize:%d\n",
                numsamples, first, fs);
     }*/
-    free(buf);
+    rfx_free(buf);
 }
 
 void swf_SetSoundStreamEnd(TAG*tag)
@@ -226,7 +226,7 @@ void swf_SetSoundDefine(TAG*tag, S16*samples, int num)
            ((double)swf_mp3_in_samplerate/swf_mp3_out_samplerate)) // account for resampling
            );
 
-    buf = malloc(bufsize);
+    buf = rfx_alloc(bufsize);
     if(!buf)
        return;
 
@@ -243,7 +243,7 @@ void swf_SetSoundDefine(TAG*tag, S16*samples, int num)
        len = 0;
     }
 
-    free(buf);
+    rfx_free(buf);
 }
 
 #endif
index b0d3de5..87c278a 100644 (file)
@@ -157,8 +157,7 @@ int swf_FontExtract_DefineFont(int id, SWFFONT * f, TAG * t)
        of = swf_GetU16(t);
        n = of / 2;
        f->numchars = n;
-       f->glyph = malloc(sizeof(SWFGLYPH) * n);
-       memset(f->glyph, 0, sizeof(SWFGLYPH) * n);
+       f->glyph = rfx_calloc(sizeof(SWFGLYPH) * n);
 
        for (i = 1; i < n; i++)
            swf_GetU16(t);
@@ -191,9 +190,9 @@ int swf_FontExtract_DefineFontInfo(int id, SWFFONT * f, TAG * t)
        }
 
        if (f->name)
-           free(f->name);
+           rfx_free(f->name);
 
-       f->name = (U8 *) malloc(l + 1);
+       f->name = (U8 *) rfx_alloc(l + 1);
        swf_GetBlock(t, f->name, l);
        f->name[l] = 0;
 
@@ -213,7 +212,7 @@ int swf_FontExtract_DefineFontInfo(int id, SWFFONT * f, TAG * t)
            f->language = swf_GetU8(t);
        }
 
-       f->glyph2ascii = (U16 *) malloc(sizeof(U16) * f->numchars);
+       f->glyph2ascii = (U16 *) rfx_alloc(sizeof(U16) * f->numchars);
        maxcode = 0;
        for (i = 0; i < f->numchars; i++) {
            f->glyph2ascii[i] = ((flags & FF_WIDECODES) ? swf_GetU16(t) : swf_GetU8(t));
@@ -224,7 +223,7 @@ int swf_FontExtract_DefineFontInfo(int id, SWFFONT * f, TAG * t)
        if (maxcode < 256)
            maxcode = 256;
        f->maxascii = maxcode;
-       f->ascii2glyph = (int *) malloc(sizeof(int) * maxcode);
+       f->ascii2glyph = (int *) rfx_alloc(sizeof(int) * maxcode);
        memset(f->ascii2glyph, -1, sizeof(int) * maxcode);
 
        for (i = 0; i < f->numchars; i++)
@@ -248,7 +247,7 @@ int swf_FontExtract_GlyphNames(int id, SWFFONT * f, TAG * tag)
     if (fid == id) {
        int num = swf_GetU16(tag);
        int t;
-       f->glyphnames = malloc(sizeof(char *) * num);
+       f->glyphnames = rfx_alloc(sizeof(char *) * num);
        for (t = 0; t < num; t++) {
            f->glyphnames[t] = strdup(swf_GetString(tag));
        }
@@ -287,17 +286,15 @@ int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag)
        font->encoding |= FONT_ENCODING_SHIFTJIS;
 
     namelen = swf_GetU8(tag);
-    font->name = (U8 *) malloc(namelen + 1);
+    font->name = (U8 *) rfx_alloc(namelen + 1);
     font->name[namelen] = 0;
     swf_GetBlock(tag, font->name, namelen);
     font->version = 2;
     glyphcount = swf_GetU16(tag);
     font->numchars = glyphcount;
 
-    font->glyph = (SWFGLYPH *) malloc(sizeof(SWFGLYPH) * glyphcount);
-    memset(font->glyph, 0, sizeof(SWFGLYPH) * glyphcount);
-    font->glyph2ascii = (U16 *) malloc(sizeof(U16) * glyphcount);
-    memset(font->glyph2ascii, 0, sizeof(U16) * glyphcount);
+    font->glyph = (SWFGLYPH *) rfx_calloc(sizeof(SWFGLYPH) * glyphcount);
+    font->glyph2ascii = (U16 *) rfx_calloc(sizeof(U16) * glyphcount);
 
     if (flags1 & 8) {          // wide offsets
        for (t = 0; t < glyphcount; t++)
@@ -332,7 +329,7 @@ int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag)
     if (maxcode < 256)
        maxcode = 256;
     font->maxascii = maxcode;
-    font->ascii2glyph = (int *) malloc(sizeof(int) * maxcode);
+    font->ascii2glyph = (int *) rfx_alloc(sizeof(int) * maxcode);
     memset(font->ascii2glyph, -1, sizeof(int) * maxcode);
     for (t = 0; t < glyphcount; t++) {
        font->ascii2glyph[font->glyph2ascii[t]] = t;
@@ -340,7 +337,7 @@ int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag)
 
     if (flags1 & 128) {                // has layout
        U16 kerningcount;
-       font->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
+       font->layout = (SWFLAYOUT *) rfx_alloc(sizeof(SWFLAYOUT));
        font->layout->ascent = swf_GetU16(tag);
        font->layout->descent = swf_GetU16(tag);
        font->layout->leading = swf_GetU16(tag);
@@ -348,7 +345,7 @@ int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag)
            S16 advance = swf_GetS16(tag);
            font->glyph[t].advance = advance;
        }
-       font->layout->bounds = malloc(glyphcount * sizeof(SRECT));
+       font->layout->bounds = rfx_alloc(glyphcount * sizeof(SRECT));
        for (t = 0; t < glyphcount; t++) {
            swf_ResetReadBits(tag);
            swf_GetRect(tag, &font->layout->bounds[t]);
@@ -357,9 +354,9 @@ int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag)
        kerningcount = swf_GetU16(tag);
        font->layout->kerningcount = kerningcount;
 
-       font->layout->kerning = (SWFKERNING *) malloc(sizeof(SWFKERNING) * kerningcount);
+       font->layout->kerning = (SWFKERNING *) rfx_alloc(sizeof(SWFKERNING) * kerningcount);
        if (kerningcount) {
-           font->layout->kerning = malloc(sizeof(*font->layout->kerning) * kerningcount);
+           font->layout->kerning = rfx_alloc(sizeof(*font->layout->kerning) * kerningcount);
            for (t = 0; t < kerningcount; t++) {
                if (flags1 & 4) {       // wide codes
                    font->layout->kerning[t].char1 = swf_GetU16(tag);
@@ -495,8 +492,7 @@ int swf_FontExtract(SWF * swf, int id, SWFFONT * *font)
     if ((!swf) || (!font))
        return -1;
 
-    f = (SWFFONT *) malloc(sizeof(SWFFONT));
-    memset(f, 0x00, sizeof(SWFFONT));
+    f = (SWFFONT *) rfx_calloc(sizeof(SWFFONT));
 
     t = swf->firstTag;
 
@@ -530,7 +526,7 @@ int swf_FontExtract(SWF * swf, int id, SWFFONT * *font)
        t = swf_NextTag(t);
     }
     if (f->id != id) {
-       free(f);
+       rfx_free(f);
        f = 0;
     }
     font[0] = f;
@@ -549,13 +545,13 @@ void swf_LayoutFree(SWFLAYOUT * l)
 {
     if (l) {
        if (l->kerning)
-           free(l->kerning);
+           rfx_free(l->kerning);
        l->kerning = NULL;
        if (l->bounds)
-           free(l->bounds);
+           rfx_free(l->bounds);
        l->bounds = NULL;
     }
-    free(l);
+    rfx_free(l);
 }
 
 
@@ -565,9 +561,9 @@ static void font_freeglyphnames(SWFFONT*f)
        int t;
        for (t = 0; t < f->numchars; t++) {
            if (f->glyphnames[t])
-               free(f->glyphnames[t]);
+               rfx_free(f->glyphnames[t]);
        }
-       free(f->glyphnames);
+       rfx_free(f->glyphnames);
        f->glyphnames = 0;
     }
 
@@ -576,9 +572,9 @@ static void font_freeusage(SWFFONT*f)
 {
     if (f->use) {
        if(f->use->chars) {
-           free(f->use->chars);f->use->chars = 0;
+           rfx_free(f->use->chars);f->use->chars = 0;
        }
-       free(f->use); f->use = 0;
+       rfx_free(f->use); f->use = 0;
     }
 }
 static void font_freelayout(SWFFONT*f)
@@ -591,7 +587,7 @@ static void font_freelayout(SWFFONT*f)
 static void font_freename(SWFFONT*f)
 {
     if (f->name) {
-       free(f->name);
+       rfx_free(f->name);
        f->name = 0;
     }
 }
@@ -647,7 +643,7 @@ void swf_FontSort(SWFFONT * font)
     if (!font)
        return;
     
-    newplace = malloc(sizeof(int) * font->numchars);
+    newplace = rfx_alloc(sizeof(int) * font->numchars);
 
     for (i = 0; i < font->numchars; i++) {
        newplace[i] = i;
@@ -685,7 +681,7 @@ void swf_FontSort(SWFFONT * font)
                }
            }
        }
-    newpos = malloc(sizeof(int) * font->numchars);
+    newpos = rfx_alloc(sizeof(int) * font->numchars);
     for (i = 0; i < font->numchars; i++) {
        newpos[newplace[i]] = i;
     }
@@ -694,8 +690,8 @@ void swf_FontSort(SWFFONT * font)
            font->ascii2glyph[i] = newpos[font->ascii2glyph[i]];
     }
 
-    free(newpos);
-    free(newplace);
+    rfx_free(newpos);
+    rfx_free(newplace);
 }
 
 void swf_FontPrepareForEditText(SWFFONT * font)
@@ -713,10 +709,9 @@ int swf_FontInitUsage(SWFFONT * f)
        fprintf(stderr, "Usage initialized twice");
        return -1;
     }
-    f->use = malloc(sizeof(FONTUSAGE));
+    f->use = rfx_alloc(sizeof(FONTUSAGE));
     f->use->is_reduced = 0;
-    f->use->chars = malloc(sizeof(f->use->chars[0]) * f->numchars);
-    memset(f->use->chars, 0, sizeof(f->use->chars[0]) * f->numchars);
+    f->use->chars = rfx_calloc(sizeof(f->use->chars[0]) * f->numchars);
     return 0;
 }
 
@@ -724,8 +719,8 @@ void swf_FontClearUsage(SWFFONT * f)
 {
     if (!f || !f->use)
        return;
-    free(f->use->chars); f->use->chars = 0;
-    free(f->use); f->use = 0;
+    rfx_free(f->use->chars); f->use->chars = 0;
+    rfx_free(f->use); f->use = 0;
 }
 
 int swf_FontUse(SWFFONT * f, U8 * s)
@@ -754,7 +749,7 @@ int swf_FontUseGlyph(SWFFONT * f, int glyph)
 
 int swf_FontSetDefine(TAG * t, SWFFONT * f)
 {
-    U16 *ofs = (U16 *) malloc(f->numchars * 2);
+    U16 *ofs = (U16 *) rfx_alloc(f->numchars * 2);
     int p, i, j;
 
     if ((!t) || (!f))
@@ -782,7 +777,7 @@ int swf_FontSetDefine(TAG * t, SWFFONT * f)
            swf_SetSimpleShape(t, f->glyph[i].shape);
 
     swf_ResetWriteBits(t);
-    free(ofs);
+    rfx_free(ofs);
     return 0;
 }
 
@@ -910,14 +905,13 @@ int swf_FontSetDefine2(TAG * tag, SWFFONT * f)
 
 void swf_FontAddLayout(SWFFONT * f, int ascent, int descent, int leading)
 {
-    f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
+    f->layout = (SWFLAYOUT *) rfx_alloc(sizeof(SWFLAYOUT));
     f->layout->ascent = ascent;
     f->layout->descent = descent;
     f->layout->leading = leading;
     f->layout->kerningcount = 0;
     f->layout->kerning = 0;
-    f->layout->bounds = (SRECT *) malloc(sizeof(SRECT) * f->numchars);
-    memset(f->layout->bounds, 0, sizeof(SRECT) * f->numchars);
+    f->layout->bounds = (SRECT *) rfx_calloc(sizeof(SRECT) * f->numchars);
 }
 
 int swf_FontSetInfo(TAG * t, SWFFONT * f)
@@ -983,15 +977,15 @@ void swf_FontFree(SWFFONT * f)
                swf_ShapeFree(f->glyph[i].shape);
                f->glyph[i].shape = NULL;
            }
-       free(f->glyph);
+       rfx_free(f->glyph);
        f->glyph = NULL;
     }
     if (f->ascii2glyph) {
-       free(f->ascii2glyph);
+       rfx_free(f->ascii2glyph);
        f->ascii2glyph = NULL;
     }
     if (f->glyph2ascii) {
-       free(f->glyph2ascii);
+       rfx_free(f->glyph2ascii);
        f->glyph2ascii = NULL;
     }
     font_freename(f);
@@ -999,7 +993,7 @@ void swf_FontFree(SWFFONT * f)
     font_freeglyphnames(f);
     font_freeusage(f);
 
-    free(f);
+    rfx_free(f);
 }
 
 int swf_TextSetInfoRecord(TAG * t, SWFFONT * font, U16 size, RGBA * color, int dx, int dy)
@@ -1468,9 +1462,8 @@ void swf_FontCreateLayout(SWFFONT * f)
     if (!f->numchars)
        return;
 
-    f->layout = (SWFLAYOUT *) malloc(sizeof(SWFLAYOUT));
-    memset(f->layout, 0, sizeof(SWFLAYOUT));
-    f->layout->bounds = (SRECT *) malloc(f->numchars * sizeof(SRECT));
+    f->layout = (SWFLAYOUT *) rfx_calloc(sizeof(SWFLAYOUT));
+    f->layout->bounds = (SRECT *) rfx_alloc(f->numchars * sizeof(SRECT));
     f->layout->ascent = -32767;
     f->layout->descent = -32767;
 
index 2de52f7..e3d20ee 100644 (file)
@@ -560,7 +560,7 @@ void enumerateUsedIDs(TAG * tag, int base, void (*callback)(TAG*, int, void*), v
                if(id == ST_END)
                    break;
                tag2->len = tag2->memsize = len;
-               tag2->data = malloc(len);
+               tag2->data = rfx_alloc(len);
                memcpy(tag2->data, &tag->data[tag->pos], len);
                /* I never saw recursive sprites, but they are (theoretically) 
                   possible, so better add base here again */
@@ -844,7 +844,7 @@ void swf_Relocate (SWF*swf, char*bitmap)
        } 
        
        num = swf_GetNumUsedIDs(tag);
-       ptr = malloc(sizeof(int)*num);
+       ptr = rfx_alloc(sizeof(int)*num);
        swf_GetUsedIDs(tag, ptr);
 
        for(t=0;t<num;t++) {
@@ -891,6 +891,26 @@ void swf_RelocateDepth(SWF*swf, char*bitmap)
     }
 }
 
+U8 swf_isShapeTag(TAG*tag)
+{
+    if(tag->id == ST_DEFINESHAPE ||
+       tag->id == ST_DEFINESHAPE2 ||
+       tag->id == ST_DEFINESHAPE3) 
+        return 1;
+    return 0;
+}
+
+U8  swf_isImageTag(TAG*tag)
+{
+    if(tag->id == ST_DEFINEBITSJPEG || 
+       tag->id == ST_DEFINEBITSJPEG2 || 
+       tag->id == ST_DEFINEBITSJPEG3 ||
+       tag->id == ST_DEFINEBITSLOSSLESS || 
+       tag->id == ST_DEFINEBITSLOSSLESS2)
+        return 1;
+    return 0;
+}
+
 TAG* swf_Concatenate (TAG*list1,TAG*list2)
 {
     TAG*tag=0,*lasttag=0;
@@ -950,14 +970,11 @@ static int tagHash(TAG*tag)
 void swf_Optimize(SWF*swf)
 {
     const int hash_size = 131072;
-    char* dontremap = malloc(sizeof(char)*65536);
-    U16* remap = malloc(sizeof(U16)*65536);
-    TAG* id2tag = malloc(sizeof(TAG*)*65536);
-    TAG** hashmap = malloc(sizeof(TAG*)*hash_size);
+    char* dontremap = rfx_calloc(sizeof(char)*65536);
+    U16* remap = rfx_alloc(sizeof(U16)*65536);
+    TAG* id2tag = rfx_calloc(sizeof(TAG*)*65536);
+    TAG** hashmap = rfx_calloc(sizeof(TAG*)*hash_size);
     TAG* tag;
-    memset(dontremap, 0, sizeof(char)*65536);
-    memset(hashmap, 0, sizeof(TAG*)*hash_size);
-    memset(id2tag, 0, sizeof(TAG*)*65536);
     int t;
     for(t=0;t<65536;t++) {
         remap[t] = t;
@@ -1034,7 +1051,7 @@ void swf_Optimize(SWF*swf)
         if(doremap)
         {
             int num = swf_GetNumUsedIDs(tag);
-            int*positions = malloc(sizeof(int)*num);
+            int*positions = rfx_alloc(sizeof(int)*num);
             int t;
             swf_GetUsedIDs(tag, positions);
             for(t=0;t<num;t++) {
@@ -1042,14 +1059,14 @@ void swf_Optimize(SWF*swf)
                 id = remap[id];
                 PUT16(&tag->data[positions[t]], id);
             }
-            free(positions);
+            rfx_free(positions);
             tag = tag->next;
         }
 
         tag = next;
     }
-    free(dontremap);
-    free(remap);
-    free(id2tag);
-    free(hashmap);
+    rfx_free(dontremap);
+    rfx_free(remap);
+    rfx_free(id2tag);
+    rfx_free(hashmap);
 }
index 061010b..ed94927 100644 (file)
 #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);
+}
+
 // internal constants
 
 #define MALLOC_SIZE     128
 
 #define MEMSIZE(l) (((l/MALLOC_SIZE)+1)*MALLOC_SIZE)
 
-
 // inline wrapper functions
 
 TAG * swf_NextTag(TAG * t) { return t->next; }
@@ -143,15 +214,7 @@ int swf_SetBlock(TAG * t,U8 * b,int l)
   swf_ResetWriteBits(t);
   if (newlen>t->memsize)
   { U32  newmem  = MEMSIZE(newlen);  
-    U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
-    if (!newdata)
-    {
-      #ifdef DEBUG_RFXSWF
-        fprintf(stderr,"Fatal Error: malloc()/realloc() failed (1). (%d bytes)\n", newmem);
-       *(int*)0=0;
-      #endif
-      return 0;
-    }
+    U8 * newdata = (U8*)(rfx_realloc(t->data,newmem));
     t->memsize = newmem;
     t->data    = newdata;
   }
@@ -741,12 +804,12 @@ int swf_VerifyPassword(TAG * t, const char * password)
        return 0;
     }
     n = x-(md5string1+3);
-    salt = (char*)malloc(n+1);
+    salt = (char*)rfx_alloc(n+1);
     memcpy(salt, md5string1+3, n);
     salt[n] = 0;
 
     md5string2 = crypt_md5(password, salt);
-    free(salt);
+    rfx_free(salt);
     if(strcmp(md5string1, md5string2) != 0)
        return 0;
     return 1;
@@ -757,18 +820,15 @@ int swf_VerifyPassword(TAG * t, const char * password)
 TAG * swf_InsertTag(TAG * after,U16 id)
 { TAG * t;
 
-  t = (TAG *)malloc(sizeof(TAG));
-  if (t)
-  { memset(t,0x00,sizeof(TAG));
-    t->id = id;
-    
-    if (after)
-    {
-      t->prev  = after;
-      t->next  = after->next;
-      after->next = t;
-      if (t->next) t->next->prev = t;
-    }
+  t = (TAG *)rfx_calloc(sizeof(TAG));
+  t->id = id;
+  
+  if (after)
+  {
+    t->prev  = after;
+    t->next  = after->next;
+    after->next = t;
+    if (t->next) t->next->prev = t;
   }
   return t;
 }
@@ -776,18 +836,15 @@ TAG * swf_InsertTag(TAG * after,U16 id)
 TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
 { TAG * t;
 
-  t = (TAG *)malloc(sizeof(TAG));
-  if (t)
-  { memset(t,0x00,sizeof(TAG));
-    t->id = id;
-    
-    if (before)
-    {
-      t->next  = before;
-      t->prev  = before->prev;
-      before->prev = t;
-      if (t->prev) t->prev->next = t;
-    }
+  t = (TAG *)rfx_calloc(sizeof(TAG));
+  t->id = id;
+  
+  if (before)
+  {
+    t->next  = before;
+    t->prev  = before->prev;
+    before->prev = t;
+    if (t->prev) t->prev->next = t;
   }
   if(swf && swf->firstTag == before) {
     swf->firstTag = t;
@@ -797,7 +854,7 @@ TAG * swf_InsertTagBefore(SWF* swf, TAG * before,U16 id)
 
 void swf_ClearTag(TAG * t)
 {
-  if (t->data) free(t->data);
+  if (t->data) rfx_free(t->data);
   t->data = 0;
   t->pos = 0;
   t->len = 0;
@@ -812,14 +869,21 @@ void swf_ResetTag(TAG*tag, U16 id)
     tag->id = id;
 }
 
+TAG* swf_CopyTag(TAG*tag, TAG*to_copy)
+{
+    tag = swf_InsertTag(tag, to_copy->id);
+    swf_SetBlock(tag, to_copy->data, to_copy->len);
+    return tag;
+}
+
 int swf_DeleteTag(TAG * t)
 { if (!t) return -1;
 
   if (t->prev) t->prev->next = t->next;
   if (t->next) t->next->prev = t->prev;
 
-  if (t->data) free(t->data);
-  free(t);
+  if (t->data) rfx_free(t->data);
+  rfx_free(t);
   return 0;
 }
 
@@ -844,30 +908,13 @@ TAG * swf_ReadTag(struct reader_t*reader, TAG * prev)
   if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
   // Sprite handling fix: Flatten sprite tree
 
-  t = (TAG *)malloc(sizeof(TAG));
-  
-  if (!t)
-  {
-    #ifdef DEBUG_RFXSWF
-      fprintf(stderr,"Fatal Error: malloc()/realloc() failed (2). (%d bytes)\n", sizeof(TAG));
-    #endif
-    return NULL;
-  }
-
-  memset(t,0x00,sizeof(TAG));
+  t = (TAG *)rfx_calloc(sizeof(TAG));
   
   t->len = len;
   t->id  = id;
 
   if (t->len)
-  { t->data = (U8*)malloc(t->len);
-    if (!t->data)
-    {
-      #ifdef DEBUG_RFXSWF
-        fprintf(stderr,"Fatal Error: malloc()/realloc() failed (3). (%d bytes)\n", t->len);
-      #endif
-      return NULL;
-    }
+  { t->data = (U8*)rfx_alloc(t->len);
     t->memsize = t->len;
     if (reader->read(reader, t->data, t->len) != t->len) return NULL;
   }
@@ -1008,7 +1055,7 @@ void swf_UnFoldSprite(TAG * t)
     it->len = len;
     it->id  = id;
     if (it->len)
-    { it->data = (U8*)malloc(it->len);
+    { it->data = (U8*)rfx_alloc(it->len);
       it->memsize = it->len;
       swf_GetBlock(t, it->data, it->len);
     }
@@ -1017,7 +1064,7 @@ void swf_UnFoldSprite(TAG * t)
        break;
   }
   
-  free(t->data); t->data = 0;
+  rfx_free(t->data); t->data = 0;
   t->memsize = t->len = t->pos = 0;
 
   swf_SetU16(t, spriteid);
@@ -1042,7 +1089,7 @@ void swf_FoldSprite(TAG * t)
 
   t->pos = 0;
   id = swf_GetU16(t);
-  free(t->data);
+  rfx_free(t->data);
   t->len = t->pos = t->memsize = 0;
   t->data = 0;
 
@@ -1425,8 +1472,8 @@ void swf_FreeTags(SWF * swf)                 // Frees all malloc'ed memory for t
 
   while (t)
   { TAG * tnew = t->next;
-    if (t->data) free(t->data);
-    free(t);
+    if (t->data) rfx_free(t->data);
+    rfx_free(t);
     t = tnew;
   }
   swf->firstTag = 0;