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 */ | 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 */ |