0N/A/*
2362N/A * Copyright (c) 2004, 2005, 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/* Misc functions for conversion of Unicode and UTF-8 and platform encoding */
0N/A
0N/A#include <stdio.h>
0N/A#include <stddef.h>
0N/A#include <stdlib.h>
0N/A#include <stdarg.h>
0N/A#include <string.h>
0N/A#include <ctype.h>
0N/A
0N/A#include "jni.h"
0N/A
0N/A#include "utf.h"
0N/A
0N/A/*
0N/A * Error handler
0N/A */
0N/Avoid
0N/AutfError(char *file, int line, char *message)
0N/A{
0N/A (void)fprintf(stderr, "UTF ERROR [\"%s\":%d]: %s\n", file, line, message);
0N/A abort();
0N/A}
0N/A
0N/A/*
0N/A * Convert UTF-8 to UTF-16
0N/A * Returns length or -1 if output overflows.
0N/A */
0N/Aint JNICALL
0N/Autf8ToUtf16(struct UtfInst *ui, jbyte *utf8, int len, unsigned short *output, int outputMaxLen)
0N/A{
0N/A int outputLen;
0N/A int i;
0N/A
0N/A UTF_ASSERT(utf8);
0N/A UTF_ASSERT(len>=0);
0N/A UTF_ASSERT(output);
0N/A UTF_ASSERT(outputMaxLen>0);
0N/A
0N/A i = 0;
0N/A outputLen = 0;
0N/A while ( i<len ) {
0N/A unsigned code, x, y, z;
0N/A
0N/A if ( outputLen >= outputMaxLen ) {
0N/A return -1;
0N/A }
0N/A x = (unsigned char)utf8[i++];
0N/A code = x;
0N/A if ( (x & 0xE0)==0xE0 ) {
0N/A y = (unsigned char)utf8[i++];
0N/A z = (unsigned char)utf8[i++];
0N/A code = ((x & 0xF)<<12) + ((y & 0x3F)<<6) + (z & 0x3F);
0N/A } else if ( (x & 0xC0)==0xC0 ) {
0N/A y = (unsigned char)utf8[i++];
0N/A code = ((x & 0x1F)<<6) + (y & 0x3F);
0N/A }
0N/A output[outputLen++] = code;
0N/A }
0N/A return outputLen;
0N/A}
0N/A
0N/A/*
0N/A * Convert UTF-16 to UTF-8 Modified
0N/A * Returns length or -1 if output overflows.
0N/A */
0N/Aint JNICALL
0N/Autf16ToUtf8m(struct UtfInst *ui, unsigned short *utf16, int len, jbyte *output, int outputMaxLen)
0N/A{
0N/A int i;
0N/A int outputLen;
0N/A
0N/A UTF_ASSERT(utf16);
0N/A UTF_ASSERT(len>=0);
0N/A UTF_ASSERT(output);
0N/A UTF_ASSERT(outputMaxLen>0);
0N/A
0N/A outputLen = 0;
0N/A for (i = 0; i < len; i++) {
0N/A unsigned code;
0N/A
0N/A code = utf16[i];
0N/A if ( code >= 0x0001 && code <= 0x007F ) {
0N/A output[outputLen++] = code;
0N/A } else if ( code == 0 || ( code >= 0x0080 && code <= 0x07FF ) ) {
0N/A output[outputLen++] = ((code>>6) & 0x1F) | 0xC0;
0N/A output[outputLen++] = (code & 0x3F) | 0x80;
0N/A } else if ( code >= 0x0800 && code <= 0xFFFF ) {
0N/A output[outputLen++] = ((code>>12) & 0x0F) | 0xE0;
0N/A output[outputLen++] = ((code>>6) & 0x3F) | 0x80;
0N/A output[outputLen++] = (code & 0x3F) | 0x80;
0N/A }
0N/A if ( outputLen > outputMaxLen ) {
0N/A return -1;
0N/A }
0N/A }
0N/A output[outputLen] = 0;
0N/A return outputLen;
0N/A}
0N/A
0N/Aint JNICALL
0N/Autf16ToUtf8s(struct UtfInst *ui, unsigned short *utf16, int len, jbyte *output, int outputMaxLen)
0N/A{
0N/A return -1; /* FIXUP */
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 JNICALL
0N/Autf8sToUtf8mLength(struct UtfInst *ui, jbyte *string, int length)
0N/A{
0N/A int newLength;
0N/A int i;
0N/A
0N/A newLength = 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 newLength++;
0N/A if ( byte == 0 ) {
0N/A newLength++; /* 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 newLength += 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 newLength += 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 newLength += 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 newLength;
0N/A}
0N/A
0N/A/* Convert Standard UTF-8 to Modified UTF-8.
0N/A * Assumes the UTF-8 encoding was validated by utf8mLength() 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 JNICALL
0N/Autf8sToUtf8m(struct UtfInst *ui, jbyte *string, int length, jbyte *newString, int newLength)
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 newString[j++] = (jbyte)0xC0;
0N/A newString[j++] = (jbyte)0x80;
0N/A } else {
0N/A /* Single byte */
0N/A newString[j++] = byte1;
0N/A }
0N/A } else if ( (byte1 & 0xE0) == 0xC0 ) { /* 2byte encoding */
0N/A newString[j++] = byte1;
0N/A newString[j++] = string[++i];
0N/A } else if ( (byte1 & 0xF0) == 0xE0 ) { /* 3byte encoding */
0N/A newString[j++] = byte1;
0N/A newString[j++] = string[++i];
0N/A newString[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 newString[j++] = (jbyte)0xED;
0N/A newString[j++] = (jbyte)(0xA0 + (((u21 >> 16) - 1) & 0x0F));
0N/A newString[j++] = (jbyte)(0x80 + ((u21 >> 10) & 0x3F));
0N/A /* Bits out: 11101101 1011xxxx 10xxxxxx */
0N/A newString[j++] = (jbyte)0xED;
0N/A newString[j++] = (jbyte)(0xB0 + ((u21 >> 6) & 0x0F));
0N/A newString[j++] = byte4;
0N/A }
0N/A }
0N/A UTF_ASSERT(i==length);
0N/A UTF_ASSERT(j==newLength);
0N/A newString[j] = (jbyte)0;
0N/A}
0N/A
0N/A/* Given a Modified UTF-8 string, calculate the Standard UTF-8 length.
0N/A * Basic validation of the UTF encoding rules is done, and length is
0N/A * returned (no change) when errors are detected.
0N/A *
0N/A * Note: No validation is made that this is indeed Modified UTF-8 coming in.
0N/A *
0N/A */
0N/Aint JNICALL
0N/Autf8mToUtf8sLength(struct UtfInst *ui, jbyte *string, int length)
0N/A{
0N/A int newLength;
0N/A int i;
0N/A
0N/A newLength = 0;
0N/A for ( i = 0 ; i < length ; i++ ) {
0N/A unsigned byte1, byte2, byte3, byte4, byte5, byte6;
0N/A
0N/A byte1 = (unsigned char)string[i];
0N/A if ( (byte1 & 0x80) == 0 ) { /* 1byte encoding */
0N/A newLength++;
0N/A } else if ( (byte1 & 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 byte2 = (unsigned char)string[++i];
0N/A if ( byte1 != 0xC0 || byte2 != 0x80 ) {
0N/A newLength += 2; /* Normal 2byte encoding, not 0xC080 */
0N/A } else {
0N/A newLength++; /* We will turn 0xC080 into 0 */
0N/A }
0N/A } else if ( (byte1 & 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 byte2 = (unsigned char)string[++i];
0N/A byte3 = (unsigned char)string[++i];
0N/A newLength += 3;
0N/A /* Possible process a second 3byte encoding */
0N/A if ( (i+3) < length && byte1 == 0xED && (byte2 & 0xF0) == 0xA0 ) {
0N/A /* See if this is a pair of 3byte encodings */
0N/A byte4 = (unsigned char)string[i+1];
0N/A byte5 = (unsigned char)string[i+2];
0N/A byte6 = (unsigned char)string[i+3];
0N/A if ( byte4 == 0xED && (byte5 & 0xF0) == 0xB0 ) {
0N/A /* Check encoding of 3rd byte */
0N/A if ( (byte6 & 0xC0) != 0x80 ) {
0N/A break; /* Error condition */
0N/A }
0N/A newLength++; /* New string will have 4byte encoding */
0N/A i += 3; /* Skip next 3 bytes */
0N/A }
0N/A }
0N/A } else {
0N/A break; /* Error condition */
0N/A }
0N/A }
0N/A if ( i != length ) {
0N/A /* Error in UTF encoding */
0N/A /* FIXUP: ERROR_MESSAGE()? */
0N/A return length;
0N/A }
0N/A return newLength;
0N/A}
0N/A
0N/A/* Convert a Modified UTF-8 string into a Standard UTF-8 string
0N/A * It is assumed that this string has been validated in terms of the
0N/A * basic UTF encoding rules by utf8Length() above.
0N/A *
0N/A * Note: No validation is made that this is indeed Modified UTF-8 coming in.
0N/A *
0N/A */
0N/Avoid JNICALL
0N/Autf8mToUtf8s(struct UtfInst *ui, jbyte *string, int length, jbyte *newString, int newLength)
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, byte2, byte3, byte4, byte5, byte6;
0N/A
0N/A byte1 = (unsigned char)string[i];
0N/A if ( (byte1 & 0x80) == 0 ) { /* 1byte encoding */
0N/A /* Single byte */
0N/A newString[j++] = byte1;
0N/A } else if ( (byte1 & 0xE0) == 0xC0 ) { /* 2byte encoding */
0N/A byte2 = (unsigned char)string[++i];
0N/A if ( byte1 != 0xC0 || byte2 != 0x80 ) {
0N/A newString[j++] = byte1;
0N/A newString[j++] = byte2;
0N/A } else {
0N/A newString[j++] = 0;
0N/A }
0N/A } else if ( (byte1 & 0xF0) == 0xE0 ) { /* 3byte encoding */
0N/A byte2 = (unsigned char)string[++i];
0N/A byte3 = (unsigned char)string[++i];
0N/A if ( i+3 < length && byte1 == 0xED && (byte2 & 0xF0) == 0xA0 ) {
0N/A /* See if this is a pair of 3byte encodings */
0N/A byte4 = (unsigned char)string[i+1];
0N/A byte5 = (unsigned char)string[i+2];
0N/A byte6 = (unsigned char)string[i+3];
0N/A if ( byte4 == 0xED && (byte5 & 0xF0) == 0xB0 ) {
0N/A unsigned u21;
0N/A
0N/A /* Bits in: 11101101 1010xxxx 10xxxxxx */
0N/A /* Bits in: 11101101 1011xxxx 10xxxxxx */
0N/A i += 3;
0N/A
0N/A /* Reconstruct 21 bit code */
0N/A u21 = ((byte2 & 0x0F) + 1) << 16;
0N/A u21 += (byte3 & 0x3F) << 10;
0N/A u21 += (byte5 & 0x0F) << 6;
0N/A u21 += (byte6 & 0x3F);
0N/A
0N/A /* Bits out: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
0N/A
0N/A /* Convert to 4byte encoding */
0N/A newString[j++] = 0xF0 + ((u21 >> 18) & 0x07);
0N/A newString[j++] = 0x80 + ((u21 >> 12) & 0x3F);
0N/A newString[j++] = 0x80 + ((u21 >> 6) & 0x3F);
0N/A newString[j++] = 0x80 + (u21 & 0x3F);
0N/A continue;
0N/A }
0N/A }
0N/A /* Normal 3byte encoding */
0N/A newString[j++] = byte1;
0N/A newString[j++] = byte2;
0N/A newString[j++] = byte3;
0N/A }
0N/A }
0N/A UTF_ASSERT(i==length);
0N/A UTF_ASSERT(j==newLength);
0N/A newString[j] = 0;
0N/A}
0N/A
0N/A/* ================================================================= */
0N/A
0N/A#if 1 /* Test program */
0N/A
0N/A/*
0N/A * Convert any byte array into a printable string.
0N/A * Returns length or -1 if output overflows.
0N/A */
0N/Astatic int
0N/AbytesToPrintable(struct UtfInst *ui, char *bytes, int len, char *output, int outputMaxLen)
0N/A{
0N/A int outputLen;
0N/A int i;
0N/A
0N/A UTF_ASSERT(bytes);
0N/A UTF_ASSERT(len>=0);
0N/A UTF_ASSERT(output);
0N/A UTF_ASSERT(outputMaxLen>=0);
0N/A
0N/A outputLen = 0;
0N/A for ( i=0; i<len ; i++ ) {
0N/A unsigned byte;
0N/A
0N/A byte = bytes[i];
0N/A if ( outputLen >= outputMaxLen ) {
0N/A return -1;
0N/A }
0N/A if ( byte <= 0x7f && isprint(byte) && !iscntrl(byte) ) {
0N/A output[outputLen++] = (char)byte;
0N/A } else {
0N/A (void)sprintf(output+outputLen,"\\x%02x",byte);
0N/A outputLen += 4;
0N/A }
0N/A }
0N/A output[outputLen] = 0;
0N/A return outputLen;
0N/A}
0N/A
0N/Astatic void
0N/Atest(void)
0N/A{
0N/A static char *strings[] = {
0N/A "characters",
0N/A "abcdefghijklmnopqrstuvwxyz",
0N/A "0123456789",
0N/A "!@#$%^&*()_+=-{}[]:;",
0N/A NULL };
0N/A int i;
0N/A struct UtfInst *ui;
0N/A
0N/A ui = utfInitialize(NULL);
0N/A
0N/A i = 0;
0N/A while ( strings[i] != NULL ) {
0N/A char *str;
0N/A #define MAX 1024
0N/A char buf0[MAX];
0N/A char buf1[MAX];
0N/A char buf2[MAX];
0N/A unsigned short buf3[MAX];
0N/A int len1;
0N/A int len2;
0N/A int len3;
0N/A
0N/A str = strings[i];
0N/A
0N/A (void)bytesToPrintable(ui, str, (int)strlen(str), buf0, 1024);
0N/A
0N/A len1 = utf8FromPlatform(ui, str, (int)strlen(str), (jbyte*)buf1, 1024);
0N/A
0N/A UTF_ASSERT(len1==(int)strlen(str));
0N/A
0N/A len3 = utf8ToUtf16(ui, (jbyte*)buf1, len1, (jchar*)buf3, 1024);
0N/A
0N/A UTF_ASSERT(len3==len1);
0N/A
0N/A len1 = utf16ToUtf8m(ui, (jchar*)buf3, len3, (jbyte*)buf1, 1024);
0N/A
0N/A UTF_ASSERT(len1==len3);
0N/A UTF_ASSERT(strcmp(str, buf1) == 0);
0N/A
0N/A len2 = utf8ToPlatform(ui, (jbyte*)buf1, len1, buf2, 1024);
0N/A
0N/A UTF_ASSERT(len2==len1);
0N/A UTF_ASSERT(strcmp(str, buf2) == 0);
0N/A
0N/A i++;
0N/A }
0N/A
0N/A utfTerminate(ui, NULL);
0N/A
0N/A}
0N/A
0N/Aint
0N/Amain(int argc, char **argv)
0N/A{
0N/A test();
0N/A return 0;
0N/A}
0N/A
0N/A#endif