Compressor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**
  2. * Copyright (c) 2006-2023 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Compressor.h"
  22. #include "common/config.h"
  23. #include "common/int.h"
  24. #include "libraries/lz4/lz4.h"
  25. #include "libraries/lz4/lz4hc.h"
  26. #include <zlib.h>
  27. namespace love
  28. {
  29. namespace data
  30. {
  31. class LZ4Compressor : public Compressor
  32. {
  33. public:
  34. char *compress(Format format, const char *data, size_t dataSize, int level, size_t &compressedSize) override
  35. {
  36. if (format != FORMAT_LZ4)
  37. throw love::Exception("Invalid format (expecting LZ4)");
  38. if (dataSize > LZ4_MAX_INPUT_SIZE)
  39. throw love::Exception("Data is too large for LZ4 compressor.");
  40. // We use a custom header to store some info with the compressed data.
  41. const size_t headersize = sizeof(uint32);
  42. int maxdestsize = LZ4_compressBound((int) dataSize);
  43. size_t maxsize = headersize + (size_t) maxdestsize;
  44. char *compressedbytes = nullptr;
  45. try
  46. {
  47. compressedbytes = new char[maxsize];
  48. }
  49. catch (std::bad_alloc &)
  50. {
  51. throw love::Exception("Out of memory.");
  52. }
  53. // Store the size of the uncompressed data as a header.
  54. #ifdef LOVE_BIG_ENDIAN
  55. // Make sure it's little-endian for storage.
  56. *(uint32 *) compressedbytes = swapuint32((uint32) dataSize);
  57. #else
  58. *(uint32 *) compressedbytes = (uint32) dataSize;
  59. #endif
  60. // Use LZ4-HC for compression level 9 and higher.
  61. int csize = 0;
  62. if (level > 8)
  63. csize = LZ4_compress_HC(data, compressedbytes + headersize, (int) dataSize, maxdestsize, LZ4HC_CLEVEL_DEFAULT);
  64. else
  65. csize = LZ4_compress_default(data, compressedbytes + headersize, (int) dataSize, maxdestsize);
  66. if (csize <= 0)
  67. {
  68. delete[] compressedbytes;
  69. throw love::Exception("Could not LZ4-compress data.");
  70. }
  71. // We allocated space for the maximum possible amount of data, but the
  72. // actual compressed size might be much smaller, so we should shrink the
  73. // data buffer if so.
  74. if ((double) maxsize / (double) (csize + headersize) >= 1.2)
  75. {
  76. char *cbytes = new (std::nothrow) char[csize + headersize];
  77. if (cbytes)
  78. {
  79. memcpy(cbytes, compressedbytes, csize + headersize);
  80. delete[] compressedbytes;
  81. compressedbytes = cbytes;
  82. }
  83. }
  84. compressedSize = (size_t) csize + headersize;
  85. return compressedbytes;
  86. }
  87. char *decompress(Format format, const char *data, size_t dataSize, size_t &decompressedSize) override
  88. {
  89. if (format != FORMAT_LZ4)
  90. throw love::Exception("Invalid format (expecting LZ4)");
  91. const size_t headersize = sizeof(uint32);
  92. char *rawbytes = nullptr;
  93. if (dataSize < headersize)
  94. throw love::Exception("Invalid LZ4-compressed data size.");
  95. // Extract the original uncompressed size (stored in our custom header.)
  96. #ifdef LOVE_BIG_ENDIAN
  97. // Convert from stored little-endian to big-endian.
  98. uint32 rawsize = swapuint32(*(uint32 *) data);
  99. #else
  100. uint32 rawsize = *(uint32 *) data;
  101. #endif
  102. try
  103. {
  104. rawbytes = new char[rawsize];
  105. }
  106. catch (std::bad_alloc &)
  107. {
  108. throw love::Exception("Out of memory.");
  109. }
  110. // If the uncompressed size is passed in as an argument (non-zero) and
  111. // it matches the header's stored size, then we assume it's 100% accurate
  112. // and we use a more efficient decompression function.
  113. if (decompressedSize > 0 && decompressedSize == (size_t) rawsize)
  114. {
  115. // We don't use the header here, but we need to account for its size.
  116. if (LZ4_decompress_fast(data + headersize, rawbytes, (int) decompressedSize) < 0)
  117. {
  118. delete[] rawbytes;
  119. throw love::Exception("Could not decompress LZ4-compressed data.");
  120. }
  121. }
  122. else
  123. {
  124. // Account for our custom header's size in the decompress arguments.
  125. int result = LZ4_decompress_safe(data + headersize, rawbytes,
  126. (int) (dataSize - headersize), rawsize);
  127. if (result < 0)
  128. {
  129. delete[] rawbytes;
  130. throw love::Exception("Could not decompress LZ4-compressed data.");
  131. }
  132. decompressedSize = (size_t) result;
  133. }
  134. return rawbytes;
  135. }
  136. bool isSupported(Format format) const override
  137. {
  138. return format == FORMAT_LZ4;
  139. }
  140. }; // LZ4Compressor
  141. class zlibCompressor : public Compressor
  142. {
  143. private:
  144. // The following three functions are mostly copied from the zlib source
  145. // (compressBound, compress2, and uncompress), but modified to support both
  146. // zlib and gzip.
  147. uLong zlibCompressBound(Format format, uLong sourceLen)
  148. {
  149. uLong size = sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + (sourceLen >> 25) + 13;
  150. // The gzip header is slightly larger than the zlib header.
  151. if (format == FORMAT_GZIP)
  152. size += 18 - 6;
  153. return size;
  154. }
  155. int zlibCompress(Format format, Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
  156. {
  157. z_stream stream = {};
  158. stream.next_in = (Bytef *) source;
  159. stream.avail_in = (uInt) sourceLen;
  160. stream.next_out = dest;
  161. stream.avail_out = (uInt) (*destLen);
  162. int windowbits = 15;
  163. if (format == FORMAT_GZIP)
  164. windowbits += 16; // This tells zlib to use a gzip header.
  165. else if (format == FORMAT_DEFLATE)
  166. windowbits = -windowbits;
  167. int err = deflateInit2(&stream, level, Z_DEFLATED, windowbits, 8, Z_DEFAULT_STRATEGY);
  168. if (err != Z_OK)
  169. return err;
  170. err = deflate(&stream, Z_FINISH);
  171. if (err != Z_STREAM_END)
  172. {
  173. deflateEnd(&stream);
  174. return err == Z_OK ? Z_BUF_ERROR : err;
  175. }
  176. *destLen = stream.total_out;
  177. return deflateEnd(&stream);
  178. }
  179. int zlibDecompress(Format format, Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
  180. {
  181. z_stream stream = {};
  182. stream.next_in = (Bytef *) source;
  183. stream.avail_in = (uInt) sourceLen;
  184. stream.next_out = dest;
  185. stream.avail_out = (uInt) (*destLen);
  186. // 15 is the default. Adding 32 makes zlib auto-detect the header type.
  187. int windowbits = 15 + 32;
  188. if (format == FORMAT_DEFLATE)
  189. windowbits = -15;
  190. int err = inflateInit2(&stream, windowbits);
  191. if (err != Z_OK)
  192. return err;
  193. err = inflate(&stream, Z_FINISH);
  194. if (err != Z_STREAM_END)
  195. {
  196. inflateEnd(&stream);
  197. if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
  198. return Z_DATA_ERROR;
  199. return err;
  200. }
  201. *destLen = stream.total_out;
  202. return inflateEnd(&stream);
  203. }
  204. public:
  205. char *compress(Format format, const char *data, size_t dataSize, int level, size_t &compressedSize) override
  206. {
  207. if (!isSupported(format))
  208. throw love::Exception("Invalid format (expecting zlib or gzip)");
  209. if (level < 0)
  210. level = Z_DEFAULT_COMPRESSION;
  211. else if (level > 9)
  212. level = 9;
  213. uLong maxsize = zlibCompressBound(format, (uLong) dataSize);
  214. char *compressedbytes = nullptr;
  215. try
  216. {
  217. compressedbytes = new char[maxsize];
  218. }
  219. catch (std::bad_alloc &)
  220. {
  221. throw love::Exception("Out of memory.");
  222. }
  223. uLongf destlen = maxsize;
  224. int status = zlibCompress(format, (Bytef *) compressedbytes, &destlen, (const Bytef *) data, (uLong) dataSize, level);
  225. if (status != Z_OK)
  226. {
  227. delete[] compressedbytes;
  228. throw love::Exception("Could not zlib/gzip-compress data.");
  229. }
  230. // We allocated space for the maximum possible amount of data, but the
  231. // actual compressed size might be much smaller, so we should shrink the
  232. // data buffer if so.
  233. if ((double) maxsize / (double) destlen >= 1.3)
  234. {
  235. char *cbytes = new (std::nothrow) char[destlen];
  236. if (cbytes)
  237. {
  238. memcpy(cbytes, compressedbytes, destlen);
  239. delete[] compressedbytes;
  240. compressedbytes = cbytes;
  241. }
  242. }
  243. compressedSize = (size_t) destlen;
  244. return compressedbytes;
  245. }
  246. char *decompress(Format format, const char *data, size_t dataSize, size_t &decompressedSize) override
  247. {
  248. if (!isSupported(format))
  249. throw love::Exception("Invalid format (expecting zlib or gzip)");
  250. char *rawbytes = nullptr;
  251. // We might know the output size before decompression. If not, we guess.
  252. size_t rawsize = decompressedSize > 0 ? decompressedSize : dataSize * 2;
  253. // Repeatedly try to decompress with an increasingly large output buffer.
  254. while (true)
  255. {
  256. try
  257. {
  258. rawbytes = new char[rawsize];
  259. }
  260. catch (std::bad_alloc &)
  261. {
  262. throw love::Exception("Out of memory.");
  263. }
  264. uLongf destLen = (uLongf) rawsize;
  265. int status = zlibDecompress(format, (Bytef *) rawbytes, &destLen, (const Bytef *) data, (uLong) dataSize);
  266. if (status == Z_OK)
  267. {
  268. decompressedSize = (size_t) destLen;
  269. break;
  270. }
  271. else if (status != Z_BUF_ERROR)
  272. {
  273. // For any error other than "not enough room", throw an exception.
  274. delete[] rawbytes;
  275. throw love::Exception("Could not decompress zlib/gzip-compressed data.");
  276. }
  277. // Not enough room in the output buffer: try again with a larger size.
  278. delete[] rawbytes;
  279. rawsize *= 2;
  280. }
  281. return rawbytes;
  282. }
  283. bool isSupported(Format format) const override
  284. {
  285. return format == FORMAT_ZLIB || format == FORMAT_GZIP || format == FORMAT_DEFLATE;
  286. }
  287. }; // zlibCompressor
  288. Compressor *Compressor::getCompressor(Format format)
  289. {
  290. static LZ4Compressor lz4compressor;
  291. static zlibCompressor zlibcompressor;
  292. Compressor *compressors[] = {&lz4compressor, &zlibcompressor};
  293. for (Compressor *c : compressors)
  294. {
  295. if (c->isSupported(format))
  296. return c;
  297. }
  298. return nullptr;
  299. }
  300. bool Compressor::getConstant(const char *in, Format &out)
  301. {
  302. return formatNames.find(in, out);
  303. }
  304. bool Compressor::getConstant(Format in, const char *&out)
  305. {
  306. return formatNames.find(in, out);
  307. }
  308. std::vector<std::string> Compressor::getConstants(Format)
  309. {
  310. return formatNames.getNames();
  311. }
  312. StringMap<Compressor::Format, Compressor::FORMAT_MAX_ENUM>::Entry Compressor::formatEntries[] =
  313. {
  314. { "lz4", FORMAT_LZ4 },
  315. { "zlib", FORMAT_ZLIB },
  316. { "gzip", FORMAT_GZIP },
  317. { "deflate", FORMAT_DEFLATE },
  318. };
  319. StringMap<Compressor::Format, Compressor::FORMAT_MAX_ENUM> Compressor::formatNames(Compressor::formatEntries, sizeof(Compressor::formatEntries));
  320. } // data
  321. } // love