cws2fws.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <zlib.h>
  14. #ifdef DEBUG
  15. #define dbgprintf printf
  16. #else
  17. #define dbgprintf(...)
  18. #endif
  19. int main(int argc, char *argv[])
  20. {
  21. int fd_in, fd_out, comp_len, uncomp_len, i, last_out;
  22. char buf_in[1024], buf_out[65536];
  23. z_stream zstream;
  24. struct stat statbuf;
  25. if (argc < 3)
  26. {
  27. printf("Usage: %s <infile.swf> <outfile.swf>\n", argv[0]);
  28. exit(1);
  29. }
  30. fd_in = open(argv[1], O_RDONLY);
  31. if (fd_in < 0)
  32. {
  33. perror("Error while opening: ");
  34. exit(1);
  35. }
  36. fd_out = open(argv[2], O_WRONLY|O_CREAT, 00644);
  37. if (fd_out < 0)
  38. {
  39. perror("Error while opening: ");
  40. close(fd_in);
  41. exit(1);
  42. }
  43. if (read(fd_in, &buf_in, 8) != 8)
  44. {
  45. printf("Header error\n");
  46. close(fd_in);
  47. close(fd_out);
  48. exit(1);
  49. }
  50. if (buf_in[0] != 'C' || buf_in[1] != 'W' || buf_in[2] != 'S')
  51. {
  52. printf("Not a compressed flash file\n");
  53. exit(1);
  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", comp_len-4, uncomp_len-4);
  59. // write out modified header
  60. buf_in[0] = 'F';
  61. write(fd_out, &buf_in, 8);
  62. zstream.zalloc = NULL;
  63. zstream.zfree = NULL;
  64. zstream.opaque = NULL;
  65. inflateInit(&zstream);
  66. for (i = 0; i < comp_len-8;)
  67. {
  68. int ret, len = read(fd_in, &buf_in, 1024);
  69. dbgprintf("read %d bytes\n", len);
  70. last_out = zstream.total_out;
  71. zstream.next_in = &buf_in[0];
  72. zstream.avail_in = len;
  73. zstream.next_out = &buf_out[0];
  74. zstream.avail_out = 65536;
  75. ret = inflate(&zstream, Z_SYNC_FLUSH);
  76. if (ret != Z_STREAM_END && ret != Z_OK)
  77. {
  78. printf("Error while decompressing: %d\n", ret);
  79. inflateEnd(&zstream);
  80. exit(1);
  81. }
  82. dbgprintf("a_in: %d t_in: %lu a_out: %d t_out: %lu -- %lu out\n",
  83. zstream.avail_in, zstream.total_in, zstream.avail_out, zstream.total_out,
  84. zstream.total_out-last_out);
  85. write(fd_out, &buf_out, zstream.total_out-last_out);
  86. i += len;
  87. if (ret == Z_STREAM_END || ret == Z_BUF_ERROR)
  88. break;
  89. }
  90. if (zstream.total_out != uncomp_len-8)
  91. {
  92. printf("Size mismatch (%lu != %d), updating header...\n",
  93. zstream.total_out, uncomp_len-8);
  94. buf_in[0] = (zstream.total_out+8) & 0xff;
  95. buf_in[1] = ((zstream.total_out+8) >> 8) & 0xff;
  96. buf_in[2] = ((zstream.total_out+8) >> 16) & 0xff;
  97. buf_in[3] = ((zstream.total_out+8) >> 24) & 0xff;
  98. lseek(fd_out, 4, SEEK_SET);
  99. write(fd_out, &buf_in, 4);
  100. }
  101. inflateEnd(&zstream);
  102. close(fd_in);
  103. close(fd_out);
  104. return 0;
  105. }