1/* Handle configuration data.
2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20#include <assert.h>
21#include <ctype.h>
22#include <errno.h>
23#include <limits.h>
24#include <locale.h>
25#include <search.h>
26#include <stddef.h>
27#include <stdio.h>
28#include <stdio_ext.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <sys/param.h>
33
34#include <libc-lock.h>
35#include <gconv_int.h>
36#include <gconv_parseconfdir.h>
37
38/* This is the default path where we look for module lists. */
39static const char default_gconv_path[] = GCONV_PATH;
40
41/* Type to represent search path. */
42struct path_elem
43{
44 const char *name;
45 size_t len;
46};
47
48/* The path elements, as determined by the __gconv_get_path function.
49 All path elements end in a slash. */
50struct path_elem *__gconv_path_elem;
51/* Maximum length of a single path element in __gconv_path_elem. */
52size_t __gconv_max_path_elem_len;
53
54/* We use the following struct if we couldn't allocate memory. */
55static const struct path_elem empty_path_elem = { NULL, 0 };
56
57/* Filename extension for the modules. */
58#ifndef MODULE_EXT
59# define MODULE_EXT ".so"
60#endif
61static const char gconv_module_ext[] = MODULE_EXT;
62
63/* We have a few builtin transformations. */
64static struct gconv_module builtin_modules[] =
65{
66#define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
67 MinF, MaxF, MinT, MaxT) \
68 { \
69 .from_string = From, \
70 .to_string = To, \
71 .cost_hi = Cost, \
72 .cost_lo = INT_MAX, \
73 .module_name = Name \
74 },
75#define BUILTIN_ALIAS(From, To)
76
77#include "gconv_builtin.h"
78
79#undef BUILTIN_TRANSFORMATION
80#undef BUILTIN_ALIAS
81};
82
83static const char builtin_aliases[] =
84{
85#define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
86 MinF, MaxF, MinT, MaxT)
87#define BUILTIN_ALIAS(From, To) From "\0" To "\0"
88
89#include "gconv_builtin.h"
90
91#undef BUILTIN_TRANSFORMATION
92#undef BUILTIN_ALIAS
93};
94
95
96/* Value of the GCONV_PATH environment variable. */
97const char *__gconv_path_envvar;
98
99
100/* Test whether there is already a matching module known. */
101static int
102detect_conflict (const char *alias)
103{
104 struct gconv_module *node = __gconv_modules_db;
105
106 while (node != NULL)
107 {
108 int cmpres = strcmp (alias, node->from_string);
109
110 if (cmpres == 0)
111 /* We have a conflict. */
112 return 1;
113 else if (cmpres < 0)
114 node = node->left;
115 else
116 node = node->right;
117 }
118
119 return node != NULL;
120}
121
122
123/* The actual code to add aliases. */
124static void
125add_alias2 (const char *from, const char *to, const char *wp)
126{
127 /* Test whether this alias conflicts with any available module. */
128 if (detect_conflict (from))
129 /* It does conflict, don't add the alias. */
130 return;
131
132 struct gconv_alias *new_alias = (struct gconv_alias *)
133 malloc (sizeof (struct gconv_alias) + (wp - from));
134 if (new_alias != NULL)
135 {
136 void **inserted;
137
138 new_alias->fromname = memcpy ((char *) new_alias
139 + sizeof (struct gconv_alias),
140 from, wp - from);
141 new_alias->toname = new_alias->fromname + (to - from);
142
143 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
144 __gconv_alias_compare);
145 if (inserted == NULL || *inserted != new_alias)
146 /* Something went wrong, free this entry. */
147 free (new_alias);
148 }
149}
150
151
152/* Add new alias. */
153static void
154add_alias (char *rp)
155{
156 /* We now expect two more string. The strings are normalized
157 (converted to UPPER case) and strored in the alias database. */
158 char *from, *to, *wp;
159
160 while (__isspace_l (*rp, _nl_C_locobj_ptr))
161 ++rp;
162 from = wp = rp;
163 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
164 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
165 if (*rp == '\0')
166 /* There is no `to' string on the line. Ignore it. */
167 return;
168 *wp++ = '\0';
169 to = ++rp;
170 while (__isspace_l (*rp, _nl_C_locobj_ptr))
171 ++rp;
172 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
173 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
174 if (to == wp)
175 /* No `to' string, ignore the line. */
176 return;
177 *wp++ = '\0';
178
179 add_alias2 (from, to, wp);
180}
181
182
183/* Insert a data structure for a new module in the search tree. */
184static void
185insert_module (struct gconv_module *newp, int tobefreed)
186{
187 struct gconv_module **rootp = &__gconv_modules_db;
188
189 while (*rootp != NULL)
190 {
191 struct gconv_module *root = *rootp;
192 int cmpres;
193
194 cmpres = strcmp (newp->from_string, root->from_string);
195 if (cmpres == 0)
196 {
197 /* Both strings are identical. Insert the string at the
198 end of the `same' list if it is not already there. */
199 while (strcmp (newp->from_string, root->from_string) != 0
200 || strcmp (newp->to_string, root->to_string) != 0)
201 {
202 rootp = &root->same;
203 root = *rootp;
204 if (root == NULL)
205 break;
206 }
207
208 if (root != NULL)
209 {
210 /* This is a no new conversion. But maybe the cost is
211 better. */
212 if (newp->cost_hi < root->cost_hi
213 || (newp->cost_hi == root->cost_hi
214 && newp->cost_lo < root->cost_lo))
215 {
216 newp->left = root->left;
217 newp->right = root->right;
218 newp->same = root->same;
219 *rootp = newp;
220
221 free (root);
222 }
223 else if (tobefreed)
224 free (newp);
225 return;
226 }
227
228 break;
229 }
230 else if (cmpres < 0)
231 rootp = &root->left;
232 else
233 rootp = &root->right;
234 }
235
236 /* Plug in the new node here. */
237 *rootp = newp;
238}
239
240
241/* Add new module. */
242static void
243add_module (char *rp, const char *directory, size_t dir_len, int modcounter)
244{
245 /* We expect now
246 1. `from' name
247 2. `to' name
248 3. filename of the module
249 4. an optional cost value
250 */
251 struct gconv_alias fake_alias;
252 struct gconv_module *new_module;
253 char *from, *to, *module, *wp;
254 int need_ext;
255 int cost_hi;
256
257 while (__isspace_l (*rp, _nl_C_locobj_ptr))
258 ++rp;
259 from = rp;
260 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
261 {
262 *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
263 ++rp;
264 }
265 if (*rp == '\0')
266 return;
267 *rp++ = '\0';
268 to = wp = rp;
269 while (__isspace_l (*rp, _nl_C_locobj_ptr))
270 ++rp;
271 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
272 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
273 if (*rp == '\0')
274 return;
275 *wp++ = '\0';
276 do
277 ++rp;
278 while (__isspace_l (*rp, _nl_C_locobj_ptr));
279 module = wp;
280 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
281 *wp++ = *rp++;
282 if (*rp == '\0')
283 {
284 /* There is no cost, use one by default. */
285 *wp++ = '\0';
286 cost_hi = 1;
287 }
288 else
289 {
290 /* There might be a cost value. */
291 char *endp;
292
293 *wp++ = '\0';
294 cost_hi = strtol (rp, &endp, 10);
295 if (rp == endp || cost_hi < 1)
296 /* No useful information. */
297 cost_hi = 1;
298 }
299
300 if (module[0] == '\0')
301 /* No module name given. */
302 return;
303 if (module[0] == '/')
304 dir_len = 0;
305
306 /* See whether we must add the ending. */
307 need_ext = 0;
308 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
309 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
310 sizeof (gconv_module_ext)) != 0)
311 /* We must add the module extension. */
312 need_ext = sizeof (gconv_module_ext) - 1;
313
314 /* See whether we have already an alias with this name defined. */
315 fake_alias.fromname = strndupa (from, to - from);
316
317 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
318 /* This module duplicates an alias. */
319 return;
320
321 new_module = (struct gconv_module *) calloc (1,
322 sizeof (struct gconv_module)
323 + (wp - from)
324 + dir_len + need_ext);
325 if (new_module != NULL)
326 {
327 char *tmp;
328
329 new_module->from_string = tmp = (char *) (new_module + 1);
330 tmp = __mempcpy (tmp, from, to - from);
331
332 new_module->to_string = tmp;
333 tmp = __mempcpy (tmp, to, module - to);
334
335 new_module->cost_hi = cost_hi;
336 new_module->cost_lo = modcounter;
337
338 new_module->module_name = tmp;
339
340 if (dir_len != 0)
341 tmp = __mempcpy (tmp, directory, dir_len);
342
343 tmp = __mempcpy (tmp, module, wp - module);
344
345 if (need_ext)
346 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
347
348 /* Now insert the new module data structure in our search tree. */
349 insert_module (new_module, 1);
350 }
351}
352
353
354/* Determine the directories we are looking for data in. This function should
355 only be called from __gconv_read_conf. */
356static void
357__gconv_get_path (void)
358{
359 struct path_elem *result;
360
361 /* This function is only ever called when __gconv_path_elem is NULL. */
362 result = __gconv_path_elem;
363 assert (result == NULL);
364
365 /* Determine the complete path first. */
366 char *gconv_path;
367 size_t gconv_path_len;
368 char *elem;
369 char *oldp;
370 char *cp;
371 int nelems;
372 char *cwd;
373 size_t cwdlen;
374
375 if (__gconv_path_envvar == NULL)
376 {
377 /* No user-defined path. Make a modifiable copy of the
378 default path. */
379 gconv_path = strdupa (default_gconv_path);
380 gconv_path_len = sizeof (default_gconv_path);
381 cwd = NULL;
382 cwdlen = 0;
383 }
384 else
385 {
386 /* Append the default path to the user-defined path. */
387 size_t user_len = strlen (__gconv_path_envvar);
388
389 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
390 gconv_path = alloca (gconv_path_len);
391 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
392 user_len),
393 ":", 1),
394 default_gconv_path, sizeof (default_gconv_path));
395 cwd = __getcwd (NULL, 0);
396 cwdlen = __glibc_unlikely (cwd == NULL) ? 0 : strlen (cwd);
397 }
398 assert (default_gconv_path[0] == '/');
399
400 /* In a first pass we calculate the number of elements. */
401 oldp = NULL;
402 cp = strchr (gconv_path, ':');
403 nelems = 1;
404 while (cp != NULL)
405 {
406 if (cp != oldp + 1)
407 ++nelems;
408 oldp = cp;
409 cp = strchr (cp + 1, ':');
410 }
411
412 /* Allocate the memory for the result. */
413 result = malloc ((nelems + 1)
414 * sizeof (struct path_elem)
415 + gconv_path_len + nelems
416 + (nelems - 1) * (cwdlen + 1));
417 if (result != NULL)
418 {
419 char *strspace = (char *) &result[nelems + 1];
420 int n = 0;
421
422 /* Separate the individual parts. */
423 __gconv_max_path_elem_len = 0;
424 elem = __strtok_r (gconv_path, ":", &gconv_path);
425 assert (elem != NULL);
426 do
427 {
428 result[n].name = strspace;
429 if (elem[0] != '/')
430 {
431 assert (cwd != NULL);
432 strspace = __mempcpy (strspace, cwd, cwdlen);
433 *strspace++ = '/';
434 }
435 strspace = __stpcpy (strspace, elem);
436 if (strspace[-1] != '/')
437 *strspace++ = '/';
438
439 result[n].len = strspace - result[n].name;
440 if (result[n].len > __gconv_max_path_elem_len)
441 __gconv_max_path_elem_len = result[n].len;
442
443 *strspace++ = '\0';
444 ++n;
445 }
446 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
447
448 result[n].name = NULL;
449 result[n].len = 0;
450 }
451
452 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
453
454 free (cwd);
455}
456
457
458/* Read all configuration files found in the user-specified and the default
459 path. This function should only be called once during the program's
460 lifetime. It disregards locking and synchronization because its only
461 caller, __gconv_load_conf, handles this. */
462static void
463__gconv_read_conf (void)
464{
465 int save_errno = errno;
466 size_t cnt;
467
468 /* First see whether we should use the cache. */
469 if (__gconv_load_cache () == 0)
470 {
471 /* Yes, we are done. */
472 __set_errno (save_errno);
473 return;
474 }
475
476#ifndef STATIC_GCONV
477 /* Find out where we have to look. */
478 __gconv_get_path ();
479
480 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
481 gconv_parseconfdir (__gconv_path_elem[cnt].name,
482 __gconv_path_elem[cnt].len);
483#endif
484
485 /* Add the internal modules. */
486 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
487 ++cnt)
488 {
489 struct gconv_alias fake_alias;
490
491 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
492
493 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
494 != NULL)
495 /* It'll conflict so don't add it. */
496 continue;
497
498 insert_module (&builtin_modules[cnt], 0);
499 }
500
501 /* Add aliases for builtin conversions. */
502 const char *cp = builtin_aliases;
503 do
504 {
505 const char *from = cp;
506 const char *to = __rawmemchr (from, '\0') + 1;
507 cp = __rawmemchr (to, '\0') + 1;
508
509 add_alias2 (from, to, cp);
510 }
511 while (*cp != '\0');
512
513 /* Restore the error number. */
514 __set_errno (save_errno);
515}
516
517
518/* This "once" variable is used to do a one-time load of the configuration. */
519__libc_once_define (static, once);
520
521
522/* Read all configuration files found in the user-specified and the default
523 path, but do it only "once" using __gconv_read_conf to do the actual
524 work. This is the function that must be called when reading iconv
525 configuration. */
526void
527__gconv_load_conf (void)
528{
529 __libc_once (once, __gconv_read_conf);
530}
531
532
533/* Free all resources if necessary. */
534libc_freeres_fn (free_mem)
535{
536 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
537 free ((void *) __gconv_path_elem);
538}
539