source: vendor/3.6.0/source3/lib/util_unistr.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 15.1 KB
Line 
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
7
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.
12
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.
17
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/>.
20*/
21
22#include "includes.h"
23
24/* these 3 tables define the unicode case handling. They are loaded
25 at startup either via mmap() or read() from the lib directory */
26static uint8 *valid_table;
27static bool initialized;
28
29/**
30 * Destroy global objects allocated by load_case_tables()
31 **/
32void gfree_case_tables(void)
33{
34 if ( valid_table ) {
35 unmap_file(valid_table, 0x10000);
36 valid_table = NULL;
37 }
38 initialized = false;
39}
40
41/**
42 * Load the valid character map table from <tt>valid.dat</tt> or
43 * create from the configured codepage.
44 *
45 * This function is called whenever the configuration is reloaded.
46 * However, the valid character table is not changed if it's loaded
47 * from a file, because we can't unmap files.
48 **/
49
50static void init_valid_table(void)
51{
52 if (valid_table) {
53 return;
54 }
55
56 valid_table = (uint8 *)map_file(data_path("valid.dat"), 0x10000);
57 if (!valid_table) {
58 smb_panic("Could not load valid.dat file required for mangle method=hash");
59 return;
60 }
61}
62
63/*******************************************************************
64 Write a string in (little-endian) unicode format. src is in
65 the current DOS codepage. len is the length in bytes of the
66 string pointed to by dst.
67
68 if null_terminate is True then null terminate the packet (adds 2 bytes)
69
70 the return value is the length in bytes consumed by the string, including the
71 null termination if applied
72********************************************************************/
73
74size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate)
75{
76 int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
77 : STR_UNICODE|STR_NOALIGN;
78 return push_ucs2(NULL, dst, src, len, flags);
79}
80
81
82/* Converts a string from internal samba format to unicode
83 */
84
85int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags)
86{
87 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
88}
89
90/* Converts a string from internal samba format to unicode. Always terminates.
91 * Actually just a wrapper round push_ucs2_talloc().
92 */
93
94int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
95{
96 size_t size;
97 if (push_ucs2_talloc(ctx, dest, src, &size))
98 return size;
99 else
100 return -1;
101}
102
103/*******************************************************************
104 Determine if a character is valid in a 8.3 name.
105********************************************************************/
106
107bool isvalid83_w(smb_ucs2_t c)
108{
109 init_valid_table();
110 return valid_table[SVAL(&c,0)] != 0;
111}
112
113/*******************************************************************
114 Count the number of characters in a smb_ucs2_t string.
115********************************************************************/
116
117size_t strlen_w(const smb_ucs2_t *src)
118{
119 size_t len;
120 smb_ucs2_t c;
121
122 for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
123 ;
124 }
125
126 return len;
127}
128
129/*******************************************************************
130 Count up to max number of characters in a smb_ucs2_t string.
131********************************************************************/
132
133size_t strnlen_w(const smb_ucs2_t *src, size_t max)
134{
135 size_t len;
136 smb_ucs2_t c;
137
138 for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
139 ;
140 }
141
142 return len;
143}
144
145/*******************************************************************
146 Wide strchr().
147********************************************************************/
148
149smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
150{
151 smb_ucs2_t cp;
152 while (*(COPY_UCS2_CHAR(&cp,s))) {
153 if (c == cp) {
154 return (smb_ucs2_t *)s;
155 }
156 s++;
157 }
158 if (c == cp) {
159 return (smb_ucs2_t *)s;
160 }
161
162 return NULL;
163}
164
165smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
166{
167 return strchr_w(s, UCS2_CHAR(c));
168}
169
170/*******************************************************************
171 Wide strrchr().
172********************************************************************/
173
174smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
175{
176 smb_ucs2_t cp;
177 const smb_ucs2_t *p = s;
178 int len = strlen_w(s);
179
180 if (len == 0) {
181 return NULL;
182 }
183 p += (len - 1);
184 do {
185 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
186 return (smb_ucs2_t *)p;
187 }
188 } while (p-- != s);
189 return NULL;
190}
191
192/*******************************************************************
193 Wide version of strrchr that returns after doing strrchr 'n' times.
194********************************************************************/
195
196smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
197{
198 smb_ucs2_t cp;
199 const smb_ucs2_t *p = s;
200 int len = strlen_w(s);
201
202 if (len == 0 || !n) {
203 return NULL;
204 }
205 p += (len - 1);
206 do {
207 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
208 n--;
209 }
210
211 if (!n) {
212 return (smb_ucs2_t *)p;
213 }
214 } while (p-- != s);
215 return NULL;
216}
217
218/*******************************************************************
219 Wide strstr().
220********************************************************************/
221
222smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
223{
224 smb_ucs2_t *r;
225 size_t inslen;
226
227 if (!s || !*s || !ins || !*ins) {
228 return NULL;
229 }
230
231 inslen = strlen_w(ins);
232 r = (smb_ucs2_t *)s;
233
234 while ((r = strchr_w(r, *ins))) {
235 if (strncmp_w(r, ins, inslen) == 0) {
236 return r;
237 }
238 r++;
239 }
240
241 return NULL;
242}
243
244/*******************************************************************
245 Convert a string to lower case.
246 return True if any char is converted
247
248 This is unsafe for any string involving a UTF16 character
249********************************************************************/
250
251bool strlower_w(smb_ucs2_t *s)
252{
253 smb_ucs2_t cp;
254 bool ret = False;
255
256 while (*(COPY_UCS2_CHAR(&cp,s))) {
257 smb_ucs2_t v = tolower_w(cp);
258 if (v != cp) {
259 COPY_UCS2_CHAR(s,&v);
260 ret = True;
261 }
262 s++;
263 }
264 return ret;
265}
266
267/*******************************************************************
268 Convert a string to upper case.
269 return True if any char is converted
270
271 This is unsafe for any string involving a UTF16 character
272********************************************************************/
273
274bool strupper_w(smb_ucs2_t *s)
275{
276 smb_ucs2_t cp;
277 bool ret = False;
278 while (*(COPY_UCS2_CHAR(&cp,s))) {
279 smb_ucs2_t v = toupper_w(cp);
280 if (v != cp) {
281 COPY_UCS2_CHAR(s,&v);
282 ret = True;
283 }
284 s++;
285 }
286 return ret;
287}
288
289/*******************************************************************
290 Convert a string to "normal" form.
291********************************************************************/
292
293void strnorm_w(smb_ucs2_t *s, int case_default)
294{
295 if (case_default == CASE_UPPER) {
296 strupper_w(s);
297 } else {
298 strlower_w(s);
299 }
300}
301
302int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
303{
304 smb_ucs2_t cpa, cpb;
305
306 while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
307 a++;
308 b++;
309 }
310 return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
311 /* warning: if *a != *b and both are not 0 we return a random
312 greater or lesser than 0 number not realted to which
313 string is longer */
314}
315
316int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
317{
318 smb_ucs2_t cpa, cpb;
319 size_t n = 0;
320
321 while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
322 a++;
323 b++;
324 n++;
325 }
326 return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
327}
328
329/*******************************************************************
330 Case insensitive string comparison.
331********************************************************************/
332
333int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
334{
335 smb_ucs2_t cpa, cpb;
336
337 while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
338 a++;
339 b++;
340 }
341 return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
342}
343
344/*******************************************************************
345 Case insensitive string comparison, length limited.
346********************************************************************/
347
348int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
349{
350 smb_ucs2_t cpa, cpb;
351 size_t n = 0;
352
353 while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
354 a++;
355 b++;
356 n++;
357 }
358 return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
359}
360
361/*******************************************************************
362 Compare 2 strings.
363********************************************************************/
364
365bool strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
366{
367 if (s1 == s2) {
368 return(True);
369 }
370 if (!s1 || !s2) {
371 return(False);
372 }
373
374 return(strcasecmp_w(s1,s2)==0);
375}
376
377/*******************************************************************
378 Compare 2 strings up to and including the nth char.
379******************************************************************/
380
381bool strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
382{
383 if (s1 == s2) {
384 return(True);
385 }
386 if (!s1 || !s2 || !n) {
387 return(False);
388 }
389
390 return(strncasecmp_w(s1,s2,n)==0);
391}
392
393/*******************************************************************
394 Duplicate string.
395********************************************************************/
396
397smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
398{
399 return strndup_w(src, 0);
400}
401
402/* if len == 0 then duplicate the whole string */
403
404smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
405{
406 smb_ucs2_t *dest;
407
408 if (!len) {
409 len = strlen_w(src);
410 }
411 dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
412 if (!dest) {
413 DEBUG(0,("strdup_w: out of memory!\n"));
414 return NULL;
415 }
416
417 memcpy(dest, src, len * sizeof(smb_ucs2_t));
418 dest[len] = 0;
419 return dest;
420}
421
422/*******************************************************************
423 Copy a string with max len.
424********************************************************************/
425
426smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
427{
428 smb_ucs2_t cp;
429 size_t len;
430
431 if (!dest || !src) {
432 return NULL;
433 }
434
435 for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
436 cp = *COPY_UCS2_CHAR(dest+len,src+len);
437 }
438 cp = 0;
439 for ( /*nothing*/ ; len < max; len++ ) {
440 cp = *COPY_UCS2_CHAR(dest+len,&cp);
441 }
442
443 return dest;
444}
445
446/*******************************************************************
447 Append a string of len bytes and add a terminator.
448********************************************************************/
449
450smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
451{
452 size_t start;
453 size_t len;
454 smb_ucs2_t z = 0;
455
456 if (!dest || !src) {
457 return NULL;
458 }
459
460 start = strlen_w(dest);
461 len = strnlen_w(src, max);
462
463 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
464 z = *COPY_UCS2_CHAR(dest+start+len,&z);
465
466 return dest;
467}
468
469smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
470{
471 size_t start;
472 size_t len;
473 smb_ucs2_t z = 0;
474
475 if (!dest || !src) {
476 return NULL;
477 }
478
479 start = strlen_w(dest);
480 len = strlen_w(src);
481
482 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
483 z = *COPY_UCS2_CHAR(dest+start+len,&z);
484
485 return dest;
486}
487
488
489/*******************************************************************
490 Replace any occurence of oldc with newc in unicode string.
491********************************************************************/
492
493void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
494{
495 smb_ucs2_t cp;
496
497 for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
498 if(cp==oldc) {
499 COPY_UCS2_CHAR(s,&newc);
500 }
501 }
502}
503
504/*******************************************************************
505 Trim unicode string.
506********************************************************************/
507
508bool trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
509 const smb_ucs2_t *back)
510{
511 bool ret = False;
512 size_t len, front_len, back_len;
513
514 if (!s) {
515 return False;
516 }
517
518 len = strlen_w(s);
519
520 if (front && *front) {
521 front_len = strlen_w(front);
522 while (len && strncmp_w(s, front, front_len) == 0) {
523 memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
524 len -= front_len;
525 ret = True;
526 }
527 }
528
529 if (back && *back) {
530 back_len = strlen_w(back);
531 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
532 s[len - back_len] = 0;
533 len -= back_len;
534 ret = True;
535 }
536 }
537
538 return ret;
539}
540
541/*
542 The *_wa() functions take a combination of 7 bit ascii
543 and wide characters They are used so that you can use string
544 functions combining C string constants with ucs2 strings
545
546 The char* arguments must NOT be multibyte - to be completely sure
547 of this only pass string constants */
548
549int strcmp_wa(const smb_ucs2_t *a, const char *b)
550{
551 smb_ucs2_t cp = 0;
552
553 while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
554 a++;
555 b++;
556 }
557 return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
558}
559
560int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
561{
562 smb_ucs2_t cp = 0;
563 size_t n = 0;
564
565 while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
566 a++;
567 b++;
568 n++;
569 }
570 return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
571}
572
573smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
574{
575 smb_ucs2_t cp;
576
577 while (*(COPY_UCS2_CHAR(&cp,s))) {
578 int i;
579 for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++)
580 ;
581 if (p[i]) {
582 return (smb_ucs2_t *)s;
583 }
584 s++;
585 }
586 return NULL;
587}
588
589smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
590{
591 smb_ucs2_t *r;
592 size_t inslen;
593
594 if (!s || !ins) {
595 return NULL;
596 }
597
598 inslen = strlen(ins);
599 r = (smb_ucs2_t *)s;
600
601 while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
602 if (strncmp_wa(r, ins, inslen) == 0)
603 return r;
604 r++;
605 }
606
607 return NULL;
608}
609
610smb_ucs2_t toupper_w(smb_ucs2_t v)
611{
612 smb_ucs2_t ret;
613 /* LE to native. */
614 codepoint_t cp = SVAL(&v,0);
615 cp = toupper_m(cp);
616 /* native to LE. */
617 SSVAL(&ret,0,cp);
618 return ret;
619}
620
621bool isupper_w(smb_ucs2_t v)
622{
623 codepoint_t cp = SVAL(&v,0);
624 return isupper_m(cp);
625}
626
627smb_ucs2_t tolower_w(smb_ucs2_t v)
628{
629 smb_ucs2_t ret;
630 /* LE to native. */
631 codepoint_t cp = SVAL(&v,0);
632 cp = tolower_m(cp);
633 /* native to LE. */
634 SSVAL(&ret,0,cp);
635 return ret;
636}
637
638bool islower_w(smb_ucs2_t v)
639{
640 codepoint_t cp = SVAL(&v,0);
641 return islower_m(cp);
642}
Note: See TracBrowser for help on using the repository browser.