1 /* Generate fastloading iconv module configuration files.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <https://www.gnu.org/licenses/>. */
32 #include <stdio_ext.h>
36 #include <sys/cdefs.h>
37 #include <sys/types.h>
40 #include "iconvconfig.h"
42 /* Get libc version number. */
43 #include "../version.h"
45 #define PACKAGE _libc_intl_domainname
48 /* The hashing function we use. */
49 #include "../intl/hash-string.h"
56 struct Strent
*fromname_strent
;
58 struct Strent
*filename_strent
;
59 const char *directory
;
60 struct Strent
*directory_strent
;
63 struct Strent
*toname_strent
;
70 struct Strent
*froment
;
71 struct module
*module
;
79 struct Strent
*strent
;
86 const char *canonical_name
;
87 struct Strent
*canonical_strent
;
89 struct module
*from_internal
;
90 struct module
*to_internal
;
92 struct other_conv_list
98 struct module
*module
;
99 struct other_conv
*next
;
101 struct other_conv_list
*next
;
106 /* Name and version of program. */
107 static void print_version (FILE *stream
, struct argp_state
*state
);
108 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
110 /* Short description of program. */
111 static const char doc
[] = N_("\
112 Create fastloading iconv module configuration file.");
114 /* Strings for arguments in help texts. */
115 static const char args_doc
[] = N_("[DIR...]");
117 /* Prototype for option handler. */
118 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
120 /* Function to print some extra text in the help message. */
121 static char *more_help (int key
, const char *text
, void *input
);
123 /* Definitions of arguments for argp functions. */
124 #define OPT_PREFIX 300
125 #define OPT_NOSTDLIB 301
126 static const struct argp_option options
[] =
128 { "prefix", OPT_PREFIX
, N_("PATH"), 0,
129 N_("Prefix used for all file accesses") },
130 { "output", 'o', N_("FILE"), 0, N_("\
131 Put output in FILE instead of installed location\
132 (--prefix does not apply to FILE)") },
133 { "nostdlib", OPT_NOSTDLIB
, NULL
, 0,
134 N_("Do not search standard directories, only those on the command line") },
135 { NULL
, 0, NULL
, 0, NULL
}
138 /* Data structure to communicate with argp functions. */
139 static struct argp argp
=
141 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
145 /* The function doing the actual work. */
146 static int handle_dir (const char *dir
);
148 /* Add all known builtin conversions and aliases. */
149 static void add_builtins (void);
151 /* Create list of all aliases without circular aliases. */
152 static void get_aliases (void);
154 /* Create list of all modules. */
155 static void get_modules (void);
157 /* Get list of all the names and thereby indexing them. */
158 static void generate_name_list (void);
160 /* Collect information about all the names. */
161 static void generate_name_info (void);
163 /* Write the output file. */
164 static int write_output (void);
167 /* Prefix to be used for all file accesses. */
168 static const char *prefix
= "";
170 static size_t prefix_len
;
172 /* Directory to place output file in. */
173 static const char *output_file
;
175 static size_t output_file_len
;
177 /* If true, omit the GCONV_PATH directories and require some arguments. */
178 static bool nostdlib
;
180 /* Search tree of the modules we know. */
181 static void *modules
;
183 /* Search tree of the aliases we know. */
184 static void *aliases
;
186 /* Search tree for name to index mapping. */
189 /* Number of names we know about. */
192 /* List of all aliases. */
193 static struct alias
**alias_list
;
194 static size_t nalias_list
;
195 static size_t nalias_list_max
;
197 /* List of all modules. */
198 static struct module
**module_list
;
199 static size_t nmodule_list
;
200 static size_t nmodule_list_max
;
202 /* Names and information about them. */
203 static struct name_info
*name_info
;
204 static size_t nname_info
;
206 /* Number of translations not from or to INTERNAL. */
207 static size_t nextra_modules
;
210 /* Names and aliases for the builtin transformations. */
217 #define BUILTIN_ALIAS(alias, real) \
218 { .from = alias, .to = real },
219 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
220 MinF, MaxF, MinT, MaxT)
221 #include <gconv_builtin.h>
224 #undef BUILTIN_TRANSFORMATION
225 #define nbuiltin_alias (sizeof (builtin_alias) / sizeof (builtin_alias[0]))
235 #define BUILTIN_ALIAS(alias, real)
236 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
237 MinF, MaxF, MinT, MaxT) \
238 { .from = From, .to = To, .module = Name, .cost = Cost },
239 #include <gconv_builtin.h>
242 #undef BUILTIN_TRANSFORMATION
243 #define nbuiltin_trans (sizeof (builtin_trans) / sizeof (builtin_trans[0]))
246 /* Filename extension for the modules. */
248 # define MODULE_EXT ".so"
250 static const char gconv_module_ext
[] = MODULE_EXT
;
253 #include <programs/xmalloc.h>
256 /* C string table handling. */
260 /* Create new C string table object in memory. */
261 extern struct Strtab
*strtabinit (void);
263 /* Free resources allocated for C string table ST. */
264 extern void strtabfree (struct Strtab
*st
);
266 /* Add string STR (length LEN is != 0) to C string table ST. */
267 extern struct Strent
*strtabadd (struct Strtab
*st
, const char *str
,
270 /* Finalize string table ST and store size in *SIZE and return a pointer. */
271 extern void *strtabfinalize (struct Strtab
*st
, size_t *size
);
273 /* Get offset in string table for string associated with SE. */
274 extern size_t strtaboffset (struct Strent
*se
);
276 /* String table we construct. */
277 static struct Strtab
*strtab
;
282 main (int argc
, char *argv
[])
287 /* Enable memory use testing. */
288 /* mcheck_pedantic (NULL); */
291 /* Set locale via LC_ALL. */
292 setlocale (LC_ALL
, "");
294 /* Set the text message domain. */
295 textdomain (_libc_intl_domainname
);
297 /* Parse and process arguments. */
298 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
300 if (nostdlib
&& remaining
== argc
)
301 error (2, 0, _("Directory arguments required when using --nostdlib"));
303 /* Initialize the string table. */
304 strtab
= strtabinit ();
306 /* Handle all directories mentioned. */
307 while (remaining
< argc
)
308 status
|= handle_dir (argv
[remaining
++]);
312 /* In any case also handle the standard directory. */
313 char *path
= strdupa (GCONV_PATH
), *tp
= strsep (&path
, ":");
316 status
|= handle_dir (tp
);
318 tp
= strsep (&path
, ":");
322 /* Add the builtin transformations and aliases without overwriting
326 /* Store aliases in an array. */
329 /* Get list of all modules. */
332 /* Generate list of all the names we know to handle in some way. */
333 generate_name_list ();
335 /* Now we know all the names we will handle, collect information
337 generate_name_info ();
339 /* Write the output file, but only if we haven't seen any error. */
341 status
= write_output ();
343 error (1, 0, _("no output file produced because warnings were issued"));
349 /* Handle program arguments. */
351 parse_opt (int key
, char *arg
, struct argp_state
*state
)
357 prefix_len
= strlen (prefix
);
361 output_file_len
= strlen (output_file
);
367 return ARGP_ERR_UNKNOWN
;
374 more_help (int key
, const char *text
, void *input
)
379 case ARGP_KEY_HELP_EXTRA
:
380 /* We print some extra information. */
381 if (asprintf (&tp
, gettext ("\
382 For bug reporting instructions, please see:\n\
383 %s.\n"), REPORT_BUGS_TO
) < 0)
389 return (char *) text
;
393 /* Print the version information. */
395 print_version (FILE *stream
, struct argp_state
*state
)
397 fprintf (stream
, "iconvconfig %s%s\n", PKGVERSION
, VERSION
);
398 fprintf (stream
, gettext ("\
399 Copyright (C) %s Free Software Foundation, Inc.\n\
400 This is free software; see the source for copying conditions. There is NO\n\
401 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
403 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
408 alias_compare (const void *p1
, const void *p2
)
410 const struct alias
*a1
= (const struct alias
*) p1
;
411 const struct alias
*a2
= (const struct alias
*) p2
;
413 return strcmp (a1
->fromname
, a2
->fromname
);
418 new_alias (const char *fromname
, size_t fromlen
, const char *toname
,
424 newp
= (struct alias
*) xmalloc (sizeof (struct alias
) + fromlen
+ tolen
);
426 newp
->fromname
= mempcpy (newp
->toname
, toname
, tolen
);
427 memcpy (newp
->fromname
, fromname
, fromlen
);
430 inserted
= (void **) tsearch (newp
, &aliases
, alias_compare
);
431 if (inserted
== NULL
)
432 error (EXIT_FAILURE
, errno
, gettext ("while inserting in search tree"));
433 if (*inserted
!= newp
)
434 /* Something went wrong, free this entry. */
438 newp
->froment
= strtabadd (strtab
, newp
->fromname
, fromlen
);
439 newp
->toent
= strtabadd (strtab
, newp
->toname
, tolen
);
448 /* We now expect two more string. The strings are normalized
449 (converted to UPPER case) and strored in the alias database. */
454 while (isspace (*rp
))
457 while (*rp
!= '\0' && !isspace (*rp
))
458 *wp
++ = toupper (*rp
++);
460 /* There is no `to' string on the line. Ignore it. */
464 while (isspace (*rp
))
466 while (*rp
!= '\0' && !isspace (*rp
))
467 *wp
++ = toupper (*rp
++);
469 /* No `to' string, ignore the line. */
473 assert (strlen (from
) + 1 == (size_t) (to
- from
));
474 assert (strlen (to
) + 1 == (size_t) (wp
- to
));
476 new_alias (from
, to
- from
, to
, wp
- to
);
481 append_alias (const void *nodep
, VISIT value
, int level
)
483 if (value
!= leaf
&& value
!= postorder
)
486 if (nalias_list_max
== nalias_list
)
488 nalias_list_max
+= 50;
489 alias_list
= (struct alias
**) xrealloc (alias_list
,
491 * sizeof (struct alias
*)));
494 alias_list
[nalias_list
++] = *(struct alias
**) nodep
;
501 twalk (aliases
, append_alias
);
506 module_compare (const void *p1
, const void *p2
)
508 const struct module
*m1
= (const struct module
*) p1
;
509 const struct module
*m2
= (const struct module
*) p2
;
512 result
= strcmp (m1
->fromname
, m2
->fromname
);
514 result
= strcmp (m1
->toname
, m2
->toname
);
520 /* Create new module record. */
522 new_module (const char *fromname
, size_t fromlen
, const char *toname
,
523 size_t tolen
, const char *directory
,
524 const char *filename
, size_t filelen
, int cost
, size_t need_ext
)
526 struct module
*new_module
;
527 size_t dirlen
= strlen (directory
) + 1;
531 new_module
= (struct module
*) xmalloc (sizeof (struct module
)
532 + fromlen
+ tolen
+ filelen
535 new_module
->fromname
= mempcpy (new_module
->toname
, toname
, tolen
);
537 new_module
->filename
= mempcpy (new_module
->fromname
, fromname
, fromlen
);
539 new_module
->cost
= cost
;
540 new_module
->next
= NULL
;
542 tmp
= mempcpy (new_module
->filename
, filename
, filelen
);
545 memcpy (tmp
- 1, gconv_module_ext
, need_ext
+ 1);
548 new_module
->directory
= directory
;
550 /* Now insert the new module data structure in our search tree. */
551 inserted
= (void **) tsearch (new_module
, &modules
, module_compare
);
552 if (inserted
== NULL
)
553 error (EXIT_FAILURE
, errno
, "while inserting in search tree");
554 if (*inserted
!= new_module
)
558 new_module
->fromname_strent
= strtabadd (strtab
, new_module
->fromname
,
560 new_module
->toname_strent
= strtabadd (strtab
, new_module
->toname
,
562 new_module
->filename_strent
= strtabadd (strtab
, new_module
->filename
,
564 new_module
->directory_strent
= strtabadd (strtab
, directory
, dirlen
);
569 /* Add new module. */
571 add_module (char *rp
, const char *directory
)
576 3. filename of the module
577 4. an optional cost value
586 while (isspace (*rp
))
589 while (*rp
!= '\0' && !isspace (*rp
))
598 while (isspace (*rp
))
600 while (*rp
!= '\0' && !isspace (*rp
))
601 *wp
++ = toupper (*rp
++);
607 while (isspace (*rp
));
609 while (*rp
!= '\0' && !isspace (*rp
))
613 /* There is no cost, use one by default. */
619 /* There might be a cost value. */
623 cost
= strtol (rp
, &endp
, 10);
624 if (rp
== endp
|| cost
< 1)
625 /* No useful information. */
629 if (module
[0] == '\0')
630 /* No module name given. */
633 /* See whether we must add the ending. */
635 if ((size_t) (wp
- module
) < sizeof (gconv_module_ext
)
636 || memcmp (wp
- sizeof (gconv_module_ext
), gconv_module_ext
,
637 sizeof (gconv_module_ext
)) != 0)
638 /* We must add the module extension. */
639 need_ext
= sizeof (gconv_module_ext
) - 1;
641 assert (strlen (from
) + 1 == (size_t) (to
- from
));
642 assert (strlen (to
) + 1 == (size_t) (module
- to
));
643 assert (strlen (module
) + 1 == (size_t) (wp
- module
));
645 new_module (from
, to
- from
, to
, module
- to
, directory
, module
, wp
- module
,
649 /* Read a gconv-modules configuration file. */
651 handle_file (const char *dir
, const char *infile
)
657 fp
= fopen (infile
, "r");
661 /* No threads present. */
662 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
664 while (!feof_unlocked (fp
))
666 char *rp
, *endp
, *word
;
667 ssize_t n
= __getdelim (&line
, &linelen
, '\n', fp
);
670 /* An error occurred. */
674 /* Terminate the line (excluding comments or newline) with a NUL
675 byte to simplify the following code. */
676 endp
= strchr (rp
, '#');
680 if (rp
[n
- 1] == '\n')
683 while (isspace (*rp
))
686 /* If this is an empty line go on with the next one. */
691 while (*rp
!= '\0' && !isspace (*rp
))
694 if (rp
- word
== sizeof ("alias") - 1
695 && memcmp (word
, "alias", sizeof ("alias") - 1) == 0)
697 else if (rp
- word
== sizeof ("module") - 1
698 && memcmp (word
, "module", sizeof ("module") - 1) == 0)
699 add_module (rp
, dir
);
701 /* Otherwise ignore the line. */
711 /* Read config files and add the data for this directory to cache. */
713 handle_dir (const char *dir
)
716 size_t dirlen
= strlen (dir
);
719 if (dir
[dirlen
- 1] != '/')
721 char *newp
= (char *) xmalloc (dirlen
+ 2);
722 dir
= memcpy (newp
, dir
, dirlen
);
723 newp
[dirlen
++] = '/';
727 /* First, look for a gconv-modules file. */
728 char *buf
= malloc (prefix_len
+ dirlen
+ sizeof "gconv-modules.d");
734 cp
= mempcpy (cp
, prefix
, prefix_len
);
735 cp
= mempcpy (cp
, dir
, dirlen
);
736 cp
= stpcpy (cp
, "gconv-modules");
738 found
|= handle_file (dir
, buf
);
740 /* Next, see if there is a gconv-modules.d directory containing configuration
741 files and if it is non-empty. */
746 DIR *confdir
= opendir (buf
);
750 while ((ent
= readdir (confdir
)) != NULL
)
752 if (ent
->d_type
!= DT_REG
)
755 size_t len
= strlen (ent
->d_name
);
756 const char *suffix
= ".conf";
758 if (len
> strlen (suffix
)
759 && strcmp (ent
->d_name
+ len
- strlen (suffix
), suffix
) == 0)
762 if (asprintf (&conf
, "%s/%s", buf
, ent
->d_name
) < 0)
764 found
|= handle_file (dir
, conf
);
776 error (0, errno
, "failed to open gconv configuration files in `%s'",
779 "ensure that the directory contains either a valid "
780 "gconv-modules file or a gconv-modules.d directory with "
781 "configuration files with names ending in .conf.");
784 return found
? 0 : 1;
789 append_module (const void *nodep
, VISIT value
, int level
)
793 if (value
!= leaf
&& value
!= postorder
)
796 mo
= *(struct module
**) nodep
;
799 && strcmp (module_list
[nmodule_list
- 1]->fromname
, mo
->fromname
) == 0)
802 mo
->next
= module_list
[nmodule_list
- 1];
803 module_list
[nmodule_list
- 1] = mo
;
808 if (nmodule_list_max
== nmodule_list
)
810 nmodule_list_max
+= 50;
811 module_list
= (struct module
**) xrealloc (module_list
,
813 * sizeof (struct module
*)));
816 module_list
[nmodule_list
++] = mo
;
823 twalk (modules
, append_module
);
832 /* Add all aliases. */
833 for (cnt
= 0; cnt
< nbuiltin_alias
; ++cnt
)
834 new_alias (builtin_alias
[cnt
].from
,
835 strlen (builtin_alias
[cnt
].from
) + 1,
836 builtin_alias
[cnt
].to
,
837 strlen (builtin_alias
[cnt
].to
) + 1);
839 /* add the builtin transformations. */
840 for (cnt
= 0; cnt
< nbuiltin_trans
; ++cnt
)
841 new_module (builtin_trans
[cnt
].from
,
842 strlen (builtin_trans
[cnt
].from
) + 1,
843 builtin_trans
[cnt
].to
,
844 strlen (builtin_trans
[cnt
].to
) + 1,
845 "", builtin_trans
[cnt
].module
,
846 strlen (builtin_trans
[cnt
].module
) + 1,
847 builtin_trans
[cnt
].cost
, 0);
852 name_compare (const void *p1
, const void *p2
)
854 const struct name
*n1
= (const struct name
*) p1
;
855 const struct name
*n2
= (const struct name
*) p2
;
857 return strcmp (n1
->name
, n2
->name
);
862 new_name (const char *str
, struct Strent
*strent
)
864 struct name
*newp
= (struct name
*) xmalloc (sizeof (struct name
));
867 newp
->strent
= strent
;
868 newp
->module_idx
= -1;
869 newp
->hashval
= __hash_string (str
);
878 generate_name_list (void)
882 /* A name we always need. */
883 tsearch (new_name ("INTERNAL", strtabadd (strtab
, "INTERNAL",
884 sizeof ("INTERNAL"))),
885 &names
, name_compare
);
887 for (i
= 0; i
< nmodule_list
; ++i
)
891 if (strcmp (module_list
[i
]->fromname
, "INTERNAL") != 0)
892 tsearch (new_name (module_list
[i
]->fromname
,
893 module_list
[i
]->fromname_strent
),
894 &names
, name_compare
);
896 for (runp
= module_list
[i
]; runp
!= NULL
; runp
= runp
->next
)
897 if (strcmp (runp
->toname
, "INTERNAL") != 0)
898 tsearch (new_name (runp
->toname
, runp
->toname_strent
),
899 &names
, name_compare
);
905 name_to_module_idx (const char *name
, int add
)
908 struct name fake_name
= { .name
= name
};
911 res
= (struct name
**) tfind (&fake_name
, &names
, name_compare
);
915 idx
= (*res
)->module_idx
;
916 if (idx
== -1 && add
)
917 /* No module index assigned yet. */
918 idx
= (*res
)->module_idx
= nname_info
++;
925 generate_name_info (void)
930 name_info
= (struct name_info
*) xcalloc (nmodule_list
+ 1,
931 sizeof (struct name_info
));
933 /* First add a special entry for the INTERNAL name. This must have
935 idx
= name_to_module_idx ("INTERNAL", 1);
936 name_info
[0].canonical_name
= "INTERNAL";
937 name_info
[0].canonical_strent
= strtabadd (strtab
, "INTERNAL",
938 sizeof ("INTERNAL"));
939 assert (nname_info
== 1);
941 for (i
= 0; i
< nmodule_list
; ++i
)
945 for (runp
= module_list
[i
]; runp
!= NULL
; runp
= runp
->next
)
946 if (strcmp (runp
->fromname
, "INTERNAL") == 0)
948 idx
= name_to_module_idx (runp
->toname
, 1);
949 name_info
[idx
].from_internal
= runp
;
950 assert (name_info
[idx
].canonical_name
== NULL
951 || strcmp (name_info
[idx
].canonical_name
,
953 name_info
[idx
].canonical_name
= runp
->toname
;
954 name_info
[idx
].canonical_strent
= runp
->toname_strent
;
956 else if (strcmp (runp
->toname
, "INTERNAL") == 0)
958 idx
= name_to_module_idx (runp
->fromname
, 1);
959 name_info
[idx
].to_internal
= runp
;
960 assert (name_info
[idx
].canonical_name
== NULL
961 || strcmp (name_info
[idx
].canonical_name
,
962 runp
->fromname
) == 0);
963 name_info
[idx
].canonical_name
= runp
->fromname
;
964 name_info
[idx
].canonical_strent
= runp
->fromname_strent
;
968 /* This is a transformation not to or from the INTERNAL
970 int from_idx
= name_to_module_idx (runp
->fromname
, 1);
971 int to_idx
= name_to_module_idx (runp
->toname
, 1);
972 struct other_conv_list
*newp
;
974 newp
= (struct other_conv_list
*)
975 xmalloc (sizeof (struct other_conv_list
));
976 newp
->other_conv
.module_idx
= to_idx
;
977 newp
->other_conv
.module
= runp
;
978 newp
->other_conv
.next
= NULL
; /* XXX Allow multiple module sequence */
979 newp
->dest_idx
= to_idx
;
980 newp
->next
= name_info
[from_idx
].other_conv_list
;
981 name_info
[from_idx
].other_conv_list
= newp
;
982 assert (name_info
[from_idx
].canonical_name
== NULL
983 || strcmp (name_info
[from_idx
].canonical_name
,
984 runp
->fromname
) == 0);
985 name_info
[from_idx
].canonical_name
= runp
->fromname
;
986 name_info
[from_idx
].canonical_strent
= runp
->fromname_strent
;
992 /* Now add the module index information for all the aliases. */
993 for (i
= 0; i
< nalias_list
; ++i
)
995 struct name fake_name
= { .name
= alias_list
[i
]->toname
};
996 struct name
**tonamep
;
998 tonamep
= (struct name
**) tfind (&fake_name
, &names
, name_compare
);
1001 struct name
*newp
= new_name (alias_list
[i
]->fromname
,
1002 alias_list
[i
]->froment
);
1003 newp
->module_idx
= (*tonamep
)->module_idx
;
1004 tsearch (newp
, &names
, name_compare
);
1011 is_prime (unsigned long int candidate
)
1013 /* No even number and none less than 10 will be passed here. */
1014 unsigned long int divn
= 3;
1015 unsigned long int sq
= divn
* divn
;
1017 while (sq
< candidate
&& candidate
% divn
!= 0)
1024 return candidate
% divn
!= 0;
1029 next_prime (uint32_t seed
)
1031 /* Make it definitely odd. */
1034 while (!is_prime (seed
))
1041 /* Format of the output file.
1043 Offset Length Description
1044 0000 4 Magic header bytes
1045 0004 2 Offset of string table (stoff)
1046 0006 2 Offset of name hashing table (hoff)
1047 0008 2 Hashing table size (hsize)
1048 000A 2 Offset of module table (moff)
1049 000C 2 Offset of other conversion module table (ooff)
1051 stoff ??? String table
1053 hoff 8*hsize Array of tuples
1057 moff ??? Array of tuples
1058 canonical name offset
1059 from-internal module dir name offset
1060 from-internal module name off
1061 to-internal module dir name offset
1062 to-internal module name offset
1063 offset into other conversion table
1065 ooff ??? One or more of
1066 number of steps/modules
1067 one or more of tuple
1068 canonical name offset for output
1069 module dir name offset
1071 (following last entry with step count 0)
1074 static struct hash_entry
*hash_table
;
1075 static size_t hash_size
;
1077 /* Function to insert the names. */
1078 static void name_insert (const void *nodep
, VISIT value
, int level
)
1084 if (value
!= leaf
&& value
!= postorder
)
1087 name
= *(struct name
**) nodep
;
1088 idx
= name
->hashval
% hash_size
;
1089 hval2
= 1 + name
->hashval
% (hash_size
- 2);
1091 while (hash_table
[idx
].string_offset
!= 0)
1092 if ((idx
+= hval2
) >= hash_size
)
1095 hash_table
[idx
].string_offset
= strtaboffset (name
->strent
);
1097 assert (name
->module_idx
!= -1);
1098 hash_table
[idx
].module_idx
= name
->module_idx
;
1106 size_t string_table_size
;
1107 struct gconvcache_header header
;
1108 struct module_entry
*module_table
;
1110 char *cur_extra_table
;
1113 struct iovec iov
[6];
1114 static const gidx_t null_word
;
1116 char finalname
[prefix_len
+ sizeof GCONV_MODULES_CACHE
];
1117 char tmpfname
[(output_file
== NULL
? sizeof finalname
: output_file_len
+ 1)
1118 + strlen (".XXXXXX")];
1120 /* Open the output file. */
1121 if (output_file
== NULL
)
1123 assert (GCONV_MODULES_CACHE
[0] == '/');
1124 strcpy (stpcpy (mempcpy (tmpfname
, prefix
, prefix_len
),
1125 GCONV_MODULES_CACHE
),
1127 strcpy (mempcpy (finalname
, prefix
, prefix_len
), GCONV_MODULES_CACHE
);
1130 strcpy (mempcpy (tmpfname
, output_file
, output_file_len
), ".XXXXXX");
1131 fd
= mkstemp (tmpfname
);
1135 /* Create the string table. */
1136 string_table
= strtabfinalize (strtab
, &string_table_size
);
1138 /* Create the hashing table. We know how many strings we have.
1139 Creating a perfect hash table is not reasonable here. Therefore
1140 we use open hashing and a table size which is the next prime 50%
1141 larger than the number of strings. */
1142 hash_size
= next_prime (nnames
+ (nnames
>> 1));
1143 hash_table
= (struct hash_entry
*) xcalloc (hash_size
,
1144 sizeof (struct hash_entry
));
1145 /* Fill the hash table. */
1146 twalk (names
, name_insert
);
1148 /* Create the section for the module list. */
1149 module_table
= (struct module_entry
*) xcalloc (sizeof (struct module_entry
),
1152 /* Allocate memory for the non-INTERNAL conversions. The allocated
1153 memory can be more than is actually needed. */
1154 extra_table
= (char *) xcalloc (sizeof (struct extra_entry
)
1156 + sizeof (struct extra_entry_module
),
1158 cur_extra_table
= extra_table
;
1160 /* Fill in the module information. */
1161 for (n
= 0; n
< nname_info
; ++n
)
1163 module_table
[n
].canonname_offset
=
1164 strtaboffset (name_info
[n
].canonical_strent
);
1166 if (name_info
[n
].from_internal
== NULL
)
1168 module_table
[n
].fromdir_offset
= 0;
1169 module_table
[n
].fromname_offset
= 0;
1173 module_table
[n
].fromdir_offset
=
1174 strtaboffset (name_info
[n
].from_internal
->directory_strent
);
1175 module_table
[n
].fromname_offset
=
1176 strtaboffset (name_info
[n
].from_internal
->filename_strent
);
1179 if (name_info
[n
].to_internal
== NULL
)
1181 module_table
[n
].todir_offset
= 0;
1182 module_table
[n
].toname_offset
= 0;
1186 module_table
[n
].todir_offset
=
1187 strtaboffset (name_info
[n
].to_internal
->directory_strent
);
1188 module_table
[n
].toname_offset
=
1189 strtaboffset (name_info
[n
].to_internal
->filename_strent
);
1192 if (name_info
[n
].other_conv_list
!= NULL
)
1194 struct other_conv_list
*other
= name_info
[n
].other_conv_list
;
1196 /* Store the reference. We add 1 to distinguish the entry
1197 at offset zero from the case where no extra modules are
1198 available. The file reader has to account for the
1200 module_table
[n
].extra_offset
= 1 + cur_extra_table
- extra_table
;
1204 struct other_conv
*runp
;
1205 struct extra_entry
*extra
;
1207 /* Allocate new entry. */
1208 extra
= (struct extra_entry
*) cur_extra_table
;
1209 cur_extra_table
+= sizeof (struct extra_entry
);
1210 extra
->module_cnt
= 0;
1212 runp
= &other
->other_conv
;
1215 cur_extra_table
+= sizeof (struct extra_entry_module
);
1216 extra
->module
[extra
->module_cnt
].outname_offset
=
1218 ? other
->dest_idx
: runp
->next
->module_idx
;
1219 extra
->module
[extra
->module_cnt
].dir_offset
=
1220 strtaboffset (runp
->module
->directory_strent
);
1221 extra
->module
[extra
->module_cnt
].name_offset
=
1222 strtaboffset (runp
->module
->filename_strent
);
1223 ++extra
->module_cnt
;
1227 while (runp
!= NULL
);
1229 other
= other
->next
;
1231 while (other
!= NULL
);
1233 /* Final module_cnt is zero. */
1234 *((gidx_t
*) cur_extra_table
) = 0;
1235 cur_extra_table
+= sizeof (gidx_t
);
1239 /* Clear padding. */
1240 memset (&header
, 0, sizeof (struct gconvcache_header
));
1242 header
.magic
= GCONVCACHE_MAGIC
;
1244 iov
[0].iov_base
= &header
;
1245 iov
[0].iov_len
= sizeof (struct gconvcache_header
);
1246 total
= iov
[0].iov_len
;
1248 header
.string_offset
= total
;
1249 iov
[1].iov_base
= string_table
;
1250 iov
[1].iov_len
= string_table_size
;
1251 total
+= iov
[1].iov_len
;
1254 if ((string_table_size
& (sizeof (gidx_t
) - 1)) != 0)
1256 iov
[2].iov_base
= (void *) &null_word
;
1257 iov
[2].iov_len
= (sizeof (gidx_t
)
1258 - (string_table_size
& (sizeof (gidx_t
) - 1)));
1259 total
+= iov
[2].iov_len
;
1263 header
.hash_offset
= total
;
1264 header
.hash_size
= hash_size
;
1265 iov
[idx
].iov_base
= hash_table
;
1266 iov
[idx
].iov_len
= hash_size
* sizeof (struct hash_entry
);
1267 total
+= iov
[idx
].iov_len
;
1270 header
.module_offset
= total
;
1271 iov
[idx
].iov_base
= module_table
;
1272 iov
[idx
].iov_len
= nname_info
* sizeof (struct module_entry
);
1273 total
+= iov
[idx
].iov_len
;
1276 assert ((size_t) (cur_extra_table
- extra_table
)
1277 <= ((sizeof (struct extra_entry
) + sizeof (gidx_t
)
1278 + sizeof (struct extra_entry_module
))
1280 header
.otherconv_offset
= total
;
1281 iov
[idx
].iov_base
= extra_table
;
1282 iov
[idx
].iov_len
= cur_extra_table
- extra_table
;
1283 total
+= iov
[idx
].iov_len
;
1286 if ((size_t) TEMP_FAILURE_RETRY (writev (fd
, iov
, idx
)) != total
1287 /* The file was created with mode 0600. Make it world-readable. */
1288 || fchmod (fd
, 0644) != 0
1289 /* Rename the file, possibly replacing an old one. */
1290 || rename (tmpfname
, output_file
?: finalname
) != 0)
1292 int save_errno
= errno
;
1295 error (EXIT_FAILURE
, save_errno
,
1296 gettext ("cannot generate output file"));