Change convert_string_internal() and convert_string_error() to bool return.
authorJeremy Allison <jra@samba.org>
Wed, 30 Mar 2011 17:27:04 +0000 (10:27 -0700)
committerJeremy Allison <jra@samba.org>
Wed, 30 Mar 2011 18:58:10 +0000 (20:58 +0200)
Move closer to makeing all convert_string_XXX functions return bool.

Autobuild-User: Jeremy Allison <jra@samba.org>
Autobuild-Date: Wed Mar 30 20:58:10 CEST 2011 on sn-devel-104

source3/include/proto.h
source3/lib/charcnv.c
source3/lib/fstring.c

index c19e3a45f74502ec0c6f64d92e47bf9b902b363c..94c924591ffbeeb0f4eb0e465dee1e9a2f9e67f6 100644 (file)
@@ -77,7 +77,7 @@ void init_iconv(void);
 size_t convert_string(charset_t from, charset_t to,
                      void const *src, size_t srclen, 
                      void *dest, size_t destlen);
-size_t convert_string_error(charset_t from, charset_t to,
+bool convert_string_error(charset_t from, charset_t to,
                            void const *src, size_t srclen,
                            void *dest, size_t destlen,
                            size_t *converted_size);
index 74685408d4adccf6d56fb02f5becf90c5ffdbbcb..76fa968457142f6eb66abda148ca55b4451e86a9 100644 (file)
@@ -80,13 +80,15 @@ void init_iconv(void)
  * @param srclen length of the source string in bytes
  * @param dest pointer to destination string (multibyte or singlebyte)
  * @param destlen maximal length allowed for string
- * @returns the number of bytes occupied in the destination
+ * @param converted size is the number of bytes occupied in the destination
+ *
+ * @returns false and sets errno on fail, true on success.
  *
  * Ensure the srclen contains the terminating zero.
  *
  **/
 
-static size_t convert_string_internal(charset_t from, charset_t to,
+static bool convert_string_internal(charset_t from, charset_t to,
                      void const *src, size_t srclen, 
                      void *dest, size_t destlen, size_t *converted_size)
 {
@@ -112,16 +114,18 @@ static size_t convert_string_internal(charset_t from, charset_t to,
 
        if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
                errno = EINVAL;
-               return (size_t)-1;
+               return false;
        }
 
        i_len=srclen;
        o_len=destlen;
 
        retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
-       if (converted_size != NULL)
-               *converted_size = destlen-o_len;
-       return retval;
+       if (retval == (size_t)-1) {
+               return false;
+       }
+       *converted_size = destlen-o_len;
+       return true;
 }
 
 /**
@@ -132,7 +136,9 @@ static size_t convert_string_internal(charset_t from, charset_t to,
  * @param srclen length of the source string in bytes, or -1 for nul terminated.
  * @param dest pointer to destination string (multibyte or singlebyte)
  * @param destlen maximal length allowed for string - *NEVER* -1.
- * @returns the number of bytes occupied in the destination
+ * @param converted size is the number of bytes occupied in the destination
+ *
+ * @returns false and sets errno on fail, true on success.
  *
  * Ensure the srclen contains the terminating zero.
  *
@@ -140,7 +146,7 @@ static size_t convert_string_internal(charset_t from, charset_t to,
  * Don't change unless you really know what you are doing. JRA.
  **/
 
-size_t convert_string_error(charset_t from, charset_t to,
+bool convert_string_error(charset_t from, charset_t to,
                            void const *src, size_t srclen,
                            void *dest, size_t destlen,
                            size_t *converted_size)
@@ -155,13 +161,11 @@ size_t convert_string_error(charset_t from, charset_t to,
        SMB_ASSERT(destlen != (size_t)-1);
 #endif
 
-       if (converted_size) {
+       if (srclen == 0) {
                *converted_size = 0;
+               return true;
        }
 
-       if (srclen == 0)
-               return 0;
-
        if (from != CH_UTF16LE && from != CH_UTF16BE && to != CH_UTF16LE && to != CH_UTF16BE) {
                const unsigned char *p = (const unsigned char *)src;
                unsigned char *q = (unsigned char *)dest;
@@ -185,26 +189,24 @@ size_t convert_string_error(charset_t from, charset_t to,
 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
                                goto general_case;
 #else
-                               size_t ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size);
-                               if (converted_size) {
-                                       *converted_size += retval;
-                               }
+                               bool ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size);
+                               *converted_size += retval;
                                return ret;
 #endif
                        }
                }
-               if (converted_size) {
-                       *converted_size = retval;
-               }
+
+               *converted_size = retval;
+
                if (!dlen) {
                        /* Even if we fast path we should note if we ran out of room. */
                        if (((slen != (size_t)-1) && slen) ||
                                        ((slen == (size_t)-1) && lastp)) {
                                errno = E2BIG;
-                               return (size_t)-1;
+                               return false;
                        }
                }
-               return retval;
+               return true;
        } else if (from == CH_UTF16LE && to != CH_UTF16LE) {
                const unsigned char *p = (const unsigned char *)src;
                unsigned char *q = (unsigned char *)dest;
@@ -229,26 +231,24 @@ size_t convert_string_error(charset_t from, charset_t to,
 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
                                goto general_case;
 #else
-                               size_t ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size);
-                               if (converted_size) {
-                                       *converted_size += retval;
-                               }
+                               bool ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size);
+                               *converted_size += retval;
                                return ret;
 #endif
                        }
                }
