1 /* Copyright (C) 1998-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
25 #include <locale/localeinfo.h>
26 #include <wcsmbsload.h>
27 #include <libc-lock.h>
30 /* These are the descriptions for the default conversion functions. */
31 static const struct __gconv_step to_wc
=
33 .__shlib_handle
= NULL
,
36 .__from_name
= (char *) "ANSI_X3.4-1968//TRANSLIT",
37 .__to_name
= (char *) "INTERNAL",
38 .__fct
= __gconv_transform_ascii_internal
,
39 .__btowc_fct
= __gconv_btwoc_ascii
,
42 .__min_needed_from
= 1,
43 .__max_needed_from
= 1,
50 static const struct __gconv_step to_mb
=
52 .__shlib_handle
= NULL
,
55 .__from_name
= (char *) "INTERNAL",
56 .__to_name
= (char *) "ANSI_X3.4-1968//TRANSLIT",
57 .__fct
= __gconv_transform_internal_ascii
,
61 .__min_needed_from
= 4,
62 .__max_needed_from
= 4,
70 /* For the default locale we only have to handle ANSI_X3.4-1968. */
71 const struct gconv_fcts __wcsmbs_gconv_fcts_c
=
73 .towc
= (struct __gconv_step
*) &to_wc
,
75 .tomb
= (struct __gconv_step
*) &to_mb
,
82 __wcsmbs_getfct (const char *to
, const char *from
, size_t *nstepsp
)
85 struct __gconv_step
*result
;
91 if (__gconv_find_transform (to
, from
, &result
, &nsteps
, 0) != __GCONV_OK
)
92 /* Loading the conversion step is not possible. */
95 /* Maybe it is someday necessary to allow more than one step.
96 Currently this is not the case since the conversions handled here
97 are from and to INTERNAL and there always is a converted for
98 that. It the directly following code is enabled the libio
99 functions will have to allocate appropriate __gconv_step_data
100 elements instead of only one. */
102 /* Count the number of stateful conversions. Since we will only
103 have one 'mbstate_t' object available we can only deal with one
104 stateful conversion. */
106 for (cnt
= 0; cnt
< nsteps
; ++cnt
)
107 if (result
[cnt
].__stateful
)
114 /* We cannot handle this case. */
115 __gconv_close_transform (result
, nsteps
);
125 /* Extract from the given locale name the character set portion. Since
126 only the XPG form of the name includes this information we don't have
127 to take care for the CEN form. */
128 #define extract_charset_name(str) \
130 const char *cp = str; \
131 char *result = NULL; \
133 cp += strcspn (cp, "@.+,"); \
136 const char *endp = ++cp; \
137 while (*endp != '\0' && *endp != '@') \
140 result = strndupa (cp, endp - cp); \
146 /* Some of the functions here must not be used while setlocale is called. */
147 __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden
)
149 /* Load conversion functions for the currently selected locale. */
151 __wcsmbs_load_conv (struct __locale_data
*new_category
)
153 struct lc_ctype_data
*data
= new_category
->private;
155 /* Acquire the lock. */
156 __libc_rwlock_wrlock (__libc_setlocale_lock
);
158 /* We should repeat the test since while we waited some other thread
159 might have run this function. */
160 if (__glibc_likely (data
->fcts
== NULL
))
162 /* We must find the real functions. */
163 const char *charset_name
;
164 const char *complete_name
;
165 struct gconv_fcts
*new_fcts
;
168 /* Allocate the gconv_fcts structure. */
169 new_fcts
= calloc (1, sizeof *new_fcts
);
170 if (new_fcts
== NULL
)
173 /* Get name of charset of the locale. */
174 charset_name
= new_category
->values
[_NL_ITEM_INDEX(CODESET
)].string
;
176 /* Does the user want transliteration? */
177 use_translit
= new_category
->use_translit
;
179 /* Normalize the name and add the slashes necessary for a
181 complete_name
= norm_add_slashes (charset_name
,
182 use_translit
? "TRANSLIT" : "");
184 /* It is not necessary to use transliteration in this direction
185 since the internal character set is supposed to be able to
186 represent all others. */
187 new_fcts
->towc
= __wcsmbs_getfct ("INTERNAL", complete_name
,
188 &new_fcts
->towc_nsteps
);
189 if (new_fcts
->towc
!= NULL
)
190 new_fcts
->tomb
= __wcsmbs_getfct (complete_name
, "INTERNAL",
191 &new_fcts
->tomb_nsteps
);
193 /* If any of the conversion functions is not available we don't
194 use any since this would mean we cannot convert back and
195 forth. NB: NEW_FCTS was allocated with calloc. */
196 if (new_fcts
->tomb
== NULL
)
198 if (new_fcts
->towc
!= NULL
)
199 __gconv_close_transform (new_fcts
->towc
, new_fcts
->towc_nsteps
);
204 data
->fcts
= (void *) &__wcsmbs_gconv_fcts_c
;
207 data
->fcts
= new_fcts
;
210 __libc_rwlock_unlock (__libc_setlocale_lock
);
214 /* Clone the current conversion function set. */
216 __wcsmbs_clone_conv (struct gconv_fcts
*copy
)
218 const struct gconv_fcts
*orig
;
220 orig
= get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE
));
225 /* Now increment the usage counters. Note: This assumes
226 copy->*_nsteps == 1. The current locale holds a reference, so it
227 is still there after acquiring the lock. */
229 __libc_lock_lock (__gconv_lock
);
231 bool overflow
= false;
232 if (copy
->towc
->__shlib_handle
!= NULL
)
233 overflow
|= __builtin_add_overflow (copy
->towc
->__counter
, 1,
234 ©
->towc
->__counter
);
235 if (copy
->tomb
->__shlib_handle
!= NULL
)
236 overflow
|= __builtin_add_overflow (copy
->tomb
->__counter
, 1,
237 ©
->tomb
->__counter
);
239 __libc_lock_unlock (__gconv_lock
);
243 Fatal glibc error: gconv module reference counter overflow\n");
247 /* Get converters for named charset. */
249 __wcsmbs_named_conv (struct gconv_fcts
*copy
, const char *name
)
251 copy
->towc
= __wcsmbs_getfct ("INTERNAL", name
, ©
->towc_nsteps
);
252 if (copy
->towc
== NULL
)
255 copy
->tomb
= __wcsmbs_getfct (name
, "INTERNAL", ©
->tomb_nsteps
);
256 if (copy
->tomb
== NULL
)
258 __gconv_close_transform (copy
->towc
, copy
->towc_nsteps
);
266 _nl_cleanup_ctype (struct __locale_data
*locale
)
268 struct lc_ctype_data
*data
= locale
->private;
269 if (data
->fcts
!= NULL
&& data
->fcts
!= &__wcsmbs_gconv_fcts_c
)
271 /* Free the old conversions. */
272 __gconv_close_transform (data
->fcts
->tomb
, data
->fcts
->tomb_nsteps
);
273 __gconv_close_transform (data
->fcts
->towc
, data
->fcts
->towc_nsteps
);
275 free ((void *) data
->fcts
);
277 /* data itself is allocated within locale. */