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