source: trunk-3.0/source/lib/util_unistr.c@ 101

Last change on this file since 101 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

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