Simplify char16_t implementation
[jlayton/glibc.git] / wcsmbs / wcsmbsload.c
1 /* Copyright (C) 1998-2002,2004,2005,2008,2010,2011,2012
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <ctype.h>
22 #include <langinfo.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <locale/localeinfo.h>
28 #include <wcsmbsload.h>
29 #include <bits/libc-lock.h>
30
31
32 /* These are the descriptions for the default conversion functions.  */
33 static const struct __gconv_step to_wc =
34 {
35   .__shlib_handle = NULL,
36   .__modname = NULL,
37   .__counter = INT_MAX,
38   .__from_name = (char *) "ANSI_X3.4-1968//TRANSLIT",
39   .__to_name = (char *) "INTERNAL",
40   .__fct = __gconv_transform_ascii_internal,
41   .__btowc_fct = __gconv_btwoc_ascii,
42   .__init_fct = NULL,
43   .__end_fct = NULL,
44   .__min_needed_from = 1,
45   .__max_needed_from = 1,
46   .__min_needed_to = 4,
47   .__max_needed_to = 4,
48   .__stateful = 0,
49   .__data = NULL
50 };
51
52 static const struct __gconv_step to_mb =
53 {
54   .__shlib_handle = NULL,
55   .__modname = NULL,
56   .__counter = INT_MAX,
57   .__from_name = (char *) "INTERNAL",
58   .__to_name = (char *) "ANSI_X3.4-1968//TRANSLIT",
59   .__fct = __gconv_transform_internal_ascii,
60   .__btowc_fct = NULL,
61   .__init_fct = NULL,
62   .__end_fct = NULL,
63   .__min_needed_from = 4,
64   .__max_needed_from = 4,
65   .__min_needed_to = 1,
66   .__max_needed_to = 1,
67   .__stateful = 0,
68   .__data = NULL
69 };
70
71
72 /* For the default locale we only have to handle ANSI_X3.4-1968.  */
73 const struct gconv_fcts __wcsmbs_gconv_fcts_c =
74 {
75   .towc = (struct __gconv_step *) &to_wc,
76   .towc_nsteps = 1,
77   .tomb = (struct __gconv_step *) &to_mb,
78   .tomb_nsteps = 1,
79 };
80
81
82 attribute_hidden
83 struct __gconv_step *
84 __wcsmbs_getfct (const char *to, const char *from, size_t *nstepsp)
85 {
86   size_t nsteps;
87   struct __gconv_step *result;
88 #if 0
89   size_t nstateful;
90   size_t cnt;
91 #endif
92
93   if (__gconv_find_transform (to, from, &result, &nsteps, 0) != __GCONV_OK)
94     /* Loading the conversion step is not possible.  */
95     return NULL;
96
97   /* Maybe it is someday necessary to allow more than one step.
98      Currently this is not the case since the conversions handled here
99      are from and to INTERNAL and there always is a converted for
100      that.  It the directly following code is enabled the libio
101      functions will have to allocate appropriate __gconv_step_data
102      elements instead of only one.  */
103 #if 0
104   /* Count the number of stateful conversions.  Since we will only
105      have one 'mbstate_t' object available we can only deal with one
106      stateful conversion.  */
107   nstateful = 0;
108   for (cnt = 0; cnt < nsteps; ++cnt)
109     if (result[cnt].__stateful)
110       ++nstateful;
111   if (nstateful > 1)
112 #else
113   if (nsteps > 1)
114 #endif
115     {
116       /* We cannot handle this case.  */
117       __gconv_close_transform (result, nsteps);
118       result = NULL;
119     }
120   else
121     *nstepsp = nsteps;
122
123   return result;
124 }
125
126
127 /* Extract from the given locale name the character set portion.  Since
128    only the XPG form of the name includes this information we don't have
129    to take care for the CEN form.  */
130 #define extract_charset_name(str) \
131   ({                                                                          \
132     const char *cp = str;                                                     \
133     char *result = NULL;                                                      \
134                                                                               \
135     cp += strcspn (cp, "@.+,");                                               \
136     if (*cp == '.')                                                           \
137       {                                                                       \
138         const char *endp = ++cp;                                              \
139         while (*endp != '\0' && *endp != '@')                                 \
140           ++endp;                                                             \
141         if (endp != cp)                                                       \
142           result = strndupa (cp, endp - cp);                                  \
143       }                                                                       \
144     result;                                                                   \
145   })
146
147
148 /* Some of the functions here must not be used while setlocale is called.  */
149 __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
150
151 /* Load conversion functions for the currently selected locale.  */
152 void
153 internal_function
154 __wcsmbs_load_conv (struct __locale_data *new_category)
155 {
156   /* Acquire the lock.  */
157   __libc_rwlock_wrlock (__libc_setlocale_lock);
158
159   /* We should repeat the test since while we waited some other thread
160      might have run this function.  */
161   if (__builtin_expect (new_category->private.ctype == NULL, 1))
162     {
163       /* We must find the real functions.  */
164       const char *charset_name;
165       const char *complete_name;
166       struct gconv_fcts *new_fcts;
167       int use_translit;
168
169       /* Allocate the gconv_fcts structure.  */
170       new_fcts = calloc (1, sizeof *new_fcts);
171       if (new_fcts == NULL)
172         goto failed;
173
174       /* Get name of charset of the locale.  */
175       charset_name = new_category->values[_NL_ITEM_INDEX(CODESET)].string;
176
177       /* Does the user want transliteration?  */
178       use_translit = new_category->use_translit;
179
180       /* Normalize the name and add the slashes necessary for a
181          complete lookup.  */
182       complete_name = norm_add_slashes (charset_name,
183                                         use_translit ? "TRANSLIT" : "");
184
185       /* It is not necessary to use transliteration in this direction
186          since the internal character set is supposed to be able to
187          represent all others.  */
188       new_fcts->towc = __wcsmbs_getfct ("INTERNAL", complete_name,
189                                         &new_fcts->towc_nsteps);
190       if (new_fcts->towc != NULL)
191         new_fcts->tomb = __wcsmbs_getfct (complete_name, "INTERNAL",
192                                           &new_fcts->tomb_nsteps);
193
194       /* If any of the conversion functions is not available we don't
195          use any since this would mean we cannot convert back and
196          forth.  NB: NEW_FCTS was allocated with calloc.  */
197       if (new_fcts->tomb == NULL)
198         {
199           if (new_fcts->towc != NULL)
200             __gconv_close_transform (new_fcts->towc, new_fcts->towc_nsteps);
201
202           free (new_fcts);
203
204         failed:
205           new_category->private.ctype = &__wcsmbs_gconv_fcts_c;
206         }
207       else
208         {
209           new_category->private.ctype = new_fcts;
210           new_category->private.cleanup = &_nl_cleanup_ctype;
211         }
212     }
213
214   __libc_rwlock_unlock (__libc_setlocale_lock);
215 }
216
217
218 /* Clone the current conversion function set.  */
219 void
220 internal_function
221 __wcsmbs_clone_conv (struct gconv_fcts *copy)
222 {
223   const struct gconv_fcts *orig;
224
225   orig = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
226
227   /* Copy the data.  */
228   *copy = *orig;
229
230   /* Now increment the usage counters.
231      Note: This assumes copy->*_nsteps == 1.  */
232   if (copy->towc->__shlib_handle != NULL)
233     ++copy->towc->__counter;
234   if (copy->tomb->__shlib_handle != NULL)
235     ++copy->tomb->__counter;
236 }
237
238
239 /* Get converters for named charset.  */
240 int
241 internal_function
242 __wcsmbs_named_conv (struct gconv_fcts *copy, const char *name)
243 {
244   copy->towc = __wcsmbs_getfct ("INTERNAL", name, &copy->towc_nsteps);
245   if (copy->towc == NULL)
246     return 1;
247
248   copy->tomb = __wcsmbs_getfct (name, "INTERNAL", &copy->tomb_nsteps);
249   if (copy->tomb == NULL)
250     {
251       __gconv_close_transform (copy->towc, copy->towc_nsteps);
252       return 1;
253     }
254
255   return 0;
256 }
257
258 void internal_function
259 _nl_cleanup_ctype (struct __locale_data *locale)
260 {
261   const struct gconv_fcts *const data = locale->private.ctype;
262   if (data != NULL)
263     {
264       locale->private.ctype = NULL;
265       locale->private.cleanup = NULL;
266
267       /* Free the old conversions.  */
268       __gconv_close_transform (data->tomb, data->tomb_nsteps);
269       __gconv_close_transform (data->towc, data->towc_nsteps);
270       free ((char *) data);
271     }
272 }