We should return 'true' even if resulting path is shorter than MAX_PATH, because we can also use this function to open files with non-standard characters, even if their path length is normal.
| 906 | // because we can also use this function to open files with non-standard |
| 907 | // characters, even if their path length is normal. |
| 908 | bool GetWinLongPath(const wchar *Src,wchar *Dest,size_t MaxSize) |
| 909 | { |
| 910 | if (*Src==0) |
| 911 | return false; |
| 912 | const wchar *Prefix=L"\\\\?\\"; |
| 913 | const size_t PrefixLength=4; |
| 914 | bool FullPath=IsDriveLetter(Src) && IsPathDiv(Src[2]); |
| 915 | size_t SrcLength=wcslen(Src); |
| 916 | if (IsFullPath(Src)) // Paths in d:\path\name format. |
| 917 | { |
| 918 | if (IsDriveLetter(Src)) |
| 919 | { |
| 920 | if (MaxSize<=PrefixLength+SrcLength) |
| 921 | return false; |
| 922 | wcsncpyz(Dest,Prefix,MaxSize); |
| 923 | wcsncatz(Dest,Src,MaxSize); // "\\?\D:\very long path". |
| 924 | return true; |
| 925 | } |
| 926 | else |
| 927 | if (Src[0]=='\\' && Src[1]=='\\') |
| 928 | { |
| 929 | if (MaxSize<=PrefixLength+SrcLength+2) |
| 930 | return false; |
| 931 | wcsncpyz(Dest,Prefix,MaxSize); |
| 932 | wcsncatz(Dest,L"UNC",MaxSize); |
| 933 | wcsncatz(Dest,Src+1,MaxSize); // "\\?\UNC\server\share". |
| 934 | return true; |
| 935 | } |
| 936 | // We may be here only if we modify IsFullPath in the future. |
| 937 | return false; |
| 938 | } |
| 939 | else |
| 940 | { |
| 941 | wchar CurDir[NM]; |
| 942 | DWORD DirCode=GetCurrentDirectory(ASIZE(CurDir)-1,CurDir); |
| 943 | if (DirCode==0 || DirCode>ASIZE(CurDir)-1) |
| 944 | return false; |
| 945 | |
| 946 | if (IsPathDiv(Src[0])) // Paths in \path\name format. |
| 947 | { |
| 948 | if (MaxSize<=PrefixLength+SrcLength+2) |
| 949 | return false; |
| 950 | wcsncpyz(Dest,Prefix,MaxSize); |
| 951 | CurDir[2]=0; |
| 952 | wcsncatz(Dest,CurDir,MaxSize); // Copy drive letter 'd:'. |
| 953 | wcsncatz(Dest,Src,MaxSize); |
| 954 | return true; |
| 955 | } |
| 956 | else // Paths in path\name format. |
| 957 | { |
| 958 | AddEndSlash(CurDir,ASIZE(CurDir)); |
| 959 | if (MaxSize<=PrefixLength+wcslen(CurDir)+SrcLength) |
| 960 | return false; |
| 961 | wcsncpyz(Dest,Prefix,MaxSize); |
| 962 | wcsncatz(Dest,CurDir,MaxSize); |
| 963 | |
| 964 | if (Src[0]=='.' && IsPathDiv(Src[1])) // Remove leading .\ in pathname. |
| 965 | Src+=2; |
no test coverage detected