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