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 while opening: ");
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 while opening: ");
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 write(fd_out, &buf_in, 8);
00073
00074 zstream.zalloc = NULL;
00075 zstream.zfree = NULL;
00076 zstream.opaque = NULL;
00077 inflateInit(&zstream);
00078
00079 for (i = 0; i < comp_len-8;)
00080 {
00081 int ret, len = read(fd_in, &buf_in, 1024);
00082
00083 dbgprintf("read %d bytes\n", len);
00084
00085 last_out = zstream.total_out;
00086
00087 zstream.next_in = &buf_in[0];
00088 zstream.avail_in = len;
00089 zstream.next_out = &buf_out[0];
00090 zstream.avail_out = 65536;
00091
00092 ret = inflate(&zstream, Z_SYNC_FLUSH);
00093 if (ret != Z_STREAM_END && ret != Z_OK)
00094 {
00095 printf("Error while decompressing: %d\n", ret);
00096 inflateEnd(&zstream);
00097 exit(1);
00098 }
00099
00100 dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
00101 zstream.avail_in, zstream.total_in, zstream.avail_out, zstream.total_out,
00102 zstream.total_out-last_out);
00103
00104 write(fd_out, &buf_out, zstream.total_out-last_out);
00105
00106 i += len;
00107
00108 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
00109 break;
00110 }
00111
00112 if (zstream.total_out != uncomp_len-8)
00113 {
00114 printf("Size mismatch (%lu != %d), updating header...\n",
00115 zstream.total_out, uncomp_len-8);
00116
00117 buf_in[0] = (zstream.total_out+8) & 0xff;
00118 buf_in[1] = ((zstream.total_out+8) >> 8) & 0xff;
00119 buf_in[2] = ((zstream.total_out+8) >> 16) & 0xff;
00120 buf_in[3] = ((zstream.total_out+8) >> 24) & 0xff;
00121
00122 lseek(fd_out, 4, SEEK_SET);
00123 write(fd_out, &buf_in, 4);
00124 }
00125
00126 inflateEnd(&zstream);
00127 close(fd_in);
00128 close(fd_out);
00129 return 0;
00130 }