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