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