1 /* Copyright (C) 1998, 1999, 2000, 2001 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 Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the 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 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with the GNU C Library; if not, write to the Free 17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 18 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 = (char *) "ANSI_X3.4-1968//TRANSLIT", 45 .__to_name = (char *) "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 = (char *) "INTERNAL", 63 .__to_name = (char *) "ANSI_X3.4-1968//TRANSLIT", 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 .towc_nsteps = 1, 81 .tomb = &to_mb, 82 .tomb_nsteps = 1 83 }; 84 85 86 static inline struct __gconv_step * 87 getfct (const char *to, const char *from, size_t *nstepsp) 88 { 89 size_t nsteps; 90 struct __gconv_step *result; 91 #if 0 92 size_t nstateful; 93 size_t cnt; 94 #endif 95 96 if (__gconv_find_transform (to, from, &result, &nsteps, 0) != __GCONV_OK) 97 /* Loading the conversion step is not possible. */ 98 return NULL; 99 100 /* Maybe it is someday necessary to allow more than one step. 101 Currently this is not the case since the conversions handled here 102 are from and to INTERNAL and there always is a converted for 103 that. It the directly following code is enabled the libio 104 functions will have to allocate appropriate __gconv_step_data 105 elements instead of only one. */ 106 #if 0 107 /* Count the number of stateful conversions. Since we will only 108 have one 'mbstate_t' object available we can only deal with one 109 stateful conversion. */ 110 nstateful = 0; 111 for (cnt = 0; cnt < nsteps; ++cnt) 112 if (result[cnt].__stateful) 113 ++nstateful; 114 if (nstateful > 1) 115 #else 116 if (nsteps > 1) 117 #endif 118 { 119 /* We cannot handle this case. */ 120 __gconv_close_transform (result, nsteps); 121 result = NULL; 122 } 123 else 124 *nstepsp = nsteps; 125 126 return result; 127 } 128 129 130 /* Extract from the given locale name the character set portion. Since 131 only the XPG form of the name includes this information we don't have 132 to take care for the CEN form. */ 133 #define extract_charset_name(str) \ 134 ({ \ 135 const char *cp = str; \ 136 char *result = NULL; \ 137 \ 138 cp += strcspn (cp, "@.+,"); \ 139 if (*cp == '.') \ 140 { \ 141 const char *endp = ++cp; \ 142 while (*endp != '\0' && *endp != '@') \ 143 ++endp; \ 144 if (endp != cp) \ 145 result = strndupa (cp, endp - cp); \ 146 } \ 147 result; \ 148 }) 149 150 151 /* We must modify global data. */ 152 __libc_lock_define_initialized (static, lock) 153 154 155 /* Load conversion functions for the currently selected locale. */ 156 void 157 internal_function 158 __wcsmbs_load_conv (const struct locale_data *new_category) 159 { 160 /* Acquire the lock. */ 161 __libc_lock_lock (lock); 162 163 /* We should repeat the test since while we waited some other thread 164 might have run this function. */ 165 if (__builtin_expect (__wcsmbs_last_locale != new_category, 1)) 166 { 167 if (new_category->name == _nl_C_name) /* Yes, pointer comparison. */ 168 { 169 failed: 170 __wcsmbs_gconv_fcts.towc = &to_wc; 171 __wcsmbs_gconv_fcts.tomb = &to_mb; 172 } 173 else 174 { 175 /* We must find the real functions. */ 176 const char *charset_name; 177 const char *complete_name; 178 struct __gconv_step *new_towc; 179 size_t new_towc_nsteps; 180 struct __gconv_step *new_tomb; 181 size_t new_tomb_nsteps; 182 int use_translit; 183 184 /* Free the old conversions. */ 185 if (__wcsmbs_gconv_fcts.tomb != &to_mb) 186 __gconv_close_transform (__wcsmbs_gconv_fcts.tomb, 187 __wcsmbs_gconv_fcts.tomb_nsteps); 188 if (__wcsmbs_gconv_fcts.towc != &to_wc) 189 __gconv_close_transform (__wcsmbs_gconv_fcts.towc, 190 __wcsmbs_gconv_fcts.towc_nsteps); 191 192 /* Get name of charset of the locale. */ 193 charset_name = new_category->values[_NL_ITEM_INDEX(CODESET)].string; 194 195 /* Does the user want transliteration? */ 196 use_translit = new_category->use_translit; 197 198 /* Normalize the name and add the slashes necessary for a 199 complete lookup. */ 200 complete_name = norm_add_slashes (charset_name, 201 use_translit ? "TRANSLIT" : NULL); 202 203 /* It is not necessary to use transliteration in this direction 204 since the internal character set is supposed to be able to 205 represent all others. */ 206 new_towc = getfct ("INTERNAL", complete_name, &new_towc_nsteps); 207 new_tomb = (new_towc != NULL 208 ? getfct (complete_name, "INTERNAL", &new_tomb_nsteps) 209 : NULL); 210 211 /* If any of the conversion functions is not available we don't 212 use any since this would mean we cannot convert back and 213 forth.*/ 214 if (new_towc == NULL || new_tomb == NULL) 215 { 216 if (new_towc != NULL) 217 __gconv_close_transform (new_towc, 1); 218 219 goto failed; 220 } 221 222 __wcsmbs_gconv_fcts.tomb = new_tomb; 223 __wcsmbs_gconv_fcts.tomb_nsteps = new_tomb_nsteps; 224 __wcsmbs_gconv_fcts.towc = new_towc; 225 __wcsmbs_gconv_fcts.towc_nsteps = new_towc_nsteps; 226 } 227 228 /* Set last-used variable for current locale. */ 229 __wcsmbs_last_locale = new_category; 230 } 231 232 __libc_lock_unlock (lock); 233 } 234 235 236 /* Clone the current conversion function set. */ 237 void 238 internal_function 239 __wcsmbs_clone_conv (struct gconv_fcts *copy) 240 { 241 /* First make sure the function table is up-to-date. */ 242 update_conversion_ptrs (); 243 244 /* Make sure the data structures remain the same until we are finished. */ 245 __libc_lock_lock (lock); 246 247 /* Copy the data. */ 248 *copy = __wcsmbs_gconv_fcts; 249 250 /* Now increment the usage counters. */ 251 if (copy->towc->__shlib_handle != NULL) 252 ++copy->towc->__counter; 253 if (copy->tomb->__shlib_handle != NULL) 254 ++copy->tomb->__counter; 255 256 __libc_lock_unlock (lock); 257 } 258 259 260 /* Get converters for named charset. */ 261 int 262 internal_function 263 __wcsmbs_named_conv (struct gconv_fcts *copy, const char *name) 264 { 265 copy->towc = getfct ("INTERNAL", name, ©->towc_nsteps); 266 if (copy->towc != NULL) 267 { 268 copy->tomb = getfct (name, "INTERNAL", ©->tomb_nsteps); 269 if (copy->tomb == NULL) 270 __gconv_close_transform (copy->towc, copy->towc_nsteps); 271 } 272 273 return copy->towc == NULL || copy->tomb == NULL ? 1 : 0; 274 } 275 276 277 /* Free all resources if necessary. */ 278 static void __attribute__ ((unused)) 279 free_mem (void) 280 { 281 if (__wcsmbs_gconv_fcts.tomb != &to_mb) 282 { 283 struct __gconv_step *old = __wcsmbs_gconv_fcts.tomb; 284 size_t nold = __wcsmbs_gconv_fcts.tomb_nsteps; 285 __wcsmbs_gconv_fcts.tomb = &to_mb; 286 __wcsmbs_gconv_fcts.tomb_nsteps = 1; 287 __gconv_release_cache (old, nold); 288 } 289 290 if (__wcsmbs_gconv_fcts.towc != &to_wc) 291 { 292 struct __gconv_step *old = __wcsmbs_gconv_fcts.towc; 293 size_t nold = __wcsmbs_gconv_fcts.towc_nsteps; 294 __wcsmbs_gconv_fcts.towc = &to_wc; 295 __wcsmbs_gconv_fcts.towc_nsteps = 1; 296 __gconv_release_cache (old, nold); 297 } 298 } 299 300 301 text_set_element (__libc_subfreeres, free_mem); 302