1 /* Handle configuration data.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 #include <stdio_ext.h>
31 #include <sys/param.h>
33 #include <libc-lock.h>
34 #include <gconv_int.h>
35 #include <gconv_parseconfdir.h>
37 /* This is the default path where we look for module lists. */
38 static const char default_gconv_path[] = GCONV_PATH;
40 /* Type to represent search path. */
47 /* The path elements, as determined by the __gconv_get_path function.
48 All path elements end in a slash. */
49 struct path_elem *__gconv_path_elem;
50 /* Maximum length of a single path element in __gconv_path_elem. */
51 size_t __gconv_max_path_elem_len;
53 /* We use the following struct if we couldn't allocate memory. */
54 static const struct path_elem empty_path_elem = { NULL, 0 };
56 /* Filename extension for the modules. */
58 # define MODULE_EXT ".so"
60 static const char gconv_module_ext[] = MODULE_EXT;
62 /* We have a few builtin transformations. */
63 static struct gconv_module builtin_modules[] =
65 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
66 MinF, MaxF, MinT, MaxT) \
68 .from_string = From, \
74 #define BUILTIN_ALIAS(From, To)
76 #include "gconv_builtin.h"
78 #undef BUILTIN_TRANSFORMATION
82 static const char builtin_aliases[] =
84 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
85 MinF, MaxF, MinT, MaxT)
86 #define BUILTIN_ALIAS(From, To) From "\0" To "\0"
88 #include "gconv_builtin.h"
90 #undef BUILTIN_TRANSFORMATION
95 /* Value of the GCONV_PATH environment variable. */
96 const char *__gconv_path_envvar;
99 /* Test whether there is already a matching module known. */
101 detect_conflict (const char *alias)
103 struct gconv_module *node = __gconv_modules_db;
107 int cmpres = strcmp (alias, node->from_string);
110 /* We have a conflict. */
122 /* The actual code to add aliases. */
124 add_alias2 (const char *from, const char *to, const char *wp)
126 /* Test whether this alias conflicts with any available module. */
127 if (detect_conflict (from))
128 /* It does conflict, don't add the alias. */
131 struct gconv_alias *new_alias = (struct gconv_alias *)
132 malloc (sizeof (struct gconv_alias) + (wp - from));
133 if (new_alias != NULL)
137 new_alias->fromname = memcpy ((char *) new_alias
138 + sizeof (struct gconv_alias),
140 new_alias->toname = new_alias->fromname + (to - from);
142 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
143 __gconv_alias_compare);
144 if (inserted == NULL || *inserted != new_alias)
145 /* Something went wrong, free this entry. */
155 /* We now expect two more string. The strings are normalized
156 (converted to UPPER case) and stored in the alias database. */
157 char *from, *to, *wp;
159 while (__isspace_l (*rp, _nl_C_locobj_ptr))
162 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
163 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
165 /* There is no `to' string on the line. Ignore it. */
169 while (__isspace_l (*rp, _nl_C_locobj_ptr))
171 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
172 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
174 /* No `to' string, ignore the line. */
178 add_alias2 (from, to, wp);
182 /* Insert a data structure for a new module in the search tree. */
184 insert_module (struct gconv_module *newp, int tobefreed)
186 struct gconv_module **rootp = &__gconv_modules_db;
188 while (*rootp != NULL)
190 struct gconv_module *root = *rootp;
193 cmpres = strcmp (newp->from_string, root->from_string);
196 /* Both strings are identical. Insert the string at the
197 end of the `same' list if it is not already there. */
198 while (strcmp (newp->from_string, root->from_string) != 0
199 || strcmp (newp->to_string, root->to_string) != 0)
209 /* This is a no new conversion. But maybe the cost is
211 if (newp->cost_hi < root->cost_hi
212 || (newp->cost_hi == root->cost_hi
213 && newp->cost_lo < root->cost_lo))
215 newp->left = root->left;
216 newp->right = root->right;
217 newp->same = root->same;
232 rootp = &root->right;
235 /* Plug in the new node here. */
240 /* Add new module. */
242 add_module (char *rp, const char *directory, size_t dir_len, int modcounter)
247 3. filename of the module
248 4. an optional cost value
250 struct gconv_alias fake_alias;
251 struct gconv_module *new_module;
252 char *from, *to, *module, *wp;
256 while (__isspace_l (*rp, _nl_C_locobj_ptr))
259 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
261 *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
268 while (__isspace_l (*rp, _nl_C_locobj_ptr))
270 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
271 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
277 while (__isspace_l (*rp, _nl_C_locobj_ptr));
279 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
283 /* There is no cost, use one by default. */
289 /* There might be a cost value. */
293 cost_hi = strtol (rp, &endp, 10);
294 if (rp == endp || cost_hi < 1)
295 /* No useful information. */
299 if (module[0] == '\0')
300 /* No module name given. */
302 if (module[0] == '/')
305 /* See whether we must add the ending. */
307 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
308 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
309 sizeof (gconv_module_ext)) != 0)
310 /* We must add the module extension. */
311 need_ext = sizeof (gconv_module_ext) - 1;
313 /* See whether we have already an alias with this name defined. */
314 fake_alias.fromname = strndupa (from, to - from);
316 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
317 /* This module duplicates an alias. */
320 new_module = (struct gconv_module *) calloc (1,
321 sizeof (struct gconv_module)
323 + dir_len + need_ext);
324 if (new_module != NULL)
328 new_module->from_string = tmp = (char *) (new_module + 1);
329 tmp = __mempcpy (tmp, from, to - from);
331 new_module->to_string = tmp;
332 tmp = __mempcpy (tmp, to, module - to);
334 new_module->cost_hi = cost_hi;
335 new_module->cost_lo = modcounter;
337 new_module->module_name = tmp;
340 tmp = __mempcpy (tmp, directory, dir_len);
342 tmp = __mempcpy (tmp, module, wp - module);
345 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
347 /* Now insert the new module data structure in our search tree. */
348 insert_module (new_module, 1);
353 /* Determine the directories we are looking for data in. This function should
354 only be called from __gconv_read_conf. */
356 __gconv_get_path (void)
358 struct path_elem *result;
360 /* This function is only ever called when __gconv_path_elem is NULL. */
361 result = __gconv_path_elem;
362 assert (result == NULL);
364 /* Determine the complete path first. */
366 size_t gconv_path_len;
374 if (__gconv_path_envvar == NULL)
376 /* No user-defined path. Make a modifiable copy of the
378 gconv_path = strdupa (default_gconv_path);
379 gconv_path_len = sizeof (default_gconv_path);
385 /* Append the default path to the user-defined path. */
386 size_t user_len = strlen (__gconv_path_envvar);
388 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
389 gconv_path = alloca (gconv_path_len);
390 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
393 default_gconv_path, sizeof (default_gconv_path));
394 cwd = __getcwd (NULL, 0);
395 cwdlen = __glibc_unlikely (cwd == NULL) ? 0 : strlen (cwd);
397 assert (default_gconv_path[0] == '/');
399 /* In a first pass we calculate the number of elements. */
401 cp = strchr (gconv_path, ':');
408 cp = strchr (cp + 1, ':');
411 /* Allocate the memory for the result. */
412 result = malloc ((nelems + 1)
413 * sizeof (struct path_elem)
414 + gconv_path_len + nelems
415 + (nelems - 1) * (cwdlen + 1));
418 char *strspace = (char *) &result[nelems + 1];
421 /* Separate the individual parts. */
422 __gconv_max_path_elem_len = 0;
423 elem = __strtok_r (gconv_path, ":", &gconv_path);
424 assert (elem != NULL);
427 result[n].name = strspace;
430 assert (cwd != NULL);
431 strspace = __mempcpy (strspace, cwd, cwdlen);
434 strspace = __stpcpy (strspace, elem);
435 if (strspace[-1] != '/')
438 result[n].len = strspace - result[n].name;
439 if (result[n].len > __gconv_max_path_elem_len)
440 __gconv_max_path_elem_len = result[n].len;
445 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
447 result[n].name = NULL;
451 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
457 /* Read all configuration files found in the user-specified and the default
458 path. This function should only be called once during the program's
459 lifetime. It disregards locking and synchronization because its only
460 caller, __gconv_load_conf, handles this. */
462 __gconv_read_conf (void)
464 int save_errno = errno;
467 /* First see whether we should use the cache. */
468 if (__gconv_load_cache () == 0)
470 /* Yes, we are done. */
471 __set_errno (save_errno);
476 /* Find out where we have to look. */
479 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
480 gconv_parseconfdir (NULL, __gconv_path_elem[cnt].name,
481 __gconv_path_elem[cnt].len);
484 /* Add the internal modules. */
485 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
488 struct gconv_alias fake_alias;
490 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
492 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
494 /* It'll conflict so don't add it. */
497 insert_module (&builtin_modules[cnt], 0);
500 /* Add aliases for builtin conversions. */
501 const char *cp = builtin_aliases;
504 const char *from = cp;
505 const char *to = strchr (from, '\0') + 1;
506 cp = strchr (to, '\0') + 1;
508 add_alias2 (from, to, cp);
512 /* Restore the error number. */
513 __set_errno (save_errno);
517 /* This "once" variable is used to do a one-time load of the configuration. */
518 __libc_once_define (static, once);
521 /* Read all configuration files found in the user-specified and the default
522 path, but do it only "once" using __gconv_read_conf to do the actual
523 work. This is the function that must be called when reading iconv
526 __gconv_load_conf (void)
528 __libc_once (once, __gconv_read_conf);
532 /* Free all resources if necessary. */
534 __gconv_conf_freemem (void)
536 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
537 free ((void *) __gconv_path_elem);