| 1 |
/* Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#include "libbinlogevents/export/binary_log_funcs.h" |
| 25 |
|
| 26 |
#include "byteorder.h" |
| 27 |
#include "field_types.h" // enum_field_types |
| 28 |
#include "my_time.h" // DATETIME_MAX_DECIMALS |
| 29 |
#include "wrapper_functions.h" |
| 30 |
|
| 31 |
#include <stdint.h> |
| 32 |
#include <string.h> |
| 33 |
#include <climits> |
| 34 |
#include <cstring> |
| 35 |
|
| 36 |
/** |
| 37 |
Max value for an unsigned integer of 'bits' bits. |
| 38 |
|
| 39 |
The somewhat contorted expression is to avoid overflow. |
| 40 |
*/ |
| 41 |
static unsigned int uint_max(int bits) { |
| 42 |
BAPI_ASSERT(static_cast<unsigned int>(bits) <= |
| 43 |
sizeof(unsigned int) * CHAR_BIT); |
| 44 |
return (((1U << (bits - 1)) - 1) << 1) | 1; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
Calculate binary size of packed numeric time representation. |
| 49 |
|
| 50 |
@param dec Precision. |
| 51 |
The same formula is used to find the binary size of the packed numeric time |
| 52 |
in libbinlogevents/src/value.cpp calc_field_size(). |
| 53 |
If any modification is done here the same needs to be done in the |
| 54 |
aforementioned method in libbinlogevents also. |
| 55 |
*/ |
| 56 |
unsigned int my_time_binary_length(unsigned int dec) { |
| 57 |
BAPI_ASSERT(dec <= DATETIME_MAX_DECIMALS); |
| 58 |
return 3 + (dec + 1) / 2; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
Calculate binary size of packed datetime representation. |
| 63 |
@param dec Precision. |
| 64 |
|
| 65 |
The same formula is used to find the binary size of the packed numeric time |
| 66 |
in libbinlogevents/src/value.cpp calc_field_size(). |
| 67 |
If any modification is done here the same needs to be done in the |
| 68 |
aforementioned method in libbinlogevents also. |
| 69 |
*/ |
| 70 |
unsigned int my_datetime_binary_length(unsigned int dec) { |
| 71 |
BAPI_ASSERT(dec <= DATETIME_MAX_DECIMALS); |
| 72 |
return 5 + (dec + 1) / 2; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
Calculate on-disk size of a timestamp value. |
| 77 |
|
| 78 |
@param dec Precision. |
| 79 |
|
| 80 |
The same formula is used to find the binary size of the packed numeric time |
| 81 |
in libbinlogevents/src/value.cpp calc_field_size(). |
| 82 |
If any modification is done here the same needs to be done in the |
| 83 |
aforementioned method in libbinlogevents also. |
| 84 |
*/ |
| 85 |
unsigned int my_timestamp_binary_length(unsigned int dec) { |
| 86 |
BAPI_ASSERT(dec <= DATETIME_MAX_DECIMALS); |
| 87 |
return 4 + (dec + 1) / 2; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
Compute the maximum display length of a field. |
| 92 |
|
| 93 |
@param sql_type Type of the field |
| 94 |
@param metadata The metadata from the master for the field. |
| 95 |
@return Maximum length of the field in bytes. |
| 96 |
*/ |
| 97 |
unsigned int max_display_length_for_field(enum_field_types sql_type, |
| 98 |
unsigned int metadata) { |
| 99 |
BAPI_ASSERT(metadata >> 16 == 0); |
| 100 |
|
| 101 |
switch (sql_type) { |
| 102 |
case MYSQL_TYPE_NEWDECIMAL: |
| 103 |
return metadata >> 8; |
| 104 |
|
| 105 |
case MYSQL_TYPE_FLOAT: |
| 106 |
return 12; |
| 107 |
|
| 108 |
case MYSQL_TYPE_DOUBLE: |
| 109 |
return 22; |
| 110 |
|
| 111 |
case MYSQL_TYPE_SET: |
| 112 |
case MYSQL_TYPE_ENUM: |
| 113 |
return metadata & 0x00ff; |
| 114 |
|
| 115 |
case MYSQL_TYPE_STRING: { |
| 116 |
unsigned char type = metadata >> 8; |
| 117 |
if (type == MYSQL_TYPE_SET || type == MYSQL_TYPE_ENUM) |
| 118 |
return metadata & 0xff; |
| 119 |
else |
| 120 |
return (((metadata >> 4) & 0x300) ^ 0x300) + (metadata & 0x00ff); |
| 121 |
} |
| 122 |
|
| 123 |
case MYSQL_TYPE_YEAR: |
| 124 |
case MYSQL_TYPE_TINY: |
| 125 |
return 4; |
| 126 |
|
| 127 |
case MYSQL_TYPE_SHORT: |
| 128 |
return 6; |
| 129 |
|
| 130 |
case MYSQL_TYPE_INT24: |
| 131 |
return 9; |
| 132 |
|
| 133 |
case MYSQL_TYPE_LONG: |
| 134 |
return 11; |
| 135 |
|
| 136 |
case MYSQL_TYPE_LONGLONG: |
| 137 |
return 20; |
| 138 |
|
| 139 |
case MYSQL_TYPE_NULL: |
| 140 |
return 0; |
| 141 |
|
| 142 |
case MYSQL_TYPE_NEWDATE: |
| 143 |
return 3; |
| 144 |
|
| 145 |
case MYSQL_TYPE_DATE: |
| 146 |
case MYSQL_TYPE_TIME: |
| 147 |
case MYSQL_TYPE_TIME2: |
| 148 |
return 3; |
| 149 |
|
| 150 |
case MYSQL_TYPE_TIMESTAMP: |
| 151 |
case MYSQL_TYPE_TIMESTAMP2: |
| 152 |
return 4; |
| 153 |
|
| 154 |
case MYSQL_TYPE_DATETIME: |
| 155 |
case MYSQL_TYPE_DATETIME2: |
| 156 |
return 8; |
| 157 |
|
| 158 |
case MYSQL_TYPE_BIT: |
| 159 |
/* |
| 160 |
Decode the size of the bit field from the master. |
| 161 |
*/ |
| 162 |
BAPI_ASSERT((metadata & 0xff) <= 7); |
| 163 |
return 8 * (metadata >> 8U) + (metadata & 0x00ff); |
| 164 |
|
| 165 |
case MYSQL_TYPE_VAR_STRING: |
| 166 |
case MYSQL_TYPE_VARCHAR: |
| 167 |
return metadata; |
| 168 |
|
| 169 |
/* |
| 170 |
The actual length for these types does not really matter since |
| 171 |
they are used to calc_pack_length, which ignores the given |
| 172 |
length for these types. |
| 173 |
|
| 174 |
Since we want this to be accurate for other uses, we return the |
| 175 |
maximum size in bytes of these BLOBs. |
| 176 |
*/ |
| 177 |
|
| 178 |
case MYSQL_TYPE_TINY_BLOB: |
| 179 |
return uint_max(1 * 8); |
| 180 |
|
| 181 |
case MYSQL_TYPE_MEDIUM_BLOB: |
| 182 |
return uint_max(3 * 8); |
| 183 |
|
| 184 |
case MYSQL_TYPE_BLOB: |
| 185 |
/* |
| 186 |
For the blob type, Field::real_type() lies and say that all |
| 187 |
blobs are of type MYSQL_TYPE_BLOB. In that case, we have to look |
| 188 |
at the length instead to decide what the max display size is. |
| 189 |
*/ |
| 190 |
return uint_max(metadata * 8); |
| 191 |
|
| 192 |
case MYSQL_TYPE_LONG_BLOB: |
| 193 |
case MYSQL_TYPE_GEOMETRY: |
| 194 |
case MYSQL_TYPE_JSON: |
| 195 |
return uint_max(4 * 8); |
| 196 |
|
| 197 |
default: |
| 198 |
return UINT_MAX; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
int decimal_binary_size(int precision, int scale) { |
| 203 |
static const int dig2bytes[10] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4}; |
| 204 |
int intg = precision - scale, intg0 = intg / 9, frac0 = scale / 9, |
| 205 |
intg0x = intg - intg0 * 9, frac0x = scale - frac0 * 9; |
| 206 |
|
| 207 |
BAPI_ASSERT(scale >= 0 && precision > 0 && scale <= precision); |
| 208 |
return intg0 * sizeof(uint32_t) + dig2bytes[intg0x] + |
| 209 |
frac0 * sizeof(uint32_t) + dig2bytes[frac0x]; |
| 210 |
} |
| 211 |
|
| 212 |
uint32_t calc_field_size(unsigned char col, const unsigned char *master_data, |
| 213 |
unsigned int metadata) { |
| 214 |
uint32_t length = 0; |
| 215 |
|
| 216 |
switch ((col)) { |
| 217 |
case MYSQL_TYPE_NEWDECIMAL: |
| 218 |
length = decimal_binary_size(metadata >> 8, metadata & 0xff); |
| 219 |
break; |
| 220 |
case MYSQL_TYPE_DECIMAL: |
| 221 |
case MYSQL_TYPE_FLOAT: |
| 222 |
case MYSQL_TYPE_DOUBLE: |
| 223 |
length = metadata; |
| 224 |
break; |
| 225 |
/* |
| 226 |
The cases for SET and ENUM are include for completeness, however |
| 227 |
both are mapped to type MYSQL_TYPE_STRING and their real types |
| 228 |
are encoded in the field metadata. |
| 229 |
*/ |
| 230 |
case MYSQL_TYPE_SET: |
| 231 |
case MYSQL_TYPE_ENUM: |
| 232 |
case MYSQL_TYPE_STRING: { |
| 233 |
unsigned char type = metadata >> 8U; |
| 234 |
if ((type == MYSQL_TYPE_SET) || (type == MYSQL_TYPE_ENUM)) |
| 235 |
length = metadata & 0x00ff; |
| 236 |
else { |
| 237 |
/* |
| 238 |
We are reading the actual size from the master_data record |
| 239 |
because this field has the actual length stored in the first |
| 240 |
one or two bytes. |
| 241 |
*/ |
| 242 |
length = max_display_length_for_field(MYSQL_TYPE_STRING, metadata) > 255 |
| 243 |
? 2 |
| 244 |
: 1; |
| 245 |
|
| 246 |
if (length == 1) |
| 247 |
length += *master_data; |
| 248 |
else { |
| 249 |
uint32_t temp = 0; |
| 250 |
memcpy(&temp, master_data, 2); |
| 251 |
length = length + le32toh(temp); |
| 252 |
} |
| 253 |
} |
| 254 |
break; |
| 255 |
} |
| 256 |
case MYSQL_TYPE_YEAR: |
| 257 |
case MYSQL_TYPE_TINY: |
| 258 |
length = 1; |
| 259 |
break; |
| 260 |
case MYSQL_TYPE_SHORT: |
| 261 |
length = 2; |
| 262 |
break; |
| 263 |
case MYSQL_TYPE_INT24: |
| 264 |
length = 3; |
| 265 |
break; |
| 266 |
case MYSQL_TYPE_LONG: |
| 267 |
length = 4; |
| 268 |
break; |
| 269 |
case MYSQL_TYPE_LONGLONG: |
| 270 |
length = 8; |
| 271 |
break; |
| 272 |
case MYSQL_TYPE_NULL: |
| 273 |
length = 0; |
| 274 |
break; |
| 275 |
case MYSQL_TYPE_NEWDATE: |
| 276 |
length = 3; |
| 277 |
break; |
| 278 |
case MYSQL_TYPE_DATE: |
| 279 |
case MYSQL_TYPE_TIME: |
| 280 |
length = 3; |
| 281 |
break; |
| 282 |
case MYSQL_TYPE_TIME2: |
| 283 |
/* |
| 284 |
The original methods in the server to calculate the binary size of the |
| 285 |
packed numeric time representation is defined in my_time.c, the |
| 286 |
signature being unsigned int my_time_binary_length(uint) |
| 287 |
|
| 288 |
The length below needs to be updated if the above method is updated in |
| 289 |
the server |
| 290 |
*/ |
| 291 |
length = my_time_binary_length(metadata); |
| 292 |
break; |
| 293 |
case MYSQL_TYPE_TIMESTAMP: |
| 294 |
length = 4; |
| 295 |
break; |
| 296 |
case MYSQL_TYPE_TIMESTAMP2: |
| 297 |
/* |
| 298 |
The original methods in the server to calculate the binary size of the |
| 299 |
packed numeric time representation is defined in time.c, the signature |
| 300 |
being unsigned int my_timestamp_binary_length(uint) |
| 301 |
|
| 302 |
The length below needs to be updated if the above method is updated in |
| 303 |
the server |
| 304 |
*/ |
| 305 |
length = my_timestamp_binary_length(metadata); |
| 306 |
break; |
| 307 |
case MYSQL_TYPE_DATETIME: |
| 308 |
length = 8; |
| 309 |
break; |
| 310 |
case MYSQL_TYPE_DATETIME2: |
| 311 |
/* |
| 312 |
The original methods in the server to calculate the binary size of the |
| 313 |
packed numeric time representation is defined in time.c, the signature |
| 314 |
being unsigned int my_datetime_binary_length(uint) |
| 315 |
|
| 316 |
The length below needs to be updated if the above method is updated in |
| 317 |
the server |
| 318 |
*/ |
| 319 |
length = my_datetime_binary_length(metadata); |
| 320 |
break; |
| 321 |
case MYSQL_TYPE_BIT: { |
| 322 |
/* |
| 323 |
Decode the size of the bit field from the master. |
| 324 |
from_len is the length in bytes from the master |
| 325 |
from_bit_len is the number of extra bits stored in the master record |
| 326 |
If from_bit_len is not 0, add 1 to the length to account for accurate |
| 327 |
number of bytes needed. |
| 328 |
*/ |
| 329 |
unsigned int from_len = (metadata >> 8U) & 0x00ff; |
| 330 |
unsigned int from_bit_len = metadata & 0x00ff; |
| 331 |
BAPI_ASSERT(from_bit_len <= 7); |
| 332 |
length = from_len + ((from_bit_len > 0) ? 1 : 0); |
| 333 |
break; |
| 334 |
} |
| 335 |
case MYSQL_TYPE_VARCHAR: { |
| 336 |
length = metadata > 255 ? 2 : 1; |
| 337 |
if (length == 1) |
| 338 |
length += (uint32_t)*master_data; |
| 339 |
else { |
| 340 |
uint32_t temp = 0; |
| 341 |
memcpy(&temp, master_data, 2); |
| 342 |
length = length + le32toh(temp); |
| 343 |
} |
| 344 |
break; |
| 345 |
} |
| 346 |
case MYSQL_TYPE_TINY_BLOB: |
| 347 |
case MYSQL_TYPE_MEDIUM_BLOB: |
| 348 |
case MYSQL_TYPE_LONG_BLOB: |
| 349 |
case MYSQL_TYPE_BLOB: |
| 350 |
case MYSQL_TYPE_GEOMETRY: |
| 351 |
case MYSQL_TYPE_JSON: { |
| 352 |
/* |
| 353 |
Compute the length of the data. We cannot use get_length() here |
| 354 |
since it is dependent on the specific table (and also checks the |
| 355 |
packlength using the internal 'table' pointer) and replication |
| 356 |
is using a fixed format for storing data in the binlog. |
| 357 |
*/ |
| 358 |
switch (metadata) { |
| 359 |
case 1: |
| 360 |
length = *master_data; |
| 361 |
break; |
| 362 |
case 2: |
| 363 |
memcpy(&length, master_data, 2); |
| 364 |
length = le32toh(length); |
| 365 |
break; |
| 366 |
case 3: |
| 367 |
memcpy(&length, master_data, 3); |
| 368 |
length = le32toh(length); |
| 369 |
break; |
| 370 |
case 4: |
| 371 |
memcpy(&length, master_data, 4); |
| 372 |
length = le32toh(length); |
| 373 |
break; |
| 374 |
default: |
| 375 |
BAPI_ASSERT(0); // Should not come here |
| 376 |
break; |
| 377 |
} |
| 378 |
|
| 379 |
length += metadata; |
| 380 |
break; |
| 381 |
} |
| 382 |
default: |
| 383 |
length = UINT_MAX; |
| 384 |
} |
| 385 |
return length; |
| 386 |
} |