wcsrtombs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <stdlib.h>
  18. #include <gconv.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. size_t
  28. __wcsrtombs (char *dst, const wchar_t **src, size_t len, mbstate_t *ps)
  29. {
  30. struct __gconv_step_data data;
  31. int status;
  32. size_t result;
  33. struct __gconv_step *tomb;
  34. const struct gconv_fcts *fcts;
  35. /* Tell where we want the result. */
  36. data.__invocation_counter = 0;
  37. data.__internal_use = 1;
  38. data.__flags = __GCONV_IS_LAST;
  39. data.__statep = ps ?: &state;
  40. /* Get the conversion functions. */
  41. fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
  42. /* Get the structure with the function pointers. */
  43. tomb = fcts->tomb;
  44. __gconv_fct fct = tomb->__fct;
  45. if (tomb->__shlib_handle != NULL)
  46. PTR_DEMANGLE (fct);
  47. /* We have to handle DST == NULL special. */
  48. if (dst == NULL)
  49. {
  50. mbstate_t temp_state;
  51. unsigned char buf[256]; /* Just an arbitrary value. */
  52. const wchar_t *srcend = *src + __wcslen (*src) + 1;
  53. const unsigned char *inbuf = (const unsigned char *) *src;
  54. size_t dummy;
  55. temp_state = *data.__statep;
  56. data.__statep = &temp_state;
  57. result = 0;
  58. data.__outbufend = buf + sizeof (buf);
  59. do
  60. {
  61. data.__outbuf = buf;
  62. status = DL_CALL_FCT (fct, (tomb, &data, &inbuf,
  63. (const unsigned char *) srcend, NULL,
  64. &dummy, 0, 1));
  65. /* Count the number of bytes. */
  66. result += data.__outbuf - buf;
  67. }
  68. while (status == __GCONV_FULL_OUTPUT);
  69. if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  70. {
  71. /* There better should be a NUL byte at the end. */
  72. assert (data.__outbuf[-1] == '\0');
  73. /* Don't count the NUL character in. */
  74. --result;
  75. }
  76. }
  77. else
  78. {
  79. /* This code is based on the safe assumption that all internal
  80. multi-byte encodings use the NUL byte only to mark the end
  81. of the string. */
  82. const wchar_t *srcend = *src + __wcsnlen (*src, len) + 1;
  83. size_t dummy;
  84. data.__outbuf = (unsigned char *) dst;
  85. data.__outbufend = (unsigned char *) dst + len;
  86. status = DL_CALL_FCT (fct, (tomb, &data, (const unsigned char **) src,
  87. (const unsigned char *) srcend, NULL,
  88. &dummy, 0, 1));
  89. /* Count the number of bytes. */
  90. result = data.__outbuf - (unsigned char *) dst;
  91. /* We have to determine whether the last character converted
  92. is the NUL character. */
  93. if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  94. && data.__outbuf[-1] == '\0')
  95. {
  96. assert (data.__outbuf != (unsigned char *) dst);
  97. assert (__mbsinit (data.__statep));
  98. *src = NULL;
  99. --result;
  100. }
  101. }
  102. /* There must not be any problems with the conversion but illegal input
  103. characters. */
  104. assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
  105. || status == __GCONV_ILLEGAL_INPUT
  106. || status == __GCONV_INCOMPLETE_INPUT
  107. || status == __GCONV_FULL_OUTPUT);
  108. if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
  109. && status != __GCONV_EMPTY_INPUT)
  110. {
  111. result = (size_t) -1;
  112. __set_errno (EILSEQ);
  113. }
  114. return result;
  115. }
  116. weak_alias (__wcsrtombs, wcsrtombs)