X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FC%2FTianoCompress%2FTianoCompress.c;h=f810511f5f2f9a57c57bec78b4e9c63a06f9ac50;hb=87d97b6a771a5f81277732d5d9985aa9d14ae63f;hp=145288bb8038a508956d44169fa92cd4146aaa41;hpb=b36d134faf4305247830522b8e2bb255e98c5699;p=mirror_edk2.git diff --git a/BaseTools/Source/C/TianoCompress/TianoCompress.c b/BaseTools/Source/C/TianoCompress/TianoCompress.c index 145288bb80..f810511f5f 100644 --- a/BaseTools/Source/C/TianoCompress/TianoCompress.c +++ b/BaseTools/Source/C/TianoCompress/TianoCompress.c @@ -1,6 +1,11 @@ /** @file - -Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.
+Compression routine. The compression algorithm is a mixture of LZ77 and Huffman +coding. LZ77 transforms the source data into a sequence of Original Characters +and Pointers to repeated strings. +This sequence is further divided into Blocks and Huffman codings are applied to +each Block. + +Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -9,18 +14,6 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - TianoCompress.c - -Abstract: - - Compression routine. The compression algorithm is a mixture of - LZ77 and Huffman coding. LZ77 transforms the source data into a - sequence of Original Characters and Pointers to repeated strings. - This sequence is further divided into Blocks and Huffman codings - are applied to each Block. - **/ #include "Compress.h" @@ -247,6 +240,10 @@ Returns: UINT32 Index; mText = malloc (WNDSIZ * 2 + MAXMATCH); + if (mText == NULL) { + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!"); + return EFI_OUT_OF_RESOURCES; + } for (Index = 0; Index < WNDSIZ * 2 + MAXMATCH; Index++) { mText[Index] = 0; } @@ -257,6 +254,11 @@ Returns: mParent = malloc (WNDSIZ * 2 * sizeof (*mParent)); mPrev = malloc (WNDSIZ * 2 * sizeof (*mPrev)); mNext = malloc ((MAX_HASH_VAL + 1) * sizeof (*mNext)); + if (mLevel == NULL || mChildCount == NULL || mPosition == NULL || + mParent == NULL || mPrev == NULL || mNext == NULL) { + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!"); + return EFI_OUT_OF_RESOURCES; + } mBufSiz = BLKSIZ; mBuf = malloc (mBufSiz); @@ -1613,7 +1615,7 @@ Returns: // // Copy the file contents to the output buffer. // - InputFile = fopen (InputFileName, "rb"); + InputFile = fopen (LongFilePath (InputFileName), "rb"); if (InputFile == NULL) { Error (NULL, 0, 0001, "Error opening file: %s", InputFileName); return EFI_ABORTED; @@ -1695,7 +1697,7 @@ Returns: // // Copyright declaration // - fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n"); + fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n"); // // Details Option @@ -1762,6 +1764,8 @@ Returns: InputLength = 0; InputFileName = NULL; OutputFileName = NULL; + InputFile = NULL; + OutputFile = NULL; DstSize=0; DebugLevel = 0; DebugMode = FALSE; @@ -1889,7 +1893,7 @@ Returns: goto ERROR; } - InputFile = fopen (InputFileName, "rb"); + InputFile = fopen (LongFilePath (InputFileName), "rb"); if (InputFile == NULL) { Error (NULL, 0, 0001, "Error opening input file", InputFileName); goto ERROR; @@ -1904,7 +1908,7 @@ Returns: FileBuffer = (UINT8 *) malloc (InputLength); if (FileBuffer == NULL) { Error (NULL, 0, 4001, "Resource:", "Memory cannot be allocated!"); - return 1; + goto ERROR; } Status = GetFileContents ( @@ -1915,23 +1919,18 @@ Returns: } if (EFI_ERROR(Status)) { - free(FileBuffer); - return 1; + Error (NULL, 0, 0004, "Error getting contents of file: %s", InputFileName); + goto ERROR; + } + + if (OutputFileName == NULL) { + OutputFileName = DEFAULT_OUTPUT_FILE; + } + OutputFile = fopen (LongFilePath (OutputFileName), "wb"); + if (OutputFile == NULL) { + Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName); + goto ERROR; } - - if (OutputFileName != NULL) { - OutputFile = fopen (OutputFileName, "wb"); - if (OutputFile == NULL) { - Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName); - if (InputFile != NULL) { - fclose (InputFile); - } - goto ERROR; - } - } else { - OutputFileName = DEFAULT_OUTPUT_FILE; - OutputFile = fopen (OutputFileName, "wb"); - } if (ENCODE) { // @@ -1949,13 +1948,21 @@ Returns: goto ERROR; } } + Status = TianoCompress ((UINT8 *)FileBuffer, InputLength, OutBuffer, &DstSize); if (Status != EFI_SUCCESS) { Error (NULL, 0, 0007, "Error compressing file", NULL); goto ERROR; } + if (OutBuffer == NULL) { + Error (NULL, 0, 4001, "Resource:", "Memory cannot be allocated!"); + goto ERROR; + } + fwrite(OutBuffer,(size_t)DstSize, 1, OutputFile); + fclose(OutputFile); + fclose(InputFile); free(Scratch); free(FileBuffer); free(OutBuffer); @@ -1993,6 +2000,8 @@ Returns: } fwrite(OutBuffer, (size_t)(Scratch->mOrigSize), 1, OutputFile); + fclose(OutputFile); + fclose(InputFile); free(Scratch); free(FileBuffer); free(OutBuffer); @@ -2015,6 +2024,12 @@ ERROR: DebugMsg(UTILITY_NAME, 0, DebugLevel, "Decoding Error\n", NULL); } } + if (OutputFile != NULL) { + fclose(OutputFile); + } + if (InputFile != NULL) { + fclose (InputFile); + } if (Scratch != NULL) { free(Scratch); } @@ -2147,7 +2162,7 @@ Returns: UINT16 Start[18]; UINT16 *Pointer; UINT16 Index3; - volatile UINT16 Index; + UINT16 Index; UINT16 Len; UINT16 Char; UINT16 JuBits; @@ -2157,7 +2172,7 @@ Returns: UINT16 WordOfStart; UINT16 WordOfCount; - for (Index = 1; Index <= 16; Index++) { + for (Index = 0; Index <= 16; Index++) { Count[Index] = 0; } @@ -2165,6 +2180,7 @@ Returns: Count[BitLen[Index]]++; } + Start[0] = 0; Start[1] = 0; for (Index = 1; Index <= 16; Index++) { @@ -2182,6 +2198,7 @@ Returns: JuBits = (UINT16) (16 - TableBits); + Weight[0] = 0; for (Index = 1; Index <= TableBits; Index++) { Start[Index] >>= JuBits; Weight[Index] = (UINT16) (1U << (TableBits - Index)); @@ -2207,7 +2224,7 @@ Returns: for (Char = 0; Char < NumOfChar; Char++) { Len = BitLen[Char]; - if (Len == 0) { + if (Len == 0 || Len >= 17) { continue; } @@ -2338,6 +2355,8 @@ Returns: volatile UINT16 Index; UINT32 Mask; + assert (nn <= NPT); + Number = (UINT16) GetBits (Sd, nbit); if (Number == 0) {