]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / RegularExpressionDxe / RegularExpressionDxe.c
1 /** @file
2
3 EFI_REGULAR_EXPRESSION_PROTOCOL Implementation
4
5 (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "RegularExpressionDxe.h"
12
13 STATIC
14 EFI_REGEX_SYNTAX_TYPE *CONST mSupportedSyntaxes[] = {
15 &gEfiRegexSyntaxTypePosixExtendedGuid,
16 &gEfiRegexSyntaxTypePerlGuid
17 };
18
19 STATIC
20 EFI_REGULAR_EXPRESSION_PROTOCOL mProtocolInstance = {
21 RegularExpressionMatch,
22 RegularExpressionGetInfo
23 };
24
25 #define CHAR16_ENCODING ONIG_ENCODING_UTF16_LE
26
27 /**
28 Call the Oniguruma regex match API.
29
30 Same parameters as RegularExpressionMatch, except SyntaxType is required.
31
32 @param String A pointer to a NULL terminated string to match against the
33 regular expression string specified by Pattern.
34
35 @param Pattern A pointer to a NULL terminated string that represents the
36 regular expression.
37 @param SyntaxType A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the
38 regular expression syntax type to use. May be NULL in which
39 case the function will use its default regular expression
40 syntax type.
41
42 @param Result On return, points to TRUE if String fully matches against
43 the regular expression Pattern using the regular expression
44 SyntaxType. Otherwise, points to FALSE.
45
46 @param Captures A Pointer to an array of EFI_REGEX_CAPTURE objects to receive
47 the captured groups in the event of a match. The full
48 sub-string match is put in Captures[0], and the results of N
49 capturing groups are put in Captures[1:N]. If Captures is
50 NULL, then this function doesn't allocate the memory for the
51 array and does not build up the elements. It only returns the
52 number of matching patterns in CapturesCount. If Captures is
53 not NULL, this function returns a pointer to an array and
54 builds up the elements in the array. CapturesCount is also
55 updated to the number of matching patterns found. It is the
56 caller's responsibility to free the memory pool in Captures
57 and in each CapturePtr in the array elements.
58
59 @param CapturesCount On output, CapturesCount is the number of matching patterns
60 found in String. Zero means no matching patterns were found
61 in the string.
62
63 @retval EFI_SUCCESS Regex compilation and match completed successfully.
64 @retval EFI_DEVICE_ERROR Regex compilation failed.
65
66 **/
67 STATIC
68 EFI_STATUS
69 OnigurumaMatch (
70 IN CHAR16 *String,
71 IN CHAR16 *Pattern,
72 IN EFI_REGEX_SYNTAX_TYPE *SyntaxType,
73 OUT BOOLEAN *Result,
74 OUT EFI_REGEX_CAPTURE **Captures OPTIONAL,
75 OUT UINTN *CapturesCount
76 )
77 {
78 regex_t *OnigRegex;
79 OnigSyntaxType *OnigSyntax;
80 OnigRegion *Region;
81 INT32 OnigResult;
82 OnigErrorInfo ErrorInfo;
83 OnigUChar ErrorMessage[ONIG_MAX_ERROR_MESSAGE_LEN];
84 UINT32 Index;
85 OnigUChar *Start;
86 EFI_STATUS Status;
87
88 Status = EFI_SUCCESS;
89
90 //
91 // Detemine the internal syntax type
92 //
93 OnigSyntax = ONIG_SYNTAX_DEFAULT;
94 if (CompareGuid (SyntaxType, &gEfiRegexSyntaxTypePosixExtendedGuid)) {
95 OnigSyntax = ONIG_SYNTAX_POSIX_EXTENDED;
96 } else if (CompareGuid (SyntaxType, &gEfiRegexSyntaxTypePerlGuid)) {
97 OnigSyntax = ONIG_SYNTAX_PERL;
98 } else {
99 DEBUG ((DEBUG_ERROR, "Unsupported regex syntax - using default\n"));
100 return EFI_UNSUPPORTED;
101 }
102
103 //
104 // Compile pattern
105 //
106 Start = (OnigUChar *)Pattern;
107 OnigResult = onig_new (
108 &OnigRegex,
109 Start,
110 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),
111 ONIG_OPTION_DEFAULT,
112 CHAR16_ENCODING,
113 OnigSyntax,
114 &ErrorInfo
115 );
116
117 if (OnigResult != ONIG_NORMAL) {
118 onig_error_code_to_str (ErrorMessage, OnigResult, &ErrorInfo);
119 DEBUG ((DEBUG_ERROR, "Regex compilation failed: %a\n", ErrorMessage));
120 return EFI_DEVICE_ERROR;
121 }
122
123 //
124 // Try to match
125 //
126 Start = (OnigUChar *)String;
127 Region = onig_region_new ();
128 if (Region == NULL) {
129 onig_free (OnigRegex);
130 return EFI_OUT_OF_RESOURCES;
131 }
132
133 OnigResult = onig_search (
134 OnigRegex,
135 Start,
136 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),
137 Start,
138 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),
139 Region,
140 ONIG_OPTION_NONE
141 );
142
143 if (OnigResult >= 0) {
144 *Result = TRUE;
145 } else {
146 *Result = FALSE;
147 if (OnigResult != ONIG_MISMATCH) {
148 onig_error_code_to_str (ErrorMessage, OnigResult);
149 DEBUG ((DEBUG_ERROR, "Regex match failed: %a\n", ErrorMessage));
150 onig_region_free (Region, 1);
151 onig_free (OnigRegex);
152 return EFI_DEVICE_ERROR;
153 }
154 }
155
156 //
157 // If successful, copy out the region (capture) information
158 //
159 if (*Result && (Captures != NULL)) {
160 *CapturesCount = Region->num_regs;
161 *Captures = AllocateZeroPool (*CapturesCount * sizeof (**Captures));
162 if (*Captures != NULL) {
163 for (Index = 0; Index < *CapturesCount; ++Index) {
164 //
165 // Region beg/end values represent bytes, not characters
166 //
167 (*Captures)[Index].Length = (Region->end[Index] - Region->beg[Index]) / sizeof (CHAR16);
168 (*Captures)[Index].CapturePtr = AllocateCopyPool (
169 ((*Captures)[Index].Length) * sizeof (CHAR16),
170 (CHAR16 *)((UINTN)String + Region->beg[Index])
171 );
172 if ((*Captures)[Index].CapturePtr == NULL) {
173 Status = EFI_OUT_OF_RESOURCES;
174 break;
175 }
176 }
177
178 if (EFI_ERROR (Status)) {
179 for (Index = 0; Index < *CapturesCount; ++Index) {
180 if ((*Captures)[Index].CapturePtr != NULL) {
181 FreePool ((CHAR16 *)(*Captures)[Index].CapturePtr);
182 }
183 }
184
185 FreePool (*Captures);
186 }
187 }
188 }
189
190 onig_region_free (Region, 1);
191 onig_free (OnigRegex);
192
193 return Status;
194 }
195
196 /**
197 Returns information about the regular expression syntax types supported
198 by the implementation.
199
200 @param This A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL
201 instance.
202
203 @param RegExSyntaxTypeListSize On input, the size in bytes of RegExSyntaxTypeList.
204 On output with a return code of EFI_SUCCESS, the
205 size in bytes of the data returned in
206 RegExSyntaxTypeList. On output with a return code
207 of EFI_BUFFER_TOO_SMALL, the size of
208 RegExSyntaxTypeList required to obtain the list.
209
210 @param RegExSyntaxTypeList A caller-allocated memory buffer filled by the
211 driver with one EFI_REGEX_SYNTAX_TYPE element
212 for each supported Regular expression syntax
213 type. The list must not change across multiple
214 calls to the same driver. The first syntax
215 type in the list is the default type for the
216 driver.
217
218 @retval EFI_SUCCESS The regular expression syntax types list
219 was returned successfully.
220 @retval EFI_UNSUPPORTED The service is not supported by this driver.
221 @retval EFI_DEVICE_ERROR The list of syntax types could not be
222 retrieved due to a hardware or firmware error.
223 @retval EFI_BUFFER_TOO_SMALL The buffer RegExSyntaxTypeList is too small
224 to hold the result.
225 @retval EFI_INVALID_PARAMETER RegExSyntaxTypeListSize is NULL
226
227 **/
228 EFI_STATUS
229 EFIAPI
230 RegularExpressionGetInfo (
231 IN EFI_REGULAR_EXPRESSION_PROTOCOL *This,
232 IN OUT UINTN *RegExSyntaxTypeListSize,
233 OUT EFI_REGEX_SYNTAX_TYPE *RegExSyntaxTypeList
234 )
235 {
236 UINTN SyntaxSize;
237 UINTN Index;
238
239 if ((This == NULL) || (RegExSyntaxTypeListSize == NULL)) {
240 return EFI_INVALID_PARAMETER;
241 }
242
243 if ((*RegExSyntaxTypeListSize != 0) && (RegExSyntaxTypeList == NULL)) {
244 return EFI_INVALID_PARAMETER;
245 }
246
247 SyntaxSize = ARRAY_SIZE (mSupportedSyntaxes) * sizeof (**mSupportedSyntaxes);
248
249 if (*RegExSyntaxTypeListSize < SyntaxSize) {
250 *RegExSyntaxTypeListSize = SyntaxSize;
251 return EFI_BUFFER_TOO_SMALL;
252 }
253
254 for (Index = 0; Index < ARRAY_SIZE (mSupportedSyntaxes); ++Index) {
255 CopyMem (&RegExSyntaxTypeList[Index], mSupportedSyntaxes[Index], sizeof (**mSupportedSyntaxes));
256 }
257
258 *RegExSyntaxTypeListSize = SyntaxSize;
259
260 return EFI_SUCCESS;
261 }
262
263 /**
264 Checks if the input string matches to the regular expression pattern.
265
266 @param This A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL instance.
267 Type EFI_REGULAR_EXPRESSION_PROTOCOL is defined in Section
268 XYZ.
269
270 @param String A pointer to a NULL terminated string to match against the
271 regular expression string specified by Pattern.
272
273 @param Pattern A pointer to a NULL terminated string that represents the
274 regular expression.
275
276 @param SyntaxType A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the
277 regular expression syntax type to use. May be NULL in which
278 case the function will use its default regular expression
279 syntax type.
280
281 @param Result On return, points to TRUE if String fully matches against
282 the regular expression Pattern using the regular expression
283 SyntaxType. Otherwise, points to FALSE.
284
285 @param Captures A Pointer to an array of EFI_REGEX_CAPTURE objects to receive
286 the captured groups in the event of a match. The full
287 sub-string match is put in Captures[0], and the results of N
288 capturing groups are put in Captures[1:N]. If Captures is
289 NULL, then this function doesn't allocate the memory for the
290 array and does not build up the elements. It only returns the
291 number of matching patterns in CapturesCount. If Captures is
292 not NULL, this function returns a pointer to an array and
293 builds up the elements in the array. CapturesCount is also
294 updated to the number of matching patterns found. It is the
295 caller's responsibility to free the memory pool in Captures
296 and in each CapturePtr in the array elements.
297
298 @param CapturesCount On output, CapturesCount is the number of matching patterns
299 found in String. Zero means no matching patterns were found
300 in the string.
301
302 @retval EFI_SUCCESS The regular expression string matching
303 completed successfully.
304 @retval EFI_UNSUPPORTED The regular expression syntax specified by
305 SyntaxType is not supported by this driver.
306 @retval EFI_DEVICE_ERROR The regular expression string matching
307 failed due to a hardware or firmware error.
308 @retval EFI_INVALID_PARAMETER String, Pattern, Result, or CapturesCountis
309 NULL.
310
311 **/
312 EFI_STATUS
313 EFIAPI
314 RegularExpressionMatch (
315 IN EFI_REGULAR_EXPRESSION_PROTOCOL *This,
316 IN CHAR16 *String,
317 IN CHAR16 *Pattern,
318 IN EFI_REGEX_SYNTAX_TYPE *SyntaxType OPTIONAL,
319 OUT BOOLEAN *Result,
320 OUT EFI_REGEX_CAPTURE **Captures OPTIONAL,
321 OUT UINTN *CapturesCount
322 )
323 {
324 EFI_STATUS Status;
325 UINT32 Index;
326 BOOLEAN Supported;
327
328 if ((This == NULL) || (String == NULL) || (Pattern == NULL) || (Result == NULL) || (CapturesCount == NULL)) {
329 return EFI_INVALID_PARAMETER;
330 }
331
332 //
333 // Figure out which syntax to use
334 //
335 if (SyntaxType == NULL) {
336 SyntaxType = mSupportedSyntaxes[0];
337 } else {
338 Supported = FALSE;
339 for (Index = 0; Index < ARRAY_SIZE (mSupportedSyntaxes); ++Index) {
340 if (CompareGuid (SyntaxType, mSupportedSyntaxes[Index])) {
341 Supported = TRUE;
342 break;
343 }
344 }
345
346 if (!Supported) {
347 return EFI_UNSUPPORTED;
348 }
349 }
350
351 Status = OnigurumaMatch (String, Pattern, SyntaxType, Result, Captures, CapturesCount);
352
353 return Status;
354 }
355
356 /**
357 Entry point for RegularExpressionDxe.
358
359 @param ImageHandle Image handle this driver.
360 @param SystemTable Pointer to SystemTable.
361
362 @retval Status Whether this function complete successfully.
363
364 **/
365 EFI_STATUS
366 EFIAPI
367 RegularExpressionDxeEntry (
368 IN EFI_HANDLE ImageHandle,
369 IN EFI_SYSTEM_TABLE *SystemTable
370 )
371 {
372 EFI_STATUS Status;
373
374 Status = gBS->InstallMultipleProtocolInterfaces (
375 &ImageHandle,
376 &gEfiRegularExpressionProtocolGuid,
377 &mProtocolInstance,
378 NULL
379 );
380
381 return Status;
382 }