15 &gEfiRegexSyntaxTypePosixExtendedGuid,
16 &gEfiRegexSyntaxTypePerlGuid
25#define CHAR16_ENCODING ONIG_ENCODING_UTF16_LE
79 OnigSyntaxType *OnigSyntax;
82 OnigErrorInfo ErrorInfo;
83 OnigUChar ErrorMessage[ONIG_MAX_ERROR_MESSAGE_LEN];
93 OnigSyntax = ONIG_SYNTAX_DEFAULT;
94 if (
CompareGuid (SyntaxType, &gEfiRegexSyntaxTypePosixExtendedGuid)) {
95 OnigSyntax = ONIG_SYNTAX_POSIX_EXTENDED;
96 }
else if (
CompareGuid (SyntaxType, &gEfiRegexSyntaxTypePerlGuid)) {
97 OnigSyntax = ONIG_SYNTAX_PERL;
99 DEBUG ((DEBUG_ERROR,
"Unsupported regex syntax - using default\n"));
100 return EFI_UNSUPPORTED;
106 Start = (OnigUChar *)Pattern;
107 OnigResult = onig_new (
110 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),
117 if (OnigResult != ONIG_NORMAL) {
118 onig_error_code_to_str (ErrorMessage, OnigResult, &ErrorInfo);
119 DEBUG ((DEBUG_ERROR,
"Regex compilation failed: %a\n", ErrorMessage));
120 return EFI_DEVICE_ERROR;
126 Start = (OnigUChar *)String;
127 Region = onig_region_new ();
128 if (Region ==
NULL) {
129 onig_free (OnigRegex);
130 return EFI_OUT_OF_RESOURCES;
133 OnigResult = onig_search (
136 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),
138 Start + onigenc_str_bytelen_null (CHAR16_ENCODING, Start),
143 if (OnigResult >= 0) {
147 if (OnigResult != ONIG_MISMATCH) {
148 onig_error_code_to_str (ErrorMessage, OnigResult);
149 DEBUG ((DEBUG_ERROR,
"Regex match failed: %a\n", ErrorMessage));
150 onig_region_free (Region, 1);
151 onig_free (OnigRegex);
152 return EFI_DEVICE_ERROR;
159 if (*Result && (Captures !=
NULL)) {
160 *CapturesCount = Region->num_regs;
162 if (*Captures !=
NULL) {
163 for (Index = 0; Index < *CapturesCount; ++Index) {
167 (*Captures)[Index].Length = (Region->end[Index] - Region->beg[Index]) /
sizeof (CHAR16);
169 ((*Captures)[Index].Length) *
sizeof (CHAR16),
170 (CHAR16 *)((
UINTN)String + Region->beg[Index])
172 if ((*Captures)[Index].CapturePtr ==
NULL) {
173 Status = EFI_OUT_OF_RESOURCES;
178 if (EFI_ERROR (Status)) {
179 for (Index = 0; Index < *CapturesCount; ++Index) {
180 if ((*Captures)[Index].CapturePtr !=
NULL) {
181 FreePool ((CHAR16 *)(*Captures)[Index].CapturePtr);
190 onig_region_free (Region, 1);
191 onig_free (OnigRegex);
239 if ((This ==
NULL) || (RegExSyntaxTypeListSize ==
NULL)) {
240 return EFI_INVALID_PARAMETER;
243 if ((*RegExSyntaxTypeListSize != 0) && (RegExSyntaxTypeList ==
NULL)) {
244 return EFI_INVALID_PARAMETER;
247 SyntaxSize =
ARRAY_SIZE (mSupportedSyntaxes) *
sizeof (**mSupportedSyntaxes);
249 if (*RegExSyntaxTypeListSize < SyntaxSize) {
250 *RegExSyntaxTypeListSize = SyntaxSize;
251 return EFI_BUFFER_TOO_SMALL;
254 for (Index = 0; Index <
ARRAY_SIZE (mSupportedSyntaxes); ++Index) {
255 CopyMem (&RegExSyntaxTypeList[Index], mSupportedSyntaxes[Index],
sizeof (**mSupportedSyntaxes));
258 *RegExSyntaxTypeListSize = SyntaxSize;
328 if ((This ==
NULL) || (String ==
NULL) || (Pattern ==
NULL) || (Result ==
NULL) || (CapturesCount ==
NULL)) {
329 return EFI_INVALID_PARAMETER;
335 if (SyntaxType ==
NULL) {
336 SyntaxType = mSupportedSyntaxes[0];
339 for (Index = 0; Index <
ARRAY_SIZE (mSupportedSyntaxes); ++Index) {
340 if (
CompareGuid (SyntaxType, mSupportedSyntaxes[Index])) {
347 return EFI_UNSUPPORTED;
351 Status =
OnigurumaMatch (String, Pattern, SyntaxType, Result, Captures, CapturesCount);
374 Status =
gBS->InstallMultipleProtocolInterfaces (
376 &gEfiRegularExpressionProtocolGuid,
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define ARRAY_SIZE(Array)
#define DEBUG(Expression)
STATIC EFI_STATUS OnigurumaMatch(IN CHAR16 *String, IN CHAR16 *Pattern, IN EFI_REGEX_SYNTAX_TYPE *SyntaxType, OUT BOOLEAN *Result, OUT EFI_REGEX_CAPTURE **Captures OPTIONAL, OUT UINTN *CapturesCount)
EFI_STATUS EFIAPI RegularExpressionGetInfo(IN EFI_REGULAR_EXPRESSION_PROTOCOL *This, IN OUT UINTN *RegExSyntaxTypeListSize, OUT EFI_REGEX_SYNTAX_TYPE *RegExSyntaxTypeList)
EFI_STATUS EFIAPI RegularExpressionDxeEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI RegularExpressionMatch(IN EFI_REGULAR_EXPRESSION_PROTOCOL *This, IN CHAR16 *String, IN CHAR16 *Pattern, IN EFI_REGEX_SYNTAX_TYPE *SyntaxType OPTIONAL, OUT BOOLEAN *Result, OUT EFI_REGEX_CAPTURE **Captures OPTIONAL, OUT UINTN *CapturesCount)