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