2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) Simo Sorce 2001
6 Copyright (C) Andrew Bartlett 2011
7 Copyright (C) Jeremy Allison 1992-2007
8 Copyright (C) Martin Pool 2003
9 Copyright (C) James Peach 2006
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/>.
26 #include "system/locale.h"
33 Case insensitive string compararison, handle specified for testing
35 _PUBLIC_
int strcasecmp_m_handle(struct smb_iconv_handle
*iconv_handle
,
36 const char *s1
, const char *s2
)
38 codepoint_t c1
=0, c2
=0;
41 /* handle null ptr comparisons to simplify the use in qsort */
42 if (s1
== s2
) return 0;
43 if (s1
== NULL
) return -1;
44 if (s2
== NULL
) return 1;
47 c1
= next_codepoint_handle(iconv_handle
, s1
, &size1
);
48 c2
= next_codepoint_handle(iconv_handle
, s2
, &size2
);
57 if (c1
== INVALID_CODEPOINT
||
58 c2
== INVALID_CODEPOINT
) {
59 /* what else can we do?? */
60 return strcasecmp(s1
, s2
);
63 if (toupper_m(c1
) != toupper_m(c2
)) {
72 Case insensitive string compararison
74 _PUBLIC_
int strcasecmp_m(const char *s1
, const char *s2
)
76 struct smb_iconv_handle
*iconv_handle
= get_iconv_handle();
77 return strcasecmp_m_handle(iconv_handle
, s1
, s2
);
81 Case insensitive string compararison, length limited, handle specified for testing
83 _PUBLIC_
int strncasecmp_m_handle(struct smb_iconv_handle
*iconv_handle
,
84 const char *s1
, const char *s2
, size_t n
)
86 codepoint_t c1
=0, c2
=0;
89 /* handle null ptr comparisons to simplify the use in qsort */
90 if (s1
== s2
) return 0;
91 if (s1
== NULL
) return -1;
92 if (s2
== NULL
) return 1;
94 while (*s1
&& *s2
&& n
) {
97 c1
= next_codepoint_handle(iconv_handle
, s1
, &size1
);
98 c2
= next_codepoint_handle(iconv_handle
, s2
, &size2
);
107 if (c1
== INVALID_CODEPOINT
||
108 c2
== INVALID_CODEPOINT
) {
109 /* what else can we do?? */
110 return strcasecmp(s1
, s2
);
113 if (toupper_m(c1
) != toupper_m(c2
)) {
126 Case insensitive string compararison, length limited
128 _PUBLIC_
int strncasecmp_m(const char *s1
, const char *s2
, size_t n
)
130 struct smb_iconv_handle
*iconv_handle
= get_iconv_handle();
131 return strncasecmp_m_handle(iconv_handle
, s1
, s2
, n
);
137 * @note The comparison is case-insensitive.
139 _PUBLIC_
bool strequal_m(const char *s1
, const char *s2
)
141 return strcasecmp_m(s1
,s2
) == 0;
145 Compare 2 strings (case sensitive).
147 _PUBLIC_
bool strcsequal(const char *s1
,const char *s2
)
154 return strcmp(s1
,s2
) == 0;
158 * Calculate the number of units (8 or 16-bit, depending on the
159 * destination charset), that would be needed to convert the input
160 * string which is expected to be in in src_charset encoding to the
161 * destination charset (which should be a unicode charset).
163 _PUBLIC_
size_t strlen_m_ext_handle(struct smb_iconv_handle
*ic
,
164 const char *s
, charset_t src_charset
, charset_t dst_charset
)
169 switch (dst_charset
) {
172 smb_panic("cannot call strlen_m_ext() with a variable dest charset (must be UTF16* or UTF8)");
177 switch (src_charset
) {
180 smb_panic("cannot call strlen_m_ext() with a UTF16 src charset (must be DOS, UNIX, DISPLAY or UTF8)");
189 while (*s
&& !(((uint8_t)*s
) & 0x80)) {
200 codepoint_t c
= next_codepoint_handle_ext(ic
, s
, src_charset
, &c_size
);
203 switch (dst_charset
) {
208 /* Unicode char fits into 16 bits. */
211 /* Double-width unicode char - 32 bits. */
217 * this only checks ranges, and does not
218 * check for invalid codepoints
222 } else if (c
< 0x800) {
224 } else if (c
< 0x10000) {
232 * non-unicode encoding:
233 * assume that each codepoint fits into
234 * one unit in the destination encoding.
244 * Calculate the number of units (8 or 16-bit, depending on the
245 * destination charset), that would be needed to convert the input
246 * string which is expected to be in in src_charset encoding to the
247 * destination charset (which should be a unicode charset).
249 _PUBLIC_
size_t strlen_m_ext(const char *s
, charset_t src_charset
, charset_t dst_charset
)
251 struct smb_iconv_handle
*ic
= get_iconv_handle();
252 return strlen_m_ext_handle(ic
, s
, src_charset
, dst_charset
);
255 _PUBLIC_
size_t strlen_m_ext_term(const char *s
, const charset_t src_charset
,
256 const charset_t dst_charset
)
261 return strlen_m_ext(s
, src_charset
, dst_charset
) + 1;
265 * Calculate the number of 16-bit units that would be needed to convert
266 * the input string which is expected to be in CH_UNIX encoding to UTF16.
268 * This will be the same as the number of bytes in a string for single
269 * byte strings, but will be different for multibyte.
271 _PUBLIC_
size_t strlen_m(const char *s
)
273 return strlen_m_ext(s
, CH_UNIX
, CH_UTF16LE
);
277 Work out the number of multibyte chars in a string, including the NULL
280 _PUBLIC_
size_t strlen_m_term(const char *s
)
286 return strlen_m(s
) + 1;
290 * Weird helper routine for the winreg pipe: If nothing is around, return 0,
291 * if a string is there, include the terminator.
294 _PUBLIC_
size_t strlen_m_term_null(const char *s
)
309 Strchr and strrchr_m are a bit complex on general multi-byte strings.
311 _PUBLIC_
char *strchr_m(const char *src
, char c
)
314 struct smb_iconv_handle
*ic
= get_iconv_handle();
318 /* characters below 0x3F are guaranteed to not appear in
319 non-initial position in multi-byte charsets */
320 if ((c
& 0xC0) == 0) {
321 return strchr(src
, c
);
324 /* this is quite a common operation, so we want it to be
325 fast. We optimise for the ascii case, knowing that all our
326 supported multi-byte character sets are ascii-compatible
327 (ie. they match for the first 128 chars) */
329 for (s
= src
; *s
&& !(((unsigned char)s
[0]) & 0x80); s
++) {
331 return discard_const_p(char, s
);
337 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
338 /* With compose characters we must restart from the beginning. JRA. */
344 codepoint_t c2
= next_codepoint_handle(ic
, s
, &size
);
346 return discard_const_p(char, s
);
355 * Multibyte-character version of strrchr
357 _PUBLIC_
char *strrchr_m(const char *s
, char c
)
359 struct smb_iconv_handle
*ic
= get_iconv_handle();
366 /* characters below 0x3F are guaranteed to not appear in
367 non-initial position in multi-byte charsets */
368 if ((c
& 0xC0) == 0) {
369 return strrchr(s
, c
);
372 /* this is quite a common operation, so we want it to be
373 fast. We optimise for the ascii case, knowing that all our
374 supported multi-byte character sets are ascii-compatible
375 (ie. they match for the first 128 chars). Also, in Samba
376 we only search for ascii characters in 'c' and that
377 in all mb character sets with a compound character
378 containing c, if 'c' is not a match at position
379 p, then p[-1] > 0x7f. JRA. */
382 size_t len
= strlen(s
);
391 /* Could be a match. Part of a multibyte ? */
393 (((unsigned char)cp
[-1]) & 0x80)) {
394 /* Yep - go slow :-( */
398 /* No - we have a match ! */
399 return discard_const_p(char , cp
);
408 codepoint_t c2
= next_codepoint_handle(ic
, s
, &size
);
410 ret
= discard_const_p(char, s
);
419 return True if any (multi-byte) character is lower case
421 _PUBLIC_
bool strhaslower_handle(struct smb_iconv_handle
*ic
,
429 s
= next_codepoint_handle(ic
, string
, &c_size
);
435 return true; /* that means it has lower case chars */
442 _PUBLIC_
bool strhaslower(const char *string
)
444 struct smb_iconv_handle
*ic
= get_iconv_handle();
445 return strhaslower_handle(ic
, string
);
449 return True if any (multi-byte) character is upper case
451 _PUBLIC_
bool strhasupper_handle(struct smb_iconv_handle
*ic
,
459 s
= next_codepoint_handle(ic
, string
, &c_size
);
465 return true; /* that means it has upper case chars */
472 _PUBLIC_
bool strhasupper(const char *string
)
474 struct smb_iconv_handle
*ic
= get_iconv_handle();
475 return strhasupper_handle(ic
, string
);
478 /***********************************************************************
479 strstr_m - We convert via ucs2 for now.
480 ***********************************************************************/
482 char *strstr_m(const char *src
, const char *findstr
)
485 smb_ucs2_t
*src_w
, *find_w
;
489 size_t converted_size
, findstr_len
= 0;
491 TALLOC_CTX
*frame
; /* Only set up in the iconv case */
493 /* for correctness */
495 return discard_const_p(char, src
);
498 /* Samba does single character findstr calls a *lot*. */
499 if (findstr
[1] == '\0')
500 return strchr_m(src
, *findstr
);
502 /* We optimise for the ascii case, knowing that all our
503 supported multi-byte character sets are ascii-compatible
504 (ie. they match for the first 128 chars) */
506 for (s
= src
; *s
&& !(((unsigned char)s
[0]) & 0x80); s
++) {
507 if (*s
== *findstr
) {
509 findstr_len
= strlen(findstr
);
511 if (strncmp(s
, findstr
, findstr_len
) == 0) {
512 return discard_const_p(char, s
);
520 #if 1 /* def BROKEN_UNICODE_COMPOSE_CHARACTERS */
521 /* 'make check' fails unless we do this */
523 /* With compose characters we must restart from the beginning. JRA. */
527 frame
= talloc_stackframe();
529 if (!push_ucs2_talloc(frame
, &src_w
, src
, &converted_size
)) {
530 DEBUG(0,("strstr_m: src malloc fail\n"));
535 if (!push_ucs2_talloc(frame
, &find_w
, findstr
, &converted_size
)) {
536 DEBUG(0,("strstr_m: find malloc fail\n"));
541 p
= strstr_w(src_w
, find_w
);
549 if (!pull_ucs2_talloc(frame
, &s2
, src_w
, &converted_size
)) {
551 DEBUG(0,("strstr_m: dest malloc fail\n"));
554 retp
= discard_const_p(char, (s
+strlen(s2
)));