• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5  */
6 /* ISO C1x Unicode utilities
7  * Based on ISO/IEC SC22/WG14 9899 TR 19769 (SC22 N1326)
8  *
9  *  THIS SOFTWARE IS NOT COPYRIGHTED
10  *
11  *  This source code is offered for use in the public domain. You may
12  *  use, modify or distribute it freely.
13  *
14  *  This code is distributed in the hope that it will be useful but
15  *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
16  *  DISCLAIMED. This includes but is not limited to warranties of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  *  Date: 2011-09-27
20  */
21 
22 #include <errno.h>
23 #include <uchar.h>
24 
mbrtoc32(char32_t * __restrict__ pc32,const char * __restrict__ s,size_t n,mbstate_t * __restrict__ __UNUSED_PARAM (ps))25 size_t mbrtoc32 (char32_t *__restrict__ pc32,
26 		 const char *__restrict__ s,
27 		 size_t n,
28 		 mbstate_t *__restrict__ __UNUSED_PARAM(ps))
29 {
30     if (*s == 0)
31     {
32 	*pc32 = 0;
33 	return 0;
34     }
35 
36     /* ASCII character - high bit unset */
37     if ((*s & 0x80) == 0)
38     {
39 	*pc32 = *s;
40 	return 1;
41     }
42 
43     /* Multibyte chars */
44     if ((*s & 0xE0) == 0xC0) /* 110xxxxx needs 2 bytes */
45     {
46 	if (n < 2)
47 	    return (size_t)-2;
48 
49 	*pc32 = ((s[0] & 31) << 6) | (s[1] & 63);
50 	return 2;
51     }
52     else if ((*s & 0xf0) == 0xE0) /* 1110xxxx needs 3 bytes */
53     {
54 	if (n < 3)
55 	    return (size_t)-2;
56 
57 	*pc32 = ((s[0] & 15) << 12) | ((s[1] & 63) << 6) | (s[2] & 63);
58 	return 3;
59     }
60     else if ((*s & 0xF8) == 0xF0) /* 11110xxx needs 4 bytes */
61     {
62 	if (n < 4)
63 	    return (size_t)-2;
64 
65 	*pc32 = ((s[0] & 7) << 18) | ((s[1] & 63) << 12) | ((s[2] & 63) << 6) | (s[4] & 63);
66 	return 4;
67     }
68 
69     errno = EILSEQ;
70     return (size_t)-1;
71 }
72 
73