cws2fws.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(...)
  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. fstat(fd_in, &statbuf);
  56. comp_len = statbuf.st_size;
  57. uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
  58. printf("Compressed size: %d Uncompressed size: %d\n",
  59. comp_len - 4, uncomp_len - 4);
  60. // write out modified header
  61. buf_in[0] = 'F';
  62. if (write(fd_out, &buf_in, 8) < 8) {
  63. perror("Error writing output file");
  64. goto out;
  65. }
  66. zstream.zalloc = NULL;
  67. zstream.zfree = NULL;
  68. zstream.opaque = NULL;
  69. inflateInit(&zstream);
  70. for (i = 0; i < comp_len - 8;) {
  71. int ret, len = read(fd_in, &buf_in, 1024);
  72. dbgprintf("read %d bytes\n", len);
  73. last_out = zstream.total_out;
  74. zstream.next_in = &buf_in[0];
  75. zstream.avail_in = len;
  76. zstream.next_out = &buf_out[0];
  77. zstream.avail_out = 65536;
  78. ret = inflate(&zstream, Z_SYNC_FLUSH);
  79. if (ret != Z_STREAM_END && ret != Z_OK) {
  80. printf("Error while decompressing: %d\n", ret);
  81. inflateEnd(&zstream);
  82. goto out;
  83. }
  84. dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
  85. zstream.avail_in, zstream.total_in, zstream.avail_out,
  86. zstream.total_out, zstream.total_out - last_out);
  87. if (write(fd_out, &buf_out, zstream.total_out - last_out) <
  88. zstream.total_out - last_out) {
  89. perror("Error writing output file");
  90. inflateEnd(&zstream);
  91. goto out;
  92. }
  93. i += len;
  94. if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
  95. break;
  96. }
  97. if (zstream.total_out != uncomp_len - 8) {
  98. printf("Size mismatch (%lu != %d), updating header...\n",
  99. zstream.total_out, uncomp_len - 8);
  100. buf_in[0] = (zstream.total_out + 8) & 0xff;
  101. buf_in[1] = ((zstream.total_out + 8) >> 8) & 0xff;
  102. buf_in[2] = ((zstream.total_out + 8) >> 16) & 0xff;
  103. buf_in[3] = ((zstream.total_out + 8) >> 24) & 0xff;
  104. lseek(fd_out, 4, SEEK_SET);
  105. if (write(fd_out, &buf_in, 4) < 4) {
  106. perror("Error writing output file");
  107. inflateEnd(&zstream);
  108. goto out;
  109. }
  110. }
  111. ret = 0;
  112. inflateEnd(&zstream);
  113. out:
  114. close(fd_in);
  115. close(fd_out);
  116. return ret;
  117. }