mbrtoc16.c revision 250883
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: head/lib/libc/locale/mbrtoc16.c 250883 2013-05-21 19:59:37Z ed $"); 29250883Sed 30250883Sed#include <uchar.h> 31250883Sed#include "xlocale_private.h" 32250883Sed 33250883Sedtypedef struct { 34250883Sed char16_t trail_surrogate; 35250883Sed mbstate_t c32_mbstate; 36250883Sed} _Char16State; 37250883Sed 38250883Sedsize_t 39250883Sedmbrtoc16_l(char16_t * __restrict pc16, const char * __restrict s, size_t n, 40250883Sed mbstate_t * __restrict ps, locale_t locale) 41250883Sed{ 42250883Sed _Char16State *cs; 43250883Sed char32_t c32; 44250883Sed ssize_t len; 45250883Sed 46250883Sed FIX_LOCALE(locale); 47250883Sed if (ps == NULL) 48250883Sed ps = &locale->mbrtoc16; 49250883Sed cs = (_Char16State *)ps; 50250883Sed 51250883Sed /* 52250883Sed * Call straight into mbrtoc32_l() if we don't need to return a 53250883Sed * character value. According to the spec, if s is a null 54250883Sed * pointer, the value of parameter pc16 is also ignored. 55250883Sed */ 56250883Sed if (pc16 == NULL || s == NULL) { 57250883Sed cs->trail_surrogate = 0; 58250883Sed return (mbrtoc32_l(NULL, s, n, &cs->c32_mbstate, locale)); 59250883Sed } 60250883Sed 61250883Sed /* Return the trail surrogate from the previous invocation. */ 62250883Sed if (cs->trail_surrogate >= 0xdc00 && cs->trail_surrogate <= 0xdfff) { 63250883Sed *pc16 = cs->trail_surrogate; 64250883Sed cs->trail_surrogate = 0; 65250883Sed return ((size_t)-3); 66250883Sed } 67250883Sed 68250883Sed len = mbrtoc32_l(&c32, s, n, &cs->c32_mbstate, locale); 69250883Sed if (len >= 0) { 70250883Sed if (c32 < 0x10000) { 71250883Sed /* Fits in one UTF-16 character. */ 72250883Sed *pc16 = c32; 73250883Sed } else { 74250883Sed /* Split up in a surrogate pair. */ 75250883Sed c32 -= 0x10000; 76250883Sed *pc16 = 0xd800 | (c32 >> 10); 77250883Sed cs->trail_surrogate = 0xdc00 | (c32 & 0x3ff); 78250883Sed } 79250883Sed } 80250883Sed return (len); 81250883Sed} 82250883Sed 83250883Sedsize_t 84250883Sedmbrtoc16(char16_t * __restrict pc16, const char * __restrict s, size_t n, 85250883Sed mbstate_t * __restrict ps) 86250883Sed{ 87250883Sed 88250883Sed return (mbrtoc16_l(pc16, s, n, ps, __get_locale())); 89250883Sed} 90