X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Frfxswf.c;h=f0f23ecc72cfa14af2add3adbb0b422c48f0137a;hb=dae2250691184ef6d638fc9d3c45b3c24299148b;hp=801171fbf34fc71f1778538fec5f5f078f6130c6;hpb=e448c7a56df8e289c9dbd5b8d87753addd541091;p=swftools.git diff --git a/lib/rfxswf.c b/lib/rfxswf.c index 801171f..f0f23ec 100644 --- a/lib/rfxswf.c +++ b/lib/rfxswf.c @@ -16,6 +16,7 @@ #ifdef HAVE_LIBJPEG #ifdef HAVE_JPEGLIB_H +#define HAVE_BOOLEAN #include #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)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; } @@ -637,6 +660,11 @@ int RFXSWF_WriteTag(int handle,TAG * t) return t->len+(short_tag?2:6); } +int swf_WriteTag(int handle,TAG * t) +{ + return RFXSWF_WriteTag(handle, t); +} + int RFXSWF_DefineSprite_GetRealSize(TAG * t) // Sprite Handling: Helper function to pack DefineSprite-Tag { U32 len = t->len; @@ -649,10 +677,43 @@ int RFXSWF_DefineSprite_GetRealSize(TAG * t) } #define swf_ReadTag(a,b) RFXSWF_ReadTag(a,b) -#define swf_WriteTag(a,b) RFXSWF_WriteTag(a,b) +#define swf_WriteTag(a,b) RFXSWF_WriteTag(a,b) // Movie Functions +int swf_InitSWF(void*data, int length, SWF * swf) /* copy a swf in memory into SWF struct */ +{ + TAG reader; + /* + unfinished! + */ + *(int*)0=0xDEAD; + if (!swf) return -1; + memset(swf,0x00,sizeof(SWF)); + memset(&reader,0x00,sizeof(TAG)); + reader.data = data; + reader.len = reader.memsize = length; + + { char b[32]; // read Header + TAG * t; + + if (swf_GetU8(&reader)!=(U8)'F') return -1; + if (swf_GetU8(&reader)!=(U8)'W') return -1; + if (swf_GetU8(&reader)!=(U8)'S') return -1; + + swf->fileVersion = swf_GetU8(&reader); + swf->fileSize = swf_GetU32(&reader); + swf_GetRect(&reader,&swf->movieSize); + swf->frameRate = swf_GetU16(&reader); + swf->frameCount = swf_GetU16(&reader); + + /*t = &t1; + while (t) t = swf_ReadTag(handle,t); + swf->firstTag = t1.next; + t1.next->prev = NULL;*/ + } +} + int swf_ReadSWF(int handle,SWF * swf) // Reads SWF to memory (malloc'ed), returns length or <0 if fails { if (!swf) return -1; @@ -689,6 +750,7 @@ int swf_ReadSWF(int handle,SWF * swf) // Reads SWF to memory (malloc'ed), retu return 0; } + int swf_WriteSWF(int handle,SWF * swf) // Writes SWF to file, returns length or <0 if fails { U32 len; TAG * t; @@ -699,7 +761,7 @@ int swf_WriteSWF(int handle,SWF * swf) // Writes SWF to file, returns lengt #ifdef INSERT_RFX_TAG - if (swf_NextTag(swf->firstTag)) + if (swf->firstTag && swf_NextTag(swf->firstTag)) if (swf_GetTagID(swf_NextTag(swf->firstTag))!=ST_REFLEX) swf_SetBlock(swf_InsertTag(swf->firstTag,ST_REFLEX),"rfx",3); @@ -730,15 +792,17 @@ int swf_WriteSWF(int handle,SWF * swf) // Writes SWF to file, returns lengt swf_SetU8(&t1,'S'); swf_SetU8(&t1,swf->fileVersion); - swf_SetU32(&t1,0); // Keep space for filesize + swf_SetU32(&t1,swf->fileSize); // Keep space for filesize swf_SetRect(&t1,&swf->movieSize); 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); + if(swf->firstTag) { + t1.len = 4; // bad & ugly trick ! + swf_SetU32(&t1,swf->fileSize); + } if (handle>=0) { @@ -763,7 +827,15 @@ int swf_WriteSWF(int handle,SWF * swf) // Writes SWF to file, returns lengt return (int)swf->fileSize; } -int WriteCGI(SWF * swf) +int swf_WriteHeader(int handle,SWF * swf) +{ + SWF myswf; + memcpy(&myswf,swf,sizeof(SWF)); + myswf.firstTag = 0; + swf_WriteSWF(handle, &myswf); +} + +int swf_WriteCGI(SWF * swf) { int len; char s[1024];