src/share/vm/utilities/utf8.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "utilities/utf8.hpp"
aoqi@0 27
aoqi@0 28 // Assume the utf8 string is in legal form and has been
aoqi@0 29 // checked in the class file parser/format checker.
aoqi@0 30 char* UTF8::next(const char* str, jchar* value) {
aoqi@0 31 unsigned const char *ptr = (const unsigned char *)str;
aoqi@0 32 unsigned char ch, ch2, ch3;
aoqi@0 33 int length = -1; /* bad length */
aoqi@0 34 jchar result;
aoqi@0 35 switch ((ch = ptr[0]) >> 4) {
aoqi@0 36 default:
aoqi@0 37 result = ch;
aoqi@0 38 length = 1;
aoqi@0 39 break;
aoqi@0 40
aoqi@0 41 case 0x8: case 0x9: case 0xA: case 0xB: case 0xF:
aoqi@0 42 /* Shouldn't happen. */
aoqi@0 43 break;
aoqi@0 44
aoqi@0 45 case 0xC: case 0xD:
aoqi@0 46 /* 110xxxxx 10xxxxxx */
aoqi@0 47 if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
aoqi@0 48 unsigned char high_five = ch & 0x1F;
aoqi@0 49 unsigned char low_six = ch2 & 0x3F;
aoqi@0 50 result = (high_five << 6) + low_six;
aoqi@0 51 length = 2;
aoqi@0 52 break;
aoqi@0 53 }
aoqi@0 54 break;
aoqi@0 55
aoqi@0 56 case 0xE:
aoqi@0 57 /* 1110xxxx 10xxxxxx 10xxxxxx */
aoqi@0 58 if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
aoqi@0 59 if (((ch3 = ptr[2]) & 0xC0) == 0x80) {
aoqi@0 60 unsigned char high_four = ch & 0x0f;
aoqi@0 61 unsigned char mid_six = ch2 & 0x3f;
aoqi@0 62 unsigned char low_six = ch3 & 0x3f;
aoqi@0 63 result = (((high_four << 6) + mid_six) << 6) + low_six;
aoqi@0 64 length = 3;
aoqi@0 65 }
aoqi@0 66 }
aoqi@0 67 break;
aoqi@0 68 } /* end of switch */
aoqi@0 69
aoqi@0 70 if (length <= 0) {
aoqi@0 71 *value = ptr[0]; /* default bad result; */
aoqi@0 72 return (char*)(ptr + 1); // make progress somehow
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 *value = result;
aoqi@0 76
aoqi@0 77 // The assert is correct but the .class file is wrong
aoqi@0 78 // assert(UNICODE::utf8_size(result) == length, "checking reverse computation");
aoqi@0 79 return (char *)(ptr + length);
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 char* UTF8::next_character(const char* str, jint* value) {
aoqi@0 83 unsigned const char *ptr = (const unsigned char *)str;
aoqi@0 84 /* See if it's legal supplementary character:
aoqi@0 85 11101101 1010xxxx 10xxxxxx 11101101 1011xxxx 10xxxxxx */
aoqi@0 86 if (is_supplementary_character(ptr)) {
aoqi@0 87 *value = get_supplementary_character(ptr);
aoqi@0 88 return (char *)(ptr + 6);
aoqi@0 89 }
aoqi@0 90 jchar result;
aoqi@0 91 char* next_ch = next(str, &result);
aoqi@0 92 *value = result;
aoqi@0 93 return next_ch;
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 // Count bytes of the form 10xxxxxx and deduct this count
aoqi@0 97 // from the total byte count. The utf8 string must be in
aoqi@0 98 // legal form which has been verified in the format checker.
aoqi@0 99 int UTF8::unicode_length(const char* str, int len) {
aoqi@0 100 int num_chars = len;
aoqi@0 101 for (int i = 0; i < len; i++) {
aoqi@0 102 if ((str[i] & 0xC0) == 0x80) {
aoqi@0 103 --num_chars;
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106 return num_chars;
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 // Count bytes of the utf8 string except those in form
aoqi@0 110 // 10xxxxxx which only appear in multibyte characters.
aoqi@0 111 // The utf8 string must be in legal form and has been
aoqi@0 112 // verified in the format checker.
aoqi@0 113 int UTF8::unicode_length(const char* str) {
aoqi@0 114 int num_chars = 0;
aoqi@0 115 for (const char* p = str; *p; p++) {
aoqi@0 116 if (((*p) & 0xC0) != 0x80) {
aoqi@0 117 num_chars++;
aoqi@0 118 }
aoqi@0 119 }
aoqi@0 120 return num_chars;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 // Writes a jchar a utf8 and returns the end
aoqi@0 124 static u_char* utf8_write(u_char* base, jchar ch) {
aoqi@0 125 if ((ch != 0) && (ch <=0x7f)) {
aoqi@0 126 base[0] = (u_char) ch;
aoqi@0 127 return base + 1;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 if (ch <= 0x7FF) {
aoqi@0 131 /* 11 bits or less. */
aoqi@0 132 unsigned char high_five = ch >> 6;
aoqi@0 133 unsigned char low_six = ch & 0x3F;
aoqi@0 134 base[0] = high_five | 0xC0; /* 110xxxxx */
aoqi@0 135 base[1] = low_six | 0x80; /* 10xxxxxx */
aoqi@0 136 return base + 2;
aoqi@0 137 }
aoqi@0 138 /* possibly full 16 bits. */
aoqi@0 139 char high_four = ch >> 12;
aoqi@0 140 char mid_six = (ch >> 6) & 0x3F;
aoqi@0 141 char low_six = ch & 0x3f;
aoqi@0 142 base[0] = high_four | 0xE0; /* 1110xxxx */
aoqi@0 143 base[1] = mid_six | 0x80; /* 10xxxxxx */
aoqi@0 144 base[2] = low_six | 0x80; /* 10xxxxxx */
aoqi@0 145 return base + 3;
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 void UTF8::convert_to_unicode(const char* utf8_str, jchar* unicode_str, int unicode_length) {
aoqi@0 149 unsigned char ch;
aoqi@0 150 const char *ptr = utf8_str;
aoqi@0 151 int index = 0;
aoqi@0 152
aoqi@0 153 /* ASCII case loop optimization */
aoqi@0 154 for (; index < unicode_length; index++) {
aoqi@0 155 if((ch = ptr[0]) > 0x7F) { break; }
aoqi@0 156 unicode_str[index] = ch;
aoqi@0 157 ptr = (const char *)(ptr + 1);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 for (; index < unicode_length; index++) {
aoqi@0 161 ptr = UTF8::next(ptr, &unicode_str[index]);
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 // returns the quoted ascii length of a 0-terminated utf8 string
aoqi@0 166 int UTF8::quoted_ascii_length(const char* utf8_str, int utf8_length) {
aoqi@0 167 const char *ptr = utf8_str;
aoqi@0 168 const char* end = ptr + utf8_length;
aoqi@0 169 int result = 0;
aoqi@0 170 while (ptr < end) {
aoqi@0 171 jchar c;
aoqi@0 172 ptr = UTF8::next(ptr, &c);
aoqi@0 173 if (c >= 32 && c < 127) {
aoqi@0 174 result++;
aoqi@0 175 } else {
aoqi@0 176 result += 6;
aoqi@0 177 }
aoqi@0 178 }
aoqi@0 179 return result;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 // converts a utf8 string to quoted ascii
aoqi@0 183 void UTF8::as_quoted_ascii(const char* utf8_str, int utf8_length, char* buf, int buflen) {
aoqi@0 184 const char *ptr = utf8_str;
aoqi@0 185 const char *utf8_end = ptr + utf8_length;
aoqi@0 186 char* p = buf;
aoqi@0 187 char* end = buf + buflen;
aoqi@0 188 while (ptr < utf8_end) {
aoqi@0 189 jchar c;
aoqi@0 190 ptr = UTF8::next(ptr, &c);
aoqi@0 191 if (c >= 32 && c < 127) {
aoqi@0 192 if (p + 1 >= end) break; // string is truncated
aoqi@0 193 *p++ = (char)c;
aoqi@0 194 } else {
aoqi@0 195 if (p + 6 >= end) break; // string is truncated
aoqi@0 196 sprintf(p, "\\u%04x", c);
aoqi@0 197 p += 6;
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200 assert(p < end, "sanity");
aoqi@0 201 *p = '\0';
aoqi@0 202 }
aoqi@0 203
aoqi@0 204
aoqi@0 205 const char* UTF8::from_quoted_ascii(const char* quoted_ascii_str) {
aoqi@0 206 const char *ptr = quoted_ascii_str;
aoqi@0 207 char* result = NULL;
aoqi@0 208 while (*ptr != '\0') {
aoqi@0 209 char c = *ptr;
aoqi@0 210 if (c < 32 || c >= 127) break;
aoqi@0 211 }
aoqi@0 212 if (*ptr == '\0') {
aoqi@0 213 // nothing to do so return original string
aoqi@0 214 return quoted_ascii_str;
aoqi@0 215 }
aoqi@0 216 // everything up to this point was ok.
aoqi@0 217 int length = ptr - quoted_ascii_str;
aoqi@0 218 char* buffer = NULL;
aoqi@0 219 for (int round = 0; round < 2; round++) {
aoqi@0 220 while (*ptr != '\0') {
aoqi@0 221 if (*ptr != '\\') {
aoqi@0 222 if (buffer != NULL) {
aoqi@0 223 buffer[length] = *ptr;
aoqi@0 224 }
aoqi@0 225 length++;
aoqi@0 226 } else {
aoqi@0 227 switch (ptr[1]) {
aoqi@0 228 case 'u': {
aoqi@0 229 ptr += 2;
aoqi@0 230 jchar value=0;
aoqi@0 231 for (int i=0; i<4; i++) {
aoqi@0 232 char c = *ptr++;
aoqi@0 233 switch (c) {
aoqi@0 234 case '0': case '1': case '2': case '3': case '4':
aoqi@0 235 case '5': case '6': case '7': case '8': case '9':
aoqi@0 236 value = (value << 4) + c - '0';
aoqi@0 237 break;
aoqi@0 238 case 'a': case 'b': case 'c':
aoqi@0 239 case 'd': case 'e': case 'f':
aoqi@0 240 value = (value << 4) + 10 + c - 'a';
aoqi@0 241 break;
aoqi@0 242 case 'A': case 'B': case 'C':
aoqi@0 243 case 'D': case 'E': case 'F':
aoqi@0 244 value = (value << 4) + 10 + c - 'A';
aoqi@0 245 break;
aoqi@0 246 default:
aoqi@0 247 ShouldNotReachHere();
aoqi@0 248 }
aoqi@0 249 }
aoqi@0 250 if (buffer == NULL) {
aoqi@0 251 char utf8_buffer[4];
aoqi@0 252 char* next = (char*)utf8_write((u_char*)utf8_buffer, value);
aoqi@0 253 length += next - utf8_buffer;
aoqi@0 254 } else {
aoqi@0 255 char* next = (char*)utf8_write((u_char*)&buffer[length], value);
aoqi@0 256 length += next - &buffer[length];
aoqi@0 257 }
aoqi@0 258 break;
aoqi@0 259 }
aoqi@0 260 case 't': if (buffer != NULL) buffer[length] = '\t'; ptr += 2; length++; break;
aoqi@0 261 case 'n': if (buffer != NULL) buffer[length] = '\n'; ptr += 2; length++; break;
aoqi@0 262 case 'r': if (buffer != NULL) buffer[length] = '\r'; ptr += 2; length++; break;
aoqi@0 263 case 'f': if (buffer != NULL) buffer[length] = '\f'; ptr += 2; length++; break;
aoqi@0 264 default:
aoqi@0 265 ShouldNotReachHere();
aoqi@0 266 }
aoqi@0 267 }
aoqi@0 268 }
aoqi@0 269 if (round == 0) {
aoqi@0 270 buffer = NEW_RESOURCE_ARRAY(char, length + 1);
aoqi@0 271 ptr = quoted_ascii_str;
aoqi@0 272 } else {
aoqi@0 273 buffer[length] = '\0';
aoqi@0 274 }
aoqi@0 275 }
aoqi@0 276 return buffer;
aoqi@0 277 }
aoqi@0 278
aoqi@0 279
aoqi@0 280 // Returns NULL if 'c' it not found. This only works as long
aoqi@0 281 // as 'c' is an ASCII character
aoqi@0 282 const jbyte* UTF8::strrchr(const jbyte* base, int length, jbyte c) {
aoqi@0 283 assert(length >= 0, "sanity check");
aoqi@0 284 assert(c >= 0, "does not work for non-ASCII characters");
aoqi@0 285 // Skip backwards in string until 'c' is found or end is reached
aoqi@0 286 while(--length >= 0 && base[length] != c);
aoqi@0 287 return (length < 0) ? NULL : &base[length];
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 bool UTF8::equal(const jbyte* base1, int length1, const jbyte* base2, int length2) {
aoqi@0 291 // Length must be the same
aoqi@0 292 if (length1 != length2) return false;
aoqi@0 293 for (int i = 0; i < length1; i++) {
aoqi@0 294 if (base1[i] != base2[i]) return false;
aoqi@0 295 }
aoqi@0 296 return true;
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 bool UTF8::is_supplementary_character(const unsigned char* str) {
aoqi@0 300 return ((str[0] & 0xFF) == 0xED) && ((str[1] & 0xF0) == 0xA0) && ((str[2] & 0xC0) == 0x80)
aoqi@0 301 && ((str[3] & 0xFF) == 0xED) && ((str[4] & 0xF0) == 0xB0) && ((str[5] & 0xC0) == 0x80);
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 jint UTF8::get_supplementary_character(const unsigned char* str) {
aoqi@0 305 return 0x10000 + ((str[1] & 0x0f) << 16) + ((str[2] & 0x3f) << 10)
aoqi@0 306 + ((str[4] & 0x0f) << 6) + (str[5] & 0x3f);
aoqi@0 307 }
aoqi@0 308
aoqi@0 309
aoqi@0 310 //-------------------------------------------------------------------------------------
aoqi@0 311
aoqi@0 312
aoqi@0 313 int UNICODE::utf8_size(jchar c) {
aoqi@0 314 if ((0x0001 <= c) && (c <= 0x007F)) return 1;
aoqi@0 315 if (c <= 0x07FF) return 2;
aoqi@0 316 return 3;
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 int UNICODE::utf8_length(jchar* base, int length) {
aoqi@0 320 int result = 0;
aoqi@0 321 for (int index = 0; index < length; index++) {
aoqi@0 322 jchar c = base[index];
aoqi@0 323 if ((0x0001 <= c) && (c <= 0x007F)) result += 1;
aoqi@0 324 else if (c <= 0x07FF) result += 2;
aoqi@0 325 else result += 3;
aoqi@0 326 }
aoqi@0 327 return result;
aoqi@0 328 }
aoqi@0 329
aoqi@0 330 char* UNICODE::as_utf8(jchar* base, int length) {
aoqi@0 331 int utf8_len = utf8_length(base, length);
aoqi@0 332 u_char* result = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1);
aoqi@0 333 u_char* p = result;
aoqi@0 334 for (int index = 0; index < length; index++) {
aoqi@0 335 p = utf8_write(p, base[index]);
aoqi@0 336 }
aoqi@0 337 *p = '\0';
aoqi@0 338 assert(p == &result[utf8_len], "length prediction must be correct");
aoqi@0 339 return (char*) result;
aoqi@0 340 }
aoqi@0 341
aoqi@0 342 char* UNICODE::as_utf8(jchar* base, int length, char* buf, int buflen) {
aoqi@0 343 u_char* p = (u_char*)buf;
aoqi@0 344 u_char* end = (u_char*)buf + buflen;
aoqi@0 345 for (int index = 0; index < length; index++) {
aoqi@0 346 jchar c = base[index];
aoqi@0 347 if (p + utf8_size(c) >= end) break; // string is truncated
aoqi@0 348 p = utf8_write(p, base[index]);
aoqi@0 349 }
aoqi@0 350 *p = '\0';
aoqi@0 351 return buf;
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 void UNICODE::convert_to_utf8(const jchar* base, int length, char* utf8_buffer) {
aoqi@0 355 for(int index = 0; index < length; index++) {
aoqi@0 356 utf8_buffer = (char*)utf8_write((u_char*)utf8_buffer, base[index]);
aoqi@0 357 }
aoqi@0 358 *utf8_buffer = '\0';
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 // returns the quoted ascii length of a unicode string
aoqi@0 362 int UNICODE::quoted_ascii_length(jchar* base, int length) {
aoqi@0 363 int result = 0;
aoqi@0 364 for (int i = 0; i < length; i++) {
aoqi@0 365 jchar c = base[i];
aoqi@0 366 if (c >= 32 && c < 127) {
aoqi@0 367 result++;
aoqi@0 368 } else {
aoqi@0 369 result += 6;
aoqi@0 370 }
aoqi@0 371 }
aoqi@0 372 return result;
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 // converts a utf8 string to quoted ascii
aoqi@0 376 void UNICODE::as_quoted_ascii(const jchar* base, int length, char* buf, int buflen) {
aoqi@0 377 char* p = buf;
aoqi@0 378 char* end = buf + buflen;
aoqi@0 379 for (int index = 0; index < length; index++) {
aoqi@0 380 jchar c = base[index];
aoqi@0 381 if (c >= 32 && c < 127) {
aoqi@0 382 if (p + 1 >= end) break; // string is truncated
aoqi@0 383 *p++ = (char)c;
aoqi@0 384 } else {
aoqi@0 385 if (p + 6 >= end) break; // string is truncated
aoqi@0 386 sprintf(p, "\\u%04x", c);
aoqi@0 387 p += 6;
aoqi@0 388 }
aoqi@0 389 }
aoqi@0 390 *p = '\0';
aoqi@0 391 }

mercurial