src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c
author stefank
Mon, 25 Nov 2019 12:32:40 +0100
changeset 59251 4cbfa5077d68
parent 51854 3c6d285c8168
permissions -rw-r--r--
8234739: Harmonize parameter order in Atomic - xchg Reviewed-by: rehn, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51854
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 2018, 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
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stddef.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 <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <locale.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <langinfo.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <iconv.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/* Routines to convert back and forth between Platform Encoding and UTF-8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
13678
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    36
/* Use THIS_FILE when it is available. */
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    37
#ifndef THIS_FILE
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    38
    #define THIS_FILE __FILE__
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    39
#endif
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Error and assert macros */
13678
5c8001201f98 7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__
ohair
parents: 5506
diff changeset
    42
#define UTF_ERROR(m) utfError(THIS_FILE, __LINE__,  m)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#define UTF_ASSERT(x) ( (x)==0 ? UTF_ERROR("ASSERT ERROR " #x) : (void)0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define UTF_DEBUG(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/* Global variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static iconv_t iconvToPlatform          = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static iconv_t iconvFromPlatform        = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Error handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
utfError(char *file, int line, char *message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    (void)fprintf(stderr, "UTF ERROR [\"%s\":%d]: %s\n", file, line, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Initialize all utf processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
utfInitialize(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
51854
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    66
    const char* codeset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Set the locale from the environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    (void)setlocale(LC_ALL, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* Get the codeset name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    codeset = (char*)nl_langinfo(CODESET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    if ( codeset == NULL || codeset[0] == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    UTF_DEBUG(("Codeset = %s\n", codeset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
51854
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    80
#ifdef MACOSX
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    81
    /* On Mac, if US-ASCII, but with no env hints, use UTF-8 */
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    82
    const char* env_lang = getenv("LANG");
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    83
    const char* env_lc_all = getenv("LC_ALL");
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    84
    const char* env_lc_ctype = getenv("LC_CTYPE");
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    85
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    86
    if (strcmp(codeset,"US-ASCII") == 0 &&
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    87
        (env_lang == NULL || strlen(env_lang) == 0) &&
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    88
        (env_lc_all == NULL || strlen(env_lc_all) == 0) &&
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    89
        (env_lc_ctype == NULL || strlen(env_lc_ctype) == 0)) {
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    90
        codeset = "UTF-8";
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    91
    }
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    92
#endif
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
    93
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /* If we don't need this, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        UTF_DEBUG(("NO iconv() being used because it is not needed\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /* Open conversion descriptors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    iconvToPlatform   = iconv_open(codeset, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if ( iconvToPlatform == (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        UTF_ERROR("Failed to complete iconv_open() setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    iconvFromPlatform = iconv_open("UTF-8", codeset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    if ( iconvFromPlatform == (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        UTF_ERROR("Failed to complete iconv_open() setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * Terminate all utf processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
utfTerminate(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    if ( iconvFromPlatform!=(iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        (void)iconv_close(iconvFromPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    if ( iconvToPlatform!=(iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        (void)iconv_close(iconvToPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    iconvToPlatform   = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    iconvFromPlatform = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Do iconv() conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    int outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    UTF_ASSERT(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    UTF_ASSERT(len>=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    UTF_ASSERT(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    UTF_ASSERT(outputMaxLen>len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if ( ic != (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int          returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        size_t       inLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        size_t       outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        char        *inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        char        *outbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        inbuf        = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        outbuf       = output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        inLeft       = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        outLeft      = outputMaxLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        returnValue  = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if ( returnValue >= 0 && inLeft==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            outputLen = outputMaxLen-outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            output[outputLen] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        /* Failed to do the conversion */
51854
3c6d285c8168 8072130: java/lang/instrument/BootClassPath/BootClassPathTest.sh fails on Mac OSX
bchristi
parents: 47216
diff changeset
   163
        UTF_DEBUG(("iconv() failed to do the conversion\n"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /* Just copy bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    outputLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    (void)memcpy(output, bytes, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    output[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * Convert UTF-8 to Platform Encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * Convert Platform Encoding to UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
platformToUtf8(char *str, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    if (iconvToPlatform ==  (iconv_t)-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        utfInitialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}