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 
114 /* Prototypes of normal stringfunctions (with may ours) */
115 #ifndef HAVE_STRNLEN
116 extern size_t strnlen(const char *s, size_t n);
117 #endif
118 
119 extern int is_prefix(const char *, const char *);
120 
121 /* Conversion routines */
122 typedef enum {
123   MY_GCVT_ARG_FLOAT,
124   MY_GCVT_ARG_DOUBLE
125 } my_gcvt_arg_type;
126 
127 double my_strtod(const char *str, char **end, int *error);
128 double my_atof(const char *nptr);
129 size_t my_fcvt(double x, int precision, char *to, my_bool *error);
130 size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
131                my_bool *error);
132 
133 #define NOT_FIXED_DEC 31
134 
135 /*
136   The longest string my_fcvt can return is 311 + "precision" bytes.
137   Here we assume that we never cal my_fcvt() with precision >= NOT_FIXED_DEC
138   (+ 1 byte for the terminating '\0').
139 */
140 #define FLOATING_POINT_BUFFER (311 + NOT_FIXED_DEC)
141 
142 /*
143   We want to use the 'e' format in some cases even if we have enough space
144   for the 'f' one just to mimic sprintf("%.15g") behavior for large integers,
145   and to improve it for numbers < 10^(-4).
146   That is, for |x| < 1 we require |x| >= 10^(-15), and for |x| > 1 we require
147   it to be integer and be <= 10^DBL_DIG for the 'f' format to be used.
148   We don't lose precision, but make cases like "1e200" or "0.00001" look nicer.
149 */
150 #define MAX_DECPT_FOR_F_FORMAT DBL_DIG
151 
152 /*
153   The maximum possible field width for my_gcvt() conversion.
154   (DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
155   MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
156 */
157 #define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \
158 
159 extern char *llstr(longlong value,char *buff);
160 extern char *ullstr(longlong value,char *buff);
161 #ifndef HAVE_STRTOUL
162 extern long strtol(const char *str, char **ptr, int base);
163 extern ulong strtoul(const char *str, char **ptr, int base);
164 #endif
165 
166 extern char *int2str(long val, char *dst, int radix, int upcase);
167 extern char *int10_to_str(long val,char *dst,int radix);
168 extern char *str2int(const char *src,int radix,long lower,long upper,
169 			 long *val);
170 longlong my_strtoll10(const char *nptr, char **endptr, int *error);
171 #if SIZEOF_LONG == SIZEOF_LONG_LONG
172 #define ll2str(A,B,C,D) int2str((A),(B),(C),(D))
173 #define longlong10_to_str(A,B,C) int10_to_str((A),(B),(C))
174 #undef strtoll
175 #define strtoll(A,B,C) strtol((A),(B),(C))
176 #define strtoull(A,B,C) strtoul((A),(B),(C))
177 #ifndef HAVE_STRTOULL
178 #define HAVE_STRTOULL
179 #endif
180 #ifndef HAVE_STRTOLL
181 #define HAVE_STRTOLL
182 #endif
183 #else
184 #ifdef HAVE_LONG_LONG
185 extern char *ll2str(longlong val,char *dst,int radix, int upcase);
186 extern char *longlong10_to_str(longlong val,char *dst,int radix);
187 #if (!defined(HAVE_STRTOULL) || defined(NO_STRTOLL_PROTO))
188 extern longlong strtoll(const char *str, char **ptr, int base);
189 extern ulonglong strtoull(const char *str, char **ptr, int base);
190 #endif
191 #endif
192 #endif
193 #define longlong2str(A,B,C) ll2str((A),(B),(C),1)
194 
195 #if defined(__cplusplus)
196 }
197 #endif
198 
199 /*
200   LEX_STRING -- a pair of a C-string and its length.
201   (it's part of the plugin API as a MYSQL_LEX_STRING)
202 */
203 
204 #include <mysql/plugin.h>
205 typedef struct st_mysql_lex_string LEX_STRING;
206 
207 #define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))
208 #define USTRING_WITH_LEN(X) ((uchar*) X), ((size_t) (sizeof(X) - 1))
209 #define C_STRING_WITH_LEN(X) ((char *) (X)), ((size_t) (sizeof(X) - 1))
210 
211 struct st_mysql_const_lex_string
212 {
213   const char *str;
214   size_t length;
215 };
216 typedef struct st_mysql_const_lex_string LEX_CSTRING;
217 
218 /* SPACE_INT is a word that contains only spaces */
219 #if SIZEOF_INT == 4
220 #define SPACE_INT 0x20202020
221 #elif SIZEOF_INT == 8
222 #define SPACE_INT 0x2020202020202020
223 #else
224 #error define the appropriate constant for a word full of spaces
225 #endif
226 
227 /**
228   Skip trailing space.
229 
230   On most systems reading memory in larger chunks (ideally equal to the size of
231   the chinks that the machine physically reads from memory) causes fewer memory
232   access loops and hence increased performance.
233   This is why the 'int' type is used : it's closest to that (according to how
234   it's defined in C).
235   So when we determine the amount of whitespace at the end of a string we do
236   the following :
237     1. We divide the string into 3 zones :
238       a) from the start of the string (__start) to the first multiple
239         of sizeof(int)  (__start_words)
240       b) from the end of the string (__end) to the last multiple of sizeof(int)
241         (__end_words)
242       c) a zone that is aligned to sizeof(int) and can be safely accessed
243         through an int *
244     2. We start comparing backwards from (c) char-by-char. If all we find is
245        space then we continue
246     3. If there are elements in zone (b) we compare them as unsigned ints to a
247        int mask (SPACE_INT) consisting of all spaces
248     4. Finally we compare the remaining part (a) of the string char by char.
249        This covers for the last non-space unsigned int from 3. (if any)
250 
251    This algorithm works well for relatively larger strings, but it will slow
252    the things down for smaller strings (because of the additional calculations
253    and checks compared to the naive method). Thus the barrier of length 20
254    is added.
255 
256    @param     ptr   pointer to the input string
257    @param     len   the length of the string
258    @return          the last non-space character
259 */
260 
skip_trailing_space(const uchar * ptr,size_t len)261 static inline const uchar *skip_trailing_space(const uchar *ptr,size_t len)
262 {
263   const uchar *end= ptr + len;
264 
265   if (len > 20)
266   {
267     const uchar *end_words= (const uchar *)(intptr)
268       (((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
269     const uchar *start_words= (const uchar *)(intptr)
270        ((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);
271 
272     DBUG_ASSERT(((ulonglong)(intptr)ptr) >= SIZEOF_INT);
273     if (end_words > ptr)
274     {
275       while (end > end_words && end[-1] == 0x20)
276         end--;
277       if (end[-1] == 0x20 && start_words < end_words)
278         while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
279           end -= SIZEOF_INT;
280     }
281   }
282   while (end > ptr && end[-1] == 0x20)
283     end--;
284   return (end);
285 }
286 
lex_string_set(LEX_STRING * lex_str,const char * c_str)287 static inline void lex_string_set(LEX_STRING *lex_str, const char *c_str)
288 {
289   lex_str->str= (char *) c_str;
290   lex_str->length= strlen(c_str);
291 }
292 
293 #endif
294