cws2fws.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. dbgprintf("read %d bytes\n", len);
  79. last_out = zstream.total_out;
  80. zstream.next_in = &buf_in[0];
  81. zstream.avail_in = len;
  82. zstream.next_out = &buf_out[0];
  83. zstream.avail_out = 65536;
  84. ret = inflate(&zstream, Z_SYNC_FLUSH);
  85. if (ret != Z_STREAM_END && ret != Z_OK) {
  86. printf("Error while decompressing: %d\n", ret);
  87. inflateEnd(&zstream);
  88. goto out;
  89. }
  90. dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
  91. zstream.avail_in, zstream.total_in, zstream.avail_out,
  92. zstream.total_out, zstream.total_out - last_out);
  93. if (write(fd_out, &buf_out, zstream.total_out - last_out) <
  94. zstream.total_out - last_out) {
  95. perror("Error writing output file");
  96. inflateEnd(&zstream);
  97. goto out;
  98. }
  99. i += len;
  100. if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
  101. break;
  102. }
  103. if (zstream.total_out != uncomp_len - 8) {
  104. printf("Size mismatch (%lu != %d), updating header...\n",
  105. zstream.total_out, uncomp_len - 8);
  106. buf_in[0] = (zstream.total_out + 8) & 0xff;
  107. buf_in[1] = ((zstream.total_out + 8) >> 8) & 0xff;
  108. buf_in[2] = ((zstream.total_out + 8) >> 16) & 0xff;
  109. buf_in[3] = ((zstream.total_out + 8) >> 24) & 0xff;
  110. if ( lseek(fd_out, 4, SEEK_SET) < 0
  111. || write(fd_out, &buf_in, 4) < 4) {
  112. perror("Error writing output file");
  113. inflateEnd(&zstream);
  114. goto out;
  115. }
  116. }
  117. ret = 0;
  118. inflateEnd(&zstream);
  119. out:
  120. close(fd_in);
  121. close(fd_out);
  122. return ret;
  123. }