Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 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 <sys/param.h> |
| 31 | #include <uchar.h> |
| 32 | #include <wchar.h> |
| 33 | |
| 34 | #include "private/bionic_mbstate.h" |
| 35 | |
| 36 | size_t mbrtoc32(char32_t* pc32, const char* s, size_t n, mbstate_t* ps) { |
| 37 | static mbstate_t __private_state; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 38 | mbstate_t* state = (ps == nullptr) ? &__private_state : ps; |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 39 | |
| 40 | // We should never get to a state which has all 4 bytes of the sequence set. |
| 41 | // Full state verification is done when decoding the sequence (after we have |
| 42 | // all the bytes). |
| 43 | if (mbstate_get_byte(state, 3) != 0) { |
Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 44 | return mbstate_reset_and_return_illegal(EINVAL, state); |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 47 | if (s == nullptr) { |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 48 | s = ""; |
| 49 | n = 1; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 50 | pc32 = nullptr; |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | if (n == 0) { |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | uint8_t ch; |
Elliott Hughes | 2c96639 | 2021-11-16 11:03:19 -0800 | [diff] [blame] | 58 | if (mbstate_is_initial(state) && (((ch = static_cast<uint8_t>(*s)) & ~0x7f) == 0)) { |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 59 | // Fast path for plain ASCII characters. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 60 | if (pc32 != nullptr) { |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 61 | *pc32 = ch; |
| 62 | } |
| 63 | return (ch != '\0' ? 1 : 0); |
| 64 | } |
| 65 | |
| 66 | // Determine the number of octets that make up this character |
| 67 | // from the first octet, and a mask that extracts the |
| 68 | // interesting bits of the first octet. We already know |
| 69 | // the character is at least two bytes long. |
| 70 | size_t length; |
| 71 | int mask; |
| 72 | |
| 73 | // We also specify a lower bound for the character code to |
| 74 | // detect redundant, non-"shortest form" encodings. For |
| 75 | // example, the sequence C0 80 is _not_ a legal representation |
| 76 | // of the null character. This enforces a 1-to-1 mapping |
| 77 | // between character codes and their multibyte representations. |
| 78 | char32_t lower_bound; |
| 79 | |
| 80 | // The first byte in the state (if any) tells the length. |
| 81 | size_t bytes_so_far = mbstate_bytes_so_far(state); |
| 82 | ch = bytes_so_far > 0 ? mbstate_get_byte(state, 0) : static_cast<uint8_t>(*s); |
Elliott Hughes | ad1658e | 2021-12-15 13:41:20 -0800 | [diff] [blame] | 83 | // We already handled the 1-byte case above, so we go straight to 2-bytes... |
| 84 | if ((ch & 0xe0) == 0xc0) { |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 85 | mask = 0x1f; |
| 86 | length = 2; |
| 87 | lower_bound = 0x80; |
| 88 | } else if ((ch & 0xf0) == 0xe0) { |
| 89 | mask = 0x0f; |
| 90 | length = 3; |
| 91 | lower_bound = 0x800; |
| 92 | } else if ((ch & 0xf8) == 0xf0) { |
| 93 | mask = 0x07; |
| 94 | length = 4; |
| 95 | lower_bound = 0x10000; |
| 96 | } else { |
| 97 | // Malformed input; input is not UTF-8. See RFC 3629. |
Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 98 | return mbstate_reset_and_return_illegal(EILSEQ, state); |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Fill in the state. |
| 102 | size_t bytes_wanted = length - bytes_so_far; |
| 103 | size_t i; |
| 104 | for (i = 0; i < MIN(bytes_wanted, n); i++) { |
Elliott Hughes | 2c96639 | 2021-11-16 11:03:19 -0800 | [diff] [blame] | 105 | if (!mbstate_is_initial(state) && ((*s & 0xc0) != 0x80)) { |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 106 | // Malformed input; bad characters in the middle of a character. |
Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 107 | return mbstate_reset_and_return_illegal(EILSEQ, state); |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 108 | } |
| 109 | mbstate_set_byte(state, bytes_so_far + i, *s++); |
| 110 | } |
| 111 | if (i < bytes_wanted) { |
Dan Albert | a9e914d | 2023-07-21 21:41:55 +0000 | [diff] [blame^] | 112 | return BIONIC_MULTIBYTE_RESULT_INCOMPLETE_SEQUENCE; |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Decode the octet sequence representing the character in chunks |
| 116 | // of 6 bits, most significant first. |
| 117 | char32_t c32 = mbstate_get_byte(state, 0) & mask; |
| 118 | for (i = 1; i < length; i++) { |
| 119 | c32 <<= 6; |
| 120 | c32 |= mbstate_get_byte(state, i) & 0x3f; |
| 121 | } |
| 122 | |
| 123 | if (c32 < lower_bound) { |
| 124 | // Malformed input; redundant encoding. |
Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 125 | return mbstate_reset_and_return_illegal(EILSEQ, state); |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 126 | } |
Elliott Hughes | 402c762 | 2018-07-06 17:18:05 -0700 | [diff] [blame] | 127 | if ((c32 >= 0xd800 && c32 <= 0xdfff) || (c32 > 0x10ffff)) { |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 128 | // Malformed input; invalid code points. |
Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 129 | return mbstate_reset_and_return_illegal(EILSEQ, state); |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 130 | } |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 131 | if (pc32 != nullptr) { |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 132 | *pc32 = c32; |
| 133 | } |
Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 134 | return mbstate_reset_and_return(c32 == U'\0' ? 0 : bytes_wanted, state); |
Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 135 | } |