]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiSortLib/UefiSortLib.c
MdeModulePkg/PartitionDxe: Fixed El Torito support when the medium is not a CDROM
[mirror_edk2.git] / ShellPkg / Library / UefiSortLib / UefiSortLib.c
CommitLineData
a4ee2a4f 1/** @file\r
2 Library used for sorting routines.\r
3\r
9586a351 4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved. <BR>\r
1e6e84c7 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
a4ee2a4f 9\r
1e6e84c7 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
a4ee2a4f 12\r
13**/\r
14\r
15#include <Uefi.h>\r
eca218a7 16#include <ShellBase.h>\r
a4ee2a4f 17\r
18#include <Protocol/UnicodeCollation.h>\r
19#include <Protocol/DevicePath.h>\r
a4ee2a4f 20\r
21#include <Library/UefiBootServicesTableLib.h>\r
22#include <Library/BaseLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/MemoryAllocationLib.h>\r
1e6e84c7 26#include <Library/SortLib.h>\r
863986b3 27#include <Library/DevicePathLib.h>\r
a4ee2a4f 28\r
11d2decf 29STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollation = NULL;\r
30\r
31\r
a4ee2a4f 32/**\r
1e6e84c7 33 Worker function for QuickSorting. This function is identical to PerformQuickSort,\r
34 except that is uses the pre-allocated buffer so the in place sorting does not need to\r
a4ee2a4f 35 allocate and free buffers constantly.\r
36\r
37 Each element must be equal sized.\r
38\r
39 if BufferToSort is NULL, then ASSERT.\r
40 if CompareFunction is NULL, then ASSERT.\r
41 if Buffer is NULL, then ASSERT.\r
42\r
43 if Count is < 2 then perform no action.\r
44 if Size is < 1 then perform no action.\r
45\r
4ff7e37b
ED
46 @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements\r
47 on return a buffer of sorted elements\r
48 @param[in] Count the number of elements in the buffer to sort\r
49 @param[in] ElementSize Size of an element in bytes\r
50 @param[in] CompareFunction The function to call to perform the comparison\r
51 of any 2 elements\r
52 @param[in] Buffer Buffer of size ElementSize for use in swapping\r
a4ee2a4f 53**/\r
54VOID\r
55EFIAPI\r
56QuickSortWorker (\r
57 IN OUT VOID *BufferToSort,\r
58 IN CONST UINTN Count,\r
59 IN CONST UINTN ElementSize,\r
60 IN SORT_COMPARE CompareFunction,\r
61 IN VOID *Buffer\r
125c2cf4 62 )\r
63{\r
a4ee2a4f 64 VOID *Pivot;\r
65 UINTN LoopCount;\r
66 UINTN NextSwapLocation;\r
67\r
68 ASSERT(BufferToSort != NULL);\r
69 ASSERT(CompareFunction != NULL);\r
70 ASSERT(Buffer != NULL);\r
71\r
1e6e84c7 72 if ( Count < 2\r
a4ee2a4f 73 || ElementSize < 1\r
a405b86d 74 ){\r
a4ee2a4f 75 return;\r
76 }\r
77\r
78 NextSwapLocation = 0;\r
79\r
80 //\r
81 // pick a pivot (we choose last element)\r
82 //\r
83 Pivot = ((UINT8*)BufferToSort+((Count-1)*ElementSize));\r
84\r
85 //\r
86 // Now get the pivot such that all on "left" are below it\r
87 // and everything "right" are above it\r
88 //\r
89 for ( LoopCount = 0\r
1e6e84c7 90 ; LoopCount < Count -1\r
a4ee2a4f 91 ; LoopCount++\r
a405b86d 92 ){\r
a4ee2a4f 93 //\r
94 // if the element is less than the pivot\r
95 //\r
96 if (CompareFunction((VOID*)((UINT8*)BufferToSort+((LoopCount)*ElementSize)),Pivot) <= 0){\r
97 //\r
1e6e84c7 98 // swap\r
a4ee2a4f 99 //\r
100 CopyMem (Buffer, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r
101 CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), (UINT8*)BufferToSort+((LoopCount)*ElementSize), ElementSize);\r
102 CopyMem ((UINT8*)BufferToSort+((LoopCount)*ElementSize), Buffer, ElementSize);\r
103\r
104 //\r
105 // increment NextSwapLocation\r
1e6e84c7 106 //\r
a4ee2a4f 107 NextSwapLocation++;\r
108 }\r
109 }\r
110 //\r
111 // swap pivot to it's final position (NextSwapLocaiton)\r
112 //\r
113 CopyMem (Buffer, Pivot, ElementSize);\r
114 CopyMem (Pivot, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r
115 CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), Buffer, ElementSize);\r
116\r
117 //\r
1e6e84c7 118 // Now recurse on 2 paritial lists. neither of these will have the 'pivot' element\r
a4ee2a4f 119 // IE list is sorted left half, pivot element, sorted right half...\r
120 //\r
5dcb5355 121 if (NextSwapLocation >= 2) {\r
122 QuickSortWorker(\r
123 BufferToSort,\r
124 NextSwapLocation,\r
125 ElementSize,\r
126 CompareFunction,\r
127 Buffer);\r
128 }\r
129\r
130 if ((Count - NextSwapLocation - 1) >= 2) {\r
131 QuickSortWorker(\r
132 (UINT8 *)BufferToSort + (NextSwapLocation+1) * ElementSize,\r
133 Count - NextSwapLocation - 1,\r
134 ElementSize,\r
135 CompareFunction,\r
136 Buffer);\r
137 }\r
a4ee2a4f 138\r
139 return;\r
140}\r
141/**\r
142 Function to perform a Quick Sort alogrithm on a buffer of comparable elements.\r
143\r
144 Each element must be equal sized.\r
145\r
146 if BufferToSort is NULL, then ASSERT.\r
147 if CompareFunction is NULL, then ASSERT.\r
148\r
149 if Count is < 2 then perform no action.\r
150 if Size is < 1 then perform no action.\r
151\r
4ff7e37b
ED
152 @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements\r
153 on return a buffer of sorted elements\r
154 @param[in] Count the number of elements in the buffer to sort\r
155 @param[in] ElementSize Size of an element in bytes\r
156 @param[in] CompareFunction The function to call to perform the comparison\r
157 of any 2 elements\r
a4ee2a4f 158**/\r
159VOID\r
160EFIAPI\r
161PerformQuickSort (\r
162 IN OUT VOID *BufferToSort,\r
163 IN CONST UINTN Count,\r
164 IN CONST UINTN ElementSize,\r
165 IN SORT_COMPARE CompareFunction\r
125c2cf4 166 )\r
167{\r
a4ee2a4f 168 VOID *Buffer;\r
169\r
170 ASSERT(BufferToSort != NULL);\r
171 ASSERT(CompareFunction != NULL);\r
172\r
9586a351 173 Buffer = AllocateZeroPool(ElementSize);\r
a4ee2a4f 174 ASSERT(Buffer != NULL);\r
175\r
176 QuickSortWorker(\r
177 BufferToSort,\r
178 Count,\r
179 ElementSize,\r
180 CompareFunction,\r
181 Buffer);\r
182\r
183 FreePool(Buffer);\r
184 return;\r
185}\r
186\r
187/**\r
da70d025 188 Function to compare 2 device paths for use in QuickSort.\r
a4ee2a4f 189\r
190 @param[in] Buffer1 pointer to Device Path poiner to compare\r
191 @param[in] Buffer2 pointer to second DevicePath pointer to compare\r
192\r
193 @retval 0 Buffer1 equal to Buffer2\r
194 @return < 0 Buffer1 is less than Buffer2\r
1e6e84c7 195 @return > 0 Buffer1 is greater than Buffer2\r
a4ee2a4f 196**/\r
197INTN\r
e26d7b59 198EFIAPI\r
a4ee2a4f 199DevicePathCompare (\r
b3011f40 200 IN CONST VOID *Buffer1,\r
201 IN CONST VOID *Buffer2\r
125c2cf4 202 )\r
203{\r
a4ee2a4f 204 EFI_DEVICE_PATH_PROTOCOL *DevicePath1;\r
205 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r
206 CHAR16 *TextPath1;\r
207 CHAR16 *TextPath2;\r
208 EFI_STATUS Status;\r
209 INTN RetVal;\r
1e6e84c7 210\r
a4ee2a4f 211 DevicePath1 = *(EFI_DEVICE_PATH_PROTOCOL**)Buffer1;\r
212 DevicePath2 = *(EFI_DEVICE_PATH_PROTOCOL**)Buffer2;\r
213\r
214 if (DevicePath1 == NULL) {\r
215 if (DevicePath2 == NULL) {\r
216 return 0;\r
217 }\r
218\r
219 return -1;\r
220 }\r
221\r
222 if (DevicePath2 == NULL) {\r
223 return 1;\r
224 }\r
1e6e84c7 225\r
11d2decf 226 if (mUnicodeCollation == NULL) {\r
a4ee2a4f 227 Status = gBS->LocateProtocol(\r
228 &gEfiUnicodeCollation2ProtocolGuid,\r
229 NULL,\r
11d2decf 230 (VOID**)&mUnicodeCollation);\r
a4ee2a4f 231\r
232 ASSERT_EFI_ERROR(Status);\r
233 }\r
234\r
863986b3 235 TextPath1 = ConvertDevicePathToText(\r
a4ee2a4f 236 DevicePath1,\r
237 FALSE,\r
238 FALSE);\r
239\r
863986b3 240 TextPath2 = ConvertDevicePathToText(\r
a4ee2a4f 241 DevicePath2,\r
242 FALSE,\r
243 FALSE);\r
1e6e84c7 244\r
eca218a7 245 if (TextPath1 == NULL) {\r
246 RetVal = -1;\r
247 } else if (TextPath2 == NULL) {\r
248 RetVal = 1;\r
249 } else {\r
250 RetVal = mUnicodeCollation->StriColl(\r
251 mUnicodeCollation,\r
252 TextPath1,\r
253 TextPath2);\r
254 }\r
a4ee2a4f 255\r
eca218a7 256 SHELL_FREE_NON_NULL(TextPath1);\r
257 SHELL_FREE_NON_NULL(TextPath2);\r
a4ee2a4f 258\r
259 return (RetVal);\r
11d2decf 260}\r
261\r
262/**\r
263 Function to compare 2 strings without regard to case of the characters.\r
264\r
265 @param[in] Buffer1 Pointer to String to compare.\r
266 @param[in] Buffer2 Pointer to second String to compare.\r
267\r
268 @retval 0 Buffer1 equal to Buffer2.\r
269 @return < 0 Buffer1 is less than Buffer2.\r
1e6e84c7 270 @return > 0 Buffer1 is greater than Buffer2.\r
11d2decf 271**/\r
272INTN\r
273EFIAPI\r
274StringNoCaseCompare (\r
b3011f40 275 IN CONST VOID *Buffer1,\r
276 IN CONST VOID *Buffer2\r
11d2decf 277 )\r
278{\r
279 EFI_STATUS Status;\r
280 if (mUnicodeCollation == NULL) {\r
281 Status = gBS->LocateProtocol(\r
282 &gEfiUnicodeCollation2ProtocolGuid,\r
283 NULL,\r
284 (VOID**)&mUnicodeCollation);\r
285\r
286 ASSERT_EFI_ERROR(Status);\r
287 }\r
288\r
289 return (mUnicodeCollation->StriColl(\r
290 mUnicodeCollation,\r
291 *(CHAR16**)Buffer1,\r
292 *(CHAR16**)Buffer2));\r
293}\r
294\r
295\r
a405b86d 296/**\r
297 Function to compare 2 strings.\r
298\r
299 @param[in] Buffer1 Pointer to String to compare (CHAR16**).\r
300 @param[in] Buffer2 Pointer to second String to compare (CHAR16**).\r
301\r
302 @retval 0 Buffer1 equal to Buffer2.\r
303 @return < 0 Buffer1 is less than Buffer2.\r
304 @return > 0 Buffer1 is greater than Buffer2.\r
305**/\r
306INTN\r
307EFIAPI\r
308StringCompare (\r
309 IN CONST VOID *Buffer1,\r
310 IN CONST VOID *Buffer2\r
311 )\r
312{\r
313 return (StrCmp(\r
314 *(CHAR16**)Buffer1,\r
315 *(CHAR16**)Buffer2));\r
316}\r