1 /* Implementation of the internal dcigettext function.
2    Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
3 
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU Library General Public License as published
6    by the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public
15    License along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17    USA.  */
18 
19 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
20    This must come before <autoconf.h> because <autoconf.h> may include
21    <features.h>, and once <features.h> has been included, it's too late.  */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE	1
24 #endif
25 
26 #ifdef HAVE_CONFIG_H
27 # include <autoconf.h>
28 #endif
29 
30 /* see AC_FUNC_ALLOCA macro */
31 #ifdef __GNUC__
32 # define alloca __builtin_alloca
33 #else
34 # ifdef _MSC_VER
35 #  include <malloc.h>
36 #  define alloca _alloca
37 # else
38 #  if HAVE_ALLOCA_H
39 #   include <alloca.h>
40 #  else
41 #   ifdef _AIX
42  #pragma alloca
43 #   else
44 #    ifndef alloca /* predefined by HP cc +Olibcalls */
45 char *alloca ();
46 #    endif
47 #   endif
48 #  endif
49 # endif
50 #endif
51 
52 #include <sys/types.h>
53 
54 #include <errno.h>
55 #ifndef errno
56 extern int errno;
57 #endif
58 #ifndef __set_errno
59 # define __set_errno(val) errno = (val)
60 #endif
61 
62 #include <stddef.h>
63 #include <stdlib.h>
64 
65 #include <string.h>
66 #if !HAVE_STRCHR && !defined _LIBC
67 # ifndef strchr
68 #  define strchr index
69 # endif
70 #endif
71 
72 #if defined HAVE_UNISTD_H || defined _LIBC
73 # include <unistd.h>
74 #endif
75 
76 #include <locale.h>
77 
78 #if defined HAVE_SYS_PARAM_H || defined _LIBC
79 # include <sys/param.h>
80 #endif
81 
82 #include "gettextP.h"
83 #ifdef _LIBC
84 # include <libintl.h>
85 #else
86 # include "libgnuintl.h"
87 #endif
88 #include "hash-string.h"
89 
90 /* Thread safetyness.  */
91 #ifdef _LIBC
92 # include <bits/libc-lock.h>
93 #else
94 /* Provide dummy implementation if this is outside glibc.  */
95 # define __libc_lock_define_initialized(CLASS, NAME)
96 # define __libc_lock_lock(NAME)
97 # define __libc_lock_unlock(NAME)
98 # define __libc_rwlock_define_initialized(CLASS, NAME)
99 # define __libc_rwlock_rdlock(NAME)
100 # define __libc_rwlock_unlock(NAME)
101 #endif
102 
103 /* Alignment of types.  */
104 #if defined __GNUC__ && __GNUC__ >= 2
105 # define alignof(TYPE) __alignof__ (TYPE)
106 #else
107 # define alignof(TYPE) \
108     ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2)
109 #endif
110 
111 /* The internal variables in the standalone libintl.a must have different
112    names than the internal variables in GNU libc, otherwise programs
113    using libintl.a cannot be linked statically.  */
114 #if !defined _LIBC
115 # define _nl_default_default_domain _nl_default_default_domain__
116 # define _nl_current_default_domain _nl_current_default_domain__
117 # define _nl_default_dirname _nl_default_dirname__
118 # define _nl_domain_bindings _nl_domain_bindings__
119 #endif
120 
121 /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>.  */
122 #ifndef offsetof
123 # define offsetof(type,ident) ((size_t)&(((type*)0)->ident))
124 #endif
125 
126 /* @@ end of prolog @@ */
127 
128 #ifdef _LIBC
129 /* Rename the non ANSI C functions.  This is required by the standard
130    because some ANSI C functions will require linking with this object
131    file and the name space must not be polluted.  */
132 # define getcwd __getcwd
133 # ifndef stpcpy
134 #  define stpcpy __stpcpy
135 # endif
136 # define tfind __tfind
137 #else
138 # if !defined HAVE_GETCWD
139 char *getwd ();
140 #  define getcwd(buf, max) getwd (buf)
141 # else
142 char *getcwd ();
143 # endif
144 # ifndef HAVE_STPCPY
145 static char *stpcpy PARAMS ((char *dest, const char *src));
146 # endif
147 # ifndef HAVE_MEMPCPY
148 static void *mempcpy PARAMS ((void *dest, const void *src, size_t n));
149 # endif
150 #endif
151 
152 /* Amount to increase buffer size by in each try.  */
153 #define PATH_INCR 32
154 
155 /* The following is from pathmax.h.  */
156 /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define
157    PATH_MAX but might cause redefinition warnings when sys/param.h is
158    later included (as on MORE/BSD 4.3).  */
159 #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__)
160 # include <limits.h>
161 #endif
162 
163 #ifndef _POSIX_PATH_MAX
164 # define _POSIX_PATH_MAX 255
165 #endif
166 
167 #if !defined PATH_MAX && defined _PC_PATH_MAX
168 # define PATH_MAX (pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : pathconf ("/", _PC_PATH_MAX))
169 #endif
170 
171 /* Don't include sys/param.h if it already has been.  */
172 #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN
173 # include <sys/param.h>
174 #endif
175 
176 #if !defined PATH_MAX && defined MAXPATHLEN
177 # define PATH_MAX MAXPATHLEN
178 #endif
179 
180 #ifndef PATH_MAX
181 # define PATH_MAX _POSIX_PATH_MAX
182 #endif
183 
184 /* Pathname support.
185    ISSLASH(C)           tests whether C is a directory separator character.
186    IS_ABSOLUTE_PATH(P)  tests whether P is an absolute path.  If it is not,
187                         it may be concatenated to a directory pathname.
188    IS_PATH_WITH_DIR(P)  tests whether P contains a directory specification.
189  */
190 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
191   /* Win32, OS/2, DOS */
192 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
193 # define HAS_DEVICE(P) \
194     ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
195      && (P)[1] == ':')
196 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
197 # define IS_PATH_WITH_DIR(P) \
198     (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
199 #else
200   /* Unix */
201 # define ISSLASH(C) ((C) == '/')
202 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
203 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
204 #endif
205 
206 /* XPG3 defines the result of `setlocale (category, NULL)' as:
207    ``Directs `setlocale()' to query `category' and return the current
208      setting of `local'.''
209    However it does not specify the exact format.  Neither do SUSV2 and
210    ISO C 99.  So we can use this feature only on selected systems (e.g.
211    those using GNU C Library).  */
212 #if defined _LIBC || (defined __GNU_LIBRARY__ && __GNU_LIBRARY__ >= 2)
213 # define HAVE_LOCALE_NULL
214 #endif
215 
216 /* This is the type used for the search tree where known translations
217    are stored.  */
218 struct known_translation_t
219 {
220   /* Domain in which to search.  */
221   char *domainname;
222 
223   /* The category.  */
224   int category;
225 
226   /* State of the catalog counter at the point the string was found.  */
227   int counter;
228 
229   /* Catalog where the string was found.  */
230   struct loaded_l10nfile *domain;
231 
232   /* And finally the translation.  */
233   const char *translation;
234   size_t translation_length;
235 
236   /* Pointer to the string in question.  */
237   char msgid[ZERO];
238 };
239 
240 /* Root of the search tree with known translations.  We can use this
241    only if the system provides the `tsearch' function family.  */
242 #if defined HAVE_TSEARCH || defined _LIBC
243 # include <search.h>
244 
245 static void *root;
246 
247 # ifdef _LIBC
248 #  define tsearch __tsearch
249 # endif
250 
251 /* Function to compare two entries in the table of known translations.  */
252 static int transcmp PARAMS ((const void *p1, const void *p2));
253 static int
transcmp(p1,p2)254 transcmp (p1, p2)
255      const void *p1;
256      const void *p2;
257 {
258   const struct known_translation_t *s1;
259   const struct known_translation_t *s2;
260   int result;
261 
262   s1 = (const struct known_translation_t *) p1;
263   s2 = (const struct known_translation_t *) p2;
264 
265   result = strcmp (s1->msgid, s2->msgid);
266   if (result == 0)
267     {
268       result = strcmp (s1->domainname, s2->domainname);
269       if (result == 0)
270 	/* We compare the category last (though this is the cheapest
271 	   operation) since it is hopefully always the same (namely
272 	   LC_MESSAGES).  */
273 	result = s1->category - s2->category;
274     }
275 
276   return result;
277 }
278 #endif
279 
280 /* Name of the default domain used for gettext(3) prior any call to
281    textdomain(3).  The default value for this is "messages".  */
282 const char _nl_default_default_domain[] = "messages";
283 
284 /* Value used as the default domain for gettext(3).  */
285 const char *_nl_current_default_domain = _nl_default_default_domain;
286 
287 /* Contains the default location of the message catalogs.  */
288 const char _nl_default_dirname[] = LOCALEDIR;
289 
290 /* List with bindings of specific domains created by bindtextdomain()
291    calls.  */
292 struct binding *_nl_domain_bindings;
293 
294 /* Prototypes for local functions.  */
295 static char *plural_lookup PARAMS ((struct loaded_l10nfile *domain,
296 				    unsigned long int n,
297 				    const char *translation,
298 				    size_t translation_len))
299      internal_function;
300 static unsigned long int plural_eval PARAMS ((struct expression *pexp,
301 					      unsigned long int n))
302      internal_function;
303 static const char *category_to_name PARAMS ((int category)) internal_function;
304 static const char *guess_category_value PARAMS ((int category,
305 						 const char *categoryname))
306      internal_function;
307 
308 
309 /* For those loosing systems which don't have `alloca' we have to add
310    some additional code emulating it.  */
311 #ifdef HAVE_ALLOCA
312 /* Nothing has to be done.  */
313 # define ADD_BLOCK(list, address) /* nothing */
314 # define FREE_BLOCKS(list) /* nothing */
315 #else
316 struct block_list
317 {
318   void *address;
319   struct block_list *next;
320 };
321 # define ADD_BLOCK(list, addr)						      \
322   do {									      \
323     struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \
324     /* If we cannot get a free block we cannot add the new element to	      \
325        the list.  */							      \
326     if (newp != NULL) {							      \
327       newp->address = (addr);						      \
328       newp->next = (list);						      \
329       (list) = newp;							      \
330     }									      \
331   } while (0)
332 # define FREE_BLOCKS(list)						      \
333   do {									      \
334     while (list != NULL) {						      \
335       struct block_list *old = list;					      \
336       list = list->next;						      \
337       free (old);							      \
338     }									      \
339   } while (0)
340 # undef alloca
341 # define alloca(size) (malloc (size))
342 #endif	/* have alloca */
343 
344 
345 #ifdef _LIBC
346 /* List of blocks allocated for translations.  */
347 typedef struct transmem_list
348 {
349   struct transmem_list *next;
350   char data[ZERO];
351 } transmem_block_t;
352 static struct transmem_list *transmem_list;
353 #else
354 typedef unsigned char transmem_block_t;
355 #endif
356 
357 
358 /* Names for the libintl functions are a problem.  They must not clash
359    with existing names and they should follow ANSI C.  But this source
360    code is also used in GNU C Library where the names have a __
361    prefix.  So we have to make a difference here.  */
362 #ifdef _LIBC
363 # define DCIGETTEXT __dcigettext
364 #else
365 # define DCIGETTEXT dcigettext__
366 #endif
367 
368 /* Lock variable to protect the global data in the gettext implementation.  */
369 #ifdef _LIBC
370 __libc_rwlock_define_initialized (, _nl_state_lock)
371 #endif
372 
373 /* Checking whether the binaries runs SUID must be done and glibc provides
374    easier methods therefore we make a difference here.  */
375 #ifdef _LIBC
376 # define ENABLE_SECURE __libc_enable_secure
377 # define DETERMINE_SECURE
378 #else
379 # ifndef HAVE_GETUID
380 #  define getuid() 0
381 # endif
382 # ifndef HAVE_GETGID
383 #  define getgid() 0
384 # endif
385 # ifndef HAVE_GETEUID
386 #  define geteuid() getuid()
387 # endif
388 # ifndef HAVE_GETEGID
389 #  define getegid() getgid()
390 # endif
391 static int enable_secure;
392 # define ENABLE_SECURE (enable_secure == 1)
393 # define DETERMINE_SECURE \
394   if (enable_secure == 0)						      \
395     {									      \
396       if (getuid () != geteuid () || getgid () != getegid ())		      \
397 	enable_secure = 1;						      \
398       else								      \
399 	enable_secure = -1;						      \
400     }
401 #endif
402 
403 /* Look up MSGID in the DOMAINNAME message catalog for the current
404    CATEGORY locale and, if PLURAL is nonzero, search over string
405    depending on the plural form determined by N.  */
406 char *
407 DCIGETTEXT (domainname, msgid1, msgid2, plural, n, category)
408      const char *domainname;
409      const char *msgid1;
410      const char *msgid2;
411      int plural;
412      unsigned long int n;
413      int category;
414 {
415 #ifndef HAVE_ALLOCA
416   struct block_list *block_list = NULL;
417 #endif
418   struct loaded_l10nfile *domain;
419   struct binding *binding;
420   const char *categoryname;
421   const char *categoryvalue;
422   char *dirname, *xdomainname;
423   char *single_locale;
424   char *retval;
425   size_t retlen;
426   int saved_errno;
427 #if defined HAVE_TSEARCH || defined _LIBC
428   struct known_translation_t *search;
429   struct known_translation_t **foundp = NULL;
430   size_t msgid_len;
431 #endif
432   size_t domainname_len;
433 
434   /* If no real MSGID is given return NULL.  */
435   if (msgid1 == NULL)
436     return NULL;
437 
438   __libc_rwlock_rdlock (_nl_state_lock);
439 
440   /* If DOMAINNAME is NULL, we are interested in the default domain.  If
441      CATEGORY is not LC_MESSAGES this might not make much sense but the
442      definition left this undefined.  */
443   if (domainname == NULL)
444     domainname = _nl_current_default_domain;
445 
446 #if defined HAVE_TSEARCH || defined _LIBC
447   msgid_len = strlen (msgid1) + 1;
448 
449   /* Try to find the translation among those which we found at
450      some time.  */
451   search = (struct known_translation_t *)
452 	   alloca (offsetof (struct known_translation_t, msgid) + msgid_len);
453   memcpy (search->msgid, msgid1, msgid_len);
454   search->domainname = (char *) domainname;
455   search->category = category;
456 
457   foundp = (struct known_translation_t **) tfind (search, &root, transcmp);
458   if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr)
459     {
460       /* Now deal with plural.  */
461       if (plural)
462 	retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation,
463 				(*foundp)->translation_length);
464       else
465 	retval = (char *) (*foundp)->translation;
466 
467       __libc_rwlock_unlock (_nl_state_lock);
468       return retval;
469     }
470 #endif
471 
472   /* Preserve the `errno' value.  */
473   saved_errno = errno;
474 
475   /* See whether this is a SUID binary or not.  */
476   DETERMINE_SECURE;
477 
478   /* First find matching binding.  */
479   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
480     {
481       int compare = strcmp (domainname, binding->domainname);
482       if (compare == 0)
483 	/* We found it!  */
484 	break;
485       if (compare < 0)
486 	{
487 	  /* It is not in the list.  */
488 	  binding = NULL;
489 	  break;
490 	}
491     }
492 
493   if (binding == NULL)
494     dirname = (char *) _nl_default_dirname;
495   else if (IS_ABSOLUTE_PATH (binding->dirname))
496     dirname = binding->dirname;
497   else
498     {
499       /* We have a relative path.  Make it absolute now.  */
500       size_t dirname_len = strlen (binding->dirname) + 1;
501       size_t path_max;
502       char *ret;
503 
504       path_max = (unsigned int) PATH_MAX;
505       path_max += 2;		/* The getcwd docs say to do this.  */
506 
507       for (;;)
508 	{
509 	  dirname = (char *) alloca (path_max + dirname_len);
510 	  ADD_BLOCK (block_list, dirname);
511 
512 	  __set_errno (0);
513 	  ret = getcwd (dirname, path_max);
514 	  if (ret != NULL || errno != ERANGE)
515 	    break;
516 
517 	  path_max += path_max / 2;
518 	  path_max += PATH_INCR;
519 	}
520 
521       if (ret == NULL)
522 	{
523 	  /* We cannot get the current working directory.  Don't signal an
524 	     error but simply return the default string.  */
525 	  FREE_BLOCKS (block_list);
526 	  __libc_rwlock_unlock (_nl_state_lock);
527 	  __set_errno (saved_errno);
528 	  return (plural == 0
529 		  ? (char *) msgid1
530 		  /* Use the Germanic plural rule.  */
531 		  : n == 1 ? (char *) msgid1 : (char *) msgid2);
532 	}
533 
534       stpcpy (stpcpy (strchr (dirname, '\0'), "/"), binding->dirname);
535     }
536 
537   /* Now determine the symbolic name of CATEGORY and its value.  */
538   categoryname = category_to_name (category);
539   categoryvalue = guess_category_value (category, categoryname);
540 
541   domainname_len = strlen (domainname);
542   xdomainname = (char *) alloca (strlen (categoryname)
543 				 + domainname_len + 5);
544   ADD_BLOCK (block_list, xdomainname);
545 
546   stpcpy (mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"),
547 		  domainname, domainname_len),
548 	  ".mo");
549 
550   /* Creating working area.  */
551   single_locale = (char *) alloca (strlen (categoryvalue) + 1);
552   ADD_BLOCK (block_list, single_locale);
553 
554 
555   /* Search for the given string.  This is a loop because we perhaps
556      got an ordered list of languages to consider for the translation.  */
557   while (1)
558     {
559       /* Make CATEGORYVALUE point to the next element of the list.  */
560       while (categoryvalue[0] != '\0' && categoryvalue[0] == ':')
561 	++categoryvalue;
562       if (categoryvalue[0] == '\0')
563 	{
564 	  /* The whole contents of CATEGORYVALUE has been searched but
565 	     no valid entry has been found.  We solve this situation
566 	     by implicitly appending a "C" entry, i.e. no translation
567 	     will take place.  */
568 	  single_locale[0] = 'C';
569 	  single_locale[1] = '\0';
570 	}
571       else
572 	{
573 	  char *cp = single_locale;
574 	  while (categoryvalue[0] != '\0' && categoryvalue[0] != ':')
575 	    *cp++ = *categoryvalue++;
576 	  *cp = '\0';
577 
578 	  /* When this is a SUID binary we must not allow accessing files
579 	     outside the dedicated directories.  */
580 	  if (ENABLE_SECURE && IS_PATH_WITH_DIR (single_locale))
581 	    /* Ingore this entry.  */
582 	    continue;
583 	}
584 
585       /* If the current locale value is C (or POSIX) we don't load a
586 	 domain.  Return the MSGID.  */
587       if (strcmp (single_locale, "C") == 0
588 	  || strcmp (single_locale, "POSIX") == 0)
589 	{
590 	  FREE_BLOCKS (block_list);
591 	  __libc_rwlock_unlock (_nl_state_lock);
592 	  __set_errno (saved_errno);
593 	  return (plural == 0
594 		  ? (char *) msgid1
595 		  /* Use the Germanic plural rule.  */
596 		  : n == 1 ? (char *) msgid1 : (char *) msgid2);
597 	}
598 
599 
600       /* Find structure describing the message catalog matching the
601 	 DOMAINNAME and CATEGORY.  */
602       domain = _nl_find_domain (dirname, single_locale, xdomainname, binding);
603 
604       if (domain != NULL)
605 	{
606 	  retval = _nl_find_msg (domain, binding, msgid1, &retlen);
607 
608 	  if (retval == NULL)
609 	    {
610 	      int cnt;
611 
612 	      for (cnt = 0; domain->successor[cnt] != NULL; ++cnt)
613 		{
614 		  retval = _nl_find_msg (domain->successor[cnt], binding,
615 					 msgid1, &retlen);
616 
617 		  if (retval != NULL)
618 		    {
619 		      domain = domain->successor[cnt];
620 		      break;
621 		    }
622 		}
623 	    }
624 
625 	  if (retval != NULL)
626 	    {
627 	      /* Found the translation of MSGID1 in domain DOMAIN:
628 		 starting at RETVAL, RETLEN bytes.  */
629 	      FREE_BLOCKS (block_list);
630 	      __set_errno (saved_errno);
631 #if defined HAVE_TSEARCH || defined _LIBC
632 	      if (foundp == NULL)
633 		{
634 		  /* Create a new entry and add it to the search tree.  */
635 		  struct known_translation_t *newp;
636 
637 		  newp = (struct known_translation_t *)
638 		    malloc (offsetof (struct known_translation_t, msgid)
639 			    + msgid_len + domainname_len + 1);
640 		  if (newp != NULL)
641 		    {
642 		      newp->domainname =
643 			mempcpy (newp->msgid, msgid1, msgid_len);
644 		      memcpy (newp->domainname, domainname, domainname_len + 1);
645 		      newp->category = category;
646 		      newp->counter = _nl_msg_cat_cntr;
647 		      newp->domain = domain;
648 		      newp->translation = retval;
649 		      newp->translation_length = retlen;
650 
651 		      /* Insert the entry in the search tree.  */
652 		      foundp = (struct known_translation_t **)
653 			tsearch (newp, &root, transcmp);
654 		      if (foundp == NULL
655 			  || __builtin_expect (*foundp != newp, 0))
656 			/* The insert failed.  */
657 			free (newp);
658 		    }
659 		}
660 	      else
661 		{
662 		  /* We can update the existing entry.  */
663 		  (*foundp)->counter = _nl_msg_cat_cntr;
664 		  (*foundp)->domain = domain;
665 		  (*foundp)->translation = retval;
666 		  (*foundp)->translation_length = retlen;
667 		}
668 #endif
669 	      /* Now deal with plural.  */
670 	      if (plural)
671 		retval = plural_lookup (domain, n, retval, retlen);
672 
673 	      __libc_rwlock_unlock (_nl_state_lock);
674 	      return retval;
675 	    }
676 	}
677     }
678   /* NOTREACHED */
679 }
680 
681 
682 char *
683 internal_function
_nl_find_msg(domain_file,domainbinding,msgid,lengthp)684 _nl_find_msg (domain_file, domainbinding, msgid, lengthp)
685      struct loaded_l10nfile *domain_file;
686      struct binding *domainbinding;
687      const char *msgid;
688      size_t *lengthp;
689 {
690   struct loaded_domain *domain;
691   size_t act;
692   char *result;
693   size_t resultlen;
694 
695   if (domain_file->decided == 0)
696     _nl_load_domain (domain_file, domainbinding);
697 
698   if (domain_file->data == NULL)
699     return NULL;
700 
701   domain = (struct loaded_domain *) domain_file->data;
702 
703   /* Locate the MSGID and its translation.  */
704   if (domain->hash_size > 2 && domain->hash_tab != NULL)
705     {
706       /* Use the hashing table.  */
707       nls_uint32 len = strlen (msgid);
708       nls_uint32 hash_val = hash_string (msgid);
709       nls_uint32 idx = hash_val % domain->hash_size;
710       nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
711 
712       while (1)
713 	{
714 	  nls_uint32 nstr = W (domain->must_swap, domain->hash_tab[idx]);
715 
716 	  if (nstr == 0)
717 	    /* Hash table entry is empty.  */
718 	    return NULL;
719 
720 	  /* Compare msgid with the original string at index nstr-1.
721 	     We compare the lengths with >=, not ==, because plural entries
722 	     are represented by strings with an embedded NUL.  */
723 	  if (W (domain->must_swap, domain->orig_tab[nstr - 1].length) >= len
724 	      && (strcmp (msgid,
725 			  domain->data + W (domain->must_swap,
726 					    domain->orig_tab[nstr - 1].offset))
727 		  == 0))
728 	    {
729 	      act = nstr - 1;
730 	      goto found;
731 	    }
732 
733 	  if (idx >= domain->hash_size - incr)
734 	    idx -= domain->hash_size - incr;
735 	  else
736 	    idx += incr;
737 	}
738       /* NOTREACHED */
739     }
740   else
741     {
742       /* Try the default method:  binary search in the sorted array of
743 	 messages.  */
744       size_t top, bottom;
745 
746       bottom = 0;
747       top = domain->nstrings;
748       while (bottom < top)
749 	{
750 	  int cmp_val;
751 
752 	  act = (bottom + top) / 2;
753 	  cmp_val = strcmp (msgid, (domain->data
754 				    + W (domain->must_swap,
755 					 domain->orig_tab[act].offset)));
756 	  if (cmp_val < 0)
757 	    top = act;
758 	  else if (cmp_val > 0)
759 	    bottom = act + 1;
760 	  else
761 	    goto found;
762 	}
763       /* No translation was found.  */
764       return NULL;
765     }
766 
767  found:
768   /* The translation was found at index ACT.  If we have to convert the
769      string to use a different character set, this is the time.  */
770   result = ((char *) domain->data
771 	    + W (domain->must_swap, domain->trans_tab[act].offset));
772   resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1;
773 
774 #if defined _LIBC || defined HAVE_ICONV
775   if (domain->codeset_cntr
776       != (domainbinding != NULL ? domainbinding->codeset_cntr : 0))
777     {
778       /* The domain's codeset has changed through bind_textdomain_codeset()
779 	 since the message catalog was initialized or last accessed.  We
780 	 have to reinitialize the converter.  */
781       _nl_free_domain_conv (domain);
782       _nl_init_domain_conv (domain_file, domain, domainbinding);
783     }
784 
785   if (
786 # ifdef _LIBC
787       domain->conv != (__gconv_t) -1
788 # else
789 #  ifdef HAVE_ICONV
790       domain->conv != (iconv_t) -1
791 #  endif
792 # endif
793       )
794     {
795       /* We are supposed to do a conversion.  First allocate an
796 	 appropriate table with the same structure as the table
797 	 of translations in the file, where we can put the pointers
798 	 to the converted strings in.
799 	 There is a slight complication with plural entries.  They
800 	 are represented by consecutive NUL terminated strings.  We
801 	 handle this case by converting RESULTLEN bytes, including
802 	 NULs.  */
803 
804       if (domain->conv_tab == NULL
805 	  && ((domain->conv_tab = (char **) calloc (domain->nstrings,
806 						    sizeof (char *)))
807 	      == NULL))
808 	/* Mark that we didn't succeed allocating a table.  */
809 	domain->conv_tab = (char **) -1;
810 
811       if (__builtin_expect (domain->conv_tab == (char **) -1, 0))
812 	/* Nothing we can do, no more memory.  */
813 	goto converted;
814 
815       if (domain->conv_tab[act] == NULL)
816 	{
817 	  /* We haven't used this string so far, so it is not
818 	     translated yet.  Do this now.  */
819 	  /* We use a bit more efficient memory handling.
820 	     We allocate always larger blocks which get used over
821 	     time.  This is faster than many small allocations.   */
822 	  __libc_lock_define_initialized (static, lock)
823 # define INITIAL_BLOCK_SIZE	4080
824 	  static unsigned char *freemem;
825 	  static size_t freemem_size;
826 
827 	  const unsigned char *inbuf;
828 	  unsigned char *outbuf;
829 	  int malloc_count;
830 # ifndef _LIBC
831 	  transmem_block_t *transmem_list = NULL;
832 # endif
833 
834 	  __libc_lock_lock (lock);
835 
836 	  inbuf = (const unsigned char *) result;
837 	  outbuf = freemem + sizeof (size_t);
838 
839 	  malloc_count = 0;
840 	  while (1)
841 	    {
842 	      transmem_block_t *newmem;
843 # ifdef _LIBC
844 	      size_t non_reversible;
845 	      int res;
846 
847 	      if (freemem_size < sizeof (size_t))
848 		goto resize_freemem;
849 
850 	      res = __gconv (domain->conv,
851 			     &inbuf, inbuf + resultlen,
852 			     &outbuf,
853 			     outbuf + freemem_size - sizeof (size_t),
854 			     &non_reversible);
855 
856 	      if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
857 		break;
858 
859 	      if (res != __GCONV_FULL_OUTPUT)
860 		{
861 		  __libc_lock_unlock (lock);
862 		  goto converted;
863 		}
864 
865 	      inbuf = result;
866 # else
867 #  ifdef HAVE_ICONV
868 	      const char *inptr = (const char *) inbuf;
869 	      size_t inleft = resultlen;
870 	      char *outptr = (char *) outbuf;
871 	      size_t outleft;
872 
873 	      if (freemem_size < sizeof (size_t))
874 		goto resize_freemem;
875 
876 	      outleft = freemem_size - sizeof (size_t);
877 	      if (iconv (domain->conv,
878 			 (ICONV_CONST char **) &inptr, &inleft,
879 			 &outptr, &outleft)
880 		  != (size_t) (-1))
881 		{
882 		  outbuf = (unsigned char *) outptr;
883 		  break;
884 		}
885 	      if (errno != E2BIG)
886 		{
887 		  __libc_lock_unlock (lock);
888 		  goto converted;
889 		}
890 #  endif
891 # endif
892 
893 	    resize_freemem:
894 	      /* We must allocate a new buffer or resize the old one.  */
895 	      if (malloc_count > 0)
896 		{
897 		  ++malloc_count;
898 		  freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
899 		  newmem = (transmem_block_t *) realloc (transmem_list,
900 							 freemem_size);
901 # ifdef _LIBC
902 		  if (newmem != NULL)
903 		    transmem_list = transmem_list->next;
904 		  else
905 		    {
906 		      struct transmem_list *old = transmem_list;
907 
908 		      transmem_list = transmem_list->next;
909 		      free (old);
910 		    }
911 # endif
912 		}
913 	      else
914 		{
915 		  malloc_count = 1;
916 		  freemem_size = INITIAL_BLOCK_SIZE;
917 		  newmem = (transmem_block_t *) malloc (freemem_size);
918 		}
919 	      if (__builtin_expect (newmem == NULL, 0))
920 		{
921 		  freemem = NULL;
922 		  freemem_size = 0;
923 		  __libc_lock_unlock (lock);
924 		  goto converted;
925 		}
926 
927 # ifdef _LIBC
928 	      /* Add the block to the list of blocks we have to free
929                  at some point.  */
930 	      newmem->next = transmem_list;
931 	      transmem_list = newmem;
932 
933 	      freemem = newmem->data;
934 	      freemem_size -= offsetof (struct transmem_list, data);
935 # else
936 	      transmem_list = newmem;
937 	      freemem = newmem;
938 # endif
939 
940 	      outbuf = freemem + sizeof (size_t);
941 	    }
942 
943 	  /* We have now in our buffer a converted string.  Put this
944 	     into the table of conversions.  */
945 	  *(size_t *) freemem = outbuf - freemem - sizeof (size_t);
946 	  domain->conv_tab[act] = (char *) freemem;
947 	  /* Shrink freemem, but keep it aligned.  */
948 	  freemem_size -= outbuf - freemem;
949 	  freemem = outbuf;
950 	  freemem += freemem_size & (alignof (size_t) - 1);
951 	  freemem_size = freemem_size & ~ (alignof (size_t) - 1);
952 
953 	  __libc_lock_unlock (lock);
954 	}
955 
956       /* Now domain->conv_tab[act] contains the translation of all
957 	 the plural variants.  */
958       result = domain->conv_tab[act] + sizeof (size_t);
959       resultlen = *(size_t *) domain->conv_tab[act];
960     }
961 
962  converted:
963   /* The result string is converted.  */
964 
965 #endif /* _LIBC || HAVE_ICONV */
966 
967   *lengthp = resultlen;
968   return result;
969 }
970 
971 
972 /* Look up a plural variant.  */
973 static char *
974 internal_function
plural_lookup(domain,n,translation,translation_len)975 plural_lookup (domain, n, translation, translation_len)
976      struct loaded_l10nfile *domain;
977      unsigned long int n;
978      const char *translation;
979      size_t translation_len;
980 {
981   struct loaded_domain *domaindata = (struct loaded_domain *) domain->data;
982   unsigned long int index;
983   const char *p;
984 
985   index = plural_eval (domaindata->plural, n);
986   if (index >= domaindata->nplurals)
987     /* This should never happen.  It means the plural expression and the
988        given maximum value do not match.  */
989     index = 0;
990 
991   /* Skip INDEX strings at TRANSLATION.  */
992   p = translation;
993   while (index-- > 0)
994     {
995 #ifdef _LIBC
996       p = __rawmemchr (p, '\0');
997 #else
998       p = strchr (p, '\0');
999 #endif
1000       /* And skip over the NUL byte.  */
1001       p++;
1002 
1003       if (p >= translation + translation_len)
1004 	/* This should never happen.  It means the plural expression
1005 	   evaluated to a value larger than the number of variants
1006 	   available for MSGID1.  */
1007 	return (char *) translation;
1008     }
1009   return (char *) p;
1010 }
1011 
1012 
1013 /* Function to evaluate the plural expression and return an index value.  */
1014 static unsigned long int
1015 internal_function
plural_eval(pexp,n)1016 plural_eval (pexp, n)
1017      struct expression *pexp;
1018      unsigned long int n;
1019 {
1020   switch (pexp->nargs)
1021     {
1022     case 0:
1023       switch (pexp->operation)
1024 	{
1025 	case var:
1026 	  return n;
1027 	case num:
1028 	  return pexp->val.num;
1029 	default:
1030 	  break;
1031 	}
1032       /* NOTREACHED */
1033       break;
1034     case 1:
1035       {
1036 	/* pexp->operation must be lnot.  */
1037 	unsigned long int arg = plural_eval (pexp->val.args[0], n);
1038 	return ! arg;
1039       }
1040     case 2:
1041       {
1042 	unsigned long int leftarg = plural_eval (pexp->val.args[0], n);
1043 	if (pexp->operation == lor)
1044 	  return leftarg || plural_eval (pexp->val.args[1], n);
1045 	else if (pexp->operation == land)
1046 	  return leftarg && plural_eval (pexp->val.args[1], n);
1047 	else
1048 	  {
1049 	    unsigned long int rightarg = plural_eval (pexp->val.args[1], n);
1050 
1051 	    switch (pexp->operation)
1052 	      {
1053 	      case mult:
1054 		return leftarg * rightarg;
1055 	      case divide:
1056 		return leftarg / rightarg;
1057 	      case module:
1058 		return leftarg % rightarg;
1059 	      case plus:
1060 		return leftarg + rightarg;
1061 	      case minus:
1062 		return leftarg - rightarg;
1063 	      case less_than:
1064 		return leftarg < rightarg;
1065 	      case greater_than:
1066 		return leftarg > rightarg;
1067 	      case less_or_equal:
1068 		return leftarg <= rightarg;
1069 	      case greater_or_equal:
1070 		return leftarg >= rightarg;
1071 	      case equal:
1072 		return leftarg == rightarg;
1073 	      case not_equal:
1074 		return leftarg != rightarg;
1075 	      default:
1076 		break;
1077 	      }
1078 	  }
1079 	/* NOTREACHED */
1080 	break;
1081       }
1082     case 3:
1083       {
1084 	/* pexp->operation must be qmop.  */
1085 	unsigned long int boolarg = plural_eval (pexp->val.args[0], n);
1086 	return plural_eval (pexp->val.args[boolarg ? 1 : 2], n);
1087       }
1088     }
1089   /* NOTREACHED */
1090   return 0;
1091 }
1092 
1093 
1094 /* Return string representation of locale CATEGORY.  */
1095 static const char *
1096 internal_function
category_to_name(category)1097 category_to_name (category)
1098      int category;
1099 {
1100   const char *retval;
1101 
1102   switch (category)
1103   {
1104 #ifdef LC_COLLATE
1105   case LC_COLLATE:
1106     retval = "LC_COLLATE";
1107     break;
1108 #endif
1109 #ifdef LC_CTYPE
1110   case LC_CTYPE:
1111     retval = "LC_CTYPE";
1112     break;
1113 #endif
1114 #ifdef LC_MONETARY
1115   case LC_MONETARY:
1116     retval = "LC_MONETARY";
1117     break;
1118 #endif
1119 #ifdef LC_NUMERIC
1120   case LC_NUMERIC:
1121     retval = "LC_NUMERIC";
1122     break;
1123 #endif
1124 #ifdef LC_TIME
1125   case LC_TIME:
1126     retval = "LC_TIME";
1127     break;
1128 #endif
1129 #ifdef LC_MESSAGES
1130   case LC_MESSAGES:
1131     retval = "LC_MESSAGES";
1132     break;
1133 #endif
1134 #ifdef LC_RESPONSE
1135   case LC_RESPONSE:
1136     retval = "LC_RESPONSE";
1137     break;
1138 #endif
1139 #ifdef LC_ALL
1140   case LC_ALL:
1141     /* This might not make sense but is perhaps better than any other
1142        value.  */
1143     retval = "LC_ALL";
1144     break;
1145 #endif
1146   default:
1147     /* If you have a better idea for a default value let me know.  */
1148     retval = "LC_XXX";
1149   }
1150 
1151   return retval;
1152 }
1153 
1154 /* Guess value of current locale from value of the environment variables.  */
1155 static const char *
1156 internal_function
guess_category_value(category,categoryname)1157 guess_category_value (category, categoryname)
1158      int category;
1159      const char *categoryname;
1160 {
1161   const char *language;
1162   const char *retval;
1163 
1164   /* The highest priority value is the `LANGUAGE' environment
1165      variable.  But we don't use the value if the currently selected
1166      locale is the C locale.  This is a GNU extension.  */
1167   language = getenv ("LANGUAGE");
1168   if (language != NULL && language[0] == '\0')
1169     language = NULL;
1170 
1171   /* We have to proceed with the POSIX methods of looking to `LC_ALL',
1172      `LC_xxx', and `LANG'.  On some systems this can be done by the
1173      `setlocale' function itself.  */
1174 #if defined _LIBC || (defined HAVE_SETLOCALE && defined HAVE_LC_MESSAGES && defined HAVE_LOCALE_NULL)
1175   retval = setlocale (category, NULL);
1176 #else
1177   /* Setting of LC_ALL overwrites all other.  */
1178   retval = getenv ("LC_ALL");
1179   if (retval == NULL || retval[0] == '\0')
1180     {
1181       /* Next comes the name of the desired category.  */
1182       retval = getenv (categoryname);
1183       if (retval == NULL || retval[0] == '\0')
1184 	{
1185 	  /* Last possibility is the LANG environment variable.  */
1186 	  retval = getenv ("LANG");
1187 	  if (retval == NULL || retval[0] == '\0')
1188 	    /* We use C as the default domain.  POSIX says this is
1189 	       implementation defined.  */
1190 	    return "C";
1191 	}
1192     }
1193 #endif
1194 
1195   return language != NULL && strcmp (retval, "C") != 0 ? language : retval;
1196 }
1197 
1198 /* @@ begin of epilog @@ */
1199 
1200 /* We don't want libintl.a to depend on any other library.  So we
1201    avoid the non-standard function stpcpy.  In GNU C Library this
1202    function is available, though.  Also allow the symbol HAVE_STPCPY
1203    to be defined.  */
1204 #if !_LIBC && !HAVE_STPCPY
1205 static char *
stpcpy(dest,src)1206 stpcpy (dest, src)
1207      char *dest;
1208      const char *src;
1209 {
1210   while ((*dest++ = *src++) != '\0')
1211     /* Do nothing. */ ;
1212   return dest - 1;
1213 }
1214 #endif
1215 
1216 #if !_LIBC && !HAVE_MEMPCPY
1217 static void *
mempcpy(dest,src,n)1218 mempcpy (dest, src, n)
1219      void *dest;
1220      const void *src;
1221      size_t n;
1222 {
1223   return (void *) ((char *) memcpy (dest, src, n) + n);
1224 }
1225 #endif
1226 
1227 
1228 #ifdef _LIBC
1229 /* If we want to free all resources we have to do some work at
1230    program's end.  */
1231 static void __attribute__ ((unused))
free_mem(void)1232 free_mem (void)
1233 {
1234   void *old;
1235 
1236   while (_nl_domain_bindings != NULL)
1237     {
1238       struct binding *oldp = _nl_domain_bindings;
1239       _nl_domain_bindings = _nl_domain_bindings->next;
1240       if (oldp->dirname != _nl_default_dirname)
1241 	/* Yes, this is a pointer comparison.  */
1242 	free (oldp->dirname);
1243       free (oldp->codeset);
1244       free (oldp);
1245     }
1246 
1247   if (_nl_current_default_domain != _nl_default_default_domain)
1248     /* Yes, again a pointer comparison.  */
1249     free ((char *) _nl_current_default_domain);
1250 
1251   /* Remove the search tree with the known translations.  */
1252   __tdestroy (root, free);
1253   root = NULL;
1254 
1255   while (transmem_list != NULL)
1256     {
1257       old = transmem_list;
1258       transmem_list = transmem_list->next;
1259       free (old);
1260     }
1261 }
1262 
1263 text_set_element (__libc_subfreeres, free_mem);
1264 #endif
1265