Cleanup size_t return values in callers of convert_string_allocate
[metze/samba/wip.git] / source3 / lib / util_unistr.c
index 45f09da85bc63b8225287a8ff5bbfcdc676f747f..76235ad041738e27c2385095f965e3a0ff940927 100644 (file)
@@ -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
-*/
-
-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
- **/
-
-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 <tt>valid.dat</tt> or
  * create from the configured codepage.
@@ -245,22 +195,24 @@ 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);
        }
-       
+
+       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);
        }
 }
 
@@ -360,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;
        }
 
@@ -408,11 +358,15 @@ 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)
 {
-       return push_ucs2_talloc(ctx, dest, src);
+       size_t size;
+       if (push_ucs2_talloc(ctx, dest, src, &size))
+               return size;
+       else
+               return -1;
 }
 
 /*******************************************************************
@@ -428,6 +382,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,6 +396,30 @@ 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
+
+/*******************************************************************
+ Duplicate a UNISTR2 string into a null terminated char*
+ using a talloc context.
+********************************************************************/
+
+char *unistr2_to_ascii_talloc(TALLOC_CTX *ctx, const UNISTR2 *str)
+{
+       char *s = NULL;
+
+       if (!str || !str->buffer) {
+               return NULL;
+       }
+       if (pull_ucs2_base_talloc(ctx,
+                               NULL,
+                               &s,
+                               str->buffer,
+                               str->uni_str_len*2,
+                               STR_NOALIGN) == (size_t)-1) {
+               return NULL;
+       }
+       return s;
+}
 
 /*******************************************************************
  Return a string for displaying a UNISTR2. Guarentees to return a
@@ -450,46 +429,20 @@ void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen)
 
 const char *unistr2_static(const UNISTR2 *str)
 {
-       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) {
+       dest = unistr2_to_ascii_talloc(talloc_tos(), str);
+       if (!dest) {
                return "";
        }
 
        return dest;
 }
 
-/*******************************************************************
- 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;
-}
-
 /*******************************************************************
  Convert a wchar to upper case.
 ********************************************************************/