mbsnrtowcs.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (C) 1996-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 <dlfcn.h>
  16. #include <errno.h>
  17. #include <gconv.h>
  18. #include <string.h>
  19. #include <wchar.h>
  20. #include <wcsmbsload.h>
  21. #include <pointer_guard.h>
  22. #ifndef EILSEQ
  23. # define EILSEQ EINVAL
  24. #endif
  25. /* This is the private state used if PS is NULL. */
  26. static mbstate_t state;
  27. /* This is a non-standard function but it is very useful in the
  28. implementation of stdio because we have to deal with unterminated
  29. buffers. At most NMC bytes will be converted. */
  30. size_t
  31. __mbsnrtowcs (wchar_t *dst, const char **src, size_t nmc, size_t len,
  32. mbstate_t *ps)
  33. {
  34. const unsigned char *srcend;
  35. struct __gconv_step_data data;
  36. size_t result;
  37. int status;
  38. struct __gconv_step *towc;
  39. size_t dummy;
  40. const struct gconv_fcts *fcts;
  41. /* Tell where we want the result. */
  42. data.__invocation_counter = 0;
  43. data.__internal_use = 1;
  44. data.__flags = __GCONV_IS_LAST;
  45. data.__statep = ps ?: &state;
  46. if (nmc == 0)
  47. return 0;
  48. srcend = (const unsigned char *) *src + __strnlen (*src, nmc - 1) + 1;
  49. /* Get the conversion functions. */
  50. fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
  51. /* Get the structure with the function pointers. */
  52. towc = fcts->towc;
  53. __gconv_fct fct = towc->__fct;
  54. if (towc->__shlib_handle != NULL)
  55. PTR_DEMANGLE (fct);
  56. /* We have to handle DST == NULL special. */
  57. if (dst == NULL)
  58. {
  59. mbstate_t temp_state;
  60. wchar_t buf[64]; /* Just an arbitrary size. */
  61. const unsigned char *inbuf = (const unsigned char *) *src;
  62. temp_state = *data.__statep;
  63. data.__statep = &temp_state;
  64. result = 0;
  65. data.__outbufend = (unsigned char *) buf + sizeof (buf);
  66. do
  67. {
  68. data.__outbuf = (unsigned char *) buf;
  69. status = DL_CALL_FCT (fct, (towc, &data, &inbuf, srcend, NULL,
  70. &dummy, 0, 1));
  71. result += (wchar_t *) data.__outbuf - buf;
  72. }
  73. while (status == __GCONV_FULL_OUTPUT);
  74. if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  75. && ((wchar_t *) data.__outbuf)[-1] == L'\0')
  76. /* Don't count the NUL character in. */
  77. --result;
  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. data.__outbuf = (unsigned char *) dst;
  85. data.__outbufend = data.__outbuf + len * sizeof (wchar_t);
  86. status = DL_CALL_FCT (fct,
  87. (towc, &data, (const unsigned char **) src, srcend,
  88. NULL, &dummy, 0, 1));
  89. result = (wchar_t *) data.__outbuf - dst;
  90. /* We have to determine whether the last character converted
  91. is the NUL character. */
  92. if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  93. && (assert (result > 0),
  94. ((wchar_t *) dst)[result - 1] == L'\0'))
  95. {
  96. assert (__mbsinit (data.__statep));
  97. *src = NULL;
  98. --result;
  99. }
  100. }
  101. /* There must not be any problems with the conversion but illegal input
  102. characters. */
  103. assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
  104. || status == __GCONV_ILLEGAL_INPUT
  105. || status == __GCONV_INCOMPLETE_INPUT
  106. || status == __GCONV_FULL_OUTPUT);
  107. if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
  108. && status != __GCONV_EMPTY_INPUT && status != __GCONV_INCOMPLETE_INPUT)
  109. {
  110. result = (size_t) -1;
  111. __set_errno (EILSEQ);
  112. }
  113. return result;
  114. }
  115. weak_alias (__mbsnrtowcs, mbsnrtowcs)