Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/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 "utf.h"
27
28
#include <windows.h>
29
#include <stdlib.h>
30
#include <stdio.h>
31
32
/*
33
* Initialize all utf processing.
34
*/
35
struct UtfInst * JNICALL
36
utfInitialize(char *options)
37
{
38
struct UtfInst *ui;
39
LANGID langID;
40
LCID localeID;
41
TCHAR strCodePage[7]; // ANSI code page id
42
43
ui = (struct UtfInst*)calloc(sizeof(struct UtfInst), 1);
44
45
/*
46
* Get the code page for this locale
47
*/
48
langID = LANGIDFROMLCID(GetUserDefaultLCID());
49
localeID = MAKELCID(langID, SORT_DEFAULT);
50
if (GetLocaleInfo(localeID, LOCALE_IDEFAULTANSICODEPAGE,
51
strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 ) {
52
ui->platformCodePage = atoi(strCodePage);
53
} else {
54
ui->platformCodePage = GetACP();
55
}
56
return ui;
57
}
58
59
/*
60
* Terminate all utf processing
61
*/
62
void JNICALL
63
utfTerminate(struct UtfInst *ui, char *options)
64
{
65
(void)free(ui);
66
}
67
68
/*
69
* Get wide string (assumes len>0)
70
*/
71
static WCHAR*
72
getWideString(UINT codePage, char* str, int len, int *pwlen)
73
{
74
int wlen;
75
WCHAR* wstr;
76
77
/* Convert the string to WIDE string */
78
wlen = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);
79
*pwlen = wlen;
80
if (wlen <= 0) {
81
UTF_ERROR(("Can't get WIDE string length"));
82
return NULL;
83
}
84
wstr = (WCHAR*)malloc(wlen * sizeof(WCHAR));
85
if (wstr == NULL) {
86
UTF_ERROR(("Can't malloc() any space"));
87
return NULL;
88
}
89
if (MultiByteToWideChar(codePage, 0, str, len, wstr, wlen) == 0) {
90
UTF_ERROR(("Can't get WIDE string"));
91
return NULL;
92
}
93
return wstr;
94
}
95
96
/*
97
* Convert UTF-8 to a platform string
98
*/
99
int JNICALL
100
utf8ToPlatform(struct UtfInst *ui, jbyte *utf8, int len, char* output, int outputMaxLen)
101
{
102
int wlen;
103
int plen;
104
WCHAR* wstr;
105
106
/* Negative length is an error */
107
if ( len < 0 ) {
108
return -1;
109
}
110
111
/* Zero length is ok, but we don't need to do much */
112
if ( len == 0 ) {
113
output[0] = 0;
114
return 0;
115
}
116
117
/* Get WIDE string version (assumes len>0) */
118
wstr = getWideString(CP_UTF8, (char*)utf8, len, &wlen);
119
if ( wstr == NULL ) {
120
return -1;
121
}
122
123
/* Convert WIDE string to MultiByte string */
124
plen = WideCharToMultiByte(ui->platformCodePage, 0, wstr, wlen,
125
output, outputMaxLen, NULL, NULL);
126
free(wstr);
127
if (plen <= 0) {
128
UTF_ERROR(("Can't convert WIDE string to multi-byte"));
129
return -1;
130
}
131
output[plen] = '\0';
132
return plen;
133
}
134
135
/*
136
* Convert Platform Encoding to UTF-8.
137
*/
138
int JNICALL
139
utf8FromPlatform(struct UtfInst *ui, char *str, int len, jbyte *output, int outputMaxLen)
140
{
141
int wlen;
142
int plen;
143
WCHAR* wstr;
144
145
/* Negative length is an error */
146
if ( len < 0 ) {
147
return -1;
148
}
149
150
/* Zero length is ok, but we don't need to do much */
151
if ( len == 0 ) {
152
output[0] = 0;
153
return 0;
154
}
155
156
/* Get WIDE string version (assumes len>0) */
157
wstr = getWideString(ui->platformCodePage, str, len, &wlen);
158
if ( wstr == NULL ) {
159
return -1;
160
}
161
162
/* Convert WIDE string to UTF-8 string */
163
plen = WideCharToMultiByte(CP_UTF8, 0, wstr, wlen,
164
(char*)output, outputMaxLen, NULL, NULL);
165
free(wstr);
166
if (plen <= 0) {
167
UTF_ERROR(("Can't convert WIDE string to multi-byte"));
168
return -1;
169
}
170
output[plen] = '\0';
171
return plen;
172
}
173
174