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/>.
25 #include "system/locale.h"
31 * @brief String utilities.
35 Safe string copy into a known length string. maxlength does not
36 include the terminating zero.
38 _PUBLIC_ char *safe_strcpy(char *dest,const char *src, size_t maxlength)
43 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
48 /* We intentionally write out at the extremity of the destination
49 * string. If the destination is too short (e.g. pstrcpy into mallocd
50 * or fstring) then this should cause an error under a memory
52 dest[maxlength] = '\0';
53 if (PTR_DIFF(&len, dest) > 0) { /* check if destination is on the stack, ok if so */
54 log_suspicious_usage("safe_strcpy", src);
65 if (len > maxlength) {
66 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
67 (uint_t)(len-maxlength), (unsigned)len, (unsigned)maxlength, src));
71 memmove(dest, src, len);
77 Safe string cat into a string. maxlength does not
78 include the terminating zero.
80 _PUBLIC_ char *safe_strcat(char *dest, const char *src, size_t maxlength)
82 size_t src_len, dest_len;
85 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
93 if (PTR_DIFF(&src_len, dest) > 0) { /* check if destination is on the stack, ok if so */
94 log_suspicious_usage("safe_strcat", src);
97 src_len = strlen(src);
98 dest_len = strlen(dest);
100 if (src_len + dest_len > maxlength) {
101 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
102 (int)(src_len + dest_len - maxlength), src));
103 if (maxlength > dest_len) {
104 memcpy(&dest[dest_len], src, maxlength - dest_len);
110 memcpy(&dest[dest_len], src, src_len);
111 dest[dest_len + src_len] = 0;
116 size_t valgrind_strlen(const char *s)
119 for(count = 0; *s++; count++)
127 format a string into length-prefixed dotted domain format, as used in NBT
128 and in some ADS structures
130 _PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
135 return talloc_strdup(mem_ctx, "");
137 ret = talloc_array(mem_ctx, char, strlen(s)+2);
142 memcpy(ret+1, s, strlen(s)+1);
145 for (i=0;ret[i];i++) {
147 char *p = strchr(ret+i+1, '.');
149 ret[i] = p-(ret+i+1);
151 ret[i] = strlen(ret+i+1);
160 * Add a string to an array of strings.
162 * num should be a pointer to an integer that holds the current
163 * number of elements in strings. It will be updated by this function.
165 _PUBLIC_ bool add_string_to_array(TALLOC_CTX *mem_ctx,
166 const char *str, const char ***strings, int *num)
168 char *dup_str = talloc_strdup(mem_ctx, str);
170 *strings = talloc_realloc(mem_ctx,
172 const char *, ((*num)+1));
174 if ((*strings == NULL) || (dup_str == NULL))
177 (*strings)[*num] = dup_str;
184 * Parse a string containing a boolean value.
186 * val will be set to the read value.
188 * @retval true if a boolean value was parsed, false otherwise.
190 _PUBLIC_ bool conv_str_bool(const char * str, bool * val)
195 if (str == NULL || *str == '\0') {
199 lval = strtol(str, &end, 10 /* base */);
200 if (end == NULL || *end != '\0' || end == str) {
201 return set_boolean(str, val);
204 *val = (lval) ? true : false;
209 * Convert a size specification like 16K into an integral number of bytes.
211 _PUBLIC_ bool conv_str_size(const char * str, uint64_t * val)
214 unsigned long long lval;
216 if (str == NULL || *str == '\0') {
220 lval = strtoull(str, &end, 10 /* base */);
221 if (end == NULL || end == str) {
226 if (strwicmp(end, "K") == 0) {
228 } else if (strwicmp(end, "M") == 0) {
229 lval *= (1024ULL * 1024ULL);
230 } else if (strwicmp(end, "G") == 0) {
231 lval *= (1024ULL * 1024ULL * 1024ULL);
232 } else if (strwicmp(end, "T") == 0) {
233 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
234 } else if (strwicmp(end, "P") == 0) {
235 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
241 *val = (uint64_t)lval;
246 * Parse a uint64_t value from a string
248 * val will be set to the value read.
250 * @retval true if parsing was successful, false otherwise
252 _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
255 unsigned long long lval;
257 if (str == NULL || *str == '\0') {
261 lval = strtoull(str, &end, 10 /* base */);
262 if (end == NULL || *end != '\0' || end == str) {
266 *val = (uint64_t)lval;
271 Do a case-insensitive, whitespace-ignoring string compare.
273 _PUBLIC_ int strwicmp(const char *psz1, const char *psz2)
275 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
276 /* appropriate value. */
279 else if (psz1 == NULL)
281 else if (psz2 == NULL)
284 /* sync the strings on first non-whitespace */
286 while (isspace((int)*psz1))
288 while (isspace((int)*psz2))
290 if (toupper((unsigned char)*psz1) != toupper((unsigned char)*psz2)
297 return (*psz1 - *psz2);
303 * @note The comparison is case-insensitive.
305 _PUBLIC_ bool strequal(const char *s1, const char *s2)
312 return strcasecmp(s1,s2) == 0;
315 _PUBLIC_ size_t ucs2_align(const void *base_ptr, const void *p, int flags)
317 if (flags & (STR_NOALIGN|STR_ASCII))
319 return PTR_DIFF(p, base_ptr) & 1;
325 _PUBLIC_ void string_replace(char *s, char oldc, char newc)
328 if (*s == oldc) *s = newc;