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