1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 /**
29   @file mysys/my_string.cc
30   Code for handling strings with can grow dynamically.
31 */
32 
33 #include <stdarg.h>
34 #include <string.h>
35 #include <sys/types.h>
36 
37 #include "m_string.h"
38 #include "my_dbug.h"
39 #include "my_inttypes.h"
40 #include "my_sys.h"
41 #include "mysql/service_mysql_alloc.h"
42 #include "mysys/mysys_priv.h"
43 
init_dynamic_string(DYNAMIC_STRING * str,const char * init_str,size_t init_alloc,size_t alloc_increment)44 bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,
45                          size_t init_alloc, size_t alloc_increment) {
46   size_t length;
47   DBUG_TRACE;
48 
49   if (!alloc_increment) alloc_increment = 128;
50   length = 1;
51   if (init_str && (length = strlen(init_str) + 1) < init_alloc)
52     init_alloc =
53         ((length + alloc_increment - 1) / alloc_increment) * alloc_increment;
54   if (!init_alloc) init_alloc = alloc_increment;
55 
56   if (!(str->str = (char *)my_malloc(key_memory_DYNAMIC_STRING, init_alloc,
57                                      MYF(MY_WME))))
58     return true;
59   str->length = length - 1;
60   if (init_str) memcpy(str->str, init_str, length);
61   str->max_length = init_alloc;
62   str->alloc_increment = alloc_increment;
63   return false;
64 }
65 
dynstr_set(DYNAMIC_STRING * str,const char * init_str)66 bool dynstr_set(DYNAMIC_STRING *str, const char *init_str) {
67   uint length = 0;
68   DBUG_TRACE;
69 
70   if (init_str && (length = (uint)strlen(init_str) + 1) > str->max_length) {
71     str->max_length =
72         ((length + str->alloc_increment - 1) / str->alloc_increment) *
73         str->alloc_increment;
74     if (!str->max_length) str->max_length = str->alloc_increment;
75     if (!(str->str = (char *)my_realloc(key_memory_DYNAMIC_STRING, str->str,
76                                         str->max_length, MYF(MY_WME))))
77       return true;
78   }
79   if (init_str) {
80     str->length = length - 1;
81     memcpy(str->str, init_str, length);
82   } else
83     str->length = 0;
84   return false;
85 }
86 
dynstr_realloc(DYNAMIC_STRING * str,size_t additional_size)87 bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size) {
88   DBUG_TRACE;
89 
90   if (!additional_size) return false;
91   if (str->length + additional_size > str->max_length) {
92     str->max_length =
93         ((str->length + additional_size + str->alloc_increment - 1) /
94          str->alloc_increment) *
95         str->alloc_increment;
96     if (!(str->str = (char *)my_realloc(key_memory_DYNAMIC_STRING, str->str,
97                                         str->max_length, MYF(MY_WME))))
98       return true;
99   }
100   return false;
101 }
102 
dynstr_append(DYNAMIC_STRING * str,const char * append)103 bool dynstr_append(DYNAMIC_STRING *str, const char *append) {
104   return dynstr_append_mem(str, append, (uint)strlen(append));
105 }
106 
dynstr_append_mem(DYNAMIC_STRING * str,const char * append,size_t length)107 bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append, size_t length) {
108   char *new_ptr;
109   if (str->length + length >= str->max_length) {
110     size_t new_length =
111         (str->length + length + str->alloc_increment) / str->alloc_increment;
112     new_length *= str->alloc_increment;
113     if (!(new_ptr = (char *)my_realloc(key_memory_DYNAMIC_STRING, str->str,
114                                        new_length, MYF(MY_WME))))
115       return true;
116     str->str = new_ptr;
117     str->max_length = new_length;
118   }
119   if (length > 0) memcpy(str->str + str->length, append, length);
120   str->length += length;
121   str->str[str->length] = 0; /* Safety for C programs */
122   return false;
123 }
124 
dynstr_trunc(DYNAMIC_STRING * str,size_t n)125 bool dynstr_trunc(DYNAMIC_STRING *str, size_t n) {
126   str->length -= n;
127   str->str[str->length] = '\0';
128   return false;
129 }
130 
131 /*
132   Concatenates any number of strings, escapes any OS quote in the result then
133   surround the whole affair in another set of quotes which is finally appended
134   to specified DYNAMIC_STRING.  This function is especially useful when
135   building strings to be executed with the system() function.
136 
137   @param str Dynamic String which will have addtional strings appended.
138   @param append String to be appended.
139   @param ... Optional. Additional string(s) to be appended.
140 
141   @note The final argument in the list must be NullS even if no additional
142   options are passed.
143 
144   @return True = Success.
145 */
146 
dynstr_append_os_quoted(DYNAMIC_STRING * str,const char * append,...)147 bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...) {
148 #ifdef _WIN32
149   const char *quote_str = "\"";
150   const uint quote_len = 1;
151 #else
152   const char *quote_str = "\'";
153   const uint quote_len = 1;
154 #endif /* _WIN32 */
155   bool ret = true;
156   va_list dirty_text;
157 
158   ret &= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */
159   va_start(dirty_text, append);
160   while (append != NullS) {
161     const char *cur_pos = append;
162     const char *next_pos = cur_pos;
163 
164     /* Search for quote in each string and replace with escaped quote */
165     while (*(next_pos = strcend(cur_pos, quote_str[0])) != '\0') {
166       ret &= dynstr_append_mem(str, cur_pos, (uint)(next_pos - cur_pos));
167       ret &= dynstr_append_mem(str, "\\", 1);
168       ret &= dynstr_append_mem(str, quote_str, quote_len);
169       cur_pos = next_pos + 1;
170     }
171     ret &= dynstr_append_mem(str, cur_pos, (uint)(next_pos - cur_pos));
172     append = va_arg(dirty_text, char *);
173   }
174   va_end(dirty_text);
175   ret &= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */
176 
177   return ret;
178 }
179 
dynstr_free(DYNAMIC_STRING * str)180 void dynstr_free(DYNAMIC_STRING *str) {
181   my_free(str->str);
182   str->str = nullptr;
183 }
184