1 /*
2 Copyright (c) 2000, 2021, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #ifndef _m_string_h
25 #define _m_string_h
26
27 #include "my_global.h" /* HAVE_* */
28
29 #include <string.h>
30
31 #include "mysql/service_my_snprintf.h"
32
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36
37 /*
38 my_str_malloc(), my_str_realloc() and my_str_free() are assigned to
39 implementations in strings/alloc.c, but can be overridden in
40 the calling program.
41 */
42 extern void *(*my_str_malloc)(size_t);
43 extern void *(*my_str_realloc)(void *, size_t);
44 extern void (*my_str_free)(void *);
45
46 /* Declared in int2str() */
47 extern char _dig_vec_upper[];
48 extern char _dig_vec_lower[];
49
50 /* Prototypes for string functions */
51
52 extern void bchange(uchar *dst,size_t old_len,const uchar *src,
53 size_t new_len,size_t tot_len);
54 extern void strappend(char *s,size_t len,pchar fill);
55 extern char *strend(const char *s);
56 extern char *strcend(const char *, pchar);
57 extern char *strfill(char * s,size_t len,pchar fill);
58 extern char *strmake(char *dst,const char *src,size_t length);
59
60 extern char *my_stpmov(char *dst,const char *src);
61 extern char *my_stpnmov(char *dst, const char *src, size_t n);
62 extern char *strcont(const char *src, const char *set);
63 extern char *strxmov(char *dst, const char *src, ...);
64 extern char *strxnmov(char *dst, size_t len, const char *src, ...);
65
66 /**
67 Copy a string from src to dst until (and including) terminating null byte.
68
69 @param dst Destination
70 @param src Source
71
72 @note src and dst cannot overlap.
73 Use my_stpmov() if src and dst overlaps.
74
75 @note Unsafe, consider using my_stpnpy() instead.
76
77 @return pointer to terminating null byte.
78 */
my_stpcpy(char * dst,const char * src)79 static inline char *my_stpcpy(char *dst, const char *src)
80 {
81 #if defined(HAVE_BUILTIN_STPCPY)
82 return __builtin_stpcpy(dst, src);
83 #elif defined(HAVE_STPCPY)
84 return stpcpy(dst, src);
85 #else
86 /* Fallback to implementation supporting overlap. */
87 return my_stpmov(dst, src);
88 #endif
89 }
90
91 /**
92 Copy fixed-size string from src to dst.
93
94 @param dst Destination
95 @param src Source
96 @param n Maximum number of characters to copy.
97
98 @note src and dst cannot overlap
99 Use my_stpnmov() if src and dst overlaps.
100
101 @return pointer to terminating null byte.
102 */
my_stpncpy(char * dst,const char * src,size_t n)103 static inline char *my_stpncpy(char *dst, const char *src, size_t n)
104 {
105 #if defined(HAVE_STPNCPY)
106 return stpncpy(dst, src, n);
107 #else
108 /* Fallback to implementation supporting overlap. */
109 return my_stpnmov(dst, src, n);
110 #endif
111 }
112
113 /**
114 Copies strlen(src) characters of source to destination.
115 If strlen(src) is equal or bigger than num then dst will be truncated.
116 The null-character is always appended at the end of destination.
117 Destination is not padded with zeros until a total of num characters.
118
119 @param dst Destination
120 @param src Source
121 @param n Maximum number of characters to copy.
122
123 @return pointer to Destination is returned.
124 */
my_strncpy_trunc(char * dst,const char * src,size_t num)125 static inline char *my_strncpy_trunc(char *dst, const char *src, size_t num)
126 {
127 size_t len= strlen(src);
128 if (unlikely(len >= num)) {
129 len= num - 1;
130 memcpy(dst, src, len);
131 dst[len]= '\0';
132 } else {
133 memcpy(dst, src, len + 1);
134 }
135 return dst;
136 }
137
my_strtoll(const char * nptr,char ** endptr,int base)138 static inline longlong my_strtoll(const char *nptr, char **endptr, int base)
139 {
140 #if defined _WIN32
141 return _strtoi64(nptr, endptr, base);
142 #else
143 return strtoll(nptr, endptr, base);
144 #endif
145 }
146
my_strtoull(const char * nptr,char ** endptr,int base)147 static inline ulonglong my_strtoull(const char *nptr, char **endptr, int base)
148 {
149 #if defined _WIN32
150 return _strtoui64(nptr, endptr, base);
151 #else
152 return strtoull(nptr, endptr, base);
153 #endif
154 }
155
my_strtok_r(char * str,const char * delim,char ** saveptr)156 static inline char *my_strtok_r(char *str, const char *delim, char **saveptr)
157 {
158 #if defined _WIN32
159 return strtok_s(str, delim, saveptr);
160 #else
161 return strtok_r(str, delim, saveptr);
162 #endif
163 }
164
165 /* native_ rather than my_ since my_strcasecmp already exists */
native_strcasecmp(const char * s1,const char * s2)166 static inline int native_strcasecmp(const char *s1, const char *s2)
167 {
168 #if defined _WIN32
169 return _stricmp(s1, s2);
170 #else
171 return strcasecmp(s1, s2);
172 #endif
173 }
174
175 /* native_ rather than my_ for consistency with native_strcasecmp */
native_strncasecmp(const char * s1,const char * s2,size_t n)176 static inline int native_strncasecmp(const char *s1, const char *s2, size_t n)
177 {
178 #if defined _WIN32
179 return _strnicmp(s1, s2, n);
180 #else
181 return strncasecmp(s1, s2, n);
182 #endif
183 }
184
185 /* Prototypes of normal stringfunctions (with may ours) */
186 #ifndef HAVE_STRNLEN
187 extern size_t strnlen(const char *s, size_t n);
188 #endif
189
190 extern int is_prefix(const char *, const char *);
191
192 /* Conversion routines */
193 typedef enum {
194 MY_GCVT_ARG_FLOAT,
195 MY_GCVT_ARG_DOUBLE
196 } my_gcvt_arg_type;
197
198 double my_strtod(const char *str, char **end, int *error);
199 double my_atof(const char *nptr);
200 size_t my_fcvt(double x, int precision, char *to, my_bool *error);
201 size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
202 my_bool *error);
203
204 #define NOT_FIXED_DEC 31
205
206 /*
207 The longest string my_fcvt can return is 311 + "precision" bytes.
208 Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
209 (+ 1 byte for the terminating '\0').
210 */
211 #define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
212
213 /*
214 We want to use the 'e' format in some cases even if we have enough space
215 for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
216 and to improve it for numbers < 10^(-4).
217 That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
218 it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
219 We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
220 */
221 #define MAX_DECPT_FOR_F_FORMAT DBL_DIG
222
223 /*
224 The maximum possible field width for my_gcvt() conversion.
225 (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
226 MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
227 */
228 #define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \
229
230 extern char *llstr(longlong value,char *buff);
231 extern char *ullstr(longlong value,char *buff);
232
233 extern char *int2str(long val, char *dst, int radix, int upcase);
234 extern char *int10_to_str(long val,char *dst,int radix);
235 extern char *str2int(const char *src,int radix,long lower,long upper,
236 long *val);
237 longlong my_strtoll10(const char *nptr, char **endptr, int *error);
238 #if SIZEOF_LONG == SIZEOF_LONG_LONG
239 #define ll2str(A,B,C,D) int2str((A),(B),(C),(D))
240 #define longlong10_to_str(A,B,C) int10_to_str((A),(B),(C))
241 #undef strtoll
242 #define strtoll(A,B,C) strtol((A),(B),(C))
243 #define strtoull(A,B,C) strtoul((A),(B),(C))
244 #else
245 extern char *ll2str(longlong val,char *dst,int radix, int upcase);
246 extern char *longlong10_to_str(longlong val,char *dst,int radix);
247 #endif
248 #define longlong2str(A,B,C) ll2str((A),(B),(C),1)
249
250 #if defined(__cplusplus)
251 }
252 #endif
253
254 /*
255 LEX_STRING -- a pair of a C-string and its length.
256 (it's part of the plugin API as a MYSQL_LEX_STRING)
257 Ditto LEX_CSTRING/MYSQL_LEX_CSTRING.
258 */
259
260 #include <mysql/mysql_lex_string.h>
261 typedef struct st_mysql_lex_string LEX_STRING;
262 typedef struct st_mysql_const_lex_string LEX_CSTRING;
263
264 #define STRING_WITH_LEN(X) (X), ((sizeof(X) - 1))
265 #define USTRING_WITH_LEN(X) ((uchar*) X), ((sizeof(X) - 1))
266 #define C_STRING_WITH_LEN(X) ((char *) (X)), ((sizeof(X) - 1))
267
268
269 /**
270 Skip trailing space.
271
272 On most systems reading memory in larger chunks (ideally equal to the size of
273 the chunks that the machine physically reads from memory) causes fewer memory
274 access loops and hence increased performance.
275 This is why the 'int' type is used : it's closest to that (according to how
276 it's defined in C).
277 So when we determine the amount of whitespace at the end of a string we do
278 the following :
279 1. We divide the string into 3 zones :
280 a) from the start of the string (__start) to the first multiple
281 of sizeof(int) (__start_words)
282 b) from the end of the string (__end) to the last multiple of sizeof(int)
283 (__end_words)
284 c) a zone that is aligned to sizeof(int) and can be safely accessed
285 through an int *
286 2. We start comparing backwards from (c) char-by-char. If all we find is
287 space then we continue
288 3. If there are elements in zone (b) we compare them as unsigned ints to a
289 int mask (SPACE_INT) consisting of all spaces
290 4. Finally we compare the remaining part (a) of the string char by char.
291 This covers for the last non-space unsigned int from 3. (if any)
292
293 This algorithm works well for relatively larger strings, but it will slow
294 the things down for smaller strings (because of the additional calculations
295 and checks compared to the naive method). Thus the barrier of length 20
296 is added.
297
298 @param ptr pointer to the input string
299 @param len the length of the string
300 @return the last non-space character
301 */
302 #if defined(__sparc) || defined(__sparcv9)
skip_trailing_space(const uchar * ptr,size_t len)303 static inline const uchar *skip_trailing_space(const uchar *ptr,size_t len)
304 {
305 /* SPACE_INT is a word that contains only spaces */
306 #if SIZEOF_INT == 4
307 const unsigned SPACE_INT= 0x20202020U;
308 #elif SIZEOF_INT == 8
309 const unsigned SPACE_INT= 0x2020202020202020ULL;
310 #else
311 #error define the appropriate constant for a word full of spaces
312 #endif
313
314 const uchar *end= ptr + len;
315
316 if (len > 20)
317 {
318 const uchar *end_words= (const uchar *)(intptr)
319 (((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
320 const uchar *start_words= (const uchar *)(intptr)
321 ((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
322
323 assert(end_words > ptr);
324 while (end > end_words && end[-1] == 0x20)
325 end--;
326 if (end[-1] == 0x20 && start_words < end_words)
327 while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
328 end -= SIZEOF_INT;
329 }
330 while (end > ptr && end[-1] == 0x20)
331 end--;
332 return (end);
333 }
334 #else
335 /*
336 Reads 8 bytes at a time, ignoring alignment.
337 We use uint8korr, which is fast (it simply reads a *ulonglong)
338 on all platforms, except sparc.
339 */
skip_trailing_space(const uchar * ptr,size_t len)340 static inline const uchar *skip_trailing_space(const uchar *ptr, size_t len)
341 {
342 const uchar *end= ptr + len;
343 while (end - ptr >= 8)
344 {
345 if (uint8korr(end-8) != 0x2020202020202020ULL)
346 break;
347 end-= 8;
348 }
349 while (end > ptr && end[-1] == 0x20)
350 end--;
351 return (end);
352 }
353 #endif
354
lex_string_set(LEX_STRING * lex_str,const char * c_str)355 static inline void lex_string_set(LEX_STRING *lex_str, const char *c_str)
356 {
357 lex_str->str= (char *) c_str;
358 lex_str->length= strlen(c_str);
359 }
360
lex_cstring_set(LEX_CSTRING * lex_str,const char * c_str)361 static inline void lex_cstring_set(LEX_CSTRING *lex_str, const char *c_str)
362 {
363 lex_str->str= c_str;
364 lex_str->length= strlen(c_str);
365 }
366
367 #endif
368