1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/utf8.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,244 @@ 1.4 +/* 1.5 + * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_utf8.cpp.incl" 1.30 + 1.31 +// Assume the utf8 string is in legal form and has been 1.32 +// checked in the class file parser/format checker. 1.33 +char* UTF8::next(const char* str, jchar* value) { 1.34 + unsigned const char *ptr = (const unsigned char *)str; 1.35 + unsigned char ch, ch2, ch3; 1.36 + int length = -1; /* bad length */ 1.37 + jchar result; 1.38 + switch ((ch = ptr[0]) >> 4) { 1.39 + default: 1.40 + result = ch; 1.41 + length = 1; 1.42 + break; 1.43 + 1.44 + case 0x8: case 0x9: case 0xA: case 0xB: case 0xF: 1.45 + /* Shouldn't happen. */ 1.46 + break; 1.47 + 1.48 + case 0xC: case 0xD: 1.49 + /* 110xxxxx 10xxxxxx */ 1.50 + if (((ch2 = ptr[1]) & 0xC0) == 0x80) { 1.51 + unsigned char high_five = ch & 0x1F; 1.52 + unsigned char low_six = ch2 & 0x3F; 1.53 + result = (high_five << 6) + low_six; 1.54 + length = 2; 1.55 + break; 1.56 + } 1.57 + break; 1.58 + 1.59 + case 0xE: 1.60 + /* 1110xxxx 10xxxxxx 10xxxxxx */ 1.61 + if (((ch2 = ptr[1]) & 0xC0) == 0x80) { 1.62 + if (((ch3 = ptr[2]) & 0xC0) == 0x80) { 1.63 + unsigned char high_four = ch & 0x0f; 1.64 + unsigned char mid_six = ch2 & 0x3f; 1.65 + unsigned char low_six = ch3 & 0x3f; 1.66 + result = (((high_four << 6) + mid_six) << 6) + low_six; 1.67 + length = 3; 1.68 + } 1.69 + } 1.70 + break; 1.71 + } /* end of switch */ 1.72 + 1.73 + if (length <= 0) { 1.74 + *value = ptr[0]; /* default bad result; */ 1.75 + return (char*)(ptr + 1); // make progress somehow 1.76 + } 1.77 + 1.78 + *value = result; 1.79 + 1.80 + // The assert is correct but the .class file is wrong 1.81 + // assert(UNICODE::utf8_size(result) == length, "checking reverse computation"); 1.82 + return (char *)(ptr + length); 1.83 +} 1.84 + 1.85 +char* UTF8::next_character(const char* str, jint* value) { 1.86 + unsigned const char *ptr = (const unsigned char *)str; 1.87 + /* See if it's legal supplementary character: 1.88 + 11101101 1010xxxx 10xxxxxx 11101101 1011xxxx 10xxxxxx */ 1.89 + if (is_supplementary_character(ptr)) { 1.90 + *value = get_supplementary_character(ptr); 1.91 + return (char *)(ptr + 6); 1.92 + } 1.93 + jchar result; 1.94 + char* next_ch = next(str, &result); 1.95 + *value = result; 1.96 + return next_ch; 1.97 +} 1.98 + 1.99 +// Count bytes of the form 10xxxxxx and deduct this count 1.100 +// from the total byte count. The utf8 string must be in 1.101 +// legal form which has been verified in the format checker. 1.102 +int UTF8::unicode_length(const char* str, int len) { 1.103 + int num_chars = len; 1.104 + for (int i = 0; i < len; i++) { 1.105 + if ((str[i] & 0xC0) == 0x80) { 1.106 + --num_chars; 1.107 + } 1.108 + } 1.109 + return num_chars; 1.110 +} 1.111 + 1.112 +// Count bytes of the utf8 string except those in form 1.113 +// 10xxxxxx which only appear in multibyte characters. 1.114 +// The utf8 string must be in legal form and has been 1.115 +// verified in the format checker. 1.116 +int UTF8::unicode_length(const char* str) { 1.117 + int num_chars = 0; 1.118 + for (const char* p = str; *p; p++) { 1.119 + if (((*p) & 0xC0) != 0x80) { 1.120 + num_chars++; 1.121 + } 1.122 + } 1.123 + return num_chars; 1.124 +} 1.125 + 1.126 +// Writes a jchar a utf8 and returns the end 1.127 +static u_char* utf8_write(u_char* base, jchar ch) { 1.128 + if ((ch != 0) && (ch <=0x7f)) { 1.129 + base[0] = (u_char) ch; 1.130 + return base + 1; 1.131 + } 1.132 + 1.133 + if (ch <= 0x7FF) { 1.134 + /* 11 bits or less. */ 1.135 + unsigned char high_five = ch >> 6; 1.136 + unsigned char low_six = ch & 0x3F; 1.137 + base[0] = high_five | 0xC0; /* 110xxxxx */ 1.138 + base[1] = low_six | 0x80; /* 10xxxxxx */ 1.139 + return base + 2; 1.140 + } 1.141 + /* possibly full 16 bits. */ 1.142 + char high_four = ch >> 12; 1.143 + char mid_six = (ch >> 6) & 0x3F; 1.144 + char low_six = ch & 0x3f; 1.145 + base[0] = high_four | 0xE0; /* 1110xxxx */ 1.146 + base[1] = mid_six | 0x80; /* 10xxxxxx */ 1.147 + base[2] = low_six | 0x80; /* 10xxxxxx */ 1.148 + return base + 3; 1.149 +} 1.150 + 1.151 +void UTF8::convert_to_unicode(const char* utf8_str, jchar* unicode_str, int unicode_length) { 1.152 + unsigned char ch; 1.153 + const char *ptr = (const char *)utf8_str; 1.154 + int index = 0; 1.155 + 1.156 + /* ASCII case loop optimization */ 1.157 + for (; index < unicode_length; index++) { 1.158 + if((ch = ptr[0]) > 0x7F) { break; } 1.159 + unicode_str[index] = ch; 1.160 + ptr = (const char *)(ptr + 1); 1.161 + } 1.162 + 1.163 + for (; index < unicode_length; index++) { 1.164 + ptr = UTF8::next(ptr, &unicode_str[index]); 1.165 + } 1.166 +} 1.167 + 1.168 +// Returns NULL if 'c' it not found. This only works as long 1.169 +// as 'c' is an ASCII character 1.170 +jbyte* UTF8::strrchr(jbyte* base, int length, jbyte c) { 1.171 + assert(length >= 0, "sanity check"); 1.172 + assert(c >= 0, "does not work for non-ASCII characters"); 1.173 + // Skip backwards in string until 'c' is found or end is reached 1.174 + while(--length >= 0 && base[length] != c); 1.175 + return (length < 0) ? NULL : &base[length]; 1.176 +} 1.177 + 1.178 +bool UTF8::equal(jbyte* base1, int length1, jbyte* base2, int length2) { 1.179 + // Length must be the same 1.180 + if (length1 != length2) return false; 1.181 + for (int i = 0; i < length1; i++) { 1.182 + if (base1[i] != base2[i]) return false; 1.183 + } 1.184 + return true; 1.185 +} 1.186 + 1.187 +bool UTF8::is_supplementary_character(const unsigned char* str) { 1.188 + return ((str[0] & 0xFF) == 0xED) && ((str[1] & 0xF0) == 0xA0) && ((str[2] & 0xC0) == 0x80) 1.189 + && ((str[3] & 0xFF) == 0xED) && ((str[4] & 0xF0) == 0xB0) && ((str[5] & 0xC0) == 0x80); 1.190 +} 1.191 + 1.192 +jint UTF8::get_supplementary_character(const unsigned char* str) { 1.193 + return 0x10000 + ((str[1] & 0x0f) << 16) + ((str[2] & 0x3f) << 10) 1.194 + + ((str[4] & 0x0f) << 6) + (str[5] & 0x3f); 1.195 +} 1.196 + 1.197 + 1.198 +//------------------------------------------------------------------------------------- 1.199 + 1.200 + 1.201 +int UNICODE::utf8_size(jchar c) { 1.202 + if ((0x0001 <= c) && (c <= 0x007F)) return 1; 1.203 + if (c <= 0x07FF) return 2; 1.204 + return 3; 1.205 +} 1.206 + 1.207 +int UNICODE::utf8_length(jchar* base, int length) { 1.208 + int result = 0; 1.209 + for (int index = 0; index < length; index++) { 1.210 + jchar c = base[index]; 1.211 + if ((0x0001 <= c) && (c <= 0x007F)) result += 1; 1.212 + else if (c <= 0x07FF) result += 2; 1.213 + else result += 3; 1.214 + } 1.215 + return result; 1.216 +} 1.217 + 1.218 +char* UNICODE::as_utf8(jchar* base, int length) { 1.219 + int utf8_len = utf8_length(base, length); 1.220 + u_char* result = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1); 1.221 + u_char* p = result; 1.222 + for (int index = 0; index < length; index++) { 1.223 + p = utf8_write(p, base[index]); 1.224 + } 1.225 + *p = '\0'; 1.226 + assert(p == &result[utf8_len], "length prediction must be correct"); 1.227 + return (char*) result; 1.228 +} 1.229 + 1.230 +char* UNICODE::as_utf8(jchar* base, int length, char* buf, int buflen) { 1.231 + u_char* p = (u_char*)buf; 1.232 + u_char* end = (u_char*)buf + buflen; 1.233 + for (int index = 0; index < length; index++) { 1.234 + jchar c = base[index]; 1.235 + if (p + utf8_size(c) >= end) break; // string is truncated 1.236 + p = utf8_write(p, base[index]); 1.237 + } 1.238 + *p = '\0'; 1.239 + return buf; 1.240 +} 1.241 + 1.242 +void UNICODE::convert_to_utf8(const jchar* base, int length, char* utf8_buffer) { 1.243 + for(int index = 0; index < length; index++) { 1.244 + utf8_buffer = (char*)utf8_write((u_char*)utf8_buffer, base[index]); 1.245 + } 1.246 + *utf8_buffer = '\0'; 1.247 +}