r23029: fixed formatting
[Samba.git] / source / lib / util / util_str.c
blob86cd3176c511a69d961b40f03e254edf57556572
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
26 #include "libcli/raw/smb.h"
27 #include "pstring.h"
28 #include "system/locale.h"
30 /**
31 * @file
32 * @brief String utilities.
33 **/
36 /**
37 Trim the specified elements off the front and back of a string.
38 **/
39 _PUBLIC_ BOOL trim_string(char *s,const char *front,const char *back)
41 BOOL ret = False;
42 size_t front_len;
43 size_t back_len;
44 size_t len;
46 /* Ignore null or empty strings. */
47 if (!s || (s[0] == '\0'))
48 return False;
50 front_len = front? strlen(front) : 0;
51 back_len = back? strlen(back) : 0;
53 len = strlen(s);
55 if (front_len) {
56 while (len && strncmp(s, front, front_len)==0) {
57 /* Must use memmove here as src & dest can
58 * easily overlap. Found by valgrind. JRA. */
59 memmove(s, s+front_len, (len-front_len)+1);
60 len -= front_len;
61 ret=True;
65 if (back_len) {
66 while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
67 s[len-back_len]='\0';
68 len -= back_len;
69 ret=True;
72 return ret;
75 /**
76 Find the number of 'c' chars in a string
77 **/
78 _PUBLIC_ size_t count_chars(const char *s, char c)
80 size_t count = 0;
82 while (*s) {
83 if (*s == c) count++;
84 s ++;
87 return count;
92 /**
93 Safe string copy into a known length string. maxlength does not
94 include the terminating zero.
95 **/
96 _PUBLIC_ char *safe_strcpy(char *dest,const char *src, size_t maxlength)
98 size_t len;
100 if (!dest) {
101 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
102 return NULL;
105 #ifdef DEVELOPER
106 /* We intentionally write out at the extremity of the destination
107 * string. If the destination is too short (e.g. pstrcpy into mallocd
108 * or fstring) then this should cause an error under a memory
109 * checker. */
110 dest[maxlength] = '\0';
111 if (PTR_DIFF(&len, dest) > 0) { /* check if destination is on the stack, ok if so */
112 log_suspicious_usage("safe_strcpy", src);
114 #endif
116 if (!src) {
117 *dest = 0;
118 return dest;
121 len = strlen(src);
123 if (len > maxlength) {
124 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
125 (uint_t)(len-maxlength), (unsigned)len, (unsigned)maxlength, src));
126 len = maxlength;
129 memmove(dest, src, len);
130 dest[len] = 0;
131 return dest;
135 Safe string cat into a string. maxlength does not
136 include the terminating zero.
138 _PUBLIC_ char *safe_strcat(char *dest, const char *src, size_t maxlength)
140 size_t src_len, dest_len;
142 if (!dest) {
143 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
144 return NULL;
147 if (!src)
148 return dest;
150 #ifdef DEVELOPER
151 if (PTR_DIFF(&src_len, dest) > 0) { /* check if destination is on the stack, ok if so */
152 log_suspicious_usage("safe_strcat", src);
154 #endif
155 src_len = strlen(src);
156 dest_len = strlen(dest);
158 if (src_len + dest_len > maxlength) {
159 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
160 (int)(src_len + dest_len - maxlength), src));
161 if (maxlength > dest_len) {
162 memcpy(&dest[dest_len], src, maxlength - dest_len);
164 dest[maxlength] = 0;
165 return NULL;
168 memcpy(&dest[dest_len], src, src_len);
169 dest[dest_len + src_len] = 0;
170 return dest;
174 Routine to get hex characters and turn them into a 16 byte array.
175 the array can be variable length, and any non-hex-numeric
176 characters are skipped. "0xnn" or "0Xnn" is specially catered
177 for.
179 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
183 _PUBLIC_ size_t strhex_to_str(char *p, size_t len, const char *strhex)
185 size_t i;
186 size_t num_chars = 0;
187 uint8_t lonybble, hinybble;
188 const char *hexchars = "0123456789ABCDEF";
189 char *p1 = NULL, *p2 = NULL;
191 for (i = 0; i < len && strhex[i] != 0; i++) {
192 if (strncasecmp(hexchars, "0x", 2) == 0) {
193 i++; /* skip two chars */
194 continue;
197 if (!(p1 = strchr(hexchars, toupper((unsigned char)strhex[i]))))
198 break;
200 i++; /* next hex digit */
202 if (!(p2 = strchr(hexchars, toupper((unsigned char)strhex[i]))))
203 break;
205 /* get the two nybbles */
206 hinybble = PTR_DIFF(p1, hexchars);
207 lonybble = PTR_DIFF(p2, hexchars);
209 p[num_chars] = (hinybble << 4) | lonybble;
210 num_chars++;
212 p1 = NULL;
213 p2 = NULL;
215 return num_chars;
218 /**
219 * Parse a hex string and return a data blob.
221 _PUBLIC_ DATA_BLOB strhex_to_data_blob(const char *strhex)
223 DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1);
225 ret_blob.length = strhex_to_str((char *)ret_blob.data,
226 strlen(strhex),
227 strhex);
229 return ret_blob;
234 * Routine to print a buffer as HEX digits, into an allocated string.
236 _PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
238 int i;
239 char *hex_buffer;
241 *out_hex_buffer = smb_xmalloc((len*2)+1);
242 hex_buffer = *out_hex_buffer;
244 for (i = 0; i < len; i++)
245 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
249 Set a string value, allocing the space for the string
251 static BOOL string_init(char **dest,const char *src)
253 if (!src) src = "";
255 (*dest) = strdup(src);
256 if ((*dest) == NULL) {
257 DEBUG(0,("Out of memory in string_init\n"));
258 return False;
260 return True;
264 Free a string value.
266 _PUBLIC_ void string_free(char **s)
268 if (s) SAFE_FREE(*s);
272 Set a string value, deallocating any existing space, and allocing the space
273 for the string
275 _PUBLIC_ BOOL string_set(char **dest, const char *src)
277 string_free(dest);
278 return string_init(dest,src);
282 Substitute a string for a pattern in another string. Make sure there is
283 enough room!
285 This routine looks for pattern in s and replaces it with
286 insert. It may do multiple replacements.
288 Any of " ; ' $ or ` in the insert string are replaced with _
289 if len==0 then the string cannot be extended. This is different from the old
290 use of len==0 which was for no length checks to be done.
293 _PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len)
295 char *p;
296 ssize_t ls,lp,li, i;
298 if (!insert || !pattern || !*pattern || !s)
299 return;
301 ls = (ssize_t)strlen(s);
302 lp = (ssize_t)strlen(pattern);
303 li = (ssize_t)strlen(insert);
305 if (len == 0)
306 len = ls + 1; /* len is number of *bytes* */
308 while (lp <= ls && (p = strstr(s,pattern))) {
309 if (ls + (li-lp) >= len) {
310 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
311 (int)(ls + (li-lp) - len),
312 pattern, (int)len));
313 break;
315 if (li != lp) {
316 memmove(p+li,p+lp,strlen(p+lp)+1);
318 for (i=0;i<li;i++) {
319 switch (insert[i]) {
320 case '`':
321 case '"':
322 case '\'':
323 case ';':
324 case '$':
325 case '%':
326 case '\r':
327 case '\n':
328 p[i] = '_';
329 break;
330 default:
331 p[i] = insert[i];
334 s = p + li;
335 ls += (li-lp);
341 Similar to string_sub() but allows for any character to be substituted.
342 Use with caution!
343 if len==0 then the string cannot be extended. This is different from the old
344 use of len==0 which was for no length checks to be done.
347 _PUBLIC_ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
349 char *p;
350 ssize_t ls,lp,li;
352 if (!insert || !pattern || !s)
353 return;
355 ls = (ssize_t)strlen(s);
356 lp = (ssize_t)strlen(pattern);
357 li = (ssize_t)strlen(insert);
359 if (!*pattern)
360 return;
362 if (len == 0)
363 len = ls + 1; /* len is number of *bytes* */
365 while (lp <= ls && (p = strstr(s,pattern))) {
366 if (ls + (li-lp) >= len) {
367 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
368 (int)(ls + (li-lp) - len),
369 pattern, (int)len));
370 break;
372 if (li != lp) {
373 memmove(p+li,p+lp,strlen(p+lp)+1);
375 memcpy(p, insert, li);
376 s = p + li;
377 ls += (li-lp);
384 Unescape a URL encoded string, in place.
387 _PUBLIC_ void rfc1738_unescape(char *buf)
389 char *p=buf;
391 while ((p=strchr(p,'+')))
392 *p = ' ';
394 p = buf;
396 while (p && *p && (p=strchr(p,'%'))) {
397 int c1 = p[1];
398 int c2 = p[2];
400 if (c1 >= '0' && c1 <= '9')
401 c1 = c1 - '0';
402 else if (c1 >= 'A' && c1 <= 'F')
403 c1 = 10 + c1 - 'A';
404 else if (c1 >= 'a' && c1 <= 'f')
405 c1 = 10 + c1 - 'a';
406 else {p++; continue;}
408 if (c2 >= '0' && c2 <= '9')
409 c2 = c2 - '0';
410 else if (c2 >= 'A' && c2 <= 'F')
411 c2 = 10 + c2 - 'A';
412 else if (c2 >= 'a' && c2 <= 'f')
413 c2 = 10 + c2 - 'a';
414 else {p++; continue;}
416 *p = (c1<<4) | c2;
418 memmove(p+1, p+3, strlen(p+3)+1);
419 p++;
423 #ifdef VALGRIND
424 size_t valgrind_strlen(const char *s)
426 size_t count;
427 for(count = 0; *s++; count++)
429 return count;
431 #endif
435 format a string into length-prefixed dotted domain format, as used in NBT
436 and in some ADS structures
438 _PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
440 char *ret;
441 int i;
442 if (!s || !*s) {
443 return talloc_strdup(mem_ctx, "");
445 ret = talloc_size(mem_ctx, strlen(s)+2);
446 if (!ret) {
447 return ret;
450 memcpy(ret+1, s, strlen(s)+1);
451 ret[0] = '.';
453 for (i=0;ret[i];i++) {
454 if (ret[i] == '.') {
455 char *p = strchr(ret+i+1, '.');
456 if (p) {
457 ret[i] = p-(ret+i+1);
458 } else {
459 ret[i] = strlen(ret+i+1);
464 return ret;
468 * Add a string to an array of strings.
470 * num should be a pointer to an integer that holds the current
471 * number of elements in strings. It will be updated by this function.
473 _PUBLIC_ BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
474 const char *str, const char ***strings, int *num)
476 char *dup_str = talloc_strdup(mem_ctx, str);
478 *strings = talloc_realloc(mem_ctx,
479 *strings,
480 const char *, ((*num)+1));
482 if ((*strings == NULL) || (dup_str == NULL))
483 return False;
485 (*strings)[*num] = dup_str;
486 *num += 1;
488 return True;
494 varient of strcmp() that handles NULL ptrs
496 _PUBLIC_ int strcmp_safe(const char *s1, const char *s2)
498 if (s1 == s2) {
499 return 0;
501 if (s1 == NULL || s2 == NULL) {
502 return s1?-1:1;
504 return strcmp(s1, s2);
509 return the number of bytes occupied by a buffer in ASCII format
510 the result includes the null termination
511 limited by 'n' bytes
513 _PUBLIC_ size_t ascii_len_n(const char *src, size_t n)
515 size_t len;
517 len = strnlen(src, n);
518 if (len+1 <= n) {
519 len += 1;
522 return len;
527 Return a string representing a CIFS attribute for a file.
529 _PUBLIC_ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
531 int i, len;
532 const struct {
533 char c;
534 uint16_t attr;
535 } attr_strs[] = {
536 {'V', FILE_ATTRIBUTE_VOLUME},
537 {'D', FILE_ATTRIBUTE_DIRECTORY},
538 {'A', FILE_ATTRIBUTE_ARCHIVE},
539 {'H', FILE_ATTRIBUTE_HIDDEN},
540 {'S', FILE_ATTRIBUTE_SYSTEM},
541 {'N', FILE_ATTRIBUTE_NORMAL},
542 {'R', FILE_ATTRIBUTE_READONLY},
543 {'d', FILE_ATTRIBUTE_DEVICE},
544 {'t', FILE_ATTRIBUTE_TEMPORARY},
545 {'s', FILE_ATTRIBUTE_SPARSE},
546 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
547 {'c', FILE_ATTRIBUTE_COMPRESSED},
548 {'o', FILE_ATTRIBUTE_OFFLINE},
549 {'n', FILE_ATTRIBUTE_NONINDEXED},
550 {'e', FILE_ATTRIBUTE_ENCRYPTED}
552 char *ret;
554 ret = talloc_size(mem_ctx, ARRAY_SIZE(attr_strs)+1);
555 if (!ret) {
556 return NULL;
559 for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
560 if (attrib & attr_strs[i].attr) {
561 ret[len++] = attr_strs[i].c;
565 ret[len] = 0;
567 return ret;
571 Set a boolean variable from the text value stored in the passed string.
572 Returns True in success, False if the passed string does not correctly
573 represent a boolean.
576 _PUBLIC_ BOOL set_boolean(const char *boolean_string, BOOL *boolean)
578 if (strwicmp(boolean_string, "yes") == 0 ||
579 strwicmp(boolean_string, "true") == 0 ||
580 strwicmp(boolean_string, "on") == 0 ||
581 strwicmp(boolean_string, "1") == 0) {
582 *boolean = True;
583 return True;
584 } else if (strwicmp(boolean_string, "no") == 0 ||
585 strwicmp(boolean_string, "false") == 0 ||
586 strwicmp(boolean_string, "off") == 0 ||
587 strwicmp(boolean_string, "0") == 0) {
588 *boolean = False;
589 return True;
591 return False;
595 * Parse a string containing a boolean value.
597 * val will be set to the read value.
599 * @retval True if a boolean value was parsed, False otherwise.
601 _PUBLIC_ BOOL conv_str_bool(const char * str, BOOL * val)
603 char * end = NULL;
604 long lval;
606 if (str == NULL || *str == '\0') {
607 return False;
610 lval = strtol(str, &end, 10 /* base */);
611 if (end == NULL || *end != '\0' || end == str) {
612 return set_boolean(str, val);
615 *val = (lval) ? True : False;
616 return True;
620 * Convert a size specification like 16K into an integral number of bytes.
622 _PUBLIC_ BOOL conv_str_size(const char * str, uint64_t * val)
624 char * end = NULL;
625 unsigned long long lval;
627 if (str == NULL || *str == '\0') {
628 return False;
631 lval = strtoull(str, &end, 10 /* base */);
632 if (end == NULL || end == str) {
633 return False;
636 if (*end) {
637 if (strwicmp(end, "K") == 0) {
638 lval *= 1024ULL;
639 } else if (strwicmp(end, "M") == 0) {
640 lval *= (1024ULL * 1024ULL);
641 } else if (strwicmp(end, "G") == 0) {
642 lval *= (1024ULL * 1024ULL * 1024ULL);
643 } else if (strwicmp(end, "T") == 0) {
644 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
645 } else if (strwicmp(end, "P") == 0) {
646 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
647 } else {
648 return False;
652 *val = (uint64_t)lval;
653 return True;
657 * Parse a uint64_t value from a string
659 * val will be set to the value read.
661 * @retval True if parsing was successful, False otherwise
663 _PUBLIC_ BOOL conv_str_u64(const char * str, uint64_t * val)
665 char * end = NULL;
666 unsigned long long lval;
668 if (str == NULL || *str == '\0') {
669 return False;
672 lval = strtoull(str, &end, 10 /* base */);
673 if (end == NULL || *end != '\0' || end == str) {
674 return False;
677 *val = (uint64_t)lval;
678 return True;
682 return the number of bytes occupied by a buffer in CH_UTF16 format
683 the result includes the null termination
685 _PUBLIC_ size_t utf16_len(const void *buf)
687 size_t len;
689 for (len = 0; SVAL(buf,len); len += 2) ;
691 return len + 2;
695 return the number of bytes occupied by a buffer in CH_UTF16 format
696 the result includes the null termination
697 limited by 'n' bytes
699 _PUBLIC_ size_t utf16_len_n(const void *src, size_t n)
701 size_t len;
703 for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;
705 if (len+2 <= n) {
706 len += 2;
709 return len;
712 _PUBLIC_ size_t ucs2_align(const void *base_ptr, const void *p, int flags)
714 if (flags & (STR_NOALIGN|STR_ASCII))
715 return 0;
716 return PTR_DIFF(p, base_ptr) & 1;
720 Do a case-insensitive, whitespace-ignoring string compare.
722 _PUBLIC_ int strwicmp(const char *psz1, const char *psz2)
724 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
725 /* appropriate value. */
726 if (psz1 == psz2)
727 return (0);
728 else if (psz1 == NULL)
729 return (-1);
730 else if (psz2 == NULL)
731 return (1);
733 /* sync the strings on first non-whitespace */
734 while (1) {
735 while (isspace((int)*psz1))
736 psz1++;
737 while (isspace((int)*psz2))
738 psz2++;
739 if (toupper((unsigned char)*psz1) != toupper((unsigned char)*psz2)
740 || *psz1 == '\0'
741 || *psz2 == '\0')
742 break;
743 psz1++;
744 psz2++;
746 return (*psz1 - *psz2);
750 String replace.
752 _PUBLIC_ void string_replace(char *s, char oldc, char newc)
754 while (*s) {
755 if (*s == oldc) *s = newc;
756 s++;
761 * Compare 2 strings.
763 * @note The comparison is case-insensitive.
765 _PUBLIC_ BOOL strequal(const char *s1, const char *s2)
767 if (s1 == s2)
768 return(True);
769 if (!s1 || !s2)
770 return(False);
772 return strcasecmp(s1,s2) == 0;