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
c32rtomb(char * __restrict__ s,char32_t c32,mbstate_t * __restrict__ __UNUSED_PARAM (ps))25 size_t c32rtomb (char *__restrict__ s,
26 char32_t c32,
27 mbstate_t *__restrict__ __UNUSED_PARAM(ps))
28 {
29 if (c32 <= 0x7F) /* 7 bits needs 1 byte */
30 {
31 *s = (char)c32 & 0x7F;
32 return 1;
33 }
34 else if (c32 <= 0x7FF) /* 11 bits needs 2 bytes */
35 {
36 s[1] = 0x80 | (char)(c32 & 0x3F);
37 s[0] = 0xC0 | (char)(c32 >> 6);
38 return 2;
39 }
40 else if (c32 <= 0xFFFF) /* 16 bits needs 3 bytes */
41 {
42 s[2] = 0x80 | (char)(c32 & 0x3F);
43 s[1] = 0x80 | (char)((c32 >> 6) & 0x3F);
44 s[0] = 0xE0 | (char)(c32 >> 12);
45 return 3;
46 }
47 else if (c32 <= 0x1FFFFF) /* 21 bits needs 4 bytes */
48 {
49 s[3] = 0x80 | (char)(c32 & 0x3F);
50 s[2] = 0x80 | (char)((c32 >> 6) & 0x3F);
51 s[1] = 0x80 | (char)((c32 >> 12) & 0x3F);
52 s[0] = 0xF0 | (char)(c32 >> 18);
53 return 4;
54 }
55
56 errno = EILSEQ;
57 return (size_t)-1;
58 }
59
60