jdk/src/windows/npt/utf_md.c
author lagergren
Tue, 12 Mar 2013 15:30:53 +0100
changeset 16523 af8b30edebce
parent 5506 202f599c92aa
permissions -rw-r--r--
8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change. Reviewed-by: attila, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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
}