]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Common/Decompress.c
a12fc595a7da5e17328def298debee32c46ed803
[mirror_edk2.git] / BaseTools / Source / C / Common / Decompress.c
1 /** @file
2
3 Copyright (c) 2004 - 2008, 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
8
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.
11
12 Module Name:
13
14 Decompress.c
15
16 Abstract:
17
18 Decompressor. Algorithm Ported from OPSD code (Decomp.asm)
19 for Efi and Tiano compress algorithm.
20
21 --*/
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "Decompress.h"
26
27 //
28 // Decompression algorithm begins here
29 //
30 #define BITBUFSIZ 32
31 #define MAXMATCH 256
32 #define THRESHOLD 3
33 #define CODE_BIT 16
34 #define BAD_TABLE - 1
35
36 //
37 // C: Char&Len Set; P: Position Set; T: exTra Set
38 //
39 #define NC (0xff + MAXMATCH + 2 - THRESHOLD)
40 #define CBIT 9
41 #define EFIPBIT 4
42 #define MAXPBIT 5
43 #define TBIT 5
44 #define MAXNP ((1U << MAXPBIT) - 1)
45 #define NT (CODE_BIT + 3)
46 #if NT > MAXNP
47 #define NPT NT
48 #else
49 #define NPT MAXNP
50 #endif
51
52 typedef struct {
53 UINT8 *mSrcBase; // Starting address of compressed data
54 UINT8 *mDstBase; // Starting address of decompressed data
55 UINT32 mOutBuf;
56 UINT32 mInBuf;
57
58 UINT16 mBitCount;
59 UINT32 mBitBuf;
60 UINT32 mSubBitBuf;
61 UINT16 mBlockSize;
62 UINT32 mCompSize;
63 UINT32 mOrigSize;
64
65 UINT16 mBadTableFlag;
66
67 UINT16 mLeft[2 * NC - 1];
68 UINT16 mRight[2 * NC - 1];
69 UINT8 mCLen[NC];
70 UINT8 mPTLen[NPT];
71 UINT16 mCTable[4096];
72 UINT16 mPTTable[256];
73 } SCRATCH_DATA;
74
75 STATIC UINT16 mPbit = EFIPBIT;
76
77 STATIC
78 VOID
79 FillBuf (
80 IN SCRATCH_DATA *Sd,
81 IN UINT16 NumOfBits
82 )
83 /*++
84
85 Routine Description:
86
87 Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
88
89 Arguments:
90
91 Sd - The global scratch data
92 NumOfBit - The number of bits to shift and read.
93
94 Returns: (VOID)
95
96 --*/
97 {
98 Sd->mBitBuf = (UINT32) (Sd->mBitBuf << NumOfBits);
99
100 while (NumOfBits > Sd->mBitCount) {
101
102 Sd->mBitBuf |= (UINT32) (Sd->mSubBitBuf << (NumOfBits = (UINT16) (NumOfBits - Sd->mBitCount)));
103
104 if (Sd->mCompSize > 0) {
105 //
106 // Get 1 byte into SubBitBuf
107 //
108 Sd->mCompSize--;
109 Sd->mSubBitBuf = 0;
110 Sd->mSubBitBuf = Sd->mSrcBase[Sd->mInBuf++];
111 Sd->mBitCount = 8;
112
113 } else {
114 //
115 // No more bits from the source, just pad zero bit.
116 //
117 Sd->mSubBitBuf = 0;
118 Sd->mBitCount = 8;
119
120 }
121 }
122
123 Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits);
124 Sd->mBitBuf |= Sd->mSubBitBuf >> Sd->mBitCount;
125 }
126
127 STATIC
128 UINT32
129 GetBits (
130 IN SCRATCH_DATA *Sd,
131 IN UINT16 NumOfBits
132 )
133 /*++
134
135 Routine Description:
136
137 Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent
138 NumOfBits of bits from source. Returns NumOfBits of bits that are
139 popped out.
140
141 Arguments:
142
143 Sd - The global scratch data.
144 NumOfBits - The number of bits to pop and read.
145
146 Returns:
147
148 The bits that are popped out.
149
150 --*/
151 {
152 UINT32 OutBits;
153
154 OutBits = (UINT32) (Sd->mBitBuf >> (BITBUFSIZ - NumOfBits));
155
156 FillBuf (Sd, NumOfBits);
157
158 return OutBits;
159 }
160
161 STATIC
162 UINT16
163 MakeTable (
164 IN SCRATCH_DATA *Sd,
165 IN UINT16 NumOfChar,
166 IN UINT8 *BitLen,
167 IN UINT16 TableBits,
168 OUT UINT16 *Table
169 )
170 /*++
171
172 Routine Description:
173
174 Creates Huffman Code mapping table according to code length array.
175
176 Arguments:
177
178 Sd - The global scratch data
179 NumOfChar - Number of symbols in the symbol set
180 BitLen - Code length array
181 TableBits - The width of the mapping table
182 Table - The table
183
184 Returns:
185
186 0 - OK.
187 BAD_TABLE - The table is corrupted.
188
189 --*/
190 {
191 UINT16 Count[17];
192 UINT16 Weight[17];
193 UINT16 Start[18];
194 UINT16 *Pointer;
195 UINT16 Index3;
196 UINT16 Index;
197 UINT16 Len;
198 UINT16 Char;
199 UINT16 JuBits;
200 UINT16 Avail;
201 UINT16 NextCode;
202 UINT16 Mask;
203
204 for (Index = 1; Index <= 16; Index++) {
205 Count[Index] = 0;
206 }
207
208 for (Index = 0; Index < NumOfChar; Index++) {
209 Count[BitLen[Index]]++;
210 }
211
212 Start[1] = 0;
213
214 for (Index = 1; Index <= 16; Index++) {
215 Start[Index + 1] = (UINT16) (Start[Index] + (Count[Index] << (16 - Index)));
216 }
217
218 if (Start[17] != 0) {
219 /*(1U << 16)*/
220 return (UINT16) BAD_TABLE;
221 }
222
223 JuBits = (UINT16) (16 - TableBits);
224
225 for (Index = 1; Index <= TableBits; Index++) {
226 Start[Index] >>= JuBits;
227 Weight[Index] = (UINT16) (1U << (TableBits - Index));
228 }
229
230 while (Index <= 16) {
231 Weight[Index] = (UINT16) (1U << (16 - Index));
232 Index++;
233 }
234
235 Index = (UINT16) (Start[TableBits + 1] >> JuBits);
236
237 if (Index != 0) {
238 Index3 = (UINT16) (1U << TableBits);
239 while (Index != Index3) {
240 Table[Index++] = 0;
241 }
242 }
243
244 Avail = NumOfChar;
245 Mask = (UINT16) (1U << (15 - TableBits));
246
247 for (Char = 0; Char < NumOfChar; Char++) {
248
249 Len = BitLen[Char];
250 if (Len == 0) {
251 continue;
252 }
253
254 NextCode = (UINT16) (Start[Len] + Weight[Len]);
255
256 if (Len <= TableBits) {
257
258 for (Index = Start[Len]; Index < NextCode; Index++) {
259 Table[Index] = Char;
260 }
261
262 } else {
263
264 Index3 = Start[Len];
265 Pointer = &Table[Index3 >> JuBits];
266 Index = (UINT16) (Len - TableBits);
267
268 while (Index != 0) {
269 if (*Pointer == 0) {
270 Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;
271 *Pointer = Avail++;
272 }
273
274 if (Index3 & Mask) {
275 Pointer = &Sd->mRight[*Pointer];
276 } else {
277 Pointer = &Sd->mLeft[*Pointer];
278 }
279
280 Index3 <<= 1;
281 Index--;
282 }
283
284 *Pointer = Char;
285
286 }
287
288 Start[Len] = NextCode;
289 }
290 //
291 // Succeeds
292 //
293 return 0;
294 }
295
296 STATIC
297 UINT32
298 DecodeP (
299 IN SCRATCH_DATA *Sd
300 )
301 /*++
302
303 Routine Description:
304
305 Decodes a position value.
306
307 Arguments:
308
309 Sd - the global scratch data
310
311 Returns:
312
313 The position value decoded.
314
315 --*/
316 {
317 UINT16 Val;
318 UINT32 Mask;
319 UINT32 Pos;
320
321 Val = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];
322
323 if (Val >= MAXNP) {
324 Mask = 1U << (BITBUFSIZ - 1 - 8);
325
326 do {
327
328 if (Sd->mBitBuf & Mask) {
329 Val = Sd->mRight[Val];
330 } else {
331 Val = Sd->mLeft[Val];
332 }
333
334 Mask >>= 1;
335 } while (Val >= MAXNP);
336 }
337 //
338 // Advance what we have read
339 //
340 FillBuf (Sd, Sd->mPTLen[Val]);
341
342 Pos = Val;
343 if (Val > 1) {
344 Pos = (UINT32) ((1U << (Val - 1)) + GetBits (Sd, (UINT16) (Val - 1)));
345 }
346
347 return Pos;
348 }
349
350 STATIC
351 UINT16
352 ReadPTLen (
353 IN SCRATCH_DATA *Sd,
354 IN UINT16 nn,
355 IN UINT16 nbit,
356 IN UINT16 Special
357 )
358 /*++
359
360 Routine Description:
361
362 Reads code lengths for the Extra Set or the Position Set
363
364 Arguments:
365
366 Sd - The global scratch data
367 nn - Number of symbols
368 nbit - Number of bits needed to represent nn
369 Special - The special symbol that needs to be taken care of
370
371 Returns:
372
373 0 - OK.
374 BAD_TABLE - Table is corrupted.
375
376 --*/
377 {
378 UINT16 Number;
379 UINT16 CharC;
380 UINT16 Index;
381 UINT32 Mask;
382
383 Number = (UINT16) GetBits (Sd, nbit);
384
385 if (Number == 0) {
386 CharC = (UINT16) GetBits (Sd, nbit);
387
388 for (Index = 0; Index < 256; Index++) {
389 Sd->mPTTable[Index] = CharC;
390 }
391
392 for (Index = 0; Index < nn; Index++) {
393 Sd->mPTLen[Index] = 0;
394 }
395
396 return 0;
397 }
398
399 Index = 0;
400
401 while (Index < Number) {
402
403 CharC = (UINT16) (Sd->mBitBuf >> (BITBUFSIZ - 3));
404
405 if (CharC == 7) {
406 Mask = 1U << (BITBUFSIZ - 1 - 3);
407 while (Mask & Sd->mBitBuf) {
408 Mask >>= 1;
409 CharC += 1;
410 }
411 }
412
413 FillBuf (Sd, (UINT16) ((CharC < 7) ? 3 : CharC - 3));
414
415 Sd->mPTLen[Index++] = (UINT8) CharC;
416
417 if (Index == Special) {
418 CharC = (UINT16) GetBits (Sd, 2);
419 CharC--;
420 while ((INT16) (CharC) >= 0) {
421 Sd->mPTLen[Index++] = 0;
422 CharC--;
423 }
424 }
425 }
426
427 while (Index < nn) {
428 Sd->mPTLen[Index++] = 0;
429 }
430
431 return MakeTable (Sd, nn, Sd->mPTLen, 8, Sd->mPTTable);
432 }
433
434 STATIC
435 VOID
436 ReadCLen (
437 SCRATCH_DATA *Sd
438 )
439 /*++
440
441 Routine Description:
442
443 Reads code lengths for Char&Len Set.
444
445 Arguments:
446
447 Sd - the global scratch data
448
449 Returns: (VOID)
450
451 --*/
452 {
453 UINT16 Number;
454 UINT16 CharC;
455 UINT16 Index;
456 UINT32 Mask;
457
458 Number = (UINT16) GetBits (Sd, CBIT);
459
460 if (Number == 0) {
461 CharC = (UINT16) GetBits (Sd, CBIT);
462
463 for (Index = 0; Index < NC; Index++) {
464 Sd->mCLen[Index] = 0;
465 }
466
467 for (Index = 0; Index < 4096; Index++) {
468 Sd->mCTable[Index] = CharC;
469 }
470
471 return ;
472 }
473
474 Index = 0;
475 while (Index < Number) {
476
477 CharC = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];
478 if (CharC >= NT) {
479 Mask = 1U << (BITBUFSIZ - 1 - 8);
480
481 do {
482
483 if (Mask & Sd->mBitBuf) {
484 CharC = Sd->mRight[CharC];
485 } else {
486 CharC = Sd->mLeft[CharC];
487 }
488
489 Mask >>= 1;
490
491 } while (CharC >= NT);
492 }
493 //
494 // Advance what we have read
495 //
496 FillBuf (Sd, Sd->mPTLen[CharC]);
497
498 if (CharC <= 2) {
499
500 if (CharC == 0) {
501 CharC = 1;
502 } else if (CharC == 1) {
503 CharC = (UINT16) (GetBits (Sd, 4) + 3);
504 } else if (CharC == 2) {
505 CharC = (UINT16) (GetBits (Sd, CBIT) + 20);
506 }
507
508 CharC--;
509 while ((INT16) (CharC) >= 0) {
510 Sd->mCLen[Index++] = 0;
511 CharC--;
512 }
513
514 } else {
515
516 Sd->mCLen[Index++] = (UINT8) (CharC - 2);
517
518 }
519 }
520
521 while (Index < NC) {
522 Sd->mCLen[Index++] = 0;
523 }
524
525 MakeTable (Sd, NC, Sd->mCLen, 12, Sd->mCTable);
526
527 return ;
528 }
529
530 STATIC
531 UINT16
532 DecodeC (
533 SCRATCH_DATA *Sd
534 )
535 /*++
536
537 Routine Description:
538
539 Decode a character/length value.
540
541 Arguments:
542
543 Sd - The global scratch data.
544
545 Returns:
546
547 The value decoded.
548
549 --*/
550 {
551 UINT16 Index2;
552 UINT32 Mask;
553
554 if (Sd->mBlockSize == 0) {
555 //
556 // Starting a new block
557 //
558 Sd->mBlockSize = (UINT16) GetBits (Sd, 16);
559 Sd->mBadTableFlag = ReadPTLen (Sd, NT, TBIT, 3);
560 if (Sd->mBadTableFlag != 0) {
561 return 0;
562 }
563
564 ReadCLen (Sd);
565
566 Sd->mBadTableFlag = ReadPTLen (Sd, MAXNP, mPbit, (UINT16) (-1));
567 if (Sd->mBadTableFlag != 0) {
568 return 0;
569 }
570 }
571
572 Sd->mBlockSize--;
573 Index2 = Sd->mCTable[Sd->mBitBuf >> (BITBUFSIZ - 12)];
574
575 if (Index2 >= NC) {
576 Mask = 1U << (BITBUFSIZ - 1 - 12);
577
578 do {
579 if (Sd->mBitBuf & Mask) {
580 Index2 = Sd->mRight[Index2];
581 } else {
582 Index2 = Sd->mLeft[Index2];
583 }
584
585 Mask >>= 1;
586 } while (Index2 >= NC);
587 }
588 //
589 // Advance what we have read
590 //
591 FillBuf (Sd, Sd->mCLen[Index2]);
592
593 return Index2;
594 }
595
596 STATIC
597 VOID
598 Decode (
599 SCRATCH_DATA *Sd
600 )
601 /*++
602
603 Routine Description:
604
605 Decode the source data and put the resulting data into the destination buffer.
606
607 Arguments:
608
609 Sd - The global scratch data
610
611 Returns: (VOID)
612
613 --*/
614 {
615 UINT16 BytesRemain;
616 UINT32 DataIdx;
617 UINT16 CharC;
618
619 BytesRemain = (UINT16) (-1);
620
621 DataIdx = 0;
622
623 for (;;) {
624 CharC = DecodeC (Sd);
625 if (Sd->mBadTableFlag != 0) {
626 return ;
627 }
628
629 if (CharC < 256) {
630 //
631 // Process an Original character
632 //
633 Sd->mDstBase[Sd->mOutBuf++] = (UINT8) CharC;
634 if (Sd->mOutBuf >= Sd->mOrigSize) {
635 return ;
636 }
637
638 } else {
639 //
640 // Process a Pointer
641 //
642 CharC = (UINT16) (CharC - (UINT8_MAX + 1 - THRESHOLD));
643
644 BytesRemain = CharC;
645
646 DataIdx = Sd->mOutBuf - DecodeP (Sd) - 1;
647
648 BytesRemain--;
649 while ((INT16) (BytesRemain) >= 0) {
650 Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];
651 if (Sd->mOutBuf >= Sd->mOrigSize) {
652 return ;
653 }
654
655 BytesRemain--;
656 }
657 }
658 }
659
660 return ;
661 }
662
663 EFI_STATUS
664 GetInfo (
665 IN VOID *Source,
666 IN UINT32 SrcSize,
667 OUT UINT32 *DstSize,
668 OUT UINT32 *ScratchSize
669 )
670 /*++
671
672 Routine Description:
673
674 The implementation of EFI_DECOMPRESS_PROTOCOL.GetInfo().
675
676 Arguments:
677
678 Source - The source buffer containing the compressed data.
679 SrcSize - The size of source buffer
680 DstSize - The size of destination buffer.
681 ScratchSize - The size of scratch buffer.
682
683 Returns:
684
685 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
686 EFI_INVALID_PARAMETER - The source data is corrupted
687
688 --*/
689 {
690 UINT8 *Src;
691
692 *ScratchSize = sizeof (SCRATCH_DATA);
693
694 Src = Source;
695 if (SrcSize < 8) {
696 return EFI_INVALID_PARAMETER;
697 }
698
699 *DstSize = Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24);
700 return EFI_SUCCESS;
701 }
702
703 EFI_STATUS
704 Decompress (
705 IN VOID *Source,
706 IN UINT32 SrcSize,
707 IN OUT VOID *Destination,
708 IN UINT32 DstSize,
709 IN OUT VOID *Scratch,
710 IN UINT32 ScratchSize
711 )
712 /*++
713
714 Routine Description:
715
716 The implementation Efi and Tiano Decompress().
717
718 Arguments:
719
720 Source - The source buffer containing the compressed data.
721 SrcSize - The size of source buffer
722 Destination - The destination buffer to store the decompressed data
723 DstSize - The size of destination buffer.
724 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
725 ScratchSize - The size of scratch buffer.
726
727 Returns:
728
729 EFI_SUCCESS - Decompression is successfull
730 EFI_INVALID_PARAMETER - The source data is corrupted
731
732 --*/
733 {
734 UINT32 Index;
735 UINT32 CompSize;
736 UINT32 OrigSize;
737 EFI_STATUS Status;
738 SCRATCH_DATA *Sd;
739 UINT8 *Src;
740 UINT8 *Dst;
741
742 Status = EFI_SUCCESS;
743 Src = Source;
744 Dst = Destination;
745
746 if (ScratchSize < sizeof (SCRATCH_DATA)) {
747 return EFI_INVALID_PARAMETER;
748 }
749
750 Sd = (SCRATCH_DATA *) Scratch;
751
752 if (SrcSize < 8) {
753 return EFI_INVALID_PARAMETER;
754 }
755
756 CompSize = Src[0] + (Src[1] << 8) + (Src[2] << 16) + (Src[3] << 24);
757 OrigSize = Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24);
758
759 if (SrcSize < CompSize + 8) {
760 return EFI_INVALID_PARAMETER;
761 }
762
763 if (DstSize != OrigSize) {
764 return EFI_INVALID_PARAMETER;
765 }
766
767 Src = Src + 8;
768
769 for (Index = 0; Index < sizeof (SCRATCH_DATA); Index++) {
770 ((UINT8 *) Sd)[Index] = 0;
771 }
772
773 Sd->mSrcBase = Src;
774 Sd->mDstBase = Dst;
775 Sd->mCompSize = CompSize;
776 Sd->mOrigSize = OrigSize;
777
778 //
779 // Fill the first BITBUFSIZ bits
780 //
781 FillBuf (Sd, BITBUFSIZ);
782
783 //
784 // Decompress it
785 //
786 Decode (Sd);
787
788 if (Sd->mBadTableFlag != 0) {
789 //
790 // Something wrong with the source
791 //
792 Status = EFI_INVALID_PARAMETER;
793 }
794
795 return Status;
796 }
797
798 EFI_STATUS
799 EfiGetInfo (
800 IN VOID *Source,
801 IN UINT32 SrcSize,
802 OUT UINT32 *DstSize,
803 OUT UINT32 *ScratchSize
804 )
805 /*++
806
807 Routine Description:
808
809 The implementation Efi Decompress GetInfo().
810
811 Arguments:
812
813 Source - The source buffer containing the compressed data.
814 SrcSize - The size of source buffer
815 DstSize - The size of destination buffer.
816 ScratchSize - The size of scratch buffer.
817
818 Returns:
819
820 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
821 EFI_INVALID_PARAMETER - The source data is corrupted
822
823 --*/
824 {
825 return GetInfo (Source, SrcSize, DstSize, ScratchSize);
826 }
827
828 EFI_STATUS
829 TianoGetInfo (
830 IN VOID *Source,
831 IN UINT32 SrcSize,
832 OUT UINT32 *DstSize,
833 OUT UINT32 *ScratchSize
834 )
835 /*++
836
837 Routine Description:
838
839 The implementation Tiano Decompress GetInfo().
840
841 Arguments:
842
843 Source - The source buffer containing the compressed data.
844 SrcSize - The size of source buffer
845 DstSize - The size of destination buffer.
846 ScratchSize - The size of scratch buffer.
847
848 Returns:
849
850 EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
851 EFI_INVALID_PARAMETER - The source data is corrupted
852
853 --*/
854 {
855 return GetInfo (Source, SrcSize, DstSize, ScratchSize);
856 }
857
858 EFI_STATUS
859 EfiDecompress (
860 IN VOID *Source,
861 IN UINT32 SrcSize,
862 IN OUT VOID *Destination,
863 IN UINT32 DstSize,
864 IN OUT VOID *Scratch,
865 IN UINT32 ScratchSize
866 )
867 /*++
868
869 Routine Description:
870
871 The implementation of Efi Decompress().
872
873 Arguments:
874
875 Source - The source buffer containing the compressed data.
876 SrcSize - The size of source buffer
877 Destination - The destination buffer to store the decompressed data
878 DstSize - The size of destination buffer.
879 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
880 ScratchSize - The size of scratch buffer.
881
882 Returns:
883
884 EFI_SUCCESS - Decompression is successfull
885 EFI_INVALID_PARAMETER - The source data is corrupted
886
887 --*/
888 {
889 mPbit = EFIPBIT;
890 return Decompress (Source, SrcSize, Destination, DstSize, Scratch, ScratchSize);
891 }
892
893 EFI_STATUS
894 TianoDecompress (
895 IN VOID *Source,
896 IN UINT32 SrcSize,
897 IN OUT VOID *Destination,
898 IN UINT32 DstSize,
899 IN OUT VOID *Scratch,
900 IN UINT32 ScratchSize
901 )
902 /*++
903
904 Routine Description:
905
906 The implementation of Tiano Decompress().
907
908 Arguments:
909
910 Source - The source buffer containing the compressed data.
911 SrcSize - The size of source buffer
912 Destination - The destination buffer to store the decompressed data
913 DstSize - The size of destination buffer.
914 Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
915 ScratchSize - The size of scratch buffer.
916
917 Returns:
918
919 EFI_SUCCESS - Decompression is successfull
920 EFI_INVALID_PARAMETER - The source data is corrupted
921
922 --*/
923 {
924 mPbit = MAXPBIT;
925 return Decompress (Source, SrcSize, Destination, DstSize, Scratch, ScratchSize);
926 }
927
928 EFI_STATUS
929 Extract (
930 IN VOID *Source,
931 IN UINT32 SrcSize,
932 OUT VOID **Destination,
933 OUT UINT32 *DstSize,
934 IN UINTN Algorithm
935 )
936 {
937 VOID *Scratch;
938 UINT32 ScratchSize;
939 EFI_STATUS Status;
940
941 Status = EFI_SUCCESS;
942 switch (Algorithm) {
943 case 0:
944 *Destination = (VOID *)malloc(SrcSize);
945 if (*Destination != NULL) {
946 memcpy(*Destination, Source, SrcSize);
947 } else {
948 Status = EFI_OUT_OF_RESOURCES;
949 }
950 break;
951 case 1:
952 Status = EfiGetInfo(Source, SrcSize, DstSize, &ScratchSize);
953 if (Status == EFI_SUCCESS) {
954 Scratch = (VOID *)malloc(ScratchSize);
955 *Destination = (VOID *)malloc(*DstSize);
956 if (Scratch != NULL && *Destination != NULL) {
957 Status = EfiDecompress(Source, SrcSize, *Destination, *DstSize, Scratch, ScratchSize);
958 } else {
959 Status = EFI_OUT_OF_RESOURCES;
960 }
961 }
962 break;
963 case 2:
964 Status = TianoGetInfo(Source, SrcSize, DstSize, &ScratchSize);
965 if (Status == EFI_SUCCESS) {
966 Scratch = (VOID *)malloc(ScratchSize);
967 *Destination = (VOID *)malloc(*DstSize);
968 if (Scratch != NULL && *Destination != NULL) {
969 Status = TianoDecompress(Source, SrcSize, *Destination, *DstSize, Scratch, ScratchSize);
970 } else {
971 Status = EFI_OUT_OF_RESOURCES;
972 }
973 }
974 break;
975 default:
976 Status = EFI_INVALID_PARAMETER;
977 }
978
979 return Status;
980 }
981
982