0N/A/*
1753N/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
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/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,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/A#include "utf.h"
0N/A
0N/A#include <windows.h>
0N/A#include <stdlib.h>
0N/A#include <stdio.h>
0N/A
0N/A/*
0N/A * Initialize all utf processing.
0N/A */
0N/Astruct UtfInst * JNICALL
0N/AutfInitialize(char *options)
0N/A{
0N/A struct UtfInst *ui;
0N/A LANGID langID;
0N/A LCID localeID;
0N/A TCHAR strCodePage[7]; // ANSI code page id
0N/A
0N/A ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);
0N/A
0N/A /*
0N/A * Get the code page for this locale
0N/A */
0N/A langID = LANGIDFROMLCID(GetUserDefaultLCID());
0N/A localeID = MAKELCID(langID, SORT_DEFAULT);
0N/A if (GetLocaleInfo(localeID, LOCALE_IDEFAULTANSICODEPAGE,
0N/A strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 ) {
0N/A ui->platformCodePage = atoi(strCodePage);
0N/A } else {
0N/A ui->platformCodePage = GetACP();
0N/A }
0N/A return ui;
1753N/A}
1753N/A
1753N/A/*
1753N/A * Terminate all utf processing
1753N/A */
1753N/Avoid JNICALL
1753N/AutfTerminate(struct UtfInst *ui, char *options)
1753N/A{
1753N/A (void)free(ui);
1753N/A}
1753N/A
1753N/A/*
1753N/A * Get wide string (assumes len>0)
1753N/A */
1753N/Astatic WCHAR*
1753N/AgetWideString(UINT codePage, char* str, int len, int *pwlen)
1753N/A{
1753N/A int wlen;
1753N/A WCHAR* wstr;
0N/A
0N/A /* Convert the string to WIDE string */
0N/A wlen = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);
0N/A *pwlen = wlen;
0N/A if (wlen <= 0) {
0N/A UTF_ERROR(("Can't get WIDE string length"));
0N/A return NULL;
0N/A }
0N/A wstr = (WCHAR*)malloc(wlen * sizeof(WCHAR));
0N/A if (wstr == NULL) {
0N/A UTF_ERROR(("Can't malloc() any space"));
0N/A return NULL;
0N/A }
0N/A if (MultiByteToWideChar(codePage, 0, str, len, wstr, wlen) == 0) {
0N/A UTF_ERROR(("Can't get WIDE string"));
0N/A return NULL;
1753N/A }
0N/A return wstr;
0N/A}
0N/A
0N/A/*
0N/A * Convert UTF-8 to a platform string
1753N/A */
0N/Aint JNICALL
1753N/Autf8ToPlatform(struct UtfInst *ui, jbyte *utf8, int len, char* output, int outputMaxLen)
0N/A{
0N/A int wlen;
0N/A int plen;
0N/A WCHAR* wstr;
0N/A
0N/A /* Negative length is an error */
0N/A if ( len < 0 ) {
0N/A return -1;
0N/A }
0N/A
0N/A /* Zero length is ok, but we don't need to do much */
0N/A if ( len == 0 ) {
0N/A output[0] = 0;
0N/A return 0;
0N/A }
0N/A
0N/A /* Get WIDE string version (assumes len>0) */
0N/A wstr = getWideString(CP_UTF8, (char*)utf8, len, &wlen);
0N/A if ( wstr == NULL ) {
0N/A return -1;
0N/A }
0N/A
0N/A /* Convert WIDE string to MultiByte string */
0N/A plen = WideCharToMultiByte(ui->platformCodePage, 0, wstr, wlen,
0N/A output, outputMaxLen, NULL, NULL);
0N/A free(wstr);
0N/A if (plen <= 0) {
0N/A UTF_ERROR(("Can't convert WIDE string to multi-byte"));
0N/A return -1;
0N/A }
0N/A output[plen] = '\0';
0N/A return plen;
0N/A}
0N/A
0N/A/*
0N/A * Convert Platform Encoding to UTF-8.
0N/A */
0N/Aint JNICALL
0N/Autf8FromPlatform(struct UtfInst *ui, char *str, int len, jbyte *output, int outputMaxLen)
0N/A{
0N/A int wlen;
0N/A int plen;
0N/A WCHAR* wstr;
0N/A
0N/A /* Negative length is an error */
0N/A if ( len < 0 ) {
0N/A return -1;
0N/A }
0N/A
1753N/A /* Zero length is ok, but we don't need to do much */
0N/A if ( len == 0 ) {
0N/A output[0] = 0;
0N/A return 0;
342N/A }
342N/A
0N/A /* Get WIDE string version (assumes len>0) */
0N/A wstr = getWideString(ui->platformCodePage, str, len, &wlen);
0N/A if ( wstr == NULL ) {
0N/A return -1;
0N/A }
0N/A
1753N/A /* Convert WIDE string to UTF-8 string */
1753N/A plen = WideCharToMultiByte(CP_UTF8, 0, wstr, wlen,
1753N/A (char*)output, outputMaxLen, NULL, NULL);
0N/A free(wstr);
0N/A if (plen <= 0) {
0N/A UTF_ERROR(("Can't convert WIDE string to multi-byte"));
0N/A return -1;
0N/A }
0N/A output[plen] = '\0';
0N/A return plen;
0N/A}
0N/A