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) Jeremy Allison 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "lib/util/byteorder.h"
25 #include "lib/util/debug.h"
26 #include "lib/util/fault.h"
28 /* Copy into a smb_ucs2_t from a possibly unaligned buffer. Return the copied smb_ucs2_t */
29 #define COPY_UCS2_CHAR(dest,src) (((unsigned char *)(dest))[0] = ((const unsigned char *)(src))[0],\
30 ((unsigned char *)(dest))[1] = ((const unsigned char *)(src))[1], (dest))
33 /* return an ascii version of a ucs2 character */
34 #define UCS2_TO_CHAR(c) (((c) >> UCS2_SHIFT) & 0xff)
36 static int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
);
38 /*******************************************************************
39 Count the number of two-byte pairs in a UTF16 string.
40 ********************************************************************/
42 size_t strlen_w(const smb_ucs2_t
*src
)
47 for(len
= 0; *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
54 /*******************************************************************
55 Count up to max number of characters in a smb_ucs2_t string.
56 ********************************************************************/
58 size_t strnlen_w(const smb_ucs2_t
*src
, size_t max
)
63 for(len
= 0; (len
< max
) && *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
70 /*******************************************************************
72 ********************************************************************/
74 smb_ucs2_t
*strchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
77 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
79 return discard_const_p(smb_ucs2_t
, s
);
84 return discard_const_p(smb_ucs2_t
, s
);
90 smb_ucs2_t
*strchr_wa(const smb_ucs2_t
*s
, char c
)
92 return strchr_w(s
, UCS2_CHAR(c
));
95 /*******************************************************************
97 ********************************************************************/
99 smb_ucs2_t
*strrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
102 const smb_ucs2_t
*p
= s
;
103 int len
= strlen_w(s
);
110 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
111 return discard_const_p(smb_ucs2_t
, p
);
117 /*******************************************************************
119 ********************************************************************/
121 smb_ucs2_t
*strstr_w(const smb_ucs2_t
*s
, const smb_ucs2_t
*ins
)
126 if (!s
|| !*s
|| !ins
|| !*ins
) {
130 inslen
= strlen_w(ins
);
133 while ((r
= strchr_w(r
, *ins
))) {
134 if (strncmp_w(r
, ins
, inslen
) == 0) {
135 return discard_const_p(smb_ucs2_t
, r
);
143 /*******************************************************************
144 Convert a string to lower case.
145 return True if any char is converted
147 This is unsafe for any string involving a UTF16 character
148 ********************************************************************/
150 bool strlower_w(smb_ucs2_t
*s
)
155 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
156 smb_ucs2_t v
= tolower_m(cp
);
158 (void)COPY_UCS2_CHAR(s
,&v
);
166 /*******************************************************************
167 Convert a string to upper case.
168 return True if any char is converted
170 This is unsafe for any string involving a UTF16 character
171 ********************************************************************/
173 bool strupper_w(smb_ucs2_t
*s
)
177 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
178 smb_ucs2_t v
= toupper_m(cp
);
180 (void)COPY_UCS2_CHAR(s
,&v
);
188 static int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
193 while ((n
< len
) && (*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
198 return (len
- n
)?(*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
))):0;
202 The *_wa() functions take a combination of 7 bit ascii
203 and wide characters They are used so that you can use string
204 functions combining C string constants with ucs2 strings
206 The char* arguments must NOT be multibyte - to be completely sure
207 of this only pass string constants */
209 int strcmp_wa(const smb_ucs2_t
*a
, const char *b
)
213 while (*b
&& *(COPY_UCS2_CHAR(&cp
,a
)) == UCS2_CHAR(*b
)) {
217 return (*(COPY_UCS2_CHAR(&cp
,a
)) - UCS2_CHAR(*b
));
220 smb_ucs2_t
toupper_w(smb_ucs2_t v
)
224 codepoint_t cp
= SVAL(&v
,0);