r25027: Fix more warnings.
[jelmer/samba4-debian.git] / source / lib / charset / charcnv.c
index d1d4561fa5ead9b1b2e7906c5b9d9163ab87c787..ca96277679d99a014b198d3fa3bc69a9a567a8ab 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 #include "includes.h"
 #include "system/iconv.h"
+#include "param/param.h"
 
 /**
  * @file
  * @sa lib/iconv.c
  */
 
+char *unix_charset = NULL;
+char *dos_charset = NULL;
+char *display_charset = NULL;
+
 /**
  * Return the name of a charset to give to iconv().
  **/
 static const char *charset_name(charset_t ch)
 {
-       const char *ret = NULL;
-
-       if (ch == CH_UTF16) ret = "UTF-16LE";
-       else if (ch == CH_UNIX) ret = lp_unix_charset();
-       else if (ch == CH_DOS) ret = lp_dos_charset();
-       else if (ch == CH_DISPLAY) ret = lp_display_charset();
-       else if (ch == CH_UTF8) ret = "UTF8";
-       else if (ch == CH_UTF16BE) ret = "UTF-16BE";
-
-       if (!ret || !*ret) ret = "ASCII";
-       return ret;
+       switch (ch) {
+       case CH_UTF16: return "UTF-16LE";
+       case CH_UNIX: return unix_charset;
+       case CH_DOS: return dos_charset;
+       case CH_DISPLAY: return display_charset;
+       case CH_UTF8: return "UTF8";
+       case CH_UTF16BE: return "UTF-16BE";
+       default:
+       return "ASCII";
+       }
 }
 
 static smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
@@ -149,7 +152,7 @@ _PUBLIC_ ssize_t convert_string(charset_t from, charset_t to,
        smb_iconv_t descriptor;
 
        if (srclen == (size_t)-1)
-               srclen = strlen(src)+1;
+               srclen = strlen(inbuf)+1;
 
        descriptor = get_conv_handle(from, to);
 
@@ -287,7 +290,7 @@ convert:
  * @param dest_len the maximum length in bytes allowed in the
  * destination.  If @p dest_len is -1 then no maximum is used.
  **/
-_PUBLIC_ ssize_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
+static ssize_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
 {
        size_t src_len;
        ssize_t ret;
@@ -322,7 +325,6 @@ _PUBLIC_ ssize_t push_ascii(void *dest, const char *src, size_t dest_len, int fl
 _PUBLIC_ ssize_t push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
 {
        size_t src_len = strlen(src)+1;
-
        *dest = NULL;
        return convert_string_talloc(ctx, CH_UNIX, CH_DOS, src, src_len, (void **)dest);
 }
@@ -343,15 +345,15 @@ _PUBLIC_ ssize_t push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src
  * @param src_len is the length of the source area in bytes.
  * @returns the number of bytes occupied by the string in @p src.
  **/
-_PUBLIC_ ssize_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
+static ssize_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
 {
        size_t ret;
 
        if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) {
                if (src_len == (size_t)-1) {
-                       src_len = strlen(src) + 1;
+                       src_len = strlen((const char *)src) + 1;
                } else {
-                       size_t len = strnlen(src, src_len);
+                       size_t len = strnlen((const char *)src, src_len);
                        if (len < src_len)
                                len++;
                        src_len = len;
@@ -382,7 +384,7 @@ _PUBLIC_ ssize_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t
  * @param dest_len is the maximum length allowed in the
  * destination. If dest_len is -1 then no maxiumum is used.
  **/
-_PUBLIC_ ssize_t push_ucs2(void *dest, const char *src, size_t dest_len, int flags)
+static ssize_t push_ucs2(void *dest, const char *src, size_t dest_len, int flags)
 {
        size_t len=0;
        size_t src_len = strlen(src);
@@ -450,7 +452,6 @@ _PUBLIC_ ssize_t push_ucs2_talloc(TALLOC_CTX *ctx, void **dest, const char *src)
 _PUBLIC_ ssize_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
 {
        size_t src_len = strlen(src)+1;
-
        *dest = NULL;
        return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void **)dest);
 }
@@ -466,7 +467,7 @@ _PUBLIC_ ssize_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
  The resulting string in "dest" is always null terminated.
 **/
 
-_PUBLIC_ size_t pull_ucs2(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
+static size_t pull_ucs2(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
 {
        size_t ret;
 
@@ -495,6 +496,21 @@ _PUBLIC_ size_t pull_ucs2(char *dest, const void *src, size_t dest_len, size_t s
        return src_len;
 }
 
+/**
+ * Copy a string from a ASCII src to a unix char * destination, allocating a buffer using talloc
+ *
+ * @param dest always set at least to NULL 
+ *
+ * @returns The number of bytes occupied by the string in the destination
+ **/
+
+_PUBLIC_ ssize_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
+{
+       size_t src_len = strlen(src)+1;
+       *dest = NULL;
+       return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest);
+}
+
 /**
  * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
  *
@@ -618,11 +634,11 @@ _PUBLIC_ codepoint_t next_codepoint(const char *str, size_t *size)
        /* this looks a little strange, but it is needed to cope
           with codepoints above 64k */
        olen = 2;
-       outbuf = buf;
+       outbuf = (char *)buf;
        smb_iconv(descriptor,  &str, &ilen, &outbuf, &olen);
        if (olen == 2) {
                olen = 4;
-               outbuf = buf;
+               outbuf = (char *)buf;
                smb_iconv(descriptor,  &str, &ilen, &outbuf, &olen);
                if (olen == 4) {
                        /* we didn't convert any bytes */
@@ -680,7 +696,7 @@ _PUBLIC_ ssize_t push_codepoint(char *str, codepoint_t c)
        if (c < 0x10000) {
                ilen = 2;
                olen = 5;
-               inbuf = buf;
+               inbuf = (char *)buf;
                SSVAL(buf, 0, c);
                smb_iconv(descriptor, &inbuf, &ilen, &str, &olen);
                if (ilen != 0) {
@@ -698,7 +714,7 @@ _PUBLIC_ ssize_t push_codepoint(char *str, codepoint_t c)
 
        ilen = 4;
        olen = 5;
-       inbuf = buf;
+       inbuf = (char *)buf;
 
        smb_iconv(descriptor, &inbuf, &ilen, &str, &olen);
        if (ilen != 0) {