xref: /freebsd/lib/libc/locale/mbrtoc16.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
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 <uchar.h>
3098f3acfaSYuri Pankov #include "mblocal.h"
3150c77c6eSEd Schouten 
3250c77c6eSEd Schouten typedef struct {
3350c77c6eSEd Schouten 	char16_t	trail_surrogate;
3450c77c6eSEd Schouten 	mbstate_t	c32_mbstate;
3550c77c6eSEd Schouten } _Char16State;
3650c77c6eSEd Schouten 
3750c77c6eSEd Schouten size_t
mbrtoc16_l(char16_t * __restrict pc16,const char * __restrict s,size_t n,mbstate_t * __restrict ps,locale_t locale)3850c77c6eSEd Schouten mbrtoc16_l(char16_t * __restrict pc16, const char * __restrict s, size_t n,
3950c77c6eSEd Schouten     mbstate_t * __restrict ps, locale_t locale)
4050c77c6eSEd Schouten {
4150c77c6eSEd Schouten 	_Char16State *cs;
4250c77c6eSEd Schouten 	char32_t c32;
4350c77c6eSEd Schouten 	ssize_t len;
4450c77c6eSEd Schouten 
4550c77c6eSEd Schouten 	FIX_LOCALE(locale);
4650c77c6eSEd Schouten 	if (ps == NULL)
4798f3acfaSYuri Pankov 		ps = &(XLOCALE_CTYPE(locale)->mbrtoc16);
4850c77c6eSEd Schouten 	cs = (_Char16State *)ps;
4950c77c6eSEd Schouten 
5050c77c6eSEd Schouten 	/*
5150c77c6eSEd Schouten 	 * Call straight into mbrtoc32_l() if we don't need to return a
5250c77c6eSEd Schouten 	 * character value. According to the spec, if s is a null
5350c77c6eSEd Schouten 	 * pointer, the value of parameter pc16 is also ignored.
5450c77c6eSEd Schouten 	 */
5550c77c6eSEd Schouten 	if (pc16 == NULL || s == NULL) {
5650c77c6eSEd Schouten 		cs->trail_surrogate = 0;
5750c77c6eSEd Schouten 		return (mbrtoc32_l(NULL, s, n, &cs->c32_mbstate, locale));
5850c77c6eSEd Schouten 	}
5950c77c6eSEd Schouten 
6050c77c6eSEd Schouten 	/* Return the trail surrogate from the previous invocation. */
6150c77c6eSEd Schouten 	if (cs->trail_surrogate >= 0xdc00 && cs->trail_surrogate <= 0xdfff) {
6250c77c6eSEd Schouten 		*pc16 = cs->trail_surrogate;
6350c77c6eSEd Schouten 		cs->trail_surrogate = 0;
6450c77c6eSEd Schouten 		return ((size_t)-3);
6550c77c6eSEd Schouten 	}
6650c77c6eSEd Schouten 
6750c77c6eSEd Schouten 	len = mbrtoc32_l(&c32, s, n, &cs->c32_mbstate, locale);
6850c77c6eSEd Schouten 	if (len >= 0) {
6950c77c6eSEd Schouten 		if (c32 < 0x10000) {
7050c77c6eSEd Schouten 			/* Fits in one UTF-16 character. */
7150c77c6eSEd Schouten 			*pc16 = c32;
7250c77c6eSEd Schouten 		} else {
7350c77c6eSEd Schouten 			/* Split up in a surrogate pair. */
7450c77c6eSEd Schouten 			c32 -= 0x10000;
7550c77c6eSEd Schouten 			*pc16 = 0xd800 | (c32 >> 10);
7650c77c6eSEd Schouten 			cs->trail_surrogate = 0xdc00 | (c32 & 0x3ff);
7750c77c6eSEd Schouten 		}
7850c77c6eSEd Schouten 	}
7950c77c6eSEd Schouten 	return (len);
8050c77c6eSEd Schouten }
8150c77c6eSEd Schouten 
8250c77c6eSEd Schouten size_t
mbrtoc16(char16_t * __restrict pc16,const char * __restrict s,size_t n,mbstate_t * __restrict ps)8350c77c6eSEd Schouten mbrtoc16(char16_t * __restrict pc16, const char * __restrict s, size_t n,
8450c77c6eSEd Schouten     mbstate_t * __restrict ps)
8550c77c6eSEd Schouten {
8650c77c6eSEd Schouten 
8750c77c6eSEd Schouten 	return (mbrtoc16_l(pc16, s, n, ps, __get_locale()));
8850c77c6eSEd Schouten }
89