Statistics
| Branch: | Revision:

ffmpeg / cws2fws.c @ 25e4f8aa

History | View | Annotate | Download (2.97 KB)

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