You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.7KB

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