]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/UdfDxe/FileName.c
MdeModulePkg/Udf: Refine function description comments
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / UdfDxe / FileName.c
CommitLineData
99c9b949
PA
1/** @file\r
2 Helper functions for mangling file names in UDF/ECMA-167 file systems.\r
3\r
4 Copyright (C) 2014-2017 Paulo Alcantara <pcacjr@zytor.com>\r
5\r
6 This program and the accompanying materials are licensed and made available\r
7 under the terms and conditions of the BSD License which accompanies this\r
8 distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
12 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13**/\r
14\r
15#include "Udf.h"\r
16\r
077f8c43
HW
17/**\r
18 Trim the leading and trailing spaces for a give Unicode string.\r
19\r
20 @param[in] String The Unicode string to trim.\r
21\r
22 @return A pointer to the trimmed string.\r
23\r
24**/\r
99c9b949
PA
25CHAR16 *\r
26TrimString (\r
27 IN CHAR16 *String\r
28 )\r
29{\r
30 CHAR16 *TempString;\r
31\r
32 for ( ; *String != L'\0' && *String == L' '; String++) {\r
33 ;\r
34 }\r
35\r
36 TempString = String + StrLen (String) - 1;\r
37 while ((TempString >= String) && (*TempString == L' ')) {\r
38 TempString--;\r
39 }\r
40\r
41 *(TempString + 1) = L'\0';\r
42\r
43 return String;\r
44}\r
45\r
077f8c43
HW
46/**\r
47 Replace the content of a Unicode string with the content of another Unicode\r
48 string.\r
49\r
50 @param[in] Destination A pointer to a Unicode string.\r
51 @param[in] Source A pointer to a Unicode string.\r
52\r
53**/\r
99c9b949
PA
54VOID\r
55ReplaceLeft (\r
56 IN CHAR16 *Destination,\r
57 IN CONST CHAR16 *Source\r
58 )\r
59{\r
60 CONST CHAR16 *EndString;\r
61\r
62 EndString = Source + StrLen (Source);\r
63 while (Source <= EndString) {\r
64 *Destination++ = *Source++;\r
65 }\r
66}\r
67\r
077f8c43
HW
68/**\r
69 Remove one or more consecutive backslashes starting from the second character\r
70 of a given Unicode string.\r
71\r
72 @param[in] String A pointer to a Unicode string.\r
73\r
74 @return A pointer to the modified string.\r
75\r
76**/\r
99c9b949
PA
77CHAR16 *\r
78ExcludeTrailingBackslashes (\r
79 IN CHAR16 *String\r
80 )\r
81{\r
82 CHAR16 *TempString;\r
83\r
84 switch (*(String + 1)) {\r
85 case L'\\':\r
86 break;\r
87 case L'\0':\r
88 default:\r
89 String++;\r
90 goto Exit;\r
91 }\r
92\r
93 TempString = String;\r
94 while (*TempString != L'\0' && *TempString == L'\\') {\r
95 TempString++;\r
96 }\r
97\r
98 if (TempString - 1 > String) {\r
99 ReplaceLeft (String + 1, TempString);\r
100 }\r
101\r
102 String++;\r
103\r
104Exit:\r
105 return String;\r
106}\r
107\r
108/**\r
109 Mangle a filename by cutting off trailing whitespaces, "\\", "." and "..".\r
110\r
111 @param[in] FileName Filename.\r
112\r
077f8c43 113 @retval The mangled Filename.\r
99c9b949
PA
114\r
115**/\r
116CHAR16 *\r
117MangleFileName (\r
118 IN CHAR16 *FileName\r
119 )\r
120{\r
121 CHAR16 *FileNameSavedPointer;\r
122 CHAR16 *TempFileName;\r
123 UINTN BackslashesNo;\r
124\r
125 if (FileName == NULL || *FileName == L'\0') {\r
126 FileName = NULL;\r
127 goto Exit;\r
128 }\r
129\r
130 FileName = TrimString (FileName);\r
131 if (*FileName == L'\0') {\r
132 goto Exit;\r
133 }\r
134\r
135 if ((StrLen (FileName) > 1) && (FileName[StrLen (FileName) - 1] == L'\\')) {\r
136 FileName[StrLen (FileName) - 1] = L'\0';\r
137 }\r
138\r
139 FileNameSavedPointer = FileName;\r
140\r
141 if (FileName[0] == L'.') {\r
142 if (FileName[1] == L'.') {\r
143 if (FileName[2] == L'\0') {\r
144 goto Exit;\r
145 } else {\r
146 FileName += 2;\r
147 }\r
148 } else if (FileName[1] == L'\0') {\r
149 goto Exit;\r
150 }\r
151 }\r
152\r
153 while (*FileName != L'\0') {\r
154 if (*FileName == L'\\') {\r
155 FileName = ExcludeTrailingBackslashes (FileName);\r
156 } else if (*FileName == L'.') {\r
157 switch (*(FileName + 1)) {\r
158 case L'\0':\r
159 *FileName = L'\0';\r
160 break;\r
161 case L'\\':\r
162 TempFileName = FileName + 1;\r
163 TempFileName = ExcludeTrailingBackslashes (TempFileName);\r
164 ReplaceLeft (FileName, TempFileName);\r
165 break;\r
166 case '.':\r
167 if ((*(FileName - 1) != L'\\') && ((*(FileName + 2) != L'\\') ||\r
168 (*(FileName + 2) != L'\0'))) {\r
169 FileName++;\r
170 continue;\r
171 }\r
172\r
173 BackslashesNo = 0;\r
174 TempFileName = FileName - 1;\r
175 while (TempFileName >= FileNameSavedPointer) {\r
176 if (*TempFileName == L'\\') {\r
177 if (++BackslashesNo == 2) {\r
178 break;\r
179 }\r
180 }\r
181\r
182 TempFileName--;\r
183 }\r
184\r
185 TempFileName++;\r
186\r
187 if ((*TempFileName == L'.') && (*(TempFileName + 1) == L'.')) {\r
188 FileName += 2;\r
189 } else {\r
190 if (*(FileName + 2) != L'\0') {\r
191 ReplaceLeft (TempFileName, FileName + 3);\r
192 if (*(TempFileName - 1) == L'\\') {\r
193 FileName = TempFileName;\r
194 ExcludeTrailingBackslashes (TempFileName - 1);\r
195 TempFileName = FileName;\r
196 }\r
197 } else {\r
198 *TempFileName = L'\0';\r
199 }\r
200\r
201 FileName = TempFileName;\r
202 }\r
203\r
204 break;\r
205 default:\r
206 FileName++;\r
207 }\r
208 } else {\r
209 FileName++;\r
210 }\r
211 }\r
212\r
213 FileName = FileNameSavedPointer;\r
214 if ((StrLen (FileName) > 1) && (FileName [StrLen (FileName) - 1] == L'\\')) {\r
215 FileName [StrLen (FileName) - 1] = L'\0';\r
216 }\r
217\r
218Exit:\r
219 return FileName;\r
220}\r