Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.instrument/unix/native/libinstrument/EncodingSupport_md.c
41149 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
#ifndef __ANDROID__
68
codeset = (char*)nl_langinfo(CODESET);
69
#else
70
codeset = "ASCII"; // (MB_CUR_MAX == 1) ? "ASCII" : "UTF-8";
71
#endif
72
if ( codeset == NULL || codeset[0] == 0 ) {
73
UTF_DEBUG(("NO codeset returned by nl_langinfo(CODESET)\n"));
74
return;
75
}
76
77
UTF_DEBUG(("Codeset = %s\n", codeset));
78
79
#ifdef MACOSX
80
/* On Mac, if US-ASCII, but with no env hints, use UTF-8 */
81
const char* env_lang = getenv("LANG");
82
const char* env_lc_all = getenv("LC_ALL");
83
const char* env_lc_ctype = getenv("LC_CTYPE");
84
85
if (strcmp(codeset,"US-ASCII") == 0 &&
86
(env_lang == NULL || strlen(env_lang) == 0) &&
87
(env_lc_all == NULL || strlen(env_lc_all) == 0) &&
88
(env_lc_ctype == NULL || strlen(env_lc_ctype) == 0)) {
89
codeset = "UTF-8";
90
}
91
#endif
92
93
/* If we don't need this, skip it */
94
if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
95
UTF_DEBUG(("NO iconv() being used because it is not needed\n"));
96
return;
97
}
98
99
/* Open conversion descriptors */
100
iconvToPlatform = iconv_open(codeset, "UTF-8");
101
if ( iconvToPlatform == (iconv_t)-1 ) {
102
UTF_ERROR("Failed to complete iconv_open() setup");
103
}
104
iconvFromPlatform = iconv_open("UTF-8", codeset);
105
if ( iconvFromPlatform == (iconv_t)-1 ) {
106
UTF_ERROR("Failed to complete iconv_open() setup");
107
}
108
}
109
110
/*
111
* Terminate all utf processing
112
*/
113
static void
114
utfTerminate(void)
115
{
116
if ( iconvFromPlatform!=(iconv_t)-1 ) {
117
(void)iconv_close(iconvFromPlatform);
118
}
119
if ( iconvToPlatform!=(iconv_t)-1 ) {
120
(void)iconv_close(iconvToPlatform);
121
}
122
iconvToPlatform = (iconv_t)-1;
123
iconvFromPlatform = (iconv_t)-1;
124
}
125
126
/*
127
* Do iconv() conversion.
128
* Returns length or -1 if output overflows.
129
*/
130
static int
131
iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
132
{
133
int outputLen = 0;
134
135
UTF_ASSERT(bytes);
136
UTF_ASSERT(len>=0);
137
UTF_ASSERT(output);
138
UTF_ASSERT(outputMaxLen>len);
139
140
output[0] = 0;
141
outputLen = 0;
142
143
if ( ic != (iconv_t)-1 ) {
144
int returnValue;
145
size_t inLeft;
146
size_t outLeft;
147
char *inbuf;
148
char *outbuf;
149
150
inbuf = bytes;
151
outbuf = output;
152
inLeft = len;
153
outLeft = outputMaxLen;
154
returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
155
if ( returnValue >= 0 && inLeft==0 ) {
156
outputLen = outputMaxLen-outLeft;
157
output[outputLen] = 0;
158
return outputLen;
159
}
160
161
/* Failed to do the conversion */
162
UTF_DEBUG(("iconv() failed to do the conversion\n"));
163
return -1;
164
}
165
166
/* Just copy bytes */
167
outputLen = len;
168
(void)memcpy(output, bytes, len);
169
output[len] = 0;
170
return outputLen;
171
}
172
173
/*
174
* Convert UTF-8 to Platform Encoding.
175
* Returns length or -1 if output overflows.
176
*/
177
static int
178
utf8ToPlatform(char *utf8, int len, char *output, int outputMaxLen)
179
{
180
return iconvConvert(iconvToPlatform, utf8, len, output, outputMaxLen);
181
}
182
183
/*
184
* Convert Platform Encoding to UTF-8.
185
* Returns length or -1 if output overflows.
186
*/
187
static int
188
platformToUtf8(char *str, int len, char *output, int outputMaxLen)
189
{
190
return iconvConvert(iconvFromPlatform, str, len, output, outputMaxLen);
191
}
192
193
int
194
convertUft8ToPlatformString(char* utf8_str, int utf8_len, char* platform_str, int platform_len) {
195
if (iconvToPlatform == (iconv_t)-1) {
196
utfInitialize();
197
}
198
return utf8ToPlatform(utf8_str, utf8_len, platform_str, platform_len);
199
}
200
201