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
35size_t c32rtomb(char* s, char32_t c32, mbstate_t* ps) {
36  static mbstate_t __private_state;
37  mbstate_t* state = (ps == NULL) ? &__private_state : ps;
38
39  if (s == NULL) {
40    // Equivalent to c32rtomb(buf, U'\0', ps).
41    return 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    reset_and_return(1, state);
51  }
52
53  if (!mbsinit(state)) {
54    return 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  if ((c32 & ~0x7f) == 0) {
70    lead = 0;
71    length = 1;
72  } else if ((c32 & ~0x7ff) == 0) {
73    lead = 0xc0;
74    length = 2;
75  } else if ((c32 & ~0xffff) == 0) {
76    lead = 0xe0;
77    length = 3;
78  } else if ((c32 & ~0x1fffff) == 0) {
79    lead = 0xf0;
80    length = 4;
81  } else {
82    errno = EILSEQ;
83    return __MB_ERR_ILLEGAL_SEQUENCE;
84  }
85
86  // Output the octets representing the character in chunks
87  // of 6 bits, least significant last. The first octet is
88  // a special case because it contains the sequence length
89  // information.
90  for (size_t i = length - 1; i > 0; i--) {
91    s[i] = (c32 & 0x3f) | 0x80;
92    c32 >>= 6;
93  }
94  *s = (c32 & 0xff) | lead;
95
96  return length;
97}
98