Debian Codegraph @

storage/innobase/include/ut0mem.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 1994, 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/ut0mem.ic
29 Memory primitives
30
31 Created 5/30/1994 Heikki Tuuri
32 ************************************************************************/
33
34 #include "mach0data.h"
35 #include "ut0byte.h"
36
37 /** Wrapper for memcpy(3). Copy memory area when the source and
38 target are not overlapping.
39 @param[in,out] dest copy to
40 @param[in] src copy from
41 @param[in] n number of bytes to copy
42 @return dest */
43 static inline void *ut_memcpy(void *dest, const void *src, ulint n) {
44 return (memcpy(dest, src, n));
45 }
46
47 /** Wrapper for memmove(3). Copy memory area when the source and
48 target are overlapping.
49 @param[in,out] dest Move to
50 @param[in] src Move from
51 @param[in] n number of bytes to move
52 @return dest */
53 static inline void *ut_memmove(void *dest, const void *src, ulint n) {
54 return (memmove(dest, src, n));
55 }
56
57 /** Wrapper for memcmp(3). Compare memory areas.
58 @param[in] str1 first memory block to compare
59 @param[in] str2 second memory block to compare
60 @param[in] n number of bytes to compare
61 @return negative, 0, or positive if str1 is smaller, equal,
62 or greater than str2, respectively. */
63 static inline int ut_memcmp(const void *str1, const void *str2, ulint n) {
64 return (memcmp(str1, str2, n));
65 }
66
67 /** Wrapper for strcpy(3). Copy a NUL-terminated string.
68 @param[in,out] dest Destination to copy to
69 @param[in] src Source to copy from
70 @return dest */
71 static inline char *ut_strcpy(char *dest, const char *src) {
72 return (strcpy(dest, src));
73 }
74
75 /** Wrapper for strlen(3). Determine the length of a NUL-terminated string.
76 @param[in] str string
77 @return length of the string in bytes, excluding the terminating NUL */
78 static inline ulint ut_strlen(const char *str) { return (strlen(str)); }
79
80 /** Wrapper for strcmp(3). Compare NUL-terminated strings.
81 @param[in] str1 first string to compare
82 @param[in] str2 second string to compare
83 @return negative, 0, or positive if str1 is smaller, equal,
84 or greater than str2, respectively. */
85 static inline int ut_strcmp(const char *str1, const char *str2) {
86 return (strcmp(str1, str2));
87 }
88
89 /** Converts a raw binary data to a NUL-terminated hex string. The output is
90 truncated if there is not enough space in "hex", make sure "hex_size" is at
91 least (2 * raw_size + 1) if you do not want this to happen. Returns the
92 actual number of characters written to "hex" (including the NUL).
93 @return number of chars written */
94 static inline ulint ut_raw_to_hex(
95 const void *raw, /*!< in: raw data */
96 ulint raw_size, /*!< in: "raw" length in bytes */
97 char *hex, /*!< out: hex string */
98 ulint hex_size) /*!< in: "hex" size in bytes */
99 {
100 #ifdef WORDS_BIGENDIAN
101
102 #define MK_UINT16(a, b) (((uint16)(a)) << 8 | (uint16)(b))
103
104 #define UINT16_GET_A(u) ((unsigned char)((u) >> 8))
105 #define UINT16_GET_B(u) ((unsigned char)((u)&0xFF))
106
107 #else /* WORDS_BIGENDIAN */
108
109 #define MK_UINT16(a, b) (((uint16)(b)) << 8 | (uint16)(a))
110
111 #define UINT16_GET_A(u) ((unsigned char)((u)&0xFF))
112 #define UINT16_GET_B(u) ((unsigned char)((u) >> 8))
113
114 #endif /* WORDS_BIGENDIAN */
115
116 #define MK_ALL_UINT16_WITH_A(a) \
117 MK_UINT16(a, '0'), MK_UINT16(a, '1'), MK_UINT16(a, '2'), MK_UINT16(a, '3'), \
118 MK_UINT16(a, '4'), MK_UINT16(a, '5'), MK_UINT16(a, '6'), \
119 MK_UINT16(a, '7'), MK_UINT16(a, '8'), MK_UINT16(a, '9'), \
120 MK_UINT16(a, 'A'), MK_UINT16(a, 'B'), MK_UINT16(a, 'C'), \
121 MK_UINT16(a, 'D'), MK_UINT16(a, 'E'), MK_UINT16(a, 'F')
122
123 static const uint16 hex_map[256] = {
124 MK_ALL_UINT16_WITH_A('0'), MK_ALL_UINT16_WITH_A('1'),
125 MK_ALL_UINT16_WITH_A('2'), MK_ALL_UINT16_WITH_A('3'),
126 MK_ALL_UINT16_WITH_A('4'), MK_ALL_UINT16_WITH_A('5'),
127 MK_ALL_UINT16_WITH_A('6'), MK_ALL_UINT16_WITH_A('7'),
128 MK_ALL_UINT16_WITH_A('8'), MK_ALL_UINT16_WITH_A('9'),
129 MK_ALL_UINT16_WITH_A('A'), MK_ALL_UINT16_WITH_A('B'),
130 MK_ALL_UINT16_WITH_A('C'), MK_ALL_UINT16_WITH_A('D'),
131 MK_ALL_UINT16_WITH_A('E'), MK_ALL_UINT16_WITH_A('F')};
132 const unsigned char *rawc;
133 ulint read_bytes;
134 ulint write_bytes;
135 ulint i;
136
137 rawc = (const unsigned char *)raw;
138
139 if (hex_size == 0) {
140 return (0);
141 }
142
143 if (hex_size <= 2 * raw_size) {
144 read_bytes = hex_size / 2;
145 write_bytes = hex_size;
146 } else {
147 read_bytes = raw_size;
148 write_bytes = 2 * raw_size + 1;
149 }
150
151 #define LOOP_READ_BYTES(ASSIGN) \
152 for (i = 0; i < read_bytes; i++) { \
153 ASSIGN; \
154 hex += 2; \
155 rawc++; \
156 }
157
158 if (ut_align_offset(hex, 2) == 0) {
159 LOOP_READ_BYTES(*(uint16 *)hex = hex_map[*rawc]);
160 } else {
161 LOOP_READ_BYTES(*hex = UINT16_GET_A(hex_map[*rawc]);
162 *(hex + 1) = UINT16_GET_B(hex_map[*rawc]));
163 }
164
165 if (hex_size <= 2 * raw_size && hex_size % 2 == 0) {
166 hex--;
167 }
168
169 *hex = '\0';
170
171 return (write_bytes);
172 }
173
174 /** Adds single quotes to the start and end of string and escapes any quotes
175 by doubling them. Returns the number of bytes that were written to "buf"
176 (including the terminating NUL). If buf_size is too small then the
177 trailing bytes from "str" are discarded.
178 @return number of bytes that were written */
179 static inline ulint ut_str_sql_format(
180 const char *str, /*!< in: string */
181 ulint str_len, /*!< in: string length in bytes */
182 char *buf, /*!< out: output buffer */
183 ulint buf_size) /*!< in: output buffer size
184 in bytes */
185 {
186 ulint str_i;
187 ulint buf_i;
188
189 buf_i = 0;
190
191 switch (buf_size) {
192 case 3:
193
194 if (str_len == 0) {
195 buf[buf_i] = '\'';
196 buf_i++;
197 buf[buf_i] = '\'';
198 buf_i++;
199 }
200 [[fallthrough]];
201 case 2:
202 case 1:
203
204 buf[buf_i] = '\0';
205 buf_i++;
206 [[fallthrough]];
207 case 0:
208
209 return (buf_i);
210 }
211
212 /* buf_size >= 4 */
213
214 buf[0] = '\'';
215 buf_i = 1;
216
217 for (str_i = 0; str_i < str_len; str_i++) {
218 char ch;
219
220 if (buf_size - buf_i == 2) {
221 break;
222 }
223
224 ch = str[str_i];
225
226 switch (ch) {
227 case '\0':
228
229 if (buf_size - buf_i < 4) {
230 goto func_exit;
231 }
232 buf[buf_i] = '\\';
233 buf_i++;
234 buf[buf_i] = '0';
235 buf_i++;
236 break;
237 case '\'':
238 case '\\':
239
240 if (buf_size - buf_i < 4) {
241 goto func_exit;
242 }
243 buf[buf_i] = ch;
244 buf_i++;
245 [[fallthrough]];
246 default:
247
248 buf[buf_i] = ch;
249 buf_i++;
250 }
251 }
252
253 func_exit:
254
255 buf[buf_i] = '\'';
256 buf_i++;
257 buf[buf_i] = '\0';
258 buf_i++;
259
260 return (buf_i);
261 }