| 1 | /* Copyright (C) 2017 MariaDB Foundation |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | |
| 16 | |
| 17 | #include <my_global.h> |
| 18 | #include "sql_string.h" |
| 19 | #include "sql_class.h" |
| 20 | #include "field_comp.h" |
| 21 | #include <zlib.h> |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | Compresses string using zlib |
| 26 | |
| 27 | @param[out] to destination buffer for compressed data |
| 28 | @param[in] from data to compress |
| 29 | @param[in] length from length |
| 30 | |
| 31 | Requirement is such that string stored at `to' must not exceed `from' length. |
| 32 | Otherwise 0 is returned and caller stores string uncompressed. |
| 33 | |
| 34 | `to' must be large enough to hold `length' bytes. |
| 35 | |
| 36 | length == 1 is an edge case that may break stream.avail_out calculation: at |
| 37 | least 2 bytes required to store metadata. |
| 38 | */ |
| 39 | |
| 40 | static uint compress_zlib(THD *thd, char *to, const char *from, uint length) |
| 41 | { |
| 42 | uint level= thd->variables.column_compression_zlib_level; |
| 43 | |
| 44 | /* Caller takes care of empty strings. */ |
| 45 | DBUG_ASSERT(length); |
| 46 | |
| 47 | if (level > 0 && length > 1) |
| 48 | { |
| 49 | z_stream stream; |
| 50 | int wbits= thd->variables.column_compression_zlib_wrap ? MAX_WBITS : |
| 51 | -MAX_WBITS; |
| 52 | uint strategy= thd->variables.column_compression_zlib_strategy; |
| 53 | /* Store only meaningful bytes of original data length. */ |
| 54 | uchar original_pack_length= number_storage_requirement(length); |
| 55 | |
| 56 | *to= 0x80 + original_pack_length + (wbits < 0 ? 8 : 0); |
| 57 | store_bigendian(length, (uchar*) to + 1, original_pack_length); |
| 58 | |
| 59 | stream.avail_in= length; |
| 60 | stream.next_in= (Bytef*) from; |
| 61 | |
| 62 | DBUG_ASSERT(length >= static_cast<uint>(original_pack_length) + 1); |
| 63 | stream.avail_out= length - original_pack_length - 1; |
| 64 | stream.next_out= (Bytef*) to + original_pack_length + 1; |
| 65 | |
| 66 | stream.zalloc= 0; |
| 67 | stream.zfree= 0; |
| 68 | stream.opaque= 0; |
| 69 | |
| 70 | if (deflateInit2(&stream, level, Z_DEFLATED, wbits, 8, strategy) == Z_OK && |
| 71 | deflate(&stream, Z_FINISH) == Z_STREAM_END && |
| 72 | deflateEnd(&stream) == Z_OK) |
| 73 | return (uint) (stream.next_out - (Bytef*) to); |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static int uncompress_zlib(String *to, const uchar *from, uint from_length, |
| 80 | uint field_length) |
| 81 | { |
| 82 | z_stream stream; |
| 83 | uchar original_pack_length; |
| 84 | int wbits; |
| 85 | ulonglong avail_out; |
| 86 | |
| 87 | original_pack_length= *from & 0x07; |
| 88 | wbits= *from & 8 ? -MAX_WBITS : MAX_WBITS; |
| 89 | |
| 90 | from++; |
| 91 | from_length--; |
| 92 | |
| 93 | if (from_length < original_pack_length) |
| 94 | { |
| 95 | my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0)); |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | avail_out= (ulonglong)read_bigendian(from, original_pack_length); |
| 100 | |
| 101 | if (avail_out > field_length) |
| 102 | { |
| 103 | my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0)); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | stream.avail_out= (uint)avail_out; |
| 108 | if (to->alloc(stream.avail_out)) |
| 109 | return 1; |
| 110 | |
| 111 | stream.next_out= (Bytef*) to->ptr(); |
| 112 | |
| 113 | stream.avail_in= from_length - original_pack_length; |
| 114 | stream.next_in= (Bytef*) from + original_pack_length; |
| 115 | |
| 116 | stream.zalloc= 0; |
| 117 | stream.zfree= 0; |
| 118 | stream.opaque= 0; |
| 119 | |
| 120 | if (inflateInit2(&stream, wbits) == Z_OK && |
| 121 | inflate(&stream, Z_FINISH) == Z_STREAM_END && |
| 122 | inflateEnd(&stream) == Z_OK) |
| 123 | { |
| 124 | to->length(stream.total_out); |
| 125 | return 0; |
| 126 | } |
| 127 | my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0)); |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | Compression_method compression_methods[MAX_COMPRESSION_METHODS]= |
| 133 | { |
| 134 | { 0, 0, 0 }, |
| 135 | { 0, 0, 0 }, |
| 136 | { 0, 0, 0 }, |
| 137 | { 0, 0, 0 }, |
| 138 | { 0, 0, 0 }, |
| 139 | { 0, 0, 0 }, |
| 140 | { 0, 0, 0 }, |
| 141 | { 0, 0, 0 }, |
| 142 | { "zlib" , compress_zlib, uncompress_zlib }, |
| 143 | { 0, 0, 0 }, |
| 144 | { 0, 0, 0 }, |
| 145 | { 0, 0, 0 }, |
| 146 | { 0, 0, 0 }, |
| 147 | { 0, 0, 0 }, |
| 148 | { 0, 0, 0 }, |
| 149 | { 0, 0, 0 } |
| 150 | }; |
| 151 | |