iconvconfig.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /* Generate fastloading iconv module configuration files.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  14. #include <argp.h>
  15. #include <assert.h>
  16. #include <error.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <libintl.h>
  20. #include <locale.h>
  21. #include <mcheck.h>
  22. #include <search.h>
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include <stdio.h>
  26. #include <stdio_ext.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/cdefs.h>
  31. #include <sys/uio.h>
  32. #include "iconvconfig.h"
  33. #include <gconv_parseconfdir.h>
  34. /* Get libc version number. */
  35. #include "../version.h"
  36. #define PACKAGE _libc_intl_domainname
  37. /* The hashing function we use. */
  38. #include "../intl/hash-string.h"
  39. /* Types used. */
  40. struct module
  41. {
  42. char *fromname;
  43. struct Strent *fromname_strent;
  44. char *filename;
  45. struct Strent *filename_strent;
  46. const char *directory;
  47. struct Strent *directory_strent;
  48. struct module *next;
  49. int cost;
  50. struct Strent *toname_strent;
  51. char toname[0];
  52. };
  53. struct alias
  54. {
  55. char *fromname;
  56. struct Strent *froment;
  57. struct module *module;
  58. struct Strent *toent;
  59. char toname[0];
  60. };
  61. struct name
  62. {
  63. const char *name;
  64. struct Strent *strent;
  65. int module_idx;
  66. uint32_t hashval;
  67. };
  68. struct name_info
  69. {
  70. const char *canonical_name;
  71. struct Strent *canonical_strent;
  72. struct module *from_internal;
  73. struct module *to_internal;
  74. struct other_conv_list
  75. {
  76. int dest_idx;
  77. struct other_conv
  78. {
  79. gidx_t module_idx;
  80. struct module *module;
  81. struct other_conv *next;
  82. } other_conv;
  83. struct other_conv_list *next;
  84. } *other_conv_list;
  85. };
  86. /* Name and version of program. */
  87. static void print_version (FILE *stream, struct argp_state *state);
  88. void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
  89. /* Short description of program. */
  90. static const char doc[] = N_("\
  91. Create fastloading iconv module configuration file.");
  92. /* Strings for arguments in help texts. */
  93. static const char args_doc[] = N_("[DIR...]");
  94. /* Prototype for option handler. */
  95. static error_t parse_opt (int key, char *arg, struct argp_state *state);
  96. /* Function to print some extra text in the help message. */
  97. static char *more_help (int key, const char *text, void *input);
  98. /* Definitions of arguments for argp functions. */
  99. #define OPT_PREFIX 300
  100. #define OPT_NOSTDLIB 301
  101. static const struct argp_option options[] =
  102. {
  103. { "prefix", OPT_PREFIX, N_("PATH"), 0,
  104. N_("Prefix used for all file accesses") },
  105. { "output", 'o', N_("FILE"), 0, N_("\
  106. Put output in FILE instead of installed location\
  107. (--prefix does not apply to FILE)") },
  108. { "nostdlib", OPT_NOSTDLIB, NULL, 0,
  109. N_("Do not search standard directories, only those on the command line") },
  110. { NULL, 0, NULL, 0, NULL }
  111. };
  112. /* Data structure to communicate with argp functions. */
  113. static struct argp argp =
  114. {
  115. options, parse_opt, args_doc, doc, NULL, more_help
  116. };
  117. /* The function doing the actual work. */
  118. static int handle_dir (const char *dir);
  119. /* Add all known builtin conversions and aliases. */
  120. static void add_builtins (void);
  121. /* Create list of all aliases without circular aliases. */
  122. static void get_aliases (void);
  123. /* Create list of all modules. */
  124. static void get_modules (void);
  125. /* Get list of all the names and thereby indexing them. */
  126. static void generate_name_list (void);
  127. /* Collect information about all the names. */
  128. static void generate_name_info (void);
  129. /* Write the output file. */
  130. static int write_output (void);
  131. /* Prefix to be used for all file accesses. */
  132. static const char *prefix = "";
  133. /* Its length. */
  134. static size_t prefix_len;
  135. /* Directory to place output file in. */
  136. static const char *output_file;
  137. /* Its length. */
  138. static size_t output_file_len;
  139. /* If true, omit the GCONV_PATH directories and require some arguments. */
  140. static bool nostdlib;
  141. /* Search tree of the modules we know. */
  142. static void *modules;
  143. /* Search tree of the aliases we know. */
  144. static void *aliases;
  145. /* Search tree for name to index mapping. */
  146. static void *names;
  147. /* Number of names we know about. */
  148. static int nnames;
  149. /* List of all aliases. */
  150. static struct alias **alias_list;
  151. static size_t nalias_list;
  152. static size_t nalias_list_max;
  153. /* List of all modules. */
  154. static struct module **module_list;
  155. static size_t nmodule_list;
  156. static size_t nmodule_list_max;
  157. /* Names and information about them. */
  158. static struct name_info *name_info;
  159. static size_t nname_info;
  160. /* Number of translations not from or to INTERNAL. */
  161. static size_t nextra_modules;
  162. /* Names and aliases for the builtin transformations. */
  163. static struct
  164. {
  165. const char *from;
  166. const char *to;
  167. } builtin_alias[] =
  168. {
  169. #define BUILTIN_ALIAS(alias, real) \
  170. { .from = alias, .to = real },
  171. #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
  172. MinF, MaxF, MinT, MaxT)
  173. #include <gconv_builtin.h>
  174. };
  175. #undef BUILTIN_ALIAS
  176. #undef BUILTIN_TRANSFORMATION
  177. #define nbuiltin_alias (sizeof (builtin_alias) / sizeof (builtin_alias[0]))
  178. static struct
  179. {
  180. const char *from;
  181. const char *to;
  182. const char *module;
  183. int cost;
  184. } builtin_trans[] =
  185. {
  186. #define BUILTIN_ALIAS(alias, real)
  187. #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
  188. MinF, MaxF, MinT, MaxT) \
  189. { .from = From, .to = To, .module = Name, .cost = Cost },
  190. #include <gconv_builtin.h>
  191. };
  192. #undef BUILTIN_ALIAS
  193. #undef BUILTIN_TRANSFORMATION
  194. #define nbuiltin_trans (sizeof (builtin_trans) / sizeof (builtin_trans[0]))
  195. /* Filename extension for the modules. */
  196. #ifndef MODULE_EXT
  197. # define MODULE_EXT ".so"
  198. #endif
  199. static const char gconv_module_ext[] = MODULE_EXT;
  200. #include <programs/xmalloc.h>
  201. #include <programs/xasprintf.h>
  202. /* C string table handling. */
  203. struct Strtab;
  204. struct Strent;
  205. /* Create new C string table object in memory. */
  206. extern struct Strtab *strtabinit (void);
  207. /* Free resources allocated for C string table ST. */
  208. extern void strtabfree (struct Strtab *st);
  209. /* Add string STR (length LEN is != 0) to C string table ST. */
  210. extern struct Strent *strtabadd (struct Strtab *st, const char *str,
  211. size_t len);
  212. /* Finalize string table ST and store size in *SIZE and return a pointer. */
  213. extern void *strtabfinalize (struct Strtab *st, size_t *size);
  214. /* Get offset in string table for string associated with SE. */
  215. extern size_t strtaboffset (struct Strent *se);
  216. /* String table we construct. */
  217. static struct Strtab *strtab;
  218. int
  219. main (int argc, char *argv[])
  220. {
  221. int remaining;
  222. int status = 0;
  223. /* Enable memory use testing. */
  224. /* mcheck_pedantic (NULL); */
  225. mtrace ();
  226. /* Set locale via LC_ALL. */
  227. setlocale (LC_ALL, "");
  228. /* Set the text message domain. */
  229. textdomain (_libc_intl_domainname);
  230. /* Parse and process arguments. */
  231. argp_parse (&argp, argc, argv, 0, &remaining, NULL);
  232. if (nostdlib && remaining == argc)
  233. error (2, 0, _("Directory arguments required when using --nostdlib"));
  234. /* Initialize the string table. */
  235. strtab = strtabinit ();
  236. /* Handle all directories mentioned. */
  237. while (remaining < argc)
  238. status |= handle_dir (argv[remaining++]);
  239. if (! nostdlib)
  240. {
  241. /* In any case also handle the standard directory. */
  242. char *path = strdupa (GCONV_PATH), *tp = strsep (&path, ":");
  243. while (tp != NULL)
  244. {
  245. status |= handle_dir (tp);
  246. tp = strsep (&path, ":");
  247. }
  248. }
  249. /* Add the builtin transformations and aliases without overwriting
  250. anything. */
  251. add_builtins ();
  252. /* Store aliases in an array. */
  253. get_aliases ();
  254. /* Get list of all modules. */
  255. get_modules ();
  256. /* Generate list of all the names we know to handle in some way. */
  257. generate_name_list ();
  258. /* Now we know all the names we will handle, collect information
  259. about them. */
  260. generate_name_info ();
  261. /* Write the output file, but only if we haven't seen any error. */
  262. if (status == 0)
  263. status = write_output ();
  264. else
  265. error (1, 0, _("no output file produced because warnings were issued"));
  266. return status;
  267. }
  268. /* Handle program arguments. */
  269. static error_t
  270. parse_opt (int key, char *arg, struct argp_state *state)
  271. {
  272. switch (key)
  273. {
  274. case OPT_PREFIX:
  275. prefix = arg;
  276. prefix_len = strlen (prefix);
  277. break;
  278. case 'o':
  279. output_file = arg;
  280. output_file_len = strlen (output_file);
  281. break;
  282. case OPT_NOSTDLIB:
  283. nostdlib = true;
  284. break;
  285. default:
  286. return ARGP_ERR_UNKNOWN;
  287. }
  288. return 0;
  289. }
  290. static char *
  291. more_help (int key, const char *text, void *input)
  292. {
  293. char *tp = NULL;
  294. switch (key)
  295. {
  296. case ARGP_KEY_HELP_EXTRA:
  297. /* We print some extra information. */
  298. if (asprintf (&tp, gettext ("\
  299. For bug reporting instructions, please see:\n\
  300. %s.\n"), REPORT_BUGS_TO) < 0)
  301. return NULL;
  302. return tp;
  303. default:
  304. break;
  305. }
  306. return (char *) text;
  307. }
  308. /* Print the version information. */
  309. static void
  310. print_version (FILE *stream, struct argp_state *state)
  311. {
  312. fprintf (stream, "iconvconfig %s%s\n", PKGVERSION, VERSION);
  313. fprintf (stream, gettext ("\
  314. Copyright (C) %s Free Software Foundation, Inc.\n\
  315. This is free software; see the source for copying conditions. There is NO\n\
  316. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
  317. "), "2024");
  318. fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
  319. }
  320. static int
  321. alias_compare (const void *p1, const void *p2)
  322. {
  323. const struct alias *a1 = (const struct alias *) p1;
  324. const struct alias *a2 = (const struct alias *) p2;
  325. return strcmp (a1->fromname, a2->fromname);
  326. }
  327. static void
  328. new_alias (const char *fromname, size_t fromlen, const char *toname,
  329. size_t tolen)
  330. {
  331. struct alias *newp;
  332. void **inserted;
  333. newp = (struct alias *) xmalloc (sizeof (struct alias) + fromlen + tolen);
  334. newp->fromname = mempcpy (newp->toname, toname, tolen);
  335. memcpy (newp->fromname, fromname, fromlen);
  336. newp->module = NULL;
  337. inserted = (void **) tsearch (newp, &aliases, alias_compare);
  338. if (inserted == NULL)
  339. error (EXIT_FAILURE, errno, gettext ("while inserting in search tree"));
  340. if (*inserted != newp)
  341. /* Something went wrong, free this entry. */
  342. free (newp);
  343. else
  344. {
  345. newp->froment = strtabadd (strtab, newp->fromname, fromlen);
  346. newp->toent = strtabadd (strtab, newp->toname, tolen);
  347. }
  348. }
  349. /* Add new alias. */
  350. static void
  351. add_alias (char *rp)
  352. {
  353. /* We now expect two more string. The strings are normalized
  354. (converted to UPPER case) and stored in the alias database. */
  355. char *from;
  356. char *to;
  357. char *wp;
  358. while (isspace (*rp))
  359. ++rp;
  360. from = wp = rp;
  361. while (*rp != '\0' && !isspace (*rp))
  362. *wp++ = toupper (*rp++);
  363. if (*rp == '\0')
  364. /* There is no `to' string on the line. Ignore it. */
  365. return;
  366. *wp++ = '\0';
  367. to = ++rp;
  368. while (isspace (*rp))
  369. ++rp;
  370. while (*rp != '\0' && !isspace (*rp))
  371. *wp++ = toupper (*rp++);
  372. if (to == wp)
  373. /* No `to' string, ignore the line. */
  374. return;
  375. *wp++ = '\0';
  376. assert (strlen (from) + 1 == (size_t) (to - from));
  377. assert (strlen (to) + 1 == (size_t) (wp - to));
  378. new_alias (from, to - from, to, wp - to);
  379. }
  380. static void
  381. append_alias (const void *nodep, VISIT value, int level)
  382. {
  383. if (value != leaf && value != postorder)
  384. return;
  385. if (nalias_list_max == nalias_list)
  386. {
  387. nalias_list_max += 50;
  388. alias_list = (struct alias **) xrealloc (alias_list,
  389. (nalias_list_max
  390. * sizeof (struct alias *)));
  391. }
  392. alias_list[nalias_list++] = *(struct alias **) nodep;
  393. }
  394. static void
  395. get_aliases (void)
  396. {
  397. twalk (aliases, append_alias);
  398. }
  399. static int
  400. module_compare (const void *p1, const void *p2)
  401. {
  402. const struct module *m1 = (const struct module *) p1;
  403. const struct module *m2 = (const struct module *) p2;
  404. int result;
  405. result = strcmp (m1->fromname, m2->fromname);
  406. if (result == 0)
  407. result = strcmp (m1->toname, m2->toname);
  408. return result;
  409. }
  410. /* Create new module record. */
  411. static void
  412. new_module (const char *fromname, size_t fromlen, const char *toname,
  413. size_t tolen, const char *dir_in,
  414. const char *filename, size_t filelen, int cost, size_t need_ext)
  415. {
  416. struct module *new_module;
  417. size_t dirlen = strlen (dir_in) + 1;
  418. const char *directory = xstrdup (dir_in);
  419. char *tmp;
  420. void **inserted;
  421. new_module = (struct module *) xmalloc (sizeof (struct module)
  422. + fromlen + tolen + filelen
  423. + need_ext);
  424. new_module->fromname = mempcpy (new_module->toname, toname, tolen);
  425. new_module->filename = mempcpy (new_module->fromname, fromname, fromlen);
  426. new_module->cost = cost;
  427. new_module->next = NULL;
  428. tmp = mempcpy (new_module->filename, filename, filelen);
  429. if (need_ext)
  430. {
  431. memcpy (tmp - 1, gconv_module_ext, need_ext + 1);
  432. filelen += need_ext;
  433. }
  434. new_module->directory = directory;
  435. /* Now insert the new module data structure in our search tree. */
  436. inserted = (void **) tsearch (new_module, &modules, module_compare);
  437. if (inserted == NULL)
  438. error (EXIT_FAILURE, errno, "while inserting in search tree");
  439. if (*inserted != new_module)
  440. free (new_module);
  441. else
  442. {
  443. new_module->fromname_strent = strtabadd (strtab, new_module->fromname,
  444. fromlen);
  445. new_module->toname_strent = strtabadd (strtab, new_module->toname,
  446. tolen);
  447. new_module->filename_strent = strtabadd (strtab, new_module->filename,
  448. filelen);
  449. new_module->directory_strent = strtabadd (strtab, directory, dirlen);
  450. }
  451. }
  452. /* Add new module. */
  453. static void
  454. add_module (char *rp, const char *directory,
  455. size_t dirlen __attribute__ ((__unused__)),
  456. int modcount __attribute__ ((__unused__)))
  457. {
  458. /* We expect now
  459. 1. `from' name
  460. 2. `to' name
  461. 3. filename of the module
  462. 4. an optional cost value
  463. */
  464. char *from;
  465. char *to;
  466. char *module;
  467. char *wp;
  468. int need_ext;
  469. int cost;
  470. while (isspace (*rp))
  471. ++rp;
  472. from = rp;
  473. while (*rp != '\0' && !isspace (*rp))
  474. {
  475. *rp = toupper (*rp);
  476. ++rp;
  477. }
  478. if (*rp == '\0')
  479. return;
  480. *rp++ = '\0';
  481. to = wp = rp;
  482. while (isspace (*rp))
  483. ++rp;
  484. while (*rp != '\0' && !isspace (*rp))
  485. *wp++ = toupper (*rp++);
  486. if (*rp == '\0')
  487. return;
  488. *wp++ = '\0';
  489. do
  490. ++rp;
  491. while (isspace (*rp));
  492. module = wp;
  493. while (*rp != '\0' && !isspace (*rp))
  494. *wp++ = *rp++;
  495. if (*rp == '\0')
  496. {
  497. /* There is no cost, use one by default. */
  498. *wp++ = '\0';
  499. cost = 1;
  500. }
  501. else
  502. {
  503. /* There might be a cost value. */
  504. char *endp;
  505. *wp++ = '\0';
  506. cost = strtol (rp, &endp, 10);
  507. if (rp == endp || cost < 1)
  508. /* No useful information. */
  509. cost = 1;
  510. }
  511. if (module[0] == '\0')
  512. /* No module name given. */
  513. return;
  514. /* See whether we must add the ending. */
  515. need_ext = 0;
  516. if ((size_t) (wp - module) < sizeof (gconv_module_ext)
  517. || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
  518. sizeof (gconv_module_ext)) != 0)
  519. /* We must add the module extension. */
  520. need_ext = sizeof (gconv_module_ext) - 1;
  521. assert (strlen (from) + 1 == (size_t) (to - from));
  522. assert (strlen (to) + 1 == (size_t) (module - to));
  523. assert (strlen (module) + 1 == (size_t) (wp - module));
  524. new_module (from, to - from, to, module - to, directory, module, wp - module,
  525. cost, need_ext);
  526. }
  527. /* Read config files and add the data for this directory to cache. */
  528. static int
  529. handle_dir (const char *dir)
  530. {
  531. char *newp = NULL;
  532. size_t dirlen = strlen (dir);
  533. bool found = false;
  534. /* End directory path with a '/' if it doesn't already. */
  535. if (dir[dirlen - 1] != '/')
  536. {
  537. newp = xmalloc (dirlen + 2);
  538. memcpy (newp, dir, dirlen);
  539. newp[dirlen++] = '/';
  540. newp[dirlen] = '\0';
  541. dir = newp;
  542. }
  543. found = gconv_parseconfdir (dir[0] == '/' ? prefix : NULL, dir, dirlen);
  544. if (!found)
  545. {
  546. error (0, errno, "failed to open gconv configuration files in `%s'",
  547. dir);
  548. error (0, 0,
  549. "ensure that the directory contains either a valid "
  550. "gconv-modules file or a gconv-modules.d directory with "
  551. "configuration files with names ending in .conf.");
  552. }
  553. free (newp);
  554. return found ? 0 : 1;
  555. }
  556. static void
  557. append_module (const void *nodep, VISIT value, int level)
  558. {
  559. struct module *mo;
  560. if (value != leaf && value != postorder)
  561. return;
  562. mo = *(struct module **) nodep;
  563. if (nmodule_list > 0
  564. && strcmp (module_list[nmodule_list - 1]->fromname, mo->fromname) == 0)
  565. {
  566. /* Same name. */
  567. mo->next = module_list[nmodule_list - 1];
  568. module_list[nmodule_list - 1] = mo;
  569. return;
  570. }
  571. if (nmodule_list_max == nmodule_list)
  572. {
  573. nmodule_list_max += 50;
  574. module_list = (struct module **) xrealloc (module_list,
  575. (nmodule_list_max
  576. * sizeof (struct module *)));
  577. }
  578. module_list[nmodule_list++] = mo;
  579. }
  580. static void
  581. get_modules (void)
  582. {
  583. twalk (modules, append_module);
  584. }
  585. static void
  586. add_builtins (void)
  587. {
  588. size_t cnt;
  589. /* Add all aliases. */
  590. for (cnt = 0; cnt < nbuiltin_alias; ++cnt)
  591. new_alias (builtin_alias[cnt].from,
  592. strlen (builtin_alias[cnt].from) + 1,
  593. builtin_alias[cnt].to,
  594. strlen (builtin_alias[cnt].to) + 1);
  595. /* add the builtin transformations. */
  596. for (cnt = 0; cnt < nbuiltin_trans; ++cnt)
  597. new_module (builtin_trans[cnt].from,
  598. strlen (builtin_trans[cnt].from) + 1,
  599. builtin_trans[cnt].to,
  600. strlen (builtin_trans[cnt].to) + 1,
  601. "", builtin_trans[cnt].module,
  602. strlen (builtin_trans[cnt].module) + 1,
  603. builtin_trans[cnt].cost, 0);
  604. }
  605. static int
  606. name_compare (const void *p1, const void *p2)
  607. {
  608. const struct name *n1 = (const struct name *) p1;
  609. const struct name *n2 = (const struct name *) p2;
  610. return strcmp (n1->name, n2->name);
  611. }
  612. static struct name *
  613. new_name (const char *str, struct Strent *strent)
  614. {
  615. struct name *newp = (struct name *) xmalloc (sizeof (struct name));
  616. newp->name = str;
  617. newp->strent = strent;
  618. newp->module_idx = -1;
  619. newp->hashval = __hash_string (str);
  620. ++nnames;
  621. return newp;
  622. }
  623. static void
  624. generate_name_list (void)
  625. {
  626. size_t i;
  627. /* A name we always need. */
  628. tsearch (new_name ("INTERNAL", strtabadd (strtab, "INTERNAL",
  629. sizeof ("INTERNAL"))),
  630. &names, name_compare);
  631. for (i = 0; i < nmodule_list; ++i)
  632. {
  633. struct module *runp;
  634. if (strcmp (module_list[i]->fromname, "INTERNAL") != 0)
  635. tsearch (new_name (module_list[i]->fromname,
  636. module_list[i]->fromname_strent),
  637. &names, name_compare);
  638. for (runp = module_list[i]; runp != NULL; runp = runp->next)
  639. if (strcmp (runp->toname, "INTERNAL") != 0)
  640. tsearch (new_name (runp->toname, runp->toname_strent),
  641. &names, name_compare);
  642. }
  643. }
  644. static int
  645. name_to_module_idx (const char *name, int add)
  646. {
  647. struct name **res;
  648. struct name fake_name = { .name = name };
  649. int idx;
  650. res = (struct name **) tfind (&fake_name, &names, name_compare);
  651. if (res == NULL)
  652. abort ();
  653. idx = (*res)->module_idx;
  654. if (idx == -1 && add)
  655. /* No module index assigned yet. */
  656. idx = (*res)->module_idx = nname_info++;
  657. return idx;
  658. }
  659. static void
  660. generate_name_info (void)
  661. {
  662. size_t i;
  663. int idx;
  664. name_info = (struct name_info *) xcalloc (nmodule_list + 1,
  665. sizeof (struct name_info));
  666. /* First add a special entry for the INTERNAL name. This must have
  667. index zero. */
  668. idx = name_to_module_idx ("INTERNAL", 1);
  669. name_info[0].canonical_name = "INTERNAL";
  670. name_info[0].canonical_strent = strtabadd (strtab, "INTERNAL",
  671. sizeof ("INTERNAL"));
  672. assert (nname_info == 1);
  673. for (i = 0; i < nmodule_list; ++i)
  674. {
  675. struct module *runp;
  676. for (runp = module_list[i]; runp != NULL; runp = runp->next)
  677. if (strcmp (runp->fromname, "INTERNAL") == 0)
  678. {
  679. idx = name_to_module_idx (runp->toname, 1);
  680. name_info[idx].from_internal = runp;
  681. assert (name_info[idx].canonical_name == NULL
  682. || strcmp (name_info[idx].canonical_name,
  683. runp->toname) == 0);
  684. name_info[idx].canonical_name = runp->toname;
  685. name_info[idx].canonical_strent = runp->toname_strent;
  686. }
  687. else if (strcmp (runp->toname, "INTERNAL") == 0)
  688. {
  689. idx = name_to_module_idx (runp->fromname, 1);
  690. name_info[idx].to_internal = runp;
  691. assert (name_info[idx].canonical_name == NULL
  692. || strcmp (name_info[idx].canonical_name,
  693. runp->fromname) == 0);
  694. name_info[idx].canonical_name = runp->fromname;
  695. name_info[idx].canonical_strent = runp->fromname_strent;
  696. }
  697. else
  698. {
  699. /* This is a transformation not to or from the INTERNAL
  700. encoding. */
  701. int from_idx = name_to_module_idx (runp->fromname, 1);
  702. int to_idx = name_to_module_idx (runp->toname, 1);
  703. struct other_conv_list *newp;
  704. newp = (struct other_conv_list *)
  705. xmalloc (sizeof (struct other_conv_list));
  706. newp->other_conv.module_idx = to_idx;
  707. newp->other_conv.module = runp;
  708. newp->other_conv.next = NULL; /* XXX Allow multiple module sequence */
  709. newp->dest_idx = to_idx;
  710. newp->next = name_info[from_idx].other_conv_list;
  711. name_info[from_idx].other_conv_list = newp;
  712. assert (name_info[from_idx].canonical_name == NULL
  713. || strcmp (name_info[from_idx].canonical_name,
  714. runp->fromname) == 0);
  715. name_info[from_idx].canonical_name = runp->fromname;
  716. name_info[from_idx].canonical_strent = runp->fromname_strent;
  717. ++nextra_modules;
  718. }
  719. }
  720. /* Now add the module index information for all the aliases. */
  721. for (i = 0; i < nalias_list; ++i)
  722. {
  723. struct name fake_name = { .name = alias_list[i]->toname };
  724. struct name **tonamep;
  725. tonamep = (struct name **) tfind (&fake_name, &names, name_compare);
  726. if (tonamep != NULL)
  727. {
  728. struct name *newp = new_name (alias_list[i]->fromname,
  729. alias_list[i]->froment);
  730. newp->module_idx = (*tonamep)->module_idx;
  731. tsearch (newp, &names, name_compare);
  732. }
  733. }
  734. }
  735. static int
  736. is_prime (unsigned long int candidate)
  737. {
  738. /* No even number and none less than 10 will be passed here. */
  739. unsigned long int divn = 3;
  740. unsigned long int sq = divn * divn;
  741. while (sq < candidate && candidate % divn != 0)
  742. {
  743. ++divn;
  744. sq += 4 * divn;
  745. ++divn;
  746. }
  747. return candidate % divn != 0;
  748. }
  749. static uint32_t
  750. next_prime (uint32_t seed)
  751. {
  752. /* Make it definitely odd. */
  753. seed |= 1;
  754. while (!is_prime (seed))
  755. seed += 2;
  756. return seed;
  757. }
  758. /* Format of the output file.
  759. Offset Length Description
  760. 0000 4 Magic header bytes
  761. 0004 2 Offset of string table (stoff)
  762. 0006 2 Offset of name hashing table (hoff)
  763. 0008 2 Hashing table size (hsize)
  764. 000A 2 Offset of module table (moff)
  765. 000C 2 Offset of other conversion module table (ooff)
  766. stoff ??? String table
  767. hoff 8*hsize Array of tuples
  768. string table offset
  769. module index
  770. moff ??? Array of tuples
  771. canonical name offset
  772. from-internal module dir name offset
  773. from-internal module name off
  774. to-internal module dir name offset
  775. to-internal module name offset
  776. offset into other conversion table
  777. ooff ??? One or more of
  778. number of steps/modules
  779. one or more of tuple
  780. canonical name offset for output
  781. module dir name offset
  782. module name offset
  783. (following last entry with step count 0)
  784. */
  785. static struct hash_entry *hash_table;
  786. static size_t hash_size;
  787. /* Function to insert the names. */
  788. static void name_insert (const void *nodep, VISIT value, int level)
  789. {
  790. struct name *name;
  791. unsigned int idx;
  792. unsigned int hval2;
  793. if (value != leaf && value != postorder)
  794. return;
  795. name = *(struct name **) nodep;
  796. idx = name->hashval % hash_size;
  797. hval2 = 1 + name->hashval % (hash_size - 2);
  798. while (hash_table[idx].string_offset != 0)
  799. if ((idx += hval2) >= hash_size)
  800. idx -= hash_size;
  801. hash_table[idx].string_offset = strtaboffset (name->strent);
  802. assert (name->module_idx != -1);
  803. hash_table[idx].module_idx = name->module_idx;
  804. }
  805. static int
  806. write_output (void)
  807. {
  808. int fd;
  809. char *string_table;
  810. size_t string_table_size;
  811. struct gconvcache_header header;
  812. struct module_entry *module_table;
  813. char *extra_table;
  814. char *cur_extra_table;
  815. size_t n;
  816. int idx;
  817. struct iovec iov[6];
  818. static const gidx_t null_word;
  819. size_t total;
  820. char finalname[prefix_len + sizeof GCONV_MODULES_CACHE];
  821. char tmpfname[(output_file == NULL ? sizeof finalname : output_file_len + 1)
  822. + strlen (".XXXXXX")];
  823. /* Open the output file. */
  824. if (output_file == NULL)
  825. {
  826. assert (GCONV_MODULES_CACHE[0] == '/');
  827. strcpy (stpcpy (mempcpy (tmpfname, prefix, prefix_len),
  828. GCONV_MODULES_CACHE),
  829. ".XXXXXX");
  830. strcpy (mempcpy (finalname, prefix, prefix_len), GCONV_MODULES_CACHE);
  831. }
  832. else
  833. strcpy (mempcpy (tmpfname, output_file, output_file_len), ".XXXXXX");
  834. fd = mkstemp (tmpfname);
  835. if (fd == -1)
  836. return 1;
  837. /* Create the string table. */
  838. string_table = strtabfinalize (strtab, &string_table_size);
  839. /* Create the hashing table. We know how many strings we have.
  840. Creating a perfect hash table is not reasonable here. Therefore
  841. we use open hashing and a table size which is the next prime 50%
  842. larger than the number of strings. */
  843. hash_size = next_prime (nnames + (nnames >> 1));
  844. hash_table = (struct hash_entry *) xcalloc (hash_size,
  845. sizeof (struct hash_entry));
  846. /* Fill the hash table. */
  847. twalk (names, name_insert);
  848. /* Create the section for the module list. */
  849. module_table = (struct module_entry *) xcalloc (sizeof (struct module_entry),
  850. nname_info);
  851. /* Allocate memory for the non-INTERNAL conversions. The allocated
  852. memory can be more than is actually needed. */
  853. extra_table = (char *) xcalloc (sizeof (struct extra_entry)
  854. + sizeof (gidx_t)
  855. + sizeof (struct extra_entry_module),
  856. nextra_modules);
  857. cur_extra_table = extra_table;
  858. /* Fill in the module information. */
  859. for (n = 0; n < nname_info; ++n)
  860. {
  861. module_table[n].canonname_offset =
  862. strtaboffset (name_info[n].canonical_strent);
  863. if (name_info[n].from_internal == NULL)
  864. {
  865. module_table[n].fromdir_offset = 0;
  866. module_table[n].fromname_offset = 0;
  867. }
  868. else
  869. {
  870. module_table[n].fromdir_offset =
  871. strtaboffset (name_info[n].from_internal->directory_strent);
  872. module_table[n].fromname_offset =
  873. strtaboffset (name_info[n].from_internal->filename_strent);
  874. }
  875. if (name_info[n].to_internal == NULL)
  876. {
  877. module_table[n].todir_offset = 0;
  878. module_table[n].toname_offset = 0;
  879. }
  880. else
  881. {
  882. module_table[n].todir_offset =
  883. strtaboffset (name_info[n].to_internal->directory_strent);
  884. module_table[n].toname_offset =
  885. strtaboffset (name_info[n].to_internal->filename_strent);
  886. }
  887. if (name_info[n].other_conv_list != NULL)
  888. {
  889. struct other_conv_list *other = name_info[n].other_conv_list;
  890. /* Store the reference. We add 1 to distinguish the entry
  891. at offset zero from the case where no extra modules are
  892. available. The file reader has to account for the
  893. offset. */
  894. module_table[n].extra_offset = 1 + cur_extra_table - extra_table;
  895. do
  896. {
  897. struct other_conv *runp;
  898. struct extra_entry *extra;
  899. /* Allocate new entry. */
  900. extra = (struct extra_entry *) cur_extra_table;
  901. cur_extra_table += sizeof (struct extra_entry);
  902. extra->module_cnt = 0;
  903. runp = &other->other_conv;
  904. do
  905. {
  906. cur_extra_table += sizeof (struct extra_entry_module);
  907. extra->module[extra->module_cnt].outname_offset =
  908. runp->next == NULL
  909. ? other->dest_idx : runp->next->module_idx;
  910. extra->module[extra->module_cnt].dir_offset =
  911. strtaboffset (runp->module->directory_strent);
  912. extra->module[extra->module_cnt].name_offset =
  913. strtaboffset (runp->module->filename_strent);
  914. ++extra->module_cnt;
  915. runp = runp->next;
  916. }
  917. while (runp != NULL);
  918. other = other->next;
  919. }
  920. while (other != NULL);
  921. /* Final module_cnt is zero. */
  922. *((gidx_t *) cur_extra_table) = 0;
  923. cur_extra_table += sizeof (gidx_t);
  924. }
  925. }
  926. /* Clear padding. */
  927. memset (&header, 0, sizeof (struct gconvcache_header));
  928. header.magic = GCONVCACHE_MAGIC;
  929. iov[0].iov_base = &header;
  930. iov[0].iov_len = sizeof (struct gconvcache_header);
  931. total = iov[0].iov_len;
  932. header.string_offset = total;
  933. iov[1].iov_base = string_table;
  934. iov[1].iov_len = string_table_size;
  935. total += iov[1].iov_len;
  936. idx = 2;
  937. if ((string_table_size & (sizeof (gidx_t) - 1)) != 0)
  938. {
  939. iov[2].iov_base = (void *) &null_word;
  940. iov[2].iov_len = (sizeof (gidx_t)
  941. - (string_table_size & (sizeof (gidx_t) - 1)));
  942. total += iov[2].iov_len;
  943. ++idx;
  944. }
  945. header.hash_offset = total;
  946. header.hash_size = hash_size;
  947. iov[idx].iov_base = hash_table;
  948. iov[idx].iov_len = hash_size * sizeof (struct hash_entry);
  949. total += iov[idx].iov_len;
  950. ++idx;
  951. header.module_offset = total;
  952. iov[idx].iov_base = module_table;
  953. iov[idx].iov_len = nname_info * sizeof (struct module_entry);
  954. total += iov[idx].iov_len;
  955. ++idx;
  956. assert ((size_t) (cur_extra_table - extra_table)
  957. <= ((sizeof (struct extra_entry) + sizeof (gidx_t)
  958. + sizeof (struct extra_entry_module))
  959. * nextra_modules));
  960. header.otherconv_offset = total;
  961. iov[idx].iov_base = extra_table;
  962. iov[idx].iov_len = cur_extra_table - extra_table;
  963. total += iov[idx].iov_len;
  964. ++idx;
  965. if ((size_t) TEMP_FAILURE_RETRY (writev (fd, iov, idx)) != total
  966. /* The file was created with mode 0600. Make it world-readable. */
  967. || fchmod (fd, 0644) != 0
  968. /* Rename the file, possibly replacing an old one. */
  969. || rename (tmpfname, output_file ?: finalname) != 0)
  970. {
  971. int save_errno = errno;
  972. close (fd);
  973. unlink (tmpfname);
  974. error (EXIT_FAILURE, save_errno,
  975. gettext ("cannot generate output file"));
  976. }
  977. close (fd);
  978. return 0;
  979. }