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 printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
00031 return 1;
00032 }
00033
00034 fd_in = open(argv[1], O_RDONLY);
00035 if (fd_in < 0) {
00036 perror("Error opening input file");
00037 return 1;
00038 }
00039
00040 fd_out = open(argv[2], O_WRONLY | O_CREAT, 00644);
00041 if (fd_out < 0) {
00042 perror("Error opening output file");
00043 close(fd_in);
00044 return 1;
00045 }
00046
00047 if (read(fd_in, &buf_in, 8) != 8) {
00048 printf("Header error\n");
00049 close(fd_in);
00050 close(fd_out);
00051 return 1;
00052 }
00053
00054 if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S') {
00055 printf("Not a compressed flash file\n");
00056 return 1;
00057 }
00058
00059 fstat(fd_in, &statbuf);
00060 comp_len = statbuf.st_size;
00061 uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
00062
00063 printf("Compressed size: %d Uncompressed size: %d\n",
00064 comp_len - 4, uncomp_len - 4);
00065
00066
00067 buf_in[0] = 'F';
00068 if (write(fd_out, &buf_in, 8) < 8) {
00069 perror("Error writing output file");
00070 return 1;
00071 }
00072
00073 zstream.zalloc = NULL;
00074 zstream.zfree = NULL;
00075 zstream.opaque = NULL;
00076 inflateInit(&zstream);
00077
00078 for (i = 0; i < comp_len - 8;) {
00079 int ret, len = read(fd_in, &buf_in, 1024);
00080
00081 dbgprintf("read %d bytes\n", len);
00082
00083 last_out = zstream.total_out;
00084
00085 zstream.next_in = &buf_in[0];
00086 zstream.avail_in = len;
00087 zstream.next_out = &buf_out[0];
00088 zstream.avail_out = 65536;
00089
00090 ret = inflate(&zstream, Z_SYNC_FLUSH);
00091 if (ret != Z_STREAM_END && ret != Z_OK) {
00092 printf("Error while decompressing: %d\n", ret);
00093 inflateEnd(&zstream);
00094 return 1;
00095 }
00096
00097 dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
00098 zstream.avail_in, zstream.total_in, zstream.avail_out,
00099 zstream.total_out, zstream.total_out - last_out);
00100
00101 if (write(fd_out, &buf_out, zstream.total_out - last_out) <
00102 zstream.total_out - last_out) {
00103 perror("Error writing output file");
00104 return 1;
00105 }
00106
00107 i += len;
00108
00109 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
00110 break;
00111 }
00112
00113 if (zstream.total_out != uncomp_len - 8) {
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 if (write(fd_out, &buf_in, 4) < 4) {
00124 perror("Error writing output file");
00125 return 1;
00126 }
00127 }
00128
00129 inflateEnd(&zstream);
00130 close(fd_in);
00131 close(fd_out);
00132 return 0;
00133 }