xref: /freebsd/lib/libc/locale/c16rtomb.c (revision 50c77c6e8b152e463f99c4e9225f5603c02d979c)
1*50c77c6eSEd Schouten /*-
2*50c77c6eSEd Schouten  * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
3*50c77c6eSEd Schouten  * All rights reserved.
4*50c77c6eSEd Schouten  *
5*50c77c6eSEd Schouten  * Redistribution and use in source and binary forms, with or without
6*50c77c6eSEd Schouten  * modification, are permitted provided that the following conditions
7*50c77c6eSEd Schouten  * are met:
8*50c77c6eSEd Schouten  * 1. Redistributions of source code must retain the above copyright
9*50c77c6eSEd Schouten  *    notice, this list of conditions and the following disclaimer.
10*50c77c6eSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
11*50c77c6eSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
12*50c77c6eSEd Schouten  *    documentation and/or other materials provided with the distribution.
13*50c77c6eSEd Schouten  *
14*50c77c6eSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*50c77c6eSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*50c77c6eSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*50c77c6eSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*50c77c6eSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*50c77c6eSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*50c77c6eSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*50c77c6eSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*50c77c6eSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*50c77c6eSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*50c77c6eSEd Schouten  * SUCH DAMAGE.
25*50c77c6eSEd Schouten  */
26*50c77c6eSEd Schouten 
27*50c77c6eSEd Schouten #include <sys/cdefs.h>
28*50c77c6eSEd Schouten __FBSDID("$FreeBSD$");
29*50c77c6eSEd Schouten 
30*50c77c6eSEd Schouten #include <errno.h>
31*50c77c6eSEd Schouten #include <uchar.h>
32*50c77c6eSEd Schouten #include "xlocale_private.h"
33*50c77c6eSEd Schouten 
34*50c77c6eSEd Schouten typedef struct {
35*50c77c6eSEd Schouten 	char16_t	lead_surrogate;
36*50c77c6eSEd Schouten 	mbstate_t	c32_mbstate;
37*50c77c6eSEd Schouten } _Char16State;
38*50c77c6eSEd Schouten 
39*50c77c6eSEd Schouten size_t
40*50c77c6eSEd Schouten c16rtomb_l(char * __restrict s, char16_t c16, mbstate_t * __restrict ps,
41*50c77c6eSEd Schouten     locale_t locale)
42*50c77c6eSEd Schouten {
43*50c77c6eSEd Schouten 	_Char16State *cs;
44*50c77c6eSEd Schouten 	char32_t c32;
45*50c77c6eSEd Schouten 
46*50c77c6eSEd Schouten 	FIX_LOCALE(locale);
47*50c77c6eSEd Schouten 	if (ps == NULL)
48*50c77c6eSEd Schouten 		ps = &locale->c16rtomb;
49*50c77c6eSEd Schouten 	cs = (_Char16State *)ps;
50*50c77c6eSEd Schouten 
51*50c77c6eSEd Schouten 	/* If s is a null pointer, the value of parameter c16 is ignored. */
52*50c77c6eSEd Schouten 	if (s == NULL) {
53*50c77c6eSEd Schouten 		c32 = 0;
54*50c77c6eSEd Schouten 	} else if (cs->lead_surrogate >= 0xd800 &&
55*50c77c6eSEd Schouten 	    cs->lead_surrogate <= 0xdbff) {
56*50c77c6eSEd Schouten 		/* We should see a trail surrogate now. */
57*50c77c6eSEd Schouten 		if (c16 < 0xdc00 || c16 > 0xdfff) {
58*50c77c6eSEd Schouten 			errno = EILSEQ;
59*50c77c6eSEd Schouten 			return ((size_t)-1);
60*50c77c6eSEd Schouten 		}
61*50c77c6eSEd Schouten 		c32 = 0x10000 + ((cs->lead_surrogate & 0x3ff) << 10 |
62*50c77c6eSEd Schouten 		    (c16 & 0x3ff));
63*50c77c6eSEd Schouten 	} else if (c16 >= 0xd800 && c16 <= 0xdbff) {
64*50c77c6eSEd Schouten 		/* Store lead surrogate for next invocation. */
65*50c77c6eSEd Schouten 		cs->lead_surrogate = c16;
66*50c77c6eSEd Schouten 		return (0);
67*50c77c6eSEd Schouten 	} else {
68*50c77c6eSEd Schouten 		/* Regular character. */
69*50c77c6eSEd Schouten 		c32 = c16;
70*50c77c6eSEd Schouten 	}
71*50c77c6eSEd Schouten 	cs->lead_surrogate = 0;
72*50c77c6eSEd Schouten 
73*50c77c6eSEd Schouten 	return (c32rtomb_l(s, c32, &cs->c32_mbstate, locale));
74*50c77c6eSEd Schouten }
75*50c77c6eSEd Schouten 
76*50c77c6eSEd Schouten size_t
77*50c77c6eSEd Schouten c16rtomb(char * __restrict s, char16_t c16, mbstate_t * __restrict ps)
78*50c77c6eSEd Schouten {
79*50c77c6eSEd Schouten 
80*50c77c6eSEd Schouten 	return (c16rtomb_l(s, c16, ps, __get_locale()));
81*50c77c6eSEd Schouten }
82