From 07b215b2a0b442a8f9b832b9ff94755bb6191d48 Mon Sep 17 00:00:00 2001 From: boehme Date: Wed, 31 Oct 2001 13:08:37 +0000 Subject: [PATCH] rfxswf cleanups: TAG structure and ResetWriteBits/ResetReadBits fixed GetName()/TagGetName() confusion --- TODO | 3 +- lib/modules/swfshape.c | 10 ++-- lib/modules/swftext.c | 8 +-- lib/modules/swftools.c | 4 +- lib/old_rfxswf.h | 46 ++++++++--------- lib/rfxswf.c | 52 +++++++++---------- lib/rfxswf.h | 23 ++++++--- pdf2swf/swfoutput.cc | 4 +- src/swfdump.c | 134 ++++++++++++++++++++++++------------------------ 9 files changed, 146 insertions(+), 138 deletions(-) diff --git a/TODO b/TODO index 965b60c..0cb33dd 100644 --- a/TODO +++ b/TODO @@ -26,6 +26,5 @@ swfcombine: rfxswf-lib: * modules/swfshape doesn't allow changing to fillstyle 0 - * DataPos should be named DataReadPos, DataSize DataWritePos. - * bitcount should be named BitWritePos, bitmask BitReadPos + diff --git a/lib/modules/swfshape.c b/lib/modules/swfshape.c index f107f92..4f41406 100644 --- a/lib/modules/swfshape.c +++ b/lib/modules/swfshape.c @@ -54,7 +54,7 @@ int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Recor if (FAILED(swf_ShapeNew(s))) return -1; sh = s[0]; - swf_ResetBitmask(t); + swf_ResetReadBits(t); sh->bits.fill = (U16)swf_GetBits(t,4); sh->bits.line = (U16)swf_GetBits(t,4); bitl = 0; end = 0; pos = swf_GetTagPos(t); @@ -144,13 +144,13 @@ int swf_SetSimpleShape(TAG * t,SHAPE * s) // without Linestyle/Fillstyle Record l = (s->bitlen+7)/8; if (t) - { swf_ResetBitcount(t); + { swf_ResetWriteBits(t); swf_SetBits(t,s->bits.fill,4); swf_SetBits(t,s->bits.line,4); swf_SetBlock(t,s->data,l); - swf_ResetBitcount(t); + swf_ResetWriteBits(t); } return l+1; } @@ -231,7 +231,7 @@ int swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits) int swf_SetShapeBits(TAG * t,SHAPE * s) { if ((!t)||(!s)) return -1; - swf_ResetBitcount(t); + swf_ResetWriteBits(t); swf_SetBits(t,s->bits.fill,4); swf_SetBits(t,s->bits.line,4); return 0; @@ -439,7 +439,7 @@ int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,U16 line,U16 fill0,U16 fill1) int swf_ShapeSetEnd(TAG * t) { if (!t) return -1; swf_SetBits(t,0,6); - swf_ResetBitcount(t); + swf_ResetWriteBits(t); return 0; } diff --git a/lib/modules/swftext.c b/lib/modules/swftext.c index 78b3bce..1b77024 100644 --- a/lib/modules/swftext.c +++ b/lib/modules/swftext.c @@ -267,7 +267,7 @@ int swf_FontSetDefine(TAG * t,SWFFONT * f) int p,i,j; if ((!t)||(!f)) return -1; - swf_ResetBitcount(t); + swf_ResetWriteBits(t); swf_SetU16(t,f->id); p = 0; j = 0; @@ -283,14 +283,14 @@ int swf_FontSetDefine(TAG * t,SWFFONT * f) if (f->glyph[i].shape) swf_SetSimpleShape(t,f->glyph[i].shape); - swf_ResetBitcount(t); + swf_ResetWriteBits(t); return 0; } int swf_FontSetInfo(TAG * t,SWFFONT * f) { int l,i; if ((!t)||(!f)) return -1; - swf_ResetBitcount(t); + swf_ResetWriteBits(t); swf_SetU16(t,f->id); l = strlen(f->name); if (l>255) l = 255; swf_SetU8(t,l); @@ -484,7 +484,7 @@ int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 ab swf_SetBits(t,(((U32)font->glyph[s[i]].advance)*scale)/100,abits); } - swf_ResetBitcount(t); + swf_ResetWriteBits(t); return 0; } diff --git a/lib/modules/swftools.c b/lib/modules/swftools.c index bbbfee3..5349620 100644 --- a/lib/modules/swftools.c +++ b/lib/modules/swftools.c @@ -223,7 +223,7 @@ U16 swf_GetDepth(TAG * t) return depth; } -char* swf_GetTagName(TAG * t) +char* swf_GetName(TAG * t) { char* name = 0; U32 oldTagPos; @@ -248,7 +248,7 @@ char* swf_GetTagName(TAG * t) if(flags&PF_RATIO) swf_GetU16(t); if(flags&PF_NAME) { - swf_ResetBitmask(t); + swf_ResetReadBits(t); name = &t->data[swf_GetTagPos(t)]; } } diff --git a/lib/old_rfxswf.h b/lib/old_rfxswf.h index 475c321..95b3f11 100644 --- a/lib/old_rfxswf.h +++ b/lib/old_rfxswf.h @@ -1,23 +1,23 @@ -/* old_rfxswf.h - - File which defines macros and typedefs for old versions of - the rfxswf lib functions and datatypes. (For routines which - still use them) - - Part of the swftools package. - - Copyright (c) 2000, 2001 Rainer Böhme - - This file is distributed under the GPL, see file COPYING for details - -*/ - -#include "rfxswf.h" - -/* -#define DataPos dataReadPos -#define DataSize dataWritePos -#define bitcount bitWritePos -#define bitmask bitWritePos - -*/ +/* old_rfxswf.h + + File which defines macros and typedefs for old versions of + the rfxswf lib functions and datatypes. (For routines which + still use them) + + Part of the swftools package. + + Copyright (c) 2000, 2001 Rainer Böhme + + This file is distributed under the GPL, see file COPYING for details + +*/ + +#include "rfxswf.h" + +/* +#define DataPos dataReadPos +#define DataSize dataWritePos +#define bitcount bitWritePos +#define bitmask bitWritePos + +*/ diff --git a/lib/rfxswf.c b/lib/rfxswf.c index 801171f..7b2bf69 100644 --- a/lib/rfxswf.c +++ b/lib/rfxswf.c @@ -58,16 +58,16 @@ 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 +75,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 +87,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 +101,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 +117,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 +127,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 +148,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 +159,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 +173,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 +185,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 +215,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 +313,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,7 +347,7 @@ 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 @@ -391,7 +391,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 +451,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 +498,7 @@ TAG * swf_InsertTag(TAG * after,U16 id) // updates frames, if nescessary if (t) { memset(t,0x00,sizeof(TAG)); t->id = id; - t->bitcount = 0x80; + t->writeBit = 0x80; if (after) { t->frame = after->frame; diff --git a/lib/rfxswf.h b/lib/rfxswf.h index b572f52..377123e 100644 --- a/lib/rfxswf.h +++ b/lib/rfxswf.h @@ -70,18 +70,27 @@ typedef struct _CXFORM typedef struct _TAG // NEVER access a Tag-Struct directly ! { U16 id; - U32 len; U8 * data; + U32 memsize; // to minimize realloc() calls + + union + { U32 len; // for Set-Access + U32 dataWritePos; + }; + + union + { U32 pos; // for Get-Access + U32 dataReadPos; + }; - int frame; + int frame; // not really up-to-date struct _TAG * next; struct _TAG * prev; - U32 memsize; // to minimize realloc() calls - U32 pos; // for Get/Set-Access - U8 bitmask; // for Bit-Manipulating Functions [read] - U8 bitcount; // [write] + U8 readBit; // for Bit-Manipulating Functions [read] + U8 writeBit; // [write] + } TAG, * LPTAG; typedef struct _ActionTAG @@ -457,7 +466,7 @@ U8 swf_isAllowedSpriteTag(TAG * t); U16 swf_GetDefineID(TAG * t); U16 swf_GetPlaceID(TAG * t); //PLACEOBJECT, PLACEOBJECT2 (sometimes), REMOVEOBJECT U16 swf_GetDepth(TAG * t); //PLACEOBJECT,PLACEOBJECT2,REMOVEOBJECT,REMOVEOBJECT2 -char* swf_GetTagName(TAG * t); //PLACEOBJECT2, FRAMELABEL +char* swf_GetName(TAG * t); //PLACEOBJECT2, FRAMELABEL MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2); MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy, int x0,int y0,int x1,int y1,int x2,int y2); diff --git a/pdf2swf/swfoutput.cc b/pdf2swf/swfoutput.cc index 3fd1379..6cef2e6 100644 --- a/pdf2swf/swfoutput.cc +++ b/pdf2swf/swfoutput.cc @@ -333,7 +333,7 @@ void putcharacters(TAG*tag) } if(charstorepos && pass==1) { - tag->bitcount = 0; + tag->writeBit = 0; // Q&D swf_SetBits(tag, 0, 1); // GLYPH Record swf_SetBits(tag, charstorepos, 7); // number of glyphs int s; @@ -366,7 +366,7 @@ void putcharacters(TAG*tag) if(lastfontid != chardata[t].fontid || lastsize != chardata[t].size) newfont = &font; - tag->bitcount = 0; + tag->writeBit = 0; // Q&D swf_TextSetInfoRecord(tag, newfont, chardata[t].size, newcolor, newx,newy); } diff --git a/src/swfdump.c b/src/swfdump.c index d0734f5..c292c89 100644 --- a/src/swfdump.c +++ b/src/swfdump.c @@ -47,15 +47,15 @@ struct options_t options[] = int args_callback_option(char*name,char*val) { if(!strcmp(name, "V")) { - printf("swfdump - part of %s %s\n", PACKAGE, VERSION); - exit(0); + printf("swfdump - part of %s %s\n", PACKAGE, VERSION); + exit(0); } else if(name[0]=='a') { - action = 1; - return 0; + action = 1; + return 0; } else { - printf("Unknown option: -%s\n", name); + printf("Unknown option: -%s\n", name); } return 0; @@ -74,8 +74,8 @@ void args_callback_usage(char*name) int args_callback_command(char*name,char*val) { if(filename) { - fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n", - filename, name); + fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n", + filename, name); } filename = name; return 0; @@ -104,29 +104,29 @@ int main (int argc,char ** argv) if(!filename) { - fprintf(stderr, "You must supply a filename.\n"); - return 1; + fprintf(stderr, "You must supply a filename.\n"); + return 1; } f = open(filename,O_RDONLY); if (f<0) { - perror("Couldn't open file: "); - exit(1); + perror("Couldn't open file: "); + exit(1); } if FAILED(swf_ReadSWF(f,&swf)) { - fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename); + fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename); close(f); - exit(1); + exit(1); } #ifdef HAVE_STAT fstat(f, &statbuf); if(statbuf.st_size != swf.FileSize) - fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)", - statbuf.st_size, swf.FileSize); + fprintf(stderr, "Error: Real Filesize (%d) doesn't match header Filesize (%d)", + statbuf.st_size, swf.FileSize); #endif close(f); @@ -141,59 +141,59 @@ int main (int argc,char ** argv) tag = swf.firstTag; while(tag) { - char*name = swf_GetTagName(tag); - if(!name) { - fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id); - tag = tag->next; - continue; - } - printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_GetTagName(tag)); - - if(swf_isDefiningTag(tag)) { - U16 id = swf_GetDefineID(tag); - printf(" defines id %04x", id); - if(idtab[id]) - fprintf(stderr, "Error: Id %04x is defined more than once.\n", id); - idtab[id] = 1; - } - else if(tag->id == ST_PLACEOBJECT || - tag->id == ST_PLACEOBJECT2) { - printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag)); - if(swf_TagGetName(tag)) - printf(" name \"%s\"",swf_TagGetName(tag)); + char*name = swf_TagGetName(tag); + if(!name) { + fprintf(stderr, "Error: Unknown tag:0x%03x\n", tag->id); + tag = tag->next; + continue; } - else if(tag->id == ST_REMOVEOBJECT) { - printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag)); - } - else if(tag->id == ST_REMOVEOBJECT2) { - printf(" removes object from depth %04x", swf_GetDepth(tag)); - } - - printf("\n"); - - if(tag->id == ST_DEFINESPRITE) { - sprintf(prefix, " "); - } - else if(tag->id == ST_END) { - *prefix = 0; - } - else if(tag->id == ST_DOACTION && action) { - char myprefix[128]; - ActionTAG*actions; - sprintf(myprefix, " %s", prefix); - - actions = swf_GetActions(tag); - - swf_DumpActions(actions, myprefix); - -/* what = "URL"; - ActionEnumerateURLs(actions, testfunc); - what = "String"; - ActionEnumerateStrings(actions, testfunc); - what = "Target"; - ActionEnumerateTargets(actions, testfunc);*/ - } - tag = tag->next; + printf("[%03x] %9ld %s%s", tag->id, tag->len, prefix, swf_TagGetName(tag)); + + if(swf_isDefiningTag(tag)) { + U16 id = swf_GetDefineID(tag); + printf(" defines id %04x", id); + if(idtab[id]) + fprintf(stderr, "Error: Id %04x is defined more than once.\n", id); + idtab[id] = 1; + } + else if(tag->id == ST_PLACEOBJECT || + tag->id == ST_PLACEOBJECT2) { + printf(" places id %04x at depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag)); + if(swf_GetName(tag)) + printf(" name \"%s\"",swf_GetName(tag)); + } + else if(tag->id == ST_REMOVEOBJECT) { + printf(" removes id %04x from depth %04x", swf_GetPlaceID(tag), swf_GetDepth(tag)); + } + else if(tag->id == ST_REMOVEOBJECT2) { + printf(" removes object from depth %04x", swf_GetDepth(tag)); + } + + printf("\n"); + + if(tag->id == ST_DEFINESPRITE) { + sprintf(prefix, " "); + } + else if(tag->id == ST_END) { + *prefix = 0; + } + else if(tag->id == ST_DOACTION && action) { + char myprefix[128]; + ActionTAG*actions; + sprintf(myprefix, " %s", prefix); + + actions = swf_GetActions(tag); + + swf_DumpActions(actions, myprefix); + +/* what = "URL"; + ActionEnumerateURLs(actions, testfunc); + what = "String"; + ActionEnumerateStrings(actions, testfunc); + what = "Target"; + ActionEnumerateTargets(actions, testfunc);*/ + } + tag = tag->next; } swf_FreeTags(&swf); -- 1.7.10.4