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