c32rtomb.c revision 250883
1169689Skan/*-
2169689Skan * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
3169689Skan * All rights reserved.
4169689Skan *
5169689Skan * Redistribution and use in source and binary forms, with or without
6169689Skan * modification, are permitted provided that the following conditions
7169689Skan * are met:
8169689Skan * 1. Redistributions of source code must retain the above copyright
9169689Skan *    notice, this list of conditions and the following disclaimer.
10169689Skan * 2. Redistributions in binary form must reproduce the above copyright
11169689Skan *    notice, this list of conditions and the following disclaimer in the
12169689Skan *    documentation and/or other materials provided with the distribution.
13169689Skan *
14169689Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24169689Skan * SUCH DAMAGE.
25169689Skan */
26169689Skan
27169689Skan#include <sys/cdefs.h>
28169689Skan__FBSDID("$FreeBSD: head/lib/libc/locale/c32rtomb.c 250883 2013-05-21 19:59:37Z ed $");
29169689Skan
30169689Skan#include <errno.h>
31169689Skan#include <uchar.h>
32169689Skan#include <wchar.h>
33169689Skan#include "xlocale_private.h"
34169689Skan
35169689Skansize_t
36169689Skanc32rtomb_l(char * __restrict s, char32_t c32, mbstate_t * __restrict ps,
37169689Skan    locale_t locale)
38169689Skan{
39169689Skan
40169689Skan	/* Unicode Standard 5.0, D90: ill-formed characters. */
41169689Skan	if ((c32 >= 0xd800 && c32 <= 0xdfff) || c32 > 0x10ffff) {
42169689Skan		errno = EILSEQ;
43169689Skan		return ((size_t)-1);
44169689Skan	}
45169689Skan
46169689Skan	FIX_LOCALE(locale);
47169689Skan	if (ps == NULL)
48169689Skan		ps = &locale->c32rtomb;
49169689Skan
50169689Skan	/* Assume wchar_t uses UTF-32. */
51169689Skan	return (wcrtomb_l(s, c32, ps, locale));
52}
53
54size_t
55c32rtomb(char * __restrict s, char32_t c32, mbstate_t * __restrict ps)
56{
57
58	return (c32rtomb_l(s, c32, ps, __get_locale()));
59}
60