Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c
66644 views
1
/*
2
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
#include <stdio.h>
26
#include <stddef.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <ctype.h>
30
#include <locale.h>
31
#include <langinfo.h>
32
#include <iconv.h>
33
34
/* Routines to convert back and forth between Platform Encoding and UTF-8 */
35
36
/* Error and assert macros */
37
#define UTF_ERROR(m) utfError(__FILE__, __LINE__, m)
38
#define UTF_ASSERT(x) ( (x)==0 ? UTF_ERROR("ASSERT ERROR " #x) : (void)0 )
39
#define UTF_DEBUG(x)
40
41
/* Global variables */
42
static iconv_t iconvToPlatform = (iconv_t)-1;
43
static iconv_t iconvFromPlatform = (iconv_t)-1;
44
45
/*
46
* Error handler
47
*/
48
static void
49
utfError(char *file, int line, char *message)
50
{
51
(void)fprintf(stderr, "UTF ERROR [\"%s\":%d]: %s\n", file, line, message);
52
abort();
53
}
54
55
/*
56
* Initialize all utf processing.
57
*/
58
static void
59
utfInitialize(void)
60
{
61
const char* codeset;
62
63
/* Set the locale from the environment */
64
(void)setlocale(LC_ALL, "");
65
66
/* Get the codeset name */
67
codeset = (char*)nl_langinfo(CODESET);
68
if ( codeset == NULL || codeset[0] == 0 ) {
69
UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n"));
70
return;
71
}
72
73
UTF_DEBUG(("Codeset = %s\n", codeset));
74
75
#ifdef MACOSX
76
/* On Mac, if US-ASCII, but with no env hints, use UTF-8 */
77
const char* env_lang = getenv("LANG");
78
const char* env_lc_all = getenv("LC_ALL");
79
const char* env_lc_ctype = getenv("LC_CTYPE");
80
81
if (strcmp(codeset,"US-ASCII") == 0 &&
82
(env_lang == NULL || strlen(env_lang) == 0) &&
83
(env_lc_all == NULL || strlen(env_lc_all) == 0) &&
84
(env_lc_ctype == NULL || strlen(env_lc_ctype) == 0)) {
85
codeset = "UTF-8";
86
}
87
#endif
88
89
/* If we don't need this, skip it */
90
if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
91
UTF_DEBUG(("NO iconv() being used because it is not needed\n"));
92
return;
93
}
94
95
/* Open conversion descriptors */
96
iconvToPlatform = iconv_open(codeset, "UTF-8");
97
if ( iconvToPlatform == (iconv_t)-1 ) {
98
UTF_ERROR("Failed to complete iconv_open() setup");
99
}
100
iconvFromPlatform = iconv_open("UTF-8", codeset);
101
if ( iconvFromPlatform == (iconv_t)-1 ) {
102
UTF_ERROR("Failed to complete iconv_open() setup");
103
}
104
}
105
106
/*
107
* Terminate all utf processing
108
*/
109
static void
110
utfTerminate(void)
111
{
112
if ( iconvFromPlatform!=(iconv_t)-1 ) {
113
(void)iconv_close(iconvFromPlatform);
114
}
115
if ( iconvToPlatform!=(iconv_t)-1 ) {
116
(void)iconv_close(iconvToPlatform);
117
}
118
iconvToPlatform = (iconv_t)-1;
119
iconvFromPlatform = (iconv_t)-1;
120
}
121
122
/*
123
* Do iconv() conversion.
124
* Returns length or -1 if output overflows.
125
*/
126
static int
127
iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
128
{
129
int outputLen = 0;
130
131
UTF_ASSERT(bytes);
132
UTF_ASSERT(len>=0);
133
UTF_ASSERT(output);
134
UTF_ASSERT(outputMaxLen>len);
135
136
output[0] = 0;
137
outputLen = 0;
138
139
if ( ic != (iconv_t)-1 ) {
140
int returnValue;
141
size_t inLeft;
142
size_t outLeft;
143
char *inbuf;
144
char *outbuf;
145
146
inbuf = bytes;
147
outbuf = output;
148
inLeft = len;
149
outLeft = outputMaxLen;
150
returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
151
if ( returnValue >= 0 && inLeft==0 ) {
152
outputLen = outputMaxLen-outLeft;
153
output[outputLen] = 0;
154
return outputLen;
155
}
156
157
/* Failed to do the conversion */
158
UTF_DEBUG(("iconv() failed to do the conversion\n"));
159
return -1;
160
}
161
162
/* Just copy bytes */
163
outputLen = len;
164
(void)memcpy(output, bytes, len);
165
output[len] = 0;
166
return outputLen;
167
}
168
169
/*
170
* Convert UTF-8 to Platform Encoding.
171
* Returns length or -1 if output overflows.
172
*/
173
static int
174
utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)
175
{
176
return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);
177
}
178
179
/*
180
* Convert Platform Encoding to UTF-8.
181
* Returns length or -1 if output overflows.
182
*/
183
static int
184
platformToUtf8(char *str, int len, char *output, int outputMaxLen)
185
{
186
return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);
187
}
188
189
int
190
convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {
191
if (iconvToPlatform == (iconv_t)-1) {
192
utfInitialize();
193
}
194
return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);
195
}
196
197