cws2fws.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (argc < 3) {
  32. printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
  33. return 1;
  34. }
  35. fd_in = open(argv[1], O_RDONLY);
  36. if (fd_in < 0) {
  37. perror("Error opening input file");
  38. return 1;
  39. }
  40. fd_out = open(argv[2], O_WRONLY | O_CREAT, 00644);
  41. if (fd_out < 0) {
  42. perror("Error opening output file");
  43. close(fd_in);
  44. return 1;
  45. }
  46. if (read(fd_in, &buf_in, 8) != 8) {
  47. printf("Header error\n");
  48. close(fd_in);
  49. close(fd_out);
  50. return 1;
  51. }
  52. if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S') {
  53. printf("Not a compressed flash file\n");
  54. return 1;
  55. }
  56. fstat(fd_in, &statbuf);
  57. comp_len = statbuf.st_size;
  58. uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
  59. printf("Compressed size: %d Uncompressed size: %d\n",
  60. comp_len - 4, uncomp_len - 4);
  61. // write out modified header
  62. buf_in[0] = 'F';
  63. if (write(fd_out, &buf_in, 8) < 8) {
  64. perror("Error writing output file");
  65. return 1;
  66. }
  67. zstream.zalloc = NULL;
  68. zstream.zfree = NULL;
  69. zstream.opaque = NULL;
  70. inflateInit(&zstream);
  71. for (i = 0; i < comp_len - 8;) {
  72. int ret, len = read(fd_in, &buf_in, 1024);
  73. dbgprintf("read %d bytes\n", len);
  74. last_out = zstream.total_out;
  75. zstream.next_in = &buf_in[0];
  76. zstream.avail_in = len;
  77. zstream.next_out = &buf_out[0];
  78. zstream.avail_out = 65536;
  79. ret = inflate(&zstream, Z_SYNC_FLUSH);
  80. if (ret != Z_STREAM_END && ret != Z_OK) {
  81. printf("Error while decompressing: %d\n", ret);
  82. inflateEnd(&zstream);
  83. return 1;
  84. }
  85. dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
  86. zstream.avail_in, zstream.total_in, zstream.avail_out,
  87. zstream.total_out, zstream.total_out - last_out);
  88. if (write(fd_out, &buf_out, zstream.total_out - last_out) <
  89. zstream.total_out - last_out) {
  90. perror("Error writing output file");
  91. return 1;
  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. return 1;
  108. }
  109. }
  110. inflateEnd(&zstream);
  111. close(fd_in);
  112. close(fd_out);
  113. return 0;
  114. }