From a2213b89d4963e38e322a36be938f9dea972f638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 7 Nov 2024 06:47:21 +0200 Subject: [PATCH 1/2] Add MYSQL_OPT_ZSTD_COMPRESSION_LEVEL The compression level was always hard-coded to 3. --- include/ma_common.h | 1 + include/mysql.h | 1 + libmariadb/mariadb_lib.c | 4 ++++ plugins/auth/my_auth.c | 9 ++++++++- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/ma_common.h b/include/ma_common.h index 1ac0cb68..252fdb32 100644 --- a/include/ma_common.h +++ b/include/ma_common.h @@ -87,6 +87,7 @@ struct st_mysql_options_extension { void (*status_callback)(void *ptr, enum enum_mariadb_status_info type, ...); void *status_data; my_bool tls_verify_server_cert; + char zstd_compression_level; }; typedef struct st_connection_handler diff --git a/include/mysql.h b/include/mysql.h index 76b16830..effec1b1 100644 --- a/include/mysql.h +++ b/include/mysql.h @@ -223,6 +223,7 @@ extern const char *SQLSTATE_UNKNOWN; MYSQL_OPT_MAX_ALLOWED_PACKET, MYSQL_OPT_NET_BUFFER_LENGTH, MYSQL_OPT_TLS_VERSION, + MYSQL_OPT_ZSTD_COMPRESSION_LEVEL, /* MariaDB specific */ MYSQL_PROGRESS_CALLBACK=5999, diff --git a/libmariadb/mariadb_lib.c b/libmariadb/mariadb_lib.c index 7d5f6241..9d89fef3 100644 --- a/libmariadb/mariadb_lib.c +++ b/libmariadb/mariadb_lib.c @@ -711,6 +711,7 @@ struct st_default_options mariadb_defaults[] = {{MYSQL_OPT_SSL_ENFORCE}, MARIADB_OPTION_BOOL, "tls-enforce"}, {{MYSQL_OPT_SSL_VERIFY_SERVER_CERT}, MARIADB_OPTION_BOOL,"tls-verify-peer"}, {{MARIADB_OPT_RESTRICTED_AUTH}, MARIADB_OPTION_STR, "restricted-auth"}, + {{MYSQL_OPT_ZSTD_COMPRESSION_LEVEL}, MARIADB_OPTION_INT, "zstd-compression-level"}, {{0}, 0, NULL} }; @@ -3836,6 +3837,9 @@ mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...) } } break; + case MYSQL_OPT_ZSTD_COMPRESSION_LEVEL: + OPT_SET_EXTENDED_VALUE_INT(&mysql->options, zstd_compression_level, *((unsigned int *)arg1)); + break; default: va_end(ap); SET_CLIENT_ERROR(mysql, CR_NOT_IMPLEMENTED, SQLSTATE_UNKNOWN, 0); diff --git a/plugins/auth/my_auth.c b/plugins/auth/my_auth.c index 6c17259b..e21da3b0 100644 --- a/plugins/auth/my_auth.c +++ b/plugins/auth/my_auth.c @@ -407,7 +407,14 @@ static int send_client_reply_packet(MCPVIO_EXT *mpvio, */ if (mysql->client_flag & CLIENT_ZSTD_COMPRESSION) { - *end++= 3; + uchar compression_level= 3; + if (mysql->options.extension && + mysql->options.extension->zstd_compression_level >= 1 && + mysql->options.extension->zstd_compression_level <= 20) + { + compression_level= mysql->options.extension->zstd_compression_level; + } + *end++= compression_level; } /* Write authentication package */ From 16e5b88bab008dd83e1c4a0268eb16da32995e46 Mon Sep 17 00:00:00 2001 From: Georg Richter Date: Tue, 10 Dec 2024 05:18:08 +0100 Subject: [PATCH 2/2] MYSQL_OPT_ZSTD_COMPRESSION_LEVEL fixes: Follow up for commit e633858c9e1ad25628c2d04f4af1781e8bdbbffb: - Fixed ASAN bug (int to char conversion) - Allow to retrieve zstd compression level via mysql_get_optionv() --- include/ma_common.h | 2 +- libmariadb/mariadb_lib.c | 5 ++++- unittest/libmariadb/misc.c | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/ma_common.h b/include/ma_common.h index 252fdb32..f7d3730a 100644 --- a/include/ma_common.h +++ b/include/ma_common.h @@ -87,7 +87,7 @@ struct st_mysql_options_extension { void (*status_callback)(void *ptr, enum enum_mariadb_status_info type, ...); void *status_data; my_bool tls_verify_server_cert; - char zstd_compression_level; + unsigned char zstd_compression_level; }; typedef struct st_connection_handler diff --git a/libmariadb/mariadb_lib.c b/libmariadb/mariadb_lib.c index 92b18c73..633bdd30 100644 --- a/libmariadb/mariadb_lib.c +++ b/libmariadb/mariadb_lib.c @@ -3844,7 +3844,7 @@ mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...) } break; case MYSQL_OPT_ZSTD_COMPRESSION_LEVEL: - OPT_SET_EXTENDED_VALUE_INT(&mysql->options, zstd_compression_level, *((unsigned int *)arg1)); + OPT_SET_EXTENDED_VALUE(&mysql->options, zstd_compression_level, *((unsigned char *)arg1)); break; default: va_end(ap); @@ -3872,6 +3872,9 @@ mysql_get_optionv(MYSQL *mysql, enum mysql_option option, void *arg, ...) case MYSQL_OPT_COMPRESS: *((my_bool *)arg)= mysql->options.compress; break; + case MYSQL_OPT_ZSTD_COMPRESSION_LEVEL: + *((unsigned char *)arg)= mysql->options.extension->zstd_compression_level; + break; case MYSQL_OPT_NAMED_PIPE: *((my_bool *)arg)= mysql->options.named_pipe; break; diff --git a/unittest/libmariadb/misc.c b/unittest/libmariadb/misc.c index 5f6f854d..b13a4bf1 100644 --- a/unittest/libmariadb/misc.c +++ b/unittest/libmariadb/misc.c @@ -1663,7 +1663,23 @@ static int test_ext_field_attr(MYSQL *mysql) return OK; } +static int test_comp_level(MYSQL *my __attribute__((unused))) +{ + unsigned char clevel= 5; + unsigned char clevel1= 0; + MYSQL *mysql= mysql_init(NULL); + + mysql_optionsv(mysql, MYSQL_OPT_ZSTD_COMPRESSION_LEVEL, &clevel); + mysql_get_optionv(mysql, MYSQL_OPT_ZSTD_COMPRESSION_LEVEL, &clevel1); + + FAIL_IF(clevel != clevel1, "Different compression levels"); + mysql_close(mysql); + + return OK; +} + struct my_tests_st my_tests[] = { + {"test_comp_level", test_comp_level, TEST_CONNECTION_NONE, 0, NULL, NULL}, {"test_ext_field_attr", test_ext_field_attr, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, {"test_conc533", test_conc533, TEST_CONNECTION_NEW, 0, NULL, NULL}, {"test_conc163", test_conc163, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},