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