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