/* * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
// Assume the utf8 string is in legal form and has been // checked in the class file parser/format checker. template<typename T> char* UTF8::next(constchar* str, T* value) { unsignedconstchar *ptr = (constunsignedchar *)str; unsignedchar ch, ch2, ch3; int length = -1; /* bad length */
jchar result; switch ((ch = ptr[0]) >> 4) { default:
result = ch;
length = 1; break;
case 0x8: case 0x9: case 0xA: case 0xB: case 0xF: /* Shouldn't happen. */ break;
if (length <= 0) {
*value = (T)ptr[0]; /* default bad result; */ return (char*)(ptr + 1); // make progress somehow
}
*value = (T)result;
// The assert is correct but the .class file is wrong // assert(UNICODE::utf8_size(result) == length, "checking reverse computation"); return (char *)(ptr + length);
}
// Count bytes of the form 10xxxxxx and deduct this count // from the total byte count. The utf8 string must be in // legal form which has been verified in the format checker. int UTF8::unicode_length(constchar* str, int len, bool& is_latin1, bool& has_multibyte) { int num_chars = len;
has_multibyte = false;
is_latin1 = true; unsignedchar prev = 0; for (int i = 0; i < len; i++) { unsignedchar c = str[i]; if ((c & 0xC0) == 0x80) { // Multibyte, check if valid latin1 character.
has_multibyte = true; if (prev > 0xC3) {
is_latin1 = false;
}
--num_chars;
}
prev = c;
} return num_chars;
}
// Count bytes of the utf8 string except those in form // 10xxxxxx which only appear in multibyte characters. // The utf8 string must be in legal form and has been // verified in the format checker. int UTF8::unicode_length(constchar* str, bool& is_latin1, bool& has_multibyte) { int num_chars = 0;
has_multibyte = false;
is_latin1 = true; unsignedchar prev = 0; for (constchar* p = str; *p; p++) { unsignedchar c = (*p); if ((c & 0xC0) == 0x80) { // Multibyte, check if valid latin1 character.
has_multibyte = true; if (prev > 0xC3) {
is_latin1 = false;
}
} else {
num_chars++;
}
prev = c;
} return num_chars;
}
// Writes a jchar as utf8 and returns the end static u_char* utf8_write(u_char* base, jchar ch) { if ((ch != 0) && (ch <=0x7f)) {
base[0] = (u_char) ch; return base + 1;
}
for (; index < unicode_length; index++) {
ptr = UTF8::next(ptr, &unicode_str[index]);
}
}
// Explicit instantiation for all supported string types. templatechar* UTF8::next<jchar>(constchar* str, jchar* value); templatechar* UTF8::next<jbyte>(constchar* str, jbyte* value); templatevoid UTF8::convert_to_unicode<jchar>(constchar* utf8_str, jchar* unicode_str, int unicode_length); templatevoid UTF8::convert_to_unicode<jbyte>(constchar* utf8_str, jbyte* unicode_str, int unicode_length);
// returns the quoted ascii length of a 0-terminated utf8 string int UTF8::quoted_ascii_length(constchar* utf8_str, int utf8_length) { constchar *ptr = utf8_str; constchar* end = ptr + utf8_length; int result = 0; while (ptr < end) {
jchar c;
ptr = UTF8::next(ptr, &c); if (c >= 32 && c < 127) {
result++;
} else {
result += 6;
}
} return result;
}
// converts a utf8 string to quoted ascii void UTF8::as_quoted_ascii(constchar* utf8_str, int utf8_length, char* buf, int buflen) { constchar *ptr = utf8_str; constchar *utf8_end = ptr + utf8_length; char* p = buf; char* end = buf + buflen; while (ptr < utf8_end) {
jchar c;
ptr = UTF8::next(ptr, &c); if (c >= 32 && c < 127) { if (p + 1 >= end) break; // string is truncated
*p++ = (char)c;
} else { if (p + 6 >= end) break; // string is truncated
sprintf(p, "\\u%04x", c);
p += 6;
}
}
assert(p < end, "sanity");
*p = '\0';
}
#ifndef PRODUCT // converts a quoted ascii string back to utf8 // no longer used, but could be useful to test output of UTF8::as_quoted_ascii constchar* UTF8::from_quoted_ascii(constchar* quoted_ascii_str) { constchar *ptr = quoted_ascii_str; char* result = NULL; while (*ptr != '\0') { char c = *ptr; if (c < 32 || c >= 127) break;
} if (*ptr == '\0') { // nothing to do so return original string return quoted_ascii_str;
} // everything up to this point was ok. int length = ptr - quoted_ascii_str; char* buffer = NULL; for (int round = 0; round < 2; round++) { while (*ptr != '\0') { if (*ptr != '\\') { if (buffer != NULL) {
buffer[length] = *ptr;
}
length++;
} else { switch (ptr[1]) { case'u': {
ptr += 2;
jchar value=0; for (int i=0; i<4; i++) { char c = *ptr++; switch (c) { case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': case'8': case'9':
value = (value << 4) + c - '0'; break; case'a': case'b': case'c': case'd': case'e': case'f':
value = (value << 4) + 10 + c - 'a'; break; case'A': case'B': case'C': case'D': case'E': case'F':
value = (value << 4) + 10 + c - 'A'; break; default:
ShouldNotReachHere();
}
} if (buffer == NULL) { char utf8_buffer[4]; char* next = (char*)utf8_write((u_char*)utf8_buffer, value);
length += next - utf8_buffer;
} else { char* next = (char*)utf8_write((u_char*)&buffer[length], value);
length += next - &buffer[length];
} break;
} case't': if (buffer != NULL) buffer[length] = '\t'; ptr += 2; length++; break; case'n': if (buffer != NULL) buffer[length] = '\n'; ptr += 2; length++; break; case'r': if (buffer != NULL) buffer[length] = '\r'; ptr += 2; length++; break; case'f': if (buffer != NULL) buffer[length] = '\f'; ptr += 2; length++; break; default:
ShouldNotReachHere();
}
}
} if (round == 0) {
buffer = NEW_RESOURCE_ARRAY(char, length + 1);
ptr = quoted_ascii_str;
} else {
buffer[length] = '\0';
}
} return buffer;
} #endif// !PRODUCT
bool UTF8::equal(const jbyte* base1, int length1, const jbyte* base2, int length2) { // Length must be the same if (length1 != length2) returnfalse; for (int i = 0; i < length1; i++) { if (base1[i] != base2[i]) returnfalse;
} returntrue;
}
bool UNICODE::is_latin1(jchar c) { return (c <= 0x00FF);
}
bool UNICODE::is_latin1(const jchar* base, int length) { for (int index = 0; index < length; index++) { if (base[index] > 0x00FF) { returnfalse;
}
} returntrue;
}
int UNICODE::utf8_size(jchar c) { if ((0x0001 <= c) && (c <= 0x007F)) { // ASCII character return 1;
} elseif (c <= 0x07FF) { return 2;
} else { return 3;
}
}
int UNICODE::utf8_size(jbyte c) { if (c >= 0x01) { // ASCII character. Check is equivalent to // (0x01 <= c) && (c <= 0x7F) because c is signed. return 1;
} else { // Non-ASCII character or 0x00 which needs to be // two-byte encoded as 0xC080 in modified UTF-8. return 2;
}
}
template<typename T> int UNICODE::utf8_length(const T* base, int length) { int result = 0; for (int index = 0; index < length; index++) {
T c = base[index];
result += utf8_size(c);
} return result;
}
template<typename T> char* UNICODE::as_utf8(const T* base, int& length) { int utf8_len = utf8_length(base, length);
u_char* buf = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1); char* result = as_utf8(base, length, (char*) buf, utf8_len + 1);
assert((int) strlen(result) == utf8_len, "length prediction must be correct"); // Set string length to uft8 length
length = utf8_len; return (char*) result;
}
char* UNICODE::as_utf8(const jchar* base, int length, char* buf, int buflen) {
assert(buflen > 0, "zero length output buffer");
u_char* p = (u_char*)buf; for (int index = 0; index < length; index++) {
jchar c = base[index];
buflen -= utf8_size(c); if (buflen <= 0) break; // string is truncated
p = utf8_write(p, c);
}
*p = '\0'; return buf;
}
char* UNICODE::as_utf8(const jbyte* base, int length, char* buf, int buflen) {
assert(buflen > 0, "zero length output buffer");
u_char* p = (u_char*)buf; for (int index = 0; index < length; index++) {
jbyte c = base[index]; int sz = utf8_size(c);
buflen -= sz; if (buflen <= 0) break; // string is truncated if (sz == 1) { // Copy ASCII characters (UTF-8 is ASCII compatible)
*p++ = c;
} else { // Non-ASCII character or 0x00 which should // be encoded as 0xC080 in "modified" UTF8.
p = utf8_write(p, ((jchar) c) & 0xff);
}
}
*p = '\0'; return buf;
}
void UNICODE::convert_to_utf8(const jchar* base, int length, char* utf8_buffer) { for(int index = 0; index < length; index++) {
utf8_buffer = (char*)utf8_write((u_char*)utf8_buffer, base[index]);
}
*utf8_buffer = '\0';
}
// returns the quoted ascii length of a unicode string template<typename T> int UNICODE::quoted_ascii_length(const T* base, int length) { int result = 0; for (int i = 0; i < length; i++) {
T c = base[i]; if (c >= 32 && c < 127) {
result++;
} else {
result += 6;
}
} return result;
}
// converts a unicode string to quoted ascii template<typename T> void UNICODE::as_quoted_ascii(const T* base, int length, char* buf, int buflen) { char* p = buf; char* end = buf + buflen; for (int index = 0; index < length; index++) {
T c = base[index]; if (c >= 32 && c < 127) { if (p + 1 >= end) break; // string is truncated
*p++ = (char)c;
} else { if (p + 6 >= end) break; // string is truncated
sprintf(p, "\\u%04x", c);
p += 6;
}
}
*p = '\0';
}
// Explicit instantiation for all supported types. templateint UNICODE::utf8_length(const jbyte* base, int length); templateint UNICODE::utf8_length(const jchar* base, int length); templatechar* UNICODE::as_utf8(const jbyte* base, int& length); templatechar* UNICODE::as_utf8(const jchar* base, int& length); templateint UNICODE::quoted_ascii_length<jbyte>(const jbyte* base, int length); templateint UNICODE::quoted_ascii_length<jchar>(const jchar* base, int length); templatevoid UNICODE::as_quoted_ascii<jbyte>(const jbyte* base, int length, char* buf, intbuflen); templatevoid UNICODE::as_quoted_ascii<jchar>(const jchar* base, int length, char* buf, intbuflen);
Messung V0.5
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.