mbsrtowcs_l.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (C) 2002-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 <assert.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include "wcsmbsload.h"
  18. #include <dlfcn.h>
  19. #include <errno.h>
  20. #include <gconv.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <wchar.h>
  24. #include <wcsmbsload.h>
  25. #include <pointer_guard.h>
  26. #ifndef EILSEQ
  27. # define EILSEQ EINVAL
  28. #endif
  29. size_t
  30. attribute_hidden
  31. __mbsrtowcs_l (wchar_t *dst, const char **src, size_t len, mbstate_t *ps,
  32. locale_t l)
  33. {
  34. struct __gconv_step_data data;
  35. size_t result;
  36. int status;
  37. struct __gconv_step *towc;
  38. size_t non_reversible;
  39. const struct gconv_fcts *fcts;
  40. /* Tell where we want the result. */
  41. data.__invocation_counter = 0;
  42. data.__internal_use = 1;
  43. data.__flags = __GCONV_IS_LAST;
  44. data.__statep = ps;
  45. /* Get the conversion functions. */
  46. fcts = get_gconv_fcts (l->__locales[LC_CTYPE]);
  47. /* Get the structure with the function pointers. */
  48. towc = fcts->towc;
  49. __gconv_fct fct = towc->__fct;
  50. if (towc->__shlib_handle != NULL)
  51. PTR_DEMANGLE (fct);
  52. /* We have to handle DST == NULL special. */
  53. if (dst == NULL)
  54. {
  55. mbstate_t temp_state;
  56. wchar_t buf[64]; /* Just an arbitrary size. */
  57. const unsigned char *inbuf = (const unsigned char *) *src;
  58. const unsigned char *srcend = inbuf + strlen (*src) + 1;
  59. temp_state = *data.__statep;
  60. data.__statep = &temp_state;
  61. result = 0;
  62. data.__outbufend = (unsigned char *) buf + sizeof (buf);
  63. do
  64. {
  65. data.__outbuf = (unsigned char *) buf;
  66. status = DL_CALL_FCT (fct, (towc, &data, &inbuf, srcend, NULL,
  67. &non_reversible, 0, 1));
  68. result += (wchar_t *) data.__outbuf - buf;
  69. }
  70. while (status == __GCONV_FULL_OUTPUT);
  71. if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  72. {
  73. /* There better should be a NUL wide char at the end. */
  74. assert (((wchar_t *) data.__outbuf)[-1] == L'\0');
  75. /* Don't count the NUL character in. */
  76. --result;
  77. }
  78. }
  79. else
  80. {
  81. /* This code is based on the safe assumption that all internal
  82. multi-byte encodings use the NUL byte only to mark the end
  83. of the string. */
  84. const unsigned char *srcp = (const unsigned char *) *src;
  85. const unsigned char *srcend;
  86. data.__outbuf = (unsigned char *) dst;
  87. data.__outbufend = data.__outbuf + len * sizeof (wchar_t);
  88. status = __GCONV_FULL_OUTPUT;
  89. while (len > 0)
  90. {
  91. /* Pessimistic guess as to how much input we can use. In the
  92. worst case we need one input byte for one output wchar_t. */
  93. srcend = srcp + __strnlen ((const char *) srcp, len) + 1;
  94. status = DL_CALL_FCT (fct, (towc, &data, &srcp, srcend, NULL,
  95. &non_reversible, 0, 1));
  96. if ((status != __GCONV_EMPTY_INPUT
  97. && status != __GCONV_INCOMPLETE_INPUT)
  98. /* Not all input read. */
  99. || srcp != srcend
  100. /* Reached the end of the input. */
  101. || srcend[-1] == '\0')
  102. break;
  103. len = (wchar_t *) data.__outbufend - (wchar_t *) data.__outbuf;
  104. }
  105. /* Make the end if the input known to the caller. */
  106. *src = (const char *) srcp;
  107. result = (wchar_t *) data.__outbuf - dst;
  108. /* We have to determine whether the last character converted
  109. is the NUL character. */
  110. if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  111. && ((wchar_t *) dst)[result - 1] == L'\0')
  112. {
  113. assert (result > 0);
  114. assert (__mbsinit (data.__statep));
  115. *src = NULL;
  116. --result;
  117. }
  118. }
  119. /* There must not be any problems with the conversion but illegal input
  120. characters. */
  121. assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
  122. || status == __GCONV_ILLEGAL_INPUT
  123. || status == __GCONV_INCOMPLETE_INPUT
  124. || status == __GCONV_FULL_OUTPUT);
  125. if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
  126. && status != __GCONV_EMPTY_INPUT && status != __GCONV_INCOMPLETE_INPUT)
  127. {
  128. result = (size_t) -1;
  129. __set_errno (EILSEQ);
  130. }
  131. return result;
  132. }