source: branches/samba-3.2.x/source/lib/util_unistr.c

Last change on this file was 149, checked in by Paul Smedley, 17 years ago

Update trunk to v3.2.2

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