1 /* $OpenBSD: multibyte_citrus.c,v 1.8 2017/09/05 03:16:13 schwarze Exp $ */
2 /* $NetBSD: multibyte_amd1.c,v 1.7 2009/01/11 02:46:28 christos Exp $ */
3
4 /*-
5 * Copyright (c)2002, 2008 Citrus Project,
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <wchar.h>
34
35 #include "citrus_ctype.h"
36
37 int
mbsinit(const mbstate_t * ps)38 mbsinit(const mbstate_t *ps)
39 {
40 if (ps == NULL || __mb_cur_max() == 1)
41 return 1;
42 return _citrus_utf8_ctype_mbsinit(ps);
43 }
44 DEF_STRONG(mbsinit);
45
46 size_t
mbrtowc(wchar_t * pwc,const char * s,size_t n,mbstate_t * ps)47 mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
48 {
49 static mbstate_t mbs;
50
51 if (ps == NULL)
52 ps = &mbs;
53 if (__mb_cur_max() == 1)
54 return _citrus_none_ctype_mbrtowc(pwc, s, n);
55 return _citrus_utf8_ctype_mbrtowc(pwc, s, n, ps);
56 }
57 DEF_STRONG(mbrtowc);
58
59 size_t
mbsrtowcs(wchar_t * dst,const char ** src,size_t len,mbstate_t * ps)60 mbsrtowcs(wchar_t *dst, const char **src, size_t len, mbstate_t *ps)
61 {
62 static mbstate_t mbs;
63
64 if (ps == NULL)
65 ps = &mbs;
66 return (mbsnrtowcs(dst, src, SIZE_MAX, len, ps));
67 }
68 DEF_STRONG(mbsrtowcs);
69
70 size_t
mbsnrtowcs(wchar_t * dst,const char ** src,size_t nmc,size_t len,mbstate_t * ps)71 mbsnrtowcs(wchar_t *dst, const char **src, size_t nmc, size_t len,
72 mbstate_t *ps)
73 {
74 static mbstate_t mbs;
75
76 if (ps == NULL)
77 ps = &mbs;
78 if (__mb_cur_max() == 1)
79 return _citrus_none_ctype_mbsnrtowcs(dst, src, nmc, len);
80 return _citrus_utf8_ctype_mbsnrtowcs(dst, src, nmc, len, ps);
81 }
82 DEF_WEAK(mbsnrtowcs);
83
84 size_t
wcrtomb(char * s,wchar_t wc,mbstate_t * ps)85 wcrtomb(char *s, wchar_t wc, mbstate_t *ps)
86 {
87 static mbstate_t mbs;
88
89 if (ps == NULL)
90 ps = &mbs;
91 if (__mb_cur_max() == 1)
92 return _citrus_none_ctype_wcrtomb(s, wc);
93 return _citrus_utf8_ctype_wcrtomb(s, wc, ps);
94 }
95 DEF_STRONG(wcrtomb);
96
97 size_t
wcsrtombs(char * dst,const wchar_t ** src,size_t len,mbstate_t * ps)98 wcsrtombs(char *dst, const wchar_t **src, size_t len, mbstate_t *ps)
99 {
100 static mbstate_t mbs;
101
102 if (ps == NULL)
103 ps = &mbs;
104 return (wcsnrtombs(dst, src, SIZE_MAX, len, ps));
105 }
106 DEF_STRONG(wcsrtombs);
107
108 size_t
wcsnrtombs(char * dst,const wchar_t ** src,size_t nwc,size_t len,mbstate_t * ps)109 wcsnrtombs(char *dst, const wchar_t **src, size_t nwc, size_t len,
110 mbstate_t *ps)
111 {
112 static mbstate_t mbs;
113
114 if (ps == NULL)
115 ps = &mbs;
116 if (__mb_cur_max() == 1)
117 return _citrus_none_ctype_wcsnrtombs(dst, src, nwc, len);
118 return _citrus_utf8_ctype_wcsnrtombs(dst, src, nwc, len, ps);
119 }
120 DEF_WEAK(wcsnrtombs);
121