File: compression.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (152 lines) | stat: -rw-r--r-- 5,681 bytes parent folder | download
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
/* Copyright (c) 2019, 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.

   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 */

#include "compression.h"
#include "m_ctype.h"
#include "my_dbug.h"
#include "my_sys.h"
#include "mysqld_error.h"

/**
  This function is used to validate compression algorithm specified as part
  of change master to statement.

  @param name   compression algorithm name. Name can be either zlib,zstd or
                empty string.

  @retval  an enum to represents what algorithm is specified in case it is
           a valid algorithm else return INVALID.
*/
enum_compression_algorithm get_compression_algorithm(std::string name) {
  if (name.empty() || name.c_str() == nullptr)
    return enum_compression_algorithm::MYSQL_INVALID;
  if (!my_strcasecmp(&my_charset_latin1, name.c_str(),
                     COMPRESSION_ALGORITHM_ZLIB))
    return enum_compression_algorithm::MYSQL_ZLIB;
  else if (!my_strcasecmp(&my_charset_latin1, name.c_str(),
                          COMPRESSION_ALGORITHM_ZSTD))
    return enum_compression_algorithm::MYSQL_ZSTD;
  else if (!my_strcasecmp(&my_charset_latin1, name.c_str(),
                          COMPRESSION_ALGORITHM_UNCOMPRESSED))
    return enum_compression_algorithm::MYSQL_UNCOMPRESSED;
  return enum_compression_algorithm::MYSQL_INVALID;
}

/**
  This function is used to parse comma separated list of compression algorithm
  names and return a list containing every algorithm name.

  @param       name    comma separated list of compression algorithm names
  @param[out]  list    list containing algorithm names
*/
void parse_compression_algorithms_list(std::string name,
                                       std::vector<std::string> &list) {
  std::string token;
  std::stringstream str(name);
  while (getline(str, token, ',')) list.push_back(token);
}

/**
  This function is used to validate compression level for zstd compression

  @param level  compression level to be validated against compression name

  @retval false if level is not valid.
  @retval true if level is valid.
*/
bool is_zstd_compression_level_valid(uint level) {
  return (level >= 1 && level <= 22);
}

/**
  This function is used to validate compression algorithm names and maximum
  names is not more than 3

  @param     algorithm_names   list of compression algorithm names.
  @param     channel_name      Replication channel name.
  @param     ignore_errors     If set to false, report errors to the client,
                               otherwise do not report errors"

  @retval 0  success
  @retval 1  error or warnings
*/
bool validate_compression_attributes(std::string algorithm_names,
                                     std::string channel_name [[maybe_unused]],
                                     bool ignore_errors [[maybe_unused]]) {
  DBUG_TRACE;
  /*
    Note: there's no real limit like that to the string. But, since the
    replication
  */
  if (algorithm_names.length() >= COMPRESSION_ALGORITHM_NAME_BUFFER_SIZE) {
#ifdef MYSQL_SERVER
    if (!ignore_errors) {
      my_error(ER_CHANGE_RPL_SRC_WRONG_COMPRESSION_ALGORITHM_SIZE, MYF(0),
               algorithm_names.length(), channel_name.data());
    }
#endif
    return true;
  }
  std::vector<std::string> algorithm_name_list;

  parse_compression_algorithms_list(algorithm_names, algorithm_name_list);
  unsigned int total_names = algorithm_name_list.size();

  if (!total_names) {
#ifdef MYSQL_SERVER
    if (!ignore_errors) {
      my_error(ER_CHANGE_SOURCE_WRONG_COMPRESSION_ALGORITHM_CLIENT, MYF(0),
               algorithm_names.c_str(), channel_name.c_str());
    }
#endif
    return true;
  }
  if (total_names > COMPRESSION_ALGORITHM_COUNT_MAX) {
#ifdef MYSQL_SERVER
    if (!ignore_errors) {
      my_error(ER_CHANGE_SOURCE_WRONG_COMPRESSION_ALGORITHM_LIST_CLIENT, MYF(0),
               algorithm_names.c_str(), channel_name.c_str());
    }
#endif
    return true;
  }
  /* validate compression algorithm names */
  auto name_it = algorithm_name_list.begin();
  enum_compression_algorithm method = enum_compression_algorithm::MYSQL_INVALID;
  while (name_it != algorithm_name_list.end()) {
    std::string algorithm_name = *name_it;
    /* validate algorithm name */
    method = get_compression_algorithm(algorithm_name);
    if (method == enum_compression_algorithm::MYSQL_INVALID) {
#ifdef MYSQL_SERVER
      if (!ignore_errors) {
        my_error(ER_CHANGE_SOURCE_WRONG_COMPRESSION_ALGORITHM_CLIENT, MYF(0),
                 algorithm_name.c_str(), channel_name.c_str());
      }
#endif
      return true;
    }
    name_it++;
  }
  return false;
}