cws2fws.c 3.9 KB

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