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