src/share/vm/utilities/utf8.cpp

Fri, 16 Aug 2013 10:06:58 -0700

author
dcubed
date
Fri, 16 Aug 2013 10:06:58 -0700
changeset 5529
e5003079dfa5
parent 4851
8c03fc47511d
child 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

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

mercurial