2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
26 smb_ucs2_t wchar_list_sep[] = { (smb_ucs2_t)' ', (smb_ucs2_t)'\t', (smb_ucs2_t)',',
27 (smb_ucs2_t)';', (smb_ucs2_t)':', (smb_ucs2_t)'\n',
28 (smb_ucs2_t)'\r', 0 };
30 * The following are the codepage to ucs2 and vica versa maps.
31 * These are dynamically loaded from a unicode translation file.
34 static smb_ucs2_t *doscp_to_ucs2;
35 static uint16 *ucs2_to_doscp;
37 static smb_ucs2_t *unixcp_to_ucs2;
38 static uint16 *ucs2_to_unixcp;
44 /*******************************************************************
45 Write a string in (little-endian) unicode format. src is in
46 the current DOS codepage. len is the length in bytes of the
47 string pointed to by dst.
49 if null_terminate is True then null terminate the packet (adds 2 bytes)
51 the return value is the length consumed by the string, including the
52 null termination if applied
53 ********************************************************************/
55 int dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
58 while (*src && (len > 2)) {
59 size_t skip = get_character_len(*src);
60 smb_ucs2_t val = (*src & 0xff);
63 * If this is a multibyte character (and all DOS/Windows
64 * codepages have at maximum 2 byte multibyte characters)
65 * then work out the index value for the unicode conversion.
69 val = ((val << 8) | (src[1] & 0xff));
71 SSVAL(dst,ret,doscp_to_ucs2[val]);
86 /*******************************************************************
87 Put an ASCII string into a UNICODE array (uint16's).
89 Warning: doesn't do any codepage !!! BAD !!!
91 Help ! Fix Me ! Fix Me !
92 ********************************************************************/
94 void ascii_to_unistr(uint16 *dest, const char *src, int maxlen)
96 uint16 *destend = dest + maxlen;
99 while (dest < destend)
107 *(dest++) = (uint16)c;
113 /*******************************************************************
114 Pull an ASCII string out of a UNICODE array (uint16's).
116 Warning: doesn't do any codepage !!! BAD !!!
118 Help ! Fix Me ! Fix Me !
119 ********************************************************************/
121 void unistr_to_ascii(char *dest, const uint16 *src, int len)
123 char *destend = dest + len;
126 while (dest < destend)
140 /*******************************************************************
141 Skip past a unicode string, but not more than len. Always move
142 past a terminating zero if found.
143 ********************************************************************/
145 char *skip_unibuf(char *src, size_t len)
147 char *srcend = src + len;
149 while (src < srcend && SVAL(src,0))
158 /*******************************************************************
159 Return a DOS codepage version of a little-endian unicode string.
160 len is the filename length (ignoring any terminating zero) in uin16
161 units. Always null terminates.
162 Hack alert: uses fixed buffer(s).
163 ********************************************************************/
165 char *dos_unistrn2(uint16 *src, int len)
167 static char lbufs[8][MAXUNI];
169 char *lbuf = lbufs[nexti];
174 for (p = lbuf; (len > 0) && (p-lbuf < MAXUNI-3) && *src; len--, src++) {
175 uint16 ucs2_val = SVAL(src,0);
176 uint16 cp_val = ucs2_to_doscp[ucs2_val];
181 *p++ = (cp_val >> 8) & 0xff;
182 *p++ = (cp_val & 0xff);
190 static char lbufs[8][MAXUNI];
193 /*******************************************************************
194 Return a DOS codepage version of a little-endian unicode string.
195 Hack alert: uses fixed buffer(s).
196 ********************************************************************/
198 char *dos_unistr2(uint16 *src)
200 char *lbuf = lbufs[nexti];
205 for (p = lbuf; *src && (p-lbuf < MAXUNI-3); src++) {
206 uint16 ucs2_val = SVAL(src,0);
207 uint16 cp_val = ucs2_to_doscp[ucs2_val];
212 *p++ = (cp_val >> 8) & 0xff;
213 *p++ = (cp_val & 0xff);
221 /*******************************************************************
222 Return a DOS codepage version of a little-endian unicode string
223 ********************************************************************/
225 char *dos_unistr2_to_str(UNISTR2 *str)
227 char *lbuf = lbufs[nexti];
229 uint16 *src = str->buffer;
230 int max_size = MIN(sizeof(str->buffer)-3, str->uni_str_len);
234 for (p = lbuf; *src && p-lbuf < max_size; src++) {
235 uint16 ucs2_val = SVAL(src,0);
236 uint16 cp_val = ucs2_to_doscp[ucs2_val];
241 *p++ = (cp_val >> 8) & 0xff;
242 *p++ = (cp_val & 0xff);
250 /*******************************************************************
251 Convert a UNISTR2 structure to an ASCII string
252 Warning: this version does DOS codepage.
253 ********************************************************************/
255 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
263 len = MIN(str->uni_str_len, maxlen);
264 destend = dest + len;
266 while (dest < destend)
277 ucs2_val = SVAL(src++,0);
278 cp_val = ucs2_to_doscp[ucs2_val];
281 *(dest++) = (char)cp_val;
283 *dest= (cp_val >> 8) & 0xff;
284 *(dest++) = (cp_val & 0xff);
292 /*******************************************************************
293 Return a number stored in a buffer
294 ********************************************************************/
296 uint32 buffer2_to_uint32(BUFFER2 *str)
298 if (str->buf_len == 4)
299 return IVAL(str->buffer, 0);
304 /*******************************************************************
305 Return a DOS codepage version of a NOTunicode string
306 ********************************************************************/
308 char *dos_buffer2_to_str(BUFFER2 *str)
310 char *lbuf = lbufs[nexti];
312 uint16 *src = str->buffer;
313 int max_size = MIN(sizeof(str->buffer)-3, str->buf_len/2);
317 for (p = lbuf; *src && p-lbuf < max_size; src++) {
318 uint16 ucs2_val = SVAL(src,0);
319 uint16 cp_val = ucs2_to_doscp[ucs2_val];
324 *p++ = (cp_val >> 8) & 0xff;
325 *p++ = (cp_val & 0xff);
333 /*******************************************************************
334 Return a dos codepage version of a NOTunicode string
335 ********************************************************************/
337 char *dos_buffer2_to_multistr(BUFFER2 *str)
339 char *lbuf = lbufs[nexti];
341 uint16 *src = str->buffer;
342 int max_size = MIN(sizeof(str->buffer)-3, str->buf_len/2);
346 for (p = lbuf; p-lbuf < max_size; src++) {
350 uint16 ucs2_val = SVAL(src,0);
351 uint16 cp_val = ucs2_to_doscp[ucs2_val];
356 *p++ = (cp_val >> 8) & 0xff;
357 *p++ = (cp_val & 0xff);
366 /*******************************************************************
367 Create a null-terminated unicode string from a null-terminated DOS
369 Return number of unicode chars copied, excluding the null character.
370 Unicode strings created are in little-endian format.
371 ********************************************************************/
373 size_t dos_struni2(char *dst, const char *src, size_t max_len)
381 for (; *src && len < max_len-2; len++, dst +=2) {
382 size_t skip = get_character_len(*src);
383 smb_ucs2_t val = (*src & 0xff);
386 * If this is a multibyte character (and all DOS/Windows
387 * codepages have at maximum 2 byte multibyte characters)
388 * then work out the index value for the unicode conversion.
392 val = ((val << 8) | (src[1] & 0xff));
394 SSVAL(dst,0,doscp_to_ucs2[val]);
407 /*******************************************************************
408 Return a DOS codepage version of a little-endian unicode string.
409 Hack alert: uses fixed buffer(s).
410 ********************************************************************/
412 char *dos_unistr(char *buf)
414 char *lbuf = lbufs[nexti];
415 uint16 *src = (uint16 *)buf;
420 for (p = lbuf; *src && p-lbuf < MAXUNI-3; src++) {
421 uint16 ucs2_val = SVAL(src,0);
422 uint16 cp_val = ucs2_to_doscp[ucs2_val];
427 *p++ = (cp_val >> 8) & 0xff;
428 *p++ = (cp_val & 0xff);
436 /*******************************************************************
437 Strcpy for unicode strings. returns length (in num of wide chars)
438 ********************************************************************/
440 int unistrcpy(char *dst, char *src)
443 uint16 *wsrc = (uint16 *)src;
444 uint16 *wdst = (uint16 *)dst;
457 /*******************************************************************
458 Free any existing maps.
459 ********************************************************************/
461 static void free_maps(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp)
463 /* this handles identity mappings where we share the pointer */
464 if (*pp_ucs2_to_cp == *pp_cp_to_ucs2) {
465 *pp_ucs2_to_cp = NULL;
468 if (*pp_cp_to_ucs2) {
469 free(*pp_cp_to_ucs2);
470 *pp_cp_to_ucs2 = NULL;
473 if (*pp_ucs2_to_cp) {
474 free(*pp_ucs2_to_cp);
475 *pp_ucs2_to_cp = NULL;
480 /*******************************************************************
481 Build a default (null) codepage to unicode map.
482 ********************************************************************/
484 void default_unicode_map(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp)
488 free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp);
490 if ((*pp_ucs2_to_cp = (uint16 *)malloc(2*65536)) == NULL) {
491 DEBUG(0,("default_unicode_map: malloc fail for ucs2_to_cp size %u.\n", 2*65536));
495 *pp_cp_to_ucs2 = *pp_ucs2_to_cp; /* Default map is an identity. */
496 for (i = 0; i < 65536; i++)
497 (*pp_cp_to_ucs2)[i] = i;
500 /*******************************************************************
501 Load a codepage to unicode and vica-versa map.
502 ********************************************************************/
504 BOOL load_unicode_map(const char *codepage, smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp)
506 pstring unicode_map_file_name;
509 smb_ucs2_t *cp_to_ucs2 = *pp_cp_to_ucs2;
510 uint16 *ucs2_to_cp = *pp_ucs2_to_cp;
511 size_t cp_to_ucs2_size;
512 size_t ucs2_to_cp_size;
515 char buf[UNICODE_MAP_HEADER_SIZE];
517 DEBUG(5, ("load_unicode_map: loading unicode map for codepage %s.\n", codepage));
519 if (*codepage == '\0')
522 if(strlen(CODEPAGEDIR) + 13 + strlen(codepage) > sizeof(unicode_map_file_name)) {
523 DEBUG(0,("load_unicode_map: filename too long to load\n"));
527 pstrcpy(unicode_map_file_name, CODEPAGEDIR);
528 pstrcat(unicode_map_file_name, "/");
529 pstrcat(unicode_map_file_name, "unicode_map.");
530 pstrcat(unicode_map_file_name, codepage);
532 if(sys_stat(unicode_map_file_name,&st)!=0) {
533 DEBUG(0,("load_unicode_map: filename %s does not exist.\n",
534 unicode_map_file_name));
540 if ((size != UNICODE_MAP_HEADER_SIZE + 4*65536) && (size != UNICODE_MAP_HEADER_SIZE +(2*256 + 2*65536))) {
541 DEBUG(0,("load_unicode_map: file %s is an incorrect size for a \
542 unicode map file (size=%d).\n", unicode_map_file_name, (int)size));
546 if((fp = sys_fopen( unicode_map_file_name, "r")) == NULL) {
547 DEBUG(0,("load_unicode_map: cannot open file %s. Error was %s\n",
548 unicode_map_file_name, strerror(errno)));
552 if(fread( buf, 1, UNICODE_MAP_HEADER_SIZE, fp)!=UNICODE_MAP_HEADER_SIZE) {
553 DEBUG(0,("load_unicode_map: cannot read header from file %s. Error was %s\n",
554 unicode_map_file_name, strerror(errno)));
558 /* Check the version value */
559 if(SVAL(buf,UNICODE_MAP_VERSION_OFFSET) != UNICODE_MAP_FILE_VERSION_ID) {
560 DEBUG(0,("load_unicode_map: filename %s has incorrect version id. \
561 Needed %hu, got %hu.\n",
562 unicode_map_file_name, (uint16)UNICODE_MAP_FILE_VERSION_ID,
563 SVAL(buf,UNICODE_MAP_VERSION_OFFSET)));
567 /* Check the codepage value */
568 if(!strequal(&buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET], codepage)) {
569 DEBUG(0,("load_unicode_map: codepage %s in file %s is not the same as that \
570 requested (%s).\n", &buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET], unicode_map_file_name, codepage ));
574 ucs2_to_cp_size = 2*65536;
575 if (size == UNICODE_MAP_HEADER_SIZE + 4*65536) {
577 * This is a multibyte code page.
579 cp_to_ucs2_size = 2*65536;
582 * Single byte code page.
584 cp_to_ucs2_size = 2*256;
588 * Free any old translation tables.
591 free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp);
593 if ((cp_to_ucs2 = (smb_ucs2_t *)malloc(cp_to_ucs2_size)) == NULL) {
594 DEBUG(0,("load_unicode_map: malloc fail for cp_to_ucs2 size %u.\n", cp_to_ucs2_size ));
598 if ((ucs2_to_cp = (uint16 *)malloc(ucs2_to_cp_size)) == NULL) {
599 DEBUG(0,("load_unicode_map: malloc fail for ucs2_to_cp size %u.\n", ucs2_to_cp_size ));
603 if(fread( (char *)cp_to_ucs2, 1, cp_to_ucs2_size, fp)!=cp_to_ucs2_size) {
604 DEBUG(0,("load_unicode_map: cannot read cp_to_ucs2 from file %s. Error was %s\n",
605 unicode_map_file_name, strerror(errno)));
609 if(fread( (char *)ucs2_to_cp, 1, ucs2_to_cp_size, fp)!=ucs2_to_cp_size) {
610 DEBUG(0,("load_unicode_map: cannot read ucs2_to_cp from file %s. Error was %s\n",
611 unicode_map_file_name, strerror(errno)));
616 * Now ensure the 16 bit values are in the correct endianness.
619 for (i = 0; i < cp_to_ucs2_size/2; i++)
620 cp_to_ucs2[i] = SVAL(cp_to_ucs2,i*2);
622 for (i = 0; i < ucs2_to_cp_size/2; i++)
623 ucs2_to_cp[i] = SVAL(ucs2_to_cp,i*2);
627 *pp_cp_to_ucs2 = cp_to_ucs2;
628 *pp_ucs2_to_cp = ucs2_to_cp;
634 /* pseudo destructor :-) */
639 free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp);
641 default_unicode_map(pp_cp_to_ucs2, pp_ucs2_to_cp);
646 /*******************************************************************
647 Load a dos codepage to unicode and vica-versa map.
648 ********************************************************************/
650 BOOL load_dos_unicode_map(int codepage)
652 fstring codepage_str;
654 slprintf(codepage_str, sizeof(fstring)-1, "%03d", codepage);
655 return load_unicode_map(codepage_str, &doscp_to_ucs2, &ucs2_to_doscp);
658 /*******************************************************************
659 Load a UNIX codepage to unicode and vica-versa map.
660 ********************************************************************/
662 BOOL load_unix_unicode_map(const char *unix_char_set)
664 fstring upper_unix_char_set;
666 fstrcpy(upper_unix_char_set, unix_char_set);
667 strupper(upper_unix_char_set);
668 return load_unicode_map(upper_unix_char_set, &unixcp_to_ucs2, &ucs2_to_unixcp);
671 /*******************************************************************
672 The following functions reproduce many of the non-UNICODE standard
673 string functions in Samba.
674 ********************************************************************/
676 /*******************************************************************
677 Convert a UNICODE string to multibyte format. Note that the 'src' is in
678 native byte order, not little endian. Always zero terminates.
680 ********************************************************************/
682 static char *unicode_to_multibyte(char *dst, const smb_ucs2_t *src,
683 size_t dst_len, const uint16 *ucs2_to_cp)
687 for(dst_pos = 0; *src && (dst_pos < dst_len - 1);) {
688 smb_ucs2_t val = ucs2_to_cp[*src++];
690 dst[dst_pos++] = (char)val;
693 if(dst_pos >= dst_len - 2)
697 * A 2 byte value is always written as
698 * high/low into the buffer stream.
701 dst[dst_pos++] = (char)((val >> 8) & 0xff);
702 dst[dst_pos++] = (char)(val & 0xff);
711 /*******************************************************************
712 Convert a multibyte string to UNICODE format. Note that the 'dst' is in
713 native byte order, not little endian. Always zero terminates.
715 ********************************************************************/
717 smb_ucs2_t *multibyte_to_unicode(smb_ucs2_t *dst, const char *src,
718 size_t dst_len, smb_ucs2_t *cp_to_ucs2)
722 dst_len /= sizeof(smb_ucs2_t); /* Convert to smb_ucs2_t units. */
724 for(i = 0; (i < (dst_len - 1)) && src[i];) {
725 size_t skip = skip_multibyte_char(*src);
726 smb_ucs2_t val = (*src & 0xff);
729 * If this is a multibyte character
730 * then work out the index value for the unicode conversion.
734 val = ((val << 8) | (src[1] & 0xff));
736 dst[i++] = cp_to_ucs2[val];
748 /*******************************************************************
749 Convert a UNICODE string to multibyte format. Note that the 'src' is in
750 native byte order, not little endian. Always zero terminates.
751 This function may be replaced if the MB codepage format is an
752 encoded one (ie. utf8, hex). See the code in lib/kanji.c
753 for details. dst_len is in bytes.
754 ********************************************************************/
756 char *unicode_to_unix(char *dst, const smb_ucs2_t *src, size_t dst_len)
758 return unicode_to_multibyte(dst, src, dst_len, ucs2_to_unixcp);
761 /*******************************************************************
762 Convert a UNIX string to UNICODE format. Note that the 'dst' is in
763 native byte order, not little endian. Always zero terminates.
764 This function may be replaced if the UNIX codepage format is a
765 multi-byte one (ie. JIS, SJIS or utf8). See the code in lib/kanji.c
766 for details. dst_len is in bytes, not ucs2 units.
767 ********************************************************************/
769 smb_ucs2_t *unix_to_unicode(smb_ucs2_t *dst, const char *src, size_t dst_len)
771 return multibyte_to_unicode(dst, src, dst_len, unixcp_to_ucs2);
774 /*******************************************************************
775 Convert a UNICODE string to DOS format. Note that the 'src' is in
776 native byte order, not little endian. Always zero terminates.
778 ********************************************************************/
780 char *unicode_to_dos(char *dst, const smb_ucs2_t *src, size_t dst_len)
782 return unicode_to_multibyte(dst, src, dst_len, ucs2_to_doscp);
785 /*******************************************************************
786 Convert a DOS string to UNICODE format. Note that the 'dst' is in
787 native byte order, not little endian. Always zero terminates.
788 This function may be replaced if the DOS codepage format is a
789 multi-byte one (ie. JIS, SJIS or utf8). See the code in lib/kanji.c
790 for details. dst_len is in bytes, not ucs2 units.
791 ********************************************************************/
793 smb_ucs2_t *dos_to_unicode(smb_ucs2_t *dst, const char *src, size_t dst_len)
795 return multibyte_to_unicode(dst, src, dst_len, doscp_to_ucs2);
798 /*******************************************************************
799 Count the number of characters in a smb_ucs2_t string.
800 ********************************************************************/
802 size_t strlen_w(const smb_ucs2_t *src)
806 for(len = 0; *src; len++)
812 /*******************************************************************
813 Safe wstring copy into a known length string. maxlength includes
814 the terminating zero. maxlength is in ucs2 units.
815 ********************************************************************/
817 smb_ucs2_t *safe_strcpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlength)
822 DEBUG(0,("ERROR: NULL dest in safe_strcpy_w\n"));
831 maxlength /= sizeof(smb_ucs2_t);
833 ucs2_len = strlen_w(src);
835 if (ucs2_len >= maxlength) {
837 DEBUG(0,("ERROR: string overflow by %u bytes in safe_strcpy_w [%.50s]\n",
838 (unsigned int)((ucs2_len-maxlength)*sizeof(smb_ucs2_t)),
839 unicode_to_unix(out,src,sizeof(out))) );
840 ucs2_len = maxlength - 1;
843 memcpy(dest, src, ucs2_len*sizeof(smb_ucs2_t));
848 /*******************************************************************
849 Safe string cat into a string. maxlength includes the terminating zero.
850 maxlength is in ucs2 units.
851 ********************************************************************/
853 smb_ucs2_t *safe_strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength)
855 size_t ucs2_src_len, ucs2_dest_len;
858 DEBUG(0,("ERROR: NULL dest in safe_strcat_w\n"));
865 ucs2_src_len = strlen_w(src);
866 ucs2_dest_len = strlen_w(dest);
868 if (ucs2_src_len + ucs2_dest_len >= maxlength) {
870 int new_len = maxlength - ucs2_dest_len - 1;
871 DEBUG(0,("ERROR: string overflow by %u characters in safe_strcat_w [%.50s]\n",
872 (unsigned int)(sizeof(smb_ucs2_t)*(ucs2_src_len + ucs2_dest_len - maxlength)),
873 unicode_to_unix(out,src,sizeof(out))) );
874 ucs2_src_len = (size_t)(new_len > 0 ? new_len : 0);
877 memcpy(&dest[ucs2_dest_len], src, ucs2_src_len*sizeof(smb_ucs2_t));
878 dest[ucs2_dest_len + ucs2_src_len] = 0;
882 /*******************************************************************
883 Compare the two strings s1 and s2.
884 ********************************************************************/
886 int strcmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
903 /*******************************************************************
904 Compare the first n characters of s1 to s2. len is in ucs2 units.
905 ********************************************************************/
907 int strncmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2, size_t len)
911 for (; len != 0; --len) {
925 /*******************************************************************
926 Search string s2 from s1.
927 ********************************************************************/
929 smb_ucs2_t *strstr_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
931 size_t len = strlen_w(s2);
934 return (smb_ucs2_t *)s1;
938 if (strncmp_w(s1, s2, len) == 0)
939 return (smb_ucs2_t *)s1;
945 /*******************************************************************
946 Search for ucs2 char c from the beginning of s.
947 ********************************************************************/
949 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
953 return (smb_ucs2_t *)s;
959 /*******************************************************************
960 Search for ucs2 char c from the end of s.
961 ********************************************************************/
963 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
965 smb_ucs2_t *retval = 0;
969 retval = (smb_ucs2_t *)s;
975 /*******************************************************************
976 Search token from s1 separated by any ucs2 char of s2.
977 ********************************************************************/
979 smb_ucs2_t *strtok_w(smb_ucs2_t *s1, const smb_ucs2_t *s2)
981 static smb_ucs2_t *s = NULL;
990 for (q = s1; *s1; s1++) {
991 smb_ucs2_t *p = strchr_w(s2, *s1);
1009 /*******************************************************************
1010 Duplicate a ucs2 string.
1011 ********************************************************************/
1013 smb_ucs2_t *strdup_w(const smb_ucs2_t *s)
1015 size_t newlen = (strlen_w(s)+1)*sizeof(smb_ucs2_t);
1016 smb_ucs2_t *newstr = (smb_ucs2_t *)malloc(newlen);
1019 safe_strcpy_w(newstr, s, newlen);
1023 /*******************************************************************
1024 Mapping tables for UNICODE character. Allows toupper/tolower and
1025 isXXX functions to work.
1027 tridge: split into 2 pieces. This saves us 5/6 of the memory
1028 with a small speed penalty
1029 The magic constants are the lower/upper range of the tables two
1031 ********************************************************************/
1036 unsigned char flags;
1037 } smb_unicode_table_t;
1039 #define TABLE1_BOUNDARY 9450
1040 #define TABLE2_BOUNDARY 64256
1042 static smb_unicode_table_t map_table1[] = {
1043 #include "unicode_map_table1.h"
1046 static smb_unicode_table_t map_table2[] = {
1047 #include "unicode_map_table2.h"
1050 static unsigned char map_table_flags(smb_ucs2_t v)
1052 if (v < TABLE1_BOUNDARY) return map_table1[v].flags;
1053 if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].flags;
1057 static smb_ucs2_t map_table_lower(smb_ucs2_t v)
1059 if (v < TABLE1_BOUNDARY) return map_table1[v].lower;
1060 if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].lower;
1064 static smb_ucs2_t map_table_upper(smb_ucs2_t v)
1066 if (v < TABLE1_BOUNDARY) return map_table1[v].upper;
1067 if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].upper;
1071 /*******************************************************************
1072 Is an upper case wchar.
1073 ********************************************************************/
1075 int isupper_w( smb_ucs2_t val)
1077 return (map_table_flags(val) & UNI_UPPER);
1080 /*******************************************************************
1081 Is a lower case wchar.
1082 ********************************************************************/
1084 int islower_w( smb_ucs2_t val)
1086 return (map_table_flags(val) & UNI_LOWER);
1089 /*******************************************************************
1091 ********************************************************************/
1093 int isdigit_w( smb_ucs2_t val)
1095 return (map_table_flags(val) & UNI_DIGIT);
1098 /*******************************************************************
1099 Is a hex digit wchar.
1100 ********************************************************************/
1102 int isxdigit_w( smb_ucs2_t val)
1104 return (map_table_flags(val) & UNI_XDIGIT);
1107 /*******************************************************************
1109 ********************************************************************/
1111 int isspace_w( smb_ucs2_t val)
1113 return (map_table_flags(val) & UNI_SPACE);
1116 /*******************************************************************
1117 Convert a wchar to upper case.
1118 ********************************************************************/
1120 smb_ucs2_t toupper_w( smb_ucs2_t val )
1122 return map_table_upper(val);
1125 /*******************************************************************
1126 Convert a wchar to lower case.
1127 ********************************************************************/
1129 smb_ucs2_t tolower_w( smb_ucs2_t val )
1131 return map_table_lower(val);
1134 static smb_ucs2_t *last_ptr = NULL;
1136 void set_first_token_w(smb_ucs2_t *ptr)
1141 /****************************************************************************
1142 Get the next token from a string, return False if none found
1143 handles double-quotes.
1144 Based on a routine by GJC@VILLAGE.COM.
1145 Extensively modified by Andrew.Tridgell@anu.edu.au
1146 bufsize is in bytes.
1147 ****************************************************************************/
1149 static smb_ucs2_t sep_list[] = { (smb_ucs2_t)' ', (smb_ucs2_t)'\t', (smb_ucs2_t)'\n', (smb_ucs2_t)'\r', 0};
1150 static smb_ucs2_t quotechar = (smb_ucs2_t)'\"';
1152 BOOL next_token_w(smb_ucs2_t **ptr, smb_ucs2_t *buff, smb_ucs2_t *sep, size_t bufsize)
1159 * Convert bufsize to smb_ucs2_t units.
1162 bufsize /= sizeof(smb_ucs2_t);
1172 * Default to simple separators.
1179 * Find the first non sep char.
1182 while(*s && strchr_w(sep,*s))
1193 * Copy over the token.
1196 for (quoted = False; len < bufsize && *s && (quoted || !strchr_w(sep,*s)); s++) {
1197 if (*s == quotechar) {
1205 *ptr = (*s) ? s+1 : s;
1212 /****************************************************************************
1213 Convert list of tokens to array; dependent on above routine.
1214 Uses last_ptr from above - bit of a hack.
1215 ****************************************************************************/
1217 smb_ucs2_t **toktocliplist_w(int *ctok, smb_ucs2_t *sep)
1219 smb_ucs2_t *s=last_ptr;
1221 smb_ucs2_t **ret, **iret;
1226 while(*s && strchr_w(sep,*s))
1238 while(*s && (!strchr_w(sep,*s)))
1240 while(*s && strchr_w(sep,*s))
1247 if (!(ret=iret=malloc(ictok*sizeof(smb_ucs2_t *))))
1261 /*******************************************************************
1262 Case insensitive string compararison.
1263 ********************************************************************/
1265 int StrCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t)
1268 * Compare until we run out of string, either t or s, or find a difference.
1271 while (*s && *t && toupper_w(*s) == toupper_w(*t)) {
1276 return(toupper_w(*s) - toupper_w(*t));
1279 /*******************************************************************
1280 Case insensitive string compararison, length limited.
1282 ********************************************************************/
1284 int StrnCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t, size_t n)
1287 * Compare until we run out of string, either t or s, or chars.
1290 while (n && *s && *t && toupper_w(*s) == toupper_w(*t)) {
1297 * Not run out of chars - strings are different lengths.
1301 return(toupper_w(*s) - toupper_w(*t));
1304 * Identical up to where we run out of chars,
1305 * and strings are same length.
1311 /*******************************************************************
1313 ********************************************************************/
1315 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
1322 return(StrCaseCmp_w(s1,s2)==0);
1325 /*******************************************************************
1326 Compare 2 strings up to and including the nth char. n is in ucs2
1328 ******************************************************************/
1330 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
1334 if (!s1 || !s2 || !n)
1337 return(StrnCaseCmp_w(s1,s2,n)==0);
1340 /*******************************************************************
1341 Compare 2 strings (case sensitive).
1342 ********************************************************************/
1344 BOOL strcsequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2)
1351 return(strcmp_w(s1,s2)==0);
1354 /*******************************************************************
1355 Convert a string to lower case.
1356 ********************************************************************/
1358 void strlower_w(smb_ucs2_t *s)
1367 /*******************************************************************
1368 Convert a string to upper case.
1369 ********************************************************************/
1371 void strupper_w(smb_ucs2_t *s)
1380 /*******************************************************************
1381 Convert a string to "normal" form.
1382 ********************************************************************/
1384 void strnorm_w(smb_ucs2_t *s)
1386 extern int case_default;
1387 if (case_default == CASE_UPPER)
1393 /*******************************************************************
1394 Check if a string is in "normal" case.
1395 ********************************************************************/
1397 BOOL strisnormal_w(smb_ucs2_t *s)
1399 extern int case_default;
1400 if (case_default == CASE_UPPER)
1401 return(!strhaslower_w(s));
1403 return(!strhasupper_w(s));
1406 /****************************************************************************
1408 ****************************************************************************/
1410 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
1419 /*******************************************************************
1420 Skip past some strings in a buffer. n is in bytes.
1421 ********************************************************************/
1423 smb_ucs2_t *skip_string_w(smb_ucs2_t *buf,size_t n)
1426 buf += (strlen_w(buf)*sizeof(smb_ucs2_t)) + 1;
1430 /*******************************************************************
1431 Count the number of characters in a string. Same as strlen_w in
1432 smb_ucs2_t string units.
1433 ********************************************************************/
1435 size_t str_charnum_w(const smb_ucs2_t *s)
1440 /*******************************************************************
1441 Trim the specified elements off the front and back of a string.
1442 ********************************************************************/
1444 BOOL trim_string_w(smb_ucs2_t *s,const smb_ucs2_t *front,const smb_ucs2_t *back)
1447 size_t front_len = (front && *front) ? strlen_w(front) : 0;
1448 size_t back_len = (back && *back) ? strlen_w(back) : 0;
1451 while (front_len && strncmp_w(s, front, front_len) == 0) {
1456 if (!(*p = p[front_len]))
1463 s_len = strlen_w(s);
1464 while ((s_len >= back_len) &&
1465 (strncmp_w(s + s_len - back_len, back, back_len)==0)) {
1467 s[s_len - back_len] = 0;
1468 s_len = strlen_w(s);
1475 /****************************************************************************
1476 Does a string have any uppercase chars in it ?
1477 ****************************************************************************/
1479 BOOL strhasupper_w(const smb_ucs2_t *s)
1489 /****************************************************************************
1490 Does a string have any lowercase chars in it ?
1491 ****************************************************************************/
1493 BOOL strhaslower_w(const smb_ucs2_t *s)
1503 /****************************************************************************
1504 Find the number of 'c' chars in a string.
1505 ****************************************************************************/
1507 size_t count_chars_w(const smb_ucs2_t *s,smb_ucs2_t c)
1519 /*******************************************************************
1520 Return True if a string consists only of one particular character.
1521 ********************************************************************/
1523 BOOL str_is_all_w(const smb_ucs2_t *s,smb_ucs2_t c)
1538 /*******************************************************************
1539 Paranoid strcpy into a buffer of given length (includes terminating
1540 zero. Strips out all but 'a-Z0-9' and replaces with '_'. Deliberately
1541 does *NOT* check for multibyte characters. Don't change it !
1542 maxlength is in ucs2 units.
1543 ********************************************************************/
1545 smb_ucs2_t *alpha_strcpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength)
1550 DEBUG(0,("ERROR: NULL dest in alpha_strcpy_w\n"));
1559 len = strlen_w(src);
1560 if (len >= maxlength)
1561 len = maxlength - 1;
1563 for(i = 0; i < len; i++) {
1564 smb_ucs2_t val = src[i];
1565 if(isupper_w(val) ||islower_w(val) || isdigit_w(val))
1568 dest[i] = (smb_ucs2_t)'_';
1576 /****************************************************************************
1577 Like strncpy but always null terminates. Make sure there is room !
1578 The variable n should always be one less than the available size and is in
1580 ****************************************************************************/
1582 smb_ucs2_t *StrnCpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src,size_t n)
1584 smb_ucs2_t *d = dest;
1592 while (n-- && (*d++ = *src++))
1598 /****************************************************************************
1599 Like strncpy but copies up to the character marker. Always null terminates.
1600 returns a pointer to the character marker in the source string (src).
1602 ****************************************************************************/
1604 smb_ucs2_t *strncpyn_w(smb_ucs2_t *dest, const smb_ucs2_t *src,size_t n, smb_ucs2_t c)
1609 p = strchr_w(src, c);
1612 smb_ucs2_t mbcval[2];
1615 DEBUG(5, ("strncpyn_w: separator character (%s) not found\n",
1616 unicode_to_unix(cval,mbcval,sizeof(cval)) ));
1620 str_len = PTR_DIFF(p, src) + 1;
1621 safe_strcpy_w(dest, src, MIN(n, str_len));
1626 /*************************************************************
1627 Routine to get hex characters and turn them into a 16 byte array.
1628 The array can be variable length, and any non-hex-numeric
1629 characters are skipped. "0xnn" or "0Xnn" is specially catered
1630 for. len is in bytes.
1631 Valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
1632 **************************************************************/
1634 static smb_ucs2_t hexprefix[] = { (smb_ucs2_t)'0', (smb_ucs2_t)'x', 0 };
1635 static smb_ucs2_t hexchars[] = { (smb_ucs2_t)'0', (smb_ucs2_t)'1', (smb_ucs2_t)'2', (smb_ucs2_t)'3',
1636 (smb_ucs2_t)'4', (smb_ucs2_t)'5', (smb_ucs2_t)'6', (smb_ucs2_t)'7',
1637 (smb_ucs2_t)'8', (smb_ucs2_t)'9', (smb_ucs2_t)'A', (smb_ucs2_t)'B',
1638 (smb_ucs2_t)'C', (smb_ucs2_t)'D', (smb_ucs2_t)'E', (smb_ucs2_t)'F', 0 };
1640 size_t strhex_to_str_w(char *p, size_t len, const smb_ucs2_t *strhex)
1643 size_t num_chars = 0;
1644 unsigned char lonybble, hinybble;
1645 smb_ucs2_t *p1 = NULL, *p2 = NULL;
1648 * Convert to smb_ucs2_t units.
1651 len /= sizeof(smb_ucs2_t);
1653 for (i = 0; i < len && strhex[i] != 0; i++) {
1654 if (strnequal_w(hexchars, hexprefix, 2)) {
1655 i++; /* skip two chars */
1659 if (!(p1 = strchr_w(hexchars, toupper_w(strhex[i]))))
1662 i++; /* next hex digit */
1664 if (!(p2 = strchr_w(hexchars, toupper_w(strhex[i]))))
1667 /* get the two nybbles */
1668 hinybble = (PTR_DIFF(p1, hexchars)/sizeof(smb_ucs2_t));
1669 lonybble = (PTR_DIFF(p2, hexchars)/sizeof(smb_ucs2_t));
1671 p[num_chars] = (hinybble << 4) | lonybble;
1680 /****************************************************************************
1681 Check if a string is part of a list.
1682 ****************************************************************************/
1684 BOOL in_list_w(smb_ucs2_t *s,smb_ucs2_t *list,BOOL casesensitive)
1692 while (next_token_w(&p,tok,LIST_SEP_W,sizeof(tok))) {
1693 if (casesensitive) {
1694 if (strcmp_w(tok,s) == 0)
1697 if (StrCaseCmp_w(tok,s) == 0)
1704 /* This is used to prevent lots of mallocs of size 2 */
1705 static smb_ucs2_t *null_string = NULL;
1707 /****************************************************************************
1708 Set a string value, allocing the space for the string.
1709 ****************************************************************************/
1711 BOOL string_init_w(smb_ucs2_t **dest,const smb_ucs2_t *src)
1716 if((null_string = (smb_ucs2_t *)malloc(sizeof(smb_ucs2_t))) == NULL) {
1717 DEBUG(0,("string_init_w: malloc fail for null_string.\n"));
1729 *dest = null_string;
1731 (*dest) = (smb_ucs2_t *)malloc(sizeof(smb_ucs2_t)*(l+1));
1732 if ((*dest) == NULL) {
1733 DEBUG(0,("Out of memory in string_init_w\n"));
1737 wpstrcpy(*dest,src);
1742 /****************************************************************************
1743 Free a string value.
1744 ****************************************************************************/
1746 void string_free_w(smb_ucs2_t **s)
1750 if (*s == null_string)
1757 /****************************************************************************
1758 Set a string value, allocing the space for the string, and deallocating any
1760 ****************************************************************************/
1762 BOOL string_set_w(smb_ucs2_t **dest,const smb_ucs2_t *src)
1764 string_free_w(dest);
1766 return(string_init_w(dest,src));
1769 /****************************************************************************
1770 Substitute a string for a pattern in another string. Make sure there is
1773 This routine looks for pattern in s and replaces it with
1774 insert. It may do multiple replacements.
1776 Any of " ; ' $ or ` in the insert string are replaced with _
1777 if len==0 then no length check is performed
1778 len is in ucs2 units.
1779 ****************************************************************************/
1781 void string_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t *insert, size_t len)
1784 ssize_t ls,lp,li, i;
1786 if (!insert || !pattern || !s)
1789 ls = (ssize_t)strlen_w(s);
1790 lp = (ssize_t)strlen_w(pattern);
1791 li = (ssize_t)strlen_w(insert);
1796 while (lp <= ls && (p = strstr_w(s,pattern))) {
1797 if (len && (ls + (li-lp) >= len)) {
1799 DEBUG(0,("ERROR: string overflow by %d in string_sub_w(%.50s, %d)\n",
1800 (int)(sizeof(smb_ucs2_t)*(ls + (li-lp) - len)),
1801 unicode_to_unix(out,pattern,sizeof(out)), (int)len*sizeof(smb_ucs2_t)));
1805 memmove(p+li,p+lp,sizeof(smb_ucs2_t)*(strlen_w(p+lp)+1));
1807 for (i=0;i<li;i++) {
1808 switch (insert[i]) {
1809 case (smb_ucs2_t)'`':
1810 case (smb_ucs2_t)'"':
1811 case (smb_ucs2_t)'\'':
1812 case (smb_ucs2_t)';':
1813 case (smb_ucs2_t)'$':
1814 case (smb_ucs2_t)'%':
1815 case (smb_ucs2_t)'\r':
1816 case (smb_ucs2_t)'\n':
1817 p[i] = (smb_ucs2_t)'_';
1828 void fstring_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t *insert)
1830 string_sub_w(s, pattern, insert, sizeof(wfstring));
1833 void pstring_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,smb_ucs2_t *insert)
1835 string_sub_w(s, pattern, insert, sizeof(wpstring));
1838 /****************************************************************************
1839 Similar to string_sub() but allows for any character to be substituted.
1841 if len==0 then no length check is performed.
1842 ****************************************************************************/
1844 void all_string_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t *insert, size_t len)
1849 if (!insert || !pattern || !s)
1852 ls = (ssize_t)strlen_w(s);
1853 lp = (ssize_t)strlen_w(pattern);
1854 li = (ssize_t)strlen_w(insert);
1859 while (lp <= ls && (p = strstr_w(s,pattern))) {
1860 if (len && (ls + (li-lp) >= len)) {
1862 DEBUG(0,("ERROR: string overflow by %d in all_string_sub_w(%.50s, %d)\n",
1863 (int)(sizeof(smb_ucs2_t)*(ls + (li-lp) - len)),
1864 unicode_to_unix(out,pattern,sizeof(out)), (int)len*sizeof(smb_ucs2_t)));
1868 memmove(p+li,p+lp,sizeof(smb_ucs2_t)*(strlen_w(p+lp)+1));
1870 memcpy(p, insert, li*sizeof(smb_ucs2_t));
1876 /****************************************************************************
1877 Splits out the front and back at a separator.
1878 ****************************************************************************/
1880 void split_at_last_component_w(smb_ucs2_t *path, smb_ucs2_t *front, smb_ucs2_t sep, smb_ucs2_t *back)
1882 smb_ucs2_t *p = strrchr_w(path, sep);
1888 wpstrcpy(front, path);
1892 wpstrcpy(back, p+1);
1893 *p = (smb_ucs2_t)'\\';
1901 /****************************************************************************
1902 Write an octal as a string.
1903 ****************************************************************************/
1905 smb_ucs2_t *octal_string_w(int i)
1907 static smb_ucs2_t wret[64];
1911 slprintf(ret, sizeof(ret), "-1");
1913 slprintf(ret, sizeof(ret), "0%o", i);
1914 return unix_to_unicode(wret, ret, sizeof(wret));
1918 /****************************************************************************
1919 Truncate a string at a specified length.
1920 length is in ucs2 units.
1921 ****************************************************************************/
1923 smb_ucs2_t *string_truncate_w(smb_ucs2_t *s, size_t length)
1925 if (s && strlen_w(s) > length)