| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/mach0data.ic |
| 29 |
Utilities for converting data from the database file |
| 30 |
to the machine format. |
| 31 |
|
| 32 |
Created 11/28/1995 Heikki Tuuri |
| 33 |
***********************************************************************/ |
| 34 |
|
| 35 |
#include "mtr0types.h" |
| 36 |
#include "ut0byte.h" |
| 37 |
|
| 38 |
/** The following function is used to store data in one byte. |
| 39 |
@param[in] b pointer to byte where to store |
| 40 |
@param[in] n ulint integer to be stored, >= 0, < 256 */ |
| 41 |
static inline void mach_write_to_1(byte *b, ulint n) { |
| 42 |
ut_ad(b); |
| 43 |
ut_ad((n | 0xFFUL) <= 0xFFUL); |
| 44 |
|
| 45 |
b[0] = (byte)n; |
| 46 |
} |
| 47 |
|
| 48 |
/** The following function is used to store data in two consecutive |
| 49 |
bytes. We store the most significant byte to the lower address. |
| 50 |
@param[in] b pointer to 2 bytes where to store |
| 51 |
@param[in] n 2 byte integer to be stored, >= 0, < 64k */ |
| 52 |
static inline void mach_write_to_2(byte *b, ulint n) { |
| 53 |
ut_ad(b); |
| 54 |
ut_ad((n | 0xFFFFUL) <= 0xFFFFUL); |
| 55 |
|
| 56 |
b[0] = (byte)(n >> 8); |
| 57 |
b[1] = (byte)(n); |
| 58 |
} |
| 59 |
|
| 60 |
/** The following function is used to fetch data from one byte. |
| 61 |
@param[in] b pointer to a byte to read |
| 62 |
@return ulint integer, >= 0, < 256 */ |
| 63 |
static inline uint8_t mach_read_from_1(const byte *b) { |
| 64 |
ut_ad(b); |
| 65 |
return ((uint8_t)(b[0])); |
| 66 |
} |
| 67 |
|
| 68 |
/** The following function is used to fetch data from 2 consecutive |
| 69 |
bytes. The most significant byte is at the lowest address. |
| 70 |
@param[in] b pointer to 2 bytes to read |
| 71 |
@return 2-byte integer, >= 0, < 64k */ |
| 72 |
static inline uint16_t mach_read_from_2(const byte *b) { |
| 73 |
return (((ulint)(b[0]) << 8) | (ulint)(b[1])); |
| 74 |
} |
| 75 |
|
| 76 |
/** The following function is used to convert a 16-bit data item |
| 77 |
to the canonical format, for fast bytewise equality test |
| 78 |
against memory. |
| 79 |
@return 16-bit integer in canonical format */ |
| 80 |
static inline uint16_t mach_encode_2( |
| 81 |
ulint n) /*!< in: integer in machine-dependent format */ |
| 82 |
{ |
| 83 |
uint16 ret; |
| 84 |
static_assert(2 == sizeof ret); |
| 85 |
mach_write_to_2((byte *)&ret, n); |
| 86 |
return (ret); |
| 87 |
} |
| 88 |
/** The following function is used to convert a 16-bit data item |
| 89 |
from the canonical format, for fast bytewise equality test |
| 90 |
against memory. |
| 91 |
@return integer in machine-dependent format */ |
| 92 |
static inline ulint mach_decode_2( |
| 93 |
uint16 n) /*!< in: 16-bit integer in canonical format */ |
| 94 |
{ |
| 95 |
static_assert(2 == sizeof n); |
| 96 |
return (mach_read_from_2((const byte *)&n)); |
| 97 |
} |
| 98 |
|
| 99 |
/** The following function is used to store data in 3 consecutive |
| 100 |
bytes. We store the most significant byte to the lowest address. |
| 101 |
@param[in] b pointer to 3 bytes where to store |
| 102 |
@param[in] n 3 byte integer to be stored */ |
| 103 |
static inline void mach_write_to_3(byte *b, ulint n) { |
| 104 |
ut_ad(b); |
| 105 |
ut_ad((n | 0xFFFFFFUL) <= 0xFFFFFFUL); |
| 106 |
|
| 107 |
b[0] = (byte)(n >> 16); |
| 108 |
b[1] = (byte)(n >> 8); |
| 109 |
b[2] = (byte)(n); |
| 110 |
} |
| 111 |
|
| 112 |
/** The following function is used to fetch data from 3 consecutive |
| 113 |
bytes. The most significant byte is at the lowest address. |
| 114 |
@param[in] b pointer to 3 bytes to read |
| 115 |
@return uint32_t integer */ |
| 116 |
static inline uint32_t mach_read_from_3(const byte *b) { |
| 117 |
ut_ad(b); |
| 118 |
return ((static_cast<uint32_t>(b[0]) << 16) | |
| 119 |
(static_cast<uint32_t>(b[1]) << 8) | static_cast<uint32_t>(b[2])); |
| 120 |
} |
| 121 |
|
| 122 |
/** The following function is used to store data in 4 consecutive |
| 123 |
bytes. We store the most significant byte to the lowest address. |
| 124 |
@param[in] b pointer to 4 bytes where to store |
| 125 |
@param[in] n 4 byte integer to be stored */ |
| 126 |
static inline void mach_write_to_4(byte *b, ulint n) { |
| 127 |
ut_ad(b); |
| 128 |
|
| 129 |
b[0] = static_cast<byte>(n >> 24); |
| 130 |
b[1] = static_cast<byte>(n >> 16); |
| 131 |
b[2] = static_cast<byte>(n >> 8); |
| 132 |
b[3] = static_cast<byte>(n); |
| 133 |
} |
| 134 |
|
| 135 |
/** The following function is used to fetch data from 4 consecutive |
| 136 |
bytes. The most significant byte is at the lowest address. |
| 137 |
@param[in] b pointer to 4 bytes to read |
| 138 |
@return 32 bit integer */ |
| 139 |
static inline uint32_t mach_read_from_4(const byte *b) { |
| 140 |
ut_ad(b); |
| 141 |
return ((static_cast<uint32_t>(b[0]) << 24) | |
| 142 |
(static_cast<uint32_t>(b[1]) << 16) | |
| 143 |
(static_cast<uint32_t>(b[2]) << 8) | static_cast<uint32_t>(b[3])); |
| 144 |
} |
| 145 |
|
| 146 |
/** Writes a ulint in a compressed form where the first byte codes the |
| 147 |
length of the stored ulint. We look at the most significant bits of |
| 148 |
the byte. If the most significant bit is zero, it means 1-byte storage, |
| 149 |
else if the 2nd bit is 0, it means 2-byte storage, else if 3rd is 0, |
| 150 |
it means 3-byte storage, else if 4th is 0, it means 4-byte storage, |
| 151 |
else the storage is 5-byte. |
| 152 |
@param[in] b pointer to memory where to store |
| 153 |
@param[in] n ulint integer (< 2^32) to be stored |
| 154 |
@return compressed size in bytes */ |
| 155 |
static inline ulint mach_write_compressed(byte *b, ulint n) { |
| 156 |
ut_ad(b); |
| 157 |
|
| 158 |
if (n < 0x80) { |
| 159 |
/* 0nnnnnnn (7 bits) */ |
| 160 |
mach_write_to_1(b, n); |
| 161 |
return (1); |
| 162 |
} else if (n < 0x4000) { |
| 163 |
/* 10nnnnnn nnnnnnnn (14 bits) */ |
| 164 |
mach_write_to_2(b, n | 0x8000); |
| 165 |
return (2); |
| 166 |
} else if (n < 0x200000) { |
| 167 |
/* 110nnnnn nnnnnnnn nnnnnnnn (21 bits) */ |
| 168 |
mach_write_to_3(b, n | 0xC00000); |
| 169 |
return (3); |
| 170 |
} else if (n < 0x10000000) { |
| 171 |
/* 1110nnnn nnnnnnnn nnnnnnnn nnnnnnnn (28 bits) */ |
| 172 |
mach_write_to_4(b, n | 0xE0000000); |
| 173 |
return (4); |
| 174 |
} else if (n >= 0xFFFFFC00) { |
| 175 |
/* 111110nn nnnnnnnn (10 bits) (extended) */ |
| 176 |
mach_write_to_2(b, (n & 0x3FF) | 0xF800); |
| 177 |
return (2); |
| 178 |
} else if (n >= 0xFFFE0000) { |
| 179 |
/* 1111110n nnnnnnnn nnnnnnnn (17 bits) (extended) */ |
| 180 |
mach_write_to_3(b, (n & 0x1FFFF) | 0xFC0000); |
| 181 |
return (3); |
| 182 |
} else if (n >= 0xFF000000) { |
| 183 |
/* 11111110 nnnnnnnn nnnnnnnn nnnnnnnn (24 bits) (extended) */ |
| 184 |
mach_write_to_4(b, (n & 0xFFFFFF) | 0xFE000000); |
| 185 |
return (4); |
| 186 |
} else { |
| 187 |
/* 11110000 nnnnnnnn nnnnnnnn nnnnnnnn nnnnnnnn (32 bits) */ |
| 188 |
mach_write_to_1(b, 0xF0); |
| 189 |
mach_write_to_4(b + 1, n); |
| 190 |
return (5); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** Return the size of an ulint when written in the compressed form. |
| 195 |
@param[in] n uint32_t integer (< 2^32) to be stored |
| 196 |
@return compressed size in bytes */ |
| 197 |
static inline uint32_t mach_get_compressed_size(ulint n) { |
| 198 |
if (n < 0x80) { |
| 199 |
/* 0nnnnnnn (7 bits) */ |
| 200 |
return (1); |
| 201 |
} else if (n < 0x4000) { |
| 202 |
/* 10nnnnnn nnnnnnnn (14 bits) */ |
| 203 |
return (2); |
| 204 |
} else if (n < 0x200000) { |
| 205 |
/* 110nnnnn nnnnnnnn nnnnnnnn (21 bits) */ |
| 206 |
return (3); |
| 207 |
} else if (n < 0x10000000) { |
| 208 |
/* 1110nnnn nnnnnnnn nnnnnnnn nnnnnnnn (28 bits) */ |
| 209 |
return (4); |
| 210 |
} else if (n >= 0xFFFFFC00) { |
| 211 |
/* 111110nn nnnnnnnn (10 bits) (extended) */ |
| 212 |
return (2); |
| 213 |
} else if (n >= 0xFFFE0000) { |
| 214 |
/* 1111110n nnnnnnnn nnnnnnnn (17 bits) (extended) */ |
| 215 |
return (3); |
| 216 |
} else if (n >= 0xFF000000) { |
| 217 |
/* 11111110 nnnnnnnn nnnnnnnn nnnnnnnn (24 bits) (extended) */ |
| 218 |
return (4); |
| 219 |
} else { |
| 220 |
/* 11110000 nnnnnnnn nnnnnnnn nnnnnnnn nnnnnnnn (32 bits) */ |
| 221 |
return (5); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** Read a ulint in a compressed form. |
| 226 |
@param[in] b pointer to memory from where to read |
| 227 |
@return read integer (< 2^32) */ |
| 228 |
static inline ulint mach_read_compressed(const byte *b) { |
| 229 |
ulint val; |
| 230 |
|
| 231 |
ut_ad(b); |
| 232 |
|
| 233 |
val = mach_read_from_1(b); |
| 234 |
|
| 235 |
if (val < 0x80) { |
| 236 |
/* 0nnnnnnn (7 bits) */ |
| 237 |
} else if (val < 0xC0) { |
| 238 |
/* 10nnnnnn nnnnnnnn (14 bits) */ |
| 239 |
val = mach_read_from_2(b) & 0x3FFF; |
| 240 |
ut_ad(val > 0x7F); |
| 241 |
} else if (val < 0xE0) { |
| 242 |
/* 110nnnnn nnnnnnnn nnnnnnnn (21 bits) */ |
| 243 |
val = mach_read_from_3(b) & 0x1FFFFF; |
| 244 |
ut_ad(val > 0x3FFF); |
| 245 |
} else if (val < 0xF0) { |
| 246 |
/* 1110nnnn nnnnnnnn nnnnnnnn nnnnnnnn (28 bits) */ |
| 247 |
val = mach_read_from_4(b) & 0xFFFFFFF; |
| 248 |
ut_ad(val > 0x1FFFFF); |
| 249 |
} else if (val < 0xF8) { |
| 250 |
/* 11110000 nnnnnnnn nnnnnnnn nnnnnnnn nnnnnnnn (32 bits) */ |
| 251 |
ut_ad(val == 0xF0); |
| 252 |
val = mach_read_from_4(b + 1); |
| 253 |
/* this can treat not-extended format also. */ |
| 254 |
ut_ad(val > 0xFFFFFFF); |
| 255 |
} else if (val < 0xFC) { |
| 256 |
/* 111110nn nnnnnnnn (10 bits) (extended) */ |
| 257 |
val = (mach_read_from_2(b) & 0x3FF) | 0xFFFFFC00; |
| 258 |
} else if (val < 0xFE) { |
| 259 |
/* 1111110n nnnnnnnn nnnnnnnn (17 bits) (extended) */ |
| 260 |
val = (mach_read_from_3(b) & 0x1FFFF) | 0xFFFE0000; |
| 261 |
ut_ad(val < 0xFFFFFC00); |
| 262 |
} else { |
| 263 |
/* 11111110 nnnnnnnn nnnnnnnn nnnnnnnn (24 bits) (extended) */ |
| 264 |
ut_ad(val == 0xFE); |
| 265 |
val = mach_read_from_3(b + 1) | 0xFF000000; |
| 266 |
ut_ad(val < 0xFFFE0000); |
| 267 |
} |
| 268 |
|
| 269 |
return (val); |
| 270 |
} |
| 271 |
|
| 272 |
/** Read a 32-bit integer in a compressed form. |
| 273 |
@param[in,out] b pointer to memory where to read; |
| 274 |
advanced by the number of bytes consumed |
| 275 |
@return unsigned value */ |
| 276 |
static inline uint32_t mach_read_next_compressed(const byte **b) { |
| 277 |
ulint val = mach_read_from_1(*b); |
| 278 |
|
| 279 |
if (val < 0x80) { |
| 280 |
/* 0nnnnnnn (7 bits) */ |
| 281 |
++*b; |
| 282 |
} else if (val < 0xC0) { |
| 283 |
/* 10nnnnnn nnnnnnnn (14 bits) */ |
| 284 |
val = mach_read_from_2(*b) & 0x3FFF; |
| 285 |
ut_ad(val > 0x7F); |
| 286 |
*b += 2; |
| 287 |
} else if (val < 0xE0) { |
| 288 |
/* 110nnnnn nnnnnnnn nnnnnnnn (21 bits) */ |
| 289 |
val = mach_read_from_3(*b) & 0x1FFFFF; |
| 290 |
ut_ad(val > 0x3FFF); |
| 291 |
*b += 3; |
| 292 |
} else if (val < 0xF0) { |
| 293 |
/* 1110nnnn nnnnnnnn nnnnnnnn nnnnnnnn (28 bits) */ |
| 294 |
val = mach_read_from_4(*b) & 0xFFFFFFF; |
| 295 |
ut_ad(val > 0x1FFFFF); |
| 296 |
*b += 4; |
| 297 |
} else if (val < 0xF8) { |
| 298 |
/* 11110000 nnnnnnnn nnnnnnnn nnnnnnnn nnnnnnnn (32 bits) */ |
| 299 |
ut_ad(val == 0xF0); |
| 300 |
val = mach_read_from_4(*b + 1); |
| 301 |
/* this can treat not-extended format also. */ |
| 302 |
ut_ad(val > 0xFFFFFFF); |
| 303 |
*b += 5; |
| 304 |
} else if (val < 0xFC) { |
| 305 |
/* 111110nn nnnnnnnn (10 bits) (extended) */ |
| 306 |
val = (mach_read_from_2(*b) & 0x3FF) | 0xFFFFFC00; |
| 307 |
*b += 2; |
| 308 |
} else if (val < 0xFE) { |
| 309 |
/* 1111110n nnnnnnnn nnnnnnnn (17 bits) (extended) */ |
| 310 |
val = (mach_read_from_3(*b) & 0x1FFFF) | 0xFFFE0000; |
| 311 |
ut_ad(val < 0xFFFFFC00); |
| 312 |
*b += 3; |
| 313 |
} else { |
| 314 |
/* 11111110 nnnnnnnn nnnnnnnn nnnnnnnn (24 bits) (extended) */ |
| 315 |
ut_ad(val == 0xFE); |
| 316 |
val = mach_read_from_3(*b + 1) | 0xFF000000; |
| 317 |
ut_ad(val < 0xFFFE0000); |
| 318 |
*b += 4; |
| 319 |
} |
| 320 |
|
| 321 |
return (static_cast<uint32_t>(val)); |
| 322 |
} |
| 323 |
|
| 324 |
/** The following function is used to store data in 8 consecutive |
| 325 |
bytes. We store the most significant byte to the lowest address. |
| 326 |
@param[in] b pointer to 8 bytes where to store |
| 327 |
@param[in] n 64-bit integer (< 2^64) to be stored */ |
| 328 |
static inline void mach_write_to_8(void *b, uint64_t n) { |
| 329 |
ut_ad(b); |
| 330 |
|
| 331 |
mach_write_to_4(static_cast<byte *>(b), (ulint)(n >> 32)); |
| 332 |
mach_write_to_4(static_cast<byte *>(b) + 4, (ulint)n); |
| 333 |
} |
| 334 |
|
| 335 |
/** The following function is used to fetch data from 8 consecutive |
| 336 |
bytes. The most significant byte is at the lowest address. |
| 337 |
@param[in] b pointer to 8 bytes from where read |
| 338 |
@return 64-bit integer */ |
| 339 |
static inline uint64_t mach_read_from_8(const byte *b) { |
| 340 |
uint64_t u64; |
| 341 |
|
| 342 |
u64 = mach_read_from_4(b); |
| 343 |
u64 <<= 32; |
| 344 |
u64 |= mach_read_from_4(b + 4); |
| 345 |
|
| 346 |
return (u64); |
| 347 |
} |
| 348 |
|
| 349 |
/** The following function is used to store data in 7 consecutive |
| 350 |
bytes. We store the most significant byte to the lowest address. |
| 351 |
@param[in] b pointer to 7 bytes where to store |
| 352 |
@param[in] n 56-bit integer */ |
| 353 |
static inline void mach_write_to_7(byte *b, uint64_t n) { |
| 354 |
ut_ad(b); |
| 355 |
|
| 356 |
mach_write_to_3(b, (ulint)(n >> 32)); |
| 357 |
mach_write_to_4(b + 3, (ulint)n); |
| 358 |
} |
| 359 |
|
| 360 |
/** The following function is used to fetch data from 7 consecutive |
| 361 |
bytes. The most significant byte is at the lowest address. |
| 362 |
@param[in] b pointer to 7 bytes to read |
| 363 |
@return 56-bit integer */ |
| 364 |
static inline uint64_t mach_read_from_7(const byte *b) { |
| 365 |
ut_ad(b); |
| 366 |
|
| 367 |
return (ut_ull_create(mach_read_from_3(b), mach_read_from_4(b + 3))); |
| 368 |
} |
| 369 |
|
| 370 |
/** The following function is used to store data in 6 consecutive |
| 371 |
bytes. We store the most significant byte to the lowest address. |
| 372 |
@param[in] b pointer to 6 bytes where to store |
| 373 |
@param[in] n 48-bit integer to write */ |
| 374 |
static inline void mach_write_to_6(byte *b, uint64_t n) { |
| 375 |
ut_ad(b); |
| 376 |
|
| 377 |
mach_write_to_2(b, (ulint)(n >> 32)); |
| 378 |
mach_write_to_4(b + 2, (ulint)n); |
| 379 |
} |
| 380 |
|
| 381 |
/** The following function is used to fetch data from 6 consecutive |
| 382 |
bytes. The most significant byte is at the lowest address. |
| 383 |
@param[in] b pointer to 6 bytes to read |
| 384 |
@return 48-bit integer */ |
| 385 |
static inline uint64_t mach_read_from_6(const byte *b) { |
| 386 |
ut_ad(b); |
| 387 |
|
| 388 |
return (ut_ull_create(mach_read_from_2(b), mach_read_from_4(b + 2))); |
| 389 |
} |
| 390 |
|
| 391 |
/** Writes a 64-bit integer in a compressed form (5..9 bytes). |
| 392 |
@return size in bytes */ |
| 393 |
static inline ulint mach_u64_write_compressed( |
| 394 |
byte *b, /*!< in: pointer to memory where to store */ |
| 395 |
uint64_t n) /*!< in: 64-bit integer to be stored */ |
| 396 |
{ |
| 397 |
ulint size; |
| 398 |
|
| 399 |
ut_ad(b); |
| 400 |
|
| 401 |
size = mach_write_compressed(b, (ulint)(n >> 32)); |
| 402 |
mach_write_to_4(b + size, (ulint)n); |
| 403 |
|
| 404 |
return (size + 4); |
| 405 |
} |
| 406 |
|
| 407 |
/** Read a 64-bit integer in a compressed form. |
| 408 |
@param[in,out] b pointer to memory where to read; |
| 409 |
advanced by the number of bytes consumed |
| 410 |
@return unsigned value */ |
| 411 |
static inline uint64_t mach_u64_read_next_compressed(const byte **b) { |
| 412 |
uint64_t val; |
| 413 |
|
| 414 |
val = mach_read_next_compressed(b); |
| 415 |
val <<= 32; |
| 416 |
val |= mach_read_from_4(*b); |
| 417 |
*b += 4; |
| 418 |
return (val); |
| 419 |
} |
| 420 |
|
| 421 |
/** Writes a 64-bit integer in a compressed form (1..11 bytes). |
| 422 |
@return size in bytes */ |
| 423 |
static inline ulint mach_u64_write_much_compressed( |
| 424 |
byte *b, /*!< in: pointer to memory where to store */ |
| 425 |
uint64_t n) /*!< in: 64-bit integer to be stored */ |
| 426 |
{ |
| 427 |
ulint size; |
| 428 |
|
| 429 |
ut_ad(b); |
| 430 |
|
| 431 |
if (!(n >> 32)) { |
| 432 |
return (mach_write_compressed(b, (ulint)n)); |
| 433 |
} |
| 434 |
|
| 435 |
*b = (byte)0xFF; |
| 436 |
size = 1 + mach_write_compressed(b + 1, (ulint)(n >> 32)); |
| 437 |
|
| 438 |
size += mach_write_compressed(b + size, (ulint)n & 0xFFFFFFFF); |
| 439 |
|
| 440 |
return (size); |
| 441 |
} |
| 442 |
|
| 443 |
/** Reads a 64-bit integer in a compressed form. |
| 444 |
@return the value read |
| 445 |
@see mach_parse_u64_much_compressed() */ |
| 446 |
static inline uint64_t mach_u64_read_much_compressed( |
| 447 |
const byte *b) /*!< in: pointer to memory from where to read */ |
| 448 |
{ |
| 449 |
uint64_t n; |
| 450 |
|
| 451 |
if (*b != 0xFF) { |
| 452 |
return (mach_read_compressed(b)); |
| 453 |
} |
| 454 |
|
| 455 |
b++; |
| 456 |
n = mach_read_next_compressed(&b); |
| 457 |
n <<= 32; |
| 458 |
n |= mach_read_compressed(b); |
| 459 |
|
| 460 |
return (n); |
| 461 |
} |
| 462 |
|
| 463 |
/** Read a 64-bit integer in a compressed form. |
| 464 |
@param[in,out] b pointer to memory where to read; |
| 465 |
advanced by the number of bytes consumed |
| 466 |
@return unsigned value */ |
| 467 |
static inline uint64_t mach_read_next_much_compressed(const byte **b) { |
| 468 |
uint64_t val = mach_read_from_1(*b); |
| 469 |
|
| 470 |
if (val < 0x80) { |
| 471 |
/* 0nnnnnnn (7 bits) */ |
| 472 |
++*b; |
| 473 |
} else if (val < 0xC0) { |
| 474 |
/* 10nnnnnn nnnnnnnn (14 bits) */ |
| 475 |
val = mach_read_from_2(*b) & 0x3FFF; |
| 476 |
ut_ad(val > 0x7F); |
| 477 |
*b += 2; |
| 478 |
} else if (val < 0xE0) { |
| 479 |
/* 110nnnnn nnnnnnnn nnnnnnnn (21 bits) */ |
| 480 |
val = mach_read_from_3(*b) & 0x1FFFFF; |
| 481 |
ut_ad(val > 0x3FFF); |
| 482 |
*b += 3; |
| 483 |
} else if (val < 0xF0) { |
| 484 |
/* 1110nnnn nnnnnnnn nnnnnnnn nnnnnnnn (28 bits) */ |
| 485 |
val = mach_read_from_4(*b) & 0xFFFFFFF; |
| 486 |
ut_ad(val > 0x1FFFFF); |
| 487 |
*b += 4; |
| 488 |
} else if (val < 0xF8) { |
| 489 |
/* 11110000 nnnnnnnn nnnnnnnn nnnnnnnn nnnnnnnn (32 bits) */ |
| 490 |
ut_ad(val == 0xF0); |
| 491 |
val = mach_read_from_4(*b + 1); |
| 492 |
/* this can treat not-extended format also. */ |
| 493 |
ut_ad(val > 0xFFFFFFF); |
| 494 |
*b += 5; |
| 495 |
} else if (val < 0xFC) { |
| 496 |
/* 111110nn nnnnnnnn (10 bits) (extended) */ |
| 497 |
val = (mach_read_from_2(*b) & 0x3FF) | 0xFFFFFC00; |
| 498 |
*b += 2; |
| 499 |
} else if (val < 0xFE) { |
| 500 |
/* 1111110n nnnnnnnn nnnnnnnn (17 bits) (extended) */ |
| 501 |
val = (mach_read_from_3(*b) & 0x1FFFF) | 0xFFFE0000; |
| 502 |
ut_ad(val < 0xFFFFFC00); |
| 503 |
*b += 3; |
| 504 |
} else if (val == 0xFE) { |
| 505 |
/* 11111110 nnnnnnnn nnnnnnnn nnnnnnnn (24 bits) (extended) */ |
| 506 |
ut_ad(val == 0xFE); |
| 507 |
val = mach_read_from_3(*b + 1) | 0xFF000000; |
| 508 |
ut_ad(val < 0xFFFE0000); |
| 509 |
*b += 4; |
| 510 |
} else { |
| 511 |
/* 11111111 followed by up to 80 bits */ |
| 512 |
ut_ad(val == 0xFF); |
| 513 |
++*b; |
| 514 |
val = mach_read_next_compressed(b); |
| 515 |
ut_ad(val > 0); |
| 516 |
val <<= 32; |
| 517 |
val |= mach_read_next_compressed(b); |
| 518 |
} |
| 519 |
|
| 520 |
return (val); |
| 521 |
} |
| 522 |
|
| 523 |
/** Read a 64-bit integer in a compressed form. |
| 524 |
@param[in,out] ptr pointer to memory where to read; |
| 525 |
advanced by the number of bytes consumed, or set NULL if out of space |
| 526 |
@param[in] end_ptr end of the buffer |
| 527 |
@return unsigned value */ |
| 528 |
static inline uint64_t mach_u64_parse_compressed(const byte **ptr, |
| 529 |
const byte *end_ptr) { |
| 530 |
uint64_t val = 0; |
| 531 |
|
| 532 |
if (end_ptr < *ptr + 5) { |
| 533 |
*ptr = nullptr; |
| 534 |
return (val); |
| 535 |
} |
| 536 |
|
| 537 |
val = mach_read_next_compressed(ptr); |
| 538 |
|
| 539 |
if (end_ptr < *ptr + 4) { |
| 540 |
*ptr = nullptr; |
| 541 |
return (val); |
| 542 |
} |
| 543 |
|
| 544 |
val <<= 32; |
| 545 |
val |= mach_read_from_4(*ptr); |
| 546 |
*ptr += 4; |
| 547 |
|
| 548 |
return (val); |
| 549 |
} |
| 550 |
/** Reads a double. It is stored in a little-endian format. |
| 551 |
@return double read */ |
| 552 |
static inline double mach_double_read( |
| 553 |
const byte *b) /*!< in: pointer to memory from where to read */ |
| 554 |
{ |
| 555 |
double d; |
| 556 |
ulint i; |
| 557 |
byte *ptr; |
| 558 |
|
| 559 |
ptr = (byte *)&d; |
| 560 |
|
| 561 |
for (i = 0; i < sizeof(double); i++) { |
| 562 |
#ifdef WORDS_BIGENDIAN |
| 563 |
ptr[sizeof(double) - i - 1] = b[i]; |
| 564 |
#else |
| 565 |
ptr[i] = b[i]; |
| 566 |
#endif |
| 567 |
} |
| 568 |
|
| 569 |
return (d); |
| 570 |
} |
| 571 |
|
| 572 |
/** Writes a double. It is stored in a little-endian format. */ |
| 573 |
static inline void mach_double_write( |
| 574 |
byte *b, /*!< in: pointer to memory where to write */ |
| 575 |
double d) /*!< in: double */ |
| 576 |
{ |
| 577 |
ulint i; |
| 578 |
byte *ptr; |
| 579 |
|
| 580 |
ptr = (byte *)&d; |
| 581 |
|
| 582 |
for (i = 0; i < sizeof(double); i++) { |
| 583 |
#ifdef WORDS_BIGENDIAN |
| 584 |
b[i] = ptr[sizeof(double) - i - 1]; |
| 585 |
#else |
| 586 |
b[i] = ptr[i]; |
| 587 |
#endif |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
/** Reads a float. It is stored in a little-endian format. |
| 592 |
@return float read */ |
| 593 |
static inline float mach_float_read( |
| 594 |
const byte *b) /*!< in: pointer to memory from where to read */ |
| 595 |
{ |
| 596 |
float d; |
| 597 |
ulint i; |
| 598 |
byte *ptr; |
| 599 |
|
| 600 |
ptr = (byte *)&d; |
| 601 |
|
| 602 |
for (i = 0; i < sizeof(float); i++) { |
| 603 |
#ifdef WORDS_BIGENDIAN |
| 604 |
ptr[sizeof(float) - i - 1] = b[i]; |
| 605 |
#else |
| 606 |
ptr[i] = b[i]; |
| 607 |
#endif |
| 608 |
} |
| 609 |
|
| 610 |
return (d); |
| 611 |
} |
| 612 |
|
| 613 |
/** Writes a float. It is stored in a little-endian format. */ |
| 614 |
static inline void mach_float_write( |
| 615 |
byte *b, /*!< in: pointer to memory where to write */ |
| 616 |
float d) /*!< in: float */ |
| 617 |
{ |
| 618 |
ulint i; |
| 619 |
byte *ptr; |
| 620 |
|
| 621 |
ptr = (byte *)&d; |
| 622 |
|
| 623 |
for (i = 0; i < sizeof(float); i++) { |
| 624 |
#ifdef WORDS_BIGENDIAN |
| 625 |
b[i] = ptr[sizeof(float) - i - 1]; |
| 626 |
#else |
| 627 |
b[i] = ptr[i]; |
| 628 |
#endif |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
#ifndef UNIV_HOTBACKUP |
| 633 |
static inline ulint mach_read_from_n_little_endian(const byte *buf, |
| 634 |
ulint buf_size) { |
| 635 |
ulint n = 0; |
| 636 |
const byte *ptr; |
| 637 |
|
| 638 |
ut_ad(buf_size > 0); |
| 639 |
|
| 640 |
ptr = buf + buf_size; |
| 641 |
|
| 642 |
for (;;) { |
| 643 |
ptr--; |
| 644 |
|
| 645 |
n = n << 8; |
| 646 |
|
| 647 |
n += (ulint)(*ptr); |
| 648 |
|
| 649 |
if (ptr == buf) { |
| 650 |
break; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
return (n); |
| 655 |
} |
| 656 |
|
| 657 |
/** Writes a ulint in the little-endian format. */ |
| 658 |
static inline void mach_write_to_n_little_endian( |
| 659 |
byte *dest, /*!< in: where to write */ |
| 660 |
ulint dest_size, /*!< in: into how many bytes to write */ |
| 661 |
ulint n) /*!< in: unsigned long int to write */ |
| 662 |
{ |
| 663 |
byte *end; |
| 664 |
|
| 665 |
ut_ad(dest_size <= sizeof(ulint)); |
| 666 |
ut_ad(dest_size > 0); |
| 667 |
|
| 668 |
end = dest + dest_size; |
| 669 |
|
| 670 |
for (;;) { |
| 671 |
*dest = (byte)(n & 0xFF); |
| 672 |
|
| 673 |
n = n >> 8; |
| 674 |
|
| 675 |
dest++; |
| 676 |
|
| 677 |
if (dest == end) { |
| 678 |
break; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
ut_ad(n == 0); |
| 683 |
} |
| 684 |
|
| 685 |
/** Reads a ulint stored in the little-endian format. |
| 686 |
@return unsigned long int */ |
| 687 |
static inline ulint mach_read_from_2_little_endian( |
| 688 |
const byte *buf) /*!< in: from where to read */ |
| 689 |
{ |
| 690 |
return ((ulint)(buf[0]) | ((ulint)(buf[1]) << 8)); |
| 691 |
} |
| 692 |
|
| 693 |
/** Writes a ulint in the little-endian format. */ |
| 694 |
static inline void mach_write_to_2_little_endian( |
| 695 |
byte *dest, /*!< in: where to write */ |
| 696 |
ulint n) /*!< in: unsigned long int to write */ |
| 697 |
{ |
| 698 |
ut_ad(n < 256 * 256); |
| 699 |
|
| 700 |
*dest = (byte)(n & 0xFFUL); |
| 701 |
|
| 702 |
n = n >> 8; |
| 703 |
dest++; |
| 704 |
|
| 705 |
*dest = (byte)(n & 0xFFUL); |
| 706 |
} |
| 707 |
#endif /* !UNIV_HOTBACKUP */ |
| 708 |
|
| 709 |
/** Convert integral type from storage byte order (big endian) to |
| 710 |
host byte order. |
| 711 |
@return integer value */ |
| 712 |
static inline uint64_t mach_read_int_type( |
| 713 |
const byte *src, /*!< in: where to read from */ |
| 714 |
ulint len, /*!< in: length of src */ |
| 715 |
bool unsigned_type) /*!< in: signed or unsigned flag */ |
| 716 |
{ |
| 717 |
/* XXX this can be optimized on big-endian machines */ |
| 718 |
|
| 719 |
uintmax_t ret; |
| 720 |
uint i; |
| 721 |
|
| 722 |
if (unsigned_type || (src[0] & 0x80)) { |
| 723 |
ret = 0x0000000000000000ULL; |
| 724 |
} else { |
| 725 |
ret = 0xFFFFFFFFFFFFFF00ULL; |
| 726 |
} |
| 727 |
|
| 728 |
if (unsigned_type) { |
| 729 |
ret |= src[0]; |
| 730 |
} else { |
| 731 |
ret |= src[0] ^ 0x80; |
| 732 |
} |
| 733 |
|
| 734 |
for (i = 1; i < len; i++) { |
| 735 |
ret <<= 8; |
| 736 |
ret |= src[i]; |
| 737 |
} |
| 738 |
|
| 739 |
return (ret); |
| 740 |
} |
| 741 |
#ifndef UNIV_HOTBACKUP |
| 742 |
/** Swap byte ordering. */ |
| 743 |
static inline void mach_swap_byte_order( |
| 744 |
byte *dest, /*!< out: where to write */ |
| 745 |
const byte *from, /*!< in: where to read from */ |
| 746 |
ulint len) /*!< in: length of src */ |
| 747 |
{ |
| 748 |
ut_ad(len > 0); |
| 749 |
ut_ad(len <= 8); |
| 750 |
|
| 751 |
dest += len; |
| 752 |
|
| 753 |
switch (len & 0x7) { |
| 754 |
case 0: |
| 755 |
*--dest = *from++; |
| 756 |
[[fallthrough]]; |
| 757 |
case 7: |
| 758 |
*--dest = *from++; |
| 759 |
[[fallthrough]]; |
| 760 |
case 6: |
| 761 |
*--dest = *from++; |
| 762 |
[[fallthrough]]; |
| 763 |
case 5: |
| 764 |
*--dest = *from++; |
| 765 |
[[fallthrough]]; |
| 766 |
case 4: |
| 767 |
*--dest = *from++; |
| 768 |
[[fallthrough]]; |
| 769 |
case 3: |
| 770 |
*--dest = *from++; |
| 771 |
[[fallthrough]]; |
| 772 |
case 2: |
| 773 |
*--dest = *from++; |
| 774 |
[[fallthrough]]; |
| 775 |
case 1: |
| 776 |
*--dest = *from; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
/************************************************************* |
| 781 |
Convert integral type from host byte order (big-endian) storage |
| 782 |
byte order. */ |
| 783 |
static inline void mach_write_int_type( |
| 784 |
byte *dest, /*!< in: where to write */ |
| 785 |
const byte *src, /*!< in: where to read from */ |
| 786 |
ulint len, /*!< in: length of src */ |
| 787 |
bool usign) /*!< in: signed or unsigned flag */ |
| 788 |
{ |
| 789 |
ut_ad(len >= 1 && len <= 8); |
| 790 |
|
| 791 |
#ifdef WORDS_BIGENDIAN |
| 792 |
memcpy(dest, src, len); |
| 793 |
#else |
| 794 |
mach_swap_byte_order(dest, src, len); |
| 795 |
#endif /* WORDS_BIGENDIAN */ |
| 796 |
|
| 797 |
if (!usign) { |
| 798 |
*dest ^= 0x80; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
/************************************************************* |
| 803 |
Convert a ulonglong integer from host byte order to (big-endian) |
| 804 |
storage byte order. */ |
| 805 |
static inline void mach_write_ulonglong( |
| 806 |
byte *dest, /*!< in: where to write */ |
| 807 |
ulonglong src, /*!< in: where to read from */ |
| 808 |
ulint len, /*!< in: length of dest */ |
| 809 |
bool usign) /*!< in: signed or unsigned flag */ |
| 810 |
{ |
| 811 |
byte *ptr = reinterpret_cast<byte *>(&src); |
| 812 |
|
| 813 |
ut_ad(len <= sizeof(ulonglong)); |
| 814 |
|
| 815 |
#ifdef WORDS_BIGENDIAN |
| 816 |
memcpy(dest, ptr + (sizeof(src) - len), len); |
| 817 |
#else |
| 818 |
mach_swap_byte_order(dest, reinterpret_cast<byte *>(ptr), len); |
| 819 |
#endif /* WORDS_BIGENDIAN */ |
| 820 |
|
| 821 |
if (!usign) { |
| 822 |
*dest ^= 0x80; |
| 823 |
} |
| 824 |
} |
| 825 |
#endif /* !UNIV_HOTBACKUP */ |
| 826 |
|
| 827 |
/** Read 1 to 4 bytes from a file page buffered in the buffer pool. |
| 828 |
@param[in] ptr pointer where to read |
| 829 |
@param[in] type MLOG_1BYTE, MLOG_2BYTES, or MLOG_4BYTES |
| 830 |
@return value read */ |
| 831 |
static inline uint32_t mach_read_ulint(const byte *ptr, mlog_id_t type) { |
| 832 |
switch (type) { |
| 833 |
case MLOG_1BYTE: |
| 834 |
return (mach_read_from_1(ptr)); |
| 835 |
case MLOG_2BYTES: |
| 836 |
return (mach_read_from_2(ptr)); |
| 837 |
case MLOG_4BYTES: |
| 838 |
return (mach_read_from_4(ptr)); |
| 839 |
default: |
| 840 |
break; |
| 841 |
} |
| 842 |
|
| 843 |
ut_error; |
| 844 |
} |