1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
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, version 2.0, 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 */
/**
@file mysys/my_compress.cc
*/
#include "my_compress.h"
#include <string.h>
#include <sys/types.h>
#include <zlib.h>
#include <zstd.h>
#include <algorithm>
#include <cstddef>
#include <mysql_com.h>
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include "mysys/mysys_priv.h"
/**
Initialize a compress context object to be associated with a NET object.
@param cmp_ctx Pointer to compression context.
@param algorithm Compression algorithm.
@param compression_level Compression level corresponding to the compression
algorithm.
*/
void mysql_compress_context_init(mysql_compress_context *cmp_ctx,
enum enum_compression_algorithm algorithm,
unsigned int compression_level) {
cmp_ctx->algorithm = algorithm;
if (algorithm == enum_compression_algorithm::MYSQL_ZLIB)
cmp_ctx->u.zlib_ctx.compression_level = compression_level;
else if (algorithm == enum_compression_algorithm::MYSQL_ZSTD) {
cmp_ctx->u.zstd_ctx.compression_level = compression_level;
// This is set after connect phase during first network i/o.
cmp_ctx->u.zstd_ctx.cctx = nullptr;
cmp_ctx->u.zstd_ctx.dctx = nullptr;
}
}
/**
Deinitialize the compression context allocated.
@param mysql_compress_ctx Pointer to Compression context.
*/
void mysql_compress_context_deinit(mysql_compress_context *mysql_compress_ctx) {
if (mysql_compress_ctx->algorithm == enum_compression_algorithm::MYSQL_ZSTD) {
if (mysql_compress_ctx->u.zstd_ctx.cctx != nullptr) {
ZSTD_freeCCtx(mysql_compress_ctx->u.zstd_ctx.cctx);
mysql_compress_ctx->u.zstd_ctx.cctx = nullptr;
}
if (mysql_compress_ctx->u.zstd_ctx.dctx != nullptr) {
ZSTD_freeDCtx(mysql_compress_ctx->u.zstd_ctx.dctx);
mysql_compress_ctx->u.zstd_ctx.dctx = nullptr;
}
}
}
/**
Allocate zstd compression contexts if necessary and compress using zstd the
buffer.
@param comp_ctx Compression context info relating to zstd.
@param packet Data to compress. This is is replaced with the compressed
data.
@param len Length of data to compress at 'packet'
@param complen out: 0 if packet was not compressed
@return nullptr if error (len is not changed) else pointer to buffer.
size of compressed packet).
*/
uchar *zstd_compress_alloc(mysql_zstd_compress_context *comp_ctx,
const uchar *packet, size_t *len, size_t *complen) {
if (comp_ctx->cctx == nullptr) {
if (!(comp_ctx->cctx = ZSTD_createCCtx())) {
return nullptr;
}
}
size_t zstd_len = ZSTD_compressBound(*len);
void *compbuf;
size_t zstd_res;
if (!(compbuf = my_malloc(PSI_NOT_INSTRUMENTED, zstd_len, MYF(MY_WME)))) {
return nullptr;
}
zstd_res =
ZSTD_compressCCtx(comp_ctx->cctx, compbuf, zstd_len, (const void *)packet,
*len, comp_ctx->compression_level);
if (ZSTD_isError(zstd_res)) {
DBUG_PRINT("error", ("Can't compress zstd packet, error: %zd, %s", zstd_res,
ZSTD_getErrorName(zstd_res)));
my_free(compbuf);
return nullptr;
}
if (zstd_res > *len) {
*complen = 0;
my_free(compbuf);
DBUG_PRINT("note",
("Packet got longer on zstd compression; Not compressed"));
return nullptr;
}
*complen = *len;
*len = zstd_res;
return (uchar *)compbuf;
}
/**
Uncompress a zstd compressed data.
@param comp_ctx Pointer to compression context.
@param packet Packet with zstd compressed data.
@param len Length of zstd compressed packet.
@param[out] complen Length of uncompressed packet.
@return true on error else false.
*/
static bool zstd_uncompress(mysql_zstd_compress_context *comp_ctx,
uchar *packet, size_t len, size_t *complen) {
assert(comp_ctx != nullptr);
size_t zstd_res;
void *compbuf;
if (comp_ctx->dctx == nullptr) {
if (!(comp_ctx->dctx = ZSTD_createDCtx())) {
return true;
}
}
if (!(compbuf = my_malloc(PSI_NOT_INSTRUMENTED, *complen, MYF(MY_WME)))) {
return true;
}
zstd_res = ZSTD_decompressDCtx(comp_ctx->dctx, compbuf, *complen,
(const void *)packet, len);
if (ZSTD_isError(zstd_res) || zstd_res != *complen) {
DBUG_PRINT("error", ("Can't uncompress zstd packet, error: %zd, %s",
zstd_res, ZSTD_getErrorName(zstd_res)));
my_free(compbuf);
return true;
}
memcpy(packet, compbuf, *complen);
my_free(compbuf);
return false;
}
/**
Allocate zlib compression contexts if necessary and compress using zlib the
buffer.
@param comp_ctx Compression context info relating to zlib.
@param packet Data to compress. This is is replaced with the compressed
data.
@param len Length of data to compress at 'packet'
@param [out] complen 0 if packet was not compressed
@return nullptr if error (len is not changed) else pointer to buffer.
size of compressed packet).
*/
static uchar *zlib_compress_alloc(mysql_zlib_compress_context *comp_ctx,
const uchar *packet, size_t *len,
size_t *complen) {
uchar *compbuf;
uLongf tmp_complen;
int res;
*complen = *len * 120 / 100 + 12;
if (!(compbuf = (uchar *)my_malloc(key_memory_my_compress_alloc, *complen,
MYF(MY_WME))))
return nullptr; /* Not enough memory */
tmp_complen = (uint)*complen;
res = compress2((Bytef *)compbuf, &tmp_complen,
(Bytef *)const_cast<uchar *>(packet), (uLong)*len,
comp_ctx->compression_level);
*complen = tmp_complen;
if (res != Z_OK) {
my_free(compbuf);
return nullptr;
}
if (*complen >= *len) {
*complen = 0;
my_free(compbuf);
DBUG_PRINT("note", ("Packet got longer on compression; Not compressed"));
return nullptr;
}
/* Store length of compressed packet in *len */
std::swap(*len, *complen);
return compbuf;
}
/**
Uncompress a zlib compressed data.
@param packet Packet which zstd compressed data.
@param len Length of zstd compressed packet.
@param[out] complen Length of uncompressed packet.
@return true on error else false.
*/
static bool zlib_uncompress(uchar *packet, size_t len, size_t *complen) {
uLongf tmp_complen;
uchar *compbuf =
(uchar *)my_malloc(key_memory_my_compress_alloc, *complen, MYF(MY_WME));
int error;
if (!compbuf) return true; /* Not enough memory */
tmp_complen = (uint)*complen;
error =
uncompress((Bytef *)compbuf, &tmp_complen, (Bytef *)packet, (uLong)len);
*complen = tmp_complen;
if (error != Z_OK) { /* Probably wrong packet */
DBUG_PRINT("error", ("Can't uncompress packet, error: %d", error));
my_free(compbuf);
return true;
}
memcpy(packet, compbuf, *complen);
my_free(compbuf);
return false;
}
/**
This replaces the packet with a compressed packet
@param [in] comp_ctx Compress context
@param [in, out] packet Data to compress. This is replaced with the
compressed data.
@param [in] len Length of data to compress at 'packet'
@param [out] complen Compressed packet length. 0, if packet was not
compressed
@retval 1 error. 'len' is not changed
@retval 0 ok. 'len' contains the size of the compressed packet
*/
bool my_compress(mysql_compress_context *comp_ctx, uchar *packet, size_t *len,
size_t *complen) {
DBUG_ENTER("my_compress");
if (*len < MIN_COMPRESS_LENGTH) {
*complen = 0;
DBUG_PRINT("note", ("Packet too short: Not compressed"));
} else {
uchar *compbuf = my_compress_alloc(comp_ctx, packet, len, complen);
if (!compbuf) DBUG_RETURN(*complen ? 0 : 1);
memcpy(packet, compbuf, *len);
my_free(compbuf);
}
DBUG_RETURN(0);
}
uchar *my_compress_alloc(mysql_compress_context *comp_ctx, const uchar *packet,
size_t *len, size_t *complen) {
if (comp_ctx->algorithm == enum_compression_algorithm::MYSQL_ZSTD)
return zstd_compress_alloc(&comp_ctx->u.zstd_ctx, packet, len, complen);
if (comp_ctx->algorithm == enum_compression_algorithm::MYSQL_UNCOMPRESSED) {
// If compression algorithm is set to none do not compress, even if compress
// flag was set.
*complen = 0;
return nullptr;
}
assert(comp_ctx->algorithm == enum_compression_algorithm::MYSQL_ZLIB);
return zlib_compress_alloc(&comp_ctx->u.zlib_ctx, packet, len, complen);
}
/**
Uncompress packet
@param comp_ctx Pointer to compression context.
@param packet Compressed data. This is is replaced with the original
data.
@param len Length of compressed data
@param[out] complen Length of the packet buffer after uncompression (must be
enough for the original data)
@return true on error else false on success
*/
bool my_uncompress(mysql_compress_context *comp_ctx, uchar *packet, size_t len,
size_t *complen) {
DBUG_ENTER("my_uncompress");
assert(comp_ctx != nullptr);
if (*complen) /* If compressed */
{
if (comp_ctx->algorithm == enum_compression_algorithm::MYSQL_ZSTD)
DBUG_RETURN(zstd_uncompress(&comp_ctx->u.zstd_ctx, packet, len, complen));
else if (comp_ctx->algorithm == enum_compression_algorithm::MYSQL_ZLIB)
DBUG_RETURN(zlib_uncompress(packet, len, complen));
}
*complen = len;
DBUG_RETURN(0);
}
/**
Get default compression level corresponding to a given compression method.
@param algorithm Compression Method. Possible values are zlib or zstd.
@return an unsigned int representing default compression level.
6 is the default compression level for zlib and 3 is the
default compression level for zstd.
*/
unsigned int mysql_default_compression_level(
enum enum_compression_algorithm algorithm) {
switch (algorithm) {
case MYSQL_ZLIB:
return 6;
case MYSQL_ZSTD:
return 3;
default:
assert(0); // should not reach here.
return 0; // To make compiler happy.
}
}
|