| 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.h |
| 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 |
#ifndef mach0data_h |
| 36 |
#define mach0data_h |
| 37 |
|
| 38 |
#include "mtr0types.h" |
| 39 |
#include "univ.i" |
| 40 |
|
| 41 |
#ifdef UNIV_HOTBACKUP |
| 42 |
#include "ut0byte.h" |
| 43 |
#endif /* UNIV_HOTBACKUP */ |
| 44 |
|
| 45 |
/* The data and all fields are always stored in a database file |
| 46 |
in the same format: ascii, big-endian, ... . |
| 47 |
All data in the files MUST be accessed using the functions in this |
| 48 |
module. */ |
| 49 |
|
| 50 |
/** The following function is used to store data in one byte. |
| 51 |
@param[in] b pointer to byte where to store |
| 52 |
@param[in] n One byte integer to be stored, >= 0, < 256 */ |
| 53 |
static inline void mach_write_to_1(byte *b, ulint n); |
| 54 |
|
| 55 |
/** The following function is used to fetch data from one byte. |
| 56 |
@param[in] b pointer to a byte to read |
| 57 |
@return ulint integer, >= 0, < 256 */ |
| 58 |
[[nodiscard]] static inline uint8_t mach_read_from_1(const byte *b); |
| 59 |
|
| 60 |
/** The following function is used to store data in two consecutive bytes. We |
| 61 |
store the most significant byte to the lower address. |
| 62 |
@param[in] b pointer to 2 bytes where to store |
| 63 |
@param[in] n 2-byte integer to be stored, >= 0, < 64k */ |
| 64 |
static inline void mach_write_to_2(byte *b, ulint n); |
| 65 |
|
| 66 |
/** The following function is used to fetch data from 2 consecutive |
| 67 |
bytes. The most significant byte is at the lowest address. |
| 68 |
@param[in] b pointer to 2 bytes where to store |
| 69 |
@return 2-byte integer, >= 0, < 64k */ |
| 70 |
[[nodiscard]] static inline uint16_t mach_read_from_2(const byte *b); |
| 71 |
|
| 72 |
/** The following function is used to convert a 16-bit data item to the |
| 73 |
canonical format, for fast bytewise equality test against memory. |
| 74 |
@param[in] n integer in machine-dependent format |
| 75 |
@return 16-bit integer in canonical format */ |
| 76 |
static inline uint16_t mach_encode_2(ulint n); |
| 77 |
|
| 78 |
/** The following function is used to convert a 16-bit data item from the |
| 79 |
canonical format, for fast bytewise equality test against memory. |
| 80 |
@param[in] n 16-bit integer in canonical format |
| 81 |
@return integer in machine-dependent format */ |
| 82 |
static inline ulint mach_decode_2(uint16 n); |
| 83 |
|
| 84 |
/** The following function is used to store data in 3 consecutive |
| 85 |
bytes. We store the most significant byte to the lowest address. |
| 86 |
@param[in] b pointer to 3 bytes where to store |
| 87 |
@param[in] n 3 byte integer to be stored */ |
| 88 |
static inline void mach_write_to_3(byte *b, ulint n); |
| 89 |
|
| 90 |
/** The following function is used to fetch data from 3 consecutive |
| 91 |
bytes. The most significant byte is at the lowest address. |
| 92 |
@param[in] b pointer to 3 bytes to read |
| 93 |
@return 32 bit integer */ |
| 94 |
[[nodiscard]] static inline uint32_t mach_read_from_3(const byte *b); |
| 95 |
|
| 96 |
/** The following function is used to store data in 4 consecutive |
| 97 |
bytes. We store the most significant byte to the lowest address. |
| 98 |
@param[in] b pointer to 4 bytes where to store |
| 99 |
@param[in] n 4 byte integer to be stored */ |
| 100 |
static inline void mach_write_to_4(byte *b, ulint n); |
| 101 |
|
| 102 |
/** The following function is used to fetch data from 4 consecutive |
| 103 |
bytes. The most significant byte is at the lowest address. |
| 104 |
@param[in] b pointer to 4 bytes to read |
| 105 |
@return 32 bit integer */ |
| 106 |
[[nodiscard]] static inline uint32_t mach_read_from_4(const byte *b); |
| 107 |
|
| 108 |
/** Write a ulint in a compressed form (1..5 bytes). |
| 109 |
@param[in] b pointer to memory where to store |
| 110 |
@param[in] n ulint integer to be stored |
| 111 |
@return stored size in bytes */ |
| 112 |
static inline ulint mach_write_compressed(byte *b, ulint n); |
| 113 |
|
| 114 |
/** Return the size of an ulint when written in the compressed form. |
| 115 |
@param[in] n ulint integer to be stored |
| 116 |
@return compressed size in bytes */ |
| 117 |
static inline uint32_t mach_get_compressed_size(ulint n); |
| 118 |
|
| 119 |
/** Read a 32-bit integer in a compressed form. |
| 120 |
@param[in,out] b pointer to memory where to read; |
| 121 |
advanced by the number of bytes consumed |
| 122 |
@return unsigned value */ |
| 123 |
static inline uint32_t mach_read_next_compressed(const byte **b); |
| 124 |
|
| 125 |
/** The following function is used to store data in 6 consecutive |
| 126 |
bytes. We store the most significant byte to the lowest address. |
| 127 |
@param[in] b pointer to 6 bytes where to store |
| 128 |
@param[in] id 48-bit integer to write */ |
| 129 |
static inline void mach_write_to_6(byte *b, uint64_t id); |
| 130 |
|
| 131 |
/** The following function is used to fetch data from 6 consecutive |
| 132 |
bytes. The most significant byte is at the lowest address. |
| 133 |
@param[in] b pointer to 6 bytes to read |
| 134 |
@return 48-bit integer */ |
| 135 |
[[nodiscard]] static inline uint64_t mach_read_from_6(const byte *b); |
| 136 |
|
| 137 |
/** The following function is used to store data in 7 consecutive |
| 138 |
bytes. We store the most significant byte to the lowest address. |
| 139 |
@param[in] b pointer to 7 bytes where to store |
| 140 |
@param[in] n 56-bit integer */ |
| 141 |
static inline void mach_write_to_7(byte *b, uint64_t n); |
| 142 |
|
| 143 |
/** The following function is used to fetch data from 7 consecutive |
| 144 |
bytes. The most significant byte is at the lowest address. |
| 145 |
@param[in] b pointer to 7 bytes to read |
| 146 |
@return 56-bit integer */ |
| 147 |
[[nodiscard]] static inline uint64_t mach_read_from_7(const byte *b); |
| 148 |
|
| 149 |
/** The following function is used to store data in 8 consecutive |
| 150 |
bytes. We store the most significant byte to the lowest address. |
| 151 |
@param[in] b pointer to 8 bytes where to store |
| 152 |
@param[in] n 64-bit integer to be stored */ |
| 153 |
static inline void mach_write_to_8(void *b, uint64_t n); |
| 154 |
|
| 155 |
/** The following function is used to fetch data from 8 consecutive |
| 156 |
bytes. The most significant byte is at the lowest address. |
| 157 |
@param[in] b pointer to 8 bytes to read |
| 158 |
@return 64-bit integer */ |
| 159 |
[[nodiscard]] static inline uint64_t mach_read_from_8(const byte *b); |
| 160 |
|
| 161 |
/** Writes a 64-bit integer in a compressed form (5..9 bytes). |
| 162 |
@param[in] b pointer to memory where to store |
| 163 |
@param[in] n 64-bit integer to be stored |
| 164 |
@return size in bytes */ |
| 165 |
static inline ulint mach_u64_write_compressed(byte *b, uint64_t n); |
| 166 |
|
| 167 |
/** Read a 64-bit integer in a compressed form. |
| 168 |
@param[in,out] b pointer to memory where to read; |
| 169 |
advanced by the number of bytes consumed |
| 170 |
@return unsigned value */ |
| 171 |
static inline uint64_t mach_u64_read_next_compressed(const byte **b); |
| 172 |
|
| 173 |
/** Writes a 64-bit integer in a compressed form (1..11 bytes). |
| 174 |
@param[in] b pointer to memory where to store |
| 175 |
@param[in] n 64-bit integer to be stored |
| 176 |
@return size in bytes */ |
| 177 |
static inline ulint mach_u64_write_much_compressed(byte *b, uint64_t n); |
| 178 |
|
| 179 |
/** Reads a 64-bit integer in a compressed form. |
| 180 |
@param[in] b pointer to memory from where to read |
| 181 |
@return the value read */ |
| 182 |
[[nodiscard]] static inline uint64_t mach_u64_read_much_compressed( |
| 183 |
const byte *b); |
| 184 |
|
| 185 |
/** Read a 64-bit integer in a much compressed form. |
| 186 |
@param[in,out] ptr pointer to memory from where to read, |
| 187 |
advanced by the number of bytes consumed, or set NULL if out of space |
| 188 |
@param[in] end_ptr end of the buffer |
| 189 |
@return unsigned 64-bit integer */ |
| 190 |
uint64_t mach_parse_u64_much_compressed(const byte **ptr, const byte *end_ptr); |
| 191 |
|
| 192 |
/** Read a 32-bit integer in a compressed form. |
| 193 |
@param[in,out] ptr pointer to memory from where to read; |
| 194 |
advanced by the number of bytes consumed, or set NULL if out of space |
| 195 |
@param[in] end_ptr end of the buffer |
| 196 |
@return unsigned value */ |
| 197 |
uint32_t mach_parse_compressed(const byte **ptr, const byte *end_ptr); |
| 198 |
|
| 199 |
/** Read a 64-bit integer in a compressed form. |
| 200 |
@param[in,out] ptr pointer to memory from where to read; |
| 201 |
advanced by the number of bytes consumed, or set NULL if out of space |
| 202 |
@param[in] end_ptr end of the buffer |
| 203 |
@return unsigned value */ |
| 204 |
static inline uint64_t mach_u64_parse_compressed(const byte **ptr, |
| 205 |
const byte *end_ptr); |
| 206 |
|
| 207 |
/** Reads a double. It is stored in a little-endian format. |
| 208 |
@return double read */ |
| 209 |
[[nodiscard]] static inline double mach_double_read( |
| 210 |
const byte *b); /*!< in: pointer to memory from where to read */ |
| 211 |
|
| 212 |
/** Writes a double. It is stored in a little-endian format. |
| 213 |
@param[in] b pointer to memory where to write |
| 214 |
@param[in] d double */ |
| 215 |
static inline void mach_double_write(byte *b, double d); |
| 216 |
|
| 217 |
/** Reads a float. It is stored in a little-endian format. |
| 218 |
@return float read */ |
| 219 |
[[nodiscard]] static inline float mach_float_read( |
| 220 |
const byte *b); /*!< in: pointer to memory from where to read */ |
| 221 |
|
| 222 |
/** Writes a float. It is stored in a little-endian format. |
| 223 |
@param[in] b pointer to memory where to write |
| 224 |
@param[in] d float */ |
| 225 |
static inline void mach_float_write(byte *b, float d); |
| 226 |
|
| 227 |
#ifndef UNIV_HOTBACKUP |
| 228 |
/** Reads a ulint stored in the little-endian format. |
| 229 |
@param[in] buf From where to read. |
| 230 |
@param[in] buf_size From how many bytes to read. |
| 231 |
@return unsigned long int */ |
| 232 |
[[nodiscard]] static inline ulint mach_read_from_n_little_endian( |
| 233 |
const byte *buf, ulint buf_size); |
| 234 |
|
| 235 |
/** Writes a ulint in the little-endian format. |
| 236 |
@param[in] dest where to write |
| 237 |
@param[in] dest_size into how many bytes to write |
| 238 |
@param[in] n unsigned long int to write */ |
| 239 |
static inline void mach_write_to_n_little_endian(byte *dest, ulint dest_size, |
| 240 |
ulint n); |
| 241 |
|
| 242 |
/** Reads a ulint stored in the little-endian format. |
| 243 |
@return unsigned long int */ |
| 244 |
[[nodiscard]] static inline ulint mach_read_from_2_little_endian( |
| 245 |
const byte *buf); /*!< in: from where to read */ |
| 246 |
|
| 247 |
/** Writes a ulint in the little-endian format. |
| 248 |
@param[in] dest where to write |
| 249 |
@param[in] n unsigned long int to write */ |
| 250 |
static inline void mach_write_to_2_little_endian(byte *dest, ulint n); |
| 251 |
|
| 252 |
/** Convert integral type from storage byte order (big endian) to host byte |
| 253 |
order. |
| 254 |
@param[in] src where to read from |
| 255 |
@param[in] len length of src |
| 256 |
@param[in] unsigned_type signed or unsigned flag |
| 257 |
@return integer value */ |
| 258 |
static inline uint64_t mach_read_int_type(const byte *src, ulint len, |
| 259 |
bool unsigned_type); |
| 260 |
|
| 261 |
/** Convert integral type from host byte order to (big-endian) storage |
| 262 |
byte order. |
| 263 |
@param[in] dest where to write |
| 264 |
@param[in] src where to read |
| 265 |
@param[in] len length of src |
| 266 |
@param[in] usign signed or unsigned flag */ |
| 267 |
static inline void mach_write_int_type(byte *dest, const byte *src, ulint len, |
| 268 |
bool usign); |
| 269 |
|
| 270 |
/** Convert a ulonglong integer from host byte order to (big-endian) storage |
| 271 |
byte order. |
| 272 |
@param[in] dest where to write |
| 273 |
@param[in] src where to read from |
| 274 |
@param[in] len length of dest |
| 275 |
@param[in] usign signed or unsigned flag */ |
| 276 |
static inline void mach_write_ulonglong(byte *dest, ulonglong src, ulint len, |
| 277 |
bool usign); |
| 278 |
|
| 279 |
#endif |
| 280 |
|
| 281 |
/** Read 1 to 4 bytes from a file page buffered in the buffer pool. |
| 282 |
@param[in] ptr pointer where to read |
| 283 |
@param[in] type MLOG_1BYTE, MLOG_2BYTES, or MLOG_4BYTES |
| 284 |
@return value read */ |
| 285 |
[[nodiscard]] static inline uint32_t mach_read_ulint(const byte *ptr, |
| 286 |
mlog_id_t type); |
| 287 |
|
| 288 |
#include "mach0data.ic" |
| 289 |
|
| 290 |
#endif |