tools/cws2fws.c
f7599d5f
 /*
a7b91ac6
  * cws2fws by Alex Beregszaszi
3e2503ba
  * This file is placed in the public domain.
  * Use the program however you see fit.
f7599d5f
  *
  * This utility converts compressed Macromedia Flash files to uncompressed ones.
  */
 
212ec5fa
 #include "config.h"
f7599d5f
 #include <sys/stat.h>
 #include <fcntl.h>
a65bafef
 #include <stdio.h>
 #include <stdlib.h>
212ec5fa
 #if HAVE_UNISTD_H
f7599d5f
 #include <unistd.h>
212ec5fa
 #endif
 #if HAVE_IO_H
 #include <io.h>
 #endif
f7599d5f
 #include <zlib.h>
 
 #ifdef DEBUG
 #define dbgprintf printf
 #else
1f48345a
 #define dbgprintf(...)
f7599d5f
 #endif
 
226cbe07
 int main(int argc, char *argv[])
f7599d5f
 {
15bf461b
     int fd_in, fd_out, comp_len, uncomp_len, i, last_out;
0c2ed2df
     char buf_in[1024], buf_out[65536];
f7599d5f
     z_stream zstream;
     struct stat statbuf;
 
4e81b5f5
     if (argc < 3) {
bb270c08
         printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
9a5d6c23
         return 1;
f7599d5f
     }
115329f1
 
f7599d5f
     fd_in = open(argv[1], O_RDONLY);
4e81b5f5
     if (fd_in < 0) {
c540061f
         perror("Error opening input file");
9a5d6c23
         return 1;
f7599d5f
     }
 
4e81b5f5
     fd_out = open(argv[2], O_WRONLY | O_CREAT, 00644);
     if (fd_out < 0) {
c540061f
         perror("Error opening output file");
bb270c08
         close(fd_in);
9a5d6c23
         return 1;
f7599d5f
     }
115329f1
 
4e81b5f5
     if (read(fd_in, &buf_in, 8) != 8) {
bb270c08
         printf("Header error\n");
         close(fd_in);
         close(fd_out);
9a5d6c23
         return 1;
f7599d5f
     }
115329f1
 
4e81b5f5
     if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S') {
bb270c08
         printf("Not a compressed flash file\n");
9a5d6c23
         return 1;
f7599d5f
     }
 
5b45b662
     if (fstat(fd_in, &statbuf) < 0) {
         perror("fstat failed");
         return 1;
     }
4e81b5f5
     comp_len   = statbuf.st_size;
f7599d5f
     uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
115329f1
 
4e81b5f5
     printf("Compressed size: %d Uncompressed size: %d\n",
            comp_len - 4, uncomp_len - 4);
f7599d5f
 
     // write out modified header
     buf_in[0] = 'F';
d39facc7
     if (write(fd_out, &buf_in, 8) < 8) {
         perror("Error writing output file");
9a5d6c23
         return 1;
d39facc7
     }
f7599d5f
 
     zstream.zalloc = NULL;
4e81b5f5
     zstream.zfree  = NULL;
f7599d5f
     zstream.opaque = NULL;
59eae884
     if (inflateInit(&zstream) != Z_OK) {
         fprintf(stderr, "inflateInit failed\n");
         return 1;
     }
115329f1
 
4e81b5f5
     for (i = 0; i < comp_len - 8;) {
bb270c08
         int ret, len = read(fd_in, &buf_in, 1024);
f7599d5f
 
bb270c08
         dbgprintf("read %d bytes\n", len);
115329f1
 
bb270c08
         last_out = zstream.total_out;
115329f1
 
4e81b5f5
         zstream.next_in   = &buf_in[0];
         zstream.avail_in  = len;
         zstream.next_out  = &buf_out[0];
0c2ed2df
         zstream.avail_out = 65536;
115329f1
 
bb270c08
         ret = inflate(&zstream, Z_SYNC_FLUSH);
4e81b5f5
         if (ret != Z_STREAM_END && ret != Z_OK) {
bb270c08
             printf("Error while decompressing: %d\n", ret);
             inflateEnd(&zstream);
9a5d6c23
             return 1;
bb270c08
         }
115329f1
 
1f48345a
         dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
4e81b5f5
                   zstream.avail_in, zstream.total_in, zstream.avail_out,
                   zstream.total_out, zstream.total_out - last_out);
115329f1
 
4e81b5f5
         if (write(fd_out, &buf_out, zstream.total_out - last_out) <
             zstream.total_out - last_out) {
d39facc7
             perror("Error writing output file");
9a5d6c23
             return 1;
d39facc7
         }
f7599d5f
 
bb270c08
         i += len;
0c2ed2df
 
         if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
             break;
f7599d5f
     }
115329f1
 
4e81b5f5
     if (zstream.total_out != uncomp_len - 8) {
1f48345a
         printf("Size mismatch (%lu != %d), updating header...\n",
4e81b5f5
                zstream.total_out, uncomp_len - 8);
f7599d5f
 
4e81b5f5
         buf_in[0] =  (zstream.total_out + 8)        & 0xff;
         buf_in[1] = ((zstream.total_out + 8) >>  8) & 0xff;
         buf_in[2] = ((zstream.total_out + 8) >> 16) & 0xff;
         buf_in[3] = ((zstream.total_out + 8) >> 24) & 0xff;
115329f1
 
86aba86b
         if (   lseek(fd_out, 4, SEEK_SET) < 0
             || write(fd_out, &buf_in, 4) < 4) {
d39facc7
             perror("Error writing output file");
9a5d6c23
             return 1;
d39facc7
         }
f7599d5f
     }
115329f1
 
f7599d5f
     inflateEnd(&zstream);
     close(fd_in);
     close(fd_out);
226cbe07
     return 0;
f7599d5f
 }