]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/HiiDatabaseDxe/Font.c
MdeModulePkg HiiDataBase: Fix the potential NULL pointer reference
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / Font.c
index 61e50c4e9c681e35bd043a4209979ee3502b8aca..b85cf88f54e53a8386ad1ed7005f8c09f52df9cb 100644 (file)
@@ -2,7 +2,7 @@
 Implementation for EFI_HII_FONT_PROTOCOL.\r
 \r
 \r
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -275,7 +275,7 @@ GetGlyphBuffer (
   @param  RowWidth       The width of the text on the line, in pixels.\r
   @param  RowHeight      The height of the line, in pixels.\r
   @param  Transparent    If TRUE, the Background color is ignored and all\r
-                         "off" pixels in the character's drawn wil use the\r
+                         "off" pixels in the character's drawn will use the\r
                          pixel value from BltBuffer.\r
   @param  Origin         On input, points to the origin of the to be\r
                          displayed character, on output, points to the\r
@@ -351,7 +351,7 @@ NarrowGlyphToBlt (
   @param  RowWidth                The width of the text on the line, in pixels.\r
   @param  RowHeight               The height of the line, in pixels.\r
   @param  Transparent             If TRUE, the Background color is ignored and all\r
-                                  "off" pixels in the character's drawn wil use the\r
+                                  "off" pixels in the character's drawn will use the\r
                                   pixel value from BltBuffer.\r
   @param  Cell                    Points to EFI_HII_GLYPH_INFO structure.\r
   @param  Attributes              The attribute of incoming glyph in GlyphBuffer.\r
@@ -411,7 +411,7 @@ GlyphToBlt (
   // The glyph's upper left hand corner pixel is the most significant bit of the\r
   // first bitmap byte.\r
   //\r
-  for (Ypos = 0; Ypos < Cell->Height && ((UINTN) (Ypos + YposOffset) < RowHeight); Ypos++) {\r
+  for (Ypos = 0; Ypos < Cell->Height && (((UINT32) Ypos + YposOffset) < RowHeight); Ypos++) {\r
     OffsetY = BITMAP_LEN_1_BIT (Cell->Width, Ypos);\r
 \r
     //\r
@@ -419,7 +419,7 @@ GlyphToBlt (
     //\r
     for (Xpos = 0; Xpos < Cell->Width / 8; Xpos++) {\r
       Data  = *(GlyphBuffer + OffsetY + Xpos);\r
-      for (Index = 0; Index < 8 && ((UINTN) (Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
+      for (Index = 0; Index < 8 && (((UINT32) Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
         if ((Data & (1 << (8 - Index - 1))) != 0) {\r
           BltBuffer[Ypos * ImageWidth + Xpos * 8 + Index] = Foreground;\r
         } else {\r
@@ -435,7 +435,7 @@ GlyphToBlt (
       // There are some padding bits in this byte. Ignore them.\r
       //\r
       Data  = *(GlyphBuffer + OffsetY + Xpos);\r
-      for (Index = 0; Index < Cell->Width % 8 && ((UINTN) (Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
+      for (Index = 0; Index < Cell->Width % 8 && (((UINT32) Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {\r
         if ((Data & (1 << (8 - Index - 1))) != 0) {\r
           BltBuffer[Ypos * ImageWidth + Xpos * 8 + Index] = Foreground;\r
         } else {\r
@@ -467,7 +467,7 @@ GlyphToBlt (
   @param  RowWidth                The width of the text on the line, in pixels.\r
   @param  RowHeight               The height of the line, in pixels.\r
   @param  Transparent             If TRUE, the Background color is ignored and all\r
-                                  "off" pixels in the character's drawn wil use the\r
+                                  "off" pixels in the character's drawn will use the\r
                                   pixel value from BltBuffer.\r
   @param  Cell                    Points to EFI_HII_GLYPH_INFO structure.\r
   @param  Attributes              The attribute of incoming glyph in GlyphBuffer.\r
@@ -593,7 +593,7 @@ GlyphToImage (
   @param  InputCell               Buffer which stores cell information of the\r
                                   encoded bitmap.\r
   @param  GlyphBuffer             Output the corresponding bitmap data of the found\r
-                                  block. It is the caller's responsiblity to free\r
+                                  block. It is the caller's responsibility to free\r
                                   this buffer.\r
   @param  Cell                    Output cell information of the encoded bitmap.\r
   @param  GlyphBufferLen          If not NULL, output the length of GlyphBuffer.\r
@@ -647,7 +647,7 @@ WriteOutputParam (
   @param  CharValue               Unicode character value, which identifies a glyph\r
                                   block.\r
   @param  GlyphBuffer             Output the corresponding bitmap data of the found\r
-                                  block. It is the caller's responsiblity to free\r
+                                  block. It is the caller's responsibility to free\r
                                   this buffer.\r
   @param  Cell                    Output cell information of the encoded bitmap.\r
   @param  GlyphBufferLen          If not NULL, output the length of GlyphBuffer.\r
@@ -704,10 +704,6 @@ FindGlyphBlock (
       (UINT8 *) FontPackage->FontPkgHdr + 3 * sizeof (UINT32),\r
       sizeof (EFI_HII_GLYPH_INFO)\r
       );\r
-    BaseLine = (UINT16) (LocalCell.Height + LocalCell.OffsetY);\r
-    if (MinOffsetY > LocalCell.OffsetY) {\r
-      MinOffsetY = LocalCell.OffsetY;\r
-    }\r
   }\r
 \r
   BlockPtr    = FontPackage->GlyphBlock;\r
@@ -843,6 +839,14 @@ FindGlyphBlock (
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
+      if (CharValue == (CHAR16) (-1)) {\r
+        if (BaseLine < DefaultCell.Height + DefaultCell.OffsetY) {\r
+          BaseLine = (UINT16) (DefaultCell.Height + DefaultCell.OffsetY);\r
+        }\r
+        if (MinOffsetY > DefaultCell.OffsetY) {\r
+          MinOffsetY = DefaultCell.OffsetY;\r
+        }\r
+      }\r
       BufferLen = BITMAP_LEN_1_BIT (DefaultCell.Width, DefaultCell.Height);\r
 \r
       if (CharCurrent == CharValue) {\r
@@ -865,6 +869,14 @@ FindGlyphBlock (
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
+      if (CharValue == (CHAR16) (-1)) {\r
+        if (BaseLine < DefaultCell.Height + DefaultCell.OffsetY) {\r
+          BaseLine = (UINT16) (DefaultCell.Height + DefaultCell.OffsetY);\r
+        }\r
+        if (MinOffsetY > DefaultCell.OffsetY) {\r
+          MinOffsetY = DefaultCell.OffsetY;\r
+        }\r
+      }\r
       BufferLen = BITMAP_LEN_1_BIT (DefaultCell.Width, DefaultCell.Height);\r
       BlockPtr += sizeof (EFI_HII_GIBT_GLYPHS_DEFAULT_BLOCK) - sizeof (UINT8);\r
       for (Index = 0; Index < Length16; Index++) {\r
@@ -933,16 +945,18 @@ SaveFontName (
   )\r
 {\r
   UINTN         FontInfoLen;\r
+  UINTN         NameSize;\r
 \r
   ASSERT (FontName != NULL && FontInfo != NULL);\r
 \r
-  FontInfoLen = sizeof (EFI_FONT_INFO) - sizeof (CHAR16) + StrSize (FontName);\r
+  NameSize = StrSize (FontName);\r
+  FontInfoLen = sizeof (EFI_FONT_INFO) - sizeof (CHAR16) + NameSize;\r
   *FontInfo = (EFI_FONT_INFO *) AllocateZeroPool (FontInfoLen);\r
   if (*FontInfo == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  StrCpy ((*FontInfo)->FontName, FontName);\r
+  StrCpyS ((*FontInfo)->FontName, NameSize / sizeof (CHAR16), FontName);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -971,6 +985,7 @@ GetSystemFont (
 {\r
   EFI_FONT_DISPLAY_INFO              *Info;\r
   UINTN                              InfoSize;\r
+  UINTN                              NameSize;\r
 \r
   if (Private == NULL || Private->Signature != HII_DATABASE_PRIVATE_DATA_SIGNATURE) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -982,7 +997,8 @@ GetSystemFont (
   //\r
   // The standard font always has the name "sysdefault".\r
   //\r
-  InfoSize = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + StrSize (L"sysdefault");\r
+  NameSize = StrSize (L"sysdefault");\r
+  InfoSize = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + NameSize;\r
   Info = (EFI_FONT_DISPLAY_INFO *) AllocateZeroPool (InfoSize);\r
   if (Info == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -993,7 +1009,7 @@ GetSystemFont (
   Info->FontInfoMask       = EFI_FONT_INFO_SYS_FONT | EFI_FONT_INFO_SYS_SIZE | EFI_FONT_INFO_SYS_STYLE;\r
   Info->FontInfo.FontStyle = 0;\r
   Info->FontInfo.FontSize  = EFI_GLYPH_HEIGHT;\r
-  StrCpy (Info->FontInfo.FontName, L"sysdefault");\r
+  StrCpyS (Info->FontInfo.FontName, NameSize / sizeof (CHAR16), L"sysdefault");\r
 \r
   *FontInfo = Info;\r
   if (FontInfoSize != NULL) {\r
@@ -1127,7 +1143,7 @@ Exit:
   @param  FontHandle              On entry, Points to the font handle returned by a\r
                                   previous  call to GetFontInfo() or NULL to start\r
                                   with the first font.\r
-  @param  GlobalFontInfo          If not NULL, output the corresponding globa font\r
+  @param  GlobalFontInfo          If not NULL, output the corresponding global font\r
                                   info.\r
 \r
   @retval TRUE                    Existed\r
@@ -1156,7 +1172,7 @@ IsFontInfoExisted (
   ASSERT (FontInfo != NULL);\r
 \r
   //\r
-  // Matched flag represents an exactly match; VagueMatched1 repensents a RESIZE\r
+  // Matched flag represents an exactly match; VagueMatched1 represents a RESIZE\r
   // or RESTYLE match; VagueMatched2 represents a RESIZE | RESTYLE match.\r
   //\r
   Matched           = FALSE;\r
@@ -1232,7 +1248,7 @@ IsFontInfoExisted (
         }\r
         break;\r
       //\r
-      // If EFI_FONT_INFO_RESIZE is specified, then the sytem may attempt to\r
+      // If EFI_FONT_INFO_RESIZE is specified, then the system may attempt to\r
       // stretch or shrink a font to meet the size requested.\r
       //\r
       case EFI_FONT_INFO_ANY_FONT | EFI_FONT_INFO_RESIZE:\r
@@ -1609,6 +1625,7 @@ HiiStringToImage (
   UINTN                               StrLength;\r
   EFI_GRAPHICS_OUTPUT_BLT_PIXEL       *RowBufferPtr;\r
   HII_GLOBAL_FONT_INFO                *GlobalFont;\r
+  UINT32                              PreInitBkgnd;\r
 \r
   //\r
   // Check incoming parameters.\r
@@ -1730,8 +1747,8 @@ HiiStringToImage (
   }\r
   \r
   //\r
-  // Use the maxinum height of font as the base line.\r
-  // And, use the maxinum height as line height.\r
+  // Use the maximum height of font as the base line.\r
+  // And, use the maximum height as line height.\r
   //\r
   LineHeight     = Height;\r
   LastLineHeight = Height;\r
@@ -1766,7 +1783,7 @@ HiiStringToImage (
   }\r
   //\r
   // If EFI_HII_IGNORE_IF_NO_GLYPH is set, then characters which have no glyphs\r
-  // are not drawn. Otherwise they are replaced wth Unicode character 0xFFFD.\r
+  // are not drawn. Otherwise they are replaced with Unicode character 0xFFFD.\r
   //\r
   StringIn2  = AllocateZeroPool (StrSize (StringPtr));\r
   if (StringIn2 == NULL) {\r
@@ -1910,7 +1927,7 @@ HiiStringToImage (
     // If this character is the last character of a row, we need not\r
     // draw its (AdvanceX - Width - OffsetX) for next character.\r
     //\r
-    LineWidth -= (UINTN) (Cell[Index].AdvanceX - Cell[Index].Width - Cell[Index].OffsetX);\r
+    LineWidth -= (Cell[Index].AdvanceX - Cell[Index].Width - Cell[Index].OffsetX);\r
 \r
     //\r
     // Clip the right-most character if cannot fit when EFI_HII_OUT_FLAG_CLEAN_X is set.\r
@@ -1933,8 +1950,8 @@ HiiStringToImage (
         //\r
         // Don't draw the last char on this row. And, don't draw the second last char (AdvanceX - Width - OffsetX).\r
         //\r
-        LineWidth -= (UINTN) (Cell[Index].Width + Cell[Index].OffsetX);\r
-        LineWidth -= (UINTN) (Cell[Index - 1].AdvanceX - Cell[Index - 1].Width - Cell[Index - 1].OffsetX);\r
+        LineWidth -= (Cell[Index].Width + Cell[Index].OffsetX);\r
+        LineWidth -= (Cell[Index - 1].AdvanceX - Cell[Index - 1].Width - Cell[Index - 1].OffsetX);\r
         RowInfo[RowIndex].EndIndex       = Index - 1;\r
         RowInfo[RowIndex].LineWidth      = LineWidth;\r
         RowInfo[RowIndex].LineHeight     = LineHeight;\r
@@ -1991,7 +2008,7 @@ HiiStringToImage (
           if (Index1 == RowInfo[RowIndex].StartIndex) {\r
             LineWidth = 0;\r
           } else {\r
-            LineWidth -= (UINTN) (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
+            LineWidth -= (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
           }\r
           RowInfo[RowIndex].LineWidth = LineWidth;\r
         }\r
@@ -2008,8 +2025,8 @@ HiiStringToImage (
             //\r
             // Don't draw the last char on this row. And, don't draw the second last char (AdvanceX - Width - OffsetX).\r
             //\r
-            LineWidth -= (UINTN) (Cell[Index1].Width + Cell[Index1].OffsetX);\r
-            LineWidth -= (UINTN) (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
+            LineWidth -= (Cell[Index1].Width + Cell[Index1].OffsetX);\r
+            LineWidth -= (Cell[Index1 - 1].AdvanceX - Cell[Index1 - 1].Width - Cell[Index1 - 1].OffsetX);\r
             RowInfo[RowIndex].EndIndex       = Index1 - 1;\r
             RowInfo[RowIndex].LineWidth      = LineWidth;\r
           } else {\r
@@ -2039,12 +2056,17 @@ HiiStringToImage (
     if ((Flags & EFI_HII_DIRECT_TO_SCREEN) == EFI_HII_DIRECT_TO_SCREEN) {\r
       BltBuffer = NULL;\r
       if (RowInfo[RowIndex].LineWidth != 0) {\r
-        BltBuffer = AllocateZeroPool (RowInfo[RowIndex].LineWidth * RowInfo[RowIndex].LineHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
+        BltBuffer = AllocatePool (RowInfo[RowIndex].LineWidth * RowInfo[RowIndex].LineHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL));\r
         if (BltBuffer == NULL) {\r
           Status = EFI_OUT_OF_RESOURCES;\r
           goto Exit;\r
         }\r
         //\r
+        // Initialize the background color.\r
+        //\r
+        PreInitBkgnd = Background.Blue | Background.Green << 8 | Background.Red << 16;\r
+        SetMem32 (BltBuffer,RowInfo[RowIndex].LineWidth * RowInfo[RowIndex].LineHeight * sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL),PreInitBkgnd);\r
+        //\r
         // Set BufferPtr to Origin by adding baseline to the starting position.\r
         //\r
         BufferPtr = BltBuffer + BaseLine * RowInfo[RowIndex].LineWidth;\r
@@ -2052,7 +2074,7 @@ HiiStringToImage (
       for (Index1 = RowInfo[RowIndex].StartIndex; Index1 <= RowInfo[RowIndex].EndIndex; Index1++) {\r
         if (RowInfo[RowIndex].LineWidth > 0 && RowInfo[RowIndex].LineWidth > LineOffset) {\r
           //\r
-          // Only BLT these character which have corrsponding glyph in font basebase.\r
+          // Only BLT these character which have corresponding glyph in font database.\r
           //\r
           GlyphToImage (\r
             GlyphBuf[Index1],\r
@@ -2102,7 +2124,7 @@ HiiStringToImage (
       }\r
     } else {\r
       //\r
-      // Save the starting position for calculate the starting postition of next row. \r
+      // Save the starting position for calculate the starting position of next row.\r
       //\r
       RowBufferPtr = BufferPtr;\r
       //\r
@@ -2112,7 +2134,7 @@ HiiStringToImage (
       for (Index1 = RowInfo[RowIndex].StartIndex; Index1 <= RowInfo[RowIndex].EndIndex; Index1++) {\r
         if (RowInfo[RowIndex].LineWidth > 0 && RowInfo[RowIndex].LineWidth > LineOffset) {\r
           //\r
-          // Only BLT these character which have corrsponding glyph in font basebase.\r
+          // Only BLT these character which have corresponding glyph in font database.\r
           //\r
           GlyphToImage (\r
             GlyphBuf[Index1],\r
@@ -2152,9 +2174,8 @@ HiiStringToImage (
 \r
 NextLine:\r
     //\r
-    // Recalculate the start point of X/Y axis to draw multi-lines with the order of top-to-down\r
+    // Recalculate the start point of Y axis to draw multi-lines with the order of top-to-down\r
     //\r
-    BltX = 0;\r
     BltY += RowInfo[RowIndex].LineHeight;\r
 \r
     RowIndex++;\r
@@ -2278,13 +2299,13 @@ Exit:
                                   when character display is normalized that some\r
                                   character cells overlap.\r
 \r
-  @retval EFI_SUCCESS             The string was successfully rendered.\r
-  @retval EFI_OUT_OF_RESOURCES    Unable to allocate an output buffer for\r
-                                  RowInfoArray or Blt.\r
+  @retval EFI_SUCCESS            The string was successfully rendered.\r
+  @retval EFI_OUT_OF_RESOURCES   Unable to allocate an output buffer for\r
+                                 RowInfoArray or Blt.\r
   @retval EFI_INVALID_PARAMETER  The Blt or PackageList was NULL.\r
   @retval EFI_INVALID_PARAMETER  Flags were invalid combination.\r
-  @retval EFI_NOT_FOUND         The specified PackageList is not in the Database or the stringid is not \r
-                          in the specified PackageList. \r
+  @retval EFI_NOT_FOUND          The specified PackageList is not in the Database or the string id is not\r
+                                 in the specified PackageList.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -2310,6 +2331,7 @@ HiiStringIdToImage (
   EFI_STRING                          String;\r
   UINTN                               StringSize;\r
   UINTN                               FontLen;\r
+  UINTN                               NameSize;\r
   EFI_FONT_INFO                       *StringFontInfo;\r
   EFI_FONT_DISPLAY_INFO               *NewStringInfo;\r
   CHAR8                               TempSupportedLanguages;\r
@@ -2432,7 +2454,8 @@ HiiStringIdToImage (
   // StringFontInfo equals NULL means system default font attaches with the string block.\r
   //\r
   if (StringFontInfo != NULL && IsSystemFontInfo (Private, (EFI_FONT_DISPLAY_INFO *) StringInfo, NULL, NULL)) {\r
-    FontLen = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + StrSize (StringFontInfo->FontName);\r
+    NameSize = StrSize (StringFontInfo->FontName);\r
+    FontLen = sizeof (EFI_FONT_DISPLAY_INFO) - sizeof (CHAR16) + NameSize;\r
     NewStringInfo = AllocateZeroPool (FontLen);\r
     if (NewStringInfo == NULL) {      \r
       Status = EFI_OUT_OF_RESOURCES;\r
@@ -2441,7 +2464,7 @@ HiiStringIdToImage (
     NewStringInfo->FontInfoMask       = EFI_FONT_INFO_SYS_FORE_COLOR | EFI_FONT_INFO_SYS_BACK_COLOR;\r
     NewStringInfo->FontInfo.FontStyle = StringFontInfo->FontStyle;\r
     NewStringInfo->FontInfo.FontSize  = StringFontInfo->FontSize;    \r
-    StrCpy (NewStringInfo->FontInfo.FontName, StringFontInfo->FontName);\r
+    StrCpyS (NewStringInfo->FontInfo.FontName, NameSize / sizeof (CHAR16), StringFontInfo->FontName);\r
   \r
     Status = HiiStringToImage (\r
                This, \r