the first independent msrpc daemon - lsarpcd.
[kai/samba.git] / source3 / lib / util_unistr.c
index c58820cfeced603c8000191128ff94ad945e89b6..6f90528bf43c0c8c9ebd484d56327a92a0f40d4b 100644 (file)
 #include "includes.h"
 
 /*******************************************************************
-write a string in unicoode format
-********************************************************************/
-int PutUniCode(char *dst,char *src)
+ Put an ASCII string into a UNICODE buffer (little endian).
+ ********************************************************************/
+
+char *ascii_to_unibuf(char *dest, const char *src, int maxlen)
 {
-  int ret = 0;
-  while (*src) {
-    dst[ret++] = src[0];
-    dst[ret++] = 0;    
-    src++;
-  }
-  dst[ret++]=0;
-  dst[ret++]=0;
-  return(ret);
+       char *destend = dest + maxlen;
+       register char c;
+
+       while (dest < destend)
+       {
+               c = *(src++);
+               if (c == 0)
+               {
+                       break;
+               }
+
+               *(dest++) = c;
+               *(dest++) = 0;
+       }
+
+       *dest++ = 0;
+       *dest++ = 0;
+       return dest;
 }
 
+
 /*******************************************************************
-skip past some unicode strings in a buffer
-********************************************************************/
-char *skip_unicode_string(char *buf,int n)
+ Pull an ASCII string out of a UNICODE buffer (little endian).
+ ********************************************************************/
+
+const char* unibuf_to_ascii(char *dest, const char *src, int maxlen)
 {
-  while (n--)
-  {
-    while (*buf)
-      buf += 2;
-    buf += 2;
-  }
-  return(buf);
+       char *destend = dest + maxlen;
+       register char c;
+
+       while (dest < destend)
+       {
+               c = *(src++);
+               if ((c == 0) && (*src == 0))
+               {
+                       break;
+               }
+
+               *dest++ = c;
+               src++;
+       }
+
+       *dest = 0;
+
+       return src;
 }
 
+
 /*******************************************************************
-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)
+ Put an ASCII string into a UNICODE array (uint16's).
+ ********************************************************************/
+
+void ascii_to_unistr(uint16 *dest, const char *src, int maxlen)
 {
-       static char lbufs[8][MAXUNI];
-       static int nexti;
-       char *lbuf = lbufs[nexti];
-       char *p;
+       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).
+ ********************************************************************/
 
-       nexti = (nexti+1)%8;
+void unistr_to_ascii(char *dest, const uint16 *src, int len)
+{
+       char *destend = dest + len;
+       register uint16 c;
 
-       for (p = lbuf; *buf && p-lbuf < MAXUNI-2 && len > 0; len--, p++, buf++)
+       while (dest < destend)
        {
-               *p = *buf;
+               c = *(src++);
+               if (c == 0)
+               {
+                       break;
+               }
+
+               *(dest++) = (char)c;
        }
 
-       *p = 0;
-       return lbuf;
+       *dest = 0;
 }
 
-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)
+ Convert a UNISTR2 structure to an ASCII string
+ ********************************************************************/
+
+void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
 {
-       char *lbuf = lbufs[nexti];
-       char *p;
+       char *destend;
+       const uint16 *src;
+       size_t len;
+       register uint16 c;
 
-       nexti = (nexti+1)%8;
+       src = str->buffer;
+       len = MIN(str->uni_str_len, maxlen);
+       destend = dest + len;
 
-       for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf++)
+       while (dest < destend)
        {
-               *p = *buf;
+               c = *(src++);
+               if (c == 0)
+               {
+                       break;
+               }
+
+               *(dest++) = (char)c;
        }
 
-       *p = 0;
-       return lbuf;
+       *dest = 0;
 }
 
+
 /*******************************************************************
-Return a ascii version of a unicode string
-********************************************************************/
-char *unistr2_to_str(UNISTR2 *str)
+ Skip a UNICODE string in a little endian buffer.
+ ********************************************************************/
+
+char *skip_unibuf(char *srcbuf, int len)
 {
-       char *lbuf = lbufs[nexti];
-       char *p;
-       uint16 *buf = str->buffer;
-       int max_size = MIN(sizeof(str->buffer)-2, str->uni_str_len);
+       uint16 *src = (uint16 *)srcbuf;
+       uint16 *srcend = src + len/2;
+
+       while ((src < srcend) && (*(src++) != 0))
+       {
+       }
+
+       return (char *)src;
+}
+
+
+/*******************************************************************
+ UNICODE strcpy between buffers.
+ ********************************************************************/
 
-       nexti = (nexti+1)%8;
+char *uni_strncpy(char *destbuf, const char *srcbuf, int len)
+{
+       const uint16 *src = (const uint16 *)srcbuf;
+       uint16 *dest = (uint16 *)destbuf;
+       uint16 *destend = dest + len/2;
+       register uint16 c;
 
-       for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++)
+       while (dest < destend)
        {
-               *p = *buf;
+               c = *(src++);
+               if (c == 0)
+               {
+                       break;
+               }
+
+               *(dest++) = c;
        }
 
