2014-05-01 07:03:12 +02:00
|
|
|
/* $OpenBSD: citrus_utf8.c,v 1.6 2012/12/05 23:19:59 deraadt Exp $ */
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002-2004 Tim J. Robbins
|
2012-10-23 02:05:27 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2014-05-01 07:03:12 +02:00
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2012-10-23 02:05:27 +02:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2014-05-01 07:03:12 +02:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2012-10-23 02:05:27 +02:00
|
|
|
*
|
2014-05-01 07:03:12 +02:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
2012-10-23 02:05:27 +02:00
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2014-06-02 20:33:04 +02:00
|
|
|
#include <string.h>
|
2023-07-21 23:41:55 +02:00
|
|
|
#include <sys/param.h>
|
2014-06-02 20:33:04 +02:00
|
|
|
#include <uchar.h>
|
2023-07-21 23:41:55 +02:00
|
|
|
#include <wchar.h>
|
2014-06-02 20:33:04 +02:00
|
|
|
|
|
|
|
#include "private/bionic_mbstate.h"
|
2012-10-23 02:05:27 +02:00
|
|
|
|
2014-05-01 07:03:12 +02:00
|
|
|
//
|
2014-05-08 15:38:35 +02:00
|
|
|
// This file is basically OpenBSD's citrus_utf8.c but rewritten to not require a
|
|
|
|
// 12-byte mbstate_t so we're backwards-compatible with our LP32 ABI where
|
|
|
|
// mbstate_t was only 4 bytes.
|
2014-05-01 07:03:12 +02:00
|
|
|
//
|
2014-05-08 15:38:35 +02:00
|
|
|
// The state is the UTF-8 sequence. We only support <= 4-bytes sequences so LP32
|
|
|
|
// mbstate_t already has enough space (out of the 4 available bytes we only
|
|
|
|
// need 3 since we should never need to store the entire sequence in the
|
|
|
|
// intermediary state).
|
|
|
|
//
|
|
|
|
// The C standard leaves the conversion state undefined after a bad conversion.
|
|
|
|
// To avoid unexpected failures due to the possible use of the internal private
|
|
|
|
// state we always reset the conversion state when encountering illegal
|
|
|
|
// sequences.
|
|
|
|
//
|
|
|
|
// We also implement the POSIX interface directly rather than being accessed via
|
|
|
|
// function pointers.
|
2014-05-01 07:03:12 +02:00
|
|
|
//
|
|
|
|
|
2014-05-08 15:38:35 +02:00
|
|
|
int mbsinit(const mbstate_t* ps) {
|
2021-11-16 20:03:19 +01:00
|
|
|
return ps == nullptr || mbstate_is_initial(ps);
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 15:38:35 +02:00
|
|
|
size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* ps) {
|
|
|
|
static mbstate_t __private_state;
|
2018-08-03 02:31:13 +02:00
|
|
|
mbstate_t* state = (ps == nullptr) ? &__private_state : ps;
|
2014-05-08 15:38:35 +02:00
|
|
|
|
2016-10-24 23:50:31 +02:00
|
|
|
// Our wchar_t is UTF-32.
|
2014-06-02 20:33:04 +02:00
|
|
|
return mbrtoc32(reinterpret_cast<char32_t*>(pwc), s, n, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t mbsnrtowcs(wchar_t* dst, const char** src, size_t nmc, size_t len, mbstate_t* ps) {
|
2014-05-08 15:38:35 +02:00
|
|
|
static mbstate_t __private_state;
|
2018-08-03 02:31:13 +02:00
|
|
|
mbstate_t* state = (ps == nullptr) ? &__private_state : ps;
|
2014-05-01 07:03:12 +02:00
|
|
|
size_t i, o, r;
|
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
// The fast paths in the loops below are not safe if an ASCII
|
|
|
|
// character appears as anything but the first byte of a
|
|
|
|
// multibyte sequence. Check now to avoid doing it in the loops.
|
|
|
|
if (nmc > 0 && mbstate_bytes_so_far(state) > 0 && static_cast<uint8_t>((*src)[0]) < 0x80) {
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return_illegal(EILSEQ, state);
|
2016-09-30 02:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Measure only?
|
2018-08-03 02:31:13 +02:00
|
|
|
if (dst == nullptr) {
|
2014-05-01 07:03:12 +02:00
|
|
|
for (i = o = 0; i < nmc; i += r, o++) {
|
|
|
|
if (static_cast<uint8_t>((*src)[i]) < 0x80) {
|
|
|
|
// Fast path for plain ASCII characters.
|
|
|
|
if ((*src)[i] == '\0') {
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return(o, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
|
|
|
r = 1;
|
|
|
|
} else {
|
2018-08-03 02:31:13 +02:00
|
|
|
r = mbrtowc(nullptr, *src + i, nmc - i, state);
|
2023-07-21 23:41:55 +02:00
|
|
|
if (r == BIONIC_MULTIBYTE_RESULT_ILLEGAL_SEQUENCE) {
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return_illegal(EILSEQ, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
2023-07-21 23:41:55 +02:00
|
|
|
if (r == BIONIC_MULTIBYTE_RESULT_INCOMPLETE_SEQUENCE) {
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return_illegal(EILSEQ, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
|
|
|
if (r == 0) {
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return(o, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
2014-04-29 23:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return(o, state);
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
2014-05-01 07:03:12 +02:00
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
// Actually convert, updating `dst` and `src`.
|
2014-05-01 07:03:12 +02:00
|
|
|
for (i = o = 0; i < nmc && o < len; i += r, o++) {
|
|
|
|
if (static_cast<uint8_t>((*src)[i]) < 0x80) {
|
|
|
|
// Fast path for plain ASCII characters.
|
|
|
|
dst[o] = (*src)[i];
|
2014-07-20 20:51:26 +02:00
|
|
|
r = 1;
|
2014-05-01 07:03:12 +02:00
|
|
|
if ((*src)[i] == '\0') {
|
2014-07-31 20:31:03 +02:00
|
|
|
*src = nullptr;
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return(o, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
2014-04-29 23:46:56 +02:00
|
|
|
} else {
|
2014-05-08 15:38:35 +02:00
|
|
|
r = mbrtowc(dst + o, *src + i, nmc - i, state);
|
2023-07-21 23:41:55 +02:00
|
|
|
if (r == BIONIC_MULTIBYTE_RESULT_ILLEGAL_SEQUENCE) {
|
2014-05-01 07:03:12 +02:00
|
|
|
*src += i;
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return_illegal(EILSEQ, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
2023-07-21 23:41:55 +02:00
|
|
|
if (r == BIONIC_MULTIBYTE_RESULT_INCOMPLETE_SEQUENCE) {
|
2014-05-01 07:03:12 +02:00
|
|
|
*src += nmc;
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return_illegal(EILSEQ, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
|
|
|
if (r == 0) {
|
2018-08-03 02:31:13 +02:00
|
|
|
*src = nullptr;
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return(o, state);
|
2014-05-01 07:03:12 +02:00
|
|
|
}
|
2014-04-29 23:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-01 07:03:12 +02:00
|
|
|
*src += i;
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return(o, state);
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 07:03:12 +02:00
|
|
|
size_t mbsrtowcs(wchar_t* dst, const char** src, size_t len, mbstate_t* ps) {
|
|
|
|
return mbsnrtowcs(dst, src, SIZE_MAX, len, ps);
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
Expose tzalloc()/localtime_rz()/mktime_z()/tzfree().
* Rationale
The question often comes up of how to use multiple time zones in C code.
If you're single-threaded, you can just use setenv() to manipulate $TZ.
toybox does this, for example. But that's not thread-safe in two
distinct ways: firstly, getenv() is not thread-safe with respect to
modifications to the environment (and between the way putenv() is
specified and the existence of environ, it's not obvious how to fully
fix that), and secondly the _caller_ needs to ensure that no other
threads are using tzset() or any function that behaves "as if" tzset()
was called (which is neither easy to determine nor easy to ensure).
This isn't a bigger problem because most of the time the right answer
is to stop pretending that libc is at all suitable for any i18n, and
switch to icu4c instead. (The NDK icu4c headers do not include ucal_*,
so this is not a realistic option for most applications.)
But what if you're somewhere in between? Like the rust chrono library,
for example? What then?
Currently their "least worst" option is to reinvent the entire wheel and
read our tzdata files. Which isn't a great solution for anyone, for
obvious maintainability reasons.
So it's probably time we broke the catch-22 here and joined NetBSD in
offering a less broken API than standard C has for the last 40 years.
Sure, any would-be caller will have to have a separate "is this
Android?" and even "is this API level >= 35?" path, but that will fix
itself sometime in the 2030s when developers can just assume "yes, it
is", whereas if we keep putting off exposing anything, this problem
never gets solved.
(No-one's bothered to try to implement the std::chrono::time_zone
functionality in libc++ yet, but they'll face a similar problem if/when
they do.)
* Implementation
The good news is that tzcode already implements these functions, so
there's relatively little here.
I've chosen not to expose `struct state` because `struct __timezone_t`
makes for clearer error messages, given that compiler diagnostics will
show the underlying type name (`struct __timezone_t*`) rather than the
typedef name (`timezone_t`) that's used in calling code.
I've moved us over to FreeBSD's wcsftime() rather than keep the OpenBSD
one building --- I've long wanted to only have one implementation here,
and FreeBSD is already doing the "convert back and forth, calling the
non-wide function in the middle" dance that I'd hoped to get round to
doing myself someday. This should mean that our strftime() and
wcsftime() behaviors can't easily diverge in future, plus macOS/iOS are
mostly FreeBSD, so any bugs will likely be interoperable with the other
major mobile operating system, so there's something nice for everyone
there!
The FreeBSD wcsftime() implementation includes a wcsftime_l()
implementation, so that's one stub we can remove. The flip side of that
is that it uses mbsrtowcs_l() and wcsrtombs_l() which we didn't
previously have. So expose those as aliases of mbsrtowcs() and
wcsrtombs().
Bug: https://github.com/chronotope/chrono/issues/499
Test: treehugger
Change-Id: Iee1b9d763ead15eef3d2c33666b3403b68940c3c
2023-06-15 22:17:08 +02:00
|
|
|
__strong_alias(mbsrtowcs_l, mbsrtowcs);
|
2012-10-23 02:05:27 +02:00
|
|
|
|
2014-05-08 15:38:35 +02:00
|
|
|
size_t wcrtomb(char* s, wchar_t wc, mbstate_t* ps) {
|
|
|
|
static mbstate_t __private_state;
|
2018-08-03 02:31:13 +02:00
|
|
|
mbstate_t* state = (ps == nullptr) ? &__private_state : ps;
|
2014-05-08 15:38:35 +02:00
|
|
|
|
2016-09-30 02:21:43 +02:00
|
|
|
// Our wchar_t is UTF-32.
|
2014-06-02 20:33:04 +02:00
|
|
|
return c32rtomb(s, static_cast<char32_t>(wc), state);
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 07:03:12 +02:00
|
|
|
size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstate_t* ps) {
|
2014-05-08 15:38:35 +02:00
|
|
|
static mbstate_t __private_state;
|
2018-08-03 02:31:13 +02:00
|
|
|
mbstate_t* state = (ps == nullptr) ? &__private_state : ps;
|
2014-05-08 15:38:35 +02:00
|
|
|
|
2021-11-16 20:03:19 +01:00
|
|
|
if (!mbstate_is_initial(state)) {
|
2017-07-15 02:00:05 +02:00
|
|
|
return mbstate_reset_and_return_illegal(EILSEQ, state);
|
2014-05-08 15:38:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 07:03:12 +02:00
|
|
|
char buf[MB_LEN_MAX];
|
|
|
|
size_t i, o, r;
|
2018-08-03 02:31:13 +02:00
|
|
|
if (dst == nullptr) {
|
2014-05-01 07:03:12 +02:00
|
|
|
for (i = o = 0; i < nwc; i++, o += r) {
|
|
|
|
wchar_t wc = (*src)[i];
|
2014-05-02 04:03:18 +02:00
|
|
|
if (static_cast<uint32_t>(wc) < 0x80) {
|
2014-05-01 07:03:12 +02:00
|
|
|
// Fast path for plain ASCII characters.
|
|
|
|
if (wc == 0) {
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
r = 1;
|
|
|
|
} else {
|
2014-05-08 15:38:35 +02:00
|
|
|
r = wcrtomb(buf, wc, state);
|
2023-07-21 23:41:55 +02:00
|
|
|
if (r == BIONIC_MULTIBYTE_RESULT_ILLEGAL_SEQUENCE) {
|
2014-05-01 07:03:12 +02:00
|
|
|
return r;
|
|
|
|
}
|
2014-04-18 22:32:33 +02:00
|
|
|
}
|
2014-04-18 02:30:03 +02:00
|
|
|
}
|
2014-05-01 07:03:12 +02:00
|
|
|
return o;
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
2014-05-01 07:03:12 +02:00
|
|
|
|
|
|
|
for (i = o = 0; i < nwc && o < len; i++, o += r) {
|
|
|
|
wchar_t wc = (*src)[i];
|
2014-05-02 04:03:18 +02:00
|
|
|
if (static_cast<uint32_t>(wc) < 0x80) {
|
2014-05-01 07:03:12 +02:00
|
|
|
// Fast path for plain ASCII characters.
|
|
|
|
dst[o] = wc;
|
|
|
|
if (wc == 0) {
|
2018-08-03 02:31:13 +02:00
|
|
|
*src = nullptr;
|
2014-05-01 07:03:12 +02:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
r = 1;
|
|
|
|
} else if (len - o >= sizeof(buf)) {
|
|
|
|
// Enough space to translate in-place.
|
2014-05-08 15:38:35 +02:00
|
|
|
r = wcrtomb(dst + o, wc, state);
|
2023-07-21 23:41:55 +02:00
|
|
|
if (r == BIONIC_MULTIBYTE_RESULT_ILLEGAL_SEQUENCE) {
|
2014-05-01 07:03:12 +02:00
|
|
|
*src += i;
|
|
|
|
return r;
|
|
|
|
}
|
2014-04-18 02:30:03 +02:00
|
|
|
} else {
|
2014-05-01 07:03:12 +02:00
|
|
|
// May not be enough space; use temp buffer.
|
2014-05-08 15:38:35 +02:00
|
|
|
r = wcrtomb(buf, wc, state);
|
2023-07-21 23:41:55 +02:00
|
|
|
if (r == BIONIC_MULTIBYTE_RESULT_ILLEGAL_SEQUENCE) {
|
2014-05-01 07:03:12 +02:00
|
|
|
*src += i;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (r > len - o) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy(dst + o, buf, r);
|
2014-04-18 02:30:03 +02:00
|
|
|
}
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
2014-05-01 07:03:12 +02:00
|
|
|
*src += i;
|
2014-04-18 02:30:03 +02:00
|
|
|
return o;
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
|
|
|
|
2014-05-01 07:03:12 +02:00
|
|
|
size_t wcsrtombs(char* dst, const wchar_t** src, size_t len, mbstate_t* ps) {
|
|
|
|
return wcsnrtombs(dst, src, SIZE_MAX, len, ps);
|
2012-10-23 02:05:27 +02:00
|
|
|
}
|
Expose tzalloc()/localtime_rz()/mktime_z()/tzfree().
* Rationale
The question often comes up of how to use multiple time zones in C code.
If you're single-threaded, you can just use setenv() to manipulate $TZ.
toybox does this, for example. But that's not thread-safe in two
distinct ways: firstly, getenv() is not thread-safe with respect to
modifications to the environment (and between the way putenv() is
specified and the existence of environ, it's not obvious how to fully
fix that), and secondly the _caller_ needs to ensure that no other
threads are using tzset() or any function that behaves "as if" tzset()
was called (which is neither easy to determine nor easy to ensure).
This isn't a bigger problem because most of the time the right answer
is to stop pretending that libc is at all suitable for any i18n, and
switch to icu4c instead. (The NDK icu4c headers do not include ucal_*,
so this is not a realistic option for most applications.)
But what if you're somewhere in between? Like the rust chrono library,
for example? What then?
Currently their "least worst" option is to reinvent the entire wheel and
read our tzdata files. Which isn't a great solution for anyone, for
obvious maintainability reasons.
So it's probably time we broke the catch-22 here and joined NetBSD in
offering a less broken API than standard C has for the last 40 years.
Sure, any would-be caller will have to have a separate "is this
Android?" and even "is this API level >= 35?" path, but that will fix
itself sometime in the 2030s when developers can just assume "yes, it
is", whereas if we keep putting off exposing anything, this problem
never gets solved.
(No-one's bothered to try to implement the std::chrono::time_zone
functionality in libc++ yet, but they'll face a similar problem if/when
they do.)
* Implementation
The good news is that tzcode already implements these functions, so
there's relatively little here.
I've chosen not to expose `struct state` because `struct __timezone_t`
makes for clearer error messages, given that compiler diagnostics will
show the underlying type name (`struct __timezone_t*`) rather than the
typedef name (`timezone_t`) that's used in calling code.
I've moved us over to FreeBSD's wcsftime() rather than keep the OpenBSD
one building --- I've long wanted to only have one implementation here,
and FreeBSD is already doing the "convert back and forth, calling the
non-wide function in the middle" dance that I'd hoped to get round to
doing myself someday. This should mean that our strftime() and
wcsftime() behaviors can't easily diverge in future, plus macOS/iOS are
mostly FreeBSD, so any bugs will likely be interoperable with the other
major mobile operating system, so there's something nice for everyone
there!
The FreeBSD wcsftime() implementation includes a wcsftime_l()
implementation, so that's one stub we can remove. The flip side of that
is that it uses mbsrtowcs_l() and wcsrtombs_l() which we didn't
previously have. So expose those as aliases of mbsrtowcs() and
wcsrtombs().
Bug: https://github.com/chronotope/chrono/issues/499
Test: treehugger
Change-Id: Iee1b9d763ead15eef3d2c33666b3403b68940c3c
2023-06-15 22:17:08 +02:00
|
|
|
__strong_alias(wcsrtombs_l, wcsrtombs);
|