wcsnrtombs.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <wchar.h>
  19. #include <wcsmbsload.h>
  20. #include <pointer_guard.h>
  21. #ifndef EILSEQ
  22. # define EILSEQ EINVAL
  23. #endif
  24. /* This is the private state used if PS is NULL. */
  25. static mbstate_t state;
  26. /* This is a non-standard function but it is very useful in the
  27. implementation of stdio because we have to deal with unterminated
  28. buffers. At most NWC wide character will be converted. */
  29. size_t
  30. __wcsnrtombs (char *dst, const wchar_t **src, size_t nwc, size_t len,
  31. mbstate_t *ps)
  32. {
  33. struct __gconv_step_data data;
  34. const wchar_t *srcend;
  35. int status;
  36. size_t result;
  37. struct __gconv_step *tomb;
  38. const struct gconv_fcts *fcts;
  39. /* Tell where we want the result. */
  40. data.__invocation_counter = 0;
  41. data.__internal_use = 1;
  42. data.__flags = __GCONV_IS_LAST;
  43. data.__statep = ps ?: &state;
  44. if (nwc == 0)
  45. return 0;
  46. srcend = *src + __wcsnlen (*src, nwc - 1) + 1;
  47. /* Get the conversion functions. */
  48. fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
  49. /* Get the structure with the function pointers. */
  50. tomb = fcts->tomb;
  51. __gconv_fct fct = tomb->__fct;
  52. if (tomb->__shlib_handle != NULL)
  53. PTR_DEMANGLE (fct);
  54. /* We have to handle DST == NULL special. */
  55. if (dst == NULL)
  56. {
  57. mbstate_t temp_state;
  58. unsigned char buf[256]; /* Just an arbitrary value. */
  59. const unsigned char *inbuf = (const unsigned char *) *src;
  60. size_t dummy;
  61. temp_state = *data.__statep;
  62. data.__statep = &temp_state;
  63. result = 0;
  64. data.__outbufend = buf + sizeof (buf);
  65. do
  66. {
  67. data.__outbuf = buf;
  68. status = DL_CALL_FCT (fct, (tomb, &data, &inbuf,
  69. (const unsigned char *) srcend, NULL,
  70. &dummy, 0, 1));
  71. /* Count the number of bytes. */
  72. result += data.__outbuf - buf;
  73. }
  74. while (status == __GCONV_FULL_OUTPUT);
  75. if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
  76. && data.__outbuf[-1] == '\0')
  77. /* Don't count the NUL character in. */
  78. --result;
  79. }
  80. else
  81. {
  82. /* This code is based on the safe assumption that all internal
  83. multi-byte encodings use the NUL byte only to mark the end
  84. of the string. */
  85. size_t dummy;
  86. data.__outbuf = (unsigned char *) dst;
  87. data.__outbufend = (unsigned char *) dst + len;
  88. status = DL_CALL_FCT (fct, (tomb, &data, (const unsigned char **) src,
  89. (const unsigned char *) srcend, NULL,
  90. &dummy, 0, 1));
  91. /* Count the number of bytes. */
  92. result = data.__outbuf - (unsigned char *) 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. && data.__outbuf[-1] == '\0')
  97. {
  98. assert (data.__outbuf != (unsigned char *) dst);
  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)
  112. {
  113. result = (size_t) -1;
  114. __set_errno (EILSEQ);
  115. }
  116. return result;
  117. }
  118. weak_alias (__wcsnrtombs, wcsnrtombs)