s3: Add an async smbsock_connect
[Samba.git] / source3 / lib / util_unistr.c
blob7d80b3cdea26ed396a4f308dd285630d53dcc2c3
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) Simo Sorce 2001
6 Copyright (C) Jeremy Allison 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 #ifndef MAXUNI
25 #define MAXUNI 1024
26 #endif
28 /* these 3 tables define the unicode case handling. They are loaded
29 at startup either via mmap() or read() from the lib directory */
30 static smb_ucs2_t *upcase_table;
31 static smb_ucs2_t *lowcase_table;
32 static uint8 *valid_table;
33 static bool upcase_table_use_unmap;
34 static bool lowcase_table_use_unmap;
35 static bool valid_table_use_unmap;
36 static bool initialized;
38 /**
39 * Destroy global objects allocated by load_case_tables()
40 **/
41 void gfree_case_tables(void)
43 if ( upcase_table ) {
44 if ( upcase_table_use_unmap )
45 unmap_file(upcase_table, 0x20000);
46 else
47 SAFE_FREE(upcase_table);
50 if ( lowcase_table ) {
51 if ( lowcase_table_use_unmap )
52 unmap_file(lowcase_table, 0x20000);
53 else
54 SAFE_FREE(lowcase_table);
57 if ( valid_table ) {
58 if ( valid_table_use_unmap )
59 unmap_file(valid_table, 0x10000);
60 else
61 SAFE_FREE(valid_table);
63 initialized = false;
66 /**
67 * Load or generate the case handling tables.
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 **/
73 void load_case_tables(void)
75 char *old_locale = NULL, *saved_locale = NULL;
76 int i;
77 TALLOC_CTX *frame = NULL;
79 if (initialized) {
80 return;
82 initialized = true;
84 frame = talloc_stackframe();
86 upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"),
87 0x20000);
88 upcase_table_use_unmap = ( upcase_table != NULL );
90 lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"),
91 0x20000);
92 lowcase_table_use_unmap = ( lowcase_table != NULL );
94 #ifdef HAVE_SETLOCALE
95 /* Get the name of the current locale. */
96 old_locale = setlocale(LC_ALL, NULL);
98 if (old_locale) {
99 /* Save it as it is in static storage. */
100 saved_locale = SMB_STRDUP(old_locale);
103 /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */
104 setlocale(LC_ALL, "C");
105 #endif
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;
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);
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;
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);
139 #ifdef HAVE_SETLOCALE
140 /* Restore the old locale. */
141 if (saved_locale) {
142 setlocale (LC_ALL, saved_locale);
143 SAFE_FREE(saved_locale);
145 #endif
146 TALLOC_FREE(frame);
149 static int check_dos_char_slowly(smb_ucs2_t c)
151 char buf[10];
152 smb_ucs2_t c2 = 0;
153 int len1, len2;
155 len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);
156 if (len1 == 0) {
157 return 0;
159 len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);
160 if (len2 != 2) {
161 return 0;
163 return (c == c2);
167 * Load the valid character map table from <tt>valid.dat</tt> or
168 * create from the configured codepage.
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.
175 void init_valid_table(void)
177 static int mapped_file;
178 int i;
179 const char *allowed = ".!#$%&'()_-@^`~";
180 uint8 *valid_file;
182 if (mapped_file) {
183 /* Can't unmap files, so stick with what we have */
184 return;
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;
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);
201 /* use free rather than unmap */
202 valid_table_use_unmap = False;
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);
211 lazy_initialize_conv();
213 for (;i<0x10000;i++) {
214 smb_ucs2_t c;
215 SSVAL(&c, 0, i);
216 valid_table[i] = check_dos_char_slowly(c);
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.
225 if null_terminate is True then null terminate the packet (adds 2 bytes)
227 the return value is the length in bytes consumed by the string, including the
228 null termination if applied
229 ********************************************************************/
231 size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate)
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);
239 /*******************************************************************
240 Skip past a unicode string, but not more than len. Always move
241 past a terminating zero if found.
242 ********************************************************************/
244 char *skip_unibuf(char *src, size_t len)
246 char *srcend = src + len;
248 while (src < srcend && SVAL(src,0)) {
249 src += 2;
252 if(!SVAL(src,0)) {
253 src += 2;
256 return src;
259 /* Copy a string from little-endian or big-endian unicode source (depending
260 * on flags) to internal samba format destination
263 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
265 if (!src) {
266 dest[0] = 0;
267 return 0;
269 if(dest_len==-1) {
270 dest_len=MAXUNI-3;
272 return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
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.
279 int rpcstr_pull_talloc(TALLOC_CTX *ctx,
280 char **dest,
281 void *src,
282 int src_len,
283 int flags)
285 return pull_ucs2_base_talloc(ctx,
286 NULL,
287 dest,
288 src,
289 src_len,
290 flags|STR_UNICODE|STR_NOALIGN);
294 /* Converts a string from internal samba format to unicode
297 int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags)
299 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
302 /* Converts a string from internal samba format to unicode. Always terminates.
303 * Actually just a wrapper round push_ucs2_talloc().
306 int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
308 size_t size;
309 if (push_ucs2_talloc(ctx, dest, src, &size))
310 return size;
311 else
312 return -1;
315 /*******************************************************************
316 Convert a wchar to upper case.
317 ********************************************************************/
319 smb_ucs2_t toupper_w(smb_ucs2_t val)
321 return upcase_table[SVAL(&val,0)];
324 /*******************************************************************
325 Convert a wchar to lower case.
326 ********************************************************************/
328 smb_ucs2_t tolower_w( smb_ucs2_t val )
330 return lowcase_table[SVAL(&val,0)];
333 /*******************************************************************
334 Determine if a character is lowercase.
335 ********************************************************************/
337 bool islower_w(smb_ucs2_t c)
339 return upcase_table[SVAL(&c,0)] != c;
342 /*******************************************************************
343 Determine if a character is uppercase.
344 ********************************************************************/
346 bool isupper_w(smb_ucs2_t c)
348 return lowcase_table[SVAL(&c,0)] != c;
351 /*******************************************************************
352 Determine if a character is valid in a 8.3 name.
353 ********************************************************************/
355 bool isvalid83_w(smb_ucs2_t c)
357 return valid_table[SVAL(&c,0)] != 0;
360 /*******************************************************************
361 Count the number of characters in a smb_ucs2_t string.
362 ********************************************************************/
364 size_t strlen_w(const smb_ucs2_t *src)
366 size_t len;
367 smb_ucs2_t c;
369 for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
373 return len;
376 /*******************************************************************
377 Count up to max number of characters in a smb_ucs2_t string.
378 ********************************************************************/
380 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
382 size_t len;
383 smb_ucs2_t c;
385 for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
389 return len;
392 /*******************************************************************
393 Wide strchr().
394 ********************************************************************/
396 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
398 smb_ucs2_t cp;
399 while (*(COPY_UCS2_CHAR(&cp,s))) {
400 if (c == cp) {
401 return (smb_ucs2_t *)s;
403 s++;
405 if (c == cp) {
406 return (smb_ucs2_t *)s;
409 return NULL;
412 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
414 return strchr_w(s, UCS2_CHAR(c));
417 /*******************************************************************
418 Wide strrchr().
419 ********************************************************************/
421 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
423 smb_ucs2_t cp;
424 const smb_ucs2_t *p = s;
425 int len = strlen_w(s);
427 if (len == 0) {
428 return NULL;
430 p += (len - 1);
431 do {
432 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
433 return (smb_ucs2_t *)p;
435 } while (p-- != s);
436 return NULL;
439 /*******************************************************************
440 Wide version of strrchr that returns after doing strrchr 'n' times.
441 ********************************************************************/
443 smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
445 smb_ucs2_t cp;
446 const smb_ucs2_t *p = s;
447 int len = strlen_w(s);
449 if (len == 0 || !n) {
450 return NULL;
452 p += (len - 1);
453 do {
454 if (c == *(COPY_UCS2_CHAR(&cp,p))) {
455 n--;
458 if (!n) {
459 return (smb_ucs2_t *)p;
461 } while (p-- != s);
462 return NULL;
465 /*******************************************************************
466 Wide strstr().
467 ********************************************************************/
469 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
471 smb_ucs2_t *r;
472 size_t inslen;
474 if (!s || !*s || !ins || !*ins) {
475 return NULL;
478 inslen = strlen_w(ins);
479 r = (smb_ucs2_t *)s;
481 while ((r = strchr_w(r, *ins))) {
482 if (strncmp_w(r, ins, inslen) == 0) {
483 return r;
485 r++;
488 return NULL;
491 /*******************************************************************
492 Convert a string to lower case.
493 return True if any char is converted
494 ********************************************************************/
496 bool strlower_w(smb_ucs2_t *s)
498 smb_ucs2_t cp;
499 bool ret = False;
501 while (*(COPY_UCS2_CHAR(&cp,s))) {
502 smb_ucs2_t v = tolower_w(cp);
503 if (v != cp) {
504 COPY_UCS2_CHAR(s,&v);
505 ret = True;
507 s++;
509 return ret;
512 /*******************************************************************
513 Convert a string to upper case.
514 return True if any char is converted
515 ********************************************************************/
517 bool strupper_w(smb_ucs2_t *s)
519 smb_ucs2_t cp;
520 bool ret = False;
521 while (*(COPY_UCS2_CHAR(&cp,s))) {
522 smb_ucs2_t v = toupper_w(cp);
523 if (v != cp) {
524 COPY_UCS2_CHAR(s,&v);
525 ret = True;
527 s++;
529 return ret;
532 /*******************************************************************
533 Convert a string to "normal" form.
534 ********************************************************************/
536 void strnorm_w(smb_ucs2_t *s, int case_default)
538 if (case_default == CASE_UPPER) {
539 strupper_w(s);
540 } else {
541 strlower_w(s);
545 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
547 smb_ucs2_t cpa, cpb;
549 while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
550 a++;
551 b++;
553 return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
554 /* warning: if *a != *b and both are not 0 we return a random
555 greater or lesser than 0 number not realted to which
556 string is longer */
559 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
561 smb_ucs2_t cpa, cpb;
562 size_t n = 0;
564 while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
565 a++;
566 b++;
567 n++;
569 return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
572 /*******************************************************************
573 Case insensitive string comparison.
574 ********************************************************************/
576 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
578 smb_ucs2_t cpa, cpb;
580 while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
581 a++;
582 b++;
584 return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
587 /*******************************************************************
588 Case insensitive string comparison, length limited.
589 ********************************************************************/
591 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
593 smb_ucs2_t cpa, cpb;
594 size_t n = 0;
596 while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
597 a++;
598 b++;
599 n++;
601 return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
604 /*******************************************************************
605 Compare 2 strings.
606 ********************************************************************/
608 bool strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
610 if (s1 == s2) {
611 return(True);
613 if (!s1 || !s2) {
614 return(False);
617 return(strcasecmp_w(s1,s2)==0);
620 /*******************************************************************
621 Compare 2 strings up to and including the nth char.
622 ******************************************************************/
624 bool strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
626 if (s1 == s2) {
627 return(True);
629 if (!s1 || !s2 || !n) {
630 return(False);
633 return(strncasecmp_w(s1,s2,n)==0);
636 /*******************************************************************
637 Duplicate string.
638 ********************************************************************/
640 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
642 return strndup_w(src, 0);
645 /* if len == 0 then duplicate the whole string */
647 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
649 smb_ucs2_t *dest;
651 if (!len) {
652 len = strlen_w(src);
654 dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
655 if (!dest) {
656 DEBUG(0,("strdup_w: out of memory!\n"));
657 return NULL;
660 memcpy(dest, src, len * sizeof(smb_ucs2_t));
661 dest[len] = 0;
662 return dest;
665 /*******************************************************************
666 Copy a string with max len.
667 ********************************************************************/
669 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
671 smb_ucs2_t cp;
672 size_t len;
674 if (!dest || !src) {
675 return NULL;
678 for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
679 cp = *COPY_UCS2_CHAR(dest+len,src+len);
681 cp = 0;
682 for ( /*nothing*/ ; len < max; len++ ) {
683 cp = *COPY_UCS2_CHAR(dest+len,&cp);
686 return dest;
689 /*******************************************************************
690 Append a string of len bytes and add a terminator.
691 ********************************************************************/
693 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
695 size_t start;
696 size_t len;
697 smb_ucs2_t z = 0;
699 if (!dest || !src) {
700 return NULL;
703 start = strlen_w(dest);
704 len = strnlen_w(src, max);
706 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
707 z = *COPY_UCS2_CHAR(dest+start+len,&z);
709 return dest;
712 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
714 size_t start;
715 size_t len;
716 smb_ucs2_t z = 0;
718 if (!dest || !src) {
719 return NULL;
722 start = strlen_w(dest);
723 len = strlen_w(src);
725 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
726 z = *COPY_UCS2_CHAR(dest+start+len,&z);
728 return dest;
732 /*******************************************************************
733 Replace any occurence of oldc with newc in unicode string.
734 ********************************************************************/
736 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
738 smb_ucs2_t cp;
740 for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
741 if(cp==oldc) {
742 COPY_UCS2_CHAR(s,&newc);
747 /*******************************************************************
748 Trim unicode string.
749 ********************************************************************/
751 bool trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
752 const smb_ucs2_t *back)
754 bool ret = False;
755 size_t len, front_len, back_len;
757 if (!s) {
758 return False;
761 len = strlen_w(s);
763 if (front && *front) {
764 front_len = strlen_w(front);
765 while (len && strncmp_w(s, front, front_len) == 0) {
766 memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
767 len -= front_len;
768 ret = True;
772 if (back && *back) {
773 back_len = strlen_w(back);
774 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
775 s[len - back_len] = 0;
776 len -= back_len;
777 ret = True;
781 return ret;
785 The *_wa() functions take a combination of 7 bit ascii
786 and wide characters They are used so that you can use string
787 functions combining C string constants with ucs2 strings
789 The char* arguments must NOT be multibyte - to be completely sure
790 of this only pass string constants */
792 int strcmp_wa(const smb_ucs2_t *a, const char *b)
794 smb_ucs2_t cp = 0;
796 while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
797 a++;
798 b++;
800 return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
803 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
805 smb_ucs2_t cp = 0;
806 size_t n = 0;
808 while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
809 a++;
810 b++;
811 n++;
813 return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
816 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
818 smb_ucs2_t cp;
820 while (*(COPY_UCS2_CHAR(&cp,s))) {
821 int i;
822 for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++)
824 if (p[i]) {
825 return (smb_ucs2_t *)s;
827 s++;
829 return NULL;
832 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
834 smb_ucs2_t *r;
835 size_t inslen;
837 if (!s || !ins) {
838 return NULL;
841 inslen = strlen(ins);
842 r = (smb_ucs2_t *)s;
844 while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
845 if (strncmp_wa(r, ins, inslen) == 0)
846 return r;
847 r++;
850 return NULL;
853 /*************************************************************
854 ascii only toupper - saves the need for smbd to be in C locale.
855 *************************************************************/
857 int toupper_ascii(int c)
859 smb_ucs2_t uc = toupper_w(UCS2_CHAR(c));
860 return UCS2_TO_CHAR(uc);
863 /*************************************************************
864 ascii only tolower - saves the need for smbd to be in C locale.
865 *************************************************************/
867 int tolower_ascii(int c)
869 smb_ucs2_t uc = tolower_w(UCS2_CHAR(c));
870 return UCS2_TO_CHAR(uc);
873 /*************************************************************
874 ascii only isupper - saves the need for smbd to be in C locale.
875 *************************************************************/
877 int isupper_ascii(int c)
879 return isupper_w(UCS2_CHAR(c));
882 /*************************************************************
883 ascii only islower - saves the need for smbd to be in C locale.
884 *************************************************************/
886 int islower_ascii(int c)
888 return islower_w(UCS2_CHAR(c));