iofwide.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* Copyright (C) 1999-2019 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. <http://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include <libioP.h>
  23. #include <dlfcn.h>
  24. #include <wchar.h>
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <langinfo.h>
  29. #include <locale/localeinfo.h>
  30. #include <wcsmbs/wcsmbsload.h>
  31. #include <iconv/gconv_int.h>
  32. #include <shlib-compat.h>
  33. #include <sysdep.h>
  34. /* Prototypes of libio's codecvt functions. */
  35. static enum __codecvt_result do_out (struct _IO_codecvt *codecvt,
  36. __mbstate_t *statep,
  37. const wchar_t *from_start,
  38. const wchar_t *from_end,
  39. const wchar_t **from_stop, char *to_start,
  40. char *to_end, char **to_stop);
  41. static enum __codecvt_result do_unshift (struct _IO_codecvt *codecvt,
  42. __mbstate_t *statep, char *to_start,
  43. char *to_end, char **to_stop);
  44. static enum __codecvt_result do_in (struct _IO_codecvt *codecvt,
  45. __mbstate_t *statep,
  46. const char *from_start,
  47. const char *from_end,
  48. const char **from_stop, wchar_t *to_start,
  49. wchar_t *to_end, wchar_t **to_stop);
  50. static int do_encoding (struct _IO_codecvt *codecvt);
  51. static int do_length (struct _IO_codecvt *codecvt, __mbstate_t *statep,
  52. const char *from_start,
  53. const char *from_end, size_t max);
  54. static int do_max_length (struct _IO_codecvt *codecvt);
  55. static int do_always_noconv (struct _IO_codecvt *codecvt);
  56. /* The functions used in `codecvt' for libio are always the same. */
  57. const struct _IO_codecvt __libio_codecvt =
  58. {
  59. .__codecvt_destr = NULL, /* Destructor, never used. */
  60. .__codecvt_do_out = do_out,
  61. .__codecvt_do_unshift = do_unshift,
  62. .__codecvt_do_in = do_in,
  63. .__codecvt_do_encoding = do_encoding,
  64. .__codecvt_do_always_noconv = do_always_noconv,
  65. .__codecvt_do_length = do_length,
  66. .__codecvt_do_max_length = do_max_length
  67. };
  68. /* Return orientation of stream. If mode is nonzero try to change
  69. the orientation first. */
  70. #undef _IO_fwide
  71. int
  72. _IO_fwide (FILE *fp, int mode)
  73. {
  74. /* Normalize the value. */
  75. mode = mode < 0 ? -1 : (mode == 0 ? 0 : 1);
  76. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
  77. if (__builtin_expect (&_IO_stdin_used == NULL, 0)
  78. && (fp == _IO_stdin || fp == _IO_stdout || fp == _IO_stderr))
  79. /* This is for a stream in the glibc 2.0 format. */
  80. return -1;
  81. #endif
  82. /* The orientation already has been determined. */
  83. if (fp->_mode != 0
  84. /* Or the caller simply wants to know about the current orientation. */
  85. || mode == 0)
  86. return fp->_mode;
  87. /* Set the orientation appropriately. */
  88. if (mode > 0)
  89. {
  90. struct _IO_codecvt *cc = fp->_codecvt = &fp->_wide_data->_codecvt;
  91. fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end;
  92. fp->_wide_data->_IO_write_ptr = fp->_wide_data->_IO_write_base;
  93. /* Get the character conversion functions based on the currently
  94. selected locale for LC_CTYPE. */
  95. {
  96. /* Clear the state. We start all over again. */
  97. memset (&fp->_wide_data->_IO_state, '\0', sizeof (__mbstate_t));
  98. memset (&fp->_wide_data->_IO_last_state, '\0', sizeof (__mbstate_t));
  99. struct gconv_fcts fcts;
  100. __wcsmbs_clone_conv (&fcts);
  101. assert (fcts.towc_nsteps == 1);
  102. assert (fcts.tomb_nsteps == 1);
  103. /* The functions are always the same. */
  104. *cc = __libio_codecvt;
  105. cc->__cd_in.__cd.__nsteps = fcts.towc_nsteps;
  106. cc->__cd_in.__cd.__steps = fcts.towc;
  107. cc->__cd_in.__cd.__data[0].__invocation_counter = 0;
  108. cc->__cd_in.__cd.__data[0].__internal_use = 1;
  109. cc->__cd_in.__cd.__data[0].__flags = __GCONV_IS_LAST;
  110. cc->__cd_in.__cd.__data[0].__statep = &fp->_wide_data->_IO_state;
  111. cc->__cd_out.__cd.__nsteps = fcts.tomb_nsteps;
  112. cc->__cd_out.__cd.__steps = fcts.tomb;
  113. cc->__cd_out.__cd.__data[0].__invocation_counter = 0;
  114. cc->__cd_out.__cd.__data[0].__internal_use = 1;
  115. cc->__cd_out.__cd.__data[0].__flags
  116. = __GCONV_IS_LAST | __GCONV_TRANSLIT;
  117. cc->__cd_out.__cd.__data[0].__statep = &fp->_wide_data->_IO_state;
  118. }
  119. /* From now on use the wide character callback functions. */
  120. _IO_JUMPS_FILE_plus (fp) = fp->_wide_data->_wide_vtable;
  121. }
  122. /* Set the mode now. */
  123. fp->_mode = mode;
  124. return mode;
  125. }
  126. static enum __codecvt_result
  127. do_out (struct _IO_codecvt *codecvt, __mbstate_t *statep,
  128. const wchar_t *from_start, const wchar_t *from_end,
  129. const wchar_t **from_stop, char *to_start, char *to_end,
  130. char **to_stop)
  131. {
  132. enum __codecvt_result result;
  133. struct __gconv_step *gs = codecvt->__cd_out.__cd.__steps;
  134. int status;
  135. size_t dummy;
  136. const unsigned char *from_start_copy = (unsigned char *) from_start;
  137. codecvt->__cd_out.__cd.__data[0].__outbuf = (unsigned char *) to_start;
  138. codecvt->__cd_out.__cd.__data[0].__outbufend = (unsigned char *) to_end;
  139. codecvt->__cd_out.__cd.__data[0].__statep = statep;
  140. __gconv_fct fct = gs->__fct;
  141. #ifdef PTR_DEMANGLE
  142. if (gs->__shlib_handle != NULL)
  143. PTR_DEMANGLE (fct);
  144. #endif
  145. status = DL_CALL_FCT (fct,
  146. (gs, codecvt->__cd_out.__cd.__data, &from_start_copy,
  147. (const unsigned char *) from_end, NULL,
  148. &dummy, 0, 0));
  149. *from_stop = (wchar_t *) from_start_copy;
  150. *to_stop = (char *) codecvt->__cd_out.__cd.__data[0].__outbuf;
  151. switch (status)
  152. {
  153. case __GCONV_OK:
  154. case __GCONV_EMPTY_INPUT:
  155. result = __codecvt_ok;
  156. break;
  157. case __GCONV_FULL_OUTPUT:
  158. case __GCONV_INCOMPLETE_INPUT:
  159. result = __codecvt_partial;
  160. break;
  161. default:
  162. result = __codecvt_error;
  163. break;
  164. }
  165. return result;
  166. }
  167. static enum __codecvt_result
  168. do_unshift (struct _IO_codecvt *codecvt, __mbstate_t *statep,
  169. char *to_start, char *to_end, char **to_stop)
  170. {
  171. enum __codecvt_result result;
  172. struct __gconv_step *gs = codecvt->__cd_out.__cd.__steps;
  173. int status;
  174. size_t dummy;
  175. codecvt->__cd_out.__cd.__data[0].__outbuf = (unsigned char *) to_start;
  176. codecvt->__cd_out.__cd.__data[0].__outbufend = (unsigned char *) to_end;
  177. codecvt->__cd_out.__cd.__data[0].__statep = statep;
  178. __gconv_fct fct = gs->__fct;
  179. #ifdef PTR_DEMANGLE
  180. if (gs->__shlib_handle != NULL)
  181. PTR_DEMANGLE (fct);
  182. #endif
  183. status = DL_CALL_FCT (fct,
  184. (gs, codecvt->__cd_out.__cd.__data, NULL, NULL,
  185. NULL, &dummy, 1, 0));
  186. *to_stop = (char *) codecvt->__cd_out.__cd.__data[0].__outbuf;
  187. switch (status)
  188. {
  189. case __GCONV_OK:
  190. case __GCONV_EMPTY_INPUT:
  191. result = __codecvt_ok;
  192. break;
  193. case __GCONV_FULL_OUTPUT:
  194. case __GCONV_INCOMPLETE_INPUT:
  195. result = __codecvt_partial;
  196. break;
  197. default:
  198. result = __codecvt_error;
  199. break;
  200. }
  201. return result;
  202. }
  203. static enum __codecvt_result
  204. do_in (struct _IO_codecvt *codecvt, __mbstate_t *statep,
  205. const char *from_start, const char *from_end, const char **from_stop,
  206. wchar_t *to_start, wchar_t *to_end, wchar_t **to_stop)
  207. {
  208. enum __codecvt_result result;
  209. struct __gconv_step *gs = codecvt->__cd_in.__cd.__steps;
  210. int status;
  211. size_t dummy;
  212. const unsigned char *from_start_copy = (unsigned char *) from_start;
  213. codecvt->__cd_in.__cd.__data[0].__outbuf = (unsigned char *) to_start;
  214. codecvt->__cd_in.__cd.__data[0].__outbufend = (unsigned char *) to_end;
  215. codecvt->__cd_in.__cd.__data[0].__statep = statep;
  216. __gconv_fct fct = gs->__fct;
  217. #ifdef PTR_DEMANGLE
  218. if (gs->__shlib_handle != NULL)
  219. PTR_DEMANGLE (fct);
  220. #endif
  221. status = DL_CALL_FCT (fct,
  222. (gs, codecvt->__cd_in.__cd.__data, &from_start_copy,
  223. (const unsigned char *) from_end, NULL,
  224. &dummy, 0, 0));
  225. *from_stop = (const char *) from_start_copy;
  226. *to_stop = (wchar_t *) codecvt->__cd_in.__cd.__data[0].__outbuf;
  227. switch (status)
  228. {
  229. case __GCONV_OK:
  230. case __GCONV_EMPTY_INPUT:
  231. result = __codecvt_ok;
  232. break;
  233. case __GCONV_FULL_OUTPUT:
  234. case __GCONV_INCOMPLETE_INPUT:
  235. result = __codecvt_partial;
  236. break;
  237. default:
  238. result = __codecvt_error;
  239. break;
  240. }
  241. return result;
  242. }
  243. static int
  244. do_encoding (struct _IO_codecvt *codecvt)
  245. {
  246. /* See whether the encoding is stateful. */
  247. if (codecvt->__cd_in.__cd.__steps[0].__stateful)
  248. return -1;
  249. /* Fortunately not. Now determine the input bytes for the conversion
  250. necessary for each wide character. */
  251. if (codecvt->__cd_in.__cd.__steps[0].__min_needed_from
  252. != codecvt->__cd_in.__cd.__steps[0].__max_needed_from)
  253. /* Not a constant value. */
  254. return 0;
  255. return codecvt->__cd_in.__cd.__steps[0].__min_needed_from;
  256. }
  257. static int
  258. do_always_noconv (struct _IO_codecvt *codecvt)
  259. {
  260. return 0;
  261. }
  262. static int
  263. do_length (struct _IO_codecvt *codecvt, __mbstate_t *statep,
  264. const char *from_start, const char *from_end, size_t max)
  265. {
  266. int result;
  267. const unsigned char *cp = (const unsigned char *) from_start;
  268. wchar_t to_buf[max];
  269. struct __gconv_step *gs = codecvt->__cd_in.__cd.__steps;
  270. size_t dummy;
  271. codecvt->__cd_in.__cd.__data[0].__outbuf = (unsigned char *) to_buf;
  272. codecvt->__cd_in.__cd.__data[0].__outbufend = (unsigned char *) &to_buf[max];
  273. codecvt->__cd_in.__cd.__data[0].__statep = statep;
  274. __gconv_fct fct = gs->__fct;
  275. #ifdef PTR_DEMANGLE
  276. if (gs->__shlib_handle != NULL)
  277. PTR_DEMANGLE (fct);
  278. #endif
  279. DL_CALL_FCT (fct,
  280. (gs, codecvt->__cd_in.__cd.__data, &cp,
  281. (const unsigned char *) from_end, NULL,
  282. &dummy, 0, 0));
  283. result = cp - (const unsigned char *) from_start;
  284. return result;
  285. }
  286. static int
  287. do_max_length (struct _IO_codecvt *codecvt)
  288. {
  289. return codecvt->__cd_in.__cd.__steps[0].__max_needed_from;
  290. }