]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Library used for sorting routines.\r | |
3 | \r | |
4 | Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved. <BR>\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | \r | |
7 | **/\r | |
8 | \r | |
9 | #include <Uefi.h>\r | |
10 | \r | |
11 | #include <Protocol/UnicodeCollation.h>\r | |
12 | #include <Protocol/DevicePath.h>\r | |
13 | \r | |
14 | #include <Library/UefiBootServicesTableLib.h>\r | |
15 | #include <Library/BaseLib.h>\r | |
16 | #include <Library/BaseMemoryLib.h>\r | |
17 | #include <Library/DebugLib.h>\r | |
18 | #include <Library/MemoryAllocationLib.h>\r | |
19 | #include <Library/SortLib.h>\r | |
20 | #include <Library/DevicePathLib.h>\r | |
21 | \r | |
22 | STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollation = NULL;\r | |
23 | \r | |
24 | #define USL_FREE_NON_NULL(Pointer) \\r | |
25 | { \\r | |
26 | if ((Pointer) != NULL) { \\r | |
27 | FreePool((Pointer)); \\r | |
28 | (Pointer) = NULL; \\r | |
29 | } \\r | |
30 | }\r | |
31 | \r | |
32 | /**\r | |
33 | Function to perform a Quick Sort alogrithm on a buffer of comparable elements.\r | |
34 | \r | |
35 | Each element must be equal sized.\r | |
36 | \r | |
37 | if BufferToSort is NULL, then ASSERT.\r | |
38 | if CompareFunction is NULL, then ASSERT.\r | |
39 | \r | |
40 | if Count is < 2 then perform no action.\r | |
41 | if Size is < 1 then perform no action.\r | |
42 | \r | |
43 | @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements\r | |
44 | on return a buffer of sorted elements\r | |
45 | @param[in] Count the number of elements in the buffer to sort\r | |
46 | @param[in] ElementSize Size of an element in bytes\r | |
47 | @param[in] CompareFunction The function to call to perform the comparison\r | |
48 | of any 2 elements\r | |
49 | **/\r | |
50 | VOID\r | |
51 | EFIAPI\r | |
52 | PerformQuickSort (\r | |
53 | IN OUT VOID *BufferToSort,\r | |
54 | IN CONST UINTN Count,\r | |
55 | IN CONST UINTN ElementSize,\r | |
56 | IN SORT_COMPARE CompareFunction\r | |
57 | )\r | |
58 | {\r | |
59 | VOID *Buffer;\r | |
60 | \r | |
61 | ASSERT (BufferToSort != NULL);\r | |
62 | ASSERT (CompareFunction != NULL);\r | |
63 | \r | |
64 | Buffer = AllocateZeroPool (ElementSize);\r | |
65 | ASSERT (Buffer != NULL);\r | |
66 | \r | |
67 | QuickSort (\r | |
68 | BufferToSort,\r | |
69 | Count,\r | |
70 | ElementSize,\r | |
71 | CompareFunction,\r | |
72 | Buffer\r | |
73 | );\r | |
74 | \r | |
75 | FreePool (Buffer);\r | |
76 | return;\r | |
77 | }\r | |
78 | \r | |
79 | /**\r | |
80 | Function to compare 2 device paths for use in QuickSort.\r | |
81 | \r | |
82 | @param[in] Buffer1 pointer to Device Path poiner to compare\r | |
83 | @param[in] Buffer2 pointer to second DevicePath pointer to compare\r | |
84 | \r | |
85 | @retval 0 Buffer1 equal to Buffer2\r | |
86 | @retval <0 Buffer1 is less than Buffer2\r | |
87 | @retval >0 Buffer1 is greater than Buffer2\r | |
88 | **/\r | |
89 | INTN\r | |
90 | EFIAPI\r | |
91 | DevicePathCompare (\r | |
92 | IN CONST VOID *Buffer1,\r | |
93 | IN CONST VOID *Buffer2\r | |
94 | )\r | |
95 | {\r | |
96 | EFI_DEVICE_PATH_PROTOCOL *DevicePath1;\r | |
97 | EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r | |
98 | CHAR16 *TextPath1;\r | |
99 | CHAR16 *TextPath2;\r | |
100 | EFI_STATUS Status;\r | |
101 | INTN RetVal;\r | |
102 | \r | |
103 | DevicePath1 = *(EFI_DEVICE_PATH_PROTOCOL **)Buffer1;\r | |
104 | DevicePath2 = *(EFI_DEVICE_PATH_PROTOCOL **)Buffer2;\r | |
105 | \r | |
106 | if (DevicePath1 == NULL) {\r | |
107 | if (DevicePath2 == NULL) {\r | |
108 | return 0;\r | |
109 | }\r | |
110 | \r | |
111 | return -1;\r | |
112 | }\r | |
113 | \r | |
114 | if (DevicePath2 == NULL) {\r | |
115 | return 1;\r | |
116 | }\r | |
117 | \r | |
118 | if (mUnicodeCollation == NULL) {\r | |
119 | Status = gBS->LocateProtocol (\r | |
120 | &gEfiUnicodeCollation2ProtocolGuid,\r | |
121 | NULL,\r | |
122 | (VOID **)&mUnicodeCollation\r | |
123 | );\r | |
124 | \r | |
125 | ASSERT_EFI_ERROR (Status);\r | |
126 | }\r | |
127 | \r | |
128 | TextPath1 = ConvertDevicePathToText (\r | |
129 | DevicePath1,\r | |
130 | FALSE,\r | |
131 | FALSE\r | |
132 | );\r | |
133 | \r | |
134 | TextPath2 = ConvertDevicePathToText (\r | |
135 | DevicePath2,\r | |
136 | FALSE,\r | |
137 | FALSE\r | |
138 | );\r | |
139 | \r | |
140 | if (TextPath1 == NULL) {\r | |
141 | RetVal = -1;\r | |
142 | } else if (TextPath2 == NULL) {\r | |
143 | RetVal = 1;\r | |
144 | } else {\r | |
145 | RetVal = mUnicodeCollation->StriColl (\r | |
146 | mUnicodeCollation,\r | |
147 | TextPath1,\r | |
148 | TextPath2\r | |
149 | );\r | |
150 | }\r | |
151 | \r | |
152 | USL_FREE_NON_NULL (TextPath1);\r | |
153 | USL_FREE_NON_NULL (TextPath2);\r | |
154 | \r | |
155 | return (RetVal);\r | |
156 | }\r | |
157 | \r | |
158 | /**\r | |
159 | Function to compare 2 strings without regard to case of the characters.\r | |
160 | \r | |
161 | @param[in] Buffer1 Pointer to String to compare.\r | |
162 | @param[in] Buffer2 Pointer to second String to compare.\r | |
163 | \r | |
164 | @retval 0 Buffer1 equal to Buffer2.\r | |
165 | @retval <0 Buffer1 is less than Buffer2.\r | |
166 | @retval >0 Buffer1 is greater than Buffer2.\r | |
167 | **/\r | |
168 | INTN\r | |
169 | EFIAPI\r | |
170 | StringNoCaseCompare (\r | |
171 | IN CONST VOID *Buffer1,\r | |
172 | IN CONST VOID *Buffer2\r | |
173 | )\r | |
174 | {\r | |
175 | EFI_STATUS Status;\r | |
176 | \r | |
177 | if (mUnicodeCollation == NULL) {\r | |
178 | Status = gBS->LocateProtocol (\r | |
179 | &gEfiUnicodeCollation2ProtocolGuid,\r | |
180 | NULL,\r | |
181 | (VOID **)&mUnicodeCollation\r | |
182 | );\r | |
183 | \r | |
184 | ASSERT_EFI_ERROR (Status);\r | |
185 | }\r | |
186 | \r | |
187 | return (mUnicodeCollation->StriColl (\r | |
188 | mUnicodeCollation,\r | |
189 | *(CHAR16 **)Buffer1,\r | |
190 | *(CHAR16 **)Buffer2\r | |
191 | ));\r | |
192 | }\r | |
193 | \r | |
194 | /**\r | |
195 | Function to compare 2 strings.\r | |
196 | \r | |
197 | @param[in] Buffer1 Pointer to String to compare (CHAR16**).\r | |
198 | @param[in] Buffer2 Pointer to second String to compare (CHAR16**).\r | |
199 | \r | |
200 | @retval 0 Buffer1 equal to Buffer2.\r | |
201 | @retval <0 Buffer1 is less than Buffer2.\r | |
202 | @retval >0 Buffer1 is greater than Buffer2.\r | |
203 | **/\r | |
204 | INTN\r | |
205 | EFIAPI\r | |
206 | StringCompare (\r | |
207 | IN CONST VOID *Buffer1,\r | |
208 | IN CONST VOID *Buffer2\r | |
209 | )\r | |
210 | {\r | |
211 | return (StrCmp (\r | |
212 | *(CHAR16 **)Buffer1,\r | |
213 | *(CHAR16 **)Buffer2\r | |
214 | ));\r | |
215 | }\r |