tools: K&R reformatting cosmetics
[libav.git] / tools / cws2fws.c
CommitLineData
f7599d5f 1/*
a7b91ac6 2 * cws2fws by Alex Beregszaszi
3e2503ba
DB
3 * This file is placed in the public domain.
4 * Use the program however you see fit.
f7599d5f
AB
5 *
6 * This utility converts compressed Macromedia Flash files to uncompressed ones.
f7599d5f
AB
7 */
8
9#include <sys/stat.h>
10#include <fcntl.h>
a65bafef
DB
11#include <stdio.h>
12#include <stdlib.h>
f7599d5f
AB
13#include <unistd.h>
14#include <zlib.h>
15
16#ifdef DEBUG
17#define dbgprintf printf
18#else
1f48345a 19#define dbgprintf(...)
f7599d5f
AB
20#endif
21
226cbe07 22int main(int argc, char *argv[])
f7599d5f 23{
15bf461b 24 int fd_in, fd_out, comp_len, uncomp_len, i, last_out;
0c2ed2df 25 char buf_in[1024], buf_out[65536];
f7599d5f
AB
26 z_stream zstream;
27 struct stat statbuf;
28
4e81b5f5 29 if (argc < 3) {
bb270c08 30 printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
9a5d6c23 31 return 1;
f7599d5f 32 }
115329f1 33
f7599d5f 34 fd_in = open(argv[1], O_RDONLY);
4e81b5f5 35 if (fd_in < 0) {
c540061f 36 perror("Error opening input file");
9a5d6c23 37 return 1;
f7599d5f
AB
38 }
39
4e81b5f5
DB
40 fd_out = open(argv[2], O_WRONLY | O_CREAT, 00644);
41 if (fd_out < 0) {
c540061f 42 perror("Error opening output file");
bb270c08 43 close(fd_in);
9a5d6c23 44 return 1;
f7599d5f 45 }
115329f1 46
4e81b5f5 47 if (read(fd_in, &buf_in, 8) != 8) {
bb270c08
DB
48 printf("Header error\n");
49 close(fd_in);
50 close(fd_out);
9a5d6c23 51 return 1;
f7599d5f 52 }
115329f1 53
4e81b5f5 54 if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S') {
bb270c08 55 printf("Not a compressed flash file\n");
9a5d6c23 56 return 1;
f7599d5f
AB
57 }
58
59 fstat(fd_in, &statbuf);
4e81b5f5 60 comp_len = statbuf.st_size;
f7599d5f 61 uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
115329f1 62
4e81b5f5
DB
63 printf("Compressed size: %d Uncompressed size: %d\n",
64 comp_len - 4, uncomp_len - 4);
f7599d5f
AB
65
66 // write out modified header
67 buf_in[0] = 'F';
d39facc7
DB
68 if (write(fd_out, &buf_in, 8) < 8) {
69 perror("Error writing output file");
9a5d6c23 70 return 1;
d39facc7 71 }
f7599d5f
AB
72
73 zstream.zalloc = NULL;
4e81b5f5 74 zstream.zfree = NULL;
f7599d5f
AB
75 zstream.opaque = NULL;
76 inflateInit(&zstream);
115329f1 77
4e81b5f5 78 for (i = 0; i < comp_len - 8;) {
bb270c08 79 int ret, len = read(fd_in, &buf_in, 1024);
f7599d5f 80
bb270c08 81 dbgprintf("read %d bytes\n", len);
115329f1 82
bb270c08 83 last_out = zstream.total_out;
115329f1 84
4e81b5f5
DB
85 zstream.next_in = &buf_in[0];
86 zstream.avail_in = len;
87 zstream.next_out = &buf_out[0];
0c2ed2df 88 zstream.avail_out = 65536;
115329f1 89
bb270c08 90 ret = inflate(&zstream, Z_SYNC_FLUSH);
4e81b5f5 91 if (ret != Z_STREAM_END && ret != Z_OK) {
bb270c08
DB
92 printf("Error while decompressing: %d\n", ret);
93 inflateEnd(&zstream);
9a5d6c23 94 return 1;
bb270c08 95 }
115329f1 96
1f48345a 97 dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
4e81b5f5
DB
98 zstream.avail_in, zstream.total_in, zstream.avail_out,
99 zstream.total_out, zstream.total_out - last_out);
115329f1 100
4e81b5f5
DB
101 if (write(fd_out, &buf_out, zstream.total_out - last_out) <
102 zstream.total_out - last_out) {
d39facc7 103 perror("Error writing output file");
9a5d6c23 104 return 1;
d39facc7 105 }
f7599d5f 106
bb270c08 107 i += len;
0c2ed2df
AB
108
109 if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
110 break;
f7599d5f 111 }
115329f1 112
4e81b5f5 113 if (zstream.total_out != uncomp_len - 8) {
1f48345a 114 printf("Size mismatch (%lu != %d), updating header...\n",
4e81b5f5 115 zstream.total_out, uncomp_len - 8);
f7599d5f 116
4e81b5f5
DB
117 buf_in[0] = (zstream.total_out + 8) & 0xff;
118 buf_in[1] = ((zstream.total_out + 8) >> 8) & 0xff;
119 buf_in[2] = ((zstream.total_out + 8) >> 16) & 0xff;
120 buf_in[3] = ((zstream.total_out + 8) >> 24) & 0xff;
115329f1 121
bb270c08 122 lseek(fd_out, 4, SEEK_SET);
d39facc7
DB
123 if (write(fd_out, &buf_in, 4) < 4) {
124 perror("Error writing output file");
9a5d6c23 125 return 1;
d39facc7 126 }
f7599d5f 127 }
115329f1 128
f7599d5f
AB
129 inflateEnd(&zstream);
130 close(fd_in);
131 close(fd_out);
226cbe07 132 return 0;
f7599d5f 133}