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') 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