-       *p = 0;
-       return lbuf;
+       *dest++ = 0;
+       return (char *)dest;
 }
 
+
 /*******************************************************************
-Return a number stored in a buffer
-********************************************************************/
-uint32 buffer2_to_uint32(BUFFER2 *str)
+ Return a number stored in a buffer
+ ********************************************************************/
+
+uint32 buffer2_to_uint32(const BUFFER2 *str)
 {
        if (str->buf_len == 4)
        {
-               return IVAL(str->buffer, 0);
+               const uchar *src = str->buffer;
+               return IVAL(src, 0);
        }
        else
        {
@@ -133,118 +214,94 @@ uint32 buffer2_to_uint32(BUFFER2 *str)
        }
 }
 
+
 /*******************************************************************
-Return a ascii version of a NOTunicode string
-********************************************************************/
-char *buffer2_to_str(BUFFER2 *str)
+  Convert a 'multi-string' buffer to space-separated ASCII.
+ ********************************************************************/
+void buffer2_to_multistr(char *dest, const BUFFER2 *str, size_t maxlen)
 {
-       char *lbuf = lbufs[nexti];
-       char *p;
-       uint16 *buf = str->buffer;
-       int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2);
+       char *destend;
+       const uchar *src;
+       size_t len;
+       register uint16 c;
 
-       nexti = (nexti+1)%8;
+       src = str->buffer;
+       len = MIN(str->buf_len/2, maxlen);
+       destend = dest + len;
 
-       for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++)
+       while (dest < destend)
        {
-               *p = *buf;
+               c = *(src++);
+               *(dest++) = (c == 0) ? ' ' : (char)c;
+               src++;
        }
 
-       *p = 0;
-       return lbuf;
+       *dest = 0;
 }
 
 /*******************************************************************
-Return a ascii version of a NOTunicode string
-********************************************************************/
-char *buffer2_to_multistr(BUFFER2 *str)
+  Convert a buffer4 to space-separated ASCII.
+ ********************************************************************/
+void buffer4_to_str(char *dest, const BUFFER4 *str, size_t maxlen)
 {
-       char *lbuf = lbufs[nexti];
-       char *p;
-       uint16 *buf = str->buffer;
-       int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2);
+       char *destend;
+       const uchar *src;
+       size_t len;
+       register uint16 c;
 
-       nexti = (nexti+1)%8;
+       src = str->buffer;
+       len = MIN(str->buf_len, maxlen);
+       destend = dest + len;
 
-       for (p = lbuf; p-lbuf < max_size; p++, buf++)
+       while (dest < destend)
        {
-               if (*buf == 0)
-               {
-                       *p = ' ';
-               }
-               else
-               {
-                       *p = *buf;
-               }
+               c = *(src++);
+               *(dest++) = (char)c;
        }
 
-       *p = 0;
-       return lbuf;
+       *dest = 0;
 }
 
 /*******************************************************************
-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
+copies a UNISTR2 structure.
 ********************************************************************/
-#define MAXUNI 1024
-int struni2(uint16 *p, const char *buf)
+BOOL copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
 {
-       int len = 0;
-
-       if (p == NULL) return 0;
+       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;
 
-       if (buf != NULL)
+               /* copy the string */
+               memcpy(str->buffer, from->buffer, sizeof(from->buffer));
+       }
+       else
        {
-               for (; *buf && len < MAXUNI-2; len++, p++, buf++)
-               {
-                       *p = *buf;
-               }
+               str->uni_max_len = 1;
+               str->undoc = 0;
+               str->uni_str_len = 1;
+               str->buffer[0] = 0;
        }
 
-       *p = 0;
-
-       return len;
+       return True;
 }
 
 /*******************************************************************
-Return a ascii version of a unicode string
-Hack alert: uses fixed buffer(s) and only handles ascii strings
+duplicates a UNISTR2 structure.
 ********************************************************************/
-#define MAXUNI 1024
-char *unistr(char *buf)
+UNISTR2 *unistr2_dup(const UNISTR2 *name)
 {
-       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;
+       UNISTR2 *copy = (UNISTR2*)malloc(sizeof(*copy));
+       copy_unistr2(copy, name);
+       return copy;
 }
 
-
 /*******************************************************************
-strcpy for unicode strings.  returns length (in num of wide chars)
+frees a UNISTR2 structure.
 ********************************************************************/
-int unistrcpy(char *dst, char *src)
+void unistr2_free(UNISTR2 *name)
 {
-       int num_wchars = 0;
-
-       while (*src)
-       {
-               *dst++ = *src++;
-               *dst++ = *src++;
-               num_wchars++;
-       }
-       *dst++ = 0;
-       *dst++ = 0;
-
-       return num_wchars;
+       free(name);
 }
-