source: branches/samba-3.0/source/lib/util_unistr.c

Last change on this file was 366, checked in by Herwig Bauernfeind, 16 years ago

Better fix for hardcoded \lib directory in 3.0.x

File size: 26.7 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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include "includes.h"
24
25#ifndef MAXUNI
26#define MAXUNI 1024
27#endif
28
29/* these 3 tables define the unicode case handling. They are loaded
30 at startup either via mmap() or read() from the lib directory */
31static smb_ucs2_t *upcase_table;
32static smb_ucs2_t *lowcase_table;
33static uint8 *valid_table;
34static BOOL upcase_table_use_unmap;
35static BOOL lowcase_table_use_unmap;
36static BOOL valid_table_use_unmap;
37
38/**
39 * This table says which Unicode characters are valid dos
40 * characters.
41 *
42 * Each value is just a single bit.
43 **/
44static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */
45
46/**
47 * Destroy global objects allocated by load_case_tables()
48 **/
49void gfree_case_tables(void)
50{
51 if ( upcase_table ) {
52 if ( upcase_table_use_unmap )
53 unmap_file(upcase_table, 0x20000);
54 else
55 SAFE_FREE(upcase_table);
56 }
57
58 if ( lowcase_table ) {
59 if ( lowcase_table_use_unmap )
60 unmap_file(lowcase_table, 0x20000);
61 else
62 SAFE_FREE(lowcase_table);
63 }
64
65 if ( valid_table ) {
66 if ( valid_table_use_unmap )
67 unmap_file(valid_table, 0x10000);
68 else
69 SAFE_FREE(valid_table);
70 }
71}
72
73/**
74 * Load or generate the case handling tables.
75 *
76 * The case tables are defined in UCS2 and don't depend on any
77 * configured parameters, so they never need to be reloaded.
78 **/
79
80void load_case_tables(void)
81{
82 static int initialised;
83 char *old_locale = NULL, *saved_locale = NULL;
84 int i;
85
86 if (initialised) {
87 return;
88 }
89 initialised = 1;
90
91 upcase_table = (smb_ucs2_t *)map_file(lib_path("upcase.dat"),
92 0x20000);
93 upcase_table_use_unmap = ( upcase_table != NULL );
94
95 lowcase_table = (smb_ucs2_t *)map_file(lib_path("lowcase.dat"),
96 0x20000);
97 lowcase_table_use_unmap = ( lowcase_table != NULL );
98
99
100#ifdef HAVE_SETLOCALE
101 /* Get the name of the current locale. */
102 old_locale = setlocale(LC_ALL, NULL);
103
104 if (old_locale) {
105 /* Save it as it is in static storage. */
106 saved_locale = SMB_STRDUP(old_locale);
107 }
108
109 /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */
110 setlocale(LC_ALL, "C");
111#endif
112
113 /* we would like Samba to limp along even if these tables are
114 not available */
115 if (!upcase_table) {
116#ifdef __OS2__
117 DEBUG(1,("could not load %s",lib_path("upcase.dat")));
118#endif
119 DEBUG(1,("creating lame upcase table\n"));
120
121 upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
122 for (i=0;i<0x10000;i++) {
123 smb_ucs2_t v;
124 SSVAL(&v, 0, i);
125 upcase_table[v] = i;
126 }
127 for (i=0;i<256;i++) {
128 smb_ucs2_t v;
129 SSVAL(&v, 0, UCS2_CHAR(i));
130 upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
131 }
132 }
133
134 if (!lowcase_table) {
135#ifdef __OS2__
136 DEBUG(1,("could not load %s\n",lib_path("lowcase.dat")));
137#endif
138 DEBUG(1,("creating lame lowcase table\n"));
139
140 lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
141 for (i=0;i<0x10000;i++) {
142 smb_ucs2_t v;
143 SSVAL(&v, 0, i);
144 lowcase_table[v] = i;
145 }
146 for (i=0;i<256;i++) {
147 smb_ucs2_t v;
148 SSVAL(&v, 0, UCS2_CHAR(i));
149 lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
150 }
151 }
152
153#ifdef HAVE_SETLOCALE
154 /* Restore the old locale. */
155 if (saved_locale) {
156 setlocale (LC_ALL, saved_locale);
157 SAFE_FREE(saved_locale);
158 }
159#endif
160}
161
162/*
163 see if a ucs2 character can be mapped correctly to a dos character
164 and mapped back to the same character in ucs2
165*/
166
167int check_dos_char(smb_ucs2_t c)
168{
169 lazy_initialize_conv();
170
171 /* Find the right byte, and right bit within the byte; return
172 * 1 or 0 */
173 return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0;
174}
175
176
177static int check_dos_char_slowly(smb_ucs2_t c)
178{
179 char buf[10];
180 smb_ucs2_t c2 = 0;
181 int len1, len2;
182
183 len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);
184 if (len1 == 0) {
185 return 0;
186 }
187 len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);
188 if (len2 != 2) {
189 return 0;
190 }
191 return (c == c2);
192}
193
194
195/**
196 * Fill out doschar table the hard way, by examining each character
197 **/
198
199void init_doschar_table(void)
200{
201 int i, j, byteval;
202
203 /* For each byte of packed table */
204
205 for (i = 0; i <= 0xffff; i += 8) {
206 byteval = 0;
207 for (j = 0; j <= 7; j++) {
208 smb_ucs2_t c;
209
210 c = i + j;
211
212 if (check_dos_char_slowly(c)) {
213 byteval |= 1 << j;
214 }
215 }
216 doschar_table[i/8] = byteval;
217 }
218}
219
220
221/**
222 * Load the valid character map table from <tt>valid.dat</tt> or
223 * create from the configured codepage.
224 *
225 * This function is called whenever the configuration is reloaded.
226 * However, the valid character table is not changed if it's loaded
227 * from a file, because we can't unmap files.
228 **/
229
230void init_valid_table(void)
231{
232 static int mapped_file;
233 int i;
234 const char *allowed = ".!#$%&'()_-@^`~";
235 uint8 *valid_file;
236
237 if (mapped_file) {
238 /* Can't unmap files, so stick with what we have */
239 return;
240 }
241
242 valid_file = (uint8 *)map_file(lib_path("valid.dat"), 0x10000);
243 if (valid_file) {
244 valid_table = valid_file;
245 mapped_file = 1;
246 valid_table_use_unmap = True;
247 return;
248 }
249
250 /* Otherwise, we're using a dynamically created valid_table.
251 * It might need to be regenerated if the code page changed.
252 * We know that we're not using a mapped file, so we can
253 * free() the old one. */
254 if (valid_table)
255 SAFE_FREE(valid_table);
256
257 /* use free rather than unmap */
258 valid_table_use_unmap = False;
259#ifdef __OS2__
260 DEBUG(1,("could not load %s\n",lib_path("valid.dat")));
261#endif
262 DEBUG(1,("creating default valid table\n"));
263
264 valid_table = (uint8 *)SMB_MALLOC(0x10000);
265 for (i=0;i<128;i++) {
266 valid_table[i] = isalnum(i) || strchr(allowed,i);
267 }
268
269 for (;i<0x10000;i++) {
270 smb_ucs2_t c;
271 SSVAL(&c, 0, i);
272 valid_table[i] = check_dos_char(c);
273 }
274}
275
276/*******************************************************************
277 Write a string in (little-endian) unicode format. src is in
278 the current DOS codepage. len is the length in bytes of the
279 string pointed to by dst.
280
281 if null_terminate is True then null terminate the packet (adds 2 bytes)
282
283 the return value is the length in bytes consumed by the string, including the
284 null termination if applied
285********************************************************************/
286
287size_t dos_PutUniCode(char *dst,const char *src, size_t len, BOOL null_terminate)
288{
289 int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
290 : STR_UNICODE|STR_NOALIGN;
291 return push_ucs2(NULL, dst, src, len, flags);
292}
293
294
295/*******************************************************************
296 Skip past a unicode string, but not more than len. Always move
297 past a terminating zero if found.
298********************************************************************/
299
300char *skip_unibuf(char *src, size_t len)
301{
302 char *srcend = src + len;
303
304 while (src < srcend && SVAL(src,0)) {
305 src += 2;
306 }
307
308 if(!SVAL(src,0)) {
309 src += 2;
310 }
311
312 return src;
313}
314
315/* Copy a string from little-endian or big-endian unicode source (depending
316 * on flags) to internal samba format destination
317 */
318
319int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
320{
321 if (!src) {
322 dest[0] = 0;
323 return 0;
324 }
325 if(dest_len==-1) {
326 dest_len=MAXUNI-3;
327 }
328 return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
329}
330
331/* Copy a string from a unistr2 source to internal samba format
332 destination. Use this instead of direct calls to rpcstr_pull() to avoid
333 having to determine whether the source string is null terminated. */
334
335int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
336{
337 return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
338 src->uni_str_len * 2, 0);
339}
340
341/* Helper function to return a talloc'ed string. I have implemented it with a
342 * copy because I don't really know how pull_ucs2 and friends calculate the
343 * target size. If this turns out to be a major bottleneck someone with deeper
344 * multi-byte knowledge needs to revisit this.
345 * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would
346 * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c.
347 */
348
349char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, const UNISTR2 *src)
350{
351 pstring tmp;
352 size_t result;
353
354 result = pull_ucs2(NULL, tmp, src->buffer, sizeof(tmp),
355 src->uni_str_len * 2, 0);
356 if (result == (size_t)-1) {
357 return NULL;
358 }
359
360 return talloc_strdup(mem_ctx, tmp);
361}
362
363/* Converts a string from internal samba format to unicode
364 */
365
366int rpcstr_push(void* dest, const char *src, size_t dest_len, int flags)
367{
368 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
369}
370
371/*******************************************************************
372 Convert a (little-endian) UNISTR2 structure to an ASCII string.
373********************************************************************/
374
375void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
376{
377 if (str == NULL) {
378 *dest='\0';
379 return;
380 }
381 pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
382}
383
384/*******************************************************************
385 Convert a (little-endian) UNISTR3 structure to an ASCII string.
386********************************************************************/
387
388void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen)
389{
390 if (str == NULL) {
391 *dest='\0';
392 return;
393 }
394 pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2,
395 STR_NOALIGN);
396}
397
398/*******************************************************************
399 Give a static string for displaying a UNISTR2.
400********************************************************************/
401
402const char *unistr2_static(const UNISTR2 *str)
403{
404 static pstring ret;
405 unistr2_to_ascii(ret, str, sizeof(ret));
406 return ret;
407}
408
409/*******************************************************************
410 Duplicate a UNISTR2 string into a null terminated char*
411 using a talloc context.
412********************************************************************/
413
414char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str)
415{
416 char *s;
417 int maxlen = (str->uni_str_len+1)*4;
418 if (!str->buffer) {
419 return NULL;
420 }
421 s = (char *)TALLOC(ctx, maxlen); /* convervative */
422 if (!s) {
423 return NULL;
424 }
425 pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
426 return s;
427}
428
429/*******************************************************************
430 Convert a wchar to upper case.
431********************************************************************/
432
433smb_ucs2_t toupper_w(smb_ucs2_t val)
434{
435 return upcase_table[SVAL(&val,0)];
436}
437
438/*******************************************************************
439 Convert a wchar to lower case.
440********************************************************************/
441
442smb_ucs2_t tolower_w( smb_ucs2_t val )
443{
444 return lowcase_table[SVAL(&val,0)];
445}
446
447/*******************************************************************
448 Determine if a character is lowercase.
449********************************************************************/
450
451BOOL islower_w(smb_ucs2_t c)
452{
453 return upcase_table[SVAL(&c,0)] != c;
454}
455
456/*******************************************************************
457 Determine if a character is uppercase.
458********************************************************************/
459
460BOOL isupper_w(smb_ucs2_t c)
461{
462 return lowcase_table[SVAL(&c,0)] != c;
463}
464
465/*******************************************************************
466 Determine if a character is valid in a 8.3 name.
467********************************************************************/
468
469BOOL isvalid83_w(smb_ucs2_t c)
470{
471 return valid_table[SVAL(&c,0)] != 0;
472}
473
474/*******************************************************************
475 Count the number of characters in a smb_ucs2_t string.
476********************************************************************/
477
478size_t strlen_w(const smb_ucs2_t *src)
479{
480 size_t len;
481 smb_ucs2_t c;
482
483 for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
484 ;
485 }
486
487 return len;
488}
489
490/*******************************************************************
491 Count up to max number of characters in a smb_ucs2_t string.
492********************************************************************/
493
494size_t strnlen_w(const smb_ucs2_t *src, size_t max)
495{
496 size_t len;
497 smb_ucs2_t c;
498
499 for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
500 ;
501 }
502
503 return len;
504}
505
506/*******************************************************************
507 Wide strchr().
508********************************************************************/
509
510smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
511{
512 smb_ucs2_t cp;
513 while (*(COPY_UCS2_CHAR(&cp,s))) {
514 if (c == cp) {
515 return (smb_ucs2_t *)s;
516 }
517 s++;
518 }
519 if (c == cp) {
520 return (smb_ucs2_t *)s;
521 }
522
523 return NULL;
524}
525
526smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
527{
528 return strchr_w(s, UCS2_CHAR(c));
529}
530
531/*******************************************************************
532 Wide strrchr().
533********************************************************************/
534
535smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
536{
537 smb_ucs2_t cp;
538 const smb_ucs2_t *p = s;
539 int len = strlen_w(s);
540
541 if (len == 0) {
542 return NULL;
543 }
544 p += (len - 1);
545 do {
546 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
547 return (smb_ucs2_t *)p;
548 }
549 } while (p-- != s);
550 return NULL;
551}
552
553/*******************************************************************
554 Wide version of strrchr that returns after doing strrchr 'n' times.
555********************************************************************/
556
557smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
558{
559 smb_ucs2_t cp;
560 const smb_ucs2_t *p = s;
561 int len = strlen_w(s);
562
563 if (len == 0 || !n) {
564 return NULL;
565 }
566 p += (len - 1);
567 do {
568 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
569 n--;
570 }
571
572 if (!n) {
573 return (smb_ucs2_t *)p;
574 }
575 } while (p-- != s);
576 return NULL;
577}
578
579/*******************************************************************
580 Wide strstr().
581********************************************************************/
582
583smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
584{
585 smb_ucs2_t *r;
586 size_t inslen;
587
588 if (!s || !*s || !ins || !*ins) {
589 return NULL;
590 }
591
592 inslen = strlen_w(ins);
593 r = (smb_ucs2_t *)s;
594
595 while ((r = strchr_w(r, *ins))) {
596 if (strncmp_w(r, ins, inslen) == 0) {
597 return r;
598 }
599 r++;
600 }
601
602 return NULL;
603}
604
605/*******************************************************************
606 Convert a string to lower case.
607 return True if any char is converted
608********************************************************************/
609
610BOOL strlower_w(smb_ucs2_t *s)
611{
612 smb_ucs2_t cp;
613 BOOL ret = False;
614
615 while (*(COPY_UCS2_CHAR(&cp,s))) {
616 smb_ucs2_t v = tolower_w(cp);
617 if (v != cp) {
618 COPY_UCS2_CHAR(s,&v);
619 ret = True;
620 }
621 s++;
622 }
623 return ret;
624}
625
626/*******************************************************************
627 Convert a string to upper case.
628 return True if any char is converted
629********************************************************************/
630
631BOOL strupper_w(smb_ucs2_t *s)
632{
633 smb_ucs2_t cp;
634 BOOL ret = False;
635 while (*(COPY_UCS2_CHAR(&cp,s))) {
636 smb_ucs2_t v = toupper_w(cp);
637 if (v != cp) {
638 COPY_UCS2_CHAR(s,&v);
639 ret = True;
640 }
641 s++;
642 }
643 return ret;
644}
645
646/*******************************************************************
647 Convert a string to "normal" form.
648********************************************************************/
649
650void strnorm_w(smb_ucs2_t *s, int case_default)
651{
652 if (case_default == CASE_UPPER) {
653 strupper_w(s);
654 } else {
655 strlower_w(s);
656 }
657}
658
659int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
660{
661 smb_ucs2_t cpa, cpb;
662
663 while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
664 a++;
665 b++;
666 }
667 return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
668 /* warning: if *a != *b and both are not 0 we return a random
669 greater or lesser than 0 number not realted to which
670 string is longer */
671}
672
673int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
674{
675 smb_ucs2_t cpa, cpb;
676 size_t n = 0;
677
678 while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
679 a++;
680 b++;
681 n++;
682 }
683 return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
684}
685
686/*******************************************************************
687 Case insensitive string comparison.
688********************************************************************/
689
690int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
691{
692 smb_ucs2_t cpa, cpb;
693
694 while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
695 a++;
696 b++;
697 }
698 return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
699}
700
701/*******************************************************************
702 Case insensitive string comparison, length limited.
703********************************************************************/
704
705int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
706{
707 smb_ucs2_t cpa, cpb;
708 size_t n = 0;
709
710 while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
711 a++;
712 b++;
713 n++;
714 }
715 return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
716}
717
718/*******************************************************************
719 Compare 2 strings.
720********************************************************************/
721
722BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
723{
724 if (s1 == s2) {
725 return(True);
726 }
727 if (!s1 || !s2) {
728 return(False);
729 }
730
731 return(strcasecmp_w(s1,s2)==0);
732}
733
734/*******************************************************************
735 Compare 2 strings up to and including the nth char.
736******************************************************************/
737
738BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
739{
740 if (s1 == s2) {
741 return(True);
742 }
743 if (!s1 || !s2 || !n) {
744 return(False);
745 }
746
747 return(strncasecmp_w(s1,s2,n)==0);
748}
749
750/*******************************************************************
751 Duplicate string.
752********************************************************************/
753
754smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
755{
756 return strndup_w(src, 0);
757}
758
759/* if len == 0 then duplicate the whole string */
760
761smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
762{
763 smb_ucs2_t *dest;
764
765 if (!len) {
766 len = strlen_w(src);
767 }
768 dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
769 if (!dest) {
770 DEBUG(0,("strdup_w: out of memory!\n"));
771 return NULL;
772 }
773
774 memcpy(dest, src, len * sizeof(smb_ucs2_t));
775 dest[len] = 0;
776 return dest;
777}
778
779/*******************************************************************
780 Copy a string with max len.
781********************************************************************/
782
783smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
784{
785 smb_ucs2_t cp;
786 size_t len;
787
788 if (!dest || !src) {
789 return NULL;
790 }
791
792 for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
793 cp = *COPY_UCS2_CHAR(dest+len,src+len);
794 }
795 cp = 0;
796 for ( /*nothing*/ ; len < max; len++ ) {
797 cp = *COPY_UCS2_CHAR(dest+len,&cp);
798 }
799
800 return dest;
801}
802
803/*******************************************************************
804 Append a string of len bytes and add a terminator.
805********************************************************************/
806
807smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
808{
809 size_t start;
810 size_t len;
811 smb_ucs2_t z = 0;
812
813 if (!dest || !src) {
814 return NULL;
815 }
816
817 start = strlen_w(dest);
818 len = strnlen_w(src, max);
819
820 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
821 z = *COPY_UCS2_CHAR(dest+start+len,&z);
822
823 return dest;
824}
825
826smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
827{
828 size_t start;
829 size_t len;
830 smb_ucs2_t z = 0;
831
832 if (!dest || !src) {
833 return NULL;
834 }
835
836 start = strlen_w(dest);
837 len = strlen_w(src);
838
839 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
840 z = *COPY_UCS2_CHAR(dest+start+len,&z);
841
842 return dest;
843}
844
845
846/*******************************************************************
847 Replace any occurence of oldc with newc in unicode string.
848********************************************************************/
849
850void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
851{
852 smb_ucs2_t cp;
853
854 for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
855 if(cp==oldc) {
856 COPY_UCS2_CHAR(s,&newc);
857 }
858 }
859}
860
861/*******************************************************************
862 Trim unicode string.
863********************************************************************/
864
865BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
866 const smb_ucs2_t *back)
867{
868 BOOL ret = False;
869 size_t len, front_len, back_len;
870
871 if (!s) {
872 return False;
873 }
874
875 len = strlen_w(s);
876
877 if (front && *front) {
878 front_len = strlen_w(front);
879 while (len && strncmp_w(s, front, front_len) == 0) {
880 memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
881 len -= front_len;
882 ret = True;
883 }
884 }
885
886 if (back && *back) {
887 back_len = strlen_w(back);
888 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
889 s[len - back_len] = 0;
890 len -= back_len;
891 ret = True;
892 }
893 }
894
895 return ret;
896}
897
898/*
899 The *_wa() functions take a combination of 7 bit ascii
900 and wide characters They are used so that you can use string
901 functions combining C string constants with ucs2 strings
902
903 The char* arguments must NOT be multibyte - to be completely sure
904 of this only pass string constants */
905
906int strcmp_wa(const smb_ucs2_t *a, const char *b)
907{
908 smb_ucs2_t cp = 0;
909
910 while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
911 a++;
912 b++;
913 }
914 return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
915}
916
917int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
918{
919 smb_ucs2_t cp = 0;
920 size_t n = 0;
921
922 while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
923 a++;
924 b++;
925 n++;
926 }
927 return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
928}
929
930smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
931{
932 smb_ucs2_t cp;
933
934 while (*(COPY_UCS2_CHAR(&cp,s))) {
935 int i;
936 for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++)
937 ;
938 if (p[i]) {
939 return (smb_ucs2_t *)s;
940 }
941 s++;
942 }
943 return NULL;
944}
945
946smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
947{
948 smb_ucs2_t *r;
949 size_t inslen;
950
951 if (!s || !ins) {
952 return NULL;
953 }
954
955 inslen = strlen(ins);
956 r = (smb_ucs2_t *)s;
957
958 while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
959 if (strncmp_wa(r, ins, inslen) == 0)
960 return r;
961 r++;
962 }
963
964 return NULL;
965}
966
967BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
968 const char *back)
969{
970 wpstring f, b;
971
972 if (front) {
973 push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
974 } else {
975 *f = 0;
976 }
977 if (back) {
978 push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
979 } else {
980 *b = 0;
981 }
982 return trim_string_w(s, f, b);
983}
984
985/*******************************************************************
986 Returns the length in number of wide characters.
987******************************************************************/
988
989int unistrlen(uint16 *s)
990{
991 int len;
992
993 if (!s) {
994 return -1;
995 }
996
997 for (len=0; SVAL(s,0); s++,len++) {
998 ;
999 }
1000
1001 return len;
1002}
1003
1004/*******************************************************************
1005 Strcpy for unicode strings. Returns length (in num of wide chars).
1006 Not odd align safe.
1007********************************************************************/
1008
1009int unistrcpy(uint16 *dst, uint16 *src)
1010{
1011 int num_wchars = 0;
1012
1013 while (SVAL(src,0)) {
1014 *dst++ = *src++;
1015 num_wchars++;
1016 }
1017 *dst = 0;
1018
1019 return num_wchars;
1020}
1021
1022/**
1023 * Samba ucs2 type to UNISTR2 conversion
1024 *
1025 * @param ctx Talloc context to create the dst strcture (if null) and the
1026 * contents of the unicode string.
1027 * @param dst UNISTR2 destination. If equals null, then it's allocated.
1028 * @param src smb_ucs2_t source.
1029 * @param max_len maximum number of unicode characters to copy. If equals
1030 * null, then null-termination of src is taken
1031 *
1032 * @return copied UNISTR2 destination
1033 **/
1034
1035UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
1036{
1037 size_t len;
1038
1039 if (!src) {
1040 return NULL;
1041 }
1042
1043 len = strlen_w(src);
1044
1045 /* allocate UNISTR2 destination if not given */
1046 if (!dst) {
1047 dst = TALLOC_P(ctx, UNISTR2);
1048 if (!dst)
1049 return NULL;
1050 }
1051 if (!dst->buffer) {
1052 dst->buffer = TALLOC_ARRAY(ctx, uint16, len + 1);
1053 if (!dst->buffer)
1054 return NULL;
1055 }
1056
1057 /* set UNISTR2 parameters */
1058 dst->uni_max_len = len + 1;
1059 dst->offset = 0;
1060 dst->uni_str_len = len;
1061
1062 /* copy the actual unicode string */
1063 strncpy_w(dst->buffer, src, dst->uni_max_len);
1064
1065 return dst;
1066}
1067
1068/*************************************************************
1069 ascii only toupper - saves the need for smbd to be in C locale.
1070*************************************************************/
1071
1072int toupper_ascii(int c)
1073{
1074 smb_ucs2_t uc = toupper_w(UCS2_CHAR(c));
1075 return UCS2_TO_CHAR(uc);
1076}
1077
1078/*************************************************************
1079 ascii only tolower - saves the need for smbd to be in C locale.
1080*************************************************************/
1081
1082int tolower_ascii(int c)
1083{
1084 smb_ucs2_t uc = tolower_w(UCS2_CHAR(c));
1085 return UCS2_TO_CHAR(uc);
1086}
1087
1088/*************************************************************
1089 ascii only isupper - saves the need for smbd to be in C locale.
1090*************************************************************/
1091
1092int isupper_ascii(int c)
1093{
1094 return isupper_w(UCS2_CHAR(c));
1095}
1096
1097/*************************************************************
1098 ascii only islower - saves the need for smbd to be in C locale.
1099*************************************************************/
1100
1101int islower_ascii(int c)
1102{
1103 return islower_w(UCS2_CHAR(c));
1104}
Note: See TracBrowser for help on using the repository browser.