1 | /** @file
|
---|
2 | UEFI Decompress Library implementation refer to UEFI specification.
|
---|
3 |
|
---|
4 | Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
|
---|
5 | Portions copyright (c) 2008 - 2009, Apple Inc. 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.
|
---|
10 |
|
---|
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.
|
---|
13 |
|
---|
14 | **/
|
---|
15 |
|
---|
16 |
|
---|
17 | #include <Base.h>
|
---|
18 | #include <Library/BaseLib.h>
|
---|
19 | #include <Library/DebugLib.h>
|
---|
20 | #include <Library/BaseMemoryLib.h>
|
---|
21 | #include <Library/UefiDecompressLib.h>
|
---|
22 |
|
---|
23 | #include "BaseUefiDecompressLibInternals.h"
|
---|
24 |
|
---|
25 | /**
|
---|
26 | Read NumOfBit of bits from source into mBitBuf.
|
---|
27 |
|
---|
28 | Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
|
---|
29 |
|
---|
30 | @param Sd The global scratch data.
|
---|
31 | @param NumOfBits The number of bits to shift and read.
|
---|
32 |
|
---|
33 | **/
|
---|
34 | VOID
|
---|
35 | FillBuf (
|
---|
36 | IN SCRATCH_DATA *Sd,
|
---|
37 | IN UINT16 NumOfBits
|
---|
38 | )
|
---|
39 | {
|
---|
40 | //
|
---|
41 | // Left shift NumOfBits of bits in advance
|
---|
42 | //
|
---|
43 | Sd->mBitBuf = (UINT32) LShiftU64 (((UINT64)Sd->mBitBuf), NumOfBits);
|
---|
44 |
|
---|
45 | //
|
---|
46 | // Copy data needed in bytes into mSbuBitBuf
|
---|
47 | //
|
---|
48 | while (NumOfBits > Sd->mBitCount) {
|
---|
49 | NumOfBits = (UINT16) (NumOfBits - Sd->mBitCount);
|
---|
50 | Sd->mBitBuf |= (UINT32) LShiftU64 (((UINT64)Sd->mSubBitBuf), NumOfBits);
|
---|
51 |
|
---|
52 | if (Sd->mCompSize > 0) {
|
---|
53 | //
|
---|
54 | // Get 1 byte into SubBitBuf
|
---|
55 | //
|
---|
56 | Sd->mCompSize--;
|
---|
57 | Sd->mSubBitBuf = Sd->mSrcBase[Sd->mInBuf++];
|
---|
58 | Sd->mBitCount = 8;
|
---|
59 |
|
---|
60 | } else {
|
---|
61 | //
|
---|
62 | // No more bits from the source, just pad zero bit.
|
---|
63 | //
|
---|
64 | Sd->mSubBitBuf = 0;
|
---|
65 | Sd->mBitCount = 8;
|
---|
66 |
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | //
|
---|
71 | // Calculate additional bit count read to update mBitCount
|
---|
72 | //
|
---|
73 | Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits);
|
---|
74 |
|
---|
75 | //
|
---|
76 | // Copy NumOfBits of bits from mSubBitBuf into mBitBuf
|
---|
77 | //
|
---|
78 | Sd->mBitBuf |= Sd->mSubBitBuf >> Sd->mBitCount;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | Get NumOfBits of bits out from mBitBuf.
|
---|
83 |
|
---|
84 | Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent
|
---|
85 | NumOfBits of bits from source. Returns NumOfBits of bits that are
|
---|
86 | popped out.
|
---|
87 |
|
---|
88 | @param Sd The global scratch data.
|
---|
89 | @param NumOfBits The number of bits to pop and read.
|
---|
90 |
|
---|
91 | @return The bits that are popped out.
|
---|
92 |
|
---|
93 | **/
|
---|
94 | UINT32
|
---|
95 | GetBits (
|
---|
96 | IN SCRATCH_DATA *Sd,
|
---|
97 | IN UINT16 NumOfBits
|
---|
98 | )
|
---|
99 | {
|
---|
100 | UINT32 OutBits;
|
---|
101 |
|
---|
102 | //
|
---|
103 | // Pop NumOfBits of Bits from Left
|
---|
104 | //
|
---|
105 | OutBits = (UINT32) (Sd->mBitBuf >> (BITBUFSIZ - NumOfBits));
|
---|
106 |
|
---|
107 | //
|
---|
108 | // Fill up mBitBuf from source
|
---|
109 | //
|
---|
110 | FillBuf (Sd, NumOfBits);
|
---|
111 |
|
---|
112 | return OutBits;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | Creates Huffman Code mapping table according to code length array.
|
---|
117 |
|
---|
118 | Creates Huffman Code mapping table for Extra Set, Char&Len Set
|
---|
119 | and Position Set according to code length array.
|
---|
120 | If TableBits > 16, then ASSERT ().
|
---|
121 |
|
---|
122 | @param Sd The global scratch data.
|
---|
123 | @param NumOfChar The number of symbols in the symbol set.
|
---|
124 | @param BitLen Code length array.
|
---|
125 | @param TableBits The width of the mapping table.
|
---|
126 | @param Table The table to be created.
|
---|
127 |
|
---|
128 | @retval 0 OK.
|
---|
129 | @retval BAD_TABLE The table is corrupted.
|
---|
130 |
|
---|
131 | **/
|
---|
132 | UINT16
|
---|
133 | MakeTable (
|
---|
134 | IN SCRATCH_DATA *Sd,
|
---|
135 | IN UINT16 NumOfChar,
|
---|
136 | IN UINT8 *BitLen,
|
---|
137 | IN UINT16 TableBits,
|
---|
138 | OUT UINT16 *Table
|
---|
139 | )
|
---|
140 | {
|
---|
141 | UINT16 Count[17];
|
---|
142 | UINT16 Weight[17];
|
---|
143 | UINT16 Start[18];
|
---|
144 | UINT16 *Pointer;
|
---|
145 | UINT16 Index3;
|
---|
146 | UINT16 Index;
|
---|
147 | UINT16 Len;
|
---|
148 | UINT16 Char;
|
---|
149 | UINT16 JuBits;
|
---|
150 | UINT16 Avail;
|
---|
151 | UINT16 NextCode;
|
---|
152 | UINT16 Mask;
|
---|
153 | UINT16 WordOfStart;
|
---|
154 | UINT16 WordOfCount;
|
---|
155 | UINT16 MaxTableLength;
|
---|
156 |
|
---|
157 | //
|
---|
158 | // The maximum mapping table width supported by this internal
|
---|
159 | // working function is 16.
|
---|
160 | //
|
---|
161 | ASSERT (TableBits <= 16);
|
---|
162 |
|
---|
163 | for (Index = 0; Index <= 16; Index++) {
|
---|
164 | Count[Index] = 0;
|
---|
165 | }
|
---|
166 |
|
---|
167 | for (Index = 0; Index < NumOfChar; Index++) {
|
---|
168 | if (BitLen[Index] > 16) {
|
---|
169 | return (UINT16) BAD_TABLE;
|
---|
170 | }
|
---|
171 | Count[BitLen[Index]]++;
|
---|
172 | }
|
---|
173 |
|
---|
174 | Start[0] = 0;
|
---|
175 | Start[1] = 0;
|
---|
176 |
|
---|
177 | for (Index = 1; Index <= 16; Index++) {
|
---|
178 | WordOfStart = Start[Index];
|
---|
179 | WordOfCount = Count[Index];
|
---|
180 | Start[Index + 1] = (UINT16) (WordOfStart + (WordOfCount << (16 - Index)));
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (Start[17] != 0) {
|
---|
184 | /*(1U << 16)*/
|
---|
185 | return (UINT16) BAD_TABLE;
|
---|
186 | }
|
---|
187 |
|
---|
188 | JuBits = (UINT16) (16 - TableBits);
|
---|
189 |
|
---|
190 | Weight[0] = 0;
|
---|
191 | for (Index = 1; Index <= TableBits; Index++) {
|
---|
192 | Start[Index] >>= JuBits;
|
---|
193 | Weight[Index] = (UINT16) (1U << (TableBits - Index));
|
---|
194 | }
|
---|
195 |
|
---|
196 | while (Index <= 16) {
|
---|
197 | Weight[Index] = (UINT16) (1U << (16 - Index));
|
---|
198 | Index++;
|
---|
199 | }
|
---|
200 |
|
---|
201 | Index = (UINT16) (Start[TableBits + 1] >> JuBits);
|
---|
202 |
|
---|
203 | if (Index != 0) {
|
---|
204 | Index3 = (UINT16) (1U << TableBits);
|
---|
205 | if (Index < Index3) {
|
---|
206 | SetMem16 (Table + Index, (Index3 - Index) * sizeof (*Table), 0);
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | Avail = NumOfChar;
|
---|
211 | Mask = (UINT16) (1U << (15 - TableBits));
|
---|
212 | MaxTableLength = (UINT16) (1U << TableBits);
|
---|
213 |
|
---|
214 | for (Char = 0; Char < NumOfChar; Char++) {
|
---|
215 |
|
---|
216 | Len = BitLen[Char];
|
---|
217 | if (Len == 0 || Len >= 17) {
|
---|
218 | continue;
|
---|
219 | }
|
---|
220 |
|
---|
221 | NextCode = (UINT16) (Start[Len] + Weight[Len]);
|
---|
222 |
|
---|
223 | if (Len <= TableBits) {
|
---|
224 |
|
---|
225 | for (Index = Start[Len]; Index < NextCode; Index++) {
|
---|
226 | if (Index >= MaxTableLength) {
|
---|
227 | return (UINT16) BAD_TABLE;
|
---|
228 | }
|
---|
229 | Table[Index] = Char;
|
---|
230 | }
|
---|
231 |
|
---|
232 | } else {
|
---|
233 |
|
---|
234 | Index3 = Start[Len];
|
---|
235 | Pointer = &Table[Index3 >> JuBits];
|
---|
236 | Index = (UINT16) (Len - TableBits);
|
---|
237 |
|
---|
238 | while (Index != 0) {
|
---|
239 | if (*Pointer == 0 && Avail < (2 * NC - 1)) {
|
---|
240 | Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;
|
---|
241 | *Pointer = Avail++;
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (*Pointer < (2 * NC - 1)) {
|
---|
245 | if ((Index3 & Mask) != 0) {
|
---|
246 | Pointer = &Sd->mRight[*Pointer];
|
---|
247 | } else {
|
---|
248 | Pointer = &Sd->mLeft[*Pointer];
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | Index3 <<= 1;
|
---|
253 | Index--;
|
---|
254 | }
|
---|
255 |
|
---|
256 | *Pointer = Char;
|
---|
257 |
|
---|
258 | }
|
---|
259 |
|
---|
260 | Start[Len] = NextCode;
|
---|
261 | }
|
---|
262 | //
|
---|
263 | // Succeeds
|
---|
264 | //
|
---|
265 | return 0;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | Decodes a position value.
|
---|
270 |
|
---|
271 | Get a position value according to Position Huffman Table.
|
---|
272 |
|
---|
273 | @param Sd The global scratch data.
|
---|
274 |
|
---|
275 | @return The position value decoded.
|
---|
276 |
|
---|
277 | **/
|
---|
278 | UINT32
|
---|
279 | DecodeP (
|
---|
280 | IN SCRATCH_DATA *Sd
|
---|
281 | )
|
---|
282 | {
|
---|
283 | UINT16 Val;
|
---|
284 | UINT32 Mask;
|
---|
285 | UINT32 Pos;
|
---|
286 |
|
---|
287 | Val = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];
|
---|
288 |
|
---|
289 | if (Val >= MAXNP) {
|
---|
290 | Mask = 1U << (BITBUFSIZ - 1 - 8);
|
---|
291 |
|
---|
292 | do {
|
---|
293 |
|
---|
294 | if ((Sd->mBitBuf & Mask) != 0) {
|
---|
295 | Val = Sd->mRight[Val];
|
---|
296 | } else {
|
---|
297 | Val = Sd->mLeft[Val];
|
---|
298 | }
|
---|
299 |
|
---|
300 | Mask >>= 1;
|
---|
301 | } while (Val >= MAXNP);
|
---|
302 | }
|
---|
303 | //
|
---|
304 | // Advance what we have read
|
---|
305 | //
|
---|
306 | FillBuf (Sd, Sd->mPTLen[Val]);
|
---|
307 |
|
---|
308 | Pos = Val;
|
---|
309 | if (Val > 1) {
|
---|
310 | Pos = (UINT32) ((1U << (Val - 1)) + GetBits (Sd, (UINT16) (Val - 1)));
|
---|
311 | }
|
---|
312 |
|
---|
313 | return Pos;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | Reads code lengths for the Extra Set or the Position Set.
|
---|
318 |
|
---|
319 | Read in the Extra Set or Position Set Length Array, then
|
---|
320 | generate the Huffman code mapping for them.
|
---|
321 |
|
---|
322 | @param Sd The global scratch data.
|
---|
323 | @param nn The number of symbols.
|
---|
324 | @param nbit The number of bits needed to represent nn.
|
---|
325 | @param Special The special symbol that needs to be taken care of.
|
---|
326 |
|
---|
327 | @retval 0 OK.
|
---|
328 | @retval BAD_TABLE Table is corrupted.
|
---|
329 |
|
---|
330 | **/
|
---|
331 | UINT16
|
---|
332 | ReadPTLen (
|
---|
333 | IN SCRATCH_DATA *Sd,
|
---|
334 | IN UINT16 nn,
|
---|
335 | IN UINT16 nbit,
|
---|
336 | IN UINT16 Special
|
---|
337 | )
|
---|
338 | {
|
---|
339 | UINT16 Number;
|
---|
340 | UINT16 CharC;
|
---|
341 | UINT16 Index;
|
---|
342 | UINT32 Mask;
|
---|
343 |
|
---|
344 | ASSERT (nn <= NPT);
|
---|
345 | //
|
---|
346 | // Read Extra Set Code Length Array size
|
---|
347 | //
|
---|
348 | Number = (UINT16) GetBits (Sd, nbit);
|
---|
349 |
|
---|
350 | if (Number == 0) {
|
---|
351 | //
|
---|
352 | // This represents only Huffman code used
|
---|
353 | //
|
---|
354 | CharC = (UINT16) GetBits (Sd, nbit);
|
---|
355 |
|
---|
356 | SetMem16 (&Sd->mPTTable[0] , sizeof (Sd->mPTTable), CharC);
|
---|
357 |
|
---|
358 | SetMem (Sd->mPTLen, nn, 0);
|
---|
359 |
|
---|
360 | return 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 | Index = 0;
|
---|
364 |
|
---|
365 | while (Index < Number && Index < NPT) {
|
---|
366 |
|
---|
367 | CharC = (UINT16) (Sd->mBitBuf >> (BITBUFSIZ - 3));
|
---|
368 |
|
---|
369 | //
|
---|
370 | // If a code length is less than 7, then it is encoded as a 3-bit
|
---|
371 | // value. Or it is encoded as a series of "1"s followed by a
|
---|
372 | // terminating "0". The number of "1"s = Code length - 4.
|
---|
373 | //
|
---|
374 | if (CharC == 7) {
|
---|
375 | Mask = 1U << (BITBUFSIZ - 1 - 3);
|
---|
376 | while (Mask & Sd->mBitBuf) {
|
---|
377 | Mask >>= 1;
|
---|
378 | CharC += 1;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | FillBuf (Sd, (UINT16) ((CharC < 7) ? 3 : CharC - 3));
|
---|
383 |
|
---|
384 | Sd->mPTLen[Index++] = (UINT8) CharC;
|
---|
385 |
|
---|
386 | //
|
---|
387 | // For Code&Len Set,
|
---|
388 | // After the third length of the code length concatenation,
|
---|
389 | // a 2-bit value is used to indicated the number of consecutive
|
---|
390 | // zero lengths after the third length.
|
---|
391 | //
|
---|
392 | if (Index == Special) {
|
---|
393 | CharC = (UINT16) GetBits (Sd, 2);
|
---|
394 | while ((INT16) (--CharC) >= 0 && Index < NPT) {
|
---|
395 | Sd->mPTLen[Index++] = 0;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
400 | while (Index < nn && Index < NPT) {
|
---|
401 | Sd->mPTLen[Index++] = 0;
|
---|
402 | }
|
---|
403 |
|
---|
404 | return MakeTable (Sd, nn, Sd->mPTLen, 8, Sd->mPTTable);
|
---|
405 | }
|
---|
406 |
|
---|
407 | /**
|
---|
408 | Reads code lengths for Char&Len Set.
|
---|
409 |
|
---|
410 | Read in and decode the Char&Len Set Code Length Array, then
|
---|
411 | generate the Huffman Code mapping table for the Char&Len Set.
|
---|
412 |
|
---|
413 | @param Sd The global scratch data.
|
---|
414 |
|
---|
415 | **/
|
---|
416 | VOID
|
---|
417 | ReadCLen (
|
---|
418 | SCRATCH_DATA *Sd
|
---|
419 | )
|
---|
420 | {
|
---|
421 | UINT16 Number;
|
---|
422 | UINT16 CharC;
|
---|
423 | UINT16 Index;
|
---|
424 | UINT32 Mask;
|
---|
425 |
|
---|
426 | Number = (UINT16) GetBits (Sd, CBIT);
|
---|
427 |
|
---|
428 | if (Number == 0) {
|
---|
429 | //
|
---|
430 | // This represents only Huffman code used
|
---|
431 | //
|
---|
432 | CharC = (UINT16) GetBits (Sd, CBIT);
|
---|
433 |
|
---|
434 | SetMem (Sd->mCLen, NC, 0);
|
---|
435 | SetMem16 (&Sd->mCTable[0], sizeof (Sd->mCTable), CharC);
|
---|
436 |
|
---|
437 | return ;
|
---|
438 | }
|
---|
439 |
|
---|
440 | Index = 0;
|
---|
441 | while (Index < Number && Index < NC) {
|
---|
442 | CharC = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];
|
---|
443 | if (CharC >= NT) {
|
---|
444 | Mask = 1U << (BITBUFSIZ - 1 - 8);
|
---|
445 |
|
---|
446 | do {
|
---|
447 |
|
---|
448 | if (Mask & Sd->mBitBuf) {
|
---|
449 | CharC = Sd->mRight[CharC];
|
---|
450 | } else {
|
---|
451 | CharC = Sd->mLeft[CharC];
|
---|
452 | }
|
---|
453 |
|
---|
454 | Mask >>= 1;
|
---|
455 |
|
---|
456 | } while (CharC >= NT);
|
---|
457 | }
|
---|
458 | //
|
---|
459 | // Advance what we have read
|
---|
460 | //
|
---|
461 | FillBuf (Sd, Sd->mPTLen[CharC]);
|
---|
462 |
|
---|
463 | if (CharC <= 2) {
|
---|
464 |
|
---|
465 | if (CharC == 0) {
|
---|
466 | CharC = 1;
|
---|
467 | } else if (CharC == 1) {
|
---|
468 | CharC = (UINT16) (GetBits (Sd, 4) + 3);
|
---|
469 | } else if (CharC == 2) {
|
---|
470 | CharC = (UINT16) (GetBits (Sd, CBIT) + 20);
|
---|
471 | }
|
---|
472 |
|
---|
473 | while ((INT16) (--CharC) >= 0 && Index < NC) {
|
---|
474 | Sd->mCLen[Index++] = 0;
|
---|
475 | }
|
---|
476 |
|
---|
477 | } else {
|
---|
478 |
|
---|
479 | Sd->mCLen[Index++] = (UINT8) (CharC - 2);
|
---|
480 |
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | SetMem (Sd->mCLen + Index, NC - Index, 0);
|
---|
485 |
|
---|
486 | MakeTable (Sd, NC, Sd->mCLen, 12, Sd->mCTable);
|
---|
487 |
|
---|
488 | return ;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | Decode a character/length value.
|
---|
493 |
|
---|
494 | Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates
|
---|
495 | Huffman code mapping table for Extra Set, Code&Len Set and
|
---|
496 | Position Set.
|
---|
497 |
|
---|
498 | @param Sd The global scratch data.
|
---|
499 |
|
---|
500 | @return The value decoded.
|
---|
501 |
|
---|
502 | **/
|
---|
503 | UINT16
|
---|
504 | DecodeC (
|
---|
505 | SCRATCH_DATA *Sd
|
---|
506 | )
|
---|
507 | {
|
---|
508 | UINT16 Index2;
|
---|
509 | UINT32 Mask;
|
---|
510 |
|
---|
511 | if (Sd->mBlockSize == 0) {
|
---|
512 | //
|
---|
513 | // Starting a new block
|
---|
514 | // Read BlockSize from block header
|
---|
515 | //
|
---|
516 | Sd->mBlockSize = (UINT16) GetBits (Sd, 16);
|
---|
517 |
|
---|
518 | //
|
---|
519 | // Read in the Extra Set Code Length Array,
|
---|
520 | // Generate the Huffman code mapping table for Extra Set.
|
---|
521 | //
|
---|
522 | Sd->mBadTableFlag = ReadPTLen (Sd, NT, TBIT, 3);
|
---|
523 | if (Sd->mBadTableFlag != 0) {
|
---|
524 | return 0;
|
---|
525 | }
|
---|
526 |
|
---|
527 | //
|
---|
528 | // Read in and decode the Char&Len Set Code Length Array,
|
---|
529 | // Generate the Huffman code mapping table for Char&Len Set.
|
---|
530 | //
|
---|
531 | ReadCLen (Sd);
|
---|
532 |
|
---|
533 | //
|
---|
534 | // Read in the Position Set Code Length Array,
|
---|
535 | // Generate the Huffman code mapping table for the Position Set.
|
---|
536 | //
|
---|
537 | Sd->mBadTableFlag = ReadPTLen (Sd, MAXNP, Sd->mPBit, (UINT16) (-1));
|
---|
538 | if (Sd->mBadTableFlag != 0) {
|
---|
539 | return 0;
|
---|
540 | }
|
---|
541 | }
|
---|
542 |
|
---|
543 | //
|
---|
544 | // Get one code according to Code&Set Huffman Table
|
---|
545 | //
|
---|
546 | Sd->mBlockSize--;
|
---|
547 | Index2 = Sd->mCTable[Sd->mBitBuf >> (BITBUFSIZ - 12)];
|
---|
548 |
|
---|
549 | if (Index2 >= NC) {
|
---|
550 | Mask = 1U << (BITBUFSIZ - 1 - 12);
|
---|
551 |
|
---|
552 | do {
|
---|
553 | if ((Sd->mBitBuf & Mask) != 0) {
|
---|
554 | Index2 = Sd->mRight[Index2];
|
---|
555 | } else {
|
---|
556 | Index2 = Sd->mLeft[Index2];
|
---|
557 | }
|
---|
558 |
|
---|
559 | Mask >>= 1;
|
---|
560 | } while (Index2 >= NC);
|
---|
561 | }
|
---|
562 | //
|
---|
563 | // Advance what we have read
|
---|
564 | //
|
---|
565 | FillBuf (Sd, Sd->mCLen[Index2]);
|
---|
566 |
|
---|
567 | return Index2;
|
---|
568 | }
|
---|
569 |
|
---|
570 | /**
|
---|
571 | Decode the source data and put the resulting data into the destination buffer.
|
---|
572 |
|
---|
573 | @param Sd The global scratch data.
|
---|
574 |
|
---|
575 | **/
|
---|
576 | VOID
|
---|
577 | Decode (
|
---|
578 | SCRATCH_DATA *Sd
|
---|
579 | )
|
---|
580 | {
|
---|
581 | UINT16 BytesRemain;
|
---|
582 | UINT32 DataIdx;
|
---|
583 | UINT16 CharC;
|
---|
584 |
|
---|
585 | BytesRemain = (UINT16) (-1);
|
---|
586 |
|
---|
587 | DataIdx = 0;
|
---|
588 |
|
---|
589 | for (;;) {
|
---|
590 | //
|
---|
591 | // Get one code from mBitBuf
|
---|
592 | //
|
---|
593 | CharC = DecodeC (Sd);
|
---|
594 | if (Sd->mBadTableFlag != 0) {
|
---|
595 | goto Done;
|
---|
596 | }
|
---|
597 |
|
---|
598 | if (CharC < 256) {
|
---|
599 | //
|
---|
600 | // Process an Original character
|
---|
601 | //
|
---|
602 | if (Sd->mOutBuf >= Sd->mOrigSize) {
|
---|
603 | goto Done;
|
---|
604 | } else {
|
---|
605 | //
|
---|
606 | // Write orignal character into mDstBase
|
---|
607 | //
|
---|
608 | Sd->mDstBase[Sd->mOutBuf++] = (UINT8) CharC;
|
---|
609 | }
|
---|
610 |
|
---|
611 | } else {
|
---|
612 | //
|
---|
613 | // Process a Pointer
|
---|
614 | //
|
---|
615 | CharC = (UINT16) (CharC - (BIT8 - THRESHOLD));
|
---|
616 |
|
---|
617 | //
|
---|
618 | // Get string length
|
---|
619 | //
|
---|
620 | BytesRemain = CharC;
|
---|
621 |
|
---|
622 | //
|
---|
623 | // Locate string position
|
---|
624 | //
|
---|
625 | DataIdx = Sd->mOutBuf - DecodeP (Sd) - 1;
|
---|
626 |
|
---|
627 | //
|
---|
628 | // Write BytesRemain of bytes into mDstBase
|
---|
629 | //
|
---|
630 | BytesRemain--;
|
---|
631 |
|
---|
632 | while ((INT16) (BytesRemain) >= 0) {
|
---|
633 | if (Sd->mOutBuf >= Sd->mOrigSize) {
|
---|
634 | goto Done;
|
---|
635 | }
|
---|
636 | if (DataIdx >= Sd->mOrigSize) {
|
---|
637 | Sd->mBadTableFlag = (UINT16) BAD_TABLE;
|
---|
638 | goto Done;
|
---|
639 | }
|
---|
640 | Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];
|
---|
641 |
|
---|
642 | BytesRemain--;
|
---|
643 | }
|
---|
644 | //
|
---|
645 | // Once mOutBuf is fully filled, directly return
|
---|
646 | //
|
---|
647 | if (Sd->mOutBuf >= Sd->mOrigSize) {
|
---|
648 | goto Done;
|
---|
649 | }
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | Done:
|
---|
654 | return ;
|
---|
655 | }
|
---|
656 |
|
---|
657 | /**
|
---|
658 | Given a compressed source buffer, this function retrieves the size of
|
---|
659 | the uncompressed buffer and the size of the scratch buffer required
|
---|
660 | to decompress the compressed source buffer.
|
---|
661 |
|
---|
662 | Retrieves the size of the uncompressed buffer and the temporary scratch buffer
|
---|
663 | required to decompress the buffer specified by Source and SourceSize.
|
---|
664 | If the size of the uncompressed buffer or the size of the scratch buffer cannot
|
---|
665 | be determined from the compressed data specified by Source and SourceData,
|
---|
666 | then RETURN_INVALID_PARAMETER is returned. Otherwise, the size of the uncompressed
|
---|
667 | buffer is returned in DestinationSize, the size of the scratch buffer is returned
|
---|
668 | in ScratchSize, and RETURN_SUCCESS is returned.
|
---|
669 | This function does not have scratch buffer available to perform a thorough
|
---|
670 | checking of the validity of the source data. It just retrieves the "Original Size"
|
---|
671 | field from the beginning bytes of the source data and output it as DestinationSize.
|
---|
672 | And ScratchSize is specific to the decompression implementation.
|
---|
673 |
|
---|
674 | If Source is NULL, then ASSERT().
|
---|
675 | If DestinationSize is NULL, then ASSERT().
|
---|
676 | If ScratchSize is NULL, then ASSERT().
|
---|
677 |
|
---|
678 | @param Source The source buffer containing the compressed data.
|
---|
679 | @param SourceSize The size, in bytes, of the source buffer.
|
---|
680 | @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
|
---|
681 | that will be generated when the compressed buffer specified
|
---|
682 | by Source and SourceSize is decompressed.
|
---|
683 | @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that
|
---|
684 | is required to decompress the compressed buffer specified
|
---|
685 | by Source and SourceSize.
|
---|
686 |
|
---|
687 | @retval RETURN_SUCCESS The size of the uncompressed data was returned
|
---|
688 | in DestinationSize, and the size of the scratch
|
---|
689 | buffer was returned in ScratchSize.
|
---|
690 | @retval RETURN_INVALID_PARAMETER
|
---|
691 | The size of the uncompressed data or the size of
|
---|
692 | the scratch buffer cannot be determined from
|
---|
693 | the compressed data specified by Source
|
---|
694 | and SourceSize.
|
---|
695 | **/
|
---|
696 | RETURN_STATUS
|
---|
697 | EFIAPI
|
---|
698 | UefiDecompressGetInfo (
|
---|
699 | IN CONST VOID *Source,
|
---|
700 | IN UINT32 SourceSize,
|
---|
701 | OUT UINT32 *DestinationSize,
|
---|
702 | OUT UINT32 *ScratchSize
|
---|
703 | )
|
---|
704 | {
|
---|
705 | UINT32 CompressedSize;
|
---|
706 |
|
---|
707 | ASSERT (Source != NULL);
|
---|
708 | ASSERT (DestinationSize != NULL);
|
---|
709 | ASSERT (ScratchSize != NULL);
|
---|
710 |
|
---|
711 | if (SourceSize < 8) {
|
---|
712 | return RETURN_INVALID_PARAMETER;
|
---|
713 | }
|
---|
714 |
|
---|
715 | CompressedSize = ReadUnaligned32 ((UINT32 *)Source);
|
---|
716 | if (SourceSize < (CompressedSize + 8) || (CompressedSize + 8) < 8) {
|
---|
717 | return RETURN_INVALID_PARAMETER;
|
---|
718 | }
|
---|
719 |
|
---|
720 | *ScratchSize = sizeof (SCRATCH_DATA);
|
---|
721 | *DestinationSize = ReadUnaligned32 ((UINT32 *)Source + 1);
|
---|
722 |
|
---|
723 | return RETURN_SUCCESS;
|
---|
724 | }
|
---|
725 |
|
---|
726 | /**
|
---|
727 | Decompresses a compressed source buffer.
|
---|
728 |
|
---|
729 | Extracts decompressed data to its original form.
|
---|
730 | This function is designed so that the decompression algorithm can be implemented
|
---|
731 | without using any memory services. As a result, this function is not allowed to
|
---|
732 | call any memory allocation services in its implementation. It is the caller's
|
---|
733 | responsibility to allocate and free the Destination and Scratch buffers.
|
---|
734 | If the compressed source data specified by Source is successfully decompressed
|
---|
735 | into Destination, then RETURN_SUCCESS is returned. If the compressed source data
|
---|
736 | specified by Source is not in a valid compressed data format,
|
---|
737 | then RETURN_INVALID_PARAMETER is returned.
|
---|
738 |
|
---|
739 | If Source is NULL, then ASSERT().
|
---|
740 | If Destination is NULL, then ASSERT().
|
---|
741 | If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT().
|
---|
742 |
|
---|
743 | @param Source The source buffer containing the compressed data.
|
---|
744 | @param Destination The destination buffer to store the decompressed data.
|
---|
745 | @param Scratch A temporary scratch buffer that is used to perform the decompression.
|
---|
746 | This is an optional parameter that may be NULL if the
|
---|
747 | required scratch buffer size is 0.
|
---|
748 |
|
---|
749 | @retval RETURN_SUCCESS Decompression completed successfully, and
|
---|
750 | the uncompressed buffer is returned in Destination.
|
---|
751 | @retval RETURN_INVALID_PARAMETER
|
---|
752 | The source buffer specified by Source is corrupted
|
---|
753 | (not in a valid compressed format).
|
---|
754 | **/
|
---|
755 | RETURN_STATUS
|
---|
756 | EFIAPI
|
---|
757 | UefiDecompress (
|
---|
758 | IN CONST VOID *Source,
|
---|
759 | IN OUT VOID *Destination,
|
---|
760 | IN OUT VOID *Scratch OPTIONAL
|
---|
761 | )
|
---|
762 | {
|
---|
763 | UINT32 CompSize;
|
---|
764 | UINT32 OrigSize;
|
---|
765 | SCRATCH_DATA *Sd;
|
---|
766 | CONST UINT8 *Src;
|
---|
767 | UINT8 *Dst;
|
---|
768 |
|
---|
769 | ASSERT (Source != NULL);
|
---|
770 | ASSERT (Destination != NULL);
|
---|
771 | ASSERT (Scratch != NULL);
|
---|
772 |
|
---|
773 | Src = Source;
|
---|
774 | Dst = Destination;
|
---|
775 |
|
---|
776 | Sd = (SCRATCH_DATA *) Scratch;
|
---|
777 |
|
---|
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);
|
---|
780 |
|
---|
781 | //
|
---|
782 | // If compressed file size is 0, return
|
---|
783 | //
|
---|
784 | if (OrigSize == 0) {
|
---|
785 | return RETURN_SUCCESS;
|
---|
786 | }
|
---|
787 |
|
---|
788 | Src = Src + 8;
|
---|
789 | SetMem (Sd, sizeof (SCRATCH_DATA), 0);
|
---|
790 |
|
---|
791 | //
|
---|
792 | // The length of the field 'Position Set Code Length Array Size' in Block Header.
|
---|
793 | // For UEFI 2.0 de/compression algorithm(Version 1), mPBit = 4
|
---|
794 | //
|
---|
795 | Sd->mPBit = 4;
|
---|
796 | Sd->mSrcBase = (UINT8 *)Src;
|
---|
797 | Sd->mDstBase = Dst;
|
---|
798 | //
|
---|
799 | // CompSize and OrigSize are calculated in bytes
|
---|
800 | //
|
---|
801 | Sd->mCompSize = CompSize;
|
---|
802 | Sd->mOrigSize = OrigSize;
|
---|
803 |
|
---|
804 | //
|
---|
805 | // Fill the first BITBUFSIZ bits
|
---|
806 | //
|
---|
807 | FillBuf (Sd, BITBUFSIZ);
|
---|
808 |
|
---|
809 | //
|
---|
810 | // Decompress it
|
---|
811 | //
|
---|
812 | Decode (Sd);
|
---|
813 |
|
---|
814 | if (Sd->mBadTableFlag != 0) {
|
---|
815 | //
|
---|
816 | // Something wrong with the source
|
---|
817 | //
|
---|
818 | return RETURN_INVALID_PARAMETER;
|
---|
819 | }
|
---|
820 |
|
---|
821 | return RETURN_SUCCESS;
|
---|
822 | }
|
---|