1 /* Conversion UTF-16 to UCS-4. 2 Copyright (C) 2001-2002 Free Software Foundation, Inc. 3 Written by Bruno Haible <haible@clisp.cons.org>, 2001. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2, or (at your option) 8 any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 19 20 #include <stddef.h> 21 22 /* Return the length (number of units) of the first character in S, putting 23 its 'ucs4_t' representation in *PUC. */ 24 static int 25 u16_mbtouc_aux (unsigned int *puc, const unsigned short *s, size_t n) 26 { 27 unsigned short c = *s; 28 29 if (c < 0xdc00) 30 { 31 if (n >= 2) 32 { 33 if (s[1] >= 0xdc00 && s[1] < 0xe000) 34 { 35 *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00); 36 return 2; 37 } 38 /* invalid multibyte character */ 39 } 40 else 41 { 42 /* incomplete multibyte character */ 43 *puc = 0xfffd; 44 return n; 45 } 46 } 47 /* invalid multibyte character */ 48 *puc = 0xfffd; 49 return 1; 50 } 51 static inline int 52 u16_mbtouc (unsigned int *puc, const unsigned short *s, size_t n) 53 { 54 unsigned short c = *s; 55 56 if (c < 0xd800 || c >= 0xe000) 57 { 58 *puc = c; 59 return 1; 60 } 61 else 62 return u16_mbtouc_aux (puc, s, n); 63 } 64