multibyte_citrus_none.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* $OpenBSD: multibyte_citrus.c,v 1.4 2012/12/05 23:20:00 deraadt Exp $ */
  2. /* $NetBSD: multibyte_amd1.c,v 1.7 2009/01/11 02:46:28 christos Exp $ */
  3. /* $OpenBSD: citrus_none.c,v 1.5 2013/03/07 18:12:31 stsp Exp $ */
  4. /* $NetBSD: citrus_none.c,v 1.18 2008/06/14 16:01:07 tnozaki Exp $ */
  5. /*-
  6. * Copyright (c)2002 Citrus Project,
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/types.h>
  31. #include <errno.h>
  32. #include <limits.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <stddef.h>
  37. #include <wchar.h>
  38. #include "internal/util.h"
  39. size_t mbsnrtowcs(wchar_t *, const char **, size_t, size_t, mbstate_t *);
  40. size_t wcsnrtombs(char *, const wchar_t **, size_t, size_t, mbstate_t *);
  41. /*
  42. * Convert an unsigned char value into a char value without relying on
  43. * signed overflow behavior.
  44. */
  45. static __inline char
  46. wrapv(unsigned char ch)
  47. {
  48. if (ch >= 0x80)
  49. return ((int)ch - 0x100);
  50. else
  51. return (ch);
  52. }
  53. int
  54. mbsinit(const mbstate_t *ps)
  55. {
  56. UNUSED(ps);
  57. return (1); /* always initial state */
  58. }
  59. size_t
  60. mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
  61. {
  62. /* pwc may be NULL */
  63. /* s may be NULL */
  64. /* ps appears to be unused */
  65. UNUSED(ps);
  66. if (s == NULL)
  67. return 0;
  68. if (n == 0)
  69. return (size_t)-2;
  70. if (pwc)
  71. *pwc = (wchar_t)(unsigned char)*s;
  72. return (*s != '\0');
  73. }
  74. size_t
  75. mbsrtowcs(wchar_t *dst, const char **src, size_t len, mbstate_t *ps)
  76. {
  77. return (mbsnrtowcs(dst, src, SIZE_MAX, len, ps));
  78. }
  79. size_t
  80. mbsnrtowcs(wchar_t *dst, const char **src, size_t nmc, size_t len,
  81. mbstate_t *ps)
  82. {
  83. size_t i;
  84. /* dst may be NULL */
  85. /* ps appears to be unused */
  86. UNUSED(ps);
  87. if (dst == NULL)
  88. return strnlen(*src, nmc);
  89. for (i = 0; i < nmc && i < len; i++)
  90. if ((dst[i] = (wchar_t)(unsigned char)(*src)[i]) == L'\0') {
  91. *src = NULL;
  92. return (i);
  93. }
  94. *src += i;
  95. return (i);
  96. }
  97. size_t
  98. wcrtomb(char *s, wchar_t wc, mbstate_t *ps)
  99. {
  100. /* s may be NULL */
  101. /* ps appears to be unused */
  102. UNUSED(ps);
  103. if (s == NULL)
  104. return (1);
  105. if (wc < 0 || wc > 0xff) {
  106. errno = EILSEQ;
  107. return (-1);
  108. }
  109. *s = wrapv(wc);
  110. return (1);
  111. }
  112. size_t
  113. wcsrtombs(char *dst, const wchar_t **src, size_t len, mbstate_t *ps)
  114. {
  115. return (wcsnrtombs(dst, src, SIZE_MAX, len, ps));
  116. }
  117. size_t
  118. wcsnrtombs(char *dst, const wchar_t **src, size_t nwc, size_t len,
  119. mbstate_t *ps)
  120. {
  121. size_t i;
  122. /* dst may be NULL */
  123. /* ps appears to be unused */
  124. UNUSED(ps);
  125. if (dst == NULL) {
  126. for (i = 0; i < nwc; i++) {
  127. wchar_t wc = (*src)[i];
  128. if (wc < 0 || wc > 0xff) {
  129. errno = EILSEQ;
  130. return (-1);
  131. }
  132. if (wc == L'\0')
  133. return (i);
  134. }
  135. return (i);
  136. }
  137. for (i = 0; i < nwc && i < len; i++) {
  138. wchar_t wc = (*src)[i];
  139. if (wc < 0 || wc > 0xff) {
  140. *src += i;
  141. errno = EILSEQ;
  142. return (-1);
  143. }
  144. dst[i] = wrapv(wc);
  145. if (wc == L'\0') {
  146. *src = NULL;
  147. return (i);
  148. }
  149. }
  150. *src += i;
  151. return (i);
  152. }