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 * 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 * 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 * 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. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 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 * 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 if ( (
byte &
0x80) == 0 ) {
/* 1byte encoding */ 0N/A }
else if ( (
byte &
0xE0) ==
0xC0 ) {
/* 2byte encoding */ 0N/A /* Check encoding of following bytes */ 0N/A break;
/* Error condition */ 0N/A i++;
/* Skip next byte */ 0N/A }
else if ( (
byte &
0xF0) ==
0xE0 ) {
/* 3byte encoding */ 0N/A /* Check encoding of following bytes */ 0N/A break;
/* Error condition */ 0N/A i +=
2;
/* Skip next two bytes */ 0N/A }
else if ( (
byte &
0xF8) ==
0xF0 ) {
/* 4byte encoding */ 0N/A /* Check encoding of following bytes */ 0N/A break;
/* Error condition */ 0N/A i +=
3;
/* Skip next 3 bytes */ 0N/A break;
/* Error condition */ 0N/A /* Error in finding new length, return old length so no conversion */ 0N/A /* FIXUP: ERROR_MESSAGE? */ 0N/A * Convert Standard UTF-8 to Modified UTF-8. 0N/A * Assumes the UTF-8 encoding was validated by modifiedLength() above. 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 /* NULL bytes and bytes starting with 11110xxx are special */ 0N/A if ( (
byte1 &
0x80) == 0 ) {
/* 1byte encoding */ 0N/A /* Bits out: 11000000 10000000 */ 0N/A }
else if ( (
byte1 &
0xE0) ==
0xC0 ) {
/* 2byte encoding */ 0N/A }
else if ( (
byte1 &
0xF0) ==
0xE0 ) {
/* 3byte encoding */ 0N/A }
else if ( (
byte1 &
0xF8) ==
0xF0 ) {
/* 4byte encoding */ 0N/A /* Beginning of 4byte encoding, turn into 2 3byte encodings */ 0N/A /* Bits in: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ 0N/A /* Reconstruct full 21bit value */ 0N/A /* Bits out: 11101101 1010xxxx 10xxxxxx */ 0N/A /* Bits out: 11101101 1011xxxx 10xxxxxx */