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