< prev index next >

src/java.base/windows/native/libjava/canonicalize_md.c

Print this page




 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     MultiByteToWideChar(CP_THREAD_ACP, MB_ERR_INVALID_CHARS,
 348                         orig, -1, wpath, wpath_len);
 349 
 350     if ((wresult = (wchar_t*) malloc(sizeof(wchar_t) * len)) == NULL) {
 351         goto finish;
 352     }
 353 
 354     if (wcanonicalize(wpath, wresult, len) != 0) {
 355         goto finish;
 356     }
 357 
 358     if (WideCharToMultiByte(CP_THREAD_ACP, 0,
 359                             wresult, -1, out, len, NULL, NULL) == 0) {
 360         goto finish;
 361     }
 362 
 363     // Change return value to success.
 364     ret = 0;
 365 
 366 finish:
 367     free(wresult);
 368     free(wpath);
 369 
 370     return ret;
 371 }
 372 
 373 /* The appropriate location of getPrefixed() is io_util_md.c, but it is
 374    also used in a non-OpenJDK context within Oracle. There, canonicalize_md.c
 375    is already pulled in and compiled, so to avoid more complicated solutions
 376    we keep this method here.
 377  */
 378 
 379 /* copy \\?\ or \\?\UNC\ to the front of path */
< prev index next >