]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/gconv_conf.c
Handle DT_UNKNOWN in gconv-modules.d
[thirdparty/glibc.git] / iconv / gconv_conf.c
CommitLineData
6973fc01 1/* Handle configuration data.
2b778ceb 2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
6973fc01
UD
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
41bdb6e2
AJ
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.
6973fc01
UD
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
41bdb6e2 14 Lesser General Public License for more details.
6973fc01 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
6973fc01 19
d6204268 20#include <assert.h>
6973fc01 21#include <ctype.h>
e34b0f29 22#include <errno.h>
fab6d621 23#include <limits.h>
5db91571 24#include <locale.h>
6973fc01 25#include <search.h>
17427edd 26#include <stddef.h>
6973fc01 27#include <stdio.h>
2706ee38 28#include <stdio_ext.h>
6973fc01
UD
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <sys/param.h>
33
ec999b8e 34#include <libc-lock.h>
e62c19f1 35#include <gconv_int.h>
d8e8097f 36#include <gconv_parseconfdir.h>
6973fc01
UD
37
38/* This is the default path where we look for module lists. */
39static const char default_gconv_path[] = GCONV_PATH;
40
335a3b0a
AS
41/* Type to represent search path. */
42struct path_elem
43{
44 const char *name;
45 size_t len;
46};
47
835bf8e0
UD
48/* The path elements, as determined by the __gconv_get_path function.
49 All path elements end in a slash. */
17427edd 50struct path_elem *__gconv_path_elem;
835bf8e0 51/* Maximum length of a single path element in __gconv_path_elem. */
d6204268
UD
52size_t __gconv_max_path_elem_len;
53
54/* We use the following struct if we couldn't allocate memory. */
4c038b68 55static const struct path_elem empty_path_elem = { NULL, 0 };
d6204268 56
e34b0f29
UD
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
6973fc01
UD
63/* We have a few builtin transformations. */
64static struct gconv_module builtin_modules[] =
65{
f9ad060c
UD
66#define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
67 MinF, MaxF, MinT, MaxT) \
6973fc01 68 { \
0274d73c
RM
69 .from_string = From, \
70 .to_string = To, \
71 .cost_hi = Cost, \
72 .cost_lo = INT_MAX, \
73 .module_name = Name \
6973fc01 74 },
5891046a
UD
75#define BUILTIN_ALIAS(From, To)
76
77#include "gconv_builtin.h"
5891046a
UD
78
79#undef BUILTIN_TRANSFORMATION
80#undef BUILTIN_ALIAS
f9ad060c 81};
5891046a 82
88dbff8c 83static const char builtin_aliases[] =
5891046a 84{
f9ad060c
UD
85#define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
86 MinF, MaxF, MinT, MaxT)
88dbff8c 87#define BUILTIN_ALIAS(From, To) From "\0" To "\0"
6973fc01
UD
88
89#include "gconv_builtin.h"
f9ad060c
UD
90
91#undef BUILTIN_TRANSFORMATION
92#undef BUILTIN_ALIAS
6973fc01
UD
93};
94
95
6b98979f
UD
96/* Value of the GCONV_PATH environment variable. */
97const char *__gconv_path_envvar;
98
99
2bd60880 100/* Test whether there is already a matching module known. */
6973fc01 101static int
d2dfc5de 102detect_conflict (const char *alias)
6973fc01 103{
2bd60880 104 struct gconv_module *node = __gconv_modules_db;
6973fc01 105
2bd60880 106 while (node != NULL)
6973fc01 107 {
d2dfc5de 108 int cmpres = strcmp (alias, node->from_string);
8c479619 109
2bd60880 110 if (cmpres == 0)
d2dfc5de
UD
111 /* We have a conflict. */
112 return 1;
2bd60880
UD
113 else if (cmpres < 0)
114 node = node->left;
115 else
116 node = node->right;
8c479619 117 }
2bd60880
UD
118
119 return node != NULL;
8c479619
UD
120}
121
122
88dbff8c
UD
123/* The actual code to add aliases. */
124static void
23e15ea1 125add_alias2 (const char *from, const char *to, const char *wp)
88dbff8c
UD
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
a334319f 152/* Add new alias. */
dd9423a6 153static void
23e15ea1 154add_alias (char *rp)
6973fc01 155{
a334319f
UD
156 /* We now expect two more string. The strings are normalized
157 (converted to UPPER case) and strored in the alias database. */
a334319f
UD
158 char *from, *to, *wp;
159
4b5b009c 160 while (__isspace_l (*rp, _nl_C_locobj_ptr))
a334319f
UD
161 ++rp;
162 from = wp = rp;
4b5b009c
UD
163 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
164 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
a334319f
UD
165 if (*rp == '\0')
166 /* There is no `to' string on the line. Ignore it. */
167 return;
168 *wp++ = '\0';
169 to = ++rp;
4b5b009c 170 while (__isspace_l (*rp, _nl_C_locobj_ptr))
a334319f 171 ++rp;
4b5b009c
UD
172 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
173 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
a334319f
UD
174 if (to == wp)
175 /* No `to' string, ignore the line. */
176 return;
177 *wp++ = '\0';
178
23e15ea1 179 add_alias2 (from, to, wp);
6973fc01
UD
180}
181
182
2bd60880 183/* Insert a data structure for a new module in the search tree. */
dd9423a6 184static void
b2fe29dd 185insert_module (struct gconv_module *newp, int tobefreed)
2bd60880
UD
186{
187 struct gconv_module **rootp = &__gconv_modules_db;
188
189 while (*rootp != NULL)
190 {
191 struct gconv_module *root = *rootp;
2bd60880
UD
192 int cmpres;
193
d2dfc5de 194 cmpres = strcmp (newp->from_string, root->from_string);
2bd60880
UD
195 if (cmpres == 0)
196 {
d2dfc5de
UD
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)
2bd60880 201 {
d2dfc5de
UD
202 rootp = &root->same;
203 root = *rootp;
204 if (root == NULL)
205 break;
2bd60880
UD
206 }
207
d2dfc5de 208 if (root != NULL)
b2fe29dd 209 {
2debc8c5
UD
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 {
9c0592ab
UD
216 newp->left = root->left;
217 newp->right = root->right;
218 newp->same = root->same;
219 *rootp = newp;
2debc8c5 220
9c0592ab
UD
221 free (root);
222 }
223 else if (tobefreed)
b2fe29dd
UD
224 free (newp);
225 return;
226 }
2bd60880 227
d2dfc5de 228 break;
2bd60880
UD
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
6973fc01 241/* Add new module. */
d2dfc5de 242static void
23e15ea1 243add_module (char *rp, const char *directory, size_t dir_len, int modcounter)
6973fc01
UD
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 */
d2dfc5de 251 struct gconv_alias fake_alias;
6973fc01
UD
252 struct gconv_module *new_module;
253 char *from, *to, *module, *wp;
e34b0f29 254 int need_ext;
fab6d621 255 int cost_hi;
6973fc01 256
4b5b009c 257 while (__isspace_l (*rp, _nl_C_locobj_ptr))
6973fc01
UD
258 ++rp;
259 from = rp;
4b5b009c 260 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
6973fc01 261 {
4b5b009c 262 *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
6973fc01
UD
263 ++rp;
264 }
265 if (*rp == '\0')
266 return;
267 *rp++ = '\0';
268 to = wp = rp;
4b5b009c 269 while (__isspace_l (*rp, _nl_C_locobj_ptr))
6973fc01 270 ++rp;
4b5b009c
UD
271 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
272 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
6973fc01
UD
273 if (*rp == '\0')
274 return;
275 *wp++ = '\0';
276 do
277 ++rp;
4b5b009c 278 while (__isspace_l (*rp, _nl_C_locobj_ptr));
6973fc01 279 module = wp;
4b5b009c 280 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
6973fc01
UD
281 *wp++ = *rp++;
282 if (*rp == '\0')
283 {
284 /* There is no cost, use one by default. */
285 *wp++ = '\0';
fab6d621 286 cost_hi = 1;
6973fc01
UD
287 }
288 else
289 {
290 /* There might be a cost value. */
291 char *endp;
292
293 *wp++ = '\0';
fab6d621 294 cost_hi = strtol (rp, &endp, 10);
2bd60880 295 if (rp == endp || cost_hi < 1)
6973fc01 296 /* No useful information. */
fab6d621 297 cost_hi = 1;
6973fc01
UD
298 }
299
300 if (module[0] == '\0')
301 /* No module name given. */
302 return;
303 if (module[0] == '/')
304 dir_len = 0;
6973fc01 305
e34b0f29
UD
306 /* See whether we must add the ending. */
307 need_ext = 0;
17427edd 308 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
e34b0f29
UD
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
d2dfc5de
UD
314 /* See whether we have already an alias with this name defined. */
315 fake_alias.fromname = strndupa (from, to - from);
6973fc01 316
d2dfc5de
UD
317 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
318 /* This module duplicates an alias. */
319 return;
6973fc01 320
2bd60880
UD
321 new_module = (struct gconv_module *) calloc (1,
322 sizeof (struct gconv_module)
e34b0f29
UD
323 + (wp - from)
324 + dir_len + need_ext);
6973fc01
UD
325 if (new_module != NULL)
326 {
e34b0f29
UD
327 char *tmp;
328
17427edd
UD
329 new_module->from_string = tmp = (char *) (new_module + 1);
330 tmp = __mempcpy (tmp, from, to - from);
e34b0f29 331
17427edd
UD
332 new_module->to_string = tmp;
333 tmp = __mempcpy (tmp, to, module - to);
6973fc01 334
fab6d621
UD
335 new_module->cost_hi = cost_hi;
336 new_module->cost_lo = modcounter;
6973fc01 337
17427edd 338 new_module->module_name = tmp;
e34b0f29 339
17427edd
UD
340 if (dir_len != 0)
341 tmp = __mempcpy (tmp, directory, dir_len);
6973fc01 342
e34b0f29
UD
343 tmp = __mempcpy (tmp, module, wp - module);
344
345 if (need_ext)
346 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
347
2bd60880 348 /* Now insert the new module data structure in our search tree. */
b2fe29dd 349 insert_module (new_module, 1);
6973fc01
UD
350 }
351}
352
353
c5288d37
AS
354/* Determine the directories we are looking for data in. This function should
355 only be called from __gconv_read_conf. */
335a3b0a 356static void
d6204268
UD
357__gconv_get_path (void)
358{
359 struct path_elem *result;
d6204268 360
c5288d37
AS
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)
d6204268 376 {
c5288d37
AS
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] == '/');
d6204268 399
c5288d37
AS
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 }
40739d9f 411
c5288d37
AS
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;
d6204268
UD
450 }
451
c5288d37
AS
452 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
453
454 free (cwd);
d6204268
UD
455}
456
457
6973fc01 458/* Read all configuration files found in the user-specified and the default
c5288d37
AS
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
6973fc01
UD
463__gconv_read_conf (void)
464{
e34b0f29 465 int save_errno = errno;
5891046a 466 size_t cnt;
6973fc01 467
6b98979f
UD
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
d6204268 477 /* Find out where we have to look. */
6d6ee046 478 __gconv_get_path ();
6973fc01 479
d6204268 480 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
d8e8097f
SP
481 gconv_parseconfdir (__gconv_path_elem[cnt].name,
482 __gconv_path_elem[cnt].len);
6b98979f 483#endif
6973fc01 484
2bd60880
UD
485 /* Add the internal modules. */
486 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
487 ++cnt)
6973fc01 488 {
d2dfc5de 489 struct gconv_alias fake_alias;
6973fc01 490
8a0746ae 491 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
6973fc01 492
d2dfc5de
UD
493 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
494 != NULL)
495 /* It'll conflict so don't add it. */
496 continue;
2bd60880 497
b2fe29dd 498 insert_module (&builtin_modules[cnt], 0);
e34b0f29 499 }
6973fc01 500
5891046a 501 /* Add aliases for builtin conversions. */
88dbff8c
UD
502 const char *cp = builtin_aliases;
503 do
5891046a 504 {
88dbff8c
UD
505 const char *from = cp;
506 const char *to = __rawmemchr (from, '\0') + 1;
507 cp = __rawmemchr (to, '\0') + 1;
508
23e15ea1 509 add_alias2 (from, to, cp);
5891046a 510 }
88dbff8c 511 while (*cp != '\0');
5891046a 512
e34b0f29
UD
513 /* Restore the error number. */
514 __set_errno (save_errno);
6973fc01 515}
d6204268
UD
516
517
c5288d37
AS
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
d6204268
UD
532
533/* Free all resources if necessary. */
c877418f 534libc_freeres_fn (free_mem)
d6204268
UD
535{
536 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
537 free ((void *) __gconv_path_elem);
538}