Bug 23308: Update to Unicode 11.0.0
[glibc.git] / iconv / gconv_conf.c
blobf173cde71b2a61d7eea4b9fd63d033a064a365f5
1 /* Handle configuration data.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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.
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.
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 <http://www.gnu.org/licenses/>. */
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>
34 #include <libc-lock.h>
35 #include <gconv_int.h>
38 /* This is the default path where we look for module lists. */
39 static const char default_gconv_path[] = GCONV_PATH;
41 /* The path elements, as determined by the __gconv_get_path function.
42 All path elements end in a slash. */
43 struct path_elem *__gconv_path_elem;
44 /* Maximum length of a single path element in __gconv_path_elem. */
45 size_t __gconv_max_path_elem_len;
47 /* We use the following struct if we couldn't allocate memory. */
48 static const struct path_elem empty_path_elem = { NULL, 0 };
50 /* Name of the file containing the module information in the directories
51 along the path. */
52 static const char gconv_conf_filename[] = "gconv-modules";
54 /* Filename extension for the modules. */
55 #ifndef MODULE_EXT
56 # define MODULE_EXT ".so"
57 #endif
58 static const char gconv_module_ext[] = MODULE_EXT;
60 /* We have a few builtin transformations. */
61 static struct gconv_module builtin_modules[] =
63 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
64 MinF, MaxF, MinT, MaxT) \
65 { \
66 .from_string = From, \
67 .to_string = To, \
68 .cost_hi = Cost, \
69 .cost_lo = INT_MAX, \
70 .module_name = Name \
72 #define BUILTIN_ALIAS(From, To)
74 #include "gconv_builtin.h"
76 #undef BUILTIN_TRANSFORMATION
77 #undef BUILTIN_ALIAS
80 static const char builtin_aliases[] =
82 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
83 MinF, MaxF, MinT, MaxT)
84 #define BUILTIN_ALIAS(From, To) From "\0" To "\0"
86 #include "gconv_builtin.h"
88 #undef BUILTIN_TRANSFORMATION
89 #undef BUILTIN_ALIAS
92 #include <libio/libioP.h>
93 #define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
96 /* Value of the GCONV_PATH environment variable. */
97 const char *__gconv_path_envvar;
100 /* Test whether there is already a matching module known. */
101 static int
102 detect_conflict (const char *alias)
104 struct gconv_module *node = __gconv_modules_db;
106 while (node != NULL)
108 int cmpres = strcmp (alias, node->from_string);
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;
119 return node != NULL;
123 /* The actual code to add aliases. */
124 static void
125 add_alias2 (const char *from, const char *to, const char *wp, void *modules)
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;
132 struct gconv_alias *new_alias = (struct gconv_alias *)
133 malloc (sizeof (struct gconv_alias) + (wp - from));
134 if (new_alias != NULL)
136 void **inserted;
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);
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);
152 /* Add new alias. */
153 static void
154 add_alias (char *rp, void *modules)
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;
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';
179 add_alias2 (from, to, wp, modules);
183 /* Insert a data structure for a new module in the search tree. */
184 static void
185 insert_module (struct gconv_module *newp, int tobefreed)
187 struct gconv_module **rootp = &__gconv_modules_db;
189 while (*rootp != NULL)
191 struct gconv_module *root = *rootp;
192 int cmpres;
194 cmpres = strcmp (newp->from_string, root->from_string);
195 if (cmpres == 0)
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)
202 rootp = &root->same;
203 root = *rootp;
204 if (root == NULL)
205 break;
208 if (root != NULL)
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))
216 newp->left = root->left;
217 newp->right = root->right;
218 newp->same = root->same;
219 *rootp = newp;
221 free (root);
223 else if (tobefreed)
224 free (newp);
225 return;
228 break;
230 else if (cmpres < 0)
231 rootp = &root->left;
232 else
233 rootp = &root->right;
236 /* Plug in the new node here. */
237 *rootp = newp;
241 /* Add new module. */
242 static void
243 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
244 size_t *nmodules, int modcounter)
246 /* We expect now
247 1. `from' name
248 2. `to' name
249 3. filename of the module
250 4. an optional cost value
252 struct gconv_alias fake_alias;
253 struct gconv_module *new_module;
254 char *from, *to, *module, *wp;
255 int need_ext;
256 int cost_hi;
258 while (__isspace_l (*rp, _nl_C_locobj_ptr))
259 ++rp;
260 from = rp;
261 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
263 *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
264 ++rp;
266 if (*rp == '\0')
267 return;
268 *rp++ = '\0';
269 to = wp = rp;
270 while (__isspace_l (*rp, _nl_C_locobj_ptr))
271 ++rp;
272 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
273 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
274 if (*rp == '\0')
275 return;
276 *wp++ = '\0';
278 ++rp;
279 while (__isspace_l (*rp, _nl_C_locobj_ptr));
280 module = wp;
281 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
282 *wp++ = *rp++;
283 if (*rp == '\0')
285 /* There is no cost, use one by default. */
286 *wp++ = '\0';
287 cost_hi = 1;
289 else
291 /* There might be a cost value. */
292 char *endp;
294 *wp++ = '\0';
295 cost_hi = strtol (rp, &endp, 10);
296 if (rp == endp || cost_hi < 1)
297 /* No useful information. */
298 cost_hi = 1;
301 if (module[0] == '\0')
302 /* No module name given. */
303 return;
304 if (module[0] == '/')
305 dir_len = 0;
307 /* See whether we must add the ending. */
308 need_ext = 0;
309 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
310 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
311 sizeof (gconv_module_ext)) != 0)
312 /* We must add the module extension. */
313 need_ext = sizeof (gconv_module_ext) - 1;
315 /* See whether we have already an alias with this name defined. */
316 fake_alias.fromname = strndupa (from, to - from);
318 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
319 /* This module duplicates an alias. */
320 return;
322 new_module = (struct gconv_module *) calloc (1,
323 sizeof (struct gconv_module)
324 + (wp - from)
325 + dir_len + need_ext);
326 if (new_module != NULL)
328 char *tmp;
330 new_module->from_string = tmp = (char *) (new_module + 1);
331 tmp = __mempcpy (tmp, from, to - from);
333 new_module->to_string = tmp;
334 tmp = __mempcpy (tmp, to, module - to);
336 new_module->cost_hi = cost_hi;
337 new_module->cost_lo = modcounter;
339 new_module->module_name = tmp;
341 if (dir_len != 0)
342 tmp = __mempcpy (tmp, directory, dir_len);
344 tmp = __mempcpy (tmp, module, wp - module);
346 if (need_ext)
347 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
349 /* Now insert the new module data structure in our search tree. */
350 insert_module (new_module, 1);
355 /* Read the next configuration file. */
356 static void
357 read_conf_file (const char *filename, const char *directory, size_t dir_len,
358 void **modules, size_t *nmodules)
360 /* Note the file is opened with cancellation in the I/O functions
361 disabled. */
362 FILE *fp = fopen (filename, "rce");
363 char *line = NULL;
364 size_t line_len = 0;
365 static int modcounter;
367 /* Don't complain if a file is not present or readable, simply silently
368 ignore it. */
369 if (fp == NULL)
370 return;
372 /* No threads reading from this stream. */
373 __fsetlocking (fp, FSETLOCKING_BYCALLER);
375 /* Process the known entries of the file. Comments start with `#' and
376 end with the end of the line. Empty lines are ignored. */
377 while (!__feof_unlocked (fp))
379 char *rp, *endp, *word;
380 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
381 if (n < 0)
382 /* An error occurred. */
383 break;
385 rp = line;
386 /* Terminate the line (excluding comments or newline) by an NUL byte
387 to simplify the following code. */
388 endp = strchr (rp, '#');
389 if (endp != NULL)
390 *endp = '\0';
391 else
392 if (rp[n - 1] == '\n')
393 rp[n - 1] = '\0';
395 while (__isspace_l (*rp, _nl_C_locobj_ptr))
396 ++rp;
398 /* If this is an empty line go on with the next one. */
399 if (rp == endp)
400 continue;
402 word = rp;
403 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
404 ++rp;
406 if (rp - word == sizeof ("alias") - 1
407 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
408 add_alias (rp, *modules);
409 else if (rp - word == sizeof ("module") - 1
410 && memcmp (word, "module", sizeof ("module") - 1) == 0)
411 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
412 /* else */
413 /* Otherwise ignore the line. */
416 free (line);
418 fclose (fp);
422 /* Determine the directories we are looking for data in. */
423 void
424 __gconv_get_path (void)
426 struct path_elem *result;
427 __libc_lock_define_initialized (static, lock);
429 __libc_lock_lock (lock);
431 /* Make sure there wasn't a second thread doing it already. */
432 result = (struct path_elem *) __gconv_path_elem;
433 if (result == NULL)
435 /* Determine the complete path first. */
436 char *gconv_path;
437 size_t gconv_path_len;
438 char *elem;
439 char *oldp;
440 char *cp;
441 int nelems;
442 char *cwd;
443 size_t cwdlen;
445 if (__gconv_path_envvar == NULL)
447 /* No user-defined path. Make a modifiable copy of the
448 default path. */
449 gconv_path = strdupa (default_gconv_path);
450 gconv_path_len = sizeof (default_gconv_path);
451 cwd = NULL;
452 cwdlen = 0;
454 else
456 /* Append the default path to the user-defined path. */
457 size_t user_len = strlen (__gconv_path_envvar);
459 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
460 gconv_path = alloca (gconv_path_len);
461 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
462 user_len),
463 ":", 1),
464 default_gconv_path, sizeof (default_gconv_path));
465 cwd = __getcwd (NULL, 0);
466 cwdlen = __glibc_unlikely (cwd == NULL) ? 0 : strlen (cwd);
468 assert (default_gconv_path[0] == '/');
470 /* In a first pass we calculate the number of elements. */
471 oldp = NULL;
472 cp = strchr (gconv_path, ':');
473 nelems = 1;
474 while (cp != NULL)
476 if (cp != oldp + 1)
477 ++nelems;
478 oldp = cp;
479 cp = strchr (cp + 1, ':');
482 /* Allocate the memory for the result. */
483 result = (struct path_elem *) malloc ((nelems + 1)
484 * sizeof (struct path_elem)
485 + gconv_path_len + nelems
486 + (nelems - 1) * (cwdlen + 1));
487 if (result != NULL)
489 char *strspace = (char *) &result[nelems + 1];
490 int n = 0;
492 /* Separate the individual parts. */
493 __gconv_max_path_elem_len = 0;
494 elem = __strtok_r (gconv_path, ":", &gconv_path);
495 assert (elem != NULL);
498 result[n].name = strspace;
499 if (elem[0] != '/')
501 assert (cwd != NULL);
502 strspace = __mempcpy (strspace, cwd, cwdlen);
503 *strspace++ = '/';
505 strspace = __stpcpy (strspace, elem);
506 if (strspace[-1] != '/')
507 *strspace++ = '/';
509 result[n].len = strspace - result[n].name;
510 if (result[n].len > __gconv_max_path_elem_len)
511 __gconv_max_path_elem_len = result[n].len;
513 *strspace++ = '\0';
514 ++n;
516 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
518 result[n].name = NULL;
519 result[n].len = 0;
522 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
524 free (cwd);
527 __libc_lock_unlock (lock);
531 /* Read all configuration files found in the user-specified and the default
532 path. */
533 void
534 attribute_hidden
535 __gconv_read_conf (void)
537 void *modules = NULL;
538 size_t nmodules = 0;
539 int save_errno = errno;
540 size_t cnt;
542 /* First see whether we should use the cache. */
543 if (__gconv_load_cache () == 0)
545 /* Yes, we are done. */
546 __set_errno (save_errno);
547 return;
550 #ifndef STATIC_GCONV
551 /* Find out where we have to look. */
552 if (__gconv_path_elem == NULL)
553 __gconv_get_path ();
555 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
557 const char *elem = __gconv_path_elem[cnt].name;
558 size_t elem_len = __gconv_path_elem[cnt].len;
559 char *filename;
561 /* No slash needs to be inserted between elem and gconv_conf_filename;
562 elem already ends in a slash. */
563 filename = alloca (elem_len + sizeof (gconv_conf_filename));
564 __mempcpy (__mempcpy (filename, elem, elem_len),
565 gconv_conf_filename, sizeof (gconv_conf_filename));
567 /* Read the next configuration file. */
568 read_conf_file (filename, elem, elem_len, &modules, &nmodules);
570 #endif
572 /* Add the internal modules. */
573 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
574 ++cnt)
576 struct gconv_alias fake_alias;
578 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
580 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
581 != NULL)
582 /* It'll conflict so don't add it. */
583 continue;
585 insert_module (&builtin_modules[cnt], 0);
588 /* Add aliases for builtin conversions. */
589 const char *cp = builtin_aliases;
592 const char *from = cp;
593 const char *to = __rawmemchr (from, '\0') + 1;
594 cp = __rawmemchr (to, '\0') + 1;
596 add_alias2 (from, to, cp, modules);
598 while (*cp != '\0');
600 /* Restore the error number. */
601 __set_errno (save_errno);
606 /* Free all resources if necessary. */
607 libc_freeres_fn (free_mem)
609 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
610 free ((void *) __gconv_path_elem);