• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <locale.h>
2 #include <string.h>
3 #include <sys/mman.h>
4 #include <stdlib.h>
5 #include "locale_impl.h"
6 #include "libc.h"
7 #include "lock.h"
8 #include "fork_impl.h"
9 
10 #define malloc __libc_malloc
11 #define calloc undef
12 #define realloc undef
13 #define free undef
14 
__lctrans_impl(const char * msg,const struct __locale_map * lm)15 const char *__lctrans_impl(const char *msg, const struct __locale_map *lm)
16 {
17 	const char *trans = 0;
18 	if (lm) trans = __mo_lookup(lm->map, lm->map_size, msg);
19 	return trans ? trans : msg;
20 }
21 
22 #ifndef __LITEOS__
23 static const char envvars[][18] = {
24 	"LC_CTYPE",
25 	"LC_NUMERIC",
26 	"LC_TIME",
27 	"LC_COLLATE",
28 	"LC_MONETARY",
29 	"LC_MESSAGES",
30 	"LC_PAPER",
31 	"LC_NAME",
32 	"LC_ADDRESS",
33 	"LC_TELEPHONE",
34 	"LC_MEASUREMENT",
35 	"LC_IDENTIFICATION",
36 };
37 #else
38 static const char envvars[][12] = {
39 	"LC_CTYPE",
40 	"LC_NUMERIC",
41 	"LC_TIME",
42 	"LC_COLLATE",
43 	"LC_MONETARY",
44 	"LC_MESSAGES",
45 };
46 #endif
47 
48 volatile int __locale_lock[1];
49 volatile int *const __locale_lockptr = __locale_lock;
50 
51 #ifdef FEATURE_ICU_LOCALE
52 static const char *g_valid_locale_table[] = {"zh_CN", "zh_CN.UTF-8"};
53 static int g_locale_table_size = sizeof(g_valid_locale_table) / sizeof(g_valid_locale_table[0]);
54 
55 typedef void (*f_set_icu_directory)(void);
56 static f_set_icu_directory g_icuuc_set_icu_directory;
57 #endif
58 
__get_locale(int cat,const char * val)59 const struct __locale_map *__get_locale(int cat, const char *val)
60 {
61 	static void *volatile loc_head;
62 	const struct __locale_map *p;
63 	struct __locale_map *new = 0;
64 	const char *path = 0, *z;
65 	char buf[256];
66 	size_t l, n;
67 
68 	if (!*val) {
69 		(val = getenv("LC_ALL")) && *val ||
70 		(val = getenv(envvars[cat])) && *val ||
71 		(val = getenv("LANG")) && *val ||
72 		(val = "C.UTF-8");
73 	}
74 
75 	/* Limit name length and forbid leading dot or any slashes. */
76 	for (n=0; n<LOCALE_NAME_MAX && val[n] && val[n]!='/'; n++);
77 	if (val[0]=='.' || val[n]) val = "C.UTF-8";
78 	int builtin = (val[0]=='C' && !val[1])
79 #ifndef __LITEOS__
80 		|| !strcmp(val, "POSIX")
81 		|| !strcmp(val, "en_US");
82 #else
83 		|| !strcmp(val, "C.UTF-8")
84 		|| !strcmp(val, "POSIX");
85 #endif
86 
87 	if (builtin) {
88 		if (cat == LC_CTYPE && val[1]=='.')
89 			return (void *)&__c_dot_utf8;
90 		return 0;
91 	}
92 
93 	for (p=loc_head; p; p=p->next)
94 		if (!strcmp(val, p->name)) return p;
95 
96 	if (!strcmp(val, "en_US.UTF-8") || !strcmp(val, "C.UTF-8")) {
97 		/* If no locale definition was found, make a locale map
98 		* object anyway to store the name, which is kept for the
99 		* sake of being able to do message translations at the
100 		* application level. */
101 		if (!new && (new = malloc(sizeof *new))) {
102 			new->map = __c_dot_utf8.map;
103 			new->map_size = __c_dot_utf8.map_size;
104 			memcpy(new->name, val, n);
105 			new->name[n] = 0;
106 			new->next = loc_head;
107 			new->flag = VALID;
108 			loc_head = new;
109 		}
110 	}
111 
112 	if (!libc.secure) path = getenv("MUSL_LOCPATH");
113 	/* FIXME: add a default path? */
114 
115 	if (path) for (; *path; path=z+!!*z) {
116 		z = __strchrnul(path, ':');
117 		l = z - path;
118 		if (l >= sizeof buf - n - 2) continue;
119 		memcpy(buf, path, l);
120 		buf[l] = '/';
121 		memcpy(buf+l+1, val, n);
122 		buf[l+1+n] = 0;
123 		size_t map_size;
124 		const void *map = __map_file(buf, &map_size);
125 		if (map) {
126 			new = malloc(sizeof *new);
127 			if (!new) {
128 				__munmap((void *)map, map_size);
129 				break;
130 			}
131 			new->map = map;
132 			new->map_size = map_size;
133 			memcpy(new->name, val, n);
134 			new->name[n] = 0;
135 			new->next = loc_head;
136 			new->flag = VALID;
137 			loc_head = new;
138 			break;
139 		}
140 	}
141 
142 	/* If no locale definition was found, make a locale map
143 	 * object anyway to store the name, which is kept for the
144 	 * sake of being able to do message translations at the
145 	 * application level. */
146 	if (!new && (new = malloc(sizeof *new))) {
147 		new->map = __c_dot_utf8.map;
148 		new->map_size = __c_dot_utf8.map_size;
149 		memcpy(new->name, val, n);
150 		new->name[n] = 0;
151 		new->next = loc_head;
152 		new->flag = INVALID;
153 		loc_head = new;
154 	}
155 
156 #ifdef FEATURE_ICU_LOCALE
157 	/* For locales with no definition, checks whether this locale setting
158 	 * is valid for icu. We use a table to record all the valid icu locale
159 	 * settings. */
160 	if (new->flag == INVALID) {
161 		for (int i = 0; i < g_locale_table_size; i++) {
162 			if (strcmp(g_valid_locale_table[i], val) == 0) {
163 				new->flag = ICU_VALID;
164 				break;
165 			}
166 		}
167 		/* Use ICU_VALID flag to indicate that other icu-related functions can use icu methods */
168 		if (new->flag == ICU_VALID) {
169 			if (!g_icuuc_set_icu_directory) {
170 				/* Load ICU data to memory */
171 				g_icuuc_set_icu_directory = (f_set_icu_directory)get_icu_handle(ICU_UC, "_Z17SetHwIcuDirectoryv");
172 			}
173 			if (g_icuuc_set_icu_directory) {
174 				g_icuuc_set_icu_directory();
175 			}
176 		}
177 	}
178 #endif
179 
180 	/* For LC_CTYPE, never return a null pointer unless the
181 	 * requested name was "C" or "POSIX". */
182 	if (!new && cat == LC_CTYPE) new = (void *)&__c_dot_utf8;
183 
184 	return new;
185 }
186