]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.c
MdeModulePkg: Delete useless case code
[mirror_edk2.git] / MdeModulePkg / Universal / RegularExpressionDxe / RegularExpressionDxe.c
index 6c62957ca380ca4916f6c02669722fdf9dbf37cc..cf325fced7f35e303b42be49f9078fbf07e4e8f2 100644 (file)
@@ -1,9 +1,8 @@
-/**\r
-  @file\r
+/** @file\r
 \r
   EFI_REGULAR_EXPRESSION_PROTOCOL Implementation\r
 \r
-  Copyright (c) 2015, Hewlett Packard Enterprise Development, L.P.<BR>\r
+  (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP<BR>\r
 \r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License that accompanies this\r
@@ -12,6 +11,7 @@
 \r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
   WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
 **/\r
 \r
 #include "RegularExpressionDxe.h"\r
@@ -37,8 +37,40 @@ EFI_REGULAR_EXPRESSION_PROTOCOL mProtocolInstance = {
 \r
   Same parameters as RegularExpressionMatch, except SyntaxType is required.\r
 \r
-  @retval EFI_SUCCESS       Regex compilation and match completed successfully.\r
-  @retval EFI_DEVICE_ERROR  Regex compilation failed.\r
+  @param String         A pointer to a NULL terminated string to match against the\r
+                        regular expression string specified by Pattern.\r
+\r
+  @param Pattern        A pointer to a NULL terminated string that represents the\r
+                        regular expression.\r
+  @param SyntaxType     A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the\r
+                        regular expression syntax type to use. May be NULL in which\r
+                        case the function will use its default regular expression\r
+                        syntax type.\r
+\r
+  @param Result         On return, points to TRUE if String fully matches against\r
+                        the regular expression Pattern using the regular expression\r
+                        SyntaxType. Otherwise, points to FALSE.\r
+\r
+  @param Captures       A Pointer to an array of EFI_REGEX_CAPTURE objects to receive\r
+                        the captured groups in the event of a match. The full\r
+                        sub-string match is put in Captures[0], and the results of N\r
+                        capturing groups are put in Captures[1:N]. If Captures is\r
+                        NULL, then this function doesn't allocate the memory for the\r
+                        array and does not build up the elements. It only returns the\r
+                        number of matching patterns in CapturesCount. If Captures is\r
+                        not NULL, this function returns a pointer to an array and\r
+                        builds up the elements in the array. CapturesCount is also\r
+                        updated to the number of matching patterns found. It is the\r
+                        caller's responsibility to free the memory pool in Captures\r
+                        and in each CapturePtr in the array elements.\r
+\r
+  @param CapturesCount  On output, CapturesCount is the number of matching patterns\r
+                        found in String. Zero means no matching patterns were found\r
+                        in the string.\r
+\r
+  @retval  EFI_SUCCESS       Regex compilation and match completed successfully.\r
+  @retval  EFI_DEVICE_ERROR  Regex compilation failed.\r
+\r
 **/\r
 STATIC\r
 EFI_STATUS\r
@@ -56,9 +88,13 @@ OnigurumaMatch (
   OnigRegion      *Region;\r
   INT32           OnigResult;\r
   OnigErrorInfo   ErrorInfo;\r
-  CHAR8           ErrorMessage[ONIG_MAX_ERROR_MESSAGE_LEN];\r
+  OnigUChar       ErrorMessage[ONIG_MAX_ERROR_MESSAGE_LEN];\r
   UINT32          Index;\r
   OnigUChar       *Start;\r
+  EFI_STATUS      Status;\r
+\r
+\r
+  Status = EFI_SUCCESS;\r
 \r
   //\r
   // Detemine the internal syntax type\r
@@ -70,7 +106,7 @@ OnigurumaMatch (
     OnigSyntax = ONIG_SYNTAX_PERL;\r
   } else {\r
     DEBUG ((DEBUG_ERROR, "Unsupported regex syntax - using default\n"));\r
-    ASSERT (FALSE);\r
+    return EFI_UNSUPPORTED;\r
   }\r
 \r
   //\r
@@ -98,6 +134,10 @@ OnigurumaMatch (
   //\r
   Start = (OnigUChar*)String;\r
   Region = onig_region_new ();\r
+  if (Region == NULL) {\r
+    onig_free (OnigRegex);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   OnigResult = onig_search (\r
                  OnigRegex,\r
                  Start,\r
@@ -107,6 +147,7 @@ OnigurumaMatch (
                  Region,\r
                  ONIG_OPTION_NONE\r
                  );\r
+\r
   if (OnigResult >= 0) {\r
     *Result = TRUE;\r
   } else {\r
@@ -114,6 +155,9 @@ OnigurumaMatch (
     if (OnigResult != ONIG_MISMATCH) {\r
       onig_error_code_to_str (ErrorMessage, OnigResult);\r
       DEBUG ((DEBUG_ERROR, "Regex match failed: %a\n", ErrorMessage));\r
+      onig_region_free (Region, 1);\r
+      onig_free (OnigRegex);\r
+      return EFI_DEVICE_ERROR;\r
     }\r
   }\r
 \r
@@ -122,14 +166,30 @@ OnigurumaMatch (
   //\r
   if (*Result && Captures != NULL) {\r
     *CapturesCount = Region->num_regs;\r
-    *Captures = AllocatePool (*CapturesCount * sizeof(**Captures));\r
+    *Captures = AllocateZeroPool (*CapturesCount * sizeof(**Captures));\r
     if (*Captures != NULL) {\r
       for (Index = 0; Index < *CapturesCount; ++Index) {\r
         //\r
         // Region beg/end values represent bytes, not characters\r
         //\r
-        (*Captures)[Index].CapturePtr = (CHAR16*)((UINTN)String + Region->beg[Index]);\r
         (*Captures)[Index].Length = (Region->end[Index] - Region->beg[Index]) / sizeof(CHAR16);\r
+        (*Captures)[Index].CapturePtr = AllocateCopyPool (\r
+                                          ((*Captures)[Index].Length) * sizeof (CHAR16),\r
+                                          (CHAR16*)((UINTN)String + Region->beg[Index])\r
+                                          );\r
+        if ((*Captures)[Index].CapturePtr == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
+      }\r
+\r
+      if (EFI_ERROR (Status)) {\r
+        for (Index = 0; Index < *CapturesCount; ++Index) {\r
+          if ((*Captures)[Index].CapturePtr != NULL) {\r
+            FreePool ((CHAR16*)(*Captures)[Index].CapturePtr);\r
+          }\r
+        }\r
+        FreePool (*Captures);\r
       }\r
     }\r
   }\r
@@ -137,30 +197,30 @@ OnigurumaMatch (
   onig_region_free (Region, 1);\r
   onig_free (OnigRegex);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 /**\r
   Returns information about the regular expression syntax types supported\r
   by the implementation.\r
 \r
-  This                     A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL\r
-                           instance.\r
+  @param This                      A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL\r
+                                   instance.\r
 \r
-  RegExSyntaxTypeListSize  On input, the size in bytes of RegExSyntaxTypeList.\r
-                           On output with a return code of EFI_SUCCESS, the\r
-                           size in bytes of the data returned in\r
-                           RegExSyntaxTypeList. On output with a return code\r
-                           of EFI_BUFFER_TOO_SMALL, the size of\r
-                           RegExSyntaxTypeList required to obtain the list.\r
+  @param  RegExSyntaxTypeListSize  On input, the size in bytes of RegExSyntaxTypeList.\r
+                                   On output with a return code of EFI_SUCCESS, the\r
+                                   size in bytes of the data returned in\r
+                                   RegExSyntaxTypeList. On output with a return code\r
+                                   of EFI_BUFFER_TOO_SMALL, the size of\r
+                                   RegExSyntaxTypeList required to obtain the list.\r
 \r
-  RegExSyntaxTypeList      A caller-allocated memory buffer filled by the\r
-                           driver with one EFI_REGEX_SYNTAX_TYPE element\r
-                           for each supported Regular expression syntax\r
-                           type. The list must not change across multiple\r
-                           calls to the same driver. The first syntax\r
-                           type in the list is the default type for the\r
-                           driver.\r
+  @param   RegExSyntaxTypeList     A caller-allocated memory buffer filled by the\r
+                                   driver with one EFI_REGEX_SYNTAX_TYPE element\r
+                                   for each supported Regular expression syntax\r
+                                   type. The list must not change across multiple\r
+                                   calls to the same driver. The first syntax\r
+                                   type in the list is the default type for the\r
+                                   driver.\r
 \r
   @retval EFI_SUCCESS            The regular expression syntax types list\r
                                  was returned successfully.\r
@@ -209,41 +269,41 @@ RegularExpressionGetInfo (
 /**\r
   Checks if the input string matches to the regular expression pattern.\r
 \r
-  This          A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL instance.\r
-                Type EFI_REGULAR_EXPRESSION_PROTOCOL is defined in Section\r
-                XYZ.\r
-\r
-  String        A pointer to a NULL terminated string to match against the\r
-                regular expression string specified by Pattern.\r
-\r
-  Pattern       A pointer to a NULL terminated string that represents the\r
-                regular expression.\r
-\r
-  SyntaxType    A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the\r
-                regular expression syntax type to use. May be NULL in which\r
-                case the function will use its default regular expression\r
-                syntax type.\r
-\r
-  Result        On return, points to TRUE if String fully matches against\r
-                the regular expression Pattern using the regular expression\r
-                SyntaxType. Otherwise, points to FALSE.\r
-\r
-  Captures      A Pointer to an array of EFI_REGEX_CAPTURE objects to receive\r
-                the captured groups in the event of a match. The full\r
-                sub-string match is put in Captures[0], and the results of N\r
-                capturing groups are put in Captures[1:N]. If Captures is\r
-                NULL, then this function doesn't allocate the memory for the\r
-                array and does not build up the elements. It only returns the\r
-                number of matching patterns in CapturesCount. If Captures is\r
-                not NULL, this function returns a pointer to an array and\r
-                builds up the elements in the array. CapturesCount is also\r
-                updated to the number of matching patterns found. It is the\r
-                caller's responsibility to free the memory pool in Captures\r
-                and in each CapturePtr in the array elements.\r
-\r
-  CapturesCount On output, CapturesCount is the number of matching patterns\r
-                found in String. Zero means no matching patterns were found\r
-                in the string.\r
+  @param This          A pointer to the EFI_REGULAR_EXPRESSION_PROTOCOL instance.\r
+                       Type EFI_REGULAR_EXPRESSION_PROTOCOL is defined in Section\r
+                       XYZ.\r
+\r
+  @param String        A pointer to a NULL terminated string to match against the\r
+                       regular expression string specified by Pattern.\r
+\r
+  @param Pattern       A pointer to a NULL terminated string that represents the\r
+                       regular expression.\r
+\r
+  @param SyntaxType    A pointer to the EFI_REGEX_SYNTAX_TYPE that identifies the\r
+                       regular expression syntax type to use. May be NULL in which\r
+                       case the function will use its default regular expression\r
+                       syntax type.\r
+\r
+  @param Result        On return, points to TRUE if String fully matches against\r
+                       the regular expression Pattern using the regular expression\r
+                       SyntaxType. Otherwise, points to FALSE.\r
+\r
+  @param Captures      A Pointer to an array of EFI_REGEX_CAPTURE objects to receive\r
+                       the captured groups in the event of a match. The full\r
+                       sub-string match is put in Captures[0], and the results of N\r
+                       capturing groups are put in Captures[1:N]. If Captures is\r
+                       NULL, then this function doesn't allocate the memory for the\r
+                       array and does not build up the elements. It only returns the\r
+                       number of matching patterns in CapturesCount. If Captures is\r
+                       not NULL, this function returns a pointer to an array and\r
+                       builds up the elements in the array. CapturesCount is also\r
+                       updated to the number of matching patterns found. It is the\r
+                       caller's responsibility to free the memory pool in Captures\r
+                       and in each CapturePtr in the array elements.\r
+\r
+  @param CapturesCount On output, CapturesCount is the number of matching patterns\r
+                       found in String. Zero means no matching patterns were found\r
+                       in the string.\r
 \r
   @retval EFI_SUCCESS            The regular expression string matching\r
                                  completed successfully.\r
@@ -300,6 +360,12 @@ RegularExpressionMatch (
 \r
 /**\r
   Entry point for RegularExpressionDxe.\r
+\r
+  @param ImageHandle     Image handle this driver.\r
+  @param SystemTable     Pointer to SystemTable.\r
+\r
+  @retval Status         Whether this function complete successfully.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r