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.

130 lines
3.0KB

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