2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) Simo Sorce 2001-2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Get the next token from a string, return False if none found.
26 * Handles double-quotes.
28 * Based on a routine by GJC@VILLAGE.COM.
29 * Extensively modified by Andrew.Tridgell@anu.edu.au
31 BOOL next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
42 /* default to simple separators */
46 /* find the first non sep char */
47 while (*s && strchr_m(sep,*s))
54 /* copy over the token */
55 for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
64 *ptr = (*s) ? s+1 : s;
70 static uint16_t tmpbuf[sizeof(pstring)];
73 Convert list of tokens to array; dependent on above routine.
74 Uses last_ptr from above - bit of a hack.
77 char **toktocliplist(const char *ptr, int *ctok, const char *sep)
86 while(*s && strchr_m(sep,*s))
95 while(*s && (!strchr_m(sep,*s)))
97 while(*s && strchr_m(sep,*s))
104 if (!(ret=iret=malloc(ictok*sizeof(char *))))
119 Case insensitive string compararison.
121 static int StrCaseCmp_slow(const char *s1, const char *s2)
126 if (convert_string_allocate(CH_UNIX, CH_UTF16, s1, strlen(s1)+1, &u1) == -1 ||
127 convert_string_allocate(CH_UNIX, CH_UTF16, s2, strlen(s2)+1, &u2) == -1) {
128 /* fallback to a simple comparison */
129 return strcasecmp(s1, s2);
132 ret = strcasecmp_w(u1, u2);
141 Case insensitive string compararison, accelerated version
143 int StrCaseCmp(const char *s1, const char *s2)
148 char u1 = toupper(*s1);
149 char u2 = toupper(*s2);
157 if (*s1 == 0 || *s2 == 0) {
161 return StrCaseCmp_slow(s1, s2);
167 * @note The comparison is case-insensitive.
169 BOOL strequal(const char *s1, const char *s2)
176 return(StrCaseCmp(s1,s2)==0);
180 Compare 2 strings (case sensitive).
183 BOOL strcsequal(const char *s1,const char *s2)
190 return(strcmp(s1,s2)==0);
194 Do a case-insensitive, whitespace-ignoring string compare.
197 int strwicmp(const char *psz1, const char *psz2)
199 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
200 /* appropriate value. */
203 else if (psz1 == NULL)
205 else if (psz2 == NULL)
208 /* sync the strings on first non-whitespace */
210 while (isspace((int)*psz1))
212 while (isspace((int)*psz2))
214 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
220 return (*psz1 - *psz2);
224 Convert a string to upper case, but don't modify it.
227 char *strupper_talloc(TALLOC_CTX *mem_ctx, const char *s)
231 str = talloc_strdup(mem_ctx, s);
240 NOTE: oldc and newc must be 7 bit characters
243 void string_replace(char *s,char oldc,char newc)
245 if (strchr(s, oldc)) {
246 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
247 string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc));
248 pull_ucs2(NULL, s, tmpbuf, strlen(s)+1, sizeof(tmpbuf), STR_TERMINATE);
253 Count the number of characters in a string. Normally this will
254 be the same as the number of bytes in a string for single byte strings,
255 but will be different for multibyte.
258 size_t str_charnum(const char *s)
260 uint16_t tmpbuf2[sizeof(pstring)];
261 push_ucs2(NULL, tmpbuf2,s, sizeof(tmpbuf2), STR_TERMINATE);
262 return strlen_w(tmpbuf2);
266 Count the number of characters in a string. Normally this will
267 be the same as the number of bytes in a string for single byte strings,
268 but will be different for multibyte.
271 size_t str_ascii_charnum(const char *s)
274 push_ascii(tmpbuf2, s, sizeof(tmpbuf2), STR_TERMINATE);
275 return strlen(tmpbuf2);
279 Trim the specified elements off the front and back of a string.
282 BOOL trim_string(char *s,const char *front,const char *back)
289 /* Ignore null or empty strings. */
290 if (!s || (s[0] == '\0'))
293 front_len = front? strlen(front) : 0;
294 back_len = back? strlen(back) : 0;
299 while (len && strncmp(s, front, front_len)==0) {
300 memcpy(s, s+front_len, (len-front_len)+1);
307 while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
308 s[len-back_len]='\0';
317 Does a string have any uppercase chars in it?
320 BOOL strhasupper(const char *s)
323 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
324 for(ptr=tmpbuf;*ptr;ptr++)
331 Does a string have any lowercase chars in it?
334 BOOL strhaslower(const char *s)
337 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
338 for(ptr=tmpbuf;*ptr;ptr++)
345 Find the number of 'c' chars in a string
348 size_t count_chars(const char *s,char c)
352 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
353 for(count=0,ptr=tmpbuf;*ptr;ptr++)
354 if(*ptr==UCS2_CHAR(c))
360 Safe string copy into a known length string. maxlength does not
361 include the terminating zero.
364 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
369 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
374 /* We intentionally write out at the extremity of the destination
375 * string. If the destination is too short (e.g. pstrcpy into mallocd
376 * or fstring) then this should cause an error under a memory
378 dest[maxlength] = '\0';
379 if (PTR_DIFF(&len, dest) > 0) { /* check if destination is on the stack, ok if so */
380 log_suspicious_usage("safe_strcpy", src);
391 if (len > maxlength) {
392 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
393 (uint_t)(len-maxlength), len, maxlength, src));
397 memmove(dest, src, len);
403 Safe string cat into a string. maxlength does not
404 include the terminating zero.
407 char *safe_strcat(char *dest, const char *src, size_t maxlength)
409 size_t src_len, dest_len;
412 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
420 if (PTR_DIFF(&src_len, dest) > 0) { /* check if destination is on the stack, ok if so */
421 log_suspicious_usage("safe_strcat", src);
424 src_len = strlen(src);
425 dest_len = strlen(dest);
427 if (src_len + dest_len > maxlength) {
428 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
429 (int)(src_len + dest_len - maxlength), src));
430 if (maxlength > dest_len) {
431 memcpy(&dest[dest_len], src, maxlength - dest_len);
437 memcpy(&dest[dest_len], src, src_len);
438 dest[dest_len + src_len] = 0;
443 Paranoid strcpy into a buffer of given length (includes terminating
444 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
445 and replaces with '_'. Deliberately does *NOT* check for multibyte
446 characters. Don't change it !
449 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
453 if (maxlength == 0) {
454 /* can't fit any bytes at all! */
459 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
469 if (len >= maxlength)
472 if (!other_safe_chars)
473 other_safe_chars = "";
475 for(i = 0; i < len; i++) {
476 int val = (src[i] & 0xff);
477 if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val))
489 Like strncpy but always null terminates. Make sure there is room!
490 The variable n should always be one less than the available size.
493 char *StrnCpy(char *dest,const char *src,size_t n)
502 while (n-- && (*d++ = *src++))
510 Check if a string is part of a list.
513 BOOL in_list(const char *s, const char *list, BOOL casesensitive)
521 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
523 if (strcmp(tok,s) == 0)
526 if (StrCaseCmp(tok,s) == 0)
534 Set a string value, allocing the space for the string
536 static BOOL string_init(char **dest,const char *src)
540 (*dest) = strdup(src);
541 if ((*dest) == NULL) {
542 DEBUG(0,("Out of memory in string_init\n"));
551 void string_free(char **s)
553 if (s) SAFE_FREE(*s);
557 Set a string value, deallocating any existing space, and allocing the space
560 BOOL string_set(char **dest, const char *src)
563 return string_init(dest,src);
567 Substitute a string for a pattern in another string. Make sure there is
570 This routine looks for pattern in s and replaces it with
571 insert. It may do multiple replacements.
573 Any of " ; ' $ or ` in the insert string are replaced with _
574 if len==0 then the string cannot be extended. This is different from the old
575 use of len==0 which was for no length checks to be done.
578 void string_sub(char *s,const char *pattern, const char *insert, size_t len)
583 if (!insert || !pattern || !*pattern || !s)
586 ls = (ssize_t)strlen(s);
587 lp = (ssize_t)strlen(pattern);
588 li = (ssize_t)strlen(insert);
591 len = ls + 1; /* len is number of *bytes* */
593 while (lp <= ls && (p = strstr(s,pattern))) {
594 if (ls + (li-lp) >= len) {
595 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
596 (int)(ls + (li-lp) - len),
601 memmove(p+li,p+lp,strlen(p+lp)+1);
626 Similar to string_sub() but allows for any character to be substituted.
628 if len==0 then the string cannot be extended. This is different from the old
629 use of len==0 which was for no length checks to be done.
632 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
637 if (!insert || !pattern || !s)
640 ls = (ssize_t)strlen(s);
641 lp = (ssize_t)strlen(pattern);
642 li = (ssize_t)strlen(insert);
648 len = ls + 1; /* len is number of *bytes* */
650 while (lp <= ls && (p = strstr(s,pattern))) {
651 if (ls + (li-lp) >= len) {
652 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
653 (int)(ls + (li-lp) - len),
658 memmove(p+li,p+lp,strlen(p+lp)+1);
660 memcpy(p, insert, li);
667 Write an octal as a string.
670 const char *octal_string(int i)
675 slprintf(ret, sizeof(ret)-1, "0%o", i);
681 Strchr and strrchr_m are very hard to do on general multi-byte strings.
682 We convert via ucs2 for now.
685 char *strchr_m(const char *s, char c)
691 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
692 p = strchr_w(ws, UCS2_CHAR(c));
696 pull_ucs2_pstring(s2, ws);
697 return (char *)(s+strlen(s2));
700 char *strrchr_m(const char *s, char c)
706 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
707 p = strrchr_w(ws, UCS2_CHAR(c));
711 pull_ucs2_pstring(s2, ws);
712 return (char *)(s+strlen(s2));
716 Convert a string to lower case.
719 void strlower_m(char *s)
721 /* this is quite a common operation, so we want it to be
722 fast. We optimise for the ascii case, knowing that all our
723 supported multi-byte character sets are ascii-compatible
724 (ie. they match for the first 128 chars) */
726 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
727 *s = tolower((uint8_t)*s);
734 /* I assume that lowercased string takes the same number of bytes
735 * as source string even in UTF-8 encoding. (VIV) */
736 unix_strlower(s,strlen(s)+1,s,strlen(s)+1);
740 Convert a string to upper case.
743 void strupper_m(char *s)
745 /* this is quite a common operation, so we want it to be
746 fast. We optimise for the ascii case, knowing that all our
747 supported multi-byte character sets are ascii-compatible
748 (ie. they match for the first 128 chars) */
750 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
751 *s = toupper((uint8_t)*s);
758 /* I assume that lowercased string takes the same number of bytes
759 * as source string even in multibyte encoding. (VIV) */
760 unix_strupper(s,strlen(s)+1,s,strlen(s)+1);
765 work out the number of multibyte chars in a string
767 size_t strlen_m(const char *s)
775 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
784 push_ucs2(NULL,tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
785 return count + strlen_w(tmpbuf);
789 Work out the number of multibyte chars in a string, including the NULL
792 size_t strlen_m_term(const char *s)
798 return strlen_m(s) + 1;
802 Convert a string to upper case.
805 char *strdup_upper(const char *s)
809 DEBUG(0, ("strdup_upper: Out of memory!\n"));
817 Return a RFC2254 binary string representation of a buffer.
818 Used in LDAP filters.
822 char *binary_string(char *buf, int len)
826 const char *hex = "0123456789ABCDEF";
827 s = malloc(len * 3 + 1);
830 for (j=i=0;i<len;i++) {
832 s[j+1] = hex[((uint8_t)buf[i]) >> 4];
833 s[j+2] = hex[((uint8_t)buf[i]) & 0xF];
841 Just a typesafety wrapper for snprintf into a pstring.
844 int pstr_sprintf(pstring s, const char *fmt, ...)
850 ret = vsnprintf(s, PSTRING_LEN, fmt, ap);
857 Some platforms don't have strndup.
859 char *strndup(const char *s, size_t n)
876 Some platforms don't have strnlen
878 size_t strnlen(const char *s, size_t n)
881 for (i=0; s[i] && i<n; i++)
888 List of Strings manipulation functions
891 #define S_LIST_ABS 16 /* List Allocation Block Size */
893 char **str_list_make(const char *string, const char *sep)
895 char **list, **rlist;
901 if (!string || !*string)
905 DEBUG(0,("str_list_make: Unable to allocate memory"));
908 if (!sep) sep = LIST_SEP;
914 while (next_token(&str, tok, sep, sizeof(tok))) {
917 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
919 DEBUG(0,("str_list_make: Unable to allocate memory"));
920 str_list_free(&list);
925 memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1)));
928 list[num] = strdup(tok);
930 DEBUG(0,("str_list_make: Unable to allocate memory"));
931 str_list_free(&list);
943 BOOL str_list_copy(char ***dest, const char **src)
945 char **list, **rlist;
958 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
960 DEBUG(0,("str_list_copy: Unable to re-allocate memory"));
961 str_list_free(&list);
965 memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1)));
968 list[num] = strdup(src[num]);
970 DEBUG(0,("str_list_copy: Unable to allocate memory"));
971 str_list_free(&list);
983 * Return true if all the elements of the list match exactly.
985 BOOL str_list_compare(char **list1, char **list2)
989 if (!list1 || !list2)
990 return (list1 == list2);
992 for (num = 0; list1[num]; num++) {
995 if (!strcsequal(list1[num], list2[num]))
999 return False; /* if list2 has more elements than list1 fail */
1004 void str_list_free(char ***list)
1008 if (!list || !*list)
1011 for(; *tlist; tlist++)
1016 BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
1019 ssize_t ls, lp, li, ld, i, d;
1028 lp = (ssize_t)strlen(pattern);
1029 li = (ssize_t)strlen(insert);
1034 ls = (ssize_t)strlen(s);
1036 while ((p = strstr(s, pattern))) {
1040 t = (char *) malloc(ls +ld +1);
1042 DEBUG(0,("str_list_substitute: Unable to allocate memory"));
1045 memcpy(t, *list, d);
1046 memcpy(t +d +li, p +lp, ls -d -lp +1);
1053 for (i = 0; i < li; i++) {
1054 switch (insert[i]) {
1066 t[d +i] = insert[i];
1078 #define IPSTR_LIST_SEP ","
1081 * Add ip string representation to ipstr list. Used also
1082 * as part of @function ipstr_list_make
1084 * @param ipstr_list pointer to string containing ip list;
1085 * MUST BE already allocated and IS reallocated if necessary
1086 * @param ipstr_size pointer to current size of ipstr_list (might be changed
1087 * as a result of reallocation)
1088 * @param ip IP address which is to be added to list
1089 * @return pointer to string appended with new ip and possibly
1090 * reallocated to new length
1093 char* ipstr_list_add(char** ipstr_list, const struct in_addr *ip)
1095 char* new_ipstr = NULL;
1097 /* arguments checking */
1098 if (!ipstr_list || !ip) return NULL;
1100 /* attempt to convert ip to a string and append colon separator to it */
1102 asprintf(&new_ipstr, "%s%s%s", *ipstr_list, IPSTR_LIST_SEP,inet_ntoa(*ip));
1103 SAFE_FREE(*ipstr_list);
1105 asprintf(&new_ipstr, "%s", inet_ntoa(*ip));
1107 *ipstr_list = new_ipstr;
1112 * Allocate and initialise an ipstr list using ip adresses
1113 * passed as arguments.
1115 * @param ipstr_list pointer to string meant to be allocated and set
1116 * @param ip_list array of ip addresses to place in the list
1117 * @param ip_count number of addresses stored in ip_list
1118 * @return pointer to allocated ip string
1121 char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_count)
1125 /* arguments checking */
1126 if (!ip_list && !ipstr_list) return 0;
1130 /* process ip addresses given as arguments */
1131 for (i = 0; i < ip_count; i++)
1132 *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
1134 return (*ipstr_list);
1139 * Parse given ip string list into array of ip addresses
1140 * (as in_addr structures)
1142 * @param ipstr ip string list to be parsed
1143 * @param ip_list pointer to array of ip addresses which is
1144 * allocated by this function and must be freed by caller
1145 * @return number of succesfully parsed addresses
1148 int ipstr_list_parse(const char* ipstr_list, struct in_addr** ip_list)
1153 if (!ipstr_list || !ip_list) return 0;
1155 for (*ip_list = NULL, count = 0;
1156 next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN);
1159 struct in_addr addr;
1161 /* convert single token to ip address */
1162 if ( (addr.s_addr = inet_addr(token_str)) == INADDR_NONE )
1165 /* prepare place for another in_addr structure */
1166 *ip_list = Realloc(*ip_list, (count + 1) * sizeof(struct in_addr));
1167 if (!*ip_list) return -1;
1169 (*ip_list)[count] = addr;
1177 * Safely free ip string list
1179 * @param ipstr_list ip string list to be freed
1182 void ipstr_list_free(char* ipstr_list)
1184 SAFE_FREE(ipstr_list);
1188 Routine to get hex characters and turn them into a 16 byte array.
1189 the array can be variable length, and any non-hex-numeric
1190 characters are skipped. "0xnn" or "0Xnn" is specially catered
1193 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1197 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1200 size_t num_chars = 0;
1201 uint8_t lonybble, hinybble;
1202 const char *hexchars = "0123456789ABCDEF";
1203 char *p1 = NULL, *p2 = NULL;
1205 for (i = 0; i < len && strhex[i] != 0; i++) {
1206 if (strncasecmp(hexchars, "0x", 2) == 0) {
1207 i++; /* skip two chars */
1211 if (!(p1 = strchr_m(hexchars, toupper(strhex[i]))))
1214 i++; /* next hex digit */
1216 if (!(p2 = strchr_m(hexchars, toupper(strhex[i]))))
1219 /* get the two nybbles */
1220 hinybble = PTR_DIFF(p1, hexchars);
1221 lonybble = PTR_DIFF(p2, hexchars);
1223 p[num_chars] = (hinybble << 4) | lonybble;
1232 DATA_BLOB strhex_to_data_blob(const char *strhex)
1234 DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1);
1236 ret_blob.length = strhex_to_str(ret_blob.data,
1244 * Routine to print a buffer as HEX digits, into an allocated string.
1247 void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
1252 *out_hex_buffer = smb_xmalloc((len*2)+1);
1253 hex_buffer = *out_hex_buffer;
1255 for (i = 0; i < len; i++)
1256 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
1261 Unescape a URL encoded string, in place.
1264 void rfc1738_unescape(char *buf)
1268 while ((p=strchr_m(p,'+')))
1273 while (p && *p && (p=strchr_m(p,'%'))) {
1277 if (c1 >= '0' && c1 <= '9')
1279 else if (c1 >= 'A' && c1 <= 'F')
1281 else if (c1 >= 'a' && c1 <= 'f')
1283 else {p++; continue;}
1285 if (c2 >= '0' && c2 <= '9')
1287 else if (c2 >= 'A' && c2 <= 'F')
1289 else if (c2 >= 'a' && c2 <= 'f')
1291 else {p++; continue;}
1295 memmove(p+1, p+3, strlen(p+3)+1);
1300 static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1303 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
1305 DATA_BLOB base64_decode_data_blob(const char *s)
1307 int bit_offset, byte_offset, idx, i, n;
1308 DATA_BLOB decoded = data_blob(s, strlen(s)+1);
1309 uint8_t *d = decoded.data;
1314 while (*s && (p=strchr_m(b64,*s))) {
1315 idx = (int)(p - b64);
1316 byte_offset = (i*6)/8;
1317 bit_offset = (i*6)%8;
1318 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
1319 if (bit_offset < 3) {
1320 d[byte_offset] |= (idx << (2-bit_offset));
1323 d[byte_offset] |= (idx >> (bit_offset-2));
1324 d[byte_offset+1] = 0;
1325 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
1337 * Decode a base64 string in-place - wrapper for the above
1339 void base64_decode_inplace(char *s)
1341 DATA_BLOB decoded = base64_decode_data_blob(s);
1342 memcpy(s, decoded.data, decoded.length);
1343 data_blob_free(&decoded);
1345 /* null terminate */
1346 s[decoded.length] = '\0';
1350 * Encode a base64 string into a malloc()ed string caller to free.
1352 *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
1354 char * base64_encode_data_blob(DATA_BLOB data)
1359 size_t len = data.length;
1360 size_t output_len = data.length * 2;
1361 char *result = malloc(output_len); /* get us plenty of space */
1363 while (len-- && out_cnt < (data.length * 2) - 5) {
1364 int c = (uint8_t) *(data.data++);
1367 if (char_count == 3) {
1368 result[out_cnt++] = b64[bits >> 18];
1369 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
1370 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
1371 result[out_cnt++] = b64[bits & 0x3f];
1378 if (char_count != 0) {
1379 bits <<= 16 - (8 * char_count);
1380 result[out_cnt++] = b64[bits >> 18];
1381 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
1382 if (char_count == 1) {
1383 result[out_cnt++] = '=';
1384 result[out_cnt++] = '=';
1386 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
1387 result[out_cnt++] = '=';
1390 result[out_cnt] = '\0'; /* terminate */
1395 size_t valgrind_strlen(const char *s)
1398 for(count = 0; *s++; count++)
1406 format a string into length-prefixed dotted domain format, as used in NBT
1407 and in some ADS structures
1409 const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
1414 return talloc_strdup(mem_ctx, "");
1416 ret = talloc(mem_ctx, strlen(s)+2);
1421 memcpy(ret+1, s, strlen(s)+1);
1424 for (i=0;ret[i];i++) {
1425 if (ret[i] == '.') {
1426 char *p = strchr(ret+i+1, '.');
1428 ret[i] = p-(ret+i+1);
1430 ret[i] = strlen(ret+i+1);
1438 BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
1439 const char *str, const char ***strings, int *num)
1441 char *dup_str = talloc_strdup(mem_ctx, str);
1443 *strings = talloc_realloc(*strings,
1444 ((*num)+1) * sizeof(**strings));
1446 if ((*strings == NULL) || (dup_str == NULL))
1449 (*strings)[*num] = dup_str;
1458 varient of strcmp() that handles NULL ptrs
1460 int strcmp_safe(const char *s1, const char *s2)
1465 if (s1 == NULL || s2 == NULL) {
1468 return strcmp(s1, s2);
1472 /*******************************************************************
1473 Return a string representing a CIFS attribute for a file.
1474 ********************************************************************/
1475 char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
1482 {'V', FILE_ATTRIBUTE_VOLUME},
1483 {'D', FILE_ATTRIBUTE_DIRECTORY},
1484 {'A', FILE_ATTRIBUTE_ARCHIVE},
1485 {'H', FILE_ATTRIBUTE_HIDDEN},
1486 {'S', FILE_ATTRIBUTE_SYSTEM},
1487 {'R', FILE_ATTRIBUTE_READONLY},
1488 {'d', FILE_ATTRIBUTE_DEVICE},
1489 {'t', FILE_ATTRIBUTE_TEMPORARY},
1490 {'s', FILE_ATTRIBUTE_SPARSE},
1491 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
1492 {'c', FILE_ATTRIBUTE_COMPRESSED},
1493 {'o', FILE_ATTRIBUTE_OFFLINE},
1494 {'n', FILE_ATTRIBUTE_NONINDEXED},
1495 {'e', FILE_ATTRIBUTE_ENCRYPTED}
1499 ret = talloc(mem_ctx, ARRAY_SIZE(attr_strs)+1);
1504 for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
1505 if (attrib & attr_strs[i].attr) {
1506 ret[len++] = attr_strs[i].c;