]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/TianoCompress/TianoCompress.c
BaseTools/TianoCompress: Fix potential memory leak
[mirror_edk2.git] / BaseTools / Source / C / TianoCompress / TianoCompress.c
index 81adcc7876ba68a02fdecb3ddb2dcab11694cf85..44dbccf9ad2bd046f9e50b4cf946b9dedc3b08b6 100644 (file)
@@ -1,6 +1,11 @@
 /** @file\r
-\r
-Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Compression routine. The compression algorithm is a mixture of LZ77 and Huffman \r
+coding. LZ77 transforms the source data into a sequence of Original Characters \r
+and Pointers to repeated strings.\r
+This sequence is further divided into Blocks and Huffman codings are applied to \r
+each Block.\r
+  \r
+Copyright (c) 2007 - 2016, 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
@@ -9,18 +14,6 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
 \r
-Module Name:\r
-\r
-  TianoCompress.c\r
-\r
-Abstract:\r
-\r
-  Compression routine. The compression algorithm is a mixture of\r
-  LZ77 and Huffman coding. LZ77 transforms the source data into a\r
-  sequence of Original Characters and Pointers to repeated strings.\r
-  This sequence is further divided into Blocks and Huffman codings\r
-  are applied to each Block.\r
-\r
 **/\r
 \r
 #include "Compress.h"\r
@@ -247,6 +240,10 @@ Returns:
   UINT32  Index;\r
 \r
   mText = malloc (WNDSIZ * 2 + MAXMATCH);\r
