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;
71 This is like next_token but is not re-entrant and "remembers" the first
72 parameter so you can pass NULL. This is useful for user interface code
73 but beware the fact that it is not re-entrant!
76 static char *last_ptr=NULL;
78 BOOL next_token_nr(const char **ptr, char *buff, const char *sep, size_t bufsize)
82 ptr = (const char **)&last_ptr;
84 ret = next_token(ptr, buff, sep, bufsize);
89 static uint16_t tmpbuf[sizeof(pstring)];
92 Convert list of tokens to array; dependent on above routine.
93 Uses last_ptr from above - bit of a hack.
96 char **toktocliplist(int *ctok, const char *sep)
105 while(*s && strchr_m(sep,*s))
114 while(*s && (!strchr_m(sep,*s)))
116 while(*s && strchr_m(sep,*s))
123 if (!(ret=iret=malloc(ictok*sizeof(char *))))
138 Case insensitive string compararison.
141 int StrCaseCmp(const char *s, const char *t)
144 unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1));
145 unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2));
146 return strcmp(buf1,buf2);
150 Case insensitive string compararison, length limited.
153 int StrnCaseCmp(const char *s, const char *t, size_t n)
156 unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1));
157 unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2));
158 return strncmp(buf1,buf2,n);
164 * @note The comparison is case-insensitive.
166 BOOL strequal(const char *s1, const char *s2)
173 return(StrCaseCmp(s1,s2)==0);
177 * Compare 2 strings up to and including the nth char.
179 * @note The comparison is case-insensitive.
181 BOOL strnequal(const char *s1,const char *s2,size_t n)
185 if (!s1 || !s2 || !n)
188 return(StrnCaseCmp(s1,s2,n)==0);
192 Compare 2 strings (case sensitive).
195 BOOL strcsequal(const char *s1,const char *s2)
202 return(strcmp(s1,s2)==0);
206 Do a case-insensitive, whitespace-ignoring string compare.
209 int strwicmp(const char *psz1, const char *psz2)
211 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
212 /* appropriate value. */
215 else if (psz1 == NULL)
217 else if (psz2 == NULL)
220 /* sync the strings on first non-whitespace */
222 while (isspace((int)*psz1))
224 while (isspace((int)*psz2))
226 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0'
232 return (*psz1 - *psz2);
236 Convert a string to upper case, but don't modify it.
239 char *strupper_talloc(TALLOC_CTX *mem_ctx, const char *s)
243 str = talloc_strdup(mem_ctx, s);
252 NOTE: oldc and newc must be 7 bit characters
255 void string_replace(char *s,char oldc,char newc)
257 if (strchr(s, oldc)) {
258 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
259 string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc));
260 pull_ucs2(NULL, s, tmpbuf, strlen(s)+1, sizeof(tmpbuf), STR_TERMINATE);
265 Count the number of characters in a string. Normally this will
266 be the same as the number of bytes in a string for single byte strings,
267 but will be different for multibyte.
270 size_t str_charnum(const char *s)
272 uint16_t tmpbuf2[sizeof(pstring)];
273 push_ucs2(NULL, tmpbuf2,s, sizeof(tmpbuf2), STR_TERMINATE);
274 return strlen_w(tmpbuf2);
278 Count the number of characters in a string. Normally this will
279 be the same as the number of bytes in a string for single byte strings,
280 but will be different for multibyte.
283 size_t str_ascii_charnum(const char *s)
286 push_ascii(tmpbuf2, s, sizeof(tmpbuf2), STR_TERMINATE);
287 return strlen(tmpbuf2);
291 Trim the specified elements off the front and back of a string.
294 BOOL trim_string(char *s,const char *front,const char *back)
301 /* Ignore null or empty strings. */
302 if (!s || (s[0] == '\0'))
305 front_len = front? strlen(front) : 0;
306 back_len = back? strlen(back) : 0;
311 while (len && strncmp(s, front, front_len)==0) {
312 memcpy(s, s+front_len, (len-front_len)+1);
319 while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
320 s[len-back_len]='\0';
329 Does a string have any uppercase chars in it?
332 BOOL strhasupper(const char *s)
335 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
336 for(ptr=tmpbuf;*ptr;ptr++)
343 Does a string have any lowercase chars in it?
346 BOOL strhaslower(const char *s)
349 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
350 for(ptr=tmpbuf;*ptr;ptr++)
357 Find the number of 'c' chars in a string
360 size_t count_chars(const char *s,char c)
364 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
365 for(count=0,ptr=tmpbuf;*ptr;ptr++)
366 if(*ptr==UCS2_CHAR(c))
372 Safe string copy into a known length string. maxlength does not
373 include the terminating zero.
376 char *safe_strcpy(char *dest,const char *src, size_t maxlength)
381 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
386 /* We intentionally write out at the extremity of the destination
387 * string. If the destination is too short (e.g. pstrcpy into mallocd
388 * or fstring) then this should cause an error under a memory
390 dest[maxlength] = '\0';
391 if (PTR_DIFF(&len, dest) > 0) { /* check if destination is on the stack, ok if so */
392 log_suspicious_usage("safe_strcpy", src);
403 if (len > maxlength) {
404 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
405 (uint_t)(len-maxlength), len, maxlength, src));
409 memmove(dest, src, len);
415 Safe string cat into a string. maxlength does not
416 include the terminating zero.
419 char *safe_strcat(char *dest, const char *src, size_t maxlength)
421 size_t src_len, dest_len;
424 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
432 if (PTR_DIFF(&src_len, dest) > 0) { /* check if destination is on the stack, ok if so */
433 log_suspicious_usage("safe_strcat", src);
436 src_len = strlen(src);
437 dest_len = strlen(dest);
439 if (src_len + dest_len > maxlength) {
440 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
441 (int)(src_len + dest_len - maxlength), src));
442 if (maxlength > dest_len) {
443 memcpy(&dest[dest_len], src, maxlength - dest_len);
449 memcpy(&dest[dest_len], src, src_len);
450 dest[dest_len + src_len] = 0;
455 Paranoid strcpy into a buffer of given length (includes terminating
456 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
457 and replaces with '_'. Deliberately does *NOT* check for multibyte
458 characters. Don't change it !
461 char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
465 if (maxlength == 0) {
466 /* can't fit any bytes at all! */
471 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
481 if (len >= maxlength)
484 if (!other_safe_chars)
485 other_safe_chars = "";
487 for(i = 0; i < len; i++) {
488 int val = (src[i] & 0xff);
489 if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val))
501 Like strncpy but always null terminates. Make sure there is room!
502 The variable n should always be one less than the available size.
505 char *StrnCpy(char *dest,const char *src,size_t n)
514 while (n-- && (*d++ = *src++))
522 Check if a string is part of a list.
525 BOOL in_list(const char *s, const char *list, BOOL casesensitive)
533 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
535 if (strcmp(tok,s) == 0)
538 if (StrCaseCmp(tok,s) == 0)
546 Set a string value, allocing the space for the string
548 static BOOL string_init(char **dest,const char *src)
552 (*dest) = strdup(src);
553 if ((*dest) == NULL) {
554 DEBUG(0,("Out of memory in string_init\n"));
563 void string_free(char **s)
565 if (s) SAFE_FREE(*s);
569 Set a string value, deallocating any existing space, and allocing the space
572 BOOL string_set(char **dest, const char *src)
575 return string_init(dest,src);
579 Substitute a string for a pattern in another string. Make sure there is
582 This routine looks for pattern in s and replaces it with
583 insert. It may do multiple replacements.
585 Any of " ; ' $ or ` in the insert string are replaced with _
586 if len==0 then the string cannot be extended. This is different from the old
587 use of len==0 which was for no length checks to be done.
590 void string_sub(char *s,const char *pattern, const char *insert, size_t len)
595 if (!insert || !pattern || !*pattern || !s)
598 ls = (ssize_t)strlen(s);
599 lp = (ssize_t)strlen(pattern);
600 li = (ssize_t)strlen(insert);
603 len = ls + 1; /* len is number of *bytes* */
605 while (lp <= ls && (p = strstr(s,pattern))) {
606 if (ls + (li-lp) >= len) {
607 DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
608 (int)(ls + (li-lp) - len),
613 memmove(p+li,p+lp,strlen(p+lp)+1);
638 Similar to string_sub() but allows for any character to be substituted.
640 if len==0 then the string cannot be extended. This is different from the old
641 use of len==0 which was for no length checks to be done.
644 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
649 if (!insert || !pattern || !s)
652 ls = (ssize_t)strlen(s);
653 lp = (ssize_t)strlen(pattern);
654 li = (ssize_t)strlen(insert);
660 len = ls + 1; /* len is number of *bytes* */
662 while (lp <= ls && (p = strstr(s,pattern))) {
663 if (ls + (li-lp) >= len) {
664 DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
665 (int)(ls + (li-lp) - len),
670 memmove(p+li,p+lp,strlen(p+lp)+1);
672 memcpy(p, insert, li);
679 Write an octal as a string.
682 const char *octal_string(int i)
687 slprintf(ret, sizeof(ret)-1, "0%o", i);
693 Strchr and strrchr_m are very hard to do on general multi-byte strings.
694 We convert via ucs2 for now.
697 char *strchr_m(const char *s, char c)
703 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
704 p = strchr_w(ws, UCS2_CHAR(c));
708 pull_ucs2_pstring(s2, ws);
709 return (char *)(s+strlen(s2));
712 char *strrchr_m(const char *s, char c)
718 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
719 p = strrchr_w(ws, UCS2_CHAR(c));
723 pull_ucs2_pstring(s2, ws);
724 return (char *)(s+strlen(s2));
728 Convert a string to lower case.
731 void strlower_m(char *s)
733 /* this is quite a common operation, so we want it to be
734 fast. We optimise for the ascii case, knowing that all our
735 supported multi-byte character sets are ascii-compatible
736 (ie. they match for the first 128 chars) */
738 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
739 *s = tolower((uint8_t)*s);
746 /* I assume that lowercased string takes the same number of bytes
747 * as source string even in UTF-8 encoding. (VIV) */
748 unix_strlower(s,strlen(s)+1,s,strlen(s)+1);
752 Convert a string to upper case.
755 void strupper_m(char *s)
757 /* this is quite a common operation, so we want it to be
758 fast. We optimise for the ascii case, knowing that all our
759 supported multi-byte character sets are ascii-compatible
760 (ie. they match for the first 128 chars) */
762 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
763 *s = toupper((uint8_t)*s);
770 /* I assume that lowercased string takes the same number of bytes
771 * as source string even in multibyte encoding. (VIV) */
772 unix_strupper(s,strlen(s)+1,s,strlen(s)+1);
777 work out the number of multibyte chars in a string
779 size_t strlen_m(const char *s)
787 while (*s && !(((uint8_t)s[0]) & 0x7F)) {
796 push_ucs2(NULL,tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
797 return count + strlen_w(tmpbuf);
801 Work out the number of multibyte chars in a string, including the NULL
804 size_t strlen_m_term(const char *s)
810 return strlen_m(s) + 1;
814 Convert a string to upper case.
817 char *strdup_upper(const char *s)
821 DEBUG(0, ("strdup_upper: Out of memory!\n"));
829 Return a RFC2254 binary string representation of a buffer.
830 Used in LDAP filters.
834 char *binary_string(char *buf, int len)
838 const char *hex = "0123456789ABCDEF";
839 s = malloc(len * 3 + 1);
842 for (j=i=0;i<len;i++) {
844 s[j+1] = hex[((uint8_t)buf[i]) >> 4];
845 s[j+2] = hex[((uint8_t)buf[i]) & 0xF];
853 Just a typesafety wrapper for snprintf into a pstring.
856 int pstr_sprintf(pstring s, const char *fmt, ...)
862 ret = vsnprintf(s, PSTRING_LEN, fmt, ap);
869 Some platforms don't have strndup.
871 char *strndup(const char *s, size_t n)
888 Some platforms don't have strnlen
890 size_t strnlen(const char *s, size_t n)
893 for (i=0; s[i] && i<n; i++)
900 List of Strings manipulation functions
903 #define S_LIST_ABS 16 /* List Allocation Block Size */
905 char **str_list_make(const char *string, const char *sep)
907 char **list, **rlist;
913 if (!string || !*string)
917 DEBUG(0,("str_list_make: Unable to allocate memory"));
920 if (!sep) sep = LIST_SEP;
926 while (next_token(&str, tok, sep, sizeof(tok))) {
929 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
931 DEBUG(0,("str_list_make: Unable to allocate memory"));
932 str_list_free(&list);
937 memset (&list[num], 0, ((sizeof(char**)) * (S_LIST_ABS +1)));
940 list[num] = strdup(tok);
942 DEBUG(0,("str_list_make: Unable to allocate memory"));
943 str_list_free(&list);
955 BOOL str_list_copy(char ***dest, const char **src)
957 char **list, **rlist;
970 rlist = (char **)Realloc(list, ((sizeof(char **)) * (lsize +1)));
972 DEBUG(0,("str_list_copy: Unable to re-allocate memory"));
973 str_list_free(&list);
977 memset (&list[num], 0, ((sizeof(char **)) * (S_LIST_ABS +1)));
980 list[num] = strdup(src[num]);
982 DEBUG(0,("str_list_copy: Unable to allocate memory"));
983 str_list_free(&list);
995 * Return true if all the elements of the list match exactly.
997 BOOL str_list_compare(char **list1, char **list2)
1001 if (!list1 || !list2)
1002 return (list1 == list2);
1004 for (num = 0; list1[num]; num++) {
1007 if (!strcsequal(list1[num], list2[num]))
1011 return False; /* if list2 has more elements than list1 fail */
1016 void str_list_free(char ***list)
1020 if (!list || !*list)
1023 for(; *tlist; tlist++)
1028 BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
1031 ssize_t ls, lp, li, ld, i, d;
1040 lp = (ssize_t)strlen(pattern);
1041 li = (ssize_t)strlen(insert);
1046 ls = (ssize_t)strlen(s);
1048 while ((p = strstr(s, pattern))) {
1052 t = (char *) malloc(ls +ld +1);
1054 DEBUG(0,("str_list_substitute: Unable to allocate memory"));
1057 memcpy(t, *list, d);
1058 memcpy(t +d +li, p +lp, ls -d -lp +1);
1065 for (i = 0; i < li; i++) {
1066 switch (insert[i]) {
1078 t[d +i] = insert[i];
1090 #define IPSTR_LIST_SEP ","
1093 * Add ip string representation to ipstr list. Used also
1094 * as part of @function ipstr_list_make
1096 * @param ipstr_list pointer to string containing ip list;
1097 * MUST BE already allocated and IS reallocated if necessary
1098 * @param ipstr_size pointer to current size of ipstr_list (might be changed
1099 * as a result of reallocation)
1100 * @param ip IP address which is to be added to list
1101 * @return pointer to string appended with new ip and possibly
1102 * reallocated to new length
1105 char* ipstr_list_add(char** ipstr_list, const struct in_addr *ip)
1107 char* new_ipstr = NULL;
1109 /* arguments checking */
1110 if (!ipstr_list || !ip) return NULL;
1112 /* attempt to convert ip to a string and append colon separator to it */
1114 asprintf(&new_ipstr, "%s%s%s", *ipstr_list, IPSTR_LIST_SEP,inet_ntoa(*ip));
1115 SAFE_FREE(*ipstr_list);
1117 asprintf(&new_ipstr, "%s", inet_ntoa(*ip));
1119 *ipstr_list = new_ipstr;
1124 * Allocate and initialise an ipstr list using ip adresses
1125 * passed as arguments.
1127 * @param ipstr_list pointer to string meant to be allocated and set
1128 * @param ip_list array of ip addresses to place in the list
1129 * @param ip_count number of addresses stored in ip_list
1130 * @return pointer to allocated ip string
1133 char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_count)
1137 /* arguments checking */
1138 if (!ip_list && !ipstr_list) return 0;
1142 /* process ip addresses given as arguments */
1143 for (i = 0; i < ip_count; i++)
1144 *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
1146 return (*ipstr_list);
1151 * Parse given ip string list into array of ip addresses
1152 * (as in_addr structures)
1154 * @param ipstr ip string list to be parsed
1155 * @param ip_list pointer to array of ip addresses which is
1156 * allocated by this function and must be freed by caller
1157 * @return number of succesfully parsed addresses
1160 int ipstr_list_parse(const char* ipstr_list, struct in_addr** ip_list)
1165 if (!ipstr_list || !ip_list) return 0;
1167 for (*ip_list = NULL, count = 0;
1168 next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN);
1171 struct in_addr addr;
1173 /* convert single token to ip address */
1174 if ( (addr.s_addr = inet_addr(token_str)) == INADDR_NONE )
1177 /* prepare place for another in_addr structure */
1178 *ip_list = Realloc(*ip_list, (count + 1) * sizeof(struct in_addr));
1179 if (!*ip_list) return -1;
1181 (*ip_list)[count] = addr;
1189 * Safely free ip string list
1191 * @param ipstr_list ip string list to be freed
1194 void ipstr_list_free(char* ipstr_list)
1196 SAFE_FREE(ipstr_list);
1200 Routine to get hex characters and turn them into a 16 byte array.
1201 the array can be variable length, and any non-hex-numeric
1202 characters are skipped. "0xnn" or "0Xnn" is specially catered
1205 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1209 size_t strhex_to_str(char *p, size_t len, const char *strhex)
1212 size_t num_chars = 0;
1213 uint8_t lonybble, hinybble;
1214 const char *hexchars = "0123456789ABCDEF";
1215 char *p1 = NULL, *p2 = NULL;
1217 for (i = 0; i < len && strhex[i] != 0; i++) {
1218 if (strnequal(hexchars, "0x", 2)) {
1219 i++; /* skip two chars */
1223 if (!(p1 = strchr_m(hexchars, toupper(strhex[i]))))
1226 i++; /* next hex digit */
1228 if (!(p2 = strchr_m(hexchars, toupper(strhex[i]))))
1231 /* get the two nybbles */
1232 hinybble = PTR_DIFF(p1, hexchars);
1233 lonybble = PTR_DIFF(p2, hexchars);
1235 p[num_chars] = (hinybble << 4) | lonybble;
1244 DATA_BLOB strhex_to_data_blob(const char *strhex)
1246 DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1);
1248 ret_blob.length = strhex_to_str(ret_blob.data,
1256 * Routine to print a buffer as HEX digits, into an allocated string.
1259 void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
1264 *out_hex_buffer = smb_xmalloc((len*2)+1);
1265 hex_buffer = *out_hex_buffer;
1267 for (i = 0; i < len; i++)
1268 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
1273 Unescape a URL encoded string, in place.
1276 void rfc1738_unescape(char *buf)
1280 while ((p=strchr_m(p,'+')))
1285 while (p && *p && (p=strchr_m(p,'%'))) {
1289 if (c1 >= '0' && c1 <= '9')
1291 else if (c1 >= 'A' && c1 <= 'F')
1293 else if (c1 >= 'a' && c1 <= 'f')
1295 else {p++; continue;}
1297 if (c2 >= '0' && c2 <= '9')
1299 else if (c2 >= 'A' && c2 <= 'F')
1301 else if (c2 >= 'a' && c2 <= 'f')
1303 else {p++; continue;}
1307 memmove(p+1, p+3, strlen(p+3)+1);
1312 static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1315 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
1317 DATA_BLOB base64_decode_data_blob(const char *s)
1319 int bit_offset, byte_offset, idx, i, n;
1320 DATA_BLOB decoded = data_blob(s, strlen(s)+1);
1321 uint8_t *d = decoded.data;
1326 while (*s && (p=strchr_m(b64,*s))) {
1327 idx = (int)(p - b64);
1328 byte_offset = (i*6)/8;
1329 bit_offset = (i*6)%8;
1330 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
1331 if (bit_offset < 3) {
1332 d[byte_offset] |= (idx << (2-bit_offset));
1335 d[byte_offset] |= (idx >> (bit_offset-2));
1336 d[byte_offset+1] = 0;
1337 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
1349 * Decode a base64 string in-place - wrapper for the above
1351 void base64_decode_inplace(char *s)
1353 DATA_BLOB decoded = base64_decode_data_blob(s);
1354 memcpy(s, decoded.data, decoded.length);
1355 data_blob_free(&decoded);
1357 /* null terminate */
1358 s[decoded.length] = '\0';
1362 * Encode a base64 string into a malloc()ed string caller to free.
1364 *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments
1366 char * base64_encode_data_blob(DATA_BLOB data)
1371 size_t len = data.length;
1372 size_t output_len = data.length * 2;
1373 char *result = malloc(output_len); /* get us plenty of space */
1375 while (len-- && out_cnt < (data.length * 2) - 5) {
1376 int c = (uint8_t) *(data.data++);
1379 if (char_count == 3) {
1380 result[out_cnt++] = b64[bits >> 18];
1381 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
1382 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
1383 result[out_cnt++] = b64[bits & 0x3f];
1390 if (char_count != 0) {
1391 bits <<= 16 - (8 * char_count);
1392 result[out_cnt++] = b64[bits >> 18];
1393 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
1394 if (char_count == 1) {
1395 result[out_cnt++] = '=';
1396 result[out_cnt++] = '=';
1398 result[out_cnt++] = b64[(bits >> 6) & 0x3f];
1399 result[out_cnt++] = '=';
1402 result[out_cnt] = '\0'; /* terminate */
1407 size_t valgrind_strlen(const char *s)
1410 for(count = 0; *s++; count++)
1418 format a string into length-prefixed dotted domain format, as used in NBT
1419 and in some ADS structures
1421 const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
1426 return talloc_strdup(mem_ctx, "");
1428 ret = talloc(mem_ctx, strlen(s)+2);
1433 memcpy(ret+1, s, strlen(s)+1);
1436 for (i=0;ret[i];i++) {
1437 if (ret[i] == '.') {
1438 char *p = strchr(ret+i+1, '.');
1440 ret[i] = p-(ret+i+1);
1442 ret[i] = strlen(ret+i+1);
1450 BOOL add_string_to_array(TALLOC_CTX *mem_ctx,
1451 const char *str, const char ***strings, int *num)
1453 char *dup_str = talloc_strdup(mem_ctx, str);
1455 *strings = talloc_realloc(*strings,
1456 ((*num)+1) * sizeof(**strings));
1458 if ((*strings == NULL) || (dup_str == NULL))
1461 (*strings)[*num] = dup_str;
1470 varient of strcmp() that handles NULL ptrs
1472 int strcmp_safe(const char *s1, const char *s2)
1477 if (s1 == NULL || s2 == NULL) {
1480 return strcmp(s1, s2);