1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifdef FEATURE_ICU_LOCALE
17 #include <dlfcn.h>
18 #include <musl_log.h>
19 #include <string.h>
20 #include "locale_impl.h"
21
22 #define ICU_UC_SO "libhmicuuc.z.so"
23 #define ICU_I18N_SO "libhmicui18n.z.so"
24
25 static void *g_icuuc_handle = NULL;
26 static void *g_icui18n_handle = NULL;
27 hidden struct icu_opt_func g_icu_opt_func = { NULL };
28
29 #define VALID_ICU_NAME_LEN 5
30 static char g_valid_icu_locale_name[VALID_ICU_NAME_LEN + 1];
31
get_icu_handle(ICU_SO_TYPE type,const char * symbol_name)32 void *get_icu_handle(ICU_SO_TYPE type, const char *symbol_name)
33 {
34 void *cur_handle;
35 char *cur_so;
36 if (type == ICU_UC) {
37 cur_handle = g_icuuc_handle;
38 cur_so = ICU_UC_SO;
39 } else {
40 cur_handle = g_icui18n_handle;
41 cur_so = ICU_I18N_SO;
42 }
43
44 if (!cur_handle) {
45 cur_handle = dlopen(cur_so, RTLD_LOCAL);
46 }
47 if (!cur_handle) {
48 MUSL_LOGE("dlopen icu so for musl locale fail %{public}s", dlerror());
49 return NULL;
50 }
51 return dlsym(cur_handle, symbol_name);
52 }
53
get_icu_symbol(ICU_SO_TYPE type,void ** icu_symbol_handle,const char * symbol_name)54 void get_icu_symbol(ICU_SO_TYPE type, void **icu_symbol_handle, const char *symbol_name)
55 {
56 if (!(*icu_symbol_handle)) {
57 *icu_symbol_handle = get_icu_handle(type, symbol_name);
58 }
59 }
60
61 /* ICU methods don't need charset for locale, process the given locale name */
get_valid_icu_locale_name(const char * name)62 char *get_valid_icu_locale_name(const char *name)
63 {
64 strncpy(g_valid_icu_locale_name, name, VALID_ICU_NAME_LEN);
65 g_valid_icu_locale_name[VALID_ICU_NAME_LEN] = '\0';
66 return g_valid_icu_locale_name;
67 }
68 #endif
69