Statistics
| Branch: | Revision:

ffmpeg / cws2fws.c @ ed90d370

History | View | Annotate | Download (2.96 KB)

1 f7599d5f Alex Beregszaszi
/*
2
 * cws2fws by Alex Beregszaszi <alex@fsn.hu>
3
 *
4
 * This utility converts compressed Macromedia Flash files to uncompressed ones.
5
 *
6
 */
7
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11
#include <zlib.h>
12
13
#ifdef DEBUG
14
#define dbgprintf printf
15
#else
16
#define dbgprintf
17
#endif
18
19
main(int argc, char *argv[])
20
{
21
    int fd_in, fd_out, comp_len, uncomp_len, tag, i, last_out;
22 0c2ed2df Alex Beregszaszi
    char buf_in[1024], buf_out[65536];
23 f7599d5f Alex Beregszaszi
    z_stream zstream;
24
    struct stat statbuf;
25
26
    if (argc < 3)
27
    {
28 bb270c08 Diego Biurrun
        printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
29
        exit(1);
30 f7599d5f Alex Beregszaszi
    }
31 115329f1 Diego Biurrun
32 f7599d5f Alex Beregszaszi
    fd_in = open(argv[1], O_RDONLY);
33
    if (fd_in < 0)
34
    {
35 bb270c08 Diego Biurrun
        perror("Error while opening: ");
36
        exit(1);
37 f7599d5f Alex Beregszaszi
    }
38
39
    fd_out = open(argv[2], O_WRONLY|O_CREAT, 00644);
40
    if (fd_out < 0)
41
    {
42 bb270c08 Diego Biurrun
        perror("Error while opening: ");
43
        close(fd_in);
44
        exit(1);
45 f7599d5f Alex Beregszaszi
    }
46 115329f1 Diego Biurrun
47 f7599d5f Alex Beregszaszi
    if (read(fd_in, &buf_in, 8) != 8)
48
    {
49 bb270c08 Diego Biurrun
        printf("Header error\n");
50
        close(fd_in);
51
        close(fd_out);
52
        exit(1);
53 f7599d5f Alex Beregszaszi
    }
54 115329f1 Diego Biurrun
55 f7599d5f Alex Beregszaszi
    if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S')
56
    {
57 bb270c08 Diego Biurrun
        printf("Not a compressed flash file\n");
58
        exit(1);
59 f7599d5f Alex Beregszaszi
    }
60
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);
64 115329f1 Diego Biurrun
65 f7599d5f Alex Beregszaszi
    printf("Compressed size: %d Uncompressed size: %d\n", comp_len-4, uncomp_len-4);
66
67
    // write out modified header
68
    buf_in[0] = 'F';
69
    write(fd_out, &buf_in, 8);
70
71
    zstream.zalloc = NULL;
72
    zstream.zfree = NULL;
73
    zstream.opaque = NULL;
74
    inflateInit(&zstream);
75 115329f1 Diego Biurrun
76 0c2ed2df Alex Beregszaszi
    for (i = 0; i < comp_len-8;)
77 f7599d5f Alex Beregszaszi
    {
78 bb270c08 Diego Biurrun
        int ret, len = read(fd_in, &buf_in, 1024);
79 f7599d5f Alex Beregszaszi
80 bb270c08 Diego Biurrun
        dbgprintf("read %d bytes\n", len);
81 115329f1 Diego Biurrun
82 bb270c08 Diego Biurrun
        last_out = zstream.total_out;
83 115329f1 Diego Biurrun
84 bb270c08 Diego Biurrun
        zstream.next_in = &buf_in[0];
85
        zstream.avail_in = len;
86
        zstream.next_out = &buf_out[0];
87 0c2ed2df Alex Beregszaszi
        zstream.avail_out = 65536;
88 115329f1 Diego Biurrun
89 bb270c08 Diego Biurrun
        ret = inflate(&zstream, Z_SYNC_FLUSH);
90 0c2ed2df Alex Beregszaszi
        if (ret != Z_STREAM_END && ret != Z_OK)
91 bb270c08 Diego Biurrun
        {
92
            printf("Error while decompressing: %d\n", ret);
93
            inflateEnd(&zstream);
94
            exit(1);
95
        }
96 115329f1 Diego Biurrun
97 bb270c08 Diego Biurrun
        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);
100 115329f1 Diego Biurrun
101 bb270c08 Diego Biurrun
        write(fd_out, &buf_out, zstream.total_out-last_out);
102 f7599d5f Alex Beregszaszi
103 bb270c08 Diego Biurrun
        i += len;
104 0c2ed2df Alex Beregszaszi
105
        if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
106
            break;
107 f7599d5f Alex Beregszaszi
    }
108 115329f1 Diego Biurrun
109 f7599d5f Alex Beregszaszi
    if (zstream.total_out != uncomp_len-8)
110
    {
111 bb270c08 Diego Biurrun
        printf("Size mismatch (%d != %d), updating header...\n",
112
            zstream.total_out, uncomp_len-8);
113 f7599d5f Alex Beregszaszi
114 bb270c08 Diego Biurrun
        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;
118 115329f1 Diego Biurrun
119 bb270c08 Diego Biurrun
        lseek(fd_out, 4, SEEK_SET);
120
        write(fd_out, &buf_in, 4);
121 f7599d5f Alex Beregszaszi
    }
122 115329f1 Diego Biurrun
123 f7599d5f Alex Beregszaszi
    inflateEnd(&zstream);
124
    close(fd_in);
125
    close(fd_out);
126
}