]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/Decompress.c
2 Decompressor. Algorithm Ported from OPSD code (Decomp.asm) for Efi and Tiano
5 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "Decompress.h"
22 // Decompression algorithm begins here
31 // C: Char&Len Set; P: Position Set; T: exTra Set
33 #define NC (0xff + MAXMATCH + 2 - THRESHOLD)
38 #define MAXNP ((1U << MAXPBIT) - 1)
39 #define NT (CODE_BIT + 3)
47 UINT8
*mSrcBase
; // Starting address of compressed data
48 UINT8
*mDstBase
; // Starting address of decompressed data
61 UINT16 mLeft
[2 * NC
- 1];
62 UINT16 mRight
[2 * NC
- 1];
69 STATIC UINT16 mPbit
= EFIPBIT
;
81 Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
85 Sd - The global scratch data
86 NumOfBit - The number of bits to shift and read.
92 Sd
->mBitBuf
= (UINT32
) (((UINT64
)Sd
->mBitBuf
) << NumOfBits
);
94 while (NumOfBits
> Sd
->mBitCount
) {
96 Sd
->mBitBuf
|= (UINT32
) (((UINT64
)Sd
->mSubBitBuf
) << (NumOfBits
= (UINT16
) (NumOfBits
- Sd
->mBitCount
)));
98 if (Sd
->mCompSize
> 0) {
100 // Get 1 byte into SubBitBuf
104 Sd
->mSubBitBuf
= Sd
->mSrcBase
[Sd
->mInBuf
++];
109 // No more bits from the source, just pad zero bit.
117 Sd
->mBitCount
= (UINT16
) (Sd
->mBitCount
- NumOfBits
);
118 Sd
->mBitBuf
|= Sd
->mSubBitBuf
>> Sd
->mBitCount
;
131 Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent
132 NumOfBits of bits from source. Returns NumOfBits of bits that are
137 Sd - The global scratch data.
138 NumOfBits - The number of bits to pop and read.
142 The bits that are popped out.
148 OutBits
= (UINT32
) (Sd
->mBitBuf
>> (BITBUFSIZ
- NumOfBits
));
150 FillBuf (Sd
, NumOfBits
);
168 Creates Huffman Code mapping table according to code length array.
172 Sd - The global scratch data
173 NumOfChar - Number of symbols in the symbol set
174 BitLen - Code length array
175 TableBits - The width of the mapping table
181 BAD_TABLE - The table is corrupted.
197 UINT16 MaxTableLength
;
199 for (Index
= 1; Index
<= 16; Index
++) {
203 for (Index
= 0; Index
< NumOfChar
; Index
++) {
204 if (BitLen
[Index
] > 16) {
205 return (UINT16
) BAD_TABLE
;
207 Count
[BitLen
[Index
]]++;
212 for (Index
= 1; Index
<= 16; Index
++) {
213 Start
[Index
+ 1] = (UINT16
) (Start
[Index
] + (Count
[Index
] << (16 - Index
)));
216 if (Start
[17] != 0) {
218 return (UINT16
) BAD_TABLE
;
221 JuBits
= (UINT16
) (16 - TableBits
);
223 for (Index
= 1; Index
<= TableBits
; Index
++) {
224 Start
[Index
] >>= JuBits
;
225 Weight
[Index
] = (UINT16
) (1U << (TableBits
- Index
));
228 while (Index
<= 16) {
229 Weight
[Index
] = (UINT16
) (1U << (16 - Index
));
233 Index
= (UINT16
) (Start
[TableBits
+ 1] >> JuBits
);
236 Index3
= (UINT16
) (1U << TableBits
);
237 while (Index
!= Index3
) {
243 Mask
= (UINT16
) (1U << (15 - TableBits
));
244 MaxTableLength
= (UINT16
) (1U << TableBits
);
246 for (Char
= 0; Char
< NumOfChar
; Char
++) {
249 if (Len
== 0 || Len
>= 17) {
253 NextCode
= (UINT16
) (Start
[Len
] + Weight
[Len
]);
255 if (Len
<= TableBits
) {
257 if (Start
[Len
] >= NextCode
|| NextCode
> MaxTableLength
){
258 return (UINT16
) BAD_TABLE
;
261 for (Index
= Start
[Len
]; Index
< NextCode
; Index
++) {
268 Pointer
= &Table
[Index3
>> JuBits
];
269 Index
= (UINT16
) (Len
- TableBits
);
273 Sd
->mRight
[Avail
] = Sd
->mLeft
[Avail
] = 0;
278 Pointer
= &Sd
->mRight
[*Pointer
];
280 Pointer
= &Sd
->mLeft
[*Pointer
];
291 Start
[Len
] = NextCode
;
308 Decodes a position value.
312 Sd - the global scratch data
316 The position value decoded.
324 Val
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
327 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
331 if (Sd
->mBitBuf
& Mask
) {
332 Val
= Sd
->mRight
[Val
];
334 Val
= Sd
->mLeft
[Val
];
338 } while (Val
>= MAXNP
);
341 // Advance what we have read
343 FillBuf (Sd
, Sd
->mPTLen
[Val
]);
347 Pos
= (UINT32
) ((1U << (Val
- 1)) + GetBits (Sd
, (UINT16
) (Val
- 1)));
365 Reads code lengths for the Extra Set or the Position Set
369 Sd - The global scratch data
370 nn - Number of symbols
371 nbit - Number of bits needed to represent nn
372 Special - The special symbol that needs to be taken care of
377 BAD_TABLE - Table is corrupted.
388 Number
= (UINT16
) GetBits (Sd
, nbit
);
391 CharC
= (UINT16
) GetBits (Sd
, nbit
);
393 for (Index
= 0; Index
< 256; Index
++) {
394 Sd
->mPTTable
[Index
] = CharC
;
397 for (Index
= 0; Index
< nn
; Index
++) {
398 Sd
->mPTLen
[Index
] = 0;
406 while (Index
< Number
&& Index
< NPT
) {
408 CharC
= (UINT16
) (Sd
->mBitBuf
>> (BITBUFSIZ
- 3));
411 Mask
= 1U << (BITBUFSIZ
- 1 - 3);
412 while (Mask
& Sd
->mBitBuf
) {
418 FillBuf (Sd
, (UINT16
) ((CharC
< 7) ? 3 : CharC
- 3));
420 Sd
->mPTLen
[Index
++] = (UINT8
) CharC
;
422 if (Index
== Special
) {
423 CharC
= (UINT16
) GetBits (Sd
, 2);
425 while ((INT16
) (CharC
) >= 0 && Index
< NPT
) {
426 Sd
->mPTLen
[Index
++] = 0;
432 while (Index
< nn
&& Index
< NPT
) {
433 Sd
->mPTLen
[Index
++] = 0;
436 return MakeTable (Sd
, nn
, Sd
->mPTLen
, 8, Sd
->mPTTable
);
448 Reads code lengths for Char&Len Set.
452 Sd - the global scratch data
463 Number
= (UINT16
) GetBits (Sd
, CBIT
);
466 CharC
= (UINT16
) GetBits (Sd
, CBIT
);
468 for (Index
= 0; Index
< NC
; Index
++) {
469 Sd
->mCLen
[Index
] = 0;
472 for (Index
= 0; Index
< 4096; Index
++) {
473 Sd
->mCTable
[Index
] = CharC
;
480 while (Index
< Number
) {
482 CharC
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
484 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
488 if (Mask
& Sd
->mBitBuf
) {
489 CharC
= Sd
->mRight
[CharC
];
491 CharC
= Sd
->mLeft
[CharC
];
496 } while (CharC
>= NT
);
499 // Advance what we have read
501 FillBuf (Sd
, Sd
->mPTLen
[CharC
]);
507 } else if (CharC
== 1) {
508 CharC
= (UINT16
) (GetBits (Sd
, 4) + 3);
509 } else if (CharC
== 2) {
510 CharC
= (UINT16
) (GetBits (Sd
, CBIT
) + 20);
514 while ((INT16
) (CharC
) >= 0) {
515 Sd
->mCLen
[Index
++] = 0;
521 Sd
->mCLen
[Index
++] = (UINT8
) (CharC
- 2);
527 Sd
->mCLen
[Index
++] = 0;
530 MakeTable (Sd
, NC
, Sd
->mCLen
, 12, Sd
->mCTable
);
544 Decode a character/length value.
548 Sd - The global scratch data.
559 if (Sd
->mBlockSize
== 0) {
561 // Starting a new block
563 Sd
->mBlockSize
= (UINT16
) GetBits (Sd
, 16);
564 Sd
->mBadTableFlag
= ReadPTLen (Sd
, NT
, TBIT
, 3);
565 if (Sd
->mBadTableFlag
!= 0) {
571 Sd
->mBadTableFlag
= ReadPTLen (Sd
, MAXNP
, mPbit
, (UINT16
) (-1));
572 if (Sd
->mBadTableFlag
!= 0) {
578 Index2
= Sd
->mCTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 12)];
581 Mask
= 1U << (BITBUFSIZ
- 1 - 12);
584 if (Sd
->mBitBuf
& Mask
) {
585 Index2
= Sd
->mRight
[Index2
];
587 Index2
= Sd
->mLeft
[Index2
];
591 } while (Index2
>= NC
);
594 // Advance what we have read
596 FillBuf (Sd
, Sd
->mCLen
[Index2
]);
610 Decode the source data and put the resulting data into the destination buffer.
614 Sd - The global scratch data
624 BytesRemain
= (UINT16
) (-1);
629 CharC
= DecodeC (Sd
);
630 if (Sd
->mBadTableFlag
!= 0) {
636 // Process an Original character
638 Sd
->mDstBase
[Sd
->mOutBuf
++] = (UINT8
) CharC
;
639 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
647 CharC
= (UINT16
) (CharC
- (UINT8_MAX
+ 1 - THRESHOLD
));
651 DataIdx
= Sd
->mOutBuf
- DecodeP (Sd
) - 1;
654 while ((INT16
) (BytesRemain
) >= 0) {
655 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
658 if (DataIdx
>= Sd
->mOrigSize
) {
659 Sd
->mBadTableFlag
= (UINT16
) BAD_TABLE
;
662 Sd
->mDstBase
[Sd
->mOutBuf
++] = Sd
->mDstBase
[DataIdx
++];
667 // Once mOutBuf is fully filled, directly return
669 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
683 OUT UINT32
*ScratchSize
689 The implementation of EFI_DECOMPRESS_PROTOCOL.GetInfo().
693 Source - The source buffer containing the compressed data.
694 SrcSize - The size of source buffer
695 DstSize - The size of destination buffer.
696 ScratchSize - The size of scratch buffer.
700 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
701 EFI_INVALID_PARAMETER - The source data is corrupted
708 *ScratchSize
= sizeof (SCRATCH_DATA
);
712 return EFI_INVALID_PARAMETER
;
715 CompSize
= Src
[0] + (Src
[1] << 8) + (Src
[2] << 16) + (Src
[3] << 24);
716 *DstSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
718 if (SrcSize
< CompSize
+ 8 || (CompSize
+ 8) < 8) {
719 return EFI_INVALID_PARAMETER
;
729 IN OUT VOID
*Destination
,
731 IN OUT VOID
*Scratch
,
732 IN UINT32 ScratchSize
738 The implementation Efi and Tiano Decompress().
742 Source - The source buffer containing the compressed data.
743 SrcSize - The size of source buffer
744 Destination - The destination buffer to store the decompressed data
745 DstSize - The size of destination buffer.
746 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
747 ScratchSize - The size of scratch buffer.
751 EFI_SUCCESS - Decompression is successful
752 EFI_INVALID_PARAMETER - The source data is corrupted
764 Status
= EFI_SUCCESS
;
768 if (ScratchSize
< sizeof (SCRATCH_DATA
)) {
769 return EFI_INVALID_PARAMETER
;
772 Sd
= (SCRATCH_DATA
*) Scratch
;
775 return EFI_INVALID_PARAMETER
;
778 CompSize
= Src
[0] + (Src
[1] << 8) + (Src
[2] << 16) + (Src
[3] << 24);
779 OrigSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
781 if (SrcSize
< CompSize
+ 8 || (CompSize
+ 8) < 8) {
782 return EFI_INVALID_PARAMETER
;
785 if (DstSize
!= OrigSize
) {
786 return EFI_INVALID_PARAMETER
;
791 for (Index
= 0; Index
< sizeof (SCRATCH_DATA
); Index
++) {
792 ((UINT8
*) Sd
)[Index
] = 0;
797 Sd
->mCompSize
= CompSize
;
798 Sd
->mOrigSize
= OrigSize
;
801 // Fill the first BITBUFSIZ bits
803 FillBuf (Sd
, BITBUFSIZ
);
810 if (Sd
->mBadTableFlag
!= 0) {
812 // Something wrong with the source
814 Status
= EFI_INVALID_PARAMETER
;
825 OUT UINT32
*ScratchSize
831 The implementation Efi Decompress GetInfo().
835 Source - The source buffer containing the compressed data.
836 SrcSize - The size of source buffer
837 DstSize - The size of destination buffer.
838 ScratchSize - The size of scratch buffer.
842 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
843 EFI_INVALID_PARAMETER - The source data is corrupted
847 return GetInfo (Source
, SrcSize
, DstSize
, ScratchSize
);
855 OUT UINT32
*ScratchSize
861 The implementation Tiano Decompress GetInfo().
865 Source - The source buffer containing the compressed data.
866 SrcSize - The size of source buffer
867 DstSize - The size of destination buffer.
868 ScratchSize - The size of scratch buffer.
872 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
873 EFI_INVALID_PARAMETER - The source data is corrupted
877 return GetInfo (Source
, SrcSize
, DstSize
, ScratchSize
);
884 IN OUT VOID
*Destination
,
886 IN OUT VOID
*Scratch
,
887 IN UINT32 ScratchSize
893 The implementation of Efi Decompress().
897 Source - The source buffer containing the compressed data.
898 SrcSize - The size of source buffer
899 Destination - The destination buffer to store the decompressed data
900 DstSize - The size of destination buffer.
901 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
902 ScratchSize - The size of scratch buffer.
906 EFI_SUCCESS - Decompression is successful
907 EFI_INVALID_PARAMETER - The source data is corrupted
912 return Decompress (Source
, SrcSize
, Destination
, DstSize
, Scratch
, ScratchSize
);
919 IN OUT VOID
*Destination
,
921 IN OUT VOID
*Scratch
,
922 IN UINT32 ScratchSize
928 The implementation of Tiano Decompress().
932 Source - The source buffer containing the compressed data.
933 SrcSize - The size of source buffer
934 Destination - The destination buffer to store the decompressed data
935 DstSize - The size of destination buffer.
936 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
937 ScratchSize - The size of scratch buffer.
941 EFI_SUCCESS - Decompression is successful
942 EFI_INVALID_PARAMETER - The source data is corrupted
947 return Decompress (Source
, SrcSize
, Destination
, DstSize
, Scratch
, ScratchSize
);
954 OUT VOID
**Destination
,
964 Status
= EFI_SUCCESS
;
968 *Destination
= (VOID
*)malloc(SrcSize
);
969 if (*Destination
!= NULL
) {
970 memcpy(*Destination
, Source
, SrcSize
);
972 Status
= EFI_OUT_OF_RESOURCES
;
976 Status
= EfiGetInfo(Source
, SrcSize
, DstSize
, &ScratchSize
);
977 if (Status
== EFI_SUCCESS
) {
978 Scratch
= (VOID
*)malloc(ScratchSize
);
979 if (Scratch
== NULL
) {
980 return EFI_OUT_OF_RESOURCES
;
983 *Destination
= (VOID
*)malloc(*DstSize
);
984 if (*Destination
== NULL
) {
986 return EFI_OUT_OF_RESOURCES
;
989 Status
= EfiDecompress(Source
, SrcSize
, *Destination
, *DstSize
, Scratch
, ScratchSize
);
993 Status
= TianoGetInfo(Source
, SrcSize
, DstSize
, &ScratchSize
);
994 if (Status
== EFI_SUCCESS
) {
995 Scratch
= (VOID
*)malloc(ScratchSize
);
996 if (Scratch
== NULL
) {
997 return EFI_OUT_OF_RESOURCES
;
1000 *Destination
= (VOID
*)malloc(*DstSize
);
1001 if (*Destination
== NULL
) {
1003 return EFI_OUT_OF_RESOURCES
;
1006 Status
= TianoDecompress(Source
, SrcSize
, *Destination
, *DstSize
, Scratch
, ScratchSize
);
1010 Status
= EFI_INVALID_PARAMETER
;
1013 if (Scratch
!= NULL
) {