+  if (mText == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   for (Index = 0; Index < WNDSIZ * 2 + MAXMATCH; Index++) {\r
     mText[Index] = 0;\r
   }\r
@@ -257,6 +254,11 @@ Returns:
   mParent     = malloc (WNDSIZ * 2 * sizeof (*mParent));\r
   mPrev       = malloc (WNDSIZ * 2 * sizeof (*mPrev));\r
   mNext       = malloc ((MAX_HASH_VAL + 1) * sizeof (*mNext));\r
+  if (mLevel == NULL || mChildCount == NULL || mPosition == NULL ||\r
+    mParent == NULL || mPrev == NULL || mNext == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
 \r
   mBufSiz     = BLKSIZ;\r
   mBuf        = malloc (mBufSiz);\r
@@ -1613,7 +1615,7 @@ Returns:
   //\r
   // Copy the file contents to the output buffer.\r
   //\r
-  InputFile = fopen (InputFileName, "rb");\r
+  InputFile = fopen (LongFilePath (InputFileName), "rb");\r
     if (InputFile == NULL) {\r
       Error (NULL, 0, 0001, "Error opening file: %s", InputFileName);\r
       return EFI_ABORTED;\r
@@ -1664,7 +1666,7 @@ Returns:
 \r
 --*/\r
 {\r
-  fprintf (stdout, "%s Version %d.%d\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
+  fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
 }\r
 \r
 VOID\r
@@ -1695,7 +1697,7 @@ Returns:
   //\r
   // Copyright declaration\r
   // \r
-  fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");\r
+  fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");\r
 \r
   //\r
   // Details Option\r
@@ -1889,7 +1891,7 @@ Returns:
     goto ERROR;\r
   }\r
     \r
-  InputFile = fopen (InputFileName, "rb");\r
+  InputFile = fopen (LongFilePath (InputFileName), "rb");\r
   if (InputFile == NULL) {\r
     Error (NULL, 0, 0001, "Error opening input file", InputFileName);\r
     goto ERROR;\r
@@ -1904,7 +1906,7 @@ Returns:
     FileBuffer = (UINT8 *) malloc (InputLength);\r
     if (FileBuffer == NULL) {\r
       Error (NULL, 0, 4001, "Resource:", "Memory cannot be allocated!");\r
-      return 1;\r
+      goto ERROR;\r
     }\r
 \r
     Status = GetFileContents (\r
@@ -1915,23 +1917,21 @@ Returns:
   }\r
 \r
   if (EFI_ERROR(Status)) {\r
-    free(FileBuffer);\r
-    return 1;\r
+    Error (NULL, 0, 0004, "Error getting contents of file: %s", InputFileName);\r
+    goto ERROR;\r
+  }\r
+\r
+  if (OutputFileName == NULL) {\r
+    OutputFileName = DEFAULT_OUTPUT_FILE;\r
   }\r
-   \r
-  if (OutputFileName != NULL) {\r
-    OutputFile = fopen (OutputFileName, "wb");\r
-    if (OutputFile == NULL) {\r
-      Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName);\r
+  OutputFile = fopen (LongFilePath (OutputFileName), "wb");\r
+  if (OutputFile == NULL) {\r
+    Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName);\r
     if (InputFile != NULL) {\r
       fclose (InputFile);\r
-      }\r
-      goto ERROR;\r
-      }\r
-    } else {\r
-      OutputFileName = DEFAULT_OUTPUT_FILE;\r
-      OutputFile = fopen (OutputFileName, "wb");\r
     }\r
+    goto ERROR;\r
+  }\r
     \r
   if (ENCODE) {\r
   //\r
@@ -1949,12 +1949,18 @@ Returns:
       goto ERROR;\r
     }\r
   }\r
+\r
   Status = TianoCompress ((UINT8 *)FileBuffer, InputLength, OutBuffer, &DstSize);\r
   if (Status != EFI_SUCCESS) {\r
     Error (NULL, 0, 0007, "Error compressing file", NULL);\r
     goto ERROR;\r
   }\r
 \r
+  if (OutBuffer == NULL) {\r
+    Error (NULL, 0, 4001, "Resource:", "Memory cannot be allocated!");\r
+    goto ERROR;\r
+  }\r
+\r
   fwrite(OutBuffer,(size_t)DstSize, 1, OutputFile);\r
   free(Scratch);\r
   free(FileBuffer);\r
@@ -2147,7 +2153,7 @@ Returns:
   UINT16  Start[18];\r
   UINT16  *Pointer;\r
   UINT16  Index3;\r
-  volatile UINT16  Index;\r
+  UINT16  Index;\r
   UINT16  Len;\r
   UINT16  Char;\r
   UINT16  JuBits;\r
@@ -2157,7 +2163,7 @@ Returns:
   UINT16  WordOfStart;\r
   UINT16  WordOfCount;\r
 \r
-  for (Index = 1; Index <= 16; Index++) {\r
+  for (Index = 0; Index <= 16; Index++) {\r
     Count[Index] = 0;\r
   }\r
 \r
@@ -2165,6 +2171,7 @@ Returns:
     Count[BitLen[Index]]++;\r
   }\r
 \r
+  Start[0] = 0;\r
   Start[1] = 0;\r
 \r
   for (Index = 1; Index <= 16; Index++) {\r
@@ -2182,6 +2189,7 @@ Returns:
 \r
   JuBits = (UINT16) (16 - TableBits);\r
 \r
+  Weight[0] = 0;\r
   for (Index = 1; Index <= TableBits; Index++) {\r
     Start[Index] >>= JuBits;\r
     Weight[Index] = (UINT16) (1U << (TableBits - Index));\r
@@ -2207,7 +2215,7 @@ Returns:
   for (Char = 0; Char < NumOfChar; Char++) {\r
 \r
     Len = BitLen[Char];\r
-    if (Len == 0) {\r
+    if (Len == 0 || Len >= 17) {\r
       continue;\r
     }\r
 \r
@@ -2338,6 +2346,8 @@ Returns:
   volatile UINT16  Index;\r
   UINT32  Mask;\r
 \r
+  assert (nn <= NPT);\r
+\r
   Number = (UINT16) GetBits (Sd, nbit);\r
 \r
   if (Number == 0) {\r