r4267: fixed the charset code to use the builtin_functions.
[samba.git] / source4 / lib / iconv.c
index 98905c4b84446610d0bc1c5dd8d48b64668f6301..181834d66aaeddb6c7f77bdbc1a0ea69db0ea057 100644 (file)
@@ -20,6 +20,7 @@
 */
 
 #include "includes.h"
+#include "dlinklist.h"
 #include "system/iconv.h"
 
 
@@ -55,7 +56,7 @@ static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
 static size_t iconv_copy  (void *,const char **, size_t *, char **, size_t *);
 static size_t iconv_swab  (void *,const char **, size_t *, char **, size_t *);
 
-static const struct charset_functions const builtin_functions[] = {
+static const struct charset_functions builtin_functions[] = {
        /* windows is closest to UTF-16 */
        {"UCS-2LE",  iconv_copy, iconv_copy},
        {"UTF-16LE",  iconv_copy, iconv_copy},
@@ -66,13 +67,12 @@ static const struct charset_functions const builtin_functions[] = {
        {"UTF8",   utf8_pull,  utf8_push},
        {"UTF-8",   utf8_pull,  utf8_push},
        {"ASCII", ascii_pull, ascii_push},
-       {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
-       {NULL, NULL, NULL}
+       {"UCS2-HEX", ucs2hex_pull, ucs2hex_push}
 };
 
 static struct charset_functions *charsets = NULL;
 
-static NTSTATUS charset_register_backend(const void *_funcs) 
+NTSTATUS charset_register_backend(const void *_funcs) 
 {
        struct charset_functions *funcs = memdup(_funcs,sizeof(struct charset_functions));
        struct charset_functions *c = charsets;
@@ -91,20 +91,6 @@ static NTSTATUS charset_register_backend(const void *_funcs)
        return NT_STATUS_OK;
 }
 
-static void lazy_initialize_iconv(void)
-{
-       static BOOL initialized = False;
-       int i;
-
-       if (!initialized) {
-               initialized = True;
-               register_subsystem("charset", charset_register_backend);
-               
-               for(i = 0; builtin_functions[i].name; i++) 
-                       register_backend("charset", &builtin_functions[i]);
-       }
-}
-
 #ifdef HAVE_NATIVE_ICONV
 /* if there was an error then reset the internal state,
    this ensures that we don't have a shift state remaining for
@@ -174,11 +160,8 @@ static BOOL is_utf16(const char *name)
 smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
 {
        smb_iconv_t ret;
-       struct charset_functions *from, *to;
-       
-       lazy_initialize_iconv();
-       from = charsets;
-       to = charsets;
+       const struct charset_functions *from=NULL, *to=NULL;
+       int i;
 
        ret = (smb_iconv_t)talloc_named(NULL, sizeof(*ret), 
                                        "iconv(%s,%s)", tocode, fromcode);
@@ -194,14 +177,25 @@ smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
                return ret;
        }
 
-       while (from) {
-               if (strcasecmp(from->name, fromcode) == 0) break;
-               from = from->next;
+       for (i=0;i<ARRAY_SIZE(builtin_functions);i++) {
+               if (strcasecmp(fromcode, builtin_functions[i].name) == 0) {
+                       from = &builtin_functions[i];
+               }
+               if (strcasecmp(tocode, builtin_functions[i].name) == 0) {
+                       to = &builtin_functions[i];
+               }
        }
 
-       while (to) {
-               if (strcasecmp(to->name, tocode) == 0) break;
-               to = to->next;
+       if (from == NULL) {
+               for (from=charsets; from; from=from->next) {
+                       if (strcasecmp(from->name, fromcode) == 0) break;
+               }
+       }
+
+       if (to == NULL) {
+               for (to=charsets; to; to=to->next) {
+                       if (strcasecmp(to->name, tocode) == 0) break;
+               }
        }
 
 #ifdef HAVE_NATIVE_ICONV