1 /*
2  * Copyright (c) 2004, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 
27 /**
28  * Determine length of this Standard UTF-8 in Modified UTF-8.
29  *    Validation is done of the basic UTF encoding rules, returns
30  *    length (no change) when errors are detected in the UTF encoding.
31  *
32  *    Note: Accepts Modified UTF-8 also, no verification on the
33  *          correctness of Standard UTF-8 is done. e,g, 0xC080 input is ok.
34  */
35 int
modifiedUtf8LengthOfUtf8(char * string,int length)36 modifiedUtf8LengthOfUtf8(char* string, int length) {
37     int new_length;
38     int i;
39 
40     new_length = 0;
41     /*
42      * if length < 0 or new_length becomes < 0 => string is too big
43      * (handled as error after the cycle).
44      */
45     for ( i = 0 ; i < length && new_length >= 0 ; i++ ) {
46         unsigned byte;
47 
48         byte = (unsigned char)string[i];
49         if ( (byte & 0x80) == 0 ) { /* 1byte encoding */
50             new_length++;
51             if ( byte == 0 ) {
52                 new_length++; /* We gain one byte in length on NULL bytes */
53             }
54         } else if ( (byte & 0xE0) == 0xC0 ) { /* 2byte encoding */
55             /* Check encoding of following bytes */
56             if ( (i+1) >= length || (string[i+1] & 0xC0) != 0x80 ) {
57                 break; /* Error condition */
58             }
59             i++; /* Skip next byte */
60             new_length += 2;
61         } else if ( (byte & 0xF0) == 0xE0 ) { /* 3byte encoding */
62             /* Check encoding of following bytes */
63             if ( (i+2) >= length || (string[i+1] & 0xC0) != 0x80
64                                  || (string[i+2] & 0xC0) != 0x80 ) {
65                 break; /* Error condition */
66             }
67             i += 2; /* Skip next two bytes */
68             new_length += 3;
69         } else if ( (byte & 0xF8) == 0xF0 ) { /* 4byte encoding */
70             /* Check encoding of following bytes */
71             if ( (i+3) >= length || (string[i+1] & 0xC0) != 0x80
72                                  || (string[i+2] & 0xC0) != 0x80
73                                  || (string[i+3] & 0xC0) != 0x80 ) {
74                 break; /* Error condition */
75             }
76             i += 3; /* Skip next 3 bytes */
77             new_length += 6; /* 4byte encoding turns into 2 3byte ones */
78         } else {
79             break; /* Error condition */
80         }
81     }
82     if ( i != length ) {
83         /* Error in finding new length, return old length so no conversion */
84         /* FIXUP: ERROR_MESSAGE? */
85         return length;
86     }
87     return new_length;
88 }
89 
90 /*
91  * Convert Standard UTF-8 to Modified UTF-8.
92  *    Assumes the UTF-8 encoding was validated by modifiedLength() above.
93  *
94  *    Note: Accepts Modified UTF-8 also, no verification on the
95  *          correctness of Standard UTF-8 is done. e,g, 0xC080 input is ok.
96  */
97 void
convertUtf8ToModifiedUtf8(char * string,int length,char * new_string,int new_length)98 convertUtf8ToModifiedUtf8(char *string, int length, char *new_string, int new_length)
99 {
100     int i;
101     int j;
102 
103     j = 0;
104     for ( i = 0 ; i < length ; i++ ) {
105         unsigned byte1;
106 
107         byte1 = (unsigned char)string[i];
108 
109         /* NULL bytes and bytes starting with 11110xxx are special */
110         if ( (byte1 & 0x80) == 0 ) { /* 1byte encoding */
111             if ( byte1 == 0 ) {
112                 /* Bits out: 11000000 10000000 */
113                 new_string[j++] = (char)0xC0;
114                 new_string[j++] = (char)0x80;
115             } else {
116                 /* Single byte */
117                 new_string[j++] = byte1;
118             }
119         } else if ( (byte1 & 0xE0) == 0xC0 ) { /* 2byte encoding */
120             new_string[j++] = byte1;
121             new_string[j++] = string[++i];
122         } else if ( (byte1 & 0xF0) == 0xE0 ) { /* 3byte encoding */
123             new_string[j++] = byte1;
124             new_string[j++] = string[++i];
125             new_string[j++] = string[++i];
126         } else if ( (byte1 & 0xF8) == 0xF0 ) { /* 4byte encoding */
127             /* Beginning of 4byte encoding, turn into 2 3byte encodings */
128             unsigned byte2, byte3, byte4, u21;
129 
130             /* Bits in: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
131             byte2 = (unsigned char)string[++i];
132             byte3 = (unsigned char)string[++i];
133             byte4 = (unsigned char)string[++i];
134             /* Reconstruct full 21bit value */
135             u21  = (byte1 & 0x07) << 18;
136             u21 += (byte2 & 0x3F) << 12;
137             u21 += (byte3 & 0x3F) << 6;
138             u21 += (byte4 & 0x3F);
139             /* Bits out: 11101101 1010xxxx 10xxxxxx */
140             new_string[j++] = (char)0xED;
141             new_string[j++] = 0xA0 + (((u21 >> 16) - 1) & 0x0F);
142             new_string[j++] = 0x80 + ((u21 >> 10) & 0x3F);
143             /* Bits out: 11101101 1011xxxx 10xxxxxx */
144             new_string[j++] = (char)0xED;
145             new_string[j++] = 0xB0 + ((u21 >>  6) & 0x0F);
146             new_string[j++] = byte4;
147         }
148     }
149     new_string[j] = 0;
150 }
151