]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/FilePaths.c
MdePkg-BaseLib: Fix PathCleanUpDirectories() error involving "\..\.."
[mirror_edk2.git] / MdePkg / Library / BaseLib / FilePaths.c
1 /** @file
2 Defines file-path manipulation functions.
3
4 Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2018, Dell Technologies. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/BaseLib.h>
16
17 /**
18 Removes the last directory or file entry in a path. For a path which is
19 like L"fs0:startup.nsh", it's converted to L"fs0:".
20
21 @param[in,out] Path A pointer to the path to modify.
22
23 @retval FALSE Nothing was found to remove.
24 @retval TRUE A directory or file was removed.
25 **/
26 BOOLEAN
27 EFIAPI
28 PathRemoveLastItem(
29 IN OUT CHAR16 *Path
30 )
31 {
32 CHAR16 *Walker;
33 CHAR16 *LastSlash;
34 //
35 // get directory name from path... ('chop' off extra)
36 //
37 for ( Walker = Path, LastSlash = NULL
38 ; Walker != NULL && *Walker != CHAR_NULL
39 ; Walker++
40 ){
41 if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {
42 LastSlash = Walker+1;
43 } else if (*Walker == L':' && *(Walker + 1) != L'\\' && *(Walker + 1) != CHAR_NULL) {
44 LastSlash = Walker+1;
45 }
46 }
47 if (LastSlash != NULL) {
48 *LastSlash = CHAR_NULL;
49 return (TRUE);
50 }
51 return (FALSE);
52 }
53
54 /**
55 Function to clean up paths.
56
57 - Single periods in the path are removed.
58 - Double periods in the path are removed along with a single parent directory.
59 - Forward slashes L'/' are converted to backward slashes L'\'.
60
61 This will be done inline and the existing buffer may be larger than required
62 upon completion.
63
64 @param[in] Path The pointer to the string containing the path.
65
66 @return Returns Path, otherwise returns NULL to indicate that an error has occured.
67 **/
68 CHAR16*
69 EFIAPI
70 PathCleanUpDirectories(
71 IN CHAR16 *Path
72 )
73 {
74 CHAR16 *TempString;
75
76 if (Path == NULL) {
77 return NULL;
78 }
79
80 //
81 // Replace the '/' with '\'
82 //
83 for (TempString = Path; *TempString != CHAR_NULL; TempString++) {
84 if (*TempString == L'/') {
85 *TempString = L'\\';
86 }
87 }
88
89 //
90 // Remove all the "\.". E.g.: fs0:\abc\.\def\.
91 //
92 while ((TempString = StrStr (Path, L"\\.\\")) != NULL) {
93 CopyMem (TempString, TempString + 2, StrSize (TempString + 2));
94 }
95 if ((StrLen (Path) >= 2) && (StrCmp (Path + StrLen (Path) - 2, L"\\.") == 0)) {
96 Path[StrLen (Path) - 1] = CHAR_NULL;
97 }
98
99 //
100 // Remove all the "\..". E.g.: fs0:\abc\..\def\..
101 //
102 while (((TempString = StrStr(Path, L"\\..")) != NULL) &&
103 ((*(TempString + 3) == L'\\') || (*(TempString + 3) == CHAR_NULL))
104 ) {
105 *(TempString + 1) = CHAR_NULL;
106 PathRemoveLastItem(Path);
107 if (*(TempString + 3) != CHAR_NULL) {
108 CopyMem (Path + StrLen (Path), TempString + 4, StrSize (TempString + 4));
109 }
110 }
111
112 //
113 // Replace the "\\" with "\"
114 //
115 while ((TempString = StrStr (Path, L"\\\\")) != NULL) {
116 CopyMem (TempString, TempString + 1, StrSize (TempString + 1));
117 }
118
119 return Path;
120 }
121