]>
git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/EfiLdr/TianoDecompress.c
3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. 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.
200 SetMem (&Count
[1], sizeof(UINT16
) * 16, 0);
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
));
228 Index
= (UINT16
) (Start
[TableBits
+ 1] >> JuBits
);
231 Index3
= (UINT16
) (1U << TableBits
);
232 SetMem(&Table
[Index
], sizeof(UINT16
) * (Index3
- Index
+ 1), 0);
236 Mask
= (UINT16
) (1U << (15 - TableBits
));
238 for (Char
= 0; Char
< NumOfChar
; Char
++) {
245 NextCode
= (UINT16
) (Start
[Len
] + Weight
[Len
]);
247 if (Len
<= TableBits
) {
249 for (Index
= Start
[Len
]; Index
< NextCode
; Index
++) {
256 Pointer
= &Table
[Index3
>> JuBits
];
257 Index
= (UINT16
) (Len
- TableBits
);
261 Sd
->mRight
[Avail
] = Sd
->mLeft
[Avail
] = 0;
266 Pointer
= &Sd
->mRight
[*Pointer
];
268 Pointer
= &Sd
->mLeft
[*Pointer
];
279 Start
[Len
] = NextCode
;
295 Decodes a position value.
299 Sd - the global scratch data
303 The position value decoded.
311 Val
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
314 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
318 if (Sd
->mBitBuf
& Mask
) {
319 Val
= Sd
->mRight
[Val
];
321 Val
= Sd
->mLeft
[Val
];
325 } while (Val
>= MAXNP
);
328 // Advance what we have read
330 FillBuf (Sd
, Sd
->mPTLen
[Val
]);
334 Pos
= (UINT32
) ((1U << (Val
- 1)) + GetBits (Sd
, (UINT16
) (Val
- 1)));
351 Reads code lengths for the Extra Set or the Position Set
355 Sd - The global scratch data
356 nn - Number of symbols
357 nbit - Number of bits needed to represent nn
358 Special - The special symbol that needs to be taken care of
363 BAD_TABLE - Table is corrupted.
372 Number
= (UINT16
) GetBits (Sd
, nbit
);
375 CharC
= (UINT16
) GetBits (Sd
, nbit
);
377 for (Index
= 0; Index
< 256; Index
++) {
378 Sd
->mPTTable
[Index
] = CharC
;
381 SetMem ((VOID
*) &Sd
->mPTLen
, nn
* sizeof(UINT8
), 0);
388 while (Index
< Number
) {
390 CharC
= (UINT16
) (Sd
->mBitBuf
>> (BITBUFSIZ
- 3));
393 Mask
= 1U << (BITBUFSIZ
- 1 - 3);
394 while (Mask
& Sd
->mBitBuf
) {
400 FillBuf (Sd
, (UINT16
) ((CharC
< 7) ? 3 : CharC
- 3));
402 Sd
->mPTLen
[Index
++] = (UINT8
) CharC
;
404 if (Index
== Special
) {
405 CharC
= (UINT16
) GetBits (Sd
, 2);
406 while ((INT16
) (--CharC
) >= 0) {
407 Sd
->mPTLen
[Index
++] = 0;
412 SetMem ((VOID
*) &Sd
->mPTLen
[Index
], (nn
- Index
) * sizeof(UINT8
), 0);
413 return MakeTable (Sd
, nn
, Sd
->mPTLen
, 8, Sd
->mPTTable
);
424 Reads code lengths for Char&Len Set.
428 Sd - the global scratch data
439 Number
= (UINT16
) GetBits (Sd
, CBIT
);
442 CharC
= (UINT16
) GetBits (Sd
, CBIT
);
444 SetMem ((VOID
*)&Sd
->mCLen
, sizeof(UINT8
) * NC
, 0);
446 for (Index
= 0; Index
< 4096; Index
++) {
447 Sd
->mCTable
[Index
] = CharC
;
454 while (Index
< Number
) {
456 CharC
= Sd
->mPTTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 8)];
458 Mask
= 1U << (BITBUFSIZ
- 1 - 8);
462 if (Mask
& Sd
->mBitBuf
) {
463 CharC
= Sd
->mRight
[CharC
];
465 CharC
= Sd
->mLeft
[CharC
];
470 } while (CharC
>= NT
);
473 // Advance what we have read
475 FillBuf (Sd
, Sd
->mPTLen
[CharC
]);
481 } else if (CharC
== 1) {
482 CharC
= (UINT16
) (GetBits (Sd
, 4) + 3);
483 } else if (CharC
== 2) {
484 CharC
= (UINT16
) (GetBits (Sd
, CBIT
) + 20);
487 while ((INT16
) (--CharC
) >= 0) {
488 Sd
->mCLen
[Index
++] = 0;
493 Sd
->mCLen
[Index
++] = (UINT8
) (CharC
- 2);
498 SetMem ((VOID
*) &Sd
->mCLen
[Index
], sizeof(UINT8
) * (NC
- Index
), 0);
500 MakeTable (Sd
, NC
, Sd
->mCLen
, 12, Sd
->mCTable
);
513 Decode a character/length value.
517 Sd - The global scratch data.
528 if (Sd
->mBlockSize
== 0) {
530 // Starting a new block
532 Sd
->mBlockSize
= (UINT16
) GetBits (Sd
, 16);
533 Sd
->mBadTableFlag
= ReadPTLen (Sd
, NT
, TBIT
, 3);
534 if (Sd
->mBadTableFlag
!= 0) {
540 Sd
->mBadTableFlag
= ReadPTLen (Sd
, MAXNP
, Sd
->mPBit
, (UINT16
) (-1));
541 if (Sd
->mBadTableFlag
!= 0) {
547 Index2
= Sd
->mCTable
[Sd
->mBitBuf
>> (BITBUFSIZ
- 12)];
550 Mask
= 1U << (BITBUFSIZ
- 1 - 12);
553 if (Sd
->mBitBuf
& Mask
) {
554 Index2
= Sd
->mRight
[Index2
];
556 Index2
= Sd
->mLeft
[Index2
];
560 } while (Index2
>= NC
);
563 // Advance what we have read
565 FillBuf (Sd
, Sd
->mCLen
[Index2
]);
578 Decode the source data and put the resulting data into the destination buffer.
582 Sd - The global scratch data
592 BytesRemain
= (UINT16
) (-1);
597 CharC
= DecodeC (Sd
);
598 if (Sd
->mBadTableFlag
!= 0) {
604 // Process an Original character
606 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
609 Sd
->mDstBase
[Sd
->mOutBuf
++] = (UINT8
) CharC
;
616 CharC
= (UINT16
) (CharC
- (BIT8
- THRESHOLD
));
620 DataIdx
= Sd
->mOutBuf
- DecodeP (Sd
) - 1;
623 while ((INT16
) (BytesRemain
) >= 0) {
624 Sd
->mDstBase
[Sd
->mOutBuf
++] = Sd
->mDstBase
[DataIdx
++];
625 if (Sd
->mOutBuf
>= Sd
->mOrigSize
) {
641 OUT UINT32
*ScratchSize
647 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().
651 Source - The source buffer containing the compressed data.
652 SrcSize - The size of source buffer
653 DstSize - The size of destination buffer.
654 ScratchSize - The size of scratch buffer.
658 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
659 EFI_INVALID_PARAMETER - The source data is corrupted
665 *ScratchSize
= sizeof (SCRATCH_DATA
);
669 return EFI_INVALID_PARAMETER
;
672 *DstSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
680 IN OUT VOID
*Destination
,
682 IN OUT VOID
*Scratch
,
683 IN UINT32 ScratchSize
,
690 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().
694 Source - The source buffer containing the compressed data.
695 SrcSize - The size of source buffer
696 Destination - The destination buffer to store the decompressed data
697 DstSize - The size of destination buffer.
698 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
699 ScratchSize - The size of scratch buffer.
700 Version - The version of de/compression algorithm.
701 Version 1 for EFI 1.1 de/compression algorithm.
702 Version 2 for Tiano de/compression algorithm.
706 EFI_SUCCESS - Decompression is successfull
707 EFI_INVALID_PARAMETER - The source data is corrupted
718 Status
= EFI_SUCCESS
;
722 if (ScratchSize
< sizeof (SCRATCH_DATA
)) {
723 return EFI_INVALID_PARAMETER
;
726 Sd
= (SCRATCH_DATA
*) Scratch
;
729 return EFI_INVALID_PARAMETER
;
732 CompSize
= Src
[0] + (Src
[1] << 8) + (Src
[2] << 16) + (Src
[3] << 24);
733 OrigSize
= Src
[4] + (Src
[5] << 8) + (Src
[6] << 16) + (Src
[7] << 24);
736 // If compressed file size is 0, return
742 if (SrcSize
< CompSize
+ 8) {
743 return EFI_INVALID_PARAMETER
;
746 if (DstSize
!= OrigSize
) {
747 return EFI_INVALID_PARAMETER
;
752 SetMem ((VOID
*) Sd
, sizeof(SCRATCH_DATA
), 0);
755 // The length of the field 'Position Set Code Length Array Size' in Block Header.
756 // For EFI 1.1 de/compression algorithm(Version 1), mPBit = 4
757 // For Tiano de/compression algorithm(Version 2), mPBit = 5
770 // Currently, only have 2 versions
772 return EFI_INVALID_PARAMETER
;
777 Sd
->mCompSize
= CompSize
;
778 Sd
->mOrigSize
= OrigSize
;
781 // Fill the first BITBUFSIZ bits
783 FillBuf (Sd
, BITBUFSIZ
);
790 if (Sd
->mBadTableFlag
!= 0) {
792 // Something wrong with the source
794 Status
= EFI_INVALID_PARAMETER
;
806 OUT UINT32
*ScratchSize
812 The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.GetInfo().
816 This - The protocol instance pointer
817 Source - The source buffer containing the compressed data.
818 SrcSize - The size of source buffer
819 DstSize - The size of destination buffer.
820 ScratchSize - The size of scratch buffer.
824 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
825 EFI_INVALID_PARAMETER - The source data is corrupted
842 IN OUT VOID
*Destination
,
844 IN OUT VOID
*Scratch
,
845 IN UINT32 ScratchSize
851 The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.Decompress().
855 This - The protocol instance pointer
856 Source - The source buffer containing the compressed data.
857 SrcSize - The size of source buffer
858 Destination - The destination buffer to store the decompressed data
859 DstSize - The size of destination buffer.
860 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
861 ScratchSize - The size of scratch buffer.
865 EFI_SUCCESS - Decompression is successfull
866 EFI_INVALID_PARAMETER - The source data is corrupted
871 // For EFI 1.1 de/compression algorithm, the version is 1.
890 OUT UINT32
*ScratchSize
896 The implementation is same as that of EFI_TIANO_DECOMPRESS_PROTOCOL.GetInfo().
900 This - The protocol instance pointer
901 Source - The source buffer containing the compressed data.
902 SrcSize - The size of source buffer
903 DstSize - The size of destination buffer.
904 ScratchSize - The size of scratch buffer.
908 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
909 EFI_INVALID_PARAMETER - The source data is corrupted
926 IN OUT VOID
*Destination
,
928 IN OUT VOID
*Scratch
,
929 IN UINT32 ScratchSize
935 The implementation is same as that of EFI_TIANO_DECOMPRESS_PROTOCOL.Decompress().
939 This - The protocol instance pointer
940 Source - The source buffer containing the compressed data.
941 SrcSize - The size of source buffer
942 Destination - The destination buffer to store the decompressed data
943 DstSize - The size of destination buffer.
944 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
945 ScratchSize - The size of scratch buffer.
949 EFI_SUCCESS - Decompression is successfull
950 EFI_INVALID_PARAMETER - The source data is corrupted
955 // For Tiano de/compression algorithm, the version is 2.