Generated from pdf2swf_usage.html
[swftools.git] / lib / rfxswf.c
index 801171f..5f49939 100644 (file)
@@ -16,6 +16,7 @@
 
 #ifdef HAVE_LIBJPEG
 #ifdef HAVE_JPEGLIB_H
+#define HAVE_BOOLEAN
 #include <jpeglib.h>
 #define _JPEGLIB_INCLUDED_
 #endif // HAVE_JPEGLIB_H
@@ -52,22 +53,22 @@ TAG * swf_NextTag(TAG * t) { return t->next; }
 TAG * swf_PrevTag(TAG * t) { return t->prev; }
 int   swf_GetFrameNo(TAG * t)  { return t->frame; }
 U16   swf_GetTagID(TAG * t)    { return t->id; }
-U32   swf_GetDataSize(TAG * t) { return t->len; }
-U8*   swf_GetDataSizePtr(TAG * t) { return &(t->data[t->len]); }
+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; }
 
 // Basic Data Access Functions
 
-#define swf_ResetBitmask(tag)   if (tag->bitmask)  { tag->pos++; tag->bitmask = 0; }
-#define swf_ResetBitcount(tag)  if (tag->bitcount) { tag->bitcount = 0; }
+#define swf_ResetReadBits(tag)   if (tag->readBit)  { tag->pos++; tag->readBit = 0; }
+#define swf_ResetWriteBits(tag)  if (tag->writeBit) { tag->writeBit = 0; }
 
-// for future purpose: avoid high level lib functions to change tagpos/bitcount
+// 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_ResetBitmask(t);
+{ swf_ResetReadBits(t);
   if (pos<=t->len) t->pos = pos;
   #ifdef DEBUG_RFXSWF
   else fprintf(stderr,"SetTagPos() out of bounds: TagID = %i\n",t->id);
@@ -75,7 +76,7 @@ void swf_SetTagPos(TAG * t,U32 pos)
 }
 
 U8 swf_GetU8(TAG * t)
