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