xref: /aosp_15_r20/bionic/libc/bionic/c32rtomb.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <errno.h>
30 #include <uchar.h>
31 #include <wchar.h>
32 
33 #include "private/bionic_mbstate.h"
34 
c32rtomb(char * s,char32_t c32,mbstate_t * ps)35 size_t c32rtomb(char* s, char32_t c32, mbstate_t* ps) {
36   static mbstate_t __private_state;
37   mbstate_t* state = (ps == nullptr) ? &__private_state : ps;
38 
39   if (s == nullptr) {
40     // Equivalent to c32rtomb(buf, U'\0', ps).
41     return mbstate_reset_and_return(1, state);
42   }
43 
44   // POSIX states that if char32_t is a null wide character, a null byte shall
45   // be stored, preceded by any shift sequence needed to restore the initial
46   // shift state. Since shift states are not supported, only the null byte is
47   // stored.
48   if (c32 == U'\0') {
49     *s = '\0';
50     return mbstate_reset_and_return(1, state);
51   }
52 
53   if (!mbstate_is_initial(state)) {
54     return mbstate_reset_and_return_illegal(EILSEQ, state);
55   }
56 
57   if ((c32 & ~0x7f) == 0) {
58     // Fast path for plain ASCII characters.
59     *s = c32;
60     return 1;
61   }
62 
63   // Determine the number of octets needed to represent this character.
64   // We always output the shortest sequence possible. Also specify the
65   // first few bits of the first octet, which contains the information
66   // about the sequence length.
67   uint8_t lead;
68   size_t length;
69   // We already handled the 1-byte case above, so we go straight to 2-bytes...
70   if ((c32 & ~0x7ff) == 0) {
71     lead = 0xc0;
72     length = 2;
73   } else if ((c32 & ~0xffff) == 0) {
74     lead = 0xe0;
75     length = 3;
76   } else if ((c32 & ~0x1fffff) == 0) {
77     lead = 0xf0;
78     length = 4;
79   } else {
80     errno = EILSEQ;
81     return BIONIC_MULTIBYTE_RESULT_ILLEGAL_SEQUENCE;
82   }
83 
84   // Output the octets representing the character in chunks
85   // of 6 bits, least significant last. The first octet is
86   // a special case because it contains the sequence length
87   // information.
88   for (size_t i = length - 1; i > 0; i--) {
89     s[i] = (c32 & 0x3f) | 0x80;
90     c32 >>= 6;
91   }
92   *s = (c32 & 0xff) | lead;
93 
94   return length;
95 }
96