cws2fws.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. if (fstat(fd_in, &statbuf) < 0) {
  57. perror("fstat failed");
  58. return 1;
  59. }
  60. comp_len = statbuf.st_size;
  61. uncomp_len = buf_in[4] | (buf_in[5] << 8) | (buf_in[6] << 16) | (buf_in[7] << 24);
  62. printf("Compressed size: %d Uncompressed size: %d\n",
  63. comp_len - 4, uncomp_len - 4);
  64. // write out modified header
  65. buf_in[0] = 'F';
  66. if (write(fd_out, &buf_in, 8) < 8) {
  67. perror("Error writing output file");
  68. return 1;
  69. }
  70. zstream.zalloc = NULL;
  71. zstream.zfree = NULL;
  72. zstream.opaque = NULL;
  73. if (inflateInit(&zstream) != Z_OK) {
  74. fprintf(stderr, "inflateInit failed\n");
  75. return 1;
  76. }
  77. for (i = 0; i < comp_len - 8;) {
  78. int ret, len = read(fd_in, &buf_in, 1024);
  79. dbgprintf("read %d bytes\n", len);
  80. last_out = zstream.total_out;
  81. zstream.next_in = &buf_in[0];
  82. zstream.avail_in = len;
  83. zstream.next_out = &buf_out[0];
  84. zstream.avail_out = 65536;
  85. ret = inflate(&zstream, Z_SYNC_FLUSH);
  86. if (ret != Z_STREAM_END && ret != Z_OK) {
  87. printf("Error while decompressing: %d\n", ret);
  88. inflateEnd(&zstream);
  89. return 1;
  90. }
  91. dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
  92. zstream.avail_in, zstream.total_in, zstream.avail_out,
  93. zstream.total_out, zstream.total_out - last_out);
  94. if (write(fd_out, &buf_out, zstream.total_out - last_out) <
  95. zstream.total_out - last_out) {
  96. perror("Error writing output file");
  97. return 1;
  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. return 1;
  114. }
  115. }
  116. inflateEnd(&zstream);
  117. close(fd_in);
  118. close(fd_out);
  119. return 0;
  120. }