src/share/vm/utilities/utf8.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/utf8.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,391 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "utilities/utf8.hpp"
    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 = 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 the quoted ascii length of a 0-terminated utf8 string
   1.169 +int UTF8::quoted_ascii_length(const char* utf8_str, int utf8_length) {
   1.170 +  const char *ptr = utf8_str;
   1.171 +  const char* end = ptr + utf8_length;
   1.172 +  int result = 0;
   1.173 +  while (ptr < end) {
   1.174 +    jchar c;
   1.175 +    ptr = UTF8::next(ptr, &c);
   1.176 +    if (c >= 32 && c < 127) {
   1.177 +      result++;
   1.178 +    } else {
   1.179 +      result += 6;
   1.180 +    }
   1.181 +  }
   1.182 +  return result;
   1.183 +}
   1.184 +
   1.185 +// converts a utf8 string to quoted ascii
   1.186 +void UTF8::as_quoted_ascii(const char* utf8_str, int utf8_length, char* buf, int buflen) {
   1.187 +  const char *ptr = utf8_str;
   1.188 +  const char *utf8_end = ptr + utf8_length;
   1.189 +  char* p = buf;
   1.190 +  char* end = buf + buflen;
   1.191 +  while (ptr < utf8_end) {
   1.192 +    jchar c;
   1.193 +    ptr = UTF8::next(ptr, &c);
   1.194 +    if (c >= 32 && c < 127) {
   1.195 +      if (p + 1 >= end) break;      // string is truncated
   1.196 +      *p++ = (char)c;
   1.197 +    } else {
   1.198 +      if (p + 6 >= end) break;      // string is truncated
   1.199 +      sprintf(p, "\\u%04x", c);
   1.200 +      p += 6;
   1.201 +    }
   1.202 +  }
   1.203 +  assert(p < end, "sanity");
   1.204 +  *p = '\0';
   1.205 +}
   1.206 +
   1.207 +
   1.208 +const char* UTF8::from_quoted_ascii(const char* quoted_ascii_str) {
   1.209 +  const char *ptr = quoted_ascii_str;
   1.210 +  char* result = NULL;
   1.211 +  while (*ptr != '\0') {
   1.212 +    char c = *ptr;
   1.213 +    if (c < 32 || c >= 127) break;
   1.214 +  }
   1.215 +  if (*ptr == '\0') {
   1.216 +    // nothing to do so return original string
   1.217 +    return quoted_ascii_str;
   1.218 +  }
   1.219 +  // everything up to this point was ok.
   1.220 +  int length = ptr - quoted_ascii_str;
   1.221 +  char* buffer = NULL;
   1.222 +  for (int round = 0; round < 2; round++) {
   1.223 +    while (*ptr != '\0') {
   1.224 +      if (*ptr != '\\') {
   1.225 +        if (buffer != NULL) {
   1.226 +          buffer[length] = *ptr;
   1.227 +        }
   1.228 +        length++;
   1.229 +      } else {
   1.230 +        switch (ptr[1]) {
   1.231 +          case 'u': {
   1.232 +            ptr += 2;
   1.233 +            jchar value=0;
   1.234 +            for (int i=0; i<4; i++) {
   1.235 +              char c = *ptr++;
   1.236 +              switch (c) {
   1.237 +                case '0': case '1': case '2': case '3': case '4':
   1.238 +                case '5': case '6': case '7': case '8': case '9':
   1.239 +                  value = (value << 4) + c - '0';
   1.240 +                  break;
   1.241 +                case 'a': case 'b': case 'c':
   1.242 +                case 'd': case 'e': case 'f':
   1.243 +                  value = (value << 4) + 10 + c - 'a';
   1.244 +                  break;
   1.245 +                case 'A': case 'B': case 'C':
   1.246 +                case 'D': case 'E': case 'F':
   1.247 +                  value = (value << 4) + 10 + c - 'A';
   1.248 +                  break;
   1.249 +                default:
   1.250 +                  ShouldNotReachHere();
   1.251 +              }
   1.252 +            }
   1.253 +            if (buffer == NULL) {
   1.254 +              char utf8_buffer[4];
   1.255 +              char* next = (char*)utf8_write((u_char*)utf8_buffer, value);
   1.256 +              length += next - utf8_buffer;
   1.257 +            } else {
   1.258 +              char* next = (char*)utf8_write((u_char*)&buffer[length], value);
   1.259 +              length += next - &buffer[length];
   1.260 +            }
   1.261 +            break;
   1.262 +          }
   1.263 +          case 't': if (buffer != NULL) buffer[length] = '\t'; ptr += 2; length++; break;
   1.264 +          case 'n': if (buffer != NULL) buffer[length] = '\n'; ptr += 2; length++; break;
   1.265 +          case 'r': if (buffer != NULL) buffer[length] = '\r'; ptr += 2; length++; break;
   1.266 +          case 'f': if (buffer != NULL) buffer[length] = '\f'; ptr += 2; length++; break;
   1.267 +          default:
   1.268 +            ShouldNotReachHere();
   1.269 +        }
   1.270 +      }
   1.271 +    }
   1.272 +    if (round == 0) {
   1.273 +      buffer = NEW_RESOURCE_ARRAY(char, length + 1);
   1.274 +      ptr = quoted_ascii_str;
   1.275 +    } else {
   1.276 +      buffer[length] = '\0';
   1.277 +    }
   1.278 +  }
   1.279 +  return buffer;
   1.280 +}
   1.281 +
   1.282 +
   1.283 +// Returns NULL if 'c' it not found. This only works as long
   1.284 +// as 'c' is an ASCII character
   1.285 +const jbyte* UTF8::strrchr(const jbyte* base, int length, jbyte c) {
   1.286 +  assert(length >= 0, "sanity check");
   1.287 +  assert(c >= 0, "does not work for non-ASCII characters");
   1.288 +  // Skip backwards in string until 'c' is found or end is reached
   1.289 +  while(--length >= 0 && base[length] != c);
   1.290 +  return (length < 0) ? NULL : &base[length];
   1.291 +}
   1.292 +
   1.293 +bool UTF8::equal(const jbyte* base1, int length1, const jbyte* base2, int length2) {
   1.294 +  // Length must be the same
   1.295 +  if (length1 != length2) return false;
   1.296 +  for (int i = 0; i < length1; i++) {
   1.297 +    if (base1[i] != base2[i]) return false;
   1.298 +  }
   1.299 +  return true;
   1.300 +}
   1.301 +
   1.302 +bool UTF8::is_supplementary_character(const unsigned char* str) {
   1.303 +  return ((str[0] & 0xFF) == 0xED) && ((str[1] & 0xF0) == 0xA0) && ((str[2] & 0xC0) == 0x80)
   1.304 +      && ((str[3] & 0xFF) == 0xED) && ((str[4] & 0xF0) == 0xB0) && ((str[5] & 0xC0) == 0x80);
   1.305 +}
   1.306 +
   1.307 +jint UTF8::get_supplementary_character(const unsigned char* str) {
   1.308 +  return 0x10000 + ((str[1] & 0x0f) << 16) + ((str[2] & 0x3f) << 10)
   1.309 +                 + ((str[4] & 0x0f) << 6)  + (str[5] & 0x3f);
   1.310 +}
   1.311 +
   1.312 +
   1.313 +//-------------------------------------------------------------------------------------
   1.314 +
   1.315 +
   1.316 +int UNICODE::utf8_size(jchar c) {
   1.317 +  if ((0x0001 <= c) && (c <= 0x007F)) return 1;
   1.318 +  if (c <= 0x07FF) return 2;
   1.319 +  return 3;
   1.320 +}
   1.321 +
   1.322 +int UNICODE::utf8_length(jchar* base, int length) {
   1.323 +  int result = 0;
   1.324 +  for (int index = 0; index < length; index++) {
   1.325 +    jchar c = base[index];
   1.326 +    if ((0x0001 <= c) && (c <= 0x007F)) result += 1;
   1.327 +    else if (c <= 0x07FF) result += 2;
   1.328 +    else result += 3;
   1.329 +  }
   1.330 +  return result;
   1.331 +}
   1.332 +
   1.333 +char* UNICODE::as_utf8(jchar* base, int length) {
   1.334 +  int utf8_len = utf8_length(base, length);
   1.335 +  u_char* result = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1);
   1.336 +  u_char* p = result;
   1.337 +  for (int index = 0; index < length; index++) {
   1.338 +    p = utf8_write(p, base[index]);
   1.339 +  }
   1.340 +  *p = '\0';
   1.341 +  assert(p == &result[utf8_len], "length prediction must be correct");
   1.342 +  return (char*) result;
   1.343 +}
   1.344 +
   1.345 +char* UNICODE::as_utf8(jchar* base, int length, char* buf, int buflen) {
   1.346 +  u_char* p = (u_char*)buf;
   1.347 +  u_char* end = (u_char*)buf + buflen;
   1.348 +  for (int index = 0; index < length; index++) {
   1.349 +    jchar c = base[index];
   1.350 +    if (p + utf8_size(c) >= end) break;      // string is truncated
   1.351 +    p = utf8_write(p, base[index]);
   1.352 +  }
   1.353 +  *p = '\0';
   1.354 +  return buf;
   1.355 +}
   1.356 +
   1.357 +void UNICODE::convert_to_utf8(const jchar* base, int length, char* utf8_buffer) {
   1.358 +  for(int index = 0; index < length; index++) {
   1.359 +    utf8_buffer = (char*)utf8_write((u_char*)utf8_buffer, base[index]);
   1.360 +  }
   1.361 +  *utf8_buffer = '\0';
   1.362 +}
   1.363 +
   1.364 +// returns the quoted ascii length of a unicode string
   1.365 +int UNICODE::quoted_ascii_length(jchar* base, int length) {
   1.366 +  int result = 0;
   1.367 +  for (int i = 0; i < length; i++) {
   1.368 +    jchar c = base[i];
   1.369 +    if (c >= 32 && c < 127) {
   1.370 +      result++;
   1.371 +    } else {
   1.372 +      result += 6;
   1.373 +    }
   1.374 +  }
   1.375 +  return result;
   1.376 +}
   1.377 +
   1.378 +// converts a utf8 string to quoted ascii
   1.379 +void UNICODE::as_quoted_ascii(const jchar* base, int length, char* buf, int buflen) {
   1.380 +  char* p = buf;
   1.381 +  char* end = buf + buflen;
   1.382 +  for (int index = 0; index < length; index++) {
   1.383 +    jchar c = base[index];
   1.384 +    if (c >= 32 && c < 127) {
   1.385 +      if (p + 1 >= end) break;      // string is truncated
   1.386 +      *p++ = (char)c;
   1.387 +    } else {
   1.388 +      if (p + 6 >= end) break;      // string is truncated
   1.389 +      sprintf(p, "\\u%04x", c);
   1.390 +      p += 6;
   1.391 +    }
   1.392 +  }
   1.393 +  *p = '\0';
   1.394 +}

mercurial