Fix from TAKAHASHI Motonobu <monyo@samba.gr.jp> for multibyte conversion
authorJeremy Allison <jra@samba.org>
Tue, 12 Jun 2001 18:20:26 +0000 (18:20 +0000)
committerJeremy Allison <jra@samba.org>
Tue, 12 Jun 2001 18:20:26 +0000 (18:20 +0000)
problems.
Jeremy.
(This used to be commit 24eea8a309ff0151277b9537a5c00321041e70d3)

source3/lib/util_unistr.c

index 18f3b54bf71270e2bdd00e0cfa3227549944bb6e..f6bb7e806810546a8f8c96933217e2d6e730acda 100644 (file)
@@ -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.
 ********************************************************************/