aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "utilities/utf8.hpp" aoqi@0: aoqi@0: // Assume the utf8 string is in legal form and has been aoqi@0: // checked in the class file parser/format checker. aoqi@0: char* UTF8::next(const char* str, jchar* value) { aoqi@0: unsigned const char *ptr = (const unsigned char *)str; aoqi@0: unsigned char ch, ch2, ch3; aoqi@0: int length = -1; /* bad length */ aoqi@0: jchar result; aoqi@0: switch ((ch = ptr[0]) >> 4) { aoqi@0: default: aoqi@0: result = ch; aoqi@0: length = 1; aoqi@0: break; aoqi@0: aoqi@0: case 0x8: case 0x9: case 0xA: case 0xB: case 0xF: aoqi@0: /* Shouldn't happen. */ aoqi@0: break; aoqi@0: aoqi@0: case 0xC: case 0xD: aoqi@0: /* 110xxxxx 10xxxxxx */ aoqi@0: if (((ch2 = ptr[1]) & 0xC0) == 0x80) { aoqi@0: unsigned char high_five = ch & 0x1F; aoqi@0: unsigned char low_six = ch2 & 0x3F; aoqi@0: result = (high_five << 6) + low_six; aoqi@0: length = 2; aoqi@0: break; aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case 0xE: aoqi@0: /* 1110xxxx 10xxxxxx 10xxxxxx */ aoqi@0: if (((ch2 = ptr[1]) & 0xC0) == 0x80) { aoqi@0: if (((ch3 = ptr[2]) & 0xC0) == 0x80) { aoqi@0: unsigned char high_four = ch & 0x0f; aoqi@0: unsigned char mid_six = ch2 & 0x3f; aoqi@0: unsigned char low_six = ch3 & 0x3f; aoqi@0: result = (((high_four << 6) + mid_six) << 6) + low_six; aoqi@0: length = 3; aoqi@0: } aoqi@0: } aoqi@0: break; aoqi@0: } /* end of switch */ aoqi@0: aoqi@0: if (length <= 0) { aoqi@0: *value = ptr[0]; /* default bad result; */ aoqi@0: return (char*)(ptr + 1); // make progress somehow aoqi@0: } aoqi@0: aoqi@0: *value = result; aoqi@0: aoqi@0: // The assert is correct but the .class file is wrong aoqi@0: // assert(UNICODE::utf8_size(result) == length, "checking reverse computation"); aoqi@0: return (char *)(ptr + length); aoqi@0: } aoqi@0: aoqi@0: char* UTF8::next_character(const char* str, jint* value) { aoqi@0: unsigned const char *ptr = (const unsigned char *)str; aoqi@0: /* See if it's legal supplementary character: aoqi@0: 11101101 1010xxxx 10xxxxxx 11101101 1011xxxx 10xxxxxx */ aoqi@0: if (is_supplementary_character(ptr)) { aoqi@0: *value = get_supplementary_character(ptr); aoqi@0: return (char *)(ptr + 6); aoqi@0: } aoqi@0: jchar result; aoqi@0: char* next_ch = next(str, &result); aoqi@0: *value = result; aoqi@0: return next_ch; aoqi@0: } aoqi@0: aoqi@0: // Count bytes of the form 10xxxxxx and deduct this count aoqi@0: // from the total byte count. The utf8 string must be in aoqi@0: // legal form which has been verified in the format checker. aoqi@0: int UTF8::unicode_length(const char* str, int len) { aoqi@0: int num_chars = len; aoqi@0: for (int i = 0; i < len; i++) { aoqi@0: if ((str[i] & 0xC0) == 0x80) { aoqi@0: --num_chars; aoqi@0: } aoqi@0: } aoqi@0: return num_chars; aoqi@0: } aoqi@0: aoqi@0: // Count bytes of the utf8 string except those in form aoqi@0: // 10xxxxxx which only appear in multibyte characters. aoqi@0: // The utf8 string must be in legal form and has been aoqi@0: // verified in the format checker. aoqi@0: int UTF8::unicode_length(const char* str) { aoqi@0: int num_chars = 0; aoqi@0: for (const char* p = str; *p; p++) { aoqi@0: if (((*p) & 0xC0) != 0x80) { aoqi@0: num_chars++; aoqi@0: } aoqi@0: } aoqi@0: return num_chars; aoqi@0: } aoqi@0: aoqi@0: // Writes a jchar a utf8 and returns the end aoqi@0: static u_char* utf8_write(u_char* base, jchar ch) { aoqi@0: if ((ch != 0) && (ch <=0x7f)) { aoqi@0: base[0] = (u_char) ch; aoqi@0: return base + 1; aoqi@0: } aoqi@0: aoqi@0: if (ch <= 0x7FF) { aoqi@0: /* 11 bits or less. */ aoqi@0: unsigned char high_five = ch >> 6; aoqi@0: unsigned char low_six = ch & 0x3F; aoqi@0: base[0] = high_five | 0xC0; /* 110xxxxx */ aoqi@0: base[1] = low_six | 0x80; /* 10xxxxxx */ aoqi@0: return base + 2; aoqi@0: } aoqi@0: /* possibly full 16 bits. */ aoqi@0: char high_four = ch >> 12; aoqi@0: char mid_six = (ch >> 6) & 0x3F; aoqi@0: char low_six = ch & 0x3f; aoqi@0: base[0] = high_four | 0xE0; /* 1110xxxx */ aoqi@0: base[1] = mid_six | 0x80; /* 10xxxxxx */ aoqi@0: base[2] = low_six | 0x80; /* 10xxxxxx */ aoqi@0: return base + 3; aoqi@0: } aoqi@0: aoqi@0: void UTF8::convert_to_unicode(const char* utf8_str, jchar* unicode_str, int unicode_length) { aoqi@0: unsigned char ch; aoqi@0: const char *ptr = utf8_str; aoqi@0: int index = 0; aoqi@0: aoqi@0: /* ASCII case loop optimization */ aoqi@0: for (; index < unicode_length; index++) { aoqi@0: if((ch = ptr[0]) > 0x7F) { break; } aoqi@0: unicode_str[index] = ch; aoqi@0: ptr = (const char *)(ptr + 1); aoqi@0: } aoqi@0: aoqi@0: for (; index < unicode_length; index++) { aoqi@0: ptr = UTF8::next(ptr, &unicode_str[index]); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // returns the quoted ascii length of a 0-terminated utf8 string aoqi@0: int UTF8::quoted_ascii_length(const char* utf8_str, int utf8_length) { aoqi@0: const char *ptr = utf8_str; aoqi@0: const char* end = ptr + utf8_length; aoqi@0: int result = 0; aoqi@0: while (ptr < end) { aoqi@0: jchar c; aoqi@0: ptr = UTF8::next(ptr, &c); aoqi@0: if (c >= 32 && c < 127) { aoqi@0: result++; aoqi@0: } else { aoqi@0: result += 6; aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: // converts a utf8 string to quoted ascii aoqi@0: void UTF8::as_quoted_ascii(const char* utf8_str, int utf8_length, char* buf, int buflen) { aoqi@0: const char *ptr = utf8_str; aoqi@0: const char *utf8_end = ptr + utf8_length; aoqi@0: char* p = buf; aoqi@0: char* end = buf + buflen; aoqi@0: while (ptr < utf8_end) { aoqi@0: jchar c; aoqi@0: ptr = UTF8::next(ptr, &c); aoqi@0: if (c >= 32 && c < 127) { aoqi@0: if (p + 1 >= end) break; // string is truncated aoqi@0: *p++ = (char)c; aoqi@0: } else { aoqi@0: if (p + 6 >= end) break; // string is truncated aoqi@0: sprintf(p, "\\u%04x", c); aoqi@0: p += 6; aoqi@0: } aoqi@0: } aoqi@0: assert(p < end, "sanity"); aoqi@0: *p = '\0'; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: const char* UTF8::from_quoted_ascii(const char* quoted_ascii_str) { aoqi@0: const char *ptr = quoted_ascii_str; aoqi@0: char* result = NULL; aoqi@0: while (*ptr != '\0') { aoqi@0: char c = *ptr; aoqi@0: if (c < 32 || c >= 127) break; aoqi@0: } aoqi@0: if (*ptr == '\0') { aoqi@0: // nothing to do so return original string aoqi@0: return quoted_ascii_str; aoqi@0: } aoqi@0: // everything up to this point was ok. aoqi@0: int length = ptr - quoted_ascii_str; aoqi@0: char* buffer = NULL; aoqi@0: for (int round = 0; round < 2; round++) { aoqi@0: while (*ptr != '\0') { aoqi@0: if (*ptr != '\\') { aoqi@0: if (buffer != NULL) { aoqi@0: buffer[length] = *ptr; aoqi@0: } aoqi@0: length++; aoqi@0: } else { aoqi@0: switch (ptr[1]) { aoqi@0: case 'u': { aoqi@0: ptr += 2; aoqi@0: jchar value=0; aoqi@0: for (int i=0; i<4; i++) { aoqi@0: char c = *ptr++; aoqi@0: switch (c) { aoqi@0: case '0': case '1': case '2': case '3': case '4': aoqi@0: case '5': case '6': case '7': case '8': case '9': aoqi@0: value = (value << 4) + c - '0'; aoqi@0: break; aoqi@0: case 'a': case 'b': case 'c': aoqi@0: case 'd': case 'e': case 'f': aoqi@0: value = (value << 4) + 10 + c - 'a'; aoqi@0: break; aoqi@0: case 'A': case 'B': case 'C': aoqi@0: case 'D': case 'E': case 'F': aoqi@0: value = (value << 4) + 10 + c - 'A'; aoqi@0: break; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: if (buffer == NULL) { aoqi@0: char utf8_buffer[4]; aoqi@0: char* next = (char*)utf8_write((u_char*)utf8_buffer, value); aoqi@0: length += next - utf8_buffer; aoqi@0: } else { aoqi@0: char* next = (char*)utf8_write((u_char*)&buffer[length], value); aoqi@0: length += next - &buffer[length]; aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case 't': if (buffer != NULL) buffer[length] = '\t'; ptr += 2; length++; break; aoqi@0: case 'n': if (buffer != NULL) buffer[length] = '\n'; ptr += 2; length++; break; aoqi@0: case 'r': if (buffer != NULL) buffer[length] = '\r'; ptr += 2; length++; break; aoqi@0: case 'f': if (buffer != NULL) buffer[length] = '\f'; ptr += 2; length++; break; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (round == 0) { aoqi@0: buffer = NEW_RESOURCE_ARRAY(char, length + 1); aoqi@0: ptr = quoted_ascii_str; aoqi@0: } else { aoqi@0: buffer[length] = '\0'; aoqi@0: } aoqi@0: } aoqi@0: return buffer; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Returns NULL if 'c' it not found. This only works as long aoqi@0: // as 'c' is an ASCII character aoqi@0: const jbyte* UTF8::strrchr(const jbyte* base, int length, jbyte c) { aoqi@0: assert(length >= 0, "sanity check"); aoqi@0: assert(c >= 0, "does not work for non-ASCII characters"); aoqi@0: // Skip backwards in string until 'c' is found or end is reached aoqi@0: while(--length >= 0 && base[length] != c); aoqi@0: return (length < 0) ? NULL : &base[length]; aoqi@0: } aoqi@0: aoqi@0: bool UTF8::equal(const jbyte* base1, int length1, const jbyte* base2, int length2) { aoqi@0: // Length must be the same aoqi@0: if (length1 != length2) return false; aoqi@0: for (int i = 0; i < length1; i++) { aoqi@0: if (base1[i] != base2[i]) return false; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: bool UTF8::is_supplementary_character(const unsigned char* str) { aoqi@0: return ((str[0] & 0xFF) == 0xED) && ((str[1] & 0xF0) == 0xA0) && ((str[2] & 0xC0) == 0x80) aoqi@0: && ((str[3] & 0xFF) == 0xED) && ((str[4] & 0xF0) == 0xB0) && ((str[5] & 0xC0) == 0x80); aoqi@0: } aoqi@0: aoqi@0: jint UTF8::get_supplementary_character(const unsigned char* str) { aoqi@0: return 0x10000 + ((str[1] & 0x0f) << 16) + ((str[2] & 0x3f) << 10) aoqi@0: + ((str[4] & 0x0f) << 6) + (str[5] & 0x3f); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //------------------------------------------------------------------------------------- aoqi@0: aoqi@0: aoqi@0: int UNICODE::utf8_size(jchar c) { aoqi@0: if ((0x0001 <= c) && (c <= 0x007F)) return 1; aoqi@0: if (c <= 0x07FF) return 2; aoqi@0: return 3; aoqi@0: } aoqi@0: aoqi@0: int UNICODE::utf8_length(jchar* base, int length) { aoqi@0: int result = 0; aoqi@0: for (int index = 0; index < length; index++) { aoqi@0: jchar c = base[index]; aoqi@0: if ((0x0001 <= c) && (c <= 0x007F)) result += 1; aoqi@0: else if (c <= 0x07FF) result += 2; aoqi@0: else result += 3; aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: char* UNICODE::as_utf8(jchar* base, int length) { aoqi@0: int utf8_len = utf8_length(base, length); aoqi@0: u_char* result = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1); aoqi@0: u_char* p = result; aoqi@0: for (int index = 0; index < length; index++) { aoqi@0: p = utf8_write(p, base[index]); aoqi@0: } aoqi@0: *p = '\0'; aoqi@0: assert(p == &result[utf8_len], "length prediction must be correct"); aoqi@0: return (char*) result; aoqi@0: } aoqi@0: aoqi@0: char* UNICODE::as_utf8(jchar* base, int length, char* buf, int buflen) { aoqi@0: u_char* p = (u_char*)buf; aoqi@0: u_char* end = (u_char*)buf + buflen; aoqi@0: for (int index = 0; index < length; index++) { aoqi@0: jchar c = base[index]; aoqi@0: if (p + utf8_size(c) >= end) break; // string is truncated aoqi@0: p = utf8_write(p, base[index]); aoqi@0: } aoqi@0: *p = '\0'; aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: void UNICODE::convert_to_utf8(const jchar* base, int length, char* utf8_buffer) { aoqi@0: for(int index = 0; index < length; index++) { aoqi@0: utf8_buffer = (char*)utf8_write((u_char*)utf8_buffer, base[index]); aoqi@0: } aoqi@0: *utf8_buffer = '\0'; aoqi@0: } aoqi@0: aoqi@0: // returns the quoted ascii length of a unicode string aoqi@0: int UNICODE::quoted_ascii_length(jchar* base, int length) { aoqi@0: int result = 0; aoqi@0: for (int i = 0; i < length; i++) { aoqi@0: jchar c = base[i]; aoqi@0: if (c >= 32 && c < 127) { aoqi@0: result++; aoqi@0: } else { aoqi@0: result += 6; aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: // converts a utf8 string to quoted ascii aoqi@0: void UNICODE::as_quoted_ascii(const jchar* base, int length, char* buf, int buflen) { aoqi@0: char* p = buf; aoqi@0: char* end = buf + buflen; aoqi@0: for (int index = 0; index < length; index++) { aoqi@0: jchar c = base[index]; aoqi@0: if (c >= 32 && c < 127) { aoqi@0: if (p + 1 >= end) break; // string is truncated aoqi@0: *p++ = (char)c; aoqi@0: } else { aoqi@0: if (p + 6 >= end) break; // string is truncated aoqi@0: sprintf(p, "\\u%04x", c); aoqi@0: p += 6; aoqi@0: } aoqi@0: } aoqi@0: *p = '\0'; aoqi@0: }