]>
Commit | Line | Data |
---|---|---|
3db51098 | 1 | /**@file\r |
2 | UEFI and Custom Decompress Library \r | |
ed7752ec | 3 | The function of UefiTianoDecompress() is interface for this module,\r |
4 | it will do tiano or uefi decompress with different verison parameter.\r | |
5 | See EFI specification 1.1 Chapter 17 to get LZ77 compress/decompress.\r | |
3db51098 | 6 | \r |
54bd896e | 7 | Copyright (c) 2006, Intel Corporation \r |
8 | All rights reserved. This program and the accompanying materials \r | |
9 | are licensed and made available under the terms and conditions of the BSD License \r | |
10 | which accompanies this distribution. The full text of the license may be found at \r | |
11 | http://opensource.org/licenses/bsd-license.php \r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
15 | \r | |
3db51098 | 16 | **/\r |
54bd896e | 17 | \r |
d8c79a81 | 18 | #include <Guid/CustomDecompress.h>\r |
54bd896e | 19 | #include "BaseUefiTianoCustomDecompressLibInternals.h"\r |
20 | \r | |
ed7752ec | 21 | /**\r |
22 | Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.\r | |
23 | \r | |
24 | @param Sd The global scratch data\r | |
25 | @param NumOfBits The number of bits to shift and read. \r | |
26 | **/\r | |
54bd896e | 27 | VOID\r |
28 | FillBuf (\r | |
29 | IN SCRATCH_DATA *Sd,\r | |
30 | IN UINT16 NumOfBits\r | |
31 | )\r | |
54bd896e | 32 | {\r |
33 | Sd->mBitBuf = (UINT32) (Sd->mBitBuf << NumOfBits);\r | |
34 | \r | |
35 | while (NumOfBits > Sd->mBitCount) {\r | |
36 | \r | |
37 | Sd->mBitBuf |= (UINT32) (Sd->mSubBitBuf << (NumOfBits = (UINT16) (NumOfBits - Sd->mBitCount)));\r | |
38 | \r | |
39 | if (Sd->mCompSize > 0) {\r | |
40 | //\r | |
41 | // Get 1 byte into SubBitBuf\r | |
42 | //\r | |
43 | Sd->mCompSize--;\r | |
44 | Sd->mSubBitBuf = 0;\r | |
45 | Sd->mSubBitBuf = Sd->mSrcBase[Sd->mInBuf++];\r | |
46 | Sd->mBitCount = 8;\r | |
47 | \r | |
48 | } else {\r | |
49 | //\r | |
50 | // No more bits from the source, just pad zero bit.\r | |
51 | //\r | |
52 | Sd->mSubBitBuf = 0;\r | |
53 | Sd->mBitCount = 8;\r | |
54 | \r | |
55 | }\r | |
56 | }\r | |
57 | \r | |
58 | Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits);\r | |
59 | Sd->mBitBuf |= Sd->mSubBitBuf >> Sd->mBitCount;\r | |
60 | }\r | |
61 | \r | |
ed7752ec | 62 | /**\r |
63 | Get NumOfBits of bits out from mBitBuf\r | |
54bd896e | 64 | \r |
65 | Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent \r | |
66 | NumOfBits of bits from source. Returns NumOfBits of bits that are \r | |
67 | popped out.\r | |
68 | \r | |
ed7752ec | 69 | @param Sd The global scratch data.\r |
70 | @param NumOfBits The number of bits to pop and read.\r | |
54bd896e | 71 | \r |
ed7752ec | 72 | @return The bits that are popped out.\r |
54bd896e | 73 | \r |
ed7752ec | 74 | **/\r |
75 | UINT32\r | |
76 | GetBits (\r | |
77 | IN SCRATCH_DATA *Sd,\r | |
78 | IN UINT16 NumOfBits\r | |
79 | )\r | |
54bd896e | 80 | {\r |
81 | UINT32 OutBits;\r | |
82 | \r | |
83 | OutBits = (UINT32) (Sd->mBitBuf >> (BITBUFSIZ - NumOfBits));\r | |
84 | \r | |
85 | FillBuf (Sd, NumOfBits);\r | |
86 | \r | |
87 | return OutBits;\r | |
88 | }\r | |
89 | \r | |
ed7752ec | 90 | /**\r |
91 | Creates Huffman Code mapping table according to code length array.\r | |
92 | \r | |
93 | Creates Huffman Code mapping table for Extra Set, Char&Len Set \r | |
94 | and Position Set according to code length array.\r | |
95 | \r | |
96 | @param Sd The global scratch data\r | |
97 | @param NumOfChar Number of symbols in the symbol set\r | |
98 | @param BitLen Code length array\r | |
99 | @param TableBits The width of the mapping table\r | |
100 | @param Table The table\r | |
101 | \r | |
102 | @retval 0 OK.\r | |
103 | @retval BAD_TABLE The table is corrupted.\r | |
104 | \r | |
105 | **/\r | |
54bd896e | 106 | UINT16\r |
107 | MakeTable (\r | |
108 | IN SCRATCH_DATA *Sd,\r | |
109 | IN UINT16 NumOfChar,\r | |
110 | IN UINT8 *BitLen,\r | |
111 | IN UINT16 TableBits,\r | |
112 | OUT UINT16 *Table\r | |
113 | )\r | |
54bd896e | 114 | {\r |
115 | UINT16 Count[17];\r | |
116 | UINT16 Weight[17];\r | |
117 | UINT16 Start[18];\r | |
118 | UINT16 *Pointer;\r | |
119 | UINT16 Index3;\r | |
120 | volatile UINT16 Index;\r | |
121 | UINT16 Len;\r | |
122 | UINT16 Char;\r | |
123 | UINT16 JuBits;\r | |
124 | UINT16 Avail;\r | |
125 | UINT16 NextCode;\r | |
126 | UINT16 Mask;\r | |
127 | UINT16 WordOfStart;\r | |
128 | UINT16 WordOfCount;\r | |
129 | \r | |
130 | for (Index = 1; Index <= 16; Index++) {\r | |
131 | Count[Index] = 0;\r | |
132 | }\r | |
133 | \r | |
134 | for (Index = 0; Index < NumOfChar; Index++) {\r | |
135 | Count[BitLen[Index]]++;\r | |
136 | }\r | |
137 | \r | |
138 | Start[1] = 0;\r | |
139 | \r | |
140 | for (Index = 1; Index <= 16; Index++) {\r | |
141 | WordOfStart = Start[Index];\r | |
142 | WordOfCount = Count[Index];\r | |
143 | Start[Index + 1] = (UINT16) (WordOfStart + (WordOfCount << (16 - Index)));\r | |
144 | }\r | |
145 | \r | |
146 | if (Start[17] != 0) {\r | |
147 | /*(1U << 16)*/\r | |
148 | return (UINT16) BAD_TABLE;\r | |
149 | }\r | |
150 | \r | |
151 | JuBits = (UINT16) (16 - TableBits);\r | |
152 | \r | |
153 | for (Index = 1; Index <= TableBits; Index++) {\r | |
154 | Start[Index] >>= JuBits;\r | |
155 | Weight[Index] = (UINT16) (1U << (TableBits - Index));\r | |
156 | }\r | |
157 | \r | |
158 | while (Index <= 16) {\r | |
159 | Weight[Index] = (UINT16) (1U << (16 - Index));\r | |
160 | Index++;\r | |
161 | }\r | |
162 | \r | |
163 | Index = (UINT16) (Start[TableBits + 1] >> JuBits);\r | |
164 | \r | |
165 | if (Index != 0) {\r | |
166 | Index3 = (UINT16) (1U << TableBits);\r | |
167 | while (Index != Index3) {\r | |
168 | Table[Index++] = 0;\r | |
169 | }\r | |
170 | }\r | |
171 | \r | |
172 | Avail = NumOfChar;\r | |
173 | Mask = (UINT16) (1U << (15 - TableBits));\r | |
174 | \r | |
175 | for (Char = 0; Char < NumOfChar; Char++) {\r | |
176 | \r | |
177 | Len = BitLen[Char];\r | |
178 | if (Len == 0) {\r | |
179 | continue;\r | |
180 | }\r | |
181 | \r | |
182 | NextCode = (UINT16) (Start[Len] + Weight[Len]);\r | |
183 | \r | |
184 | if (Len <= TableBits) {\r | |
185 | \r | |
186 | for (Index = Start[Len]; Index < NextCode; Index++) {\r | |
187 | Table[Index] = Char;\r | |
188 | }\r | |
189 | \r | |
190 | } else {\r | |
191 | \r | |
192 | Index3 = Start[Len];\r | |
193 | Pointer = &Table[Index3 >> JuBits];\r | |
194 | Index = (UINT16) (Len - TableBits);\r | |
195 | \r | |
196 | while (Index != 0) {\r | |
197 | if (*Pointer == 0) {\r | |
198 | Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;\r | |
199 | *Pointer = Avail++;\r | |
200 | }\r | |
201 | \r | |
202 | if (Index3 & Mask) {\r | |
203 | Pointer = &Sd->mRight[*Pointer];\r | |
204 | } else {\r | |
205 | Pointer = &Sd->mLeft[*Pointer];\r | |
206 | }\r | |
207 | \r | |
208 | Index3 <<= 1;\r | |
209 | Index--;\r | |
210 | }\r | |
211 | \r | |
212 | *Pointer = Char;\r | |
213 | \r | |
214 | }\r | |
215 | \r | |
216 | Start[Len] = NextCode;\r | |
217 | }\r | |
218 | //\r | |
219 | // Succeeds\r | |
220 | //\r | |
221 | return 0;\r | |
222 | }\r | |
223 | \r | |
ed7752ec | 224 | /**\r |
225 | Decodes a position value.\r | |
226 | \r | |
227 | @param Sd the global scratch data\r | |
228 | \r | |
229 | @return The position value decoded.\r | |
230 | **/\r | |
54bd896e | 231 | UINT32\r |
232 | DecodeP (\r | |
233 | IN SCRATCH_DATA *Sd\r | |
234 | )\r | |
54bd896e | 235 | {\r |
236 | UINT16 Val;\r | |
237 | UINT32 Mask;\r | |
238 | UINT32 Pos;\r | |
239 | \r | |
240 | Val = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];\r | |
241 | \r | |
242 | if (Val >= MAXNP) {\r | |
243 | Mask = 1U << (BITBUFSIZ - 1 - 8);\r | |
244 | \r | |
245 | do {\r | |
246 | \r | |
247 | if (Sd->mBitBuf & Mask) {\r | |
248 | Val = Sd->mRight[Val];\r | |
249 | } else {\r | |
250 | Val = Sd->mLeft[Val];\r | |
251 | }\r | |
252 | \r | |
253 | Mask >>= 1;\r | |
254 | } while (Val >= MAXNP);\r | |
255 | }\r | |
256 | //\r | |
257 | // Advance what we have read\r | |
258 | //\r | |
259 | FillBuf (Sd, Sd->mPTLen[Val]);\r | |
260 | \r | |
261 | Pos = Val;\r | |
262 | if (Val > 1) {\r | |
263 | Pos = (UINT32) ((1U << (Val - 1)) + GetBits (Sd, (UINT16) (Val - 1)));\r | |
264 | }\r | |
265 | \r | |
266 | return Pos;\r | |
267 | }\r | |
268 | \r | |
ed7752ec | 269 | /**\r |
270 | Reads code lengths for the Extra Set or the Position Set.\r | |
271 | \r | |
272 | Read in the Extra Set or Pointion Set Length Arrary, then\r | |
273 | generate the Huffman code mapping for them.\r | |
274 | \r | |
275 | @param Sd The global scratch data.\r | |
276 | @param nn Number of symbols.\r | |
277 | @param nbit Number of bits needed to represent nn.\r | |
278 | @param Special The special symbol that needs to be taken care of.\r | |
279 | \r | |
280 | @retval 0 OK.\r | |
281 | @retval BAD_TABLE Table is corrupted.\r | |
282 | \r | |
283 | **/\r | |
54bd896e | 284 | UINT16\r |
285 | ReadPTLen (\r | |
286 | IN SCRATCH_DATA *Sd,\r | |
287 | IN UINT16 nn,\r | |
288 | IN UINT16 nbit,\r | |
289 | IN UINT16 Special\r | |
290 | )\r | |
54bd896e | 291 | {\r |
292 | UINT16 Number;\r | |
293 | UINT16 CharC;\r | |
294 | volatile UINT16 Index;\r | |
295 | UINT32 Mask;\r | |
296 | \r | |
297 | Number = (UINT16) GetBits (Sd, nbit);\r | |
298 | \r | |
299 | if (Number == 0) {\r | |
300 | CharC = (UINT16) GetBits (Sd, nbit);\r | |
301 | \r | |
302 | for (Index = 0; Index < 256; Index++) {\r | |
303 | Sd->mPTTable[Index] = CharC;\r | |
304 | }\r | |
305 | \r | |
306 | for (Index = 0; Index < nn; Index++) {\r | |
307 | Sd->mPTLen[Index] = 0;\r | |
308 | }\r | |
309 | \r | |
310 | return 0;\r | |
311 | }\r | |
312 | \r | |
313 | Index = 0;\r | |
314 | \r | |
315 | while (Index < Number) {\r | |
316 | \r | |
317 | CharC = (UINT16) (Sd->mBitBuf >> (BITBUFSIZ - 3));\r | |
318 | \r | |
319 | if (CharC == 7) {\r | |
320 | Mask = 1U << (BITBUFSIZ - 1 - 3);\r | |
321 | while (Mask & Sd->mBitBuf) {\r | |
322 | Mask >>= 1;\r | |
323 | CharC += 1;\r | |
324 | }\r | |
325 | }\r | |
326 | \r | |
327 | FillBuf (Sd, (UINT16) ((CharC < 7) ? 3 : CharC - 3));\r | |
328 | \r | |
329 | Sd->mPTLen[Index++] = (UINT8) CharC;\r | |
330 | \r | |
331 | if (Index == Special) {\r | |
332 | CharC = (UINT16) GetBits (Sd, 2);\r | |
333 | while ((INT16) (--CharC) >= 0) {\r | |
334 | Sd->mPTLen[Index++] = 0;\r | |
335 | }\r | |
336 | }\r | |
337 | }\r | |
338 | \r | |
339 | while (Index < nn) {\r | |
340 | Sd->mPTLen[Index++] = 0;\r | |
341 | }\r | |
342 | \r | |
343 | return MakeTable (Sd, nn, Sd->mPTLen, 8, Sd->mPTTable);\r | |
344 | }\r | |
345 | \r | |
ed7752ec | 346 | /**\r |
347 | Reads code lengths for Char&Len Set.\r | |
348 | \r | |
349 | Read in and decode the Char&Len Set Code Length Array, then\r | |
350 | generate the Huffman Code mapping table for the Char&Len Set.\r | |
351 | \r | |
352 | @param Sd the global scratch data\r | |
353 | \r | |
354 | **/\r | |
54bd896e | 355 | VOID\r |
356 | ReadCLen (\r | |
357 | SCRATCH_DATA *Sd\r | |
358 | )\r | |
54bd896e | 359 | {\r |
360 | UINT16 Number;\r | |
361 | UINT16 CharC;\r | |
362 | volatile UINT16 Index;\r | |
363 | UINT32 Mask;\r | |
364 | \r | |
365 | Number = (UINT16) GetBits (Sd, CBIT);\r | |
366 | \r | |
367 | if (Number == 0) {\r | |
368 | CharC = (UINT16) GetBits (Sd, CBIT);\r | |
369 | \r | |
370 | for (Index = 0; Index < NC; Index++) {\r | |
371 | Sd->mCLen[Index] = 0;\r | |
372 | }\r | |
373 | \r | |
374 | for (Index = 0; Index < 4096; Index++) {\r | |
375 | Sd->mCTable[Index] = CharC;\r | |
376 | }\r | |
377 | \r | |
378 | return ;\r | |
379 | }\r | |
380 | \r | |
381 | Index = 0;\r | |
382 | while (Index < Number) {\r | |
383 | \r | |
384 | CharC = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];\r | |
385 | if (CharC >= NT) {\r | |
386 | Mask = 1U << (BITBUFSIZ - 1 - 8);\r | |
387 | \r | |
388 | do {\r | |
389 | \r | |
390 | if (Mask & Sd->mBitBuf) {\r | |
391 | CharC = Sd->mRight[CharC];\r | |
392 | } else {\r | |
393 | CharC = Sd->mLeft[CharC];\r | |
394 | }\r | |
395 | \r | |
396 | Mask >>= 1;\r | |
397 | \r | |
398 | } while (CharC >= NT);\r | |
399 | }\r | |
400 | //\r | |
401 | // Advance what we have read\r | |
402 | //\r | |
403 | FillBuf (Sd, Sd->mPTLen[CharC]);\r | |
404 | \r | |
405 | if (CharC <= 2) {\r | |
406 | \r | |
407 | if (CharC == 0) {\r | |
408 | CharC = 1;\r | |
409 | } else if (CharC == 1) {\r | |
410 | CharC = (UINT16) (GetBits (Sd, 4) + 3);\r | |
411 | } else if (CharC == 2) {\r | |
412 | CharC = (UINT16) (GetBits (Sd, CBIT) + 20);\r | |
413 | }\r | |
414 | \r | |
415 | while ((INT16) (--CharC) >= 0) {\r | |
416 | Sd->mCLen[Index++] = 0;\r | |
417 | }\r | |
418 | \r | |
419 | } else {\r | |
420 | \r | |
421 | Sd->mCLen[Index++] = (UINT8) (CharC - 2);\r | |
422 | \r | |
423 | }\r | |
424 | }\r | |
425 | \r | |
426 | while (Index < NC) {\r | |
427 | Sd->mCLen[Index++] = 0;\r | |
428 | }\r | |
429 | \r | |
430 | MakeTable (Sd, NC, Sd->mCLen, 12, Sd->mCTable);\r | |
431 | \r | |
432 | return ;\r | |
433 | }\r | |
434 | \r | |
ed7752ec | 435 | /**\r |
54bd896e | 436 | Decode a character/length value.\r |
ed7752ec | 437 | \r |
438 | Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates\r | |
439 | Huffman code mapping table for Extra Set, Code&Len Set and\r | |
440 | Position Set.\r | |
54bd896e | 441 | \r |
ed7752ec | 442 | @param Sd The global scratch data.\r |
54bd896e | 443 | \r |
ed7752ec | 444 | @return The value decoded.\r |
54bd896e | 445 | \r |
ed7752ec | 446 | **/\r |
447 | UINT16\r | |
448 | DecodeC (\r | |
449 | SCRATCH_DATA *Sd\r | |
450 | )\r | |
54bd896e | 451 | {\r |
452 | UINT16 Index2;\r | |
453 | UINT32 Mask;\r | |
454 | \r | |
455 | if (Sd->mBlockSize == 0) {\r | |
456 | //\r | |
457 | // Starting a new block\r | |
458 | //\r | |
459 | Sd->mBlockSize = (UINT16) GetBits (Sd, 16);\r | |
460 | Sd->mBadTableFlag = ReadPTLen (Sd, NT, TBIT, 3);\r | |
461 | if (Sd->mBadTableFlag != 0) {\r | |
462 | return 0;\r | |
463 | }\r | |
464 | \r | |
465 | ReadCLen (Sd);\r | |
466 | \r | |
467 | Sd->mBadTableFlag = ReadPTLen (Sd, MAXNP, Sd->mPBit, (UINT16) (-1));\r | |
468 | if (Sd->mBadTableFlag != 0) {\r | |
469 | return 0;\r | |
470 | }\r | |
471 | }\r | |
472 | \r | |
473 | Sd->mBlockSize--;\r | |
474 | Index2 = Sd->mCTable[Sd->mBitBuf >> (BITBUFSIZ - 12)];\r | |
475 | \r | |
476 | if (Index2 >= NC) {\r | |
477 | Mask = 1U << (BITBUFSIZ - 1 - 12);\r | |
478 | \r | |
479 | do {\r | |
480 | if (Sd->mBitBuf & Mask) {\r | |
481 | Index2 = Sd->mRight[Index2];\r | |
482 | } else {\r | |
483 | Index2 = Sd->mLeft[Index2];\r | |
484 | }\r | |
485 | \r | |
486 | Mask >>= 1;\r | |
487 | } while (Index2 >= NC);\r | |
488 | }\r | |
489 | //\r | |
490 | // Advance what we have read\r | |
491 | //\r | |
492 | FillBuf (Sd, Sd->mCLen[Index2]);\r | |
493 | \r | |
494 | return Index2;\r | |
495 | }\r | |
496 | \r | |
ed7752ec | 497 | /**\r |
498 | Decode the source data ad put the resulting data into the destination buffer.\r | |
499 | \r | |
500 | @param Sd - The global scratch data\r | |
501 | **/\r | |
54bd896e | 502 | VOID\r |
503 | Decode (\r | |
504 | SCRATCH_DATA *Sd\r | |
505 | )\r | |
54bd896e | 506 | {\r |
507 | UINT16 BytesRemain;\r | |
508 | UINT32 DataIdx;\r | |
509 | UINT16 CharC;\r | |
510 | \r | |
511 | BytesRemain = (UINT16) (-1);\r | |
512 | \r | |
513 | DataIdx = 0;\r | |
514 | \r | |
515 | for (;;) {\r | |
516 | CharC = DecodeC (Sd);\r | |
517 | if (Sd->mBadTableFlag != 0) {\r | |
518 | goto Done ;\r | |
519 | }\r | |
520 | \r | |
521 | if (CharC < 256) {\r | |
522 | //\r | |
523 | // Process an Original character\r | |
524 | //\r | |
525 | if (Sd->mOutBuf >= Sd->mOrigSize) {\r | |
526 | goto Done ;\r | |
527 | } else {\r | |
528 | Sd->mDstBase[Sd->mOutBuf++] = (UINT8) CharC;\r | |
529 | }\r | |
530 | \r | |
531 | } else {\r | |
532 | //\r | |
533 | // Process a Pointer\r | |
534 | //\r | |
535 | CharC = (UINT16) (CharC - (UINT8_MAX + 1 - THRESHOLD));\r | |
536 | \r | |
537 | BytesRemain = CharC;\r | |
538 | \r | |
539 | DataIdx = Sd->mOutBuf - DecodeP (Sd) - 1;\r | |
540 | \r | |
541 | BytesRemain--;\r | |
542 | while ((INT16) (BytesRemain) >= 0) {\r | |
543 | Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];\r | |
544 | if (Sd->mOutBuf >= Sd->mOrigSize) {\r | |
545 | goto Done ;\r | |
546 | }\r | |
547 | \r | |
548 | BytesRemain--;\r | |
549 | }\r | |
550 | }\r | |
551 | }\r | |
552 | \r | |
553 | Done:\r | |
554 | return ;\r | |
555 | }\r | |
556 | \r | |
ed7752ec | 557 | /**\r |
558 | The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().\r | |
559 | \r | |
560 | @param Source The source buffer containing the compressed data.\r | |
561 | @param SourceSize The size of source buffer\r | |
562 | @param DestinationSize The size of destination buffer.\r | |
563 | @param ScratchSize The size of scratch buffer.\r | |
564 | \r | |
565 | @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.\r | |
566 | @retval RETURN_INVALID_PARAMETER - The source data is corrupted\r | |
567 | **/\r | |
54bd896e | 568 | RETURN_STATUS\r |
569 | EFIAPI\r | |
570 | UefiDecompressGetInfo (\r | |
571 | IN CONST VOID *Source,\r | |
572 | IN UINT32 SourceSize,\r | |
573 | OUT UINT32 *DestinationSize,\r | |
574 | OUT UINT32 *ScratchSize\r | |
575 | )\r | |
54bd896e | 576 | {\r |
577 | UINT32 CompressedSize;\r | |
578 | \r | |
579 | ASSERT (Source != NULL);\r | |
580 | ASSERT (DestinationSize != NULL);\r | |
581 | ASSERT (ScratchSize != NULL);\r | |
582 | \r | |
54bd896e | 583 | if (SourceSize < 8) {\r |
584 | return RETURN_INVALID_PARAMETER;\r | |
585 | }\r | |
586 | \r | |
18fd8d65 | 587 | CompressedSize = *(UINT32 *) Source;\r |
54bd896e | 588 | if (SourceSize < (CompressedSize + 8)) {\r |
589 | return RETURN_INVALID_PARAMETER;\r | |
590 | }\r | |
591 | \r | |
18fd8d65 LG |
592 | *ScratchSize = sizeof (SCRATCH_DATA);\r |
593 | *DestinationSize = *((UINT32 *) Source + 1);\r | |
594 | \r | |
54bd896e | 595 | return RETURN_SUCCESS;\r |
596 | }\r | |
597 | \r | |
ed7752ec | 598 | /**\r |
599 | The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r | |
600 | \r | |
601 | @param Source The source buffer containing the compressed data.\r | |
602 | @param Destination The destination buffer to store the decompressed data\r | |
603 | @param Scratch The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.\r | |
604 | @param Version 1 for UEFI Decompress algoruthm, 2 for Tiano Decompess algorithm\r | |
605 | \r | |
606 | @retval RETURN_SUCCESS Decompression is successfull\r | |
607 | @retval RETURN_INVALID_PARAMETER The source data is corrupted\r | |
608 | **/\r | |
54bd896e | 609 | RETURN_STATUS\r |
610 | EFIAPI\r | |
611 | UefiTianoDecompress (\r | |
612 | IN CONST VOID *Source,\r | |
613 | IN OUT VOID *Destination,\r | |
614 | IN OUT VOID *Scratch,\r | |
615 | IN UINT32 Version\r | |
616 | )\r | |
54bd896e | 617 | {\r |
618 | volatile UINT32 Index;\r | |
619 | UINT32 CompSize;\r | |
620 | UINT32 OrigSize;\r | |
621 | SCRATCH_DATA *Sd;\r | |
622 | CONST UINT8 *Src;\r | |
623 | UINT8 *Dst;\r | |
624 | \r | |
625 | ASSERT (Source != NULL);\r | |
626 | ASSERT (Destination != NULL);\r | |
627 | ASSERT (Scratch != NULL);\r | |
628 | \r | |
629 | Src = Source;\r | |
630 | Dst = Destination;\r | |
631 | \r | |
632 | Sd = (SCRATCH_DATA *) Scratch;\r | |
633 | \r | |
634 | CompSize = Src[0] + (Src[1] << 8) + (Src[2] << 16) + (Src[3] << 24);\r | |
635 | OrigSize = Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24);\r | |
636 | \r | |
637 | //\r | |
638 | // If compressed file size is 0, return\r | |
639 | //\r | |
640 | if (OrigSize == 0) {\r | |
641 | return RETURN_SUCCESS;\r | |
642 | }\r | |
643 | \r | |
644 | Src = Src + 8;\r | |
645 | \r | |
646 | for (Index = 0; Index < sizeof (SCRATCH_DATA); Index++) {\r | |
647 | ((UINT8 *) Sd)[Index] = 0;\r | |
648 | }\r | |
649 | //\r | |
650 | // The length of the field 'Position Set Code Length Array Size' in Block Header.\r | |
8a7d75b0 | 651 | // For UEFI 2.0 de/compression algorithm(Version 1), mPBit = 4\r |
54bd896e | 652 | // For Tiano de/compression algorithm(Version 2), mPBit = 5\r |
653 | //\r | |
654 | switch (Version) {\r | |
655 | case 1 :\r | |
656 | Sd->mPBit = 4;\r | |
657 | break;\r | |
658 | case 2 :\r | |
659 | Sd->mPBit = 5;\r | |
660 | break;\r | |
661 | default:\r | |
662 | ASSERT (FALSE);\r | |
663 | }\r | |
664 | Sd->mSrcBase = (UINT8 *)Src;\r | |
665 | Sd->mDstBase = Dst;\r | |
666 | Sd->mCompSize = CompSize;\r | |
667 | Sd->mOrigSize = OrigSize;\r | |
668 | \r | |
669 | //\r | |
670 | // Fill the first BITBUFSIZ bits\r | |
671 | //\r | |
672 | FillBuf (Sd, BITBUFSIZ);\r | |
673 | \r | |
674 | //\r | |
675 | // Decompress it\r | |
676 | //\r | |
677 | Decode (Sd);\r | |
678 | \r | |
679 | if (Sd->mBadTableFlag != 0) {\r | |
680 | //\r | |
681 | // Something wrong with the source\r | |
682 | //\r | |
683 | return RETURN_INVALID_PARAMETER;\r | |
684 | }\r | |
685 | \r | |
686 | return RETURN_SUCCESS;\r | |
687 | }\r | |
688 | \r | |
ed7752ec | 689 | /**\r |
690 | The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r | |
691 | \r | |
692 | @param Source - The source buffer containing the compressed data.\r | |
693 | @param Destination - The destination buffer to store the decompressed data\r | |
694 | @param Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.\r | |
695 | \r | |
696 | @retval RETURN_SUCCESS - Decompression is successfull\r | |
697 | @retval RETURN_INVALID_PARAMETER - The source data is corrupted \r | |
698 | **/\r | |
54bd896e | 699 | RETURN_STATUS\r |
700 | EFIAPI\r | |
701 | UefiDecompress (\r | |
702 | IN CONST VOID *Source,\r | |
703 | IN OUT VOID *Destination,\r | |
704 | IN OUT VOID *Scratch\r | |
705 | )\r | |
54bd896e | 706 | {\r |
707 | return UefiTianoDecompress (Source, Destination, Scratch, 1);\r | |
708 | }\r | |
709 | \r | |
ed7752ec | 710 | /**\r |
711 | The internal implementation of Tiano decompress GetInfo.\r | |
712 | \r | |
713 | @param InputSection Buffer containing the input GUIDed section to be processed. \r | |
714 | @param OutputBufferSize The size of OutputBuffer.\r | |
715 | @param ScratchBufferSize The size of ScratchBuffer.\r | |
716 | @param SectionAttribute The attribute of the input guided section.\r | |
717 | \r | |
718 | @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.\r | |
719 | @retval RETURN_INVALID_PARAMETER - The source data is corrupted\r | |
720 | The GUID in InputSection does not match this instance guid.\r | |
721 | **/\r | |
54bd896e | 722 | RETURN_STATUS\r |
723 | EFIAPI\r | |
18fd8d65 LG |
724 | TianoDecompressGetInfo (\r |
725 | IN CONST VOID *InputSection,\r | |
726 | OUT UINT32 *OutputBufferSize,\r | |
727 | OUT UINT32 *ScratchBufferSize,\r | |
728 | OUT UINT16 *SectionAttribute\r | |
54bd896e | 729 | )\r |
54bd896e | 730 | \r |
54bd896e | 731 | {\r |
18fd8d65 LG |
732 | ASSERT (SectionAttribute != NULL);\r |
733 | \r | |
734 | if (InputSection == NULL) {\r | |
735 | return RETURN_INVALID_PARAMETER;\r | |
d8c79a81 | 736 | }\r |
e6c560aa LG |
737 | \r |
738 | if (!CompareGuid (\r | |
739 | &gTianoCustomDecompressGuid, \r | |
740 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r | |
741 | return RETURN_INVALID_PARAMETER;\r | |
742 | }\r | |
18fd8d65 LG |
743 | //\r |
744 | // Get guid attribute of guid section. \r | |
745 | //\r | |
746 | *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r | |
747 | \r | |
748 | //\r | |
749 | // Call Tiano GetInfo to get the required size info.\r | |
750 | //\r | |
751 | return UefiDecompressGetInfo (\r | |
752 | (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r | |
9c294371 | 753 | (*(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r |
18fd8d65 LG |
754 | OutputBufferSize,\r |
755 | ScratchBufferSize\r | |
756 | );\r | |
54bd896e | 757 | }\r |
758 | \r | |
ed7752ec | 759 | /**\r |
760 | The implementation of Tiano Decompress().\r | |
761 | \r | |
762 | @param InputSection Buffer containing the input GUIDed section to be processed. \r | |
763 | @param OutputBuffer OutputBuffer to point to the start of the section's contents.\r | |
764 | if guided data is not prcessed. Otherwise,\r | |
765 | OutputBuffer to contain the output data, which is allocated by the caller.\r | |
766 | @param ScratchBuffer A pointer to a caller-allocated buffer for function internal use. \r | |
767 | @param AuthenticationStatus A pointer to a caller-allocated UINT32 that indicates the\r | |
768 | authentication status of the output buffer. \r | |
769 | \r | |
770 | @retval RETURN_SUCCESS Decompression is successfull\r | |
771 | @retval RETURN_INVALID_PARAMETER The source data is corrupted, or\r | |
772 | The GUID in InputSection does not match this instance guid.\r | |
773 | \r | |
bcd70414 | 774 | **/\r |
54bd896e | 775 | RETURN_STATUS\r |
776 | EFIAPI\r | |
18fd8d65 LG |
777 | TianoDecompress (\r |
778 | IN CONST VOID *InputSection,\r | |
779 | OUT VOID **OutputBuffer,\r | |
780 | IN VOID *ScratchBuffer, OPTIONAL\r | |
781 | OUT UINT32 *AuthenticationStatus\r | |
54bd896e | 782 | )\r |
54bd896e | 783 | {\r |
18fd8d65 LG |
784 | ASSERT (OutputBuffer != NULL);\r |
785 | \r | |
786 | if (InputSection == NULL) {\r | |
787 | return RETURN_INVALID_PARAMETER;\r | |
e6c560aa LG |
788 | }\r |
789 | \r | |
790 | if (!CompareGuid (\r | |
791 | &gTianoCustomDecompressGuid, \r | |
792 | &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r | |
793 | return RETURN_INVALID_PARAMETER;\r | |
794 | }\r | |
795 | \r | |
18fd8d65 LG |
796 | //\r |
797 | // Set Authentication to Zero.\r | |
798 | //\r | |
799 | *AuthenticationStatus = 0;\r | |
800 | \r | |
801 | //\r | |
802 | // Call Tiano Decompress to get the raw data\r | |
803 | //\r | |
804 | return UefiTianoDecompress (\r | |
805 | (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r | |
806 | *OutputBuffer,\r | |
807 | ScratchBuffer,\r | |
808 | 2\r | |
809 | );\r | |
54bd896e | 810 | }\r |
d8c79a81 | 811 | \r |
ed7752ec | 812 | /**\r |
18fd8d65 | 813 | Register TianoDecompress handler.\r |
ed7752ec | 814 | \r |
18fd8d65 | 815 | @retval RETURN_SUCCESS Register successfully.\r |
ed7752ec | 816 | @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.\r |
d8c79a81 | 817 | **/\r |
18fd8d65 | 818 | EFI_STATUS\r |
d8c79a81 | 819 | EFIAPI\r |
18fd8d65 LG |
820 | TianoDecompressLibConstructor (\r |
821 | )\r | |
d8c79a81 | 822 | {\r |
18fd8d65 LG |
823 | return ExtractGuidedSectionRegisterHandlers (\r |
824 | &gTianoCustomDecompressGuid,\r | |
825 | TianoDecompressGetInfo,\r | |
826 | TianoDecompress\r | |
827 | ); \r | |
6bee1632 | 828 | }\r |