]>
git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/EfiLdr/TianoDecompress.c
3 Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Decompressor. Algorithm Ported from OPSD code (Decomp.asm)
21 #include <TianoDecompress.h>
23 // Decompression algorithm begins here
32 // C: Char&Len Set; P: Position Set; T: exTra Set
34 #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 // The length of the field 'Position Set Code Length Array Size' in Block Header.
70 // For EFI 1.1 de/compression algorithm, mPBit = 4
71 // For Tiano de/compression algorithm, mPBit = 5
85 Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
89 Sd - The global scratch data
90 NumOfBits - The number of bits to shift and read.
96 Sd
->mBitBuf
= (UINT32
) (Sd
->mBitBuf
<< NumOfBits
);
98 while (NumOfBits
> Sd
->mBitCount
) {
100 Sd
->mBitBuf
|= (UINT32
) (Sd
->mSubBitBuf
<< (NumOfBits
= (UINT16
) (NumOfBits
- Sd
->mBitCount
)));
102 if (Sd
->mCompSize
> 0) {
104 // Get 1 byte into SubBitBuf
108 Sd
->mSubBitBuf
= Sd
->mSrcBase
[Sd
->mInBuf
++];
113 // No more bits from the source, just pad zero bit.
121 Sd
->mBitCount
= (UINT16
) (Sd
->mBitCount
- NumOfBits
);
122 Sd
->mBitBuf
|= Sd
->mSubBitBuf
>> Sd
->mBitCount
;
134 Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent
135 NumOfBits of bits from source. Returns NumOfBits of bits that are
140 Sd - The global scratch data.
141 NumOfBits - The number of bits to pop and read.
145 The bits that are popped out.
151 OutBits
= (UINT32
) (Sd
->mBitBuf
>> (BITBUFSIZ
- NumOfBits
));
153 FillBuf (Sd
, NumOfBits
);
170 Creates Huffman Code mapping table according to code length array.
174 Sd - The global scratch data
175 NumOfChar - Number of symbols in the symbol set
176 BitLen - Code length array
177 TableBits - The width of the mapping table
183 BAD_TABLE - The table is corrupted.
192 volatile UINT16 Index
;
202 for (Index
= 1; Index
<= 16; Index
++) {
206 for (Index
= 0; Index
< NumOfChar
; Index
++) {
207 Count
[BitLen
[Index
]]++;
212 for (Index
= 1; Index
<= 16; Index
++) {
213 WordOfStart
= Start
[Index
];
214 WordOfCount
= Count
[Index
];
215 Start
[Index
+ 1] = (UINT16
) (WordOfStart
+ (WordOfCount
<< (16 - Index
)));
218 if (Start
[17] != 0) {
220 return (UINT16
) BAD_TABLE
;
223 JuBits
= (UINT16
) (16 - TableBits
);
225 for (Index
= 1; Index
<= TableBits
; Index
++) {
226 Start
[Index
] >>= JuBits
;
227 Weight
[Index
] = (UINT16
) (1U << (TableBits
- Index
));
230 while (Index
<= 16) {
231 Weight
[Index
] = (UINT16
) (1U << (16 - Index
));
235 Index
= (UINT16
) (Start
[TableBits
+ 1] >> JuBits
);
238 Index3
= (UINT16
) (1U << TableBits
);
239 while (Index
!= Index3
) {
245 Mask
= (UINT16
) (1U << (15 - TableBits
));
247 for (Char
= 0; Char
< NumOfChar
; Char
++) {
254 NextCode
= (UINT16
) (Start
[Len
] + Weight
[Len
]);
256 if (Len
<= TableBits
) {
258 for (Index
= Start
[Len
]; Index
< NextCode
; Index
++) {
265 Pointer
= &Table
[Index3
>> JuBits
];
266 Index
= (UINT16
) (Len
- TableBits
);
270 Sd
->mRight
[Avail
] = Sd
->mLeft
[Avail
] = 0;
275 Pointer
= &Sd
->mRight
[*Pointer
];
277 Pointer
= &Sd
->mLeft
[*Pointer
];
288 Start
[Len
] = NextCode
;
304 Decodes a position value.
308 Sd - the global scratch data
312 The position value decoded.
320 Val
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
323 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
327 if (Sd
->mBitBuf
& Mask
) {
328 Val
= Sd
->mRight
[Val
];
330 Val
= Sd
->mLeft
[Val
];
334 } while (Val
>= MAXNP
);
337 // Advance what we have read
339 FillBuf (Sd
, Sd
->mPTLen
[Val
]);
343 Pos
= (UINT32
) ((1U << (Val
- 1)) + GetBits (Sd
, (UINT16
) (Val
- 1)));
360 Reads code lengths for the Extra Set or the Position Set
364 Sd - The global scratch data
365 nn - Number of symbols
366 nbit - Number of bits needed to represent nn
367 Special - The special symbol that needs to be taken care of
372 BAD_TABLE - Table is corrupted.
378 volatile UINT16 Index
;
381 Number
= (UINT16
) GetBits (Sd
, nbit
);
384 CharC
= (UINT16
) GetBits (Sd
, nbit
);
386 for (Index
= 0; Index
< 256; Index
++) {
387 Sd
->mPTTable
[Index
] = CharC
;
390 for (Index
= 0; Index
< nn
; Index
++) {
391 Sd
->mPTLen
[Index
] = 0;
399 while (Index
< Number
) {
401 CharC
= (UINT16
) (Sd
->mBitBuf
>> (BITBUFSIZ
- 3));
404 Mask
= 1U << (BITBUFSIZ
- 1 - 3);
405 while (Mask
& Sd
->mBitBuf
) {
411 FillBuf (Sd
, (UINT16
) ((CharC
< 7) ? 3 : CharC
- 3));
413 Sd
->mPTLen
[Index
++] = (UINT8
) CharC
;
415 if (Index
== Special
) {
416 CharC
= (UINT16
) GetBits (Sd
, 2);
417 while ((INT16
) (--CharC
) >= 0) {
418 Sd
->mPTLen
[Index
++] = 0;
424 Sd
->mPTLen
[Index
++] = 0;
427 return MakeTable (Sd
, nn
, Sd
->mPTLen
, 8, Sd
->mPTTable
);
438 Reads code lengths for Char&Len Set.
442 Sd - the global scratch data
450 volatile UINT16 Index
;
453 Number
= (UINT16
) GetBits (Sd
, CBIT
);
456 CharC
= (UINT16
) GetBits (Sd
, CBIT
);
458 for (Index
= 0; Index
< NC
; Index
++) {
459 Sd
->mCLen
[Index
] = 0;
462 for (Index
= 0; Index
< 4096; Index
++) {
463 Sd
->mCTable
[Index
] = CharC
;
470 while (Index
< Number
) {
472 CharC
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
474 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
478 if (Mask
& Sd
->mBitBuf
) {
479 CharC
= Sd
->mRight
[CharC
];
481 CharC
= Sd
->mLeft
[CharC
];
486 } while (CharC
>= NT
);
489 // Advance what we have read
491 FillBuf (Sd
, Sd
->mPTLen
[CharC
]);
497 } else if (CharC
== 1) {
498 CharC
= (UINT16
) (GetBits (Sd
, 4) + 3);
499 } else if (CharC
== 2) {
500 CharC
= (UINT16
) (GetBits (Sd
, CBIT
) + 20);
503 while ((INT16
) (--CharC
) >= 0) {
504 Sd
->mCLen
[Index
++] = 0;
509 Sd
->mCLen
[Index
++] = (UINT8
) (CharC
- 2);
515 Sd
->mCLen
[Index
++] = 0;
518 MakeTable (Sd
, NC
, Sd
->mCLen
, 12, Sd
->mCTable
);
531 Decode a character/length value.
535 Sd - The global scratch data.
546 if (Sd
->mBlockSize
== 0) {
548 // Starting a new block
550 Sd
->mBlockSize
= (UINT16
) GetBits (Sd
, 16);
551 Sd
->mBadTableFlag
= ReadPTLen (Sd
, NT
, TBIT
, 3);
552 if (Sd
->mBadTableFlag
!= 0) {
558 Sd
->mBadTableFlag
= ReadPTLen (Sd
, MAXNP
, Sd
->mPBit
, (UINT16
) (-1));
559 if (Sd
->mBadTableFlag
!= 0) {
565 Index2
= Sd
->mCTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 12)];
568 Mask
= 1U << (BITBUFSIZ
- 1 - 12);
571 if (Sd
->mBitBuf
& Mask
) {
572 Index2
= Sd
->mRight
[Index2
];
574 Index2
= Sd
->mLeft
[Index2
];
578 } while (Index2
>= NC
);
581 // Advance what we have read
583 FillBuf (Sd
, Sd
->mCLen
[Index2
]);
596 Decode the source data and put the resulting data into the destination buffer.
600 Sd - The global scratch data
610 BytesRemain
= (UINT16
) (-1);
615 CharC
= DecodeC (Sd
);
616 if (Sd
->mBadTableFlag
!= 0) {
622 // Process an Original character
624 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
627 Sd
->mDstBase
[Sd
->mOutBuf
++] = (UINT8
) CharC
;
634 CharC
= (UINT16
) (CharC
- (BIT8
- THRESHOLD
));
638 DataIdx
= Sd
->mOutBuf
- DecodeP (Sd
) - 1;
641 while ((INT16
) (BytesRemain
) >= 0) {
642 Sd
->mDstBase
[Sd
->mOutBuf
++] = Sd
->mDstBase
[DataIdx
++];
643 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
661 OUT UINT32
*ScratchSize
667 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().
671 Source - The source buffer containing the compressed data.
672 SrcSize - The size of source buffer
673 DstSize - The size of destination buffer.
674 ScratchSize - The size of scratch buffer.
678 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
679 EFI_INVALID_PARAMETER - The source data is corrupted
685 *ScratchSize
= sizeof (SCRATCH_DATA
);
689 return EFI_INVALID_PARAMETER
;
692 *DstSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
700 IN OUT VOID
*Destination
,
702 IN OUT VOID
*Scratch
,
703 IN UINT32 ScratchSize
,
710 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().
714 Source - The source buffer containing the compressed data.
715 SrcSize - The size of source buffer
716 Destination - The destination buffer to store the decompressed data
717 DstSize - The size of destination buffer.
718 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
719 ScratchSize - The size of scratch buffer.
720 Version - The version of de/compression algorithm.
721 Version 1 for EFI 1.1 de/compression algorithm.
722 Version 2 for Tiano de/compression algorithm.
726 EFI_SUCCESS - Decompression is successfull
727 EFI_INVALID_PARAMETER - The source data is corrupted
737 volatile UINT32 Index
;
739 Status
= EFI_SUCCESS
;
743 if (ScratchSize
< sizeof (SCRATCH_DATA
)) {
744 return EFI_INVALID_PARAMETER
;
747 Sd
= (SCRATCH_DATA
*) Scratch
;
750 return EFI_INVALID_PARAMETER
;
753 CompSize
= Src
[0] + (Src
[1] << 8) + (Src
[2] << 16) + (Src
[3] << 24);
754 OrigSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
757 // If compressed file size is 0, return
763 if (SrcSize
< CompSize
+ 8) {
764 return EFI_INVALID_PARAMETER
;
767 if (DstSize
!= OrigSize
) {
768 return EFI_INVALID_PARAMETER
;
773 for (Index
= 0; Index
< sizeof (SCRATCH_DATA
); Index
++) {
774 ((UINT8
*) Sd
)[Index
] = 0;
778 // The length of the field 'Position Set Code Length Array Size' in Block Header.
779 // For EFI 1.1 de/compression algorithm(Version 1), mPBit = 4
780 // For Tiano de/compression algorithm(Version 2), mPBit = 5
793 // Currently, only have 2 versions
795 return EFI_INVALID_PARAMETER
;
800 Sd
->mCompSize
= CompSize
;
801 Sd
->mOrigSize
= OrigSize
;
804 // Fill the first BITBUFSIZ bits
806 FillBuf (Sd
, BITBUFSIZ
);
813 if (Sd
->mBadTableFlag
!= 0) {
815 // Something wrong with the source
817 Status
= EFI_INVALID_PARAMETER
;
829 OUT UINT32
*ScratchSize
835 The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.GetInfo().
839 This - The protocol instance pointer
840 Source - The source buffer containing the compressed data.
841 SrcSize - The size of source buffer
842 DstSize - The size of destination buffer.
843 ScratchSize - The size of scratch buffer.
847 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
848 EFI_INVALID_PARAMETER - The source data is corrupted
865 IN OUT VOID
*Destination
,
867 IN OUT VOID
*Scratch
,
868 IN UINT32 ScratchSize
874 The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.Decompress().
878 This - The protocol instance pointer
879 Source - The source buffer containing the compressed data.
880 SrcSize - The size of source buffer
881 Destination - The destination buffer to store the decompressed data
882 DstSize - The size of destination buffer.
883 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
884 ScratchSize - The size of scratch buffer.
888 EFI_SUCCESS - Decompression is successfull
889 EFI_INVALID_PARAMETER - The source data is corrupted
894 // For EFI 1.1 de/compression algorithm, the version is 1.
913 OUT UINT32
*ScratchSize
919 The implementation is same as that of EFI_TIANO_DECOMPRESS_PROTOCOL.GetInfo().
923 This - The protocol instance pointer
924 Source - The source buffer containing the compressed data.
925 SrcSize - The size of source buffer
926 DstSize - The size of destination buffer.
927 ScratchSize - The size of scratch buffer.
931 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
932 EFI_INVALID_PARAMETER - The source data is corrupted
949 IN OUT VOID
*Destination
,
951 IN OUT VOID
*Scratch
,
952 IN UINT32 ScratchSize
958 The implementation is same as that of EFI_TIANO_DECOMPRESS_PROTOCOL.Decompress().
962 This - The protocol instance pointer
963 Source - The source buffer containing the compressed data.
964 SrcSize - The size of source buffer
965 Destination - The destination buffer to store the decompressed data
966 DstSize - The size of destination buffer.
967 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
968 ScratchSize - The size of scratch buffer.
972 EFI_SUCCESS - Decompression is successfull
973 EFI_INVALID_PARAMETER - The source data is corrupted
978 // For Tiano de/compression algorithm, the version is 2.