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
36static inline bool mbspartialc16(const mbstate_t* state) {
37  return mbstate_get_byte(state, 3) != 0;
38}
39
40static 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;
50  // Defined by POSIX as return value for first surrogate character.
51  return static_cast<size_t>(-3);
52}
53
54static size_t finish_surrogate(char16_t* pc16, mbstate_t* state) {
55  char16_t trail = mbstate_get_byte(state, 1) << 8 |
56                   mbstate_get_byte(state, 0);
57  *pc16 = trail;
58  return reset_and_return(mbstate_get_byte(state, 3), state);
59}
60
61size_t mbrtoc16(char16_t* pc16, const char* s, size_t n, mbstate_t* ps) {
62  static mbstate_t __private_state;
63  mbstate_t* state = (ps == NULL) ? &__private_state : ps;
64
65  char16_t __private_pc16;
66  if (pc16 == NULL) {
67    pc16 = &__private_pc16;
68  }
69
70  if (mbspartialc16(state)) {
71    return finish_surrogate(pc16, state);
72  }
73
74  char32_t c32;
75  size_t nconv = mbrtoc32(&c32, s, n, state);
76  if (__MB_IS_ERR(nconv)) {
77    return nconv;
78  } else if (nconv == 0) {
79    return reset_and_return(nconv, state);
80  } else if (c32 > 0x10ffff) {
81    // Input cannot be encoded as UTF-16.
82    return reset_and_return_illegal(EILSEQ, state);
83  } else if (c32 < 0x10000) {
84    *pc16 = static_cast<char16_t>(c32);
85    return reset_and_return(nconv, state);
86  } else {
87    return begin_surrogate(c32, pc16, nconv, state);
88  }
89}
90