Update.
[platform/upstream/glibc.git] / wcsmbs / wcsmbsload.c
1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <ctype.h>
21 #include <langinfo.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <locale/localeinfo.h>
27 #include <wcsmbsload.h>
28 #include <bits/libc-lock.h>
29 #include <iconv/gconv_int.h>
30
31
32 /* Last loaded locale for LC_CTYPE.  We initialize for the C locale
33    which is enabled at startup.  */
34 extern const struct locale_data _nl_C_LC_CTYPE;
35 const struct locale_data *__wcsmbs_last_locale = &_nl_C_LC_CTYPE;
36
37
38 /* These are the descriptions for the default conversion functions.  */
39 static struct __gconv_step to_wc =
40 {
41   .__shlib_handle = NULL,
42   .__modname = NULL,
43   .__counter = INT_MAX,
44   .__from_name = "ANSI_X3.4-1968//",
45   .__to_name = "INTERNAL",
46   .__fct = __gconv_transform_ascii_internal,
47   .__init_fct = NULL,
48   .__end_fct = NULL,
49   .__min_needed_from = 1,
50   .__max_needed_from = 1,
51   .__min_needed_to = 4,
52   .__max_needed_to = 4,
53   .__stateful = 0,
54   .__data = NULL
55 };
56
57 static struct __gconv_step to_mb =
58 {
59   .__shlib_handle = NULL,
60   .__modname = NULL,
61   .__counter = INT_MAX,
62   .__from_name = "INTERNAL",
63   .__to_name = "ANSI_X3.4-1968//",
64   .__fct = __gconv_transform_internal_ascii,
65   .__init_fct = NULL,
66   .__end_fct = NULL,
67   .__min_needed_from = 4,
68   .__max_needed_from = 4,
69   .__min_needed_to = 1,
70   .__max_needed_to = 1,
71   .__stateful = 0,
72   .__data = NULL
73 };
74
75
76 /* For the default locale we only have to handle ANSI_X3.4-1968.  */
77 struct gconv_fcts __wcsmbs_gconv_fcts =
78 {
79   .towc = &to_wc,
80   .tomb = &to_mb
81 };
82
83
84 static inline struct __gconv_step *
85 getfct (const char *to, const char *from)
86 {
87   size_t nsteps;
88   struct __gconv_step *result;
89   size_t nstateful;
90   size_t cnt;
91
92   if (__gconv_find_transform (to, from, &result, &nsteps, 0) != __GCONV_OK)
93     /* Loading the conversion step is not possible.  */
94     return NULL;
95
96   /* Count the number of stateful conversions.  Since we will only
97      have one 'mbstate_t' object available we can only deal with one
98      stateful conversion.  */
99   nstateful = 0;
100   for (cnt = 0; cnt < nsteps; ++cnt)
101     if (result[cnt].__stateful)
102       ++nstateful;
103   if (nstateful > 1)
104     {
105       /* We cannot handle this case.  */
106       __gconv_close_transform (result, nsteps);
107       result = NULL;
108     }
109
110   return result;
111 }
112
113
114 /* Extract from the given locale name the character set portion.  Since
115    only the XPG form of the name includes this information we don't have
116    to take care for the CEN form.  */
117 #define extract_charset_name(str) \
118   ({                                                                          \
119     const char *cp = str;                                                     \
120     char *result = NULL;                                                      \
121                                                                               \
122     cp += strcspn (cp, "@.+,");                                               \
123     if (*cp == '.')                                                           \
124       {                                                                       \
125         const char *endp = ++cp;                                              \
126         while (*endp != '\0' && *endp != '@')                                 \
127           ++endp;                                                             \
128         if (endp != cp)                                                       \
129           result = strndupa (cp, endp - cp);                                  \
130       }                                                                       \
131     result;                                                                   \
132   })
133
134
135 /* We must modify global data.  */
136 __libc_lock_define_initialized (static, lock)
137
138
139 /* Load conversion functions for the currently selected locale.  */
140 void
141 internal_function
142 __wcsmbs_load_conv (const struct locale_data *new_category)
143 {
144   /* Acquire the lock.  */
145   __libc_lock_lock (lock);
146
147   /* We should repeat the test since while we waited some other thread
148      might have run this function.  */
149   if (__wcsmbs_last_locale != new_category)
150     {
151       if (new_category->name == _nl_C_name)     /* Yes, pointer comparison.  */
152         {
153         failed:
154           __wcsmbs_gconv_fcts.towc = &to_wc;
155           __wcsmbs_gconv_fcts.tomb = &to_mb;
156         }
157       else
158         {
159           /* We must find the real functions.  */
160           const char *charset_name;
161           const char *complete_name;
162           struct __gconv_step *new_towc;
163           struct __gconv_step *new_tomb;
164
165           /* Free the old conversions.  */
166           __gconv_close_transform (__wcsmbs_gconv_fcts.tomb, 1);
167           __gconv_close_transform (__wcsmbs_gconv_fcts.towc, 1);
168
169           /* Get name of charset of the locale.  */
170           charset_name = new_category->values[_NL_ITEM_INDEX(CODESET)].string;
171
172           /* Normalize the name and add the slashes necessary for a
173              complete lookup.  */
174           complete_name = norm_add_slashes (charset_name);
175
176           new_towc = getfct ("INTERNAL", complete_name);
177           if (new_towc != NULL)
178             new_tomb = getfct (complete_name, "INTERNAL");
179
180           /* If any of the conversion functions is not available we don't
181              use any since this would mean we cannot convert back and
182              forth.*/
183           if (new_towc == NULL || new_tomb == NULL)
184             {
185               if (new_towc != NULL)
186                 __gconv_close_transform (new_towc, 1);
187
188               goto failed;
189             }
190
191           __wcsmbs_gconv_fcts.tomb = new_tomb;
192           __wcsmbs_gconv_fcts.towc = new_towc;
193         }
194
195       /* Set last-used variable for current locale.  */
196       __wcsmbs_last_locale = new_category;
197     }
198
199   __libc_lock_unlock (lock);
200 }
201
202
203 /* Clone the current conversion function set.  */
204 void
205 internal_function
206 __wcsmbs_clone_conv (struct gconv_fcts *copy)
207 {
208   /* First make sure the function table is up-to-date.  */
209   update_conversion_ptrs ();
210
211   /* Make sure the data structures remain the same until we are finished.  */
212   __libc_lock_lock (lock);
213
214   /* Copy the data.  */
215   *copy = __wcsmbs_gconv_fcts;
216
217   /* Now increment the usage counters.  */
218   if (copy->towc->__shlib_handle != NULL)
219     ++copy->towc->__counter;
220   if (copy->tomb->__shlib_handle != NULL)
221     ++copy->tomb->__counter;
222
223   __libc_lock_unlock (lock);
224 }
225
226
227 /* Clone the current conversion function set.  */
228 int
229 internal_function
230 __wcsmbs_named_conv (struct gconv_fcts *copy, const char *name)
231 {
232   copy->towc = getfct ("INTERNAL", name);
233   if (copy->towc != NULL)
234     {
235       copy->tomb = getfct (name, "INTERNAL");
236       if (copy->tomb == NULL)
237         __gconv_close_transform (copy->towc, 1);
238     }
239
240   if (copy->towc == NULL || copy->tomb == NULL)
241     return 1;
242
243   /* Now increment the usage counters.  */
244   if (copy->towc->__shlib_handle != NULL)
245     ++copy->towc->__counter;
246   if (copy->tomb->__shlib_handle != NULL)
247     ++copy->tomb->__counter;
248
249   return 0;
250 }