jdk/src/solaris/instrument/EncodingSupport_md.c
author duke
Wed, 05 Jul 2017 16:45:59 +0200
changeset 1810 54dffad0bf06
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
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 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
#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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/* Error and assert macros */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#define UTF_ERROR(m) utfError(__FILE__, __LINE__,  m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#define UTF_ASSERT(x) ( (x)==0 ? UTF_ERROR("ASSERT ERROR " #x) : (void)0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#define UTF_DEBUG(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Global variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static iconv_t iconvToPlatform          = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static iconv_t iconvFromPlatform        = (iconv_t)-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
 * Error handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
utfError(char *file, int line, char *message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    (void)fprintf(stderr, "UTF ERROR [\"%s\":%d]: %s\n", file, line, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Initialize all utf processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
utfInitialize(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    char *codeset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* Set the locale from the environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    (void)setlocale(LC_ALL, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* Get the codeset name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    codeset = (char*)nl_langinfo(CODESET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if ( codeset == NULL || codeset[0] == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    UTF_DEBUG(("Codeset = %s\n", codeset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /* If we don't need this, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        UTF_DEBUG(("NO iconv() being used because it is not needed\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* Open conversion descriptors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    iconvToPlatform   = iconv_open(codeset, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    if ( iconvToPlatform == (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        UTF_ERROR("Failed to complete iconv_open() setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    iconvFromPlatform = iconv_open("UTF-8", codeset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    if ( iconvFromPlatform == (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        UTF_ERROR("Failed to complete iconv_open() setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * Terminate all utf processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
utfTerminate(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if ( iconvFromPlatform!=(iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        (void)iconv_close(iconvFromPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    if ( iconvToPlatform!=(iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        (void)iconv_close(iconvToPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    iconvToPlatform   = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    iconvFromPlatform = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Do iconv() conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    int outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    UTF_ASSERT(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    UTF_ASSERT(len>=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    UTF_ASSERT(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    UTF_ASSERT(outputMaxLen>len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    if ( ic != (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int          returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        size_t       inLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        size_t       outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        char        *inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        char        *outbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        inbuf        = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        outbuf       = output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        inLeft       = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        outLeft      = outputMaxLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        returnValue  = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if ( returnValue >= 0 && inLeft==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            outputLen = outputMaxLen-outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            output[outputLen] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /* Failed to do the conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /* Just copy bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    outputLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    (void)memcpy(output, bytes, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    output[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * Convert UTF-8 to Platform Encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * Convert Platform Encoding to UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
platformToUtf8(char *str, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    if (iconvToPlatform ==  (iconv_t)-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        utfInitialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}