jdk/src/windows/npt/utf_md.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "utf.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Initialize all utf processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
struct UtfInst * JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
utfInitialize(char *options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    struct UtfInst *ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    LANGID langID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    LCID localeID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    TCHAR strCodePage[7];       // ANSI code page id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Get the code page for this locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    langID = LANGIDFROMLCID(GetUserDefaultLCID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    localeID = MAKELCID(langID, SORT_DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    if (GetLocaleInfo(localeID, LOCALE_IDEFAULTANSICODEPAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                      strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ui->platformCodePage = atoi(strCodePage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        ui->platformCodePage = GetACP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    return ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Terminate all utf processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
utfTerminate(struct UtfInst *ui, char *options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    (void)free(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Get wide string  (assumes len>0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
static WCHAR*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
getWideString(UINT codePage, char* str, int len, int *pwlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int wlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    WCHAR* wstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* Convert the string to WIDE string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    wlen = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    *pwlen = wlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (wlen <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        UTF_ERROR(("Can't get WIDE string length"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    wstr = (WCHAR*)malloc(wlen * sizeof(WCHAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (wstr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        UTF_ERROR(("Can't malloc() any space"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (MultiByteToWideChar(codePage, 0, str, len, wstr, wlen) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        UTF_ERROR(("Can't get WIDE string"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    return wstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Convert UTF-8 to a platform string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
utf8ToPlatform(struct UtfInst *ui, jbyte *utf8, int len, char* output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int wlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    WCHAR* wstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /* Negative length is an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    if ( len < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* Zero length is ok, but we don't need to do much */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if ( len == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* Get WIDE string version (assumes len>0) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    wstr = getWideString(CP_UTF8, (char*)utf8, len, &wlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    if ( wstr == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /* Convert WIDE string to MultiByte string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    plen = WideCharToMultiByte(ui->platformCodePage, 0, wstr, wlen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                               output, outputMaxLen, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    free(wstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    if (plen <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        UTF_ERROR(("Can't convert WIDE string to multi-byte"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    output[plen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * Convert Platform Encoding to UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
int JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
utf8FromPlatform(struct UtfInst *ui, char *str, int len, jbyte *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    int wlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    int plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    WCHAR* wstr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /* Negative length is an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    if ( len < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /* Zero length is ok, but we don't need to do much */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if ( len == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /* Get WIDE string version (assumes len>0) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    wstr = getWideString(ui->platformCodePage, str, len, &wlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if ( wstr == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /* Convert WIDE string to UTF-8 string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    plen = WideCharToMultiByte(CP_UTF8, 0, wstr, wlen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                               (char*)output, outputMaxLen, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    free(wstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    if (plen <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        UTF_ERROR(("Can't convert WIDE string to multi-byte"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    output[plen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    return plen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}