1 /***************************************************************************** 2 3 Copyright (c) 1995, 2020, Oracle and/or its affiliates. All Rights Reserved. 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 also distributed with certain software (including but not 10 limited to OpenSSL) that is licensed under separate terms, as designated in a 11 particular file or component or in included license documentation. The authors 12 of MySQL hereby grant you an additional permission to link the program and 13 your derivative works with the separately licensed software that they have 14 included with MySQL. 15 16 This program is distributed in the hope that it will be useful, but WITHOUT 17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, 19 for more details. 20 21 You should have received a copy of the GNU General Public License along with 22 this program; if not, write to the Free Software Foundation, Inc., 23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 25 *****************************************************************************/ 26 27 /** @file include/mach0data.h 28 Utilities for converting data from the database file 29 to the machine format. 30 31 Created 11/28/1995 Heikki Tuuri 32 ***********************************************************************/ 33 34 #ifndef mach0data_h 35 #define mach0data_h 36 37 #include "mtr0types.h" 38 #include "univ.i" 39 40 #ifdef UNIV_HOTBACKUP 41 #include "ut0byte.h" 42 #endif /* UNIV_HOTBACKUP */ 43 44 /* The data and all fields are always stored in a database file 45 in the same format: ascii, big-endian, ... . 46 All data in the files MUST be accessed using the functions in this 47 module. */ 48 49 /** The following function is used to store data in one byte. 50 @param[in] b pointer to byte where to store 51 @param[in] n One byte integer to be stored, >= 0, < 256 */ 52 UNIV_INLINE 53 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 UNIV_INLINE 59 uint8_t mach_read_from_1(const byte *b) MY_ATTRIBUTE((warn_unused_result)); 60 61 /** The following function is used to store data in two consecutive bytes. We 62 store the most significant byte to the lower address. 63 @param[in] b pointer to 2 bytes where to store 64 @param[in] n 2-byte integer to be stored, >= 0, < 64k */ 65 UNIV_INLINE 66 void mach_write_to_2(byte *b, ulint n); 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 where to store 71 @return 2-byte integer, >= 0, < 64k */ 72 UNIV_INLINE 73 uint16_t mach_read_from_2(const byte *b) MY_ATTRIBUTE((warn_unused_result)); 74 75 /** The following function is used to convert a 16-bit data item to the 76 canonical format, for fast bytewise equality test against memory. 77 @param[in] n integer in machine-dependent format 78 @return 16-bit integer in canonical format */ 79 UNIV_INLINE 80 uint16_t mach_encode_2(ulint n); 81 82 /** The following function is used to convert a 16-bit data item from the 83 canonical format, for fast bytewise equality test against memory. 84 @param[in] n 16-bit integer in canonical format 85 @return integer in machine-dependent format */ 86 UNIV_INLINE 87 ulint mach_decode_2(uint16 n); 88 89 /** The following function is used to store data in 3 consecutive 90 bytes. We store the most significant byte to the lowest address. 91 @param[in] b pointer to 3 bytes where to store 92 @param[in] n 3 byte integer to be stored */ 93 UNIV_INLINE 94 void mach_write_to_3(byte *b, ulint n); 95 96 /** The following function is used to fetch data from 3 consecutive 97 bytes. The most significant byte is at the lowest address. 98 @param[in] b pointer to 3 bytes to read 99 @return 32 bit integer */ 100 UNIV_INLINE 101 uint32_t mach_read_from_3(const byte *b) MY_ATTRIBUTE((warn_unused_result)); 102 103 /** The following function is used to store data in 4 consecutive 104 bytes. We store the most significant byte to the lowest address. 105 @param[in] b pointer to 4 bytes where to store 106 @param[in] n 4 byte integer to be stored */ 107 UNIV_INLINE 108 void mach_write_to_4(byte *b, ulint n); 109 110 /** The following function is used to fetch data from 4 consecutive 111 bytes. The most significant byte is at the lowest address. 112 @param[in] b pointer to 4 bytes to read 113 @return 32 bit integer */ 114 UNIV_INLINE 115 uint32_t mach_read_from_4(const byte *b) MY_ATTRIBUTE((warn_unused_result)); 116 117 /** Write a ulint in a compressed form (1..5 bytes). 118 @param[in] b pointer to memory where to store 119 @param[in] n ulint integer to be stored 120 @return stored size in bytes */ 121 UNIV_INLINE 122 ulint mach_write_compressed(byte *b, ulint n); 123 124 /** Return the size of an ulint when written in the compressed form. 125 @param[in] n ulint integer to be stored 126 @return compressed size in bytes */ 127 UNIV_INLINE 128 uint32_t mach_get_compressed_size(ulint n); 129 130 /** Read a 32-bit integer in a compressed form. 131 @param[in,out] b pointer to memory where to read; 132 advanced by the number of bytes consumed 133 @return unsigned value */ 134 UNIV_INLINE 135 ib_uint32_t mach_read_next_compressed(const byte **b); 136 137 /** The following function is used to store data in 6 consecutive 138 bytes. We store the most significant byte to the lowest address. 139 @param[in] b pointer to 6 bytes where to store 140 @param[in] id 48-bit integer to write */ 141 UNIV_INLINE 142 void mach_write_to_6(byte *b, ib_uint64_t id); 143 144 /** The following function is used to fetch data from 6 consecutive 145 bytes. The most significant byte is at the lowest address. 146 @param[in] b pointer to 6 bytes to read 147 @return 48-bit integer */ 148 UNIV_INLINE 149 ib_uint64_t mach_read_from_6(const byte *b) MY_ATTRIBUTE((warn_unused_result)); 150 151 /** The following function is used to store data in 7 consecutive 152 bytes. We store the most significant byte to the lowest address. 153 @param[in] b pointer to 7 bytes where to store 154 @param[in] n 56-bit integer */ 155 UNIV_INLINE 156 void mach_write_to_7(byte *b, ib_uint64_t n); 157 158 /** The following function is used to fetch data from 7 consecutive 159 bytes. The most significant byte is at the lowest address. 160 @param[in] b pointer to 7 bytes to read 161 @return 56-bit integer */ 162 UNIV_INLINE 163 ib_uint64_t mach_read_from_7(const byte *b) MY_ATTRIBUTE((warn_unused_result)); 164 165 /** The following function is used to store data in 8 consecutive 166 bytes. We store the most significant byte to the lowest address. 167 @param[in] b pointer to 8 bytes where to store 168 @param[in] n 64-bit integer to be stored */ 169 UNIV_INLINE 170 void mach_write_to_8(void *b, ib_uint64_t n); 171 172 /** The following function is used to fetch data from 8 consecutive 173 bytes. The most significant byte is at the lowest address. 174 @param[in] b pointer to 8 bytes to read 175 @return 64-bit integer */ 176 UNIV_INLINE 177 ib_uint64_t mach_read_from_8(const byte *b) MY_ATTRIBUTE((warn_unused_result)); 178 179 /** Writes a 64-bit integer in a compressed form (5..9 bytes). 180 @param[in] b pointer to memory where to store 181 @param[in] n 64-bit integer to be stored 182 @return size in bytes */ 183 UNIV_INLINE 184 ulint mach_u64_write_compressed(byte *b, ib_uint64_t n); 185 186 /** Read a 64-bit integer in a compressed form. 187 @param[in,out] b pointer to memory where to read; 188 advanced by the number of bytes consumed 189 @return unsigned value */ 190 UNIV_INLINE 191 ib_uint64_t mach_u64_read_next_compressed(const byte **b); 192 193 /** Writes a 64-bit integer in a compressed form (1..11 bytes). 194 @param[in] b pointer to memory where to store 195 @param[in] n 64-bit integer to be stored 196 @return size in bytes */ 197 UNIV_INLINE 198 ulint mach_u64_write_much_compressed(byte *b, ib_uint64_t n); 199 200 /** Reads a 64-bit integer in a compressed form. 201 @param[in] b pointer to memory from where to read 202 @return the value read */ 203 UNIV_INLINE 204 ib_uint64_t mach_u64_read_much_compressed(const byte *b) 205 MY_ATTRIBUTE((warn_unused_result)); 206 207 /** Read a 64-bit integer in a much compressed form. 208 @param[in,out] ptr pointer to memory from where to read, 209 advanced by the number of bytes consumed, or set NULL if out of space 210 @param[in] end_ptr end of the buffer 211 @return unsigned 64-bit integer */ 212 ib_uint64_t mach_parse_u64_much_compressed(const byte **ptr, 213 const byte *end_ptr); 214 215 /** Read a 32-bit integer in a compressed form. 216 @param[in,out] ptr pointer to memory from where to read; 217 advanced by the number of bytes consumed, or set NULL if out of space 218 @param[in] end_ptr end of the buffer 219 @return unsigned value */ 220 ib_uint32_t mach_parse_compressed(const byte **ptr, const byte *end_ptr); 221 /** Read a 64-bit integer in a compressed form. 222 @param[in,out] ptr pointer to memory from where to read; 223 advanced by the number of bytes consumed, or set NULL if out of space 224 @param[in] end_ptr end of the buffer 225 @return unsigned value */ 226 UNIV_INLINE 227 ib_uint64_t mach_u64_parse_compressed(const byte **ptr, const byte *end_ptr); 228 229 /** Reads a double. It is stored in a little-endian format. 230 @return double read */ 231 UNIV_INLINE 232 double mach_double_read( 233 const byte *b) /*!< in: pointer to memory from where to read */ 234 MY_ATTRIBUTE((warn_unused_result)); 235 236 /** Writes a double. It is stored in a little-endian format. 237 @param[in] b pointer to memory where to write 238 @param[in] d double */ 239 UNIV_INLINE 240 void mach_double_write(byte *b, double d); 241 242 /** Reads a float. It is stored in a little-endian format. 243 @return float read */ 244 UNIV_INLINE 245 float mach_float_read( 246 const byte *b) /*!< in: pointer to memory from where to read */ 247 MY_ATTRIBUTE((warn_unused_result)); 248 249 /** Writes a float. It is stored in a little-endian format. 250 @param[in] b pointer to memory where to write 251 @param[in] d float */ 252 UNIV_INLINE 253 void mach_float_write(byte *b, float d); 254 255 /** Reads a ulint stored in the little-endian format. 256 @return unsigned long int */ 257 UNIV_INLINE 258 ulint mach_read_from_n_little_endian( 259 const byte *buf, /*!< in: from where to read */ 260 ulint buf_size) /*!< in: from how many bytes to read */ 261 MY_ATTRIBUTE((warn_unused_result)); 262 263 /** Writes a ulint in the little-endian format. 264 @param[in] dest where to write 265 @param[in] dest_size into how many bytes to write 266 @param[in] n unsigned long int to write */ 267 UNIV_INLINE 268 void mach_write_to_n_little_endian(byte *dest, ulint dest_size, ulint n); 269 270 /** Reads a ulint stored in the little-endian format. 271 @return unsigned long int */ 272 UNIV_INLINE 273 ulint mach_read_from_2_little_endian( 274 const byte *buf) /*!< in: from where to read */ 275 MY_ATTRIBUTE((warn_unused_result)); 276 277 /** Writes a ulint in the little-endian format. 278 @param[in] dest where to write 279 @param[in] n unsigned long int to write */ 280 UNIV_INLINE 281 void mach_write_to_2_little_endian(byte *dest, ulint n); 282 283 /** Convert integral type from storage byte order (big endian) to host byte 284 order. 285 @param[in] src where to read from 286 @param[in] len length of src 287 @param[in] unsigned_type signed or unsigned flag 288 @return integer value */ 289 UNIV_INLINE 290 ib_uint64_t mach_read_int_type(const byte *src, ulint len, ibool unsigned_type); 291 292 /** Convert integral type from host byte order to (big-endian) storage 293 byte order. 294 @param[in] dest where to write 295 @param[in] src where to read 296 @param[in] len length of src 297 @param[in] usign signed or unsigned flag */ 298 UNIV_INLINE 299 void mach_write_int_type(byte *dest, const byte *src, ulint len, bool usign); 300 301 /** Convert a ulonglong integer from host byte order to (big-endian) storage 302 byte order. 303 @param[in] dest where to write 304 @param[in] src where to read from 305 @param[in] len length of dest 306 @param[in] usign signed or unsigned flag */ 307 UNIV_INLINE 308 void mach_write_ulonglong(byte *dest, ulonglong src, ulint len, bool usign); 309 310 /** Read 1 to 4 bytes from a file page buffered in the buffer pool. 311 @param[in] ptr pointer where to read 312 @param[in] type MLOG_1BYTE, MLOG_2BYTES, or MLOG_4BYTES 313 @return value read */ 314 UNIV_INLINE 315 uint32_t mach_read_ulint(const byte *ptr, mlog_id_t type) 316 MY_ATTRIBUTE((warn_unused_result)); 317 318 #include "mach0data.ic" 319 320 #endif 321