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 <sys/cdefs.h> 3050c77c6eSEd Schouten __FBSDID("$FreeBSD$"); 3150c77c6eSEd Schouten 3250c77c6eSEd Schouten #include <errno.h> 3350c77c6eSEd Schouten #include <uchar.h> 3498f3acfaSYuri Pankov #include "mblocal.h" 3550c77c6eSEd Schouten 3650c77c6eSEd Schouten typedef struct { 3750c77c6eSEd Schouten char16_t lead_surrogate; 3850c77c6eSEd Schouten mbstate_t c32_mbstate; 3950c77c6eSEd Schouten } _Char16State; 4050c77c6eSEd Schouten 4150c77c6eSEd Schouten size_t 4250c77c6eSEd Schouten c16rtomb_l(char * __restrict s, char16_t c16, mbstate_t * __restrict ps, 4350c77c6eSEd Schouten locale_t locale) 4450c77c6eSEd Schouten { 4550c77c6eSEd Schouten _Char16State *cs; 4650c77c6eSEd Schouten char32_t c32; 4750c77c6eSEd Schouten 4850c77c6eSEd Schouten FIX_LOCALE(locale); 4950c77c6eSEd Schouten if (ps == NULL) 5098f3acfaSYuri Pankov ps = &(XLOCALE_CTYPE(locale)->c16rtomb); 5150c77c6eSEd Schouten cs = (_Char16State *)ps; 5250c77c6eSEd Schouten 5350c77c6eSEd Schouten /* If s is a null pointer, the value of parameter c16 is ignored. */ 5450c77c6eSEd Schouten if (s == NULL) { 5550c77c6eSEd Schouten c32 = 0; 5650c77c6eSEd Schouten } else if (cs->lead_surrogate >= 0xd800 && 5750c77c6eSEd Schouten cs->lead_surrogate <= 0xdbff) { 5850c77c6eSEd Schouten /* We should see a trail surrogate now. */ 5950c77c6eSEd Schouten if (c16 < 0xdc00 || c16 > 0xdfff) { 6050c77c6eSEd Schouten errno = EILSEQ; 6150c77c6eSEd Schouten return ((size_t)-1); 6250c77c6eSEd Schouten } 6350c77c6eSEd Schouten c32 = 0x10000 + ((cs->lead_surrogate & 0x3ff) << 10 | 6450c77c6eSEd Schouten (c16 & 0x3ff)); 6550c77c6eSEd Schouten } else if (c16 >= 0xd800 && c16 <= 0xdbff) { 6650c77c6eSEd Schouten /* Store lead surrogate for next invocation. */ 6750c77c6eSEd Schouten cs->lead_surrogate = c16; 6850c77c6eSEd Schouten return (0); 6950c77c6eSEd Schouten } else { 7050c77c6eSEd Schouten /* Regular character. */ 7150c77c6eSEd Schouten c32 = c16; 7250c77c6eSEd Schouten } 7350c77c6eSEd Schouten cs->lead_surrogate = 0; 7450c77c6eSEd Schouten 7550c77c6eSEd Schouten return (c32rtomb_l(s, c32, &cs->c32_mbstate, locale)); 7650c77c6eSEd Schouten } 7750c77c6eSEd Schouten 7850c77c6eSEd Schouten size_t 7950c77c6eSEd Schouten c16rtomb(char * __restrict s, char16_t c16, mbstate_t * __restrict ps) 8050c77c6eSEd Schouten { 8150c77c6eSEd Schouten 8250c77c6eSEd Schouten return (c16rtomb_l(s, c16, ps, __get_locale())); 8350c77c6eSEd Schouten } 84