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