2 Allows to extract parts of the swf into a new file.
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25 #include "../lib/rfxswf.h"
26 #include "../lib/args.h"
27 #include "../lib/log.h"
31 #define _ZLIB_INCLUDED_
36 char * destfilename = "output.swf";
40 char* extractframes = 0;
41 char* extractjpegids = 0;
42 char* extractfontids = 0;
43 char* extractpngids = 0;
44 char* extractsoundids = 0;
45 char* extractbinaryids = 0;
48 char* extractname = 0;
51 char originalplaceobjects = 0;
56 struct options_t options[] =
77 int args_callback_option(char*name,char*val)
79 if(!strcmp(name, "V")) {
80 printf("swfextract - part of %s %s\n", PACKAGE, VERSION);
83 else if(!strcmp(name, "o")) {
87 else if(!strcmp(name, "i")) {
91 fprintf(stderr, "You can only supply either name or id\n");
96 else if(!strcmp(name, "n")) {
100 fprintf(stderr, "You can only supply either name or id\n");
105 else if(!strcmp(name, "v")) {
109 else if(!strcmp(name, "m")) {
114 else if(!strcmp(name, "j")) {
116 fprintf(stderr, "Only one --jpegs argument is allowed. (Try to use a range, e.g. -j 1,2,3)\n");
119 /* TODO: count number of IDs in val range */
121 extractjpegids = val;
124 else if(!strcmp(name, "F")) {
126 fprintf(stderr, "Only one --font argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
130 extractfontids = val;
133 else if(!strcmp(name, "s")) {
134 if(extractsoundids) {
135 fprintf(stderr, "Only one --sound argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
139 extractsoundids = val;
142 else if(!strcmp(name, "b")) {
143 if(extractbinaryids) {
144 fprintf(stderr, "Only one --binary argument is allowed. (Try to use a range, e.g. -s 1,2,3)\n");
148 extractbinaryids = val;
151 #ifdef _ZLIB_INCLUDED_
152 else if(!strcmp(name, "p")) {
154 fprintf(stderr, "Only one --png argument is allowed. (Try to use a range, e.g. -p 1,2,3)\n");
162 else if(!strcmp(name, "f")) {
167 else if(!strcmp(name, "P")) {
168 originalplaceobjects = 1;
171 else if(!strcmp(name, "0")) {
175 else if(!strcmp(name, "w")) {
180 printf("Unknown option: -%s\n", name);
186 int args_callback_longoption(char*name,char*val)
188 return args_long2shortoption(options, name, val);
190 void args_callback_usage(char*name)
192 printf("Usage: %s [-v] [-n name] [-ijf ids] file.swf\n", name);
193 printf("\t-v , --verbose\t\t\t Be more verbose\n");
194 printf("\t-o , --output filename\t\t set output filename\n");
195 printf("\t-V , --version\t\t\t Print program version and exit\n\n");
196 printf("SWF Subelement extraction:\n");
197 printf("\t-n , --name name\t\t instance name of the object (SWF Define) to extract\n");
198 printf("\t-i , --id ID\t\t\t ID of the object, shape or movieclip to extract\n");
199 printf("\t-f , --frame frames\t\t frame numbers to extract\n");
200 printf("\t-w , --hollow\t\t\t hollow mode: don't remove empty frames\n");
201 printf("\t \t\t\t (use with -f)\n");
202 printf("\t-P , --placeobject\t\t\t Insert original placeobject into output file\n");
203 printf("\t \t\t\t (use with -i)\n");
204 printf("SWF Font/Text extraction:\n");
205 printf("\t-F , --font ID\t\t\t Extract font(s)\n");
206 printf("Picture extraction:\n");
207 printf("\t-j , --jpeg ID\t\t\t Extract JPEG picture(s)\n");
208 #ifdef _ZLIB_INCLUDED_
209 printf("\t-p , --pngs ID\t\t\t Extract PNG picture(s)\n");
212 printf("Sound extraction:\n");
213 printf("\t-m , --mp3\t\t\t Extract main mp3 stream\n");
214 printf("\t-s , --sound ID\t\t\t Extract Sound(s)\n");
216 int args_callback_command(char*name,char*val)
219 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
226 U8 mainr,maing,mainb;
227 /* 1 = used, not expanded,
229 5 = wanted, not expanded
236 int extractname_id = -1;
238 void idcallback(void*data)
240 if(!(used[GET16(data)]&1)) {
242 used[GET16(data)] |= 1;
246 void enumerateIDs(TAG*tag, void(*callback)(void*))
252 data = (U8*)malloc(len);
253 PUT16(data, (tag->id<<6)+63);
254 *(U8*)&data[2] = tag->len;
255 *(U8*)&data[3] = tag->len>>8;
256 *(U8*)&data[4] = tag->len>>16;
257 *(U8*)&data[5] = tag->len>>24;
258 memcpy(&data[6], tag->data, tag->len);
261 data = (U8*)malloc(len);
262 PUT16(data, (tag->id<<6)+tag->len);
263 memcpy(&data[2], tag->data, tag->len);
265 map_ids_mem(data, len, callback);
267 int num = swf_GetNumUsedIDs(tag);
268 int *ptr = malloc(sizeof(int)*num);
270 swf_GetUsedIDs(tag, ptr);
272 callback(&tag->data[ptr[t]]);
275 void moveToZero(TAG*tag)
277 if(!swf_isPlaceTag(tag))
280 swf_GetPlaceObject(tag, &obj);
283 swf_ResetTag(tag, tag->id);
284 swf_SetPlaceObject(tag, &obj);
287 void extractTag(SWF*swf, char*filename)
299 memset(&newswf,0x00,sizeof(SWF)); // set global movie parameters
301 newswf.fileVersion = swf->fileVersion;
302 newswf.frameRate = swf->frameRate;
303 newswf.movieSize = swf->movieSize;
304 if(movetozero && originalplaceobjects) {
305 newswf.movieSize.xmax = swf->movieSize.xmax - swf->movieSize.xmin;
306 newswf.movieSize.ymax = swf->movieSize.ymax - swf->movieSize.ymin;
307 newswf.movieSize.xmin = 0;
308 newswf.movieSize.ymin = 0;
311 newswf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
312 desttag = newswf.firstTag;
316 swf_SetRGB(desttag,&rgb);
318 swf_GetRect(0, &objectbbox);
322 for(t=0;t<65536;t++) {
323 if(used[t] && !(used[t]&2)) {
325 msg("<warning> ID %d is referenced, but never defined.", t);
326 } else if(tags[t]->id==ST_DEFINESPRITE) {
328 while(tag->id != ST_END)
330 enumerateIDs(tag, idcallback);
335 enumerateIDs(tags[t], idcallback);
342 srctag = swf->firstTag;
345 while(srctag && (srctag->id || sprite)) {
350 if(srctag->id == ST_END) {
353 if(srctag->id == ST_DEFINESPRITE)
355 if(srctag->id == ST_JPEGTABLES)
357 if(swf_isDefiningTag(srctag)) {
358 int id = swf_GetDefineID(srctag);
362 b = swf_GetDefineBBox(srctag);
363 swf_ExpandRect2(&objectbbox, &b);
366 if ((((swf_isPlaceTag(srctag) && originalplaceobjects)
367 || srctag->id == ST_STARTSOUND) && (used[swf_GetPlaceID(srctag)]&4) ) ||
368 (swf_isPseudoDefiningTag(srctag) && used[swf_GetDefineID(srctag)]) ||
375 if(srctag->id == ST_REMOVEOBJECT) {
376 if(!used[swf_GetPlaceID(srctag)])
381 TAG*ttag = (TAG*)malloc(sizeof(TAG));
382 desttag = swf_InsertTag(desttag, srctag->id);
383 desttag->len = desttag->memsize = srctag->len;
384 desttag->data = malloc(srctag->len);
385 memcpy(desttag->data, srctag->data, srctag->len);
386 if(movetozero && swf_isPlaceTag(desttag)) {
393 srctag = srctag->next;
396 if(!extractframes && !hollow) {
397 if(!originalplaceobjects && (extractids||extractname_id>=0)) {
403 memset(&bbox, 0, sizeof(SRECT));
405 for(t=0;t<65536;t++) {
406 if(is_in_range(t, extractids)) {
413 printf("warning! You should use the -P when extracting multiple objects\n");
417 /* if there is only one object, we will scale it.
418 So let's figure out its bounding box */
419 TAG*tag = swf->firstTag;
421 if(swf_isDefiningTag(tag) && tag->id != ST_DEFINESPRITE) {
422 if(swf_GetDefineID(tag) == id)
423 bbox = swf_GetDefineBBox(tag);
428 newswf.movieSize.xmin = 0;
429 newswf.movieSize.ymin = 0;
430 newswf.movieSize.xmax = 512*20;
431 newswf.movieSize.ymax = 512*20;
433 if((objectbbox.xmin|objectbbox.ymin|objectbbox.xmax|objectbbox.ymax)!=0)
434 newswf.movieSize = objectbbox;
437 if(extractname_id>=0) {
438 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
439 swf_ObjectPlace(desttag, extractname_id, extractname_id, 0,0,extractname);
441 for(t=0;t<65536;t++) {
442 if(is_in_range(t, extractids)) {
444 desttag = swf_InsertTag(desttag, ST_PLACEOBJECT2);
445 swf_GetMatrix(0, &m);
447 int width = bbox.xmax - bbox.xmin;
448 int height = bbox.ymax - bbox.ymin;
449 int max = width>height?width:height;
453 m.sx = (512*20*65536)/max;
454 m.sy = (512*20*65536)/max;
456 //newswf.movieSize = swf_TurnRect(newswf.movieSize, &m);
458 swf_ObjectPlace(desttag, t, t, &m,0,0);
463 desttag = swf_InsertTag(desttag,ST_SHOWFRAME);
465 desttag = swf_InsertTag(desttag,ST_END);
467 f = open(filename, O_TRUNC|O_WRONLY|O_CREAT|O_BINARY, 0644);
468 if FAILED(swf_WriteSWF(f,&newswf)) fprintf(stderr,"WriteSWF() failed.\n");
471 swf_FreeTags(&newswf); // cleanup
474 int isOfType(int t, TAG*tag)
477 if(t == 0 && (tag->id == ST_DEFINESHAPE ||
478 tag->id == ST_DEFINESHAPE2 ||
479 tag->id == ST_DEFINESHAPE3)) {
482 if(t==1 && tag->id == ST_DEFINESPRITE) {
485 if(t == 2 && (tag->id == ST_DEFINEBITS ||
486 tag->id == ST_DEFINEBITSJPEG2 ||
487 tag->id == ST_DEFINEBITSJPEG3)) {
490 if(t == 3 && (tag->id == ST_DEFINEBITSLOSSLESS ||
491 tag->id == ST_DEFINEBITSLOSSLESS2)) {
494 if(t == 4 && (tag->id == ST_DEFINESOUND)) {
497 if(t == 5 && (tag->id == ST_DEFINEFONT || tag->id == ST_DEFINEFONT2 || tag->id == ST_DEFINEFONT3)) {
500 if (t== 6 && (tag->id == ST_DEFINEBINARY)) {
506 void listObjects(SWF*swf)
512 char*names[] = {"Shape", "MovieClip", "JPEG", "PNG", "Sound", "Font", "Binary"};
513 char*options[] = {"-i", "-i", "-j", "-p", "-s", "-F","-b"};
515 printf("Objects in file %s:\n",filename);
517 for(t=0;t<sizeof(names)/sizeof(names[0]);t++) {
519 int lastid = -2, lastprint=-1;
524 if(tag->id == ST_SOUNDSTREAMHEAD || tag->id == ST_SOUNDSTREAMHEAD2)
533 printf(" [%s] %d %s%s: ID(s) ", options[t], nr, names[t], nr>1?"s":"");
538 char show = isOfType(t,tag);
544 id = swf_GetDefineID(tag);
549 if(first || !follow) {
554 if(lastprint + 1 == lastid)
555 printf(", %d, %d", lastid, id);
557 printf("-%d, %d", lastid, id);
567 if(lastprint + 1 == lastid)
568 printf(", %d", lastid);
570 printf("-%d", lastid);
576 printf(" [-f] %d Frames: ID(s) 0-%d\n", frame, frame);
578 printf(" [-f] 1 Frame: ID(s) 0\n");
581 printf(" [-m] 1 MP3 Soundstream\n");
584 void handlefont(SWF*swf, TAG*tag)
589 char*filename = name;
592 id = swf_GetDefineID(tag);
593 sprintf(name, "font%d.swf", id);
595 filename = destfilename;
598 swf_FontExtract(swf, id, &f);
600 printf("Couldn't extract font %d\n", id);
604 swf_WriteFont(f, filename);
608 static char has_jpegtables=0;
609 static U8*jpegtables = 0;
610 static int jpegtablessize = 0;
612 void handlejpegtables(TAG*tag)
614 if(tag->id == ST_JPEGTABLES) {
615 jpegtables = tag->data;
616 jpegtablessize = tag->len;
621 FILE* save_fopen(char* name, char* mode)
623 FILE*fi = fopen(name, mode);
625 fprintf(stderr, "Error: Couldn't open %s\n", name);
631 int findjpegboundary(U8*data, int len)
635 for(t=0;t<len-4;t++) {
646 /* extract jpeg data out of a tag */
647 void handlejpeg(TAG*tag)
650 char*filename = name;
653 sprintf(name, "pic%d.jpg", GET16(tag->data));
655 filename = destfilename;
656 if(!strcmp(filename,"output.swf"))
657 filename = "output.jpg";
659 /* swf jpeg images have two streams, which both start with ff d8 and
660 end with ff d9. The following code handles sorting the middle
661 <ff d9 ff d8> bytes out, so that one stream remains */
662 if(tag->id == ST_DEFINEBITSJPEG && tag->len>2 && has_jpegtables) {
663 fi = save_fopen(filename, "wb");
664 if(jpegtablessize>=2) {
665 fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
666 fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
668 fwrite(tag->data+2, tag->len-2, 1, fi);
672 else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
674 int pos = findjpegboundary(&tag->data[2], tag->len-2);
677 fi = save_fopen(filename, "wb");
678 fwrite(&tag->data[2], pos-2, 1, fi);
679 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
682 fi = save_fopen(filename, "wb");
683 fwrite(&tag->data[2], end-2, 1, fi);
687 else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
688 U32 end = GET32(&tag->data[2])+6;
689 int pos = findjpegboundary(&tag->data[6], tag->len-6);
691 fi = save_fopen(filename, "wb");
692 fwrite(&tag->data[6], end-6, 1, fi);
696 fi = save_fopen(filename, "wb");
697 fwrite(&tag->data[6], pos-6, 1, fi);
698 fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
703 int id = GET16(tag->data);
704 fprintf(stderr, "Object %d is not a JPEG picture!\n",id, jpegtables);
709 #ifdef _ZLIB_INCLUDED_
712 static U32*crc32_table = 0;
713 static void make_crc32_table(void)
718 crc32_table = (U32*)malloc(1024);
720 for (t = 0; t < 256; t++) {
723 for (s = 0; s < 8; s++) {
724 c = (0xedb88320L*(c&1)) ^ (c >> 1);
729 static inline void png_write_byte(FILE*fi, U8 byte)
731 fwrite(&byte,1,1,fi);
732 mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
734 static void png_start_chunk(FILE*fi, char*type, int len)
736 U8 mytype[4]={0,0,0,0};
737 U32 mylen = REVERSESWAP32(len);
738 memcpy(mytype,type,strlen(type));
739 fwrite(&mylen, 4, 1, fi);
741 png_write_byte(fi,mytype[0]);
742 png_write_byte(fi,mytype[1]);
743 png_write_byte(fi,mytype[2]);
744 png_write_byte(fi,mytype[3]);
746 static void png_write_bytes(FILE*fi, U8*bytes, int len)
750 png_write_byte(fi,bytes[t]);
752 static void png_write_dword(FILE*fi, U32 dword)
754 png_write_byte(fi,dword>>24);
755 png_write_byte(fi,dword>>16);
756 png_write_byte(fi,dword>>8);
757 png_write_byte(fi,dword);
759 static void png_end_chunk(FILE*fi)
761 U32 tmp = REVERSESWAP32((mycrc32^0xffffffff));
766 /* extract a lossless image (png) out of a tag
767 This routine was originally meant to be a one-pager. I just
768 didn't know png is _that_ much fun. :) -mk
770 void handlelossless(TAG*tag)
773 char*filename = name;
788 U8 head[] = {137,80,78,71,13,10,26,10};
790 char alpha = tag->id == ST_DEFINEBITSLOSSLESS2;
798 if(tag->id != ST_DEFINEBITSLOSSLESS &&
799 tag->id != ST_DEFINEBITSLOSSLESS2) {
800 int id = GET16(tag->data);
801 fprintf(stderr, "Object %d is not a PNG picture!\n",id);
806 format = swf_GetU8(tag);
807 if(format == 3) bpp = 8;
808 if(format == 4) bpp = 16;
809 if(format == 5) bpp = 32;
810 if(format!=3 && format!=5) {
812 fprintf(stderr, "Can't handle 16-bit palette images yet (image %d)\n",id);
814 fprintf(stderr, "Unknown image type %d in image %d\n", format, id);
817 width = swf_GetU16(tag);
818 height = swf_GetU16(tag);
819 if(format == 3) cols = swf_GetU8(tag) + 1;
820 // this is what format means according to the flash specification. (which is
822 // if(format == 4) cols = swf_GetU16(tag) + 1;
823 // if(format == 5) cols = swf_GetU32(tag) + 1;
826 msg("<verbose> Width %d", width);
827 msg("<verbose> Height %d", height);
828 msg("<verbose> Format %d", format);
829 msg("<verbose> Cols %d", cols);
830 msg("<verbose> Bpp %d", bpp);
832 datalen = (width*height*bpp/8+cols*8);
837 data = malloc(datalen);
838 error = uncompress (data, &datalen, &tag->data[tag->pos], tag->len-tag->pos);
839 } while(error == Z_BUF_ERROR);
841 fprintf(stderr, "Zlib error %d (image %d)\n", error, id);
844 msg("<verbose> Uncompressed image is %d bytes (%d colormap)", datalen, (3+alpha)*cols);
846 datalen2 = datalen+16;
847 data2 = malloc(datalen2);
848 palette = (RGBA*)malloc(cols*sizeof(RGBA));
850 for(t=0;t<cols;t++) {
851 palette[t].r = data[pos++];
852 palette[t].g = data[pos++];
853 palette[t].b = data[pos++];
855 palette[t].a = data[pos++];
859 sprintf(name, "pic%d.png", id);
861 filename = destfilename;
862 if(!strcmp(filename,"output.swf"))
863 filename = "output.png";
865 fi = save_fopen(filename, "wb");
866 fwrite(head,sizeof(head),1,fi);
868 png_start_chunk(fi, "IHDR", 13);
869 png_write_dword(fi,width);
870 png_write_dword(fi,height);
871 png_write_byte(fi,8);
873 png_write_byte(fi,3); //indexed
874 else if(format == 5 && alpha==0)
875 png_write_byte(fi,2); //rgb
876 else if(format == 5 && alpha==1)
877 png_write_byte(fi,6); //rgba
880 png_write_byte(fi,0); //compression mode
881 png_write_byte(fi,0); //filter mode
882 png_write_byte(fi,0); //interlace mode
886 png_start_chunk(fi, "PLTE", 768);
889 png_write_byte(fi,palette[t].r);
890 png_write_byte(fi,palette[t].g);
891 png_write_byte(fi,palette[t].b);
896 /* write alpha palette */
897 png_start_chunk(fi, "tRNS", 256);
899 png_write_byte(fi,palette[t].a);
907 int srcwidth = width * (bpp/8);
908 datalen3 = (width*4+5)*height;
909 data3 = (U8*)malloc(datalen3);
910 for(y=0;y<height;y++)
912 data3[pos2++]=0; //filter type
915 // 32 bit to 24 bit "conversion"
916 for(x=0;x<width;x++) {
917 data3[pos2++]=data[pos+1];
918 data3[pos2++]=data[pos+2];
919 data3[pos2++]=data[pos+3];
920 pos+=4; //ignore padding byte
923 for(x=0;x<width;x++) {
924 data3[pos2++]=data[pos+1];
925 data3[pos2++]=data[pos+2];
926 data3[pos2++]=data[pos+3];
927 data3[pos2++]=data[pos+0]; //alpha
933 for(x=0;x<srcwidth;x++)
934 data3[pos2++]=data[pos++];
937 pos+=((srcwidth+3)&~3)-srcwidth; //align
942 if(compress (data2, &datalen2, data3, datalen3) != Z_OK) {
943 fprintf(stderr, "zlib error in pic %d\n", id);
946 msg("<verbose> Compressed data is %d bytes", datalen2);
947 png_start_chunk(fi, "IDAT", datalen2);
948 png_write_bytes(fi,data2,datalen2);
950 png_start_chunk(fi, "IEND", 0);
959 static FILE*mp3file=0;
960 void handlesoundstream(TAG*tag)
962 char*filename = "output.mp3";
964 filename = destfilename;
965 if(!strcmp(filename,"output.swf"))
966 filename = "output.mp3";
969 case ST_SOUNDSTREAMHEAD:
970 if((tag->data[1]&0x30) == 0x20) { //mp3 compression
971 mp3file = fopen(filename, "wb");
972 msg("<notice> Writing mp3 data to %s",filename);
975 msg("<error> Soundstream is not mp3");
977 case ST_SOUNDSTREAMHEAD2:
978 if((tag->data[1]&0x30) == 0x20) {//mp3 compression
979 mp3file = fopen(filename, "wb");
980 msg("<notice> Writing mp3 data to %s",filename);
983 msg("<error> Soundstream is not mp3 (2)");
985 case ST_SOUNDSTREAMBLOCK:
987 fwrite(&tag->data[4],tag->len-4,1,mp3file);
992 void handledefinesound(TAG*tag)
1002 int rate,bits,stereo;
1003 char*rates[] = {"5500","11025","22050","44100"};
1004 id = swf_GetU16(tag); //id
1006 flags = swf_GetU8(tag);
1008 rate = (flags>>2)&3;
1009 bits = flags&2?16:8;
1012 samples = swf_GetU32(tag);
1016 if(format == 2) { // mp3
1017 swf_GetU16(tag); //numsamples_seek
1019 } else if(format == 0) { // raw
1020 printf("Sound is RAW, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
1021 // TODO: convert to WAV
1023 } else if(format == 1) { // adpcm
1024 printf("Sound is ADPCM, format: %s samples/sec, %d bit, %s\n", rates[rate], bits, stereo?"stereo":"mono");
1025 extension = "adpcm";
1027 sprintf(buf, "sound%d.%s", id, extension);
1028 if(numextracts==1) {
1029 filename = destfilename;
1030 if(!strcmp(filename,"output.swf")) {
1031 sprintf(buf, "output.%s", extension);
1035 fi = save_fopen(filename, "wb");
1036 fwrite(&tag->data[tag->pos], tag->len - tag->pos, 1, fi);
1040 void handlebinary(TAG*tag) {
1043 char *filename = buf;
1044 int len = tag->memsize;
1045 int dx = 6; // offset to binary data
1046 if (tag->id!=ST_DEFINEBINARY) {
1047 fprintf(stderr, "Object %d is not a binary entity!\n",
1051 sprintf(buf, "binary%d.bin", GET16(tag->data));
1052 if(numextracts==1) {
1053 filename = destfilename;
1054 if(!strcmp(filename,"output.swf")) {
1055 sprintf(buf, "output.bin");
1059 fout = fopen(filename, "wb");
1060 fwrite(tag->data+dx,len-dx,1,fout);
1064 int main (int argc,char ** argv)
1073 char listavailable = 0;
1074 processargs(argc, argv);
1076 if(!extractframes && !extractids && ! extractname && !extractjpegids && !extractpngids
1077 && !extractmp3 && !extractsoundids && !extractfontids && !extractbinaryids)
1080 if(!originalplaceobjects && movetozero) {
1081 fprintf(stderr, "Error: -0 (--movetozero) can only be used in conjunction with -P (--placeobject)\n");
1087 fprintf(stderr, "You must supply a filename.\n");
1090 initLog(0,-1,0,0,-1, verbose);
1092 f = open(filename,O_RDONLY|O_BINARY);
1096 perror("Couldn't open file: ");
1099 if (swf_ReadSWF(f,&swf) < 0)
1101 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
1120 tagused = (char*)malloc(tagnum);
1121 memset(tagused, 0, tagnum);
1122 memset(used, 0, 65536);
1123 memset(depths, 0, 65536);
1128 if(swf_isAllowedSpriteTag(tag)) {
1130 if(extractframes && is_in_range(frame, extractframes)) {
1132 if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
1133 depths[swf_GetDepth(tag)] = 1;
1135 if(tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) {
1136 int depth = swf_GetDepth(tag);
1139 depths[swf_GetDepth(tag)] = 0;
1142 if((tag->id == ST_REMOVEOBJECT || tag->id == ST_REMOVEOBJECT2) &&
1143 (depths[swf_GetDepth(tag)]) && hollow) {
1145 depths[swf_GetDepth(tag)] = 0;
1149 enumerateIDs(tag, idcallback);
1151 tagused[tagnum] = 1;
1155 if(tag->id == ST_SOUNDSTREAMHEAD ||
1156 tag->id == ST_SOUNDSTREAMHEAD2 ||
1157 tag->id == ST_SOUNDSTREAMBLOCK) {
1159 handlesoundstream(tag);
1162 if(tag->id == ST_JPEGTABLES) {
1163 handlejpegtables(tag);
1166 if(swf_isDefiningTag(tag)) {
1167 int id = swf_GetDefineID(tag);
1169 if(extractids && is_in_range(id, extractids)) {
1173 if(extractfontids && is_in_range(id, extractfontids)) {
1174 handlefont(&swf, tag);
1176 if(extractjpegids && is_in_range(id, extractjpegids)) {
1179 if(extractsoundids && is_in_range(id, extractsoundids)) {
1180 handledefinesound(tag);
1182 if(extractbinaryids && is_in_range(id, extractbinaryids)) {
1185 #ifdef _ZLIB_INCLUDED_
1186 if(extractpngids && is_in_range(id, extractpngids)) {
1187 handlelossless(tag);
1191 else if (tag->id == ST_SETBACKGROUNDCOLOR) {
1192 mainr = tag->data[0];
1193 maing = tag->data[1];
1194 mainb = tag->data[2];
1196 else if(swf_isPlaceTag(tag) && tag->id != ST_PLACEOBJECT ) {
1197 char*name = swf_GetName(tag);
1198 if(name && extractname && !strcmp(name, extractname)) {
1199 int id = swf_GetPlaceID(tag);
1202 if(originalplaceobjects) {
1203 tagused[tagnum] = 1;
1205 depths[swf_GetDepth(tag)] = 1;
1206 extractname_id = id;
1209 else if(tag->id == ST_SHOWFRAME) {
1212 tagused[tagnum] = 1;
1217 if(tag->id == ST_DEFINESPRITE) {
1218 while(tag->id != ST_END) {
1227 extractTag(&swf, destfilename);
1233 msg("<error> Didn't find a soundstream in file");