lib: Simplify load_case_tables_library()
[obnox/samba/samba-obnox.git] / lib / util / charset / codepoints.c
index 71611bfc4c1438f1bc773b761ad3c1b81983ac77..499cea42f0969efd2bbad46de766941d1ce264c1 100644 (file)
@@ -47,15 +47,16 @@ This is the function that should be called from library code.
 ********************************************************************/
 void load_case_tables_library(void)
 {
-       TALLOC_CTX *mem_ctx;
+       const char *codepagedir = get_dyn_CODEPAGEDIR();
+       size_t codepagedir_len = strlen(codepagedir);
+       char buf[codepagedir_len+13];
+
+       snprintf(buf, sizeof(buf), "%s/upcase.dat", codepagedir);
+       upcase_table = map_file(buf, 0x20000);
+
+       snprintf(buf, sizeof(buf), "%s/lowcase.dat", codepagedir);
+       lowcase_table = map_file(buf, 0x20000);
 
-       mem_ctx = talloc_init("load_case_tables");
-       if (!mem_ctx) {
-               smb_panic("No memory for case_tables");
-       }
-       upcase_table = map_file(talloc_asprintf(mem_ctx, "%s/upcase.dat", get_dyn_CODEPAGEDIR()), 0x20000);
-       lowcase_table = map_file(talloc_asprintf(mem_ctx, "%s/lowcase.dat", get_dyn_CODEPAGEDIR()), 0x20000);
-       talloc_free(mem_ctx);
        if (upcase_table == NULL) {
                DEBUG(1, ("Failed to load upcase.dat, will use lame ASCII-only case sensitivity rules\n"));
                upcase_table = (void *)-1;
@@ -158,7 +159,7 @@ struct smb_iconv_handle {
        const char *unix_charset;
        const char *dos_charset;
        const char *display_charset;
-       bool native_iconv;
+       bool use_builtin_handlers;
        smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
 };
 
@@ -168,17 +169,17 @@ struct smb_iconv_handle *get_iconv_handle(void)
 {
        if (global_iconv_handle == NULL)
                global_iconv_handle = smb_iconv_handle_reinit(talloc_autofree_context(),
-                                                                       "ASCII", "UTF-8", "ASCII", true, NULL);
+                                                             "ASCII", "UTF-8", true, NULL);
        return global_iconv_handle;
 }
 
 struct smb_iconv_handle *get_iconv_testing_handle(TALLOC_CTX *mem_ctx, 
                                                  const char *dos_charset, 
-                                                 const char *unix_charset, 
-                                                 const char *display_charset)
+                                                 const char *unix_charset,
+                                                 bool use_builtin_handlers)
 {
        return smb_iconv_handle_reinit(mem_ctx,
-                                      dos_charset, unix_charset, display_charset, true, NULL);
+                                      dos_charset, unix_charset, use_builtin_handlers, NULL);
 }
 
 /**
@@ -190,7 +191,6 @@ const char *charset_name(struct smb_iconv_handle *ic, charset_t ch)
        case CH_UTF16: return "UTF-16LE";
        case CH_UNIX: return ic->unix_charset;
        case CH_DOS: return ic->dos_charset;
-       case CH_DISPLAY: return ic->display_charset;
        case CH_UTF8: return "UTF8";
        case CH_UTF16BE: return "UTF-16BE";
        case CH_UTF16MUNGED: return "UTF16_MUNGED";
@@ -219,37 +219,6 @@ static int close_iconv_handle(struct smb_iconv_handle *data)
        return 0;
 }
 
-static const char *map_locale(const char *charset)
-{
-       if (strcmp(charset, "LOCALE") != 0) {
-               return charset;
-       }
-#if defined(HAVE_NL_LANGINFO) && defined(CODESET)
-       {
-               const char *ln;
-               smb_iconv_t handle;
-
-               ln = nl_langinfo(CODESET);
-               if (ln == NULL) {
-                       DEBUG(1,("Unable to determine charset for LOCALE - using ASCII\n"));
-                       return "ASCII";
-               }
-               /* Check whether the charset name is supported
-                  by iconv */
-               handle = smb_iconv_open(ln, "UCS-2LE");
-               if (handle == (smb_iconv_t) -1) {
-                       DEBUG(5,("Locale charset '%s' unsupported, using ASCII instead\n", ln));
-                       return "ASCII";
-               } else {
-                       DEBUG(5,("Substituting charset '%s' for LOCALE\n", ln));
-                       smb_iconv_close(handle);
-               }
-               return ln;
-       }
-#endif
-       return "ASCII";
-}
-
 /*
   the old_ic is passed in here as the smb_iconv_handle structure
   is used as a global pointer in some places (eg. python modules). We
@@ -261,14 +230,11 @@ static const char *map_locale(const char *charset)
 _PUBLIC_ struct smb_iconv_handle *smb_iconv_handle_reinit(TALLOC_CTX *mem_ctx,
                                                                    const char *dos_charset,
                                                                    const char *unix_charset,
-                                                                   const char *display_charset,
-                                                                   bool native_iconv,
+                                                                   bool use_builtin_handlers,
                                                                    struct smb_iconv_handle *old_ic)
 {
        struct smb_iconv_handle *ret;
 
-       display_charset = map_locale(display_charset);
-
        if (old_ic != NULL) {
                ret = old_ic;
                close_iconv_handle(ret);
@@ -297,8 +263,7 @@ _PUBLIC_ struct smb_iconv_handle *smb_iconv_handle_reinit(TALLOC_CTX *mem_ctx,
 
        ret->dos_charset = talloc_strdup(ret->child_ctx, dos_charset);
        ret->unix_charset = talloc_strdup(ret->child_ctx, unix_charset);
-       ret->display_charset = talloc_strdup(ret->child_ctx, display_charset);
-       ret->native_iconv = native_iconv;
+       ret->use_builtin_handlers = use_builtin_handlers;
 
        return ret;
 }
@@ -319,7 +284,7 @@ smb_iconv_t get_conv_handle(struct smb_iconv_handle *ic,
        n2 = charset_name(ic, to);
 
        ic->conv_handles[from][to] = smb_iconv_open_ex(ic, n2, n1,
-                                                      ic->native_iconv);
+                                                      ic->use_builtin_handlers);
 
        if (ic->conv_handles[from][to] == (smb_iconv_t)-1) {
                if ((from == CH_DOS || to == CH_DOS) &&
@@ -332,7 +297,7 @@ smb_iconv_t get_conv_handle(struct smb_iconv_handle *ic,
                        n2 = charset_name(ic, to);
 
                        ic->conv_handles[from][to] =
-                               smb_iconv_open_ex(ic, n2, n1, ic->native_iconv);
+                               smb_iconv_open_ex(ic, n2, n1, ic->use_builtin_handlers);
                }
        }