150c77c6eSEd Schouten /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
450c77c6eSEd Schouten * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
550c77c6eSEd Schouten * All rights reserved.
650c77c6eSEd Schouten *
750c77c6eSEd Schouten * Redistribution and use in source and binary forms, with or without
850c77c6eSEd Schouten * modification, are permitted provided that the following conditions
950c77c6eSEd Schouten * are met:
1050c77c6eSEd Schouten * 1. Redistributions of source code must retain the above copyright
1150c77c6eSEd Schouten * notice, this list of conditions and the following disclaimer.
1250c77c6eSEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
1350c77c6eSEd Schouten * notice, this list of conditions and the following disclaimer in the
1450c77c6eSEd Schouten * documentation and/or other materials provided with the distribution.
1550c77c6eSEd Schouten *
1650c77c6eSEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1750c77c6eSEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1850c77c6eSEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1950c77c6eSEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2050c77c6eSEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2150c77c6eSEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2250c77c6eSEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2350c77c6eSEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2450c77c6eSEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2550c77c6eSEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2650c77c6eSEd Schouten * SUCH DAMAGE.
2750c77c6eSEd Schouten */
2850c77c6eSEd Schouten
2950c77c6eSEd Schouten #include <errno.h>
3050c77c6eSEd Schouten #include <uchar.h>
3150c77c6eSEd Schouten #include <wchar.h>
3298f3acfaSYuri Pankov #include "mblocal.h"
3350c77c6eSEd Schouten
3450c77c6eSEd Schouten size_t
c32rtomb_l(char * __restrict s,char32_t c32,mbstate_t * __restrict ps,locale_t locale)3550c77c6eSEd Schouten c32rtomb_l(char * __restrict s, char32_t c32, mbstate_t * __restrict ps,
3650c77c6eSEd Schouten locale_t locale)
3750c77c6eSEd Schouten {
3850c77c6eSEd Schouten
3950c77c6eSEd Schouten /* Unicode Standard 5.0, D90: ill-formed characters. */
4050c77c6eSEd Schouten if ((c32 >= 0xd800 && c32 <= 0xdfff) || c32 > 0x10ffff) {
4150c77c6eSEd Schouten errno = EILSEQ;
4250c77c6eSEd Schouten return ((size_t)-1);
4350c77c6eSEd Schouten }
4450c77c6eSEd Schouten
4550c77c6eSEd Schouten FIX_LOCALE(locale);
4650c77c6eSEd Schouten if (ps == NULL)
4798f3acfaSYuri Pankov ps = &(XLOCALE_CTYPE(locale)->c32rtomb);
4850c77c6eSEd Schouten
4950c77c6eSEd Schouten /* Assume wchar_t uses UTF-32. */
5050c77c6eSEd Schouten return (wcrtomb_l(s, c32, ps, locale));
5150c77c6eSEd Schouten }
5250c77c6eSEd Schouten
5350c77c6eSEd Schouten size_t
c32rtomb(char * __restrict s,char32_t c32,mbstate_t * __restrict ps)5450c77c6eSEd Schouten c32rtomb(char * __restrict s, char32_t c32, mbstate_t * __restrict ps)
5550c77c6eSEd Schouten {
5650c77c6eSEd Schouten
5750c77c6eSEd Schouten return (c32rtomb_l(s, c32, ps, __get_locale()));
5850c77c6eSEd Schouten }
59