]>
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 - 2016, 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.
198 for (Index
= 1; Index
<= 16; Index
++) {
202 for (Index
= 0; Index
< NumOfChar
; Index
++) {
203 Count
[BitLen
[Index
]]++;
208 for (Index
= 1; Index
<= 16; Index
++) {
209 Start
[Index
+ 1] = (UINT16
) (Start
[Index
] + (Count
[Index
] << (16 - Index
)));
212 if (Start
[17] != 0) {
214 return (UINT16
) BAD_TABLE
;
217 JuBits
= (UINT16
) (16 - TableBits
);
219 for (Index
= 1; Index
<= TableBits
; Index
++) {
220 Start
[Index
] >>= JuBits
;
221 Weight
[Index
] = (UINT16
) (1U << (TableBits
- Index
));
224 while (Index
<= 16) {
225 Weight
[Index
] = (UINT16
) (1U << (16 - Index
));
229 Index
= (UINT16
) (Start
[TableBits
+ 1] >> JuBits
);
232 Index3
= (UINT16
) (1U << TableBits
);
233 while (Index
!= Index3
) {
239 Mask
= (UINT16
) (1U << (15 - TableBits
));
241 for (Char
= 0; Char
< NumOfChar
; Char
++) {
244 if (Len
== 0 || Len
>= 17) {
248 NextCode
= (UINT16
) (Start
[Len
] + Weight
[Len
]);
250 if (Len
<= TableBits
) {
252 for (Index
= Start
[Len
]; Index
< NextCode
; Index
++) {
259 Pointer
= &Table
[Index3
>> JuBits
];
260 Index
= (UINT16
) (Len
- TableBits
);
264 Sd
->mRight
[Avail
] = Sd
->mLeft
[Avail
] = 0;
269 Pointer
= &Sd
->mRight
[*Pointer
];
271 Pointer
= &Sd
->mLeft
[*Pointer
];
282 Start
[Len
] = NextCode
;
299 Decodes a position value.
303 Sd - the global scratch data
307 The position value decoded.
315 Val
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
318 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
322 if (Sd
->mBitBuf
& Mask
) {
323 Val
= Sd
->mRight
[Val
];
325 Val
= Sd
->mLeft
[Val
];
329 } while (Val
>= MAXNP
);
332 // Advance what we have read
334 FillBuf (Sd
, Sd
->mPTLen
[Val
]);
338 Pos
= (UINT32
) ((1U << (Val
- 1)) + GetBits (Sd
, (UINT16
) (Val
- 1)));
356 Reads code lengths for the Extra Set or the Position Set
360 Sd - The global scratch data
361 nn - Number of symbols
362 nbit - Number of bits needed to represent nn
363 Special - The special symbol that needs to be taken care of
368 BAD_TABLE - Table is corrupted.
379 Number
= (UINT16
) GetBits (Sd
, nbit
);
382 CharC
= (UINT16
) GetBits (Sd
, nbit
);
384 for (Index
= 0; Index
< 256; Index
++) {
385 Sd
->mPTTable
[Index
] = CharC
;
388 for (Index
= 0; Index
< nn
; Index
++) {
389 Sd
->mPTLen
[Index
] = 0;
397 while (Index
< Number
) {
399 CharC
= (UINT16
) (Sd
->mBitBuf
>> (BITBUFSIZ
- 3));
402 Mask
= 1U << (BITBUFSIZ
- 1 - 3);
403 while (Mask
& Sd
->mBitBuf
) {
409 FillBuf (Sd
, (UINT16
) ((CharC
< 7) ? 3 : CharC
- 3));
411 Sd
->mPTLen
[Index
++] = (UINT8
) CharC
;
413 if (Index
== Special
) {
414 CharC
= (UINT16
) GetBits (Sd
, 2);
416 while ((INT16
) (CharC
) >= 0) {
417 Sd
->mPTLen
[Index
++] = 0;
424 Sd
->mPTLen
[Index
++] = 0;
427 return MakeTable (Sd
, nn
, Sd
->mPTLen
, 8, Sd
->mPTTable
);
439 Reads code lengths for Char&Len Set.
443 Sd - the global scratch data
454 Number
= (UINT16
) GetBits (Sd
, CBIT
);
457 CharC
= (UINT16
) GetBits (Sd
, CBIT
);
459 for (Index
= 0; Index
< NC
; Index
++) {
460 Sd
->mCLen
[Index
] = 0;
463 for (Index
= 0; Index
< 4096; Index
++) {
464 Sd
->mCTable
[Index
] = CharC
;
471 while (Index
< Number
) {
473 CharC
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
475 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
479 if (Mask
& Sd
->mBitBuf
) {
480 CharC
= Sd
->mRight
[CharC
];
482 CharC
= Sd
->mLeft
[CharC
];
487 } while (CharC
>= NT
);
490 // Advance what we have read
492 FillBuf (Sd
, Sd
->mPTLen
[CharC
]);
498 } else if (CharC
== 1) {
499 CharC
= (UINT16
) (GetBits (Sd
, 4) + 3);
500 } else if (CharC
== 2) {
501 CharC
= (UINT16
) (GetBits (Sd
, CBIT
) + 20);
505 while ((INT16
) (CharC
) >= 0) {
506 Sd
->mCLen
[Index
++] = 0;
512 Sd
->mCLen
[Index
++] = (UINT8
) (CharC
- 2);
518 Sd
->mCLen
[Index
++] = 0;
521 MakeTable (Sd
, NC
, Sd
->mCLen
, 12, Sd
->mCTable
);
535 Decode a character/length value.
539 Sd - The global scratch data.
550 if (Sd
->mBlockSize
== 0) {
552 // Starting a new block
554 Sd
->mBlockSize
= (UINT16
) GetBits (Sd
, 16);
555 Sd
->mBadTableFlag
= ReadPTLen (Sd
, NT
, TBIT
, 3);
556 if (Sd
->mBadTableFlag
!= 0) {
562 Sd
->mBadTableFlag
= ReadPTLen (Sd
, MAXNP
, mPbit
, (UINT16
) (-1));
563 if (Sd
->mBadTableFlag
!= 0) {
569 Index2
= Sd
->mCTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 12)];
572 Mask
= 1U << (BITBUFSIZ
- 1 - 12);
575 if (Sd
->mBitBuf
& Mask
) {
576 Index2
= Sd
->mRight
[Index2
];
578 Index2
= Sd
->mLeft
[Index2
];
582 } while (Index2
>= NC
);
585 // Advance what we have read
587 FillBuf (Sd
, Sd
->mCLen
[Index2
]);
601 Decode the source data and put the resulting data into the destination buffer.
605 Sd - The global scratch data
615 BytesRemain
= (UINT16
) (-1);
620 CharC
= DecodeC (Sd
);
621 if (Sd
->mBadTableFlag
!= 0) {
627 // Process an Original character
629 Sd
->mDstBase
[Sd
->mOutBuf
++] = (UINT8
) CharC
;
630 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
638 CharC
= (UINT16
) (CharC
- (UINT8_MAX
+ 1 - THRESHOLD
));
642 DataIdx
= Sd
->mOutBuf
- DecodeP (Sd
) - 1;
645 while ((INT16
) (BytesRemain
) >= 0) {
646 Sd
->mDstBase
[Sd
->mOutBuf
++] = Sd
->mDstBase
[DataIdx
++];
647 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
664 OUT UINT32
*ScratchSize
670 The implementation of EFI_DECOMPRESS_PROTOCOL.GetInfo().
674 Source - The source buffer containing the compressed data.
675 SrcSize - The size of source buffer
676 DstSize - The size of destination buffer.
677 ScratchSize - The size of scratch buffer.
681 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
682 EFI_INVALID_PARAMETER - The source data is corrupted
688 *ScratchSize
= sizeof (SCRATCH_DATA
);
692 return EFI_INVALID_PARAMETER
;
695 *DstSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
703 IN OUT VOID
*Destination
,
705 IN OUT VOID
*Scratch
,
706 IN UINT32 ScratchSize
712 The implementation Efi and Tiano Decompress().
716 Source - The source buffer containing the compressed data.
717 SrcSize - The size of source buffer
718 Destination - The destination buffer to store the decompressed data
719 DstSize - The size of destination buffer.
720 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
721 ScratchSize - The size of scratch buffer.
725 EFI_SUCCESS - Decompression is successfull
726 EFI_INVALID_PARAMETER - The source data is corrupted
738 Status
= EFI_SUCCESS
;
742 if (ScratchSize
< sizeof (SCRATCH_DATA
)) {
743 return EFI_INVALID_PARAMETER
;
746 Sd
= (SCRATCH_DATA
*) Scratch
;
749 return EFI_INVALID_PARAMETER
;
752 CompSize
= Src
[0] + (Src
[1] << 8) + (Src
[2] << 16) + (Src
[3] << 24);
753 OrigSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
755 if (SrcSize
< CompSize
+ 8) {
756 return EFI_INVALID_PARAMETER
;
759 if (DstSize
!= OrigSize
) {
760 return EFI_INVALID_PARAMETER
;
765 for (Index
= 0; Index
< sizeof (SCRATCH_DATA
); Index
++) {
766 ((UINT8
*) Sd
)[Index
] = 0;
771 Sd
->mCompSize
= CompSize
;
772 Sd
->mOrigSize
= OrigSize
;
775 // Fill the first BITBUFSIZ bits
777 FillBuf (Sd
, BITBUFSIZ
);
784 if (Sd
->mBadTableFlag
!= 0) {
786 // Something wrong with the source
788 Status
= EFI_INVALID_PARAMETER
;
799 OUT UINT32
*ScratchSize
805 The implementation Efi Decompress GetInfo().
809 Source - The source buffer containing the compressed data.
810 SrcSize - The size of source buffer
811 DstSize - The size of destination buffer.
812 ScratchSize - The size of scratch buffer.
816 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
817 EFI_INVALID_PARAMETER - The source data is corrupted
821 return GetInfo (Source
, SrcSize
, DstSize
, ScratchSize
);
829 OUT UINT32
*ScratchSize
835 The implementation Tiano Decompress GetInfo().
839 Source - The source buffer containing the compressed data.
840 SrcSize - The size of source buffer
841 DstSize - The size of destination buffer.
842 ScratchSize - The size of scratch buffer.
846 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
847 EFI_INVALID_PARAMETER - The source data is corrupted
851 return GetInfo (Source
, SrcSize
, DstSize
, ScratchSize
);
858 IN OUT VOID
*Destination
,
860 IN OUT VOID
*Scratch
,
861 IN UINT32 ScratchSize
867 The implementation of Efi Decompress().
871 Source - The source buffer containing the compressed data.
872 SrcSize - The size of source buffer
873 Destination - The destination buffer to store the decompressed data
874 DstSize - The size of destination buffer.
875 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
876 ScratchSize - The size of scratch buffer.
880 EFI_SUCCESS - Decompression is successfull
881 EFI_INVALID_PARAMETER - The source data is corrupted
886 return Decompress (Source
, SrcSize
, Destination
, DstSize
, Scratch
, ScratchSize
);
893 IN OUT VOID
*Destination
,
895 IN OUT VOID
*Scratch
,
896 IN UINT32 ScratchSize
902 The implementation of Tiano Decompress().
906 Source - The source buffer containing the compressed data.
907 SrcSize - The size of source buffer
908 Destination - The destination buffer to store the decompressed data
909 DstSize - The size of destination buffer.
910 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
911 ScratchSize - The size of scratch buffer.
915 EFI_SUCCESS - Decompression is successfull
916 EFI_INVALID_PARAMETER - The source data is corrupted
921 return Decompress (Source
, SrcSize
, Destination
, DstSize
, Scratch
, ScratchSize
);
928 OUT VOID
**Destination
,
938 Status
= EFI_SUCCESS
;
942 *Destination
= (VOID
*)malloc(SrcSize
);
943 if (*Destination
!= NULL
) {
944 memcpy(*Destination
, Source
, SrcSize
);
946 Status
= EFI_OUT_OF_RESOURCES
;
950 Status
= EfiGetInfo(Source
, SrcSize
, DstSize
, &ScratchSize
);
951 if (Status
== EFI_SUCCESS
) {
952 Scratch
= (VOID
*)malloc(ScratchSize
);
953 if (Scratch
== NULL
) {
954 return EFI_OUT_OF_RESOURCES
;
957 *Destination
= (VOID
*)malloc(*DstSize
);
958 if (*Destination
== NULL
) {
960 return EFI_OUT_OF_RESOURCES
;
963 Status
= EfiDecompress(Source
, SrcSize
, *Destination
, *DstSize
, Scratch
, ScratchSize
);
967 Status
= TianoGetInfo(Source
, SrcSize
, DstSize
, &ScratchSize
);
968 if (Status
== EFI_SUCCESS
) {
969 Scratch
= (VOID
*)malloc(ScratchSize
);
970 if (Scratch
== NULL
) {
971 return EFI_OUT_OF_RESOURCES
;
974 *Destination
= (VOID
*)malloc(*DstSize
);
975 if (*Destination
== NULL
) {
977 return EFI_OUT_OF_RESOURCES
;
980 Status
= TianoDecompress(Source
, SrcSize
, *Destination
, *DstSize
, Scratch
, ScratchSize
);
984 Status
= EFI_INVALID_PARAMETER
;
987 if (Scratch
!= NULL
) {