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