mbsnrtowcs.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Copyright (C) 1996-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <dlfcn.h>
  17. #include <errno.h>
  18. #include <gconv.h>
  19. #include <string.h>
  20. #include <wchar.h>
  21. #include <wcsmbsload.h>
  22. #include <sysdep.h>
  23. #ifndef EILSEQ
  24. # define EILSEQ EINVAL
  25. #endif
  26. /* This is the private state used if PS is NULL. */
  27. static mbstate_t state;
  28. /* This is a non-standard function but it is very useful in the
  29. implementation of stdio because we have to deal with unterminated
  30. buffers. At most NMC bytes will be converted. */
  31. size_t
  32. __mbsnrtowcs (wchar_t *dst, const char **src, size_t nmc, size_t len,
  33. mbstate_t *ps)
  34. {
  35. const unsigned char *srcend;
  36. struct __gconv_step_data data;
  37. size_t result;
  38. int status;
  39. struct __gconv_step *towc;
  40. size_t dummy;
  41. const struct gconv_fcts *fcts;
  42. /* Tell where we want the result. */
  43. data.__invocation_counter = 0;
  44. data.__internal_use = 1;
  45. data.__flags = __GCONV_IS_LAST;
  46. data.__statep = ps ?: &state;
  47. if (nmc == 0)
  48. return 0;
  49. srcend = (const unsigned char *) *src + __strnlen (*src, nmc - 1) + 1;
  50. /* Get the conversion functions. */
  51. fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
  52. /* Get the structure with the function pointers. */
  53. towc = fcts->towc;
  54. __gconv_fct fct = towc->__fct;
  55. #ifdef PTR_DEMANGLE
  56. if (towc->__shlib_handle != NULL)
  57. PTR_DEMANGLE (fct);
  58. #endif
  59. /* We have to handle DST == NULL special. */
  60. if (dst == NULL)
  61. {
  62. mbstate_t temp_state;
  63. wchar_t buf[64]; /* Just an arbitrary size. */
  64. const unsigned char *inbuf = (const unsigned char *) *src;
  65. temp_state = *data.__statep;
  66. data.__statep = &temp_state;
  67. result = 0;
  68. data.__outbufend = (unsigned char *) buf + sizeof (buf);
  69. do
  70. {
  71. data.__outbuf = (unsigned char *) buf;
  72. status = DL_CALL_FCT (fct, (towc, &data, &inbuf, srcend, NULL,
  73. &dummy, 0, 1));
  74. result += (wchar_t *) data.__outbuf - buf;
  75. }
  76. while (status == __GCONV_FULL_OUTPUT);
  77. if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  78. && ((wchar_t *) data.__outbuf)[-1] == L'\0')
  79. /* Don't count the NUL character in. */
  80. --result;
  81. }
  82. else
  83. {
  84. /* This code is based on the safe assumption that all internal
  85. multi-byte encodings use the NUL byte only to mark the end
  86. of the string. */
  87. data.__outbuf = (unsigned char *) dst;
  88. data.__outbufend = data.__outbuf + len * sizeof (wchar_t);
  89. status = DL_CALL_FCT (fct,
  90. (towc, &data, (const unsigned char **) src, srcend,
  91. NULL, &dummy, 0, 1));
  92. result = (wchar_t *) data.__outbuf - dst;
  93. /* We have to determine whether the last character converted
  94. is the NUL character. */
  95. if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  96. && (assert (result > 0),
  97. ((wchar_t *) dst)[result - 1] == L'\0'))
  98. {
  99. assert (__mbsinit (data.__statep));
  100. *src = NULL;
  101. --result;
  102. }
  103. }
  104. /* There must not be any problems with the conversion but illegal input
  105. characters. */
  106. assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
  107. || status == __GCONV_ILLEGAL_INPUT
  108. || status == __GCONV_INCOMPLETE_INPUT
  109. || status == __GCONV_FULL_OUTPUT);
  110. if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
  111. && status != __GCONV_EMPTY_INPUT && status != __GCONV_INCOMPLETE_INPUT)
  112. {
  113. result = (size_t) -1;
  114. __set_errno (EILSEQ);
  115. }
  116. return result;
  117. }
  118. weak_alias (__mbsnrtowcs, mbsnrtowcs)