s3: smbd: Deliberately currupt an uninitialized pointer.
[Samba.git] / lib / util / util_str.c
blob7c1d15dbeb0b196f18b73e89fc90c900fbe724de
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) Simo Sorce 2001-2002
7 Copyright (C) Martin Pool 2003
8 Copyright (C) James Peach 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
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 for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "replace.h"
25 #include "lib/util/samba_util.h"
26 #include "system/locale.h"
27 #include "smb_strtox.h"
28 #undef strncasecmp
29 #undef strcasemp
31 /**
32 * @file
33 * @brief String utilities.
34 **/
36 /**
37 * Parse a string containing a boolean value.
39 * val will be set to the read value.
41 * @retval true if a boolean value was parsed, false otherwise.
43 _PUBLIC_ bool conv_str_bool(const char * str, bool * val)
45 char * end = NULL;
46 long lval;
48 if (str == NULL || *str == '\0') {
49 return false;
52 lval = strtol(str, &end, 10 /* base */);
53 if (end == NULL || *end != '\0' || end == str) {
54 return set_boolean(str, val);
57 *val = (lval) ? true : false;
58 return true;
61 /**
62 * Convert a size specification like 16K into an integral number of bytes.
63 **/
64 _PUBLIC_ bool conv_str_size_error(const char * str, uint64_t * val)
66 char * end = NULL;
67 unsigned long long lval;
68 int error = 0;
70 if (str == NULL || *str == '\0') {
71 return false;
74 lval = smb_strtoull(str, &end, 10, &error, SMB_STR_STANDARD);
75 if (error != 0) {
76 return false;
79 if (*end) {
80 if (strwicmp(end, "K") == 0) {
81 lval *= 1024ULL;
82 } else if (strwicmp(end, "M") == 0) {
83 lval *= (1024ULL * 1024ULL);
84 } else if (strwicmp(end, "G") == 0) {
85 lval *= (1024ULL * 1024ULL * 1024ULL);
86 } else if (strwicmp(end, "T") == 0) {
87 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
88 } else if (strwicmp(end, "P") == 0) {
89 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
90 } else {
91 return false;
95 *val = (uint64_t)lval;
96 return true;
99 /**
100 * Parse a uint64_t value from a string
102 * val will be set to the value read.
104 * @retval true if parsing was successful, false otherwise
106 _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
108 unsigned long long lval;
109 int error = 0;
111 if (str == NULL || *str == '\0') {
112 return false;
115 lval = smb_strtoull(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
116 if (error != 0) {
117 return false;
120 *val = (uint64_t)lval;
121 return true;
125 * Compare 2 strings.
127 * @note The comparison is case-insensitive.
129 _PUBLIC_ bool strequal(const char *s1, const char *s2)
131 if (s1 == s2)
132 return true;
133 if (!s1 || !s2)
134 return false;
136 return strcasecmp_m(s1,s2) == 0;
140 * @file
141 * @brief String utilities.
144 static bool next_token_internal_talloc(TALLOC_CTX *ctx,
145 const char **ptr,
146 char **pp_buff,
147 const char *sep,
148 bool ltrim)
150 const char *s;
151 const char *saved_s;
152 char *pbuf;
153 bool quoted;
154 size_t len=1;
156 *pp_buff = NULL;
157 if (!ptr) {
158 return(false);
161 s = *ptr;
163 /* default to simple separators */
164 if (!sep) {
165 sep = " \t\n\r";
168 /* find the first non sep char, if left-trimming is requested */
169 if (ltrim) {
170 while (*s && strchr_m(sep,*s)) {
171 s++;
175 /* nothing left? */
176 if (!*s) {
177 return false;
180 /* When restarting we need to go from here. */
181 saved_s = s;
183 /* Work out the length needed. */
184 for (quoted = false; *s &&
185 (quoted || !strchr_m(sep,*s)); s++) {
186 if (*s == '\"') {
187 quoted = !quoted;
188 } else {
189 len++;
193 /* We started with len = 1 so we have space for the nul. */
194 *pp_buff = talloc_array(ctx, char, len);
195 if (!*pp_buff) {
196 return false;
199 /* copy over the token */
200 pbuf = *pp_buff;
201 s = saved_s;
202 for (quoted = false; *s &&
203 (quoted || !strchr_m(sep,*s)); s++) {
204 if ( *s == '\"' ) {
205 quoted = !quoted;
206 } else {
207 *pbuf++ = *s;
211 *ptr = (*s) ? s+1 : s;
212 *pbuf = 0;
214 return true;
217 bool next_token_talloc(TALLOC_CTX *ctx,
218 const char **ptr,
219 char **pp_buff,
220 const char *sep)
222 return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
226 * Get the next token from a string, return false if none found. Handles
227 * double-quotes. This version does not trim leading separator characters
228 * before looking for a token.
231 bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
232 const char **ptr,
233 char **pp_buff,
234 const char *sep)
236 return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
240 * Get the next token from a string, return False if none found.
241 * Handles double-quotes.
243 * Based on a routine by GJC@VILLAGE.COM.
244 * Extensively modified by Andrew.Tridgell@anu.edu.au
246 _PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
248 const char *s;
249 bool quoted;
250 size_t len=1;
252 if (!ptr)
253 return false;
255 s = *ptr;
257 /* default to simple separators */
258 if (!sep)
259 sep = " \t\n\r";
261 /* find the first non sep char */
262 while (*s && strchr_m(sep,*s))
263 s++;
265 /* nothing left? */
266 if (!*s)
267 return false;
269 /* copy over the token */
270 for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
271 if (*s == '\"') {
272 quoted = !quoted;
273 } else {
274 len++;
275 *buff++ = *s;
279 *ptr = (*s) ? s+1 : s;
280 *buff = 0;
282 return true;
286 Set a boolean variable from the text value stored in the passed string.
287 Returns true in success, false if the passed string does not correctly
288 represent a boolean.
291 _PUBLIC_ bool set_boolean(const char *boolean_string, bool *boolean)
293 if (strwicmp(boolean_string, "yes") == 0 ||
294 strwicmp(boolean_string, "true") == 0 ||
295 strwicmp(boolean_string, "on") == 0 ||
296 strwicmp(boolean_string, "1") == 0) {
297 *boolean = true;
298 return true;
299 } else if (strwicmp(boolean_string, "no") == 0 ||
300 strwicmp(boolean_string, "false") == 0 ||
301 strwicmp(boolean_string, "off") == 0 ||
302 strwicmp(boolean_string, "0") == 0) {
303 *boolean = false;
304 return true;
306 return false;