-{ swf_ResetBitmask(t);
+{ swf_ResetReadBits(t);
   #ifdef DEBUG_RFXSWF
     if (t->pos>=t->len) 
     { fprintf(stderr,"GetU8() out of bounds: TagID = %i\n",t->id);
@@ -87,7 +88,7 @@ U8 swf_GetU8(TAG * t)
 
 U16 swf_GetU16(TAG * t)
 { U16 res;
-  swf_ResetBitmask(t);
+  swf_ResetReadBits(t);
   #ifdef DEBUG_RFXSWF
     if (t->pos>(t->len-2)) 
     { fprintf(stderr,"GetU16() out of bounds: TagID = %i\n",t->id);
@@ -101,7 +102,7 @@ U16 swf_GetU16(TAG * t)
 
 U32 swf_GetU32(TAG * t)
 { U32 res;
-  swf_ResetBitmask(t);
+  swf_ResetReadBits(t);
   #ifdef DEBUG_RFXSWF
     if (t->pos>(t->len-4)) 
     { fprintf(stderr,"GetU32() out of bounds: TagID = %i\n",t->id);
@@ -117,7 +118,7 @@ U32 swf_GetU32(TAG * t)
 int swf_GetBlock(TAG * t,U8 * b,int l)
 // returns number of bytes written (<=l)
 // b = NULL -> skip data
-{ swf_ResetBitmask(t);
+{ swf_ResetReadBits(t);
   if ((t->len-t->pos)<l) l=t->len-t->pos;
   if (b && l) memcpy(b,&t->data[t->pos],l);
   t->pos+=l;
@@ -127,7 +128,7 @@ int swf_GetBlock(TAG * t,U8 * b,int l)
 int swf_SetBlock(TAG * t,U8 * b,int l)
 // Appends Block to the end of Tagdata, returns size
 { U32 newlen = t->len + l;
-  swf_ResetBitcount(t);
+  swf_ResetWriteBits(t);
   if (newlen>t->memsize)
   { U32  newmem  = MEMSIZE(newlen);  
     U8 * newdata = (U8*)((t->data)?realloc(t->data,newmem):malloc(newmem));
@@ -148,7 +149,7 @@ int swf_SetBlock(TAG * t,U8 * b,int l)
 }
 
 int swf_SetU8(TAG * t,U8 v)
-{ swf_ResetBitcount(t);
+{ swf_ResetWriteBits(t);
   if ((t->len+1)>t->memsize) return (swf_SetBlock(t,&v,1)==1)?0:-1;
   t->data[t->len++] = v;
   return 0;
@@ -159,7 +160,7 @@ int swf_SetU16(TAG * t,U16 v)
   a[0] = v&0xff;
   a[1] = v>>8;
   
-  swf_ResetBitcount(t);
+  swf_ResetWriteBits(t);
   if ((t->len+2)>t->memsize) return (swf_SetBlock(t,a,2)==2)?0:-1;
   t->data[t->len++] = a[0];
   t->data[t->len++] = a[1];
@@ -173,7 +174,7 @@ int swf_SetU32(TAG * t,U32 v)
   a[2] = (v>>16)&0xff;
   a[3] = (v>>24)&0xff;
   
-  swf_ResetBitcount(t);
+  swf_ResetWriteBits(t);
   if ((t->len+4)>t->memsize) return (swf_SetBlock(t,a,4)==4)?0:-1;
   t->data[t->len++] = a[0];
   t->data[t->len++] = a[1];
@@ -185,14 +186,14 @@ int swf_SetU32(TAG * t,U32 v)
 U32 swf_GetBits(TAG * t,int nbits)
 { U32 res = 0;
   if (!nbits) return 0;
-  if (!t->bitmask) t->bitmask = 0x80;
+  if (!t->readBit) t->readBit = 0x80;
   while (nbits)
   { res<<=1;
-    if (t->data[t->pos]&t->bitmask) res|=1;
-    t->bitmask>>=1;
+    if (t->data[t->pos]&t->readBit) res|=1;
+    t->readBit>>=1;
     nbits--;
-    if (!t->bitmask)
-    { if (nbits) t->bitmask = 0x80;
+    if (!t->readBit)
+    { if (nbits) t->readBit = 0x80;
       #ifdef DEBUG_RFXSWF
       if (t->pos>=t->len) 
       { fprintf(stderr,"GetBits() out of bounds: TagID = %i\n",t->id);
@@ -215,13 +216,13 @@ int swf_SetBits(TAG * t,U32 v,int nbits)
 { U32 bm = 1<<(nbits-1);
 
   while (nbits)
-  { if (!t->bitcount)
+  { if (!t->writeBit)
     { if (FAILED(swf_SetU8(t,0))) return -1;
-      t->bitcount = 0x80;
+      t->writeBit = 0x80;
     }
-    if (v&bm) t->data[t->len-1] |= t->bitcount;
+    if (v&bm) t->data[t->len-1] |= t->writeBit;
     bm>>=1;
-    t->bitcount>>=1;
+    t->writeBit>>=1;
     nbits--;
   }
   return 0;
@@ -313,7 +314,7 @@ int swf_GetMatrix(TAG * t,MATRIX * m)
     return -1;
   }
 
-  swf_ResetBitmask(t);
+  swf_ResetReadBits(t);
   
   if (swf_GetBits(t,1))
   { nbits = swf_GetBits(t,5);
@@ -347,13 +348,17 @@ int swf_SetMatrix(TAG * t,MATRIX * m)
     ma.tx = ma.ty = 0;
   }
 
-  swf_ResetBitcount(t);
+  swf_ResetWriteBits(t);
 
   if ((m->sx==0x10000)&&(m->sy==0x10000)) swf_SetBits(t,0,1);
   else
   { swf_SetBits(t,1,1);
     nbits = swf_CountBits(m->sx,0);
     nbits = swf_CountBits(m->sy,nbits);
+    if(nbits>=32) {
+       fprintf(stderr,"rfxswf: Error: matrix values too large\n");
+       nbits = 31;
+    }
     swf_SetBits(t,nbits,5);
     swf_SetBits(t,m->sx,nbits);
     swf_SetBits(t,m->sy,nbits);
@@ -364,6 +369,10 @@ int swf_SetMatrix(TAG * t,MATRIX * m)
   { swf_SetBits(t,1,1);
     nbits = swf_CountBits(m->r0,0);
     nbits = swf_CountBits(m->r1,nbits);
+    if(nbits>=32) {
+       fprintf(stderr,"rfxswf: Error: matrix values too large\n");
+       nbits = 31;
+    }
     swf_SetBits(t,nbits,5);
     swf_SetBits(t,m->r0,nbits);
     swf_SetBits(t,m->r1,nbits);
@@ -371,6 +380,10 @@ int swf_SetMatrix(TAG * t,MATRIX * m)
 
   nbits = swf_CountBits(m->tx,0);
   nbits = swf_CountBits(m->ty,nbits);
+  if(nbits>=32) {
+      fprintf(stderr,"rfxswf: Error: matrix values too large\n");
+      nbits = 31;
+  }
   swf_SetBits(t,nbits,5);
   swf_SetBits(t,m->tx,nbits);
   swf_SetBits(t,m->ty,nbits);
@@ -391,7 +404,7 @@ int swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha) //FIXME: alpha should be type bo
 
   if (!t) return 0;
   
-  swf_ResetBitmask(t);
+  swf_ResetReadBits(t);
   hasadd = swf_GetBits(t,1);
   hasmul = swf_GetBits(t,1);
   nbits  = swf_GetBits(t,4);
@@ -451,7 +464,7 @@ int swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha)
     nbits = swf_CountBits((S32)cx->b1,nbits);
   }
   
-  swf_ResetBitcount(t);
+  swf_ResetWriteBits(t);
   swf_SetBits(t,hasadd?1:0,1);
   swf_SetBits(t,hasmul?1:0,1);
   swf_SetBits(t,nbits,4);
@@ -498,7 +511,6 @@ TAG * swf_InsertTag(TAG * after,U16 id)     // updates frames, if nescessary
   if (t)
   { memset(t,0x00,sizeof(TAG));
     t->id = id;
-    t->bitcount = 0x80;
     
     if (after)
     { t->frame = after->frame;
@@ -533,12 +545,15 @@ TAG * RFXSWF_ReadTag(int handle,TAG * prev)
   int id;
 
   if (read(handle,&raw,2)!=2) return NULL;
+  raw = SWAP16(raw);
 
   len = raw&0x3f;
   id  = raw>>6;
 
   if (len==0x3f)
-  { if (read(handle,&len,4)!=4) return NULL;
+  {
+      if (read(handle,&len,4)!=4) return NULL;
+      len = SWAP32(len);
   }
 
   if (id==ST_DEFINESPRITE) len = 2*sizeof(U16);
@@ -598,7 +613,7 @@ int RFXSWF_WriteTag(int handle,TAG * t)
 
   if (handle>=0)
   { if (short_tag)
-    { raw[0] = len|((t->id&0x3ff)<<6);
+    { raw[0] = SWAP16(len|((t->id&0x3ff)<<6));
       if (write(handle,raw,2)!=2)
       {
         #ifdef DEBUG_RFXSWF
@@ -608,13 +623,21 @@ int RFXSWF_WriteTag(int handle,TAG * t)
       }
     }
     else
-    { raw[0] = (t->id<<6)|0x3f;
-      raw[1] = (U16)(len&0xffff);
-      raw[2] = (U16)(len>>16);
-      if (write(handle,raw,6)!=6)
+    {
+      raw[0] = SWAP16((t->id<<6)|0x3f);
+      if (write(handle,raw,2)!=2)
+      {
+#ifdef DEBUG_RFXSWF
+          fprintf(stderr,"WriteTag() failed: Long Header (1).\n");
+#endif
+         return -1;
+      }
+      
+      len = SWAP32(len);
+      if (write(handle,&len,4)!=4)
       {
         #ifdef DEBUG_RFXSWF
-          fprintf(stderr,"WriteTag() failed: Long Header.\n");
+          fprintf(stderr,"WriteTag() failed: Long Header (2).\n");
         #endif
         return -1;
       }
@@ -735,7 +758,7 @@ int  swf_WriteSWF(int handle,SWF * swf)     // Writes SWF to file, returns lengt
     swf_SetU16(&t1,swf->frameRate);
     swf_SetU16(&t1,swf->frameCount);
 
-    l = swf_GetDataSize(&t1);
+    l = swf_GetTagLen(&t1);
     swf->fileSize = l+len;
     t1.len = 4;                         // bad & ugly trick !
     swf_SetU32(&t1,swf->fileSize);
@@ -763,7 +786,7 @@ int  swf_WriteSWF(int handle,SWF * swf)     // Writes SWF to file, returns lengt
   return (int)swf->fileSize;
 }
 
-int WriteCGI(SWF * swf)
+int swf_WriteCGI(SWF * swf)
 { int len;
   char s[1024];