fixed, moved and added some functions
authorSimo Sorce <idra@samba.org>
Sat, 10 Nov 2001 15:21:54 +0000 (15:21 +0000)
committerSimo Sorce <idra@samba.org>
Sat, 10 Nov 2001 15:21:54 +0000 (15:21 +0000)
note the useful acnv_uxu2 and acnv_u2ux functions in charcnv.c
(This used to be commit 64dde3b64fc091cda95fc4ed145595b5d79b2e01)

source3/lib/charcnv.c
source3/lib/util_unistr.c
source3/smbd/mangle.c

index 2929233388afc0778c67575f48e4184a8e9afd6d..59a2af72a495ef6259059151d3bfff8a6afbe710 100644 (file)
@@ -171,7 +171,7 @@ size_t convert_string_allocate(charset_t from, charset_t to,
                return -1;
        }
 
-       destlen = MAX(srclen, 1024);
+       destlen = MAX(srclen, 512);
        outbuf = NULL;
 convert:
        destlen = destlen * 2;
@@ -208,15 +208,13 @@ convert:
        }
        
        destlen = destlen - o_len;
-       *dest = (char *)malloc(destlen);
+       *dest = (char *)realloc(outbuf,destlen);
        if (!*dest) {
                DEBUG(0, ("convert_string_allocate: out of memory!\n"));
                free(outbuf);
                return -1;
        }
-       memcpy(*dest, outbuf, destlen);
-       free(outbuf);
-       
+
        return destlen;
 }
 
@@ -478,3 +476,41 @@ int align_string(const void *base_ptr, const char *p, int flags)
        }
        return 0;
 }
+
+
+
+/****************************************************************************
+convert from ucs2 to unix charset and return the
+allocated and converted string or NULL if an error occurred.
+you must provide a zero terminated string.
+the returning string will be zero terminated.
+****************************************************************************/
+char *acnv_u2ux(const smb_ucs2_t *src)
+{
+       size_t slen;
+       size_t dlen;
+       void *dest;
+       
+       slen = strlen_w(src) + 1;
+       dlen = convert_string_allocate(CH_UCS2, CH_UNIX, src, slen, &dest);
+       if (dlen == -1) return NULL;
+       else return dest;
+}
+
+/****************************************************************************
+convert from ucs2 to unix charset and return the
+allocated and converted string or NULL if an error occurred.
+you must provide a zero terminated string.
+the returning string will be zero terminated.
+****************************************************************************/
+smb_ucs2_t *acnv_uxu2(const char *src)
+{
+       size_t slen;
+       size_t dlen;
+       void *dest;
+       
+       slen = strlen(src) + 1;
+       dlen = convert_string_allocate(CH_UNIX, CH_UCS2, src, slen, &dest);
+       if (dlen == -1) return NULL;
+       else return dest;
+}
index 8a7253a71884765172e227cd45281a0d827be5f9..08281fec4f2ffe11509e95cb60c0880e57491ebb 100644 (file)
@@ -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,
index aa5c49c1263bc321b918409b4d27700e9165537c..44ffd155e42f8502f68a7bb5957d0b1559957609 100644 (file)
@@ -253,7 +253,7 @@ smb_ucs2_t *mangle(const smb_ucs2_t *unmangled)
 
        /* if it is a valid 8_3 do not mangle again */
        if (NT_STATUS_IS_OK(is_8_3_w(unmangled)))
-               return strdup_w(unmangled);
+               return NULL;
 
        if (NT_STATUS_IS_ERR(mangle_get_prefix(unmangled, &umpref, &ext)))
                return NULL;