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.
28 * @brief String utilities.
32 * Get the next token from a string, return False if none found.
33 * Handles double-quotes.
35 * Based on a routine by GJC@VILLAGE.COM.
36 * Extensively modified by Andrew.Tridgell@anu.edu.au
38 BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
49 /* default to simple separators */
53 /* find the first non sep char */
54 while (*s && strchr_m(sep,*s))
61 /* copy over the token */
62 for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
71 *ptr = (*s) ? s+1 : s;
77 static uint16_t tmpbuf[sizeof(pstring)];
81 Case insensitive string compararison.
83 static int StrCaseCmp_slow(const char *s1, const char *s2)
85 smb_ucs2_t *u1 = NULL;
89 if (convert_string_talloc(NULL, CH_UNIX, CH_UTF16, s1, strlen(s1)+1, (void **)&u1) == -1 ||
90 convert_string_talloc(u1, CH_UNIX, CH_UTF16, s2, strlen(s2)+1, (void **)&u2) == -1) {
92 /* fallback to a simple comparison */
93 return strcasecmp(s1, s2);
96 ret = strcasecmp_w(u1, u2);
104 Case insensitive string compararison, accelerated version
106 int StrCaseCmp(const char *s1, const char *s2)
111 char u1 = toupper(*s1);
112 char u2 = toupper(*s2);
120 if (*s1 == 0 || *s2 == 0) {
124 return StrCaseCmp_slow(s1, s2);
130 * @note The comparison is case-insensitive.
132 BOOL strequal(const char *s1, const char *s2)
139 return(StrCaseCmp(s1,s2)==0);
143 Compare 2 strings (case sensitive).
146 BOOL strcsequal(const char *s1,const char *s2)
153 return(strcmp(s1,s2)==0);
157 Do a case-insensitive, whitespace-ignoring string compare.
160 int strwicmp(const char *psz1, const char *psz2)
162 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
163 /* appropriate value. */
166 else if (psz1 == NULL)
168 else if (psz2 == NULL)
171 /* sync the strings on first non-whitespace */
173 while (isspace((int)*psz1))
175 while (isspace((int)*psz2))
177 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
183 return (*psz1 - *psz2);
188 NOTE: oldc and newc must be 7 bit characters
191 void string_replace(char *s,char oldc,char newc)
193 if (strchr(s, oldc)) {
194 push_ucs2(tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
195 string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc));
196 pull_ucs2(s, tmpbuf, strlen(s)+1, sizeof(tmpbuf), STR_TERMINATE);
201 Trim the specified elements off the front and back of a string.
204 BOOL trim_string(char *s,const char *front,const char *back)
211 /* Ignore null or empty strings. */
212 if (!s || (s[0] == '\0'))
215 front_len = front? strlen(front) : 0;
216 back_len = back? strlen(back) : 0;
221 while (len && strncmp(s, front, front_len)==0) {
222 /* Must use memmove here as src & dest can
223 * easily overlap. Found by valgrind. JRA. */
224 memmove(s, s+front_len, (len-front_len)+1);
231 while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
232 s[len-back_len]='\0';
241 Does a string have any uppercase chars in it?
244 BOOL strhasupper(const char *s)
247 push_ucs2(tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
248 for(ptr=tmpbuf;*ptr;ptr++)
255 Does a string have any lowercase chars in it?
258 BOOL strhaslower(const char *s)
261 push_ucs2(tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
262 for(ptr=tmpbuf;*ptr;ptr++)
269 Find the number of 'c' chars in a string
272 size_t count_chars(const char *s,char c)
276 smb_ucs2_t *alloc_tmpbuf = NULL;
278 if (push_ucs2_talloc(NULL, &alloc_tmpbuf, s) == (size_t)-1) {
282 for(count=0,ptr=alloc_tmpbuf;*ptr;ptr++)
283 if(*ptr==UCS2_CHAR(c))
286 talloc_free(alloc_tmpbuf);
291 Safe string copy into a known length string. maxlength does not
292 include the terminating zero.
295 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
300 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
305 /* We intentionally write out at the extremity of the destination
306 * string. If the destination is too short (e.g. pstrcpy into mallocd
307 * or fstring) then this should cause an error under a memory
309 dest[maxlength] = '\0';
310 if (PTR_DIFF(&len, dest) > 0) { /* check if destination is on the stack, ok if so */
311 log_suspicious_usage("safe_strcpy", src);
322 if (len > maxlength) {
323 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
324 (uint_t)(len-maxlength), len, maxlength, src));
328 memmove(dest, src, len);
334 Safe string cat into a string. maxlength does not
335 include the terminating zero.
338 char *safe_strcat(char *dest, const char *src, size_t maxlength)
340 size_t src_len, dest_len;
343 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
351 if (PTR_DIFF(&src_len, dest) > 0) { /* check if destination is on the stack, ok if so */
352 log_suspicious_usage("safe_strcat", src);
355 src_len = strlen(src);
356 dest_len = strlen(dest);
358 if (src_len + dest_len > maxlength) {
359 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
360 (int)(src_len + dest_len - maxlength), src));
361 if (maxlength > dest_len) {
362 memcpy(&dest[dest_len], src, maxlength - dest_len);
368 memcpy(&dest[dest_len], src, src_len);
369 dest[dest_len + src_len] = 0;
374 Paranoid strcpy into a buffer of given length (includes terminating
375 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
376 and replaces with '_'. Deliberately does *NOT* check for multibyte
377 characters. Don't change it !
380 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
384 if (maxlength == 0) {
385 /* can't fit any bytes at all! */
390 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
400 if (len >= maxlength)
403 if (!other_safe_chars)
404 other_safe_chars = "";
406 for(i = 0; i < len; i++) {
407 int val = (src[i] & 0xff);
408 if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val))
420 Like strncpy but always null terminates. Make sure there is room!
421 The variable n should always be one less than the available size.
424 char *StrnCpy(char *dest,const char *src,size_t n)
433 while (n-- && (*d++ = *src++))
441 Routine to get hex characters and turn them into a 16 byte array.
442 the array can be variable length, and any non-hex-numeric
443 characters are skipped. "0xnn" or "0Xnn" is specially catered
446 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
450 size_t strhex_to_str(char *p, size_t len, const char *strhex)
453 size_t num_chars = 0;
454 uint8_t lonybble, hinybble;
455 const char *hexchars = "0123456789ABCDEF";
456 char *p1 = NULL, *p2 = NULL;
458 for (i = 0; i < len && strhex[i] != 0; i++) {
459 if (strncasecmp(hexchars, "0x", 2) == 0) {
460 i++; /* skip two chars */
464 if (!(p1 = strchr_m(hexchars, toupper(strhex[i]))))
467 i++; /* next hex digit */
469 if (!(p2 = strchr_m(hexchars, toupper(strhex[i]))))
472 /* get the two nybbles */
473 hinybble = PTR_DIFF(p1, hexchars);
474 lonybble = PTR_DIFF(p2, hexchars);
476 p[num_chars] = (hinybble << 4) | lonybble;
485 DATA_BLOB strhex_to_data_blob(const char *strhex)
487 DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1);
489 ret_blob.length = strhex_to_str(ret_blob.data,
497 * Routine to print a buffer as HEX digits, into an allocated string.
500 void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
505 *out_hex_buffer = smb_xmalloc((len*2)+1);
506 hex_buffer = *out_hex_buffer;
508 for (i = 0; i < len; i++)
509 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
513 Check if a string is part of a list.
516 BOOL in_list(const char *s, const char *list, BOOL casesensitive)
524 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
526 if (strcmp(tok,s) == 0)
529 if (StrCaseCmp(tok,s) == 0)
537 Set a string value, allocing the space for the string
539 static BOOL string_init(char **dest,const char *src)
543 (*dest) = strdup(src);
544 if ((*dest) == NULL) {
545 DEBUG(0,("Out of memory in string_init\n"));
554 void string_free(char **s)
556 if (s) SAFE_FREE(*s);
560 Set a string value, deallocating any existing space, and allocing the space
563 BOOL string_set(char **dest, const char *src)
566 return string_init(dest,src);
570 Substitute a string for a pattern in another string. Make sure there is
573 This routine looks for pattern in s and replaces it with
574 insert. It may do multiple replacements.
576 Any of " ; ' $ or ` in the insert string are replaced with _
577 if len==0 then the string cannot be extended. This is different from the old
578 use of len==0 which was for no length checks to be done.
581 void string_sub(char *s,const char *pattern, const char *insert, size_t len)
586 if (!insert || !pattern || !*pattern || !s)
589 ls = (ssize_t)strlen(s);
590 lp = (ssize_t)strlen(pattern);
591 li = (ssize_t)strlen(insert);
594 len = ls + 1; /* len is number of *bytes* */
596 while (lp <= ls && (p = strstr(s,pattern))) {
597 if (ls + (li-lp) >= len) {
598 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
599 (int)(ls + (li-lp) - len),
604 memmove(p+li,p+lp,strlen(p+lp)+1);
629 Similar to string_sub() but allows for any character to be substituted.
631 if len==0 then the string cannot be extended. This is different from the old
632 use of len==0 which was for no length checks to be done.
635 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
640 if (!insert || !pattern || !s)
643 ls = (ssize_t)strlen(s);
644 lp = (ssize_t)strlen(pattern);
645 li = (ssize_t)strlen(insert);
651 len = ls + 1; /* len is number of *bytes* */
653 while (lp <= ls && (p = strstr(s,pattern))) {
654 if (ls + (li-lp) >= len) {
655 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
656 (int)(ls + (li-lp) - len),
661 memmove(p+li,p+lp,strlen(p+lp)+1);
663 memcpy(p, insert, li);
670 Write an octal as a string.
673 const char *octal_string(int i)
678 slprintf(ret, sizeof(ret)-1, "0%o", i);
684 Strchr and strrchr_m are very hard to do on general multi-byte strings.
685 We convert via ucs2 for now.
688 char *strchr_m(const char *s, char c)
694 /* characters below 0x3F are guaranteed to not appear in
695 non-initial position in multi-byte charsets */
696 if ((c & 0xC0) == 0) {
700 push_ucs2(ws, s, sizeof(ws), STR_TERMINATE);
701 p = strchr_w(ws, UCS2_CHAR(c));
705 pull_ucs2_pstring(s2, ws);
706 return discard_const_p(char, s+strlen(s2));
709 char *strrchr_m(const char *s, char c)
715 /* characters below 0x3F are guaranteed to not appear in
716 non-initial position in multi-byte charsets */
717 if ((c & 0xC0) == 0) {
718 return strrchr(s, c);
721 push_ucs2(ws, s, sizeof(ws), STR_TERMINATE);
722 p = strrchr_w(ws, UCS2_CHAR(c));
726 pull_ucs2_pstring(s2, ws);
727 return discard_const_p(char, s+strlen(s2));
731 Convert a string to lower case, allocated with talloc
734 char *strlower_talloc(TALLOC_CTX *ctx, const char *src)
740 size = push_ucs2_talloc(ctx, &buffer, src);
746 size = pull_ucs2_talloc(ctx, &dest, buffer);
752 Convert a string to UPPER case, allocated with talloc
755 char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
761 size = push_ucs2_talloc(ctx, &buffer, src);
767 size = pull_ucs2_talloc(ctx, &dest, buffer);
773 Convert a string to lower case.
776 void strlower_m(char *s)
779 /* this is quite a common operation, so we want it to be
780 fast. We optimise for the ascii case, knowing that all our
781 supported multi-byte character sets are ascii-compatible
782 (ie. they match for the first 128 chars) */
784 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
785 *s = tolower((uint8_t)*s);
792 /* I assume that lowercased string takes the same number of bytes
793 * as source string even in UTF-8 encoding. (VIV) */
794 lower = strlower_talloc(NULL, s);
796 safe_strcpy(s, lower, strlen(s));
802 Convert a string to UPPER case.
805 void strupper_m(char *s)
808 /* this is quite a common operation, so we want it to be
809 fast. We optimise for the ascii case, knowing that all our
810 supported multi-byte character sets are ascii-compatible
811 (ie. they match for the first 128 chars) */
813 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
814 *s = toupper((uint8_t)*s);
821 /* I assume that uppercased string takes the same number of bytes
822 * as source string even in UTF-8 encoding. (VIV) */
823 upper = strupper_talloc(NULL, s);
825 safe_strcpy(s, upper, strlen(s));
831 Count the number of UCS2 characters in a string. Normally this will
832 be the same as the number of bytes in a string for single byte strings,
833 but will be different for multibyte.
836 size_t strlen_m(const char *s)
847 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
856 SMB_ASSERT(push_ucs2_talloc(NULL, &tmp, s) != -1);
858 len = count + strlen_w(tmp);
865 Work out the number of multibyte chars in a string, including the NULL
868 size_t strlen_m_term(const char *s)
874 return strlen_m(s) + 1;
878 Return a RFC2254 binary string representation of a buffer.
879 Used in LDAP filters.
883 char *binary_string(char *buf, int len)
887 const char *hex = "0123456789ABCDEF";
888 s = malloc(len * 3 + 1);
891 for (j=i=0;i<len;i++) {
893 s[j+1] = hex[((uint8_t)buf[i]) >> 4];
894 s[j+2] = hex[((uint8_t)buf[i]) & 0xF];
902 Just a typesafety wrapper for snprintf into a pstring.
905 int pstr_sprintf(pstring s, const char *fmt, ...)
911 ret = vsnprintf(s, PSTRING_LEN, fmt, ap);
918 Some platforms don't have strndup.
920 char *strndup(const char *s, size_t n)
937 Some platforms don't have strnlen
939 size_t strnlen(const char *s, size_t n)
942 for (i=0; s[i] && i<n; i++)
949 List of Strings manipulation functions
952 #define S_LIST_ABS 16 /* List Allocation Block Size */
954 char **str_list_make(const char *string, const char *sep)
956 char **list, **rlist;
962 if (!string || !*string)
966 DEBUG(0,("str_list_make: Unable to allocate memory"));
969 if (!sep) sep = LIST_SEP;
975 while (next_token(&str, tok, sep, sizeof(tok))) {
978 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
980 DEBUG(0,("str_list_make: Unable to allocate memory"));
981 str_list_free(&list);
986 memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1)));
989 list[num] = strdup(tok);
991 DEBUG(0,("str_list_make: Unable to allocate memory"));
992 str_list_free(&list);
1004 BOOL str_list_copy(char ***dest, const char **src)
1006 char **list, **rlist;
1018 lsize += S_LIST_ABS;
1019 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
1021 DEBUG(0,("str_list_copy: Unable to re-allocate memory"));
1022 str_list_free(&list);
1026 memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1)));
1029 list[num] = strdup(src[num]);
1031 DEBUG(0,("str_list_copy: Unable to allocate memory"));
1032 str_list_free(&list);
1044 * Return true if all the elements of the list match exactly.
1046 BOOL str_list_compare(char **list1, char **list2)
1050 if (!list1 || !list2)
1051 return (list1 == list2);
1053 for (num = 0; list1[num]; num++) {
1056 if (!strcsequal(list1[num], list2[num]))
1060 return False; /* if list2 has more elements than list1 fail */
1065 void str_list_free(char ***list)
1069 if (!list || !*list)
1072 for(; *tlist; tlist++)
1077 BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
1080 ssize_t ls, lp, li, ld, i, d;
1089 lp = (ssize_t)strlen(pattern);
1090 li = (ssize_t)strlen(insert);
1095 ls = (ssize_t)strlen(s);
1097 while ((p = strstr(s, pattern))) {
1101 t = (char *) malloc(ls +ld +1);
1103 DEBUG(0,("str_list_substitute: Unable to allocate memory"));
1106 memcpy(t, *list, d);
1107 memcpy(t +d +li, p +lp, ls -d -lp +1);
1114 for (i = 0; i < li; i++) {
1115 switch (insert[i]) {
1127 t[d +i] = insert[i];
1139 #define IPSTR_LIST_SEP ","
1142 * Add ip string representation to ipstr list. Used also
1143 * as part of @function ipstr_list_make
1145 * @param ipstr_list pointer to string containing ip list;
1146 * MUST BE already allocated and IS reallocated if necessary
1147 * @param ipstr_size pointer to current size of ipstr_list (might be changed
1148 * as a result of reallocation)
1149 * @param ip IP address which is to be added to list
1150 * @return pointer to string appended with new ip and possibly
1151 * reallocated to new length
1154 char* ipstr_list_add(char** ipstr_list, const struct in_addr *ip)
1156 char* new_ipstr = NULL;
1158 /* arguments checking */
1159 if (!ipstr_list || !ip) return NULL;
1161 /* attempt to convert ip to a string and append colon separator to it */
1163 asprintf(&new_ipstr, "%s%s%s", *ipstr_list, IPSTR_LIST_SEP,inet_ntoa(*ip));
1164 SAFE_FREE(*ipstr_list);
1166 asprintf(&new_ipstr, "%s", inet_ntoa(*ip));
1168 *ipstr_list = new_ipstr;
1173 * Allocate and initialise an ipstr list using ip adresses
1174 * passed as arguments.
1176 * @param ipstr_list pointer to string meant to be allocated and set
1177 * @param ip_list array of ip addresses to place in the list
1178 * @param ip_count number of addresses stored in ip_list
1179 * @return pointer to allocated ip string
1182 char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_count)
1186 /* arguments checking */
1187 if (!ip_list && !ipstr_list) return 0;
1191 /* process ip addresses given as arguments */
1192 for (i = 0; i < ip_count; i++)
1193 *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
1195 return (*ipstr_list);
1200 * Parse given ip string list into array of ip addresses
1201 * (as in_addr structures)
1203 * @param ipstr ip string list to be parsed
1204 * @param ip_list pointer to array of ip addresses which is
1205 * allocated by this function and must be freed by caller
1206 * @return number of succesfully parsed addresses
1209 int ipstr_list_parse(const char* ipstr_list, struct in_addr** ip_list)
1214 if (!ipstr_list || !ip_list) return 0;
1216 for (*ip_list = NULL, count = 0;
1217 next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN);
1220 struct in_addr addr;
1222 /* convert single token to ip address */
1223 if ( (addr.s_addr = inet_addr(token_str)) == INADDR_NONE )
1226 /* prepare place for another in_addr structure */
1227 *ip_list = Realloc(*ip_list, (count + 1) * sizeof(struct in_addr));
1228 if (!*ip_list) return -1;
1230 (*ip_list)[count] = addr;
1238 * Safely free ip string list
1240 * @param ipstr_list ip string list to be freed
1243 void ipstr_list_free(char* ipstr_list)
1245 SAFE_FREE(ipstr_list);
1249 Unescape a URL encoded string, in place.
1252 void rfc1738_unescape(char *buf)
1256 while ((p=strchr_m(p,'+')))
1261 while (p && *p && (p=strchr_m(p,'%'))) {
1265 if (c1 >= '0' && c1 <= '9')
1267 else if (c1 >= 'A' && c1 <= 'F')
1269 else if (c1 >= 'a' && c1 <= 'f')
1271 else {p++; continue;}
1273 if (c2 >= '0' && c2 <= '9')
1275 else if (c2 >= 'A' && c2 <= 'F')
1277 else if (c2 >= 'a' && c2 <= 'f')
1279 else {p++; continue;}
1283 memmove(p+1, p+3, strlen(p+3)+1);
1288 static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1291 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
1293 DATA_BLOB base64_decode_data_blob(const char *s)
1295 int bit_offset, byte_offset, idx, i, n;
1296 DATA_BLOB decoded = data_blob(s, strlen(s)+1);
1297 uint8_t *d = decoded.data;
1302 while (*s && (p=strchr_m(b64,*s))) {
1303 idx = (int)(p - b64);
1304 byte_offset = (i*6)/8;
1305 bit_offset = (i*6)%8;
1306 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
1307 if (bit_offset < 3) {
1308 d[byte_offset] |= (idx << (2-bit_offset));
1311 d[byte_offset] |= (idx >> (bit_offset-2));
1312 d[byte_offset+1] = 0;
1313 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
1325 * Decode a base64 string in-place - wrapper for the above
1327 void base64_decode_inplace(char *s)
1329 DATA_BLOB decoded = base64_decode_data_blob(s);
1330 memcpy(s, decoded.data, decoded.length);
1331 data_blob_free(&decoded);
1333 /* null terminate */
1334 s[decoded.length] = '\0';
1338 * Encode a base64 string into a malloc()ed string caller to free.
1340 *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
1342 char * base64_encode_data_blob(DATA_BLOB data)
1347 size_t len = data.length;
1348 size_t output_len = data.length * 2;
1349 char *result = malloc(output_len); /* get us plenty of space */
1351 while (len-- && out_cnt < (data.length * 2) - 5) {
1352 int c = (uint8_t) *(data.data++);
1355 if (char_count == 3) {
1356 result[out_cnt++] = b64[bits >> 18];
1357 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
1358 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
1359 result[out_cnt++] = b64[bits & 0x3f];
1366 if (char_count != 0) {
1367 bits <<= 16 - (8 * char_count);
1368 result[out_cnt++] = b64[bits >> 18];
1369 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
1370 if (char_count == 1) {
1371 result[out_cnt++] = '=';
1372 result[out_cnt++] = '=';
1374 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
1375 result[out_cnt++] = '=';
1378 result[out_cnt] = '\0'; /* terminate */
1383 size_t valgrind_strlen(const char *s)
1386 for(count = 0; *s++; count++)
1394 format a string into length-prefixed dotted domain format, as used in NBT
1395 and in some ADS structures
1397 const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
1402 return talloc_strdup(mem_ctx, "");
1404 ret = talloc(mem_ctx, strlen(s)+2);
1409 memcpy(ret+1, s, strlen(s)+1);
1412 for (i=0;ret[i];i++) {
1413 if (ret[i] == '.') {
1414 char *p = strchr(ret+i+1, '.');
1416 ret[i] = p-(ret+i+1);
1418 ret[i] = strlen(ret+i+1);
1426 BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
1427 const char *str, const char ***strings, int *num)
1429 char *dup_str = talloc_strdup(mem_ctx, str);
1431 *strings = talloc_realloc(*strings,
1432 ((*num)+1) * sizeof(**strings));
1434 if ((*strings == NULL) || (dup_str == NULL))
1437 (*strings)[*num] = dup_str;
1446 varient of strcmp() that handles NULL ptrs
1448 int strcmp_safe(const char *s1, const char *s2)
1453 if (s1 == NULL || s2 == NULL) {
1456 return strcmp(s1, s2);
1460 /*******************************************************************
1461 Return a string representing a CIFS attribute for a file.
1462 ********************************************************************/
1463 char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
1470 {'V', FILE_ATTRIBUTE_VOLUME},
1471 {'D', FILE_ATTRIBUTE_DIRECTORY},
1472 {'A', FILE_ATTRIBUTE_ARCHIVE},
1473 {'H', FILE_ATTRIBUTE_HIDDEN},
1474 {'S', FILE_ATTRIBUTE_SYSTEM},
1475 {'R', FILE_ATTRIBUTE_READONLY},
1476 {'d', FILE_ATTRIBUTE_DEVICE},
1477 {'t', FILE_ATTRIBUTE_TEMPORARY},
1478 {'s', FILE_ATTRIBUTE_SPARSE},
1479 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
1480 {'c', FILE_ATTRIBUTE_COMPRESSED},
1481 {'o', FILE_ATTRIBUTE_OFFLINE},
1482 {'n', FILE_ATTRIBUTE_NONINDEXED},
1483 {'e', FILE_ATTRIBUTE_ENCRYPTED}
1487 ret = talloc(mem_ctx, ARRAY_SIZE(attr_strs)+1);
1492 for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
1493 if (attrib & attr_strs[i].attr) {
1494 ret[len++] = attr_strs[i].c;