]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/TianoCompress/TianoCompress.c
BaseTools: Add Brotli algorithm tool
[mirror_edk2.git] / BaseTools / Source / C / TianoCompress / TianoCompress.c
index 145288bb8038a508956d44169fa92cd4146aaa41..f810511f5f2f9a57c57bec78b4e9c63a06f9ac50 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
@@ -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
@@ -1762,6 +1764,8 @@ Returns:
   InputLength = 0;\r
   InputFileName = NULL;\r
   OutputFileName = NULL;\r
+  InputFile = NULL;\r
+  OutputFile = NULL;\r
   DstSize=0;\r
   DebugLevel = 0;\r
   DebugMode = FALSE;\r
@@ -1889,7 +1893,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 +1908,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 +1919,18 @@ 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
+  OutputFile = fopen (LongFilePath (OutputFileName), "wb");\r
+  if (OutputFile == NULL) {\r
+    Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName);\r
+    goto ERROR;\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
-    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
     \r
   if (ENCODE) {\r
   //\r
@@ -1949,13 +1948,21 @@ 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
+  fclose(OutputFile);\r
+  fclose(InputFile);\r
   free(Scratch);\r
   free(FileBuffer);\r
   free(OutBuffer);\r
@@ -1993,6 +2000,8 @@ Returns:
   }\r
 \r
   fwrite(OutBuffer, (size_t)(Scratch->mOrigSize), 1, OutputFile);\r
+  fclose(OutputFile);\r
+  fclose(InputFile);\r
   free(Scratch);\r
   free(FileBuffer);\r
   free(OutBuffer);\r
@@ -2015,6 +2024,12 @@ ERROR:
       DebugMsg(UTILITY_NAME, 0, DebugLevel, "Decoding Error\n", NULL);\r
     }\r
   }\r
+  if (OutputFile != NULL) {\r
+    fclose(OutputFile);\r
+  }\r
+  if (InputFile != NULL) {\r
+    fclose (InputFile);\r
+  }\r
   if (Scratch != NULL) {\r
     free(Scratch);\r
   }\r
@@ -2147,7 +2162,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 +2172,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 +2180,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 +2198,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 +2224,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 +2355,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