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.

291 lines
11 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. /*
  4. COPYING CONDITIONS NOTICE:
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License as
  7. published by the Free Software Foundation, and provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain this COPYING
  10. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  11. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  12. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  13. GRANT (below).
  14. * Redistributions in binary form must reproduce this COPYING
  15. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  16. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  17. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  18. GRANT (below) in the documentation and/or other materials
  19. provided with the distribution.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. 02110-1301, USA.
  24. COPYRIGHT NOTICE:
  25. TokuDB, Tokutek Fractal Tree Indexing Library.
  26. Copyright (C) 2007-2013 Tokutek, Inc.
  27. DISCLAIMER:
  28. This program is distributed in the hope that it will be useful, but
  29. WITHOUT ANY WARRANTY; without even the implied warranty of
  30. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. General Public License for more details.
  32. UNIVERSITY PATENT NOTICE:
  33. The technology is licensed by the Massachusetts Institute of
  34. Technology, Rutgers State University of New Jersey, and the Research
  35. Foundation of State University of New York at Stony Brook under
  36. United States of America Serial No. 11/760379 and to the patents
  37. and/or patent applications resulting from it.
  38. PATENT MARKING NOTICE:
  39. This software is covered by US Patent No. 8,185,551.
  40. PATENT RIGHTS GRANT:
  41. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  42. Tokutek as part of the Fractal Tree project.
  43. "PATENT CLAIMS" means the claims of patents that are owned or
  44. licensable by Tokutek, both currently or in the future; and that in
  45. the absence of this license would be infringed by THIS
  46. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  47. "PATENT CHALLENGE" shall mean a challenge to the validity,
  48. patentability, enforceability and/or non-infringement of any of the
  49. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  50. Tokutek hereby grants to you, for the term and geographical scope of
  51. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  52. irrevocable (except as stated in this section) patent license to
  53. make, have made, use, offer to sell, sell, import, transfer, and
  54. otherwise run, modify, and propagate the contents of THIS
  55. IMPLEMENTATION, where such license applies only to the PATENT
  56. CLAIMS. This grant does not include claims that would be infringed
  57. only as a consequence of further modifications of THIS
  58. IMPLEMENTATION. If you or your agent or licensee institute or order
  59. or agree to the institution of patent litigation against any entity
  60. (including a cross-claim or counterclaim in a lawsuit) alleging that
  61. THIS IMPLEMENTATION constitutes direct or contributory patent
  62. infringement, or inducement of patent infringement, then any rights
  63. granted to you under this License shall terminate as of the date
  64. such litigation is filed. If you or your agent or exclusive
  65. licensee institute or order or agree to the institution of a PATENT
  66. CHALLENGE, then Tokutek may terminate any rights granted to you
  67. under this License.
  68. */
  69. #ident "Copyright (c) 2011-2013 Tokutek Inc. All rights reserved."
  70. #ident "$Id$"
  71. #include <toku_portability.h>
  72. #include <zlib.h>
  73. #include <lzma.h>
  74. #include "compress.h"
  75. #include "memory.h"
  76. #include "quicklz.h"
  77. #include "toku_assert.h"
  78. static inline enum toku_compression_method
  79. normalize_compression_method(enum toku_compression_method method)
  80. // Effect: resolve "friendly" names like "fast" and "small" into their real values.
  81. {
  82. switch (method) {
  83. case TOKU_DEFAULT_COMPRESSION_METHOD:
  84. case TOKU_FAST_COMPRESSION_METHOD:
  85. return TOKU_QUICKLZ_METHOD;
  86. case TOKU_SMALL_COMPRESSION_METHOD:
  87. return TOKU_LZMA_METHOD;
  88. default:
  89. return method; // everything else is fine
  90. }
  91. }
  92. size_t toku_compress_bound (enum toku_compression_method a, size_t size)
  93. // See compress.h for the specification of this function.
  94. {
  95. a = normalize_compression_method(a);
  96. switch (a) {
  97. case TOKU_NO_COMPRESSION:
  98. return size + 1;
  99. case TOKU_LZMA_METHOD:
  100. return 1+lzma_stream_buffer_bound(size); // We need one extra for the rfc1950-style header byte (bits -03 are TOKU_LZMA_METHOD (1), bits 4-7 are the compression level)
  101. case TOKU_QUICKLZ_METHOD:
  102. return size+400 + 1; // quicklz manual says 400 bytes is enough. We need one more byte for the rfc1950-style header byte. bits 0-3 are 9, bits 4-7 are the QLZ_COMPRESSION_LEVEL.
  103. case TOKU_ZLIB_METHOD:
  104. return compressBound (size);
  105. case TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD:
  106. return 2+deflateBound(nullptr, size); // We need one extra for the rfc1950-style header byte, and one extra to store windowBits (a bit over cautious about future upgrades maybe).
  107. default:
  108. break;
  109. }
  110. // fall through for bad enum (thus compiler can warn us if we didn't use all the enums
  111. assert(0); return 0;
  112. }
  113. void toku_compress (enum toku_compression_method a,
  114. // the following types and naming conventions come from zlib.h
  115. Bytef *dest, uLongf *destLen,
  116. const Bytef *source, uLong sourceLen)
  117. // See compress.h for the specification of this function.
  118. {
  119. static const int zlib_compression_level = 5;
  120. static const int zlib_without_checksum_windowbits = -15;
  121. a = normalize_compression_method(a);
  122. assert(sourceLen < (1LL << 32));
  123. switch (a) {
  124. case TOKU_NO_COMPRESSION:
  125. dest[0] = TOKU_NO_COMPRESSION;
  126. memcpy(dest + 1, source, sourceLen);
  127. *destLen = sourceLen + 1;
  128. return;
  129. case TOKU_ZLIB_METHOD: {
  130. int r = compress2(dest, destLen, source, sourceLen, zlib_compression_level);
  131. assert(r == Z_OK);
  132. assert((dest[0]&0xF) == TOKU_ZLIB_METHOD);
  133. return;
  134. }
  135. case TOKU_QUICKLZ_METHOD: {
  136. if (sourceLen==0) {
  137. // quicklz requires at least one byte, so we handle this ourselves
  138. assert(1 <= *destLen);
  139. *destLen = 1;
  140. } else {
  141. qlz_state_compress *XCALLOC(qsc);
  142. size_t actual_destlen = qlz_compress(source, (char*)(dest+1), sourceLen, qsc);
  143. assert(actual_destlen +1 <= *destLen);
  144. *destLen = actual_destlen+1; // add one for the rfc1950-style header byte.
  145. toku_free(qsc);
  146. }
  147. // Fill in that first byte
  148. dest[0] = TOKU_QUICKLZ_METHOD + (QLZ_COMPRESSION_LEVEL << 4);
  149. return;
  150. }
  151. case TOKU_LZMA_METHOD: {
  152. const int lzma_compression_level = 2;
  153. if (sourceLen==0) {
  154. // lzma version 4.999 requires at least one byte, so we'll do it ourselves.
  155. assert(1<=*destLen);
  156. *destLen = 1;
  157. } else {
  158. size_t out_pos = 1;
  159. lzma_ret r = lzma_easy_buffer_encode(lzma_compression_level, LZMA_CHECK_NONE, NULL,
  160. source, sourceLen,
  161. dest, &out_pos, *destLen);
  162. assert(out_pos < *destLen);
  163. if (r != LZMA_OK) {
  164. fprintf(stderr, "lzma_easy_buffer_encode() returned %d\n", (int) r);
  165. }
  166. assert(r==LZMA_OK);
  167. *destLen = out_pos;
  168. }
  169. dest[0] = TOKU_LZMA_METHOD + (lzma_compression_level << 4);
  170. return;
  171. }
  172. case TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD: {
  173. z_stream strm;
  174. strm.zalloc = Z_NULL;
  175. strm.zfree = Z_NULL;
  176. strm.opaque = Z_NULL;
  177. strm.next_in = const_cast<Bytef *>(source);
  178. strm.avail_in = sourceLen;
  179. int r = deflateInit2(&strm, zlib_compression_level, Z_DEFLATED,
  180. zlib_without_checksum_windowbits, 8, Z_DEFAULT_STRATEGY);
  181. lazy_assert(r == Z_OK);
  182. strm.next_out = dest + 2;
  183. strm.avail_out = *destLen - 2;
  184. r = deflate(&strm, Z_FINISH);
  185. lazy_assert(r == Z_STREAM_END);
  186. r = deflateEnd(&strm);
  187. lazy_assert(r == Z_OK);
  188. *destLen = strm.total_out + 2;
  189. dest[0] = TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD + (zlib_compression_level << 4);
  190. dest[1] = zlib_without_checksum_windowbits;
  191. return;
  192. }
  193. default:
  194. break;
  195. }
  196. // default fall through to error.
  197. assert(0);
  198. }
  199. void toku_decompress (Bytef *dest, uLongf destLen,
  200. const Bytef *source, uLongf sourceLen)
  201. // See compress.h for the specification of this function.
  202. {
  203. assert(sourceLen>=1); // need at least one byte for the RFC header.
  204. switch (source[0] & 0xF) {
  205. case TOKU_NO_COMPRESSION:
  206. memcpy(dest, source + 1, sourceLen - 1);
  207. return;
  208. case TOKU_ZLIB_METHOD: {
  209. uLongf actual_destlen = destLen;
  210. int r = uncompress(dest, &actual_destlen, source, sourceLen);
  211. assert(r == Z_OK);
  212. assert(actual_destlen == destLen);
  213. return;
  214. }
  215. case TOKU_QUICKLZ_METHOD:
  216. if (sourceLen>1) {
  217. qlz_state_decompress *XCALLOC(qsd);
  218. uLongf actual_destlen = qlz_decompress((char*)source+1, dest, qsd);
  219. assert(actual_destlen == destLen);
  220. toku_free(qsd);
  221. } else {
  222. // length 1 means there is no data, so do nothing.
  223. assert(destLen==0);
  224. }
  225. return;
  226. case TOKU_LZMA_METHOD: {
  227. if (sourceLen>1) {
  228. uint64_t memlimit = UINT64_MAX;
  229. size_t out_pos = 0;
  230. size_t in_pos = 1;
  231. lzma_ret r = lzma_stream_buffer_decode(&memlimit, // memlimit, use UINT64_MAX to disable this check
  232. 0, // flags
  233. NULL, // allocator
  234. source, &in_pos, sourceLen,
  235. dest, &out_pos, destLen);
  236. assert(r==LZMA_OK);
  237. assert(out_pos == destLen);
  238. } else {
  239. // length 1 means there is no data, so do nothing.
  240. assert(destLen==0);
  241. }
  242. return;
  243. }
  244. case TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD: {
  245. z_stream strm;
  246. strm.next_in = const_cast<Bytef *>(source + 2);
  247. strm.avail_in = sourceLen - 2;
  248. strm.zalloc = Z_NULL;
  249. strm.zfree = Z_NULL;
  250. strm.opaque = Z_NULL;
  251. char windowBits = source[1];
  252. int r = inflateInit2(&strm, windowBits);
  253. lazy_assert(r == Z_OK);
  254. strm.next_out = dest;
  255. strm.avail_out = destLen;
  256. r = inflate(&strm, Z_FINISH);
  257. lazy_assert(r == Z_STREAM_END);
  258. r = inflateEnd(&strm);
  259. lazy_assert(r == Z_OK);
  260. return;
  261. }
  262. }
  263. // default fall through to error.
  264. assert(0);
  265. }