2017-04-24 15:54:18 +02:00
|
|
|
/* Copyright (C) 2017 MariaDB Foundation
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
|
|
|
|
|
|
|
|
|
|
|
#include <my_global.h>
|
|
|
|
#include "sql_string.h"
|
|
|
|
#include "sql_class.h"
|
|
|
|
#include "field_comp.h"
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
|
2018-04-03 16:34:46 +02:00
|
|
|
/**
|
|
|
|
Compresses string using zlib
|
|
|
|
|
|
|
|
@param[out] to destination buffer for compressed data
|
|
|
|
@param[in] from data to compress
|
|
|
|
@param[in] length from length
|
|
|
|
|
|
|
|
Requirement is such that string stored at `to' must not exceed `from' length.
|
|
|
|
Otherwise 0 is returned and caller stores string uncompressed.
|
|
|
|
|
|
|
|
`to' must be large enough to hold `length' bytes.
|
|
|
|
|
|
|
|
length == 1 is an edge case that may break stream.avail_out calculation: at
|
|
|
|
least 2 bytes required to store metadata.
|
|
|
|
*/
|
|
|
|
|
2017-04-24 15:54:18 +02:00
|
|
|
static uint compress_zlib(THD *thd, char *to, const char *from, uint length)
|
|
|
|
{
|
|
|
|
uint level= thd->variables.column_compression_zlib_level;
|
|
|
|
|
2018-04-03 16:34:46 +02:00
|
|
|
/* Caller takes care of empty strings. */
|
|
|
|
DBUG_ASSERT(length);
|
|
|
|
|
|
|
|
if (level > 0 && length > 1)
|
2017-04-24 15:54:18 +02:00
|
|
|
{
|
|
|
|
z_stream stream;
|
|
|
|
int wbits= thd->variables.column_compression_zlib_wrap ? MAX_WBITS :
|
|
|
|
-MAX_WBITS;
|
|
|
|
uint strategy= thd->variables.column_compression_zlib_strategy;
|
|
|
|
/* Store only meaningful bytes of original data length. */
|
|
|
|
uchar original_pack_length= number_storage_requirement(length);
|
|
|
|
|
|
|
|
*to= 0x80 + original_pack_length + (wbits < 0 ? 8 : 0);
|
|
|
|
store_bigendian(length, (uchar*) to + 1, original_pack_length);
|
|
|
|
|
|
|
|
stream.avail_in= length;
|
|
|
|
stream.next_in= (Bytef*) from;
|
|
|
|
|
2018-04-20 16:28:38 +02:00
|
|
|
DBUG_ASSERT(length >= static_cast<uint>(original_pack_length) + 1);
|
2017-04-24 15:54:18 +02:00
|
|
|
stream.avail_out= length - original_pack_length - 1;
|
|
|
|
stream.next_out= (Bytef*) to + original_pack_length + 1;
|
|
|
|
|
|
|
|
stream.zalloc= 0;
|
|
|
|
stream.zfree= 0;
|
|
|
|
stream.opaque= 0;
|
|
|
|
|
2020-04-01 22:39:54 +02:00
|
|
|
if (deflateInit2(&stream, level, Z_DEFLATED, wbits, 8, strategy) == Z_OK)
|
|
|
|
{
|
|
|
|
int res= deflate(&stream, Z_FINISH);
|
|
|
|
if (deflateEnd(&stream) == Z_OK && res == Z_STREAM_END)
|
|
|
|
return (uint) (stream.next_out - (Bytef*) to);
|
|
|
|
}
|
2017-04-24 15:54:18 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int uncompress_zlib(String *to, const uchar *from, uint from_length,
|
|
|
|
uint field_length)
|
|
|
|
{
|
|
|
|
z_stream stream;
|
|
|
|
uchar original_pack_length;
|
|
|
|
int wbits;
|
2017-10-27 21:21:58 +02:00
|
|
|
ulonglong avail_out;
|
2017-04-24 15:54:18 +02:00
|
|
|
|
|
|
|
original_pack_length= *from & 0x07;
|
|
|
|
wbits= *from & 8 ? -MAX_WBITS : MAX_WBITS;
|
|
|
|
|
|
|
|
from++;
|
|
|
|
from_length--;
|
|
|
|
|
|
|
|
if (from_length < original_pack_length)
|
|
|
|
{
|
|
|
|
my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:21:58 +02:00
|
|
|
avail_out= (ulonglong)read_bigendian(from, original_pack_length);
|
2017-04-24 15:54:18 +02:00
|
|
|
|
2017-10-27 21:21:58 +02:00
|
|
|
if (avail_out > field_length)
|
2017-04-24 15:54:18 +02:00
|
|
|
{
|
|
|
|
my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:21:58 +02:00
|
|
|
stream.avail_out= (uint)avail_out;
|
2017-04-24 15:54:18 +02:00
|
|
|
if (to->alloc(stream.avail_out))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
stream.next_out= (Bytef*) to->ptr();
|
|
|
|
|
|
|
|
stream.avail_in= from_length - original_pack_length;
|
|
|
|
stream.next_in= (Bytef*) from + original_pack_length;
|
|
|
|
|
|
|
|
stream.zalloc= 0;
|
|
|
|
stream.zfree= 0;
|
|
|
|
stream.opaque= 0;
|
|
|
|
|
2020-04-01 22:39:54 +02:00
|
|
|
if (inflateInit2(&stream, wbits) == Z_OK)
|
2017-04-24 15:54:18 +02:00
|
|
|
{
|
2020-04-01 22:39:54 +02:00
|
|
|
int res= inflate(&stream, Z_FINISH);
|
|
|
|
if (inflateEnd(&stream) == Z_OK && res == Z_STREAM_END)
|
|
|
|
{
|
|
|
|
to->length(stream.total_out);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-24 15:54:18 +02:00
|
|
|
}
|
|
|
|
my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Compression_method compression_methods[MAX_COMPRESSION_METHODS]=
|
|
|
|
{
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ "zlib", compress_zlib, uncompress_zlib },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 },
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
};
|