tdb: version 1.4.12
[samba4-gss.git] / lib / util / charset / util_unistr_w.c
blobe49b796bac4a4dac61cbb9ffd7c679d485644e4c
1 /*
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/>.
22 #include "replace.h"
23 #include "charset.h"
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)
44 size_t len;
45 smb_ucs2_t c;
47 for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
51 return 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)
60 size_t len;
61 smb_ucs2_t c;
63 for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
67 return len;
70 /*******************************************************************
71 Wide strchr().
72 ********************************************************************/
74 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
76 smb_ucs2_t cp;
77 while (*(COPY_UCS2_CHAR(&cp,s))) {
78 if (c == cp) {
79 return discard_const_p(smb_ucs2_t, s);
81 s++;
83 if (c == cp) {
84 return discard_const_p(smb_ucs2_t, s);
87 return NULL;
90 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
92 return strchr_w(s, UCS2_CHAR(c));
95 /*******************************************************************
96 Wide strrchr().
97 ********************************************************************/
99 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
101 smb_ucs2_t cp;
102 const smb_ucs2_t *p = s;
103 int len = strlen_w(s);
105 if (len == 0) {
106 return NULL;
108 p += (len - 1);
109 do {
110 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
111 return discard_const_p(smb_ucs2_t, p);
113 } while (p-- != s);
114 return NULL;
117 /*******************************************************************
118 Wide strstr().
119 ********************************************************************/
121 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
123 const smb_ucs2_t *r;
124 size_t inslen;
126 if (!s || !*s || !ins || !*ins) {
127 return NULL;
130 inslen = strlen_w(ins);
131 r = s;
133 while ((r = strchr_w(r, *ins))) {
134 if (strncmp_w(r, ins, inslen) == 0) {
135 return discard_const_p(smb_ucs2_t, r);
137 r++;
140 return NULL;
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)
152 smb_ucs2_t cp;
153 bool ret = false;
155 while (*(COPY_UCS2_CHAR(&cp,s))) {
156 smb_ucs2_t v = tolower_m(cp);
157 if (v != cp) {
158 (void)COPY_UCS2_CHAR(s,&v);
159 ret = true;
161 s++;
163 return ret;
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)
175 smb_ucs2_t cp;
176 bool ret = false;
177 while (*(COPY_UCS2_CHAR(&cp,s))) {
178 smb_ucs2_t v = toupper_m(cp);
179 if (v != cp) {
180 (void)COPY_UCS2_CHAR(s,&v);
181 ret = true;
183 s++;
185 return ret;
188 static int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
190 smb_ucs2_t cpa, cpb;
191 size_t n = 0;
193 while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
194 a++;
195 b++;
196 n++;
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)
211 smb_ucs2_t cp = 0;
213 while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
214 a++;
215 b++;
217 return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
220 smb_ucs2_t toupper_w(smb_ucs2_t v)
222 smb_ucs2_t ret;
223 /* LE to native. */
224 codepoint_t cp = SVAL(&v,0);
225 cp = toupper_m(cp);
226 /* native to LE. */
227 SSVAL(&ret,0,cp);
228 return ret;