cws2fws.c 3.0 KB

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