| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2016, 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 | #ifndef _mach0data_h_ |
| 28 | #define _mach0data_h_ |
| 29 | |
| 30 | #include "lot0types.h" |
| 31 | #include "mtr0types.h" |
| 32 | #include "ut0byte.h" |
| 33 | #include "ut0dbg.h" |
| 34 | |
| 35 | inline uint8_t mach_read_from_1(const byte *b) { |
| 36 | ut_ad(b != nullptr); |
| 37 | return ((uint8_t)(b[0])); |
| 38 | } |
| 39 | |
| 40 | inline uint16_t mach_read_from_2(const byte *b) { |
| 41 | return (((ulint)(b[0]) << 8) | (ulint)(b[1])); |
| 42 | } |
| 43 | |
| 44 | inline void mach_write_to_1(byte *b, ulint n) { b[0] = (byte)n; } |
| 45 | |
| 46 | inline void mach_write_to_2(byte *b, ulint n) { |
| 47 | b[0] = (byte)(n >> 8); |
| 48 | b[1] = (byte)(n); |
| 49 | } |
| 50 | |
| 51 | inline void mach_write_to_4(byte *b, ulint n) { |
| 52 | b[0] = (byte)(n >> 24); |
| 53 | b[1] = (byte)(n >> 16); |
| 54 | b[2] = (byte)(n >> 8); |
| 55 | b[3] = (byte)n; |
| 56 | } |
| 57 | |
| 58 | /** The following function is used to fetch data from 4 consecutive |
| 59 | bytes. The most significant byte is at the lowest address. |
| 60 | @param[in] b pointer to four bytes. |
| 61 | @return ulint integer */ |
| 62 | inline ulint mach_read_from_4(const byte *b) { |
| 63 | return (((ulint)(b[0]) << 24) | ((ulint)(b[1]) << 16) | ((ulint)(b[2]) << 8) | |
| 64 | (ulint)(b[3])); |
| 65 | } |
| 66 | |
| 67 | inline uint32_t mach_read_ulint(const byte *ptr, mlog_id_t type) { |
| 68 | switch (type) { |
| 69 | case MLOG_1BYTE: |
| 70 | return (mach_read_from_1(ptr)); |
| 71 | case MLOG_2BYTES: |
| 72 | return (mach_read_from_2(ptr)); |
| 73 | case MLOG_4BYTES: |
| 74 | return (mach_read_from_4(ptr)); |
| 75 | default: |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | ut_error; |
| 80 | return (0); |
| 81 | } |
| 82 | |
| 83 | /** The following function is used to fetch data from 6 consecutive |
| 84 | bytes. The most significant byte is at the lowest address. |
| 85 | @param[in] b pointer to 6 bytes to read |
| 86 | @return 48-bit integer */ |
| 87 | inline uint64_t mach_read_from_6(const byte *b) { |
| 88 | ut_ad(b != nullptr); |
| 89 | |
| 90 | return (ut_ull_create(mach_read_from_2(b), mach_read_from_4(b + 2))); |
| 91 | } |
| 92 | |
| 93 | /** The following function is used to store data in 6 consecutive |
| 94 | bytes. We store the most significant byte to the lowest address. |
| 95 | @param[in] b pointer to 6 bytes where to store |
| 96 | @param[in] n 48-bit integer to write */ |
| 97 | inline void mach_write_to_6(byte *b, uint64_t n) { |
| 98 | ut_ad(b != nullptr); |
| 99 | |
| 100 | mach_write_to_2(b, (ulint)(n >> 32)); |
| 101 | mach_write_to_4(b + 2, (ulint)n); |
| 102 | } |
| 103 | |
| 104 | #endif // _mach0data_h_ |