/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Determine length of this Standard UTF-8 in Modified UTF-8.
* Validation is done of the basic UTF encoding rules, returns
* length (no change) when errors are detected in the UTF encoding.
*
* Note: Accepts Modified UTF-8 also, no verification on the
* correctness of Standard UTF-8 is done. e,g, 0xC080 input is ok.
*/
int
int new_length;
int i;
new_length = 0;
for ( i = 0 ; i < length ; i++ ) {
unsigned byte;
new_length++;
if ( byte == 0 ) {
new_length++; /* We gain one byte in length on NULL bytes */
}
/* Check encoding of following bytes */
break; /* Error condition */
}
i++; /* Skip next byte */
new_length += 2;
/* Check encoding of following bytes */
break; /* Error condition */
}
i += 2; /* Skip next two bytes */
new_length += 3;
/* Check encoding of following bytes */
break; /* Error condition */
}
i += 3; /* Skip next 3 bytes */
} else {
break; /* Error condition */
}
}
if ( i != length ) {
/* Error in finding new length, return old length so no conversion */
/* FIXUP: ERROR_MESSAGE? */
return length;
}
return new_length;
}
/*
* Convert Standard UTF-8 to Modified UTF-8.
* Assumes the UTF-8 encoding was validated by modifiedLength() above.
*
* Note: Accepts Modified UTF-8 also, no verification on the
* correctness of Standard UTF-8 is done. e,g, 0xC080 input is ok.
*/
void
{
int i;
int j;
j = 0;
for ( i = 0 ; i < length ; i++ ) {
unsigned byte1;
/* NULL bytes and bytes starting with 11110xxx are special */
if ( byte1 == 0 ) {
/* Bits out: 11000000 10000000 */
new_string[j++] = (char)0xC0;
new_string[j++] = (char)0x80;
} else {
/* Single byte */
new_string[j++] = byte1;
}
new_string[j++] = byte1;
new_string[j++] = string[++i];
new_string[j++] = byte1;
new_string[j++] = string[++i];
new_string[j++] = string[++i];
/* Beginning of 4byte encoding, turn into 2 3byte encodings */
/* Bits in: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
/* Reconstruct full 21bit value */
/* Bits out: 11101101 1010xxxx 10xxxxxx */
new_string[j++] = (char)0xED;
/* Bits out: 11101101 1011xxxx 10xxxxxx */
new_string[j++] = (char)0xED;
new_string[j++] = byte4;
}
}
new_string[j] = 0;
}