From 8c3dcf128bb1a1c8d0b7a62a7c42a17457730024 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 5 Nov 1998 16:48:35 +0000 Subject: split string and unicode string routines into these files. these are *not* going to be added into the Makefile.in yet so they still also exist in util.c. (This used to be commit 3f5feda6749dace6bc51fb0e02b16d2b72a930b8) --- source3/lib/util_unistr.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 source3/lib/util_unistr.c (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c new file mode 100644 index 0000000000..2365090f24 --- /dev/null +++ b/source3/lib/util_unistr.c @@ -0,0 +1,186 @@ +/* + Unix SMB/Netbios implementation. + Version 1.9. + Samba utility functions + Copyright (C) Andrew Tridgell 1992-1998 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" + +/******************************************************************* +write a string in unicoode format +********************************************************************/ +int PutUniCode(char *dst,char *src) +{ + int ret = 0; + while (*src) { + dst[ret++] = src[0]; + dst[ret++] = 0; + src++; + } + dst[ret++]=0; + dst[ret++]=0; + return(ret); +} + +/******************************************************************* +skip past some unicode strings in a buffer +********************************************************************/ +char *skip_unicode_string(char *buf,int n) +{ + while (n--) + { + while (*buf) + buf += 2; + buf += 2; + } + return(buf); +} + +/******************************************************************* +Return a ascii version of a unicode string +Hack alert: uses fixed buffer(s) and only handles ascii strings +********************************************************************/ +#define MAXUNI 1024 +char *unistrn2(uint16 *buf, int len) +{ + static char lbufs[8][MAXUNI]; + static int nexti; + char *lbuf = lbufs[nexti]; + char *p; + + nexti = (nexti+1)%8; + + for (p = lbuf; *buf && p-lbuf < MAXUNI-2 && len > 0; len--, p++, buf++) + { + *p = *buf; + } + + *p = 0; + return lbuf; +} + +static char lbufs[8][MAXUNI]; +static int nexti; +/******************************************************************* +Return a ascii version of a unicode string +Hack alert: uses fixed buffer(s) and only handles ascii strings +********************************************************************/ +#define MAXUNI 1024 +char *unistr2(uint16 *buf) +{ + char *lbuf = lbufs[nexti]; + char *p; + + nexti = (nexti+1)%8; + + for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf++) + { + *p = *buf; + } + + *p = 0; + return lbuf; +} + +/******************************************************************* +Return a ascii version of a unicode string +********************************************************************/ +char *unistr2_to_str(UNISTR2 *str) +{ + char *lbuf = lbufs[nexti]; + char *p; + uint16 *buf = str->buffer; + int max_size = MIN(sizeof(str->buffer)-2, str->uni_str_len); + + nexti = (nexti+1)%8; + + for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++) + { + *p = *buf; + } + + *p = 0; + return lbuf; +} + +/******************************************************************* +create a null-terminated unicode string from a null-terminated ascii string. +return number of unicode chars copied, excluding the null character. + +only handles ascii strings +********************************************************************/ +#define MAXUNI 1024 +int struni2(uint16 *p, char *buf) +{ + int len = 0; + + if (p == NULL) return 0; + + if (buf != NULL) + { + for (; *buf && len < MAXUNI-2; len++, p++, buf++) + { + *p = *buf; + } + } + + *p = 0; + + return len; +} + +/******************************************************************* +Return a ascii version of a unicode string +Hack alert: uses fixed buffer(s) and only handles ascii strings +********************************************************************/ +#define MAXUNI 1024 +char *unistr(char *buf) +{ + char *lbuf = lbufs[nexti]; + char *p; + + nexti = (nexti+1)%8; + + for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf += 2) + { + *p = *buf; + } + *p = 0; + return lbuf; +} + + +/******************************************************************* +strcpy for unicode strings. returns length (in num of wide chars) +********************************************************************/ +int unistrcpy(char *dst, char *src) +{ + int num_wchars = 0; + + while (*src) + { + *dst++ = *src++; + *dst++ = *src++; + num_wchars++; + } + *dst++ = 0; + *dst++ = 0; + + return num_wchars; +} + -- cgit From 1e1c2ec93c204e6fd3ebba6dfb11e4fbc136e10c Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Tue, 10 Nov 1998 19:05:00 +0000 Subject: rpcclient registry commands. (This used to be commit 36fcb4a6e643a05d06a2a273d74318fee7f2c647) --- source3/lib/util_unistr.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 2365090f24..49fb729267 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -118,6 +118,70 @@ char *unistr2_to_str(UNISTR2 *str) return lbuf; } +/******************************************************************* +Return a number stored in a buffer +********************************************************************/ +uint32 buffer2_to_uint32(BUFFER2 *str) +{ + if (str->buf_len == 4) + { + return IVAL(str->buffer, 0); + } + else + { + return 0; + } +} + +/******************************************************************* +Return a ascii version of a NOTunicode string +********************************************************************/ +char *buffer2_to_str(BUFFER2 *str) +{ + char *lbuf = lbufs[nexti]; + char *p; + uint16 *buf = str->buffer; + int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2); + + nexti = (nexti+1)%8; + + for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++) + { + *p = *buf; + } + + *p = 0; + return lbuf; +} + +/******************************************************************* +Return a ascii version of a NOTunicode string +********************************************************************/ +char *buffer2_to_multistr(BUFFER2 *str) +{ + char *lbuf = lbufs[nexti]; + char *p; + uint16 *buf = str->buffer; + int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2); + + nexti = (nexti+1)%8; + + for (p = lbuf; p-lbuf < max_size; p++, buf++) + { + if (*buf == 0) + { + *p = ' '; + } + else + { + *p = *buf; + } + } + + *p = 0; + return lbuf; +} + /******************************************************************* create a null-terminated unicode string from a null-terminated ascii string. return number of unicode chars copied, excluding the null character. -- cgit From d85dcf86d59c14cb624bbb69b658fc6aba842593 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 12 Nov 1998 06:12:19 +0000 Subject: largely rewrote smbpasswd so that the code is understandable. This should allow us to call a function in swat rather than piping to smbpasswd. while doing this I also fixed quite a few "const char *" versus "char *" issues that cropped up while using const to track down bugs in the code. This led to changes in several generic functions. The smbpasswd changes should be correct but they have not been extensively tested. At least if I have introduced bugs then we should be able to fix them more easily than before. (This used to be commit 713864dd0322ae2ae2d83e333d85be35a7eed4ec) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 49fb729267..c58820cfec 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -189,7 +189,7 @@ return number of unicode chars copied, excluding the null character. only handles ascii strings ********************************************************************/ #define MAXUNI 1024 -int struni2(uint16 *p, char *buf) +int struni2(uint16 *p, const char *buf) { int len = 0; -- cgit From 99a9b0f7c4f85f46102457cf4707e8948b77fb3f Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Mon, 8 Feb 1999 23:40:49 +0000 Subject: UNICODE byte ordering issue: typecast to uint16* replaced with SSVAL() (This used to be commit 9084b7e33dfe717bd8d5604ee71d137e3baef0f5) --- source3/lib/util_unistr.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c58820cfec..50bb73f4fb 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -56,7 +56,7 @@ Return a ascii version of a unicode string Hack alert: uses fixed buffer(s) and only handles ascii strings ********************************************************************/ #define MAXUNI 1024 -char *unistrn2(uint16 *buf, int len) +char *unistrn2(char *buf, int len) { static char lbufs[8][MAXUNI]; static int nexti; @@ -65,9 +65,9 @@ char *unistrn2(uint16 *buf, int len) nexti = (nexti+1)%8; - for (p = lbuf; *buf && p-lbuf < MAXUNI-2 && len > 0; len--, p++, buf++) + for (p = lbuf; *buf && p-lbuf < MAXUNI-2 && len > 0; len--, p++, buf+=2) { - *p = *buf; + SSVAL(p, 0, *buf); } *p = 0; @@ -189,7 +189,7 @@ return number of unicode chars copied, excluding the null character. only handles ascii strings ********************************************************************/ #define MAXUNI 1024 -int struni2(uint16 *p, const char *buf) +int struni2(char *p, const char *buf) { int len = 0; @@ -197,9 +197,9 @@ int struni2(uint16 *p, const char *buf) if (buf != NULL) { - for (; *buf && len < MAXUNI-2; len++, p++, buf++) + for (; *buf && len < MAXUNI-2; len++, p += 2, buf++) { - *p = *buf; + SSVAL(p, 0, *buf); } } -- cgit From 8b6b6b57b54aeafb915cf99e5610941ee1d464b8 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Wed, 10 Feb 1999 22:30:47 +0000 Subject: use jeremy's versions of the UNICODE routines. (This used to be commit c5109ff782be8774db47a92b48ca6335ec8d6065) --- source3/lib/util_unistr.c | 88 +++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 38 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 50bb73f4fb..5e73fe6ada 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -21,25 +21,31 @@ #include "includes.h" +#ifndef MAXUNI +#define MAXUNI 1024 +#endif + /******************************************************************* -write a string in unicoode format +write a string in (little-endian) unicoode format ********************************************************************/ + int PutUniCode(char *dst,char *src) { int ret = 0; while (*src) { - dst[ret++] = src[0]; - dst[ret++] = 0; + SSVAL(dst,ret,(*src) & 0xFF); + ret += 2; src++; } - dst[ret++]=0; - dst[ret++]=0; + SSVAL(dst,ret,0); + ret += 2; return(ret); } /******************************************************************* skip past some unicode strings in a buffer ********************************************************************/ + char *skip_unicode_string(char *buf,int n) { while (n--) @@ -52,11 +58,11 @@ char *skip_unicode_string(char *buf,int n) } /******************************************************************* -Return a ascii version of a unicode string +Return a ascii version of a little-endian unicode string. Hack alert: uses fixed buffer(s) and only handles ascii strings ********************************************************************/ -#define MAXUNI 1024 -char *unistrn2(char *buf, int len) + +char *unistrn2(uint16 *src, int len) { static char lbufs[8][MAXUNI]; static int nexti; @@ -65,9 +71,9 @@ char *unistrn2(char *buf, int len) nexti = (nexti+1)%8; - for (p = lbuf; *buf && p-lbuf < MAXUNI-2 && len > 0; len--, p++, buf+=2) + for (p = lbuf; *src && p-lbuf < MAXUNI-2 && len > 0; len--, src++) { - SSVAL(p, 0, *buf); + *p++ = (*src & 0xff); } *p = 0; @@ -76,21 +82,22 @@ char *unistrn2(char *buf, int len) static char lbufs[8][MAXUNI]; static int nexti; + /******************************************************************* -Return a ascii version of a unicode string +Return a ascii version of a little-endian unicode string. Hack alert: uses fixed buffer(s) and only handles ascii strings ********************************************************************/ -#define MAXUNI 1024 -char *unistr2(uint16 *buf) + +char *unistr2(uint16 *src) { char *lbuf = lbufs[nexti]; char *p; nexti = (nexti+1)%8; - for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf++) + for (p = lbuf; *src && p-lbuf < MAXUNI-2; p++, src++) { - *p = *buf; + *p = (*src & 0xff); } *p = 0; @@ -98,20 +105,21 @@ char *unistr2(uint16 *buf) } /******************************************************************* -Return a ascii version of a unicode string +Return a ascii version of a little-endian unicode string ********************************************************************/ + char *unistr2_to_str(UNISTR2 *str) { char *lbuf = lbufs[nexti]; char *p; - uint16 *buf = str->buffer; + uint16 *src = str->buffer; int max_size = MIN(sizeof(str->buffer)-2, str->uni_str_len); nexti = (nexti+1)%8; - for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++) + for (p = lbuf; *src && p-lbuf < max_size; p++, src++) { - *p = *buf; + *p = (*src & 0xff); } *p = 0; @@ -121,6 +129,7 @@ char *unistr2_to_str(UNISTR2 *str) /******************************************************************* Return a number stored in a buffer ********************************************************************/ + uint32 buffer2_to_uint32(BUFFER2 *str) { if (str->buf_len == 4) @@ -136,18 +145,19 @@ uint32 buffer2_to_uint32(BUFFER2 *str) /******************************************************************* Return a ascii version of a NOTunicode string ********************************************************************/ + char *buffer2_to_str(BUFFER2 *str) { char *lbuf = lbufs[nexti]; char *p; - uint16 *buf = str->buffer; + uint16 *src = str->buffer; int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2); nexti = (nexti+1)%8; - for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++) + for (p = lbuf; *src && p-lbuf < max_size; p++, src++) { - *p = *buf; + *p = (*src & 0xff); } *p = 0; @@ -157,24 +167,25 @@ char *buffer2_to_str(BUFFER2 *str) /******************************************************************* Return a ascii version of a NOTunicode string ********************************************************************/ + char *buffer2_to_multistr(BUFFER2 *str) { char *lbuf = lbufs[nexti]; char *p; - uint16 *buf = str->buffer; + uint16 *src = str->buffer; int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2); nexti = (nexti+1)%8; - for (p = lbuf; p-lbuf < max_size; p++, buf++) + for (p = lbuf; p-lbuf < max_size; p++, src++) { - if (*buf == 0) + if (*src == 0) { *p = ' '; } else { - *p = *buf; + *p = (*src & 0xff); } } @@ -185,34 +196,35 @@ char *buffer2_to_multistr(BUFFER2 *str) /******************************************************************* create a null-terminated unicode string from a null-terminated ascii string. return number of unicode chars copied, excluding the null character. - only handles ascii strings +Unicode strings created are in little-endian format. ********************************************************************/ -#define MAXUNI 1024 -int struni2(char *p, const char *buf) + +int struni2(char *dst, const char *src) { - int len = 0; + size_t len = 0; - if (p == NULL) return 0; + if (dst == NULL) + return 0; - if (buf != NULL) + if (src != NULL) { - for (; *buf && len < MAXUNI-2; len++, p += 2, buf++) + for (; *src && len < MAXUNI-2; len++, dst +=2, src++) { - SSVAL(p, 0, *buf); + SSVAL(dst,0,(*src) & 0xFF); } } - *p = 0; + SSVAL(dst,0,0); return len; } /******************************************************************* -Return a ascii version of a unicode string +Return a ascii version of a little-endian unicode string. Hack alert: uses fixed buffer(s) and only handles ascii strings ********************************************************************/ -#define MAXUNI 1024 + char *unistr(char *buf) { char *lbuf = lbufs[nexti]; @@ -232,6 +244,7 @@ char *unistr(char *buf) /******************************************************************* strcpy for unicode strings. returns length (in num of wide chars) ********************************************************************/ + int unistrcpy(char *dst, char *src) { int num_wchars = 0; @@ -247,4 +260,3 @@ int unistrcpy(char *dst, char *src) return num_wchars; } - -- cgit From c6d16eea4394ff1c4d12cb435eebb0686b5ee736 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 11 Feb 1999 18:50:13 +0000 Subject: the UNICODE issue... (This used to be commit 73db80f34183324845407b00f58462ff2d7b47ea) --- source3/lib/util_unistr.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5e73fe6ada..641f891f83 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -193,6 +193,32 @@ char *buffer2_to_multistr(BUFFER2 *str) return lbuf; } +/******************************************************************* +create a null-terminated unicode string from a null-terminated ascii string. +return number of unicode chars copied, excluding the null character. +only handles ascii strings +Unicode strings created are in little-endian format. +********************************************************************/ +int str_to_unistr16(uint16 *dst, const char *src) +{ + size_t len = 0; + + if (dst == NULL) + return 0; + + if (src != NULL) + { + for (; *src && len < MAXUNI-2; len++, dst++, src++) + { + *dst = *src; + } + } + + *dst = 0; + + return len; +} + /******************************************************************* create a null-terminated unicode string from a null-terminated ascii string. return number of unicode chars copied, excluding the null character. @@ -200,7 +226,7 @@ only handles ascii strings Unicode strings created are in little-endian format. ********************************************************************/ -int struni2(char *dst, const char *src) +int str_to_unistr8(char *dst, const char *src) { size_t len = 0; -- cgit From f38bfc7d9aa07c4e21448aa846956bd89a259a65 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 11 Feb 1999 22:12:49 +0000 Subject: UNICODE issues. (This used to be commit 6a437cfb33f24913e0c1f8484c0b08ef317e513b) --- source3/lib/util_unistr.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 641f891f83..2e908cbd7a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -62,7 +62,7 @@ Return a ascii version of a little-endian unicode string. Hack alert: uses fixed buffer(s) and only handles ascii strings ********************************************************************/ -char *unistrn2(uint16 *src, int len) +char *unistrn2(char *src, int len) { static char lbufs[8][MAXUNI]; static int nexti; @@ -71,9 +71,9 @@ char *unistrn2(uint16 *src, int len) nexti = (nexti+1)%8; - for (p = lbuf; *src && p-lbuf < MAXUNI-2 && len > 0; len--, src++) + for (p = lbuf; *src && p-lbuf < MAXUNI-2 && len > 0; len--, src += 2) { - *p++ = (*src & 0xff); + *p++ = SVAL(src, 0) & 0xFF; } *p = 0; @@ -88,16 +88,16 @@ Return a ascii version of a little-endian unicode string. Hack alert: uses fixed buffer(s) and only handles ascii strings ********************************************************************/ -char *unistr2(uint16 *src) +char *unistr2(char *src) { char *lbuf = lbufs[nexti]; char *p; nexti = (nexti+1)%8; - for (p = lbuf; *src && p-lbuf < MAXUNI-2; p++, src++) + for (p = lbuf; *src && p-lbuf < MAXUNI-2; p++, src += 2) { - *p = (*src & 0xff); + *p = SVAL(src, 0) & 0xFF; } *p = 0; @@ -260,7 +260,7 @@ char *unistr(char *buf) for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf += 2) { - *p = *buf; + *p = SVAL(buf, 0) & 0xFF; } *p = 0; return lbuf; -- cgit From fd96929ec1fa27e0affd4c4e9ba307c4ee30b978 Mon Sep 17 00:00:00 2001 From: Matthew Chapman Date: Fri, 12 Feb 1999 00:16:09 +0000 Subject: UNICODE cleanup (see lib/util_unistr.c). No more ugly static library buffers and all functions take a destination string length (especially unistrcpy was rather dangerous; we were only saved by the fact that datagrams are limited in size). (This used to be commit a1d39af1ce1d451b811dbd7c2ba391214851b87e) --- source3/lib/util_unistr.c | 310 +++++++++++++++++----------------------------- 1 file changed, 114 insertions(+), 196 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 2e908cbd7a..28c96bdc38 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -21,268 +21,186 @@ #include "includes.h" -#ifndef MAXUNI -#define MAXUNI 1024 -#endif - /******************************************************************* -write a string in (little-endian) unicoode format -********************************************************************/ + Put an ASCII string into a UNICODE buffer (little endian). + ********************************************************************/ -int PutUniCode(char *dst,char *src) +char *ascii_to_unibuf(char *dest, const char *src, int maxlen) { - int ret = 0; - while (*src) { - SSVAL(dst,ret,(*src) & 0xFF); - ret += 2; - src++; - } - SSVAL(dst,ret,0); - ret += 2; - return(ret); -} - -/******************************************************************* -skip past some unicode strings in a buffer -********************************************************************/ - -char *skip_unicode_string(char *buf,int n) -{ - while (n--) - { - while (*buf) - buf += 2; - buf += 2; - } - return(buf); -} - -/******************************************************************* -Return a ascii version of a little-endian unicode string. -Hack alert: uses fixed buffer(s) and only handles ascii strings -********************************************************************/ - -char *unistrn2(char *src, int len) -{ - static char lbufs[8][MAXUNI]; - static int nexti; - char *lbuf = lbufs[nexti]; - char *p; - - nexti = (nexti+1)%8; - - for (p = lbuf; *src && p-lbuf < MAXUNI-2 && len > 0; len--, src += 2) - { - *p++ = SVAL(src, 0) & 0xFF; + char *destend = dest + maxlen - 1; + register char c; + + while (dest < destend) { + *(dest++) = c = *(src++); + *(dest++) = 0; + if (c == 0) { + break; + } } - *p = 0; - return lbuf; + return dest; } -static char lbufs[8][MAXUNI]; -static int nexti; /******************************************************************* -Return a ascii version of a little-endian unicode string. -Hack alert: uses fixed buffer(s) and only handles ascii strings -********************************************************************/ + Pull an ASCII string out of a UNICODE buffer (little endian). + ********************************************************************/ -char *unistr2(char *src) +void unibuf_to_ascii(char *dest, const char *src, int maxlen) { - char *lbuf = lbufs[nexti]; - char *p; + char *destend = dest + maxlen; + register char c; - nexti = (nexti+1)%8; - - for (p = lbuf; *src && p-lbuf < MAXUNI-2; p++, src += 2) - { - *p = SVAL(src, 0) & 0xFF; + while (dest < destend) { + *(dest++) = c = *(src++); + if ((c == 0) && (*src == 0)) { + break; + } + src++; } - - *p = 0; - return lbuf; } + /******************************************************************* -Return a ascii version of a little-endian unicode string -********************************************************************/ + Put an ASCII string into a UNICODE array (uint16's). + ********************************************************************/ -char *unistr2_to_str(UNISTR2 *str) +void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) { - char *lbuf = lbufs[nexti]; - char *p; - uint16 *src = str->buffer; - int max_size = MIN(sizeof(str->buffer)-2, str->uni_str_len); + uint16 *destend = dest + maxlen; + register char c; - nexti = (nexti+1)%8; + while (dest < destend) { + c = *(src++); + *(dest++) = (uint16)c; - for (p = lbuf; *src && p-lbuf < max_size; p++, src++) - { - *p = (*src & 0xff); + if (c == 0) { + break; + } } - - *p = 0; - return lbuf; } -/******************************************************************* -Return a number stored in a buffer -********************************************************************/ - -uint32 buffer2_to_uint32(BUFFER2 *str) -{ - if (str->buf_len == 4) - { - return IVAL(str->buffer, 0); - } - else - { - return 0; - } -} /******************************************************************* -Return a ascii version of a NOTunicode string -********************************************************************/ + Pull an ASCII string out of a UNICODE array (uint16's). + ********************************************************************/ -char *buffer2_to_str(BUFFER2 *str) +void unistr_to_ascii(char *dest, const uint16 *src, int len) { - char *lbuf = lbufs[nexti]; - char *p; - uint16 *src = str->buffer; - int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2); + char *destend = dest + len; + register uint16 c; - nexti = (nexti+1)%8; + while (dest < destend) { + c = *(src++); + *(dest++) = (char)c; - for (p = lbuf; *src && p-lbuf < max_size; p++, src++) - { - *p = (*src & 0xff); + if (c == 0) { + break; + } } - - *p = 0; - return lbuf; } + /******************************************************************* -Return a ascii version of a NOTunicode string -********************************************************************/ + Convert a UNISTR2 structure to an ASCII string + ********************************************************************/ -char *buffer2_to_multistr(BUFFER2 *str) +void unistr2_to_ascii(char *dest, const UNISTR2 *str, int destlen) { - char *lbuf = lbufs[nexti]; - char *p; - uint16 *src = str->buffer; - int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2); + char *destend; + const uint16 *src; + int len; + register uint16 c; - nexti = (nexti+1)%8; + src = str->buffer; + len = MIN(str->uni_str_len, destlen); + destend = dest + len; - for (p = lbuf; p-lbuf < max_size; p++, src++) - { - if (*src == 0) - { - *p = ' '; - } - else - { - *p = (*src & 0xff); + while (dest < destend) { + c = *(src++); + *(dest++) = (char)c; + + if (c == 0) { + break; } } - - *p = 0; - return lbuf; } -/******************************************************************* -create a null-terminated unicode string from a null-terminated ascii string. -return number of unicode chars copied, excluding the null character. -only handles ascii strings -Unicode strings created are in little-endian format. -********************************************************************/ -int str_to_unistr16(uint16 *dst, const char *src) -{ - size_t len = 0; - if (dst == NULL) - return 0; +/******************************************************************* + Skip a UNICODE string in a little endian buffer. + ********************************************************************/ - if (src != NULL) - { - for (; *src && len < MAXUNI-2; len++, dst++, src++) - { - *dst = *src; - } - } +char *skip_unibuf(char *srcbuf, int len) +{ + uint16 *src = (uint16 *)srcbuf; + uint16 *srcend = src + len/2; - *dst = 0; + while ((src < srcend) && (*(src++) != 0)) + ; - return len; + return (char *)src; } + /******************************************************************* -create a null-terminated unicode string from a null-terminated ascii string. -return number of unicode chars copied, excluding the null character. -only handles ascii strings -Unicode strings created are in little-endian format. -********************************************************************/ + UNICODE strcpy between buffers. + ********************************************************************/ -int str_to_unistr8(char *dst, const char *src) +char *uni_strncpy(char *destbuf, const char *srcbuf, int len) { - size_t len = 0; - - if (dst == NULL) - return 0; - - if (src != NULL) - { - for (; *src && len < MAXUNI-2; len++, dst +=2, src++) - { - SSVAL(dst,0,(*src) & 0xFF); + const uint16 *src = (uint16 *)srcbuf; + uint16 *dest = (uint16 *)destbuf; + uint16 *destend = dest + len/2; + register uint16 c; + + while (dest < destend) { + *(dest++) = c = *(src++); + if (c == 0) { + break; } } - SSVAL(dst,0,0); - - return len; + return (char *)dest; } + /******************************************************************* -Return a ascii version of a little-endian unicode string. -Hack alert: uses fixed buffer(s) and only handles ascii strings -********************************************************************/ + Return a number stored in a buffer + ********************************************************************/ -char *unistr(char *buf) +uint32 buffer2_to_uint32(const BUFFER2 *str) { - char *lbuf = lbufs[nexti]; - char *p; - - nexti = (nexti+1)%8; - - for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf += 2) + if (str->buf_len == 4) + { + return IVAL(str->buffer, 0); + } + else { - *p = SVAL(buf, 0) & 0xFF; + return 0; } - *p = 0; - return lbuf; } /******************************************************************* -strcpy for unicode strings. returns length (in num of wide chars) -********************************************************************/ + Convert a 'multi-string' buffer to space-separated ASCII. + ********************************************************************/ -int unistrcpy(char *dst, char *src) +void buffer2_to_multistr(char *dest, const BUFFER2 *str, int destlen) { - int num_wchars = 0; - - while (*src) - { - *dst++ = *src++; - *dst++ = *src++; - num_wchars++; + char *destend; + const uint16 *src; + int len; + register uint16 c; + + src = str->buffer; + len = MIN(str->buf_len/2, destlen); + destend = dest + len - 1; + + while (dest < destend) { + c = *(src++); + *(dest++) = (c == 0) ? ' ' : (char)c; } - *dst++ = 0; - *dst++ = 0; - return num_wchars; + *dest = 0; } -- cgit From b8082d0840f97aa9b48de3f30dbc1225786467ce Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Fri, 12 Feb 1999 15:35:27 +0000 Subject: const cast issues. [p.s - tidy work, matt!] (This used to be commit dad5baef194b18c674c0d908a0e0714c0a1aefa4) --- source3/lib/util_unistr.c | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 28c96bdc38..b126feb7ff 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -30,10 +30,12 @@ char *ascii_to_unibuf(char *dest, const char *src, int maxlen) char *destend = dest + maxlen - 1; register char c; - while (dest < destend) { + while (dest < destend) + { *(dest++) = c = *(src++); *(dest++) = 0; - if (c == 0) { + if (c == 0) + { break; } } @@ -51,9 +53,11 @@ void unibuf_to_ascii(char *dest, const char *src, int maxlen) char *destend = dest + maxlen; register char c; - while (dest < destend) { + while (dest < destend) + { *(dest++) = c = *(src++); - if ((c == 0) && (*src == 0)) { + if ((c == 0) && (*src == 0)) + { break; } src++; @@ -70,11 +74,13 @@ void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) uint16 *destend = dest + maxlen; register char c; - while (dest < destend) { + while (dest < destend) + { c = *(src++); *(dest++) = (uint16)c; - if (c == 0) { + if (c == 0) + { break; } } @@ -90,11 +96,13 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) char *destend = dest + len; register uint16 c; - while (dest < destend) { + while (dest < destend) + { c = *(src++); *(dest++) = (char)c; - if (c == 0) { + if (c == 0) + { break; } } @@ -116,11 +124,13 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, int destlen) len = MIN(str->uni_str_len, destlen); destend = dest + len; - while (dest < destend) { + while (dest < destend) + { c = *(src++); *(dest++) = (char)c; - if (c == 0) { + if (c == 0) + { break; } } @@ -137,7 +147,8 @@ char *skip_unibuf(char *srcbuf, int len) uint16 *srcend = src + len/2; while ((src < srcend) && (*(src++) != 0)) - ; + { + } return (char *)src; } @@ -149,14 +160,16 @@ char *skip_unibuf(char *srcbuf, int len) char *uni_strncpy(char *destbuf, const char *srcbuf, int len) { - const uint16 *src = (uint16 *)srcbuf; + const uint16 *src = (const uint16 *)srcbuf; uint16 *dest = (uint16 *)destbuf; uint16 *destend = dest + len/2; register uint16 c; - while (dest < destend) { + while (dest < destend) + { *(dest++) = c = *(src++); - if (c == 0) { + if (c == 0) + { break; } } @@ -173,7 +186,8 @@ uint32 buffer2_to_uint32(const BUFFER2 *str) { if (str->buf_len == 4) { - return IVAL(str->buffer, 0); + const char *src = (const char*)str->buffer; + return IVAL(src, 0); } else { @@ -197,7 +211,8 @@ void buffer2_to_multistr(char *dest, const BUFFER2 *str, int destlen) len = MIN(str->buf_len/2, destlen); destend = dest + len - 1; - while (dest < destend) { + while (dest < destend) + { c = *(src++); *(dest++) = (c == 0) ? ' ' : (char)c; } -- cgit From 0baa42bdd751b81ca6d17d1b5377a4e345936cad Mon Sep 17 00:00:00 2001 From: Matthew Chapman Date: Mon, 15 Feb 1999 05:31:52 +0000 Subject: Always null-terminate strings. (This used to be commit b314430b2102e47529b093b1b98b5b6f3b6ea74f) --- source3/lib/util_unistr.c | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index b126feb7ff..01ec262f60 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -27,19 +27,23 @@ char *ascii_to_unibuf(char *dest, const char *src, int maxlen) { - char *destend = dest + maxlen - 1; + char *destend = dest + maxlen; register char c; while (dest < destend) { - *(dest++) = c = *(src++); - *(dest++) = 0; + c = *(src++); if (c == 0) { break; } + + *(dest++) = c; + *(dest++) = 0; } + *dest++ = 0; + *dest++ = 0; return dest; } @@ -55,13 +59,17 @@ void unibuf_to_ascii(char *dest, const char *src, int maxlen) while (dest < destend) { - *(dest++) = c = *(src++); + c = *(src++); if ((c == 0) && (*src == 0)) { break; } + + *dest++ = c; src++; } + + *dest = 0; } @@ -77,13 +85,15 @@ void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) while (dest < destend) { c = *(src++); - *(dest++) = (uint16)c; - if (c == 0) { break; } + + *(dest++) = (uint16)c; } + + *dest = 0; } @@ -99,13 +109,15 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) while (dest < destend) { c = *(src++); - *(dest++) = (char)c; - if (c == 0) { break; } + + *(dest++) = (char)c; } + + *dest = 0; } @@ -113,7 +125,7 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) Convert a UNISTR2 structure to an ASCII string ********************************************************************/ -void unistr2_to_ascii(char *dest, const UNISTR2 *str, int destlen) +void unistr2_to_ascii(char *dest, const UNISTR2 *str, int maxlen) { char *destend; const uint16 *src; @@ -121,19 +133,21 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, int destlen) register uint16 c; src = str->buffer; - len = MIN(str->uni_str_len, destlen); + len = MIN(str->uni_str_len, maxlen); destend = dest + len; while (dest < destend) { c = *(src++); - *(dest++) = (char)c; - if (c == 0) { break; } + + *(dest++) = (char)c; } + + *dest = 0; } @@ -167,13 +181,16 @@ char *uni_strncpy(char *destbuf, const char *srcbuf, int len) while (dest < destend) { - *(dest++) = c = *(src++); + c = *(src++); if (c == 0) { break; } + + *(dest++) = c; } + *dest++ = 0; return (char *)dest; } @@ -200,7 +217,7 @@ uint32 buffer2_to_uint32(const BUFFER2 *str) Convert a 'multi-string' buffer to space-separated ASCII. ********************************************************************/ -void buffer2_to_multistr(char *dest, const BUFFER2 *str, int destlen) +void buffer2_to_multistr(char *dest, const BUFFER2 *str, int maxlen) { char *destend; const uint16 *src; @@ -208,8 +225,8 @@ void buffer2_to_multistr(char *dest, const BUFFER2 *str, int destlen) register uint16 c; src = str->buffer; - len = MIN(str->buf_len/2, destlen); - destend = dest + len - 1; + len = MIN(str->buf_len/2, maxlen); + destend = dest + len; while (dest < destend) { -- cgit From e6ea3cc46313a53eaac6bf789f00a91bc02f7a3f Mon Sep 17 00:00:00 2001 From: Matthew Chapman Date: Thu, 8 Apr 1999 05:34:11 +0000 Subject: Fix for endianness problem reported by Edan Idzerda . A BUFFER2 is really a "unibuf" in my terminology and we should treat it as such. (This used to be commit 7ef76320b79b3ca4e04dcf809ac6388b6c47a3d8) --- source3/lib/util_unistr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 01ec262f60..4e7872021b 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -203,7 +203,7 @@ uint32 buffer2_to_uint32(const BUFFER2 *str) { if (str->buf_len == 4) { - const char *src = (const char*)str->buffer; + const char *src = str->buffer; return IVAL(src, 0); } else @@ -220,7 +220,7 @@ uint32 buffer2_to_uint32(const BUFFER2 *str) void buffer2_to_multistr(char *dest, const BUFFER2 *str, int maxlen) { char *destend; - const uint16 *src; + const char *src; int len; register uint16 c; @@ -232,6 +232,7 @@ void buffer2_to_multistr(char *dest, const BUFFER2 *str, int maxlen) { c = *(src++); *(dest++) = (c == 0) ? ' ' : (char)c; + src++; } *dest = 0; -- cgit From cba7662da1fd9ed8bd9f9969417adf1fe5f0d33b Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 7 Oct 1999 22:10:29 +0000 Subject: - added rudimentary CAP_UNICODE support because i thought it was part of a problem i was having. - added rudimentary CAP_STATUS32 support for same reason. - added hard-coded, copy-the-same-data-from-over-the-wire version of CAP_EXTENDED_SECURITY, which is a security-blob to encapsulate GSSAPI which encodes SPNEGO which is used to negotiate Kerberos or NTLMSSP. i have implemented NTLMSSP which negotiates NTLMv1 or NTLMv2 and 40-bit or 128-bit etc. i have implemented NTLMv1 / 40-bit. *whew*. (This used to be commit e5b80bd2f76fda70e41e4a9007eb035dab92ed8e) --- source3/lib/util_unistr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 4e7872021b..e047697a0f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -51,8 +51,7 @@ char *ascii_to_unibuf(char *dest, const char *src, int maxlen) /******************************************************************* Pull an ASCII string out of a UNICODE buffer (little endian). ********************************************************************/ - -void unibuf_to_ascii(char *dest, const char *src, int maxlen) +const char *unibuf_to_ascii(char *dest, const char *src, int maxlen) { char *destend = dest + maxlen; register char c; @@ -70,6 +69,8 @@ void unibuf_to_ascii(char *dest, const char *src, int maxlen) } *dest = 0; + + return src; } -- cgit From 56128244261f8e4c6e1144da66c736fbc2104665 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Mon, 25 Oct 1999 19:03:27 +0000 Subject: - typecast malloc / Realloc issues. - signed / unsigned issues. (This used to be commit c8fd555179314baf1672a23db34dc8ad9f2d02bf) --- source3/lib/util_unistr.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index e047697a0f..1dff5964fd 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -51,7 +51,8 @@ char *ascii_to_unibuf(char *dest, const char *src, int maxlen) /******************************************************************* Pull an ASCII string out of a UNICODE buffer (little endian). ********************************************************************/ -const char *unibuf_to_ascii(char *dest, const char *src, int maxlen) + +const char* unibuf_to_ascii(char *dest, const char *src, int maxlen) { char *destend = dest + maxlen; register char c; @@ -126,11 +127,11 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) Convert a UNISTR2 structure to an ASCII string ********************************************************************/ -void unistr2_to_ascii(char *dest, const UNISTR2 *str, int maxlen) +void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) { char *destend; const uint16 *src; - int len; + size_t len; register uint16 c; src = str->buffer; @@ -204,7 +205,7 @@ uint32 buffer2_to_uint32(const BUFFER2 *str) { if (str->buf_len == 4) { - const char *src = str->buffer; + const uchar *src = str->buffer; return IVAL(src, 0); } else @@ -217,12 +218,11 @@ uint32 buffer2_to_uint32(const BUFFER2 *str) /******************************************************************* Convert a 'multi-string' buffer to space-separated ASCII. ********************************************************************/ - -void buffer2_to_multistr(char *dest, const BUFFER2 *str, int maxlen) +void buffer2_to_multistr(char *dest, const BUFFER2 *str, size_t maxlen) { char *destend; - const char *src; - int len; + const uchar *src; + size_t len; register uint16 c; src = str->buffer; -- cgit From 4cda1d2b4faaf9a3abeb338e909a52f8b51fd89d Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Sat, 30 Oct 1999 22:34:38 +0000 Subject: NetServerTransportEnum parsing, client-side and rpcclient "srvtransports" added. (This used to be commit 37f4aac06fec3fbb34ed40d1010829b2e1f28558) --- source3/lib/util_unistr.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 1dff5964fd..e1a2e26623 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -238,3 +238,26 @@ void buffer2_to_multistr(char *dest, const BUFFER2 *str, size_t maxlen) *dest = 0; } + +/******************************************************************* + Convert a buffer4 to space-separated ASCII. + ********************************************************************/ +void buffer4_to_str(char *dest, const BUFFER4 *str, size_t maxlen) +{ + char *destend; + const uchar *src; + size_t len; + register uint16 c; + + src = str->buffer; + len = MIN(str->buf_len, maxlen); + destend = dest + len; + + while (dest < destend) + { + c = *(src++); + *(dest++) = (char)c; + } + + *dest = 0; +} -- cgit From 59a7e6cef89a9bd519df64978f8c8f113d66d0ef Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Wed, 3 Nov 1999 20:01:07 +0000 Subject: had to move unistr2_dup(), unistr2_free() and unistr2_copy() into util_unistr.c in order to get bin/testparm to compile. (This used to be commit e718ce9c4a3598483e38b8c32bdf2924593edc1e) --- source3/lib/util_unistr.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index e1a2e26623..9078a4fbc6 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -261,3 +261,37 @@ void buffer4_to_str(char *dest, const BUFFER4 *str, size_t maxlen) *dest = 0; } + +/******************************************************************* +copies a UNISTR2 structure. +********************************************************************/ +BOOL copy_unistr2(UNISTR2 *str, const UNISTR2 *from) +{ + /* set up string lengths. add one if string is not null-terminated */ + str->uni_max_len = from->uni_max_len; + str->undoc = from->undoc; + str->uni_str_len = from->uni_str_len; + + /* copy the string */ + memcpy(str->buffer, from->buffer, sizeof(from->buffer)); + + return True; +} + +/******************************************************************* +duplicates a UNISTR2 structure. +********************************************************************/ +UNISTR2 *unistr2_dup(const UNISTR2 *name) +{ + UNISTR2 *copy = (UNISTR2*)malloc(sizeof(*copy)); + copy_unistr2(copy, name); + return copy; +} + +/******************************************************************* +frees a UNISTR2 structure. +********************************************************************/ +void unistr2_free(UNISTR2 *name) +{ + free(name); +} -- cgit From fe8383382d5b8221093a5340705c4e5b3731a249 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 4 Nov 1999 21:41:36 +0000 Subject: samuserset -p password. YESSSSS :) you have to use "ntlmv1" at the moment (i.e set client ntlmv2 = no). (This used to be commit f52504c553becc64b89d546a57b1bd9cf1bc5b5c) --- source3/lib/util_unistr.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 9078a4fbc6..6f90528bf4 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -267,13 +267,23 @@ copies a UNISTR2 structure. ********************************************************************/ BOOL copy_unistr2(UNISTR2 *str, const UNISTR2 *from) { - /* set up string lengths. add one if string is not null-terminated */ - str->uni_max_len = from->uni_max_len; - str->undoc = from->undoc; - str->uni_str_len = from->uni_str_len; + if (from != NULL) + { + /* set up string lengths. add one if string is not null-terminated */ + str->uni_max_len = from->uni_max_len; + str->undoc = from->undoc; + str->uni_str_len = from->uni_str_len; - /* copy the string */ - memcpy(str->buffer, from->buffer, sizeof(from->buffer)); + /* copy the string */ + memcpy(str->buffer, from->buffer, sizeof(from->buffer)); + } + else + { + str->uni_max_len = 1; + str->undoc = 0; + str->uni_str_len = 1; + str->buffer[0] = 0; + } return True; } -- cgit From 3db52feb1f3b2c07ce0b06ad4a7099fa6efe3fc7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 13 Dec 1999 13:27:58 +0000 Subject: first pass at updating head branch to be to be the same as the SAMBA_2_0 branch (This used to be commit 453a822a76780063dff23526c35408866d0c0154) --- source3/lib/util_unistr.c | 639 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 445 insertions(+), 194 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 6f90528bf4..aee2a21fc0 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -21,287 +21,538 @@ #include "includes.h" -/******************************************************************* - Put an ASCII string into a UNICODE buffer (little endian). - ********************************************************************/ +extern int DEBUGLEVEL; -char *ascii_to_unibuf(char *dest, const char *src, int maxlen) -{ - char *destend = dest + maxlen; - register char c; - - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; - } +/* + * The following are the codepage to ucs2 and vica versa maps. + * These are dynamically loaded from a unicode translation file. + */ - *(dest++) = c; - *(dest++) = 0; - } +static smb_ucs2_t *doscp_to_ucs2; +static uint16 *ucs2_to_doscp; - *dest++ = 0; - *dest++ = 0; - return dest; -} +static smb_ucs2_t *unixcp_to_ucs2; +static uint16 *ucs2_to_unixcp; +#ifndef MAXUNI +#define MAXUNI 1024 +#endif /******************************************************************* - Pull an ASCII string out of a UNICODE buffer (little endian). - ********************************************************************/ + Write a string in (little-endian) unicode format. src is in + the current DOS codepage. len is the length in bytes of the + string pointed to by dst. -const char* unibuf_to_ascii(char *dest, const char *src, int maxlen) -{ - char *destend = dest + maxlen; - register char c; - - while (dest < destend) - { - c = *(src++); - if ((c == 0) && (*src == 0)) - { - break; - } + the return value is the length of the string *without* the trailing + two bytes of zero +********************************************************************/ - *dest++ = c; - src++; +int dos_PutUniCode(char *dst,const char *src, ssize_t len) +{ + int ret = 0; + while (*src && (len > 2)) { + size_t skip = skip_multibyte_char(*src); + smb_ucs2_t val = (*src & 0xff); + + /* + * If this is a multibyte character (and all DOS/Windows + * codepages have at maximum 2 byte multibyte characters) + * then work out the index value for the unicode conversion. + */ + + if (skip == 2) + val = ((val << 8) | src[1]); + + SSVAL(dst,ret,doscp_to_ucs2[val]); + ret += 2; + len -= 2; + if (skip) + src += skip; + else + src++; } + SSVAL(dst,ret,0); + return(ret); +} - *dest = 0; +/******************************************************************* + Skip past some unicode strings in a buffer. +********************************************************************/ - return src; +char *skip_unicode_string(char *buf,int n) +{ + while (n--) { + while (*buf) + buf += 2; + buf += 2; + } + return(buf); } - /******************************************************************* - Put an ASCII string into a UNICODE array (uint16's). - ********************************************************************/ + Return a DOS codepage version of a little-endian unicode string. + Hack alert: uses fixed buffer(s). +********************************************************************/ -void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) +char *dos_unistrn2(uint16 *src, int len) { - uint16 *destend = dest + maxlen; - register char c; - - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; + static char lbufs[8][MAXUNI]; + static int nexti; + char *lbuf = lbufs[nexti]; + char *p; + + nexti = (nexti+1)%8; + + for (p = lbuf; (len > 0) && (p-lbuf < MAXUNI-3) && *src; len--, src++) { + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); } - - *(dest++) = (uint16)c; } - *dest = 0; + *p = 0; + return lbuf; } +static char lbufs[8][MAXUNI]; +static int nexti; /******************************************************************* - Pull an ASCII string out of a UNICODE array (uint16's). - ********************************************************************/ + Return a DOS codepage version of a little-endian unicode string. + Hack alert: uses fixed buffer(s). +********************************************************************/ -void unistr_to_ascii(char *dest, const uint16 *src, int len) +char *dos_unistr2(uint16 *src) { - char *destend = dest + len; - register uint16 c; - - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; - } + char *lbuf = lbufs[nexti]; + char *p; + + nexti = (nexti+1)%8; + + for (p = lbuf; *src && (p-lbuf < MAXUNI-3); src++) { + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; - *(dest++) = (char)c; + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); + } } - *dest = 0; + *p = 0; + return lbuf; } - /******************************************************************* - Convert a UNISTR2 structure to an ASCII string - ********************************************************************/ +Return a DOS codepage version of a little-endian unicode string +********************************************************************/ -void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) +char *dos_unistr2_to_str(UNISTR2 *str) { - char *destend; - const uint16 *src; - size_t len; - register uint16 c; - - src = str->buffer; - len = MIN(str->uni_str_len, maxlen); - destend = dest + len; - - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; + char *lbuf = lbufs[nexti]; + char *p; + uint16 *src = str->buffer; + int max_size = MIN(sizeof(str->buffer)-3, str->uni_str_len); + + nexti = (nexti+1)%8; + + for (p = lbuf; *src && p-lbuf < max_size; src++) { + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); } - - *(dest++) = (char)c; } - *dest = 0; + *p = 0; + return lbuf; } - /******************************************************************* - Skip a UNICODE string in a little endian buffer. - ********************************************************************/ +Return a number stored in a buffer +********************************************************************/ -char *skip_unibuf(char *srcbuf, int len) +uint32 buffer2_to_uint32(BUFFER2 *str) { - uint16 *src = (uint16 *)srcbuf; - uint16 *srcend = src + len/2; + if (str->buf_len == 4) + return IVAL(str->buffer, 0); + else + return 0; +} - while ((src < srcend) && (*(src++) != 0)) - { +/******************************************************************* +Return a DOS codepage version of a NOTunicode string +********************************************************************/ + +char *dos_buffer2_to_str(BUFFER2 *str) +{ + char *lbuf = lbufs[nexti]; + char *p; + uint16 *src = str->buffer; + int max_size = MIN(sizeof(str->buffer)-3, str->buf_len/2); + + nexti = (nexti+1)%8; + + for (p = lbuf; *src && p-lbuf < max_size; src++) { + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); + } } - return (char *)src; + *p = 0; + return lbuf; } - /******************************************************************* - UNICODE strcpy between buffers. - ********************************************************************/ + Return a dos codepage version of a NOTunicode string +********************************************************************/ -char *uni_strncpy(char *destbuf, const char *srcbuf, int len) +char *dos_buffer2_to_multistr(BUFFER2 *str) { - const uint16 *src = (const uint16 *)srcbuf; - uint16 *dest = (uint16 *)destbuf; - uint16 *destend = dest + len/2; - register uint16 c; - - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; + char *lbuf = lbufs[nexti]; + char *p; + uint16 *src = str->buffer; + int max_size = MIN(sizeof(str->buffer)-3, str->buf_len/2); + + nexti = (nexti+1)%8; + + for (p = lbuf; p-lbuf < max_size; src++) { + if (*src == 0) { + *p++ = ' '; + } else { + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); + } } - - *(dest++) = c; } - *dest++ = 0; - return (char *)dest; + *p = 0; + return lbuf; } - /******************************************************************* - Return a number stored in a buffer - ********************************************************************/ + Create a null-terminated unicode string from a null-terminated DOS + codepage string. + Return number of unicode chars copied, excluding the null character. + Unicode strings created are in little-endian format. +********************************************************************/ -uint32 buffer2_to_uint32(const BUFFER2 *str) +size_t dos_struni2(char *dst, const char *src, size_t max_len) { - if (str->buf_len == 4) - { - const uchar *src = str->buffer; - return IVAL(src, 0); - } - else - { + size_t len = 0; + + if (dst == NULL) return 0; + + if (src != NULL) { + for (; *src && len < max_len-2; len++, dst +=2) { + size_t skip = skip_multibyte_char(*src); + smb_ucs2_t val = (*src & 0xff); + + /* + * If this is a multibyte character (and all DOS/Windows + * codepages have at maximum 2 byte multibyte characters) + * then work out the index value for the unicode conversion. + */ + + if (skip == 2) + val = ((val << 8) | src[1]); + + SSVAL(dst,0,doscp_to_ucs2[val]); + if (skip) + src += skip; + else + src++; + } } -} + SSVAL(dst,0,0); + + return len; +} /******************************************************************* - Convert a 'multi-string' buffer to space-separated ASCII. - ********************************************************************/ -void buffer2_to_multistr(char *dest, const BUFFER2 *str, size_t maxlen) + Return a DOS codepage version of a little-endian unicode string. + Hack alert: uses fixed buffer(s). +********************************************************************/ + +char *dos_unistr(char *buf) { - char *destend; - const uchar *src; - size_t len; - register uint16 c; - - src = str->buffer; - len = MIN(str->buf_len/2, maxlen); - destend = dest + len; - - while (dest < destend) - { - c = *(src++); - *(dest++) = (c == 0) ? ' ' : (char)c; - src++; + char *lbuf = lbufs[nexti]; + uint16 *src = (uint16 *)buf; + char *p; + + nexti = (nexti+1)%8; + + for (p = lbuf; *src && p-lbuf < MAXUNI-3; src++) { + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); + } } - *dest = 0; + *p = 0; + return lbuf; } /******************************************************************* - Convert a buffer4 to space-separated ASCII. - ********************************************************************/ -void buffer4_to_str(char *dest, const BUFFER4 *str, size_t maxlen) + Strcpy for unicode strings. returns length (in num of wide chars) +********************************************************************/ + +int unistrcpy(char *dst, char *src) { - char *destend; - const uchar *src; - size_t len; - register uint16 c; - - src = str->buffer; - len = MIN(str->buf_len, maxlen); - destend = dest + len; - - while (dest < destend) - { - c = *(src++); - *(dest++) = (char)c; + int num_wchars = 0; + uint16 *wsrc = (uint16 *)src; + uint16 *wdst = (uint16 *)dst; + + while (*wsrc) { + *wdst++ = *wsrc++; + num_wchars++; } + *wdst = 0; - *dest = 0; + return num_wchars; } + + /******************************************************************* -copies a UNISTR2 structure. + free any existing maps ********************************************************************/ -BOOL copy_unistr2(UNISTR2 *str, const UNISTR2 *from) +static void free_maps(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) { - if (from != NULL) - { - /* set up string lengths. add one if string is not null-terminated */ - str->uni_max_len = from->uni_max_len; - str->undoc = from->undoc; - str->uni_str_len = from->uni_str_len; - - /* copy the string */ - memcpy(str->buffer, from->buffer, sizeof(from->buffer)); + /* this handles identity mappings where we share the pointer */ + if (*pp_ucs2_to_cp == *pp_cp_to_ucs2) { + *pp_ucs2_to_cp = NULL; } - else - { - str->uni_max_len = 1; - str->undoc = 0; - str->uni_str_len = 1; - str->buffer[0] = 0; + + if (*pp_cp_to_ucs2) { + free(*pp_cp_to_ucs2); + *pp_cp_to_ucs2 = NULL; } - return True; + if (*pp_ucs2_to_cp) { + free(*pp_ucs2_to_cp); + *pp_ucs2_to_cp = NULL; + } } + /******************************************************************* -duplicates a UNISTR2 structure. + Build a default (null) codepage to unicode map. ********************************************************************/ -UNISTR2 *unistr2_dup(const UNISTR2 *name) + +void default_unicode_map(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) { - UNISTR2 *copy = (UNISTR2*)malloc(sizeof(*copy)); - copy_unistr2(copy, name); - return copy; + int i; + + free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp); + + if ((*pp_ucs2_to_cp = (uint16 *)malloc(2*65536)) == NULL) { + DEBUG(0,("default_unicode_map: malloc fail for ucs2_to_cp size %u.\n", 2*65536)); + abort(); + } + + *pp_cp_to_ucs2 = *pp_ucs2_to_cp; /* Default map is an identity. */ + for (i = 0; i < 65536; i++) + (*pp_cp_to_ucs2)[i] = i; } /******************************************************************* -frees a UNISTR2 structure. + Load a codepage to unicode and vica-versa map. ********************************************************************/ -void unistr2_free(UNISTR2 *name) + +BOOL load_unicode_map(const char *codepage, smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) { - free(name); + pstring unicode_map_file_name; + FILE *fp = NULL; + SMB_STRUCT_STAT st; + smb_ucs2_t *cp_to_ucs2 = *pp_cp_to_ucs2; + uint16 *ucs2_to_cp = *pp_ucs2_to_cp; + size_t cp_to_ucs2_size; + size_t ucs2_to_cp_size; + size_t i; + size_t size; + char buf[UNICODE_MAP_HEADER_SIZE]; + + DEBUG(5, ("load_unicode_map: loading unicode map for codepage %s.\n", codepage)); + + if (*codepage == '\0') + goto clean_and_exit; + + if(strlen(CODEPAGEDIR) + 13 + strlen(codepage) > sizeof(unicode_map_file_name)) { + DEBUG(0,("load_unicode_map: filename too long to load\n")); + goto clean_and_exit; + } + + pstrcpy(unicode_map_file_name, CODEPAGEDIR); + pstrcat(unicode_map_file_name, "/"); + pstrcat(unicode_map_file_name, "unicode_map."); + pstrcat(unicode_map_file_name, codepage); + + if(sys_stat(unicode_map_file_name,&st)!=0) { + DEBUG(0,("load_unicode_map: filename %s does not exist.\n", + unicode_map_file_name)); + goto clean_and_exit; + } + + size = st.st_size; + + if ((size != UNICODE_MAP_HEADER_SIZE + 4*65536) && (size != UNICODE_MAP_HEADER_SIZE +(2*256 + 2*65536))) { + DEBUG(0,("load_unicode_map: file %s is an incorrect size for a \ +unicode map file (size=%d).\n", unicode_map_file_name, (int)size)); + goto clean_and_exit; + } + + if((fp = sys_fopen( unicode_map_file_name, "r")) == NULL) { + DEBUG(0,("load_unicode_map: cannot open file %s. Error was %s\n", + unicode_map_file_name, strerror(errno))); + goto clean_and_exit; + } + + if(fread( buf, 1, UNICODE_MAP_HEADER_SIZE, fp)!=UNICODE_MAP_HEADER_SIZE) { + DEBUG(0,("load_unicode_map: cannot read header from file %s. Error was %s\n", + unicode_map_file_name, strerror(errno))); + goto clean_and_exit; + } + + /* Check the version value */ + if(SVAL(buf,UNICODE_MAP_VERSION_OFFSET) != UNICODE_MAP_FILE_VERSION_ID) { + DEBUG(0,("load_unicode_map: filename %s has incorrect version id. \ +Needed %hu, got %hu.\n", + unicode_map_file_name, (uint16)UNICODE_MAP_FILE_VERSION_ID, + SVAL(buf,UNICODE_MAP_VERSION_OFFSET))); + goto clean_and_exit; + } + + /* Check the codepage value */ + if(!strequal(&buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET], codepage)) { + DEBUG(0,("load_unicode_map: codepage %s in file %s is not the same as that \ +requested (%s).\n", &buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET], unicode_map_file_name, codepage )); + goto clean_and_exit; + } + + ucs2_to_cp_size = 2*65536; + if (size == UNICODE_MAP_HEADER_SIZE + 4*65536) { + /* + * This is a multibyte code page. + */ + cp_to_ucs2_size = 2*65536; + } else { + /* + * Single byte code page. + */ + cp_to_ucs2_size = 2*256; + } + + /* + * Free any old translation tables. + */ + + free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp); + + if ((cp_to_ucs2 = (smb_ucs2_t *)malloc(cp_to_ucs2_size)) == NULL) { + DEBUG(0,("load_unicode_map: malloc fail for cp_to_ucs2 size %u.\n", cp_to_ucs2_size )); + goto clean_and_exit; + } + + if ((ucs2_to_cp = (uint16 *)malloc(ucs2_to_cp_size)) == NULL) { + DEBUG(0,("load_unicode_map: malloc fail for ucs2_to_cp size %u.\n", ucs2_to_cp_size )); + goto clean_and_exit; + } + + if(fread( (char *)cp_to_ucs2, 1, cp_to_ucs2_size, fp)!=cp_to_ucs2_size) { + DEBUG(0,("load_unicode_map: cannot read cp_to_ucs2 from file %s. Error was %s\n", + unicode_map_file_name, strerror(errno))); + goto clean_and_exit; + } + + if(fread( (char *)ucs2_to_cp, 1, ucs2_to_cp_size, fp)!=ucs2_to_cp_size) { + DEBUG(0,("load_unicode_map: cannot read ucs2_to_cp from file %s. Error was %s\n", + unicode_map_file_name, strerror(errno))); + goto clean_and_exit; + } + + /* + * Now ensure the 16 bit values are in the correct endianness. + */ + + for (i = 0; i < cp_to_ucs2_size/2; i++) + cp_to_ucs2[i] = SVAL(cp_to_ucs2,i*2); + + for (i = 0; i < ucs2_to_cp_size/2; i++) + ucs2_to_cp[i] = SVAL(ucs2_to_cp,i*2); + + fclose(fp); + + *pp_cp_to_ucs2 = cp_to_ucs2; + *pp_ucs2_to_cp = ucs2_to_cp; + + return True; + +clean_and_exit: + + /* pseudo destructor :-) */ + + if(fp != NULL) + fclose(fp); + + free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp); + + default_unicode_map(pp_cp_to_ucs2, pp_ucs2_to_cp); + + return False; +} + +/******************************************************************* + Load a dos codepage to unicode and vica-versa map. +********************************************************************/ + +BOOL load_dos_unicode_map(int codepage) +{ + fstring codepage_str; + + slprintf(codepage_str, sizeof(fstring)-1, "%03d", codepage); + return load_unicode_map(codepage_str, &doscp_to_ucs2, &ucs2_to_doscp); +} + +/******************************************************************* + Load a UNIX codepage to unicode and vica-versa map. +********************************************************************/ + +BOOL load_unix_unicode_map(const char *unix_char_set) +{ + fstring upper_unix_char_set; + + fstrcpy(upper_unix_char_set, unix_char_set); + strupper(upper_unix_char_set); + return load_unicode_map(upper_unix_char_set, &unixcp_to_ucs2, &ucs2_to_unixcp); } -- cgit From 92a036cc37bb6e058cb86de1cc4d575d20032ce7 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 13 Dec 1999 19:17:40 +0000 Subject: Unicode conversion fix in Japanese environment from Motonobu TAKAHASHI. Jeremy. (This used to be commit decc6ebca29abd78754c275352de58b0de2e2aa5) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index aee2a21fc0..185e7c3547 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -61,7 +61,7 @@ int dos_PutUniCode(char *dst,const char *src, ssize_t len) */ if (skip == 2) - val = ((val << 8) | src[1]); + val = ((val << 8) | (src[1] & 0xff)); SSVAL(dst,ret,doscp_to_ucs2[val]); ret += 2; @@ -279,7 +279,7 @@ size_t dos_struni2(char *dst, const char *src, size_t max_len) */ if (skip == 2) - val = ((val << 8) | src[1]); + val = ((val << 8) | (src[1] & 0xff)); SSVAL(dst,0,doscp_to_ucs2[val]); if (skip) -- cgit From 8177fc778b02d9f61ef482fc60d32f353be77ba4 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 21 Dec 1999 23:14:01 +0000 Subject: Added new unicode functions - not used yet, but are the basis for the internal unicode conversion of Samba. Jeremy. (This used to be commit 302412df64aa4b6572b13ef61dfd68c3f8ebbb8b) --- source3/lib/util_unistr.c | 210 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 209 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 185e7c3547..fca9d8bfda 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -345,8 +345,9 @@ int unistrcpy(char *dst, char *src) /******************************************************************* - free any existing maps + Free any existing maps. ********************************************************************/ + static void free_maps(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) { /* this handles identity mappings where we share the pointer */ @@ -556,3 +557,210 @@ BOOL load_unix_unicode_map(const char *unix_char_set) strupper(upper_unix_char_set); return load_unicode_map(upper_unix_char_set, &unixcp_to_ucs2, &ucs2_to_unixcp); } + +/******************************************************************* + The following functions reproduce many of the non-UNICODE standard + string functions in Samba. +********************************************************************/ + +/******************************************************************* + Convert a UNICODE string to multibyte format. Note that the 'src' is in + native byte order, not little endian. Always zero terminates. + dst_len is in bytes. +********************************************************************/ + +static char *unicode_to_multibyte(char *dst, const smb_ucs2_t *src, + size_t dst_len, const uint16 *ucs2_to_cp) +{ + size_t i; + + for(i = 0; (i < (dst_len - 1)) && src[i];) { + smb_ucs2_t val = ucs2_to_cp[*src]; + if(val < 256) { + dst[i++] = (char)val; + } else if (i < (dst_len - 2)) { + + /* + * A 2 byte value is always written as + * high/low into the buffer stream. + */ + + dst[i++] = (char)((val >> 8) & 0xff); + dst[i++] = (char)(val & 0xff); + } + } + + dst[i] = '\0'; + + return dst; +} + +/******************************************************************* + Convert a multibyte string to UNICODE format. Note that the 'dst' is in + native byte order, not little endian. Always zero terminates. + dst_len is in bytes. +********************************************************************/ + +smb_ucs2_t *multibyte_to_unicode(smb_ucs2_t *dst, const char *src, + size_t dst_len, smb_ucs2_t *cp_to_ucs2) +{ + size_t i; + + dst_len /= sizeof(smb_ucs2_t); /* Convert to smb_ucs2_t units. */ + + for(i = 0; (i < (dst_len - 1)) && src[i];) { + size_t skip = skip_multibyte_char(*src); + smb_ucs2_t val = (*src & 0xff); + + /* + * If this is a multibyte character + * then work out the index value for the unicode conversion. + */ + + if (skip == 2) + val = ((val << 8) | (src[1] & 0xff)); + + dst[i++] = cp_to_ucs2[val]; + if (skip) + src += skip; + else + src++; + } + + dst[i] = 0; + + return dst; +} + +/******************************************************************* + Convert a UNICODE string to multibyte format. Note that the 'src' is in + native byte order, not little endian. Always zero terminates. + This function may be replaced if the MB codepage format is an + encoded one (ie. utf8, hex). See the code in lib/kanji.c + for details. dst_len is in bytes. +********************************************************************/ + +char *unicode_to_unix(char *dst, const smb_ucs2_t *src, size_t dst_len) +{ + return unicode_to_multibyte(dst, src, dst_len, ucs2_to_unixcp); +} + +/******************************************************************* + Convert a UNIX string to UNICODE format. Note that the 'dst' is in + native byte order, not little endian. Always zero terminates. + This function may be replaced if the UNIX codepage format is a + multi-byte one (ie. JIS, SJIS or utf8). See the code in lib/kanji.c + for details. dst_len is in bytes, not ucs2 units. +********************************************************************/ + +smb_ucs2_t *unix_to_unicode(smb_ucs2_t *dst, const char *src, size_t dst_len) +{ + return multibyte_to_unicode(dst, src, dst_len, unixcp_to_ucs2); +} + +/******************************************************************* + Convert a UNICODE string to DOS format. Note that the 'src' is in + native byte order, not little endian. Always zero terminates. + dst_len is in bytes. +********************************************************************/ + +char *unicode_to_dos(char *dst, const smb_ucs2_t *src, size_t dst_len) +{ + return unicode_to_multibyte(dst, src, dst_len, ucs2_to_doscp); +} + +/******************************************************************* + Convert a DOS string to UNICODE format. Note that the 'dst' is in + native byte order, not little endian. Always zero terminates. + This function may be replaced if the DOS codepage format is a + multi-byte one (ie. JIS, SJIS or utf8). See the code in lib/kanji.c + for details. dst_len is in bytes, not ucs2 units. +********************************************************************/ + +smb_ucs2_t *dos_to_unicode(smb_ucs2_t *dst, const char *src, size_t dst_len) +{ + return multibyte_to_unicode(dst, src, dst_len, doscp_to_ucs2); +} + +/******************************************************************* + Count the number of characters in a smb_ucs2_t string. +********************************************************************/ + +size_t wstrlen(const smb_ucs2_t *src) +{ + size_t len; + + for(len = 0; *src; len++) + ; + + return len; +} + +/******************************************************************* + Safe wstring copy into a known length string. maxlength includes + the terminating zero. maxlength is in bytes. +********************************************************************/ + +smb_ucs2_t *safe_wstrcpy(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlength) +{ + size_t ucs2_len; + + if (!dest) { + DEBUG(0,("ERROR: NULL dest in safe_wstrcpy\n")); + return NULL; + } + + if (!src) { + *dest = 0; + return dest; + } + + ucs2_len = wstrlen(src); + + if (ucs2_len >= (maxlength/sizeof(smb_ucs2_t))) { + fstring out; + DEBUG(0,("ERROR: string overflow by %u bytes in safe_wstrcpy [%.50s]\n", + (unsigned int)((ucs2_len*sizeof(smb_ucs2_t))-maxlength), + unicode_to_unix(out,src,sizeof(out))) ); + ucs2_len = (maxlength/sizeof(smb_ucs2_t)) - 1; + } + + memcpy(dest, src, ucs2_len*sizeof(smb_ucs2_t)); + dest[ucs2_len] = 0; + return dest; +} + +/******************************************************************* + Safe string cat into a string. maxlength includes the terminating zero. + maxlength is in bytes. +********************************************************************/ + +smb_ucs2_t *safe_wstrcat(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) +{ + size_t ucs2_src_len, ucs2_dest_len; + + if (!dest) { + DEBUG(0,("ERROR: NULL dest in safe_wstrcat\n")); + return NULL; + } + + if (!src) { + return dest; + } + + ucs2_src_len = wstrlen(src); + ucs2_dest_len = wstrlen(dest); + + if (ucs2_src_len + ucs2_dest_len >= (maxlength/sizeof(smb_ucs2_t))) { + fstring out; + int new_len = (maxlength/sizeof(smb_ucs2_t)) - ucs2_dest_len - 1; + DEBUG(0,("ERROR: string overflow by %u characters in safe_wstrcat [%.50s]\n", + (unsigned int)((sizeof(smb_ucs2_t)*(ucs2_src_len + ucs2_dest_len)) - maxlength), + unicode_to_unix(out,src,sizeof(out))) ); + ucs2_src_len = (size_t)(new_len > 0 ? new_len : 0); + } + + memcpy(&dest[ucs2_dest_len], src, ucs2_src_len*sizeof(smb_ucs2_t)); + dest[ucs2_dest_len + ucs2_src_len] = 0; + return dest; +} -- cgit From 02496ae4153c65b4ba3ae7b439e999f89ce4bb12 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 22 Dec 1999 00:17:35 +0000 Subject: Ok - we now have the following functions for UNICODE support : unicode_to_unix() unix_to_unicode() unicode_to_dos() dos_to_unicode() wstrlen() safe_wstrcpy() safe_wstrcat() wstrcmp() wstrncmp() wstrstr() wstrchr() wstrrchr() wstrtok() Jeremy. (This used to be commit ae34e2589ac32b7144607b77bd0d42bc74b42aff) --- source3/lib/util_unistr.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index fca9d8bfda..00f6ba4897 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -764,3 +764,130 @@ smb_ucs2_t *safe_wstrcat(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxleng dest[ucs2_dest_len + ucs2_src_len] = 0; return dest; } + +/******************************************************************* + Compare the two strings s1 and s2. len is in ucs2 units. +********************************************************************/ + +int wstrcmp(const smb_ucs2_t *s1, const smb_ucs2_t *s2) +{ + smb_ucs2_t c1, c2; + + for (;;) { + c1 = *s1++; + c2 = *s2++; + + if (c1 != c2) + return c1 - c2; + + if (c1 == 0) + return 0; + } + return 0; +} + +/******************************************************************* + Compare the first n characters of s1 to s2. len is in ucs2 units. +********************************************************************/ + +int wstrncmp(const smb_ucs2_t *s1, const smb_ucs2_t *s2, size_t len) +{ + smb_ucs2_t c1, c2; + + for (; len != 0; --len) { + c1 = *s1++; + c2 = *s2++; + + if (c1 != c2) + return c1 - c2; + + if (c1 == 0) + return 0; + + } + return 0; +} + +/******************************************************************* + Search string s2 from s1. +********************************************************************/ + +smb_ucs2_t *wstrstr(const smb_ucs2_t *s1, const smb_ucs2_t *s2) +{ + size_t len = wstrlen(s2); + + if (!*s2) + return (smb_ucs2_t *)s1; + + for(;*s1; s1++) { + if (*s1 == *s2) { + if (wstrncmp(s1, s2, len) == 0) + return (smb_ucs2_t *)s1; + } + } + return NULL; +} + +/******************************************************************* + Search for ucs2 char c from the beginning of s. +********************************************************************/ + +smb_ucs2_t *wstrchr(const smb_ucs2_t *s, smb_ucs2_t c) +{ + do { + if (*s == c) + return (smb_ucs2_t *)s; + } while (*s++); + + return NULL; +} + +/******************************************************************* + Search for ucs2 char c from the end of s. +********************************************************************/ + +smb_ucs2_t *wstrrchr(const smb_ucs2_t *s, smb_ucs2_t c) +{ + smb_ucs2_t *retval = 0; + + do { + if (*s == c) + retval = (smb_ucs2_t *)s; + } while (*s++); + + return retval; +} + +/******************************************************************* + Search token from s1 separated by any ucs2 char of s2. +********************************************************************/ + +smb_ucs2_t *wstrtok(smb_ucs2_t *s1, const smb_ucs2_t *s2) +{ + static smb_ucs2_t *s = NULL; + smb_ucs2_t *q; + + if (!s1) { + if (!s) + return NULL; + s1 = s; + } + + for (q = s1; *s1; s1++) { + smb_ucs2_t *p = wstrchr(s2, *s1); + if (p) { + if (s1 != q) { + s = s1 + 1; + *s1 = '\0'; + return q; + } + q = s1 + 1; + } + } + + s = NULL; + if (*q) + return q; + + return NULL; +} -- cgit From 5b3096a32fff88cb5f5388402f327da7b3d3dc18 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 22 Dec 1999 18:46:05 +0000 Subject: include/includes.h: Trimmed down unicode directory entry to be POSIX complient. lib/system.c: Trimmed down unicode directory entry to be POSIX complient. lib/util_unistr.c: Added wstrdup(). Jeremy. (This used to be commit ca64f4ab00c6d54022ba9bd4b869523566a242d7) --- source3/lib/util_unistr.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 00f6ba4897..cdeaefce7a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -891,3 +891,17 @@ smb_ucs2_t *wstrtok(smb_ucs2_t *s1, const smb_ucs2_t *s2) return NULL; } + +/******************************************************************* + Duplicate a ucs2 string. +********************************************************************/ + +smb_ucs2_t *wstrdup(const smb_ucs2_t *s) +{ + size_t newlen = (wstrlen(s)*sizeof(smb_ucs2_t)) + 1; + smb_ucs2_t *newstr = (smb_ucs2_t *)malloc(newlen); + if (newstr == NULL) + return NULL; + safe_wstrcpy(newstr, s, newlen); + return newstr; +} -- cgit From 7c51c5ddf31cc990610d07edb1cbe7265288e2c3 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 23 Dec 1999 01:57:43 +0000 Subject: Samba now includes a full ucs2 upper to lower case (and vica versa) map table. Jeremy. (This used to be commit d7b72d4cbfb6bd1925abc7f95c1180d3d65856a5) --- source3/lib/util_unistr.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index cdeaefce7a..486091a8b5 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -905,3 +905,59 @@ smb_ucs2_t *wstrdup(const smb_ucs2_t *s) safe_wstrcpy(newstr, s, newlen); return newstr; } + +/******************************************************************* + Mapping tables for UNICODE character. Allows toupper/tolower and + isXXX functions to work. +********************************************************************/ + +typedef struct { + smb_ucs2_t lower; + smb_ucs2_t upper; + unsigned char flags; +} smb_unicode_table_t; + +static smb_unicode_table_t map_table[] = { +#include "unicode_map_table.h" +}; + +/******************************************************************* + Is an upper case wchar. +********************************************************************/ + +int wisupper( smb_ucs2_t val) +{ + return (map_table[val].flags & UNI_UPPER); +} +/******************************************************************* + Is a lower case wchar. +********************************************************************/ + +int wislower( smb_ucs2_t val) +{ + return (map_table[val].flags & UNI_LOWER); +} +/******************************************************************* + Is a digit wchar. +********************************************************************/ + +int wisdigit( smb_ucs2_t val) +{ + return (map_table[val].flags & UNI_DIGIT); +} +/******************************************************************* + Is a hex digit wchar. +********************************************************************/ + +int wisxdigit( smb_ucs2_t val) +{ + return (map_table[val].flags & UNI_XDIGIT); +} +/******************************************************************* + Is a space wchar. +********************************************************************/ + +int wisspace( smb_ucs2_t val) +{ + return (map_table[val].flags & UNI_SPACE); +} -- cgit From 3a3afec47f5f44a2f20ec604bbb090c03672b2e0 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 23 Dec 1999 02:01:37 +0000 Subject: Added wtoupper/wtolower. Jeremy. (This used to be commit 2e03ed62542152264ca5315b9ee9bb045b7f0b03) --- source3/lib/util_unistr.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 486091a8b5..dddf4bde92 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -953,6 +953,7 @@ int wisxdigit( smb_ucs2_t val) { return (map_table[val].flags & UNI_XDIGIT); } + /******************************************************************* Is a space wchar. ********************************************************************/ @@ -961,3 +962,21 @@ int wisspace( smb_ucs2_t val) { return (map_table[val].flags & UNI_SPACE); } + +/******************************************************************* + Convert a wchar to upper case. +********************************************************************/ + +smb_ucs2_t wtoupper( smb_ucs2_t val ) +{ + return map_table[val].upper; +} + +/******************************************************************* + Convert a wchar to lower case. +********************************************************************/ + +smb_ucs2_t wtolowerr( smb_ucs2_t val ) +{ + return map_table[val].lower; +} -- cgit From f0161cf815c4c571fb703f1d4d50616f61892d69 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 23 Dec 1999 18:58:44 +0000 Subject: Fixed range checking in unicode to multibyte function. Oops. Jeremy. (This used to be commit 61e06c44b47834ed297aacee6d59c40796b4ffb5) --- source3/lib/util_unistr.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index dddf4bde92..ddcea26e38 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -572,25 +572,28 @@ BOOL load_unix_unicode_map(const char *unix_char_set) static char *unicode_to_multibyte(char *dst, const smb_ucs2_t *src, size_t dst_len, const uint16 *ucs2_to_cp) { - size_t i; + size_t dst_pos; - for(i = 0; (i < (dst_len - 1)) && src[i];) { - smb_ucs2_t val = ucs2_to_cp[*src]; + for(dst_pos = 0; *src && (dst_pos < dst_len - 1);) { + smb_ucs2_t val = ucs2_to_cp[*src++]; if(val < 256) { - dst[i++] = (char)val; - } else if (i < (dst_len - 2)) { + dst[dst_pos++] = (char)val; + } else { + + if(dst_pos >= dst_len - 2) + break; /* * A 2 byte value is always written as * high/low into the buffer stream. */ - dst[i++] = (char)((val >> 8) & 0xff); - dst[i++] = (char)(val & 0xff); + dst[dst_pos++] = (char)((val >> 8) & 0xff); + dst[dst_pos++] = (char)(val & 0xff); } } - dst[i] = '\0'; + dst[dst_pos] = '\0'; return dst; } @@ -898,7 +901,7 @@ smb_ucs2_t *wstrtok(smb_ucs2_t *s1, const smb_ucs2_t *s2) smb_ucs2_t *wstrdup(const smb_ucs2_t *s) { - size_t newlen = (wstrlen(s)*sizeof(smb_ucs2_t)) + 1; + size_t newlen = (wstrlen(s)+1)*sizeof(smb_ucs2_t); smb_ucs2_t *newstr = (smb_ucs2_t *)malloc(newlen); if (newstr == NULL) return NULL; @@ -929,6 +932,7 @@ int wisupper( smb_ucs2_t val) { return (map_table[val].flags & UNI_UPPER); } + /******************************************************************* Is a lower case wchar. ********************************************************************/ @@ -937,6 +941,7 @@ int wislower( smb_ucs2_t val) { return (map_table[val].flags & UNI_LOWER); } + /******************************************************************* Is a digit wchar. ********************************************************************/ @@ -945,6 +950,7 @@ int wisdigit( smb_ucs2_t val) { return (map_table[val].flags & UNI_DIGIT); } + /******************************************************************* Is a hex digit wchar. ********************************************************************/ -- cgit From 211697ee6cbeb2bbe86b96e17b922dd16d45c0e9 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 29 Dec 1999 02:00:38 +0000 Subject: Converted most of the functions in lib/util_str.c to smb_ucs2_t equivalents. Jeremy. (This used to be commit 1ba42aca2163c534f6be4e4a733604e3dffe0ed8) --- source3/lib/util_unistr.c | 912 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 875 insertions(+), 37 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index ddcea26e38..5e7076d5df 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -23,6 +23,9 @@ extern int DEBUGLEVEL; + smb_ucs2_t wchar_list_sep[] = { (smb_ucs2_t)' ', (smb_ucs2_t)'\t', (smb_ucs2_t)',', + (smb_ucs2_t)';', (smb_ucs2_t)':', (smb_ucs2_t)'\n', + (smb_ucs2_t)'\r', 0 }; /* * The following are the codepage to ucs2 and vica versa maps. * These are dynamically loaded from a unicode translation file. @@ -689,7 +692,7 @@ smb_ucs2_t *dos_to_unicode(smb_ucs2_t *dst, const char *src, size_t dst_len) Count the number of characters in a smb_ucs2_t string. ********************************************************************/ -size_t wstrlen(const smb_ucs2_t *src) +size_t strlen_w(const smb_ucs2_t *src) { size_t len; @@ -704,12 +707,12 @@ size_t wstrlen(const smb_ucs2_t *src) the terminating zero. maxlength is in bytes. ********************************************************************/ -smb_ucs2_t *safe_wstrcpy(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlength) +smb_ucs2_t *safe_strcpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlength) { size_t ucs2_len; if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_wstrcpy\n")); + DEBUG(0,("ERROR: NULL dest in safe_strcpy_w\n")); return NULL; } @@ -718,14 +721,20 @@ smb_ucs2_t *safe_wstrcpy(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlengt return dest; } - ucs2_len = wstrlen(src); + /* + * Convert maxlength to smb_ucs2_t units. + */ - if (ucs2_len >= (maxlength/sizeof(smb_ucs2_t))) { + maxlength /= sizeof(smb_ucs2_t); + + ucs2_len = strlen_w(src); + + if (ucs2_len >= maxlength) { fstring out; - DEBUG(0,("ERROR: string overflow by %u bytes in safe_wstrcpy [%.50s]\n", - (unsigned int)((ucs2_len*sizeof(smb_ucs2_t))-maxlength), + DEBUG(0,("ERROR: string overflow by %u bytes in safe_strcpy_w [%.50s]\n", + (unsigned int)((ucs2_len-maxlength)*sizeof(smb_ucs2_t)), unicode_to_unix(out,src,sizeof(out))) ); - ucs2_len = (maxlength/sizeof(smb_ucs2_t)) - 1; + ucs2_len = maxlength - 1; } memcpy(dest, src, ucs2_len*sizeof(smb_ucs2_t)); @@ -738,27 +747,32 @@ smb_ucs2_t *safe_wstrcpy(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlengt maxlength is in bytes. ********************************************************************/ -smb_ucs2_t *safe_wstrcat(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) +smb_ucs2_t *safe_strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) { size_t ucs2_src_len, ucs2_dest_len; if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_wstrcat\n")); + DEBUG(0,("ERROR: NULL dest in safe_strcat_w\n")); return NULL; } - if (!src) { + if (!src) return dest; - } - ucs2_src_len = wstrlen(src); - ucs2_dest_len = wstrlen(dest); + /* + * Convert maxlength to smb_ucs2_t units. + */ + + maxlength /= sizeof(smb_ucs2_t); - if (ucs2_src_len + ucs2_dest_len >= (maxlength/sizeof(smb_ucs2_t))) { + ucs2_src_len = strlen_w(src); + ucs2_dest_len = strlen_w(dest); + + if (ucs2_src_len + ucs2_dest_len >= maxlength) { fstring out; - int new_len = (maxlength/sizeof(smb_ucs2_t)) - ucs2_dest_len - 1; - DEBUG(0,("ERROR: string overflow by %u characters in safe_wstrcat [%.50s]\n", - (unsigned int)((sizeof(smb_ucs2_t)*(ucs2_src_len + ucs2_dest_len)) - maxlength), + int new_len = maxlength - ucs2_dest_len - 1; + DEBUG(0,("ERROR: string overflow by %u characters in safe_strcat_w [%.50s]\n", + (unsigned int)(sizeof(smb_ucs2_t)*(ucs2_src_len + ucs2_dest_len - maxlength)), unicode_to_unix(out,src,sizeof(out))) ); ucs2_src_len = (size_t)(new_len > 0 ? new_len : 0); } @@ -772,7 +786,7 @@ smb_ucs2_t *safe_wstrcat(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxleng Compare the two strings s1 and s2. len is in ucs2 units. ********************************************************************/ -int wstrcmp(const smb_ucs2_t *s1, const smb_ucs2_t *s2) +int strcmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) { smb_ucs2_t c1, c2; @@ -793,7 +807,7 @@ int wstrcmp(const smb_ucs2_t *s1, const smb_ucs2_t *s2) Compare the first n characters of s1 to s2. len is in ucs2 units. ********************************************************************/ -int wstrncmp(const smb_ucs2_t *s1, const smb_ucs2_t *s2, size_t len) +int strncmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2, size_t len) { smb_ucs2_t c1, c2; @@ -815,16 +829,16 @@ int wstrncmp(const smb_ucs2_t *s1, const smb_ucs2_t *s2, size_t len) Search string s2 from s1. ********************************************************************/ -smb_ucs2_t *wstrstr(const smb_ucs2_t *s1, const smb_ucs2_t *s2) +smb_ucs2_t *strstr_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) { - size_t len = wstrlen(s2); + size_t len = strlen_w(s2); if (!*s2) return (smb_ucs2_t *)s1; for(;*s1; s1++) { if (*s1 == *s2) { - if (wstrncmp(s1, s2, len) == 0) + if (strncmp_w(s1, s2, len) == 0) return (smb_ucs2_t *)s1; } } @@ -835,7 +849,7 @@ smb_ucs2_t *wstrstr(const smb_ucs2_t *s1, const smb_ucs2_t *s2) Search for ucs2 char c from the beginning of s. ********************************************************************/ -smb_ucs2_t *wstrchr(const smb_ucs2_t *s, smb_ucs2_t c) +smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { do { if (*s == c) @@ -849,7 +863,7 @@ smb_ucs2_t *wstrchr(const smb_ucs2_t *s, smb_ucs2_t c) Search for ucs2 char c from the end of s. ********************************************************************/ -smb_ucs2_t *wstrrchr(const smb_ucs2_t *s, smb_ucs2_t c) +smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { smb_ucs2_t *retval = 0; @@ -865,7 +879,7 @@ smb_ucs2_t *wstrrchr(const smb_ucs2_t *s, smb_ucs2_t c) Search token from s1 separated by any ucs2 char of s2. ********************************************************************/ -smb_ucs2_t *wstrtok(smb_ucs2_t *s1, const smb_ucs2_t *s2) +smb_ucs2_t *strtok_w(smb_ucs2_t *s1, const smb_ucs2_t *s2) { static smb_ucs2_t *s = NULL; smb_ucs2_t *q; @@ -877,7 +891,7 @@ smb_ucs2_t *wstrtok(smb_ucs2_t *s1, const smb_ucs2_t *s2) } for (q = s1; *s1; s1++) { - smb_ucs2_t *p = wstrchr(s2, *s1); + smb_ucs2_t *p = strchr_w(s2, *s1); if (p) { if (s1 != q) { s = s1 + 1; @@ -899,13 +913,13 @@ smb_ucs2_t *wstrtok(smb_ucs2_t *s1, const smb_ucs2_t *s2) Duplicate a ucs2 string. ********************************************************************/ -smb_ucs2_t *wstrdup(const smb_ucs2_t *s) +smb_ucs2_t *strdup_w(const smb_ucs2_t *s) { - size_t newlen = (wstrlen(s)+1)*sizeof(smb_ucs2_t); + size_t newlen = (strlen_w(s)+1)*sizeof(smb_ucs2_t); smb_ucs2_t *newstr = (smb_ucs2_t *)malloc(newlen); if (newstr == NULL) return NULL; - safe_wstrcpy(newstr, s, newlen); + safe_strcpy_w(newstr, s, newlen); return newstr; } @@ -928,7 +942,7 @@ static smb_unicode_table_t map_table[] = { Is an upper case wchar. ********************************************************************/ -int wisupper( smb_ucs2_t val) +int isupper_w( smb_ucs2_t val) { return (map_table[val].flags & UNI_UPPER); } @@ -937,7 +951,7 @@ int wisupper( smb_ucs2_t val) Is a lower case wchar. ********************************************************************/ -int wislower( smb_ucs2_t val) +int islower_w( smb_ucs2_t val) { return (map_table[val].flags & UNI_LOWER); } @@ -946,7 +960,7 @@ int wislower( smb_ucs2_t val) Is a digit wchar. ********************************************************************/ -int wisdigit( smb_ucs2_t val) +int isdigit_w( smb_ucs2_t val) { return (map_table[val].flags & UNI_DIGIT); } @@ -955,7 +969,7 @@ int wisdigit( smb_ucs2_t val) Is a hex digit wchar. ********************************************************************/ -int wisxdigit( smb_ucs2_t val) +int isxdigit_w( smb_ucs2_t val) { return (map_table[val].flags & UNI_XDIGIT); } @@ -964,7 +978,7 @@ int wisxdigit( smb_ucs2_t val) Is a space wchar. ********************************************************************/ -int wisspace( smb_ucs2_t val) +int isspace_w( smb_ucs2_t val) { return (map_table[val].flags & UNI_SPACE); } @@ -973,7 +987,7 @@ int wisspace( smb_ucs2_t val) Convert a wchar to upper case. ********************************************************************/ -smb_ucs2_t wtoupper( smb_ucs2_t val ) +smb_ucs2_t toupper_w( smb_ucs2_t val ) { return map_table[val].upper; } @@ -982,7 +996,831 @@ smb_ucs2_t wtoupper( smb_ucs2_t val ) Convert a wchar to lower case. ********************************************************************/ -smb_ucs2_t wtolowerr( smb_ucs2_t val ) +smb_ucs2_t tolower_w( smb_ucs2_t val ) { return map_table[val].lower; } + +static smb_ucs2_t *last_ptr = NULL; + +void set_first_token_w(smb_ucs2_t *ptr) +{ + last_ptr = ptr; +} + +/**************************************************************************** + Get the next token from a string, return False if none found + handles double-quotes. + Based on a routine by GJC@VILLAGE.COM. + Extensively modified by Andrew.Tridgell@anu.edu.au + bufsize is in bytes. +****************************************************************************/ + +static smb_ucs2_t sep_list[] = { (smb_ucs2_t)' ', (smb_ucs2_t)'\t', (smb_ucs2_t)'\n', (smb_ucs2_t)'\r', 0}; +static smb_ucs2_t quotechar = (smb_ucs2_t)'\"'; + +BOOL next_token_w(smb_ucs2_t **ptr, smb_ucs2_t *buff, smb_ucs2_t *sep, size_t bufsize) +{ + smb_ucs2_t *s; + BOOL quoted; + size_t len=1; + + /* + * Convert bufsize to smb_ucs2_t units. + */ + + bufsize /= sizeof(smb_ucs2_t); + + if (!ptr) + ptr = &last_ptr; + if (!ptr) + return(False); + + s = *ptr; + + /* + * Default to simple separators. + */ + + if (!sep) + sep = sep_list; + + /* + * Find the first non sep char. + */ + + while(*s && strchr_w(sep,*s)) + s++; + + /* + * Nothing left ? + */ + + if (!*s) + return(False); + + /* + * Copy over the token. + */ + + for (quoted = False; len < bufsize && *s && (quoted || !strchr_w(sep,*s)); s++) { + if (*s == quotechar) { + quoted = !quoted; + } else { + len++; + *buff++ = *s; + } + } + + *ptr = (*s) ? s+1 : s; + *buff = 0; + last_ptr = *ptr; + + return(True); +} + +/**************************************************************************** + Convert list of tokens to array; dependent on above routine. + Uses last_ptr from above - bit of a hack. +****************************************************************************/ + +smb_ucs2_t **toktocliplist_w(int *ctok, smb_ucs2_t *sep) +{ + smb_ucs2_t *s=last_ptr; + int ictok=0; + smb_ucs2_t **ret, **iret; + + if (!sep) + sep = sep_list; + + while(*s && strchr_w(sep,*s)) + s++; + + /* + * Nothing left ? + */ + + if (!*s) + return(NULL); + + do { + ictok++; + while(*s && (!strchr_w(sep,*s))) + s++; + while(*s && strchr_w(sep,*s)) + *s++=0; + } while(*s); + + *ctok = ictok; + s = last_ptr; + + if (!(ret=iret=malloc(ictok*sizeof(smb_ucs2_t *)))) + return NULL; + + while(ictok--) { + *iret++=s; + while(*s++) + ; + while(!*s) + s++; + } + + return ret; +} + +/******************************************************************* + Case insensitive string compararison. +********************************************************************/ + +int StrCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t) +{ + /* + * Compare until we run out of string, either t or s, or find a difference. + */ + + while (*s && *t && toupper_w(*s) == toupper_w(*t)) { + s++; + t++; + } + + return(toupper_w(*s) - toupper_w(*t)); +} + +/******************************************************************* + Case insensitive string compararison, length limited. +********************************************************************/ + +int StrnCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t, size_t n) +{ + /* + * Compare until we run out of string, either t or s, or chars. + */ + + while (n && *s && *t && toupper_w(*s) == toupper_w(*t)) { + s++; + t++; + n--; + } + + /* + * Not run out of chars - strings are different lengths. + */ + + if (n) + return(toupper_w(*s) - toupper_w(*t)); + + /* + * Identical up to where we run out of chars, + * and strings are same length. + */ + + return(0); +} + +/******************************************************************* + Compare 2 strings. +********************************************************************/ + +BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) +{ + if (s1 == s2) + return(True); + if (!s1 || !s2) + return(False); + + return(StrCaseCmp_w(s1,s2)==0); +} + +/******************************************************************* + Compare 2 strings up to and including the nth char. +******************************************************************/ + +BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n) +{ + if (s1 == s2) + return(True); + if (!s1 || !s2 || !n) + return(False); + + return(StrnCaseCmp_w(s1,s2,n)==0); +} + +/******************************************************************* + Compare 2 strings (case sensitive). +********************************************************************/ + +BOOL strcsequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2) +{ + if (s1 == s2) + return(True); + if (!s1 || !s2) + return(False); + + return(strcmp_w(s1,s2)==0); +} + +/******************************************************************* + Convert a string to lower case. +********************************************************************/ + +void strlower_w(smb_ucs2_t *s) +{ + while (*s) { + if (isupper_w(*s)) + *s = tolower_w(*s); + s++; + } +} + +/******************************************************************* + Convert a string to upper case. +********************************************************************/ + +void strupper_w(smb_ucs2_t *s) +{ + while (*s) { + if (islower_w(*s)) + *s = toupper_w(*s); + s++; + } +} + +/******************************************************************* + Convert a string to "normal" form. +********************************************************************/ + +void strnorm_w(smb_ucs2_t *s) +{ + extern int case_default; + if (case_default == CASE_UPPER) + strupper_w(s); + else + strlower_w(s); +} + +/******************************************************************* + Check if a string is in "normal" case. +********************************************************************/ + +BOOL strisnormal_w(smb_ucs2_t *s) +{ + extern int case_default; + if (case_default == CASE_UPPER) + return(!strhaslower_w(s)); + + return(!strhasupper_w(s)); +} + +/**************************************************************************** + String replace. +****************************************************************************/ + +void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) +{ + while (*s) { + if (oldc == *s) + *s = newc; + s++; + } +} + +/******************************************************************* + Skip past some strings in a buffer. n is in bytes. +********************************************************************/ + +smb_ucs2_t *skip_string_w(smb_ucs2_t *buf,size_t n) +{ + while (n--) + buf += (strlen_w(buf)*sizeof(smb_ucs2_t)) + 1; + return(buf); +} + +/******************************************************************* + Count the number of characters in a string. Same as strlen_w in + smb_ucs2_t string units. +********************************************************************/ + +size_t str_charnum_w(const smb_ucs2_t *s) +{ + return strlen_w(s); +} + +/******************************************************************* + Trim the specified elements off the front and back of a string. +********************************************************************/ + +BOOL trim_string_w(smb_ucs2_t *s,const smb_ucs2_t *front,const smb_ucs2_t *back) +{ + BOOL ret = False; + size_t front_len = (front && *front) ? strlen_w(front) : 0; + size_t back_len = (back && *back) ? strlen_w(back) : 0; + size_t s_len; + + while (front_len && strncmp_w(s, front, front_len) == 0) { + smb_ucs2_t *p = s; + ret = True; + + while (1) { + if (!(*p = p[front_len])) + break; + p++; + } + } + + if(back_len) { + s_len = strlen_w(s); + while ((s_len >= back_len) && + (strncmp_w(s + s_len - back_len, back, back_len)==0)) { + ret = True; + s[s_len - back_len] = 0; + s_len = strlen_w(s); + } + } + + return(ret); +} + +/**************************************************************************** + Does a string have any uppercase chars in it ? +****************************************************************************/ + +BOOL strhasupper_w(const smb_ucs2_t *s) +{ + while (*s) { + if (isupper_w(*s)) + return(True); + s++; + } + return(False); +} + +/**************************************************************************** + Does a string have any lowercase chars in it ? +****************************************************************************/ + +BOOL strhaslower_w(const smb_ucs2_t *s) +{ + while (*s) { + if (islower(*s)) + return(True); + s++; + } + return(False); +} + +/**************************************************************************** + Find the number of 'c' chars in a string. +****************************************************************************/ + +size_t count_chars_w(const smb_ucs2_t *s,smb_ucs2_t c) +{ + size_t count=0; + + while (*s) { + if (*s == c) + count++; + s++; + } + return(count); +} + +/******************************************************************* + Return True if a string consists only of one particular character. +********************************************************************/ + +BOOL str_is_all_w(const smb_ucs2_t *s,smb_ucs2_t c) +{ + if(s == NULL) + return False; + if(!*s) + return False; + + while (*s) { + if (*s != c) + return False; + s++; + } + return True; +} + +/******************************************************************* + Paranoid strcpy into a buffer of given length (includes terminating + zero. Strips out all but 'a-Z0-9' and replaces with '_'. Deliberately + does *NOT* check for multibyte characters. Don't change it ! + maxlength is in bytes. +********************************************************************/ + +smb_ucs2_t *alpha_strcpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) +{ + size_t len, i; + + /* + * Convert to smb_ucs2_t units. + */ + + maxlength /= sizeof(smb_ucs2_t); + + if (!dest) { + DEBUG(0,("ERROR: NULL dest in alpha_strcpy_w\n")); + return NULL; + } + + if (!src) { + *dest = 0; + return dest; + } + + len = strlen_w(src); + if (len >= maxlength) + len = maxlength - 1; + + for(i = 0; i < len; i++) { + smb_ucs2_t val = src[i]; + if(isupper_w(val) ||islower_w(val) || isdigit_w(val)) + dest[i] = src[i]; + else + dest[i] = (smb_ucs2_t)'_'; + } + + dest[i] = 0; + + return dest; +} + +/**************************************************************************** + Like strncpy but always null terminates. Make sure there is room ! + The variable n should always be one less than the available size and is in bytes. +****************************************************************************/ + +smb_ucs2_t *StrnCpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src,size_t n) +{ + smb_ucs2_t *d = dest; + if (!dest) + return(NULL); + if (!src) { + *dest = 0; + return(dest); + } + + /* + * Convert to smb_ucs2_t units. + */ + + n /= sizeof(smb_ucs2_t); + + while (n-- && (*d++ = *src++)) + ; + *d = 0; + return(dest); +} + +/**************************************************************************** + Like strncpy but copies up to the character marker. Always null terminates. + returns a pointer to the character marker in the source string (src). + n is in bytes. +****************************************************************************/ + +smb_ucs2_t *strncpyn_w(smb_ucs2_t *dest, const smb_ucs2_t *src,size_t n, smb_ucs2_t c) +{ + smb_ucs2_t *p; + size_t str_len; + + p = strchr_w(src, c); + if (p == NULL) { + fstring cval; + smb_ucs2_t mbcval[2]; + mbcval[0] = c; + mbcval[1] = 0; + DEBUG(5, ("strncpyn_w: separator character (%s) not found\n", + unicode_to_unix(cval,mbcval,sizeof(cval)) )); + return NULL; + } + + str_len = PTR_DIFF(p, src) + sizeof(smb_ucs2_t); + safe_strcpy_w(dest, src, MIN(n, str_len)); + + return p; +} + +/************************************************************* + Routine to get hex characters and turn them into a 16 byte array. + The array can be variable length, and any non-hex-numeric + characters are skipped. "0xnn" or "0Xnn" is specially catered + for. len is in bytes. + Valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n" +**************************************************************/ + +static smb_ucs2_t hexprefix[] = { (smb_ucs2_t)'0', (smb_ucs2_t)'x', 0 }; +static smb_ucs2_t hexchars[] = { (smb_ucs2_t)'0', (smb_ucs2_t)'1', (smb_ucs2_t)'2', (smb_ucs2_t)'3', + (smb_ucs2_t)'4', (smb_ucs2_t)'5', (smb_ucs2_t)'6', (smb_ucs2_t)'7', + (smb_ucs2_t)'8', (smb_ucs2_t)'9', (smb_ucs2_t)'A', (smb_ucs2_t)'B', + (smb_ucs2_t)'C', (smb_ucs2_t)'D', (smb_ucs2_t)'E', (smb_ucs2_t)'F', 0 }; + +size_t strhex_to_str_w(char *p, size_t len, const smb_ucs2_t *strhex) +{ + size_t i; + size_t num_chars = 0; + unsigned char lonybble, hinybble; + smb_ucs2_t *p1 = NULL, *p2 = NULL; + + /* + * Convert to smb_ucs2_t units. + */ + + len /= sizeof(smb_ucs2_t); + + for (i = 0; i < len && strhex[i] != 0; i++) { + if (strnequal_w(hexchars, hexprefix, 2)) { + i++; /* skip two chars */ + continue; + } + + if (!(p1 = strchr_w(hexchars, toupper_w(strhex[i])))) + break; + + i++; /* next hex digit */ + + if (!(p2 = strchr_w(hexchars, toupper_w(strhex[i])))) + break; + + /* get the two nybbles */ + hinybble = (PTR_DIFF(p1, hexchars)/sizeof(smb_ucs2_t)); + lonybble = (PTR_DIFF(p2, hexchars)/sizeof(smb_ucs2_t)); + + p[num_chars] = (hinybble << 4) | lonybble; + num_chars++; + + p1 = NULL; + p2 = NULL; + } + return num_chars; +} + +/**************************************************************************** + Check if a string is part of a list. +****************************************************************************/ + +BOOL in_list_w(smb_ucs2_t *s,smb_ucs2_t *list,BOOL casesensitive) +{ + wpstring tok; + smb_ucs2_t *p=list; + + if (!list) + return(False); + + while (next_token_w(&p,tok,LIST_SEP_W,sizeof(tok))) { + if (casesensitive) { + if (strcmp_w(tok,s) == 0) + return(True); + } else { + if (StrCaseCmp_w(tok,s) == 0) + return(True); + } + } + return(False); +} + +/* This is used to prevent lots of mallocs of size 2 */ +static smb_ucs2_t *null_string = NULL; + +/**************************************************************************** + Set a string value, allocing the space for the string. +****************************************************************************/ + +BOOL string_init_w(smb_ucs2_t **dest,const smb_ucs2_t *src) +{ + size_t l; + + if (!null_string) { + if((null_string = (smb_ucs2_t *)malloc(sizeof(smb_ucs2_t))) == NULL) { + DEBUG(0,("string_init_w: malloc fail for null_string.\n")); + return False; + } + *null_string = 0; + } + + if (!src) + src = null_string; + + l = strlen_w(src); + + if (l == 0) + *dest = null_string; + else { + (*dest) = (smb_ucs2_t *)malloc(sizeof(smb_ucs2_t)*(l+1)); + if ((*dest) == NULL) { + DEBUG(0,("Out of memory in string_init_w\n")); + return False; + } + + wpstrcpy(*dest,src); + } + return(True); +} + +/**************************************************************************** + Free a string value. +****************************************************************************/ + +void string_free_w(smb_ucs2_t **s) +{ + if (!s || !(*s)) + return; + if (*s == null_string) + *s = NULL; + if (*s) + free((char *)*s); + *s = NULL; +} + +/**************************************************************************** + Set a string value, allocing the space for the string, and deallocating any + existing space. +****************************************************************************/ + +BOOL string_set_w(smb_ucs2_t **dest,const smb_ucs2_t *src) +{ + string_free_w(dest); + + return(string_init_w(dest,src)); +} + +/**************************************************************************** + Substitute a string for a pattern in another string. Make sure there is + enough room ! + + This routine looks for pattern in s and replaces it with + insert. It may do multiple replacements. + + Any of " ; ' $ or ` in the insert string are replaced with _ + if len==0 then no length check is performed + len is in bytes. +****************************************************************************/ + +void string_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t *insert, size_t len) +{ + smb_ucs2_t *p; + ssize_t ls,lp,li, i; + + /* + * Convert to smb_ucs2_t units. + */ + + len /= sizeof(smb_ucs2_t); + + if (!insert || !pattern || !s) + return; + + ls = (ssize_t)strlen_w(s); + lp = (ssize_t)strlen_w(pattern); + li = (ssize_t)strlen_w(insert); + + if (!*pattern) + return; + + while (lp <= ls && (p = strstr_w(s,pattern))) { + if (len && (ls + (li-lp) >= len)) { + fstring out; + DEBUG(0,("ERROR: string overflow by %d in string_sub_w(%.50s, %d)\n", + (int)(sizeof(smb_ucs2_t)*(ls + (li-lp) - len)), + unicode_to_unix(out,pattern,sizeof(out)), (int)len*sizeof(smb_ucs2_t))); + break; + } + if (li != lp) + memmove(p+li,p+lp,sizeof(smb_ucs2_t)*(strlen_w(p+lp)+1)); + + for (i=0;i= len)) { + fstring out; + DEBUG(0,("ERROR: string overflow by %d in all_string_sub_w(%.50s, %d)\n", + (int)(sizeof(smb_ucs2_t)*(ls + (li-lp) - len)), + unicode_to_unix(out,pattern,sizeof(out)), (int)len*sizeof(smb_ucs2_t))); + break; + } + if (li != lp) + memmove(p+li,p+lp,sizeof(smb_ucs2_t)*(strlen_w(p+lp)+1)); + + memcpy(p, insert, li*sizeof(smb_ucs2_t)); + s = p + li; + ls += (li-lp); + } +} + +/**************************************************************************** + Splits out the front and back at a separator. +****************************************************************************/ + +void split_at_last_component_w(smb_ucs2_t *path, smb_ucs2_t *front, smb_ucs2_t sep, smb_ucs2_t *back) +{ + smb_ucs2_t *p = strrchr_w(path, sep); + + if (p != NULL) + *p = 0; + + if (front != NULL) + wpstrcpy(front, path); + + if (p != NULL) { + if (back != NULL) + wpstrcpy(back, p+1); + *p = (smb_ucs2_t)'\\'; + } else { + if (back != NULL) + back[0] = 0; + } +} + + +/**************************************************************************** + Write an octal as a string. +****************************************************************************/ + +smb_ucs2_t *octal_string_w(int i) +{ + static smb_ucs2_t wret[64]; + char ret[64]; + + if (i == -1) + slprintf(ret, sizeof(ret), "-1"); + else + slprintf(ret, sizeof(ret), "0%o", i); + return unix_to_unicode(wret, ret, sizeof(wret)); +} + + +/**************************************************************************** + Truncate a string at a specified length. + length is in bytes. +****************************************************************************/ + +smb_ucs2_t *string_truncate_w(smb_ucs2_t *s, size_t length) +{ + /* + * Convert to smb_ucs2_t units. + */ + + length /= sizeof(smb_ucs2_t); + + if (s && strlen_w(s) > length) + s[length] = 0; + + return s; +} -- cgit From d867b86721e988dee56d5e9382b32c870ccb2790 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 26 Jan 2000 00:12:35 +0000 Subject: Second set of inline optimisation fixes from Ying Chen . Stop makeing function calls for every use of skip_multibyte_char. This function is called several *million* times during a NetBench run :-). Jeremy. (This used to be commit e5a3deba46ea2d4cb49a6c4b73edd766fe8b5a5c) --- source3/lib/util_unistr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5e7076d5df..a512f68acc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -54,7 +54,7 @@ int dos_PutUniCode(char *dst,const char *src, ssize_t len) { int ret = 0; while (*src && (len > 2)) { - size_t skip = skip_multibyte_char(*src); + size_t skip = get_character_len(*src); smb_ucs2_t val = (*src & 0xff); /* @@ -272,7 +272,7 @@ size_t dos_struni2(char *dst, const char *src, size_t max_len) if (src != NULL) { for (; *src && len < max_len-2; len++, dst +=2) { - size_t skip = skip_multibyte_char(*src); + size_t skip = get_character_len(*src); smb_ucs2_t val = (*src & 0xff); /* @@ -615,7 +615,7 @@ smb_ucs2_t *multibyte_to_unicode(smb_ucs2_t *dst, const char *src, dst_len /= sizeof(smb_ucs2_t); /* Convert to smb_ucs2_t units. */ for(i = 0; (i < (dst_len - 1)) && src[i];) { - size_t skip = skip_multibyte_char(*src); + size_t skip = get_character_len(*src); smb_ucs2_t val = (*src & 0xff); /* -- cgit From 59ac32c2556e970ea1fe171e7b76cfee2142fbf0 Mon Sep 17 00:00:00 2001 From: Jean-François Micouleau Date: Mon, 7 Feb 2000 16:22:16 +0000 Subject: Jeremy can you check lib/util_unistr.c for codepages support ? I added 2 UNICODE <-> ASCII functions which _don't_ honor codepage support. J.F. (This used to be commit b81dc7b7f832cae2e66076398a134fbb6c1f78ca) --- source3/lib/util_unistr.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index a512f68acc..2c721aeb47 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -78,6 +78,61 @@ int dos_PutUniCode(char *dst,const char *src, ssize_t len) return(ret); } +/******************************************************************* + Put an ASCII string into a UNICODE array (uint16's). + + Warning: doesn't do any codepage !!! BAD !!! + + Help ! Fix Me ! Fix Me ! +********************************************************************/ + +void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) +{ + uint16 *destend = dest + maxlen; + register char c; + + while (dest < destend) + { + c = *(src++); + if (c == 0) + { + break; + } + + *(dest++) = (uint16)c; + } + + *dest = 0; +} + +/******************************************************************* + Pull an ASCII string out of a UNICODE array (uint16's). + + Warning: doesn't do any codepage !!! BAD !!! + + Help ! Fix Me ! Fix Me ! +********************************************************************/ + +void unistr_to_ascii(char *dest, const uint16 *src, int len) +{ + char *destend = dest + len; + register uint16 c; + + while (dest < destend) + { + c = *(src++); + if (c == 0) + { + break; + } + + *(dest++) = (char)c; + } + + *dest = 0; +} + + /******************************************************************* Skip past some unicode strings in a buffer. ********************************************************************/ @@ -182,6 +237,48 @@ char *dos_unistr2_to_str(UNISTR2 *str) return lbuf; } +/******************************************************************* + Convert a UNISTR2 structure to an ASCII string + Warning: this version does DOS codepage. +********************************************************************/ + +void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) +{ + char *destend; + const uint16 *src; + size_t len; + register uint16 c; + + src = str->buffer; + len = MIN(str->uni_str_len, maxlen); + destend = dest + len; + + while (dest < destend) + { + uint16 ucs2_val; + uint16 cp_val; + + c = *(src++); + if (c == 0) + { + break; + } + + ucs2_val = SVAL(src,0); + cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *(dest++) = (char)cp_val; + else { + *dest= (cp_val >> 8) & 0xff; + *(dest++) = (cp_val & 0xff); + } + } + + *dest = 0; +} + + /******************************************************************* Return a number stored in a buffer ********************************************************************/ -- cgit From 07fd3b392d8b3dc5bc8c72584e31a7369b92ff13 Mon Sep 17 00:00:00 2001 From: Jean-François Micouleau Date: Mon, 7 Feb 2000 18:06:54 +0000 Subject: Hum, I should remove my gloves when I'm in front of an xterm :) fixed a stupid bug in unistr2_to_ascii that I introduced fixed getprinterdata() (This used to be commit 2f544a807714024c0fe2ddc26e11c9ddcb47e81f) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 2c721aeb47..c0701f0427 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -258,13 +258,13 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) uint16 ucs2_val; uint16 cp_val; - c = *(src++); + c = *src; if (c == 0) { break; } - ucs2_val = SVAL(src,0); + ucs2_val = SVAL(src++,0); cp_val = ucs2_to_doscp[ucs2_val]; if (cp_val < 256) -- cgit From 8f1620125dcb9c29c223f4efb6485528ece70f11 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 22 Mar 2000 19:03:12 +0000 Subject: acconfig.h configure configure.in: Added check for UT_SYSLEN for utmp code. include/byteorder.h: Added alignment macros. include/nameserv.h: Added defines for msg_type field options - from rfc1002. lib/time.c: Typo fix. lib/util_unistr.c: Updates from UNICODE branch. printing/nt_printing.c: bzero -> memset. smbd/connection.c: Added check for UT_SYSLEN for utmp code. Other fixes : Rollback of unapproved commit from Luke. Please *ask* next time before doing large changes to HEAD. Jeremy. (This used to be commit f02999dbf7971b4ea05050d7206205d7737a78b2) --- source3/lib/util_unistr.c | 65 ++++++++++------------------------------------- 1 file changed, 14 insertions(+), 51 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c0701f0427..c981aba51a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -712,7 +712,7 @@ smb_ucs2_t *multibyte_to_unicode(smb_ucs2_t *dst, const char *src, dst_len /= sizeof(smb_ucs2_t); /* Convert to smb_ucs2_t units. */ for(i = 0; (i < (dst_len - 1)) && src[i];) { - size_t skip = get_character_len(*src); + size_t skip = skip_multibyte_char(*src); smb_ucs2_t val = (*src & 0xff); /* @@ -801,7 +801,7 @@ size_t strlen_w(const smb_ucs2_t *src) /******************************************************************* Safe wstring copy into a known length string. maxlength includes - the terminating zero. maxlength is in bytes. + the terminating zero. maxlength is in ucs2 units. ********************************************************************/ smb_ucs2_t *safe_strcpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlength) @@ -818,10 +818,6 @@ smb_ucs2_t *safe_strcpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxleng return dest; } - /* - * Convert maxlength to smb_ucs2_t units. - */ - maxlength /= sizeof(smb_ucs2_t); ucs2_len = strlen_w(src); @@ -841,7 +837,7 @@ smb_ucs2_t *safe_strcpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxleng /******************************************************************* Safe string cat into a string. maxlength includes the terminating zero. - maxlength is in bytes. + maxlength is in ucs2 units. ********************************************************************/ smb_ucs2_t *safe_strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) @@ -856,12 +852,6 @@ smb_ucs2_t *safe_strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlen if (!src) return dest; - /* - * Convert maxlength to smb_ucs2_t units. - */ - - maxlength /= sizeof(smb_ucs2_t); - ucs2_src_len = strlen_w(src); ucs2_dest_len = strlen_w(dest); @@ -880,7 +870,7 @@ smb_ucs2_t *safe_strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlen } /******************************************************************* - Compare the two strings s1 and s2. len is in ucs2 units. + Compare the two strings s1 and s2. ********************************************************************/ int strcmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) @@ -1245,6 +1235,7 @@ int StrCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t) /******************************************************************* Case insensitive string compararison, length limited. + n is in ucs2 units. ********************************************************************/ int StrnCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t, size_t n) @@ -1289,7 +1280,8 @@ BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) } /******************************************************************* - Compare 2 strings up to and including the nth char. + Compare 2 strings up to and including the nth char. n is in ucs2 + units. ******************************************************************/ BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n) @@ -1504,19 +1496,13 @@ BOOL str_is_all_w(const smb_ucs2_t *s,smb_ucs2_t c) Paranoid strcpy into a buffer of given length (includes terminating zero. Strips out all but 'a-Z0-9' and replaces with '_'. Deliberately does *NOT* check for multibyte characters. Don't change it ! - maxlength is in bytes. + maxlength is in ucs2 units. ********************************************************************/ smb_ucs2_t *alpha_strcpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) { size_t len, i; - /* - * Convert to smb_ucs2_t units. - */ - - maxlength /= sizeof(smb_ucs2_t); - if (!dest) { DEBUG(0,("ERROR: NULL dest in alpha_strcpy_w\n")); return NULL; @@ -1546,7 +1532,8 @@ smb_ucs2_t *alpha_strcpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxle /**************************************************************************** Like strncpy but always null terminates. Make sure there is room ! - The variable n should always be one less than the available size and is in bytes. + The variable n should always be one less than the available size and is in + ucs2 units. ****************************************************************************/ smb_ucs2_t *StrnCpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src,size_t n) @@ -1559,12 +1546,6 @@ smb_ucs2_t *StrnCpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src,size_t n) return(dest); } - /* - * Convert to smb_ucs2_t units. - */ - - n /= sizeof(smb_ucs2_t); - while (n-- && (*d++ = *src++)) ; *d = 0; @@ -1574,7 +1555,7 @@ smb_ucs2_t *StrnCpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src,size_t n) /**************************************************************************** Like strncpy but copies up to the character marker. Always null terminates. returns a pointer to the character marker in the source string (src). - n is in bytes. + n is in ucs2 units. ****************************************************************************/ smb_ucs2_t *strncpyn_w(smb_ucs2_t *dest, const smb_ucs2_t *src,size_t n, smb_ucs2_t c) @@ -1593,7 +1574,7 @@ smb_ucs2_t *strncpyn_w(smb_ucs2_t *dest, const smb_ucs2_t *src,size_t n, smb_ucs return NULL; } - str_len = PTR_DIFF(p, src) + sizeof(smb_ucs2_t); + str_len = PTR_DIFF(p, src) + 1; safe_strcpy_w(dest, src, MIN(n, str_len)); return p; @@ -1751,7 +1732,7 @@ BOOL string_set_w(smb_ucs2_t **dest,const smb_ucs2_t *src) Any of " ; ' $ or ` in the insert string are replaced with _ if len==0 then no length check is performed - len is in bytes. + len is in ucs2 units. ****************************************************************************/ void string_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t *insert, size_t len) @@ -1759,12 +1740,6 @@ void string_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t *inse smb_ucs2_t *p; ssize_t ls,lp,li, i; - /* - * Convert to smb_ucs2_t units. - */ - - len /= sizeof(smb_ucs2_t); - if (!insert || !pattern || !s) return; @@ -1828,12 +1803,6 @@ void all_string_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t * smb_ucs2_t *p; ssize_t ls,lp,li; - /* - * Convert to smb_ucs2_t units. - */ - - len /= sizeof(smb_ucs2_t); - if (!insert || !pattern || !s) return; @@ -1905,17 +1874,11 @@ smb_ucs2_t *octal_string_w(int i) /**************************************************************************** Truncate a string at a specified length. - length is in bytes. + length is in ucs2 units. ****************************************************************************/ smb_ucs2_t *string_truncate_w(smb_ucs2_t *s, size_t length) { - /* - * Convert to smb_ucs2_t units. - */ - - length /= sizeof(smb_ucs2_t); - if (s && strlen_w(s) > length) s[length] = 0; -- cgit From 6814d0f45689d2a17af3d95511208421d10f5fce Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 22 Mar 2000 20:39:31 +0000 Subject: Correctly convert from little-endian UNICODE to dos_codepage when doing secure file create. Jeremy. (This used to be commit 90134dd13721f60d4fb05ce9434b65e95ff09629) --- source3/lib/util_unistr.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c981aba51a..27bd615bf4 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -149,6 +149,8 @@ char *skip_unicode_string(char *buf,int n) /******************************************************************* Return a DOS codepage version of a little-endian unicode string. + len is the filename length (ignoring any terminating zero) in uin16 + units. Always null terminates. Hack alert: uses fixed buffer(s). ********************************************************************/ -- cgit From 18bc76a0c6830358a137b4198e17b1b7ce92b9bf Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 27 Mar 2000 12:38:45 +0000 Subject: changed the definition of dos_PutUniCode the previous definition could result is us overflowing a buffer. The null termination was always added yet the size returned did not include the null termination. the new function takes a BOOL null_terminate, and always returns the total number of bytes consumed by the string. (This used to be commit 426c90433396a95033eefcc4af97603abc934221) --- source3/lib/util_unistr.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 27bd615bf4..6983d1100a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -46,11 +46,13 @@ static uint16 *ucs2_to_unixcp; the current DOS codepage. len is the length in bytes of the string pointed to by dst. - the return value is the length of the string *without* the trailing - two bytes of zero + if null_terminate is True then null terminate the packet (adds 2 bytes) + + the return value is the length consumed by the string, including the + null termination if applied ********************************************************************/ -int dos_PutUniCode(char *dst,const char *src, ssize_t len) +int dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) { int ret = 0; while (*src && (len > 2)) { @@ -74,7 +76,10 @@ int dos_PutUniCode(char *dst,const char *src, ssize_t len) else src++; } - SSVAL(dst,ret,0); + if (null_terminate) { + SSVAL(dst,ret,0); + ret += 2; + } return(ret); } -- cgit From 03630a9aa9ce5c00619ad3c4bb498d95054ae03a Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 11 Apr 2000 13:47:17 +0000 Subject: added a cheap and nasty skip_unibuf() fn to allow easier merging from TNG out unicode string handling functions need a lot of work (This used to be commit 707401fc1e697362cdaadcfaac4edc964b80b1a0) --- source3/lib/util_unistr.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 6983d1100a..8a02878c13 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -137,6 +137,16 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) *dest = 0; } +/* from TNG - should be fixed */ +char *skip_unibuf(char *src, int len) +{ + char *srcend = src + len; + + while (src < srcend && SVAL(src,0)) src += 2; + + return src; +} + /******************************************************************* Skip past some unicode strings in a buffer. -- cgit From 4b7850663e209fbcd650bcacab3d61655e829ed2 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 15 Apr 2000 07:14:40 +0000 Subject: a quick hack to reduce the size of the unicode map table headers from 3MB to 250k. I split the table into 3 sections, after noticing that 5/6 of the table was empty. (This used to be commit c1496736bbdb7f6bf1eb43a54f883e5f41a4d39e) --- source3/lib/util_unistr.c | 48 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8a02878c13..3144e58b2a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1030,6 +1030,11 @@ smb_ucs2_t *strdup_w(const smb_ucs2_t *s) /******************************************************************* Mapping tables for UNICODE character. Allows toupper/tolower and isXXX functions to work. + + tridge: split into 2 pieces. This saves us 5/6 of the memory + with a small speed penalty + The magic constants are the lower/upper range of the tables two + parts ********************************************************************/ typedef struct { @@ -1038,17 +1043,42 @@ typedef struct { unsigned char flags; } smb_unicode_table_t; -static smb_unicode_table_t map_table[] = { -#include "unicode_map_table.h" +static smb_unicode_table_t map_table1[] = { +#include "unicode_map_table1.h" +}; + +static smb_unicode_table_t map_table2[] = { +#include "unicode_map_table2.h" }; +static unsigned char map_table_flags(smb_ucs2_t v) +{ + if (v < 9450) return map_table1[v].flags; + if (v >= 64256) return map_table2[v - 64256].flags; + return 0; +} + +static smb_ucs2_t map_table_lower(smb_ucs2_t v) +{ + if (v < 9450) return map_table1[v].lower; + if (v >= 64256) return map_table2[v - 64256].lower; + return v; +} + +static smb_ucs2_t map_table_upper(smb_ucs2_t v) +{ + if (v < 9450) return map_table1[v].upper; + if (v >= 64256) return map_table2[v - 64256].upper; + return v; +} + /******************************************************************* Is an upper case wchar. ********************************************************************/ int isupper_w( smb_ucs2_t val) { - return (map_table[val].flags & UNI_UPPER); + return (map_table_flags(val) & UNI_UPPER); } /******************************************************************* @@ -1057,7 +1087,7 @@ int isupper_w( smb_ucs2_t val) int islower_w( smb_ucs2_t val) { - return (map_table[val].flags & UNI_LOWER); + return (map_table_flags(val) & UNI_LOWER); } /******************************************************************* @@ -1066,7 +1096,7 @@ int islower_w( smb_ucs2_t val) int isdigit_w( smb_ucs2_t val) { - return (map_table[val].flags & UNI_DIGIT); + return (map_table_flags(val) & UNI_DIGIT); } /******************************************************************* @@ -1075,7 +1105,7 @@ int isdigit_w( smb_ucs2_t val) int isxdigit_w( smb_ucs2_t val) { - return (map_table[val].flags & UNI_XDIGIT); + return (map_table_flags(val) & UNI_XDIGIT); } /******************************************************************* @@ -1084,7 +1114,7 @@ int isxdigit_w( smb_ucs2_t val) int isspace_w( smb_ucs2_t val) { - return (map_table[val].flags & UNI_SPACE); + return (map_table_flags(val) & UNI_SPACE); } /******************************************************************* @@ -1093,7 +1123,7 @@ int isspace_w( smb_ucs2_t val) smb_ucs2_t toupper_w( smb_ucs2_t val ) { - return map_table[val].upper; + return map_table_upper(val); } /******************************************************************* @@ -1102,7 +1132,7 @@ smb_ucs2_t toupper_w( smb_ucs2_t val ) smb_ucs2_t tolower_w( smb_ucs2_t val ) { - return map_table[val].lower; + return map_table_lower(val); } static smb_ucs2_t *last_ptr = NULL; -- cgit From b8c51212db9104873fc5eceb853ae0a630299059 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 15 Apr 2000 08:07:11 +0000 Subject: use macros for table boundaries (This used to be commit a82a5acff2ad2120f5480f0cb08550e95d80c59e) --- source3/lib/util_unistr.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 3144e58b2a..0c88a0c7c7 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1043,6 +1043,9 @@ typedef struct { unsigned char flags; } smb_unicode_table_t; +#define TABLE1_BOUNDARY 9450 +#define TABLE2_BOUNDARY 64256 + static smb_unicode_table_t map_table1[] = { #include "unicode_map_table1.h" }; @@ -1053,22 +1056,22 @@ static smb_unicode_table_t map_table2[] = { static unsigned char map_table_flags(smb_ucs2_t v) { - if (v < 9450) return map_table1[v].flags; - if (v >= 64256) return map_table2[v - 64256].flags; + if (v < TABLE1_BOUNDARY) return map_table1[v].flags; + if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].flags; return 0; } static smb_ucs2_t map_table_lower(smb_ucs2_t v) { - if (v < 9450) return map_table1[v].lower; - if (v >= 64256) return map_table2[v - 64256].lower; + if (v < TABLE1_BOUNDARY) return map_table1[v].lower; + if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].lower; return v; } static smb_ucs2_t map_table_upper(smb_ucs2_t v) { - if (v < 9450) return map_table1[v].upper; - if (v >= 64256) return map_table2[v - 64256].upper; + if (v < TABLE1_BOUNDARY) return map_table1[v].upper; + if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].upper; return v; } -- cgit From 6259f51dd9918eccc9697f3763d918f7c9b82b50 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 22 Apr 2000 00:33:16 +0000 Subject: This is a *big* checkin that may break some things, but implements the new open mechanism Andrew & I discussed. config.sub: configure: Included the QNX patch. include/vfs.h: smbd/vfs-wrap.c: smbd/vfs.c: Added ftruncate vfs call (needed). Note that we will also need locking calls in the vfs (to be added). lib/util_unistr.c: nmbd/nmbd_processlogon.c: Fix for NT domain logons causing nmbd to core dump. Also fix for sidsize DOS bug. locking/locking.c: Check value of ret before using it for memdup. printing/printing.c: Convert print_fsp_open to return an allocated fsp. rpc_server/srv_lsa.c: Fix for NT domain logons. I have removed all use of lp_share_modes() from the code (although I left the parameter in the table for backwards compatibility). It no longer makes sense for this to exist. smbd/close.c: Removed lp_share_modes(). smbd/fileio.c: Fixed parameters to unlock_share_entry call in panic code. smbd/files.c: Correctly set the unix_ERR_code to ERRnofids on fsp allocation fail. smbd/nttrans.c: smbd/reply.c: smbd/trans2.c: Changed all occurrences of open_file_shared/open_directory/ open_file_stat to return an fsp from the call. smbd/open.c: Changed all occurrences of open_file_shared/open_directory/ open_file_stat to return an fsp from the call. In addition I have fixed a long standing race condition in the deny mode processing w.r.t. two smbd's creating a file. Andrew, please note that your original idea of using open with O_EXCL in this case would not work (I went over the races very carefully) and so we must re-check deny modes *after* the open() call returns. This is because there is a race between the open with O_EXCL and the lock of the share mode entry. Imagine the case where the first smbd does the open with O_EXCL and a deny mode of DENY_ALL, but is pre-empted before it locks the share modes and creates the deny mode entry for DENY_ALL. A second smbd could then come in with O_RDONLY and a deny mode of DENY_NONE and the two opens would be allowed. The *only* way to fix this race is to lock the share modes after the open and then do the deny mode checks *after* this lock in the case where the file did not originally exist. This code will need extensive testing but seems to initially work. Jeremy. (This used to be commit ab0ecc39d688f16b9692fe90b991f0b89287070a) --- source3/lib/util_unistr.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 0c88a0c7c7..71ef32a1fd 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -137,29 +137,22 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) *dest = 0; } -/* from TNG - should be fixed */ -char *skip_unibuf(char *src, int len) -{ - char *srcend = src + len; - - while (src < srcend && SVAL(src,0)) src += 2; - - return src; -} - - /******************************************************************* - Skip past some unicode strings in a buffer. + Skip past a unicode string, but not more than len. Always move + past a terminating zero if found. ********************************************************************/ -char *skip_unicode_string(char *buf,int n) +char *skip_unibuf(char *src, size_t len) { - while (n--) { - while (*buf) - buf += 2; - buf += 2; - } - return(buf); + char *srcend = src + len; + + while (src < srcend && SVAL(src,0)) + src += 2; + + if(!SVAL(src,0)) + src += 2; + + return src; } /******************************************************************* -- cgit From 700f72453ed8dfd356a5591b9447127b5066ac4b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 30 Apr 2000 11:04:28 +0000 Subject: - removed all our old wildcard matching code and replaced it with a call to ms_fnmatch(). This also removes all the Win9X semantics stuff and a bunch of other associated cruft. - moved the stat cache code into statcache.c - fixed the uint16 alignment requirements of ascii_to_unistr() and unistr_to_ascii() - trans2 SMB_FIND_FILE_BOTH_DIRECTORY_INFO returns the short name as unicode always (at least thats what NT4 does) - fixed some errors in the in-memory tdb code. Still ugly, but doesn't crash as much (This used to be commit 03e9cea004bbba72161a5323cf3b4556c94aed8e) --- source3/lib/util_unistr.c | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 71ef32a1fd..0ca148ba94 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -84,52 +84,45 @@ int dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) } /******************************************************************* - Put an ASCII string into a UNICODE array (uint16's). + Put an ASCII string into a UNICODE string. Warning: doesn't do any codepage !!! BAD !!! Help ! Fix Me ! Fix Me ! ********************************************************************/ -void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) +void ascii_to_unistr(char *dest, const char *src, int maxlen) { - uint16 *destend = dest + maxlen; - register char c; + char *destend = dest + maxlen; + char c; - while (dest < destend) - { + while (dest < destend) { c = *(src++); - if (c == 0) - { - break; - } + if (c == 0) break; - *(dest++) = (uint16)c; + SSVAL(dest, 0, (uint16)c); + dest += 2; } - - *dest = 0; + SSVAL(dest, 0, 0); } /******************************************************************* - Pull an ASCII string out of a UNICODE array (uint16's). + Pull an ASCII string out of a UNICODE array . Warning: doesn't do any codepage !!! BAD !!! Help ! Fix Me ! Fix Me ! ********************************************************************/ -void unistr_to_ascii(char *dest, const uint16 *src, int len) +void unistr_to_ascii(char *dest, char *src, int len) { char *destend = dest + len; - register uint16 c; + uint16 c; - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; - } + while (dest < destend) { + c = SVAL(src, 0); + src += 2; + if (c == 0) break; *(dest++) = (char)c; } -- cgit From 612738a9e14b6fb6a2687993d6416bbe6c3ea94d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 10 May 2000 22:47:09 +0000 Subject: lib/util_unistr.c: libsmb/clilist.c: rpc_server/srv_spoolss_nt.c: smbd/trans2.c: Changed unistr_to_ascii to unistr_to_dos - do codepage conversion. msdfs/msdfs.c: Removed stub unistr_to_dos. libsmb/pwd_cache.c: Removed obfuscation functions as they don't do anything and don't add any security. Jeremy. (This used to be commit 1ed146467e764e6a81d8f78cd58fb5765ebf5d21) --- source3/lib/util_unistr.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 0ca148ba94..4a66adc0fd 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -91,7 +91,7 @@ int dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) Help ! Fix Me ! Fix Me ! ********************************************************************/ -void ascii_to_unistr(char *dest, const char *src, int maxlen) +void ascii_to_unistr(char *dest, const char *src, size_t maxlen) { char *destend = dest + maxlen; char c; @@ -107,24 +107,28 @@ void ascii_to_unistr(char *dest, const char *src, int maxlen) } /******************************************************************* - Pull an ASCII string out of a UNICODE array . - - Warning: doesn't do any codepage !!! BAD !!! - - Help ! Fix Me ! Fix Me ! + Pull a DOS codepage string out of a UNICODE array. len is in bytes. ********************************************************************/ -void unistr_to_ascii(char *dest, char *src, int len) +void unistr_to_dos(char *dest, char *src, size_t len) { char *destend = dest + len; - uint16 c; while (dest < destend) { - c = SVAL(src, 0); + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; + src += 2; - if (c == 0) break; - *(dest++) = (char)c; + if (ucs2_val == 0) + break; + + if (cp_val < 256) + *dest++ = (char)cp_val; + else { + *dest++ = (cp_val >> 8) & 0xff; + *dest++ = (cp_val & 0xff); + } } *dest = 0; -- cgit From fa659fea9b0b71bf62de9dfec61160196c828da1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 12 May 2000 14:28:48 +0000 Subject: Added unicode_to_dos_char() function to address converting single UNICODE characters to one or more DOS codepage characters. Jeremy. (This used to be commit eefbfb5e16fcf40f335edc840a49f837f6b64111) --- source3/lib/util_unistr.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 4a66adc0fd..c28ef44934 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -779,6 +779,28 @@ char *unicode_to_dos(char *dst, const smb_ucs2_t *src, size_t dst_len) return unicode_to_multibyte(dst, src, dst_len, ucs2_to_doscp); } +/******************************************************************* + Convert a single UNICODE character to DOS codepage. Returns the + number of bytes in the DOS codepage character. +********************************************************************/ + +size_t unicode_to_dos_char(char *dst, const smb_ucs2_t src) +{ + smb_ucs2_t val = ucs2_to_doscp[src]; + if(val < 256) { + *dst = (char)val; + return (size_t)1; + } + /* + * A 2 byte value is always written as + * high/low into the buffer stream. + */ + + dst[0] = (char)((val >> 8) & 0xff); + dst[1] = (char)(val & 0xff); + return (size_t)2; +} + /******************************************************************* Convert a DOS string to UNICODE format. Note that the 'dst' is in native byte order, not little endian. Always zero terminates. -- cgit From 682cccd8af4008b508328672009a5bfc1344f55d Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Sat, 27 May 2000 02:05:15 +0000 Subject: unistr_to_dos not unistr_to_ascii (This used to be commit f46c4fe876a0cabe7044ca30c17e6c217d618635) --- source3/lib/util_unistr.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c28ef44934..7379bd6811 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -106,6 +106,28 @@ void ascii_to_unistr(char *dest, const char *src, size_t maxlen) SSVAL(dest, 0, 0); } +/******************************************************************* + * HORRIBLE HACK! +********************************************************************/ +void unistr_to_ascii(char *dest, const uint16 *src, int len) +{ + char *destend = dest + len; + register uint16 c; + + while (dest < destend) + { + c = *(src++); + if (c == 0) + { + break; + } + + *(dest++) = (char)c; + } + + *dest = 0; +} + /******************************************************************* Pull a DOS codepage string out of a UNICODE array. len is in bytes. ********************************************************************/ -- cgit From f0080e5a3979fac94d6668cf6ee9d9f61302839c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 1 Jun 2000 17:01:34 +0000 Subject: Getting back to a compilable state (not there yet but close). Added patches for random -> sys_random. Added set_effective_xxx patches for AFS code. Memory allocation changes in spoolss code. Jeremy. (This used to be commit c2099cfb033c2cdb6035f4f7f50ce21b98e1584d) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 7379bd6811..792cf2384a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -132,7 +132,7 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) Pull a DOS codepage string out of a UNICODE array. len is in bytes. ********************************************************************/ -void unistr_to_dos(char *dest, char *src, size_t len) +void unistr_to_dos(char *dest, const char *src, size_t len) { char *destend = dest + len; -- cgit From f9e9f98a4a579f24d8ad1804b22bf36ede250e23 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 22 Jun 2000 01:39:17 +0000 Subject: lib/util_unistr.c: Removed ascii_to_unistr() as it does no codepage. Removed unistr_to_ascii() as it was never used. printing/nt_printing.c: Removed "DUMMY.XX" files. rpc_server/srv_spoolss_nt.c: Use dos_PutUniCode() instead of ascii_to_unistr(). Attempted to fix the "return value" size code based on J.F's comments. This needs looking at. Jeremy. (This used to be commit de99011bf3b2a23bd1854a047382a107aaeb9c68) --- source3/lib/util_unistr.c | 51 +++-------------------------------------------- 1 file changed, 3 insertions(+), 48 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 792cf2384a..d4a159a28e 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -48,13 +48,13 @@ static uint16 *ucs2_to_unixcp; if null_terminate is True then null terminate the packet (adds 2 bytes) - the return value is the length consumed by the string, including the + the return value is the length in bytes consumed by the string, including the null termination if applied ********************************************************************/ -int dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) +size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) { - int ret = 0; + size_t ret = 0; while (*src && (len > 2)) { size_t skip = get_character_len(*src); smb_ucs2_t val = (*src & 0xff); @@ -83,51 +83,6 @@ int dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) return(ret); } -/******************************************************************* - Put an ASCII string into a UNICODE string. - - Warning: doesn't do any codepage !!! BAD !!! - - Help ! Fix Me ! Fix Me ! -********************************************************************/ - -void ascii_to_unistr(char *dest, const char *src, size_t maxlen) -{ - char *destend = dest + maxlen; - char c; - - while (dest < destend) { - c = *(src++); - if (c == 0) break; - - SSVAL(dest, 0, (uint16)c); - dest += 2; - } - SSVAL(dest, 0, 0); -} - -/******************************************************************* - * HORRIBLE HACK! -********************************************************************/ -void unistr_to_ascii(char *dest, const uint16 *src, int len) -{ - char *destend = dest + len; - register uint16 c; - - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; - } - - *(dest++) = (char)c; - } - - *dest = 0; -} - /******************************************************************* Pull a DOS codepage string out of a UNICODE array. len is in bytes. ********************************************************************/ -- cgit From 5dd2bd50768ef57072e27b42faf7790875c7bae8 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 23 Jun 2000 17:31:38 +0000 Subject: lib/util_unistr.c: Off-by-one fix for dos_PutUniStr from John Reilly jreilly@hp.com. Memory leak fix for new sec_ctx code (sorry Tim :-). Jeremy. (This used to be commit edaf49c66d5a5ccf6689b358c301e208599a468f) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index d4a159a28e..1c13ff2758 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -55,7 +55,7 @@ static uint16 *ucs2_to_unixcp; size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) { size_t ret = 0; - while (*src && (len > 2)) { + while (*src && (len >= 2)) { size_t skip = get_character_len(*src); smb_ucs2_t val = (*src & 0xff); -- cgit From d2b40a7de259377d937492acedd39988ddd108a4 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 7 Jul 2000 06:20:46 +0000 Subject: More rpcclient merge issues: * fixes some readline bugs from the merge * first attempt at commands (spoolenum almost works) * no changes to existing functions in HEAD; only additions of new functions. I'll weed out what I can as I go. --jerry (This used to be commit 61d2aad5dc2b212b11c981f1eca47efa627e9fc8) --- source3/lib/util_unistr.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 1c13ff2758..42f1dc0644 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -221,6 +221,52 @@ char *dos_unistr2_to_str(UNISTR2 *str) return lbuf; } +/******************************************************************* + Put an ASCII string into a UNICODE array (uint16's). + ********************************************************************/ +void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) +{ + uint16 *destend = dest + maxlen; + register char c; + + while (dest < destend) + { + c = *(src++); + if (c == 0) + { + break; + } + + *(dest++) = (uint16)c; + } + + *dest = 0; +} + + +/******************************************************************* + Pull an ASCII string out of a UNICODE array (uint16's). + ********************************************************************/ + +void unistr_to_ascii(char *dest, const uint16 *src, int len) +{ + char *destend = dest + len; + register uint16 c; + + while (dest < destend) + { + c = *(src++); + if (c == 0) + { + break; + } + + *(dest++) = (char)c; + } + + *dest = 0; +} + /******************************************************************* Convert a UNISTR2 structure to an ASCII string Warning: this version does DOS codepage. -- cgit From f488740112449ca49d75d411a920edc6a605ff02 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Thu, 10 Aug 2000 14:00:05 +0000 Subject: Deal will NULL UNISTR in unistr_to_ascii jerry (This used to be commit cfa4c878a2ccaf346940f8f4a201737e185f76d6) --- source3/lib/util_unistr.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 42f1dc0644..0817dcf072 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -252,19 +252,27 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) { char *destend = dest + len; register uint16 c; - - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; - } - - *(dest++) = (char)c; - } - - *dest = 0; + + /* deal with NULL src strings */ + if (src == NULL) + { + *dest = '\0'; + } + else + { + while (dest < destend) + { + c = *(src++); + if (c == 0) + { + break; + } + + *(dest++) = (char)c; + } + + *dest = 0; + } } /******************************************************************* -- cgit From cf023a7ce0ba08f3b7e37fecf8219e83c04ed418 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Sat, 12 Aug 2000 14:20:40 +0000 Subject: fixed unistr_to_ascii to deal with NULL src strings jerry (This used to be commit 8e95aae1709a9be28d6e25ff6f0fdc729dc09274) --- source3/lib/util_unistr.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 0817dcf072..b786d0c98b 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -253,26 +253,26 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) char *destend = dest + len; register uint16 c; - /* deal with NULL src strings */ if (src == NULL) { *dest = '\0'; + return; } - else + + /* normal code path for a valid 'src' */ + while (dest < destend) { - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; - } - - *(dest++) = (char)c; - } - - *dest = 0; + c = *(src++); + if (c == 0) + { + break; + } + + *(dest++) = (char)c; } + + *dest = 0; + return; } /******************************************************************* -- cgit From 3ad2ee22bb4ecee24d069bb7efadff18c25044d1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 3 Oct 2000 02:12:14 +0000 Subject: utf-8 and EUC3 patch from Hiroshi Miura Samba User Group Japan staff. mkdir high bits patch from Robert Dahlem" . jeremy. (This used to be commit b40191d27180ab1e59935086073c4d312552f717) --- source3/lib/util_unistr.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index b786d0c98b..93f5490ffc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1977,3 +1977,17 @@ smb_ucs2_t *string_truncate_w(smb_ucs2_t *s, size_t length) return s; } + +/****************************************************************** + functions for UTF8 support (using in kanji.c) + ******************************************************************/ +smb_ucs2_t doscp2ucs2(int w) +{ + return ((smb_ucs2_t)doscp_to_ucs2[w]); +} + +int ucs2doscp(smb_ucs2_t w) +{ + return ((int)ucs2_to_doscp[w]); +} + -- cgit From 23f78fd7b91878176c518471cdca84cad826cba9 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 4 Oct 2000 01:03:23 +0000 Subject: Adding Herb's compile warning fixes to HEAD. Jeremy. (This used to be commit d131ad1ce3f6e72e295f865a463f8dcbfa6f8d42) --- source3/lib/util_unistr.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 93f5490ffc..3282761ff8 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -945,8 +945,8 @@ int strcmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) return c1 - c2; if (c1 == 0) - return 0; - } + break; + } return 0; } @@ -966,9 +966,9 @@ int strncmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2, size_t len) return c1 - c2; if (c1 == 0) - return 0; + break; - } + } return 0; } -- cgit From d5087941183144c68bc02aa32da52c175e9aaa30 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 10 Oct 2000 18:34:44 +0000 Subject: Fixed nasty size wrong bug spotted by the eagle eyes of JF :-). Jeremy. (This used to be commit 443293a06530d0a5421b39d9a6a224d6ae316bd3) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 3282761ff8..ad387b4e9d 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -201,7 +201,7 @@ char *dos_unistr2_to_str(UNISTR2 *str) char *lbuf = lbufs[nexti]; char *p; uint16 *src = str->buffer; - int max_size = MIN(sizeof(str->buffer)-3, str->uni_str_len); + int max_size = MIN(MAXUNI-3, str->uni_str_len); nexti = (nexti+1)%8; -- cgit From 7d7c9a2f794ab587c56be08b6900e9887d3fa0c3 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 1 Dec 2000 01:14:55 +0000 Subject: Use lp_codepagedir() instead of CODEPAGEDIR when loading the unicode map. Found by Joseph Cheek (This used to be commit ec74fdc631fbd5f0b5450df02acb9cd98c153823) --- source3/lib/util_unistr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index ad387b4e9d..74ecc841d0 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -547,12 +547,13 @@ BOOL load_unicode_map(const char *codepage, smb_ucs2_t **pp_cp_to_ucs2, uint16 * if (*codepage == '\0') goto clean_and_exit; - if(strlen(CODEPAGEDIR) + 13 + strlen(codepage) > sizeof(unicode_map_file_name)) { + if(strlen(lp_codepagedir()) + 13 + strlen(codepage) > + sizeof(unicode_map_file_name)) { DEBUG(0,("load_unicode_map: filename too long to load\n")); goto clean_and_exit; } - pstrcpy(unicode_map_file_name, CODEPAGEDIR); + pstrcpy(unicode_map_file_name, lp_codepagedir()); pstrcat(unicode_map_file_name, "/"); pstrcat(unicode_map_file_name, "unicode_map."); pstrcat(unicode_map_file_name, codepage); -- cgit From 89af6fd745a6f49668bae5b5c2d239d3671fb299 Mon Sep 17 00:00:00 2001 From: Jean-François Micouleau Date: Fri, 15 Dec 2000 09:31:56 +0000 Subject: lib/util_unistr.c: rewrote unistr2_to_ascii() to correct a bug seen on SGI boxes. rpc_parse/parse_misc.c: rpc_parse/parse_prs.c: rewrote of BUFFER5 handling to NOT byteswap when it was already in network byte order. rpc_parse/parse_samr.c: cleanup of samr_io_q_lookup_domain(), remove the over-parsing by 2 bytes. rpc_server/srv_lsa.c: UNISTR2 strings need to be NULL terminated to pleased W2K. rpc_server/srv_spoolss_nt.c: use snprintf instead of safe_strcpy as we want the string truncated at 32 chars. That should fix SUN and SGI box not able to act as printserver and the problem with joining from a W2K wks. J.F. (This used to be commit 69fe739303b105f2c488f266f13977da1b6b201d) --- source3/lib/util_unistr.c | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 74ecc841d0..5e86d5db0b 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -282,38 +282,37 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) { - char *destend; - const uint16 *src; + char *p; + uint16 *src; size_t len; - register uint16 c; + int i; + + if (str == NULL) { + *dest='\0'; + return; + } src = str->buffer; len = MIN(str->uni_str_len, maxlen); - destend = dest + len; - while (dest < destend) - { - uint16 ucs2_val; - uint16 cp_val; + if (len == 0) { + *dest='\0'; + return; + } + + for (p = dest; *src && p-dest < len; src++) { + uint16 ucs2_val = SVAL(src,0); + uint16 cp_val = ucs2_to_doscp[ucs2_val]; - c = *src; - if (c == 0) - { - break; - } - - ucs2_val = SVAL(src++,0); - cp_val = ucs2_to_doscp[ucs2_val]; - if (cp_val < 256) - *(dest++) = (char)cp_val; + *p++ = (char)cp_val; else { - *dest= (cp_val >> 8) & 0xff; - *(dest++) = (cp_val & 0xff); + *p = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); } } - - *dest = 0; + + *p = 0; } -- cgit From fb82ab78fe556656eec605d532e0dabb2f815573 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 15 Dec 2000 23:02:01 +0000 Subject: Never free anything in the rpc_parse/prs_XXX functions. Do it in the enclosing function. lib/util_unistr.c: Check lengths *before* reading source - prevent uninitialised memory reads. Jeremy. (This used to be commit ce4f461965c872fbfc9fe5f6b98aed58bb3dd67a) --- source3/lib/util_unistr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5e86d5db0b..259b44e200 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -300,7 +300,7 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) return; } - for (p = dest; *src && p-dest < len; src++) { + for (p = dest; (p-dest < len) && *src; src++) { uint16 ucs2_val = SVAL(src,0); uint16 cp_val = ucs2_to_doscp[ucs2_val]; @@ -341,7 +341,7 @@ char *dos_buffer2_to_str(BUFFER2 *str) nexti = (nexti+1)%8; - for (p = lbuf; *src && p-lbuf < max_size; src++) { + for (p = lbuf; (p-lbuf < max_size) && *src; src++) { uint16 ucs2_val = SVAL(src,0); uint16 cp_val = ucs2_to_doscp[ucs2_val]; @@ -405,7 +405,7 @@ size_t dos_struni2(char *dst, const char *src, size_t max_len) return 0; if (src != NULL) { - for (; *src && len < max_len-2; len++, dst +=2) { + for (; (len < max_len-2) && *src; len++, dst +=2) { size_t skip = get_character_len(*src); smb_ucs2_t val = (*src & 0xff); @@ -444,7 +444,7 @@ char *dos_unistr(char *buf) nexti = (nexti+1)%8; - for (p = lbuf; *src && p-lbuf < MAXUNI-3; src++) { + for (p = lbuf; (p-lbuf < MAXUNI-3) && *src; src++) { uint16 ucs2_val = SVAL(src,0); uint16 cp_val = ucs2_to_doscp[ucs2_val]; @@ -712,7 +712,7 @@ static char *unicode_to_multibyte(char *dst, const smb_ucs2_t *src, { size_t dst_pos; - for(dst_pos = 0; *src && (dst_pos < dst_len - 1);) { + for(dst_pos = 0; (dst_pos < dst_len - 1) && *src;) { smb_ucs2_t val = ucs2_to_cp[*src++]; if(val < 256) { dst[dst_pos++] = (char)val; -- cgit From e28f0a1f55fb8184f1edfb0bd8333f3d71e12225 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 18 Dec 2000 20:03:15 +0000 Subject: A couple more UMR fixes. Jeremy. (This used to be commit a83702c99f1ecd6e68f8c178b20823fce6d4c6c6) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 259b44e200..c989f589a5 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -176,7 +176,7 @@ char *dos_unistr2(uint16 *src) nexti = (nexti+1)%8; - for (p = lbuf; *src && (p-lbuf < MAXUNI-3); src++) { + for (p = lbuf; (p-lbuf < MAXUNI-3) && *src; src++) { uint16 ucs2_val = SVAL(src,0); uint16 cp_val = ucs2_to_doscp[ucs2_val]; @@ -205,7 +205,7 @@ char *dos_unistr2_to_str(UNISTR2 *str) nexti = (nexti+1)%8; - for (p = lbuf; *src && p-lbuf < max_size; src++) { + for (p = lbuf; (p-lbuf < max_size) && *src; src++) { uint16 ucs2_val = SVAL(src,0); uint16 cp_val = ucs2_to_doscp[ucs2_val]; -- cgit From 4ac52fc52f451096a8c23fcf9116518cb4fe2180 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 20 Dec 2000 20:43:48 +0000 Subject: Removed uninitialised variable. Jeremy. (This used to be commit 56a93567a106f25e87df093e1861f6bdd1d3196a) --- source3/lib/util_unistr.c | 1 - 1 file changed, 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c989f589a5..8661f724fe 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -285,7 +285,6 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) char *p; uint16 *src; size_t len; - int i; if (str == NULL) { *dest='\0'; -- cgit From 6492d6b2f6a2743f5e794447911cbbba7e031d5d Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 20 Feb 2001 08:09:06 +0000 Subject: initial client side unicode support (needed for netapp filer) I've currently got this code disabled by default as it is incomplete. You enable it by setting a USE_UNICODE environment variable. Once the support is complete this check will be removed and the CAP_UNICODE capability bit will be the sole determination of whether the client library code uses unicode right now I have converted session_setup and tconx. I will do more fns over the next few days. see clistr.c for the new client side string interface. Luckily it tends to make the code smaller and neater while adding unicode support. (This used to be commit e1a04e621f1c28d8e6e543d43741ca0272e2237f) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8661f724fe..3cbbc6f2da 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -852,7 +852,7 @@ size_t strlen_w(const smb_ucs2_t *src) { size_t len; - for(len = 0; *src; len++) + for(len = 0; *src++; len++) ; return len; -- cgit From e5f60ce0b33787372e24716cb4867f4cb8124471 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 21 Feb 2001 05:50:29 +0000 Subject: make ascii_to_unistr always use little-endian. This fn is never used except in my code so I know this is safe. otherwise unicode client lib doesn't work on big-endian hosts (This used to be commit 18ede716d10de1ac774cca82b4b8ff5ea1a9650d) --- source3/lib/util_unistr.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 3cbbc6f2da..5e6f6a5945 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -223,6 +223,7 @@ char *dos_unistr2_to_str(UNISTR2 *str) /******************************************************************* Put an ASCII string into a UNICODE array (uint16's). + use little-endian ucs2 ********************************************************************/ void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) { @@ -237,7 +238,8 @@ void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) break; } - *(dest++) = (uint16)c; + SSVAL(dest, 0, c); + dest++; } *dest = 0; @@ -262,7 +264,8 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) /* normal code path for a valid 'src' */ while (dest < destend) { - c = *(src++); + c = SVAL(src, 0); + src++; if (c == 0) { break; -- cgit From 0ef2179d23a2f9826e17c2b858fd8f2a9634b332 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 13 Mar 2001 01:44:05 +0000 Subject: Fixed reading of strings from big-endian RPC clients. Jeremy. (This used to be commit e7ecb9410ff2e4fcd33bca9f82e14c060590942a) --- source3/lib/util_unistr.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5e6f6a5945..b1cb9ec410 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -161,6 +161,42 @@ char *dos_unistrn2(uint16 *src, int len) return lbuf; } +/******************************************************************* + Return a DOS codepage version of a big or little endian unicode string. + len is the filename length (ignoring any terminating zero) in uin16 + units. Always null terminates. Endian is 1 if it's big endian. + Hack alert: uses fixed buffer(s). +********************************************************************/ + +char *rpc_unistrn2(uint16 *src, int len, BOOL endian) +{ + static char lbufs[8][MAXUNI]; + static int nexti; + char *lbuf = lbufs[nexti]; + char *p; + + nexti = (nexti+1)%8; + + for (p = lbuf; (len > 0) && (p-lbuf < MAXUNI-3) && *src; len--, src++) { + uint16 ucs2_val; + uint16 cp_val; + + RW_SVAL(True,endian,src,ucs2_val,0); + + cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); + } + } + + *p = 0; + return lbuf; +} + static char lbufs[8][MAXUNI]; static int nexti; @@ -221,6 +257,38 @@ char *dos_unistr2_to_str(UNISTR2 *str) return lbuf; } +/******************************************************************* +Return a DOS codepage version of a big or little-endian unicode string +********************************************************************/ + +char *rpc_unistr2_to_str(UNISTR2 *str, BOOL endian) +{ + char *lbuf = lbufs[nexti]; + char *p; + uint16 *src = str->buffer; + int max_size = MIN(MAXUNI-3, str->uni_str_len); + + nexti = (nexti+1)%8; + + for (p = lbuf; (p-lbuf < max_size) && *src; src++) { + uint16 ucs2_val; + uint16 cp_val; + + RW_SVAL(True,endian,src,ucs2_val,0); + cp_val = ucs2_to_doscp[ucs2_val]; + + if (cp_val < 256) + *p++ = (char)cp_val; + else { + *p++ = (cp_val >> 8) & 0xff; + *p++ = (cp_val & 0xff); + } + } + + *p = 0; + return lbuf; +} + /******************************************************************* Put an ASCII string into a UNICODE array (uint16's). use little-endian ucs2 -- cgit From 4ab6182a0ffdbe92a01dd6533e0862aa8d0f6d83 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 15 Mar 2001 00:49:13 +0000 Subject: AS/U on a sparc now joins and authenticates against a Samba PDC ! Jeremy. (This used to be commit 28a0bc5f5710aa732db662caa38f9da2138b5db2) --- source3/lib/util_unistr.c | 70 +---------------------------------------------- 1 file changed, 1 insertion(+), 69 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index b1cb9ec410..3ab4bdca9f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -161,42 +161,6 @@ char *dos_unistrn2(uint16 *src, int len) return lbuf; } -/******************************************************************* - Return a DOS codepage version of a big or little endian unicode string. - len is the filename length (ignoring any terminating zero) in uin16 - units. Always null terminates. Endian is 1 if it's big endian. - Hack alert: uses fixed buffer(s). -********************************************************************/ - -char *rpc_unistrn2(uint16 *src, int len, BOOL endian) -{ - static char lbufs[8][MAXUNI]; - static int nexti; - char *lbuf = lbufs[nexti]; - char *p; - - nexti = (nexti+1)%8; - - for (p = lbuf; (len > 0) && (p-lbuf < MAXUNI-3) && *src; len--, src++) { - uint16 ucs2_val; - uint16 cp_val; - - RW_SVAL(True,endian,src,ucs2_val,0); - - cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; - return lbuf; -} - static char lbufs[8][MAXUNI]; static int nexti; @@ -257,38 +221,6 @@ char *dos_unistr2_to_str(UNISTR2 *str) return lbuf; } -/******************************************************************* -Return a DOS codepage version of a big or little-endian unicode string -********************************************************************/ - -char *rpc_unistr2_to_str(UNISTR2 *str, BOOL endian) -{ - char *lbuf = lbufs[nexti]; - char *p; - uint16 *src = str->buffer; - int max_size = MIN(MAXUNI-3, str->uni_str_len); - - nexti = (nexti+1)%8; - - for (p = lbuf; (p-lbuf < max_size) && *src; src++) { - uint16 ucs2_val; - uint16 cp_val; - - RW_SVAL(True,endian,src,ucs2_val,0); - cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; - return lbuf; -} - /******************************************************************* Put an ASCII string into a UNICODE array (uint16's). use little-endian ucs2 @@ -347,7 +279,7 @@ void unistr_to_ascii(char *dest, const uint16 *src, int len) } /******************************************************************* - Convert a UNISTR2 structure to an ASCII string + Convert a (little-endian) UNISTR2 structure to an ASCII string Warning: this version does DOS codepage. ********************************************************************/ -- cgit From f9a15ce1a69f905e94db7650f0a4805720cd9c88 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sun, 8 Apr 2001 20:22:39 +0000 Subject: Got "medieval on our ass" about adding the -1 to slprintf. Jeremy. (This used to be commit 94747b4639ed9b19f7d0fb896e43aa392a84989a) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 3ab4bdca9f..18f3b54bf7 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1960,9 +1960,9 @@ smb_ucs2_t *octal_string_w(int i) char ret[64]; if (i == -1) - slprintf(ret, sizeof(ret), "-1"); + slprintf(ret, sizeof(ret)-1, "-1"); else - slprintf(ret, sizeof(ret), "0%o", i); + slprintf(ret, sizeof(ret)-1, "0%o", i); return unix_to_unicode(wret, ret, sizeof(wret)); } -- cgit From 4248d14ff65201298f60546b318a776590d08dfc Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 12 Jun 2001 18:20:26 +0000 Subject: Fix from TAKAHASHI Motonobu for multibyte conversion problems. Jeremy. (This used to be commit 24eea8a309ff0151277b9537a5c00321041e70d3) --- source3/lib/util_unistr.c | 46 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 18f3b54bf7..f6bb7e8068 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -201,11 +201,10 @@ char *dos_unistr2_to_str(UNISTR2 *str) char *lbuf = lbufs[nexti]; char *p; uint16 *src = str->buffer; - int max_size = MIN(MAXUNI-3, str->uni_str_len); nexti = (nexti+1)%8; - for (p = lbuf; (p-lbuf < max_size) && *src; src++) { + for (p = lbuf; (p - lbuf < MAXUNI-3) && (src - str->buffer < str->uni_str_len) && *src; src++) { uint16 ucs2_val = SVAL(src,0); uint16 cp_val = ucs2_to_doscp[ucs2_val]; @@ -227,49 +226,41 @@ char *dos_unistr2_to_str(UNISTR2 *str) ********************************************************************/ void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) { - uint16 *destend = dest + maxlen; - register char c; + uint16 *destend = dest + maxlen; + char c; - while (dest < destend) - { - c = *(src++); - if (c == 0) - { - break; - } + while (dest < destend) { + c = *(src++); + if (c == 0) + break; SSVAL(dest, 0, c); - dest++; - } + dest++; + } - *dest = 0; + *dest = 0; } - /******************************************************************* Pull an ASCII string out of a UNICODE array (uint16's). ********************************************************************/ void unistr_to_ascii(char *dest, const uint16 *src, int len) { - char *destend = dest + len; - register uint16 c; + char *destend = dest + len; + uint16 c; - if (src == NULL) - { + if (src == NULL) { *dest = '\0'; return; } /* normal code path for a valid 'src' */ - while (dest < destend) - { + while (dest < destend) { c = SVAL(src, 0); src++; if (c == 0) - { break; - } *(dest++) = (char)c; } @@ -339,11 +330,10 @@ char *dos_buffer2_to_str(BUFFER2 *str) char *lbuf = lbufs[nexti]; char *p; uint16 *src = str->buffer; - int max_size = MIN(sizeof(str->buffer)-3, str->buf_len/2); nexti = (nexti+1)%8; - for (p = lbuf; (p-lbuf < max_size) && *src; src++) { + for (p = lbuf; (p - lbuf < sizeof(str->buffer)-3) && (src - str->buffer < str->buf_len/2) && *src; src++) { uint16 ucs2_val = SVAL(src,0); uint16 cp_val = ucs2_to_doscp[ucs2_val]; @@ -368,11 +358,10 @@ char *dos_buffer2_to_multistr(BUFFER2 *str) char *lbuf = lbufs[nexti]; char *p; uint16 *src = str->buffer; - int max_size = MIN(sizeof(str->buffer)-3, str->buf_len/2); nexti = (nexti+1)%8; - for (p = lbuf; p-lbuf < max_size; src++) { + for (p = lbuf; (p - lbuf < sizeof(str->buffer)-3) && (src - str->buffer < str->buf_len/2); src++) { if (*src == 0) { *p++ = ' '; } else { @@ -481,8 +470,6 @@ int unistrcpy(char *dst, char *src) return num_wchars; } - - /******************************************************************* Free any existing maps. ********************************************************************/ @@ -505,7 +492,6 @@ static void free_maps(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) } } - /******************************************************************* Build a default (null) codepage to unicode map. ********************************************************************/ -- cgit From 91b8a8d1d21b810b6aca44ce647837669efd6dcf Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 21 Jun 2001 09:10:42 +0000 Subject: next_token() was supposed to be a reentrant replacement for strtok(), but the code suffered from bitrot and is not now reentrant. That means we can get bizarre behaviour i've fixed this by making next_token() reentrant and creating a next_token_nr() that is a small non-reentrant wrapper for those lumps of code (mostly smbclient) that have come to rely on the non-reentrant behaviour (This used to be commit 674ee2f1d12b0afc164a9e9072758fd1c5e54df7) --- source3/lib/util_unistr.c | 150 ---------------------------------------------- 1 file changed, 150 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index f6bb7e8068..7058665145 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1169,133 +1169,6 @@ smb_ucs2_t tolower_w( smb_ucs2_t val ) return map_table_lower(val); } -static smb_ucs2_t *last_ptr = NULL; - -void set_first_token_w(smb_ucs2_t *ptr) -{ - last_ptr = ptr; -} - -/**************************************************************************** - Get the next token from a string, return False if none found - handles double-quotes. - Based on a routine by GJC@VILLAGE.COM. - Extensively modified by Andrew.Tridgell@anu.edu.au - bufsize is in bytes. -****************************************************************************/ - -static smb_ucs2_t sep_list[] = { (smb_ucs2_t)' ', (smb_ucs2_t)'\t', (smb_ucs2_t)'\n', (smb_ucs2_t)'\r', 0}; -static smb_ucs2_t quotechar = (smb_ucs2_t)'\"'; - -BOOL next_token_w(smb_ucs2_t **ptr, smb_ucs2_t *buff, smb_ucs2_t *sep, size_t bufsize) -{ - smb_ucs2_t *s; - BOOL quoted; - size_t len=1; - - /* - * Convert bufsize to smb_ucs2_t units. - */ - - bufsize /= sizeof(smb_ucs2_t); - - if (!ptr) - ptr = &last_ptr; - if (!ptr) - return(False); - - s = *ptr; - - /* - * Default to simple separators. - */ - - if (!sep) - sep = sep_list; - - /* - * Find the first non sep char. - */ - - while(*s && strchr_w(sep,*s)) - s++; - - /* - * Nothing left ? - */ - - if (!*s) - return(False); - - /* - * Copy over the token. - */ - - for (quoted = False; len < bufsize && *s && (quoted || !strchr_w(sep,*s)); s++) { - if (*s == quotechar) { - quoted = !quoted; - } else { - len++; - *buff++ = *s; - } - } - - *ptr = (*s) ? s+1 : s; - *buff = 0; - last_ptr = *ptr; - - return(True); -} - -/**************************************************************************** - Convert list of tokens to array; dependent on above routine. - Uses last_ptr from above - bit of a hack. -****************************************************************************/ - -smb_ucs2_t **toktocliplist_w(int *ctok, smb_ucs2_t *sep) -{ - smb_ucs2_t *s=last_ptr; - int ictok=0; - smb_ucs2_t **ret, **iret; - - if (!sep) - sep = sep_list; - - while(*s && strchr_w(sep,*s)) - s++; - - /* - * Nothing left ? - */ - - if (!*s) - return(NULL); - - do { - ictok++; - while(*s && (!strchr_w(sep,*s))) - s++; - while(*s && strchr_w(sep,*s)) - *s++=0; - } while(*s); - - *ctok = ictok; - s = last_ptr; - - if (!(ret=iret=malloc(ictok*sizeof(smb_ucs2_t *)))) - return NULL; - - while(ictok--) { - *iret++=s; - while(*s++) - ; - while(!*s) - s++; - } - - return ret; -} - /******************************************************************* Case insensitive string compararison. ********************************************************************/ @@ -1715,29 +1588,6 @@ size_t strhex_to_str_w(char *p, size_t len, const smb_ucs2_t *strhex) return num_chars; } -/**************************************************************************** - Check if a string is part of a list. -****************************************************************************/ - -BOOL in_list_w(smb_ucs2_t *s,smb_ucs2_t *list,BOOL casesensitive) -{ - wpstring tok; - smb_ucs2_t *p=list; - - if (!list) - return(False); - - while (next_token_w(&p,tok,LIST_SEP_W,sizeof(tok))) { - if (casesensitive) { - if (strcmp_w(tok,s) == 0) - return(True); - } else { - if (StrCaseCmp_w(tok,s) == 0) - return(True); - } - } - return(False); -} /* This is used to prevent lots of mallocs of size 2 */ static smb_ucs2_t *null_string = NULL; -- cgit From 37eb0d6c74ce158b1cc268cea446b33789550048 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 23 Jun 2001 07:22:16 +0000 Subject: Added other_safe_chars to alpha_strcpy(). Needs testing but is a better fix for the problem. Jeremy. (This used to be commit e059fffd03a1382fb2b7059b6de369d9fc765a17) --- source3/lib/util_unistr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 7058665145..96aa62a283 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1453,9 +1453,10 @@ BOOL str_is_all_w(const smb_ucs2_t *s,smb_ucs2_t c) maxlength is in ucs2 units. ********************************************************************/ -smb_ucs2_t *alpha_strcpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) +smb_ucs2_t *alpha_strcpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const smb_ucs2_t *other_safe_chars, size_t maxlength) { size_t len, i; + smb_ucs2_t nullstr_w = (smb_ucs2_t)0; if (!dest) { DEBUG(0,("ERROR: NULL dest in alpha_strcpy_w\n")); @@ -1471,9 +1472,12 @@ smb_ucs2_t *alpha_strcpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxle if (len >= maxlength) len = maxlength - 1; + if (!other_safe_chars) + other_safe_chars = &nullstr_w; + for(i = 0; i < len; i++) { smb_ucs2_t val = src[i]; - if(isupper_w(val) ||islower_w(val) || isdigit_w(val)) + if(isupper_w(val) ||islower_w(val) || isdigit_w(val) || strchr_w(other_safe_chars, val)) dest[i] = src[i]; else dest[i] = (smb_ucs2_t)'_'; -- cgit From 87fbb7092b8f8b2f0db0f361c3d625e19de57cd9 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 4 Jul 2001 07:15:53 +0000 Subject: The big character set handling changeover! This commit gets rid of all our old codepage handling and replaces it with iconv. All internal strings in Samba are now in "unix" charset, which may be multi-byte. See internals.doc and my posting to samba-technical for a more complete explanation. (This used to be commit debb471267960e56005a741817ebd227ecfc512a) --- source3/lib/util_unistr.c | 1676 +++------------------------------------------ 1 file changed, 87 insertions(+), 1589 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 96aa62a283..1bfcd179f4 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1,8 +1,8 @@ /* Unix SMB/Netbios implementation. - Version 1.9. + Version 3.0 Samba utility functions - Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Andrew Tridgell 1992-2001 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,11 +31,7 @@ extern int DEBUGLEVEL; * These are dynamically loaded from a unicode translation file. */ -static smb_ucs2_t *doscp_to_ucs2; -static uint16 *ucs2_to_doscp; - -static smb_ucs2_t *unixcp_to_ucs2; -static uint16 *ucs2_to_unixcp; +#define CONV_DEBUGLEVEL 83 #ifndef MAXUNI #define MAXUNI 1024 @@ -54,62 +50,10 @@ static uint16 *ucs2_to_unixcp; size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) { - size_t ret = 0; - while (*src && (len >= 2)) { - size_t skip = get_character_len(*src); - smb_ucs2_t val = (*src & 0xff); - - /* - * If this is a multibyte character (and all DOS/Windows - * codepages have at maximum 2 byte multibyte characters) - * then work out the index value for the unicode conversion. - */ - - if (skip == 2) - val = ((val << 8) | (src[1] & 0xff)); - - SSVAL(dst,ret,doscp_to_ucs2[val]); - ret += 2; - len -= 2; - if (skip) - src += skip; - else - src++; - } - if (null_terminate) { - SSVAL(dst,ret,0); - ret += 2; - } - return(ret); + return push_ucs2(NULL, dst, src, len, + STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0)); } -/******************************************************************* - Pull a DOS codepage string out of a UNICODE array. len is in bytes. -********************************************************************/ - -void unistr_to_dos(char *dest, const char *src, size_t len) -{ - char *destend = dest + len; - - while (dest < destend) { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - src += 2; - - if (ucs2_val == 0) - break; - - if (cp_val < 256) - *dest++ = (char)cp_val; - else { - *dest++ = (cp_val >> 8) & 0xff; - *dest++ = (cp_val & 0xff); - } - } - - *dest = 0; -} /******************************************************************* Skip past a unicode string, but not more than len. Always move @@ -129,933 +73,61 @@ char *skip_unibuf(char *src, size_t len) return src; } -/******************************************************************* - Return a DOS codepage version of a little-endian unicode string. - len is the filename length (ignoring any terminating zero) in uin16 - units. Always null terminates. - Hack alert: uses fixed buffer(s). -********************************************************************/ - -char *dos_unistrn2(uint16 *src, int len) -{ - static char lbufs[8][MAXUNI]; - static int nexti; - char *lbuf = lbufs[nexti]; - char *p; - - nexti = (nexti+1)%8; - - for (p = lbuf; (len > 0) && (p-lbuf < MAXUNI-3) && *src; len--, src++) { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; - return lbuf; -} - -static char lbufs[8][MAXUNI]; -static int nexti; - -/******************************************************************* - Return a DOS codepage version of a little-endian unicode string. - Hack alert: uses fixed buffer(s). -********************************************************************/ - -char *dos_unistr2(uint16 *src) -{ - char *lbuf = lbufs[nexti]; - char *p; - - nexti = (nexti+1)%8; - - for (p = lbuf; (p-lbuf < MAXUNI-3) && *src; src++) { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; - return lbuf; -} - -/******************************************************************* -Return a DOS codepage version of a little-endian unicode string -********************************************************************/ - -char *dos_unistr2_to_str(UNISTR2 *str) +/* Copy a string from little-endian or big-endian unicode source (depending + * on flags) to internal samba format destination + */ +int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags) { - char *lbuf = lbufs[nexti]; - char *p; - uint16 *src = str->buffer; - - nexti = (nexti+1)%8; - - for (p = lbuf; (p - lbuf < MAXUNI-3) && (src - str->buffer < str->uni_str_len) && *src; src++) { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; - return lbuf; + if(dest_len==-1) dest_len=MAXUNI-3; + return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN); } -/******************************************************************* - Put an ASCII string into a UNICODE array (uint16's). - use little-endian ucs2 - ********************************************************************/ -void ascii_to_unistr(uint16 *dest, const char *src, int maxlen) +/* Converts a string from internal samba format to unicode + */ +int rpcstr_push(void* dest, const char *src, int dest_len, int flags) { - uint16 *destend = dest + maxlen; - char c; - - while (dest < destend) { - c = *(src++); - if (c == 0) - break; - - SSVAL(dest, 0, c); - dest++; - } - - *dest = 0; -} - -/******************************************************************* - Pull an ASCII string out of a UNICODE array (uint16's). - ********************************************************************/ - -void unistr_to_ascii(char *dest, const uint16 *src, int len) -{ - char *destend = dest + len; - uint16 c; - - if (src == NULL) { - *dest = '\0'; - return; - } - - /* normal code path for a valid 'src' */ - while (dest < destend) { - c = SVAL(src, 0); - src++; - if (c == 0) - break; - - *(dest++) = (char)c; - } - - *dest = 0; - return; -} - -/******************************************************************* - Convert a (little-endian) UNISTR2 structure to an ASCII string - Warning: this version does DOS codepage. -********************************************************************/ - -void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) -{ - char *p; - uint16 *src; - size_t len; - - if (str == NULL) { - *dest='\0'; - return; - } - - src = str->buffer; - len = MIN(str->uni_str_len, maxlen); - - if (len == 0) { - *dest='\0'; - return; - } - - for (p = dest; (p-dest < len) && *src; src++) { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; -} - - -/******************************************************************* -Return a number stored in a buffer -********************************************************************/ - -uint32 buffer2_to_uint32(BUFFER2 *str) -{ - if (str->buf_len == 4) - return IVAL(str->buffer, 0); - else - return 0; -} - -/******************************************************************* -Return a DOS codepage version of a NOTunicode string -********************************************************************/ - -char *dos_buffer2_to_str(BUFFER2 *str) -{ - char *lbuf = lbufs[nexti]; - char *p; - uint16 *src = str->buffer; - - nexti = (nexti+1)%8; - - for (p = lbuf; (p - lbuf < sizeof(str->buffer)-3) && (src - str->buffer < str->buf_len/2) && *src; src++) { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; - return lbuf; -} - -/******************************************************************* - Return a dos codepage version of a NOTunicode string -********************************************************************/ - -char *dos_buffer2_to_multistr(BUFFER2 *str) -{ - char *lbuf = lbufs[nexti]; - char *p; - uint16 *src = str->buffer; - - nexti = (nexti+1)%8; - - for (p = lbuf; (p - lbuf < sizeof(str->buffer)-3) && (src - str->buffer < str->buf_len/2); src++) { - if (*src == 0) { - *p++ = ' '; - } else { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - } - - *p = 0; - return lbuf; -} - -/******************************************************************* - Create a null-terminated unicode string from a null-terminated DOS - codepage string. - Return number of unicode chars copied, excluding the null character. - Unicode strings created are in little-endian format. -********************************************************************/ - -size_t dos_struni2(char *dst, const char *src, size_t max_len) -{ - size_t len = 0; - - if (dst == NULL) - return 0; - - if (src != NULL) { - for (; (len < max_len-2) && *src; len++, dst +=2) { - size_t skip = get_character_len(*src); - smb_ucs2_t val = (*src & 0xff); - - /* - * If this is a multibyte character (and all DOS/Windows - * codepages have at maximum 2 byte multibyte characters) - * then work out the index value for the unicode conversion. - */ - - if (skip == 2) - val = ((val << 8) | (src[1] & 0xff)); - - SSVAL(dst,0,doscp_to_ucs2[val]); - if (skip) - src += skip; - else - src++; - } - } - - SSVAL(dst,0,0); - - return len; + return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN); } /******************************************************************* Return a DOS codepage version of a little-endian unicode string. - Hack alert: uses fixed buffer(s). -********************************************************************/ - -char *dos_unistr(char *buf) -{ - char *lbuf = lbufs[nexti]; - uint16 *src = (uint16 *)buf; - char *p; - - nexti = (nexti+1)%8; - - for (p = lbuf; (p-lbuf < MAXUNI-3) && *src; src++) { - uint16 ucs2_val = SVAL(src,0); - uint16 cp_val = ucs2_to_doscp[ucs2_val]; - - if (cp_val < 256) - *p++ = (char)cp_val; - else { - *p++ = (cp_val >> 8) & 0xff; - *p++ = (cp_val & 0xff); - } - } - - *p = 0; - return lbuf; -} - -/******************************************************************* - Strcpy for unicode strings. returns length (in num of wide chars) -********************************************************************/ - -int unistrcpy(char *dst, char *src) -{ - int num_wchars = 0; - uint16 *wsrc = (uint16 *)src; - uint16 *wdst = (uint16 *)dst; - - while (*wsrc) { - *wdst++ = *wsrc++; - num_wchars++; - } - *wdst = 0; - - return num_wchars; -} - -/******************************************************************* - Free any existing maps. -********************************************************************/ - -static void free_maps(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) -{ - /* this handles identity mappings where we share the pointer */ - if (*pp_ucs2_to_cp == *pp_cp_to_ucs2) { - *pp_ucs2_to_cp = NULL; - } - - if (*pp_cp_to_ucs2) { - free(*pp_cp_to_ucs2); - *pp_cp_to_ucs2 = NULL; - } - - if (*pp_ucs2_to_cp) { - free(*pp_ucs2_to_cp); - *pp_ucs2_to_cp = NULL; - } -} - -/******************************************************************* - Build a default (null) codepage to unicode map. -********************************************************************/ - -void default_unicode_map(smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) -{ - int i; - - free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp); - - if ((*pp_ucs2_to_cp = (uint16 *)malloc(2*65536)) == NULL) { - DEBUG(0,("default_unicode_map: malloc fail for ucs2_to_cp size %u.\n", 2*65536)); - abort(); - } - - *pp_cp_to_ucs2 = *pp_ucs2_to_cp; /* Default map is an identity. */ - for (i = 0; i < 65536; i++) - (*pp_cp_to_ucs2)[i] = i; -} - -/******************************************************************* - Load a codepage to unicode and vica-versa map. -********************************************************************/ - -BOOL load_unicode_map(const char *codepage, smb_ucs2_t **pp_cp_to_ucs2, uint16 **pp_ucs2_to_cp) -{ - pstring unicode_map_file_name; - FILE *fp = NULL; - SMB_STRUCT_STAT st; - smb_ucs2_t *cp_to_ucs2 = *pp_cp_to_ucs2; - uint16 *ucs2_to_cp = *pp_ucs2_to_cp; - size_t cp_to_ucs2_size; - size_t ucs2_to_cp_size; - size_t i; - size_t size; - char buf[UNICODE_MAP_HEADER_SIZE]; - - DEBUG(5, ("load_unicode_map: loading unicode map for codepage %s.\n", codepage)); - - if (*codepage == '\0') - goto clean_and_exit; - - if(strlen(lp_codepagedir()) + 13 + strlen(codepage) > - sizeof(unicode_map_file_name)) { - DEBUG(0,("load_unicode_map: filename too long to load\n")); - goto clean_and_exit; - } - - pstrcpy(unicode_map_file_name, lp_codepagedir()); - pstrcat(unicode_map_file_name, "/"); - pstrcat(unicode_map_file_name, "unicode_map."); - pstrcat(unicode_map_file_name, codepage); - - if(sys_stat(unicode_map_file_name,&st)!=0) { - DEBUG(0,("load_unicode_map: filename %s does not exist.\n", - unicode_map_file_name)); - goto clean_and_exit; - } - - size = st.st_size; - - if ((size != UNICODE_MAP_HEADER_SIZE + 4*65536) && (size != UNICODE_MAP_HEADER_SIZE +(2*256 + 2*65536))) { - DEBUG(0,("load_unicode_map: file %s is an incorrect size for a \ -unicode map file (size=%d).\n", unicode_map_file_name, (int)size)); - goto clean_and_exit; - } - - if((fp = sys_fopen( unicode_map_file_name, "r")) == NULL) { - DEBUG(0,("load_unicode_map: cannot open file %s. Error was %s\n", - unicode_map_file_name, strerror(errno))); - goto clean_and_exit; - } - - if(fread( buf, 1, UNICODE_MAP_HEADER_SIZE, fp)!=UNICODE_MAP_HEADER_SIZE) { - DEBUG(0,("load_unicode_map: cannot read header from file %s. Error was %s\n", - unicode_map_file_name, strerror(errno))); - goto clean_and_exit; - } - - /* Check the version value */ - if(SVAL(buf,UNICODE_MAP_VERSION_OFFSET) != UNICODE_MAP_FILE_VERSION_ID) { - DEBUG(0,("load_unicode_map: filename %s has incorrect version id. \ -Needed %hu, got %hu.\n", - unicode_map_file_name, (uint16)UNICODE_MAP_FILE_VERSION_ID, - SVAL(buf,UNICODE_MAP_VERSION_OFFSET))); - goto clean_and_exit; - } - - /* Check the codepage value */ - if(!strequal(&buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET], codepage)) { - DEBUG(0,("load_unicode_map: codepage %s in file %s is not the same as that \ -requested (%s).\n", &buf[UNICODE_MAP_CLIENT_CODEPAGE_OFFSET], unicode_map_file_name, codepage )); - goto clean_and_exit; - } - - ucs2_to_cp_size = 2*65536; - if (size == UNICODE_MAP_HEADER_SIZE + 4*65536) { - /* - * This is a multibyte code page. - */ - cp_to_ucs2_size = 2*65536; - } else { - /* - * Single byte code page. - */ - cp_to_ucs2_size = 2*256; - } - - /* - * Free any old translation tables. - */ - - free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp); - - if ((cp_to_ucs2 = (smb_ucs2_t *)malloc(cp_to_ucs2_size)) == NULL) { - DEBUG(0,("load_unicode_map: malloc fail for cp_to_ucs2 size %u.\n", cp_to_ucs2_size )); - goto clean_and_exit; - } - - if ((ucs2_to_cp = (uint16 *)malloc(ucs2_to_cp_size)) == NULL) { - DEBUG(0,("load_unicode_map: malloc fail for ucs2_to_cp size %u.\n", ucs2_to_cp_size )); - goto clean_and_exit; - } - - if(fread( (char *)cp_to_ucs2, 1, cp_to_ucs2_size, fp)!=cp_to_ucs2_size) { - DEBUG(0,("load_unicode_map: cannot read cp_to_ucs2 from file %s. Error was %s\n", - unicode_map_file_name, strerror(errno))); - goto clean_and_exit; - } - - if(fread( (char *)ucs2_to_cp, 1, ucs2_to_cp_size, fp)!=ucs2_to_cp_size) { - DEBUG(0,("load_unicode_map: cannot read ucs2_to_cp from file %s. Error was %s\n", - unicode_map_file_name, strerror(errno))); - goto clean_and_exit; - } - - /* - * Now ensure the 16 bit values are in the correct endianness. - */ - - for (i = 0; i < cp_to_ucs2_size/2; i++) - cp_to_ucs2[i] = SVAL(cp_to_ucs2,i*2); - - for (i = 0; i < ucs2_to_cp_size/2; i++) - ucs2_to_cp[i] = SVAL(ucs2_to_cp,i*2); - - fclose(fp); - - *pp_cp_to_ucs2 = cp_to_ucs2; - *pp_ucs2_to_cp = ucs2_to_cp; - - return True; - -clean_and_exit: - - /* pseudo destructor :-) */ - - if(fp != NULL) - fclose(fp); - - free_maps(pp_cp_to_ucs2, pp_ucs2_to_cp); - - default_unicode_map(pp_cp_to_ucs2, pp_ucs2_to_cp); - - return False; -} - -/******************************************************************* - Load a dos codepage to unicode and vica-versa map. -********************************************************************/ - -BOOL load_dos_unicode_map(int codepage) -{ - fstring codepage_str; - - slprintf(codepage_str, sizeof(fstring)-1, "%03d", codepage); - return load_unicode_map(codepage_str, &doscp_to_ucs2, &ucs2_to_doscp); -} - -/******************************************************************* - Load a UNIX codepage to unicode and vica-versa map. -********************************************************************/ - -BOOL load_unix_unicode_map(const char *unix_char_set) -{ - fstring upper_unix_char_set; - - fstrcpy(upper_unix_char_set, unix_char_set); - strupper(upper_unix_char_set); - return load_unicode_map(upper_unix_char_set, &unixcp_to_ucs2, &ucs2_to_unixcp); -} - -/******************************************************************* - The following functions reproduce many of the non-UNICODE standard - string functions in Samba. -********************************************************************/ - -/******************************************************************* - Convert a UNICODE string to multibyte format. Note that the 'src' is in - native byte order, not little endian. Always zero terminates. - dst_len is in bytes. -********************************************************************/ - -static char *unicode_to_multibyte(char *dst, const smb_ucs2_t *src, - size_t dst_len, const uint16 *ucs2_to_cp) -{ - size_t dst_pos; - - for(dst_pos = 0; (dst_pos < dst_len - 1) && *src;) { - smb_ucs2_t val = ucs2_to_cp[*src++]; - if(val < 256) { - dst[dst_pos++] = (char)val; - } else { - - if(dst_pos >= dst_len - 2) - break; - - /* - * A 2 byte value is always written as - * high/low into the buffer stream. - */ - - dst[dst_pos++] = (char)((val >> 8) & 0xff); - dst[dst_pos++] = (char)(val & 0xff); - } - } - - dst[dst_pos] = '\0'; - - return dst; -} - -/******************************************************************* - Convert a multibyte string to UNICODE format. Note that the 'dst' is in - native byte order, not little endian. Always zero terminates. - dst_len is in bytes. -********************************************************************/ - -smb_ucs2_t *multibyte_to_unicode(smb_ucs2_t *dst, const char *src, - size_t dst_len, smb_ucs2_t *cp_to_ucs2) -{ - size_t i; - - dst_len /= sizeof(smb_ucs2_t); /* Convert to smb_ucs2_t units. */ - - for(i = 0; (i < (dst_len - 1)) && src[i];) { - size_t skip = skip_multibyte_char(*src); - smb_ucs2_t val = (*src & 0xff); - - /* - * If this is a multibyte character - * then work out the index value for the unicode conversion. - */ - - if (skip == 2) - val = ((val << 8) | (src[1] & 0xff)); - - dst[i++] = cp_to_ucs2[val]; - if (skip) - src += skip; - else - src++; - } - - dst[i] = 0; - - return dst; -} - -/******************************************************************* - Convert a UNICODE string to multibyte format. Note that the 'src' is in - native byte order, not little endian. Always zero terminates. - This function may be replaced if the MB codepage format is an - encoded one (ie. utf8, hex). See the code in lib/kanji.c - for details. dst_len is in bytes. -********************************************************************/ - -char *unicode_to_unix(char *dst, const smb_ucs2_t *src, size_t dst_len) -{ - return unicode_to_multibyte(dst, src, dst_len, ucs2_to_unixcp); -} - -/******************************************************************* - Convert a UNIX string to UNICODE format. Note that the 'dst' is in - native byte order, not little endian. Always zero terminates. - This function may be replaced if the UNIX codepage format is a - multi-byte one (ie. JIS, SJIS or utf8). See the code in lib/kanji.c - for details. dst_len is in bytes, not ucs2 units. -********************************************************************/ - -smb_ucs2_t *unix_to_unicode(smb_ucs2_t *dst, const char *src, size_t dst_len) -{ - return multibyte_to_unicode(dst, src, dst_len, unixcp_to_ucs2); -} - -/******************************************************************* - Convert a UNICODE string to DOS format. Note that the 'src' is in - native byte order, not little endian. Always zero terminates. - dst_len is in bytes. -********************************************************************/ - -char *unicode_to_dos(char *dst, const smb_ucs2_t *src, size_t dst_len) -{ - return unicode_to_multibyte(dst, src, dst_len, ucs2_to_doscp); -} - -/******************************************************************* - Convert a single UNICODE character to DOS codepage. Returns the - number of bytes in the DOS codepage character. -********************************************************************/ - -size_t unicode_to_dos_char(char *dst, const smb_ucs2_t src) -{ - smb_ucs2_t val = ucs2_to_doscp[src]; - if(val < 256) { - *dst = (char)val; - return (size_t)1; - } - /* - * A 2 byte value is always written as - * high/low into the buffer stream. - */ - - dst[0] = (char)((val >> 8) & 0xff); - dst[1] = (char)(val & 0xff); - return (size_t)2; -} - -/******************************************************************* - Convert a DOS string to UNICODE format. Note that the 'dst' is in - native byte order, not little endian. Always zero terminates. - This function may be replaced if the DOS codepage format is a - multi-byte one (ie. JIS, SJIS or utf8). See the code in lib/kanji.c - for details. dst_len is in bytes, not ucs2 units. -********************************************************************/ - -smb_ucs2_t *dos_to_unicode(smb_ucs2_t *dst, const char *src, size_t dst_len) -{ - return multibyte_to_unicode(dst, src, dst_len, doscp_to_ucs2); -} - -/******************************************************************* - Count the number of characters in a smb_ucs2_t string. -********************************************************************/ - -size_t strlen_w(const smb_ucs2_t *src) -{ - size_t len; - - for(len = 0; *src++; len++) - ; - - return len; -} - -/******************************************************************* - Safe wstring copy into a known length string. maxlength includes - the terminating zero. maxlength is in ucs2 units. -********************************************************************/ - -smb_ucs2_t *safe_strcpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src, size_t maxlength) -{ - size_t ucs2_len; - - if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcpy_w\n")); - return NULL; - } - - if (!src) { - *dest = 0; - return dest; - } - - maxlength /= sizeof(smb_ucs2_t); - - ucs2_len = strlen_w(src); - - if (ucs2_len >= maxlength) { - fstring out; - DEBUG(0,("ERROR: string overflow by %u bytes in safe_strcpy_w [%.50s]\n", - (unsigned int)((ucs2_len-maxlength)*sizeof(smb_ucs2_t)), - unicode_to_unix(out,src,sizeof(out))) ); - ucs2_len = maxlength - 1; - } - - memcpy(dest, src, ucs2_len*sizeof(smb_ucs2_t)); - dest[ucs2_len] = 0; - return dest; -} - -/******************************************************************* - Safe string cat into a string. maxlength includes the terminating zero. - maxlength is in ucs2 units. -********************************************************************/ - -smb_ucs2_t *safe_strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, size_t maxlength) -{ - size_t ucs2_src_len, ucs2_dest_len; - - if (!dest) { - DEBUG(0,("ERROR: NULL dest in safe_strcat_w\n")); - return NULL; - } - - if (!src) - return dest; - - ucs2_src_len = strlen_w(src); - ucs2_dest_len = strlen_w(dest); - - if (ucs2_src_len + ucs2_dest_len >= maxlength) { - fstring out; - int new_len = maxlength - ucs2_dest_len - 1; - DEBUG(0,("ERROR: string overflow by %u characters in safe_strcat_w [%.50s]\n", - (unsigned int)(sizeof(smb_ucs2_t)*(ucs2_src_len + ucs2_dest_len - maxlength)), - unicode_to_unix(out,src,sizeof(out))) ); - ucs2_src_len = (size_t)(new_len > 0 ? new_len : 0); - } - - memcpy(&dest[ucs2_dest_len], src, ucs2_src_len*sizeof(smb_ucs2_t)); - dest[ucs2_dest_len + ucs2_src_len] = 0; - return dest; -} - -/******************************************************************* - Compare the two strings s1 and s2. -********************************************************************/ - -int strcmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) -{ - smb_ucs2_t c1, c2; - - for (;;) { - c1 = *s1++; - c2 = *s2++; - - if (c1 != c2) - return c1 - c2; - - if (c1 == 0) - break; - } - return 0; -} - -/******************************************************************* - Compare the first n characters of s1 to s2. len is in ucs2 units. -********************************************************************/ - -int strncmp_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2, size_t len) -{ - smb_ucs2_t c1, c2; - - for (; len != 0; --len) { - c1 = *s1++; - c2 = *s2++; - - if (c1 != c2) - return c1 - c2; - - if (c1 == 0) - break; - - } - return 0; -} - -/******************************************************************* - Search string s2 from s1. -********************************************************************/ - -smb_ucs2_t *strstr_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) -{ - size_t len = strlen_w(s2); - - if (!*s2) - return (smb_ucs2_t *)s1; - - for(;*s1; s1++) { - if (*s1 == *s2) { - if (strncmp_w(s1, s2, len) == 0) - return (smb_ucs2_t *)s1; - } - } - return NULL; -} - -/******************************************************************* - Search for ucs2 char c from the beginning of s. -********************************************************************/ - -smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) -{ - do { - if (*s == c) - return (smb_ucs2_t *)s; - } while (*s++); - - return NULL; -} - -/******************************************************************* - Search for ucs2 char c from the end of s. -********************************************************************/ - -smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) + len is the filename length (ignoring any terminating zero) in uin16 + units. Always null terminates. + Hack alert: uses fixed buffer(s). +********************************************************************/ +char *dos_unistrn2(uint16 *src, int len) { - smb_ucs2_t *retval = 0; - - do { - if (*s == c) - retval = (smb_ucs2_t *)s; - } while (*s++); - - return retval; + static char lbufs[8][MAXUNI]; + static int nexti; + char *lbuf = lbufs[nexti]; + nexti = (nexti+1)%8; + pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN); + return lbuf; } /******************************************************************* - Search token from s1 separated by any ucs2 char of s2. + Convert a (little-endian) UNISTR2 structure to an ASCII string ********************************************************************/ - -smb_ucs2_t *strtok_w(smb_ucs2_t *s1, const smb_ucs2_t *s2) +void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) { - static smb_ucs2_t *s = NULL; - smb_ucs2_t *q; - - if (!s1) { - if (!s) - return NULL; - s1 = s; - } - - for (q = s1; *s1; s1++) { - smb_ucs2_t *p = strchr_w(s2, *s1); - if (p) { - if (s1 != q) { - s = s1 + 1; - *s1 = '\0'; - return q; - } - q = s1 + 1; - } + if (str == NULL) { + *dest='\0'; + return; } - - s = NULL; - if (*q) - return q; - - return NULL; + pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len, STR_NOALIGN); } + /******************************************************************* - Duplicate a ucs2 string. +Return a number stored in a buffer ********************************************************************/ -smb_ucs2_t *strdup_w(const smb_ucs2_t *s) +uint32 buffer2_to_uint32(BUFFER2 *str) { - size_t newlen = (strlen_w(s)+1)*sizeof(smb_ucs2_t); - smb_ucs2_t *newstr = (smb_ucs2_t *)malloc(newlen); - if (newstr == NULL) - return NULL; - safe_strcpy_w(newstr, s, newlen); - return newstr; + if (str->buf_len == 4) + return IVAL(str->buffer, 0); + else + return 0; } /******************************************************************* @@ -1124,33 +196,6 @@ int islower_w( smb_ucs2_t val) return (map_table_flags(val) & UNI_LOWER); } -/******************************************************************* - Is a digit wchar. -********************************************************************/ - -int isdigit_w( smb_ucs2_t val) -{ - return (map_table_flags(val) & UNI_DIGIT); -} - -/******************************************************************* - Is a hex digit wchar. -********************************************************************/ - -int isxdigit_w( smb_ucs2_t val) -{ - return (map_table_flags(val) & UNI_XDIGIT); -} - -/******************************************************************* - Is a space wchar. -********************************************************************/ - -int isspace_w( smb_ucs2_t val) -{ - return (map_table_flags(val) & UNI_SPACE); -} - /******************************************************************* Convert a wchar to upper case. ********************************************************************/ @@ -1170,102 +215,33 @@ smb_ucs2_t tolower_w( smb_ucs2_t val ) } /******************************************************************* - Case insensitive string compararison. + Count the number of characters in a smb_ucs2_t string. ********************************************************************/ - -int StrCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t) +size_t strlen_w(const smb_ucs2_t *src) { - /* - * Compare until we run out of string, either t or s, or find a difference. - */ + size_t len; - while (*s && *t && toupper_w(*s) == toupper_w(*t)) { - s++; - t++; - } + for(len = 0; *src++; len++) ; - return(toupper_w(*s) - toupper_w(*t)); + return len; } /******************************************************************* - Case insensitive string compararison, length limited. - n is in ucs2 units. +wide strchr() ********************************************************************/ - -int StrnCaseCmp_w(const smb_ucs2_t *s, const smb_ucs2_t *t, size_t n) +smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { - /* - * Compare until we run out of string, either t or s, or chars. - */ - - while (n && *s && *t && toupper_w(*s) == toupper_w(*t)) { + while (*s != 0) { + if (c == *s) return (smb_ucs2_t *)s; s++; - t++; - n--; } - - /* - * Not run out of chars - strings are different lengths. - */ - - if (n) - return(toupper_w(*s) - toupper_w(*t)); - - /* - * Identical up to where we run out of chars, - * and strings are same length. - */ - - return(0); -} - -/******************************************************************* - Compare 2 strings. -********************************************************************/ - -BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) -{ - if (s1 == s2) - return(True); - if (!s1 || !s2) - return(False); - - return(StrCaseCmp_w(s1,s2)==0); -} - -/******************************************************************* - Compare 2 strings up to and including the nth char. n is in ucs2 - units. -******************************************************************/ - -BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n) -{ - if (s1 == s2) - return(True); - if (!s1 || !s2 || !n) - return(False); - - return(StrnCaseCmp_w(s1,s2,n)==0); + return NULL; } -/******************************************************************* - Compare 2 strings (case sensitive). -********************************************************************/ - -BOOL strcsequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2) -{ - if (s1 == s2) - return(True); - if (!s1 || !s2) - return(False); - - return(strcmp_w(s1,s2)==0); -} /******************************************************************* Convert a string to lower case. ********************************************************************/ - void strlower_w(smb_ucs2_t *s) { while (*s) { @@ -1278,7 +254,6 @@ void strlower_w(smb_ucs2_t *s) /******************************************************************* Convert a string to upper case. ********************************************************************/ - void strupper_w(smb_ucs2_t *s) { while (*s) { @@ -1289,547 +264,70 @@ void strupper_w(smb_ucs2_t *s) } /******************************************************************* - Convert a string to "normal" form. -********************************************************************/ - -void strnorm_w(smb_ucs2_t *s) -{ - extern int case_default; - if (case_default == CASE_UPPER) - strupper_w(s); - else - strlower_w(s); -} - -/******************************************************************* - Check if a string is in "normal" case. +case insensitive string comparison ********************************************************************/ - -BOOL strisnormal_w(smb_ucs2_t *s) -{ - extern int case_default; - if (case_default == CASE_UPPER) - return(!strhaslower_w(s)); - - return(!strhasupper_w(s)); -} - -/**************************************************************************** - String replace. -****************************************************************************/ - -void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) +int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) { - while (*s) { - if (oldc == *s) - *s = newc; - s++; - } + while (*b && tolower_w(*a) == tolower_w(*b)) { a++; b++; } + return (tolower_w(*a) - tolower_w(*b)); } -/******************************************************************* - Skip past some strings in a buffer. n is in bytes. -********************************************************************/ - -smb_ucs2_t *skip_string_w(smb_ucs2_t *buf,size_t n) -{ - while (n--) - buf += (strlen_w(buf)*sizeof(smb_ucs2_t)) + 1; - return(buf); -} -/******************************************************************* - Count the number of characters in a string. Same as strlen_w in - smb_ucs2_t string units. -********************************************************************/ +/* + The *_wa() functions take a combination of 7 bit ascii + and wide characters They are used so that you can use string + functions combining C string constants with ucs2 strings -size_t str_charnum_w(const smb_ucs2_t *s) -{ - return strlen_w(s); -} + The char* arguments must NOT be multibyte - to be completely sure + of this only pass string constants */ -/******************************************************************* - Trim the specified elements off the front and back of a string. -********************************************************************/ -BOOL trim_string_w(smb_ucs2_t *s,const smb_ucs2_t *front,const smb_ucs2_t *back) +void pstrcpy_wa(smb_ucs2_t *dest, const char *src) { - BOOL ret = False; - size_t front_len = (front && *front) ? strlen_w(front) : 0; - size_t back_len = (back && *back) ? strlen_w(back) : 0; - size_t s_len; - - while (front_len && strncmp_w(s, front, front_len) == 0) { - smb_ucs2_t *p = s; - ret = True; - - while (1) { - if (!(*p = p[front_len])) - break; - p++; - } - } - - if(back_len) { - s_len = strlen_w(s); - while ((s_len >= back_len) && - (strncmp_w(s + s_len - back_len, back, back_len)==0)) { - ret = True; - s[s_len - back_len] = 0; - s_len = strlen_w(s); - } + int i; + for (i=0;i= maxlength) - len = maxlength - 1; - - if (!other_safe_chars) - other_safe_chars = &nullstr_w; - - for(i = 0; i < len; i++) { - smb_ucs2_t val = src[i]; - if(isupper_w(val) ||islower_w(val) || isdigit_w(val) || strchr_w(other_safe_chars, val)) - dest[i] = src[i]; - else - dest[i] = (smb_ucs2_t)'_'; - } - - dest[i] = 0; - - return dest; -} - -/**************************************************************************** - Like strncpy but always null terminates. Make sure there is room ! - The variable n should always be one less than the available size and is in - ucs2 units. -****************************************************************************/ - -smb_ucs2_t *StrnCpy_w(smb_ucs2_t *dest,const smb_ucs2_t *src,size_t n) -{ - smb_ucs2_t *d = dest; - if (!dest) - return(NULL); - if (!src) { - *dest = 0; - return(dest); - } - - while (n-- && (*d++ = *src++)) - ; - *d = 0; - return(dest); -} - -/**************************************************************************** - Like strncpy but copies up to the character marker. Always null terminates. - returns a pointer to the character marker in the source string (src). - n is in ucs2 units. -****************************************************************************/ - -smb_ucs2_t *strncpyn_w(smb_ucs2_t *dest, const smb_ucs2_t *src,size_t n, smb_ucs2_t c) -{ - smb_ucs2_t *p; - size_t str_len; - - p = strchr_w(src, c); - if (p == NULL) { - fstring cval; - smb_ucs2_t mbcval[2]; - mbcval[0] = c; - mbcval[1] = 0; - DEBUG(5, ("strncpyn_w: separator character (%s) not found\n", - unicode_to_unix(cval,mbcval,sizeof(cval)) )); - return NULL; - } - - str_len = PTR_DIFF(p, src) + 1; - safe_strcpy_w(dest, src, MIN(n, str_len)); - - return p; -} - -/************************************************************* - Routine to get hex characters and turn them into a 16 byte array. - The array can be variable length, and any non-hex-numeric - characters are skipped. "0xnn" or "0Xnn" is specially catered - for. len is in bytes. - Valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n" -**************************************************************/ - -static smb_ucs2_t hexprefix[] = { (smb_ucs2_t)'0', (smb_ucs2_t)'x', 0 }; -static smb_ucs2_t hexchars[] = { (smb_ucs2_t)'0', (smb_ucs2_t)'1', (smb_ucs2_t)'2', (smb_ucs2_t)'3', - (smb_ucs2_t)'4', (smb_ucs2_t)'5', (smb_ucs2_t)'6', (smb_ucs2_t)'7', - (smb_ucs2_t)'8', (smb_ucs2_t)'9', (smb_ucs2_t)'A', (smb_ucs2_t)'B', - (smb_ucs2_t)'C', (smb_ucs2_t)'D', (smb_ucs2_t)'E', (smb_ucs2_t)'F', 0 }; - -size_t strhex_to_str_w(char *p, size_t len, const smb_ucs2_t *strhex) -{ - size_t i; - size_t num_chars = 0; - unsigned char lonybble, hinybble; - smb_ucs2_t *p1 = NULL, *p2 = NULL; - - /* - * Convert to smb_ucs2_t units. - */ - - len /= sizeof(smb_ucs2_t); - - for (i = 0; i < len && strhex[i] != 0; i++) { - if (strnequal_w(hexchars, hexprefix, 2)) { - i++; /* skip two chars */ - continue; - } - - if (!(p1 = strchr_w(hexchars, toupper_w(strhex[i])))) - break; - - i++; /* next hex digit */ - - if (!(p2 = strchr_w(hexchars, toupper_w(strhex[i])))) - break; - - /* get the two nybbles */ - hinybble = (PTR_DIFF(p1, hexchars)/sizeof(smb_ucs2_t)); - lonybble = (PTR_DIFF(p2, hexchars)/sizeof(smb_ucs2_t)); - - p[num_chars] = (hinybble << 4) | lonybble; - num_chars++; - - p1 = NULL; - p2 = NULL; - } - return num_chars; -} - - -/* This is used to prevent lots of mallocs of size 2 */ -static smb_ucs2_t *null_string = NULL; - -/**************************************************************************** - Set a string value, allocing the space for the string. -****************************************************************************/ - -BOOL string_init_w(smb_ucs2_t **dest,const smb_ucs2_t *src) -{ - size_t l; - - if (!null_string) { - if((null_string = (smb_ucs2_t *)malloc(sizeof(smb_ucs2_t))) == NULL) { - DEBUG(0,("string_init_w: malloc fail for null_string.\n")); - return False; - } - *null_string = 0; - } - - if (!src) - src = null_string; - - l = strlen_w(src); - - if (l == 0) - *dest = null_string; - else { - (*dest) = (smb_ucs2_t *)malloc(sizeof(smb_ucs2_t)*(l+1)); - if ((*dest) == NULL) { - DEBUG(0,("Out of memory in string_init_w\n")); - return False; - } - - wpstrcpy(*dest,src); - } - return(True); -} - -/**************************************************************************** - Free a string value. -****************************************************************************/ - -void string_free_w(smb_ucs2_t **s) -{ - if (!s || !(*s)) - return; - if (*s == null_string) - *s = NULL; - if (*s) - free((char *)*s); - *s = NULL; -} - -/**************************************************************************** - Set a string value, allocing the space for the string, and deallocating any - existing space. -****************************************************************************/ - -BOOL string_set_w(smb_ucs2_t **dest,const smb_ucs2_t *src) -{ - string_free_w(dest); - - return(string_init_w(dest,src)); -} - -/**************************************************************************** - Substitute a string for a pattern in another string. Make sure there is - enough room ! - - This routine looks for pattern in s and replaces it with - insert. It may do multiple replacements. - - Any of " ; ' $ or ` in the insert string are replaced with _ - if len==0 then no length check is performed - len is in ucs2 units. -****************************************************************************/ - -void string_sub_w(smb_ucs2_t *s,const smb_ucs2_t *pattern,const smb_ucs2_t *insert, size_t len) -{ - smb_ucs2_t *p; - ssize_t ls,lp,li, i; - - if (!insert || !pattern || !s) - return; - - ls = (ssize_t)strlen_w(s); - lp = (ssize_t)strlen_w(pattern); - li = (ssize_t)strlen_w(insert); - - if (!*pattern) - return; - - while (lp <= ls && (p = strstr_w(s,pattern))) { - if (len && (ls + (li-lp) >= len)) { - fstring out; - DEBUG(0,("ERROR: string overflow by %d in string_sub_w(%.50s, %d)\n", - (int)(sizeof(smb_ucs2_t)*(ls + (li-lp) - len)), - unicode_to_unix(out,pattern,sizeof(out)), (int)len*sizeof(smb_ucs2_t))); - break; - } - if (li != lp) - memmove(p+li,p+lp,sizeof(smb_ucs2_t)*(strlen_w(p+lp)+1)); - - for (i=0;i= len)) { - fstring out; - DEBUG(0,("ERROR: string overflow by %d in all_string_sub_w(%.50s, %d)\n", - (int)(sizeof(smb_ucs2_t)*(ls + (li-lp) - len)), - unicode_to_unix(out,pattern,sizeof(out)), (int)len*sizeof(smb_ucs2_t))); - break; - } - if (li != lp) - memmove(p+li,p+lp,sizeof(smb_ucs2_t)*(strlen_w(p+lp)+1)); - - memcpy(p, insert, li*sizeof(smb_ucs2_t)); - s = p + li; - ls += (li-lp); - } -} - -/**************************************************************************** - Splits out the front and back at a separator. -****************************************************************************/ - -void split_at_last_component_w(smb_ucs2_t *path, smb_ucs2_t *front, smb_ucs2_t sep, smb_ucs2_t *back) -{ - smb_ucs2_t *p = strrchr_w(path, sep); - - if (p != NULL) - *p = 0; - - if (front != NULL) - wpstrcpy(front, path); - - if (p != NULL) { - if (back != NULL) - wpstrcpy(back, p+1); - *p = (smb_ucs2_t)'\\'; - } else { - if (back != NULL) - back[0] = 0; - } -} - - -/**************************************************************************** - Write an octal as a string. -****************************************************************************/ - -smb_ucs2_t *octal_string_w(int i) -{ - static smb_ucs2_t wret[64]; - char ret[64]; - - if (i == -1) - slprintf(ret, sizeof(ret)-1, "-1"); - else - slprintf(ret, sizeof(ret)-1, "0%o", i); - return unix_to_unicode(wret, ret, sizeof(wret)); -} - - -/**************************************************************************** - Truncate a string at a specified length. - length is in ucs2 units. -****************************************************************************/ - -smb_ucs2_t *string_truncate_w(smb_ucs2_t *s, size_t length) -{ - if (s && strlen_w(s) > length) - s[length] = 0; - - return s; -} - -/****************************************************************** - functions for UTF8 support (using in kanji.c) - ******************************************************************/ -smb_ucs2_t doscp2ucs2(int w) -{ - return ((smb_ucs2_t)doscp_to_ucs2[w]); -} - -int ucs2doscp(smb_ucs2_t w) -{ - return ((int)ucs2_to_doscp[w]); + return NULL; } -- cgit From 0a33bdc7bd9aa4ecd65f8066d2c5ec82a042371a Mon Sep 17 00:00:00 2001 From: Jean-François Micouleau Date: Wed, 4 Jul 2001 18:57:16 +0000 Subject: unicode string length is twice longer ;-) (This used to be commit d39d8429d1cb8a976022ae92a1ce551f7d876aff) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 1bfcd179f4..b90532d7dc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -114,7 +114,7 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) *dest='\0'; return; } - pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len, STR_NOALIGN); + pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); } -- cgit From a732bcd9d2677dde13cb95859c90edf86fb88984 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 6 Jul 2001 22:32:57 +0000 Subject: fixed strrchr_m (This used to be commit f4359b5f7ff891d7fcb5772b589756376ae5d412) --- source3/lib/util_unistr.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index b90532d7dc..09a9eda3ad 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -95,7 +95,7 @@ int rpcstr_push(void* dest, const char *src, int dest_len, int flags) units. Always null terminates. Hack alert: uses fixed buffer(s). ********************************************************************/ -char *dos_unistrn2(uint16 *src, int len) +char *dos_unistrn2(const uint16 *src, int len) { static char lbufs[8][MAXUNI]; static int nexti; @@ -312,10 +312,9 @@ smb_ucs2_t *strrchr_wa(const smb_ucs2_t *s, char c) int len = strlen_w(s); if (len == 0) return NULL; p += (len-1); - while (p != s) { + do { if (UCS2_CHAR(c) == *p) return (smb_ucs2_t *)p; - p--; - } + } while (p-- != s); return NULL; } -- cgit From 45635a4a799f9f6efbcb8d021bec60a2eb479808 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 18 Jul 2001 21:48:36 +0000 Subject: removed some unnecessary code (This used to be commit 1436c9f424abbac2752e952172ddbdf15ec73f91) --- source3/lib/util_unistr.c | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 09a9eda3ad..4a1117d8d0 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -23,16 +23,6 @@ extern int DEBUGLEVEL; - smb_ucs2_t wchar_list_sep[] = { (smb_ucs2_t)' ', (smb_ucs2_t)'\t', (smb_ucs2_t)',', - (smb_ucs2_t)';', (smb_ucs2_t)':', (smb_ucs2_t)'\n', - (smb_ucs2_t)'\r', 0 }; -/* - * The following are the codepage to ucs2 and vica versa maps. - * These are dynamically loaded from a unicode translation file. - */ - -#define CONV_DEBUGLEVEL 83 - #ifndef MAXUNI #define MAXUNI 1024 #endif -- cgit From 8678c6d6f67f9f1f4f68eba4a3dc1bf87f4abc3a Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 20 Jul 2001 01:32:56 +0000 Subject: It looks like the rpc client code in libsmb hasn't been converted to the new internal string stuff. The main problem is that some unicode strings are null terminated and some aren't. There's no rhyme or reason to it - some pipes have 99% of the strings terminated and some have 99% unterminated. To avoid having to actually know the termination policy, I propose a set of functions that take a UNISTR2* and use the length contained there. Added rpcstr_pull_unistr2_string() function to convert a unicode string of dubious termination to a fstring. (This used to be commit e5b62b4961769e45bc377be6180ca61ef5f6c22e) --- source3/lib/util_unistr.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 4a1117d8d0..48bff4c41c 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -72,6 +72,16 @@ int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags) return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN); } +/* Copy a string from a unistr2 source to internal samba format + destination. Use this instead of direct calls to rpcstr_pull() to avoid + having to determine whether the source string is null terminated. */ + +int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) +{ + return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring), + src->uni_str_len * 2, 0); +} + /* Converts a string from internal samba format to unicode */ int rpcstr_push(void* dest, const char *src, int dest_len, int flags) -- cgit From be184b26620b64af6b7114fa29f2c2b525fa1cb8 Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Fri, 24 Aug 2001 19:17:43 +0000 Subject: fixes big endian unistring problems. Need to check that it didn't break little-endian machines. (This used to be commit 0f08e79030419b778c1229712c98b839faaf734f) --- source3/lib/util_unistr.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 48bff4c41c..d4c71ae13f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -159,6 +159,7 @@ static smb_unicode_table_t map_table2[] = { static unsigned char map_table_flags(smb_ucs2_t v) { + v = SVAL(&v,0); if (v < TABLE1_BOUNDARY) return map_table1[v].flags; if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].flags; return 0; @@ -166,6 +167,7 @@ static unsigned char map_table_flags(smb_ucs2_t v) static smb_ucs2_t map_table_lower(smb_ucs2_t v) { + v = SVAL(&v,0); if (v < TABLE1_BOUNDARY) return map_table1[v].lower; if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].lower; return v; @@ -173,6 +175,7 @@ static smb_ucs2_t map_table_lower(smb_ucs2_t v) static smb_ucs2_t map_table_upper(smb_ucs2_t v) { + v = SVAL(&v,0); if (v < TABLE1_BOUNDARY) return map_table1[v].upper; if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].upper; return v; @@ -202,7 +205,9 @@ int islower_w( smb_ucs2_t val) smb_ucs2_t toupper_w( smb_ucs2_t val ) { - return map_table_upper(val); + val = map_table_upper(val); + val = SVAL(&val,0); + return val; } /******************************************************************* @@ -211,7 +216,9 @@ smb_ucs2_t toupper_w( smb_ucs2_t val ) smb_ucs2_t tolower_w( smb_ucs2_t val ) { - return map_table_lower(val); + val = map_table_lower(val); + val = SVAL(&val,0); + return val; } /******************************************************************* -- cgit From deed36e40fcf5ca643afe896f2235dbec82e6c7d Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 25 Sep 2001 09:57:06 +0000 Subject: - the inactive core of the new mangling code that use tdb - some more utils for unicode string manipulation (This used to be commit 4ade36446e7dee1c3828d8c822f047c6e891a644) --- source3/lib/util_unistr.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index d4c71ae13f..8248ac073c 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -280,6 +280,67 @@ int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) } +/******************************************************************* +duplicate string +********************************************************************/ +smb_ucs2_t *strdup_w(const smb_ucs2_t *src) +{ + smb_ucs2_t *dest; + uint32 len; + + len = strlen_w(src); + dest = (smb_ucs2_t *)malloc((len+1)*sizeof(smb_ucs2_t)); + if (!dest) { + DEBUG(0,("strdup_w: out of memory!\n")); + return NULL; + } + + memcpy(dest, src, (len+1)*sizeof(smb_ucs2_t)); + + return dest; +} + +/******************************************************************* +copy a string with max len +********************************************************************/ + +smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) +{ + size_t len; + + if (!dest || !src) return NULL; + + for (len = 0; (src[len] != 0) && (len < max); len++) + dest[len] = src[len]; + while (len < max) + dest[len++] = 0; + + return dest; +} + + +/******************************************************************* +append a string of len bytes and add a terminator +********************************************************************/ + +smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) +{ + size_t start; + size_t len; + + if (!dest || !src) return NULL; + + start = strlen_w(dest); + len = strlen_w(src); + if (len > max) len = max; + + memcpy(&dest[start], src, len); + dest[start+len+1] = 0; + + return dest; +} + + /* The *_wa() functions take a combination of 7 bit ascii and wide characters They are used so that you can use string @@ -304,6 +365,8 @@ int strcmp_wa(const smb_ucs2_t *a, const char *b) return (*a - UCS2_CHAR(*b)); } + + smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c) { while (*s != 0) { @@ -337,3 +400,47 @@ smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) return NULL; } + +/******************************************************************* +copy a string with max len +********************************************************************/ + +smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max) +{ + smb_ucs2_t *ucs2_src; + + if (!dest || !src) return NULL; + ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t)); + if (!ucs2_src) { + DEBUG(0,("strncpy_wa: out of memory!\n")); + return NULL; + } + push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN); + + strncpy_w(dest, ucs2_src, max); + SAFE_FREE(ucs2_src); + return dest; +} + + +/******************************************************************* +append a string of len bytes and add a terminator +********************************************************************/ + +smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max) +{ + smb_ucs2_t *ucs2_src; + + if (!dest || !src) return NULL; + ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t)); + if (!ucs2_src) { + DEBUG(0,("strncat_wa: out of memory!\n")); + return NULL; + } + push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN); + + strncat_w(dest, ucs2_src, max); + SAFE_FREE(ucs2_src); + return dest; +} + -- cgit From b7ca6ec034c8b707e462e4a4457a65ce9bea0f79 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 30 Sep 2001 13:30:52 +0000 Subject: make strupper() and strlower() not modify the string if it doesn't need modifying that makes constant strings OK (This used to be commit 57196635d9b7edfcbfe1708dd22308ab30c02240) --- source3/lib/util_unistr.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8248ac073c..887c15f0d9 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -248,26 +248,36 @@ smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) /******************************************************************* Convert a string to lower case. + return True if any char is converted ********************************************************************/ -void strlower_w(smb_ucs2_t *s) +BOOL strlower_w(smb_ucs2_t *s) { + BOOL ret = False; while (*s) { - if (isupper_w(*s)) + if (isupper_w(*s)) { *s = tolower_w(*s); + ret = True; + } s++; } + return ret; } /******************************************************************* Convert a string to upper case. + return True if any char is converted ********************************************************************/ -void strupper_w(smb_ucs2_t *s) +BOOL strupper_w(smb_ucs2_t *s) { + BOOL ret = False; while (*s) { - if (islower_w(*s)) + if (islower_w(*s)) { *s = toupper_w(*s); + ret = True; + } s++; } + return ret; } /******************************************************************* -- cgit From dc1fc3ee8ec2199bc73bb5d7ec711c6800f61d65 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Tue, 2 Oct 2001 04:29:50 +0000 Subject: Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header. (This used to be commit 2d0922b0eabfdc0aaf1d0797482fef47ed7fde8e) --- source3/lib/util_unistr.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 887c15f0d9..1054eab6e1 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -21,8 +21,6 @@ #include "includes.h" -extern int DEBUGLEVEL; - #ifndef MAXUNI #define MAXUNI 1024 #endif @@ -453,4 +451,3 @@ smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max) SAFE_FREE(ucs2_src); return dest; } - -- cgit From 9bcd133e9e7b0cfe974f273fb23409d660af8358 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 3 Oct 2001 12:18:20 +0000 Subject: switched over to a new method of handling uppercase/lowercase mappings for unicode strings. The new method relies on 3 files that are mmap'd at startup to provide the mapping tables. The upcase.dat and lowcase.dat tables should be the same on all systems. The valid.dat table says what characters are valid in 8.3 names, and differs between systems. I'm committing the japanese valid.dat here, in future we need some way of automatically installing and choosing a appropriate table. This commit also adds my mini tdb based gettext replacement in intl/lang_tdb.c. I have not enabled this yet and have not removed the old gettext code as the new code is still being looked at by Monyo. Right now the code assumes that the upcase.dat, lowcase.dat and valid.dat files are installed in the Samba lib directory. That is not a good choice, but I'll leave them there until we work out the new install directory structure for Samba 3.0. simo - please look at the isvalid_w() function and think about using it in your new mangling code. That should be the final step to correctly passing the chargen test code from monyo. (This used to be commit 1c221994f118dd542a158b2db51e07d04d0e9314) --- source3/lib/util_unistr.c | 139 +++++++++++++++++++++++----------------------- 1 file changed, 70 insertions(+), 69 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 1054eab6e1..a0e1b88eb8 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -25,6 +25,53 @@ #define MAXUNI 1024 #endif +/* these 3 tables define the unicode case handling. They are loaded + at startup either via mmap() or read() from the lib directory */ +static smb_ucs2_t *upcase_table; +static smb_ucs2_t *lowcase_table; +static uint8 *valid_table; + +/******************************************************************* +load the case handling tables +********************************************************************/ +void load_case_tables(void) +{ + static int initialised; + int i; + + if (initialised) return; + initialised = 1; + + upcase_table = map_file(lib_path("upcase.dat"), 0x20000); + lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000); + valid_table = map_file(lib_path("valid.dat"), 0x10000); + + /* we would like Samba to limp along even if these tables are + not available */ + if (!upcase_table) { + DEBUG(1,("creating lame upcase table\n")); + upcase_table = malloc(0x20000); + for (i=0;i<256;i++) upcase_table[i] = islower(i)?toupper(i):i; + for (;i<0x10000;i++) upcase_table[i] = i; + } + + if (!lowcase_table) { + DEBUG(1,("creating lame lowcase table\n")); + lowcase_table = malloc(0x20000); + for (i=0;i<256;i++) lowcase_table[i] = isupper(i)?tolower(i):i; + for (;i<0x10000;i++) lowcase_table[i] = i; + } + + if (!valid_table) { + DEBUG(1,("creating lame valid table\n")); + valid_table = malloc(0x10000); + for (i=0;i<256;i++) valid_table[i] = + isalnum(i) && !strchr("*\\/?<>|\":", i); + for (;i<0x10000;i++) valid_table[i] = 0; + } +} + + /******************************************************************* Write a string in (little-endian) unicode format. src is in the current DOS codepage. len is the length in bytes of the @@ -129,94 +176,46 @@ uint32 buffer2_to_uint32(BUFFER2 *str) } /******************************************************************* - Mapping tables for UNICODE character. Allows toupper/tolower and - isXXX functions to work. - - tridge: split into 2 pieces. This saves us 5/6 of the memory - with a small speed penalty - The magic constants are the lower/upper range of the tables two - parts + Convert a wchar to upper case. ********************************************************************/ -typedef struct { - smb_ucs2_t lower; - smb_ucs2_t upper; - unsigned char flags; -} smb_unicode_table_t; - -#define TABLE1_BOUNDARY 9450 -#define TABLE2_BOUNDARY 64256 - -static smb_unicode_table_t map_table1[] = { -#include "unicode_map_table1.h" -}; - -static smb_unicode_table_t map_table2[] = { -#include "unicode_map_table2.h" -}; - -static unsigned char map_table_flags(smb_ucs2_t v) -{ - v = SVAL(&v,0); - if (v < TABLE1_BOUNDARY) return map_table1[v].flags; - if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].flags; - return 0; -} - -static smb_ucs2_t map_table_lower(smb_ucs2_t v) -{ - v = SVAL(&v,0); - if (v < TABLE1_BOUNDARY) return map_table1[v].lower; - if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].lower; - return v; -} - -static smb_ucs2_t map_table_upper(smb_ucs2_t v) +smb_ucs2_t toupper_w(smb_ucs2_t val) { - v = SVAL(&v,0); - if (v < TABLE1_BOUNDARY) return map_table1[v].upper; - if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].upper; - return v; + return upcase_table[val]; } /******************************************************************* - Is an upper case wchar. + Convert a wchar to lower case. ********************************************************************/ -int isupper_w( smb_ucs2_t val) +smb_ucs2_t tolower_w( smb_ucs2_t val ) { - return (map_table_flags(val) & UNI_UPPER); + return lowcase_table[val]; } /******************************************************************* - Is a lower case wchar. +determine if a character is lowercase ********************************************************************/ - -int islower_w( smb_ucs2_t val) +BOOL islower_w(smb_ucs2_t c) { - return (map_table_flags(val) & UNI_LOWER); + return upcase_table[c] != c; } /******************************************************************* - Convert a wchar to upper case. +determine if a character is uppercase ********************************************************************/ - -smb_ucs2_t toupper_w( smb_ucs2_t val ) +BOOL isupper_w(smb_ucs2_t c) { - val = map_table_upper(val); - val = SVAL(&val,0); - return val; + return lowcase_table[c] != c; } + /******************************************************************* - Convert a wchar to lower case. +determine if a character is valid in a 8.3 name ********************************************************************/ - -smb_ucs2_t tolower_w( smb_ucs2_t val ) +BOOL isvalid83_w(smb_ucs2_t c) { - val = map_table_lower(val); - val = SVAL(&val,0); - return val; + return valid_table[c] != 0; } /******************************************************************* @@ -252,8 +251,9 @@ BOOL strlower_w(smb_ucs2_t *s) { BOOL ret = False; while (*s) { - if (isupper_w(*s)) { - *s = tolower_w(*s); + smb_ucs2_t v = tolower_w(*s); + if (v != *s) { + *s = v; ret = True; } s++; @@ -269,8 +269,9 @@ BOOL strupper_w(smb_ucs2_t *s) { BOOL ret = False; while (*s) { - if (islower_w(*s)) { - *s = toupper_w(*s); + smb_ucs2_t v = toupper_w(*s); + if (v != *s) { + *s = v; ret = True; } s++; @@ -283,7 +284,7 @@ case insensitive string comparison ********************************************************************/ int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) { - while (*b && tolower_w(*a) == tolower_w(*b)) { a++; b++; } + while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; } return (tolower_w(*a) - tolower_w(*b)); } -- cgit From 9d4e919f4a3c901c9e70bd710335f94cc5b726df Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 3 Oct 2001 14:55:45 +0000 Subject: fixed basic ucs2 operation on big endian boxes. Still a bit more to do, but at least you can connect now. (This used to be commit 211aeffa1a770716f798fd6ba12ab29a95555526) --- source3/lib/util_unistr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index a0e1b88eb8..215a58669d 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -181,7 +181,7 @@ uint32 buffer2_to_uint32(BUFFER2 *str) smb_ucs2_t toupper_w(smb_ucs2_t val) { - return upcase_table[val]; + return upcase_table[SVAL(&val,0)]; } /******************************************************************* @@ -190,7 +190,7 @@ smb_ucs2_t toupper_w(smb_ucs2_t val) smb_ucs2_t tolower_w( smb_ucs2_t val ) { - return lowcase_table[val]; + return lowcase_table[SVAL(&val,0)]; } /******************************************************************* @@ -198,7 +198,7 @@ determine if a character is lowercase ********************************************************************/ BOOL islower_w(smb_ucs2_t c) { - return upcase_table[c] != c; + return upcase_table[SVAL(&c,0)] != c; } /******************************************************************* @@ -206,7 +206,7 @@ determine if a character is uppercase ********************************************************************/ BOOL isupper_w(smb_ucs2_t c) { - return lowcase_table[c] != c; + return lowcase_table[SVAL(&c,0)] != c; } @@ -215,7 +215,7 @@ determine if a character is valid in a 8.3 name ********************************************************************/ BOOL isvalid83_w(smb_ucs2_t c) { - return valid_table[c] != 0; + return valid_table[SVAL(&c,0)] != 0; } /******************************************************************* -- cgit From b15b39b77c23444203a24f18673650c5269eb73e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 7 Oct 2001 14:51:54 +0000 Subject: fixed lame valid table (This used to be commit 67017619afed8fe10abd65d54538348d3ea92b9f) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 215a58669d..1e73669e4e 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -63,10 +63,10 @@ void load_case_tables(void) } if (!valid_table) { + const char *allowed = "!#$%&'()_-.@^`{}~"; DEBUG(1,("creating lame valid table\n")); valid_table = malloc(0x10000); - for (i=0;i<256;i++) valid_table[i] = - isalnum(i) && !strchr("*\\/?<>|\":", i); + for (i=0;i<256;i++) valid_table[i] = isalnum(i) || strchr(allowed,i); for (;i<0x10000;i++) valid_table[i] = 0; } } -- cgit From f55172c51c9bd27c72bf6d7af877926bac90a8f8 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 9 Oct 2001 19:12:48 +0000 Subject: old fixes I forgot to commit (This used to be commit 7895593929f443c6a256d2a13035aee11bed3dbf) --- source3/lib/util_unistr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 1e73669e4e..7d690ecb9c 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -297,14 +297,14 @@ smb_ucs2_t *strdup_w(const smb_ucs2_t *src) smb_ucs2_t *dest; uint32 len; - len = strlen_w(src); - dest = (smb_ucs2_t *)malloc((len+1)*sizeof(smb_ucs2_t)); + len = strlen_w(src) + 1; + dest = (smb_ucs2_t *)malloc(len*sizeof(smb_ucs2_t)); if (!dest) { DEBUG(0,("strdup_w: out of memory!\n")); return NULL; } - memcpy(dest, src, (len+1)*sizeof(smb_ucs2_t)); + memcpy(dest, src, len*sizeof(smb_ucs2_t)); return dest; } @@ -343,8 +343,8 @@ smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) len = strlen_w(src); if (len > max) len = max; - memcpy(&dest[start], src, len); - dest[start+len+1] = 0; + memcpy(&dest[start], src, len*sizeof(smb_ucs2_t)); + dest[start+len] = 0; return dest; } -- cgit From a896bbcb5a5be2cfb248d9e3ad01633c36a3e423 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 4 Nov 2001 18:10:31 +0000 Subject: remove {} from default valid char list (This used to be commit 5dd3c7b3fb8aac7fb3a256ae40c882fb8983537f) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 7d690ecb9c..d0e2a119b8 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -63,7 +63,7 @@ void load_case_tables(void) } if (!valid_table) { - const char *allowed = "!#$%&'()_-.@^`{}~"; + const char *allowed = "!#$%&'()_-@^`~"; DEBUG(1,("creating lame valid table\n")); valid_table = malloc(0x10000); for (i=0;i<256;i++) valid_table[i] = isalnum(i) || strchr(allowed,i); -- cgit From 740d6f5dd60bef72037ed5fcd7b2192af22c2e41 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 4 Nov 2001 18:26:53 +0000 Subject: a big one: - old mangle code has gone, the new one based on tdb seem resonably ok probably the valid.dat table need to be updated to treat wild chars as invalid ones (work ok without it) - a LOT of new string manipulation function for unicode, they are somewhat tested but a review would not be bad - some new function I will need for the new unix_convert function I'm writing, this will be renamed filename_convert and use only unicode strings. - charconv, I attached a comment, if someone wnat to look if I'm right or just was hacking to late in the night to make a sane one :) of course any bug is my responsibility an will be pleased to see patches if you find any. :-) Simo. (This used to be commit ee19f7efb6ea9216fc91cf112ac1afa691983e9d) --- source3/lib/util_unistr.c | 205 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 182 insertions(+), 23 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index d0e2a119b8..287472ad65 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -242,6 +242,52 @@ smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) return NULL; } +smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) +{ + const smb_ucs2_t *p = s; + int len = strlen_w(s); + if (len == 0) return NULL; + p += (len - 1); + do { + if (c == *p) return (smb_ucs2_t *)p; + } while (p-- != s); + return NULL; +} + +/******************************************************************* +wide strstr() +********************************************************************/ +smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) +{ + smb_ucs2_t *r; + size_t slen, inslen; + + if (!s || !*s || !ins || !*ins) return NULL; + slen = strlen_w(s); + inslen = strlen_w(ins); + r = (smb_ucs2_t *)s; + while (r = strchr_w(r, *ins)) { + if (strncmp_w(r, ins, inslen) == 0) return r; + r++; + } + return NULL; +} + +smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) +{ + smb_ucs2_t *r; + size_t slen, inslen; + + if (!s || !*s || !ins || !*ins) return NULL; + slen = strlen_w(s); + inslen = strlen(ins); + r = (smb_ucs2_t *)s; + while (r = strchr_w(r, UCS2_CHAR(*ins))) { + if (strncmp_wa(r, ins, inslen) == 0) return r; + r++; + } + return NULL; +} /******************************************************************* Convert a string to lower case. @@ -279,6 +325,18 @@ BOOL strupper_w(smb_ucs2_t *s) return ret; } +/******************************************************************* + convert a string to "normal" form +********************************************************************/ +void strnorm_w(smb_ucs2_t *s) +{ + extern int case_default; + if (case_default == CASE_UPPER) + strupper_w(s); + else + strlower_w(s); +} + /******************************************************************* case insensitive string comparison ********************************************************************/ @@ -288,23 +346,60 @@ int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) return (tolower_w(*a) - tolower_w(*b)); } +/******************************************************************* +case insensitive string comparison, lenght limited +********************************************************************/ +int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) +{ + size_t n = 0; + while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; } + return (len - n)?(tolower_w(*a) - tolower_w(*b)):0; +} + +/******************************************************************* + compare 2 strings +********************************************************************/ +BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) +{ + if (s1 == s2) return(True); + if (!s1 || !s2) return(False); + + return(strcasecmp_w(s1,s2)==0); +} + +/******************************************************************* + compare 2 strings up to and including the nth char. + ******************************************************************/ +BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n) +{ + if (s1 == s2) return(True); + if (!s1 || !s2 || !n) return(False); + + return(strncasecmp_w(s1,s2,n)==0); +} /******************************************************************* duplicate string ********************************************************************/ smb_ucs2_t *strdup_w(const smb_ucs2_t *src) +{ + return strndup_w(src, 0); +} + +/* if len == 0 then duplicate the whole string */ +smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len) { smb_ucs2_t *dest; - uint32 len; - len = strlen_w(src) + 1; - dest = (smb_ucs2_t *)malloc(len*sizeof(smb_ucs2_t)); + if (!len) len = strlen_w(src); + dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t)); if (!dest) { DEBUG(0,("strdup_w: out of memory!\n")); return NULL; } - memcpy(dest, src, len*sizeof(smb_ucs2_t)); + memcpy(dest, src, len * sizeof(smb_ucs2_t)); + dest[len] = 0; return dest; } @@ -368,33 +463,33 @@ void pstrcpy_wa(smb_ucs2_t *dest, const char *src) } } -int strcmp_wa(const smb_ucs2_t *a, const char *b) +int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) { - while (*b && *a == UCS2_CHAR(*b)) { a++; b++; } - return (*a - UCS2_CHAR(*b)); + while (*b && *a == *b) { a++; b++; } + return (*a - *b); + /* warning: if *a != *b and both are not 0 we retrun a random + greater or lesser than 0 number not realted to which + string is longer */ } +int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) +{ + size_t n = 0; + while ((n < len) && *b && *a == *b) { a++; b++; n++;} + return (len - n)?(*a - *b):0; +} - -smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c) +int strcmp_wa(const smb_ucs2_t *a, const char *b) { - while (*s != 0) { - if (UCS2_CHAR(c) == *s) return (smb_ucs2_t *)s; - s++; - } - return NULL; + while (*b && *a == UCS2_CHAR(*b)) { a++; b++; } + return (*a - UCS2_CHAR(*b)); } -smb_ucs2_t *strrchr_wa(const smb_ucs2_t *s, char c) +int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len) { - const smb_ucs2_t *p = s; - int len = strlen_w(s); - if (len == 0) return NULL; - p += (len-1); - do { - if (UCS2_CHAR(c) == *p) return (smb_ucs2_t *)p; - } while (p-- != s); - return NULL; + size_t n = 0; + while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;} + return (len - n)?(*a - UCS2_CHAR(*b)):0; } smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) @@ -452,3 +547,67 @@ smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max) SAFE_FREE(ucs2_src); return dest; } + +/******************************************************************* +replace any occurence of oldc with newc in unicode string +********************************************************************/ + +void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) +{ + for(;*s;s++) { + if(*s==oldc) *s=newc; + } +} + +/******************************************************************* +trim unicode string +********************************************************************/ + +BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, + const smb_ucs2_t *back) +{ + BOOL ret = False; + size_t len, lw, front_len, flw, back_len, blw; + + if (!s || !*s) return False; + + len = strlen_w(s); + + if (front && *front) { + front_len = strlen_w(front); + flw = front_len * sizeof(smb_ucs2_t); + lw = (len + 1) * sizeof(smb_ucs2_t); + while (len && strncmp_w(s, front, front_len) == 0) { + memcpy(s, s + flw, lw - flw); + len -= front_len; + lw -= flw; + ret = True; + } + } + + if (back && *back) { + back_len = strlen_w(back); + blw = back_len * sizeof(smb_ucs2_t); + lw = len * sizeof(smb_ucs2_t); + while (len && strncmp_w(s + lw - blw, back, back_len) == 0) { + s[len - back_len] = 0; + len -= back_len; + lw -= blw; + ret = True; + } + } + + return ret; +} + +BOOL trim_string_wa(smb_ucs2_t *s, const char *front, + const char *back) +{ + wpstring f, b; + + if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE); + else *f = 0; + if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE); + else *b = 0; + return trim_string_w(s, f, b); +} -- cgit From 59bc781854d623235149eb329fd5c3b89e10501a Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sun, 4 Nov 2001 21:10:17 +0000 Subject: Added missing strchr_wa. Jeremy. (This used to be commit 16c5d279b97250ceb84ee6de6e5e801350c816c4) --- source3/lib/util_unistr.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 287472ad65..8a7253a718 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -242,6 +242,11 @@ smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) return NULL; } +smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c) +{ + return strchr_w(s, UCS2_CHAR(c)); +} + smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { const smb_ucs2_t *p = s; -- cgit From 5d152d24a39386a7b595f9fc157d86dff38c39dc Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sat, 10 Nov 2001 15:21:54 +0000 Subject: fixed, moved and added some functions note the useful acnv_uxu2 and acnv_u2ux functions in charcnv.c (This used to be commit 64dde3b64fc091cda95fc4ed145595b5d79b2e01) --- source3/lib/util_unistr.c | 205 +++++++++++++++++++++++++--------------------- 1 file changed, 112 insertions(+), 93 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8a7253a718..08281fec4f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -271,29 +271,13 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) slen = strlen_w(s); inslen = strlen_w(ins); r = (smb_ucs2_t *)s; - while (r = strchr_w(r, *ins)) { + while ((r = strchr_w(r, *ins))) { if (strncmp_w(r, ins, inslen) == 0) return r; r++; } return NULL; } -smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) -{ - smb_ucs2_t *r; - size_t slen, inslen; - - if (!s || !*s || !ins || !*ins) return NULL; - slen = strlen_w(s); - inslen = strlen(ins); - r = (smb_ucs2_t *)s; - while (r = strchr_w(r, UCS2_CHAR(*ins))) { - if (strncmp_wa(r, ins, inslen) == 0) return r; - r++; - } - return NULL; -} - /******************************************************************* Convert a string to lower case. return True if any char is converted @@ -342,6 +326,22 @@ void strnorm_w(smb_ucs2_t *s) strlower_w(s); } +int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) +{ + while (*b && *a == *b) { a++; b++; } + return (*a - *b); + /* warning: if *a != *b and both are not 0 we retrun a random + greater or lesser than 0 number not realted to which + string is longer */ +} + +int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) +{ + size_t n = 0; + while ((n < len) && *b && *a == *b) { a++; b++; n++;} + return (len - n)?(*a - *b):0; +} + /******************************************************************* case insensitive string comparison ********************************************************************/ @@ -449,6 +449,74 @@ smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) return dest; } +smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src) +{ + size_t start; + size_t len; + + if (!dest || !src) return NULL; + + start = strlen_w(dest); + len = strlen_w(src); + + memcpy(&dest[start], src, len*sizeof(smb_ucs2_t)); + dest[start+len] = 0; + + return dest; +} + + +/******************************************************************* +replace any occurence of oldc with newc in unicode string +********************************************************************/ + +void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) +{ + for(;*s;s++) { + if(*s==oldc) *s=newc; + } +} + +/******************************************************************* +trim unicode string +********************************************************************/ + +BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, + const smb_ucs2_t *back) +{ + BOOL ret = False; + size_t len, lw, front_len, flw, back_len, blw; + + if (!s || !*s) return False; + + len = strlen_w(s); + + if (front && *front) { + front_len = strlen_w(front); + flw = front_len * sizeof(smb_ucs2_t); + lw = (len + 1) * sizeof(smb_ucs2_t); + while (len && strncmp_w(s, front, front_len) == 0) { + memcpy(s, s + flw, lw - flw); + len -= front_len; + lw -= flw; + ret = True; + } + } + + if (back && *back) { + back_len = strlen_w(back); + blw = back_len * sizeof(smb_ucs2_t); + lw = len * sizeof(smb_ucs2_t); + while (len && strncmp_w(s + lw - blw, back, back_len) == 0) { + s[len - back_len] = 0; + len -= back_len; + lw -= blw; + ret = True; + } + } + + return ret; +} /* The *_wa() functions take a combination of 7 bit ascii @@ -468,22 +536,6 @@ void pstrcpy_wa(smb_ucs2_t *dest, const char *src) } } -int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) -{ - while (*b && *a == *b) { a++; b++; } - return (*a - *b); - /* warning: if *a != *b and both are not 0 we retrun a random - greater or lesser than 0 number not realted to which - string is longer */ -} - -int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) -{ - size_t n = 0; - while ((n < len) && *b && *a == *b) { a++; b++; n++;} - return (len - n)?(*a - *b):0; -} - int strcmp_wa(const smb_ucs2_t *a, const char *b) { while (*b && *a == UCS2_CHAR(*b)) { a++; b++; } @@ -509,6 +561,21 @@ smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) return NULL; } +smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) +{ + smb_ucs2_t *r; + size_t slen, inslen; + + if (!s || !*s || !ins || !*ins) return NULL; + slen = strlen_w(s); + inslen = strlen(ins); + r = (smb_ucs2_t *)s; + while ((r = strchr_w(r, UCS2_CHAR(*ins)))) { + if (strncmp_wa(r, ins, inslen) == 0) return r; + r++; + } + return NULL; +} /******************************************************************* copy a string with max len @@ -519,19 +586,14 @@ smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max) smb_ucs2_t *ucs2_src; if (!dest || !src) return NULL; - ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t)); - if (!ucs2_src) { - DEBUG(0,("strncpy_wa: out of memory!\n")); + if (!(ucs2_src = acnv_uxu2(src))) return NULL; - } - push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN); strncpy_w(dest, ucs2_src, max); SAFE_FREE(ucs2_src); return dest; } - /******************************************************************* append a string of len bytes and add a terminator ********************************************************************/ @@ -541,68 +603,25 @@ smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max) smb_ucs2_t *ucs2_src; if (!dest || !src) return NULL; - ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t)); - if (!ucs2_src) { - DEBUG(0,("strncat_wa: out of memory!\n")); + if (!(ucs2_src = acnv_uxu2(src))) return NULL; - } - push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN); strncat_w(dest, ucs2_src, max); SAFE_FREE(ucs2_src); return dest; } -/******************************************************************* -replace any occurence of oldc with newc in unicode string -********************************************************************/ - -void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) -{ - for(;*s;s++) { - if(*s==oldc) *s=newc; - } -} - -/******************************************************************* -trim unicode string -********************************************************************/ - -BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, - const smb_ucs2_t *back) -{ - BOOL ret = False; - size_t len, lw, front_len, flw, back_len, blw; - - if (!s || !*s) return False; - - len = strlen_w(s); - - if (front && *front) { - front_len = strlen_w(front); - flw = front_len * sizeof(smb_ucs2_t); - lw = (len + 1) * sizeof(smb_ucs2_t); - while (len && strncmp_w(s, front, front_len) == 0) { - memcpy(s, s + flw, lw - flw); - len -= front_len; - lw -= flw; - ret = True; - } - } +smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src) +{ + smb_ucs2_t *ucs2_src; - if (back && *back) { - back_len = strlen_w(back); - blw = back_len * sizeof(smb_ucs2_t); - lw = len * sizeof(smb_ucs2_t); - while (len && strncmp_w(s + lw - blw, back, back_len) == 0) { - s[len - back_len] = 0; - len -= back_len; - lw -= blw; - ret = True; - } - } - - return ret; + if (!dest || !src) return NULL; + if (!(ucs2_src = acnv_uxu2(src))) + return NULL; + + strcat_w(dest, ucs2_src); + SAFE_FREE(ucs2_src); + return dest; } BOOL trim_string_wa(smb_ucs2_t *s, const char *front, -- cgit From 84244244710790f2058ee90b2dc80e9c252841dd Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 12 Nov 2001 00:53:34 +0000 Subject: some bugfix and new functions, modified mangle.c to use mosltly acnv_????() functions. this should make also build farm happy (This used to be commit 8bb5cb27c2012b8967482255d48a1b48d3acd9db) --- source3/lib/util_unistr.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 08281fec4f..58ecc19723 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -230,6 +230,18 @@ size_t strlen_w(const smb_ucs2_t *src) return len; } +/******************************************************************* + Count up to max number of characters in a smb_ucs2_t string. +********************************************************************/ +size_t strnlen_w(const smb_ucs2_t *src, size_t max) +{ + size_t len; + + for(len = 0; *src++ && (len < max); len++) ; + + return len; +} + /******************************************************************* wide strchr() ********************************************************************/ @@ -440,8 +452,7 @@ smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) if (!dest || !src) return NULL; start = strlen_w(dest); - len = strlen_w(src); - if (len > max) len = max; + len = strnlen_w(src, max); memcpy(&dest[start], src, len*sizeof(smb_ucs2_t)); dest[start+len] = 0; @@ -465,7 +476,6 @@ smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src) return dest; } - /******************************************************************* replace any occurence of oldc with newc in unicode string ********************************************************************/ @@ -594,6 +604,35 @@ smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max) return dest; } +/******************************************************************* +convert and duplicate an ascii string +********************************************************************/ +smb_ucs2_t *strdup_wa(const char *src) +{ + return strndup_wa(src, 0); +} + +/* if len == 0 then duplicate the whole string */ +smb_ucs2_t *strndup_wa(const char *src, size_t len) +{ + smb_ucs2_t *dest, *s; + + s = acnv_dosu2(src); + if (!len) len = strlen_w(s); + dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t)); + if (!dest) { + DEBUG(0,("strdup_w: out of memory!\n")); + SAFE_FREE(s); + return NULL; + } + + memcpy(dest, src, len * sizeof(smb_ucs2_t)); + dest[len] = 0; + + SAFE_FREE(s); + return dest; +} + /******************************************************************* append a string of len bytes and add a terminator ********************************************************************/ -- cgit From 60906f657cb070929e96b4c81b8e24bae3a4ed61 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 18 Nov 2001 16:12:11 +0000 Subject: fixed some bugs. (This used to be commit 37edaeddce09193450b18b1b85aa41960cb39741) --- source3/lib/util_unistr.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 58ecc19723..d9bd4a4c06 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -3,6 +3,7 @@ Version 3.0 Samba utility functions Copyright (C) Andrew Tridgell 1992-2001 + Copyright (C) Simo Sorce 2001 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -476,6 +477,7 @@ smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src) return dest; } + /******************************************************************* replace any occurence of oldc with newc in unicode string ********************************************************************/ @@ -495,7 +497,7 @@ BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, const smb_ucs2_t *back) { BOOL ret = False; - size_t len, lw, front_len, flw, back_len, blw; + size_t len, front_len, back_len; if (!s || !*s) return False; @@ -503,24 +505,18 @@ BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, if (front && *front) { front_len = strlen_w(front); - flw = front_len * sizeof(smb_ucs2_t); - lw = (len + 1) * sizeof(smb_ucs2_t); while (len && strncmp_w(s, front, front_len) == 0) { - memcpy(s, s + flw, lw - flw); + memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t)); len -= front_len; - lw -= flw; ret = True; } } if (back && *back) { back_len = strlen_w(back); - blw = back_len * sizeof(smb_ucs2_t); - lw = len * sizeof(smb_ucs2_t); - while (len && strncmp_w(s + lw - blw, back, back_len) == 0) { + while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) { s[len - back_len] = 0; len -= back_len; - lw -= blw; ret = True; } } -- cgit From 8bd89c72a37407dbc4bcbe15b0bd25cd7a12ccf3 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 29 Nov 2001 04:58:29 +0000 Subject: fixed lame char tables on big endian machines (This used to be commit 568425709d11b2eed86643d18973542d0504d5ca) --- source3/lib/util_unistr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index d9bd4a4c06..5d4697e441 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -52,22 +52,22 @@ void load_case_tables(void) if (!upcase_table) { DEBUG(1,("creating lame upcase table\n")); upcase_table = malloc(0x20000); - for (i=0;i<256;i++) upcase_table[i] = islower(i)?toupper(i):i; - for (;i<0x10000;i++) upcase_table[i] = i; + for (i=0;i<256;i++) upcase_table[i] = UCS2_CHAR(islower(i)?toupper(i):i); + for (;i<0x10000;i++) upcase_table[i] = UCS2_CHAR(i); } if (!lowcase_table) { DEBUG(1,("creating lame lowcase table\n")); lowcase_table = malloc(0x20000); - for (i=0;i<256;i++) lowcase_table[i] = isupper(i)?tolower(i):i; - for (;i<0x10000;i++) lowcase_table[i] = i; + for (i=0;i<256;i++) lowcase_table[i] = UCS2_CHAR(isupper(i)?tolower(i):i); + for (;i<0x10000;i++) lowcase_table[i] = UCS2_CHAR(i); } if (!valid_table) { const char *allowed = "!#$%&'()_-@^`~"; DEBUG(1,("creating lame valid table\n")); valid_table = malloc(0x10000); - for (i=0;i<256;i++) valid_table[i] = isalnum(i) || strchr(allowed,i); + for (i=0;i<256;i++) valid_table[i] = UCS2_CHAR(isalnum(i) || strchr(allowed,i)); for (;i<0x10000;i++) valid_table[i] = 0; } } -- cgit From fbcf770865d8c5273423bb45ab0ebc73dc449dbc Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 29 Nov 2001 05:03:15 +0000 Subject: 2nd attempt at fixing lame char tables on big endian machines (This used to be commit 80841f5763b3ab03a82ab0a18a9126d89ffed2d5) --- source3/lib/util_unistr.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5d4697e441..44e792a9fd 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -52,23 +52,23 @@ void load_case_tables(void) if (!upcase_table) { DEBUG(1,("creating lame upcase table\n")); upcase_table = malloc(0x20000); - for (i=0;i<256;i++) upcase_table[i] = UCS2_CHAR(islower(i)?toupper(i):i); - for (;i<0x10000;i++) upcase_table[i] = UCS2_CHAR(i); + for (i=0;i<0x10000;i++) upcase_table[i] = i; + for (i=0;i<256;i++) upcase_table[UCS2_CHAR(i)] = UCS2_CHAR(islower(i)?toupper(i):i); } if (!lowcase_table) { DEBUG(1,("creating lame lowcase table\n")); lowcase_table = malloc(0x20000); - for (i=0;i<256;i++) lowcase_table[i] = UCS2_CHAR(isupper(i)?tolower(i):i); - for (;i<0x10000;i++) lowcase_table[i] = UCS2_CHAR(i); + for (i=0;i<0x10000;i++) upcase_table[i] = i; + for (i=0;i<256;i++) lowcase_table[UCS2_CHAR(i)] = UCS2_CHAR(isupper(i)?tolower(i):i); } if (!valid_table) { const char *allowed = "!#$%&'()_-@^`~"; DEBUG(1,("creating lame valid table\n")); valid_table = malloc(0x10000); - for (i=0;i<256;i++) valid_table[i] = UCS2_CHAR(isalnum(i) || strchr(allowed,i)); - for (;i<0x10000;i++) valid_table[i] = 0; + for (0;i<0x10000;i++) valid_table[i] = 0; + for (i=0;i<256;i++) valid_table[UCS2_CHAR(i)] = isalnum(i) || strchr(allowed,i); } } -- cgit From 5bfc7214afb23984acc7aba9fd7a57eec7c18d6d Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 29 Nov 2001 05:20:23 +0000 Subject: fixed toupper_w() and friends on big-endian this fixes the core dumps on sparc (This used to be commit e6004062a508b1fe6dd7f93d8a933da9916598f8) --- source3/lib/util_unistr.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 44e792a9fd..48f21782b3 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -67,7 +67,7 @@ void load_case_tables(void) const char *allowed = "!#$%&'()_-@^`~"; DEBUG(1,("creating lame valid table\n")); valid_table = malloc(0x10000); - for (0;i<0x10000;i++) valid_table[i] = 0; + for (i=0;i<0x10000;i++) valid_table[i] = 0; for (i=0;i<256;i++) valid_table[UCS2_CHAR(i)] = isalnum(i) || strchr(allowed,i); } } @@ -182,7 +182,7 @@ uint32 buffer2_to_uint32(BUFFER2 *str) smb_ucs2_t toupper_w(smb_ucs2_t val) { - return upcase_table[SVAL(&val,0)]; + return upcase_table[val]; } /******************************************************************* @@ -191,7 +191,7 @@ smb_ucs2_t toupper_w(smb_ucs2_t val) smb_ucs2_t tolower_w( smb_ucs2_t val ) { - return lowcase_table[SVAL(&val,0)]; + return lowcase_table[val]; } /******************************************************************* @@ -199,7 +199,7 @@ determine if a character is lowercase ********************************************************************/ BOOL islower_w(smb_ucs2_t c) { - return upcase_table[SVAL(&c,0)] != c; + return upcase_table[c] != c; } /******************************************************************* @@ -207,7 +207,7 @@ determine if a character is uppercase ********************************************************************/ BOOL isupper_w(smb_ucs2_t c) { - return lowcase_table[SVAL(&c,0)] != c; + return lowcase_table[c] != c; } @@ -216,7 +216,7 @@ determine if a character is valid in a 8.3 name ********************************************************************/ BOOL isvalid83_w(smb_ucs2_t c) { - return valid_table[SVAL(&c,0)] != 0; + return valid_table[c] != 0; } /******************************************************************* -- cgit From 7879a1a811c29660df282cba2cf6e95c50532b2b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 29 Nov 2001 06:26:05 +0000 Subject: fixed typo (This used to be commit 624cb28271289aa697f23c32f7b5159f6fcbb51d) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 48f21782b3..d2d23dafa5 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -59,7 +59,7 @@ void load_case_tables(void) if (!lowcase_table) { DEBUG(1,("creating lame lowcase table\n")); lowcase_table = malloc(0x20000); - for (i=0;i<0x10000;i++) upcase_table[i] = i; + for (i=0;i<0x10000;i++) lowcase_table[i] = i; for (i=0;i<256;i++) lowcase_table[UCS2_CHAR(i)] = UCS2_CHAR(isupper(i)?tolower(i):i); } -- cgit From de744cb970f5e3afb174a2d923d4e7d11b266173 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 1 Dec 2001 12:31:43 +0000 Subject: The beginnings of alternative backends for winbindd This just splits off the dispinfo call behind a methods structure. I'll split off a few more functions soon, then we will be ready for LDAP replacement methods (This used to be commit 0216b0fca115c903ec31ed21427a83c62077dc95) --- source3/lib/util_unistr.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index d2d23dafa5..fc29ca8dc2 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -164,6 +164,23 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) } +/******************************************************************* + duplicate a UNISTR2 string into a null terminated char* + using a talloc context +********************************************************************/ +char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str) +{ + char *s; + int maxlen = (str->uni_str_len+1)*4; + if (!str->buffer) return NULL; + s = (char *)talloc(ctx, maxlen); /* convervative */ + if (!s) return NULL; + pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, + STR_NOALIGN); + return s; +} + + /******************************************************************* Return a number stored in a buffer ********************************************************************/ -- cgit From 6c0146f74b13770509989c0057fe02bf1d5b4f7c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 20 Dec 2001 04:12:36 +0000 Subject: mark '.' as a valid character (This used to be commit 85f04fa61dffb15bc034dd756ee75b997a36d892) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index fc29ca8dc2..6034a715d3 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -64,7 +64,7 @@ void load_case_tables(void) } if (!valid_table) { - const char *allowed = "!#$%&'()_-@^`~"; + const char *allowed = ".!#$%&'()_-@^`~"; DEBUG(1,("creating lame valid table\n")); valid_table = malloc(0x10000); for (i=0;i<0x10000;i++) valid_table[i] = 0; -- cgit From ec312741639c447c83ad76683c9f1538a3f5084b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 20 Dec 2001 06:18:52 +0000 Subject: much better auto-init of valid_table[]. This should just about remove the need for valid.dat (This used to be commit 0cfd0a5e543181b1384f7afee93fbaf3ccb2b999) --- source3/lib/util_unistr.c | 50 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 6034a715d3..08fc1760ae 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -32,6 +32,7 @@ static smb_ucs2_t *upcase_table; static smb_ucs2_t *lowcase_table; static uint8 *valid_table; + /******************************************************************* load the case handling tables ********************************************************************/ @@ -45,7 +46,6 @@ void load_case_tables(void) upcase_table = map_file(lib_path("upcase.dat"), 0x20000); lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000); - valid_table = map_file(lib_path("valid.dat"), 0x10000); /* we would like Samba to limp along even if these tables are not available */ @@ -62,14 +62,50 @@ void load_case_tables(void) for (i=0;i<0x10000;i++) lowcase_table[i] = i; for (i=0;i<256;i++) lowcase_table[UCS2_CHAR(i)] = UCS2_CHAR(isupper(i)?tolower(i):i); } +} + +/* + see if a ucs2 character can be mapped correctly to a dos character + and mapped back to the same character in ucs2 +*/ +static int check_dos_char(smb_ucs2_t c) +{ + char buf[10]; + smb_ucs2_t c2 = 0; + int len1, len2; + len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf)); + if (len1 == 0) return 0; + len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2); + if (len2 != 2) return 0; + return (c == c2); +} + +/******************************************************************* +load the valid character map table +********************************************************************/ +void init_valid_table(void) +{ + static int initialised; + static int mapped_file; + int i; + const char *allowed = ".!#$%&'()_-@^`~"; + + if (initialised && mapped_file) return; + initialised = 1; - if (!valid_table) { - const char *allowed = ".!#$%&'()_-@^`~"; - DEBUG(1,("creating lame valid table\n")); - valid_table = malloc(0x10000); - for (i=0;i<0x10000;i++) valid_table[i] = 0; - for (i=0;i<256;i++) valid_table[UCS2_CHAR(i)] = isalnum(i) || strchr(allowed,i); + valid_table = map_file(lib_path("valid.dat"), 0x10000); + if (valid_table) { + mapped_file = 1; + return; } + + if (valid_table) free(valid_table); + + DEBUG(2,("creating default valid table\n")); + valid_table = malloc(0x10000); + for (i=0;i<128;i++) valid_table[UCS2_CHAR(i)] = isalnum(i) || + strchr(allowed,i); + for (;i<0x10000;i++) valid_table[UCS2_CHAR(i)] = check_dos_char(i); } -- cgit From 93d458c5f68a7168ce543e820906bc55a5d3a339 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 20 Dec 2001 10:02:30 +0000 Subject: fixed warnings on irix and crash bug on big endian machines (This used to be commit cc6c263993eaf0715f231fc80ca7e6e65694548b) --- source3/lib/util_unistr.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 08fc1760ae..0a0424763d 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -105,7 +105,11 @@ void init_valid_table(void) valid_table = malloc(0x10000); for (i=0;i<128;i++) valid_table[UCS2_CHAR(i)] = isalnum(i) || strchr(allowed,i); - for (;i<0x10000;i++) valid_table[UCS2_CHAR(i)] = check_dos_char(i); + for (;i<0x10000;i++) { + smb_ucs2_t c; + SSVAL(&c, 0, i); + valid_table[c] = check_dos_char(c); + } } -- cgit From cd68afe31256ad60748b34f7318a180cfc2127cc Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 30 Jan 2002 06:08:46 +0000 Subject: Removed version number from file header. Changed "SMB/Netbios" to "SMB/CIFS" in file header. (This used to be commit 6a58c9bd06d0d7502a24bf5ce5a2faf0a146edfa) --- source3/lib/util_unistr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 0a0424763d..31b557bbfc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -1,6 +1,5 @@ /* - Unix SMB/Netbios implementation. - Version 3.0 + Unix SMB/CIFS implementation. Samba utility functions Copyright (C) Andrew Tridgell 1992-2001 Copyright (C) Simo Sorce 2001 -- cgit From 99c4646a198a6a49a087e22a7d03af495d044ec6 Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Wed, 6 Mar 2002 20:38:51 +0000 Subject: fixed the upper/lower case table generation on big-endian machines (tridge, using Herbs console) (This used to be commit e5d80779a384c9a806fc545032330f760d8c11cb) --- source3/lib/util_unistr.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 31b557bbfc..45e6e6d87e 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -51,15 +51,31 @@ void load_case_tables(void) if (!upcase_table) { DEBUG(1,("creating lame upcase table\n")); upcase_table = malloc(0x20000); - for (i=0;i<0x10000;i++) upcase_table[i] = i; - for (i=0;i<256;i++) upcase_table[UCS2_CHAR(i)] = UCS2_CHAR(islower(i)?toupper(i):i); + for (i=0;i<0x10000;i++) { + smb_ucs2_t v; + SSVAL(&v, 0, i); + upcase_table[v] = i; + } + for (i=0;i<256;i++) { + smb_ucs2_t v; + SSVAL(&v, 0, UCS2_CHAR(i)); + upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i); + } } if (!lowcase_table) { DEBUG(1,("creating lame lowcase table\n")); lowcase_table = malloc(0x20000); - for (i=0;i<0x10000;i++) lowcase_table[i] = i; - for (i=0;i<256;i++) lowcase_table[UCS2_CHAR(i)] = UCS2_CHAR(isupper(i)?tolower(i):i); + for (i=0;i<0x10000;i++) { + smb_ucs2_t v; + SSVAL(&v, 0, i); + lowcase_table[v] = i; + } + for (i=0;i<256;i++) { + smb_ucs2_t v; + SSVAL(&v, 0, UCS2_CHAR(i)); + lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i); + } } } @@ -238,7 +254,7 @@ uint32 buffer2_to_uint32(BUFFER2 *str) smb_ucs2_t toupper_w(smb_ucs2_t val) { - return upcase_table[val]; + return upcase_table[SVAL(&val,0)]; } /******************************************************************* @@ -247,7 +263,8 @@ smb_ucs2_t toupper_w(smb_ucs2_t val) smb_ucs2_t tolower_w( smb_ucs2_t val ) { - return lowcase_table[val]; + return lowcase_table[SVAL(&val,0)]; + } /******************************************************************* @@ -255,7 +272,7 @@ determine if a character is lowercase ********************************************************************/ BOOL islower_w(smb_ucs2_t c) { - return upcase_table[c] != c; + return upcase_table[SVAL(&c,0)] != c; } /******************************************************************* @@ -263,7 +280,7 @@ determine if a character is uppercase ********************************************************************/ BOOL isupper_w(smb_ucs2_t c) { - return lowcase_table[c] != c; + return lowcase_table[SVAL(&c,0)] != c; } @@ -272,7 +289,7 @@ determine if a character is valid in a 8.3 name ********************************************************************/ BOOL isvalid83_w(smb_ucs2_t c) { - return valid_table[c] != 0; + return valid_table[SVAL(&c,0)] != 0; } /******************************************************************* -- cgit From 55058b074f9a063d74ce027176451c4021a97c5f Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Thu, 21 Mar 2002 14:01:57 +0000 Subject: must use native endian index when creating default valid.dat table so it is the same on big and little endian systems. (This used to be commit 6fa09ee88143882abcc51de05002261db767d775) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 45e6e6d87e..060460bb2c 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -118,12 +118,12 @@ void init_valid_table(void) DEBUG(2,("creating default valid table\n")); valid_table = malloc(0x10000); - for (i=0;i<128;i++) valid_table[UCS2_CHAR(i)] = isalnum(i) || + for (i=0;i<128;i++) valid_table[i] = isalnum(i) || strchr(allowed,i); for (;i<0x10000;i++) { smb_ucs2_t c; SSVAL(&c, 0, i); - valid_table[c] = check_dos_char(c); + valid_table[i] = check_dos_char(c); } } -- cgit From 737423f06ea08c38592b408faa12a55a95b9d696 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 26 Mar 2002 03:15:30 +0000 Subject: OpenPrinter() merge from 2.2 (This used to be commit 619397cc90549d4602ecddc25ee50eb247c913ee) --- source3/lib/util_unistr.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 060460bb2c..a1cff26169 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -743,3 +743,35 @@ BOOL trim_string_wa(smb_ucs2_t *s, const char *front, else *b = 0; return trim_string_w(s, f, b); } + +/******************************************************************* + returns the length in number of wide characters + ******************************************************************/ +int unistrlen(uint16 *s) +{ + int len; + + if (!s) + return -1; + + for (len=0; *s; s++,len++); + + return len; +} + +/******************************************************************* + Strcpy for unicode strings. returns length (in num of wide chars) +********************************************************************/ + +int unistrcpy(uint16 *dst, uint16 *src) +{ + int num_wchars = 0; + + while (*src) { + *dst++ = *src++; + num_wchars++; + } + *dst = 0; + + return num_wchars; +} -- cgit From e90b65284812aaa5ff9e9935ce9bbad7791cbbcd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 15 Jul 2002 10:35:28 +0000 Subject: updated the 3.0 branch from the head branch - ready for alpha18 (This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce) --- source3/lib/util_unistr.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index a1cff26169..ba02819bdc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -775,3 +775,44 @@ int unistrcpy(uint16 *dst, uint16 *src) return num_wchars; } + +/** + * Samba ucs2 type to UNISTR2 conversion + * + * @param ctx Talloc context to create the dst strcture (if null) and the + * contents of the unicode string. + * @param dst UNISTR2 destination. If equals null, then it's allocated. + * @param src smb_ucs2_t source. + * @param max_len maximum number of unicode characters to copy. If equals + * null, then null-termination of src is taken + * + * @return copied UNISTR2 destination + **/ +UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) +{ + size_t len; + + if (!src) return NULL; + len = strlen_w(src); + + /* allocate UNISTR2 destination if not given */ + if (!dst) { + dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2)); + if (!dst) return NULL; + } + if (!dst->buffer) { + dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1)); + if (!dst->buffer) return NULL; + } + + /* set UNISTR2 parameters */ + dst->uni_max_len = len + 1; + dst->undoc = 0; + dst->uni_str_len = len; + + /* copy the actual unicode string */ + strncpy_w(dst->buffer, src, dst->uni_max_len); + + return dst; +}; + -- cgit From a834a73e341059be154426390304a42e4a011f72 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 25 Sep 2002 15:19:00 +0000 Subject: sync'ing up for 3.0alpha20 release (This used to be commit 65e7b5273bb58802bf0c389b77f7fcae0a1f6139) --- source3/lib/util_unistr.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index ba02819bdc..eb47252413 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -218,6 +218,16 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); } +/******************************************************************* +give a static string for displaying a UNISTR2 +********************************************************************/ +const char *unistr2_static(const UNISTR2 *str) +{ + static pstring ret; + unistr2_to_ascii(ret, str, sizeof(ret)); + return ret; +} + /******************************************************************* duplicate a UNISTR2 string into a null terminated char* -- cgit From 698fe3f07ab059fc2b0a3bac47e36748704fde6d Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 8 Nov 2002 18:45:38 +0000 Subject: patches from Urban (This used to be commit 850b185a6e33fa924fa59cdd6316c9160ba65270) --- source3/lib/util_unistr.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index eb47252413..8e41581f75 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -335,6 +335,8 @@ smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) if (c == *s) return (smb_ucs2_t *)s; s++; } + if (c == *s) return (smb_ucs2_t *)s; + return NULL; } -- cgit From d4b82b17fc2845acaff51259282960e40b457978 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 29 Jan 2003 06:51:35 +0000 Subject: Return 0 instead of crashing when a NULL source string is passed to rpcstr_pull() - merge from HEAD (tpot). Jeremy. (This used to be commit e781388c59d694058576529fb43f861c48011091) --- source3/lib/util_unistr.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8e41581f75..5c9b4c783b 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -169,6 +169,7 @@ char *skip_unibuf(char *src, size_t len) */ int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags) { + if (!src) return 0; if(dest_len==-1) dest_len=MAXUNI-3; return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN); } -- cgit From 266ec4aac04cb8666234f18baa38ff6387f40cb3 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 24 Feb 2003 03:09:08 +0000 Subject: Merge doxygen, signed/unsigned, const and other small fixes from HEAD to 3.0. Andrew Bartlett (This used to be commit 9ef0d40c3f8aef52ab321dc065264c42065bc876) --- source3/lib/util_unistr.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5c9b4c783b..522ab7eb40 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -95,9 +95,14 @@ static int check_dos_char(smb_ucs2_t c) return (c == c2); } -/******************************************************************* -load the valid character map table -********************************************************************/ +/** + * Load the valid character map table from valid.dat or + * create from the configured codepage. + * + * This function is called whenever the configuration is reloaded. + * However, the valid character table is not changed if it's loaded + * from a file, because we can't unmap files. + **/ void init_valid_table(void) { static int initialised; @@ -114,6 +119,7 @@ void init_valid_table(void) return; } + /* Otherwise, using a dynamically loaded one. */ if (valid_table) free(valid_table); DEBUG(2,("creating default valid table\n")); @@ -128,6 +134,7 @@ void init_valid_table(void) } + /******************************************************************* Write a string in (little-endian) unicode format. src is in the current DOS codepage. len is the length in bytes of the -- cgit From 03ae31248ee28b60600887611783efc78b1d3d59 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Thu, 27 Feb 2003 05:20:23 +0000 Subject: Merge 1.100 patch from HEAD: init_valid_table: Fix a memory leak that would lose the dynamically-created valid table every time the configuration was reloaded. (This used to be commit 3ff4845bc43736bfa091419c80b9a0ebdc9d156e) --- source3/lib/util_unistr.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 522ab7eb40..c666155f9f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -105,27 +105,34 @@ static int check_dos_char(smb_ucs2_t c) **/ void init_valid_table(void) { - static int initialised; static int mapped_file; int i; const char *allowed = ".!#$%&'()_-@^`~"; + uint8 *valid_file; - if (initialised && mapped_file) return; - initialised = 1; + if (mapped_file) { + /* Can't unmap files, so stick with what we have */ + return; + } - valid_table = map_file(lib_path("valid.dat"), 0x10000); - if (valid_table) { + valid_file = map_file(lib_path("valid.dat"), 0x10000); + if (valid_file) { + valid_table = valid_file; mapped_file = 1; return; } - /* Otherwise, using a dynamically loaded one. */ + /* Otherwise, we're using a dynamically created valid_table. + * It might need to be regenerated if the code page changed. + * We know that we're not using a mapped file, so we can + * free() the old one. */ if (valid_table) free(valid_table); DEBUG(2,("creating default valid table\n")); valid_table = malloc(0x10000); - for (i=0;i<128;i++) valid_table[i] = isalnum(i) || - strchr(allowed,i); + for (i=0;i<128;i++) + valid_table[i] = isalnum(i) || strchr(allowed,i); + for (;i<0x10000;i++) { smb_ucs2_t c; SSVAL(&c, 0, i); -- cgit From 82272f3bafde3032dd2ec4ae3546a596c9b807b8 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Fri, 4 Apr 2003 03:35:06 +0000 Subject: check_dos_char: Export this function so that it can be tested in isolation by a test case. (This used to be commit 52520fda6a168132239e2adae963a766ec653348) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c666155f9f..3f319eed81 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -83,7 +83,7 @@ void load_case_tables(void) see if a ucs2 character can be mapped correctly to a dos character and mapped back to the same character in ucs2 */ -static int check_dos_char(smb_ucs2_t c) +int check_dos_char(smb_ucs2_t c) { char buf[10]; smb_ucs2_t c2 = 0; -- cgit From 75438592cd8b427cbee4a55ee4bd2c8d15f6637d Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Fri, 4 Apr 2003 08:16:14 +0000 Subject: check_dos_char: Change this to use a lazily-initialized lookup table indicating which characters are valid dos characters. This function was previously quite slow because it did two unicode conversions on every call. (This used to be commit e4ec19e03f95fb7d5b170c7e0ab5837ebc7dcd97) --- source3/lib/util_unistr.c | 52 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 3f319eed81..08bb03986f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -31,10 +31,21 @@ static smb_ucs2_t *upcase_table; static smb_ucs2_t *lowcase_table; static uint8 *valid_table; +/** + * This table says which Unicode characters are valid dos + * characters. + * + * Each value is just a single bit. + **/ +static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */ -/******************************************************************* -load the case handling tables -********************************************************************/ + +/** + * Load or generate the case handling tables. + * + * The case tables are defined in UCS2 and don't depend on any + * configured parameters, so they never need to be reloaded. + **/ void load_case_tables(void) { static int initialised; @@ -84,6 +95,16 @@ void load_case_tables(void) and mapped back to the same character in ucs2 */ int check_dos_char(smb_ucs2_t c) +{ + lazy_initialize_conv(); + + /* Find the right byte, and right bit within the byte; return + * 1 or 0 */ + return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0; +} + + +static int check_dos_char_slowly(smb_ucs2_t c) { char buf[10]; smb_ucs2_t c2 = 0; @@ -95,6 +116,31 @@ int check_dos_char(smb_ucs2_t c) return (c == c2); } + +/** + * Fill out doschar table the hard way, by examining each character + **/ +void init_doschar_table(void) +{ + int i, j, byteval; + + /* For each byte of packed table */ + + for (i = 0; i <= 0xffff; i += 8) { + byteval = 0; + for (j = 0; j <= 7; j++) { + smb_ucs2_t c; + + c = i + j; + + if (check_dos_char_slowly(c)) + byteval |= 1 << j; + } + doschar_table[i/8] = byteval; + } +} + + /** * Load the valid character map table from valid.dat or * create from the configured codepage. -- cgit From 66468d23158694383f3759464ffa7ade1c1d6d6c Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Wed, 23 Apr 2003 14:07:33 +0000 Subject: Patch by Metze to ensure that we always at least initialize our output string for rpc_pull_string. If we had a NULL or zero-length string, we would use uninitialised data in the result string. Andrew Bartlett (This used to be commit df10aee451b431a8a056a949a98393da256185da) --- source3/lib/util_unistr.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 08bb03986f..5df0828295 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -229,7 +229,10 @@ char *skip_unibuf(char *src, size_t len) */ int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags) { - if (!src) return 0; + if (!src) { + dest[0] = 0; + return 0; + } if(dest_len==-1) dest_len=MAXUNI-3; return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN); } -- cgit From af4d65889420eb3bb71be67d619dbc0c62e21101 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 2 Jul 2003 20:01:51 +0000 Subject: Added fix for Japanese case names in statcache - these can change size on upper casing. Based on patch from monyo@home.monyo.com. Jeremy. (This used to be commit 72e382e99b92666acdaf50a040b14aa16d48b80d) --- source3/lib/util_unistr.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5df0828295..ae000fba02 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -391,8 +391,9 @@ size_t strnlen_w(const smb_ucs2_t *src, size_t max) } /******************************************************************* -wide strchr() + Wide strchr(). ********************************************************************/ + smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { while (*s != 0) { @@ -409,6 +410,10 @@ smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c) return strchr_w(s, UCS2_CHAR(c)); } +/******************************************************************* + Wide strrchr(). +********************************************************************/ + smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { const smb_ucs2_t *p = s; @@ -422,8 +427,30 @@ smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) } /******************************************************************* -wide strstr() + Wide version of strrchr that returns after doing strrchr 'n' times. ********************************************************************/ + +smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n) +{ + const smb_ucs2_t *p = s; + int len = strlen_w(s); + if (len == 0 || !n) + return NULL; + p += (len - 1); + do { + if (c == *p) + n--; + + if (!n) + return (smb_ucs2_t *)p; + } while (p-- != s); + return NULL; +} + +/******************************************************************* + Wide strstr(). +********************************************************************/ + smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) { smb_ucs2_t *r; -- cgit From 224a9f35b8fa64a1a74829e0c00d3565a9e65017 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sun, 27 Jul 2003 02:42:22 +0000 Subject: When removing an 'unused' function, it helps to remove the 'unused' callers... Andrew Bartlett (This used to be commit 605a89493e971f653413b06eda4bbca75029072b) --- source3/lib/util_unistr.c | 76 ----------------------------------------------- 1 file changed, 76 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index ae000fba02..fd51f3c57d 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -759,82 +759,6 @@ smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) return NULL; } -/******************************************************************* -copy a string with max len -********************************************************************/ - -smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max) -{ - smb_ucs2_t *ucs2_src; - - if (!dest || !src) return NULL; - if (!(ucs2_src = acnv_uxu2(src))) - return NULL; - - strncpy_w(dest, ucs2_src, max); - SAFE_FREE(ucs2_src); - return dest; -} - -/******************************************************************* -convert and duplicate an ascii string -********************************************************************/ -smb_ucs2_t *strdup_wa(const char *src) -{ - return strndup_wa(src, 0); -} - -/* if len == 0 then duplicate the whole string */ -smb_ucs2_t *strndup_wa(const char *src, size_t len) -{ - smb_ucs2_t *dest, *s; - - s = acnv_dosu2(src); - if (!len) len = strlen_w(s); - dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t)); - if (!dest) { - DEBUG(0,("strdup_w: out of memory!\n")); - SAFE_FREE(s); - return NULL; - } - - memcpy(dest, src, len * sizeof(smb_ucs2_t)); - dest[len] = 0; - - SAFE_FREE(s); - return dest; -} - -/******************************************************************* -append a string of len bytes and add a terminator -********************************************************************/ - -smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max) -{ - smb_ucs2_t *ucs2_src; - - if (!dest || !src) return NULL; - if (!(ucs2_src = acnv_uxu2(src))) - return NULL; - - strncat_w(dest, ucs2_src, max); - SAFE_FREE(ucs2_src); - return dest; -} - -smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src) -{ - smb_ucs2_t *ucs2_src; - - if (!dest || !src) return NULL; - if (!(ucs2_src = acnv_uxu2(src))) - return NULL; - - strcat_w(dest, ucs2_src); - SAFE_FREE(ucs2_src); - return dest; -} - BOOL trim_string_wa(smb_ucs2_t *s, const char *front, const char *back) { -- cgit From 88c95aa7351c6037cb9f92a2c67d96d6fef91377 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 22 Sep 2003 17:53:59 +0000 Subject: fix some warnings found by the Sun C compiler (This used to be commit e1fac713e25692a5790c3261ba323732930f5249) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index fd51f3c57d..e7c200218e 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -841,5 +841,5 @@ UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) strncpy_w(dst->buffer, src, dst->uni_max_len); return dst; -}; +} -- cgit From d3b9384308e4b5130c9455b853edc4702d7af303 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 25 Sep 2003 21:26:16 +0000 Subject: Fix for #480. Change the interface for init_unistr2 to not take a length but a flags field. We were assuming that 2*strlen(mb_string) == length of ucs2-le string. This is not the case. Count it after conversion. Jeremy. (This used to be commit f82c273a42f930c7152cfab84394781744815e0e) --- source3/lib/util_unistr.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index e7c200218e..e90a824395 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -819,22 +819,25 @@ UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) { size_t len; - if (!src) return NULL; + if (!src) + return NULL; len = strlen_w(src); /* allocate UNISTR2 destination if not given */ if (!dst) { dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2)); - if (!dst) return NULL; + if (!dst) + return NULL; } if (!dst->buffer) { dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1)); - if (!dst->buffer) return NULL; + if (!dst->buffer) + return NULL; } /* set UNISTR2 parameters */ dst->uni_max_len = len + 1; - dst->undoc = 0; + dst->offset = 0; dst->uni_str_len = len; /* copy the actual unicode string */ @@ -842,4 +845,3 @@ UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) return dst; } - -- cgit From e3f5b542707e2328030b9d5eff0836a904eccde5 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 11 Mar 2004 22:48:24 +0000 Subject: Restore the contract on all convert_stringXX() interfaces. Add a "allow_bad_conv" boolean parameter that allows broken iconv conversions to work. Gets rid of the nasty errno checks in mangle_hash2 and check_path_syntax and allows correct return code checking. Jeremy. (This used to be commit 7b96765c23637613f079d37566d95d5edd511f05) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index e90a824395..005f10a4c0 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -109,9 +109,9 @@ static int check_dos_char_slowly(smb_ucs2_t c) char buf[10]; smb_ucs2_t c2 = 0; int len1, len2; - len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf)); + len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf),False); if (len1 == 0) return 0; - len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2); + len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2,False); if (len2 != 2) return 0; return (c == c2); } -- cgit From e0da56a84808c522bc7324b5d636f1cbd317a2c5 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 7 May 2004 18:37:47 +0000 Subject: r570: Remove lots of globals to handle case issues - move them to connection struct entries (as they should have been from the start). Jerry, once you've cut over to 3.0.4 release branch I'll add this to 3.0 also. - Jerry cut over :-). Jeremy. (This used to be commit 578a508509d21226ad3332fc54c3ab54cd8ae452) --- source3/lib/util_unistr.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 005f10a4c0..bfb5288826 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -506,13 +506,13 @@ BOOL strupper_w(smb_ucs2_t *s) /******************************************************************* convert a string to "normal" form ********************************************************************/ -void strnorm_w(smb_ucs2_t *s) + +void strnorm_w(smb_ucs2_t *s, int case_default) { - extern int case_default; - if (case_default == CASE_UPPER) - strupper_w(s); - else - strlower_w(s); + if (case_default == CASE_UPPER) + strupper_w(s); + else + strlower_w(s); } int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) -- cgit From d7a1c4f79c7c9d7038096a34626ba7236e856929 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 2 Dec 2004 00:55:47 +0000 Subject: r4032: Fix bug #2110 - ensure we convert to ucs2 correctly. Jeremy. (This used to be commit a1e5a2a6ab1abc9add7a606e2e3f2d6c88dcf96c) --- source3/lib/util_unistr.c | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index bfb5288826..997dde91c2 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -708,16 +708,6 @@ BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, The char* arguments must NOT be multibyte - to be completely sure of this only pass string constants */ - -void pstrcpy_wa(smb_ucs2_t *dest, const char *src) -{ - int i; - for (i=0;i Date: Tue, 7 Dec 2004 18:25:53 +0000 Subject: r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation functions so we can funnel through some well known functions. Should help greatly with malloc checking. HEAD patch to follow. Jeremy. (This used to be commit 620f2e608f70ba92f032720c031283d295c5c06a) --- source3/lib/util_unistr.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 997dde91c2..bb9d69b164 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -61,7 +61,7 @@ void load_case_tables(void) not available */ if (!upcase_table) { DEBUG(1,("creating lame upcase table\n")); - upcase_table = malloc(0x20000); + upcase_table = SMB_MALLOC(0x20000); for (i=0;i<0x10000;i++) { smb_ucs2_t v; SSVAL(&v, 0, i); @@ -76,7 +76,7 @@ void load_case_tables(void) if (!lowcase_table) { DEBUG(1,("creating lame lowcase table\n")); - lowcase_table = malloc(0x20000); + lowcase_table = SMB_MALLOC(0x20000); for (i=0;i<0x10000;i++) { smb_ucs2_t v; SSVAL(&v, 0, i); @@ -175,7 +175,7 @@ void init_valid_table(void) if (valid_table) free(valid_table); DEBUG(2,("creating default valid table\n")); - valid_table = malloc(0x10000); + valid_table = SMB_MALLOC(0x10000); for (i=0;i<128;i++) valid_table[i] = isalnum(i) || strchr(allowed,i); @@ -302,7 +302,7 @@ char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str) char *s; int maxlen = (str->uni_str_len+1)*4; if (!str->buffer) return NULL; - s = (char *)talloc(ctx, maxlen); /* convervative */ + s = (char *)TALLOC(ctx, maxlen); /* convervative */ if (!s) return NULL; pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); @@ -586,7 +586,7 @@ smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len) smb_ucs2_t *dest; if (!len) len = strlen_w(src); - dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t)); + dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1); if (!dest) { DEBUG(0,("strdup_w: out of memory!\n")); return NULL; @@ -815,12 +815,12 @@ UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) /* allocate UNISTR2 destination if not given */ if (!dst) { - dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2)); + dst = TALLOC_P(ctx, UNISTR2); if (!dst) return NULL; } if (!dst->buffer) { - dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1)); + dst->buffer = TALLOC_ARRAY(ctx, uint16, len + 1); if (!dst->buffer) return NULL; } -- cgit From 732f09990fda978fe1cbef786d6b2d15897e6f97 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Thu, 17 Feb 2005 15:17:16 +0000 Subject: r5431: couple of cimpile fixes from Jason Mader -- BUGS 2341 & 2342 (This used to be commit 0edcfc7fa20fd8e3273b29c8f1a97cb7c7179fb6) --- source3/lib/util_unistr.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index bb9d69b164..55a21ebcbb 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -454,16 +454,20 @@ smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n) smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) { smb_ucs2_t *r; - size_t slen, inslen; + size_t inslen; + + if (!s || !*s || !ins || !*ins) + return NULL; - if (!s || !*s || !ins || !*ins) return NULL; - slen = strlen_w(s); inslen = strlen_w(ins); r = (smb_ucs2_t *)s; + while ((r = strchr_w(r, *ins))) { - if (strncmp_w(r, ins, inslen) == 0) return r; + if (strncmp_w(r, ins, inslen) == 0) + return r; r++; } + return NULL; } @@ -736,16 +740,20 @@ smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) { smb_ucs2_t *r; - size_t slen, inslen; + size_t inslen; + + if (!s || !*s || !ins || !*ins) + return NULL; - if (!s || !*s || !ins || !*ins) return NULL; - slen = strlen_w(s); inslen = strlen(ins); r = (smb_ucs2_t *)s; + while ((r = strchr_w(r, UCS2_CHAR(*ins)))) { - if (strncmp_wa(r, ins, inslen) == 0) return r; + if (strncmp_wa(r, ins, inslen) == 0) + return r; r++; } + return NULL; } -- cgit From 5d1cb8e79edea9e8581d3c2c9dd297310cd9a98c Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 23 Mar 2005 23:26:33 +0000 Subject: r6014: rather large change set.... pulling back all recent rpc changes from trunk into 3.0. I've tested a compile and so don't think I've missed any files. But if so, just mail me and I'll clean backup in a couple of hours. Changes include \winreg, \eventlog, \svcctl, and general parse_misc.c updates. I am planning on bracketing the event code with an #ifdef ENABLE_EVENTLOG until I finish merging Marcin's changes (very soon). (This used to be commit 4e0ac63c36527cd8c52ef720cae17e84f67e7221) --- source3/lib/util_unistr.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 55a21ebcbb..04985c6ab6 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -282,6 +282,19 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); } +/******************************************************************* + Convert a (little-endian) UNISTR3 structure to an ASCII string +********************************************************************/ +void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen) +{ + if (str == NULL) { + *dest='\0'; + return; + } + pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2, + STR_NOALIGN); +} + /******************************************************************* give a static string for displaying a UNISTR2 ********************************************************************/ @@ -310,18 +323,6 @@ char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str) } -/******************************************************************* -Return a number stored in a buffer -********************************************************************/ - -uint32 buffer2_to_uint32(BUFFER2 *str) -{ - if (str->buf_len == 4) - return IVAL(str->buffer, 0); - else - return 0; -} - /******************************************************************* Convert a wchar to upper case. ********************************************************************/ -- cgit From 9840db418bad5a39edc4a32a1786f5e2d2c9dff8 Mon Sep 17 00:00:00 2001 From: Derrell Lipman Date: Thu, 31 Mar 2005 05:06:04 +0000 Subject: r6149: Fixes bugs #2498 and 2484. 1. using smbc_getxattr() et al, one may now request all access control entities in the ACL without getting all other NT attributes. 2. added the ability to exclude specified attributes from the result set provided by smbc_getxattr() et al, when requesting all attributes, all NT attributes, or all DOS attributes. 3. eliminated all compiler warnings, including when --enable-developer compiler flags are in use. removed -Wcast-qual flag from list, as that is specifically to force warnings in the case of casting away qualifiers. Note: In the process of eliminating compiler warnings, a few nasties were discovered. In the file libads/sasl.c, PRIVATE kerberos interfaces are being used; and in libsmb/clikrb5.c, both PRIAVE and DEPRECATED kerberos interfaces are being used. Someone who knows kerberos should look at these and determine if there is an alternate method of accomplishing the task. (This used to be commit 994694f7f26da5099f071e1381271a70407f33bb) --- source3/lib/util_unistr.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 04985c6ab6..0b4552e1f5 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -398,10 +398,10 @@ size_t strnlen_w(const smb_ucs2_t *src, size_t max) smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { while (*s != 0) { - if (c == *s) return (smb_ucs2_t *)s; + if (c == *s) return CONST_DISCARD(smb_ucs2_t *, s); s++; } - if (c == *s) return (smb_ucs2_t *)s; + if (c == *s) return CONST_DISCARD(smb_ucs2_t *, s); return NULL; } @@ -422,7 +422,7 @@ smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) if (len == 0) return NULL; p += (len - 1); do { - if (c == *p) return (smb_ucs2_t *)p; + if (c == *p) return CONST_DISCARD(smb_ucs2_t *, p); } while (p-- != s); return NULL; } @@ -443,7 +443,7 @@ smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n) n--; if (!n) - return (smb_ucs2_t *)p; + return CONST_DISCARD(smb_ucs2_t *, p); } while (p-- != s); return NULL; } @@ -461,7 +461,7 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) return NULL; inslen = strlen_w(ins); - r = (smb_ucs2_t *)s; + r = CONST_DISCARD(smb_ucs2_t *, s); while ((r = strchr_w(r, *ins))) { if (strncmp_w(r, ins, inslen) == 0) @@ -732,7 +732,7 @@ smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) int i; for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++) ; - if (p[i]) return (smb_ucs2_t *)s; + if (p[i]) return CONST_DISCARD(smb_ucs2_t *, s); s++; } return NULL; @@ -747,7 +747,7 @@ smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) return NULL; inslen = strlen(ins); - r = (smb_ucs2_t *)s; + r = CONST_DISCARD(smb_ucs2_t *, s); while ((r = strchr_w(r, UCS2_CHAR(*ins)))) { if (strncmp_wa(r, ins, inslen) == 0) -- cgit From be0c01da4ce805440638ec7b0b06c4279bc59d6a Mon Sep 17 00:00:00 2001 From: Herb Lewis Date: Wed, 6 Apr 2005 23:50:48 +0000 Subject: r6230: don't know how this ever worked! the compiler complained we were comparing an integer to a pointer and it was right. (This used to be commit b6117dd72de1f2a8e158d1a5f2b2991ef93deb72) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 0b4552e1f5..7cbae30ced 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -113,7 +113,7 @@ static int check_dos_char_slowly(smb_ucs2_t c) if (len1 == 0) return 0; len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2,False); if (len2 != 2) return 0; - return (c == c2); + return (c == *c2); } -- cgit From dc30062d71ea4b53862c9026dc9f5385c899f449 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Thu, 7 Apr 2005 03:24:02 +0000 Subject: r6231: Herb, I think this checkin was a mistake. Are you sure this is what you meant to change? Fix build breakage.... (This used to be commit b3643b732ba35af6e329dbda82bb05da81e5da64) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 7cbae30ced..0b4552e1f5 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -113,7 +113,7 @@ static int check_dos_char_slowly(smb_ucs2_t c) if (len1 == 0) return 0; len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2,False); if (len2 != 2) return 0; - return (c == *c2); + return (c == c2); } -- cgit From 0419e24287d09a71735d94ea019a7f17842201d3 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 25 May 2005 00:51:39 +0000 Subject: r6965: Remove some dead code from util_unistr.c. Start of fix for #2735 - we are not mangling some names we should. More fixes to follow. Jeremy. (This used to be commit ac0fa973774c36b72863aea369e9d243cf7420fa) --- source3/lib/util_unistr.c | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 0b4552e1f5..89639423bb 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -254,22 +254,6 @@ int rpcstr_push(void* dest, const char *src, int dest_len, int flags) return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN); } -/******************************************************************* - Return a DOS codepage version of a little-endian unicode string. - len is the filename length (ignoring any terminating zero) in uin16 - units. Always null terminates. - Hack alert: uses fixed buffer(s). -********************************************************************/ -char *dos_unistrn2(const uint16 *src, int len) -{ - static char lbufs[8][MAXUNI]; - static int nexti; - char *lbuf = lbufs[nexti]; - nexti = (nexti+1)%8; - pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN); - return lbuf; -} - /******************************************************************* Convert a (little-endian) UNISTR2 structure to an ASCII string ********************************************************************/ -- cgit From f24d88cf9da46680d52b42b92bd484e7b09ce99b Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Tue, 31 May 2005 13:46:45 +0000 Subject: r7139: trying to reduce the number of diffs between trunk and 3.0; changing version to 3.0.20pre1 (This used to be commit 9727d05241574042dd3aa8844ae5c701d22e2da1) --- source3/lib/util_unistr.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 89639423bb..678eb22d2c 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -382,10 +382,10 @@ size_t strnlen_w(const smb_ucs2_t *src, size_t max) smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { while (*s != 0) { - if (c == *s) return CONST_DISCARD(smb_ucs2_t *, s); + if (c == *s) return (smb_ucs2_t *)s; s++; } - if (c == *s) return CONST_DISCARD(smb_ucs2_t *, s); + if (c == *s) return (smb_ucs2_t *)s; return NULL; } @@ -406,7 +406,7 @@ smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) if (len == 0) return NULL; p += (len - 1); do { - if (c == *p) return CONST_DISCARD(smb_ucs2_t *, p); + if (c == *p) return (smb_ucs2_t *)p; } while (p-- != s); return NULL; } @@ -427,7 +427,7 @@ smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n) n--; if (!n) - return CONST_DISCARD(smb_ucs2_t *, p); + return (smb_ucs2_t *)p; } while (p-- != s); return NULL; } @@ -445,7 +445,7 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) return NULL; inslen = strlen_w(ins); - r = CONST_DISCARD(smb_ucs2_t *, s); + r = (smb_ucs2_t *)s; while ((r = strchr_w(r, *ins))) { if (strncmp_w(r, ins, inslen) == 0) @@ -716,7 +716,7 @@ smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) int i; for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++) ; - if (p[i]) return CONST_DISCARD(smb_ucs2_t *, s); + if (p[i]) return (smb_ucs2_t *)s; s++; } return NULL; @@ -731,7 +731,7 @@ smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) return NULL; inslen = strlen(ins); - r = CONST_DISCARD(smb_ucs2_t *, s); + r = (smb_ucs2_t *)s; while ((r = strchr_w(r, UCS2_CHAR(*ins)))) { if (strncmp_wa(r, ins, inslen) == 0) -- cgit From 7fb9e6a860b47eaf82589b0ebbcd34dbd6ed5df4 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 26 Jul 2005 00:10:45 +0000 Subject: r8762: Attempt to fix the winbindd crashing bug on Solaris (can't remember the bugid). Make the functions in lib/util_unistr.c odd-alignment safe using some evil macros. Needs testing on bigendian. Jeremy. (This used to be commit 871c647bc3ee465e613cba260d9cd7e66e47b68a) --- source3/lib/util_unistr.c | 366 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 253 insertions(+), 113 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 678eb22d2c..8ae86a7c61 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -3,6 +3,7 @@ Samba utility functions Copyright (C) Andrew Tridgell 1992-2001 Copyright (C) Simo Sorce 2001 + Copyright (C) Jeremy Allison 2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -46,12 +47,15 @@ static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */ * The case tables are defined in UCS2 and don't depend on any * configured parameters, so they never need to be reloaded. **/ + void load_case_tables(void) { static int initialised; int i; - if (initialised) return; + if (initialised) { + return; + } initialised = 1; upcase_table = map_file(lib_path("upcase.dat"), 0x20000); @@ -94,6 +98,7 @@ void load_case_tables(void) see if a ucs2 character can be mapped correctly to a dos character and mapped back to the same character in ucs2 */ + int check_dos_char(smb_ucs2_t c) { lazy_initialize_conv(); @@ -109,10 +114,15 @@ static int check_dos_char_slowly(smb_ucs2_t c) char buf[10]; smb_ucs2_t c2 = 0; int len1, len2; + len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf),False); - if (len1 == 0) return 0; + if (len1 == 0) { + return 0; + } len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2,False); - if (len2 != 2) return 0; + if (len2 != 2) { + return 0; + } return (c == c2); } @@ -120,6 +130,7 @@ static int check_dos_char_slowly(smb_ucs2_t c) /** * Fill out doschar table the hard way, by examining each character **/ + void init_doschar_table(void) { int i, j, byteval; @@ -133,8 +144,9 @@ void init_doschar_table(void) c = i + j; - if (check_dos_char_slowly(c)) + if (check_dos_char_slowly(c)) { byteval |= 1 << j; + } } doschar_table[i/8] = byteval; } @@ -149,6 +161,7 @@ void init_doschar_table(void) * However, the valid character table is not changed if it's loaded * from a file, because we can't unmap files. **/ + void init_valid_table(void) { static int mapped_file; @@ -176,8 +189,9 @@ void init_valid_table(void) DEBUG(2,("creating default valid table\n")); valid_table = SMB_MALLOC(0x10000); - for (i=0;i<128;i++) + for (i=0;i<128;i++) { valid_table[i] = isalnum(i) || strchr(allowed,i); + } for (;i<0x10000;i++) { smb_ucs2_t c; @@ -186,8 +200,6 @@ void init_valid_table(void) } } - - /******************************************************************* Write a string in (little-endian) unicode format. src is in the current DOS codepage. len is the length in bytes of the @@ -213,27 +225,32 @@ size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminat char *skip_unibuf(char *src, size_t len) { - char *srcend = src + len; + char *srcend = src + len; - while (src < srcend && SVAL(src,0)) - src += 2; + while (src < srcend && SVAL(src,0)) { + src += 2; + } - if(!SVAL(src,0)) - src += 2; + if(!SVAL(src,0)) { + src += 2; + } - return src; + return src; } /* Copy a string from little-endian or big-endian unicode source (depending * on flags) to internal samba format destination */ + int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags) { if (!src) { dest[0] = 0; return 0; } - if(dest_len==-1) dest_len=MAXUNI-3; + if(dest_len==-1) { + dest_len=MAXUNI-3; + } return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN); } @@ -249,14 +266,16 @@ int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) /* Converts a string from internal samba format to unicode */ + int rpcstr_push(void* dest, const char *src, int dest_len, int flags) { return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN); } /******************************************************************* - Convert a (little-endian) UNISTR2 structure to an ASCII string + Convert a (little-endian) UNISTR2 structure to an ASCII string. ********************************************************************/ + void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) { if (str == NULL) { @@ -267,8 +286,9 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) } /******************************************************************* - Convert a (little-endian) UNISTR3 structure to an ASCII string + Convert a (little-endian) UNISTR3 structure to an ASCII string. ********************************************************************/ + void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen) { if (str == NULL) { @@ -280,8 +300,9 @@ void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen) } /******************************************************************* -give a static string for displaying a UNISTR2 + Give a static string for displaying a UNISTR2. ********************************************************************/ + const char *unistr2_static(const UNISTR2 *str) { static pstring ret; @@ -289,24 +310,26 @@ const char *unistr2_static(const UNISTR2 *str) return ret; } - /******************************************************************* - duplicate a UNISTR2 string into a null terminated char* - using a talloc context + Duplicate a UNISTR2 string into a null terminated char* + using a talloc context. ********************************************************************/ + char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str) { char *s; int maxlen = (str->uni_str_len+1)*4; - if (!str->buffer) return NULL; + if (!str->buffer) { + return NULL; + } s = (char *)TALLOC(ctx, maxlen); /* convervative */ - if (!s) return NULL; - pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, - STR_NOALIGN); + if (!s) { + return NULL; + } + pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); return s; } - /******************************************************************* Convert a wchar to upper case. ********************************************************************/ @@ -323,29 +346,30 @@ smb_ucs2_t toupper_w(smb_ucs2_t val) smb_ucs2_t tolower_w( smb_ucs2_t val ) { return lowcase_table[SVAL(&val,0)]; - } /******************************************************************* -determine if a character is lowercase + Determine if a character is lowercase. ********************************************************************/ + BOOL islower_w(smb_ucs2_t c) { return upcase_table[SVAL(&c,0)] != c; } /******************************************************************* -determine if a character is uppercase + Determine if a character is uppercase. ********************************************************************/ + BOOL isupper_w(smb_ucs2_t c) { return lowcase_table[SVAL(&c,0)] != c; } - /******************************************************************* -determine if a character is valid in a 8.3 name + Determine if a character is valid in a 8.3 name. ********************************************************************/ + BOOL isvalid83_w(smb_ucs2_t c) { return valid_table[SVAL(&c,0)] != 0; @@ -354,11 +378,15 @@ BOOL isvalid83_w(smb_ucs2_t c) /******************************************************************* Count the number of characters in a smb_ucs2_t string. ********************************************************************/ + size_t strlen_w(const smb_ucs2_t *src) { size_t len; + smb_ucs2_t c; - for(len = 0; *src++; len++) ; + for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) { + ; + } return len; } @@ -366,11 +394,15 @@ size_t strlen_w(const smb_ucs2_t *src) /******************************************************************* Count up to max number of characters in a smb_ucs2_t string. ********************************************************************/ + size_t strnlen_w(const smb_ucs2_t *src, size_t max) { size_t len; + smb_ucs2_t c; - for(len = 0; *src++ && (len < max); len++) ; + for(len = 0; *(COPY_UCS2_CHAR(&c,src)) && (len < max); src++, len++) { + ; + } return len; } @@ -381,11 +413,16 @@ size_t strnlen_w(const smb_ucs2_t *src, size_t max) smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { - while (*s != 0) { - if (c == *s) return (smb_ucs2_t *)s; + smb_ucs2_t cp; + while (*(COPY_UCS2_CHAR(&cp,s))) { + if (c == cp) { + return (smb_ucs2_t *)s; + } s++; } - if (c == *s) return (smb_ucs2_t *)s; + if (c == cp) { + return (smb_ucs2_t *)s; + } return NULL; } @@ -401,12 +438,18 @@ smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c) smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) { + smb_ucs2_t cp; const smb_ucs2_t *p = s; int len = strlen_w(s); - if (len == 0) return NULL; + + if (len == 0) { + return NULL; + } p += (len - 1); do { - if (c == *p) return (smb_ucs2_t *)p; + if (c == *(COPY_UCS2_CHAR(&cp,p))) { + return (smb_ucs2_t *)p; + } } while (p-- != s); return NULL; } @@ -417,17 +460,22 @@ smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c) smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n) { + smb_ucs2_t cp; const smb_ucs2_t *p = s; int len = strlen_w(s); - if (len == 0 || !n) + + if (len == 0 || !n) { return NULL; + } p += (len - 1); do { - if (c == *p) + if (c == *(COPY_UCS2_CHAR(&cp,p))) { n--; + } - if (!n) + if (!n) { return (smb_ucs2_t *)p; + } } while (p-- != s); return NULL; } @@ -441,15 +489,17 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) smb_ucs2_t *r; size_t inslen; - if (!s || !*s || !ins || !*ins) + if (!s || !*s || !ins || !*ins) { return NULL; + } inslen = strlen_w(ins); r = (smb_ucs2_t *)s; while ((r = strchr_w(r, *ins))) { - if (strncmp_w(r, ins, inslen) == 0) + if (strncmp_w(r, ins, inslen) == 0) { return r; + } r++; } @@ -460,13 +510,16 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) Convert a string to lower case. return True if any char is converted ********************************************************************/ + BOOL strlower_w(smb_ucs2_t *s) { + smb_ucs2_t cp; BOOL ret = False; - while (*s) { - smb_ucs2_t v = tolower_w(*s); - if (v != *s) { - *s = v; + + while (*(COPY_UCS2_CHAR(&cp,s))) { + smb_ucs2_t v = tolower_w(cp); + if (v != cp) { + COPY_UCS2_CHAR(s,&v); ret = True; } s++; @@ -478,13 +531,15 @@ BOOL strlower_w(smb_ucs2_t *s) Convert a string to upper case. return True if any char is converted ********************************************************************/ + BOOL strupper_w(smb_ucs2_t *s) { + smb_ucs2_t cp; BOOL ret = False; - while (*s) { - smb_ucs2_t v = toupper_w(*s); - if (v != *s) { - *s = v; + while (*(COPY_UCS2_CHAR(&cp,s))) { + smb_ucs2_t v = toupper_w(cp); + if (v != cp) { + COPY_UCS2_CHAR(s,&v); ret = True; } s++; @@ -493,88 +548,127 @@ BOOL strupper_w(smb_ucs2_t *s) } /******************************************************************* - convert a string to "normal" form + Convert a string to "normal" form. ********************************************************************/ void strnorm_w(smb_ucs2_t *s, int case_default) { - if (case_default == CASE_UPPER) + if (case_default == CASE_UPPER) { strupper_w(s); - else + } else { strlower_w(s); + } } int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) { - while (*b && *a == *b) { a++; b++; } - return (*a - *b); - /* warning: if *a != *b and both are not 0 we retrun a random + smb_ucs2_t cpa, cpb; + + while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) { + a++; + b++; + } + return cpa - cpb; + /* warning: if *a != *b and both are not 0 we return a random greater or lesser than 0 number not realted to which string is longer */ } int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) { + smb_ucs2_t cpa, cpb; size_t n = 0; - while ((n < len) && *b && *a == *b) { a++; b++; n++;} - return (len - n)?(*a - *b):0; + + while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) { + a++; + b++; + n++; + } + return (len - n)?(cpa - cpb):0; } /******************************************************************* -case insensitive string comparison + Case insensitive string comparison. ********************************************************************/ + int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) { - while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; } - return (tolower_w(*a) - tolower_w(*b)); + smb_ucs2_t cpa, cpb; + + while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) { + a++; + b++; + } + return (tolower_w(cpa) - tolower_w(cpb)); } /******************************************************************* -case insensitive string comparison, lenght limited + Case insensitive string comparison, length limited. ********************************************************************/ + int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) { + smb_ucs2_t cpa, cpb; size_t n = 0; - while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; } - return (len - n)?(tolower_w(*a) - tolower_w(*b)):0; + + while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) { + a++; + b++; + n++; + } + return (len - n)?(tolower_w(cpa) - tolower_w(cpb)):0; } /******************************************************************* - compare 2 strings + Compare 2 strings. ********************************************************************/ + BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) { - if (s1 == s2) return(True); - if (!s1 || !s2) return(False); + if (s1 == s2) { + return(True); + } + if (!s1 || !s2) { + return(False); + } return(strcasecmp_w(s1,s2)==0); } /******************************************************************* - compare 2 strings up to and including the nth char. - ******************************************************************/ + Compare 2 strings up to and including the nth char. +******************************************************************/ + BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n) { - if (s1 == s2) return(True); - if (!s1 || !s2 || !n) return(False); + if (s1 == s2) { + return(True); + } + if (!s1 || !s2 || !n) { + return(False); + } - return(strncasecmp_w(s1,s2,n)==0); + return(strncasecmp_w(s1,s2,n)==0); } /******************************************************************* -duplicate string + Duplicate string. ********************************************************************/ + smb_ucs2_t *strdup_w(const smb_ucs2_t *src) { return strndup_w(src, 0); } /* if len == 0 then duplicate the whole string */ + smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len) { smb_ucs2_t *dest; - if (!len) len = strlen_w(src); + if (!len) { + len = strlen_w(src); + } dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1); if (!dest) { DEBUG(0,("strdup_w: out of memory!\n")); @@ -583,46 +677,53 @@ smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len) memcpy(dest, src, len * sizeof(smb_ucs2_t)); dest[len] = 0; - return dest; } /******************************************************************* -copy a string with max len + Copy a string with max len. ********************************************************************/ smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) { + smb_ucs2_t cp; size_t len; - if (!dest || !src) return NULL; + if (!dest || !src) { + return NULL; + } - for (len = 0; (src[len] != 0) && (len < max); len++) - dest[len] = src[len]; - while (len < max) - dest[len++] = 0; + for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) { + cp = *COPY_UCS2_CHAR(dest+len,src+len); + } + cp = 0; + while (len < max) { + cp = *COPY_UCS2_CHAR(dest+len,&cp); + } return dest; } - /******************************************************************* -append a string of len bytes and add a terminator + Append a string of len bytes and add a terminator. ********************************************************************/ smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) { size_t start; size_t len; - - if (!dest || !src) return NULL; + smb_ucs2_t z = 0; + + if (!dest || !src) { + return NULL; + } start = strlen_w(dest); len = strnlen_w(src, max); memcpy(&dest[start], src, len*sizeof(smb_ucs2_t)); - dest[start+len] = 0; - + z = *COPY_UCS2_CHAR(dest+start+len,&z); + return dest; } @@ -630,32 +731,39 @@ smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src) { size_t start; size_t len; + smb_ucs2_t z = 0; - if (!dest || !src) return NULL; + if (!dest || !src) { + return NULL; + } start = strlen_w(dest); len = strlen_w(src); memcpy(&dest[start], src, len*sizeof(smb_ucs2_t)); - dest[start+len] = 0; + z = *COPY_UCS2_CHAR(dest+start+len,&z); return dest; } /******************************************************************* -replace any occurence of oldc with newc in unicode string + Replace any occurence of oldc with newc in unicode string. ********************************************************************/ void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) { - for(;*s;s++) { - if(*s==oldc) *s=newc; + smb_ucs2_t cp; + + for(;*(COPY_UCS2_CHAR(&cp,s));s++) { + if(cp==oldc) { + COPY_UCS2_CHAR(s,&newc); + } } } /******************************************************************* -trim unicode string + Trim unicode string. ********************************************************************/ BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, @@ -664,7 +772,9 @@ BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, BOOL ret = False; size_t len, front_len, back_len; - if (!s || !*s) return False; + if (!s) { + return False; + } len = strlen_w(s); @@ -699,24 +809,39 @@ BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, int strcmp_wa(const smb_ucs2_t *a, const char *b) { - while (*b && *a == UCS2_CHAR(*b)) { a++; b++; } - return (*a - UCS2_CHAR(*b)); + smb_ucs2_t cp = 0; + + while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) { + a++; + b++; + } + return (cp - UCS2_CHAR(*b)); } int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len) { + smb_ucs2_t cp = 0; size_t n = 0; - while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;} - return (len - n)?(*a - UCS2_CHAR(*b)):0; + + while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) { + a++; + b++; + n++; + } + return (len - n)?(cp - UCS2_CHAR(*b)):0; } smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) { - while (*s != 0) { + smb_ucs2_t cp; + + while (*(COPY_UCS2_CHAR(&cp,s))) { int i; - for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++) + for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++) ; - if (p[i]) return (smb_ucs2_t *)s; + if (p[i]) { + return (smb_ucs2_t *)s; + } s++; } return NULL; @@ -727,8 +852,9 @@ smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) smb_ucs2_t *r; size_t inslen; - if (!s || !*s || !ins || !*ins) + if (!s || !ins) { return NULL; + } inslen = strlen(ins); r = (smb_ucs2_t *)s; @@ -747,37 +873,48 @@ BOOL trim_string_wa(smb_ucs2_t *s, const char *front, { wpstring f, b; - if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE); - else *f = 0; - if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE); - else *b = 0; + if (front) { + push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE); + } else { + *f = 0; + } + if (back) { + push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE); + } else { + *b = 0; + } return trim_string_w(s, f, b); } /******************************************************************* - returns the length in number of wide characters - ******************************************************************/ + Returns the length in number of wide characters. +******************************************************************/ + int unistrlen(uint16 *s) { int len; - if (!s) + if (!s) { return -1; + } - for (len=0; *s; s++,len++); + for (len=0; SVAL(s,0); s++,len++) { + ; + } return len; } /******************************************************************* - Strcpy for unicode strings. returns length (in num of wide chars) + Strcpy for unicode strings. Returns length (in num of wide chars). + Not odd align safe. ********************************************************************/ int unistrcpy(uint16 *dst, uint16 *src) { int num_wchars = 0; - while (*src) { + while (SVAL(src,0)) { *dst++ = *src++; num_wchars++; } @@ -798,12 +935,15 @@ int unistrcpy(uint16 *dst, uint16 *src) * * @return copied UNISTR2 destination **/ + UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) { size_t len; - if (!src) + if (!src) { return NULL; + } + len = strlen_w(src); /* allocate UNISTR2 destination if not given */ -- cgit From 754b05a3e825bc040312fc1726ceaf6905f7fa2d Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 27 Jul 2005 14:21:27 +0000 Subject: r8795: fix our perpetual motion maching in strncpy_w() (This used to be commit 3228e93ef2beda6f3551b2630e48db18bafd2e20) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8ae86a7c61..b31e01474a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -697,7 +697,7 @@ smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max) cp = *COPY_UCS2_CHAR(dest+len,src+len); } cp = 0; - while (len < max) { + for ( /*nothing*/ ; len < max; len++ ) { cp = *COPY_UCS2_CHAR(dest+len,&cp); } -- cgit From 6e98a3c9994883f911b7027e4bd8fb3238bd2bb3 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 2 Aug 2005 17:52:44 +0000 Subject: r8928: Fix mangle method = hash - bugid #2946. Incorrect strcmp_wa and strncmp_wa. Jeremy. (This used to be commit 604c1b239bca316f7afaf76b1a586c638f3a2562) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index b31e01474a..6b29a0d26a 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -815,7 +815,7 @@ int strcmp_wa(const smb_ucs2_t *a, const char *b) a++; b++; } - return (cp - UCS2_CHAR(*b)); + return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)); } int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len) @@ -828,7 +828,7 @@ int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len) b++; n++; } - return (len - n)?(cp - UCS2_CHAR(*b)):0; + return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0; } smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p) -- cgit From e4edd9527595faf480b80bf2b2df0392565ea51f Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 12 Aug 2005 20:12:31 +0000 Subject: r9271: Fix problems with german umlauts - strcmp_w was broken (needs to always re-call macro on termination). Fix all other cases where this was also occurring. Jeremy. (This used to be commit 816e2fbb39b544b7f62d5351f3a8e0af63717227) --- source3/lib/util_unistr.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 6b29a0d26a..8ed8c5c37f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -568,7 +568,7 @@ int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) a++; b++; } - return cpa - cpb; + return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))); /* warning: if *a != *b and both are not 0 we return a random greater or lesser than 0 number not realted to which string is longer */ @@ -584,7 +584,7 @@ int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) b++; n++; } - return (len - n)?(cpa - cpb):0; + return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0; } /******************************************************************* @@ -599,7 +599,7 @@ int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b) a++; b++; } - return (tolower_w(cpa) - tolower_w(cpb)); + return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))); } /******************************************************************* @@ -616,7 +616,7 @@ int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) b++; n++; } - return (len - n)?(tolower_w(cpa) - tolower_w(cpb)):0; + return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0; } /******************************************************************* -- cgit From 8c072021efba737539b46e993df0c21a6438a82a Mon Sep 17 00:00:00 2001 From: James Peach Date: Tue, 30 Aug 2005 06:41:32 +0000 Subject: r9780: Clean up a bunch of compiler warnings. (This used to be commit 623d2e69319ffead31a780a4d6156dae45f386d7) --- source3/lib/util_unistr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8ed8c5c37f..d80bb2ce52 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -213,8 +213,9 @@ void init_valid_table(void) size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) { - return push_ucs2(NULL, dst, src, len, - STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0)); + int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE + : STR_UNICODE|STR_NOALIGN; + return push_ucs2(NULL, dst, src, len, flags); } -- cgit From 32cf16f3cfaf52414c324c0a333ff29cb9a326aa Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 13 Nov 2005 11:38:17 +0000 Subject: r11706: Implement dsr_getdcname client code. It's handy: It not only gives you the IP address but also the fqdn of the remote dc and site info. Volker (This used to be commit 62d01ce7e6c14971084c208ab61f379cb172cb22) --- source3/lib/util_unistr.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index d80bb2ce52..b979745d36 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -265,6 +265,34 @@ int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) src->uni_str_len * 2, 0); } +/* Helper function to return a talloc'ed string. I have implemented it with a + * copy because I don't really know how pull_ucs2 and friends calculate the + * target size. If this turns out to be a major bottleneck someone with deeper + * multi-byte knowledge needs to revisit this. + * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would + * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c. + */ + +size_t rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, char **dest, + UNISTR2 *src) +{ + pstring tmp; + size_t result; + + result = pull_ucs2(NULL, tmp, src->buffer, sizeof(tmp), + src->uni_str_len * 2, 0); + if (result < 0) { + return result; + } + + *dest = talloc_strdup(mem_ctx, tmp); + if (*dest == NULL) { + return -1; + } + + return result; +} + /* Converts a string from internal samba format to unicode */ -- cgit From 5a4881bf396e691524329bcd6aa1ae4a7f4084ec Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 27 Dec 2005 20:52:36 +0000 Subject: r12522: Try and fix bug #2926 by removing setlocale(LC_ALL, "C") and replace calls to isupper/islower/toupper/tolower with ASCII equivalents (mapping into _w variants). Jeremy. (This used to be commit c2752347eb2deeb2798c580ec7fc751a847717e9) --- source3/lib/util_unistr.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index b979745d36..880416a549 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -51,6 +51,7 @@ static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */ void load_case_tables(void) { static int initialised; + char *old_locale = NULL, *saved_locale = NULL; int i; if (initialised) { @@ -61,6 +62,17 @@ void load_case_tables(void) upcase_table = map_file(lib_path("upcase.dat"), 0x20000); lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000); +#ifdef HAVE_SETLOCALE + /* Get the name of the current locale. */ + old_locale = setlocale(LC_ALL, NULL); + + /* Save it as it is in static storage. */ + saved_locale = SMB_STRDUP(old_locale); + + /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */ + setlocale(LC_ALL, "C"); +#endif + /* we would like Samba to limp along even if these tables are not available */ if (!upcase_table) { @@ -92,6 +104,12 @@ void load_case_tables(void) lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i); } } + +#ifdef HAVE_SETLOCALE + /* Restore the old locale. */ + setlocale (LC_ALL, saved_locale); + SAFE_FREE(saved_locale); +#endif } /* @@ -997,3 +1015,41 @@ UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src) return dst; } + +/************************************************************* + ascii only toupper - saves the need for smbd to be in C locale. +*************************************************************/ + +int toupper_ascii(int c) +{ + smb_ucs2_t uc = toupper_w(UCS2_CHAR(c)); + return UCS2_TO_CHAR(uc); +} + +/************************************************************* + ascii only tolower - saves the need for smbd to be in C locale. +*************************************************************/ + +int tolower_ascii(int c) +{ + smb_ucs2_t uc = tolower_w(UCS2_CHAR(c)); + return UCS2_TO_CHAR(uc); +} + +/************************************************************* + ascii only isupper - saves the need for smbd to be in C locale. +*************************************************************/ + +int isupper_ascii(int c) +{ + return isupper_w(UCS2_CHAR(c)); +} + +/************************************************************* + ascii only islower - saves the need for smbd to be in C locale. +*************************************************************/ + +int islower_ascii(int c) +{ + return islower_w(UCS2_CHAR(c)); +} -- cgit From 0af1500fc0bafe61019f1b2ab1d9e1d369221240 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 3 Feb 2006 22:19:41 +0000 Subject: r13316: Let the carnage begin.... Sync with trunk as off r13315 (This used to be commit 17e63ac4ed8325c0d44fe62b2442449f3298559f) --- source3/lib/util_unistr.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 880416a549..bc90314fce 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -291,24 +291,18 @@ int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c. */ -size_t rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, char **dest, - UNISTR2 *src) +char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, UNISTR2 *src) { pstring tmp; size_t result; result = pull_ucs2(NULL, tmp, src->buffer, sizeof(tmp), src->uni_str_len * 2, 0); - if (result < 0) { - return result; - } - - *dest = talloc_strdup(mem_ctx, tmp); - if (*dest == NULL) { - return -1; + if (result == (size_t)-1) { + return NULL; } - return result; + return talloc_strdup(mem_ctx, tmp); } /* Converts a string from internal samba format to unicode -- cgit From 8d5ef34aa35406c32b00a75bcb3aed0c1c0979b3 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 Feb 2006 01:06:21 +0000 Subject: r13521: Implement LOOKUPNAME3 and 4. Jeremy. (This used to be commit 6ec0e9124a1a7b19c9853b8e26075cbbb8751f10) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index bc90314fce..892782b181 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -291,7 +291,7 @@ int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c. */ -char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, UNISTR2 *src) +char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, const UNISTR2 *src) { pstring tmp; size_t result; -- cgit From e6676a9a6928deb03c2a43a66c1245dd05d03e71 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 14 Mar 2006 17:21:30 +0000 Subject: r14387: Try and fix the coverity issues (#53, #54) with negative sink by ensuring all uses of rpcstr_push are consistent with a size_t dest size arg. Jeremy. (This used to be commit f65d7afe1977d9d85046732842f9643716c15088) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 892782b181..9713c0ccb7 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -229,7 +229,7 @@ void init_valid_table(void) null termination if applied ********************************************************************/ -size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate) +size_t dos_PutUniCode(char *dst,const char *src, size_t len, BOOL null_terminate) { int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE : STR_UNICODE|STR_NOALIGN; @@ -308,7 +308,7 @@ char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, const UNISTR2 *src) /* Converts a string from internal samba format to unicode */ -int rpcstr_push(void* dest, const char *src, int dest_len, int flags) +int rpcstr_push(void* dest, const char *src, size_t dest_len, int flags) { return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN); } -- cgit From bbf666e447132a5f6b206ddf9ca918298b756392 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Sat, 8 Apr 2006 17:25:31 +0000 Subject: r15003: patch based on code from Arkady Glabek to ensure that global memory is freed when unloading pam_winbind.so (needs more testing on non-linux platforms) (This used to be commit 1e0b79e591d70352a96e0a0487d8f394dc7b36ba) --- source3/lib/util_unistr.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 9713c0ccb7..eef484148d 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -31,6 +31,9 @@ static smb_ucs2_t *upcase_table; static smb_ucs2_t *lowcase_table; static uint8 *valid_table; +static BOOL upcase_table_use_unmap; +static BOOL lowcase_table_use_unmap; +static BOOL valid_table_use_unmap; /** * This table says which Unicode characters are valid dos @@ -40,6 +43,32 @@ static uint8 *valid_table; **/ static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */ +/** + * Destroy global objects allocated by load_case_tables() + **/ +void gfree_case_tables(void) +{ + if ( upcase_table ) { + if ( upcase_table_use_unmap ) + unmap_file(upcase_table, 0x20000); + else + SAFE_FREE(upcase_table); + } + + if ( lowcase_table ) { + if ( lowcase_table_use_unmap ) + unmap_file(lowcase_table, 0x20000); + else + SAFE_FREE(lowcase_table); + } + + if ( valid_table ) { + if ( valid_table_use_unmap ) + unmap_file(valid_table, 0x10000); + else + SAFE_FREE(valid_table); + } +} /** * Load or generate the case handling tables. @@ -60,7 +89,10 @@ void load_case_tables(void) initialised = 1; upcase_table = map_file(lib_path("upcase.dat"), 0x20000); + upcase_table_use_unmap = ( upcase_table != NULL ); + lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000); + lowcase_table_use_unmap = ( lowcase_table != NULL ); #ifdef HAVE_SETLOCALE /* Get the name of the current locale. */ @@ -196,6 +228,7 @@ void init_valid_table(void) if (valid_file) { valid_table = valid_file; mapped_file = 1; + valid_table_use_unmap = True; return; } @@ -203,7 +236,11 @@ void init_valid_table(void) * It might need to be regenerated if the code page changed. * We know that we're not using a mapped file, so we can * free() the old one. */ - if (valid_table) free(valid_table); + if (valid_table) + SAFE_FREE(valid_table); + + /* use free rather than unmap */ + valid_table_use_unmap = False; DEBUG(2,("creating default valid table\n")); valid_table = SMB_MALLOC(0x10000); -- cgit From dc36abe3a234ddf7973a3cc19c06d3cef17fa4e1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 20 Jun 2006 18:14:33 +0000 Subject: r16420: Fix Klocwork #1674. Null deref. Jeremy. (This used to be commit f5dddf339ee3a867e21f34a81bd0b33195b7397d) --- source3/lib/util_unistr.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index eef484148d..a0015c265f 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -98,8 +98,10 @@ void load_case_tables(void) /* Get the name of the current locale. */ old_locale = setlocale(LC_ALL, NULL); - /* Save it as it is in static storage. */ - saved_locale = SMB_STRDUP(old_locale); + if (old_locale) { + /* Save it as it is in static storage. */ + saved_locale = SMB_STRDUP(old_locale); + } /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */ setlocale(LC_ALL, "C"); @@ -139,8 +141,10 @@ void load_case_tables(void) #ifdef HAVE_SETLOCALE /* Restore the old locale. */ - setlocale (LC_ALL, saved_locale); - SAFE_FREE(saved_locale); + if (saved_locale) { + setlocale (LC_ALL, saved_locale); + SAFE_FREE(saved_locale); + } #endif } -- cgit From e23781b3b304d1e69ad80af5ae9c0ed8d02cf996 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 30 Jul 2006 16:36:56 +0000 Subject: r17316: More C++ warnings -- 456 left (This used to be commit 1e4ee728df7eeafc1b4d533240acb032f73b4f5c) --- source3/lib/util_unistr.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index a0015c265f..31114feb92 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -88,10 +88,12 @@ void load_case_tables(void) } initialised = 1; - upcase_table = map_file(lib_path("upcase.dat"), 0x20000); + upcase_table = (smb_ucs2_t *)map_file(lib_path("upcase.dat"), + 0x20000); upcase_table_use_unmap = ( upcase_table != NULL ); - lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000); + lowcase_table = (smb_ucs2_t *)map_file(lib_path("lowcase.dat"), + 0x20000); lowcase_table_use_unmap = ( lowcase_table != NULL ); #ifdef HAVE_SETLOCALE @@ -111,7 +113,7 @@ void load_case_tables(void) not available */ if (!upcase_table) { DEBUG(1,("creating lame upcase table\n")); - upcase_table = SMB_MALLOC(0x20000); + upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000); for (i=0;i<0x10000;i++) { smb_ucs2_t v; SSVAL(&v, 0, i); @@ -126,7 +128,7 @@ void load_case_tables(void) if (!lowcase_table) { DEBUG(1,("creating lame lowcase table\n")); - lowcase_table = SMB_MALLOC(0x20000); + lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000); for (i=0;i<0x10000;i++) { smb_ucs2_t v; SSVAL(&v, 0, i); @@ -228,7 +230,7 @@ void init_valid_table(void) return; } - valid_file = map_file(lib_path("valid.dat"), 0x10000); + valid_file = (uint8 *)map_file(lib_path("valid.dat"), 0x10000); if (valid_file) { valid_table = valid_file; mapped_file = 1; @@ -247,7 +249,7 @@ void init_valid_table(void) valid_table_use_unmap = False; DEBUG(2,("creating default valid table\n")); - valid_table = SMB_MALLOC(0x10000); + valid_table = (uint8 *)SMB_MALLOC(0x10000); for (i=0;i<128;i++) { valid_table[i] = isalnum(i) || strchr(allowed,i); } -- cgit From 3a60a6743262ab2ab221e0fe13ef6b510424ca3f Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 21 Sep 2006 18:37:09 +0000 Subject: r18793: Fix BE string handling in the auto-generated code. Should now work again with ASU. Jeremy. (This used to be commit 53e97bf92817b6cfc3f93c999a81ef8ad49a1609) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 31114feb92..84e4cf3f83 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -171,11 +171,11 @@ static int check_dos_char_slowly(smb_ucs2_t c) smb_ucs2_t c2 = 0; int len1, len2; - len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf),False); + len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False); if (len1 == 0) { return 0; } - len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2,False); + len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False); if (len2 != 2) { return 0; } -- cgit From bc112cc46d9dacf2d1cb105d99e1e01ec38c5fa6 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 27 Dec 2006 18:36:00 +0000 Subject: r20361: Prevent strnlen_w reading beyond max. Valgrind found by Volker. Jeremy (This used to be commit 08d551163c9563bd02acd437bc1a1595e7939cee) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 84e4cf3f83..cf040a2dfc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -484,7 +484,7 @@ size_t strnlen_w(const smb_ucs2_t *src, size_t max) size_t len; smb_ucs2_t c; - for(len = 0; *(COPY_UCS2_CHAR(&c,src)) && (len < max); src++, len++) { + for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) { ; } -- cgit From 971b640e0cb8bac510771d126454a8e4d2eff507 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 29 Jun 2007 17:27:59 +0000 Subject: r23660: Anybody know what check_dos_char() was used for? It wasn't called at all, so it's gone. With it 8k bss went away. (This used to be commit 7e9a4c39a5ce620f8eac74d0ae4dd80b96c81aca) --- source3/lib/util_unistr.c | 52 +---------------------------------------------- 1 file changed, 1 insertion(+), 51 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index cf040a2dfc..0221d19dc1 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -35,14 +35,6 @@ static BOOL upcase_table_use_unmap; static BOOL lowcase_table_use_unmap; static BOOL valid_table_use_unmap; -/** - * This table says which Unicode characters are valid dos - * characters. - * - * Each value is just a single bit. - **/ -static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */ - /** * Destroy global objects allocated by load_case_tables() **/ @@ -150,21 +142,6 @@ void load_case_tables(void) #endif } -/* - see if a ucs2 character can be mapped correctly to a dos character - and mapped back to the same character in ucs2 -*/ - -int check_dos_char(smb_ucs2_t c) -{ - lazy_initialize_conv(); - - /* Find the right byte, and right bit within the byte; return - * 1 or 0 */ - return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0; -} - - static int check_dos_char_slowly(smb_ucs2_t c) { char buf[10]; @@ -182,33 +159,6 @@ static int check_dos_char_slowly(smb_ucs2_t c) return (c == c2); } - -/** - * Fill out doschar table the hard way, by examining each character - **/ - -void init_doschar_table(void) -{ - int i, j, byteval; - - /* For each byte of packed table */ - - for (i = 0; i <= 0xffff; i += 8) { - byteval = 0; - for (j = 0; j <= 7; j++) { - smb_ucs2_t c; - - c = i + j; - - if (check_dos_char_slowly(c)) { - byteval |= 1 << j; - } - } - doschar_table[i/8] = byteval; - } -} - - /** * Load the valid character map table from valid.dat or * create from the configured codepage. @@ -257,7 +207,7 @@ void init_valid_table(void) for (;i<0x10000;i++) { smb_ucs2_t c; SSVAL(&c, 0, i); - valid_table[i] = check_dos_char(c); + valid_table[i] = check_dos_char_slowly(c); } } -- cgit From ef8706acd1a880a811b65310d01f309d36d9d47e Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 29 Jun 2007 17:51:31 +0000 Subject: r23662: According to simo, check_dos_char is needed (This used to be commit c195eccefea69c17169c350a13bbfe845fc6fc44) --- source3/lib/util_unistr.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 0221d19dc1..cf040a2dfc 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -35,6 +35,14 @@ static BOOL upcase_table_use_unmap; static BOOL lowcase_table_use_unmap; static BOOL valid_table_use_unmap; +/** + * This table says which Unicode characters are valid dos + * characters. + * + * Each value is just a single bit. + **/ +static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */ + /** * Destroy global objects allocated by load_case_tables() **/ @@ -142,6 +150,21 @@ void load_case_tables(void) #endif } +/* + see if a ucs2 character can be mapped correctly to a dos character + and mapped back to the same character in ucs2 +*/ + +int check_dos_char(smb_ucs2_t c) +{ + lazy_initialize_conv(); + + /* Find the right byte, and right bit within the byte; return + * 1 or 0 */ + return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0; +} + + static int check_dos_char_slowly(smb_ucs2_t c) { char buf[10]; @@ -159,6 +182,33 @@ static int check_dos_char_slowly(smb_ucs2_t c) return (c == c2); } + +/** + * Fill out doschar table the hard way, by examining each character + **/ + +void init_doschar_table(void) +{ + int i, j, byteval; + + /* For each byte of packed table */ + + for (i = 0; i <= 0xffff; i += 8) { + byteval = 0; + for (j = 0; j <= 7; j++) { + smb_ucs2_t c; + + c = i + j; + + if (check_dos_char_slowly(c)) { + byteval |= 1 << j; + } + } + doschar_table[i/8] = byteval; + } +} + + /** * Load the valid character map table from valid.dat or * create from the configured codepage. @@ -207,7 +257,7 @@ void init_valid_table(void) for (;i<0x10000;i++) { smb_ucs2_t c; SSVAL(&c, 0, i); - valid_table[i] = check_dos_char_slowly(c); + valid_table[i] = check_dos_char(c); } } -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index cf040a2dfc..fa4f0f1a76 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -7,7 +7,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or + the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, -- cgit From 5e54558c6dea67b56bbfaba5698f3a434d3dffb6 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 00:52:41 +0000 Subject: r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text (This used to be commit b0132e94fc5fef936aa766fb99a306b3628e9f07) --- source3/lib/util_unistr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index fa4f0f1a76..a4952d137e 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -16,8 +16,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program. If not, see . */ #include "includes.h" -- cgit From 7cbcb5f8c5bfd9bac6527232e7f5d011f03ed9ac Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 21 Sep 2007 10:23:35 +0000 Subject: r25274: Attempt to fix RPC-SAMBA3-GETUSERNAME (This used to be commit f6f64cf0b51f2fb841bd1c0f800c66114c213577) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index a4952d137e..64cfd52120 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -361,7 +361,7 @@ int rpcstr_push(void* dest, const char *src, size_t dest_len, int flags) void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) { - if (str == NULL) { + if ((str == NULL) || (str->uni_str_len == 0)) { *dest='\0'; return; } @@ -374,7 +374,7 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen) { - if (str == NULL) { + if ((str == NULL) || (str->uni_str_len == 0)) { *dest='\0'; return; } -- cgit From 30191d1a5704ad2b158386b511558972d539ce47 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 18 Oct 2007 17:40:25 -0700 Subject: RIP BOOL. Convert BOOL -> bool. I found a few interesting bugs in various places whilst doing this (places that assumed BOOL == int). I also need to fix the Samba4 pidl generation (next checkin). Jeremy. (This used to be commit f35a266b3cbb3e5fa6a86be60f34fe340a3ca71f) --- source3/lib/util_unistr.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 64cfd52120..2b91ea9c80 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -30,9 +30,9 @@ static smb_ucs2_t *upcase_table; static smb_ucs2_t *lowcase_table; static uint8 *valid_table; -static BOOL upcase_table_use_unmap; -static BOOL lowcase_table_use_unmap; -static BOOL valid_table_use_unmap; +static bool upcase_table_use_unmap; +static bool lowcase_table_use_unmap; +static bool valid_table_use_unmap; /** * This table says which Unicode characters are valid dos @@ -271,7 +271,7 @@ void init_valid_table(void) null termination if applied ********************************************************************/ -size_t dos_PutUniCode(char *dst,const char *src, size_t len, BOOL null_terminate) +size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate) { int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE : STR_UNICODE|STR_NOALIGN; @@ -435,7 +435,7 @@ smb_ucs2_t tolower_w( smb_ucs2_t val ) Determine if a character is lowercase. ********************************************************************/ -BOOL islower_w(smb_ucs2_t c) +bool islower_w(smb_ucs2_t c) { return upcase_table[SVAL(&c,0)] != c; } @@ -444,7 +444,7 @@ BOOL islower_w(smb_ucs2_t c) Determine if a character is uppercase. ********************************************************************/ -BOOL isupper_w(smb_ucs2_t c) +bool isupper_w(smb_ucs2_t c) { return lowcase_table[SVAL(&c,0)] != c; } @@ -453,7 +453,7 @@ BOOL isupper_w(smb_ucs2_t c) Determine if a character is valid in a 8.3 name. ********************************************************************/ -BOOL isvalid83_w(smb_ucs2_t c) +bool isvalid83_w(smb_ucs2_t c) { return valid_table[SVAL(&c,0)] != 0; } @@ -594,10 +594,10 @@ smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins) return True if any char is converted ********************************************************************/ -BOOL strlower_w(smb_ucs2_t *s) +bool strlower_w(smb_ucs2_t *s) { smb_ucs2_t cp; - BOOL ret = False; + bool ret = False; while (*(COPY_UCS2_CHAR(&cp,s))) { smb_ucs2_t v = tolower_w(cp); @@ -615,10 +615,10 @@ BOOL strlower_w(smb_ucs2_t *s) return True if any char is converted ********************************************************************/ -BOOL strupper_w(smb_ucs2_t *s) +bool strupper_w(smb_ucs2_t *s) { smb_ucs2_t cp; - BOOL ret = False; + bool ret = False; while (*(COPY_UCS2_CHAR(&cp,s))) { smb_ucs2_t v = toupper_w(cp); if (v != cp) { @@ -706,7 +706,7 @@ int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len) Compare 2 strings. ********************************************************************/ -BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) +bool strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) { if (s1 == s2) { return(True); @@ -722,7 +722,7 @@ BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2) Compare 2 strings up to and including the nth char. ******************************************************************/ -BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n) +bool strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n) { if (s1 == s2) { return(True); @@ -849,10 +849,10 @@ void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc) Trim unicode string. ********************************************************************/ -BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, +bool trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front, const smb_ucs2_t *back) { - BOOL ret = False; + bool ret = False; size_t len, front_len, back_len; if (!s) { @@ -951,7 +951,7 @@ smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) return NULL; } -BOOL trim_string_wa(smb_ucs2_t *s, const char *front, +bool trim_string_wa(smb_ucs2_t *s, const char *front, const char *back) { wpstring f, b; -- cgit From 88ee61625a5de5e443d14c54eab91a90d87cda85 Mon Sep 17 00:00:00 2001 From: "Gerald (Jerry) Carter" Date: Thu, 1 Nov 2007 15:53:44 -0400 Subject: Patch 2 of 3 from Debian Samba packagers: The point is doing the following associations: - non discardable state data (all TDB files that may need to be backed up) go to statedir - shared data (codepage stuff) go to codepagedir The patch *does not change* the default location for these directories. So, there is no behaviour change when applying it. The main change is for samba developers who have to think when dealing with files that previously pertained to libdir whether they: - go in statedir - go in codepagedir - stay in libdir (This used to be commit d6cdbfd875bb2653e831d314726c3240beb0a96b) --- source3/lib/util_unistr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 2b91ea9c80..e9e2c33fb3 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -87,11 +87,11 @@ void load_case_tables(void) } initialised = 1; - upcase_table = (smb_ucs2_t *)map_file(lib_path("upcase.dat"), + upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"), 0x20000); upcase_table_use_unmap = ( upcase_table != NULL ); - lowcase_table = (smb_ucs2_t *)map_file(lib_path("lowcase.dat"), + lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"), 0x20000); lowcase_table_use_unmap = ( lowcase_table != NULL ); @@ -229,7 +229,7 @@ void init_valid_table(void) return; } - valid_file = (uint8 *)map_file(lib_path("valid.dat"), 0x10000); + valid_file = (uint8 *)map_file(data_path("valid.dat"), 0x10000); if (valid_file) { valid_table = valid_file; mapped_file = 1; -- cgit From 2b3c44e4fb980335c22abcc07a88f32b13e5918f Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 10 Nov 2007 22:31:34 -0800 Subject: Always define PATH_MAX. Makes code simpler (removes a bunch of #defines). Remove pstring from msdfs.c. Jeremy. (This used to be commit e203ba22275320808bc11b17361ad1f2d5b0b897) --- source3/lib/util_unistr.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index e9e2c33fb3..c4569e102e 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -350,11 +350,20 @@ char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, const UNISTR2 *src) /* Converts a string from internal samba format to unicode */ -int rpcstr_push(void* dest, const char *src, size_t dest_len, int flags) +int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags) { return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN); } +/* Converts a string from internal samba format to unicode. Always terminates. + * Actually just a wrapper round push_ucs2_talloc(). + */ + +int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src) +{ + return push_ucs2_talloc(ctx, dest, src); +} + /******************************************************************* Convert a (little-endian) UNISTR2 structure to an ASCII string. ********************************************************************/ -- cgit From 68be9a820059ee96dd26c527efd7c14e679d3f2c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 15 Nov 2007 14:19:52 -0800 Subject: More pstring removal. This one was tricky. I had to add one horror (pstring_clean_name()) which will have to remain until I've removed all pstrings from the client code. Jeremy. (This used to be commit 1ea3ac80146b83c2522b69e7747c823366a2b47d) --- source3/lib/util_unistr.c | 52 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 10 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c4569e102e..8fad1162ac 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -81,12 +81,15 @@ void load_case_tables(void) static int initialised; char *old_locale = NULL, *saved_locale = NULL; int i; + TALLOC_CTX *frame = NULL; if (initialised) { return; } initialised = 1; + frame = talloc_stackframe(); + upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"), 0x20000); upcase_table_use_unmap = ( upcase_table != NULL ); @@ -147,6 +150,7 @@ void load_case_tables(void) SAFE_FREE(saved_locale); } #endif + TALLOC_FREE(frame); } /* @@ -157,7 +161,7 @@ void load_case_tables(void) int check_dos_char(smb_ucs2_t c) { lazy_initialize_conv(); - + /* Find the right byte, and right bit within the byte; return * 1 or 0 */ return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0; @@ -329,26 +333,54 @@ int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) * copy because I don't really know how pull_ucs2 and friends calculate the * target size. If this turns out to be a major bottleneck someone with deeper * multi-byte knowledge needs to revisit this. + * I just did (JRA :-). No longer uses copy. * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c. */ -char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, const UNISTR2 *src) +char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *ctx, const UNISTR2 *src) { - pstring tmp; - size_t result; - - result = pull_ucs2(NULL, tmp, src->buffer, sizeof(tmp), - src->uni_str_len * 2, 0); - if (result == (size_t)-1) { + char *dest = NULL; + size_t dest_len = convert_string_talloc(ctx, + CH_UTF16LE, + CH_UNIX, + src->buffer, + src->uni_str_len * 2, + (void **)&dest, + true); + if (dest_len == (size_t)-1) { return NULL; } - return talloc_strdup(mem_ctx, tmp); + /* Ensure we're returning a null terminated string. */ + if (dest_len) { + /* Did we already process the terminating zero ? */ + if (dest[dest_len-1] != 0) { + size_t size = talloc_get_size(dest); + /* Have we got space to append the '\0' ? */ + if (size <= dest_len) { + /* No, realloc. */ + dest = TALLOC_REALLOC_ARRAY(ctx, dest, char, + dest_len+1); + if (!dest) { + /* talloc fail. */ + dest_len = (size_t)-1; + return NULL; + } + } + /* Yay - space ! */ + dest[dest_len] = '\0'; + dest_len++; + } + } else if (dest) { + dest[0] = 0; + } + + return dest; } /* Converts a string from internal samba format to unicode - */ + */ int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags) { -- cgit From cf31bb7532484dbe26e4a00506ade0a173d8d754 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 19 Nov 2007 17:51:21 -0800 Subject: Start to remove some of the pstring functions we're no longer using. Jeremy. (This used to be commit c21e9bdc1059268adbd14207e74097349676439e) --- source3/lib/util_unistr.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 8fad1162ac..1fef6ab239 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -422,16 +422,33 @@ void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen) pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); } - + /******************************************************************* - Give a static string for displaying a UNISTR2. + Return a string for displaying a UNISTR2. Guarentees to return a + valid string - "" if nothing else. + Changed to use talloc_tos() under the covers.... JRA. ********************************************************************/ const char *unistr2_static(const UNISTR2 *str) { - static pstring ret; - unistr2_to_ascii(ret, str, sizeof(ret)); - return ret; + size_t ret = (size_t)-1; + char *dest = NULL; + + if ((str == NULL) || (str->uni_str_len == 0)) { + return ""; + } + + ret = pull_ucs2_base_talloc(talloc_tos(), + NULL, + &dest, + str->buffer, + str->uni_str_len*2, + STR_NOALIGN); + if (ret == (size_t)-1 || dest == NULL) { + return ""; + } + + return dest; } /******************************************************************* @@ -992,24 +1009,6 @@ smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins) return NULL; } -bool trim_string_wa(smb_ucs2_t *s, const char *front, - const char *back) -{ - wpstring f, b; - - if (front) { - push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE); - } else { - *f = 0; - } - if (back) { - push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE); - } else { - *b = 0; - } - return trim_string_w(s, f, b); -} - /******************************************************************* Returns the length in number of wide characters. ******************************************************************/ -- cgit From c049b8157d0bb4b5cd57b66740a26813be4f9eba Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 20 Nov 2007 15:31:37 -0800 Subject: More trivial pstring removal. Jeremy. (This used to be commit bac3bb2f6e9e15b3a9a6a6511c484f8509918ca1) --- source3/lib/util_unistr.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 1fef6ab239..45f09da85b 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -319,6 +319,25 @@ int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags) return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN); } +/* Copy a string from little-endian or big-endian unicode source (depending + * on flags) to internal samba format destination. Allocates on talloc ctx. + */ + +int rpcstr_pull_talloc(TALLOC_CTX *ctx, + char **dest, + void *src, + int src_len, + int flags) +{ + return pull_ucs2_base_talloc(ctx, + NULL, + dest, + src, + src_len, + flags|STR_UNICODE|STR_NOALIGN); + +} + /* Copy a string from a unistr2 source to internal samba format destination. Use this instead of direct calls to rpcstr_pull() to avoid having to determine whether the source string is null terminated. */ -- cgit From 3f100a597601ec5d8cc029dd99c925fc6b373e10 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 16 Nov 2007 14:48:26 +0100 Subject: Fix a type-punned warning (This used to be commit 4c992e4340eda31e686a2d2a8db6364aaa456c11) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 45f09da85b..bd2cd73cc4 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -365,7 +365,7 @@ char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *ctx, const UNISTR2 *src) CH_UNIX, src->buffer, src->uni_str_len * 2, - (void **)&dest, + (void *)&dest, true); if (dest_len == (size_t)-1) { return NULL; -- cgit From 6b6655edd90850d09c7711fc3b9fe98271e3e625 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 27 Nov 2007 14:35:30 -0800 Subject: Remove pstrings from everything except srv_spoolss_nt.c. Jeremy. (This used to be commit 0002a9e96b0ef78316295a6eb94ff29b64e2f988) --- source3/lib/util_unistr.c | 57 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 29 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index bd2cd73cc4..49b0b814f7 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -408,7 +408,7 @@ int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags) /* Converts a string from internal samba format to unicode. Always terminates. * Actually just a wrapper round push_ucs2_talloc(). - */ + */ int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src) { @@ -428,6 +428,7 @@ void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen) pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); } +#if 0 /******************************************************************* Convert a (little-endian) UNISTR3 structure to an ASCII string. ********************************************************************/ @@ -441,53 +442,51 @@ void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen) pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); } +#endif /******************************************************************* - Return a string for displaying a UNISTR2. Guarentees to return a - valid string - "" if nothing else. - Changed to use talloc_tos() under the covers.... JRA. + Duplicate a UNISTR2 string into a null terminated char* + using a talloc context. ********************************************************************/ -const char *unistr2_static(const UNISTR2 *str) +char *unistr2_to_ascii_talloc(TALLOC_CTX *ctx, const UNISTR2 *str) { - size_t ret = (size_t)-1; - char *dest = NULL; + char *s = NULL; - if ((str == NULL) || (str->uni_str_len == 0)) { - return ""; + if (!str || !str->buffer) { + return NULL; } - - ret = pull_ucs2_base_talloc(talloc_tos(), + if (pull_ucs2_base_talloc(ctx, NULL, - &dest, + &s, str->buffer, str->uni_str_len*2, - STR_NOALIGN); - if (ret == (size_t)-1 || dest == NULL) { - return ""; + STR_NOALIGN) == (size_t)-1) { + return NULL; } - - return dest; + return s; } /******************************************************************* - Duplicate a UNISTR2 string into a null terminated char* - using a talloc context. + Return a string for displaying a UNISTR2. Guarentees to return a + valid string - "" if nothing else. + Changed to use talloc_tos() under the covers.... JRA. ********************************************************************/ -char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str) +const char *unistr2_static(const UNISTR2 *str) { - char *s; - int maxlen = (str->uni_str_len+1)*4; - if (!str->buffer) { - return NULL; + char *dest = NULL; + + if ((str == NULL) || (str->uni_str_len == 0)) { + return ""; } - s = (char *)TALLOC(ctx, maxlen); /* convervative */ - if (!s) { - return NULL; + + dest = unistr2_to_ascii_talloc(talloc_tos(), str); + if (!dest) { + return ""; } - pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN); - return s; + + return dest; } /******************************************************************* -- cgit From c335e3322d1690331366972cf55dde9f4305981c Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 16 Dec 2007 00:15:34 +0100 Subject: Make check_dos_char static It's only called in t_doschar, a pretty bogus test program that is not compiled by default (This used to be commit 99c59e809ef3c5abcdfb796d50be7c40f7033787) --- source3/lib/util_unistr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 49b0b814f7..5bb8cbae96 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -158,7 +158,7 @@ void load_case_tables(void) and mapped back to the same character in ucs2 */ -int check_dos_char(smb_ucs2_t c) +static int check_dos_char(smb_ucs2_t c) { lazy_initialize_conv(); -- cgit From 2ae400b213a673253da0e3088ce1c3f54d9681cc Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 16 Dec 2007 00:17:41 +0100 Subject: Make init_doschar_table() static It's only called directly before init_valid_table() anyway, so move it there. (This used to be commit b6d1d2428866712a550a354732e2386b4f87b67b) --- source3/lib/util_unistr.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 5bb8cbae96..510f40e611 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -190,7 +190,7 @@ static int check_dos_char_slowly(smb_ucs2_t c) * Fill out doschar table the hard way, by examining each character **/ -void init_doschar_table(void) +static void init_doschar_table(void) { int i, j, byteval; @@ -228,6 +228,8 @@ void init_valid_table(void) const char *allowed = ".!#$%&'()_-@^`~"; uint8 *valid_file; + init_doschar_table(); + if (mapped_file) { /* Can't unmap files, so stick with what we have */ return; -- cgit From e28c6813e024d40104969ce8c73b503c053c41cd Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 16 Dec 2007 00:19:42 +0100 Subject: Tiny fixes to init_valid_table() (This used to be commit a8948e0727b100945ab730d4d144ccbe219bbc94) --- source3/lib/util_unistr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 510f40e611..2949d7d795 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -247,14 +247,14 @@ void init_valid_table(void) * It might need to be regenerated if the code page changed. * We know that we're not using a mapped file, so we can * free() the old one. */ - if (valid_table) - SAFE_FREE(valid_table); + SAFE_FREE(valid_table); /* use free rather than unmap */ valid_table_use_unmap = False; DEBUG(2,("creating default valid table\n")); valid_table = (uint8 *)SMB_MALLOC(0x10000); + SMB_ASSERT(valid_table != NULL); for (i=0;i<128;i++) { valid_table[i] = isalnum(i) || strchr(allowed,i); } -- cgit From 3c490d91f13df9894bf84350daa1e2da6d8206e4 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 16 Dec 2007 00:25:49 +0100 Subject: get rid of doschar_table[] If I'm not completely blind, then check_dos_char is *only* used in the case when we can't mmap() valid.dat. To me this looks as if we initialize the 65536 bits in doschar_table[] with check_dos_char_slowly, use it once to initialize valid_table[] and *never* use them again. I think there's no point in keeping these 8k of modified memory around for an unlikely case (no "valid.dat") and even that only to use it exactly once. (This used to be commit 0bfea7259e4091c6d914528a0e65fc10d8a56fb8) --- source3/lib/util_unistr.c | 58 ++++------------------------------------------- 1 file changed, 4 insertions(+), 54 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 2949d7d795..84ee673a67 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -34,14 +34,6 @@ static bool upcase_table_use_unmap; static bool lowcase_table_use_unmap; static bool valid_table_use_unmap; -/** - * This table says which Unicode characters are valid dos - * characters. - * - * Each value is just a single bit. - **/ -static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte */ - /** * Destroy global objects allocated by load_case_tables() **/ @@ -153,21 +145,6 @@ void load_case_tables(void) TALLOC_FREE(frame); } -/* - see if a ucs2 character can be mapped correctly to a dos character - and mapped back to the same character in ucs2 -*/ - -static int check_dos_char(smb_ucs2_t c) -{ - lazy_initialize_conv(); - - /* Find the right byte, and right bit within the byte; return - * 1 or 0 */ - return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0; -} - - static int check_dos_char_slowly(smb_ucs2_t c) { char buf[10]; @@ -185,33 +162,6 @@ static int check_dos_char_slowly(smb_ucs2_t c) return (c == c2); } - -/** - * Fill out doschar table the hard way, by examining each character - **/ - -static void init_doschar_table(void) -{ - int i, j, byteval; - - /* For each byte of packed table */ - - for (i = 0; i <= 0xffff; i += 8) { - byteval = 0; - for (j = 0; j <= 7; j++) { - smb_ucs2_t c; - - c = i + j; - - if (check_dos_char_slowly(c)) { - byteval |= 1 << j; - } - } - doschar_table[i/8] = byteval; - } -} - - /** * Load the valid character map table from valid.dat or * create from the configured codepage. @@ -228,8 +178,6 @@ void init_valid_table(void) const char *allowed = ".!#$%&'()_-@^`~"; uint8 *valid_file; - init_doschar_table(); - if (mapped_file) { /* Can't unmap files, so stick with what we have */ return; @@ -258,11 +206,13 @@ void init_valid_table(void) for (i=0;i<128;i++) { valid_table[i] = isalnum(i) || strchr(allowed,i); } - + + lazy_initialize_conv(); + for (;i<0x10000;i++) { smb_ucs2_t c; SSVAL(&c, 0, i); - valid_table[i] = check_dos_char(c); + valid_table[i] = check_dos_char_slowly(c); } } -- cgit From fb37f156009611af0dd454a0fb0829a09cd638ac Mon Sep 17 00:00:00 2001 From: Tim Prouty Date: Tue, 29 Apr 2008 14:36:24 -0700 Subject: Cleanup size_t return values in callers of convert_string_allocate This patch is the second iteration of an inside-out conversion to cleanup functions in charcnv.c returning size_t == -1 to indicate failure. (This used to be commit 6b189dabc562d86dcaa685419d0cb6ea276f100d) --- source3/lib/util_unistr.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 84ee673a67..76235ad041 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -312,14 +312,12 @@ int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *ctx, const UNISTR2 *src) { char *dest = NULL; - size_t dest_len = convert_string_talloc(ctx, - CH_UTF16LE, - CH_UNIX, - src->buffer, - src->uni_str_len * 2, - (void *)&dest, - true); - if (dest_len == (size_t)-1) { + size_t dest_len; + + if (!convert_string_talloc(ctx, CH_UTF16LE, CH_UNIX, src->buffer, + src->uni_str_len * 2, (void *)&dest, + &dest_len, true)) + { return NULL; } @@ -364,7 +362,11 @@ int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags) int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src) { - return push_ucs2_talloc(ctx, dest, src); + size_t size; + if (push_ucs2_talloc(ctx, dest, src, &size)) + return size; + else + return -1; } /******************************************************************* -- cgit From 03991ab0734ecbb87a75238d1356fbe0e5b1d38d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 12 Aug 2008 13:35:15 -0700 Subject: Fix bug 5686 - libsmbclient segfaults with more than one SMBCCTX. Here is a patch to allow many subsystems to be re-initialized. The only functional change I made was to remove the null context tracking, as the memory allocated here is designed to be left for the complete lifetime of the program. Freeing this early (when all smb contexts are destroyed) could crash other users of talloc. Jeremy. (This used to be commit 8c630efd25cf17aff59448ca05c1b44a41964b16) --- source3/lib/util_unistr.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_unistr.c') diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index 76235ad041..4e78d1b064 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -33,6 +33,7 @@ static uint8 *valid_table; static bool upcase_table_use_unmap; static bool lowcase_table_use_unmap; static bool valid_table_use_unmap; +static bool initialized; /** * Destroy global objects allocated by load_case_tables() @@ -59,6 +60,7 @@ void gfree_case_tables(void) else SAFE_FREE(valid_table); } + initialized = false; } /** @@ -70,15 +72,14 @@ void gfree_case_tables(void) void load_case_tables(void) { - static int initialised; char *old_locale = NULL, *saved_locale = NULL; int i; TALLOC_CTX *frame = NULL; - if (initialised) { + if (initialized) { return; } - initialised = 1; + initialized = true; frame = talloc_stackframe(); -- cgit