1250883Sed/*- 2250883Sed * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org> 3250883Sed * All rights reserved. 4250883Sed * 5250883Sed * Redistribution and use in source and binary forms, with or without 6250883Sed * modification, are permitted provided that the following conditions 7250883Sed * are met: 8250883Sed * 1. Redistributions of source code must retain the above copyright 9250883Sed * notice, this list of conditions and the following disclaimer. 10250883Sed * 2. Redistributions in binary form must reproduce the above copyright 11250883Sed * notice, this list of conditions and the following disclaimer in the 12250883Sed * documentation and/or other materials provided with the distribution. 13250883Sed * 14250883Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15250883Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16250883Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17250883Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18250883Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19250883Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20250883Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21250883Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22250883Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23250883Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24250883Sed * SUCH DAMAGE. 25250883Sed */ 26250883Sed 27250883Sed#include <sys/cdefs.h> 28250883Sed__FBSDID("$FreeBSD$"); 29250883Sed 30250883Sed#include <errno.h> 31250883Sed#include <uchar.h> 32250883Sed#include "xlocale_private.h" 33250883Sed 34250883Sedtypedef struct { 35250883Sed char16_t lead_surrogate; 36250883Sed mbstate_t c32_mbstate; 37250883Sed} _Char16State; 38250883Sed 39250883Sedsize_t 40250883Sedc16rtomb_l(char * __restrict s, char16_t c16, mbstate_t * __restrict ps, 41250883Sed locale_t locale) 42250883Sed{ 43250883Sed _Char16State *cs; 44250883Sed char32_t c32; 45250883Sed 46250883Sed FIX_LOCALE(locale); 47250883Sed if (ps == NULL) 48250883Sed ps = &locale->c16rtomb; 49250883Sed cs = (_Char16State *)ps; 50250883Sed 51250883Sed /* If s is a null pointer, the value of parameter c16 is ignored. */ 52250883Sed if (s == NULL) { 53250883Sed c32 = 0; 54250883Sed } else if (cs->lead_surrogate >= 0xd800 && 55250883Sed cs->lead_surrogate <= 0xdbff) { 56250883Sed /* We should see a trail surrogate now. */ 57250883Sed if (c16 < 0xdc00 || c16 > 0xdfff) { 58250883Sed errno = EILSEQ; 59250883Sed return ((size_t)-1); 60250883Sed } 61250883Sed c32 = 0x10000 + ((cs->lead_surrogate & 0x3ff) << 10 | 62250883Sed (c16 & 0x3ff)); 63250883Sed } else if (c16 >= 0xd800 && c16 <= 0xdbff) { 64250883Sed /* Store lead surrogate for next invocation. */ 65250883Sed cs->lead_surrogate = c16; 66250883Sed return (0); 67250883Sed } else { 68250883Sed /* Regular character. */ 69250883Sed c32 = c16; 70250883Sed } 71250883Sed cs->lead_surrogate = 0; 72250883Sed 73250883Sed return (c32rtomb_l(s, c32, &cs->c32_mbstate, locale)); 74250883Sed} 75250883Sed 76250883Sedsize_t 77250883Sedc16rtomb(char * __restrict s, char16_t c16, mbstate_t * __restrict ps) 78250883Sed{ 79250883Sed 80250883Sed return (c16rtomb_l(s, c16, ps, __get_locale())); 81250883Sed} 82