MdeModulePkg: Delete useless case code
[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
04c30dd5 5 (C) Copyright 2015-2016 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
c8206f22 91 OnigUChar ErrorMessage[ONIG_MAX_ERROR_MESSAGE_LEN];\r
14b0e578
CS
92 UINT32 Index;\r
93 OnigUChar *Start;\r
04c30dd5
CS
94 EFI_STATUS Status;\r
95\r
96\r
97 Status = EFI_SUCCESS;\r
14b0e578
CS
98\r
99 //\r
100 // Detemine the internal syntax type\r
101 //\r
102 OnigSyntax = ONIG_SYNTAX_DEFAULT;\r
103 if (CompareGuid (SyntaxType, &gEfiRegexSyntaxTypePosixExtendedGuid)) {\r
104 OnigSyntax = ONIG_SYNTAX_POSIX_EXTENDED;\r
105 } else if (CompareGuid (SyntaxType, &gEfiRegexSyntaxTypePerlGuid)) {\r
106 OnigSyntax = ONIG_SYNTAX_PERL;\r
107 } else {\r
108 DEBUG ((DEBUG_ERROR, "Unsupported regex syntax - using default\n"));\r
04c30dd5 109 return EFI_UNSUPPORTED;\r
14b0e578
CS
110 }\r
111\r
112 //\r
113 // Compile pattern\r
114 //\r
115 Start = (OnigUChar*)Pattern;\r
116 OnigResult = onig_new (\r
117 &OnigRegex,\r
118 Start,\r
119 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),\r
120 ONIG_OPTION_DEFAULT,\r
121 CHAR16_ENCODING,\r
122 OnigSyntax,\r
123 &ErrorInfo\r
124 );\r
125\r
126 if (OnigResult != ONIG_NORMAL) {\r
127 onig_error_code_to_str (ErrorMessage, OnigResult, &ErrorInfo);\r
128 DEBUG ((DEBUG_ERROR, "Regex compilation failed: %a\n", ErrorMessage));\r
129 return EFI_DEVICE_ERROR;\r
130 }\r
131\r
132 //\r
133 // Try to match\r
134 //\r
135 Start = (OnigUChar*)String;\r
136 Region = onig_region_new ();\r
b0c2b797
QS
137 if (Region == NULL) {\r
138 onig_free (OnigRegex);\r
139 return EFI_OUT_OF_RESOURCES;\r
140 }\r
14b0e578
CS
141 OnigResult = onig_search (\r
142 OnigRegex,\r
143 Start,\r
144 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),\r
145 Start,\r
146 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),\r
147 Region,\r
148 ONIG_OPTION_NONE\r
149 );\r
04c30dd5 150\r
14b0e578
CS
151 if (OnigResult >= 0) {\r
152 *Result = TRUE;\r
153 } else {\r
154 *Result = FALSE;\r
155 if (OnigResult != ONIG_MISMATCH) {\r
156 onig_error_code_to_str (ErrorMessage, OnigResult);\r
157 DEBUG ((DEBUG_ERROR, "Regex match failed: %a\n", ErrorMessage));\r
04c30dd5
CS
158 onig_region_free (Region, 1);\r
159 onig_free (OnigRegex);\r
160 return EFI_DEVICE_ERROR;\r
14b0e578
CS
161 }\r
162 }\r
163\r
164 //\r
165 // If successful, copy out the region (capture) information\r
166 //\r
167 if (*Result && Captures != NULL) {\r
168 *CapturesCount = Region->num_regs;\r
04c30dd5 169 *Captures = AllocateZeroPool (*CapturesCount * sizeof(**Captures));\r
14b0e578
CS
170 if (*Captures != NULL) {\r
171 for (Index = 0; Index < *CapturesCount; ++Index) {\r
172 //\r
173 // Region beg/end values represent bytes, not characters\r
174 //\r
14b0e578 175 (*Captures)[Index].Length = (Region->end[Index] - Region->beg[Index]) / sizeof(CHAR16);\r
04c30dd5
CS
176 (*Captures)[Index].CapturePtr = AllocateCopyPool (\r
177 ((*Captures)[Index].Length) * sizeof (CHAR16),\r
178 (CHAR16*)((UINTN)String + Region->beg[Index])\r
179 );\r
180 if ((*Captures)[Index].CapturePtr == NULL) {\r
181 Status = EFI_OUT_OF_RESOURCES;\r
182 break;\r
183 }\r
184 }\r
185\r
186 if (EFI_ERROR (Status)) {\r
187 for (Index = 0; Index < *CapturesCount; ++Index) {\r
188 if ((*Captures)[Index].CapturePtr != NULL) {\r
189 FreePool ((CHAR16*)(*Captures)[Index].CapturePtr);\r
190 }\r
191 }\r
192 FreePool (*Captures);\r
14b0e578
CS
193 }\r
194 }\r
195 }\r
196\r
197 onig_region_free (Region, 1);\r
198 onig_free (OnigRegex);\r
199\r
04c30dd5 200 return Status;\r
14b0e578
CS
201}\r
202\r
203/**\r
204 Returns information about the regular expression syntax types supported\r
205 by the implementation.\r
206\r
27b5bf5d
DB
207 @param This A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL\r
208 instance.\r
14b0e578 209\r
27b5bf5d
DB
210 @param RegExSyntaxTypeListSize On input, the size in bytes of RegExSyntaxTypeList.\r
211 On output with a return code of EFI_SUCCESS, the\r
212 size in bytes of the data returned in\r
213 RegExSyntaxTypeList. On output with a return code\r
214 of EFI_BUFFER_TOO_SMALL, the size of\r
215 RegExSyntaxTypeList required to obtain the list.\r
14b0e578 216\r
27b5bf5d
DB
217 @param RegExSyntaxTypeList A caller-allocated memory buffer filled by the\r
218 driver with one EFI_REGEX_SYNTAX_TYPE element\r
219 for each supported Regular expression syntax\r
220 type. The list must not change across multiple\r
221 calls to the same driver. The first syntax\r
222 type in the list is the default type for the\r
223 driver.\r
14b0e578
CS
224\r
225 @retval EFI_SUCCESS The regular expression syntax types list\r
226 was returned successfully.\r
227 @retval EFI_UNSUPPORTED The service is not supported by this driver.\r
228 @retval EFI_DEVICE_ERROR The list of syntax types could not be\r
229 retrieved due to a hardware or firmware error.\r
230 @retval EFI_BUFFER_TOO_SMALL The buffer RegExSyntaxTypeList is too small\r
231 to hold the result.\r
232 @retval EFI_INVALID_PARAMETER RegExSyntaxTypeListSize is NULL\r
233\r
234**/\r
235EFI_STATUS\r
236EFIAPI\r
237RegularExpressionGetInfo (\r
238 IN EFI_REGULAR_EXPRESSION_PROTOCOL *This,\r
239 IN OUT UINTN *RegExSyntaxTypeListSize,\r
240 OUT EFI_REGEX_SYNTAX_TYPE *RegExSyntaxTypeList\r
241 )\r
242{\r
243 UINTN SyntaxSize;\r
244 UINTN Index;\r
245\r
246 if (This == NULL || RegExSyntaxTypeListSize == NULL) {\r
247 return EFI_INVALID_PARAMETER;\r
248 }\r
249\r
250 if (*RegExSyntaxTypeListSize != 0 && RegExSyntaxTypeList == NULL) {\r
251 return EFI_INVALID_PARAMETER;\r
252 }\r
253\r
254 SyntaxSize = ARRAY_SIZE (mSupportedSyntaxes) * sizeof(**mSupportedSyntaxes);\r
255\r
256 if (*RegExSyntaxTypeListSize < SyntaxSize) {\r
257 *RegExSyntaxTypeListSize = SyntaxSize;\r
258 return EFI_BUFFER_TOO_SMALL;\r
259 }\r
260\r
261 for (Index = 0; Index < ARRAY_SIZE (mSupportedSyntaxes); ++Index) {\r
262 CopyMem (&RegExSyntaxTypeList[Index], mSupportedSyntaxes[Index], sizeof(**mSupportedSyntaxes));\r
263 }\r
264 *RegExSyntaxTypeListSize = SyntaxSize;\r
265\r
266 return EFI_SUCCESS;\r
267}\r
268\r
269/**\r
270 Checks if the input string matches to the regular expression pattern.\r
271\r
27b5bf5d
DB
272 @param This A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL instance.\r
273 Type EFI_REGULAR_EXPRESSION_PROTOCOL is defined in Section\r
274 XYZ.\r
275\r
276 @param String A pointer to a NULL terminated string to match against the\r
277 regular expression string specified by Pattern.\r
278\r
279 @param Pattern A pointer to a NULL terminated string that represents the\r
280 regular expression.\r
281\r
282 @param SyntaxType A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the\r
283 regular expression syntax type to use. May be NULL in which\r
284 case the function will use its default regular expression\r
285 syntax type.\r
286\r
287 @param Result On return, points to TRUE if String fully matches against\r
288 the regular expression Pattern using the regular expression\r
289 SyntaxType. Otherwise, points to FALSE.\r
290\r
291 @param Captures A Pointer to an array of EFI_REGEX_CAPTURE objects to receive\r
292 the captured groups in the event of a match. The full\r
293 sub-string match is put in Captures[0], and the results of N\r
294 capturing groups are put in Captures[1:N]. If Captures is\r
295 NULL, then this function doesn't allocate the memory for the\r
296 array and does not build up the elements. It only returns the\r
297 number of matching patterns in CapturesCount. If Captures is\r
298 not NULL, this function returns a pointer to an array and\r
299 builds up the elements in the array. CapturesCount is also\r
300 updated to the number of matching patterns found. It is the\r
301 caller's responsibility to free the memory pool in Captures\r
302 and in each CapturePtr in the array elements.\r
303\r
304 @param CapturesCount On output, CapturesCount is the number of matching patterns\r
305 found in String. Zero means no matching patterns were found\r
306 in the string.\r
14b0e578
CS
307\r
308 @retval EFI_SUCCESS The regular expression string matching\r
309 completed successfully.\r
310 @retval EFI_UNSUPPORTED The regular expression syntax specified by\r
311 SyntaxType is not supported by this driver.\r
312 @retval EFI_DEVICE_ERROR The regular expression string matching\r
313 failed due to a hardware or firmware error.\r
314 @retval EFI_INVALID_PARAMETER String, Pattern, Result, or CapturesCountis\r
315 NULL.\r
316\r
317**/\r
318EFI_STATUS\r
319EFIAPI\r
320RegularExpressionMatch (\r
321 IN EFI_REGULAR_EXPRESSION_PROTOCOL *This,\r
322 IN CHAR16 *String,\r
323 IN CHAR16 *Pattern,\r
324 IN EFI_REGEX_SYNTAX_TYPE *SyntaxType, OPTIONAL\r
325 OUT BOOLEAN *Result,\r
326 OUT EFI_REGEX_CAPTURE **Captures, OPTIONAL\r
327 OUT UINTN *CapturesCount\r
328 )\r
329{\r
330 EFI_STATUS Status;\r
331 UINT32 Index;\r
332 BOOLEAN Supported;\r
333\r
334 if (This == NULL || String == NULL || Pattern == NULL || Result == NULL || CapturesCount == NULL) {\r
335 return EFI_INVALID_PARAMETER;\r
336 }\r
337\r
338 //\r
339 // Figure out which syntax to use\r
340 //\r
341 if (SyntaxType == NULL) {\r
342 SyntaxType = mSupportedSyntaxes[0];\r
343 } else {\r
344 Supported = FALSE;\r
345 for (Index = 0; Index < ARRAY_SIZE (mSupportedSyntaxes); ++Index) {\r
346 if (CompareGuid (SyntaxType, mSupportedSyntaxes[Index])) {\r
347 Supported = TRUE;\r
348 break;\r
349 }\r
350 }\r
351 if (!Supported) {\r
352 return EFI_UNSUPPORTED;\r
353 }\r
354 }\r
355\r
356 Status = OnigurumaMatch (String, Pattern, SyntaxType, Result, Captures, CapturesCount);\r
357\r
358 return Status;\r
359}\r
360\r
361/**\r
362 Entry point for RegularExpressionDxe.\r
27b5bf5d
DB
363\r
364 @param ImageHandle Image handle this driver.\r
365 @param SystemTable Pointer to SystemTable.\r
366\r
367 @retval Status Whether this function complete successfully.\r
368\r
14b0e578
CS
369**/\r
370EFI_STATUS\r
371EFIAPI\r
372RegularExpressionDxeEntry (\r
373 IN EFI_HANDLE ImageHandle,\r
374 IN EFI_SYSTEM_TABLE *SystemTable\r
375 )\r
376{\r
377 EFI_STATUS Status;\r
378\r
379 Status = gBS->InstallMultipleProtocolInterfaces (\r
380 &ImageHandle,\r
381 &gEfiRegularExpressionProtocolGuid,\r
382 &mProtocolInstance,\r
383 NULL\r
384 );\r
385\r
386 return Status;\r
387}\r