+ if(frame)
+ printf(" [-f] %d Frames: ID(s) 0-%d\n", frame, frame);
+ else
+ printf(" [-f] 1 Frame: ID(s) 0\n");
+
+ if(mp3)
+ printf(" [-m] 1 MP3 Soundstream\n");
+}
+
+int handlefont(SWF*swf, TAG*tag)
+{
+ SWFFONT* f=0;
+ U16 id;
+ char name[80];
+ char*filename = name;
+ int t;
+
+ id = swf_GetDefineID(tag);
+ prepare_name(name, sizeof(name), "font", "swf", id);
+ if(numextracts==1) {
+ filename = destfilename;
+ }
+
+ swf_FontExtract(swf, id, &f);
+ if(!f) {
+ if (!extractanyids) {
+ printf("Couldn't extract font %d\n", id);
+ }
+ return 0;
+ }
+
+ swf_WriteFont(f, filename);
+ swf_FontFree(f);
+ return 1;
+}
+
+static char has_jpegtables=0;
+static U8*jpegtables = 0;
+static int jpegtablessize = 0;
+
+void handlejpegtables(TAG*tag)
+{
+ if(tag->id == ST_JPEGTABLES) {
+ jpegtables = tag->data;
+ jpegtablessize = tag->len;
+ has_jpegtables = 1;
+ }
+}
+
+FILE* save_fopen(char* name, char* mode)
+{
+ FILE*fi = fopen(name, mode);
+ if(!fi) {
+ fprintf(stderr, "Error: Couldn't open %s\n", name);
+ exit(1);
+ }
+ return fi;
+}
+
+int findjpegboundary(U8*data, int len)
+{
+ int t;
+ int pos=-1;
+ for(t=0;t<len-4;t++) {
+ if(data[t ]==0xff &&
+ data[t+1]==0xd9 &&
+ data[t+2]==0xff &&
+ data[t+3]==0xd8) {
+ pos = t;
+ }
+ }
+ return pos;
+}
+
+/* extract jpeg data out of a tag */
+int handlejpeg(TAG*tag)
+{
+ char name[80];
+ char*filename = name;
+ FILE*fi;
+
+ prepare_name(name, sizeof(name), "pic", "jpg", GET16(tag->data));
+ if(numextracts==1) {
+ filename = destfilename;
+ if(!strcmp(filename,"output.swf"))
+ filename = "output.jpg";
+ }
+ /* swf jpeg images have two streams, which both start with ff d8 and
+ end with ff d9. The following code handles sorting the middle
+ <ff d9 ff d8> bytes out, so that one stream remains */
+ if(tag->id == ST_DEFINEBITSJPEG && tag->len>2 && has_jpegtables) {
+ fi = save_fopen(filename, "wb");
+ if(jpegtablessize>=2) {
+ fwrite(jpegtables, 1, jpegtablessize-2, fi); //don't write end tag (ff,d8)
+ fwrite(&tag->data[2+2], tag->len-2-2, 1, fi); //don't write start tag (ff,d9)
+ } else {
+ fwrite(tag->data+2, tag->len-2, 1, fi);
+ }
+ fclose(fi);
+ }
+ else if(tag->id == ST_DEFINEBITSJPEG2 && tag->len>2) {
+ int end = tag->len;
+ int pos = findjpegboundary(&tag->data[2], tag->len-2);
+ if(pos>=0) {
+ pos+=2;
+ fi = save_fopen(filename, "wb");
+ fwrite(&tag->data[2], pos-2, 1, fi);
+ fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
+ fclose(fi);
+ } else {
+ fi = save_fopen(filename, "wb");
+ fwrite(&tag->data[2], end-2, 1, fi);
+ fclose(fi);
+ }
+ }
+ else if(tag->id == ST_DEFINEBITSJPEG3 && tag->len>6) {
+ U32 end = GET32(&tag->data[2])+6;
+ int pos = findjpegboundary(&tag->data[6], tag->len-6);
+ if(pos<0) {
+ fi = save_fopen(filename, "wb");
+ fwrite(&tag->data[6], end-6, 1, fi);
+ fclose(fi);
+ } else {
+ pos+=6;
+ fi = save_fopen(filename, "wb");
+ fwrite(&tag->data[6], pos-6, 1, fi);
+ fwrite(&tag->data[pos+4], end-(pos+4), 1, fi);
+ fclose(fi);
+ }
+ }
+ else {
+ int id = GET16(tag->data);
+ if (!extractanyids) {
+ fprintf(stderr, "Object %d is not a JPEG picture!\n", id);
+ exit(1);
+ }
+ return 0;
+ }
+ return 1;
+}
+
+#ifdef _ZLIB_INCLUDED_
+static U32 mycrc32;
+
+static U32*crc32_table = 0;
+static void make_crc32_table(void)
+{
+ int t;
+ if(crc32_table)
+ return;
+ crc32_table = (U32*)malloc(1024);
+
+ for (t = 0; t < 256; t++) {
+ U32 c = t;
+ int s;
+ for (s = 0; s < 8; s++) {
+ c = (0xedb88320L*(c&1)) ^ (c >> 1);
+ }
+ crc32_table[t] = c;
+ }
+}
+static inline void png_write_byte(FILE*fi, U8 byte)
+{
+ fwrite(&byte,1,1,fi);
+ mycrc32 = crc32_table[(mycrc32 ^ byte) & 0xff] ^ (mycrc32 >> 8);
+}
+static void png_start_chunk(FILE*fi, char*type, int len)
+{
+ U8 mytype[4]={0,0,0,0};
+ U32 mylen = BE_32_TO_NATIVE(len);
+ memcpy(mytype,type,strlen(type));
+ fwrite(&mylen, 4, 1, fi);
+ mycrc32=0xffffffff;
+ png_write_byte(fi,mytype[0]);
+ png_write_byte(fi,mytype[1]);
+ png_write_byte(fi,mytype[2]);
+ png_write_byte(fi,mytype[3]);
+}
+static void png_write_bytes(FILE*fi, U8*bytes, int len)
+{
+ int t;
+ for(t=0;t<len;t++)
+ png_write_byte(fi,bytes[t]);
+}
+static void png_write_dword(FILE*fi, U32 dword)
+{
+ png_write_byte(fi,dword>>24);
+ png_write_byte(fi,dword>>16);
+ png_write_byte(fi,dword>>8);
+ png_write_byte(fi,dword);
+}
+static void png_end_chunk(FILE*fi)
+{
+ U32 tmp = BE_32_TO_NATIVE((mycrc32^0xffffffff));
+ fwrite(&tmp,4,1,fi);
+}