swftoperl.c
来自「flash swf file player」· C语言 代码 · 共 1,914 行 · 第 1/4 页
C
1,914 行
if(flags & TEXTRECORD_HASCOLOR) { if(type == DEFINETEXT2) {readRGBA(f, &rgba);} else {readRGB(f, &rgba);} } if(flags & TEXTRECORD_HASXOFF) {readSInt16(f); /* x offset */ } if(flags & TEXTRECORD_HASYOFF) {readSInt16(f); /* y offset */ } if(flags & TEXTRECORD_HASFONT) {readUInt16(f); /* font height */ } } else /* it's a text entry */ { numGlyphs = flags & TEXTRECORD_NUMGLYPHS; for(i=0; i<numGlyphs; ++i) { readBits(f, glyphBits); /* glyph index */ readSBits(f, advanceBits); /* glyph advance */ } } return 1;}void printDefineText(FILE *f, Blocktype type) /* type 2 allows transparency */{ int glyphBits, advanceBits; int id = readUInt16(f); struct Rect r; struct Matrix m; printf("\t$s%i = new SWF::Shape(); # Empty shape for text, for now \n", id); readRect(f, &r); byteAlign(); readMatrix(f, &m); glyphBits = readUInt8(f); advanceBits = readUInt8(f); while(printTextRecord(f, glyphBits, advanceBits, type)) ;}void silentSkipBytes(FILE *f, int length){ for(; length>0; --length) readUInt8(f);}void printDoAction(FILE *f, int length){ if(movieclip == 0) { printf("\t$m->add(new SWF::Action(\"\n"); decompileAction(f, length, 0); printf("\"));\n"); } else { printf("\t$s%i->add(new SWF::Action(\"\n", movieclip); decompileAction(f, length, 0); printf("\"));\n"); }}void printMovieClip(FILE *f, int length){ int start = fileOffset; int block, type, l; int nFrames, frame = 0; movieclip = readUInt16(f); nFrames = readUInt16(f); // Sprite heisst jetzt MovieClip! names changed printf("\n\t### MovieClip %i ###\n", movieclip); printf("\t$s%i = new SWF::MovieClip(); # %i frames\n", movieclip, nFrames); while(fileOffset < start+length) { block = readUInt16(f); type = block>>6; l = block & ((1<<6)-1); if(l == 63) /* it's a long block */ l = readUInt32(f); switch(type) { case PLACEOBJECT: printPlaceObject(f, l); break; case PLACEOBJECT2: printPlaceObject2(f); break; case REMOVEOBJECT: printRemoveObject(f); break; case REMOVEOBJECT2: printRemoveObject2(f); break; case FRAMELABEL: printFrameLabel(f); break; case DOACTION: printDoAction(f, l); break; case SHOWFRAME: printf("\t$s%i->nextFrame(); # end of clip frame %i \n\n", movieclip, ++frame); break; case END: putchar('\n'); break; /* case SOUNDSTREAMHEAD: printSoundStreamHead(f, 1); break; case SOUNDSTREAMHEAD2: printSoundStreamHead(f, 2); break; case SOUNDSTREAMBLOCK: printSoundStreamBlock(f, l); break; */ default: printf("\t# %s, %i bytes \n", blockName(type), l); silentSkipBytes(f, l); } } movieclip = 0;}/* peter: Constants with numbers added by peter for testing */#define SWFTEXTFIELD_HASLENGTH (1<<1)#define SWFTEXTFIELD_2 (1<<2)#define SWFTEXTFIELD_NOEDIT (1<<3)#define SWFTEXTFIELD_PASSWORD (1<<4)#define SWFTEXTFIELD_MULTILINE (1<<5)#define SWFTEXTFIELD_WORDWRAP (1<<6)#define SWFTEXTFIELD_7 (1<<7)#define SWFTEXTFIELD_8 (1<<8)#define SWFTEXTFIELD_9 (1<<9)#define SWFTEXTFIELD_10 (1<<10)#define SWFTEXTFIELD_DRAWBOX (1<<11)#define SWFTEXTFIELD_NOSELECT (1<<12)#define SWFTEXTFIELD_ALIGN_LEFT 0#define SWFTEXTFIELD_ALIGN_RIGHT 1#define SWFTEXTFIELD_ALIGN_CENTER 2#define SWFTEXTFIELD_ALIGN_JUSTIFY 3void printTextField(FILE *f, int length){ int notFirst = 0, flags, num, end = fileOffset+length; struct Rect r; struct RGBA c; char *string; int id = readUInt16(f); readRect(f, &r); flags=readUInt16(f); printf("\n\t$s%i = new SWF::TextField(", id); if(flags & SWFTEXTFIELD_HASLENGTH) { printf("SWFTEXTFIELD_HASLENGTH"); notFirst = 1; } if(flags & SWFTEXTFIELD_NOEDIT) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_NOEDIT"); notFirst = 1; } if(flags & SWFTEXTFIELD_PASSWORD) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_PASSWORD"); notFirst = 1; } if(flags & SWFTEXTFIELD_MULTILINE) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_MULTILINE"); notFirst = 1; } if(flags & SWFTEXTFIELD_WORDWRAP) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_WORDWRAP"); notFirst = 1; } if(flags & SWFTEXTFIELD_DRAWBOX) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_DRAWBOX"); notFirst = 1; } if(flags & SWFTEXTFIELD_NOSELECT) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_NOSELECT"); notFirst = 1; }/* peter test *//* if(flags & SWFTEXTFIELD_2) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_2"); notFirst = 1; } if(flags & SWFTEXTFIELD_7) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_7"); notFirst = 1; } if(flags & SWFTEXTFIELD_8) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_8"); notFirst = 1; } if(flags & SWFTEXTFIELD_9) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_9"); notFirst = 1; } if(flags & SWFTEXTFIELD_10) { if (notFirst) { printf(" | ");} printf("SWFTEXTFIELD_10"); notFirst = 1; }*//*end peter test */ printf(");\n"); printf("\t$s%i->setBounds(%i, %i);\n", id, r.xMax, r.yMax); printf("\t$s%i->setFont($f%i);\n", id, readUInt16(f)); printf("\t$s%i->setHeight(%i);\n", id, readUInt16(f)); readRGBA(f, &c); printf("\t$s%i->setColor(0x%02x, 0x%02x, 0x%02x, 0x%02x);\n", id, c.r, c.g, c.b, c.a); if(flags & SWFTEXTFIELD_HASLENGTH) printf("\t$s%i->setLength(%i);\n", id, readUInt16(f)); printf("\t$s%i->align(", id); switch(num = readUInt8(f)) { case SWFTEXTFIELD_ALIGN_LEFT: printf("SWFTEXTFIELD_ALIGN_LEFT"); break; case SWFTEXTFIELD_ALIGN_RIGHT: printf("SWFTEXTFIELD_ALIGN_RIGHT"); break; case SWFTEXTFIELD_ALIGN_CENTER: printf("SWFTEXTFIELD_ALIGN_CENTER"); break; case SWFTEXTFIELD_ALIGN_JUSTIFY: printf("SWFTEXTFIELD_ALIGN_JUSTIFY"); break; default: error("unexpected justification: %i\n", num); } printf(");\n"); if((num = readUInt16(f)) != 0) {printf("\t$s%i->setLeftMargin(%i);\n", id, num);} if((num = readUInt16(f)) != 0) {printf("\t$s%i->setRightMargin(%i);\n", id, num);} if((num = readUInt16(f)) != 0) {printf("\t$s%i->setIndentation(%i);\n", id, num);} if((num = readUInt16(f)) != 40) {printf("\t$s%i->setLineSpacing(%i);\n", id, num);} string = readString(f); if(string[0] != '\0') {printf("\t$s%i->setName('%s');\n", id, string);} if(fileOffset<end) { string = readString(f); if(string[0] != '\0') {printf("\t$s%i->addString('%s');\n", id, string);} } if(fileOffset<end) { printf("# extra garbage (i.e., we goofed in printTextField):\n"); puts("print STDERR <<'DUMP;'"); dumpBytes(f, end-fileOffset); puts("DUMP\n"); }}void skipBytes(FILE *f, int length){ ++gIndent; printf("%s<%i bytes skipped>\n\n", indent(), length); --gIndent; for(; length>0; --length) readUInt8(f);}/* Compressed swf-files have a 8 Byte uncompressed header and a zlib-compressed body. */int cws2fws(FILE *f, uLong outsize){ struct stat statbuffer; int insize; int err,tmp_fd; Byte *inbuffer,*outbuffer; if((tmp_name = tmpnam(NULL)) == NULL){ error("Couldn't create tempfile.\n"); } //fprintf(stderr,"tempfilename: %s\n",tmp_name); if((tmp_fd = open(tmp_name, O_CREAT|O_EXCL|O_RDWR, 00600)) < 0){ error("Possible Link Attack detected!\n"); } if( (tempfile=fdopen(tmp_fd, "w+"))< 0){ error("Couldn't open tempfile.\n"); } if(stat(filename, &statbuffer)==-1){ error("stat() failed"); } insize = statbuffer.st_size-8; inbuffer = malloc(insize); if(!inbuffer){ error("malloc() failed"); } fread(inbuffer,insize,1,f); /* We don't trust the value in the swfheader. */ do{ outbuffer = malloc(outsize); if (!outbuffer){ error("malloc(%lu) failed",outsize); } err=uncompress(outbuffer,&outsize,inbuffer,insize); switch(err){ case Z_MEM_ERROR: error( "Not enough memory.\n");break; case Z_BUF_ERROR: fprintf(stderr,"resizing outbuffer..\n");break; case Z_DATA_ERROR: error("Data corrupted. Couldn't uncompress.\n");break; case Z_OK: break; default: error("Unknown returnvalue of uncompress:%i\n",err); } free(outbuffer); outsize*=2; } while(err == Z_BUF_ERROR); outsize/=2; fwrite(outbuffer, 1, outsize, tempfile); rewind(tempfile); return (int)outsize;}int main(int argc, char *argv[]){ struct Movie m; FILE *f; char first; int block, type, length, frame = 0, noactions = 0; int compressed= 0; if(argc == 3 && strcmp(argv[1], "-a") == 0) { noactions = 1; --argc; ++argv; } filename= argv[1]; if(argc<2) { error("Give me a filename.\n\n\tswftoperl myflash..swf >myflash.pl"); } if(!(f = fopen(filename,"rb"))){ error("Sorry, can't seem to read that file.\n"); } first=readUInt8(f); compressed=(first==('C'))?1:0; if(!((first=='C' || first=='F') && readUInt8(f)=='W' && readUInt8(f)=='S')){ error("Doesn't look like a swf file to me..\n"); } m.version = readUInt8(f); m.size = readUInt32(f); if(compressed){ int unzipped=cws2fws(f, m.size); if (m.size!=(unzipped+8)){ warning("m.size: %i != %i+8 Maybe wrong value in swfheader.\n",m.size, unzipped+8); } fclose(f); f=tempfile; rewind(f); } readRect(f, &(m.frame)); m.rate = readUInt8(f)/256.0+readUInt8(f); m.nFrames = readUInt16(f); printf("#!/usr/bin/perl -w\n"); printf("# Generated by swftoperl converter included with ming. Have fun. \n\n"); printf("# Change this to your needs. If you installed perl-ming global you don't need this.\n"); printf("#use lib(\"/home/peter/mystuff/lib/site_perl\");\n\n"); printf("# We import all because our converter is not so clever to select only needed. ;-)\n"); printf("use SWF qw(:ALL);\n"); printf("# Just copy from a sample, needed to use Constants like SWFFILL_RADIAL_GRADIENT\n"); printf("use SWF::Constants qw(:Text :Button :DisplayItem :Fill);\n\n"); printf("SWF::setScale(1);\n"); printf("SWF::setVersion(%i);\n", m.version); printf("\t$m = new SWF::Movie();\n\n"); printf("\t$m->setRate(%f);\n", m.rate); printf("\t$m->setDimension(%i, %i);\n", m.frame.xMax, m.frame.yMax); printf("\t$m->setFrames(%i);\n", m.nFrames); if(noactions){ m_version = 0; } else { m_version = m.version; } for(;;) { block = readUInt16(f); type = block>>6; length = block & ((1<<6)-1); if(length == 63) /* it's a long block. */ length = readUInt32(f); if(type == 0 || fileOffset >= m.size) break; switch(type) { case DEFINESHAPE3: case DEFINESHAPE2: case DEFINESHAPE: printShape(f, length, type); break; case SETBACKGROUNDCOLOR: printSetBackgroundColor(f); break; case SHOWFRAME: printf("\t$m->nextFrame(); # end of frame %i\n", ++frame); break; case PLACEOBJECT2: printPlaceObject2(f); break; case REMOVEOBJECT2: printRemoveObject2(f); break; case DOACTION: printDoAction(f, length); break; case DEFINESPRITE: printMovieClip(f, length); break; case DEFINEBUTTON: printDefineButton(f, length); break; case DEFINEBUTTON2: printDefineButton2(f, length); break; case DEFINEBUTTONSOUND: printDefineButtonSound(f, length); break; case FRAMELABEL: printFrameLabel(f); break; case DEFINETEXT: case DEFINETEXT2: printDefineText(f, type); break; case TEXTFIELD: printTextField(f, length); break; case DEFINEMORPHSHAPE: printMorphShape(f, length); break; case PLACEOBJECT: printPlaceObject(f, length); break; case REMOVEOBJECT: printRemoveObject(f); break; //case DEFINEFONT: printDefineFont(f, length); break; case DEFINEFONT2: printDefineFont2(f, length); break; /* case DEFINEFONTINFO: printFontInfo(f, length); break; case DEFINESOUND: printDefineSound(f, length); break; case SOUNDSTREAMHEAD: printSoundStreamHead(f, 1); break; case SOUNDSTREAMHEAD2: printSoundStreamHead(f, 2); break; case SOUNDSTREAMBLOCK: printSoundStreamBlock(f, length); break; case JPEGTABLES: printJpegStream(f, length); break; case DEFINEBITS: case DEFINEBITSJPEG2: printDefineBitsJpeg(f,length); break; case DEFINEBITSJPEG3: */ default: printf("\t# %s, %i bytes \n", blockName(type), length); silentSkipBytes(f, length); } } putchar('\n'); if(fileOffset < m.size) { printf("# extra garbage (i.e., we messed up in main):\n"); printf("print STDERR <<'DUMP;'"); dumpBytes(f, m.size-fileOffset); printf("\nDUMP\n\n"); } printf("\n\t#only for webserverscripts\n"); printf("\t#print('Content-type: application/x-shockwave-flash\\n\\n');\n"); printf("\t$m->output(%i);\n",(compressed)?'9':'0'); printf("\t$m->save(\"$0.swf\",%i);\n",(compressed)?'9':'0'); fclose(f); if (compressed){ unlink(tmp_name); } exit(0);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?