31#define WNDSIZ (1U << WNDBIT)
33#define BLKSIZ (1U << 14)
34#define PERC_FLAG 0x8000U
37#define MAX_HASH_VAL (3 * WNDSIZ + (WNDSIZ / 512 + 1) * UINT8_MAX)
38#define HASH(LoopVar7, LoopVar5) ((LoopVar7) + ((LoopVar5) << (WNDBIT - 9)) + WNDSIZ * 2)
40#define UPDATE_CRC(LoopVar5) mCrc = mCrcTable[(mCrc ^ (LoopVar5)) & 0xFF] ^ (mCrc >> UINT8_BIT)
45#define NC (UINT8_MAX + MAXMATCH + 2 - THRESHOLD)
47#define NP (WNDBIT + 1)
49#define NT (CODE_BIT + 3)
75STATIC UINT8 *mSrcUpperLimit;
76STATIC UINT8 *mDstUpperLimit;
102STATIC UINT16 mLeft[2 * NC - 1];
103STATIC UINT16 mRight[2 * NC - 1];
104STATIC UINT16 mCrcTable[UINT8_MAX + 1];
105STATIC UINT16 mCFreq[2 * NC - 1];
107STATIC UINT16 mPFreq[2 * NP - 1];
108STATIC UINT16 mPTCode[NPT];
109STATIC UINT16 mTFreq[2 * NT - 1];
118INT32 mHuffmanDepth = 0;
135 for (LoopVar1 = 0; LoopVar1 <= UINT8_MAX; LoopVar1++) {
137 for (LoopVar2 = 0; LoopVar2 < UINT8_BIT; LoopVar2++) {
138 if ((LoopVar4 & 1) != 0) {
139 LoopVar4 = (LoopVar4 >> 1) ^ CRCPOLY;
145 mCrcTable[LoopVar1] = (UINT16)LoopVar4;
159 if (mDst < mDstUpperLimit) {
160 *mDst++ = (UINT8)(((UINT8)(Data)) & 0xff);
163 if (mDst < mDstUpperLimit) {
164 *mDst++ = (UINT8)(((UINT8)(Data >> 0x08)) & 0xff);
167 if (mDst < mDstUpperLimit) {
168 *mDst++ = (UINT8)(((UINT8)(Data >> 0x10)) & 0xff);
171 if (mDst < mDstUpperLimit) {
172 *mDst++ = (UINT8)(((UINT8)(Data >> 0x18)) & 0xff);
189 mChildCount =
AllocateZeroPool ((WNDSIZ + UINT8_MAX + 1) *
sizeof (*mChildCount));
190 mPosition =
AllocateZeroPool ((WNDSIZ + UINT8_MAX + 1) *
sizeof (*mPosition));
197 while (mBuf ==
NULL) {
198 mBufSiz = (mBufSiz / 10U) * 9U;
199 if (mBufSiz < 4 * 1024U) {
200 return EFI_OUT_OF_RESOURCES;
220 SHELL_FREE_NON_NULL (mText);
221 SHELL_FREE_NON_NULL (mLevel);
222 SHELL_FREE_NON_NULL (mChildCount);
223 SHELL_FREE_NON_NULL (mPosition);
224 SHELL_FREE_NON_NULL (mParent);
225 SHELL_FREE_NON_NULL (mPrev);
226 SHELL_FREE_NON_NULL (mNext);
227 SHELL_FREE_NON_NULL (mBuf);
240 SetMem (mLevel + WNDSIZ, (UINT8_MAX + 1) *
sizeof (UINT8), 1);
241 SetMem (mPosition + WNDSIZ, (UINT8_MAX + 1) *
sizeof (NODE), 0);
243 SetMem (mParent + WNDSIZ, WNDSIZ *
sizeof (NODE), 0);
246 for (LoopVar1 = 1; LoopVar1 < WNDSIZ - 1; LoopVar1++) {
247 mNext[LoopVar1] = (NODE)(LoopVar1 + 1);
250 mNext[WNDSIZ - 1] = NIL;
251 SetMem (mNext + WNDSIZ * 2, (MAX_HASH_VAL - WNDSIZ * 2 + 1) *
sizeof (NODE), 0);
272 LoopVar4 = mNext[HASH (LoopVar6, LoopVar5)];
273 mParent[NIL] = LoopVar6;
274 while (mParent[LoopVar4] != LoopVar6) {
275 LoopVar4 = mNext[LoopVar4];
299 LoopVar12 = (NODE)HASH (LoopVar6, LoopVar5);
300 LoopVar10 = mNext[LoopVar12];
301 mNext[LoopVar12] = LoopVar4;
302 mNext[LoopVar4] = LoopVar10;
303 mPrev[LoopVar10] = LoopVar4;
304 mPrev[LoopVar4] = LoopVar12;
305 mParent[LoopVar4] = LoopVar6;
306 mChildCount[LoopVar6]++;
325 mChildCount[New] = 0;
326 LoopVar10 = mPrev[Old];
327 mPrev[New] = LoopVar10;
328 mNext[LoopVar10] = New;
329 LoopVar10 = mNext[Old];
330 mNext[New] = LoopVar10;
331 mPrev[LoopVar10] = New;
332 mParent[New] = mParent[Old];
333 mLevel[New] = (UINT8)mMatchLen;
334 mPosition[New] = mPos;
335 MakeChild (New, mText[mMatchPos + mMatchLen], Old);
336 MakeChild (New, mText[mPos + mMatchLen], mPos);
359 if (mMatchLen >= 4) {
368 LoopVar4 = (NODE)((mMatchPos + 1) | WNDSIZ);
369 LoopVar6 = mParent[LoopVar4];
370 while (LoopVar6 == NIL) {
371 LoopVar4 = mNext[LoopVar4];
372 LoopVar6 = mParent[LoopVar4];
375 while (mLevel[LoopVar6] >= mMatchLen) {
377 LoopVar6 = mParent[LoopVar6];
380 LoopVar10 = LoopVar6;
381 while (mPosition[LoopVar10] < 0) {
382 mPosition[LoopVar10] = mPos;
383 LoopVar10 = mParent[LoopVar10];
386 if (LoopVar10 < WNDSIZ) {
387 mPosition[LoopVar10] = (NODE)(mPos | PERC_FLAG);
393 LoopVar6 = (NODE)(mText[mPos] + WNDSIZ);
394 LoopVar5 = mText[mPos + 1];
395 LoopVar4 =
Child (LoopVar6, LoopVar5);
396 if (LoopVar4 == NIL) {
411 if (LoopVar4 >= WNDSIZ) {
413 mMatchPos = LoopVar4;
415 LoopVar2 = mLevel[LoopVar4];
416 mMatchPos = (NODE)(mPosition[LoopVar4] & ~PERC_FLAG);
419 if (mMatchPos >= mPos) {
423 TempString3 = &mText[mPos + mMatchLen];
424 TempString2 = &mText[mMatchPos + mMatchLen];
425 while (mMatchLen < LoopVar2) {
426 if (*TempString3 != *TempString2) {
436 if (mMatchLen >= MAXMATCH) {
440 mPosition[LoopVar4] = mPos;
442 LoopVar4 =
Child (LoopVar6, *TempString3);
443 if (LoopVar4 == NIL) {
444 MakeChild (LoopVar6, *TempString3, mPos);
451 LoopVar10 = mPrev[LoopVar4];
452 mPrev[mPos] = LoopVar10;
453 mNext[LoopVar10] = mPos;
454 LoopVar10 = mNext[LoopVar4];
455 mNext[mPos] = LoopVar10;
456 mPrev[LoopVar10] = mPos;
457 mParent[mPos] = LoopVar6;
458 mParent[LoopVar4] = NIL;
463 mNext[LoopVar4] = mPos;
486 if (mParent[mPos] == NIL) {
490 LoopVar4 = mPrev[mPos];
491 LoopVar11 = mNext[mPos];
492 mNext[LoopVar4] = LoopVar11;
493 mPrev[LoopVar11] = LoopVar4;
494 LoopVar4 = mParent[mPos];
496 if (LoopVar4 >= WNDSIZ) {
500 mChildCount[LoopVar4]--;
501 if (mChildCount[LoopVar4] > 1) {
505 LoopVar10 = (NODE)(mPosition[LoopVar4] & ~PERC_FLAG);
506 if (LoopVar10 >= mPos) {
510 LoopVar11 = LoopVar10;
511 LoopVar6 = mParent[LoopVar4];
512 LoopVar9 = mPosition[LoopVar6];
513 while ((LoopVar9 & PERC_FLAG) != 0) {
514 LoopVar9 &= ~PERC_FLAG;
515 if (LoopVar9 >= mPos) {
519 if (LoopVar9 > LoopVar11) {
520 LoopVar11 = LoopVar9;
523 mPosition[LoopVar6] = (NODE)(LoopVar11 | WNDSIZ);
524 LoopVar6 = mParent[LoopVar6];
525 LoopVar9 = mPosition[LoopVar6];
528 if (LoopVar6 < WNDSIZ) {
529 if (LoopVar9 >= mPos) {
533 if (LoopVar9 > LoopVar11) {
534 LoopVar11 = LoopVar9;
537 mPosition[LoopVar6] = (NODE)(LoopVar11 | WNDSIZ | PERC_FLAG);
540 LoopVar11 =
Child (LoopVar4, mText[LoopVar10 + mLevel[LoopVar4]]);
541 LoopVar10 = mPrev[LoopVar11];
542 LoopVar9 = mNext[LoopVar11];
543 mNext[LoopVar10] = LoopVar9;
544 mPrev[LoopVar9] = LoopVar10;
545 LoopVar10 = mPrev[LoopVar4];
546 mNext[LoopVar10] = LoopVar11;
547 mPrev[LoopVar11] = LoopVar10;
548 LoopVar10 = mNext[LoopVar4];
549 mPrev[LoopVar10] = LoopVar11;
550 mNext[LoopVar11] = LoopVar10;
551 mParent[LoopVar11] = mParent[LoopVar4];
552 mParent[LoopVar4] = NIL;
553 mNext[LoopVar4] = mAvail;
573 for (LoopVar1 = 0; mSrc < mSrcUpperLimit && LoopVar1 < LoopVar8; LoopVar1++) {
574 *LoopVar7++ = *mSrc++;
579 LoopVar7 -= LoopVar8;
580 mOrigSize += LoopVar8;
582 while (LoopVar1 >= 0) {
583 UPDATE_CRC (*LoopVar7++);
607 if (mPos == WNDSIZ * 2) {
613 CopyMem (Temp, &mText[WNDSIZ], WNDSIZ + MAXMATCH);
614 CopyMem (&mText[0], Temp, WNDSIZ + MAXMATCH);
616 LoopVar8 =
FreadCrc (&mText[WNDSIZ + MAXMATCH], WNDSIZ);
617 mRemainder += LoopVar8;
646 while (LoopVar1 <= mHeapSize) {
647 if ((LoopVar1 < mHeapSize) && (mFreq[mHeap[LoopVar1]] > mFreq[mHeap[LoopVar1 + 1]])) {
651 if (mFreq[LoopVar2] <= mFreq[mHeap[LoopVar1]]) {
655 mHeap[i] = mHeap[LoopVar1];
660 mHeap[i] = (INT16)LoopVar2;
673 if (LoopVar1 < mTempInt32) {
674 mLenCnt[(mHuffmanDepth < 16) ? mHuffmanDepth : 16]++;
698 for (LoopVar1 = 0; LoopVar1 <= 16; LoopVar1++) {
699 mLenCnt[LoopVar1] = 0;
709 for (LoopVar1 = 16; LoopVar1 > 0; LoopVar1--) {
710 Cum += mLenCnt[LoopVar1] << (16 - LoopVar1);
713 while (Cum != (1U << 16)) {
715 for (LoopVar1 = 15; LoopVar1 > 0; LoopVar1--) {
716 if (mLenCnt[LoopVar1] != 0) {
718 mLenCnt[LoopVar1 + 1] += 2;
726 for (LoopVar1 = 16; LoopVar1 > 0; LoopVar1--) {
727 LoopVar2 = mLenCnt[LoopVar1];
729 while (LoopVar2 >= 0) {
730 mLen[*mSortPtr++] = (UINT8)LoopVar1;
754 for (LoopVar1 = 1; LoopVar1 <= 16; LoopVar1++) {
755 Start[LoopVar1 + 1] = (UINT16)((Start[LoopVar1] + mLenCnt[LoopVar1]) << 1);
758 for (LoopVar1 = 0; LoopVar1 < LoopVar8; LoopVar1++) {
759 Code[LoopVar1] = Start[Len[LoopVar1]]++;
776 IN UINT16 FreqParm[],
778 OUT UINT16 CodeParm[]
798 for (LoopVar1 = 0; LoopVar1 < mTempInt32; LoopVar1++) {
800 if ((mFreq[LoopVar1]) != 0) {
802 mHeap[mHeapSize] = (INT16)LoopVar1;
807 CodeParm[mHeap[1]] = 0;
811 for (LoopVar1 = mHeapSize / 2; LoopVar1 >= 1; LoopVar1--) {
821 if (LoopVar1 < mTempInt32) {
822 *mSortPtr++ = (UINT16)LoopVar1;
825 mHeap[1] = mHeap[mHeapSize--];
828 if (LoopVar2 < mTempInt32) {
829 *mSortPtr++ = (UINT16)LoopVar2;
833 mFreq[LoopVar3] = (UINT16)(mFreq[LoopVar1] + mFreq[LoopVar2]);
834 mHeap[1] = (INT16)LoopVar3;
836 mLeft[LoopVar3] = (UINT16)LoopVar1;
837 mRight[LoopVar3] = (UINT16)LoopVar2;
838 }
while (mHeapSize > 1);
842 MakeCode (NParm, LenParm, CodeParm);
864 if (LoopVar8 < mBitCount) {
865 mSubBitBuf |= x << (mBitCount -= LoopVar8);
867 Temp = (UINT8)(mSubBitBuf | (x >> (LoopVar8 -= mBitCount)));
868 if (mDst < mDstUpperLimit) {
874 if (LoopVar8 < UINT8_BIT) {
875 mSubBitBuf = x << (mBitCount = UINT8_BIT - LoopVar8);
877 Temp = (UINT8)(x >> (LoopVar8 - UINT8_BIT));
878 if (mDst < mDstUpperLimit) {
884 mSubBitBuf = x << (mBitCount = 2 * UINT8_BIT - LoopVar8);
899 PutBits (mCLen[LoopVar5], mCCode[LoopVar5]);
918 while (LoopVar6 != 0) {
923 PutBits (mPTLen[LoopVar5], mPTCode[LoopVar5]);
925 PutBits (LoopVar5 - 1, LoopVar7 & (0xFFFFU >> (17 - LoopVar5)));
946 for (LoopVar1 = 0; LoopVar1 < NT; LoopVar1++) {
947 mTFreq[LoopVar1] = 0;
951 while (LoopVar8 > 0 && mCLen[LoopVar8 - 1] == 0) {
956 while (LoopVar1 < LoopVar8) {
957 LoopVar3 = mCLen[LoopVar1++];
960 while (LoopVar1 < LoopVar8 && mCLen[LoopVar1] == 0) {
966 mTFreq[0] = (UINT16)(mTFreq[0] + Count);
967 }
else if (Count <= 18) {
969 }
else if (Count == 19) {
976 ASSERT ((LoopVar3+2) < (2 * NT - 1));
977 mTFreq[LoopVar3 + 2]++;
1001 while (LoopVar8 > 0 && mPTLen[LoopVar8 - 1] == 0) {
1007 while (LoopVar1 < LoopVar8) {
1008 LoopVar3 = mPTLen[LoopVar1++];
1009 if (LoopVar3 <= 6) {
1012 PutBits (LoopVar3 - 3, (1U << (LoopVar3 - 3)) - 2);
1015 if (LoopVar1 == Special) {
1016 while (LoopVar1 < 6 && mPTLen[LoopVar1] == 0) {
1020 PutBits (2, (LoopVar1 - 3) & 3);
1042 while (LoopVar8 > 0 && mCLen[LoopVar8 - 1] == 0) {
1048 while (LoopVar1 < LoopVar8) {
1049 LoopVar3 = mCLen[LoopVar1++];
1050 if (LoopVar3 == 0) {
1052 while (LoopVar1 < LoopVar8 && mCLen[LoopVar1] == 0) {
1058 for (LoopVar3 = 0; LoopVar3 < Count; LoopVar3++) {
1059 PutBits (mPTLen[0], mPTCode[0]);
1061 }
else if (Count <= 18) {
1062 PutBits (mPTLen[1], mPTCode[1]);
1064 }
else if (Count == 19) {
1065 PutBits (mPTLen[0], mPTCode[0]);
1066 PutBits (mPTLen[1], mPTCode[1]);
1069 PutBits (mPTLen[2], mPTCode[2]);
1073 ASSERT ((LoopVar3+2) < NPT);
1074 PutBits (mPTLen[LoopVar3 + 2], mPTCode[LoopVar3 + 2]);
1102 Root =
MakeTree (NC, mCFreq, mCLen, mCCode);
1103 Size = mCFreq[Root];
1107 Root =
MakeTree (NT, mTFreq, mPTLen, mPTCode);
1123 Root =
MakeTree (NP, mPFreq, mPTLen, mPTCode);
1132 for (LoopVar1 = 0; LoopVar1 < Size; LoopVar1++) {
1133 if (LoopVar1 % UINT8_BIT == 0) {
1134 Flags = mBuf[Pos++];
1139 if ((Flags & (1U << (UINT8_BIT - 1))) != 0) {
1140 EncodeC (mBuf[Pos++] + (1U << UINT8_BIT));
1141 LoopVar3 = mBuf[Pos++] << UINT8_BIT;
1142 LoopVar3 += mBuf[Pos++];
1150 SetMem (mCFreq, NC *
sizeof (UINT16), 0);
1151 SetMem (mPFreq, NP *
sizeof (UINT16), 0);
1163 SetMem (mCFreq, NC *
sizeof (UINT16), 0);
1164 SetMem (mPFreq, NP *
sizeof (UINT16), 0);
1166 mOutputPos = mOutputMask = 0;
1168 mBitCount = UINT8_BIT;
1187 if ((mOutputMask >>= 1) == 0) {
1188 mOutputMask = 1U << (UINT8_BIT - 1);
1189 if (mOutputPos >= mBufSiz - 3 * UINT8_BIT) {
1194 CPos = mOutputPos++;
1198 mBuf[mOutputPos++] = (UINT8)LoopVar5;
1200 if (LoopVar5 >= (1U << UINT8_BIT)) {
1201 mBuf[CPos] = (UINT8)(mBuf[CPos]|mOutputMask);
1202 mBuf[mOutputPos++] = (UINT8)(LoopVar7 >> UINT8_BIT);
1203 mBuf[mOutputPos++] = (UINT8)LoopVar7;
1205 while (LoopVar7 != 0) {
1247 if (EFI_ERROR (Status)) {
1256 mRemainder =
FreadCrc (&mText[WNDSIZ], WNDSIZ + MAXMATCH);
1261 if (mMatchLen > mRemainder) {
1262 mMatchLen = mRemainder;
1265 while (mRemainder > 0) {
1266 LastMatchLen = mMatchLen;
1267 LastMatchPos = mMatchPos;
1269 Status = EFI_OUT_OF_RESOURCES;
1272 if (mMatchLen > mRemainder) {
1273 mMatchLen = mRemainder;
1276 if ((mMatchLen > LastMatchLen) || (LastMatchLen < THRESHOLD)) {
1288 LastMatchLen + (UINT8_MAX + 1 - THRESHOLD),
1289 (mPos - LastMatchPos - 2) & (WNDSIZ - 1)
1292 while (LastMatchLen > 0) {
1294 Status = EFI_OUT_OF_RESOURCES;
1300 if (mMatchLen > mRemainder) {
1301 mMatchLen = mRemainder;
1328 IN OUT UINT64 *DstSize
1347 mSrcUpperLimit = mSrc + SrcSize;
1349 mDstUpperLimit = mDst +*DstSize;
1356 mOrigSize = mCompSize = 0;
1363 if (EFI_ERROR (Status)) {
1364 return EFI_OUT_OF_RESOURCES;
1370 if (mDst < mDstUpperLimit) {
1384 if (mCompSize + 1 + 8 > *DstSize) {
1385 *DstSize = mCompSize + 1 + 8;
1386 return EFI_BUFFER_TOO_SMALL;
1388 *DstSize = mCompSize + 1 + 8;
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
VOID PutDword(IN UINT32 Data)
VOID MakeCode(IN INT32 LoopVar8, IN UINT8 Len[], OUT UINT16 Code[])
VOID MakeLen(IN INT32 Root)
VOID PutBits(IN INT32 LoopVar8, IN UINT32 x)
VOID EncodeC(IN INT32 LoopVar5)
INT32 FreadCrc(OUT UINT8 *LoopVar7, IN INT32 LoopVar8)
VOID CompressOutput(IN UINT32 LoopVar5, IN UINT32 LoopVar7)
EFI_STATUS Compress(IN VOID *SrcBuffer, IN UINT64 SrcSize, IN VOID *DstBuffer, IN OUT UINT64 *DstSize)
VOID HufEncodeStart(VOID)
VOID MakeChild(IN NODE LoopVar6, IN UINT8 LoopVar5, IN NODE LoopVar4)
INT32 MakeTree(IN INT32 NParm, IN UINT16 FreqParm[], OUT UINT8 LenParm[], OUT UINT16 CodeParm[])
VOID CountLen(IN INT32 LoopVar1)
VOID DownHeap(IN INT32 i)
VOID WritePTLen(IN INT32 LoopVar8, IN INT32 nbit, IN INT32 Special)
NODE Child(IN NODE LoopVar6, IN UINT8 LoopVar5)
BOOLEAN GetNextMatch(VOID)
VOID EncodeP(IN UINT32 LoopVar7)
EFI_STATUS AllocateMemory(VOID)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)