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 /* Copy into a smb_ucs2_t from a possibly unaligned buffer. Return the copied smb_ucs2_t */
25 #define COPY_UCS2_CHAR(dest,src) (((unsigned char *)(dest))[0] = ((const unsigned char *)(src))[0],\
26 ((unsigned char *)(dest))[1] = ((const unsigned char *)(src))[1], (dest))
29 /* return an ascii version of a ucs2 character */
30 #define UCS2_TO_CHAR(c) (((c) >> UCS2_SHIFT) & 0xff)
32 static int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
);
34 /*******************************************************************
35 Count the number of two-byte pairs in a UTF16 string.
36 ********************************************************************/
38 size_t strlen_w(const smb_ucs2_t
*src
)
43 for(len
= 0; *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
50 /*******************************************************************
51 Count up to max number of characters in a smb_ucs2_t string.
52 ********************************************************************/
54 size_t strnlen_w(const smb_ucs2_t
*src
, size_t max
)
59 for(len
= 0; (len
< max
) && *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
66 /*******************************************************************
68 ********************************************************************/
70 smb_ucs2_t
*strchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
73 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
75 return discard_const_p(smb_ucs2_t
, s
);
80 return discard_const_p(smb_ucs2_t
, s
);
86 smb_ucs2_t
*strchr_wa(const smb_ucs2_t
*s
, char c
)
88 return strchr_w(s
, UCS2_CHAR(c
));
91 /*******************************************************************
93 ********************************************************************/
95 smb_ucs2_t
*strrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
98 const smb_ucs2_t
*p
= s
;
99 int len
= strlen_w(s
);
106 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
107 return discard_const_p(smb_ucs2_t
, p
);
113 /*******************************************************************
114 Wide version of strrchr that returns after doing strrchr 'n' times.
115 ********************************************************************/
117 smb_ucs2_t
*strnrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
, unsigned int n
)
120 const smb_ucs2_t
*p
= s
;
121 int len
= strlen_w(s
);
123 if (len
== 0 || !n
) {
128 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
133 return discard_const_p(smb_ucs2_t
, p
);
139 /*******************************************************************
141 ********************************************************************/
143 smb_ucs2_t
*strstr_w(const smb_ucs2_t
*s
, const smb_ucs2_t
*ins
)
148 if (!s
|| !*s
|| !ins
|| !*ins
) {
152 inslen
= strlen_w(ins
);
155 while ((r
= strchr_w(r
, *ins
))) {
156 if (strncmp_w(r
, ins
, inslen
) == 0) {
157 return discard_const_p(smb_ucs2_t
, r
);
165 /*******************************************************************
166 Convert a string to lower case.
167 return True if any char is converted
169 This is unsafe for any string involving a UTF16 character
170 ********************************************************************/
172 bool strlower_w(smb_ucs2_t
*s
)
177 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
178 smb_ucs2_t v
= tolower_m(cp
);
180 COPY_UCS2_CHAR(s
,&v
);
188 /*******************************************************************
189 Convert a string to upper case.
190 return True if any char is converted
192 This is unsafe for any string involving a UTF16 character
193 ********************************************************************/
195 bool strupper_w(smb_ucs2_t
*s
)
199 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
200 smb_ucs2_t v
= toupper_m(cp
);
202 COPY_UCS2_CHAR(s
,&v
);
210 static int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
215 while ((n
< len
) && (*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
220 return (len
- n
)?(*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
))):0;
224 The *_wa() functions take a combination of 7 bit ascii
225 and wide characters They are used so that you can use string
226 functions combining C string constants with ucs2 strings
228 The char* arguments must NOT be multibyte - to be completely sure
229 of this only pass string constants */
231 int strcmp_wa(const smb_ucs2_t
*a
, const char *b
)
235 while (*b
&& *(COPY_UCS2_CHAR(&cp
,a
)) == UCS2_CHAR(*b
)) {
239 return (*(COPY_UCS2_CHAR(&cp
,a
)) - UCS2_CHAR(*b
));
242 smb_ucs2_t
toupper_w(smb_ucs2_t v
)
246 codepoint_t cp
= SVAL(&v
,0);