00001
00002
00003
00004
00005
00006
00007
00008 #include <sys/stat.h>
00009 #include <fcntl.h>
00010 #include <unistd.h>
00011 #include <zlib.h>
00012
00013 #ifdef DEBUG
00014 #define dbgprintf printf
00015 #else
00016 #define dbgprintf
00017 #endif
00018
00019 main(int argc, char *argv[])
00020 {
00021 int fd_in, fd_out, comp_len, uncomp_len, tag, i, last_out;
00022 char buf_in[1024], buf_out[1024];
00023 z_stream zstream;
00024 struct stat statbuf;
00025
00026 if (argc < 3)
00027 {
00028 printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
00029 exit(1);
00030 }
00031
00032 fd_in = open(argv[1], O_RDONLY);
00033 if (fd_in < 0)
00034 {
00035 perror("Error while opening: ");
00036 exit(1);
00037 }
00038
00039 fd_out = open(argv[2], O_WRONLY|O_CREAT, 00644);
00040 if (fd_out < 0)
00041 {
00042 perror("Error while opening: ");
00043 close(fd_in);
00044 exit(1);
00045 }
00046
00047 if (read(fd_in, &buf_in, 8) != 8)
00048 {
00049 printf("Header error\n");
00050 close(fd_in);
00051 close(fd_out);
00052 exit(1);
00053 }
00054
00055 if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S')
00056 {
00057 printf("Not a compressed flash file\n");
00058 exit(1);
00059 }
00060
00061 fstat(fd_in, &statbuf);
00062 comp_len = statbuf.st_size;
00063 uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
00064
00065 printf("Compressed size: %d Uncompressed size: %d\n", comp_len-4, uncomp_len-4);
00066
00067
00068 buf_in[0] = 'F';
00069 write(fd_out, &buf_in, 8);
00070
00071 zstream.zalloc = NULL;
00072 zstream.zfree = NULL;
00073 zstream.opaque = NULL;
00074 inflateInit(&zstream);
00075
00076 for (i = 0; i < comp_len-4;)
00077 {
00078 int ret, len = read(fd_in, &buf_in, 1024);
00079
00080 dbgprintf("read %d bytes\n", len);
00081
00082 last_out = zstream.total_out;
00083
00084 zstream.next_in = &buf_in[0];
00085 zstream.avail_in = len;
00086 zstream.next_out = &buf_out[0];
00087 zstream.avail_out = 1024;
00088
00089 ret = inflate(&zstream, Z_SYNC_FLUSH);
00090 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
00091 break;
00092 if (ret != Z_OK)
00093 {
00094 printf("Error while decompressing: %d\n", ret);
00095 inflateEnd(&zstream);
00096 exit(1);
00097 }
00098
00099 dbgprintf("a_in: %d t_in: %d a_out: %d t_out: %d -- %d out\n",
00100 zstream.avail_in, zstream.total_in, zstream.avail_out, zstream.total_out,
00101 zstream.total_out-last_out);
00102
00103 write(fd_out, &buf_out, zstream.total_out-last_out);
00104
00105 i += len;
00106 }
00107
00108 if (zstream.total_out != uncomp_len-8)
00109 {
00110 printf("Size mismatch (%d != %d), updating header...\n",
00111 zstream.total_out, uncomp_len-8);
00112
00113 buf_in[0] = (zstream.total_out+8) & 0xff;
00114 buf_in[1] = (zstream.total_out+8 >> 8) & 0xff;
00115 buf_in[2] = (zstream.total_out+8 >> 16) & 0xff;
00116 buf_in[3] = (zstream.total_out+8 >> 24) & 0xff;
00117
00118 lseek(fd_out, 4, SEEK_SET);
00119 write(fd_out, &buf_in, 4);
00120 }
00121
00122 inflateEnd(&zstream);
00123 close(fd_in);
00124 close(fd_out);
00125 }