2 * cws2fws by Alex Beregszaszi <alex@fsn.hu>
4 * This utility converts compressed Macromedia Flash files to uncompressed ones.
14 #define dbgprintf printf
19 main(int argc
, char *argv
[])
21 int fd_in
, fd_out
, comp_len
, uncomp_len
, tag
, i
, last_out
;
22 char buf_in
[1024], buf_out
[65536];
28 printf("Usage: %s <infile.swf> <outfile.swf>\n", argv
[0]);
32 fd_in
= open(argv
[1], O_RDONLY
);
35 perror("Error while opening: ");
39 fd_out
= open(argv
[2], O_WRONLY
|O_CREAT
, 00644);
42 perror("Error while opening: ");
47 if (read(fd_in
, &buf_in
, 8) != 8)
49 printf("Header error\n");
55 if (buf_in
[0] != 'C' || buf_in
[1] != 'W' || buf_in
[2] != 'S')
57 printf("Not a compressed flash file\n");
61 fstat(fd_in
, &statbuf
);
62 comp_len
= statbuf
.st_size
;
63 uncomp_len
= buf_in
[4] | (buf_in
[5] << 8) | (buf_in
[6] << 16) | (buf_in
[7] << 24);
65 printf("Compressed size: %d Uncompressed size: %d\n", comp_len
-4, uncomp_len
-4);
67 // write out modified header
69 write(fd_out
, &buf_in
, 8);
71 zstream
.zalloc
= NULL
;
73 zstream
.opaque
= NULL
;
74 inflateInit(&zstream
);
76 for (i
= 0; i
< comp_len
-8;)
78 int ret
, len
= read(fd_in
, &buf_in
, 1024);
80 dbgprintf("read %d bytes\n", len
);
82 last_out
= zstream
.total_out
;
84 zstream
.next_in
= &buf_in
[0];
85 zstream
.avail_in
= len
;
86 zstream
.next_out
= &buf_out
[0];
87 zstream
.avail_out
= 65536;
89 ret
= inflate(&zstream
, Z_SYNC_FLUSH
);
90 if (ret
!= Z_STREAM_END
&& ret
!= Z_OK
)
92 printf("Error while decompressing: %d\n", ret
);
97 dbgprintf("a_in: %d t_in: %d a_out: %d t_out: %d -- %d out\n",
98 zstream
.avail_in
, zstream
.total_in
, zstream
.avail_out
, zstream
.total_out
,
99 zstream
.total_out
-last_out
);
101 write(fd_out
, &buf_out
, zstream
.total_out
-last_out
);
105 if (ret
== Z_STREAM_END
|| ret
== Z_BUF_ERROR
)
109 if (zstream
.total_out
!= uncomp_len
-8)
111 printf("Size mismatch (%d != %d), updating header...\n",
112 zstream
.total_out
, uncomp_len
-8);
114 buf_in
[0] = (zstream
.total_out
+8) & 0xff;
115 buf_in
[1] = (zstream
.total_out
+8 >> 8) & 0xff;
116 buf_in
[2] = (zstream
.total_out
+8 >> 16) & 0xff;
117 buf_in
[3] = (zstream
.total_out
+8 >> 24) & 0xff;
119 lseek(fd_out
, 4, SEEK_SET
);
120 write(fd_out
, &buf_in
, 4);
123 inflateEnd(&zstream
);