s3: Fix some nonempty line endings
[Samba/gebeck_regimport.git] / lib / util / charset / util_unistr_w.c
blob52252676bb219a8f7e2fc91f1b475dac0f510893
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 "includes.h"
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)
40 size_t len;
41 smb_ucs2_t c;
43 for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
47 return 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)
56 size_t len;
57 smb_ucs2_t c;
59 for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
63 return len;
66 /*******************************************************************
67 Wide strchr().
68 ********************************************************************/
70 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
72 smb_ucs2_t cp;
73 while (*(COPY_UCS2_CHAR(&cp,s))) {
74 if (c == cp) {
75 return discard_const_p(smb_ucs2_t, s);
77 s++;
79 if (c == cp) {
80 return discard_const_p(smb_ucs2_t, s);
83 return NULL;
86 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
88 return strchr_w(s, UCS2_CHAR(c));
91 /*******************************************************************
92 Wide strrchr().
93 ********************************************************************/
95 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
97 smb_ucs2_t cp;
98 const smb_ucs2_t *p = s;
99 int len = strlen_w(s);
101 if (len == 0) {
102 return NULL;
104 p += (len - 1);
105 do {
106 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
107 return discard_const_p(smb_ucs2_t, p);
109 } while (p-- != s);
110 return NULL;
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)
119 smb_ucs2_t cp;
120 const smb_ucs2_t *p = s;
121 int len = strlen_w(s);
123 if (len == 0 || !n) {
124 return NULL;
126 p += (len - 1);
127 do {
128 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
129 n--;
132 if (!n) {
133 return discard_const_p(smb_ucs2_t, p);
135 } while (p-- != s);
136 return NULL;
139 /*******************************************************************
140 Wide strstr().
141 ********************************************************************/
143 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
145 const smb_ucs2_t *r;
146 size_t inslen;
148 if (!s || !*s || !ins || !*ins) {
149 return NULL;
152 inslen = strlen_w(ins);
153 r = s;
155 while ((r = strchr_w(r, *ins))) {
156 if (strncmp_w(r, ins, inslen) == 0) {
157 return discard_const_p(smb_ucs2_t, r);
159 r++;
162 return NULL;
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)
174 smb_ucs2_t cp;
175 bool ret = false;
177 while (*(COPY_UCS2_CHAR(&cp,s))) {
178 smb_ucs2_t v = tolower_m(cp);
179 if (v != cp) {
180 COPY_UCS2_CHAR(s,&v);
181 ret = true;
183 s++;
185 return ret;
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)
197 smb_ucs2_t cp;
198 bool ret = false;
199 while (*(COPY_UCS2_CHAR(&cp,s))) {
200 smb_ucs2_t v = toupper_m(cp);
201 if (v != cp) {
202 COPY_UCS2_CHAR(s,&v);
203 ret = true;
205 s++;
207 return ret;
210 static int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
212 smb_ucs2_t cpa, cpb;
213 size_t n = 0;
215 while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
216 a++;
217 b++;
218 n++;
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)
233 smb_ucs2_t cp = 0;
235 while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
236 a++;
237 b++;
239 return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
242 smb_ucs2_t toupper_w(smb_ucs2_t v)
244 smb_ucs2_t ret;
245 /* LE to native. */
246 codepoint_t cp = SVAL(&v,0);
247 cp = toupper_m(cp);
248 /* native to LE. */
249 SSVAL(&ret,0,cp);
250 return ret;