| 1 | /* Implementation of the internal dcigettext function. | 
|---|
| 2 | Copyright (C) 1995-2020 Free Software Foundation, Inc. | 
|---|
| 3 |  | 
|---|
| 4 | This program is free software: you can redistribute it and/or modify | 
|---|
| 5 | it under the terms of the GNU Lesser General Public License as published by | 
|---|
| 6 | the Free Software Foundation; either version 2.1 of the License, or | 
|---|
| 7 | (at your option) 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 | 
|---|
| 12 | GNU Lesser General Public License for more details. | 
|---|
| 13 |  | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License | 
|---|
| 15 | along with this program.  If not, see <https://www.gnu.org/licenses/>.  */ | 
|---|
| 16 |  | 
|---|
| 17 | /* Tell glibc's <string.h> to provide a prototype for mempcpy(). | 
|---|
| 18 | This must come before <config.h> because <config.h> may include | 
|---|
| 19 | <features.h>, and once <features.h> has been included, it's too late.  */ | 
|---|
| 20 | #ifndef _GNU_SOURCE | 
|---|
| 21 | # define _GNU_SOURCE	1 | 
|---|
| 22 | #endif | 
|---|
| 23 |  | 
|---|
| 24 | #ifdef HAVE_CONFIG_H | 
|---|
| 25 | # include <config.h> | 
|---|
| 26 | #endif | 
|---|
| 27 |  | 
|---|
| 28 | #include <sys/types.h> | 
|---|
| 29 |  | 
|---|
| 30 | #ifdef __GNUC__ | 
|---|
| 31 | # define alloca __builtin_alloca | 
|---|
| 32 | # define HAVE_ALLOCA 1 | 
|---|
| 33 | #else | 
|---|
| 34 | # ifdef _MSC_VER | 
|---|
| 35 | #  include <malloc.h> | 
|---|
| 36 | #  define alloca _alloca | 
|---|
| 37 | # else | 
|---|
| 38 | #  if defined HAVE_ALLOCA_H || defined _LIBC | 
|---|
| 39 | #   include <alloca.h> | 
|---|
| 40 | #  else | 
|---|
| 41 | #   ifdef _AIX | 
|---|
| 42 | #pragma alloca | 
|---|
| 43 | #   else | 
|---|
| 44 | #    ifndef alloca | 
|---|
| 45 | char *alloca (); | 
|---|
| 46 | #    endif | 
|---|
| 47 | #   endif | 
|---|
| 48 | #  endif | 
|---|
| 49 | # endif | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | #include <errno.h> | 
|---|
| 53 | #ifndef errno | 
|---|
| 54 | extern int errno; | 
|---|
| 55 | #endif | 
|---|
| 56 | #ifndef __set_errno | 
|---|
| 57 | # define __set_errno(val) errno = (val) | 
|---|
| 58 | #endif | 
|---|
| 59 |  | 
|---|
| 60 | #include <stddef.h> | 
|---|
| 61 | #include <stdlib.h> | 
|---|
| 62 | #include <stdio.h> | 
|---|
| 63 | #include <string.h> | 
|---|
| 64 |  | 
|---|
| 65 | #if defined HAVE_UNISTD_H || defined _LIBC | 
|---|
| 66 | # include <unistd.h> | 
|---|
| 67 | #endif | 
|---|
| 68 |  | 
|---|
| 69 | #include <locale.h> | 
|---|
| 70 |  | 
|---|
| 71 | #ifdef _LIBC | 
|---|
| 72 | /* Guess whether integer division by zero raises signal SIGFPE. | 
|---|
| 73 | Set to 1 only if you know for sure.  In case of doubt, set to 0.  */ | 
|---|
| 74 | # if defined __alpha__ || defined __arm__ || defined __i386__ \ | 
|---|
| 75 | || defined __m68k__ || defined __s390__ | 
|---|
| 76 | #  define INTDIV0_RAISES_SIGFPE 1 | 
|---|
| 77 | # else | 
|---|
| 78 | #  define INTDIV0_RAISES_SIGFPE 0 | 
|---|
| 79 | # endif | 
|---|
| 80 | #endif | 
|---|
| 81 | #if !INTDIV0_RAISES_SIGFPE | 
|---|
| 82 | # include <signal.h> | 
|---|
| 83 | #endif | 
|---|
| 84 |  | 
|---|
| 85 | #if defined HAVE_SYS_PARAM_H || defined _LIBC | 
|---|
| 86 | # include <sys/param.h> | 
|---|
| 87 | #endif | 
|---|
| 88 |  | 
|---|
| 89 | #if !defined _LIBC | 
|---|
| 90 | # include "localcharset.h" | 
|---|
| 91 | #endif | 
|---|
| 92 |  | 
|---|
| 93 | #include "gettextP.h" | 
|---|
| 94 | #include "plural-exp.h" | 
|---|
| 95 | #ifdef _LIBC | 
|---|
| 96 | # include <libintl.h> | 
|---|
| 97 | #else | 
|---|
| 98 | # ifdef IN_LIBGLOCALE | 
|---|
| 99 | #  include <libintl.h> | 
|---|
| 100 | # endif | 
|---|
| 101 | # include "libgnuintl.h" | 
|---|
| 102 | #endif | 
|---|
| 103 | #include "hash-string.h" | 
|---|
| 104 |  | 
|---|
| 105 | /* Handle multi-threaded applications.  */ | 
|---|
| 106 | #ifdef _LIBC | 
|---|
| 107 | # include <libc-lock.h> | 
|---|
| 108 | # define gl_rwlock_define_initialized __libc_rwlock_define_initialized | 
|---|
| 109 | # define gl_rwlock_rdlock __libc_rwlock_rdlock | 
|---|
| 110 | # define gl_rwlock_wrlock __libc_rwlock_wrlock | 
|---|
| 111 | # define gl_rwlock_unlock __libc_rwlock_unlock | 
|---|
| 112 | #else | 
|---|
| 113 | # include "lock.h" | 
|---|
| 114 | #endif | 
|---|
| 115 |  | 
|---|
| 116 | /* Alignment of types.  */ | 
|---|
| 117 | #if defined __GNUC__ && __GNUC__ >= 2 | 
|---|
| 118 | # define alignof(TYPE) __alignof__ (TYPE) | 
|---|
| 119 | #else | 
|---|
| 120 | # define alignof(TYPE) \ | 
|---|
| 121 | ((int) &((struct { char dummy1; TYPE dummy2; } *) 0)->dummy2) | 
|---|
| 122 | #endif | 
|---|
| 123 |  | 
|---|
| 124 | /* Some compilers, like SunOS4 cc, don't have offsetof in <stddef.h>.  */ | 
|---|
| 125 | #ifndef offsetof | 
|---|
| 126 | # define offsetof(type,ident) ((size_t)&(((type*)0)->ident)) | 
|---|
| 127 | #endif | 
|---|
| 128 |  | 
|---|
| 129 | /* @@ end of prolog @@ */ | 
|---|
| 130 |  | 
|---|
| 131 | #ifdef _LIBC | 
|---|
| 132 | /* Rename the non ANSI C functions.  This is required by the standard | 
|---|
| 133 | because some ANSI C functions will require linking with this object | 
|---|
| 134 | file and the name space must not be polluted.  */ | 
|---|
| 135 | # define strdup __strdup | 
|---|
| 136 | # define getcwd __getcwd | 
|---|
| 137 | # ifndef stpcpy | 
|---|
| 138 | #  define stpcpy __stpcpy | 
|---|
| 139 | # endif | 
|---|
| 140 | # define tfind __tfind | 
|---|
| 141 | #else | 
|---|
| 142 | # if !defined HAVE_GETCWD | 
|---|
| 143 | char *getwd (); | 
|---|
| 144 | #  define getcwd(buf, max) getwd (buf) | 
|---|
| 145 | # else | 
|---|
| 146 | #  if VMS | 
|---|
| 147 | #   define getcwd(buf, max) (getcwd) (buf, max, 0) | 
|---|
| 148 | #  else | 
|---|
| 149 | char *getcwd (); | 
|---|
| 150 | #  endif | 
|---|
| 151 | # endif | 
|---|
| 152 | # ifndef HAVE_STPCPY | 
|---|
| 153 | static char *stpcpy (char *dest, const char *src); | 
|---|
| 154 | # endif | 
|---|
| 155 | # ifndef HAVE_MEMPCPY | 
|---|
| 156 | static void *mempcpy (void *dest, const void *src, size_t n); | 
|---|
| 157 | # endif | 
|---|
| 158 | #endif | 
|---|
| 159 |  | 
|---|
| 160 | /* Use a replacement if the system does not provide the `tsearch' function | 
|---|
| 161 | family.  */ | 
|---|
| 162 | #if defined HAVE_TSEARCH || defined _LIBC | 
|---|
| 163 | # include <search.h> | 
|---|
| 164 | #else | 
|---|
| 165 | # define tsearch libintl_tsearch | 
|---|
| 166 | # define tfind libintl_tfind | 
|---|
| 167 | # define tdelete libintl_tdelete | 
|---|
| 168 | # define twalk libintl_twalk | 
|---|
| 169 | # include "tsearch.h" | 
|---|
| 170 | #endif | 
|---|
| 171 |  | 
|---|
| 172 | #ifdef _LIBC | 
|---|
| 173 | # define tsearch __tsearch | 
|---|
| 174 | #endif | 
|---|
| 175 |  | 
|---|
| 176 | /* Amount to increase buffer size by in each try.  */ | 
|---|
| 177 | #define PATH_INCR 32 | 
|---|
| 178 |  | 
|---|
| 179 | /* The following is from pathmax.h.  */ | 
|---|
| 180 | /* Non-POSIX BSD systems might have gcc's limits.h, which doesn't define | 
|---|
| 181 | PATH_MAX but might cause redefinition warnings when sys/param.h is | 
|---|
| 182 | later included (as on MORE/BSD 4.3).  */ | 
|---|
| 183 | #if defined _POSIX_VERSION || (defined HAVE_LIMITS_H && !defined __GNUC__) | 
|---|
| 184 | # include <limits.h> | 
|---|
| 185 | #endif | 
|---|
| 186 |  | 
|---|
| 187 | #ifndef _POSIX_PATH_MAX | 
|---|
| 188 | # define _POSIX_PATH_MAX 255 | 
|---|
| 189 | #endif | 
|---|
| 190 |  | 
|---|
| 191 | #if !defined PATH_MAX && defined _PC_PATH_MAX | 
|---|
| 192 | # define PATH_MAX (__pathconf ("/", _PC_PATH_MAX) < 1 ? 1024 : __pathconf ("/", _PC_PATH_MAX)) | 
|---|
| 193 | #endif | 
|---|
| 194 |  | 
|---|
| 195 | /* Don't include sys/param.h if it already has been.  */ | 
|---|
| 196 | #if defined HAVE_SYS_PARAM_H && !defined PATH_MAX && !defined MAXPATHLEN | 
|---|
| 197 | # include <sys/param.h> | 
|---|
| 198 | #endif | 
|---|
| 199 |  | 
|---|
| 200 | #if !defined PATH_MAX && defined MAXPATHLEN | 
|---|
| 201 | # define PATH_MAX MAXPATHLEN | 
|---|
| 202 | #endif | 
|---|
| 203 |  | 
|---|
| 204 | #ifndef PATH_MAX | 
|---|
| 205 | # define PATH_MAX _POSIX_PATH_MAX | 
|---|
| 206 | #endif | 
|---|
| 207 |  | 
|---|
| 208 | /* Pathname support. | 
|---|
| 209 | ISSLASH(C)           tests whether C is a directory separator character. | 
|---|
| 210 | IS_ABSOLUTE_PATH(P)  tests whether P is an absolute path.  If it is not, | 
|---|
| 211 | it may be concatenated to a directory pathname. | 
|---|
| 212 | IS_PATH_WITH_DIR(P)  tests whether P contains a directory specification. | 
|---|
| 213 | */ | 
|---|
| 214 | #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__ | 
|---|
| 215 | /* Win32, Cygwin, OS/2, DOS */ | 
|---|
| 216 | # define ISSLASH(C) ((C) == '/' || (C) == '\\') | 
|---|
| 217 | # define HAS_DEVICE(P) \ | 
|---|
| 218 | ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \ | 
|---|
| 219 | && (P)[1] == ':') | 
|---|
| 220 | # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P)) | 
|---|
| 221 | # define IS_PATH_WITH_DIR(P) \ | 
|---|
| 222 | (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P)) | 
|---|
| 223 | #else | 
|---|
| 224 | /* Unix */ | 
|---|
| 225 | # define ISSLASH(C) ((C) == '/') | 
|---|
| 226 | # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0]) | 
|---|
| 227 | # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL) | 
|---|
| 228 | #endif | 
|---|
| 229 |  | 
|---|
| 230 | /* Whether to support different locales in different threads.  */ | 
|---|
| 231 | #if defined _LIBC || HAVE_USELOCALE || defined IN_LIBGLOCALE | 
|---|
| 232 | # define HAVE_PER_THREAD_LOCALE | 
|---|
| 233 | #endif | 
|---|
| 234 |  | 
|---|
| 235 | /* This is the type used for the search tree where known translations | 
|---|
| 236 | are stored.  */ | 
|---|
| 237 | struct known_translation_t | 
|---|
| 238 | { | 
|---|
| 239 | /* Domain in which to search.  */ | 
|---|
| 240 | const char *domainname; | 
|---|
| 241 |  | 
|---|
| 242 | /* The category.  */ | 
|---|
| 243 | int category; | 
|---|
| 244 |  | 
|---|
| 245 | #ifdef HAVE_PER_THREAD_LOCALE | 
|---|
| 246 | /* Name of the relevant locale category, or "" for the global locale.  */ | 
|---|
| 247 | const char *localename; | 
|---|
| 248 | #endif | 
|---|
| 249 |  | 
|---|
| 250 | #ifdef IN_LIBGLOCALE | 
|---|
| 251 | /* The character encoding.  */ | 
|---|
| 252 | const char *encoding; | 
|---|
| 253 | #endif | 
|---|
| 254 |  | 
|---|
| 255 | /* State of the catalog counter at the point the string was found.  */ | 
|---|
| 256 | int counter; | 
|---|
| 257 |  | 
|---|
| 258 | /* Catalog where the string was found.  */ | 
|---|
| 259 | struct loaded_l10nfile *domain; | 
|---|
| 260 |  | 
|---|
| 261 | /* And finally the translation.  */ | 
|---|
| 262 | const char *translation; | 
|---|
| 263 | size_t translation_length; | 
|---|
| 264 |  | 
|---|
| 265 | /* Pointer to the string in question.  */ | 
|---|
| 266 | union | 
|---|
| 267 | { | 
|---|
| 268 | char appended[ZERO];  /* used if domain != NULL */ | 
|---|
| 269 | const char *ptr;      /* used if domain == NULL */ | 
|---|
| 270 | } | 
|---|
| 271 | msgid; | 
|---|
| 272 | }; | 
|---|
| 273 |  | 
|---|
| 274 | gl_rwlock_define_initialized (static, tree_lock) | 
|---|
| 275 |  | 
|---|
| 276 | /* Root of the search tree with known translations.  */ | 
|---|
| 277 | static void *root; | 
|---|
| 278 |  | 
|---|
| 279 | /* Function to compare two entries in the table of known translations.  */ | 
|---|
| 280 | static int | 
|---|
| 281 | transcmp (const void *p1, const void *p2) | 
|---|
| 282 | { | 
|---|
| 283 | const struct known_translation_t *s1; | 
|---|
| 284 | const struct known_translation_t *s2; | 
|---|
| 285 | int result; | 
|---|
| 286 |  | 
|---|
| 287 | s1 = (const struct known_translation_t *) p1; | 
|---|
| 288 | s2 = (const struct known_translation_t *) p2; | 
|---|
| 289 |  | 
|---|
| 290 | result = strcmp (s1->domain != NULL ? s1->msgid.appended : s1->msgid.ptr, | 
|---|
| 291 | s2->domain != NULL ? s2->msgid.appended : s2->msgid.ptr); | 
|---|
| 292 | if (result == 0) | 
|---|
| 293 | { | 
|---|
| 294 | result = strcmp (s1->domainname, s2->domainname); | 
|---|
| 295 | if (result == 0) | 
|---|
| 296 | { | 
|---|
| 297 | #ifdef HAVE_PER_THREAD_LOCALE | 
|---|
| 298 | result = strcmp (s1->localename, s2->localename); | 
|---|
| 299 | if (result == 0) | 
|---|
| 300 | #endif | 
|---|
| 301 | { | 
|---|
| 302 | #ifdef IN_LIBGLOCALE | 
|---|
| 303 | result = strcmp (s1->encoding, s2->encoding); | 
|---|
| 304 | if (result == 0) | 
|---|
| 305 | #endif | 
|---|
| 306 | /* We compare the category last (though this is the cheapest | 
|---|
| 307 | operation) since it is hopefully always the same (namely | 
|---|
| 308 | LC_MESSAGES).  */ | 
|---|
| 309 | result = s1->category - s2->category; | 
|---|
| 310 | } | 
|---|
| 311 | } | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | return result; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | /* Name of the default domain used for gettext(3) prior any call to | 
|---|
| 318 | textdomain(3).  The default value for this is "messages".  */ | 
|---|
| 319 | const char _nl_default_default_domain[] attribute_hidden = "messages"; | 
|---|
| 320 |  | 
|---|
| 321 | #ifndef IN_LIBGLOCALE | 
|---|
| 322 | /* Value used as the default domain for gettext(3).  */ | 
|---|
| 323 | const char *_nl_current_default_domain attribute_hidden | 
|---|
| 324 | = _nl_default_default_domain; | 
|---|
| 325 | #endif | 
|---|
| 326 |  | 
|---|
| 327 | /* Contains the default location of the message catalogs.  */ | 
|---|
| 328 | #if defined __EMX__ | 
|---|
| 329 | extern const char _nl_default_dirname[]; | 
|---|
| 330 | #else | 
|---|
| 331 | # ifdef _LIBC | 
|---|
| 332 | extern const char _nl_default_dirname[]; | 
|---|
| 333 | libc_hidden_proto (_nl_default_dirname) | 
|---|
| 334 | # endif | 
|---|
| 335 | const char _nl_default_dirname[] = LOCALEDIR; | 
|---|
| 336 | # ifdef _LIBC | 
|---|
| 337 | libc_hidden_data_def (_nl_default_dirname) | 
|---|
| 338 | # endif | 
|---|
| 339 | #endif | 
|---|
| 340 |  | 
|---|
| 341 | #ifndef IN_LIBGLOCALE | 
|---|
| 342 | /* List with bindings of specific domains created by bindtextdomain() | 
|---|
| 343 | calls.  */ | 
|---|
| 344 | struct binding *_nl_domain_bindings; | 
|---|
| 345 | #endif | 
|---|
| 346 |  | 
|---|
| 347 | /* Prototypes for local functions.  */ | 
|---|
| 348 | static char *plural_lookup (struct loaded_l10nfile *domain, | 
|---|
| 349 | unsigned long int n, | 
|---|
| 350 | const char *translation, size_t translation_len); | 
|---|
| 351 |  | 
|---|
| 352 | #ifdef IN_LIBGLOCALE | 
|---|
| 353 | static const char *guess_category_value (int category, | 
|---|
| 354 | const char *categoryname, | 
|---|
| 355 | const char *localename); | 
|---|
| 356 | #else | 
|---|
| 357 | static const char *guess_category_value (int category, | 
|---|
| 358 | const char *categoryname); | 
|---|
| 359 | #endif | 
|---|
| 360 |  | 
|---|
| 361 | #ifdef _LIBC | 
|---|
| 362 | # include "../locale/localeinfo.h" | 
|---|
| 363 | # define category_to_name(category) _nl_category_names_get (category) | 
|---|
| 364 | #else | 
|---|
| 365 | static const char *category_to_name (int category); | 
|---|
| 366 | #endif | 
|---|
| 367 | #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE | 
|---|
| 368 | static const char *get_output_charset (struct binding *domainbinding); | 
|---|
| 369 | #endif | 
|---|
| 370 |  | 
|---|
| 371 |  | 
|---|
| 372 | /* For those losing systems which don't have `alloca' we have to add | 
|---|
| 373 | some additional code emulating it.  */ | 
|---|
| 374 | #ifdef HAVE_ALLOCA | 
|---|
| 375 | /* Nothing has to be done.  */ | 
|---|
| 376 | # define freea(p) /* nothing */ | 
|---|
| 377 | # define ADD_BLOCK(list, address) /* nothing */ | 
|---|
| 378 | # define FREE_BLOCKS(list) /* nothing */ | 
|---|
| 379 | #else | 
|---|
| 380 | struct block_list | 
|---|
| 381 | { | 
|---|
| 382 | void *address; | 
|---|
| 383 | struct block_list *next; | 
|---|
| 384 | }; | 
|---|
| 385 | # define ADD_BLOCK(list, addr)						      \ | 
|---|
| 386 | do {									      \ | 
|---|
| 387 | struct block_list *newp = (struct block_list *) malloc (sizeof (*newp));  \ | 
|---|
| 388 | /* If we cannot get a free block we cannot add the new element to	      \ | 
|---|
| 389 | the list.  */							      \ | 
|---|
| 390 | if (newp != NULL) {							      \ | 
|---|
| 391 | newp->address = (addr);						      \ | 
|---|
| 392 | newp->next = (list);						      \ | 
|---|
| 393 | (list) = newp;							      \ | 
|---|
| 394 | }									      \ | 
|---|
| 395 | } while (0) | 
|---|
| 396 | # define FREE_BLOCKS(list)						      \ | 
|---|
| 397 | do {									      \ | 
|---|
| 398 | while (list != NULL) {						      \ | 
|---|
| 399 | struct block_list *old = list;					      \ | 
|---|
| 400 | list = list->next;						      \ | 
|---|
| 401 | free (old->address);						      \ | 
|---|
| 402 | free (old);							      \ | 
|---|
| 403 | }									      \ | 
|---|
| 404 | } while (0) | 
|---|
| 405 | # undef alloca | 
|---|
| 406 | # define alloca(size) (malloc (size)) | 
|---|
| 407 | # define freea(p) free (p) | 
|---|
| 408 | #endif	/* have alloca */ | 
|---|
| 409 |  | 
|---|
| 410 |  | 
|---|
| 411 | #ifdef _LIBC | 
|---|
| 412 | /* List of blocks allocated for translations.  */ | 
|---|
| 413 | typedef struct transmem_list | 
|---|
| 414 | { | 
|---|
| 415 | struct transmem_list *next; | 
|---|
| 416 | char data[ZERO]; | 
|---|
| 417 | } transmem_block_t; | 
|---|
| 418 | static struct transmem_list *transmem_list; | 
|---|
| 419 | #else | 
|---|
| 420 | typedef unsigned char transmem_block_t; | 
|---|
| 421 | #endif | 
|---|
| 422 |  | 
|---|
| 423 |  | 
|---|
| 424 | /* Names for the libintl functions are a problem.  They must not clash | 
|---|
| 425 | with existing names and they should follow ANSI C.  But this source | 
|---|
| 426 | code is also used in GNU C Library where the names have a __ | 
|---|
| 427 | prefix.  So we have to make a difference here.  */ | 
|---|
| 428 | #ifdef _LIBC | 
|---|
| 429 | # define DCIGETTEXT __dcigettext | 
|---|
| 430 | #else | 
|---|
| 431 | # define DCIGETTEXT libintl_dcigettext | 
|---|
| 432 | #endif | 
|---|
| 433 |  | 
|---|
| 434 | /* Lock variable to protect the global data in the gettext implementation.  */ | 
|---|
| 435 | gl_rwlock_define_initialized (, _nl_state_lock attribute_hidden) | 
|---|
| 436 |  | 
|---|
| 437 | /* Checking whether the binaries runs SUID must be done and glibc provides | 
|---|
| 438 | easier methods therefore we make a difference here.  */ | 
|---|
| 439 | #ifdef _LIBC | 
|---|
| 440 | # define ENABLE_SECURE __libc_enable_secure | 
|---|
| 441 | # define DETERMINE_SECURE | 
|---|
| 442 | #else | 
|---|
| 443 | # ifndef HAVE_GETUID | 
|---|
| 444 | #  define getuid() 0 | 
|---|
| 445 | # endif | 
|---|
| 446 | # ifndef HAVE_GETGID | 
|---|
| 447 | #  define getgid() 0 | 
|---|
| 448 | # endif | 
|---|
| 449 | # ifndef HAVE_GETEUID | 
|---|
| 450 | #  define geteuid() getuid() | 
|---|
| 451 | # endif | 
|---|
| 452 | # ifndef HAVE_GETEGID | 
|---|
| 453 | #  define getegid() getgid() | 
|---|
| 454 | # endif | 
|---|
| 455 | static int enable_secure; | 
|---|
| 456 | # define ENABLE_SECURE (enable_secure == 1) | 
|---|
| 457 | # define DETERMINE_SECURE \ | 
|---|
| 458 | if (enable_secure == 0)						      \ | 
|---|
| 459 | {									      \ | 
|---|
| 460 | if (getuid () != geteuid () || getgid () != getegid ())		      \ | 
|---|
| 461 | enable_secure = 1;						      \ | 
|---|
| 462 | else								      \ | 
|---|
| 463 | enable_secure = -1;						      \ | 
|---|
| 464 | } | 
|---|
| 465 | #endif | 
|---|
| 466 |  | 
|---|
| 467 | /* Get the function to evaluate the plural expression.  */ | 
|---|
| 468 | #include "eval-plural.h" | 
|---|
| 469 |  | 
|---|
| 470 | /* Look up MSGID in the DOMAINNAME message catalog for the current | 
|---|
| 471 | CATEGORY locale and, if PLURAL is nonzero, search over string | 
|---|
| 472 | depending on the plural form determined by N.  */ | 
|---|
| 473 | #ifdef IN_LIBGLOCALE | 
|---|
| 474 | char * | 
|---|
| 475 | gl_dcigettext (const char *domainname, | 
|---|
| 476 | const char *msgid1, const char *msgid2, | 
|---|
| 477 | int plural, unsigned long int n, | 
|---|
| 478 | int category, | 
|---|
| 479 | const char *localename, const char *encoding) | 
|---|
| 480 | #else | 
|---|
| 481 | char * | 
|---|
| 482 | DCIGETTEXT (const char *domainname, const char *msgid1, const char *msgid2, | 
|---|
| 483 | int plural, unsigned long int n, int category) | 
|---|
| 484 | #endif | 
|---|
| 485 | { | 
|---|
| 486 | #ifndef HAVE_ALLOCA | 
|---|
| 487 | struct block_list *block_list = NULL; | 
|---|
| 488 | #endif | 
|---|
| 489 | struct loaded_l10nfile *domain; | 
|---|
| 490 | struct binding *binding; | 
|---|
| 491 | const char *categoryname; | 
|---|
| 492 | const char *categoryvalue; | 
|---|
| 493 | const char *dirname; | 
|---|
| 494 | char *xdirname = NULL; | 
|---|
| 495 | char *xdomainname; | 
|---|
| 496 | char *single_locale; | 
|---|
| 497 | char *retval; | 
|---|
| 498 | size_t retlen; | 
|---|
| 499 | int saved_errno; | 
|---|
| 500 | struct known_translation_t search; | 
|---|
| 501 | struct known_translation_t **foundp = NULL; | 
|---|
| 502 | #if defined HAVE_PER_THREAD_LOCALE && !defined IN_LIBGLOCALE | 
|---|
| 503 | const char *localename; | 
|---|
| 504 | #endif | 
|---|
| 505 | size_t domainname_len; | 
|---|
| 506 |  | 
|---|
| 507 | /* If no real MSGID is given return NULL.  */ | 
|---|
| 508 | if (msgid1 == NULL) | 
|---|
| 509 | return NULL; | 
|---|
| 510 |  | 
|---|
| 511 | #ifdef _LIBC | 
|---|
| 512 | if (category < 0 || category >= __LC_LAST || category == LC_ALL) | 
|---|
| 513 | /* Bogus.  */ | 
|---|
| 514 | return (plural == 0 | 
|---|
| 515 | ? (char *) msgid1 | 
|---|
| 516 | /* Use the Germanic plural rule.  */ | 
|---|
| 517 | : n == 1 ? (char *) msgid1 : (char *) msgid2); | 
|---|
| 518 | #endif | 
|---|
| 519 |  | 
|---|
| 520 | /* Preserve the `errno' value.  */ | 
|---|
| 521 | saved_errno = errno; | 
|---|
| 522 |  | 
|---|
| 523 | #ifdef _LIBC | 
|---|
| 524 | __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden) | 
|---|
| 525 | __libc_rwlock_rdlock (__libc_setlocale_lock); | 
|---|
| 526 | #endif | 
|---|
| 527 |  | 
|---|
| 528 | gl_rwlock_rdlock (_nl_state_lock); | 
|---|
| 529 |  | 
|---|
| 530 | /* If DOMAINNAME is NULL, we are interested in the default domain.  If | 
|---|
| 531 | CATEGORY is not LC_MESSAGES this might not make much sense but the | 
|---|
| 532 | definition left this undefined.  */ | 
|---|
| 533 | if (domainname == NULL) | 
|---|
| 534 | domainname = _nl_current_default_domain; | 
|---|
| 535 |  | 
|---|
| 536 | /* OS/2 specific: backward compatibility with older libintl versions  */ | 
|---|
| 537 | #ifdef LC_MESSAGES_COMPAT | 
|---|
| 538 | if (category == LC_MESSAGES_COMPAT) | 
|---|
| 539 | category = LC_MESSAGES; | 
|---|
| 540 | #endif | 
|---|
| 541 |  | 
|---|
| 542 | /* Try to find the translation among those which we found at | 
|---|
| 543 | some time.  */ | 
|---|
| 544 | search.domain = NULL; | 
|---|
| 545 | search.msgid.ptr = msgid1; | 
|---|
| 546 | search.domainname = domainname; | 
|---|
| 547 | search.category = category; | 
|---|
| 548 | #ifdef HAVE_PER_THREAD_LOCALE | 
|---|
| 549 | # ifndef IN_LIBGLOCALE | 
|---|
| 550 | #  ifdef _LIBC | 
|---|
| 551 | localename = __current_locale_name (category); | 
|---|
| 552 | #  else | 
|---|
| 553 | categoryname = category_to_name (category); | 
|---|
| 554 | #   define CATEGORYNAME_INITIALIZED | 
|---|
| 555 | localename = _nl_locale_name_thread_unsafe (category, categoryname); | 
|---|
| 556 | if (localename == NULL) | 
|---|
| 557 | localename = ""; | 
|---|
| 558 | #  endif | 
|---|
| 559 | # endif | 
|---|
| 560 | search.localename = localename; | 
|---|
| 561 | # ifdef IN_LIBGLOCALE | 
|---|
| 562 | search.encoding = encoding; | 
|---|
| 563 | # endif | 
|---|
| 564 |  | 
|---|
| 565 | /* Since tfind/tsearch manage a balanced tree, concurrent tfind and | 
|---|
| 566 | tsearch calls can be fatal.  */ | 
|---|
| 567 | gl_rwlock_rdlock (tree_lock); | 
|---|
| 568 |  | 
|---|
| 569 | foundp = (struct known_translation_t **) tfind (&search, &root, transcmp); | 
|---|
| 570 |  | 
|---|
| 571 | gl_rwlock_unlock (tree_lock); | 
|---|
| 572 |  | 
|---|
| 573 | if (foundp != NULL && (*foundp)->counter == _nl_msg_cat_cntr) | 
|---|
| 574 | { | 
|---|
| 575 | /* Now deal with plural.  */ | 
|---|
| 576 | if (plural) | 
|---|
| 577 | retval = plural_lookup ((*foundp)->domain, n, (*foundp)->translation, | 
|---|
| 578 | (*foundp)->translation_length); | 
|---|
| 579 | else | 
|---|
| 580 | retval = (char *) (*foundp)->translation; | 
|---|
| 581 |  | 
|---|
| 582 | gl_rwlock_unlock (_nl_state_lock); | 
|---|
| 583 | # ifdef _LIBC | 
|---|
| 584 | __libc_rwlock_unlock (__libc_setlocale_lock); | 
|---|
| 585 | # endif | 
|---|
| 586 | __set_errno (saved_errno); | 
|---|
| 587 | return retval; | 
|---|
| 588 | } | 
|---|
| 589 | #endif | 
|---|
| 590 |  | 
|---|
| 591 | /* See whether this is a SUID binary or not.  */ | 
|---|
| 592 | DETERMINE_SECURE; | 
|---|
| 593 |  | 
|---|
| 594 | /* First find matching binding.  */ | 
|---|
| 595 | #ifdef IN_LIBGLOCALE | 
|---|
| 596 | /* We can use a trivial binding, since _nl_find_msg will ignore it anyway, | 
|---|
| 597 | and _nl_load_domain and _nl_find_domain just pass it through.  */ | 
|---|
| 598 | binding = NULL; | 
|---|
| 599 | dirname = bindtextdomain (domainname, NULL); | 
|---|
| 600 | #else | 
|---|
| 601 | for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next) | 
|---|
| 602 | { | 
|---|
| 603 | int compare = strcmp (domainname, binding->domainname); | 
|---|
| 604 | if (compare == 0) | 
|---|
| 605 | /* We found it!  */ | 
|---|
| 606 | break; | 
|---|
| 607 | if (compare < 0) | 
|---|
| 608 | { | 
|---|
| 609 | /* It is not in the list.  */ | 
|---|
| 610 | binding = NULL; | 
|---|
| 611 | break; | 
|---|
| 612 | } | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 | if (binding == NULL) | 
|---|
| 616 | dirname = _nl_default_dirname; | 
|---|
| 617 | else | 
|---|
| 618 | { | 
|---|
| 619 | dirname = binding->dirname; | 
|---|
| 620 | #endif | 
|---|
| 621 | if (!IS_ABSOLUTE_PATH (dirname)) | 
|---|
| 622 | { | 
|---|
| 623 | /* We have a relative path.  Make it absolute now.  */ | 
|---|
| 624 | char *cwd = getcwd (NULL, 0); | 
|---|
| 625 | if (cwd == NULL) | 
|---|
| 626 | /* We cannot get the current working directory.  Don't | 
|---|
| 627 | signal an error but simply return the default | 
|---|
| 628 | string.  */ | 
|---|
| 629 | goto return_untranslated; | 
|---|
| 630 | int ret = __asprintf (&xdirname, "%s/%s", cwd, dirname); | 
|---|
| 631 | free (cwd); | 
|---|
| 632 | if (ret < 0) | 
|---|
| 633 | goto return_untranslated; | 
|---|
| 634 | dirname = xdirname; | 
|---|
| 635 | } | 
|---|
| 636 | #ifndef IN_LIBGLOCALE | 
|---|
| 637 | } | 
|---|
| 638 | #endif | 
|---|
| 639 |  | 
|---|
| 640 | /* Now determine the symbolic name of CATEGORY and its value.  */ | 
|---|
| 641 | #ifndef CATEGORYNAME_INITIALIZED | 
|---|
| 642 | categoryname = category_to_name (category); | 
|---|
| 643 | #endif | 
|---|
| 644 | #ifdef IN_LIBGLOCALE | 
|---|
| 645 | categoryvalue = guess_category_value (category, categoryname, localename); | 
|---|
| 646 | #else | 
|---|
| 647 | categoryvalue = guess_category_value (category, categoryname); | 
|---|
| 648 | #endif | 
|---|
| 649 |  | 
|---|
| 650 | domainname_len = strlen (domainname); | 
|---|
| 651 | xdomainname = (char *) alloca (strlen (categoryname) | 
|---|
| 652 | + domainname_len + 5); | 
|---|
| 653 | ADD_BLOCK (block_list, xdomainname); | 
|---|
| 654 |  | 
|---|
| 655 | stpcpy ((char *) mempcpy (stpcpy (stpcpy (xdomainname, categoryname), "/"), | 
|---|
| 656 | domainname, domainname_len), | 
|---|
| 657 | ".mo"); | 
|---|
| 658 |  | 
|---|
| 659 | /* Creating working area.  */ | 
|---|
| 660 | single_locale = (char *) alloca (strlen (categoryvalue) + 1); | 
|---|
| 661 | ADD_BLOCK (block_list, single_locale); | 
|---|
| 662 |  | 
|---|
| 663 |  | 
|---|
| 664 | /* Search for the given string.  This is a loop because we perhaps | 
|---|
| 665 | got an ordered list of languages to consider for the translation.  */ | 
|---|
| 666 | while (1) | 
|---|
| 667 | { | 
|---|
| 668 | /* Make CATEGORYVALUE point to the next element of the list.  */ | 
|---|
| 669 | while (categoryvalue[0] != '\0' && categoryvalue[0] == ':') | 
|---|
| 670 | ++categoryvalue; | 
|---|
| 671 | if (categoryvalue[0] == '\0') | 
|---|
| 672 | { | 
|---|
| 673 | /* The whole contents of CATEGORYVALUE has been searched but | 
|---|
| 674 | no valid entry has been found.  We solve this situation | 
|---|
| 675 | by implicitly appending a "C" entry, i.e. no translation | 
|---|
| 676 | will take place.  */ | 
|---|
| 677 | single_locale[0] = 'C'; | 
|---|
| 678 | single_locale[1] = '\0'; | 
|---|
| 679 | } | 
|---|
| 680 | else | 
|---|
| 681 | { | 
|---|
| 682 | char *cp = single_locale; | 
|---|
| 683 | while (categoryvalue[0] != '\0' && categoryvalue[0] != ':') | 
|---|
| 684 | *cp++ = *categoryvalue++; | 
|---|
| 685 | *cp = '\0'; | 
|---|
| 686 |  | 
|---|
| 687 | /* When this is a SUID binary we must not allow accessing files | 
|---|
| 688 | outside the dedicated directories.  */ | 
|---|
| 689 | if (ENABLE_SECURE && IS_PATH_WITH_DIR (single_locale)) | 
|---|
| 690 | /* Ingore this entry.  */ | 
|---|
| 691 | continue; | 
|---|
| 692 | } | 
|---|
| 693 |  | 
|---|
| 694 | /* If the current locale value is C (or POSIX) we don't load a | 
|---|
| 695 | domain.  Return the MSGID.  */ | 
|---|
| 696 | if (strcmp (single_locale, "C") == 0 | 
|---|
| 697 | || strcmp (single_locale, "POSIX") == 0) | 
|---|
| 698 | break; | 
|---|
| 699 |  | 
|---|
| 700 | /* Find structure describing the message catalog matching the | 
|---|
| 701 | DOMAINNAME and CATEGORY.  */ | 
|---|
| 702 | domain = _nl_find_domain (dirname, single_locale, xdomainname, binding); | 
|---|
| 703 |  | 
|---|
| 704 | if (domain != NULL) | 
|---|
| 705 | { | 
|---|
| 706 | #if defined IN_LIBGLOCALE | 
|---|
| 707 | retval = _nl_find_msg (domain, binding, encoding, msgid1, &retlen); | 
|---|
| 708 | #else | 
|---|
| 709 | retval = _nl_find_msg (domain, binding, msgid1, 1, &retlen); | 
|---|
| 710 | #endif | 
|---|
| 711 |  | 
|---|
| 712 | if (retval == NULL) | 
|---|
| 713 | { | 
|---|
| 714 | int cnt; | 
|---|
| 715 |  | 
|---|
| 716 | for (cnt = 0; domain->successor[cnt] != NULL; ++cnt) | 
|---|
| 717 | { | 
|---|
| 718 | #if defined IN_LIBGLOCALE | 
|---|
| 719 | retval = _nl_find_msg (domain->successor[cnt], binding, | 
|---|
| 720 | encoding, msgid1, &retlen); | 
|---|
| 721 | #else | 
|---|
| 722 | retval = _nl_find_msg (domain->successor[cnt], binding, | 
|---|
| 723 | msgid1, 1, &retlen); | 
|---|
| 724 | #endif | 
|---|
| 725 |  | 
|---|
| 726 | /* Resource problems are not fatal, instead we return no | 
|---|
| 727 | translation.  */ | 
|---|
| 728 | if (__builtin_expect (retval == (char *) -1, 0)) | 
|---|
| 729 | goto return_untranslated; | 
|---|
| 730 |  | 
|---|
| 731 | if (retval != NULL) | 
|---|
| 732 | { | 
|---|
| 733 | domain = domain->successor[cnt]; | 
|---|
| 734 | break; | 
|---|
| 735 | } | 
|---|
| 736 | } | 
|---|
| 737 | } | 
|---|
| 738 |  | 
|---|
| 739 | /* Returning -1 means that some resource problem exists | 
|---|
| 740 | (likely memory) and that the strings could not be | 
|---|
| 741 | converted.  Return the original strings.  */ | 
|---|
| 742 | if (__builtin_expect (retval == (char *) -1, 0)) | 
|---|
| 743 | break; | 
|---|
| 744 |  | 
|---|
| 745 | if (retval != NULL) | 
|---|
| 746 | { | 
|---|
| 747 | /* Found the translation of MSGID1 in domain DOMAIN: | 
|---|
| 748 | starting at RETVAL, RETLEN bytes.  */ | 
|---|
| 749 | free (xdirname); | 
|---|
| 750 | FREE_BLOCKS (block_list); | 
|---|
| 751 | if (foundp == NULL) | 
|---|
| 752 | { | 
|---|
| 753 | /* Create a new entry and add it to the search tree.  */ | 
|---|
| 754 | size_t msgid_len; | 
|---|
| 755 | size_t size; | 
|---|
| 756 | struct known_translation_t *newp; | 
|---|
| 757 |  | 
|---|
| 758 | msgid_len = strlen (msgid1) + 1; | 
|---|
| 759 | size = offsetof (struct known_translation_t, msgid) | 
|---|
| 760 | + msgid_len + domainname_len + 1; | 
|---|
| 761 | #ifdef HAVE_PER_THREAD_LOCALE | 
|---|
| 762 | size += strlen (localename) + 1; | 
|---|
| 763 | #endif | 
|---|
| 764 | newp = (struct known_translation_t *) malloc (size); | 
|---|
| 765 | if (newp != NULL) | 
|---|
| 766 | { | 
|---|
| 767 | char *new_domainname; | 
|---|
| 768 | #ifdef HAVE_PER_THREAD_LOCALE | 
|---|
| 769 | char *new_localename; | 
|---|
| 770 | #endif | 
|---|
| 771 |  | 
|---|
| 772 | new_domainname = | 
|---|
| 773 | (char *) mempcpy (newp->msgid.appended, msgid1, | 
|---|
| 774 | msgid_len); | 
|---|
| 775 | memcpy (new_domainname, domainname, domainname_len + 1); | 
|---|
| 776 | #ifdef HAVE_PER_THREAD_LOCALE | 
|---|
| 777 | new_localename = new_domainname + domainname_len + 1; | 
|---|
| 778 | strcpy (new_localename, localename); | 
|---|
| 779 | #endif | 
|---|
| 780 | newp->domainname = new_domainname; | 
|---|
| 781 | newp->category = category; | 
|---|
| 782 | #ifdef HAVE_PER_THREAD_LOCALE | 
|---|
| 783 | newp->localename = new_localename; | 
|---|
| 784 | #endif | 
|---|
| 785 | #ifdef IN_LIBGLOCALE | 
|---|
| 786 | newp->encoding = encoding; | 
|---|
| 787 | #endif | 
|---|
| 788 | newp->counter = _nl_msg_cat_cntr; | 
|---|
| 789 | newp->domain = domain; | 
|---|
| 790 | newp->translation = retval; | 
|---|
| 791 | newp->translation_length = retlen; | 
|---|
| 792 |  | 
|---|
| 793 | gl_rwlock_wrlock (tree_lock); | 
|---|
| 794 |  | 
|---|
| 795 | /* Insert the entry in the search tree.  */ | 
|---|
| 796 | foundp = (struct known_translation_t **) | 
|---|
| 797 | tsearch (newp, &root, transcmp); | 
|---|
| 798 |  | 
|---|
| 799 | gl_rwlock_unlock (tree_lock); | 
|---|
| 800 |  | 
|---|
| 801 | if (foundp == NULL | 
|---|
| 802 | || __builtin_expect (*foundp != newp, 0)) | 
|---|
| 803 | /* The insert failed.  */ | 
|---|
| 804 | free (newp); | 
|---|
| 805 | } | 
|---|
| 806 | } | 
|---|
| 807 | else | 
|---|
| 808 | { | 
|---|
| 809 | /* We can update the existing entry.  */ | 
|---|
| 810 | (*foundp)->counter = _nl_msg_cat_cntr; | 
|---|
| 811 | (*foundp)->domain = domain; | 
|---|
| 812 | (*foundp)->translation = retval; | 
|---|
| 813 | (*foundp)->translation_length = retlen; | 
|---|
| 814 | } | 
|---|
| 815 |  | 
|---|
| 816 | __set_errno (saved_errno); | 
|---|
| 817 |  | 
|---|
| 818 | /* Now deal with plural.  */ | 
|---|
| 819 | if (plural) | 
|---|
| 820 | retval = plural_lookup (domain, n, retval, retlen); | 
|---|
| 821 |  | 
|---|
| 822 | gl_rwlock_unlock (_nl_state_lock); | 
|---|
| 823 | #ifdef _LIBC | 
|---|
| 824 | __libc_rwlock_unlock (__libc_setlocale_lock); | 
|---|
| 825 | #endif | 
|---|
| 826 | return retval; | 
|---|
| 827 | } | 
|---|
| 828 | } | 
|---|
| 829 | } | 
|---|
| 830 |  | 
|---|
| 831 | return_untranslated: | 
|---|
| 832 | /* Return the untranslated MSGID.  */ | 
|---|
| 833 | free (xdirname); | 
|---|
| 834 | FREE_BLOCKS (block_list); | 
|---|
| 835 | gl_rwlock_unlock (_nl_state_lock); | 
|---|
| 836 | #ifdef _LIBC | 
|---|
| 837 | __libc_rwlock_unlock (__libc_setlocale_lock); | 
|---|
| 838 | #endif | 
|---|
| 839 | #ifndef _LIBC | 
|---|
| 840 | if (!ENABLE_SECURE) | 
|---|
| 841 | { | 
|---|
| 842 | extern void _nl_log_untranslated (const char *logfilename, | 
|---|
| 843 | const char *domainname, | 
|---|
| 844 | const char *msgid1, const char *msgid2, | 
|---|
| 845 | int plural); | 
|---|
| 846 | const char *logfilename = getenv ( "GETTEXT_LOG_UNTRANSLATED"); | 
|---|
| 847 |  | 
|---|
| 848 | if (logfilename != NULL && logfilename[0] != '\0') | 
|---|
| 849 | _nl_log_untranslated (logfilename, domainname, msgid1, msgid2, plural); | 
|---|
| 850 | } | 
|---|
| 851 | #endif | 
|---|
| 852 | __set_errno (saved_errno); | 
|---|
| 853 | return (plural == 0 | 
|---|
| 854 | ? (char *) msgid1 | 
|---|
| 855 | /* Use the Germanic plural rule.  */ | 
|---|
| 856 | : n == 1 ? (char *) msgid1 : (char *) msgid2); | 
|---|
| 857 | } | 
|---|
| 858 |  | 
|---|
| 859 |  | 
|---|
| 860 | /* Look up the translation of msgid within DOMAIN_FILE and DOMAINBINDING. | 
|---|
| 861 | Return it if found.  Return NULL if not found or in case of a conversion | 
|---|
| 862 | failure (problem in the particular message catalog).  Return (char *) -1 | 
|---|
| 863 | in case of a memory allocation failure during conversion (only if | 
|---|
| 864 | ENCODING != NULL resp. CONVERT == true).  */ | 
|---|
| 865 | char * | 
|---|
| 866 | #ifdef IN_LIBGLOCALE | 
|---|
| 867 | _nl_find_msg (struct loaded_l10nfile *domain_file, | 
|---|
| 868 | struct binding *domainbinding, const char *encoding, | 
|---|
| 869 | const char *msgid, | 
|---|
| 870 | size_t *lengthp) | 
|---|
| 871 | #else | 
|---|
| 872 | _nl_find_msg (struct loaded_l10nfile *domain_file, | 
|---|
| 873 | struct binding *domainbinding, | 
|---|
| 874 | const char *msgid, int convert, | 
|---|
| 875 | size_t *lengthp) | 
|---|
| 876 | #endif | 
|---|
| 877 | { | 
|---|
| 878 | struct loaded_domain *domain; | 
|---|
| 879 | nls_uint32 nstrings; | 
|---|
| 880 | size_t act; | 
|---|
| 881 | char *result; | 
|---|
| 882 | size_t resultlen; | 
|---|
| 883 |  | 
|---|
| 884 | if (domain_file->decided <= 0) | 
|---|
| 885 | _nl_load_domain (domain_file, domainbinding); | 
|---|
| 886 |  | 
|---|
| 887 | if (domain_file->data == NULL) | 
|---|
| 888 | return NULL; | 
|---|
| 889 |  | 
|---|
| 890 | domain = (struct loaded_domain *) domain_file->data; | 
|---|
| 891 |  | 
|---|
| 892 | nstrings = domain->nstrings; | 
|---|
| 893 |  | 
|---|
| 894 | /* Locate the MSGID and its translation.  */ | 
|---|
| 895 | if (domain->hash_tab != NULL) | 
|---|
| 896 | { | 
|---|
| 897 | /* Use the hashing table.  */ | 
|---|
| 898 | nls_uint32 len = strlen (msgid); | 
|---|
| 899 | nls_uint32 hash_val = __hash_string (msgid); | 
|---|
| 900 | nls_uint32 idx = hash_val % domain->hash_size; | 
|---|
| 901 | nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2)); | 
|---|
| 902 |  | 
|---|
| 903 | while (1) | 
|---|
| 904 | { | 
|---|
| 905 | nls_uint32 nstr = | 
|---|
| 906 | W (domain->must_swap_hash_tab, domain->hash_tab[idx]); | 
|---|
| 907 |  | 
|---|
| 908 | if (nstr == 0) | 
|---|
| 909 | /* Hash table entry is empty.  */ | 
|---|
| 910 | return NULL; | 
|---|
| 911 |  | 
|---|
| 912 | nstr--; | 
|---|
| 913 |  | 
|---|
| 914 | /* Compare msgid with the original string at index nstr. | 
|---|
| 915 | We compare the lengths with >=, not ==, because plural entries | 
|---|
| 916 | are represented by strings with an embedded NUL.  */ | 
|---|
| 917 | if (nstr < nstrings | 
|---|
| 918 | ? W (domain->must_swap, domain->orig_tab[nstr].length) >= len | 
|---|
| 919 | && (strcmp (msgid, | 
|---|
| 920 | domain->data + W (domain->must_swap, | 
|---|
| 921 | domain->orig_tab[nstr].offset)) | 
|---|
| 922 | == 0) | 
|---|
| 923 | : domain->orig_sysdep_tab[nstr - nstrings].length > len | 
|---|
| 924 | && (strcmp (msgid, | 
|---|
| 925 | domain->orig_sysdep_tab[nstr - nstrings].pointer) | 
|---|
| 926 | == 0)) | 
|---|
| 927 | { | 
|---|
| 928 | act = nstr; | 
|---|
| 929 | goto found; | 
|---|
| 930 | } | 
|---|
| 931 |  | 
|---|
| 932 | if (idx >= domain->hash_size - incr) | 
|---|
| 933 | idx -= domain->hash_size - incr; | 
|---|
| 934 | else | 
|---|
| 935 | idx += incr; | 
|---|
| 936 | } | 
|---|
| 937 | /* NOTREACHED */ | 
|---|
| 938 | } | 
|---|
| 939 | else | 
|---|
| 940 | { | 
|---|
| 941 | /* Try the default method:  binary search in the sorted array of | 
|---|
| 942 | messages.  */ | 
|---|
| 943 | size_t top, bottom; | 
|---|
| 944 |  | 
|---|
| 945 | bottom = 0; | 
|---|
| 946 | top = nstrings; | 
|---|
| 947 | while (bottom < top) | 
|---|
| 948 | { | 
|---|
| 949 | int cmp_val; | 
|---|
| 950 |  | 
|---|
| 951 | act = (bottom + top) / 2; | 
|---|
| 952 | cmp_val = strcmp (msgid, (domain->data | 
|---|
| 953 | + W (domain->must_swap, | 
|---|
| 954 | domain->orig_tab[act].offset))); | 
|---|
| 955 | if (cmp_val < 0) | 
|---|
| 956 | top = act; | 
|---|
| 957 | else if (cmp_val > 0) | 
|---|
| 958 | bottom = act + 1; | 
|---|
| 959 | else | 
|---|
| 960 | goto found; | 
|---|
| 961 | } | 
|---|
| 962 | /* No translation was found.  */ | 
|---|
| 963 | return NULL; | 
|---|
| 964 | } | 
|---|
| 965 |  | 
|---|
| 966 | found: | 
|---|
| 967 | /* The translation was found at index ACT.  If we have to convert the | 
|---|
| 968 | string to use a different character set, this is the time.  */ | 
|---|
| 969 | if (act < nstrings) | 
|---|
| 970 | { | 
|---|
| 971 | result = (char *) | 
|---|
| 972 | (domain->data + W (domain->must_swap, domain->trans_tab[act].offset)); | 
|---|
| 973 | resultlen = W (domain->must_swap, domain->trans_tab[act].length) + 1; | 
|---|
| 974 | } | 
|---|
| 975 | else | 
|---|
| 976 | { | 
|---|
| 977 | result = (char *) domain->trans_sysdep_tab[act - nstrings].pointer; | 
|---|
| 978 | resultlen = domain->trans_sysdep_tab[act - nstrings].length; | 
|---|
| 979 | } | 
|---|
| 980 |  | 
|---|
| 981 | #if defined _LIBC || HAVE_ICONV | 
|---|
| 982 | # ifdef IN_LIBGLOCALE | 
|---|
| 983 | if (encoding != NULL) | 
|---|
| 984 | # else | 
|---|
| 985 | if (convert) | 
|---|
| 986 | # endif | 
|---|
| 987 | { | 
|---|
| 988 | /* We are supposed to do a conversion.  */ | 
|---|
| 989 | # ifndef IN_LIBGLOCALE | 
|---|
| 990 | const char *encoding = get_output_charset (domainbinding); | 
|---|
| 991 | # endif | 
|---|
| 992 | size_t nconversions; | 
|---|
| 993 | struct converted_domain *convd; | 
|---|
| 994 | size_t i; | 
|---|
| 995 |  | 
|---|
| 996 | /* Protect against reallocation of the table.  */ | 
|---|
| 997 | gl_rwlock_rdlock (domain->conversions_lock); | 
|---|
| 998 |  | 
|---|
| 999 | /* Search whether a table with converted translations for this | 
|---|
| 1000 | encoding has already been allocated.  */ | 
|---|
| 1001 | nconversions = domain->nconversions; | 
|---|
| 1002 | convd = NULL; | 
|---|
| 1003 |  | 
|---|
| 1004 | for (i = nconversions; i > 0; ) | 
|---|
| 1005 | { | 
|---|
| 1006 | i--; | 
|---|
| 1007 | if (strcmp (domain->conversions[i].encoding, encoding) == 0) | 
|---|
| 1008 | { | 
|---|
| 1009 | convd = &domain->conversions[i]; | 
|---|
| 1010 | break; | 
|---|
| 1011 | } | 
|---|
| 1012 | } | 
|---|
| 1013 |  | 
|---|
| 1014 | gl_rwlock_unlock (domain->conversions_lock); | 
|---|
| 1015 |  | 
|---|
| 1016 | if (convd == NULL) | 
|---|
| 1017 | { | 
|---|
| 1018 | /* We have to allocate a new conversions table.  */ | 
|---|
| 1019 | gl_rwlock_wrlock (domain->conversions_lock); | 
|---|
| 1020 | nconversions = domain->nconversions; | 
|---|
| 1021 |  | 
|---|
| 1022 | /* Maybe in the meantime somebody added the translation. | 
|---|
| 1023 | Recheck.  */ | 
|---|
| 1024 | for (i = nconversions; i > 0; ) | 
|---|
| 1025 | { | 
|---|
| 1026 | i--; | 
|---|
| 1027 | if (strcmp (domain->conversions[i].encoding, encoding) == 0) | 
|---|
| 1028 | { | 
|---|
| 1029 | convd = &domain->conversions[i]; | 
|---|
| 1030 | goto found_convd; | 
|---|
| 1031 | } | 
|---|
| 1032 | } | 
|---|
| 1033 |  | 
|---|
| 1034 | { | 
|---|
| 1035 | /* Allocate a table for the converted translations for this | 
|---|
| 1036 | encoding.  */ | 
|---|
| 1037 | struct converted_domain *new_conversions = | 
|---|
| 1038 | (struct converted_domain *) | 
|---|
| 1039 | (domain->conversions != NULL | 
|---|
| 1040 | ? realloc (domain->conversions, | 
|---|
| 1041 | (nconversions + 1) * sizeof (struct converted_domain)) | 
|---|
| 1042 | : malloc ((nconversions + 1) * sizeof (struct converted_domain))); | 
|---|
| 1043 |  | 
|---|
| 1044 | if (__builtin_expect (new_conversions == NULL, 0)) | 
|---|
| 1045 | { | 
|---|
| 1046 | /* Nothing we can do, no more memory.  We cannot use the | 
|---|
| 1047 | translation because it might be encoded incorrectly.  */ | 
|---|
| 1048 | unlock_fail: | 
|---|
| 1049 | gl_rwlock_unlock (domain->conversions_lock); | 
|---|
| 1050 | return (char *) -1; | 
|---|
| 1051 | } | 
|---|
| 1052 |  | 
|---|
| 1053 | domain->conversions = new_conversions; | 
|---|
| 1054 |  | 
|---|
| 1055 | /* Copy the 'encoding' string to permanent storage.  */ | 
|---|
| 1056 | encoding = strdup (encoding); | 
|---|
| 1057 | if (__builtin_expect (encoding == NULL, 0)) | 
|---|
| 1058 | /* Nothing we can do, no more memory.  We cannot use the | 
|---|
| 1059 | translation because it might be encoded incorrectly.  */ | 
|---|
| 1060 | goto unlock_fail; | 
|---|
| 1061 |  | 
|---|
| 1062 | convd = &new_conversions[nconversions]; | 
|---|
| 1063 | convd->encoding = encoding; | 
|---|
| 1064 |  | 
|---|
| 1065 | /* Find out about the character set the file is encoded with. | 
|---|
| 1066 | This can be found (in textual form) in the entry "".  If this | 
|---|
| 1067 | entry does not exist or if this does not contain the 'charset=' | 
|---|
| 1068 | information, we will assume the charset matches the one the | 
|---|
| 1069 | current locale and we don't have to perform any conversion.  */ | 
|---|
| 1070 | # ifdef _LIBC | 
|---|
| 1071 | convd->conv = (__gconv_t) -1; | 
|---|
| 1072 | # else | 
|---|
| 1073 | #  if HAVE_ICONV | 
|---|
| 1074 | convd->conv = (iconv_t) -1; | 
|---|
| 1075 | #  endif | 
|---|
| 1076 | # endif | 
|---|
| 1077 | { | 
|---|
| 1078 | char *nullentry; | 
|---|
| 1079 | size_t nullentrylen; | 
|---|
| 1080 |  | 
|---|
| 1081 | /* Get the header entry.  This is a recursion, but it doesn't | 
|---|
| 1082 | reallocate domain->conversions because we pass | 
|---|
| 1083 | encoding = NULL or convert = 0, respectively.  */ | 
|---|
| 1084 | nullentry = | 
|---|
| 1085 | # ifdef IN_LIBGLOCALE | 
|---|
| 1086 | _nl_find_msg (domain_file, domainbinding, NULL, "", | 
|---|
| 1087 | &nullentrylen); | 
|---|
| 1088 | # else | 
|---|
| 1089 | _nl_find_msg (domain_file, domainbinding, "", 0, &nullentrylen); | 
|---|
| 1090 | # endif | 
|---|
| 1091 |  | 
|---|
| 1092 | /* Resource problems are fatal.  If we continue onwards we will | 
|---|
| 1093 | only attempt to calloc a new conv_tab and fail later.  */ | 
|---|
| 1094 | if (__builtin_expect (nullentry == (char *) -1, 0)) | 
|---|
| 1095 | return (char *) -1; | 
|---|
| 1096 |  | 
|---|
| 1097 | if (nullentry != NULL) | 
|---|
| 1098 | { | 
|---|
| 1099 | const char *charsetstr; | 
|---|
| 1100 |  | 
|---|
| 1101 | charsetstr = strstr (nullentry, "charset="); | 
|---|
| 1102 | if (charsetstr != NULL) | 
|---|
| 1103 | { | 
|---|
| 1104 | size_t len; | 
|---|
| 1105 | char *charset; | 
|---|
| 1106 | const char *outcharset; | 
|---|
| 1107 |  | 
|---|
| 1108 | charsetstr += strlen ( "charset="); | 
|---|
| 1109 | len = strcspn (charsetstr, " \t\n"); | 
|---|
| 1110 |  | 
|---|
| 1111 | charset = (char *) alloca (len + 1); | 
|---|
| 1112 | # if defined _LIBC || HAVE_MEMPCPY | 
|---|
| 1113 | *((char *) mempcpy (charset, charsetstr, len)) = '\0'; | 
|---|
| 1114 | # else | 
|---|
| 1115 | memcpy (charset, charsetstr, len); | 
|---|
| 1116 | charset[len] = '\0'; | 
|---|
| 1117 | # endif | 
|---|
| 1118 |  | 
|---|
| 1119 | outcharset = encoding; | 
|---|
| 1120 |  | 
|---|
| 1121 | # ifdef _LIBC | 
|---|
| 1122 |  | 
|---|
| 1123 | struct gconv_spec conv_spec | 
|---|
| 1124 | = { .fromcode = norm_add_slashes (charset, ""), | 
|---|
| 1125 | .tocode = norm_add_slashes (outcharset, ""), | 
|---|
| 1126 | /* We always want to use transliteration.  */ | 
|---|
| 1127 | .translit = true, | 
|---|
| 1128 | .ignore = false | 
|---|
| 1129 | }; | 
|---|
| 1130 | int r = __gconv_open (&conv_spec, &convd->conv, | 
|---|
| 1131 | GCONV_AVOID_NOCONV); | 
|---|
| 1132 | if (__builtin_expect (r != __GCONV_OK, 0)) | 
|---|
| 1133 | { | 
|---|
| 1134 | /* If the output encoding is the same there is | 
|---|
| 1135 | nothing to do.  Otherwise do not use the | 
|---|
| 1136 | translation at all.  */ | 
|---|
| 1137 | if (__builtin_expect (r != __GCONV_NULCONV, 1)) | 
|---|
| 1138 | { | 
|---|
| 1139 | gl_rwlock_unlock (domain->conversions_lock); | 
|---|
| 1140 | free ((char *) encoding); | 
|---|
| 1141 | return NULL; | 
|---|
| 1142 | } | 
|---|
| 1143 |  | 
|---|
| 1144 | convd->conv = (__gconv_t) -1; | 
|---|
| 1145 | } | 
|---|
| 1146 | # else | 
|---|
| 1147 | #  if HAVE_ICONV | 
|---|
| 1148 | /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5, | 
|---|
| 1149 | we want to use transliteration.  */ | 
|---|
| 1150 | #   if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) \ | 
|---|
| 1151 | && !defined __UCLIBC__) \ | 
|---|
| 1152 | || _LIBICONV_VERSION >= 0x0105 | 
|---|
| 1153 | if (strchr (outcharset, '/') == NULL) | 
|---|
| 1154 | { | 
|---|
| 1155 | char *tmp; | 
|---|
| 1156 |  | 
|---|
| 1157 | len = strlen (outcharset); | 
|---|
| 1158 | tmp = (char *) alloca (len + 10 + 1); | 
|---|
| 1159 | memcpy (tmp, outcharset, len); | 
|---|
| 1160 | memcpy (tmp + len, "//TRANSLIT", 10 + 1); | 
|---|
| 1161 | outcharset = tmp; | 
|---|
| 1162 |  | 
|---|
| 1163 | convd->conv = iconv_open (outcharset, charset); | 
|---|
| 1164 |  | 
|---|
| 1165 | freea (outcharset); | 
|---|
| 1166 | } | 
|---|
| 1167 | else | 
|---|
| 1168 | #   endif | 
|---|
| 1169 | convd->conv = iconv_open (outcharset, charset); | 
|---|
| 1170 | #  endif | 
|---|
| 1171 | # endif | 
|---|
| 1172 |  | 
|---|
| 1173 | freea (charset); | 
|---|
| 1174 | } | 
|---|
| 1175 | } | 
|---|
| 1176 | } | 
|---|
| 1177 | convd->conv_tab = NULL; | 
|---|
| 1178 | /* Here domain->conversions is still == new_conversions.  */ | 
|---|
| 1179 | domain->nconversions++; | 
|---|
| 1180 | } | 
|---|
| 1181 |  | 
|---|
| 1182 | found_convd: | 
|---|
| 1183 | gl_rwlock_unlock (domain->conversions_lock); | 
|---|
| 1184 | } | 
|---|
| 1185 |  | 
|---|
| 1186 | if ( | 
|---|
| 1187 | # ifdef _LIBC | 
|---|
| 1188 | convd->conv != (__gconv_t) -1 | 
|---|
| 1189 | # else | 
|---|
| 1190 | #  if HAVE_ICONV | 
|---|
| 1191 | convd->conv != (iconv_t) -1 | 
|---|
| 1192 | #  endif | 
|---|
| 1193 | # endif | 
|---|
| 1194 | ) | 
|---|
| 1195 | { | 
|---|
| 1196 | /* We are supposed to do a conversion.  First allocate an | 
|---|
| 1197 | appropriate table with the same structure as the table | 
|---|
| 1198 | of translations in the file, where we can put the pointers | 
|---|
| 1199 | to the converted strings in. | 
|---|
| 1200 | There is a slight complication with plural entries.  They | 
|---|
| 1201 | are represented by consecutive NUL terminated strings.  We | 
|---|
| 1202 | handle this case by converting RESULTLEN bytes, including | 
|---|
| 1203 | NULs.  */ | 
|---|
| 1204 |  | 
|---|
| 1205 | /* This lock primarily protects the memory management variables | 
|---|
| 1206 | freemem, freemem_size.  It also protects write accesses to | 
|---|
| 1207 | convd->conv_tab.  It's not worth using a separate lock (such | 
|---|
| 1208 | as domain->conversions_lock) for this purpose, because when | 
|---|
| 1209 | modifying convd->conv_tab, we also need to lock freemem, | 
|---|
| 1210 | freemem_size for most of the time.  */ | 
|---|
| 1211 | __libc_lock_define_initialized (static, lock) | 
|---|
| 1212 |  | 
|---|
| 1213 | if (__builtin_expect (convd->conv_tab == NULL, 0)) | 
|---|
| 1214 | { | 
|---|
| 1215 | __libc_lock_lock (lock); | 
|---|
| 1216 | if (convd->conv_tab == NULL) | 
|---|
| 1217 | { | 
|---|
| 1218 | convd->conv_tab = | 
|---|
| 1219 | (char **) calloc (nstrings + domain->n_sysdep_strings, | 
|---|
| 1220 | sizeof (char *)); | 
|---|
| 1221 | if (convd->conv_tab != NULL) | 
|---|
| 1222 | goto not_translated_yet; | 
|---|
| 1223 | /* Mark that we didn't succeed allocating a table.  */ | 
|---|
| 1224 | convd->conv_tab = (char **) -1; | 
|---|
| 1225 | } | 
|---|
| 1226 | __libc_lock_unlock (lock); | 
|---|
| 1227 | } | 
|---|
| 1228 |  | 
|---|
| 1229 | if (__builtin_expect (convd->conv_tab == (char **) -1, 0)) | 
|---|
| 1230 | /* Nothing we can do, no more memory.  We cannot use the | 
|---|
| 1231 | translation because it might be encoded incorrectly.  */ | 
|---|
| 1232 | return (char *) -1; | 
|---|
| 1233 |  | 
|---|
| 1234 | if (convd->conv_tab[act] == NULL) | 
|---|
| 1235 | { | 
|---|
| 1236 | /* We haven't used this string so far, so it is not | 
|---|
| 1237 | translated yet.  Do this now.  */ | 
|---|
| 1238 | /* We use a bit more efficient memory handling. | 
|---|
| 1239 | We allocate always larger blocks which get used over | 
|---|
| 1240 | time.  This is faster than many small allocations.   */ | 
|---|
| 1241 | # define INITIAL_BLOCK_SIZE	4080 | 
|---|
| 1242 | static unsigned char *freemem; | 
|---|
| 1243 | static size_t freemem_size; | 
|---|
| 1244 |  | 
|---|
| 1245 | const unsigned char *inbuf; | 
|---|
| 1246 | unsigned char *outbuf; | 
|---|
| 1247 | int malloc_count; | 
|---|
| 1248 | # ifndef _LIBC | 
|---|
| 1249 | transmem_block_t *transmem_list; | 
|---|
| 1250 | # endif | 
|---|
| 1251 |  | 
|---|
| 1252 | __libc_lock_lock (lock); | 
|---|
| 1253 | not_translated_yet: | 
|---|
| 1254 |  | 
|---|
| 1255 | inbuf = (const unsigned char *) result; | 
|---|
| 1256 | outbuf = freemem + sizeof (size_t); | 
|---|
| 1257 | # ifndef _LIBC | 
|---|
| 1258 | transmem_list = NULL; | 
|---|
| 1259 | # endif | 
|---|
| 1260 |  | 
|---|
| 1261 | malloc_count = 0; | 
|---|
| 1262 | while (1) | 
|---|
| 1263 | { | 
|---|
| 1264 | transmem_block_t *newmem; | 
|---|
| 1265 | # ifdef _LIBC | 
|---|
| 1266 | size_t non_reversible; | 
|---|
| 1267 | int res; | 
|---|
| 1268 |  | 
|---|
| 1269 | if (freemem_size < sizeof (size_t)) | 
|---|
| 1270 | goto resize_freemem; | 
|---|
| 1271 |  | 
|---|
| 1272 | res = __gconv (convd->conv, | 
|---|
| 1273 | &inbuf, inbuf + resultlen, | 
|---|
| 1274 | &outbuf, | 
|---|
| 1275 | outbuf + freemem_size - sizeof (size_t), | 
|---|
| 1276 | &non_reversible); | 
|---|
| 1277 |  | 
|---|
| 1278 | if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT) | 
|---|
| 1279 | break; | 
|---|
| 1280 |  | 
|---|
| 1281 | if (res != __GCONV_FULL_OUTPUT) | 
|---|
| 1282 | { | 
|---|
| 1283 | /* We should not use the translation at all, it | 
|---|
| 1284 | is incorrectly encoded.  */ | 
|---|
| 1285 | __libc_lock_unlock (lock); | 
|---|
| 1286 | return NULL; | 
|---|
| 1287 | } | 
|---|
| 1288 |  | 
|---|
| 1289 | inbuf = (const unsigned char *) result; | 
|---|
| 1290 | # else | 
|---|
| 1291 | #  if HAVE_ICONV | 
|---|
| 1292 | const char *inptr = (const char *) inbuf; | 
|---|
| 1293 | size_t inleft = resultlen; | 
|---|
| 1294 | char *outptr = (char *) outbuf; | 
|---|
| 1295 | size_t outleft; | 
|---|
| 1296 |  | 
|---|
| 1297 | if (freemem_size < sizeof (size_t)) | 
|---|
| 1298 | goto resize_freemem; | 
|---|
| 1299 |  | 
|---|
| 1300 | outleft = freemem_size - sizeof (size_t); | 
|---|
| 1301 | if (iconv (convd->conv, | 
|---|
| 1302 | (ICONV_CONST char **) &inptr, &inleft, | 
|---|
| 1303 | &outptr, &outleft) | 
|---|
| 1304 | != (size_t) (-1)) | 
|---|
| 1305 | { | 
|---|
| 1306 | outbuf = (unsigned char *) outptr; | 
|---|
| 1307 | break; | 
|---|
| 1308 | } | 
|---|
| 1309 | if (errno != E2BIG) | 
|---|
| 1310 | { | 
|---|
| 1311 | __libc_lock_unlock (lock); | 
|---|
| 1312 | return NULL; | 
|---|
| 1313 | } | 
|---|
| 1314 | #  endif | 
|---|
| 1315 | # endif | 
|---|
| 1316 |  | 
|---|
| 1317 | resize_freemem: | 
|---|
| 1318 | /* We must allocate a new buffer or resize the old one.  */ | 
|---|
| 1319 | if (malloc_count > 0) | 
|---|
| 1320 | { | 
|---|
| 1321 | ++malloc_count; | 
|---|
| 1322 | freemem_size = malloc_count * INITIAL_BLOCK_SIZE; | 
|---|
| 1323 | newmem = (transmem_block_t *) realloc (transmem_list, | 
|---|
| 1324 | freemem_size); | 
|---|
| 1325 | # ifdef _LIBC | 
|---|
| 1326 | if (newmem != NULL) | 
|---|
| 1327 | transmem_list = newmem; | 
|---|
| 1328 | else | 
|---|
| 1329 | { | 
|---|
| 1330 | struct transmem_list *old = transmem_list; | 
|---|
| 1331 |  | 
|---|
| 1332 | transmem_list = transmem_list->next; | 
|---|
| 1333 | free (old); | 
|---|
| 1334 | } | 
|---|
| 1335 | # endif | 
|---|
| 1336 | } | 
|---|
| 1337 | else | 
|---|
| 1338 | { | 
|---|
| 1339 | malloc_count = 1; | 
|---|
| 1340 | freemem_size = INITIAL_BLOCK_SIZE; | 
|---|
| 1341 | newmem = (transmem_block_t *) malloc (freemem_size); | 
|---|
| 1342 | # ifdef _LIBC | 
|---|
| 1343 | if (newmem != NULL) | 
|---|
| 1344 | { | 
|---|
| 1345 | /* Add the block to the list of blocks we have to free | 
|---|
| 1346 | at some point.  */ | 
|---|
| 1347 | newmem->next = transmem_list; | 
|---|
| 1348 | transmem_list = newmem; | 
|---|
| 1349 | } | 
|---|
| 1350 | /* Fall through and return -1.  */ | 
|---|
| 1351 | # endif | 
|---|
| 1352 | } | 
|---|
| 1353 | if (__builtin_expect (newmem == NULL, 0)) | 
|---|
| 1354 | { | 
|---|
| 1355 | freemem = NULL; | 
|---|
| 1356 | freemem_size = 0; | 
|---|
| 1357 | __libc_lock_unlock (lock); | 
|---|
| 1358 | return (char *) -1; | 
|---|
| 1359 | } | 
|---|
| 1360 |  | 
|---|
| 1361 | # ifdef _LIBC | 
|---|
| 1362 | freemem = (unsigned char *) newmem->data; | 
|---|
| 1363 | freemem_size -= offsetof (struct transmem_list, data); | 
|---|
| 1364 | # else | 
|---|
| 1365 | transmem_list = newmem; | 
|---|
| 1366 | freemem = newmem; | 
|---|
| 1367 | # endif | 
|---|
| 1368 |  | 
|---|
| 1369 | outbuf = freemem + sizeof (size_t); | 
|---|
| 1370 | } | 
|---|
| 1371 |  | 
|---|
| 1372 | /* We have now in our buffer a converted string.  Put this | 
|---|
| 1373 | into the table of conversions.  */ | 
|---|
| 1374 | *(size_t *) freemem = outbuf - freemem - sizeof (size_t); | 
|---|
| 1375 | convd->conv_tab[act] = (char *) freemem; | 
|---|
| 1376 | /* Shrink freemem, but keep it aligned.  */ | 
|---|
| 1377 | freemem_size -= outbuf - freemem; | 
|---|
| 1378 | freemem = outbuf; | 
|---|
| 1379 | freemem += freemem_size & (alignof (size_t) - 1); | 
|---|
| 1380 | freemem_size = freemem_size & ~ (alignof (size_t) - 1); | 
|---|
| 1381 |  | 
|---|
| 1382 | __libc_lock_unlock (lock); | 
|---|
| 1383 | } | 
|---|
| 1384 |  | 
|---|
| 1385 | /* Now convd->conv_tab[act] contains the translation of all | 
|---|
| 1386 | the plural variants.  */ | 
|---|
| 1387 | result = convd->conv_tab[act] + sizeof (size_t); | 
|---|
| 1388 | resultlen = *(size_t *) convd->conv_tab[act]; | 
|---|
| 1389 | } | 
|---|
| 1390 | } | 
|---|
| 1391 |  | 
|---|
| 1392 | /* The result string is converted.  */ | 
|---|
| 1393 |  | 
|---|
| 1394 | #endif /* _LIBC || HAVE_ICONV */ | 
|---|
| 1395 |  | 
|---|
| 1396 | *lengthp = resultlen; | 
|---|
| 1397 | return result; | 
|---|
| 1398 | } | 
|---|
| 1399 |  | 
|---|
| 1400 |  | 
|---|
| 1401 | /* Look up a plural variant.  */ | 
|---|
| 1402 | static char * | 
|---|
| 1403 | plural_lookup (struct loaded_l10nfile *domain, unsigned long int n, | 
|---|
| 1404 | const char *translation, size_t translation_len) | 
|---|
| 1405 | { | 
|---|
| 1406 | struct loaded_domain *domaindata = (struct loaded_domain *) domain->data; | 
|---|
| 1407 | unsigned long int index; | 
|---|
| 1408 | const char *p; | 
|---|
| 1409 |  | 
|---|
| 1410 | index = plural_eval (domaindata->plural, n); | 
|---|
| 1411 | if (index >= domaindata->nplurals) | 
|---|
| 1412 | /* This should never happen.  It means the plural expression and the | 
|---|
| 1413 | given maximum value do not match.  */ | 
|---|
| 1414 | index = 0; | 
|---|
| 1415 |  | 
|---|
| 1416 | /* Skip INDEX strings at TRANSLATION.  */ | 
|---|
| 1417 | p = translation; | 
|---|
| 1418 | while (index-- > 0) | 
|---|
| 1419 | { | 
|---|
| 1420 | #ifdef _LIBC | 
|---|
| 1421 | p = __rawmemchr (p, '\0'); | 
|---|
| 1422 | #else | 
|---|
| 1423 | p = strchr (p, '\0'); | 
|---|
| 1424 | #endif | 
|---|
| 1425 | /* And skip over the NUL byte.  */ | 
|---|
| 1426 | p++; | 
|---|
| 1427 |  | 
|---|
| 1428 | if (p >= translation + translation_len) | 
|---|
| 1429 | /* This should never happen.  It means the plural expression | 
|---|
| 1430 | evaluated to a value larger than the number of variants | 
|---|
| 1431 | available for MSGID1.  */ | 
|---|
| 1432 | return (char *) translation; | 
|---|
| 1433 | } | 
|---|
| 1434 | return (char *) p; | 
|---|
| 1435 | } | 
|---|
| 1436 |  | 
|---|
| 1437 | #ifndef _LIBC | 
|---|
| 1438 | /* Return string representation of locale CATEGORY.  */ | 
|---|
| 1439 | static const char * | 
|---|
| 1440 | category_to_name (int category) | 
|---|
| 1441 | { | 
|---|
| 1442 | const char *retval; | 
|---|
| 1443 |  | 
|---|
| 1444 | switch (category) | 
|---|
| 1445 | { | 
|---|
| 1446 | #ifdef LC_COLLATE | 
|---|
| 1447 | case LC_COLLATE: | 
|---|
| 1448 | retval = "LC_COLLATE"; | 
|---|
| 1449 | break; | 
|---|
| 1450 | #endif | 
|---|
| 1451 | #ifdef LC_CTYPE | 
|---|
| 1452 | case LC_CTYPE: | 
|---|
| 1453 | retval = "LC_CTYPE"; | 
|---|
| 1454 | break; | 
|---|
| 1455 | #endif | 
|---|
| 1456 | #ifdef LC_MONETARY | 
|---|
| 1457 | case LC_MONETARY: | 
|---|
| 1458 | retval = "LC_MONETARY"; | 
|---|
| 1459 | break; | 
|---|
| 1460 | #endif | 
|---|
| 1461 | #ifdef LC_NUMERIC | 
|---|
| 1462 | case LC_NUMERIC: | 
|---|
| 1463 | retval = "LC_NUMERIC"; | 
|---|
| 1464 | break; | 
|---|
| 1465 | #endif | 
|---|
| 1466 | #ifdef LC_TIME | 
|---|
| 1467 | case LC_TIME: | 
|---|
| 1468 | retval = "LC_TIME"; | 
|---|
| 1469 | break; | 
|---|
| 1470 | #endif | 
|---|
| 1471 | #ifdef LC_MESSAGES | 
|---|
| 1472 | case LC_MESSAGES: | 
|---|
| 1473 | retval = "LC_MESSAGES"; | 
|---|
| 1474 | break; | 
|---|
| 1475 | #endif | 
|---|
| 1476 | #ifdef LC_RESPONSE | 
|---|
| 1477 | case LC_RESPONSE: | 
|---|
| 1478 | retval = "LC_RESPONSE"; | 
|---|
| 1479 | break; | 
|---|
| 1480 | #endif | 
|---|
| 1481 | #ifdef LC_ALL | 
|---|
| 1482 | case LC_ALL: | 
|---|
| 1483 | /* This might not make sense but is perhaps better than any other | 
|---|
| 1484 | value.  */ | 
|---|
| 1485 | retval = "LC_ALL"; | 
|---|
| 1486 | break; | 
|---|
| 1487 | #endif | 
|---|
| 1488 | default: | 
|---|
| 1489 | /* If you have a better idea for a default value let me know.  */ | 
|---|
| 1490 | retval = "LC_XXX"; | 
|---|
| 1491 | } | 
|---|
| 1492 |  | 
|---|
| 1493 | return retval; | 
|---|
| 1494 | } | 
|---|
| 1495 | #endif | 
|---|
| 1496 |  | 
|---|
| 1497 | /* Guess value of current locale from value of the environment variables | 
|---|
| 1498 | or system-dependent defaults.  */ | 
|---|
| 1499 | static const char * | 
|---|
| 1500 | #ifdef IN_LIBGLOCALE | 
|---|
| 1501 | guess_category_value (int category, const char *categoryname, | 
|---|
| 1502 | const char *locale) | 
|---|
| 1503 |  | 
|---|
| 1504 | #else | 
|---|
| 1505 | guess_category_value (int category, const char *categoryname) | 
|---|
| 1506 | #endif | 
|---|
| 1507 | { | 
|---|
| 1508 | const char *language; | 
|---|
| 1509 | #ifndef IN_LIBGLOCALE | 
|---|
| 1510 | const char *locale; | 
|---|
| 1511 | # ifndef _LIBC | 
|---|
| 1512 | const char *language_default; | 
|---|
| 1513 | int locale_defaulted; | 
|---|
| 1514 | # endif | 
|---|
| 1515 | #endif | 
|---|
| 1516 |  | 
|---|
| 1517 | /* We use the settings in the following order: | 
|---|
| 1518 | 1. The value of the environment variable 'LANGUAGE'.  This is a GNU | 
|---|
| 1519 | extension.  Its value can be a colon-separated list of locale names. | 
|---|
| 1520 | 2. The value of the environment variable 'LC_ALL', 'LC_xxx', or 'LANG'. | 
|---|
| 1521 | More precisely, the first among these that is set to a non-empty value. | 
|---|
| 1522 | This is how POSIX specifies it.  The value is a single locale name. | 
|---|
| 1523 | 3. A system-dependent preference list of languages.  Its value can be a | 
|---|
| 1524 | colon-separated list of locale names. | 
|---|
| 1525 | 4. A system-dependent default locale name. | 
|---|
| 1526 | This way: | 
|---|
| 1527 | - System-dependent settings can be overridden by environment variables. | 
|---|
| 1528 | - If the system provides both a list of languages and a default locale, | 
|---|
| 1529 | the former is used.  */ | 
|---|
| 1530 |  | 
|---|
| 1531 | #ifndef IN_LIBGLOCALE | 
|---|
| 1532 | /* Fetch the locale name, through the POSIX method of looking to `LC_ALL', | 
|---|
| 1533 | `LC_xxx', and `LANG'.  On some systems this can be done by the | 
|---|
| 1534 | `setlocale' function itself.  */ | 
|---|
| 1535 | # ifdef _LIBC | 
|---|
| 1536 | locale = __current_locale_name (category); | 
|---|
| 1537 | # else | 
|---|
| 1538 | locale_defaulted = 0; | 
|---|
| 1539 | #  if HAVE_USELOCALE | 
|---|
| 1540 | locale = _nl_locale_name_thread_unsafe (category, categoryname); | 
|---|
| 1541 | if (locale == NULL) | 
|---|
| 1542 | #  endif | 
|---|
| 1543 | { | 
|---|
| 1544 | locale = _nl_locale_name_posix (category, categoryname); | 
|---|
| 1545 | if (locale == NULL) | 
|---|
| 1546 | { | 
|---|
| 1547 | locale = _nl_locale_name_default (); | 
|---|
| 1548 | locale_defaulted = 1; | 
|---|
| 1549 | } | 
|---|
| 1550 | } | 
|---|
| 1551 | # endif | 
|---|
| 1552 | #endif | 
|---|
| 1553 |  | 
|---|
| 1554 | /* Ignore LANGUAGE and its system-dependent analogon if the locale is set | 
|---|
| 1555 | to "C" because | 
|---|
| 1556 | 1. "C" locale usually uses the ASCII encoding, and most international | 
|---|
| 1557 | messages use non-ASCII characters. These characters get displayed | 
|---|
| 1558 | as question marks (if using glibc's iconv()) or as invalid 8-bit | 
|---|
| 1559 | characters (because other iconv()s refuse to convert most non-ASCII | 
|---|
| 1560 | characters to ASCII). In any case, the output is ugly. | 
|---|
| 1561 | 2. The precise output of some programs in the "C" locale is specified | 
|---|
| 1562 | by POSIX and should not depend on environment variables like | 
|---|
| 1563 | "LANGUAGE" or system-dependent information.  We allow such programs | 
|---|
| 1564 | to use gettext().  */ | 
|---|
| 1565 | if (strcmp (locale, "C") == 0) | 
|---|
| 1566 | return locale; | 
|---|
| 1567 |  | 
|---|
| 1568 | /* The highest priority value is the value of the 'LANGUAGE' environment | 
|---|
| 1569 | variable.  */ | 
|---|
| 1570 | language = getenv ( "LANGUAGE"); | 
|---|
| 1571 | if (language != NULL && language[0] != '\0') | 
|---|
| 1572 | return language; | 
|---|
| 1573 | #if !defined IN_LIBGLOCALE && !defined _LIBC | 
|---|
| 1574 | /* The next priority value is the locale name, if not defaulted.  */ | 
|---|
| 1575 | if (locale_defaulted) | 
|---|
| 1576 | { | 
|---|
| 1577 | /* The next priority value is the default language preferences list. */ | 
|---|
| 1578 | language_default = _nl_language_preferences_default (); | 
|---|
| 1579 | if (language_default != NULL) | 
|---|
| 1580 | return language_default; | 
|---|
| 1581 | } | 
|---|
| 1582 | /* The least priority value is the locale name, if defaulted.  */ | 
|---|
| 1583 | #endif | 
|---|
| 1584 | return locale; | 
|---|
| 1585 | } | 
|---|
| 1586 |  | 
|---|
| 1587 | #if (defined _LIBC || HAVE_ICONV) && !defined IN_LIBGLOCALE | 
|---|
| 1588 | /* Returns the output charset.  */ | 
|---|
| 1589 | static const char * | 
|---|
| 1590 | get_output_charset (struct binding *domainbinding) | 
|---|
| 1591 | { | 
|---|
| 1592 | /* The output charset should normally be determined by the locale.  But | 
|---|
| 1593 | sometimes the locale is not used or not correctly set up, so we provide | 
|---|
| 1594 | a possibility for the user to override this: the OUTPUT_CHARSET | 
|---|
| 1595 | environment variable.  Moreover, the value specified through | 
|---|
| 1596 | bind_textdomain_codeset overrides both.  */ | 
|---|
| 1597 | if (domainbinding != NULL && domainbinding->codeset != NULL) | 
|---|
| 1598 | return domainbinding->codeset; | 
|---|
| 1599 | else | 
|---|
| 1600 | { | 
|---|
| 1601 | /* For speed reasons, we look at the value of OUTPUT_CHARSET only | 
|---|
| 1602 | once.  This is a user variable that is not supposed to change | 
|---|
| 1603 | during a program run.  */ | 
|---|
| 1604 | static char *output_charset_cache; | 
|---|
| 1605 | static int output_charset_cached; | 
|---|
| 1606 |  | 
|---|
| 1607 | if (!output_charset_cached) | 
|---|
| 1608 | { | 
|---|
| 1609 | const char *value = getenv ( "OUTPUT_CHARSET"); | 
|---|
| 1610 |  | 
|---|
| 1611 | if (value != NULL && value[0] != '\0') | 
|---|
| 1612 | { | 
|---|
| 1613 | size_t len = strlen (value) + 1; | 
|---|
| 1614 | char *value_copy = (char *) malloc (len); | 
|---|
| 1615 |  | 
|---|
| 1616 | if (value_copy != NULL) | 
|---|
| 1617 | memcpy (value_copy, value, len); | 
|---|
| 1618 | output_charset_cache = value_copy; | 
|---|
| 1619 | } | 
|---|
| 1620 | output_charset_cached = 1; | 
|---|
| 1621 | } | 
|---|
| 1622 |  | 
|---|
| 1623 | if (output_charset_cache != NULL) | 
|---|
| 1624 | return output_charset_cache; | 
|---|
| 1625 | else | 
|---|
| 1626 | { | 
|---|
| 1627 | # ifdef _LIBC | 
|---|
| 1628 | return _NL_CURRENT (LC_CTYPE, CODESET); | 
|---|
| 1629 | # else | 
|---|
| 1630 | #  if HAVE_ICONV | 
|---|
| 1631 | return locale_charset (); | 
|---|
| 1632 | #  endif | 
|---|
| 1633 | # endif | 
|---|
| 1634 | } | 
|---|
| 1635 | } | 
|---|
| 1636 | } | 
|---|
| 1637 | #endif | 
|---|
| 1638 |  | 
|---|
| 1639 | /* @@ begin of epilog @@ */ | 
|---|
| 1640 |  | 
|---|
| 1641 | /* We don't want libintl.a to depend on any other library.  So we | 
|---|
| 1642 | avoid the non-standard function stpcpy.  In GNU C Library this | 
|---|
| 1643 | function is available, though.  Also allow the symbol HAVE_STPCPY | 
|---|
| 1644 | to be defined.  */ | 
|---|
| 1645 | #if !_LIBC && !HAVE_STPCPY | 
|---|
| 1646 | static char * | 
|---|
| 1647 | stpcpy (char *dest, const char *src) | 
|---|
| 1648 | { | 
|---|
| 1649 | while ((*dest++ = *src++) != '\0') | 
|---|
| 1650 | /* Do nothing. */ ; | 
|---|
| 1651 | return dest - 1; | 
|---|
| 1652 | } | 
|---|
| 1653 | #endif | 
|---|
| 1654 |  | 
|---|
| 1655 | #if !_LIBC && !HAVE_MEMPCPY | 
|---|
| 1656 | static void * | 
|---|
| 1657 | mempcpy (void *dest, const void *src, size_t n) | 
|---|
| 1658 | { | 
|---|
| 1659 | return (void *) ((char *) memcpy (dest, src, n) + n); | 
|---|
| 1660 | } | 
|---|
| 1661 | #endif | 
|---|
| 1662 |  | 
|---|
| 1663 | #if !_LIBC && !HAVE_TSEARCH | 
|---|
| 1664 | # include "tsearch.c" | 
|---|
| 1665 | #endif | 
|---|
| 1666 |  | 
|---|
| 1667 |  | 
|---|
| 1668 | #ifdef _LIBC | 
|---|
| 1669 | /* If we want to free all resources we have to do some work at | 
|---|
| 1670 | program's end.  */ | 
|---|
| 1671 | libc_freeres_fn (free_mem) | 
|---|
| 1672 | { | 
|---|
| 1673 | void *old; | 
|---|
| 1674 |  | 
|---|
| 1675 | while (_nl_domain_bindings != NULL) | 
|---|
| 1676 | { | 
|---|
| 1677 | struct binding *oldp = _nl_domain_bindings; | 
|---|
| 1678 | _nl_domain_bindings = _nl_domain_bindings->next; | 
|---|
| 1679 | if (oldp->dirname != _nl_default_dirname) | 
|---|
| 1680 | /* Yes, this is a pointer comparison.  */ | 
|---|
| 1681 | free (oldp->dirname); | 
|---|
| 1682 | free (oldp->codeset); | 
|---|
| 1683 | free (oldp); | 
|---|
| 1684 | } | 
|---|
| 1685 |  | 
|---|
| 1686 | if (_nl_current_default_domain != _nl_default_default_domain) | 
|---|
| 1687 | /* Yes, again a pointer comparison.  */ | 
|---|
| 1688 | free ((char *) _nl_current_default_domain); | 
|---|
| 1689 |  | 
|---|
| 1690 | /* Remove the search tree with the known translations.  */ | 
|---|
| 1691 | __tdestroy (root, free); | 
|---|
| 1692 | root = NULL; | 
|---|
| 1693 |  | 
|---|
| 1694 | while (transmem_list != NULL) | 
|---|
| 1695 | { | 
|---|
| 1696 | old = transmem_list; | 
|---|
| 1697 | transmem_list = transmem_list->next; | 
|---|
| 1698 | free (old); | 
|---|
| 1699 | } | 
|---|
| 1700 | } | 
|---|
| 1701 | #endif | 
|---|
| 1702 |  | 
|---|