jdk/src/solaris/npt/utf_md.c
author attila
Fri, 31 May 2013 12:56:56 +0200
changeset 17967 0ac7d2a303b9
parent 5506 202f599c92aa
permissions -rw-r--r--
8015693: reduce NodeLiteralNode to NullLiteralNode Reviewed-by: jlaskey, lagergren
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 <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <locale.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <langinfo.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <iconv.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "utf.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/* Global variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Initialize all utf processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
struct UtfInst *JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
utfInitialize(char *options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    struct UtfInst *ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    char           *codeset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    ui->iconvToPlatform         = (void *)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    ui->iconvFromPlatform       = (void *)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* Set the locale from the environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    (void)setlocale(LC_ALL, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* Get the codeset name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    codeset = (char*)nl_langinfo(CODESET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    if ( codeset == NULL || codeset[0] == 0 ) {
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
    /* If we don't need this, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* Open conversion descriptors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    ui->iconvToPlatform   = iconv_open(codeset, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if ( ui->iconvToPlatform == (void *)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        UTF_ERROR("Failed to complete iconv_open() setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    ui->iconvFromPlatform = iconv_open("UTF-8", codeset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if ( ui->iconvFromPlatform == (void *)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        UTF_ERROR("Failed to complete iconv_open() setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    return ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Terminate all utf processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
void  JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
utfTerminate(struct UtfInst *ui, char *options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if ( ui->iconvFromPlatform != (void *)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        (void)iconv_close(ui->iconvFromPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if ( ui->iconvToPlatform != (void *)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        (void)iconv_close(ui->iconvToPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    ui->iconvToPlatform   = (void *)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    ui->iconvFromPlatform = (void *)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    (void)free(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Do iconv() conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    int outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    UTF_ASSERT(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    UTF_ASSERT(len>=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    UTF_ASSERT(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    UTF_ASSERT(outputMaxLen>len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    if ( ic != (iconv_t)(void *)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int          returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        size_t       inLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        size_t       outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        char        *inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        char        *outbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        inbuf        = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        outbuf       = output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        inLeft       = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        outLeft      = outputMaxLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        returnValue  = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if ( returnValue >= 0 && inLeft==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            outputLen = outputMaxLen-outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            output[outputLen] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        /* Failed to do the conversion */
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /* Just copy bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    outputLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    (void)memcpy(output, bytes, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    output[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * Convert UTF-8 to Platform Encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
int  JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
utf8ToPlatform(struct UtfInst*ui, jbyte *utf8, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /* Negative length is an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    if ( len < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /* Zero length is ok, but we don't need to do much */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    if ( len == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    return iconvConvert(ui->iconvToPlatform, (char*)utf8, len, output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * Convert Platform Encoding to UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
int  JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
utf8FromPlatform(struct UtfInst*ui, char *str, int len, jbyte *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /* Negative length is an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    if ( len < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /* Zero length is ok, but we don't need to do much */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    if ( len == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    return iconvConvert(ui->iconvFromPlatform, str, len, (char*)output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}