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