< prev index next >

src/share/vm/utilities/utf8.cpp

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "utilities/utf8.hpp"
  27 
  28 // Assume the utf8 string is in legal form and has been
  29 // checked in the class file parser/format checker.
  30 char* UTF8::next(const char* str, jchar* value) {
  31   unsigned const char *ptr = (const unsigned char *)str;
  32   unsigned char ch, ch2, ch3;
  33   int length = -1;              /* bad length */
  34   jchar result;
  35   switch ((ch = ptr[0]) >> 4) {
  36     default:
  37     result = ch;
  38     length = 1;
  39     break;
  40 
  41   case 0x8: case 0x9: case 0xA: case 0xB: case 0xF:
  42     /* Shouldn't happen. */
  43     break;
  44 
  45   case 0xC: case 0xD:
  46     /* 110xxxxx  10xxxxxx */
  47     if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
  48       unsigned char high_five = ch & 0x1F;
  49       unsigned char low_six = ch2 & 0x3F;
  50       result = (high_five << 6) + low_six;
  51       length = 2;
  52       break;
  53     }
  54     break;
  55 
  56   case 0xE:
  57     /* 1110xxxx 10xxxxxx 10xxxxxx */
  58     if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
  59       if (((ch3 = ptr[2]) & 0xC0) == 0x80) {
  60         unsigned char high_four = ch & 0x0f;
  61         unsigned char mid_six = ch2 & 0x3f;
  62         unsigned char low_six = ch3 & 0x3f;
  63         result = (((high_four << 6) + mid_six) << 6) + low_six;
  64         length = 3;
  65       }
  66     }
  67     break;
  68   } /* end of switch */
  69 
  70   if (length <= 0) {
  71     *value = ptr[0];    /* default bad result; */
  72     return (char*)(ptr + 1); // make progress somehow
  73   }
  74 
  75   *value = result;
  76 
  77   // The assert is correct but the .class file is wrong
  78   // assert(UNICODE::utf8_size(result) == length, "checking reverse computation");
  79   return (char *)(ptr + length);
  80 }
  81 
  82 char* UTF8::next_character(const char* str, jint* value) {
  83   unsigned const char *ptr = (const unsigned char *)str;
  84   /* See if it's legal supplementary character:
  85      11101101 1010xxxx 10xxxxxx 11101101 1011xxxx 10xxxxxx */
  86   if (is_supplementary_character(ptr)) {
  87     *value = get_supplementary_character(ptr);
  88     return (char *)(ptr + 6);
  89   }
  90   jchar result;
  91   char* next_ch = next(str, &result);
  92   *value = result;
  93   return next_ch;
  94 }
  95 
  96 // Count bytes of the form 10xxxxxx and deduct this count
  97 // from the total byte count.  The utf8 string must be in
  98 // legal form which has been verified in the format checker.
  99 int UTF8::unicode_length(const char* str, int len) {
 100   int num_chars = len;



 101   for (int i = 0; i < len; i++) {
 102     if ((str[i] & 0xC0) == 0x80) {






 103       --num_chars;
 104     }

 105   }
 106   return num_chars;
 107 }
 108 
 109 // Count bytes of the utf8 string except those in form
 110 // 10xxxxxx which only appear in multibyte characters.
 111 // The utf8 string must be in legal form and has been
 112 // verified in the format checker.
 113 int UTF8::unicode_length(const char* str) {
 114   int num_chars = 0;



 115   for (const char* p = str; *p; p++) {
 116     if (((*p) & 0xC0) != 0x80) {







 117       num_chars++;
 118     }

 119   }
 120   return num_chars;
 121 }
 122 
 123 // Writes a jchar a utf8 and returns the end
 124 static u_char* utf8_write(u_char* base, jchar ch) {
 125   if ((ch != 0) && (ch <=0x7f)) {
 126     base[0] = (u_char) ch;
 127     return base + 1;
 128   }
 129 
 130   if (ch <= 0x7FF) {
 131     /* 11 bits or less. */
 132     unsigned char high_five = ch >> 6;
 133     unsigned char low_six = ch & 0x3F;
 134     base[0] = high_five | 0xC0; /* 110xxxxx */
 135     base[1] = low_six | 0x80;   /* 10xxxxxx */
 136     return base + 2;
 137   }
 138   /* possibly full 16 bits. */
 139   char high_four = ch >> 12;
 140   char mid_six = (ch >> 6) & 0x3F;
 141   char low_six = ch & 0x3f;
 142   base[0] = high_four | 0xE0; /* 1110xxxx */
 143   base[1] = mid_six | 0x80;   /* 10xxxxxx */
 144   base[2] = low_six | 0x80;   /* 10xxxxxx */
 145   return base + 3;
 146 }
 147 
 148 void UTF8::convert_to_unicode(const char* utf8_str, jchar* unicode_str, int unicode_length) {
 149   unsigned char ch;
 150   const char *ptr = utf8_str;
 151   int index = 0;
 152 
 153   /* ASCII case loop optimization */
 154   for (; index < unicode_length; index++) {
 155     if((ch = ptr[0]) > 0x7F) { break; }
 156     unicode_str[index] = ch;
 157     ptr = (const char *)(ptr + 1);
 158   }
 159 
 160   for (; index < unicode_length; index++) {
 161     ptr = UTF8::next(ptr, &unicode_str[index]);
 162   }
 163 }
 164 






 165 // returns the quoted ascii length of a 0-terminated utf8 string
 166 int UTF8::quoted_ascii_length(const char* utf8_str, int utf8_length) {
 167   const char *ptr = utf8_str;
 168   const char* end = ptr + utf8_length;
 169   int result = 0;
 170   while (ptr < end) {
 171     jchar c;
 172     ptr = UTF8::next(ptr, &c);
 173     if (c >= 32 && c < 127) {
 174       result++;
 175     } else {
 176       result += 6;
 177     }
 178   }
 179   return result;
 180 }
 181 
 182 // converts a utf8 string to quoted ascii
 183 void UTF8::as_quoted_ascii(const char* utf8_str, int utf8_length, char* buf, int buflen) {
 184   const char *ptr = utf8_str;


 289 
 290 bool UTF8::equal(const jbyte* base1, int length1, const jbyte* base2, int length2) {
 291   // Length must be the same
 292   if (length1 != length2) return false;
 293   for (int i = 0; i < length1; i++) {
 294     if (base1[i] != base2[i]) return false;
 295   }
 296   return true;
 297 }
 298 
 299 bool UTF8::is_supplementary_character(const unsigned char* str) {
 300   return ((str[0] & 0xFF) == 0xED) && ((str[1] & 0xF0) == 0xA0) && ((str[2] & 0xC0) == 0x80)
 301       && ((str[3] & 0xFF) == 0xED) && ((str[4] & 0xF0) == 0xB0) && ((str[5] & 0xC0) == 0x80);
 302 }
 303 
 304 jint UTF8::get_supplementary_character(const unsigned char* str) {
 305   return 0x10000 + ((str[1] & 0x0f) << 16) + ((str[2] & 0x3f) << 10)
 306                  + ((str[4] & 0x0f) << 6)  + (str[5] & 0x3f);
 307 }
 308 
 309 
 310 //-------------------------------------------------------------------------------------
 311 












 312 
 313 int UNICODE::utf8_size(jchar c) {
 314   if ((0x0001 <= c) && (c <= 0x007F)) return 1;
 315   if (c <= 0x07FF) return 2;
 316   return 3;
 317 }
 318 





 319 int UNICODE::utf8_length(jchar* base, int length) {
 320   int result = 0;
 321   for (int index = 0; index < length; index++) {
 322     jchar c = base[index];
 323     if ((0x0001 <= c) && (c <= 0x007F)) result += 1;
 324     else if (c <= 0x07FF) result += 2;
 325     else result += 3;
 326   }
 327   return result;
 328 }
 329 









 330 char* UNICODE::as_utf8(jchar* base, int length) {
 331   int utf8_len = utf8_length(base, length);
 332   u_char* buf = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1);
 333   char* result = as_utf8(base, length, (char*) buf, utf8_len + 1);
 334   assert((int) strlen(result) == utf8_len, "length prediction must be correct");
 335   return result;
 336 }
 337 




















 338 char* UNICODE::as_utf8(jchar* base, int length, char* buf, int buflen) {
 339   u_char* p = (u_char*)buf;
 340   for (int index = 0; index < length; index++) {
 341     jchar c = base[index];
 342     buflen -= utf8_size(c);
 343     if (buflen <= 0) break; // string is truncated
 344     p = utf8_write(p, c);
 345   }
 346   *p = '\0';
 347   return buf;
 348 }
 349 




















 350 void UNICODE::convert_to_utf8(const jchar* base, int length, char* utf8_buffer) {
 351   for(int index = 0; index < length; index++) {
 352     utf8_buffer = (char*)utf8_write((u_char*)utf8_buffer, base[index]);
 353   }
 354   *utf8_buffer = '\0';
 355 }
 356 
 357 // returns the quoted ascii length of a unicode string
 358 int UNICODE::quoted_ascii_length(jchar* base, int length) {

 359   int result = 0;
 360   for (int i = 0; i < length; i++) {
 361     jchar c = base[i];
 362     if (c >= 32 && c < 127) {
 363       result++;
 364     } else {
 365       result += 6;
 366     }
 367   }
 368   return result;
 369 }
 370 
 371 // converts a utf8 string to quoted ascii
 372 void UNICODE::as_quoted_ascii(const jchar* base, int length, char* buf, int buflen) {

 373   char* p = buf;
 374   char* end = buf + buflen;
 375   for (int index = 0; index < length; index++) {
 376     jchar c = base[index];
 377     if (c >= 32 && c < 127) {
 378       if (p + 1 >= end) break;      // string is truncated
 379       *p++ = (char)c;
 380     } else {
 381       if (p + 6 >= end) break;      // string is truncated
 382       sprintf(p, "\\u%04x", c);
 383       p += 6;
 384     }
 385   }
 386   *p = '\0';
 387 }







 388 
 389 #ifndef PRODUCT
 390 void TestAsUtf8() {
 391   char res[60];
 392   jchar str[20];
 393 
 394   for (int i = 0; i < 20; i++) {
 395     str[i] = 0x0800; // char that is 2B in UTF-16 but 3B in UTF-8
 396   }
 397   str[19] = (jchar)'\0';
 398 
 399   // The resulting string in UTF-8 is 3*19 bytes long, but should be truncated
 400   UNICODE::as_utf8(str, 19, res, 10);
 401   assert(strlen(res) == 9, "string should be truncated here");
 402 
 403   UNICODE::as_utf8(str, 19, res, 18);
 404   assert(strlen(res) == 15, "string should be truncated here");
 405 
 406   UNICODE::as_utf8(str, 19, res, 20);
 407   assert(strlen(res) == 18, "string should be truncated here");
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "utilities/utf8.hpp"
  27 
  28 // Assume the utf8 string is in legal form and has been
  29 // checked in the class file parser/format checker.
  30 template<typename T> char* UTF8::next(const char* str, T* value) {
  31   unsigned const char *ptr = (const unsigned char *)str;
  32   unsigned char ch, ch2, ch3;
  33   int length = -1;              /* bad length */
  34   jchar result;
  35   switch ((ch = ptr[0]) >> 4) {
  36     default:
  37     result = ch;
  38     length = 1;
  39     break;
  40 
  41   case 0x8: case 0x9: case 0xA: case 0xB: case 0xF:
  42     /* Shouldn't happen. */
  43     break;
  44 
  45   case 0xC: case 0xD:
  46     /* 110xxxxx  10xxxxxx */
  47     if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
  48       unsigned char high_five = ch & 0x1F;
  49       unsigned char low_six = ch2 & 0x3F;
  50       result = (high_five << 6) + low_six;
  51       length = 2;
  52       break;
  53     }
  54     break;
  55 
  56   case 0xE:
  57     /* 1110xxxx 10xxxxxx 10xxxxxx */
  58     if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
  59       if (((ch3 = ptr[2]) & 0xC0) == 0x80) {
  60         unsigned char high_four = ch & 0x0f;
  61         unsigned char mid_six = ch2 & 0x3f;
  62         unsigned char low_six = ch3 & 0x3f;
  63         result = (((high_four << 6) + mid_six) << 6) + low_six;
  64         length = 3;
  65       }
  66     }
  67     break;
  68   } /* end of switch */
  69 
  70   if (length <= 0) {
  71     *value = (T)ptr[0];    /* default bad result; */
  72     return (char*)(ptr + 1); // make progress somehow
  73   }
  74 
  75   *value = (T)result;
  76 
  77   // The assert is correct but the .class file is wrong
  78   // assert(UNICODE::utf8_size(result) == length, "checking reverse computation");
  79   return (char *)(ptr + length);
  80 }
  81 
  82 char* UTF8::next_character(const char* str, jint* value) {
  83   unsigned const char *ptr = (const unsigned char *)str;
  84   /* See if it's legal supplementary character:
  85      11101101 1010xxxx 10xxxxxx 11101101 1011xxxx 10xxxxxx */
  86   if (is_supplementary_character(ptr)) {
  87     *value = get_supplementary_character(ptr);
  88     return (char *)(ptr + 6);
  89   }
  90   jchar result;
  91   char* next_ch = next(str, &result);
  92   *value = result;
  93   return next_ch;
  94 }
  95 
  96 // Count bytes of the form 10xxxxxx and deduct this count
  97 // from the total byte count.  The utf8 string must be in
  98 // legal form which has been verified in the format checker.
  99 int UTF8::unicode_length(const char* str, int len, bool& is_latin1, bool& has_multibyte) {
 100   int num_chars = len;
 101   has_multibyte = false;
 102   is_latin1 = true;
 103   unsigned char prev = 0;
 104   for (int i = 0; i < len; i++) {
 105     unsigned char c = str[i];
 106     if ((c & 0xC0) == 0x80) {
 107       // Multibyte, check if valid latin1 character.
 108       has_multibyte = true;
 109       if (prev > 0xC3) {
 110         is_latin1 = false;
 111       }
 112       --num_chars;
 113     }
 114     prev = c;
 115   }
 116   return num_chars;
 117 }
 118 
 119 // Count bytes of the utf8 string except those in form
 120 // 10xxxxxx which only appear in multibyte characters.
 121 // The utf8 string must be in legal form and has been
 122 // verified in the format checker.
 123 int UTF8::unicode_length(const char* str, bool& is_latin1, bool& has_multibyte) {
 124   int num_chars = 0;
 125   has_multibyte = false;
 126   is_latin1 = true;
 127   unsigned char prev = 0;
 128   for (const char* p = str; *p; p++) {
 129     unsigned char c = (*p);
 130     if ((c & 0xC0) == 0x80) {
 131       // Multibyte, check if valid latin1 character.
 132       has_multibyte = true;
 133       if (prev > 0xC3) {
 134         is_latin1 = false;
 135       }
 136     } else {
 137       num_chars++;
 138     }
 139     prev = c;
 140   }
 141   return num_chars;
 142 }
 143 
 144 // Writes a jchar as utf8 and returns the end
 145 static u_char* utf8_write(u_char* base, jchar ch) {
 146   if ((ch != 0) && (ch <=0x7f)) {
 147     base[0] = (u_char) ch;
 148     return base + 1;
 149   }
 150 
 151   if (ch <= 0x7FF) {
 152     /* 11 bits or less. */
 153     unsigned char high_five = ch >> 6;
 154     unsigned char low_six = ch & 0x3F;
 155     base[0] = high_five | 0xC0; /* 110xxxxx */
 156     base[1] = low_six | 0x80;   /* 10xxxxxx */
 157     return base + 2;
 158   }
 159   /* possibly full 16 bits. */
 160   char high_four = ch >> 12;
 161   char mid_six = (ch >> 6) & 0x3F;
 162   char low_six = ch & 0x3f;
 163   base[0] = high_four | 0xE0; /* 1110xxxx */
 164   base[1] = mid_six | 0x80;   /* 10xxxxxx */
 165   base[2] = low_six | 0x80;   /* 10xxxxxx */
 166   return base + 3;
 167 }
 168 
 169 template<typename T> void UTF8::convert_to_unicode(const char* utf8_str, T* unicode_str, int unicode_length) {
 170   unsigned char ch;
 171   const char *ptr = utf8_str;
 172   int index = 0;
 173 
 174   /* ASCII case loop optimization */
 175   for (; index < unicode_length; index++) {
 176     if((ch = ptr[0]) > 0x7F) { break; }
 177     unicode_str[index] = (T)ch;
 178     ptr = (const char *)(ptr + 1);
 179   }
 180 
 181   for (; index < unicode_length; index++) {
 182     ptr = UTF8::next(ptr, &unicode_str[index]);
 183   }
 184 }
 185 
 186 // Explicit instantiation for all supported string types.
 187 template char* UTF8::next<jchar>(const char* str, jchar* value);
 188 template char* UTF8::next<jbyte>(const char* str, jbyte* value);
 189 template void UTF8::convert_to_unicode<jchar>(const char* utf8_str, jchar* unicode_str, int unicode_length);
 190 template void UTF8::convert_to_unicode<jbyte>(const char* utf8_str, jbyte* unicode_str, int unicode_length);
 191 
 192 // returns the quoted ascii length of a 0-terminated utf8 string
 193 int UTF8::quoted_ascii_length(const char* utf8_str, int utf8_length) {
 194   const char *ptr = utf8_str;
 195   const char* end = ptr + utf8_length;
 196   int result = 0;
 197   while (ptr < end) {
 198     jchar c;
 199     ptr = UTF8::next(ptr, &c);
 200     if (c >= 32 && c < 127) {
 201       result++;
 202     } else {
 203       result += 6;
 204     }
 205   }
 206   return result;
 207 }
 208 
 209 // converts a utf8 string to quoted ascii
 210 void UTF8::as_quoted_ascii(const char* utf8_str, int utf8_length, char* buf, int buflen) {
 211   const char *ptr = utf8_str;


 316 
 317 bool UTF8::equal(const jbyte* base1, int length1, const jbyte* base2, int length2) {
 318   // Length must be the same
 319   if (length1 != length2) return false;
 320   for (int i = 0; i < length1; i++) {
 321     if (base1[i] != base2[i]) return false;
 322   }
 323   return true;
 324 }
 325 
 326 bool UTF8::is_supplementary_character(const unsigned char* str) {
 327   return ((str[0] & 0xFF) == 0xED) && ((str[1] & 0xF0) == 0xA0) && ((str[2] & 0xC0) == 0x80)
 328       && ((str[3] & 0xFF) == 0xED) && ((str[4] & 0xF0) == 0xB0) && ((str[5] & 0xC0) == 0x80);
 329 }
 330 
 331 jint UTF8::get_supplementary_character(const unsigned char* str) {
 332   return 0x10000 + ((str[1] & 0x0f) << 16) + ((str[2] & 0x3f) << 10)
 333                  + ((str[4] & 0x0f) << 6)  + (str[5] & 0x3f);
 334 }
 335 

 336 //-------------------------------------------------------------------------------------
 337 
 338 bool UNICODE::is_latin1(jchar c) {
 339   return (c <= 0x00FF);
 340 }
 341 
 342 bool UNICODE::is_latin1(jchar* base, int length) {
 343   for (int index = 0; index < length; index++) {
 344     if (base[index] > 0x00FF) {
 345       return false;
 346     }
 347   }
 348   return true;
 349 }
 350 
 351 int UNICODE::utf8_size(jchar c) {
 352   if ((0x0001 <= c) && (c <= 0x007F)) return 1;
 353   if (c <= 0x07FF) return 2;
 354   return 3;
 355 }
 356 
 357 int UNICODE::utf8_size(jbyte c) {
 358   if (c >= 0x0001) return 1;
 359   return 2;
 360 }
 361 
 362 int UNICODE::utf8_length(jchar* base, int length) {
 363   int result = 0;
 364   for (int index = 0; index < length; index++) {
 365     jchar c = base[index];
 366     if ((0x0001 <= c) && (c <= 0x007F)) result += 1;
 367     else if (c <= 0x07FF) result += 2;
 368     else result += 3;
 369   }
 370   return result;
 371 }
 372 
 373 int UNICODE::utf8_length(jbyte* base, int length) {
 374   int result = 0;
 375   for (int index = 0; index < length; index++) {
 376     jbyte c = base[index];
 377     result += utf8_size(c);
 378   }
 379   return result;
 380 }
 381 
 382 char* UNICODE::as_utf8(jchar* base, int length) {
 383   int utf8_len = utf8_length(base, length);
 384   u_char* buf = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1);
 385   char* result = as_utf8(base, length, (char*) buf, utf8_len + 1);
 386   assert((int) strlen(result) == utf8_len, "length prediction must be correct");
 387   return result;
 388 }
 389 
 390 char* UNICODE::as_utf8(jbyte* base, int length) {
 391   int utf8_len = utf8_length(base, length);
 392   u_char* result = NEW_RESOURCE_ARRAY(u_char, utf8_len + 1);
 393   u_char* p = result;
 394   if (utf8_len == length) {
 395     for (int index = 0; index < length; index++) {
 396       *p++ = base[index];
 397     }
 398   } else {
 399     // Unicode string contains U+0000 which should
 400     // be encoded as 0xC080 in "modified" UTF8.
 401     for (int index = 0; index < length; index++) {
 402       p = utf8_write(p, ((jchar) base[index]) & 0xff);
 403     }
 404   }
 405   *p = '\0';
 406   assert(p == &result[utf8_len], "length prediction must be correct");
 407   return (char*) result;
 408 }
 409 
 410 char* UNICODE::as_utf8(jchar* base, int length, char* buf, int buflen) {
 411   u_char* p = (u_char*)buf;
 412   for (int index = 0; index < length; index++) {
 413     jchar c = base[index];
 414     buflen -= utf8_size(c);
 415     if (buflen <= 0) break; // string is truncated
 416     p = utf8_write(p, c);
 417   }
 418   *p = '\0';
 419   return buf;
 420 }
 421 
 422 char* UNICODE::as_utf8(jbyte* base, int length, char* buf, int buflen) {
 423   u_char* p = (u_char*)buf;
 424   u_char* end = (u_char*)buf + buflen;
 425   for (int index = 0; index < length; index++) {
 426     jbyte c = base[index];
 427     int sz = utf8_size(c);
 428     buflen -= sz;
 429     if (buflen <= 0) break; // string is truncated
 430     if (sz == 1) {
 431       *p++ = c;
 432     } else {
 433       // Unicode string contains U+0000 which should
 434       // be encoded as 0xC080 in "modified" UTF8.
 435       p = utf8_write(p, ((jchar) c) & 0xff);
 436     }
 437   }
 438   *p = '\0';
 439   return buf;
 440 }
 441 
 442 void UNICODE::convert_to_utf8(const jchar* base, int length, char* utf8_buffer) {
 443   for(int index = 0; index < length; index++) {
 444     utf8_buffer = (char*)utf8_write((u_char*)utf8_buffer, base[index]);
 445   }
 446   *utf8_buffer = '\0';
 447 }
 448 
 449 // returns the quoted ascii length of a unicode string
 450 template<typename T>
 451 int UNICODE::quoted_ascii_length(T* base, int length) {
 452   int result = 0;
 453   for (int i = 0; i < length; i++) {
 454     T c = base[i];
 455     if (c >= 32 && c < 127) {
 456       result++;
 457     } else {
 458       result += 6;
 459     }
 460   }
 461   return result;
 462 }
 463 
 464 // converts a unicode string to quoted ascii
 465 template<typename T>
 466 void UNICODE::as_quoted_ascii(const T* base, int length, char* buf, int buflen) {
 467   char* p = buf;
 468   char* end = buf + buflen;
 469   for (int index = 0; index < length; index++) {
 470     T c = base[index];
 471     if (c >= 32 && c < 127) {
 472       if (p + 1 >= end) break;      // string is truncated
 473       *p++ = (char)c;
 474     } else {
 475       if (p + 6 >= end) break;      // string is truncated
 476       sprintf(p, "\\u%04x", c);
 477       p += 6;
 478     }
 479   }
 480   *p = '\0';
 481 }
 482 
 483 // Explicit instantiation for all supported types.
 484 template int UNICODE::quoted_ascii_length<jbyte>(jbyte* base, int length);
 485 template int UNICODE::quoted_ascii_length<jchar>(jchar* base, int length);
 486 template void UNICODE::as_quoted_ascii<jbyte>(const jbyte* base, int length, char* buf, int buflen);
 487 template void UNICODE::as_quoted_ascii<jchar>(const jchar* base, int length, char* buf, int buflen);
 488 
 489 
 490 #ifndef PRODUCT
 491 void TestAsUtf8() {
 492   char res[60];
 493   jchar str[20];
 494 
 495   for (int i = 0; i < 20; i++) {
 496     str[i] = 0x0800; // char that is 2B in UTF-16 but 3B in UTF-8
 497   }
 498   str[19] = (jchar)'\0';
 499 
 500   // The resulting string in UTF-8 is 3*19 bytes long, but should be truncated
 501   UNICODE::as_utf8(str, 19, res, 10);
 502   assert(strlen(res) == 9, "string should be truncated here");
 503 
 504   UNICODE::as_utf8(str, 19, res, 18);
 505   assert(strlen(res) == 15, "string should be truncated here");
 506 
 507   UNICODE::as_utf8(str, 19, res, 20);
 508   assert(strlen(res) == 18, "string should be truncated here");
< prev index next >