From 2e53052b70dfffbc4ea92e7698f7ce65928be8ef Mon Sep 17 00:00:00 2001 From: Adithya R Date: Wed, 9 Sep 2020 20:32:12 +0530 Subject: [PATCH] Revert "Reland "libcutils: remove unused "jstring.h"." This reverts commit 42a5087ab788725589b9a05ebd2e0f268ee2eaea [formerly 79198d9adee5c7f0f37ddc4bdb250aed3043cb81]. Change-Id: Icedd6b14d3002af10b0c7dbd1db4bd8e6fcdae41 Former-commit-id: 84cc5c98b6843ed5c246139ef1afa7680b286944 --- libcutils/Android.bp | 1 + libcutils/include/cutils/jstring.h | 37 ++++++ libcutils/include_vndk/cutils/jstring.h | 1 + libcutils/strdup16to8.cpp | 168 ++++++++++++++++++++++++ 4 files changed, 207 insertions(+) create mode 100644 libcutils/include/cutils/jstring.h create mode 120000 libcutils/include_vndk/cutils/jstring.h create mode 100644 libcutils/strdup16to8.cpp diff --git a/libcutils/Android.bp b/libcutils/Android.bp index 24110eeae..e4c3e6f8b 100644 --- a/libcutils/Android.bp +++ b/libcutils/Android.bp @@ -153,6 +153,7 @@ cc_library { "load_file.cpp", "native_handle.cpp", "record_stream.cpp", + "strdup16to8.cpp", "strlcpy.c", "threads.cpp", ], diff --git a/libcutils/include/cutils/jstring.h b/libcutils/include/cutils/jstring.h new file mode 100644 index 000000000..6ede78614 --- /dev/null +++ b/libcutils/include/cutils/jstring.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#if __STDC_VERSION__ < 201112L && __cplusplus < 201103L + typedef uint16_t char16_t; +#endif + // otherwise char16_t is a keyword with the right semantics + +extern char * strndup16to8 (const char16_t* s, size_t n); +extern size_t strnlen16to8 (const char16_t* s, size_t n); +extern char * strncpy16to8 (char *dest, const char16_t*s, size_t n); + +#ifdef __cplusplus +} +#endif diff --git a/libcutils/include_vndk/cutils/jstring.h b/libcutils/include_vndk/cutils/jstring.h new file mode 120000 index 000000000..f3fd546ab --- /dev/null +++ b/libcutils/include_vndk/cutils/jstring.h @@ -0,0 +1 @@ +../../include/cutils/jstring.h \ No newline at end of file diff --git a/libcutils/strdup16to8.cpp b/libcutils/strdup16to8.cpp new file mode 100644 index 000000000..d89181e14 --- /dev/null +++ b/libcutils/strdup16to8.cpp @@ -0,0 +1,168 @@ +/* libs/cutils/strdup16to8.c +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#include + +#include +#include /* for SIZE_MAX */ +#include + + +/** + * Given a UTF-16 string, compute the length of the corresponding UTF-8 + * string in bytes. + */ +extern size_t strnlen16to8(const char16_t* utf16Str, size_t len) +{ + size_t utf8Len = 0; + + /* A small note on integer overflow. The result can + * potentially be as big as 3*len, which will overflow + * for len > SIZE_MAX/3. + * + * Moreover, the result of a strnlen16to8 is typically used + * to allocate a destination buffer to strncpy16to8 which + * requires one more byte to terminate the UTF-8 copy, and + * this is generally done by careless users by incrementing + * the result without checking for integer overflows, e.g.: + * + * dst = malloc(strnlen16to8(utf16,len)+1) + * + * Due to this, the following code will try to detect + * overflows, and never return more than (SIZE_MAX-1) + * when it detects one. A careless user will try to malloc + * SIZE_MAX bytes, which will return NULL which can at least + * be detected appropriately. + * + * As far as I know, this function is only used by strndup16(), + * but better be safe than sorry. + */ + + /* Fast path for the usual case where 3*len is < SIZE_MAX-1. + */ + if (len < (SIZE_MAX-1)/3) { + while (len != 0) { + len--; + unsigned int uic = *utf16Str++; + + if (uic > 0x07ff) + utf8Len += 3; + else if (uic > 0x7f || uic == 0) + utf8Len += 2; + else + utf8Len++; + } + return utf8Len; + } + + /* The slower but paranoid version */ + while (len != 0) { + len--; + unsigned int uic = *utf16Str++; + size_t utf8Cur = utf8Len; + + if (uic > 0x07ff) + utf8Len += 3; + else if (uic > 0x7f || uic == 0) + utf8Len += 2; + else + utf8Len++; + + if (utf8Len < utf8Cur) /* overflow detected */ + return SIZE_MAX-1; + } + + /* don't return SIZE_MAX to avoid common user bug */ + if (utf8Len == SIZE_MAX) + utf8Len = SIZE_MAX-1; + + return utf8Len; +} + + +/** + * Convert a Java-Style UTF-16 string + length to a JNI-Style UTF-8 string. + * + * This basically means: embedded \0's in the UTF-16 string are encoded + * as "0xc0 0x80" + * + * Make sure you allocate "utf8Str" with the result of strlen16to8() + 1, + * not just "len". + * + * Please note, a terminated \0 is always added, so your result will always + * be "strlen16to8() + 1" bytes long. + */ +extern char* strncpy16to8(char* utf8Str, const char16_t* utf16Str, size_t len) +{ + char* utf8cur = utf8Str; + + /* Note on overflows: We assume the user did check the result of + * strnlen16to8() properly or at a minimum checked the result of + * its malloc(SIZE_MAX) in case of overflow. + */ + while (len != 0) { + len--; + unsigned int uic = *utf16Str++; + + if (uic > 0x07ff) { + *utf8cur++ = (uic >> 12) | 0xe0; + *utf8cur++ = ((uic >> 6) & 0x3f) | 0x80; + *utf8cur++ = (uic & 0x3f) | 0x80; + } else if (uic > 0x7f || uic == 0) { + *utf8cur++ = (uic >> 6) | 0xc0; + *utf8cur++ = (uic & 0x3f) | 0x80; + } else { + *utf8cur++ = uic; + + if (uic == 0) { + break; + } + } + } + + *utf8cur = '\0'; + + return utf8Str; +} + +/** + * Convert a UTF-16 string to UTF-8. + * + */ +char * strndup16to8 (const char16_t* s, size_t n) +{ + if (s == NULL) { + return NULL; + } + + size_t len = strnlen16to8(s, n); + + /* We are paranoid, and we check for SIZE_MAX-1 + * too since it is an overflow value for our + * strnlen16to8 implementation. + */ + if (len >= SIZE_MAX-1) + return NULL; + + char* ret = static_cast(malloc(len + 1)); + if (ret == NULL) + return NULL; + + strncpy16to8 (ret, s, n); + + return ret; +}