| 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 <assert.h> | 
|  | 30 | #include <errno.h> | 
|  | 31 | #include <uchar.h> | 
|  | 32 | #include <wchar.h> | 
|  | 33 |  | 
|  | 34 | #include "private/bionic_mbstate.h" | 
|  | 35 |  | 
|  | 36 | static inline bool mbspartialc16(const mbstate_t* state) { | 
|  | 37 | return mbstate_get_byte(state, 3) != 0; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | static size_t begin_surrogate(char32_t c32, char16_t* pc16, | 
|  | 41 | size_t nconv, mbstate_t* state) { | 
|  | 42 | c32 -= 0x10000; | 
|  | 43 | char16_t trail = (c32 & 0x3ff) | 0xdc00; | 
|  | 44 |  | 
|  | 45 | mbstate_set_byte(state, 0, trail & 0x00ff); | 
|  | 46 | mbstate_set_byte(state, 1, (trail & 0xff00) >> 8); | 
|  | 47 | mbstate_set_byte(state, 3, nconv & 0xff); | 
|  | 48 |  | 
|  | 49 | *pc16 = ((c32 & 0xffc00) >> 10) | 0xd800; | 
| Dan Albert | 3a8fed1 | 2023-07-20 23:08:22 +0000 | [diff] [blame] | 50 | // https://issuetracker.google.com/289419882 | 
|  | 51 | // | 
|  | 52 | // We misread the spec when implementing this. The first call should return | 
|  | 53 | // the length of the decoded character, and the second call should return -3 | 
|  | 54 | // to indicate that the output is a continuation of the character decoded by | 
|  | 55 | // the first call. | 
|  | 56 | // | 
|  | 57 | // C23 7.30.1.3.4: | 
|  | 58 | // | 
|  | 59 | //     between 1 and n inclusive if the next n or fewer bytes complete a valid | 
|  | 60 | //     multibyte character (which is the value stored); the value returned is | 
|  | 61 | //     the number of bytes that complete the multibyte character. | 
|  | 62 | // | 
|  | 63 | //     (size_t)(-3) if the next character resulting from a previous call has | 
|  | 64 | //     been stored (no bytes from the input have been consumed by this call). | 
|  | 65 | // | 
|  | 66 | // The first call returns the number of bytes consumed, and the second call | 
|  | 67 | // returns -3. | 
|  | 68 | // | 
|  | 69 | // All UTF-8 sequences that encode a surrogate pair are 4 bytes, but we may | 
|  | 70 | // not have seen the full sequence yet. | 
|  | 71 | return nconv; | 
| Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | static size_t finish_surrogate(char16_t* pc16, mbstate_t* state) { | 
|  | 75 | char16_t trail = mbstate_get_byte(state, 1) << 8 | | 
|  | 76 | mbstate_get_byte(state, 0); | 
|  | 77 | *pc16 = trail; | 
| Dan Albert | 3a8fed1 | 2023-07-20 23:08:22 +0000 | [diff] [blame] | 78 | mbstate_reset(state); | 
|  | 79 | return static_cast<size_t>(-3); | 
| Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
|  | 82 | size_t mbrtoc16(char16_t* pc16, const char* s, size_t n, mbstate_t* ps) { | 
|  | 83 | static mbstate_t __private_state; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 84 | mbstate_t* state = (ps == nullptr) ? &__private_state : ps; | 
| Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | char16_t __private_pc16; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 87 | if (pc16 == nullptr) { | 
| Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 88 | pc16 = &__private_pc16; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | if (mbspartialc16(state)) { | 
|  | 92 | return finish_surrogate(pc16, state); | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | char32_t c32; | 
|  | 96 | size_t nconv = mbrtoc32(&c32, s, n, state); | 
|  | 97 | if (__MB_IS_ERR(nconv)) { | 
|  | 98 | return nconv; | 
|  | 99 | } else if (nconv == 0) { | 
| Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 100 | return mbstate_reset_and_return(nconv, state); | 
| Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 101 | } else if (c32 < 0x10000) { | 
|  | 102 | *pc16 = static_cast<char16_t>(c32); | 
| Elliott Hughes | 697f42a | 2017-07-14 17:00:05 -0700 | [diff] [blame] | 103 | return mbstate_reset_and_return(nconv, state); | 
| Elliott Hughes | 9d66092 | 2021-11-18 10:11:07 -0800 | [diff] [blame] | 104 | } else if (c32 > 0x10ffff) { | 
|  | 105 | // This case is currently handled by mbrtoc32() returning an error, but | 
|  | 106 | // if that function is extended to cover 5-byte sequences (which are | 
|  | 107 | // illegal at the moment), we'd need to explicitly handle the case of | 
|  | 108 | // codepoints that can't be represented as a surrogate pair here. | 
|  | 109 | return mbstate_reset_and_return_illegal(EILSEQ, state); | 
| Dan Albert | 7a7f995 | 2014-06-02 11:33:04 -0700 | [diff] [blame] | 110 | } else { | 
|  | 111 | return begin_surrogate(c32, pc16, nconv, state); | 
|  | 112 | } | 
|  | 113 | } |