s3/lib/ctdbd_conn: assert hdr following read/recv
[Samba.git] / lib / util / util_str.c
blob3356df34f04f1bbcb6710a35e4181e860d04bb63
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 "includes.h"
25 #include "system/locale.h"
26 #undef strncasecmp
27 #undef strcasemp
29 /**
30 * @file
31 * @brief String utilities.
32 **/
34 /**
35 * Parse a string containing a boolean value.
37 * val will be set to the read value.
39 * @retval true if a boolean value was parsed, false otherwise.
41 _PUBLIC_ bool conv_str_bool(const char * str, bool * val)
43 char * end = NULL;
44 long lval;
46 if (str == NULL || *str == '\0') {
47 return false;
50 lval = strtol(str, &end, 10 /* base */);
51 if (end == NULL || *end != '\0' || end == str) {
52 return set_boolean(str, val);
55 *val = (lval) ? true : false;
56 return true;
59 /**
60 * Convert a size specification like 16K into an integral number of bytes.
61 **/
62 _PUBLIC_ bool conv_str_size_error(const char * str, uint64_t * val)
64 char * end = NULL;
65 unsigned long long lval;
66 int error = 0;
68 if (str == NULL || *str == '\0') {
69 return false;
72 lval = smb_strtoull(str, &end, 10, &error, SMB_STR_STANDARD);
73 if (error != 0) {
74 return false;
77 if (*end) {
78 if (strwicmp(end, "K") == 0) {
79 lval *= 1024ULL;
80 } else if (strwicmp(end, "M") == 0) {
81 lval *= (1024ULL * 1024ULL);
82 } else if (strwicmp(end, "G") == 0) {
83 lval *= (1024ULL * 1024ULL * 1024ULL);
84 } else if (strwicmp(end, "T") == 0) {
85 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
86 } else if (strwicmp(end, "P") == 0) {
87 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
88 } else {
89 return false;
93 *val = (uint64_t)lval;
94 return true;
97 /**
98 * Parse a uint64_t value from a string
100 * val will be set to the value read.
102 * @retval true if parsing was successful, false otherwise
104 _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
106 unsigned long long lval;
107 int error = 0;
109 if (str == NULL || *str == '\0') {
110 return false;
113 lval = smb_strtoull(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
114 if (error != 0) {
115 return false;
118 *val = (uint64_t)lval;
119 return true;
123 * Compare 2 strings.
125 * @note The comparison is case-insensitive.
127 _PUBLIC_ bool strequal(const char *s1, const char *s2)
129 if (s1 == s2)
130 return true;
131 if (!s1 || !s2)
132 return false;
134 return strcasecmp_m(s1,s2) == 0;
138 * @file
139 * @brief String utilities.
142 static bool next_token_internal_talloc(TALLOC_CTX *ctx,
143 const char **ptr,
144 char **pp_buff,
145 const char *sep,
146 bool ltrim)
148 const char *s;
149 const char *saved_s;
150 char *pbuf;
151 bool quoted;
152 size_t len=1;
154 *pp_buff = NULL;
155 if (!ptr) {
156 return(false);
159 s = *ptr;
161 /* default to simple separators */
162 if (!sep) {
163 sep = " \t\n\r";
166 /* find the first non sep char, if left-trimming is requested */
167 if (ltrim) {
168 while (*s && strchr_m(sep,*s)) {
169 s++;
173 /* nothing left? */
174 if (!*s) {
175 return false;
178 /* When restarting we need to go from here. */
179 saved_s = s;
181 /* Work out the length needed. */
182 for (quoted = false; *s &&
183 (quoted || !strchr_m(sep,*s)); s++) {
184 if (*s == '\"') {
185 quoted = !quoted;
186 } else {
187 len++;
191 /* We started with len = 1 so we have space for the nul. */
192 *pp_buff = talloc_array(ctx, char, len);
193 if (!*pp_buff) {
194 return false;
197 /* copy over the token */
198 pbuf = *pp_buff;
199 s = saved_s;
200 for (quoted = false; *s &&
201 (quoted || !strchr_m(sep,*s)); s++) {
202 if ( *s == '\"' ) {
203 quoted = !quoted;
204 } else {
205 *pbuf++ = *s;
209 *ptr = (*s) ? s+1 : s;
210 *pbuf = 0;
212 return true;
215 bool next_token_talloc(TALLOC_CTX *ctx,
216 const char **ptr,
217 char **pp_buff,
218 const char *sep)
220 return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
224 * Get the next token from a string, return false if none found. Handles
225 * double-quotes. This version does not trim leading separator characters
226 * before looking for a token.
229 bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
230 const char **ptr,
231 char **pp_buff,
232 const char *sep)
234 return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
238 * Get the next token from a string, return False if none found.
239 * Handles double-quotes.
241 * Based on a routine by GJC@VILLAGE.COM.
242 * Extensively modified by Andrew.Tridgell@anu.edu.au
244 _PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
246 const char *s;
247 bool quoted;
248 size_t len=1;
250 if (!ptr)
251 return false;
253 s = *ptr;
255 /* default to simple separators */
256 if (!sep)
257 sep = " \t\n\r";
259 /* find the first non sep char */
260 while (*s && strchr_m(sep,*s))
261 s++;
263 /* nothing left? */
264 if (!*s)
265 return false;
267 /* copy over the token */
268 for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
269 if (*s == '\"') {
270 quoted = !quoted;
271 } else {
272 len++;
273 *buff++ = *s;
277 *ptr = (*s) ? s+1 : s;
278 *buff = 0;
280 return true;
284 Set a boolean variable from the text value stored in the passed string.
285 Returns true in success, false if the passed string does not correctly
286 represent a boolean.
289 _PUBLIC_ bool set_boolean(const char *boolean_string, bool *boolean)
291 if (strwicmp(boolean_string, "yes") == 0 ||
292 strwicmp(boolean_string, "true") == 0 ||
293 strwicmp(boolean_string, "on") == 0 ||
294 strwicmp(boolean_string, "1") == 0) {
295 *boolean = true;
296 return true;
297 } else if (strwicmp(boolean_string, "no") == 0 ||
298 strwicmp(boolean_string, "false") == 0 ||
299 strwicmp(boolean_string, "off") == 0 ||
300 strwicmp(boolean_string, "0") == 0) {
301 *boolean = false;
302 return true;
304 return false;
308 return the number of bytes occupied by a buffer in CH_UTF16 format
309 the result includes the null termination
311 _PUBLIC_ size_t utf16_len(const void *buf)
313 size_t len;
315 for (len = 0; SVAL(buf,len); len += 2) ;
317 return len + 2;
321 return the number of bytes occupied by a buffer in CH_UTF16 format
322 the result includes the null termination
323 limited by 'n' bytes
325 _PUBLIC_ size_t utf16_len_n(const void *src, size_t n)
327 size_t len;
329 for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;
331 if (len+2 <= n) {
332 len += 2;
335 return len;
338 _PUBLIC_ int memcmp_const_time(const void *s1, const void *s2, size_t n)
340 const uint8_t *p1 = s1, *p2 = s2;
341 size_t i, sum = 0;
343 for (i = 0; i < n; i++) {
344 sum |= (p1[i] ^ p2[i]);
347 return sum != 0;