Doxygen janitor and some doc typo fixes.
[samba.git] / source3 / lib / charcnv.c
index a5355aa7c88b1c42c803b5cdecc9e6a3d99a5fbd..084b0e0c90c16a192674ed0b49ae9ecd9dcf3fa3 100644 (file)
@@ -1,8 +1,9 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    Character set conversion Extensions
-   Copyright (C) Andrew Tridgell 1992-1998
+   Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
+   Copyright (C) Andrew Tridgell 2001
+   Copyright (C) Simo Sorce 2001
    
    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
 
 */
 #include "includes.h"
-#define CTRLZ 26
-extern int DEBUGLEVEL;
 
-static char cvtbuf[sizeof(pstring)];
+static pstring cvtbuf;
 
-static BOOL mapsinited = 0;
+static smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
 
-static char unix2dos[256];
-static char dos2unix[256];
+/**
+ * @file
+ *
+ * Character set conversion routines.
+ *
+ * @sa lib/iconv.c
+ **/
 
-static void initmaps(void) {
-    int k;
 
-    for (k = 0; k < 256; k++) unix2dos[k] = k;
-    for (k = 0; k < 256; k++) dos2unix[k] = k;
+/**
+ * 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_UCS2) ret = "UCS-2LE";
+       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";
+
+       if (!ret || !*ret) ret = "ASCII";
+       return ret;
+}
+
+static void lazy_initialize_conv(void)
+{
+       static int initialized = False;
+
+       if (!initialized) {
+               initialized = True;
+               load_case_tables();
+               init_iconv();
+               init_valid_table();
+       }
+}
+
+/**
+ Initialize iconv conversion descriptors.
+**/
+
+void init_iconv(void)
+{
+       int c1, c2;
+       BOOL did_reload = False;
+
+       /* so that charset_name() works we need to get the UNIX<->UCS2 going
+          first */
+       if (!conv_handles[CH_UNIX][CH_UCS2])
+               conv_handles[CH_UNIX][CH_UCS2] = smb_iconv_open("UCS-2LE", "ASCII");
+
+       if (!conv_handles[CH_UCS2][CH_UNIX])
+               conv_handles[CH_UCS2][CH_UNIX] = smb_iconv_open("ASCII", "UCS-2LE");
+
+       for (c1=0;c1<NUM_CHARSETS;c1++) {
+               for (c2=0;c2<NUM_CHARSETS;c2++) {
+                       const char *n1 = charset_name((charset_t)c1);
+                       const char *n2 = charset_name((charset_t)c2);
+                       if (conv_handles[c1][c2] &&
+                           strcmp(n1, conv_handles[c1][c2]->from_name) == 0 &&
+                           strcmp(n2, conv_handles[c1][c2]->to_name) == 0)
+                               continue;
+
+                       did_reload = True;
+
+                       if (conv_handles[c1][c2])
+                               smb_iconv_close(conv_handles[c1][c2]);
+
+                       conv_handles[c1][c2] = smb_iconv_open(n2,n1);
+                       if (conv_handles[c1][c2] == (smb_iconv_t)-1) {
+                               DEBUG(0,("Conversion from %s to %s not supported\n",
+                                        charset_name((charset_t)c1), charset_name((charset_t)c2)));
+                               conv_handles[c1][c2] = NULL;
+                       }
+               }
+       }
+
+       if (did_reload) {
+               init_valid_table();
+       }
+}
+
+/**
+ * Convert string from one encoding to another, making error checking etc
+ *
+ * @param descriptor conversion descriptor, created in init_iconv()
+ * @param src pointer to source string (multibyte or singlebyte)
+ * @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
+ **/
+
+size_t convert_string(charset_t from, charset_t to,
+                     void const *src, size_t srclen, 
+                     void *dest, size_t destlen)
+{
+       size_t i_len, o_len;
+       size_t retval;
+       const char* inbuf = (const char*)src;
+       char* outbuf = (char*)dest;
+       smb_iconv_t descriptor;
+
+       if (srclen == (size_t)-1)
+               srclen = strlen(src)+1;
+
+       lazy_initialize_conv();
+
+       descriptor = conv_handles[from][to];
+
+       if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
+               /* conversion not supported, use as is */
+               size_t len = MIN(srclen,destlen);
+               memcpy(dest,src,len);
+               return len;
+       }
+
+       i_len=srclen;
+       o_len=destlen;
+       retval = smb_iconv(descriptor,  &inbuf, &i_len, &outbuf, &o_len);
+       if(retval==(size_t)-1) {
+               const char *reason="unknown error";
+               switch(errno) {
+                       case EINVAL:
+                               reason="Incomplete multibyte sequence";
+                               break;
+                       case E2BIG:
+                               reason="No more room"; 
+                               DEBUG(0, ("convert_string: Required %d, available %d\n",
+                                       srclen, destlen));
+                               /* we are not sure we need srclen bytes,
+                                 may be more, may be less.
+                                 We only know we need more than destlen
+                                 bytes ---simo */
+                              break;
+                       case EILSEQ:
+                              reason="Illegal multibyte sequence";
+                              break;
+               }
+               /* smb_panic(reason); */
+       }
+       return destlen-o_len;
+}
+
+/**
+ * Convert between character sets, allocating a new buffer for the result.
+ *
+ * @param srclen length of source buffer.
+ * @param dest always set at least to NULL
+ * @note -1 is not accepted for srclen.
+ *
+ * @returns Size in bytes of the converted string; or -1 in case of error.
+ **/
+
+size_t convert_string_allocate(charset_t from, charset_t to,
+                               void const *src, size_t srclen, void **dest)
+{
+       size_t i_len, o_len, destlen;
+       size_t retval;
+       const char *inbuf = (const char *)src;
+       char *outbuf, *ob;
+       smb_iconv_t descriptor;
+
+       *dest = NULL;
+
+       if (src == NULL || srclen == (size_t)-1)
+               return (size_t)-1;
+
+       lazy_initialize_conv();
+
+       descriptor = conv_handles[from][to];
+
+       if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
+               /* conversion not supported, return -1*/
+               DEBUG(3, ("convert_string_allocate: conversion not supported!\n"));
+               return -1;
+       }
+
+       destlen = MAX(srclen, 512);
+       outbuf = NULL;
+convert:
+       destlen = destlen * 2;
+       ob = (char *)realloc(outbuf, destlen);
+       if (!ob) {
+               DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
+               SAFE_FREE(outbuf);
+               return (size_t)-1;
+       }
+       else
+               outbuf = ob;
+       i_len = srclen;
+       o_len = destlen;
+       retval = smb_iconv(descriptor,
+                          &inbuf, &i_len,
+                          &outbuf, &o_len);
+       if(retval == (size_t)-1)                {
+               const char *reason="unknown error";
+               switch(errno) {
+                       case EINVAL:
+                               reason="Incomplete multibyte sequence";
+                               break;
+                       case E2BIG:
+                               goto convert;           
+                       case EILSEQ:
+                               reason="Illegal multibyte sequence";
+                               break;
+               }
+               DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
+               /* smb_panic(reason); */
+               return (size_t)-1;
+       }
+       
+       destlen = destlen - o_len;
+       *dest = (char *)Realloc(ob,destlen);
+       if (!*dest) {
+               DEBUG(0, ("convert_string_allocate: out of memory!\n"));
+               SAFE_FREE(ob);
+               return (size_t)-1;
+       }
+
+       return destlen;
+}
+
+
+/**
+ * Convert between character sets, allocating a new buffer using talloc for the result.
+ *
+ * @param srclen length of source buffer.
+ * @param dest always set at least to NULL 
+ * @note -1 is not accepted for srclen.
+ *
+ * @returns Size in bytes of the converted string; or -1 in case of error.
+ **/
+size_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
+                               void const *src, size_t srclen, void **dest)
+{
+       void *alloced_string;
+       size_t dest_len;
+
+       *dest = NULL;
+       dest_len=convert_string_allocate(from, to, src, srclen, &alloced_string);
+       if (dest_len == (size_t)-1)
+               return (size_t)-1;
+       *dest = talloc_memdup(ctx, alloced_string, dest_len);
+       SAFE_FREE(alloced_string);
+       if (*dest == NULL)
+               return (size_t)-1;
+       return dest_len;
+}
+
+size_t unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
+{
+       size_t size;
+       smb_ucs2_t *buffer=(smb_ucs2_t*)cvtbuf;
+       size=convert_string(CH_UNIX, CH_UCS2, src, srclen, buffer, sizeof(cvtbuf));
+       if (!strupper_w(buffer) && (dest == src))
+               return srclen;
+       return convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen);
+}
+
+size_t unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
+{
+       size_t size;
+       smb_ucs2_t *buffer=(smb_ucs2_t*)cvtbuf;
+       size=convert_string(CH_UNIX, CH_UCS2, src, srclen, buffer, sizeof(cvtbuf));
+       if (!strlower_w(buffer) && (dest == src))
+               return srclen;
+       return convert_string(CH_UCS2, CH_UNIX, buffer, size, dest, destlen);
+}
+
+
+size_t ucs2_align(const void *base_ptr, const void *p, int flags)
+{
+       if (flags & (STR_NOALIGN|STR_ASCII))
+               return 0;
+       return PTR_DIFF(p, base_ptr) & 1;
+}
+
+
+/**
+ * Copy a string from a char* unix src to a dos codepage string destination.
+ *
+ * @return the number of bytes occupied by the string in the destination.
+ *
+ * @param flags can include
+ * <dl>
+ * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
+ * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
+ * </dl>
+ *
+ * @param dest_len the maximum length in bytes allowed in the
+ * destination.  If @p dest_len is -1 then no maximum is used.
+ **/
+size_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
+{
+       size_t src_len = strlen(src);
+       pstring tmpbuf;
+
+       /* treat a pstring as "unlimited" length */
+       if (dest_len == (size_t)-1)
+               dest_len = sizeof(pstring);
+
+       if (flags & STR_UPPER) {
+               pstrcpy(tmpbuf, src);
+               strupper(tmpbuf);
+               src = tmpbuf;
+       }
+
+       if (flags & STR_TERMINATE)
+               src_len++;
+
+       return convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len);
+}
+
+size_t push_ascii_fstring(void *dest, const char *src)
+{
+       return push_ascii(dest, src, sizeof(fstring), STR_TERMINATE);
+}
+
+size_t push_ascii_pstring(void *dest, const char *src)
+{
+       return push_ascii(dest, src, sizeof(pstring), STR_TERMINATE);
+}
+
+size_t push_pstring(void *dest, const char *src)
+{
+       return push_ascii(dest, src, sizeof(pstring), STR_TERMINATE);
+}
+
+/**
+ * Copy a string from a dos codepage source to a unix char* destination.
+ *
+ * The resulting string in "dest" is always null terminated.
+ *
+ * @param flags can have:
+ * <dl>
+ * <dt>STR_TERMINATE</dt>
+ * <dd>STR_TERMINATE means the string in @p src
+ * is null terminated, and src_len is ignored.</dd>
+ * </dl>
+ *
+ * @param src_len is the length of the source area in bytes.
+ * @returns the number of bytes occupied by the string in @p src.
+ **/
+size_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
+{
+       size_t ret;
+
+       if (dest_len == (size_t)-1)
+               dest_len = sizeof(pstring);
+
+       if (flags & STR_TERMINATE) {
+               if (src_len == (size_t)-1) {
+                       src_len = strlen(src) + 1;
+               } else {
+                       size_t len = strnlen(src, src_len);
+                       if (len < src_len)
+                               len++;
+                       src_len = len;
+               }
+       }
+
+       ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len);
+
+       if (dest_len)
+               dest[MIN(ret, dest_len-1)] = 0;
+
+       return src_len;
+}
+
+size_t pull_ascii_pstring(char *dest, const void *src)
+{
+       return pull_ascii(dest, src, sizeof(pstring), -1, STR_TERMINATE);
+}
+
+size_t pull_ascii_fstring(char *dest, const void *src)
+{
+       return pull_ascii(dest, src, sizeof(fstring), -1, STR_TERMINATE);
+}
+
+/**
+ * Copy a string from a char* src to a unicode destination.
+ *
+ * @returns the number of bytes occupied by the string in the destination.
+ *
+ * @param flags can have:
+ *
+ * <dl>
+ * <dt>STR_TERMINATE <dd>means include the null termination.
+ * <dt>STR_UPPER     <dd>means uppercase in the destination.
+ * <dt>STR_NOALIGN   <dd>means don't do alignment.
+ * </dl>
+ *
+ * @param dest_len is the maximum length allowed in the
+ * destination. If dest_len is -1 then no maxiumum is used.
+ **/
+size_t push_ucs2(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
+{
+       size_t len=0;
+       size_t src_len = strlen(src);
+       pstring tmpbuf;
+
+       /* treat a pstring as "unlimited" length */
+       if (dest_len == (size_t)-1)
+               dest_len = sizeof(pstring);
+
+       if (flags & STR_UPPER) {
+               pstrcpy(tmpbuf, src);
+               strupper(tmpbuf);
+               src = tmpbuf;
+       }
+
+       if (flags & STR_TERMINATE)
+               src_len++;
+
+       if (ucs2_align(base_ptr, dest, flags)) {
+               *(char *)dest = 0;
+               dest = (void *)((char *)dest + 1);
+               if (dest_len) dest_len--;
+               len++;
+       }
+
+       /* ucs2 is always a multiple of 2 bytes */
+       dest_len &= ~1;
+
+       len += convert_string(CH_UNIX, CH_UCS2, src, src_len, dest, dest_len);
+       return len;
+}
+
 
-    mapsinited = True;
+/**
+ * Copy a string from a unix char* src to a UCS2 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
+ *         or -1 in case of error.
+ **/
+size_t push_ucs2_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
+{
+       size_t src_len = strlen(src)+1;
+
+       *dest = NULL;
+       return convert_string_talloc(ctx, CH_UNIX, CH_UCS2, src, src_len, (void **)dest);
+}
+
+
+/**
+ * Copy a string from a unix char* src to a UCS2 destination, allocating a buffer
+ *
+ * @param dest always set at least to NULL 
+ *
+ * @returns The number of bytes occupied by the string in the destination
+ *         or -1 in case of error.
+ **/
+
+size_t push_ucs2_allocate(smb_ucs2_t **dest, const char *src)
+{
+       size_t src_len = strlen(src)+1;
+
+       *dest = NULL;
+       return convert_string_allocate(CH_UNIX, CH_UCS2, src, src_len, (void **)dest);  
+}
+
+/**
+ Copy a string from a char* src to a UTF-8 destination.
+ Return the number of bytes occupied by the string in the destination
+ Flags can have:
+  STR_TERMINATE means include the null termination
+  STR_UPPER     means uppercase in the destination
+ dest_len is the maximum length allowed in the destination. If dest_len
+ is -1 then no maxiumum is used.
+**/
+
+size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
+{
+       size_t src_len = strlen(src);
+       pstring tmpbuf;
+
+       /* treat a pstring as "unlimited" length */
+       if (dest_len == (size_t)-1)
+               dest_len = sizeof(pstring);
+
+       if (flags & STR_UPPER) {
+               pstrcpy(tmpbuf, src);
+               strupper(tmpbuf);
+               src = tmpbuf;
+       }
+
+       if (flags & STR_TERMINATE)
+               src_len++;
+
+       return convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len);
+}
+
+size_t push_utf8_fstring(void *dest, const char *src)
+{
+       return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
+}
+
+size_t push_utf8_pstring(void *dest, const char *src)
+{
+       return push_utf8(dest, src, sizeof(pstring), STR_TERMINATE);
+}
+
+/**
+ * Copy a string from a unix char* src to a UTF-8 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
+ **/
+
+size_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);
+}
+
+/**
+ * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer
+ *
+ * @param dest always set at least to NULL 
+ *
+ * @returns The number of bytes occupied by the string in the destination
+ **/
+
+size_t push_utf8_allocate(char **dest, const char *src)
+{
+       size_t src_len = strlen(src)+1;
+
+       *dest = NULL;
+       return convert_string_allocate(CH_UNIX, CH_UTF8, src, src_len, (void **)dest);  
+}
+
+/**
+ Copy a string from a ucs2 source to a unix char* destination.
+ Flags can have:
+  STR_TERMINATE means the string in src is null terminated.
+  STR_NOALIGN   means don't try to align.
+ if STR_TERMINATE is set then src_len is ignored if it is -1.
+ src_len is the length of the source area in bytes
+ Return the number of bytes occupied by the string in src.
+ The resulting string in "dest" is always null terminated.
+**/
+
+size_t pull_ucs2(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
+{
+       size_t ret;
+
+       if (dest_len == (size_t)-1)
+               dest_len = sizeof(pstring);
+
+       if (ucs2_align(base_ptr, src, flags)) {
+               src = (const void *)((const char *)src + 1);
+               if (src_len > 0)
+                       src_len--;
+       }
+
+       if (flags & STR_TERMINATE) {
+               if (src_len == (size_t)-1) {
+                       src_len = strlen_w(src)*2 + 2;
+               } else {
+                       size_t len = strnlen_w(src, src_len/2);
+                       if (len < src_len/2)
+                               len++;
+                       src_len = len*2;
+               }
+       }
+
+       /* ucs2 is always a multiple of 2 bytes */
+       if (src_len != (size_t)-1)
+               src_len &= ~1;
+       
+       ret = convert_string(CH_UCS2, CH_UNIX, src, src_len, dest, dest_len);
+       if (dest_len)
+               dest[MIN(ret, dest_len-1)] = 0;
+
+       return src_len;
 }
 
-static void update_map(char *str) {
-    char *p;
-
-    for (p = str; *p; p++) {
-        if (p[1]) {
-            unix2dos[(unsigned char)*p] = p[1];
-            dos2unix[(unsigned char)p[1]] = *p;
-            p++;
-        }
-    }
+size_t pull_ucs2_pstring(char *dest, const void *src)
+{
+       return pull_ucs2(NULL, dest, src, sizeof(pstring), -1, STR_TERMINATE);
 }
 
-static void setupmaps(void)
+size_t pull_ucs2_fstring(char *dest, const void *src)
 {
-    int i;
-    if (!mapsinited) initmaps();
+       return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
+}
 
-    /* Do not map undefined characters to some accidental code */
-    for (i = 128; i < 256; i++)
-    {
-       unix2dos[i] = CTRLZ;
-       dos2unix[i] = CTRLZ;
-    }
+/**
+ * Copy a string from a UCS2 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
+ **/
+
+size_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const smb_ucs2_t *src)
+{
+       size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
+       *dest = NULL;
+       return convert_string_talloc(ctx, CH_UCS2, CH_UNIX, src, src_len, (void **)dest);
 }
 
-static void init_iso8859_1(int codepage) {
-
-       setupmaps();
+/**
+ * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer
+ *
+ * @param dest always set at least to NULL 
+ *
+ * @returns The number of bytes occupied by the string in the destination
+ **/
 
-    if (codepage == 437) {
-        /* MSDOS Code Page 437 -> ISO-8859-1 */
-        update_map("\xA1\xAD\xA2\x98\xA3\x9C\xA4\xED\xA5\x9D\xA6\xB3\xA7\xEE");
-        update_map("\xAA\xA6\xAB\xAE\xAC\xAA\xAE\xE9\xAF\xC4");
-        update_map("\xB0\xF8\xB1\xF1\xB2\xFD\xB5\xE6\xB7\xFA\xBA\xA7\xBC\xAC\xBD\xAB\xBF\xA8");
-        update_map("\xC0\x85\xC1\xA0\xC2\x83\xC4\x8E\xC5\x8F\xC6\x92\xC7\x80\xC8\x8A");
-        update_map("\xC9\x90\xCA\x88\xCB\x89\xCC\x8D\xCD\xA1\xCE\x8C\xCF\x8B");
-        update_map("\xD1\xA5\xD2\x96\xD3\xA2\xD4\x93\xD6\x99\xD9\x97\xDA\xA3\xDB\x96\xDC\x9A\xDF\xE1");
-        update_map("\xE0\x85\xE1\xA0\xE2\x83\xE4\x84\xE5\x86\xE6\x91\xE7\x87\xE8\x8A\xE9\x82\xEA\x88\xEB\x89\xEC\x8D\xED\xA1\xEE\x8C\xEF\x8B");
-        update_map("\xF0\xEB\xF1\xA4\xF2\x95\xF3\xA2\xF4\x93\xF6\x94\xF7\xF6\xF8\xED\xF9\x97\xFA\xA3\xFB\x96\xFC\x81\xFF\x98");
-    } else {
-        /* MSDOS Code Page 850 -> ISO-8859-1 */
-        update_map("\240\377\241\255\242\275\243\234\244\317\245\276\246\335\247\365");
-        update_map("\250\371\251\270\252\246\253\256\254\252\255\360\256\251\257\356");
-        update_map("\260\370\261\361\262\375\263\374\264\357\265\346\266\364\267\372");
-        update_map("\270\367\271\373\272\247\273\257\274\254\275\253\276\363\277\250");
-        update_map("\300\267\301\265\302\266\303\307\304\216\305\217\306\222\307\200");
-        update_map("\310\324\311\220\312\322\313\323\314\336\315\326\316\327\317\330");
-        update_map("\320\321\321\245\322\343\323\340\324\342\325\345\326\231\327\236");
-        update_map("\330\235\331\353\332\351\333\352\334\232\335\355\336\350\337\341");
-        update_map("\340\205\341\240\342\203\343\306\344\204\345\206\346\221\347\207");
-        update_map("\350\212\351\202\352\210\353\211\354\215\355\241\356\214\357\213");
-        update_map("\360\320\361\244\362\225\363\242\364\223\365\344\366\224\367\366");
-        update_map("\370\233\371\227\372\243\373\226\374\201\375\354\376\347\377\230");
-    }
+size_t pull_ucs2_allocate(void **dest, const smb_ucs2_t *src)
+{
+       size_t src_len = (strlen_w(src)+1) * sizeof(smb_ucs2_t);
+       *dest = NULL;
+       return convert_string_allocate(CH_UCS2, CH_UNIX, src, src_len, dest);   
 }
 
-/* Init for eastern european languages. */
+/**
+ Copy a string from a utf-8 source to a unix char* destination.
+ Flags can have:
+  STR_TERMINATE means the string in src is null terminated.
+ if STR_TERMINATE is set then src_len is ignored.
+ src_len is the length of the source area in bytes
+ Return the number of bytes occupied by the string in src.
+ The resulting string in "dest" is always null terminated.
+**/
+
+size_t pull_utf8(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
+{
+       size_t ret;
+
+       if (dest_len == (size_t)-1)
+               dest_len = sizeof(pstring);
+
+       if (flags & STR_TERMINATE) {
+               if (src_len == (size_t)-1) {
+                       src_len = strlen(src) + 1;
+               } else {
+                       size_t len = strnlen(src, src_len);
+                       if (len < src_len)
+                               len++;
+                       src_len = len;
+               }
+       }
+
+       ret = convert_string(CH_UTF8, CH_UNIX, src, src_len, dest, dest_len);
+       if (dest_len)
+               dest[MIN(ret, dest_len-1)] = 0;
+
+       return src_len;
+}
 
-static void init_iso8859_2(void) {
+size_t pull_utf8_pstring(char *dest, const void *src)
+{
+       return pull_utf8(dest, src, sizeof(pstring), -1, STR_TERMINATE);
+}
 
-       setupmaps();
+size_t pull_utf8_fstring(char *dest, const void *src)
+{
+       return pull_utf8(dest, src, sizeof(fstring), -1, STR_TERMINATE);
+}
 
-/*
- * Tranlation table created by Petr Hubeny <psh@capitol.cz>
- * Requires client code page = 852
- * and character set = ISO8859-2 in smb.conf
- */
+/**
+ * Copy a string from a UTF-8 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
+ **/
 
-/* MSDOS Code Page 852 -> ISO-8859-2 */
-update_map("\240\377"); /* Fix for non-breaking space */
-update_map("\241\244\242\364\243\235\244\317\245\225\246\227\247\365");
-update_map("\250\371\251\346\252\270\253\233\254\215\256\246\257\275");
-update_map("\261\245\262\362\263\210\264\357\265\226\266\230\267\363");
-update_map("\270\367\271\347\272\255\273\234\274\253\275\361\276\247\277\276");
-update_map("\300\350\301\265\302\266\303\306\304\216\305\221\306\217\307\200");
-update_map("\310\254\311\220\312\250\313\323\314\267\315\326\316\327\317\322");
-update_map("\320\321\321\343\322\325\323\340\324\342\325\212\326\231\327\236");
-update_map("\330\374\331\336\332\351\333\353\334\232\335\355\336\335\337\341");
-update_map("\340\352\341\240\342\203\343\307\344\204\345\222\346\206\347\207");
-update_map("\350\237\351\202\352\251\353\211\354\330\355\241\356\214\357\324");
-update_map("\360\320\361\344\362\345\363\242\364\223\365\213\366\224\367\366");
-update_map("\370\375\371\205\372\243\373\373\374\201\375\354\376\356\377\372");
+size_t pull_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_UTF8, CH_UNIX, src, src_len, (void **)dest);       
 }
 
-/* Init for russian language (iso8859-5) */
+/**
+ * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer
+ *
+ * @param dest always set at least to NULL 
+ *
+ * @returns The number of bytes occupied by the string in the destination
+ **/
 
-/* Added by Max Khon <max@iclub.nsu.ru> */
+size_t pull_utf8_allocate(void **dest, const char *src)
+{
+       size_t src_len = strlen(src)+1;
+       *dest = NULL;
+       return convert_string_allocate(CH_UTF8, CH_UNIX, src, src_len, dest);   
+}
+/**
+ Copy a string from a char* src to a unicode or ascii
+ dos codepage destination choosing unicode or ascii based on the 
+ flags in the SMB buffer starting at base_ptr.
+ Return the number of bytes occupied by the string in the destination.
+ flags can have:
+  STR_TERMINATE means include the null termination.
+  STR_UPPER     means uppercase in the destination.
+  STR_ASCII     use ascii even with unicode packet.
+  STR_NOALIGN   means don't do alignment.
+ dest_len is the maximum length allowed in the destination. If dest_len
+ is -1 then no maxiumum is used.
+**/
+
+size_t push_string(const void *base_ptr, void *dest, const char *src, size_t dest_len, int flags)
+{
+       if (!(flags & STR_ASCII) && \
+           ((flags & STR_UNICODE || \
+             (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
+               return push_ucs2(base_ptr, dest, src, dest_len, flags);
+       }
+       return push_ascii(dest, src, dest_len, flags);
+}
 
-static void init_iso8859_5(void)
+
+/**
+ Copy a string from a unicode or ascii source (depending on
+ the packet flags) to a char* destination.
+ Flags can have:
+  STR_TERMINATE means the string in src is null terminated.
+  STR_UNICODE   means to force as unicode.
+  STR_ASCII     use ascii even with unicode packet.
+  STR_NOALIGN   means don't do alignment.
+ if STR_TERMINATE is set then src_len is ignored is it is -1
+ src_len is the length of the source area in bytes.
+ Return the number of bytes occupied by the string in src.
+ The resulting string in "dest" is always null terminated.
+**/
+
+size_t pull_string(const void *base_ptr, char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
+{
+       if (!(flags & STR_ASCII) && \
+           ((flags & STR_UNICODE || \
+             (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
+               return pull_ucs2(base_ptr, dest, src, dest_len, src_len, flags);
+       }
+       return pull_ascii(dest, src, dest_len, src_len, flags);
+}
+
+size_t align_string(const void *base_ptr, const char *p, int flags)
+{
+       if (!(flags & STR_ASCII) && \
+           ((flags & STR_UNICODE || \
+             (SVAL(base_ptr, smb_flg2) & FLAGS2_UNICODE_STRINGS)))) {
+               return ucs2_align(base_ptr, p, 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)
 {
-       setupmaps();
-
-/* MSDOS Code Page 866 -> ISO8859-5 */
-update_map("\260\200\261\201\262\202\263\203\264\204\265\205\266\206\267\207");
-update_map("\270\210\271\211\272\212\273\213\274\214\275\215\276\216\277\217");
-update_map("\300\220\301\221\302\222\303\223\304\224\305\225\306\226\307\227");
-update_map("\310\230\311\231\312\232\313\233\314\234\315\235\316\236\317\237");
-update_map("\320\240\321\241\322\242\323\243\324\244\325\245\326\246\327\247");
-update_map("\330\250\331\251\332\252\333\253\334\254\335\255\336\256\337\257");
-update_map("\340\340\341\341\342\342\343\343\344\344\345\345\346\346\347\347");
-update_map("\350\350\351\351\352\352\353\353\354\354\355\355\356\356\357\357");
-update_map("\241\360\361\361\244\362\364\363\247\364\367\365\256\366\376\367");
-update_map("\360\374\240\377");
-}
-
-/* Added by Antonios Kavarnos (Antonios.Kavarnos@softlab.ece.ntua.gr */
-
-static void init_iso8859_7(void)
-{
-       setupmaps();
-
-/* MSDOS Code Page 737 -> ISO-8859-7 (Greek-Hellenic) */
-
-update_map("\301\200\302\201\303\202\304\203\305\204\306\205\307\206");
-update_map("\310\207\311\210\312\211\313\212\314\213\315\214\316\215\317\216");
-update_map("\320\217\321\220\323\221\324\222\325\223\326\224\327\225");
-update_map("\330\226\331\227");
-update_map("\341\230\342\231\343\232\344\233\345\234\346\235\347\236");
-update_map("\350\237\351\240\352\241\353\242\354\243\355\244\356\245\357\246");
-update_map("\360\247\361\250\362\252\363\251\364\253\365\254\366\255\367\256");
-update_map("\370\257\371\340");
-update_map("\332\364\333\365\334\341\335\342\336\343\337\345");
-update_map("\372\344\373\350\374\346\375\347\376\351");
-update_map("\266\352");
-update_map("\270\353\271\354\272\355\274\356\276\357\277\360");
-}
-
-/* Added by Deniz Akkus (akkus@alum.mit.edu) */
-
-static void init_iso8859_9(void)
-{
-  setupmaps();
-
-  /* MSDOS Code Page 857 -> ISO-8859-9 (Turkish) */
-
-  update_map("\xa0\xff\xa1\xad\xa2\xbd\xa3\x9c\xa4\xcf\xA5\xbe\xa6\xdd\xa7\xf5");
-  update_map("\xa8\xf9\xa9\xb8\xaa\xd1\xab\xae\xac\xaa\xad\xf0\xae\xa9\xaf\xee");
-  update_map("\xb0\xf8\xb1\xf1\xb2\xfd\xb3\xfc\xb4\xef\xb5\xe6\xb6\xf4\xb7\xfa");
-  update_map("\xb8\xf7\xb9\xfb\xba\xd0\xbb\xaf\xbc\xac\xbd\xab\xbe\xf3\xbf\xa8");
-  update_map("\xc0\xb7\xc1\xb5\xc2\xb6\xc3\xc7\xc4\x8e\xc5\x8f\xc6\x92\xc7\x80");
-  update_map("\xc8\xd4\xc9\x90\xca\xd2\xcb\xd3\xcc\xde\xcd\xd6\xce\xd7\xcf\xd8");
-  update_map("\xd0\xa6\xd1\xa5\xd2\xe3\xd3\xe0\xd4\xe2\xd5\xe5\xd6\x99\xd7\xe8");
-  update_map("\xd8\x9d\xd9\xeb\xda\xe9\xdb\xea\xdc\x9a\xdd\x98\xde\x9e\xdf\xe1");
-  update_map("\xe0\x85\xe1\xa0\xe2\x83\xe3\xc6\xe4\x84\xe5\x86\xe6\x91\xe7\x87");
-  update_map("\xe8\x8a\xe9\x82\xea\x88\xeb\x89\xec\xec\xed\xa1\xee\x8c\xef\x8b");
-  update_map("\xf0\xa7\xf1\xa4\xf2\x95\xf3\xa2\xf4\x93\xf5\xe4\xf6\x94\xf7\xf6");
-  update_map("\xf8\x9b\xf9\x97\xfa\xa3\xfb\x96\xfc\x81\xfd\x8d\xfe\x9f\xff\xed");
-}
+       size_t slen;
+       size_t dlen;
+       void *dest;
+       
+       slen = (strlen_w(src) + 1) * sizeof(smb_ucs2_t);
+       dlen = convert_string_allocate(CH_UCS2, CH_UNIX, src, slen, &dest);
+       if (dlen == (size_t)-1)
+               return NULL;
+       else
+               return dest;
+}
+
+/**
+ Convert from unix to ucs2 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 == (size_t)-1)
+               return NULL;
+       else
+               return dest;
+}
+
+/**
+ Convert from ucs2 to dos 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_u2dos(const smb_ucs2_t *src)
+{
+       size_t slen;
+       size_t dlen;
+       void *dest;
+       
+       slen = (strlen_w(src) + 1) * sizeof(smb_ucs2_t);
+       dlen = convert_string_allocate(CH_UCS2, CH_DOS, src, slen, &dest);
+       if (dlen == (size_t)-1)
+               return NULL;
+       else
+               return dest;
+}
 
-/* Init for russian language (koi8) */
+/**
+ Convert from dos to ucs2 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.
+**/
 
-static void init_koi8_r(void)
+smb_ucs2_t *acnv_dosu2(const char *src)
 {
-       setupmaps();
-
-/* MSDOS Code Page 866 -> KOI8-R */
-update_map("\200\304\201\263\202\332\203\277\204\300\205\331\206\303\207\264");
-update_map("\210\302\211\301\212\305\213\337\214\334\215\333\216\335\217\336");
-update_map("\220\260\221\261\222\262\223\364\224\376\225\371\226\373\227\367");
-update_map("\230\363\231\362\232\377\233\365\234\370\235\375\236\372\237\366");
-update_map("\240\315\241\272\242\325\243\361\244\326\245\311\246\270\247\267");
-update_map("\250\273\251\324\252\323\253\310\254\276\255\275\256\274\257\306");
-update_map("\260\307\261\314\262\265\263\360\264\266\265\271\266\321\267\322");
-update_map("\270\313\271\317\272\320\273\312\274\330\275\327\276\316\277\374");
-update_map("\300\356\301\240\302\241\303\346\304\244\305\245\306\344\307\243");
-update_map("\310\345\311\250\312\251\313\252\314\253\315\254\316\255\317\256");
-update_map("\320\257\321\357\322\340\323\341\324\342\325\343\326\246\327\242");
-update_map("\330\354\331\353\332\247\333\350\334\355\335\351\336\347\337\352");
-update_map("\340\236\341\200\342\201\343\226\344\204\345\205\346\224\347\203");
-update_map("\350\225\351\210\352\211\353\212\354\213\355\214\356\215\357\216");
-update_map("\360\217\361\237\362\220\363\221\364\222\365\223\366\206\367\202");
-update_map("\370\234\371\233\372\207\373\230\374\235\375\231\376\227\377\232");
-}
-
-
-/* Init for ROMAN-8 (HP-UX) */
-
-static void init_roman8(void) {
-
-       setupmaps();
-
-/* MSDOS Code Page 850 -> ROMAN8 */
-update_map("\240\377\241\267\242\266\243\324\244\322\245\323\246\327\247\330");
-update_map("\250\357\253\371\255\353\256\352\257\234");
-update_map("\260\356\261\355\262\354\263\370\264\200\265\207\266\245\267\244");
-update_map("\270\255\271\250\272\317\273\234\274\276\275\365\276\237\277\275");
-update_map("\300\203\301\210\302\223\303\226\304\240\305\202\306\242\307\243");
-update_map("\310\205\311\212\312\225\313\227\314\204\315\211\316\224\317\201");
-update_map("\320\217\321\214\322\235\323\222\324\206\325\241\326\233\327\221");
-update_map("\330\216\331\215\332\231\333\232\334\220\335\213\336\341\337\342");
-update_map("\340\265\341\307\342\306\343\321\344\320\345\326\346\336\347\340");
-update_map("\350\343\351\345\352\344\355\351\357\230");
-update_map("\360\350\361\347\362\372\363\346\364\364\365\363\366\360\367\254");
-update_map("\370\253\371\246\372\247\373\256\374\376\375\257\376\361");
-}
-
-/*
- * Convert unix to dos
- */
-char *unix2dos_format(char *str,BOOL overwrite)
-{
-  char *p;
-  char *dp;
-
-  if (!mapsinited)
-    initmaps();
-
-  if (overwrite) {
-    for (p = str; *p; p++)
-      *p = unix2dos[(unsigned char)*p];
-    return str;
-  } else {
-    for (p = str, dp = cvtbuf;*p && (dp - cvtbuf < sizeof(cvtbuf) - 1); p++,dp++)
-      *dp = unix2dos[(unsigned char)*p];
-    *dp = 0;
-    return cvtbuf;
-  }
-}
-
-/*
- * Convert dos to unix
- */
-char *dos2unix_format(char *str, BOOL overwrite)
-{
-  char *p;
-  char *dp;
-
-  if (!mapsinited)
-    initmaps();
-
-  if (overwrite) {
-    for (p = str; *p; p++)
-      *p = dos2unix[(unsigned char)*p];
-    return str;
-  } else {
-    for (p = str, dp = cvtbuf;*p && (dp - cvtbuf < sizeof(cvtbuf) - 1); p++,dp++)
-      *dp = dos2unix[(unsigned char)*p];
-    *dp = 0;
-    return cvtbuf;
-  }
-}
-
-
-/*
- * Interpret character set.
- */
-void interpret_character_set(char *str, int codepage)
-{
-    if (strequal (str, "iso8859-1")) {
-        init_iso8859_1(codepage);
-    } else if (strequal (str, "iso8859-2")) {
-        init_iso8859_2();
-    } else if (strequal (str, "iso8859-5")) {
-        init_iso8859_5();
-    } else if (strequal (str, "iso8859-7")) {
-        init_iso8859_7();
-    } else if (strequal (str, "iso8859-9")) {
-        init_iso8859_9();
-    } else if (strequal (str, "koi8-r")) {
-        init_koi8_r();
-    } else if (strequal (str, "roman8")) {
-        init_roman8();
-    } else {
-        DEBUG(0,("unrecognized character set %s\n", str));
-    }
-
-    load_unix_unicode_map(str);
+       size_t slen;
+       size_t dlen;
+       void *dest;
+       
+       slen = strlen(src) + 1;
+       dlen = convert_string_allocate(CH_DOS, CH_UCS2, src, slen, &dest);
+       if (dlen == (size_t)-1)
+               return NULL;
+       else
+               return dest;
 }