Add my last name to copyright headers
[ffmpeg-lucabe.git] / cws2fws.c
blob1f9b1604f754678fc87be420257b3906b10c399f
1 /*
2 * cws2fws by Alex Beregszaszi
3 * Public domain.
5 * This utility converts compressed Macromedia Flash files to uncompressed ones.
6 */
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <zlib.h>
15 #ifdef DEBUG
16 #define dbgprintf printf
17 #else
18 #define dbgprintf
19 #endif
21 int main(int argc, char *argv[])
23 int fd_in, fd_out, comp_len, uncomp_len, i, last_out;
24 char buf_in[1024], buf_out[65536];
25 z_stream zstream;
26 struct stat statbuf;
28 if (argc < 3)
30 printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
31 exit(1);
34 fd_in = open(argv[1], O_RDONLY);
35 if (fd_in < 0)
37 perror("Error while opening: ");
38 exit(1);
41 fd_out = open(argv[2], O_WRONLY|O_CREAT, 00644);
42 if (fd_out < 0)
44 perror("Error while opening: ");
45 close(fd_in);
46 exit(1);
49 if (read(fd_in, &buf_in, 8) != 8)
51 printf("Header error\n");
52 close(fd_in);
53 close(fd_out);
54 exit(1);
57 if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S')
59 printf("Not a compressed flash file\n");
60 exit(1);
63 fstat(fd_in, &statbuf);
64 comp_len = statbuf.st_size;
65 uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
67 printf("Compressed size: %d Uncompressed size: %d\n", comp_len-4, uncomp_len-4);
69 // write out modified header
70 buf_in[0] = 'F';
71 write(fd_out, &buf_in, 8);
73 zstream.zalloc = NULL;
74 zstream.zfree = NULL;
75 zstream.opaque = NULL;
76 inflateInit(&zstream);
78 for (i = 0; i < comp_len-8;)
80 int ret, len = read(fd_in, &buf_in, 1024);
82 dbgprintf("read %d bytes\n", len);
84 last_out = zstream.total_out;
86 zstream.next_in = &buf_in[0];
87 zstream.avail_in = len;
88 zstream.next_out = &buf_out[0];
89 zstream.avail_out = 65536;
91 ret = inflate(&zstream, Z_SYNC_FLUSH);
92 if (ret != Z_STREAM_END && ret != Z_OK)
94 printf("Error while decompressing: %d\n", ret);
95 inflateEnd(&zstream);
96 exit(1);
99 dbgprintf("a_in: %d t_in: %d a_out: %d t_out: %d -- %d out\n",
100 zstream.avail_in, zstream.total_in, zstream.avail_out, zstream.total_out,
101 zstream.total_out-last_out);
103 write(fd_out, &buf_out, zstream.total_out-last_out);
105 i += len;
107 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
108 break;
111 if (zstream.total_out != uncomp_len-8)
113 printf("Size mismatch (%d != %d), updating header...\n",
114 zstream.total_out, uncomp_len-8);
116 buf_in[0] = (zstream.total_out+8) & 0xff;
117 buf_in[1] = (zstream.total_out+8 >> 8) & 0xff;
118 buf_in[2] = (zstream.total_out+8 >> 16) & 0xff;
119 buf_in[3] = (zstream.total_out+8 >> 24) & 0xff;
121 lseek(fd_out, 4, SEEK_SET);
122 write(fd_out, &buf_in, 4);
125 inflateEnd(&zstream);
126 close(fd_in);
127 close(fd_out);
128 return 0;