Debian Codegraph @

storage/innobase/mach/mach0data.cc

raw
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 mach/mach0data.cc
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 "mach0data.h"
36
37 #include <stddef.h>
38
39 /** Read a 64-bit integer in a much compressed form.
40 @param[in,out] ptr pointer to memory from where to read,
41 advanced by the number of bytes consumed, or set NULL if out of space
42 @param[in] end_ptr end of the buffer
43 @return unsigned 64-bit integer */
44 uint64_t mach_parse_u64_much_compressed(const byte **ptr, const byte *end_ptr) {
45 ulint val;
46
47 if (*ptr >= end_ptr) {
48 *ptr = nullptr;
49 return (0);
50 }
51
52 val = mach_read_from_1(*ptr);
53
54 if (val != 0xFF) {
55 return (mach_parse_compressed(ptr, end_ptr));
56 }
57
58 ++*ptr;
59
60 uint64_t n = mach_parse_compressed(ptr, end_ptr);
61 if (*ptr == nullptr) {
62 return (0);
63 }
64
65 n <<= 32;
66
67 n |= mach_parse_compressed(ptr, end_ptr);
68 if (*ptr == nullptr) {
69 return (0);
70 }
71
72 return (n);
73 }
74
75 /** Read a 32-bit integer in a compressed form.
76 @param[in,out] ptr pointer to memory from where to read;
77 advanced by the number of bytes consumed, or set NULL if out of space
78 @param[in] end_ptr end of the buffer
79 @return unsigned value */
80 uint32_t mach_parse_compressed(const byte **ptr, const byte *end_ptr) {
81 ulint val;
82
83 if (*ptr >= end_ptr) {
84 *ptr = nullptr;
85 return (0);
86 }
87
88 val = mach_read_from_1(*ptr);
89
90 if (val < 0x80) {
91 /* 0nnnnnnn (7 bits) */
92 ++*ptr;
93 return (static_cast<uint32_t>(val));
94 }
95
96 /* Workaround GCC bug
97 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77673:
98 the compiler moves mach_read_from_4 right to the beginning of the
99 function, causing and out-of-bounds read if we are reading a short
100 integer close to the end of buffer. */
101 #if defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__clang__)
102 #define DEPLOY_FENCE
103 #endif
104
105 #ifdef DEPLOY_FENCE
106 __atomic_thread_fence(__ATOMIC_ACQUIRE);
107 #endif
108
109 if (val < 0xC0) {
110 /* 10nnnnnn nnnnnnnn (14 bits) */
111 if (end_ptr >= *ptr + 2) {
112 val = mach_read_from_2(*ptr) & 0x3FFF;
113 ut_ad(val > 0x7F);
114 *ptr += 2;
115 return (static_cast<uint32_t>(val));
116 }
117 *ptr = nullptr;
118 return (0);
119 }
120
121 #ifdef DEPLOY_FENCE
122 __atomic_thread_fence(__ATOMIC_ACQUIRE);
123 #endif
124
125 if (val < 0xE0) {
126 /* 110nnnnn nnnnnnnn nnnnnnnn (21 bits) */
127 if (end_ptr >= *ptr + 3) {
128 val = mach_read_from_3(*ptr) & 0x1FFFFF;
129 ut_ad(val > 0x3FFF);
130 *ptr += 3;
131 return (static_cast<uint32_t>(val));
132 }
133 *ptr = nullptr;
134 return (0);
135 }
136
137 #ifdef DEPLOY_FENCE
138 __atomic_thread_fence(__ATOMIC_ACQUIRE);
139 #endif
140
141 if (val < 0xF0) {
142 /* 1110nnnn nnnnnnnn nnnnnnnn nnnnnnnn (28 bits) */
143 if (end_ptr >= *ptr + 4) {
144 val = mach_read_from_4(*ptr) & 0xFFFFFFF;
145 ut_ad(val > 0x1FFFFF);
146 *ptr += 4;
147 return (static_cast<uint32_t>(val));
148 }
149 *ptr = nullptr;
150 return (0);
151 }
152
153 #ifdef DEPLOY_FENCE
154 __atomic_thread_fence(__ATOMIC_ACQUIRE);
155 #endif
156
157 if (val < 0xF8) {
158 ut_ad(val == 0xF0);
159
160 /* 11110000 nnnnnnnn nnnnnnnn nnnnnnnn nnnnnnnn (32 bits) */
161 if (end_ptr >= *ptr + 5) {
162 val = mach_read_from_4(*ptr + 1);
163 ut_ad(val > 0xFFFFFFF);
164 *ptr += 5;
165 return (static_cast<uint32_t>(val));
166 }
167
168 *ptr = nullptr;
169 return (0);
170 }
171
172 #ifdef DEPLOY_FENCE
173 __atomic_thread_fence(__ATOMIC_ACQUIRE);
174 #endif
175
176 if (val < 0xFC) {
177 /* 111110nn nnnnnnnn (10 bits) (extended) */
178 if (end_ptr >= *ptr + 2) {
179 val = (mach_read_from_2(*ptr) & 0x3FF) | 0xFFFFFC00;
180 *ptr += 2;
181 return (static_cast<uint32_t>(val));
182 }
183 *ptr = nullptr;
184 return (0);
185 }
186
187 #ifdef DEPLOY_FENCE
188 __atomic_thread_fence(__ATOMIC_ACQUIRE);
189 #endif
190
191 if (val < 0xFE) {
192 /* 1111110n nnnnnnnn nnnnnnnn (17 bits) (extended) */
193 if (end_ptr >= *ptr + 3) {
194 val = (mach_read_from_3(*ptr) & 0x1FFFF) | 0xFFFE0000;
195 ut_ad(val < 0xFFFFFC00);
196 *ptr += 3;
197 return (static_cast<uint32_t>(val));
198 }
199 *ptr = nullptr;
200 return (0);
201 }
202
203 #ifdef DEPLOY_FENCE
204 __atomic_thread_fence(__ATOMIC_ACQUIRE);
205 #endif
206
207 #undef DEPLOY_FENCE
208
209 ut_ad(val == 0xFE);
210
211 /* 11111110 nnnnnnnn nnnnnnnn nnnnnnnn (24 bits) (extended) */
212 if (end_ptr >= *ptr + 4) {
213 val = mach_read_from_3(*ptr + 1) | 0xFF000000;
214 ut_ad(val < 0xFFFE0000);
215 *ptr += 4;
216 return (static_cast<uint32_t>(val));
217 }
218
219 *ptr = nullptr;
220 return (0);
221 }