jdk/src/solaris/instrument/EncodingSupport_md.c
author ohair
Tue, 11 Sep 2012 13:40:59 -0700
changeset 13678 5c8001201f98
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7197771: Adjust jdk sources to avoid use of implementation defined value of __FILE__ 7180608: Sort the order of object files when building shared libraries Reviewed-by: ohrstrom, erikj, tbell
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, 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
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    char *codeset;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* If we don't need this, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        UTF_DEBUG(("NO iconv() being used because it is not needed\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /* Open conversion descriptors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    iconvToPlatform   = iconv_open(codeset, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    if ( iconvToPlatform == (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        UTF_ERROR("Failed to complete iconv_open() setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    iconvFromPlatform = iconv_open("UTF-8", codeset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    if ( iconvFromPlatform == (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        UTF_ERROR("Failed to complete iconv_open() setup");
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * Terminate all utf processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
utfTerminate(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if ( iconvFromPlatform!=(iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        (void)iconv_close(iconvFromPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    if ( iconvToPlatform!=(iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        (void)iconv_close(iconvToPlatform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    iconvToPlatform   = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    iconvFromPlatform = (iconv_t)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Do iconv() conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    int outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    UTF_ASSERT(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    UTF_ASSERT(len>=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    UTF_ASSERT(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    UTF_ASSERT(outputMaxLen>len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    output[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    outputLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    if ( ic != (iconv_t)-1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int          returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        size_t       inLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        size_t       outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        char        *inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        char        *outbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        inbuf        = bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        outbuf       = output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        inLeft       = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        outLeft      = outputMaxLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        returnValue  = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if ( returnValue >= 0 && inLeft==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            outputLen = outputMaxLen-outLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            output[outputLen] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /* Failed to do the conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /* Just copy bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    outputLen = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    (void)memcpy(output, bytes, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    output[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return outputLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * Convert UTF-8 to Platform Encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * Convert Platform Encoding to UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *    Returns length or -1 if output overflows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
platformToUtf8(char *str, int len, char *output, int outputMaxLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (iconvToPlatform ==  (iconv_t)-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        utfInitialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}