wcsmbsload.c 7.9 KB

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