Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/npt/utf_md.c
32285 views
1
/*
2
* Copyright (c) 2004, 2005, 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
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <locale.h>
30
#ifndef __ANDROID__
31
# include <langinfo.h>
32
# include <iconv.h>
33
#else
34
# include "langinfo.h"
35
# include "iconv.h"
36
#endif
37
38
#include "utf.h"
39
40
/* Global variables */
41
42
/*
43
* Initialize all utf processing.
44
*/
45
struct UtfInst *JNICALL
46
utfInitialize(char *options)
47
{
48
struct UtfInst *ui;
49
char *codeset;
50
51
ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);
52
ui->iconvToPlatform = (void *)-1;
53
ui->iconvFromPlatform = (void *)-1;
54
55
/* Set the locale from the environment */
56
(void)setlocale(LC_ALL, "");
57
58
/* Get the codeset name */
59
codeset = (char*)nl_langinfo(CODESET);
60
if ( codeset == NULL || codeset[0] == 0 ) {
61
return ui;
62
}
63
64
/* If we don't need this, skip it */
65
if (strcmp(codeset, "UTF-8") == 0 || strcmp(codeset, "utf8") == 0 ) {
66
return ui;
67
}
68
69
/* Open conversion descriptors */
70
ui->iconvToPlatform = iconv_open(codeset, "UTF-8");
71
if ( ui->iconvToPlatform == (void *)-1 ) {
72
UTF_ERROR("Failed to complete iconv_open() setup");
73
}
74
ui->iconvFromPlatform = iconv_open("UTF-8", codeset);
75
if ( ui->iconvFromPlatform == (void *)-1 ) {
76
UTF_ERROR("Failed to complete iconv_open() setup");
77
}
78
return ui;
79
}
80
81
/*
82
* Terminate all utf processing
83
*/
84
void JNICALL
85
utfTerminate(struct UtfInst *ui, char *options)
86
{
87
if ( ui->iconvFromPlatform != (void *)-1 ) {
88
(void)iconv_close(ui->iconvFromPlatform);
89
}
90
if ( ui->iconvToPlatform != (void *)-1 ) {
91
(void)iconv_close(ui->iconvToPlatform);
92
}
93
ui->iconvToPlatform = (void *)-1;
94
ui->iconvFromPlatform = (void *)-1;
95
(void)free(ui);
96
}
97
98
/*
99
* Do iconv() conversion.
100
* Returns length or -1 if output overflows.
101
*/
102
static int
103
iconvConvert(iconv_t ic, char *bytes, int len, char *output, int outputMaxLen)
104
{
105
int outputLen = 0;
106
107
UTF_ASSERT(bytes);
108
UTF_ASSERT(len>=0);
109
UTF_ASSERT(output);
110
UTF_ASSERT(outputMaxLen>len);
111
112
output[0] = 0;
113
outputLen = 0;
114
115
if ( ic != (iconv_t)(void *)-1 ) {
116
int returnValue;
117
size_t inLeft;
118
size_t outLeft;
119
char *inbuf;
120
char *outbuf;
121
122
inbuf = bytes;
123
outbuf = output;
124
inLeft = len;
125
outLeft = outputMaxLen;
126
returnValue = iconv(ic, (void*)&inbuf, &inLeft, &outbuf, &outLeft);
127
if ( returnValue >= 0 && inLeft==0 ) {
128
outputLen = outputMaxLen-outLeft;
129
output[outputLen] = 0;
130
return outputLen;
131
}
132
133
/* Failed to do the conversion */
134
return -1;
135
}
136
137
/* Just copy bytes */
138
outputLen = len;
139
(void)memcpy(output, bytes, len);
140
output[len] = 0;
141
return outputLen;
142
}
143
144
/*
145
* Convert UTF-8 to Platform Encoding.
146
* Returns length or -1 if output overflows.
147
*/
148
int JNICALL
149
utf8ToPlatform(struct UtfInst*ui, jbyte *utf8, int len, char *output, int outputMaxLen)
150
{
151
/* Negative length is an error */
152
if ( len < 0 ) {
153
return -1;
154
}
155
156
/* Zero length is ok, but we don't need to do much */
157
if ( len == 0 ) {
158
output[0] = 0;
159
return 0;
160
}
161
162
return iconvConvert(ui->iconvToPlatform, (char*)utf8, len, output, outputMaxLen);
163
}
164
165
/*
166
* Convert Platform Encoding to UTF-8.
167
* Returns length or -1 if output overflows.
168
*/
169
int JNICALL
170
utf8FromPlatform(struct UtfInst*ui, char *str, int len, jbyte *output, int outputMaxLen)
171
{
172
/* Negative length is an error */
173
if ( len < 0 ) {
174
return -1;
175
}
176
177
/* Zero length is ok, but we don't need to do much */
178
if ( len == 0 ) {
179
output[0] = 0;
180
return 0;
181
}
182
183
return iconvConvert(ui->iconvFromPlatform, str, len, (char*)output, outputMaxLen);
184
}
185
186