-               if (converted_size) {
-                       *converted_size = retval;
-               }
+
+               *converted_size = retval;
+
                if (!dlen) {
                        /* Even if we fast path we should note if we ran out of room. */
                        if (((slen != (size_t)-1) && slen) ||
                                        ((slen == (size_t)-1) && lastp)) {
                                errno = E2BIG;
-                               return (size_t)-1;
+                               return false;
                        }
                }
-               return retval;
+               return true;
        } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
                const unsigned char *p = (const unsigned char *)src;
                unsigned char *q = (unsigned char *)dest;
@@ -273,26 +273,24 @@ size_t convert_string_error(charset_t from, charset_t to,
 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
                                goto general_case;
 #else
-                               size_t ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size);
-                               if (converted_size) {
-                                       *converted_size += retval;
-                               }
+                               bool ret = convert_string_internal(from, to, p, slen, q, dlen, converted_size);
+                               *converted_size += retval;
                                return ret;
 #endif
                        }
                }
-               if (converted_size) {
-                       *converted_size = retval;
-               }
+
+               *converted_size = retval;
+
                if (!dlen) {
                        /* Even if we fast path we should note if we ran out of room. */
                        if (((slen != (size_t)-1) && slen) ||
                                        ((slen == (size_t)-1) && lastp)) {
                                errno = E2BIG;
-                               return (size_t)-1;
+                               return false;
                        }
                }
-               return retval;
+               return true;
        }
 
 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
@@ -303,17 +301,19 @@ size_t convert_string_error(charset_t from, charset_t to,
 
 size_t convert_string(charset_t from, charset_t to,
                      void const *src, size_t srclen,
-                     void *dest, size_t destlen) {
+                     void *dest, size_t destlen)
+{
        size_t converted_size;
-       size_t retval = convert_string_error(from, to, src, srclen, dest, destlen, &converted_size);
-       if(retval==(size_t)-1) {
+       bool ret = convert_string_error(from, to, src, srclen, dest, destlen, &converted_size);
+
+       if(ret==false) {
                const char *reason="unknown error";
                switch(errno) {
                        case EINVAL:
                                reason="Incomplete multibyte sequence";
                                DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",
                                         reason, (const char *)src));
-                               return (size_t)-1;
+                               break;
                        case E2BIG:
                        {
                                struct smb_iconv_handle *ic;
@@ -336,15 +336,15 @@ size_t convert_string(charset_t from, charset_t to,
                                reason="Illegal multibyte sequence";
                                DEBUG(3,("convert_string_internal: Conversion error: %s(%s)\n",
                                         reason, (const char *)src));
-                               return (size_t)-1;
+                               break;
                        default:
                                DEBUG(0,("convert_string_internal: Conversion error: %s(%s)\n",
                                         reason, (const char *)src));
-                               return (size_t)-1;
+                               break;
                }
                /* smb_panic(reason); */
        }
-       return converted_size;
+       return ret ? converted_size : (size_t)-1;
 }
 
 
@@ -380,11 +380,6 @@ bool convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
 
        *dest = NULL;
 
-       if (!converted_size) {
-               errno = EINVAL;
-               return false;
-       }
-
        if (src == NULL || srclen == (size_t)-1) {
                errno = EINVAL;
                return false;
index 74c2138975e3a51d77d0e2d2357d5328b91152b5..50b0765f929d912d78c3b8d995c90b201da6be00 100644 (file)
@@ -35,17 +35,19 @@ size_t push_ascii_fstring(void *dest, const char *src)
  this function uses convert_string_error() to avoid common debug
  warnings where is unable to convert strings to CH_DOS. The target
  string is truncated at the first character that cannot be converted
- The target is always null terminated. The resulting string size,
- without the null termination, it returned
+ The target is always null terminated.
 ********************************************************************/
 
 size_t push_ascii_nstring(void *dest, const char *src)
 {
        size_t converted_size = 0;
-       size_t ret;
-       ret = convert_string_error(CH_UNIX, CH_DOS, src, -1, dest, sizeof(nstring), &converted_size);
-       SCVAL(dest, sizeof(nstring)-1, 0);
-       return ret;
+       bool ret = convert_string_error(CH_UNIX, CH_DOS, src, -1, dest, sizeof(nstring), &converted_size);
+       if (ret) {
+               SCVAL(dest, sizeof(nstring)-1, 0);
+       } else {
+               SCVAL(dest, 0, 0);
+       }
+       return ret ? converted_size : (size_t)-1;
 }
 
 size_t pull_ascii_fstring(char *dest, const void *src)