1*3a628b46Sschwarze /* $OpenBSD: multibyte_citrus.c,v 1.8 2017/09/05 03:16:13 schwarze Exp $ */
2c9b8e388Sstsp /* $NetBSD: multibyte_amd1.c,v 1.7 2009/01/11 02:46:28 christos Exp $ */
3c9b8e388Sstsp
4c9b8e388Sstsp /*-
5c9b8e388Sstsp * Copyright (c)2002, 2008 Citrus Project,
6c9b8e388Sstsp * All rights reserved.
7c9b8e388Sstsp *
8c9b8e388Sstsp * Redistribution and use in source and binary forms, with or without
9c9b8e388Sstsp * modification, are permitted provided that the following conditions
10c9b8e388Sstsp * are met:
11c9b8e388Sstsp * 1. Redistributions of source code must retain the above copyright
12c9b8e388Sstsp * notice, this list of conditions and the following disclaimer.
13c9b8e388Sstsp * 2. Redistributions in binary form must reproduce the above copyright
14c9b8e388Sstsp * notice, this list of conditions and the following disclaimer in the
15c9b8e388Sstsp * documentation and/or other materials provided with the distribution.
16c9b8e388Sstsp *
17c9b8e388Sstsp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18c9b8e388Sstsp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c9b8e388Sstsp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c9b8e388Sstsp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21c9b8e388Sstsp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c9b8e388Sstsp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c9b8e388Sstsp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c9b8e388Sstsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c9b8e388Sstsp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c9b8e388Sstsp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c9b8e388Sstsp * SUCH DAMAGE.
28c9b8e388Sstsp */
29c9b8e388Sstsp
30c9b8e388Sstsp #include <sys/types.h>
311357284aSmillert #include <stdint.h>
32754110aeSschwarze #include <stdlib.h>
33c9b8e388Sstsp #include <wchar.h>
34c9b8e388Sstsp
35c9b8e388Sstsp #include "citrus_ctype.h"
36c9b8e388Sstsp
37c9b8e388Sstsp int
mbsinit(const mbstate_t * ps)38c9b8e388Sstsp mbsinit(const mbstate_t *ps)
39c9b8e388Sstsp {
40*3a628b46Sschwarze if (ps == NULL || __mb_cur_max() == 1)
41c9b8e388Sstsp return 1;
42754110aeSschwarze return _citrus_utf8_ctype_mbsinit(ps);
43c9b8e388Sstsp }
4438a75b98Sguenther DEF_STRONG(mbsinit);
45c9b8e388Sstsp
46c9b8e388Sstsp size_t
mbrtowc(wchar_t * pwc,const char * s,size_t n,mbstate_t * ps)47c9b8e388Sstsp mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
48c9b8e388Sstsp {
49c9b8e388Sstsp static mbstate_t mbs;
50c9b8e388Sstsp
51c9b8e388Sstsp if (ps == NULL)
52c9b8e388Sstsp ps = &mbs;
53*3a628b46Sschwarze if (__mb_cur_max() == 1)
54754110aeSschwarze return _citrus_none_ctype_mbrtowc(pwc, s, n);
55754110aeSschwarze return _citrus_utf8_ctype_mbrtowc(pwc, s, n, ps);
56c9b8e388Sstsp }
5738a75b98Sguenther DEF_STRONG(mbrtowc);
58c9b8e388Sstsp
59c9b8e388Sstsp size_t
mbsrtowcs(wchar_t * dst,const char ** src,size_t len,mbstate_t * ps)605a08728eSmatthew mbsrtowcs(wchar_t *dst, const char **src, size_t len, mbstate_t *ps)
615a08728eSmatthew {
625a08728eSmatthew static mbstate_t mbs;
635a08728eSmatthew
645a08728eSmatthew if (ps == NULL)
655a08728eSmatthew ps = &mbs;
665a08728eSmatthew return (mbsnrtowcs(dst, src, SIZE_MAX, len, ps));
675a08728eSmatthew }
6838a75b98Sguenther DEF_STRONG(mbsrtowcs);
695a08728eSmatthew
705a08728eSmatthew size_t
mbsnrtowcs(wchar_t * dst,const char ** src,size_t nmc,size_t len,mbstate_t * ps)715a08728eSmatthew mbsnrtowcs(wchar_t *dst, const char **src, size_t nmc, size_t len,
725a08728eSmatthew mbstate_t *ps)
73c9b8e388Sstsp {
74c9b8e388Sstsp static mbstate_t mbs;
75c9b8e388Sstsp
76c9b8e388Sstsp if (ps == NULL)
77c9b8e388Sstsp ps = &mbs;
78*3a628b46Sschwarze if (__mb_cur_max() == 1)
79754110aeSschwarze return _citrus_none_ctype_mbsnrtowcs(dst, src, nmc, len);
80754110aeSschwarze return _citrus_utf8_ctype_mbsnrtowcs(dst, src, nmc, len, ps);
81c9b8e388Sstsp }
8238a75b98Sguenther DEF_WEAK(mbsnrtowcs);
83c9b8e388Sstsp
84c9b8e388Sstsp size_t
wcrtomb(char * s,wchar_t wc,mbstate_t * ps)85c9b8e388Sstsp wcrtomb(char *s, wchar_t wc, mbstate_t *ps)
86c9b8e388Sstsp {
87c9b8e388Sstsp static mbstate_t mbs;
88c9b8e388Sstsp
89c9b8e388Sstsp if (ps == NULL)
90c9b8e388Sstsp ps = &mbs;
91*3a628b46Sschwarze if (__mb_cur_max() == 1)
92754110aeSschwarze return _citrus_none_ctype_wcrtomb(s, wc);
93754110aeSschwarze return _citrus_utf8_ctype_wcrtomb(s, wc, ps);
94c9b8e388Sstsp }
9538a75b98Sguenther DEF_STRONG(wcrtomb);
96c9b8e388Sstsp
97c9b8e388Sstsp size_t
wcsrtombs(char * dst,const wchar_t ** src,size_t len,mbstate_t * ps)985a08728eSmatthew wcsrtombs(char *dst, const wchar_t **src, size_t len, mbstate_t *ps)
995a08728eSmatthew {
1005a08728eSmatthew static mbstate_t mbs;
1015a08728eSmatthew
1025a08728eSmatthew if (ps == NULL)
1035a08728eSmatthew ps = &mbs;
1045a08728eSmatthew return (wcsnrtombs(dst, src, SIZE_MAX, len, ps));
1055a08728eSmatthew }
10638a75b98Sguenther DEF_STRONG(wcsrtombs);
1075a08728eSmatthew
1085a08728eSmatthew size_t
wcsnrtombs(char * dst,const wchar_t ** src,size_t nwc,size_t len,mbstate_t * ps)1095a08728eSmatthew wcsnrtombs(char *dst, const wchar_t **src, size_t nwc, size_t len,
1105a08728eSmatthew mbstate_t *ps)
111c9b8e388Sstsp {
112c9b8e388Sstsp static mbstate_t mbs;
113c9b8e388Sstsp
114c9b8e388Sstsp if (ps == NULL)
115c9b8e388Sstsp ps = &mbs;
116*3a628b46Sschwarze if (__mb_cur_max() == 1)
117754110aeSschwarze return _citrus_none_ctype_wcsnrtombs(dst, src, nwc, len);
118754110aeSschwarze return _citrus_utf8_ctype_wcsnrtombs(dst, src, nwc, len, ps);
119c9b8e388Sstsp }
12038a75b98Sguenther DEF_WEAK(wcsnrtombs);
121