1 /*
2 * Copyright (c) 1998, 2019, 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
313 }
314 } else {
315 return -1;
316 }
317 }
318
319 if (dst >= dend) {
320 errno = ENAMETOOLONG;
321 return -1;
322 }
323 *dst = L'\0';
324 return 0;
325 }
326
327 /* Non-Wide character version of canonicalize.
328 Converts to wchar and delegates to wcanonicalize. */
329 JNIEXPORT int
330 JDK_Canonicalize(const char *orig, char *out, int len) {
331 wchar_t* wpath = NULL;
332 wchar_t* wresult = NULL;
333 size_t conv;
334 size_t path_len = strlen(orig);
335 int ret = -1;
336
337 if ((wpath = (wchar_t*) malloc(sizeof(wchar_t) * (path_len + 1))) == NULL) {
338 goto finish;
339 }
340
341 if (mbstowcs_s(&conv, wpath, path_len + 1, orig, path_len) != 0) {
342 goto finish;
343 }
344
345 if ((wresult = (wchar_t*) malloc(sizeof(wchar_t) * len)) == NULL) {
346 goto finish;
347 }
348
349 if (wcanonicalize(wpath, wresult, len) != 0) {
350 goto finish;
351 }
352
353 if (wcstombs_s(&conv, out, (size_t) len, wresult, (size_t) (len - 1)) != 0) {
354 goto finish;
355 }
356
357 // Change return value to success.
358 ret = 0;
359
360 finish:
361 free(wresult);
362 free(wpath);
363
364 return ret;
365 }
366
367 /* The appropriate location of getPrefixed() is io_util_md.c, but it is
368 also used in a non-OpenJDK context within Oracle. There, canonicalize_md.c
369 is already pulled in and compiled, so to avoid more complicated solutions
370 we keep this method here.
371 */
372
373 /* copy \\?\ or \\?\UNC\ to the front of path */
|
1 /*
2 * Copyright (c) 1998, 2020, 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
313 }
314 } else {
315 return -1;
316 }
317 }
318
319 if (dst >= dend) {
320 errno = ENAMETOOLONG;
321 return -1;
322 }
323 *dst = L'\0';
324 return 0;
325 }
326
327 /* Non-Wide character version of canonicalize.
328 Converts to wchar and delegates to wcanonicalize. */
329 JNIEXPORT int
330 JDK_Canonicalize(const char *orig, char *out, int len) {
331 wchar_t* wpath = NULL;
332 wchar_t* wresult = NULL;
333 int wpath_len;
334 int ret = -1;
335
336 /* Get required buffer size to convert to Unicode */
337 wpath_len = MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS,
338 orig, -1, NULL, 0);
339 if (wpath_len == 0) {
340 goto finish;
341 }
342
343 if ((wpath = (wchar_t*) malloc(sizeof(wchar_t) * wpath_len)) == NULL) {
344 goto finish;
345 }
346
347 if (MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS,
348 orig, -1, wpath, wpath_len) == 0) {
349 goto finish;
350 }
351
352 if ((wresult = (wchar_t*) malloc(sizeof(wchar_t) * len)) == NULL) {
353 goto finish;
354 }
355
356 if (wcanonicalize(wpath, wresult, len) != 0) {
357 goto finish;
358 }
359
360 if (WideCharToMultiByte(CP_THREAD_ACP, 0,
361 wresult, -1, out, len, NULL, NULL) == 0) {
362 goto finish;
363 }
364
365 // Change return value to success.
366 ret = 0;
367
368 finish:
369 free(wresult);
370 free(wpath);
371
372 return ret;
373 }
374
375 /* The appropriate location of getPrefixed() is io_util_md.c, but it is
376 also used in a non-OpenJDK context within Oracle. There, canonicalize_md.c
377 is already pulled in and compiled, so to avoid more complicated solutions
378 we keep this method here.
379 */
380
381 /* copy \\?\ or \\?\UNC\ to the front of path */
|