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 2006
9 Copyright (C) Jeremy Allison 1992-2007
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * @brief String utilities.
33 * Internal function to get the next token from a string, return false if none
34 * found. Handles double-quotes. This is the work horse function called by
35 * next_token() and next_token_no_ltrim().
37 * Based on a routine by GJC@VILLAGE.COM.
38 * Extensively modified by Andrew.Tridgell@anu.edu.au
40 static bool next_token_internal(const char **ptr
,
56 /* default to simple separators */
60 /* find the first non sep char, if left-trimming is requested */
62 while (*s
&& strchr_m(sep
,*s
))
70 /* copy over the token */
72 for (quoted
= false; len
< bufsize
&& *s
&&
73 (quoted
|| !strchr_m(sep
,*s
)); s
++) {
82 *ptr
= (*s
) ? s
+1 : s
;
88 static bool next_token_internal_talloc(TALLOC_CTX
*ctx
,
107 /* default to simple separators */
112 /* find the first non sep char, if left-trimming is requested */
114 while (*s
&& strchr_m(sep
,*s
))
123 /* When restarting we need to go from here. */
126 /* Work out the length needed. */
127 for (quoted
= false; *s
&&
128 (quoted
|| !strchr_m(sep
,*s
)); s
++) {
136 /* We started with len = 1 so we have space for the nul. */
137 *pp_buff
= TALLOC_ARRAY(ctx
, char, len
);
142 /* copy over the token */
145 for (quoted
= false; *s
&&
146 (quoted
|| !strchr_m(sep
,*s
)); s
++) {
154 *ptr
= (*s
) ? s
+1 : s
;
161 * Get the next token from a string, return false if none found. Handles
162 * double-quotes. This version trims leading separator characters before
163 * looking for a token.
165 bool next_token(const char **ptr
, char *buff
, const char *sep
, size_t bufsize
)
167 return next_token_internal(ptr
, buff
, sep
, bufsize
, true);
170 bool next_token_talloc(TALLOC_CTX
*ctx
,
175 return next_token_internal_talloc(ctx
, ptr
, pp_buff
, sep
, true);
179 * Get the next token from a string, return false if none found. Handles
180 * double-quotes. This version does not trim leading separator characters
181 * before looking for a token.
183 bool next_token_no_ltrim(const char **ptr
,
188 return next_token_internal(ptr
, buff
, sep
, bufsize
, false);
191 bool next_token_no_ltrim_talloc(TALLOC_CTX
*ctx
,
196 return next_token_internal_talloc(ctx
, ptr
, pp_buff
, sep
, false);
200 This is like next_token but is not re-entrant and "remembers" the first
201 parameter so you can pass NULL. This is useful for user interface code
202 but beware the fact that it is not re-entrant!
205 static const char *last_ptr
=NULL
;
207 bool next_token_nr(const char **ptr
,char *buff
, const char *sep
, size_t bufsize
)
214 ret
= next_token(ptr
, buff
, sep
, bufsize
);
219 bool next_token_nr_talloc(TALLOC_CTX
*ctx
,
229 ret
= next_token_talloc(ctx
, ptr
, pp_buff
, sep
);
234 void set_first_token(char *ptr
)
240 Convert list of tokens to array; dependent on above routine.
241 Uses last_ptr from above - bit of a hack.
244 char **toktocliplist(int *ctok
, const char *sep
)
246 char *s
=(char *)last_ptr
;
253 while(*s
&& strchr_m(sep
,*s
))
262 while(*s
&& (!strchr_m(sep
,*s
)))
264 while(*s
&& strchr_m(sep
,*s
))
271 if (!(ret
=iret
=SMB_MALLOC_ARRAY(char *,ictok
+1)))
289 * Case insensitive string compararison.
291 * iconv does not directly give us a way to compare strings in
292 * arbitrary unix character sets -- all we can is convert and then
293 * compare. This is expensive.
295 * As an optimization, we do a first pass that considers only the
296 * prefix of the strings that is entirely 7-bit. Within this, we
297 * check whether they have the same value.
299 * Hopefully this will often give the answer without needing to copy.
300 * In particular it should speed comparisons to literal ascii strings
301 * or comparisons of strings that are "obviously" different.
303 * If we find a non-ascii character we fall back to converting via
306 * This should never be slower than convering the whole thing, and
309 * A different optimization would be to compare for bitwise equality
310 * in the binary encoding. (It would be possible thought hairy to do
311 * both simultaneously.) But in that case if they turn out to be
312 * different, we'd need to restart the whole thing.
314 * Even better is to implement strcasecmp for each encoding and use a
317 int StrCaseCmp(const char *s
, const char *t
)
322 smb_ucs2_t
*buffer_s
, *buffer_t
;
325 for (ps
= s
, pt
= t
; ; ps
++, pt
++) {
329 return 0; /* both ended */
331 return -1; /* s is a prefix */
333 return +1; /* t is a prefix */
334 else if ((*ps
& 0x80) || (*pt
& 0x80))
335 /* not ascii anymore, do it the hard way
339 us
= toupper_ascii(*ps
);
340 ut
= toupper_ascii(*pt
);
349 size
= push_ucs2_allocate(&buffer_s
, ps
);
350 if (size
== (size_t)-1) {
351 return strcmp(ps
, pt
);
352 /* Not quite the right answer, but finding the right one
353 under this failure case is expensive, and it's pretty
357 size
= push_ucs2_allocate(&buffer_t
, pt
);
358 if (size
== (size_t)-1) {
360 return strcmp(ps
, pt
);
361 /* Not quite the right answer, but finding the right one
362 under this failure case is expensive, and it's pretty
366 ret
= strcasecmp_w(buffer_s
, buffer_t
);
374 Case insensitive string compararison, length limited.
376 int StrnCaseCmp(const char *s
, const char *t
, size_t len
)
381 smb_ucs2_t
*buffer_s
, *buffer_t
;
384 for (ps
= s
, pt
= t
; n
< len
; ps
++, pt
++, n
++) {
388 return 0; /* both ended */
390 return -1; /* s is a prefix */
392 return +1; /* t is a prefix */
393 else if ((*ps
& 0x80) || (*pt
& 0x80))
394 /* not ascii anymore, do it the
395 * hard way from here on in */
398 us
= toupper_ascii(*ps
);
399 ut
= toupper_ascii(*pt
);
412 size
= push_ucs2_allocate(&buffer_s
, ps
);
413 if (size
== (size_t)-1) {
414 return strncmp(ps
, pt
, len
-n
);
415 /* Not quite the right answer, but finding the right one
416 under this failure case is expensive,
417 and it's pretty close */
420 size
= push_ucs2_allocate(&buffer_t
, pt
);
421 if (size
== (size_t)-1) {
423 return strncmp(ps
, pt
, len
-n
);
424 /* Not quite the right answer, but finding the right one
425 under this failure case is expensive,
426 and it's pretty close */
429 ret
= strncasecmp_w(buffer_s
, buffer_t
, len
-n
);
438 * @note The comparison is case-insensitive.
440 bool strequal(const char *s1
, const char *s2
)
447 return(StrCaseCmp(s1
,s2
)==0);
451 * Compare 2 strings up to and including the nth char.
453 * @note The comparison is case-insensitive.
455 bool strnequal(const char *s1
,const char *s2
,size_t n
)
459 if (!s1
|| !s2
|| !n
)
462 return(StrnCaseCmp(s1
,s2
,n
)==0);
466 Compare 2 strings (case sensitive).
469 bool strcsequal(const char *s1
,const char *s2
)
476 return(strcmp(s1
,s2
)==0);
480 Do a case-insensitive, whitespace-ignoring string compare.
483 int strwicmp(const char *psz1
, const char *psz2
)
485 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
486 /* appropriate value. */
489 else if (psz1
== NULL
)
491 else if (psz2
== NULL
)
494 /* sync the strings on first non-whitespace */
496 while (isspace((int)*psz1
))
498 while (isspace((int)*psz2
))
500 if (toupper_ascii(*psz1
) != toupper_ascii(*psz2
) ||
501 *psz1
== '\0' || *psz2
== '\0')
506 return (*psz1
- *psz2
);
510 Convert a string to "normal" form.
513 void strnorm(char *s
, int case_default
)
515 if (case_default
== CASE_UPPER
)
522 Check if a string is in "normal" case.
525 bool strisnormal(const char *s
, int case_default
)
527 if (case_default
== CASE_UPPER
)
528 return(!strhaslower(s
));
530 return(!strhasupper(s
));
536 NOTE: oldc and newc must be 7 bit characters
538 void string_replace( char *s
, char oldc
, char newc
)
542 /* this is quite a common operation, so we want it to be
543 fast. We optimise for the ascii case, knowing that all our
544 supported multi-byte character sets are ascii-compatible
545 (ie. they match for the first 128 chars) */
547 for (p
= s
; *p
; p
++) {
548 if (*p
& 0x80) /* mb string - slow path. */
558 /* Slow (mb) path. */
559 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
560 /* With compose characters we must restart from the beginning. JRA. */
566 next_codepoint(p
, &c_size
);
578 * Skip past some strings in a buffer - old version - no checks.
581 char *push_skip_string(char *buf
)
583 buf
+= strlen(buf
) + 1;
588 Skip past a string in a buffer. Buffer may not be
589 null terminated. end_ptr points to the first byte after
590 then end of the buffer.
593 char *skip_string(const char *base
, size_t len
, char *buf
)
595 const char *end_ptr
= base
+ len
;
597 if (end_ptr
< base
|| !base
|| !buf
|| buf
>= end_ptr
) {
601 /* Skip the string */
604 if (buf
>= end_ptr
) {
614 Count the number of characters in a string. Normally this will
615 be the same as the number of bytes in a string for single byte strings,
616 but will be different for multibyte.
619 size_t str_charnum(const char *s
)
622 smb_ucs2_t
*tmpbuf2
= NULL
;
623 if (push_ucs2_allocate(&tmpbuf2
, s
) == (size_t)-1) {
626 ret
= strlen_w(tmpbuf2
);
632 Count the number of characters in a string. Normally this will
633 be the same as the number of bytes in a string for single byte strings,
634 but will be different for multibyte.
637 size_t str_ascii_charnum(const char *s
)
640 char *tmpbuf2
= NULL
;
641 if (push_ascii_allocate(&tmpbuf2
, s
) == (size_t)-1) {
644 ret
= strlen(tmpbuf2
);
649 bool trim_char(char *s
,char cfront
,char cback
)
655 /* Ignore null or empty strings. */
656 if (!s
|| (s
[0] == '\0'))
660 while (*fp
&& *fp
== cfront
)
663 /* We ate the string. */
671 ep
= fp
+ strlen(fp
) - 1;
673 /* Attempt ascii only. Bail for mb strings. */
674 while ((ep
>= fp
) && (*ep
== cback
)) {
676 if ((ep
> fp
) && (((unsigned char)ep
[-1]) & 0x80)) {
677 /* Could be mb... bail back to tim_string. */
685 return trim_string(s
, cfront
? fs
: NULL
, bs
);
691 /* We ate the string. */
698 memmove(s
, fp
, ep
-fp
+2);
703 Trim the specified elements off the front and back of a string.
706 bool trim_string(char *s
,const char *front
,const char *back
)
713 /* Ignore null or empty strings. */
714 if (!s
|| (s
[0] == '\0'))
717 front_len
= front
? strlen(front
) : 0;
718 back_len
= back
? strlen(back
) : 0;
723 while (len
&& strncmp(s
, front
, front_len
)==0) {
724 /* Must use memmove here as src & dest can
725 * easily overlap. Found by valgrind. JRA. */
726 memmove(s
, s
+front_len
, (len
-front_len
)+1);
733 while ((len
>= back_len
) &&
734 strncmp(s
+len
-back_len
,back
,back_len
)==0) {
735 s
[len
-back_len
]='\0';
744 Does a string have any uppercase chars in it?
747 bool strhasupper(const char *s
)
752 if (push_ucs2_allocate(&tmp
, s
) == -1) {
756 for(p
= tmp
; *p
!= 0; p
++) {
768 Does a string have any lowercase chars in it?
771 bool strhaslower(const char *s
)
776 if (push_ucs2_allocate(&tmp
, s
) == -1) {
780 for(p
= tmp
; *p
!= 0; p
++) {
792 Find the number of 'c' chars in a string
795 size_t count_chars(const char *s
,char c
)
799 smb_ucs2_t
*alloc_tmpbuf
= NULL
;
801 if (push_ucs2_allocate(&alloc_tmpbuf
, s
) == (size_t)-1) {
805 for(count
=0,ptr
=alloc_tmpbuf
;*ptr
;ptr
++)
806 if(*ptr
==UCS2_CHAR(c
))
809 SAFE_FREE(alloc_tmpbuf
);
814 Safe string copy into a known length string. maxlength does not
815 include the terminating zero.
818 char *safe_strcpy_fn(const char *fn
,
827 DEBUG(0,("ERROR: NULL dest in safe_strcpy, "
828 "called from [%s][%d]\n", fn
, line
));
833 clobber_region(fn
,line
,dest
, maxlength
+1);
841 len
= strnlen(src
, maxlength
+1);
843 if (len
> maxlength
) {
844 DEBUG(0,("ERROR: string overflow by "
845 "%lu (%lu - %lu) in safe_strcpy [%.50s]\n",
846 (unsigned long)(len
-maxlength
), (unsigned long)len
,
847 (unsigned long)maxlength
, src
));
851 memmove(dest
, src
, len
);
857 Safe string cat into a string. maxlength does not
858 include the terminating zero.
860 char *safe_strcat_fn(const char *fn
,
866 size_t src_len
, dest_len
;
869 DEBUG(0,("ERROR: NULL dest in safe_strcat, "
870 "called from [%s][%d]\n", fn
, line
));
877 src_len
= strnlen(src
, maxlength
+ 1);
878 dest_len
= strnlen(dest
, maxlength
+ 1);
881 clobber_region(fn
, line
, dest
+ dest_len
, maxlength
+ 1 - dest_len
);
884 if (src_len
+ dest_len
> maxlength
) {
885 DEBUG(0,("ERROR: string overflow by %d "
886 "in safe_strcat [%.50s]\n",
887 (int)(src_len
+ dest_len
- maxlength
), src
));
888 if (maxlength
> dest_len
) {
889 memcpy(&dest
[dest_len
], src
, maxlength
- dest_len
);
895 memcpy(&dest
[dest_len
], src
, src_len
);
896 dest
[dest_len
+ src_len
] = 0;
901 Paranoid strcpy into a buffer of given length (includes terminating
902 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
903 and replaces with '_'. Deliberately does *NOT* check for multibyte
904 characters. Don't change it !
907 char *alpha_strcpy_fn(const char *fn
,
911 const char *other_safe_chars
,
917 clobber_region(fn
, line
, dest
, maxlength
);
921 DEBUG(0,("ERROR: NULL dest in alpha_strcpy, "
922 "called from [%s][%d]\n", fn
, line
));
932 if (len
>= maxlength
)
935 if (!other_safe_chars
)
936 other_safe_chars
= "";
938 for(i
= 0; i
< len
; i
++) {
939 int val
= (src
[i
] & 0xff);
940 if (isupper_ascii(val
) || islower_ascii(val
) ||
941 isdigit(val
) || strchr_m(other_safe_chars
, val
))
953 Like strncpy but always null terminates. Make sure there is room!
954 The variable n should always be one less than the available size.
956 char *StrnCpy_fn(const char *fn
, int line
,char *dest
,const char *src
,size_t n
)
961 clobber_region(fn
, line
, dest
, n
+1);
965 DEBUG(0,("ERROR: NULL dest in StrnCpy, "
966 "called from [%s][%d]\n", fn
, line
));
975 while (n
-- && (*d
= *src
)) {
986 Like strncpy but copies up to the character marker. always null terminates.
987 returns a pointer to the character marker in the source string (src).
990 static char *strncpyn(char *dest
, const char *src
, size_t n
, char c
)
996 clobber_region(dest
, n
+1);
998 p
= strchr_m(src
, c
);
1000 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c
));
1004 str_len
= PTR_DIFF(p
, src
);
1005 strncpy(dest
, src
, MIN(n
, str_len
));
1006 dest
[str_len
] = '\0';
1013 Routine to get hex characters and turn them into a 16 byte array.
1014 the array can be variable length, and any non-hex-numeric
1015 characters are skipped. "0xnn" or "0Xnn" is specially catered
1018 valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1022 size_t strhex_to_str(char *buf
, size_t buf_len
, const char *strhex
, size_t strhex_len
)
1025 size_t num_chars
= 0;
1026 unsigned char lonybble
, hinybble
;
1027 const char *hexchars
= "0123456789ABCDEF";
1028 char *p1
= NULL
, *p2
= NULL
;
1030 for (i
= 0; i
< strhex_len
&& strhex
[i
] != 0; i
++) {
1031 if (strnequal(hexchars
, "0x", 2)) {
1032 i
++; /* skip two chars */
1036 if (!(p1
= strchr_m(hexchars
, toupper_ascii(strhex
[i
]))))
1039 i
++; /* next hex digit */
1041 if (!(p2
= strchr_m(hexchars
, toupper_ascii(strhex
[i
]))))
1044 /* get the two nybbles */
1045 hinybble
= PTR_DIFF(p1
, hexchars
);
1046 lonybble
= PTR_DIFF(p2
, hexchars
);
1048 if (num_chars
>= buf_len
) {
1051 buf
[num_chars
] = (hinybble
<< 4) | lonybble
;
1060 DATA_BLOB
strhex_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *strhex
)
1064 if (mem_ctx
!= NULL
)
1065 ret_blob
= data_blob_talloc(mem_ctx
, NULL
, strlen(strhex
)/2+1);
1067 ret_blob
= data_blob(NULL
, strlen(strhex
)/2+1);
1069 ret_blob
.length
= strhex_to_str((char*)ret_blob
.data
,
1078 * Routine to print a buffer as HEX digits, into an allocated string.
1081 char *hex_encode(TALLOC_CTX
*mem_ctx
, const unsigned char *buff_in
, size_t len
)
1086 hex_buffer
= TALLOC_ARRAY(mem_ctx
, char, (len
*2)+1);
1088 for (i
= 0; i
< len
; i
++)
1089 slprintf(&hex_buffer
[i
*2], 3, "%02X", buff_in
[i
]);
1095 Check if a string is part of a list.
1098 bool in_list(const char *s
, const char *list
, bool casesensitive
)
1102 size_t bufsize
= strlen(list
);
1108 /* We know a token can't be larger
1109 * than the entire list. */
1111 tok
= SMB_MALLOC_ARRAY(char, bufsize
+1);
1116 while (next_token(&p
,tok
,LIST_SEP
,bufsize
+1)) {
1117 if (casesensitive
) {
1118 if (strcmp(tok
,s
) == 0) {
1123 if (StrCaseCmp(tok
,s
) == 0) {
1134 /* this is used to prevent lots of mallocs of size 1 */
1135 static const char null_string
[] = "";
1138 Set a string value, allocing the space for the string
1141 static bool string_init(char **dest
,const char *src
)
1151 *dest
= CONST_DISCARD(char*, null_string
);
1153 (*dest
) = SMB_STRDUP(src
);
1154 if ((*dest
) == NULL
) {
1155 DEBUG(0,("Out of memory in string_init\n"));
1163 Free a string value.
1166 void string_free(char **s
)
1170 if (*s
== null_string
)
1176 Set a string value, deallocating any existing space, and allocing the space
1180 bool string_set(char **dest
,const char *src
)
1183 return(string_init(dest
,src
));
1187 Substitute a string for a pattern in another string. Make sure there is
1190 This routine looks for pattern in s and replaces it with
1191 insert. It may do multiple replacements or just one.
1193 Any of " ; ' $ or ` in the insert string are replaced with _
1194 if len==0 then the string cannot be extended. This is different from the old
1195 use of len==0 which was for no length checks to be done.
1198 void string_sub2(char *s
,const char *pattern
, const char *insert
, size_t len
,
1199 bool remove_unsafe_characters
, bool replace_once
,
1200 bool allow_trailing_dollar
)
1203 ssize_t ls
,lp
,li
, i
;
1205 if (!insert
|| !pattern
|| !*pattern
|| !s
)
1208 ls
= (ssize_t
)strlen(s
);
1209 lp
= (ssize_t
)strlen(pattern
);
1210 li
= (ssize_t
)strlen(insert
);
1213 len
= ls
+ 1; /* len is number of *bytes* */
1215 while (lp
<= ls
&& (p
= strstr_m(s
,pattern
))) {
1216 if (ls
+ (li
-lp
) >= len
) {
1217 DEBUG(0,("ERROR: string overflow by "
1218 "%d in string_sub(%.50s, %d)\n",
1219 (int)(ls
+ (li
-lp
) - len
),
1220 pattern
, (int)len
));
1224 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
1226 for (i
=0;i
<li
;i
++) {
1227 switch (insert
[i
]) {
1233 /* allow a trailing $
1234 * (as in machine accounts) */
1235 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
1242 if ( remove_unsafe_characters
) {
1244 /* yes this break should be here
1245 * since we want to fall throw if
1246 * not replacing unsafe chars */
1261 void string_sub_once(char *s
, const char *pattern
,
1262 const char *insert
, size_t len
)
1264 string_sub2( s
, pattern
, insert
, len
, true, true, false );
1267 void string_sub(char *s
,const char *pattern
, const char *insert
, size_t len
)
1269 string_sub2( s
, pattern
, insert
, len
, true, false, false );
1272 void fstring_sub(char *s
,const char *pattern
,const char *insert
)
1274 string_sub(s
, pattern
, insert
, sizeof(fstring
));
1278 Similar to string_sub2, but it will accept only allocated strings
1279 and may realloc them so pay attention at what you pass on no
1280 pointers inside strings, no pstrings or const may be passed
1284 char *realloc_string_sub2(char *string
,
1285 const char *pattern
,
1287 bool remove_unsafe_characters
,
1288 bool allow_trailing_dollar
)
1292 ssize_t ls
,lp
,li
,ld
, i
;
1294 if (!insert
|| !pattern
|| !*pattern
|| !string
|| !*string
)
1299 in
= SMB_STRDUP(insert
);
1301 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
1304 ls
= (ssize_t
)strlen(s
);
1305 lp
= (ssize_t
)strlen(pattern
);
1306 li
= (ssize_t
)strlen(insert
);
1308 for (i
=0;i
<li
;i
++) {
1315 /* allow a trailing $
1316 * (as in machine accounts) */
1317 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
1323 if ( remove_unsafe_characters
) {
1333 while ((p
= strstr_m(s
,pattern
))) {
1335 int offset
= PTR_DIFF(s
,string
);
1336 string
= (char *)SMB_REALLOC(string
, ls
+ ld
+ 1);
1338 DEBUG(0, ("realloc_string_sub: "
1339 "out of memory!\n"));
1343 p
= string
+ offset
+ (p
- s
);
1346 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
1356 char *realloc_string_sub(char *string
,
1357 const char *pattern
,
1360 return realloc_string_sub2(string
, pattern
, insert
, true, false);
1364 * Internal guts of talloc_string_sub and talloc_all_string_sub.
1365 * talloc version of string_sub2.
1368 char *talloc_string_sub2(TALLOC_CTX
*mem_ctx
, const char *src
,
1369 const char *pattern
,
1371 bool remove_unsafe_characters
,
1373 bool allow_trailing_dollar
)
1378 ssize_t ls
,lp
,li
,ld
, i
;
1380 if (!insert
|| !pattern
|| !*pattern
|| !src
|| !*src
) {
1384 string
= talloc_strdup(mem_ctx
, src
);
1385 if (string
== NULL
) {
1386 DEBUG(0, ("talloc_string_sub2: "
1387 "talloc_strdup failed\n"));
1393 in
= SMB_STRDUP(insert
);
1395 DEBUG(0, ("talloc_string_sub2: ENOMEM\n"));
1398 ls
= (ssize_t
)strlen(s
);
1399 lp
= (ssize_t
)strlen(pattern
);
1400 li
= (ssize_t
)strlen(insert
);
1403 for (i
=0;i
<li
;i
++) {
1410 /* allow a trailing $
1411 * (as in machine accounts) */
1412 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
1418 if (remove_unsafe_characters
) {
1428 while ((p
= strstr_m(s
,pattern
))) {
1430 int offset
= PTR_DIFF(s
,string
);
1431 string
= (char *)TALLOC_REALLOC(mem_ctx
, string
,
1434 DEBUG(0, ("talloc_string_sub: out of "
1439 p
= string
+ offset
+ (p
- s
);
1442 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
1456 /* Same as string_sub, but returns a talloc'ed string */
1458 char *talloc_string_sub(TALLOC_CTX
*mem_ctx
,
1460 const char *pattern
,
1463 return talloc_string_sub2(mem_ctx
, src
, pattern
, insert
,
1464 true, false, false);
1468 Similar to string_sub() but allows for any character to be substituted.
1470 if len==0 then the string cannot be extended. This is different from the old
1471 use of len==0 which was for no length checks to be done.
1474 void all_string_sub(char *s
,const char *pattern
,const char *insert
, size_t len
)
1479 if (!insert
|| !pattern
|| !s
)
1482 ls
= (ssize_t
)strlen(s
);
1483 lp
= (ssize_t
)strlen(pattern
);
1484 li
= (ssize_t
)strlen(insert
);
1490 len
= ls
+ 1; /* len is number of *bytes* */
1492 while (lp
<= ls
&& (p
= strstr_m(s
,pattern
))) {
1493 if (ls
+ (li
-lp
) >= len
) {
1494 DEBUG(0,("ERROR: string overflow by "
1495 "%d in all_string_sub(%.50s, %d)\n",
1496 (int)(ls
+ (li
-lp
) - len
),
1497 pattern
, (int)len
));
1501 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
1503 memcpy(p
, insert
, li
);
1509 char *talloc_all_string_sub(TALLOC_CTX
*ctx
,
1511 const char *pattern
,
1514 return talloc_string_sub2(ctx
, src
, pattern
, insert
,
1515 false, false, false);
1520 Splits out the front and back at a separator.
1523 static void split_at_last_component(char *path
, char *front
, char sep
,
1526 char *p
= strrchr_m(path
, sep
);
1532 pstrcpy(front
, path
);
1546 Write an octal as a string.
1549 char *octal_string(int i
)
1553 result
= talloc_strdup(talloc_tos(), "-1");
1556 result
= talloc_asprintf(talloc_tos(), "0%o", i
);
1558 SMB_ASSERT(result
!= NULL
);
1564 Truncate a string at a specified length.
1567 char *string_truncate(char *s
, unsigned int length
)
1569 if (s
&& strlen(s
) > length
)
1575 Strchr and strrchr_m are very hard to do on general multi-byte strings.
1576 We convert via ucs2 for now.
1579 char *strchr_m(const char *src
, char c
)
1581 smb_ucs2_t
*ws
= NULL
;
1587 /* characters below 0x3F are guaranteed to not appear in
1588 non-initial position in multi-byte charsets */
1589 if ((c
& 0xC0) == 0) {
1590 return strchr(src
, c
);
1593 /* this is quite a common operation, so we want it to be
1594 fast. We optimise for the ascii case, knowing that all our
1595 supported multi-byte character sets are ascii-compatible
1596 (ie. they match for the first 128 chars) */
1598 for (s
= src
; *s
&& !(((unsigned char)s
[0]) & 0x80); s
++) {
1606 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
1607 /* With compose characters we must restart from the beginning. JRA. */
1611 if (push_ucs2_allocate(&ws
, s
)==(size_t)-1) {
1612 /* Wrong answer, but what can we do... */
1613 return strchr(src
, c
);
1615 p
= strchr_w(ws
, UCS2_CHAR(c
));
1621 if (pull_ucs2_allocate(&s2
, ws
)==(size_t)-1) {
1623 /* Wrong answer, but what can we do... */
1624 return strchr(src
, c
);
1626 ret
= (char *)(s
+strlen(s2
));
1632 char *strrchr_m(const char *s
, char c
)
1634 /* characters below 0x3F are guaranteed to not appear in
1635 non-initial position in multi-byte charsets */
1636 if ((c
& 0xC0) == 0) {
1637 return strrchr(s
, c
);
1640 /* this is quite a common operation, so we want it to be
1641 fast. We optimise for the ascii case, knowing that all our
1642 supported multi-byte character sets are ascii-compatible
1643 (ie. they match for the first 128 chars). Also, in Samba
1644 we only search for ascii characters in 'c' and that
1645 in all mb character sets with a compound character
1646 containing c, if 'c' is not a match at position
1647 p, then p[-1] > 0x7f. JRA. */
1650 size_t len
= strlen(s
);
1652 bool got_mb
= false;
1659 /* Could be a match. Part of a multibyte ? */
1661 (((unsigned char)cp
[-1]) & 0x80)) {
1662 /* Yep - go slow :-( */
1666 /* No - we have a match ! */
1669 } while (cp
-- != s
);
1674 /* String contained a non-ascii char. Slow path. */
1676 smb_ucs2_t
*ws
= NULL
;
1681 if (push_ucs2_allocate(&ws
,s
)==(size_t)-1) {
1682 /* Wrong answer, but what can we do. */
1683 return strrchr(s
, c
);
1685 p
= strrchr_w(ws
, UCS2_CHAR(c
));
1691 if (pull_ucs2_allocate(&s2
,ws
)==(size_t)-1) {
1693 /* Wrong answer, but what can we do. */
1694 return strrchr(s
, c
);
1696 ret
= (char *)(s
+strlen(s2
));
1703 /***********************************************************************
1704 Return the equivalent of doing strrchr 'n' times - always going
1706 ***********************************************************************/
1708 char *strnrchr_m(const char *s
, char c
, unsigned int n
)
1710 smb_ucs2_t
*ws
= NULL
;
1715 if (push_ucs2_allocate(&ws
,s
)==(size_t)-1) {
1716 /* Too hard to try and get right. */
1719 p
= strnrchr_w(ws
, UCS2_CHAR(c
), n
);
1725 if (pull_ucs2_allocate(&s2
,ws
)==(size_t)-1) {
1727 /* Too hard to try and get right. */
1730 ret
= (char *)(s
+strlen(s2
));
1736 /***********************************************************************
1737 strstr_m - We convert via ucs2 for now.
1738 ***********************************************************************/
1740 char *strstr_m(const char *src
, const char *findstr
)
1743 smb_ucs2_t
*src_w
, *find_w
;
1748 size_t findstr_len
= 0;
1750 /* for correctness */
1755 /* Samba does single character findstr calls a *lot*. */
1756 if (findstr
[1] == '\0')
1757 return strchr_m(src
, *findstr
);
1759 /* We optimise for the ascii case, knowing that all our
1760 supported multi-byte character sets are ascii-compatible
1761 (ie. they match for the first 128 chars) */
1763 for (s
= src
; *s
&& !(((unsigned char)s
[0]) & 0x80); s
++) {
1764 if (*s
== *findstr
) {
1766 findstr_len
= strlen(findstr
);
1768 if (strncmp(s
, findstr
, findstr_len
) == 0) {
1777 #if 1 /* def BROKEN_UNICODE_COMPOSE_CHARACTERS */
1778 /* 'make check' fails unless we do this */
1780 /* With compose characters we must restart from the beginning. JRA. */
1784 if (push_ucs2_allocate(&src_w
, src
) == (size_t)-1) {
1785 DEBUG(0,("strstr_m: src malloc fail\n"));
1789 if (push_ucs2_allocate(&find_w
, findstr
) == (size_t)-1) {
1791 DEBUG(0,("strstr_m: find malloc fail\n"));
1795 p
= strstr_w(src_w
, find_w
);
1804 if (pull_ucs2_allocate(&s2
, src_w
) == (size_t)-1) {
1807 DEBUG(0,("strstr_m: dest malloc fail\n"));
1810 retp
= (char *)(s
+strlen(s2
));
1818 Convert a string to lower case.
1821 void strlower_m(char *s
)
1826 /* this is quite a common operation, so we want it to be
1827 fast. We optimise for the ascii case, knowing that all our
1828 supported multi-byte character sets are ascii-compatible
1829 (ie. they match for the first 128 chars) */
1831 while (*s
&& !(((unsigned char)s
[0]) & 0x80)) {
1832 *s
= tolower_ascii((unsigned char)*s
);
1839 /* I assume that lowercased string takes the same number of bytes
1840 * as source string even in UTF-8 encoding. (VIV) */
1841 len
= strlen(s
) + 1;
1844 unix_strlower(s
,len
,s
,len
);
1845 /* Catch mb conversion errors that may not terminate. */
1852 Convert a string to upper case.
1855 void strupper_m(char *s
)
1860 /* this is quite a common operation, so we want it to be
1861 fast. We optimise for the ascii case, knowing that all our
1862 supported multi-byte character sets are ascii-compatible
1863 (ie. they match for the first 128 chars) */
1865 while (*s
&& !(((unsigned char)s
[0]) & 0x80)) {
1866 *s
= toupper_ascii((unsigned char)*s
);
1873 /* I assume that lowercased string takes the same number of bytes
1874 * as source string even in multibyte encoding. (VIV) */
1875 len
= strlen(s
) + 1;
1878 unix_strupper(s
,len
,s
,len
);
1879 /* Catch mb conversion errors that may not terminate. */
1886 Count the number of UCS2 characters in a string. Normally this will
1887 be the same as the number of bytes in a string for single byte strings,
1888 but will be different for multibyte.
1891 size_t strlen_m(const char *s
)
1899 while (*s
&& !(((uint8_t)*s
) & 0x80)) {
1910 codepoint_t c
= next_codepoint(s
, &c_size
);
1912 /* Unicode char fits into 16 bits. */
1915 /* Double-width unicode char - 32 bits. */
1925 Count the number of UCS2 characters in a string including the null
1929 size_t strlen_m_term(const char *s
)
1934 return strlen_m(s
) + 1;
1938 * Weird helper routine for the winreg pipe: If nothing is around, return 0,
1939 * if a string is there, include the terminator.
1942 size_t strlen_m_term_null(const char *s
)
1956 Return a RFC2254 binary string representation of a buffer.
1957 Used in LDAP filters.
1961 char *binary_string_rfc2254(char *buf
, int len
)
1965 const char *hex
= "0123456789ABCDEF";
1966 s
= (char *)SMB_MALLOC(len
* 3 + 1);
1969 for (j
=i
=0;i
<len
;i
++) {
1971 s
[j
+1] = hex
[((unsigned char)buf
[i
]) >> 4];
1972 s
[j
+2] = hex
[((unsigned char)buf
[i
]) & 0xF];
1979 char *binary_string(char *buf
, int len
)
1983 const char *hex
= "0123456789ABCDEF";
1984 s
= (char *)SMB_MALLOC(len
* 2 + 1);
1987 for (j
=i
=0;i
<len
;i
++) {
1988 s
[j
] = hex
[((unsigned char)buf
[i
]) >> 4];
1989 s
[j
+1] = hex
[((unsigned char)buf
[i
]) & 0xF];
1996 Just a typesafety wrapper for snprintf into a pstring.
1999 int pstr_sprintf(pstring s
, const char *fmt
, ...)
2005 ret
= vsnprintf(s
, PSTRING_LEN
, fmt
, ap
);
2012 Just a typesafety wrapper for snprintf into a fstring.
2015 int fstr_sprintf(fstring s
, const char *fmt
, ...)
2021 ret
= vsnprintf(s
, FSTRING_LEN
, fmt
, ap
);
2027 List of Strings manipulation functions
2030 #define S_LIST_ABS 16 /* List Allocation Block Size */
2032 static char **str_list_make_internal(TALLOC_CTX
*mem_ctx
,
2036 char **list
, **rlist
;
2041 TALLOC_CTX
*frame
= NULL
;
2043 if (!string
|| !*string
)
2046 s
= talloc_strdup(mem_ctx
, string
);
2048 s
= SMB_STRDUP(string
);
2051 DEBUG(0,("str_list_make: Unable to allocate memory"));
2054 if (!sep
) sep
= LIST_SEP
;
2060 frame
= talloc_stackframe();
2061 while (next_token_talloc(frame
, &str
, &tok
, sep
)) {
2063 lsize
+= S_LIST_ABS
;
2065 rlist
= TALLOC_REALLOC_ARRAY(mem_ctx
, list
,
2068 /* We need to keep the old list on
2069 * error so we can free the elements
2070 if the realloc fails. */
2071 rlist
=SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list
,
2075 DEBUG(0,("str_list_make: "
2076 "Unable to allocate memory"));
2077 str_list_free(&list
);
2088 memset (&list
[num
], 0,
2089 ((sizeof(char**)) * (S_LIST_ABS
+1)));
2093 list
[num
] = talloc_strdup(mem_ctx
, tok
);
2095 list
[num
] = SMB_STRDUP(tok
);
2099 DEBUG(0,("str_list_make: Unable to allocate memory"));
2100 str_list_free(&list
);
2124 char **str_list_make_talloc(TALLOC_CTX
*mem_ctx
,
2128 return str_list_make_internal(mem_ctx
, string
, sep
);
2131 char **str_list_make(const char *string
, const char *sep
)
2133 return str_list_make_internal(NULL
, string
, sep
);
2136 bool str_list_copy(char ***dest
, const char **src
)
2138 char **list
, **rlist
;
2150 lsize
+= S_LIST_ABS
;
2151 rlist
= SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(list
,
2154 DEBUG(0,("str_list_copy: "
2155 "Unable to re-allocate memory"));
2156 str_list_free(&list
);
2161 memset (&list
[num
], 0,
2162 ((sizeof(char **)) * (S_LIST_ABS
+1)));
2165 list
[num
] = SMB_STRDUP(src
[num
]);
2167 DEBUG(0,("str_list_copy: Unable to allocate memory"));
2168 str_list_free(&list
);
2180 * Return true if all the elements of the list match exactly.
2182 bool str_list_compare(char **list1
, char **list2
)
2186 if (!list1
|| !list2
)
2187 return (list1
== list2
);
2189 for (num
= 0; list1
[num
]; num
++) {
2192 if (!strcsequal(list1
[num
], list2
[num
]))
2196 return false; /* if list2 has more elements than list1 fail */
2201 static void str_list_free_internal(TALLOC_CTX
*mem_ctx
, char ***list
)
2205 if (!list
|| !*list
)
2208 for(; *tlist
; tlist
++) {
2210 TALLOC_FREE(*tlist
);
2216 TALLOC_FREE(*tlist
);
2222 void str_list_free_talloc(TALLOC_CTX
*mem_ctx
, char ***list
)
2224 str_list_free_internal(mem_ctx
, list
);
2227 void str_list_free(char ***list
)
2229 str_list_free_internal(NULL
, list
);
2232 /******************************************************************************
2233 *****************************************************************************/
2235 int str_list_count( const char **list
)
2242 /* count the number of list members */
2244 for ( i
=0; *list
; i
++, list
++ );
2249 /******************************************************************************
2250 version of standard_sub_basic() for string lists; uses alloc_sub_basic()
2252 *****************************************************************************/
2254 bool str_list_sub_basic( char **list
, const char *smb_name
,
2255 const char *domain_name
)
2261 tmpstr
= alloc_sub_basic(smb_name
, domain_name
, s
);
2263 DEBUG(0,("str_list_sub_basic: "
2264 "alloc_sub_basic() return NULL!\n"));
2277 /******************************************************************************
2278 substritute a specific pattern in a string list
2279 *****************************************************************************/
2281 bool str_list_substitute(char **list
, const char *pattern
, const char *insert
)
2284 ssize_t ls
, lp
, li
, ld
, i
, d
;
2293 lp
= (ssize_t
)strlen(pattern
);
2294 li
= (ssize_t
)strlen(insert
);
2299 ls
= (ssize_t
)strlen(s
);
2301 while ((p
= strstr_m(s
, pattern
))) {
2305 t
= (char *) SMB_MALLOC(ls
+ld
+1);
2307 DEBUG(0,("str_list_substitute: "
2308 "Unable to allocate memory"));
2311 memcpy(t
, *list
, d
);
2312 memcpy(t
+d
+li
, p
+lp
, ls
-d
-lp
+1);
2319 for (i
= 0; i
< li
; i
++) {
2320 switch (insert
[i
]) {
2332 t
[d
+i
] = insert
[i
];
2344 #define IPSTR_LIST_SEP ","
2345 #define IPSTR_LIST_CHAR ','
2348 * Add ip string representation to ipstr list. Used also
2349 * as part of @function ipstr_list_make
2351 * @param ipstr_list pointer to string containing ip list;
2352 * MUST BE already allocated and IS reallocated if necessary
2353 * @param ipstr_size pointer to current size of ipstr_list (might be changed
2354 * as a result of reallocation)
2355 * @param ip IP address which is to be added to list
2356 * @return pointer to string appended with new ip and possibly
2357 * reallocated to new length
2360 static char *ipstr_list_add(char **ipstr_list
, const struct ip_service
*service
)
2362 char *new_ipstr
= NULL
;
2363 char addr_buf
[INET6_ADDRSTRLEN
];
2365 /* arguments checking */
2366 if (!ipstr_list
|| !service
) {
2370 print_sockaddr(addr_buf
,
2374 /* attempt to convert ip to a string and append colon separator to it */
2376 if (service
->ss
.ss_family
== AF_INET
) {
2378 asprintf(&new_ipstr
, "%s%s%s:%d",
2385 asprintf(&new_ipstr
, "%s%s[%s]:%d",
2391 SAFE_FREE(*ipstr_list
);
2393 if (service
->ss
.ss_family
== AF_INET
) {
2395 asprintf(&new_ipstr
, "%s:%d",
2400 asprintf(&new_ipstr
, "[%s]:%d",
2405 *ipstr_list
= new_ipstr
;
2410 * Allocate and initialise an ipstr list using ip adresses
2411 * passed as arguments.
2413 * @param ipstr_list pointer to string meant to be allocated and set
2414 * @param ip_list array of ip addresses to place in the list
2415 * @param ip_count number of addresses stored in ip_list
2416 * @return pointer to allocated ip string
2419 char *ipstr_list_make(char **ipstr_list
,
2420 const struct ip_service
*ip_list
,
2425 /* arguments checking */
2426 if (!ip_list
|| !ipstr_list
) {
2432 /* process ip addresses given as arguments */
2433 for (i
= 0; i
< ip_count
; i
++) {
2434 *ipstr_list
= ipstr_list_add(ipstr_list
, &ip_list
[i
]);
2437 return (*ipstr_list
);
2442 * Parse given ip string list into array of ip addresses
2443 * (as ip_service structures)
2444 * e.g. [IPv6]:port,192.168.1.100:389,192.168.1.78, ...
2446 * @param ipstr ip string list to be parsed
2447 * @param ip_list pointer to array of ip addresses which is
2448 * allocated by this function and must be freed by caller
2449 * @return number of succesfully parsed addresses
2452 int ipstr_list_parse(const char *ipstr_list
, struct ip_service
**ip_list
)
2458 if (!ipstr_list
|| !ip_list
)
2461 count
= count_chars(ipstr_list
, IPSTR_LIST_CHAR
) + 1;
2462 if ( (*ip_list
= SMB_MALLOC_ARRAY(struct ip_service
, count
)) == NULL
) {
2463 DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n",
2464 (unsigned long)count
));
2468 for ( i
=0; next_token(&ipstr_list
, token_str
,
2469 IPSTR_LIST_SEP
, FSTRING_LEN
) && i
<count
; i
++ ) {
2470 char *s
= token_str
;
2471 char *p
= strrchr(token_str
, ':');
2475 (*ip_list
)[i
].port
= atoi(p
+1);
2478 /* convert single token to ip address */
2479 if (token_str
[0] == '[') {
2482 p
= strchr(token_str
, ']');
2488 if (!interpret_string_addr(&(*ip_list
)[i
].ss
,
2499 * Safely free ip string list
2501 * @param ipstr_list ip string list to be freed
2504 void ipstr_list_free(char* ipstr_list
)
2506 SAFE_FREE(ipstr_list
);
2510 Unescape a URL encoded string, in place.
2513 void rfc1738_unescape(char *buf
)
2517 while (p
&& *p
&& (p
=strchr_m(p
,'%'))) {
2521 if (c1
>= '0' && c1
<= '9')
2523 else if (c1
>= 'A' && c1
<= 'F')
2525 else if (c1
>= 'a' && c1
<= 'f')
2527 else {p
++; continue;}
2529 if (c2
>= '0' && c2
<= '9')
2531 else if (c2
>= 'A' && c2
<= 'F')
2533 else if (c2
>= 'a' && c2
<= 'f')
2535 else {p
++; continue;}
2539 memmove(p
+1, p
+3, strlen(p
+3)+1);
2544 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2547 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
2549 DATA_BLOB
base64_decode_data_blob(const char *s
)
2551 int bit_offset
, byte_offset
, idx
, i
, n
;
2552 DATA_BLOB decoded
= data_blob(s
, strlen(s
)+1);
2553 unsigned char *d
= decoded
.data
;
2558 while (*s
&& (p
=strchr_m(b64
,*s
))) {
2559 idx
= (int)(p
- b64
);
2560 byte_offset
= (i
*6)/8;
2561 bit_offset
= (i
*6)%8;
2562 d
[byte_offset
] &= ~((1<<(8-bit_offset
))-1);
2563 if (bit_offset
< 3) {
2564 d
[byte_offset
] |= (idx
<< (2-bit_offset
));
2567 d
[byte_offset
] |= (idx
>> (bit_offset
-2));
2568 d
[byte_offset
+1] = 0;
2569 d
[byte_offset
+1] |= (idx
<< (8-(bit_offset
-2))) & 0xFF;
2575 if ((n
> 0) && (*s
== '=')) {
2585 * Decode a base64 string in-place - wrapper for the above
2587 void base64_decode_inplace(char *s
)
2589 DATA_BLOB decoded
= base64_decode_data_blob(s
);
2591 if ( decoded
.length
!= 0 ) {
2592 memcpy(s
, decoded
.data
, decoded
.length
);
2594 /* null terminate */
2595 s
[decoded
.length
] = '\0';
2600 data_blob_free(&decoded
);
2604 * Encode a base64 string into a malloc()ed string caller to free.
2606 * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
2610 char *base64_encode_data_blob(DATA_BLOB data
)
2614 size_t out_cnt
, len
, output_len
;
2617 if (!data
.length
|| !data
.data
)
2622 output_len
= data
.length
* 2;
2623 result
= TALLOC_ARRAY(talloc_tos(), char, output_len
); /* get us plenty of space */
2624 SMB_ASSERT(result
!= NULL
);
2626 while (len
-- && out_cnt
< (data
.length
* 2) - 5) {
2627 int c
= (unsigned char) *(data
.data
++);
2630 if (char_count
== 3) {
2631 result
[out_cnt
++] = b64
[bits
>> 18];
2632 result
[out_cnt
++] = b64
[(bits
>> 12) & 0x3f];
2633 result
[out_cnt
++] = b64
[(bits
>> 6) & 0x3f];
2634 result
[out_cnt
++] = b64
[bits
& 0x3f];
2641 if (char_count
!= 0) {
2642 bits
<<= 16 - (8 * char_count
);
2643 result
[out_cnt
++] = b64
[bits
>> 18];
2644 result
[out_cnt
++] = b64
[(bits
>> 12) & 0x3f];
2645 if (char_count
== 1) {
2646 result
[out_cnt
++] = '=';
2647 result
[out_cnt
++] = '=';
2649 result
[out_cnt
++] = b64
[(bits
>> 6) & 0x3f];
2650 result
[out_cnt
++] = '=';
2653 result
[out_cnt
] = '\0'; /* terminate */
2657 /* read a SMB_BIG_UINT from a string */
2658 SMB_BIG_UINT
STR_TO_SMB_BIG_UINT(const char *nptr
, const char **entptr
)
2661 SMB_BIG_UINT val
= -1;
2662 const char *p
= nptr
;
2671 while (*p
&& isspace(*p
))
2674 #ifdef LARGE_SMB_OFF_T
2675 sscanf(p
,"%llu",&val
);
2676 #else /* LARGE_SMB_OFF_T */
2677 sscanf(p
,"%lu",&val
);
2678 #endif /* LARGE_SMB_OFF_T */
2680 while (*p
&& isdigit(*p
))
2688 /* Convert a size specification to a count of bytes. We accept the following
2690 * bytes if there is no suffix
2695 * pP whatever the ISO name for petabytes is
2697 * Returns 0 if the string can't be converted.
2699 SMB_OFF_T
conv_str_size(const char * str
)
2704 if (str
== NULL
|| *str
== '\0') {
2708 #ifdef HAVE_STRTOULL
2709 if (sizeof(SMB_OFF_T
) == 8) {
2710 lval
= strtoull(str
, &end
, 10 /* base */);
2712 lval
= strtoul(str
, &end
, 10 /* base */);
2715 lval
= strtoul(str
, &end
, 10 /* base */);
2718 if (end
== NULL
|| end
== str
) {
2723 SMB_OFF_T lval_orig
= lval
;
2725 if (strwicmp(end
, "K") == 0) {
2726 lval
*= (SMB_OFF_T
)1024;
2727 } else if (strwicmp(end
, "M") == 0) {
2728 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024);
2729 } else if (strwicmp(end
, "G") == 0) {
2730 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2732 } else if (strwicmp(end
, "T") == 0) {
2733 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2734 (SMB_OFF_T
)1024 * (SMB_OFF_T
)1024);
2735 } else if (strwicmp(end
, "P") == 0) {
2736 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2737 (SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2743 /* Primitive attempt to detect wrapping on platforms with
2744 * 4-byte SMB_OFF_T. It's better to let the caller handle
2745 * a failure than some random number.
2747 if (lval_orig
<= lval
) {
2755 void string_append(char **left
, const char *right
)
2757 int new_len
= strlen(right
) + 1;
2759 if (*left
== NULL
) {
2760 *left
= (char *)SMB_MALLOC(new_len
);
2763 new_len
+= strlen(*left
);
2764 *left
= (char *)SMB_REALLOC(*left
, new_len
);
2767 if (*left
== NULL
) {
2771 safe_strcat(*left
, right
, new_len
-1);
2774 bool add_string_to_array(TALLOC_CTX
*mem_ctx
,
2775 const char *str
, const char ***strings
,
2778 char *dup_str
= talloc_strdup(mem_ctx
, str
);
2780 *strings
= TALLOC_REALLOC_ARRAY(mem_ctx
, *strings
,
2781 const char *, (*num
)+1);
2783 if ((*strings
== NULL
) || (dup_str
== NULL
)) {
2788 (*strings
)[*num
] = dup_str
;
2793 /* Append an sprintf'ed string. Double buffer size on demand. Usable without
2794 * error checking in between. The indiation that something weird happened is
2797 void sprintf_append(TALLOC_CTX
*mem_ctx
, char **string
, ssize_t
*len
,
2798 size_t *bufsize
, const char *fmt
, ...)
2805 /* len<0 is an internal marker that something failed */
2809 if (*string
== NULL
) {
2813 *string
= TALLOC_ARRAY(mem_ctx
, char, *bufsize
);
2814 if (*string
== NULL
)
2819 ret
= vasprintf(&newstr
, fmt
, ap
);
2827 while ((*len
)+ret
>= *bufsize
) {
2830 if (*bufsize
>= (1024*1024*256))
2835 *string
= TALLOC_REALLOC_ARRAY(mem_ctx
, *string
, char,
2837 if (*string
== NULL
) {
2842 StrnCpy((*string
)+(*len
), newstr
, ret
);
2853 * asprintf into a string and strupper_m it after that.
2856 int asprintf_strupper_m(char **strp
, const char *fmt
, ...)
2863 ret
= vasprintf(&result
, fmt
, ap
);
2874 char *talloc_asprintf_strupper_m(TALLOC_CTX
*t
, const char *fmt
, ...)
2880 ret
= talloc_vasprintf(t
, fmt
, ap
);
2891 Returns the substring from src between the first occurrence of
2892 the char "front" and the first occurence of the char "back".
2893 Mallocs the return string which must be freed. Not for use
2894 with wide character strings.
2896 char *sstring_sub(const char *src
, char front
, char back
)
2898 char *temp1
, *temp2
, *temp3
;
2901 temp1
= strchr(src
, front
);
2902 if (temp1
== NULL
) return NULL
;
2903 temp2
= strchr(src
, back
);
2904 if (temp2
== NULL
) return NULL
;
2905 len
= temp2
- temp1
;
2906 if (len
<= 0) return NULL
;
2907 temp3
= (char*)SMB_MALLOC(len
);
2908 if (temp3
== NULL
) {
2909 DEBUG(1,("Malloc failure in sstring_sub\n"));
2912 memcpy(temp3
, temp1
+1, len
-1);
2913 temp3
[len
-1] = '\0';
2917 /********************************************************************
2918 Check a string for any occurrences of a specified list of invalid
2920 ********************************************************************/
2922 bool validate_net_name( const char *name
,
2923 const char *invalid_chars
,
2928 for ( i
=0; i
<max_len
&& name
[i
]; i
++ ) {
2929 /* fail if strchr_m() finds one of the invalid characters */
2930 if ( name
[i
] && strchr_m( invalid_chars
, name
[i
] ) ) {
2940 return the number of bytes occupied by a buffer in ASCII format
2941 the result includes the null termination
2942 limited by 'n' bytes
2944 size_t ascii_len_n(const char *src
, size_t n
)
2948 len
= strnlen(src
, n
);
2957 return the number of bytes occupied by a buffer in CH_UTF16 format
2958 the result includes the null termination
2960 size_t utf16_len(const void *buf
)
2964 for (len
= 0; SVAL(buf
,len
); len
+= 2) ;
2970 return the number of bytes occupied by a buffer in CH_UTF16 format
2971 the result includes the null termination
2972 limited by 'n' bytes
2974 size_t utf16_len_n(const void *src
, size_t n
)
2978 for (len
= 0; (len
+2 < n
) && SVAL(src
, len
); len
+= 2) ;
2987 /*******************************************************************
2988 Add a shell escape character '\' to any character not in a known list
2989 of characters. UNIX charset format.
2990 *******************************************************************/
2992 #define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_/ \t.,"
2993 #define INSIDE_DQUOTE_LIST "$`\n\"\\"
2995 char *escape_shell_string(const char *src
)
2997 size_t srclen
= strlen(src
);
2998 char *ret
= SMB_MALLOC_ARRAY(char, (srclen
* 2) + 1);
3000 bool in_s_quote
= false;
3001 bool in_d_quote
= false;
3002 bool next_escaped
= false;
3010 codepoint_t c
= next_codepoint(src
, &c_size
);
3012 if (c
== INVALID_CODEPOINT
) {
3018 memcpy(dest
, src
, c_size
);
3021 next_escaped
= false;
3026 * Deal with backslash escaped state.
3027 * This only lasts for one character.
3032 next_escaped
= false;
3037 * Deal with single quote state. The
3038 * only thing we care about is exiting
3051 * Deal with double quote state. The most
3052 * complex state. We must cope with \, meaning
3053 * possibly escape next char (depending what it
3054 * is), ", meaning exit this state, and possibly
3055 * add an \ escape to any unprotected character
3056 * (listed in INSIDE_DQUOTE_LIST).
3062 * Next character might be escaped.
3063 * We have to peek. Inside double
3064 * quotes only INSIDE_DQUOTE_LIST
3065 * characters are escaped by a \.
3070 c
= next_codepoint(&src
[1], &c_size
);
3071 if (c
== INVALID_CODEPOINT
) {
3077 * Don't escape the next char.
3086 if (nextchar
&& strchr(INSIDE_DQUOTE_LIST
,
3088 next_escaped
= true;
3095 /* Exit double quote state. */
3102 * We know the character isn't \ or ",
3103 * so escape it if it's any of the other
3104 * possible unprotected characters.
3107 if (strchr(INSIDE_DQUOTE_LIST
, (int)*src
)) {
3115 * From here to the end of the loop we're
3116 * not in the single or double quote state.
3120 /* Next character must be escaped. */
3121 next_escaped
= true;
3127 /* Go into single quote state. */
3134 /* Go into double quote state. */
3140 /* Check if we need to escape the character. */
3142 if (!strchr(INCLUDE_LIST
, (int)*src
)) {