duke@435: /* duke@435: * Copyright 1997-2004 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_utf8.cpp.incl" 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; duke@435: const char *ptr = (const char *)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: duke@435: // Returns NULL if 'c' it not found. This only works as long duke@435: // as 'c' is an ASCII character duke@435: jbyte* UTF8::strrchr(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: duke@435: bool UTF8::equal(jbyte* base1, int length1, 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: }