1 /*
2  * Copyright (c) 1997, 2019, 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 #ifndef SHARE_UTILITIES_UTF8_HPP
26 #define SHARE_UTILITIES_UTF8_HPP
27 
28 #include "memory/allocation.hpp"
29 
30 // Low-level interface for UTF8 strings
31 
32 class UTF8 : AllStatic {
33  public:
34   // returns the unicode length of a 0-terminated utf8 string
unicode_length(const char * utf8_str)35   static int unicode_length(const char* utf8_str) {
36     bool is_latin1, has_multibyte;
37     return unicode_length(utf8_str, is_latin1, has_multibyte);
38   }
39   static int unicode_length(const char* utf8_str, bool& is_latin1, bool& has_multibyte);
40 
41   // returns the unicode length of a non-0-terminated utf8 string
unicode_length(const char * utf8_str,int len)42   static int unicode_length(const char* utf8_str, int len) {
43     bool is_latin1, has_multibyte;
44     return unicode_length(utf8_str, len, is_latin1, has_multibyte);
45   }
46   static int unicode_length(const char* utf8_str, int len, bool& is_latin1, bool& has_multibyte);
47 
48   // converts a utf8 string to a unicode string
49   template<typename T> static void convert_to_unicode(const char* utf8_str, T* unicode_str, int unicode_length);
50 
51   // returns the quoted ascii length of a utf8 string
52   static int quoted_ascii_length(const char* utf8_str, int utf8_length);
53 
54   // converts a utf8 string to quoted ascii
55   static void as_quoted_ascii(const char* utf8_str, int utf8_length, char* buf, int buflen);
56 
57 #ifndef PRODUCT
58   // converts a quoted ascii string to utf8 string.  returns the original
59   // string unchanged if nothing needs to be done.
60   static const char* from_quoted_ascii(const char* quoted_ascii_string);
61 #endif
62 
63   // decodes the current utf8 character, stores the result in value,
64   // and returns the end of the current utf8 chararacter.
65   template<typename T> static char* next(const char* str, T* value);
66 
67   // decodes the current utf8 character, gets the supplementary character instead of
68   // the surrogate pair when seeing a supplementary character in string,
69   // stores the result in value, and returns the end of the current utf8 chararacter.
70   static char* next_character(const char* str, jint* value);
71 
72   // Utility methods
73 
74   // Returns NULL if 'c' it not found. This only works as long
75   // as 'c' is an ASCII character
strrchr(const jbyte * base,int length,jbyte c)76   static const jbyte* strrchr(const jbyte* base, int length, jbyte c) {
77     assert(length >= 0, "sanity check");
78     assert(c >= 0, "does not work for non-ASCII characters");
79     // Skip backwards in string until 'c' is found or end is reached
80     while(--length >= 0 && base[length] != c);
81     return (length < 0) ? NULL : &base[length];
82   }
83   static bool   equal(const jbyte* base1, int length1, const jbyte* base2,int length2);
84   static bool   is_supplementary_character(const unsigned char* str);
85   static jint   get_supplementary_character(const unsigned char* str);
86 
87   static bool   is_legal_utf8(const unsigned char* buffer, int length,
88                               bool version_leq_47);
89 };
90 
91 
92 // Low-level interface for UNICODE strings
93 
94 // A unicode string represents a string in the UTF-16 format in which supplementary
95 // characters are represented by surrogate pairs. Index values refer to char code
96 // units, so a supplementary character uses two positions in a unicode string.
97 
98 class UNICODE : AllStatic {
99  public:
100   // checks if the given unicode character can be encoded as latin1
101   static bool is_latin1(jchar c);
102 
103   // checks if the given string can be encoded as latin1
104   static bool is_latin1(const jchar* base, int length);
105 
106   // returns the utf8 size of a unicode character
107   static int utf8_size(jchar c);
108   static int utf8_size(jbyte c);
109 
110   // returns the utf8 length of a unicode string
111   template<typename T> static int utf8_length(const T* base, int length);
112 
113   // converts a unicode string to utf8 string
114   static void convert_to_utf8(const jchar* base, int length, char* utf8_buffer);
115 
116   // converts a unicode string to a utf8 string; result is allocated
117   // in resource area unless a buffer is provided. The unicode 'length'
118   // parameter is set to the length of the result utf8 string.
119   template<typename T> static char* as_utf8(const T* base, int& length);
120   static char* as_utf8(const jchar* base, int length, char* buf, int buflen);
121   static char* as_utf8(const jbyte* base, int length, char* buf, int buflen);
122 
123   // returns the quoted ascii length of a unicode string
124   template<typename T> static int quoted_ascii_length(const T* base, int length);
125 
126   // converts a unicode string to quoted ascii
127   template<typename T> static void as_quoted_ascii(const T* base, int length, char* buf, int buflen);
128 };
129 
130 #endif // SHARE_UTILITIES_UTF8_HPP
131