997
828
#if defined _LIBC || HAVE_ICONV
998
# ifdef IN_LIBGLOCALE
999
if (encoding != NULL)
1004
/* We are supposed to do a conversion. */
1005
# ifndef IN_LIBGLOCALE
1006
const char *encoding = get_output_charset (domainbinding);
1009
/* Search whether a table with converted translations for this
1010
encoding has already been allocated. */
1011
size_t nconversions = domain->nconversions;
1012
struct converted_domain *convd = NULL;
1015
for (i = nconversions; i > 0; )
1018
if (strcmp (domain->conversions[i].encoding, encoding) == 0)
1020
convd = &domain->conversions[i];
1027
/* Allocate a table for the converted translations for this
1029
struct converted_domain *new_conversions =
1030
(struct converted_domain *)
1031
(domain->conversions != NULL
1032
? realloc (domain->conversions,
1033
(nconversions + 1) * sizeof (struct converted_domain))
1034
: malloc ((nconversions + 1) * sizeof (struct converted_domain)));
1036
if (__builtin_expect (new_conversions == NULL, 0))
1037
/* Nothing we can do, no more memory. We cannot use the
1038
translation because it might be encoded incorrectly. */
1041
domain->conversions = new_conversions;
1043
/* Copy the 'encoding' string to permanent storage. */
1044
encoding = strdup (encoding);
1045
if (__builtin_expect (encoding == NULL, 0))
1046
/* Nothing we can do, no more memory. We cannot use the
1047
translation because it might be encoded incorrectly. */
1050
convd = &new_conversions[nconversions];
1051
convd->encoding = encoding;
1053
/* Find out about the character set the file is encoded with.
1054
This can be found (in textual form) in the entry "". If this
1055
entry does not exist or if this does not contain the 'charset='
1056
information, we will assume the charset matches the one the
1057
current locale and we don't have to perform any conversion. */
1059
convd->conv = (__gconv_t) -1;
1062
convd->conv = (iconv_t) -1;
1067
size_t nullentrylen;
1069
/* Get the header entry. This is a recursion, but it doesn't
1070
reallocate domain->conversions because we pass
1071
encoding = NULL or convert = 0, respectively. */
1073
# ifdef IN_LIBGLOCALE
1074
_nl_find_msg (domain_file, domainbinding, NULL, "",
1077
_nl_find_msg (domain_file, domainbinding, "", 0, &nullentrylen);
1080
if (nullentry != NULL)
1082
const char *charsetstr;
1084
charsetstr = strstr (nullentry, "charset=");
1085
if (charsetstr != NULL)
1089
const char *outcharset;
1091
charsetstr += strlen ("charset=");
1092
len = strcspn (charsetstr, " \t\n");
1094
charset = (char *) alloca (len + 1);
1095
# if defined _LIBC || HAVE_MEMPCPY
1096
*((char *) mempcpy (charset, charsetstr, len)) = '\0';
1098
memcpy (charset, charsetstr, len);
1099
charset[len] = '\0';
1102
outcharset = encoding;
1105
/* We always want to use transliteration. */
1106
outcharset = norm_add_slashes (outcharset, "TRANSLIT");
1107
charset = norm_add_slashes (charset, "");
1108
int r = __gconv_open (outcharset, charset, &convd->conv,
1109
GCONV_AVOID_NOCONV);
1110
if (__builtin_expect (r != __GCONV_OK, 0))
1112
/* If the output encoding is the same there is
1113
nothing to do. Otherwise do not use the
1114
translation at all. */
1115
if (__builtin_expect (r != __GCONV_NOCONV, 1))
1118
convd->conv = (__gconv_t) -1;
1122
/* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
1123
we want to use transliteration. */
1124
# if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
1125
|| _LIBICONV_VERSION >= 0x0105
1126
if (strchr (outcharset, '/') == NULL)
1130
len = strlen (outcharset);
1131
tmp = (char *) alloca (len + 10 + 1);
1132
memcpy (tmp, outcharset, len);
1133
memcpy (tmp + len, "//TRANSLIT", 10 + 1);
1136
convd->conv = iconv_open (outcharset, charset);
1142
convd->conv = iconv_open (outcharset, charset);
1150
convd->conv_tab = NULL;
1151
/* Here domain->conversions is still == new_conversions. */
1152
domain->nconversions++;
1157
convd->conv != (__gconv_t) -1
1160
convd->conv != (iconv_t) -1
1165
/* We are supposed to do a conversion. First allocate an
1166
appropriate table with the same structure as the table
1167
of translations in the file, where we can put the pointers
1168
to the converted strings in.
1169
There is a slight complication with plural entries. They
1170
are represented by consecutive NUL terminated strings. We
1171
handle this case by converting RESULTLEN bytes, including
1174
if (convd->conv_tab == NULL
1175
&& ((convd->conv_tab =
1176
(char **) calloc (nstrings + domain->n_sysdep_strings,
1179
/* Mark that we didn't succeed allocating a table. */
1180
convd->conv_tab = (char **) -1;
1182
if (__builtin_expect (convd->conv_tab == (char **) -1, 0))
1183
/* Nothing we can do, no more memory. We cannot use the
1184
translation because it might be encoded incorrectly. */
1187
if (convd->conv_tab[act] == NULL)
1189
/* We haven't used this string so far, so it is not
1190
translated yet. Do this now. */
1191
/* We use a bit more efficient memory handling.
1192
We allocate always larger blocks which get used over
1193
time. This is faster than many small allocations. */
1194
__libc_lock_define_initialized (static, lock)
829
if (domain->codeset_cntr
830
!= (domainbinding != NULL ? domainbinding->codeset_cntr : 0))
832
/* The domain's codeset has changed through bind_textdomain_codeset()
833
since the message catalog was initialized or last accessed. We
834
have to reinitialize the converter. */
835
_nl_free_domain_conv (domain);
836
_nl_init_domain_conv (domain_file, domain, domainbinding);
841
domain->conv != (__gconv_t) -1
844
domain->conv != (iconv_t) -1
849
/* We are supposed to do a conversion. First allocate an
850
appropriate table with the same structure as the table
851
of translations in the file, where we can put the pointers
852
to the converted strings in.
853
There is a slight complication with plural entries. They
854
are represented by consecutive NUL terminated strings. We
855
handle this case by converting RESULTLEN bytes, including
858
if (domain->conv_tab == NULL
859
&& ((domain->conv_tab =
860
(char **) calloc (nstrings + domain->n_sysdep_strings,
863
/* Mark that we didn't succeed allocating a table. */
864
domain->conv_tab = (char **) -1;
866
if (__builtin_expect (domain->conv_tab == (char **) -1, 0))
867
/* Nothing we can do, no more memory. */
870
if (domain->conv_tab[act] == NULL)
872
/* We haven't used this string so far, so it is not
873
translated yet. Do this now. */
874
/* We use a bit more efficient memory handling.
875
We allocate always larger blocks which get used over
876
time. This is faster than many small allocations. */
877
__libc_lock_define_initialized (static, lock)
1195
878
# define INITIAL_BLOCK_SIZE 4080
1196
static unsigned char *freemem;
1197
static size_t freemem_size;
879
static unsigned char *freemem;
880
static size_t freemem_size;
1199
const unsigned char *inbuf;
1200
unsigned char *outbuf;
882
const unsigned char *inbuf;
883
unsigned char *outbuf;
1203
transmem_block_t *transmem_list = NULL;
1206
__libc_lock_lock (lock);
1208
inbuf = (const unsigned char *) result;
886
transmem_block_t *transmem_list = NULL;
889
__libc_lock_lock (lock);
891
inbuf = (const unsigned char *) result;
892
outbuf = freemem + sizeof (size_t);
897
transmem_block_t *newmem;
899
size_t non_reversible;
902
if (freemem_size < sizeof (size_t))
905
res = __gconv (domain->conv,
906
&inbuf, inbuf + resultlen,
908
outbuf + freemem_size - sizeof (size_t),
911
if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
914
if (res != __GCONV_FULL_OUTPUT)
916
__libc_lock_unlock (lock);
923
const char *inptr = (const char *) inbuf;
924
size_t inleft = resultlen;
925
char *outptr = (char *) outbuf;
928
if (freemem_size < sizeof (size_t))
931
outleft = freemem_size - sizeof (size_t);
932
if (iconv (domain->conv,
933
(ICONV_CONST char **) &inptr, &inleft,
937
outbuf = (unsigned char *) outptr;
942
__libc_lock_unlock (lock);
949
/* We must allocate a new buffer or resize the old one. */
950
if (malloc_count > 0)
953
freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
954
newmem = (transmem_block_t *) realloc (transmem_list,
958
transmem_list = transmem_list->next;
961
struct transmem_list *old = transmem_list;
963
transmem_list = transmem_list->next;
971
freemem_size = INITIAL_BLOCK_SIZE;
972
newmem = (transmem_block_t *) malloc (freemem_size);
974
if (__builtin_expect (newmem == NULL, 0))
978
__libc_lock_unlock (lock);
983
/* Add the block to the list of blocks we have to free
985
newmem->next = transmem_list;
986
transmem_list = newmem;
988
freemem = newmem->data;
989
freemem_size -= offsetof (struct transmem_list, data);
991
transmem_list = newmem;
1209
995
outbuf = freemem + sizeof (size_t);
1214
transmem_block_t *newmem;
1216
size_t non_reversible;
1219
if (freemem_size < sizeof (size_t))
1220
goto resize_freemem;
1222
res = __gconv (convd->conv,
1223
&inbuf, inbuf + resultlen,
1225
outbuf + freemem_size - sizeof (size_t),
1228
if (res == __GCONV_OK || res == __GCONV_EMPTY_INPUT)
1231
if (res != __GCONV_FULL_OUTPUT)
1233
/* We should not use the translation at all, it
1234
is incorrectly encoded. */
1235
__libc_lock_unlock (lock);
1239
inbuf = (const unsigned char *) result;
1242
const char *inptr = (const char *) inbuf;
1243
size_t inleft = resultlen;
1244
char *outptr = (char *) outbuf;
1247
if (freemem_size < sizeof (size_t))
1248
goto resize_freemem;
1250
outleft = freemem_size - sizeof (size_t);
1251
if (iconv (convd->conv,
1252
(ICONV_CONST char **) &inptr, &inleft,
1256
outbuf = (unsigned char *) outptr;
1261
__libc_lock_unlock (lock);
1268
/* We must allocate a new buffer or resize the old one. */
1269
if (malloc_count > 0)
1272
freemem_size = malloc_count * INITIAL_BLOCK_SIZE;
1273
newmem = (transmem_block_t *) realloc (transmem_list,
1277
transmem_list = transmem_list->next;
1280
struct transmem_list *old = transmem_list;
1282
transmem_list = transmem_list->next;
1290
freemem_size = INITIAL_BLOCK_SIZE;
1291
newmem = (transmem_block_t *) malloc (freemem_size);
1293
if (__builtin_expect (newmem == NULL, 0))
1297
__libc_lock_unlock (lock);
1302
/* Add the block to the list of blocks we have to free
1304
newmem->next = transmem_list;
1305
transmem_list = newmem;
1307
freemem = (unsigned char *) newmem->data;
1308
freemem_size -= offsetof (struct transmem_list, data);
1310
transmem_list = newmem;
1314
outbuf = freemem + sizeof (size_t);
1317
/* We have now in our buffer a converted string. Put this
1318
into the table of conversions. */
1319
*(size_t *) freemem = outbuf - freemem - sizeof (size_t);
1320
convd->conv_tab[act] = (char *) freemem;
1321
/* Shrink freemem, but keep it aligned. */
1322
freemem_size -= outbuf - freemem;
1324
freemem += freemem_size & (alignof (size_t) - 1);
1325
freemem_size = freemem_size & ~ (alignof (size_t) - 1);
1327
__libc_lock_unlock (lock);
1330
/* Now convd->conv_tab[act] contains the translation of all
1331
the plural variants. */
1332
result = convd->conv_tab[act] + sizeof (size_t);
1333
resultlen = *(size_t *) convd->conv_tab[act];
998
/* We have now in our buffer a converted string. Put this
999
into the table of conversions. */
1000
*(size_t *) freemem = outbuf - freemem - sizeof (size_t);
1001
domain->conv_tab[act] = (char *) freemem;
1002
/* Shrink freemem, but keep it aligned. */
1003
freemem_size -= outbuf - freemem;
1005
freemem += freemem_size & (alignof (size_t) - 1);
1006
freemem_size = freemem_size & ~ (alignof (size_t) - 1);
1008
__libc_lock_unlock (lock);
1011
/* Now domain->conv_tab[act] contains the translation of all
1012
the plural variants. */
1013
result = domain->conv_tab[act] + sizeof (size_t);
1014
resultlen = *(size_t *) domain->conv_tab[act];
1337
1018
/* The result string is converted. */
1339
1020
#endif /* _LIBC || HAVE_ICONV */