3 EFI_REGULAR_EXPRESSION_PROTOCOL Implementation
5 Copyright (c) 2015, Hewlett Packard Enterprise Development, L.P.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License that accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "RegularExpressionDxe.h"
20 EFI_REGEX_SYNTAX_TYPE
* CONST mSupportedSyntaxes
[] = {
21 &gEfiRegexSyntaxTypePosixExtendedGuid
,
22 &gEfiRegexSyntaxTypePerlGuid
26 EFI_REGULAR_EXPRESSION_PROTOCOL mProtocolInstance
= {
27 RegularExpressionMatch
,
28 RegularExpressionGetInfo
33 #define CHAR16_ENCODING ONIG_ENCODING_UTF16_LE
36 Call the Oniguruma regex match API.
38 Same parameters as RegularExpressionMatch, except SyntaxType is required.
40 @param String A pointer to a NULL terminated string to match against the
41 regular expression string specified by Pattern.
43 @param Pattern A pointer to a NULL terminated string that represents the
45 @param SyntaxType A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the
46 regular expression syntax type to use. May be NULL in which
47 case the function will use its default regular expression
50 @param Result On return, points to TRUE if String fully matches against
51 the regular expression Pattern using the regular expression
52 SyntaxType. Otherwise, points to FALSE.
54 @param Captures A Pointer to an array of EFI_REGEX_CAPTURE objects to receive
55 the captured groups in the event of a match. The full
56 sub-string match is put in Captures[0], and the results of N
57 capturing groups are put in Captures[1:N]. If Captures is
58 NULL, then this function doesn't allocate the memory for the
59 array and does not build up the elements. It only returns the
60 number of matching patterns in CapturesCount. If Captures is
61 not NULL, this function returns a pointer to an array and
62 builds up the elements in the array. CapturesCount is also
63 updated to the number of matching patterns found. It is the
64 caller's responsibility to free the memory pool in Captures
65 and in each CapturePtr in the array elements.
67 @param CapturesCount On output, CapturesCount is the number of matching patterns
68 found in String. Zero means no matching patterns were found
71 @retval EFI_SUCCESS Regex compilation and match completed successfully.
72 @retval EFI_DEVICE_ERROR Regex compilation failed.
80 IN EFI_REGEX_SYNTAX_TYPE
*SyntaxType
,
82 OUT EFI_REGEX_CAPTURE
**Captures
, OPTIONAL
83 OUT UINTN
*CapturesCount
87 OnigSyntaxType
*OnigSyntax
;
90 OnigErrorInfo ErrorInfo
;
91 CHAR8 ErrorMessage
[ONIG_MAX_ERROR_MESSAGE_LEN
];
96 // Detemine the internal syntax type
98 OnigSyntax
= ONIG_SYNTAX_DEFAULT
;
99 if (CompareGuid (SyntaxType
, &gEfiRegexSyntaxTypePosixExtendedGuid
)) {
100 OnigSyntax
= ONIG_SYNTAX_POSIX_EXTENDED
;
101 } else if (CompareGuid (SyntaxType
, &gEfiRegexSyntaxTypePerlGuid
)) {
102 OnigSyntax
= ONIG_SYNTAX_PERL
;
104 DEBUG ((DEBUG_ERROR
, "Unsupported regex syntax - using default\n"));
111 Start
= (OnigUChar
*)Pattern
;
112 OnigResult
= onig_new (
115 Start
+ onigenc_str_bytelen_null (CHAR16_ENCODING
, Start
),
122 if (OnigResult
!= ONIG_NORMAL
) {
123 onig_error_code_to_str (ErrorMessage
, OnigResult
, &ErrorInfo
);
124 DEBUG ((DEBUG_ERROR
, "Regex compilation failed: %a\n", ErrorMessage
));
125 return EFI_DEVICE_ERROR
;
131 Start
= (OnigUChar
*)String
;
132 Region
= onig_region_new ();
133 OnigResult
= onig_search (
136 Start
+ onigenc_str_bytelen_null (CHAR16_ENCODING
, Start
),
138 Start
+ onigenc_str_bytelen_null (CHAR16_ENCODING
, Start
),
142 if (OnigResult
>= 0) {
146 if (OnigResult
!= ONIG_MISMATCH
) {
147 onig_error_code_to_str (ErrorMessage
, OnigResult
);
148 DEBUG ((DEBUG_ERROR
, "Regex match failed: %a\n", ErrorMessage
));
153 // If successful, copy out the region (capture) information
155 if (*Result
&& Captures
!= NULL
) {
156 *CapturesCount
= Region
->num_regs
;
157 *Captures
= AllocatePool (*CapturesCount
* sizeof(**Captures
));
158 if (*Captures
!= NULL
) {
159 for (Index
= 0; Index
< *CapturesCount
; ++Index
) {
161 // Region beg/end values represent bytes, not characters
163 (*Captures
)[Index
].CapturePtr
= (CHAR16
*)((UINTN
)String
+ Region
->beg
[Index
]);
164 (*Captures
)[Index
].Length
= (Region
->end
[Index
] - Region
->beg
[Index
]) / sizeof(CHAR16
);
169 onig_region_free (Region
, 1);
170 onig_free (OnigRegex
);
176 Returns information about the regular expression syntax types supported
177 by the implementation.
179 @param This A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL
182 @param RegExSyntaxTypeListSize On input, the size in bytes of RegExSyntaxTypeList.
183 On output with a return code of EFI_SUCCESS, the
184 size in bytes of the data returned in
185 RegExSyntaxTypeList. On output with a return code
186 of EFI_BUFFER_TOO_SMALL, the size of
187 RegExSyntaxTypeList required to obtain the list.
189 @param RegExSyntaxTypeList A caller-allocated memory buffer filled by the
190 driver with one EFI_REGEX_SYNTAX_TYPE element
191 for each supported Regular expression syntax
192 type. The list must not change across multiple
193 calls to the same driver. The first syntax
194 type in the list is the default type for the
197 @retval EFI_SUCCESS The regular expression syntax types list
198 was returned successfully.
199 @retval EFI_UNSUPPORTED The service is not supported by this driver.
200 @retval EFI_DEVICE_ERROR The list of syntax types could not be
201 retrieved due to a hardware or firmware error.
202 @retval EFI_BUFFER_TOO_SMALL The buffer RegExSyntaxTypeList is too small
204 @retval EFI_INVALID_PARAMETER RegExSyntaxTypeListSize is NULL
209 RegularExpressionGetInfo (
210 IN EFI_REGULAR_EXPRESSION_PROTOCOL
*This
,
211 IN OUT UINTN
*RegExSyntaxTypeListSize
,
212 OUT EFI_REGEX_SYNTAX_TYPE
*RegExSyntaxTypeList
218 if (This
== NULL
|| RegExSyntaxTypeListSize
== NULL
) {
219 return EFI_INVALID_PARAMETER
;
222 if (*RegExSyntaxTypeListSize
!= 0 && RegExSyntaxTypeList
== NULL
) {
223 return EFI_INVALID_PARAMETER
;
226 SyntaxSize
= ARRAY_SIZE (mSupportedSyntaxes
) * sizeof(**mSupportedSyntaxes
);
228 if (*RegExSyntaxTypeListSize
< SyntaxSize
) {
229 *RegExSyntaxTypeListSize
= SyntaxSize
;
230 return EFI_BUFFER_TOO_SMALL
;
233 for (Index
= 0; Index
< ARRAY_SIZE (mSupportedSyntaxes
); ++Index
) {
234 CopyMem (&RegExSyntaxTypeList
[Index
], mSupportedSyntaxes
[Index
], sizeof(**mSupportedSyntaxes
));
236 *RegExSyntaxTypeListSize
= SyntaxSize
;
242 Checks if the input string matches to the regular expression pattern.
244 @param This A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL instance.
245 Type EFI_REGULAR_EXPRESSION_PROTOCOL is defined in Section
248 @param String A pointer to a NULL terminated string to match against the
249 regular expression string specified by Pattern.
251 @param Pattern A pointer to a NULL terminated string that represents the
254 @param SyntaxType A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the
255 regular expression syntax type to use. May be NULL in which
256 case the function will use its default regular expression
259 @param Result On return, points to TRUE if String fully matches against
260 the regular expression Pattern using the regular expression
261 SyntaxType. Otherwise, points to FALSE.
263 @param Captures A Pointer to an array of EFI_REGEX_CAPTURE objects to receive
264 the captured groups in the event of a match. The full
265 sub-string match is put in Captures[0], and the results of N
266 capturing groups are put in Captures[1:N]. If Captures is
267 NULL, then this function doesn't allocate the memory for the
268 array and does not build up the elements. It only returns the
269 number of matching patterns in CapturesCount. If Captures is
270 not NULL, this function returns a pointer to an array and
271 builds up the elements in the array. CapturesCount is also
272 updated to the number of matching patterns found. It is the
273 caller's responsibility to free the memory pool in Captures
274 and in each CapturePtr in the array elements.
276 @param CapturesCount On output, CapturesCount is the number of matching patterns
277 found in String. Zero means no matching patterns were found
280 @retval EFI_SUCCESS The regular expression string matching
281 completed successfully.
282 @retval EFI_UNSUPPORTED The regular expression syntax specified by
283 SyntaxType is not supported by this driver.
284 @retval EFI_DEVICE_ERROR The regular expression string matching
285 failed due to a hardware or firmware error.
286 @retval EFI_INVALID_PARAMETER String, Pattern, Result, or CapturesCountis
292 RegularExpressionMatch (
293 IN EFI_REGULAR_EXPRESSION_PROTOCOL
*This
,
296 IN EFI_REGEX_SYNTAX_TYPE
*SyntaxType
, OPTIONAL
298 OUT EFI_REGEX_CAPTURE
**Captures
, OPTIONAL
299 OUT UINTN
*CapturesCount
306 if (This
== NULL
|| String
== NULL
|| Pattern
== NULL
|| Result
== NULL
|| CapturesCount
== NULL
) {
307 return EFI_INVALID_PARAMETER
;
311 // Figure out which syntax to use
313 if (SyntaxType
== NULL
) {
314 SyntaxType
= mSupportedSyntaxes
[0];
317 for (Index
= 0; Index
< ARRAY_SIZE (mSupportedSyntaxes
); ++Index
) {
318 if (CompareGuid (SyntaxType
, mSupportedSyntaxes
[Index
])) {
324 return EFI_UNSUPPORTED
;
328 Status
= OnigurumaMatch (String
, Pattern
, SyntaxType
, Result
, Captures
, CapturesCount
);
334 Entry point for RegularExpressionDxe.
336 @param ImageHandle Image handle this driver.
337 @param SystemTable Pointer to SystemTable.
339 @retval Status Whether this function complete successfully.
344 RegularExpressionDxeEntry (
345 IN EFI_HANDLE ImageHandle
,
346 IN EFI_SYSTEM_TABLE
*SystemTable
351 Status
= gBS
->InstallMultipleProtocolInterfaces (
353 &gEfiRegularExpressionProtocolGuid
,