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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/iconv.h"
30 * @brief String utilities.
34 * Get the next token from a string, return False if none found.
35 * Handles double-quotes.
37 * Based on a routine by GJC@VILLAGE.COM.
38 * Extensively modified by Andrew.Tridgell@anu.edu.au
40 BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
51 /* default to simple separators */
55 /* find the first non sep char */
56 while (*s && strchr_m(sep,*s))
63 /* copy over the token */
64 for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
73 *ptr = (*s) ? s+1 : s;
80 Case insensitive string compararison
82 int StrCaseCmp(const char *s1, const char *s2)
84 codepoint_t c1=0, c2=0;
88 c1 = next_codepoint(s1, &size1);
89 c2 = next_codepoint(s2, &size2);
98 if (c1 == INVALID_CODEPOINT ||
99 c2 == INVALID_CODEPOINT) {
100 /* what else can we do?? */
104 if (toupper_w(c1) != toupper_w(c2)) {
115 * @note The comparison is case-insensitive.
117 BOOL strequal(const char *s1, const char *s2)
124 return StrCaseCmp(s1,s2) == 0;
128 Compare 2 strings (case sensitive).
130 BOOL strcsequal(const char *s1,const char *s2)
137 return strcmp(s1,s2) == 0;
142 Do a case-insensitive, whitespace-ignoring string compare.
144 int strwicmp(const char *psz1, const char *psz2)
146 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
147 /* appropriate value. */
150 else if (psz1 == NULL)
152 else if (psz2 == NULL)
155 /* sync the strings on first non-whitespace */
157 while (isspace((int)*psz1))
159 while (isspace((int)*psz2))
161 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
167 return (*psz1 - *psz2);
172 NOTE: oldc and newc must be 7 bit characters
174 void string_replace(char *s, char oldc, char newc)
178 codepoint_t c = next_codepoint(s, &size);
187 Trim the specified elements off the front and back of a string.
189 BOOL trim_string(char *s,const char *front,const char *back)
196 /* Ignore null or empty strings. */
197 if (!s || (s[0] == '\0'))
200 front_len = front? strlen(front) : 0;
201 back_len = back? strlen(back) : 0;
206 while (len && strncmp(s, front, front_len)==0) {
207 /* Must use memmove here as src & dest can
208 * easily overlap. Found by valgrind. JRA. */
209 memmove(s, s+front_len, (len-front_len)+1);
216 while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
217 s[len-back_len]='\0';
226 Find the number of 'c' chars in a string
228 size_t count_chars(const char *s, char c)
234 codepoint_t c2 = next_codepoint(s, &size);
235 if (c2 == c) count++;
243 Safe string copy into a known length string. maxlength does not
244 include the terminating zero.
246 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
251 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
256 /* We intentionally write out at the extremity of the destination
257 * string. If the destination is too short (e.g. pstrcpy into mallocd
258 * or fstring) then this should cause an error under a memory
260 dest[maxlength] = '\0';
261 if (PTR_DIFF(&len, dest) > 0) { /* check if destination is on the stack, ok if so */
262 log_suspicious_usage("safe_strcpy", src);
273 if (len > maxlength) {
274 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
275 (uint_t)(len-maxlength), len, maxlength, src));
279 memmove(dest, src, len);
285 Safe string cat into a string. maxlength does not
286 include the terminating zero.
288 char *safe_strcat(char *dest, const char *src, size_t maxlength)
290 size_t src_len, dest_len;
293 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
301 if (PTR_DIFF(&src_len, dest) > 0) { /* check if destination is on the stack, ok if so */
302 log_suspicious_usage("safe_strcat", src);
305 src_len = strlen(src);
306 dest_len = strlen(dest);
308 if (src_len + dest_len > maxlength) {
309 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
310 (int)(src_len + dest_len - maxlength), src));
311 if (maxlength > dest_len) {
312 memcpy(&dest[dest_len], src, maxlength - dest_len);
318 memcpy(&dest[dest_len], src, src_len);
319 dest[dest_len + src_len] = 0;
324 Paranoid strcpy into a buffer of given length (includes terminating
325 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
326 and replaces with '_'. Deliberately does *NOT* check for multibyte
327 characters. Don't change it !
330 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
334 if (maxlength == 0) {
335 /* can't fit any bytes at all! */
340 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
350 if (len >= maxlength)
353 if (!other_safe_chars)
354 other_safe_chars = "";
356 for(i = 0; i < len; i++) {
357 int val = (src[i] & 0xff);
358 if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val))
370 Like strncpy but always null terminates. Make sure there is room!
371 The variable n should always be one less than the available size.
374 char *StrnCpy(char *dest,const char *src,size_t n)
383 while (n-- && (*d++ = *src++))
391 Routine to get hex characters and turn them into a 16 byte array.
392 the array can be variable length, and any non-hex-numeric
393 characters are skipped. "0xnn" or "0Xnn" is specially catered
396 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
400 size_t strhex_to_str(char *p, size_t len, const char *strhex)
403 size_t num_chars = 0;
404 uint8_t lonybble, hinybble;
405 const char *hexchars = "0123456789ABCDEF";
406 char *p1 = NULL, *p2 = NULL;
408 for (i = 0; i < len && strhex[i] != 0; i++) {
409 if (strncasecmp(hexchars, "0x", 2) == 0) {
410 i++; /* skip two chars */
414 if (!(p1 = strchr_m(hexchars, toupper(strhex[i]))))
417 i++; /* next hex digit */
419 if (!(p2 = strchr_m(hexchars, toupper(strhex[i]))))
422 /* get the two nybbles */
423 hinybble = PTR_DIFF(p1, hexchars);
424 lonybble = PTR_DIFF(p2, hexchars);
426 p[num_chars] = (hinybble << 4) | lonybble;
435 DATA_BLOB strhex_to_data_blob(const char *strhex)
437 DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1);
439 ret_blob.length = strhex_to_str(ret_blob.data,
448 * Routine to print a buffer as HEX digits, into an allocated string.
450 void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
455 *out_hex_buffer = smb_xmalloc((len*2)+1);
456 hex_buffer = *out_hex_buffer;
458 for (i = 0; i < len; i++)
459 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
463 Check if a string is part of a list.
465 BOOL in_list(const char *s, const char *list, BOOL casesensitive)
473 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
475 if (strcmp(tok,s) == 0)
478 if (StrCaseCmp(tok,s) == 0)
486 Set a string value, allocing the space for the string
488 static BOOL string_init(char **dest,const char *src)
492 (*dest) = strdup(src);
493 if ((*dest) == NULL) {
494 DEBUG(0,("Out of memory in string_init\n"));
503 void string_free(char **s)
505 if (s) SAFE_FREE(*s);
509 Set a string value, deallocating any existing space, and allocing the space
512 BOOL string_set(char **dest, const char *src)
515 return string_init(dest,src);
519 Substitute a string for a pattern in another string. Make sure there is
522 This routine looks for pattern in s and replaces it with
523 insert. It may do multiple replacements.
525 Any of " ; ' $ or ` in the insert string are replaced with _
526 if len==0 then the string cannot be extended. This is different from the old
527 use of len==0 which was for no length checks to be done.
530 void string_sub(char *s,const char *pattern, const char *insert, size_t len)
535 if (!insert || !pattern || !*pattern || !s)
538 ls = (ssize_t)strlen(s);
539 lp = (ssize_t)strlen(pattern);
540 li = (ssize_t)strlen(insert);
543 len = ls + 1; /* len is number of *bytes* */
545 while (lp <= ls && (p = strstr(s,pattern))) {
546 if (ls + (li-lp) >= len) {
547 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
548 (int)(ls + (li-lp) - len),
553 memmove(p+li,p+lp,strlen(p+lp)+1);
578 Similar to string_sub() but allows for any character to be substituted.
580 if len==0 then the string cannot be extended. This is different from the old
581 use of len==0 which was for no length checks to be done.
584 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
589 if (!insert || !pattern || !s)
592 ls = (ssize_t)strlen(s);
593 lp = (ssize_t)strlen(pattern);
594 li = (ssize_t)strlen(insert);
600 len = ls + 1; /* len is number of *bytes* */
602 while (lp <= ls && (p = strstr(s,pattern))) {
603 if (ls + (li-lp) >= len) {
604 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
605 (int)(ls + (li-lp) - len),
610 memmove(p+li,p+lp,strlen(p+lp)+1);
612 memcpy(p, insert, li);
620 Strchr and strrchr_m are a bit complex on general multi-byte strings.
622 char *strchr_m(const char *s, char c)
624 /* characters below 0x3F are guaranteed to not appear in
625 non-initial position in multi-byte charsets */
626 if ((c & 0xC0) == 0) {
632 codepoint_t c2 = next_codepoint(s, &size);
634 return discard_const(s);
642 char *strrchr_m(const char *s, char c)
646 /* characters below 0x3F are guaranteed to not appear in
647 non-initial position in multi-byte charsets */
648 if ((c & 0xC0) == 0) {
649 return strrchr(s, c);
654 codepoint_t c2 = next_codepoint(s, &size);
656 ret = discard_const(s);
665 Convert a string to lower case, allocated with talloc
667 char *strlower_talloc(TALLOC_CTX *ctx, const char *src)
672 /* this takes advantage of the fact that upper/lower can't
673 change the length of a character by more than 1 byte */
674 dest = talloc_size(ctx, 2*(strlen(src))+1);
681 codepoint_t c = next_codepoint(src, &c_size);
686 c_size = push_codepoint(dest+size, c);
700 Convert a string to UPPER case, allocated with talloc
702 char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
707 /* this takes advantage of the fact that upper/lower can't
708 change the length of a character by more than 1 byte */
709 dest = talloc_size(ctx, 2*(strlen(src))+1);
716 codepoint_t c = next_codepoint(src, &c_size);
721 c_size = push_codepoint(dest+size, c);
735 Convert a string to lower case.
737 void strlower_m(char *s)
741 /* this is quite a common operation, so we want it to be
742 fast. We optimise for the ascii case, knowing that all our
743 supported multi-byte character sets are ascii-compatible
744 (ie. they match for the first 128 chars) */
745 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
746 *s = tolower((uint8_t)*s);
756 size_t c_size, c_size2;
757 codepoint_t c = next_codepoint(s, &c_size);
758 c_size2 = push_codepoint(d, tolower_w(c));
759 if (c_size2 > c_size) {
760 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
761 c, tolower_w(c), c_size, c_size2));
762 smb_panic("codepoint expansion in strlower_m\n");
771 Convert a string to UPPER case.
773 void strupper_m(char *s)
777 /* this is quite a common operation, so we want it to be
778 fast. We optimise for the ascii case, knowing that all our
779 supported multi-byte character sets are ascii-compatible
780 (ie. they match for the first 128 chars) */
781 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
782 *s = toupper((uint8_t)*s);
792 size_t c_size, c_size2;
793 codepoint_t c = next_codepoint(s, &c_size);
794 c_size2 = push_codepoint(d, toupper_w(c));
795 if (c_size2 > c_size) {
796 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
797 c, toupper_w(c), c_size, c_size2));
798 smb_panic("codepoint expansion in strupper_m\n");
807 Count the number of UCS2 characters in a string. Normally this will
808 be the same as the number of bytes in a string for single byte strings,
809 but will be different for multibyte.
811 size_t strlen_m(const char *s)
819 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
830 codepoint_t c = next_codepoint(s, &c_size);
843 Work out the number of multibyte chars in a string, including the NULL
846 size_t strlen_m_term(const char *s)
852 return strlen_m(s) + 1;
856 Unescape a URL encoded string, in place.
859 void rfc1738_unescape(char *buf)
863 while ((p=strchr_m(p,'+')))
868 while (p && *p && (p=strchr_m(p,'%'))) {
872 if (c1 >= '0' && c1 <= '9')
874 else if (c1 >= 'A' && c1 <= 'F')
876 else if (c1 >= 'a' && c1 <= 'f')
878 else {p++; continue;}
880 if (c2 >= '0' && c2 <= '9')
882 else if (c2 >= 'A' && c2 <= 'F')
884 else if (c2 >= 'a' && c2 <= 'f')
886 else {p++; continue;}
890 memmove(p+1, p+3, strlen(p+3)+1);
896 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
898 DATA_BLOB base64_decode_data_blob(const char *s)
900 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
902 int bit_offset, byte_offset, idx, i, n;
903 DATA_BLOB decoded = data_blob(s, strlen(s)+1);
904 uint8_t *d = decoded.data;
909 while (*s && (p=strchr_m(b64,*s))) {
910 idx = (int)(p - b64);
911 byte_offset = (i*6)/8;
912 bit_offset = (i*6)%8;
913 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
914 if (bit_offset < 3) {
915 d[byte_offset] |= (idx << (2-bit_offset));
918 d[byte_offset] |= (idx >> (bit_offset-2));
919 d[byte_offset+1] = 0;
920 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
932 * Decode a base64 string in-place - wrapper for the above
934 void base64_decode_inplace(char *s)
936 DATA_BLOB decoded = base64_decode_data_blob(s);
937 memcpy(s, decoded.data, decoded.length);
938 data_blob_free(&decoded);
941 s[decoded.length] = '\0';
945 * Encode a base64 string into a malloc()ed string caller to free.
947 *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
949 char * base64_encode_data_blob(DATA_BLOB data)
951 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
955 size_t len = data.length;
956 size_t output_len = data.length * 2;
957 char *result = malloc(output_len); /* get us plenty of space */
959 while (len-- && out_cnt < (data.length * 2) - 5) {
960 int c = (uint8_t) *(data.data++);
963 if (char_count == 3) {
964 result[out_cnt++] = b64[bits >> 18];
965 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
966 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
967 result[out_cnt++] = b64[bits & 0x3f];
974 if (char_count != 0) {
975 bits <<= 16 - (8 * char_count);
976 result[out_cnt++] = b64[bits >> 18];
977 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
978 if (char_count == 1) {
979 result[out_cnt++] = '=';
980 result[out_cnt++] = '=';
982 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
983 result[out_cnt++] = '=';
986 result[out_cnt] = '\0'; /* terminate */
991 size_t valgrind_strlen(const char *s)
994 for(count = 0; *s++; count++)
1002 format a string into length-prefixed dotted domain format, as used in NBT
1003 and in some ADS structures
1005 const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
1010 return talloc_strdup(mem_ctx, "");
1012 ret = talloc_size(mem_ctx, strlen(s)+2);
1017 memcpy(ret+1, s, strlen(s)+1);
1020 for (i=0;ret[i];i++) {
1021 if (ret[i] == '.') {
1022 char *p = strchr(ret+i+1, '.');
1024 ret[i] = p-(ret+i+1);
1026 ret[i] = strlen(ret+i+1);
1034 BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
1035 const char *str, const char ***strings, int *num)
1037 char *dup_str = talloc_strdup(mem_ctx, str);
1039 *strings = talloc_realloc(mem_ctx,
1041 const char *, ((*num)+1));
1043 if ((*strings == NULL) || (dup_str == NULL))
1046 (*strings)[*num] = dup_str;
1055 varient of strcmp() that handles NULL ptrs
1057 int strcmp_safe(const char *s1, const char *s2)
1062 if (s1 == NULL || s2 == NULL) {
1065 return strcmp(s1, s2);
1069 /*******************************************************************
1070 return the number of bytes occupied by a buffer in ASCII format
1071 the result includes the null termination
1072 limited by 'n' bytes
1073 ********************************************************************/
1074 size_t ascii_len_n(const char *src, size_t n)
1078 len = strnlen(src, n);
1087 /*******************************************************************
1088 Return a string representing a CIFS attribute for a file.
1089 ********************************************************************/
1090 char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
1097 {'V', FILE_ATTRIBUTE_VOLUME},
1098 {'D', FILE_ATTRIBUTE_DIRECTORY},
1099 {'A', FILE_ATTRIBUTE_ARCHIVE},
1100 {'H', FILE_ATTRIBUTE_HIDDEN},
1101 {'S', FILE_ATTRIBUTE_SYSTEM},
1102 {'N', FILE_ATTRIBUTE_NORMAL},
1103 {'R', FILE_ATTRIBUTE_READONLY},
1104 {'d', FILE_ATTRIBUTE_DEVICE},
1105 {'t', FILE_ATTRIBUTE_TEMPORARY},
1106 {'s', FILE_ATTRIBUTE_SPARSE},
1107 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
1108 {'c', FILE_ATTRIBUTE_COMPRESSED},
1109 {'o', FILE_ATTRIBUTE_OFFLINE},
1110 {'n', FILE_ATTRIBUTE_NONINDEXED},
1111 {'e', FILE_ATTRIBUTE_ENCRYPTED}
1115 ret = talloc_size(mem_ctx, ARRAY_SIZE(attr_strs)+1);
1120 for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
1121 if (attrib & attr_strs[i].attr) {
1122 ret[len++] = attr_strs[i].c;