Compress.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <string>
  19. #include <cstdio>
  20. #include "Lib/BaseType.h"
  21. #include "Compression/Compression.h"
  22. #include "../CRCDiff/Debug.h"
  23. #ifndef DEBUG
  24. #include <cstdarg>
  25. void ReleaseLog(const char *fmt, ...)
  26. {
  27. static char buffer[1024];
  28. va_list va;
  29. va_start( va, fmt );
  30. vsnprintf(buffer, 1024, fmt, va );
  31. buffer[1023] = 0;
  32. va_end( va );
  33. printf( "%s", buffer );
  34. }
  35. #undef DEBUG_LOG
  36. #define DEBUG_LOG(x) ReleaseLog x
  37. #endif
  38. void dumpHelp(const char *exe)
  39. {
  40. DEBUG_LOG(("Usage:\n To print the compression type of an existing file: %s -in infile\n", exe));
  41. DEBUG_LOG((" To compress a file: %s -in infile -out outfile <-type compressionmode>\n\n", exe));
  42. DEBUG_LOG(("Compression modes:\n"));
  43. for (int i=COMPRESSION_MIN; i<=COMPRESSION_MAX; ++i)
  44. {
  45. DEBUG_LOG((" %s\n", CompressionManager::getCompressionNameByType((CompressionType)i)));
  46. }
  47. }
  48. void main(int argc, char **argv)
  49. {
  50. std::string inFile = "";
  51. std::string outFile = "";
  52. CompressionType compressType = CompressionManager::getPreferredCompression();
  53. for (int i=1; i<argc; ++i)
  54. {
  55. if ( !stricmp(argv[i], "-help") )
  56. {
  57. dumpHelp(argv[0]);
  58. exit(0);
  59. }
  60. if ( !strcmp(argv[i], "-in") )
  61. {
  62. ++i;
  63. if (i<argc)
  64. {
  65. inFile = argv[i];
  66. }
  67. }
  68. if ( !strcmp(argv[i], "-out") )
  69. {
  70. ++i;
  71. if (i<argc)
  72. {
  73. outFile = argv[i];
  74. }
  75. }
  76. if ( !strcmp(argv[i], "-type") )
  77. {
  78. ++i;
  79. if (i<argc)
  80. {
  81. for (int j=COMPRESSION_MIN; j<=COMPRESSION_MAX; ++j)
  82. {
  83. if ( !stricmp(CompressionManager::getCompressionNameByType((CompressionType)j), argv[i]) )
  84. {
  85. compressType = (CompressionType)j;
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. if (inFile.empty())
  93. {
  94. dumpHelp(argv[0]);
  95. exit(0);
  96. }
  97. DEBUG_LOG(("IN:'%s' OUT:'%s' Compression:'%s'\n",
  98. inFile.c_str(), outFile.c_str(), CompressionManager::getCompressionNameByType(compressType)));
  99. if (outFile.empty())
  100. {
  101. // just check compression
  102. FILE *fp = fopen(inFile.c_str(), "rb");
  103. if (!fp)
  104. {
  105. DEBUG_LOG(("Cannot open '%s'\n", inFile.c_str()));
  106. return;
  107. }
  108. fseek(fp, 0, SEEK_END);
  109. int size = ftell(fp);
  110. fseek(fp, 0, SEEK_SET);
  111. char data[8];
  112. int numRead = fread(data, 1, 8, fp);
  113. if (numRead != 8)
  114. {
  115. DEBUG_LOG(("Cannot read header from '%s'\n", inFile.c_str()));
  116. return;
  117. }
  118. CompressionType usedType = CompressionManager::getCompressionType(data, 8);
  119. if (usedType == COMPRESSION_NONE)
  120. {
  121. DEBUG_LOG(("No compression on '%s'\n", inFile.c_str()));
  122. return;
  123. }
  124. int uncompressedSize = CompressionManager::getUncompressedSize(data, 8);
  125. DEBUG_LOG(("'%s' is compressed using %s, from %d to %d bytes, %g%% of its original size\n",
  126. inFile.c_str(), CompressionManager::getCompressionNameByType(usedType),
  127. uncompressedSize, size, size/(double)(uncompressedSize+0.1)*100.0));
  128. return;
  129. }
  130. // compress file
  131. }