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/>.
27 const char toupper_ascii_fast_table
[128] = {
28 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
29 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
30 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
31 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
32 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
33 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
34 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
35 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
39 * Case insensitive string compararison.
41 * iconv does not directly give us a way to compare strings in
42 * arbitrary unix character sets -- all we can is convert and then
43 * compare. This is expensive.
45 * As an optimization, we do a first pass that considers only the
46 * prefix of the strings that is entirely 7-bit. Within this, we
47 * check whether they have the same value.
49 * Hopefully this will often give the answer without needing to copy.
50 * In particular it should speed comparisons to literal ascii strings
51 * or comparisons of strings that are "obviously" different.
53 * If we find a non-ascii character we fall back to converting via
56 * This should never be slower than convering the whole thing, and
59 * A different optimization would be to compare for bitwise equality
60 * in the binary encoding. (It would be possible thought hairy to do
61 * both simultaneously.) But in that case if they turn out to be
62 * different, we'd need to restart the whole thing.
64 * Even better is to implement strcasecmp for each encoding and use a
67 int StrCaseCmp(const char *s
, const char *t
)
72 smb_ucs2_t
*buffer_s
, *buffer_t
;
75 for (ps
= s
, pt
= t
; ; ps
++, pt
++) {
79 return 0; /* both ended */
81 return -1; /* s is a prefix */
83 return +1; /* t is a prefix */
84 else if ((*ps
& 0x80) || (*pt
& 0x80))
85 /* not ascii anymore, do it the hard way
89 us
= toupper_ascii_fast(*ps
);
90 ut
= toupper_ascii_fast(*pt
);
99 if (!push_ucs2_talloc(talloc_tos(), &buffer_s
, ps
, &size
)) {
100 return strcmp(ps
, pt
);
101 /* Not quite the right answer, but finding the right one
102 under this failure case is expensive, and it's pretty
106 if (!push_ucs2_talloc(talloc_tos(), &buffer_t
, pt
, &size
)) {
107 TALLOC_FREE(buffer_s
);
108 return strcmp(ps
, pt
);
109 /* Not quite the right answer, but finding the right one
110 under this failure case is expensive, and it's pretty
114 ret
= strcasecmp_w(buffer_s
, buffer_t
);
115 TALLOC_FREE(buffer_s
);
116 TALLOC_FREE(buffer_t
);
122 Case insensitive string compararison, length limited.
124 int StrnCaseCmp(const char *s
, const char *t
, size_t len
)
129 smb_ucs2_t
*buffer_s
, *buffer_t
;
132 for (ps
= s
, pt
= t
; n
< len
; ps
++, pt
++, n
++) {
136 return 0; /* both ended */
138 return -1; /* s is a prefix */
140 return +1; /* t is a prefix */
141 else if ((*ps
& 0x80) || (*pt
& 0x80))
142 /* not ascii anymore, do it the
143 * hard way from here on in */
146 us
= toupper_ascii_fast(*ps
);
147 ut
= toupper_ascii_fast(*pt
);
160 if (!push_ucs2_talloc(talloc_tos(), &buffer_s
, ps
, &size
)) {
161 return strncmp(ps
, pt
, len
-n
);
162 /* Not quite the right answer, but finding the right one
163 under this failure case is expensive,
164 and it's pretty close */
167 if (!push_ucs2_talloc(talloc_tos(), &buffer_t
, pt
, &size
)) {
168 TALLOC_FREE(buffer_s
);
169 return strncmp(ps
, pt
, len
-n
);
170 /* Not quite the right answer, but finding the right one
171 under this failure case is expensive,
172 and it's pretty close */
175 ret
= strncasecmp_w(buffer_s
, buffer_t
, len
-n
);
176 TALLOC_FREE(buffer_s
);
177 TALLOC_FREE(buffer_t
);
184 * @note The comparison is case-insensitive.
186 bool strequal(const char *s1
, const char *s2
)
193 return(StrCaseCmp(s1
,s2
)==0);
197 * Compare 2 strings up to and including the nth char.
199 * @note The comparison is case-insensitive.
201 bool strnequal(const char *s1
,const char *s2
,size_t n
)
205 if (!s1
|| !s2
|| !n
)
208 return(StrnCaseCmp(s1
,s2
,n
)==0);
212 Compare 2 strings (case sensitive).
215 bool strcsequal(const char *s1
,const char *s2
)
222 return(strcmp(s1
,s2
)==0);
226 Do a case-insensitive, whitespace-ignoring string compare.
229 int strwicmp(const char *psz1
, const char *psz2
)
231 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
232 /* appropriate value. */
235 else if (psz1
== NULL
)
237 else if (psz2
== NULL
)
240 /* sync the strings on first non-whitespace */
242 while (isspace((int)*psz1
))
244 while (isspace((int)*psz2
))
246 if (toupper_ascii(*psz1
) != toupper_ascii(*psz2
) ||
247 *psz1
== '\0' || *psz2
== '\0')
252 return (*psz1
- *psz2
);
256 Convert a string to "normal" form.
259 void strnorm(char *s
, int case_default
)
261 if (case_default
== CASE_UPPER
)
268 Check if a string is in "normal" case.
271 bool strisnormal(const char *s
, int case_default
)
273 if (case_default
== CASE_UPPER
)
274 return(!strhaslower(s
));
276 return(!strhasupper(s
));
282 NOTE: oldc and newc must be 7 bit characters
284 void string_replace( char *s
, char oldc
, char newc
)
288 /* this is quite a common operation, so we want it to be
289 fast. We optimise for the ascii case, knowing that all our
290 supported multi-byte character sets are ascii-compatible
291 (ie. they match for the first 128 chars) */
293 for (p
= s
; *p
; p
++) {
294 if (*p
& 0x80) /* mb string - slow path. */
304 /* Slow (mb) path. */
305 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
306 /* With compose characters we must restart from the beginning. JRA. */
312 next_codepoint(p
, &c_size
);
324 * Skip past some strings in a buffer - old version - no checks.
327 char *push_skip_string(char *buf
)
329 buf
+= strlen(buf
) + 1;
334 Skip past a string in a buffer. Buffer may not be
335 null terminated. end_ptr points to the first byte after
336 then end of the buffer.
339 char *skip_string(const char *base
, size_t len
, char *buf
)
341 const char *end_ptr
= base
+ len
;
343 if (end_ptr
< base
|| !base
|| !buf
|| buf
>= end_ptr
) {
347 /* Skip the string */
350 if (buf
>= end_ptr
) {
360 Count the number of characters in a string. Normally this will
361 be the same as the number of bytes in a string for single byte strings,
362 but will be different for multibyte.
365 size_t str_charnum(const char *s
)
367 size_t ret
, converted_size
;
368 smb_ucs2_t
*tmpbuf2
= NULL
;
369 if (!push_ucs2_talloc(talloc_tos(), &tmpbuf2
, s
, &converted_size
)) {
372 ret
= strlen_w(tmpbuf2
);
373 TALLOC_FREE(tmpbuf2
);
378 Count the number of characters in a string. Normally this will
379 be the same as the number of bytes in a string for single byte strings,
380 but will be different for multibyte.
383 size_t str_ascii_charnum(const char *s
)
385 size_t ret
, converted_size
;
386 char *tmpbuf2
= NULL
;
387 if (!push_ascii_talloc(talloc_tos(), &tmpbuf2
, s
, &converted_size
)) {
390 ret
= strlen(tmpbuf2
);
391 TALLOC_FREE(tmpbuf2
);
395 bool trim_char(char *s
,char cfront
,char cback
)
401 /* Ignore null or empty strings. */
402 if (!s
|| (s
[0] == '\0'))
406 while (*fp
&& *fp
== cfront
)
409 /* We ate the string. */
417 ep
= fp
+ strlen(fp
) - 1;
419 /* Attempt ascii only. Bail for mb strings. */
420 while ((ep
>= fp
) && (*ep
== cback
)) {
422 if ((ep
> fp
) && (((unsigned char)ep
[-1]) & 0x80)) {
423 /* Could be mb... bail back to tim_string. */
431 return trim_string(s
, cfront
? fs
: NULL
, bs
);
437 /* We ate the string. */
444 memmove(s
, fp
, ep
-fp
+2);
449 Does a string have any uppercase chars in it?
452 bool strhasupper(const char *s
)
456 size_t converted_size
;
458 if (!push_ucs2_talloc(talloc_tos(), &tmp
, s
, &converted_size
)) {
462 for(p
= tmp
; *p
!= 0; p
++) {
474 Does a string have any lowercase chars in it?
477 bool strhaslower(const char *s
)
481 size_t converted_size
;
483 if (!push_ucs2_talloc(talloc_tos(), &tmp
, s
, &converted_size
)) {
487 for(p
= tmp
; *p
!= 0; p
++) {
499 Safe string copy into a known length string. maxlength does not
500 include the terminating zero.
503 char *safe_strcpy_fn(const char *fn
,
512 DEBUG(0,("ERROR: NULL dest in safe_strcpy, "
513 "called from [%s][%d]\n", fn
, line
));
518 clobber_region(fn
,line
,dest
, maxlength
+1);
526 len
= strnlen(src
, maxlength
+1);
528 if (len
> maxlength
) {
529 DEBUG(0,("ERROR: string overflow by "
530 "%lu (%lu - %lu) in safe_strcpy [%.50s]\n",
531 (unsigned long)(len
-maxlength
), (unsigned long)len
,
532 (unsigned long)maxlength
, src
));
536 memmove(dest
, src
, len
);
542 Safe string cat into a string. maxlength does not
543 include the terminating zero.
545 char *safe_strcat_fn(const char *fn
,
551 size_t src_len
, dest_len
;
554 DEBUG(0,("ERROR: NULL dest in safe_strcat, "
555 "called from [%s][%d]\n", fn
, line
));
562 src_len
= strnlen(src
, maxlength
+ 1);
563 dest_len
= strnlen(dest
, maxlength
+ 1);
566 clobber_region(fn
, line
, dest
+ dest_len
, maxlength
+ 1 - dest_len
);
569 if (src_len
+ dest_len
> maxlength
) {
570 DEBUG(0,("ERROR: string overflow by %d "
571 "in safe_strcat [%.50s]\n",
572 (int)(src_len
+ dest_len
- maxlength
), src
));
573 if (maxlength
> dest_len
) {
574 memcpy(&dest
[dest_len
], src
, maxlength
- dest_len
);
580 memcpy(&dest
[dest_len
], src
, src_len
);
581 dest
[dest_len
+ src_len
] = 0;
586 Paranoid strcpy into a buffer of given length (includes terminating
587 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
588 and replaces with '_'. Deliberately does *NOT* check for multibyte
589 characters. Don't change it !
592 char *alpha_strcpy_fn(const char *fn
,
596 const char *other_safe_chars
,
602 clobber_region(fn
, line
, dest
, maxlength
);
606 DEBUG(0,("ERROR: NULL dest in alpha_strcpy, "
607 "called from [%s][%d]\n", fn
, line
));
617 if (len
>= maxlength
)
620 if (!other_safe_chars
)
621 other_safe_chars
= "";
623 for(i
= 0; i
< len
; i
++) {
624 int val
= (src
[i
] & 0xff);
625 if (isupper_ascii(val
) || islower_ascii(val
) ||
626 isdigit(val
) || strchr_m(other_safe_chars
, val
))
638 Like strncpy but always null terminates. Make sure there is room!
639 The variable n should always be one less than the available size.
641 char *StrnCpy_fn(const char *fn
, int line
,char *dest
,const char *src
,size_t n
)
646 clobber_region(fn
, line
, dest
, n
+1);
650 DEBUG(0,("ERROR: NULL dest in StrnCpy, "
651 "called from [%s][%d]\n", fn
, line
));
660 while (n
-- && (*d
= *src
)) {
671 Like strncpy but copies up to the character marker. always null terminates.
672 returns a pointer to the character marker in the source string (src).
675 static char *strncpyn(char *dest
, const char *src
, size_t n
, char c
)
681 clobber_region(dest
, n
+1);
683 p
= strchr_m(src
, c
);
685 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c
));
689 str_len
= PTR_DIFF(p
, src
);
690 strncpy(dest
, src
, MIN(n
, str_len
));
691 dest
[str_len
] = '\0';
698 Check if a string is part of a list.
701 bool in_list(const char *s
, const char *list
, bool casesensitive
)
711 frame
= talloc_stackframe();
712 while (next_token_talloc(frame
, &list
, &tok
,LIST_SEP
)) {
714 if (strcmp(tok
,s
) == 0) {
719 if (StrCaseCmp(tok
,s
) == 0) {
729 /* this is used to prevent lots of mallocs of size 1 */
730 static const char null_string
[] = "";
733 Set a string value, allocing the space for the string
736 static bool string_init(char **dest
,const char *src
)
746 *dest
= CONST_DISCARD(char*, null_string
);
748 (*dest
) = SMB_STRDUP(src
);
749 if ((*dest
) == NULL
) {
750 DEBUG(0,("Out of memory in string_init\n"));
761 void string_free(char **s
)
765 if (*s
== null_string
)
771 Set a string value, deallocating any existing space, and allocing the space
775 bool string_set(char **dest
,const char *src
)
778 return(string_init(dest
,src
));
782 Substitute a string for a pattern in another string. Make sure there is
785 This routine looks for pattern in s and replaces it with
786 insert. It may do multiple replacements or just one.
788 Any of " ; ' $ or ` in the insert string are replaced with _
789 if len==0 then the string cannot be extended. This is different from the old
790 use of len==0 which was for no length checks to be done.
793 void string_sub2(char *s
,const char *pattern
, const char *insert
, size_t len
,
794 bool remove_unsafe_characters
, bool replace_once
,
795 bool allow_trailing_dollar
)
800 if (!insert
|| !pattern
|| !*pattern
|| !s
)
803 ls
= (ssize_t
)strlen(s
);
804 lp
= (ssize_t
)strlen(pattern
);
805 li
= (ssize_t
)strlen(insert
);
808 len
= ls
+ 1; /* len is number of *bytes* */
810 while (lp
<= ls
&& (p
= strstr_m(s
,pattern
))) {
811 if (ls
+ (li
-lp
) >= len
) {
812 DEBUG(0,("ERROR: string overflow by "
813 "%d in string_sub(%.50s, %d)\n",
814 (int)(ls
+ (li
-lp
) - len
),
819 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
824 /* allow a trailing $
825 * (as in machine accounts) */
826 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
837 if ( remove_unsafe_characters
) {
839 /* yes this break should be here
840 * since we want to fall throw if
841 * not replacing unsafe chars */
856 void string_sub_once(char *s
, const char *pattern
,
857 const char *insert
, size_t len
)
859 string_sub2( s
, pattern
, insert
, len
, true, true, false );
862 void string_sub(char *s
,const char *pattern
, const char *insert
, size_t len
)
864 string_sub2( s
, pattern
, insert
, len
, true, false, false );
867 void fstring_sub(char *s
,const char *pattern
,const char *insert
)
869 string_sub(s
, pattern
, insert
, sizeof(fstring
));
873 Similar to string_sub2, but it will accept only allocated strings
874 and may realloc them so pay attention at what you pass on no
875 pointers inside strings, no const may be passed
879 char *realloc_string_sub2(char *string
,
882 bool remove_unsafe_characters
,
883 bool allow_trailing_dollar
)
887 ssize_t ls
,lp
,li
,ld
, i
;
889 if (!insert
|| !pattern
|| !*pattern
|| !string
|| !*string
)
894 in
= SMB_STRDUP(insert
);
896 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
899 ls
= (ssize_t
)strlen(s
);
900 lp
= (ssize_t
)strlen(pattern
);
901 li
= (ssize_t
)strlen(insert
);
906 /* allow a trailing $
907 * (as in machine accounts) */
908 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
918 if ( remove_unsafe_characters
) {
928 while ((p
= strstr_m(s
,pattern
))) {
930 int offset
= PTR_DIFF(s
,string
);
931 string
= (char *)SMB_REALLOC(string
, ls
+ ld
+ 1);
933 DEBUG(0, ("realloc_string_sub: "
934 "out of memory!\n"));
938 p
= string
+ offset
+ (p
- s
);
941 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
951 char *realloc_string_sub(char *string
,
955 return realloc_string_sub2(string
, pattern
, insert
, true, false);
959 * Internal guts of talloc_string_sub and talloc_all_string_sub.
960 * talloc version of string_sub2.
963 char *talloc_string_sub2(TALLOC_CTX
*mem_ctx
, const char *src
,
966 bool remove_unsafe_characters
,
968 bool allow_trailing_dollar
)
973 ssize_t ls
,lp
,li
,ld
, i
;
975 if (!insert
|| !pattern
|| !*pattern
|| !src
) {
979 string
= talloc_strdup(mem_ctx
, src
);
980 if (string
== NULL
) {
981 DEBUG(0, ("talloc_string_sub2: "
982 "talloc_strdup failed\n"));
988 in
= SMB_STRDUP(insert
);
990 DEBUG(0, ("talloc_string_sub2: ENOMEM\n"));
993 ls
= (ssize_t
)strlen(s
);
994 lp
= (ssize_t
)strlen(pattern
);
995 li
= (ssize_t
)strlen(insert
);
1001 /* allow a trailing $
1002 * (as in machine accounts) */
1003 if (allow_trailing_dollar
&& (i
== li
- 1 )) {
1013 if (remove_unsafe_characters
) {
1023 while ((p
= strstr_m(s
,pattern
))) {
1025 int offset
= PTR_DIFF(s
,string
);
1026 string
= (char *)TALLOC_REALLOC(mem_ctx
, string
,
1029 DEBUG(0, ("talloc_string_sub: out of "
1034 p
= string
+ offset
+ (p
- s
);
1037 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
1051 /* Same as string_sub, but returns a talloc'ed string */
1053 char *talloc_string_sub(TALLOC_CTX
*mem_ctx
,
1055 const char *pattern
,
1058 return talloc_string_sub2(mem_ctx
, src
, pattern
, insert
,
1059 true, false, false);
1063 Similar to string_sub() but allows for any character to be substituted.
1065 if len==0 then the string cannot be extended. This is different from the old
1066 use of len==0 which was for no length checks to be done.
1069 void all_string_sub(char *s
,const char *pattern
,const char *insert
, size_t len
)
1074 if (!insert
|| !pattern
|| !s
)
1077 ls
= (ssize_t
)strlen(s
);
1078 lp
= (ssize_t
)strlen(pattern
);
1079 li
= (ssize_t
)strlen(insert
);
1085 len
= ls
+ 1; /* len is number of *bytes* */
1087 while (lp
<= ls
&& (p
= strstr_m(s
,pattern
))) {
1088 if (ls
+ (li
-lp
) >= len
) {
1089 DEBUG(0,("ERROR: string overflow by "
1090 "%d in all_string_sub(%.50s, %d)\n",
1091 (int)(ls
+ (li
-lp
) - len
),
1092 pattern
, (int)len
));
1096 memmove(p
+li
,p
+lp
,strlen(p
+lp
)+1);
1098 memcpy(p
, insert
, li
);
1104 char *talloc_all_string_sub(TALLOC_CTX
*ctx
,
1106 const char *pattern
,
1109 return talloc_string_sub2(ctx
, src
, pattern
, insert
,
1110 false, false, false);
1114 Write an octal as a string.
1117 char *octal_string(int i
)
1121 result
= talloc_strdup(talloc_tos(), "-1");
1124 result
= talloc_asprintf(talloc_tos(), "0%o", i
);
1126 SMB_ASSERT(result
!= NULL
);
1132 Truncate a string at a specified length.
1135 char *string_truncate(char *s
, unsigned int length
)
1137 if (s
&& strlen(s
) > length
)
1143 Strchr and strrchr_m are very hard to do on general multi-byte strings.
1144 We convert via ucs2 for now.
1147 char *strchr_m(const char *src
, char c
)
1149 smb_ucs2_t
*ws
= NULL
;
1154 size_t converted_size
;
1156 /* characters below 0x3F are guaranteed to not appear in
1157 non-initial position in multi-byte charsets */
1158 if ((c
& 0xC0) == 0) {
1159 return strchr(src
, c
);
1162 /* this is quite a common operation, so we want it to be
1163 fast. We optimise for the ascii case, knowing that all our
1164 supported multi-byte character sets are ascii-compatible
1165 (ie. they match for the first 128 chars) */
1167 for (s
= src
; *s
&& !(((unsigned char)s
[0]) & 0x80); s
++) {
1175 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
1176 /* With compose characters we must restart from the beginning. JRA. */
1180 if (!push_ucs2_talloc(talloc_tos(), &ws
, s
, &converted_size
)) {
1181 /* Wrong answer, but what can we do... */
1182 return strchr(src
, c
);
1184 p
= strchr_w(ws
, UCS2_CHAR(c
));
1190 if (!pull_ucs2_talloc(talloc_tos(), &s2
, ws
, &converted_size
)) {
1192 /* Wrong answer, but what can we do... */
1193 return strchr(src
, c
);
1195 ret
= (char *)(s
+strlen(s2
));
1201 char *strrchr_m(const char *s
, char c
)
1203 /* characters below 0x3F are guaranteed to not appear in
1204 non-initial position in multi-byte charsets */
1205 if ((c
& 0xC0) == 0) {
1206 return strrchr(s
, c
);
1209 /* this is quite a common operation, so we want it to be
1210 fast. We optimise for the ascii case, knowing that all our
1211 supported multi-byte character sets are ascii-compatible
1212 (ie. they match for the first 128 chars). Also, in Samba
1213 we only search for ascii characters in 'c' and that
1214 in all mb character sets with a compound character
1215 containing c, if 'c' is not a match at position
1216 p, then p[-1] > 0x7f. JRA. */
1219 size_t len
= strlen(s
);
1221 bool got_mb
= false;
1228 /* Could be a match. Part of a multibyte ? */
1230 (((unsigned char)cp
[-1]) & 0x80)) {
1231 /* Yep - go slow :-( */
1235 /* No - we have a match ! */
1238 } while (cp
-- != s
);
1243 /* String contained a non-ascii char. Slow path. */
1245 smb_ucs2_t
*ws
= NULL
;
1249 size_t converted_size
;
1251 if (!push_ucs2_talloc(talloc_tos(), &ws
, s
, &converted_size
)) {
1252 /* Wrong answer, but what can we do. */
1253 return strrchr(s
, c
);
1255 p
= strrchr_w(ws
, UCS2_CHAR(c
));
1261 if (!pull_ucs2_talloc(talloc_tos(), &s2
, ws
, &converted_size
)) {
1263 /* Wrong answer, but what can we do. */
1264 return strrchr(s
, c
);
1266 ret
= (char *)(s
+strlen(s2
));
1273 /***********************************************************************
1274 Return the equivalent of doing strrchr 'n' times - always going
1276 ***********************************************************************/
1278 char *strnrchr_m(const char *s
, char c
, unsigned int n
)
1280 smb_ucs2_t
*ws
= NULL
;
1284 size_t converted_size
;
1286 if (!push_ucs2_talloc(talloc_tos(), &ws
, s
, &converted_size
)) {
1287 /* Too hard to try and get right. */
1290 p
= strnrchr_w(ws
, UCS2_CHAR(c
), n
);
1296 if (!pull_ucs2_talloc(talloc_tos(), &s2
, ws
, &converted_size
)) {
1298 /* Too hard to try and get right. */
1301 ret
= (char *)(s
+strlen(s2
));
1307 /***********************************************************************
1308 strstr_m - We convert via ucs2 for now.
1309 ***********************************************************************/
1311 char *strstr_m(const char *src
, const char *findstr
)
1314 smb_ucs2_t
*src_w
, *find_w
;
1319 size_t converted_size
, findstr_len
= 0;
1321 /* for correctness */
1326 /* Samba does single character findstr calls a *lot*. */
1327 if (findstr
[1] == '\0')
1328 return strchr_m(src
, *findstr
);
1330 /* We optimise for the ascii case, knowing that all our
1331 supported multi-byte character sets are ascii-compatible
1332 (ie. they match for the first 128 chars) */
1334 for (s
= src
; *s
&& !(((unsigned char)s
[0]) & 0x80); s
++) {
1335 if (*s
== *findstr
) {
1337 findstr_len
= strlen(findstr
);
1339 if (strncmp(s
, findstr
, findstr_len
) == 0) {
1348 #if 1 /* def BROKEN_UNICODE_COMPOSE_CHARACTERS */
1349 /* 'make check' fails unless we do this */
1351 /* With compose characters we must restart from the beginning. JRA. */
1355 if (!push_ucs2_talloc(talloc_tos(), &src_w
, src
, &converted_size
)) {
1356 DEBUG(0,("strstr_m: src malloc fail\n"));
1360 if (!push_ucs2_talloc(talloc_tos(), &find_w
, findstr
, &converted_size
)) {
1362 DEBUG(0,("strstr_m: find malloc fail\n"));
1366 p
= strstr_w(src_w
, find_w
);
1370 TALLOC_FREE(find_w
);
1375 if (!pull_ucs2_talloc(talloc_tos(), &s2
, src_w
, &converted_size
)) {
1377 TALLOC_FREE(find_w
);
1378 DEBUG(0,("strstr_m: dest malloc fail\n"));
1381 retp
= (char *)(s
+strlen(s2
));
1383 TALLOC_FREE(find_w
);
1389 Convert a string to lower case.
1392 void strlower_m(char *s
)
1397 /* this is quite a common operation, so we want it to be
1398 fast. We optimise for the ascii case, knowing that all our
1399 supported multi-byte character sets are ascii-compatible
1400 (ie. they match for the first 128 chars) */
1402 while (*s
&& !(((unsigned char)s
[0]) & 0x80)) {
1403 *s
= tolower_ascii((unsigned char)*s
);
1410 /* I assume that lowercased string takes the same number of bytes
1411 * as source string even in UTF-8 encoding. (VIV) */
1412 len
= strlen(s
) + 1;
1415 unix_strlower(s
,len
,s
,len
);
1416 /* Catch mb conversion errors that may not terminate. */
1423 Convert a string to upper case.
1426 void strupper_m(char *s
)
1431 /* this is quite a common operation, so we want it to be
1432 fast. We optimise for the ascii case, knowing that all our
1433 supported multi-byte character sets are ascii-compatible
1434 (ie. they match for the first 128 chars) */
1436 while (*s
&& !(((unsigned char)s
[0]) & 0x80)) {
1437 *s
= toupper_ascii_fast((unsigned char)*s
);
1444 /* I assume that lowercased string takes the same number of bytes
1445 * as source string even in multibyte encoding. (VIV) */
1446 len
= strlen(s
) + 1;
1449 unix_strupper(s
,len
,s
,len
);
1450 /* Catch mb conversion errors that may not terminate. */
1457 * Calculate the number of units (8 or 16-bit, depending on the
1458 * destination charset), that would be needed to convert the input
1459 * string which is expected to be in in src_charset encoding to the
1460 * destination charset (which should be a unicode charset).
1463 size_t strlen_m_ext(const char *s
, const charset_t src_charset
,
1464 const charset_t dst_charset
)
1472 while (*s
&& !(((uint8_t)*s
) & 0x80)) {
1483 codepoint_t c
= next_codepoint_ext(s
, src_charset
, &c_size
);
1486 switch (dst_charset
) {
1489 case CH_UTF16MUNGED
:
1491 /* Unicode char fits into 16 bits. */
1494 /* Double-width unicode char - 32 bits. */
1500 * this only checks ranges, and does not
1501 * check for invalid codepoints
1505 } else if (c
< 0x800) {
1507 } else if (c
< 0x1000) {
1515 * non-unicode encoding:
1516 * assume that each codepoint fits into
1517 * one unit in the destination encoding.
1526 size_t strlen_m_ext_term(const char *s
, const charset_t src_charset
,
1527 const charset_t dst_charset
)
1532 return strlen_m_ext(s
, src_charset
, dst_charset
) + 1;
1536 * Calculate the number of 16-bit units that would bee needed to convert
1537 * the input string which is expected to be in CH_UNIX encoding to UTF16.
1539 * This will be the same as the number of bytes in a string for single
1540 * byte strings, but will be different for multibyte.
1543 size_t strlen_m(const char *s
)
1545 return strlen_m_ext(s
, CH_UNIX
, CH_UTF16LE
);
1549 Count the number of UCS2 characters in a string including the null
1553 size_t strlen_m_term(const char *s
)
1558 return strlen_m(s
) + 1;
1562 * Weird helper routine for the winreg pipe: If nothing is around, return 0,
1563 * if a string is there, include the terminator.
1566 size_t strlen_m_term_null(const char *s
)
1580 Return a RFC2254 binary string representation of a buffer.
1581 Used in LDAP filters.
1585 char *binary_string_rfc2254(TALLOC_CTX
*mem_ctx
, const uint8_t *buf
, int len
)
1589 const char *hex
= "0123456789ABCDEF";
1590 s
= talloc_array(mem_ctx
, char, len
* 3 + 1);
1594 for (j
=i
=0;i
<len
;i
++) {
1596 s
[j
+1] = hex
[((unsigned char)buf
[i
]) >> 4];
1597 s
[j
+2] = hex
[((unsigned char)buf
[i
]) & 0xF];
1604 char *binary_string(char *buf
, int len
)
1608 const char *hex
= "0123456789ABCDEF";
1609 s
= (char *)SMB_MALLOC(len
* 2 + 1);
1612 for (j
=i
=0;i
<len
;i
++) {
1613 s
[j
] = hex
[((unsigned char)buf
[i
]) >> 4];
1614 s
[j
+1] = hex
[((unsigned char)buf
[i
]) & 0xF];
1622 Just a typesafety wrapper for snprintf into a fstring.
1625 int fstr_sprintf(fstring s
, const char *fmt
, ...)
1631 ret
= vsnprintf(s
, FSTRING_LEN
, fmt
, ap
);
1637 List of Strings manipulation functions
1640 #define S_LIST_ABS 16 /* List Allocation Block Size */
1642 /******************************************************************************
1643 version of standard_sub_basic() for string lists; uses talloc_sub_basic()
1645 *****************************************************************************/
1647 bool str_list_sub_basic( char **list
, const char *smb_name
,
1648 const char *domain_name
)
1650 TALLOC_CTX
*ctx
= list
;
1655 tmpstr
= talloc_sub_basic(ctx
, smb_name
, domain_name
, s
);
1657 DEBUG(0,("str_list_sub_basic: "
1658 "alloc_sub_basic() return NULL!\n"));
1671 /******************************************************************************
1672 substitute a specific pattern in a string list
1673 *****************************************************************************/
1675 bool str_list_substitute(char **list
, const char *pattern
, const char *insert
)
1677 TALLOC_CTX
*ctx
= list
;
1679 ssize_t ls
, lp
, li
, ld
, i
, d
;
1688 lp
= (ssize_t
)strlen(pattern
);
1689 li
= (ssize_t
)strlen(insert
);
1694 ls
= (ssize_t
)strlen(s
);
1696 while ((p
= strstr_m(s
, pattern
))) {
1700 t
= TALLOC_ARRAY(ctx
, char, ls
+ld
+1);
1702 DEBUG(0,("str_list_substitute: "
1703 "Unable to allocate memory"));
1706 memcpy(t
, *list
, d
);
1707 memcpy(t
+d
+li
, p
+lp
, ls
-d
-lp
+1);
1714 for (i
= 0; i
< li
; i
++) {
1715 switch (insert
[i
]) {
1727 t
[d
+i
] = insert
[i
];
1739 #define IPSTR_LIST_SEP ","
1740 #define IPSTR_LIST_CHAR ','
1743 * Add ip string representation to ipstr list. Used also
1744 * as part of @function ipstr_list_make
1746 * @param ipstr_list pointer to string containing ip list;
1747 * MUST BE already allocated and IS reallocated if necessary
1748 * @param ipstr_size pointer to current size of ipstr_list (might be changed
1749 * as a result of reallocation)
1750 * @param ip IP address which is to be added to list
1751 * @return pointer to string appended with new ip and possibly
1752 * reallocated to new length
1755 static char *ipstr_list_add(char **ipstr_list
, const struct ip_service
*service
)
1757 char *new_ipstr
= NULL
;
1758 char addr_buf
[INET6_ADDRSTRLEN
];
1761 /* arguments checking */
1762 if (!ipstr_list
|| !service
) {
1766 print_sockaddr(addr_buf
,
1770 /* attempt to convert ip to a string and append colon separator to it */
1772 if (service
->ss
.ss_family
== AF_INET
) {
1774 ret
= asprintf(&new_ipstr
, "%s%s%s:%d", *ipstr_list
,
1775 IPSTR_LIST_SEP
, addr_buf
,
1779 ret
= asprintf(&new_ipstr
, "%s%s[%s]:%d", *ipstr_list
,
1780 IPSTR_LIST_SEP
, addr_buf
,
1783 SAFE_FREE(*ipstr_list
);
1785 if (service
->ss
.ss_family
== AF_INET
) {
1787 ret
= asprintf(&new_ipstr
, "%s:%d", addr_buf
,
1791 ret
= asprintf(&new_ipstr
, "[%s]:%d", addr_buf
,
1798 *ipstr_list
= new_ipstr
;
1803 * Allocate and initialise an ipstr list using ip adresses
1804 * passed as arguments.
1806 * @param ipstr_list pointer to string meant to be allocated and set
1807 * @param ip_list array of ip addresses to place in the list
1808 * @param ip_count number of addresses stored in ip_list
1809 * @return pointer to allocated ip string
1812 char *ipstr_list_make(char **ipstr_list
,
1813 const struct ip_service
*ip_list
,
1818 /* arguments checking */
1819 if (!ip_list
|| !ipstr_list
) {
1825 /* process ip addresses given as arguments */
1826 for (i
= 0; i
< ip_count
; i
++) {
1827 *ipstr_list
= ipstr_list_add(ipstr_list
, &ip_list
[i
]);
1830 return (*ipstr_list
);
1835 * Parse given ip string list into array of ip addresses
1836 * (as ip_service structures)
1837 * e.g. [IPv6]:port,192.168.1.100:389,192.168.1.78, ...
1839 * @param ipstr ip string list to be parsed
1840 * @param ip_list pointer to array of ip addresses which is
1841 * allocated by this function and must be freed by caller
1842 * @return number of successfully parsed addresses
1845 int ipstr_list_parse(const char *ipstr_list
, struct ip_service
**ip_list
)
1848 char *token_str
= NULL
;
1852 if (!ipstr_list
|| !ip_list
)
1855 count
= count_chars(ipstr_list
, IPSTR_LIST_CHAR
) + 1;
1856 if ( (*ip_list
= SMB_MALLOC_ARRAY(struct ip_service
, count
)) == NULL
) {
1857 DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n",
1858 (unsigned long)count
));
1862 frame
= talloc_stackframe();
1863 for ( i
=0; next_token_talloc(frame
, &ipstr_list
, &token_str
,
1864 IPSTR_LIST_SEP
) && i
<count
; i
++ ) {
1865 char *s
= token_str
;
1866 char *p
= strrchr(token_str
, ':');
1870 (*ip_list
)[i
].port
= atoi(p
+1);
1873 /* convert single token to ip address */
1874 if (token_str
[0] == '[') {
1877 p
= strchr(token_str
, ']');
1883 if (!interpret_string_addr(&(*ip_list
)[i
].ss
,
1894 * Safely free ip string list
1896 * @param ipstr_list ip string list to be freed
1899 void ipstr_list_free(char* ipstr_list
)
1901 SAFE_FREE(ipstr_list
);
1904 static const char b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1907 * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
1909 DATA_BLOB
base64_decode_data_blob(const char *s
)
1911 int bit_offset
, byte_offset
, idx
, i
, n
;
1912 DATA_BLOB decoded
= data_blob(s
, strlen(s
)+1);
1913 unsigned char *d
= decoded
.data
;
1918 while (*s
&& (p
=strchr_m(b64
,*s
))) {
1919 idx
= (int)(p
- b64
);
1920 byte_offset
= (i
*6)/8;
1921 bit_offset
= (i
*6)%8;
1922 d
[byte_offset
] &= ~((1<<(8-bit_offset
))-1);
1923 if (bit_offset
< 3) {
1924 d
[byte_offset
] |= (idx
<< (2-bit_offset
));
1927 d
[byte_offset
] |= (idx
>> (bit_offset
-2));
1928 d
[byte_offset
+1] = 0;
1929 d
[byte_offset
+1] |= (idx
<< (8-(bit_offset
-2))) & 0xFF;
1935 if ((n
> 0) && (*s
== '=')) {
1945 * Decode a base64 string in-place - wrapper for the above
1947 void base64_decode_inplace(char *s
)
1949 DATA_BLOB decoded
= base64_decode_data_blob(s
);
1951 if ( decoded
.length
!= 0 ) {
1952 memcpy(s
, decoded
.data
, decoded
.length
);
1954 /* null terminate */
1955 s
[decoded
.length
] = '\0';
1960 data_blob_free(&decoded
);
1964 * Encode a base64 string into a talloc()ed string caller to free.
1966 * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
1970 char *base64_encode_data_blob(TALLOC_CTX
*mem_ctx
, DATA_BLOB data
)
1974 size_t out_cnt
, len
, output_len
;
1977 if (!data
.length
|| !data
.data
)
1982 output_len
= data
.length
* 2 + 4; /* Account for closing bytes. 4 is
1983 * random but should be enough for
1985 result
= TALLOC_ARRAY(mem_ctx
, char, output_len
); /* get us plenty of space */
1986 SMB_ASSERT(result
!= NULL
);
1989 int c
= (unsigned char) *(data
.data
++);
1992 if (char_count
== 3) {
1993 result
[out_cnt
++] = b64
[bits
>> 18];
1994 result
[out_cnt
++] = b64
[(bits
>> 12) & 0x3f];
1995 result
[out_cnt
++] = b64
[(bits
>> 6) & 0x3f];
1996 result
[out_cnt
++] = b64
[bits
& 0x3f];
2003 if (char_count
!= 0) {
2004 bits
<<= 16 - (8 * char_count
);
2005 result
[out_cnt
++] = b64
[bits
>> 18];
2006 result
[out_cnt
++] = b64
[(bits
>> 12) & 0x3f];
2007 if (char_count
== 1) {
2008 result
[out_cnt
++] = '=';
2009 result
[out_cnt
++] = '=';
2011 result
[out_cnt
++] = b64
[(bits
>> 6) & 0x3f];
2012 result
[out_cnt
++] = '=';
2015 result
[out_cnt
] = '\0'; /* terminate */
2019 /* read a SMB_BIG_UINT from a string */
2020 uint64_t STR_TO_SMB_BIG_UINT(const char *nptr
, const char **entptr
)
2023 uint64_t val
= (uint64_t)-1;
2024 const char *p
= nptr
;
2033 while (*p
&& isspace(*p
))
2036 sscanf(p
,"%"PRIu64
,&val
);
2038 while (*p
&& isdigit(*p
))
2046 /* Convert a size specification to a count of bytes. We accept the following
2048 * bytes if there is no suffix
2053 * pP whatever the ISO name for petabytes is
2055 * Returns 0 if the string can't be converted.
2057 SMB_OFF_T
conv_str_size(const char * str
)
2059 SMB_OFF_T lval_orig
;
2063 if (str
== NULL
|| *str
== '\0') {
2067 #ifdef HAVE_STRTOULL
2068 if (sizeof(SMB_OFF_T
) == 8) {
2069 lval
= strtoull(str
, &end
, 10 /* base */);
2071 lval
= strtoul(str
, &end
, 10 /* base */);
2074 lval
= strtoul(str
, &end
, 10 /* base */);
2077 if (end
== NULL
|| end
== str
) {
2087 if (strwicmp(end
, "K") == 0) {
2088 lval
*= (SMB_OFF_T
)1024;
2089 } else if (strwicmp(end
, "M") == 0) {
2090 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024);
2091 } else if (strwicmp(end
, "G") == 0) {
2092 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2094 } else if (strwicmp(end
, "T") == 0) {
2095 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2096 (SMB_OFF_T
)1024 * (SMB_OFF_T
)1024);
2097 } else if (strwicmp(end
, "P") == 0) {
2098 lval
*= ((SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2099 (SMB_OFF_T
)1024 * (SMB_OFF_T
)1024 *
2106 * Primitive attempt to detect wrapping on platforms with
2107 * 4-byte SMB_OFF_T. It's better to let the caller handle a
2108 * failure than some random number.
2110 if (lval_orig
<= lval
) {
2117 void string_append(char **left
, const char *right
)
2119 int new_len
= strlen(right
) + 1;
2121 if (*left
== NULL
) {
2122 *left
= (char *)SMB_MALLOC(new_len
);
2123 if (*left
== NULL
) {
2128 new_len
+= strlen(*left
);
2129 *left
= (char *)SMB_REALLOC(*left
, new_len
);
2132 if (*left
== NULL
) {
2136 safe_strcat(*left
, right
, new_len
-1);
2139 bool add_string_to_array(TALLOC_CTX
*mem_ctx
,
2140 const char *str
, const char ***strings
,
2143 char *dup_str
= talloc_strdup(mem_ctx
, str
);
2145 *strings
= TALLOC_REALLOC_ARRAY(mem_ctx
, *strings
,
2146 const char *, (*num
)+1);
2148 if ((*strings
== NULL
) || (dup_str
== NULL
)) {
2153 (*strings
)[*num
] = dup_str
;
2158 /* Append an sprintf'ed string. Double buffer size on demand. Usable without
2159 * error checking in between. The indiation that something weird happened is
2162 void sprintf_append(TALLOC_CTX
*mem_ctx
, char **string
, ssize_t
*len
,
2163 size_t *bufsize
, const char *fmt
, ...)
2170 /* len<0 is an internal marker that something failed */
2174 if (*string
== NULL
) {
2178 *string
= TALLOC_ARRAY(mem_ctx
, char, *bufsize
);
2179 if (*string
== NULL
)
2184 ret
= vasprintf(&newstr
, fmt
, ap
);
2192 while ((*len
)+ret
>= *bufsize
) {
2195 if (*bufsize
>= (1024*1024*256))
2200 *string
= TALLOC_REALLOC_ARRAY(mem_ctx
, *string
, char,
2202 if (*string
== NULL
) {
2207 StrnCpy((*string
)+(*len
), newstr
, ret
);
2218 * asprintf into a string and strupper_m it after that.
2221 int asprintf_strupper_m(char **strp
, const char *fmt
, ...)
2228 ret
= vasprintf(&result
, fmt
, ap
);
2239 char *talloc_asprintf_strupper_m(TALLOC_CTX
*t
, const char *fmt
, ...)
2245 ret
= talloc_vasprintf(t
, fmt
, ap
);
2255 char *talloc_asprintf_strlower_m(TALLOC_CTX
*t
, const char *fmt
, ...)
2261 ret
= talloc_vasprintf(t
, fmt
, ap
);
2273 Returns the substring from src between the first occurrence of
2274 the char "front" and the first occurence of the char "back".
2275 Mallocs the return string which must be freed. Not for use
2276 with wide character strings.
2278 char *sstring_sub(const char *src
, char front
, char back
)
2280 char *temp1
, *temp2
, *temp3
;
2283 temp1
= strchr(src
, front
);
2284 if (temp1
== NULL
) return NULL
;
2285 temp2
= strchr(src
, back
);
2286 if (temp2
== NULL
) return NULL
;
2287 len
= temp2
- temp1
;
2288 if (len
<= 0) return NULL
;
2289 temp3
= (char*)SMB_MALLOC(len
);
2290 if (temp3
== NULL
) {
2291 DEBUG(1,("Malloc failure in sstring_sub\n"));
2294 memcpy(temp3
, temp1
+1, len
-1);
2295 temp3
[len
-1] = '\0';
2299 /********************************************************************
2300 Check a string for any occurrences of a specified list of invalid
2302 ********************************************************************/
2304 bool validate_net_name( const char *name
,
2305 const char *invalid_chars
,
2314 for ( i
=0; i
<max_len
&& name
[i
]; i
++ ) {
2315 /* fail if strchr_m() finds one of the invalid characters */
2316 if ( name
[i
] && strchr_m( invalid_chars
, name
[i
] ) ) {
2325 /*******************************************************************
2326 Add a shell escape character '\' to any character not in a known list
2327 of characters. UNIX charset format.
2328 *******************************************************************/
2330 #define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_/ \t.,"
2331 #define INSIDE_DQUOTE_LIST "$`\n\"\\"
2333 char *escape_shell_string(const char *src
)
2335 size_t srclen
= strlen(src
);
2336 char *ret
= SMB_MALLOC_ARRAY(char, (srclen
* 2) + 1);
2338 bool in_s_quote
= false;
2339 bool in_d_quote
= false;
2340 bool next_escaped
= false;
2348 codepoint_t c
= next_codepoint(src
, &c_size
);
2350 if (c
== INVALID_CODEPOINT
) {
2356 memcpy(dest
, src
, c_size
);
2359 next_escaped
= false;
2364 * Deal with backslash escaped state.
2365 * This only lasts for one character.
2370 next_escaped
= false;
2375 * Deal with single quote state. The
2376 * only thing we care about is exiting
2389 * Deal with double quote state. The most
2390 * complex state. We must cope with \, meaning
2391 * possibly escape next char (depending what it
2392 * is), ", meaning exit this state, and possibly
2393 * add an \ escape to any unprotected character
2394 * (listed in INSIDE_DQUOTE_LIST).
2400 * Next character might be escaped.
2401 * We have to peek. Inside double
2402 * quotes only INSIDE_DQUOTE_LIST
2403 * characters are escaped by a \.
2408 c
= next_codepoint(&src
[1], &c_size
);
2409 if (c
== INVALID_CODEPOINT
) {
2415 * Don't escape the next char.
2424 if (nextchar
&& strchr(INSIDE_DQUOTE_LIST
,
2426 next_escaped
= true;
2433 /* Exit double quote state. */
2440 * We know the character isn't \ or ",
2441 * so escape it if it's any of the other
2442 * possible unprotected characters.
2445 if (strchr(INSIDE_DQUOTE_LIST
, (int)*src
)) {
2453 * From here to the end of the loop we're
2454 * not in the single or double quote state.
2458 /* Next character must be escaped. */
2459 next_escaped
= true;
2465 /* Go into single quote state. */
2472 /* Go into double quote state. */
2478 /* Check if we need to escape the character. */
2480 if (!strchr(INCLUDE_LIST
, (int)*src
)) {
2489 /***************************************************
2490 str_list_make, v3 version. The v4 version does not
2491 look at quoted strings with embedded blanks, so
2492 do NOT merge this function please!
2493 ***************************************************/
2495 #define S_LIST_ABS 16 /* List Allocation Block Size */
2497 char **str_list_make_v3(TALLOC_CTX
*mem_ctx
, const char *string
,
2505 if (!string
|| !*string
)
2508 list
= TALLOC_ARRAY(mem_ctx
, char *, S_LIST_ABS
+1);
2514 s
= talloc_strdup(list
, string
);
2516 DEBUG(0,("str_list_make: Unable to allocate memory"));
2520 if (!sep
) sep
= LIST_SEP
;
2525 while (next_token_talloc(list
, &str
, &tok
, sep
)) {
2530 lsize
+= S_LIST_ABS
;
2532 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, list
, char *,
2535 DEBUG(0,("str_list_make: "
2536 "Unable to allocate memory"));
2543 memset (&list
[num
], 0,
2544 ((sizeof(char**)) * (S_LIST_ABS
+1)));
2557 char *sanitize_username(TALLOC_CTX
*mem_ctx
, const char *username
)
2561 alpha_strcpy(tmp
, username
, ". _-$", sizeof(tmp
));
2562 return talloc_strdup(mem_ctx
, tmp
);