diff options
author | jcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524> | 2011-03-25 21:22:20 +0000 |
---|---|---|
committer | jcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524> | 2011-03-25 21:22:20 +0000 |
commit | 3737ac2bc3c2e6589d88be97345d641ea2006933 (patch) | |
tree | d2798373c799cb9c728d2ec4e7b1bb39f2d59d11 /ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c | |
parent | 2442e62af75a0c5087fce3fb2040e26a485b0d31 (diff) | |
download | edk2-platforms-3737ac2bc3c2e6589d88be97345d641ea2006933.tar.xz |
comp - add comments and add input verification
bcfg - updated for bugs.
compress - rename for coding standards. add comments.
dblk - add comments, input verification, and a header line
dmem - add comments, add input verification, add system table info
dmpstore - add comments
eficompress - add comments and add input verification
efidecompress - add comments and add input verification
loadpcirom - add comments and more output messages
memmap - add more output to exceed the spec.
mm - move functions, add comments, add input verification.
mode - add comment
pci - add input verification.
SerMode - add comments and add input verification
setsize - add comments and add input verification
setvar - add comments and add input verification
smbiosview - add input verification.
clarify error messages.
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11438 6f19259b-4bc3-4df7-8a09-765794883524
Diffstat (limited to 'ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c')
-rw-r--r-- | ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c | 1883 |
1 files changed, 790 insertions, 1093 deletions
diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c index 9e880e6d6b..870335b9a1 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c @@ -7,7 +7,7 @@ This sequence is further divided into Blocks and Huffman codings
are applied to each Block.
- Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
+ Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@@ -39,9 +39,9 @@ typedef INT16 NODE; #define CODE_BIT 16
#define NIL 0
#define MAX_HASH_VAL (3 * WNDSIZ + (WNDSIZ / 512 + 1) * UINT8_MAX)
-#define HASH(p, c) ((p) + ((c) << (WNDBIT - 9)) + WNDSIZ * 2)
+#define HASH(LoopVar7, LoopVar5) ((LoopVar7) + ((LoopVar5) << (WNDBIT - 9)) + WNDSIZ * 2)
#define CRCPOLY 0xA001
-#define UPDATE_CRC(c) mCrc = mCrcTable[(mCrc ^ (c)) & 0xFF] ^ (mCrc >> UINT8_BIT)
+#define UPDATE_CRC(LoopVar5) mCrc = mCrcTable[(mCrc ^ (LoopVar5)) & 0xFF] ^ (mCrc >> UINT8_BIT)
//
// C: the Char&Len Set; P: the Position Set; T: the exTra Set
@@ -72,287 +72,85 @@ PutDword( IN UINT32 Data
);
-EFI_STATUS
-EFIAPI
-AllocateMemory (
- VOID
- );
-
-VOID
-EFIAPI
-FreeMemory (
- VOID
- );
-
-VOID
-EFIAPI
-InitSlide (
- VOID
- );
-
-NODE
-EFIAPI
-Child (
- IN NODE q,
- IN UINT8 c
- );
-
-VOID
-EFIAPI
-MakeChild (
- IN NODE q,
- IN UINT8 c,
- IN NODE r
- );
-
-VOID
-EFIAPI
-Split (
- IN NODE Old
- );
-
-VOID
-EFIAPI
-InsertNode (
- VOID
- );
-
-VOID
-EFIAPI
-DeleteNode (
- VOID
- );
-
-VOID
-EFIAPI
-GetNextMatch (
- VOID
- );
-
-EFI_STATUS
-EFIAPI
-Encode (
- VOID
- );
-
-VOID
-EFIAPI
-CountTFreq (
- VOID
- );
-
-VOID
-EFIAPI
-WritePTLen (
- IN INT32 n,
- IN INT32 nbit,
- IN INT32 Special
- );
-
-VOID
-EFIAPI
-WriteCLen (
- VOID
- );
-
-VOID
-EFIAPI
-EncodeC (
- IN INT32 c
- );
-
-VOID
-EFIAPI
-EncodeP (
- IN UINT32 p
- );
-
-VOID
-EFIAPI
-SendBlock (
- VOID
- );
-
-VOID
-EFIAPI
-CompressOutput (
- IN UINT32 c,
- IN UINT32 p
- );
-
-VOID
-EFIAPI
-HufEncodeStart (
- VOID
- );
-
-VOID
-EFIAPI
-HufEncodeEnd (
- VOID
- );
-
-VOID
-EFIAPI
-MakeCrcTable (
- VOID
- );
-
-VOID
-EFIAPI
-PutBits (
- IN INT32 n,
- IN UINT32 x
- );
-
-INT32
-EFIAPI
-FreadCrc (
- OUT UINT8 *p,
- IN INT32 n
- );
-
-VOID
-EFIAPI
-InitPutBits (
- VOID
- );
-
-VOID
-EFIAPI
-CountLen (
- IN INT32 i
- );
-
-VOID
-EFIAPI
-MakeLen (
- IN INT32 Root
- );
-
-VOID
-EFIAPI
-DownHeap (
- IN INT32 i
- );
-
-VOID
-EFIAPI
-MakeCode (
- IN INT32 n,
- IN UINT8 Len[ ],
- OUT UINT16 Code[ ]
- );
-
-INT32
-EFIAPI
-MakeTree (
- IN INT32 NParm,
- IN UINT16 FreqParm[ ],
- OUT UINT8 LenParm[ ],
- OUT UINT16 CodeParm[ ]
- );
-
//
// Global Variables
//
-STATIC UINT8 *mSrc, *mDst, *mSrcUpperLimit, *mDstUpperLimit;
-
-STATIC UINT8 *mLevel, *mText, *mChildCount, *mBuf, mCLen[NC], mPTLen[NPT], *mLen;
+STATIC UINT8 *mSrc;
+STATIC UINT8 *mDst;
+STATIC UINT8 *mSrcUpperLimit;
+STATIC UINT8 *mDstUpperLimit;
+
+STATIC UINT8 *mLevel;
+STATIC UINT8 *mText;
+STATIC UINT8 *mChildCount;
+STATIC UINT8 *mBuf;
+STATIC UINT8 mCLen[NC];
+STATIC UINT8 mPTLen[NPT];
+STATIC UINT8 *mLen;
STATIC INT16 mHeap[NC + 1];
-STATIC INT32 mRemainder, mMatchLen, mBitCount, mHeapSize, mN;
-STATIC UINT32 mBufSiz = 0, mOutputPos, mOutputMask, mSubBitBuf, mCrc;
-STATIC UINT32 mCompSize, mOrigSize;
-
-STATIC UINT16 *mFreq, *mSortPtr, mLenCnt[17], mLeft[2 * NC - 1], mRight[2 * NC - 1],
- mCrcTable[UINT8_MAX + 1], mCFreq[2 * NC - 1], mCTable[4096], mCCode[NC],
- mPFreq[2 * NP - 1], mPTCode[NPT], mTFreq[2 * NT - 1];
+STATIC INT32 mRemainder;
+STATIC INT32 mMatchLen;
+STATIC INT32 mBitCount;
+STATIC INT32 mHeapSize;
+STATIC INT32 mTempInt32;
+STATIC UINT32 mBufSiz = 0;
+STATIC UINT32 mOutputPos;
+STATIC UINT32 mOutputMask;
+STATIC UINT32 mSubBitBuf;
+STATIC UINT32 mCrc;
+STATIC UINT32 mCompSize;
+STATIC UINT32 mOrigSize;
+
+STATIC UINT16 *mFreq;
+STATIC UINT16 *mSortPtr;
+STATIC UINT16 mLenCnt[17];
+STATIC UINT16 mLeft[2 * NC - 1];
+STATIC UINT16 mRight[2 * NC - 1];
+STATIC UINT16 mCrcTable[UINT8_MAX + 1];
+STATIC UINT16 mCFreq[2 * NC - 1];
+STATIC UINT16 mCTable[4096];
+STATIC UINT16 mCCode[NC];
+STATIC UINT16 mPFreq[2 * NP - 1];
+STATIC UINT16 mPTCode[NPT];
+STATIC UINT16 mTFreq[2 * NT - 1];
+
+STATIC NODE mPos;
+STATIC NODE mMatchPos;
+STATIC NODE mAvail;
+STATIC NODE *mPosition;
+STATIC NODE *mParent;
+STATIC NODE *mPrev;
+STATIC NODE *mNext = NULL;
+INT32 mHuffmanDepth = 0;
-STATIC NODE mPos, mMatchPos, mAvail, *mPosition, *mParent, *mPrev, *mNext = NULL;
-
-//
-// functions
-//
/**
- The compression routine.
+ Make a CRC table.
- @param[in] SrcBuffer The buffer containing the source data.
- @param[in] SrcSizae Number of bytes in SrcBuffer.
- @param[in] DstBuffer The buffer to put the compressed image in.
- @param[in,out] DstSize On input the size (in bytes) of DstBuffer, on
- return the number of bytes placed in DstBuffer.
-
- @retval EFI_SUCCESS The compression was sucessful.
- @retval EFI_BUFFER_TOO_SMALL The buffer was too small. DstSize is required.
**/
-EFI_STATUS
+VOID
EFIAPI
-Compress (
- IN VOID *SrcBuffer,
- IN UINT64 SrcSize,
- IN VOID *DstBuffer,
- IN OUT UINT64 *DstSize
+MakeCrcTable (
+ VOID
)
{
- EFI_STATUS Status;
-
- //
- // Initializations
- //
- mBufSiz = 0;
- mBuf = NULL;
- mText = NULL;
- mLevel = NULL;
- mChildCount = NULL;
- mPosition = NULL;
- mParent = NULL;
- mPrev = NULL;
- mNext = NULL;
+ UINT32 LoopVar1;
- mSrc = SrcBuffer;
- mSrcUpperLimit = mSrc + SrcSize;
- mDst = DstBuffer;
- mDstUpperLimit = mDst +*DstSize;
+ UINT32 LoopVar2;
- PutDword (0L);
- PutDword (0L);
-
- MakeCrcTable ();
-
- mOrigSize = mCompSize = 0;
- mCrc = INIT_CRC;
+ UINT32 LoopVar4;
- //
- // Compress it
- //
- Status = Encode ();
- if (EFI_ERROR (Status)) {
- return EFI_OUT_OF_RESOURCES;
- }
- //
- // Null terminate the compressed data
- //
- if (mDst < mDstUpperLimit) {
- *mDst++ = 0;
- }
- //
- // Fill in compressed size and original size
- //
- mDst = DstBuffer;
- PutDword (mCompSize + 1);
- PutDword (mOrigSize);
+ for (LoopVar1 = 0; LoopVar1 <= UINT8_MAX; LoopVar1++) {
+ LoopVar4 = LoopVar1;
+ for (LoopVar2 = 0; LoopVar2 < UINT8_BIT; LoopVar2++) {
+ if ((LoopVar4 & 1) != 0) {
+ LoopVar4 = (LoopVar4 >> 1) ^ CRCPOLY;
+ } else {
+ LoopVar4 >>= 1;
+ }
+ }
- //
- // Return
- //
- if (mCompSize + 1 + 8 > *DstSize) {
- *DstSize = mCompSize + 1 + 8;
- return EFI_BUFFER_TOO_SMALL;
- } else {
- *DstSize = mCompSize + 1 + 8;
- return EFI_SUCCESS;
+ mCrcTable[LoopVar1] = (UINT16) LoopVar4;
}
-
}
/**
@@ -383,45 +181,35 @@ PutDword ( }
}
+/**
+ Allocate memory spaces for data structures used in compression process.
+
+ @retval EFI_SUCCESS Memory was allocated successfully.
+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
+**/
EFI_STATUS
EFIAPI
AllocateMemory (
VOID
)
-/*++
-
-Routine Description:
-
- Allocate memory spaces for data structures used in compression process
-
-Arguments:
-
- None
-
-Returns:
-
- EFI_SUCCESS - Memory is allocated successfully
- EFI_OUT_OF_RESOURCES - Allocation fails
-
-**/
{
mText = AllocateZeroPool (WNDSIZ * 2 + MAXMATCH);
- mLevel = AllocatePool ((WNDSIZ + UINT8_MAX + 1) * sizeof (*mLevel));
- mChildCount = AllocatePool ((WNDSIZ + UINT8_MAX + 1) * sizeof (*mChildCount));
- mPosition = AllocatePool ((WNDSIZ + UINT8_MAX + 1) * sizeof (*mPosition));
- mParent = AllocatePool (WNDSIZ * 2 * sizeof (*mParent));
- mPrev = AllocatePool (WNDSIZ * 2 * sizeof (*mPrev));
- mNext = AllocatePool ((MAX_HASH_VAL + 1) * sizeof (*mNext));
+ mLevel = AllocateZeroPool ((WNDSIZ + UINT8_MAX + 1) * sizeof (*mLevel));
+ mChildCount = AllocateZeroPool ((WNDSIZ + UINT8_MAX + 1) * sizeof (*mChildCount));
+ mPosition = AllocateZeroPool ((WNDSIZ + UINT8_MAX + 1) * sizeof (*mPosition));
+ mParent = AllocateZeroPool (WNDSIZ * 2 * sizeof (*mParent));
+ mPrev = AllocateZeroPool (WNDSIZ * 2 * sizeof (*mPrev));
+ mNext = AllocateZeroPool ((MAX_HASH_VAL + 1) * sizeof (*mNext));
mBufSiz = BLKSIZ;
- mBuf = AllocatePool (mBufSiz);
+ mBuf = AllocateZeroPool (mBufSiz);
while (mBuf == NULL) {
mBufSiz = (mBufSiz / 10U) * 9U;
if (mBufSiz < 4 * 1024U) {
return EFI_OUT_OF_RESOURCES;
}
- mBuf = AllocatePool (mBufSiz);
+ mBuf = AllocateZeroPool (mBufSiz);
}
mBuf[0] = 0;
@@ -429,22 +217,15 @@ Returns: return EFI_SUCCESS;
}
+/**
+ Called when compression is completed to free memory previously allocated.
+
+**/
VOID
EFIAPI
FreeMemory (
VOID
)
-/*++
-
-Routine Description:
-
- Called when compression is completed to free memory previously allocated.
-
-Arguments: (VOID)
-
-Returns: (VOID)
-
-**/
{
SHELL_FREE_NON_NULL (mText);
SHELL_FREE_NON_NULL (mLevel);
@@ -456,24 +237,17 @@ Returns: (VOID) SHELL_FREE_NON_NULL (mBuf);
}
+/**
+ Initialize String Info Log data structures
+
+**/
VOID
EFIAPI
InitSlide (
VOID
)
-/*++
-
-Routine Description:
-
- Initialize String Info Log data structures
-
-Arguments: (VOID)
-
-Returns: (VOID)
-
-**/
{
- NODE i;
+ NODE LoopVar1;
SetMem (mLevel + WNDSIZ, (UINT8_MAX + 1) * sizeof (UINT8), 1);
SetMem (mPosition + WNDSIZ, (UINT8_MAX + 1) * sizeof (NODE), 0);
@@ -481,117 +255,95 @@ Returns: (VOID) SetMem (mParent + WNDSIZ, WNDSIZ * sizeof (NODE), 0);
mAvail = 1;
- for (i = 1; i < WNDSIZ - 1; i++) {
- mNext[i] = (NODE) (i + 1);
+ for (LoopVar1 = 1; LoopVar1 < WNDSIZ - 1; LoopVar1++) {
+ mNext[LoopVar1] = (NODE) (LoopVar1 + 1);
}
mNext[WNDSIZ - 1] = NIL;
SetMem (mNext + WNDSIZ * 2, (MAX_HASH_VAL - WNDSIZ * 2 + 1) * sizeof (NODE), 0);
}
-NODE
-EFIAPI
-Child (
- IN NODE q,
- IN UINT8 c
- )
-/*++
-
-Routine Description:
-
+/**
Find child node given the parent node and the edge character
-Arguments:
-
- q - the parent node
- c - the edge character
-
-Returns:
+ @param[in] LoopVar6 The parent node.
+ @param[in] LoopVar5 The edge character.
- The child node (NIL if not found)
+ @return The child node.
+ @retval NIL(Zero) No child could be found.
**/
+NODE
+EFIAPI
+Child (
+ IN NODE LoopVar6,
+ IN UINT8 LoopVar5
+ )
{
- NODE r;
+ NODE LoopVar4;
- r = mNext[HASH (q, c)];
- mParent[NIL] = q; /* sentinel */
- while (mParent[r] != q) {
- r = mNext[r];
+ LoopVar4 = mNext[HASH (LoopVar6, LoopVar5)];
+ mParent[NIL] = LoopVar6; /* sentinel */
+ while (mParent[LoopVar4] != LoopVar6) {
+ LoopVar4 = mNext[LoopVar4];
}
- return r;
+ return LoopVar4;
}
+/**
+ Create a new child for a given parent node.
+
+ @param[in] LoopVar6 The parent node.
+ @param[in] LoopVar5 The edge character.
+ @param[in] LoopVar4 The child node.
+**/
VOID
EFIAPI
MakeChild (
- IN NODE q,
- IN UINT8 c,
- IN NODE r
+ IN NODE LoopVar6,
+ IN UINT8 LoopVar5,
+ IN NODE LoopVar4
)
-/*++
-
-Routine Description:
-
- Create a new child for a given parent node.
-
-Arguments:
-
- q - the parent node
- c - the edge character
- r - the child node
-
-Returns: (VOID)
-
-**/
{
- NODE h;
-
- NODE t;
-
- h = (NODE) HASH (q, c);
- t = mNext[h];
- mNext[h] = r;
- mNext[r] = t;
- mPrev[t] = r;
- mPrev[r] = h;
- mParent[r] = q;
- mChildCount[q]++;
+ NODE LoopVar12;
+
+ NODE LoopVar10;
+
+ LoopVar12 = (NODE) HASH (LoopVar6, LoopVar5);
+ LoopVar10 = mNext[LoopVar12];
+ mNext[LoopVar12] = LoopVar4;
+ mNext[LoopVar4] = LoopVar10;
+ mPrev[LoopVar10] = LoopVar4;
+ mPrev[LoopVar4] = LoopVar12;
+ mParent[LoopVar4] = LoopVar6;
+ mChildCount[LoopVar6]++;
}
+/**
+ Split a node.
+
+ @param[in] Old The node to split.
+**/
VOID
EFIAPI
Split (
- NODE Old
+ IN NODE Old
)
-/*++
-
-Routine Description:
-
- Split a node.
-
-Arguments:
-
- Old - the node to split
-
-Returns: (VOID)
-
-**/
{
NODE New;
- NODE t;
+ NODE LoopVar10;
New = mAvail;
mAvail = mNext[New];
mChildCount[New] = 0;
- t = mPrev[Old];
- mPrev[New] = t;
- mNext[t] = New;
- t = mNext[Old];
- mNext[New] = t;
- mPrev[t] = New;
+ LoopVar10 = mPrev[Old];
+ mPrev[New] = LoopVar10;
+ mNext[LoopVar10] = New;
+ LoopVar10 = mNext[Old];
+ mNext[New] = LoopVar10;
+ mPrev[LoopVar10] = New;
mParent[New] = mParent[Old];
mLevel[New] = (UINT8) mMatchLen;
mPosition[New] = mPos;
@@ -599,33 +351,26 @@ Returns: (VOID) MakeChild (New, mText[mPos + mMatchLen], mPos);
}
+/**
+ Insert string info for current position into the String Info Log.
+
+**/
VOID
EFIAPI
InsertNode (
VOID
)
-/*++
-
-Routine Description:
-
- Insert string info for current position into the String Info Log
-
-Arguments: (VOID)
-
-Returns: (VOID)
-
-**/
{
- NODE q;
+ NODE LoopVar6;
- NODE r;
+ NODE LoopVar4;
- NODE j;
+ NODE LoopVar2;
- NODE t;
- UINT8 c;
- UINT8 *t1;
- UINT8 *t2;
+ NODE LoopVar10;
+ UINT8 LoopVar5;
+ UINT8 *TempString3;
+ UINT8 *TempString2;
if (mMatchLen >= 4) {
//
@@ -636,36 +381,36 @@ Returns: (VOID) // in DeleteNode() later.
//
mMatchLen--;
- r = (NODE) ((mMatchPos + 1) | WNDSIZ);
- q = mParent[r];
- while (q == NIL) {
- r = mNext[r];
- q = mParent[r];
+ LoopVar4 = (NODE) ((mMatchPos + 1) | WNDSIZ);
+ LoopVar6 = mParent[LoopVar4];
+ while (LoopVar6 == NIL) {
+ LoopVar4 = mNext[LoopVar4];
+ LoopVar6 = mParent[LoopVar4];
}
- while (mLevel[q] >= mMatchLen) {
- r = q;
- q = mParent[q];
+ while (mLevel[LoopVar6] >= mMatchLen) {
+ LoopVar4 = LoopVar6;
+ LoopVar6 = mParent[LoopVar6];
}
- t = q;
- while (mPosition[t] < 0) {
- mPosition[t] = mPos;
- t = mParent[t];
+ LoopVar10 = LoopVar6;
+ while (mPosition[LoopVar10] < 0) {
+ mPosition[LoopVar10] = mPos;
+ LoopVar10 = mParent[LoopVar10];
}
- if (t < WNDSIZ) {
- mPosition[t] = (NODE) (mPos | PERC_FLAG);
+ if (LoopVar10 < WNDSIZ) {
+ mPosition[LoopVar10] = (NODE) (mPos | PERC_FLAG);
}
} else {
//
// Locate the target tree
//
- q = (NODE) (mText[mPos] + WNDSIZ);
- c = mText[mPos + 1];
- r = Child (q, c);
- if (r == NIL) {
- MakeChild (q, c, mPos);
+ LoopVar6 = (NODE) (mText[mPos] + WNDSIZ);
+ LoopVar5 = mText[mPos + 1];
+ LoopVar4 = Child (LoopVar6, LoopVar5);
+ if (LoopVar4 == NIL) {
+ MakeChild (LoopVar6, LoopVar5, mPos);
mMatchLen = 1;
return ;
}
@@ -678,191 +423,211 @@ Returns: (VOID) // Node split or creation is involved.
//
for (;;) {
- if (r >= WNDSIZ) {
- j = MAXMATCH;
- mMatchPos = r;
+ if (LoopVar4 >= WNDSIZ) {
+ LoopVar2 = MAXMATCH;
+ mMatchPos = LoopVar4;
} else {
- j = mLevel[r];
- mMatchPos = (NODE) (mPosition[r] & ~PERC_FLAG);
+ LoopVar2 = mLevel[LoopVar4];
+ mMatchPos = (NODE) (mPosition[LoopVar4] & ~PERC_FLAG);
}
if (mMatchPos >= mPos) {
mMatchPos -= WNDSIZ;
}
- t1 = &mText[mPos + mMatchLen];
- t2 = &mText[mMatchPos + mMatchLen];
- while (mMatchLen < j) {
- if (*t1 != *t2) {
- Split (r);
+ TempString3 = &mText[mPos + mMatchLen];
+ TempString2 = &mText[mMatchPos + mMatchLen];
+ while (mMatchLen < LoopVar2) {
+ if (*TempString3 != *TempString2) {
+ Split (LoopVar4);
return ;
}
mMatchLen++;
- t1++;
- t2++;
+ TempString3++;
+ TempString2++;
}
if (mMatchLen >= MAXMATCH) {
break;
}
- mPosition[r] = mPos;
- q = r;
- r = Child (q, *t1);
- if (r == NIL) {
- MakeChild (q, *t1, mPos);
+ mPosition[LoopVar4] = mPos;
+ LoopVar6 = LoopVar4;
+ LoopVar4 = Child (LoopVar6, *TempString3);
+ if (LoopVar4 == NIL) {
+ MakeChild (LoopVar6, *TempString3, mPos);
return ;
}
mMatchLen++;
}
- t = mPrev[r];
- mPrev[mPos] = t;
- mNext[t] = mPos;
- t = mNext[r];
- mNext[mPos] = t;
- mPrev[t] = mPos;
- mParent[mPos] = q;
- mParent[r] = NIL;
+ LoopVar10 = mPrev[LoopVar4];
+ mPrev[mPos] = LoopVar10;
+ mNext[LoopVar10] = mPos;
+ LoopVar10 = mNext[LoopVar4];
+ mNext[mPos] = LoopVar10;
+ mPrev[LoopVar10] = mPos;
+ mParent[mPos] = LoopVar6;
+ mParent[LoopVar4] = NIL;
//
// Special usage of 'next'
//
- mNext[r] = mPos;
+ mNext[LoopVar4] = mPos;
}
+/**
+ Delete outdated string info. (The Usage of PERC_FLAG
+ ensures a clean deletion).
+
+**/
VOID
EFIAPI
DeleteNode (
VOID
)
-/*++
-
-Routine Description:
-
- Delete outdated string info. (The Usage of PERC_FLAG
- ensures a clean deletion)
-
-Arguments: (VOID)
-
-Returns: (VOID)
-
-**/
{
- NODE q;
+ NODE LoopVar6;
- NODE r;
+ NODE LoopVar4;
- NODE s;
+ NODE LoopVar11;
- NODE t;
+ NODE LoopVar10;
- NODE u;
+ NODE LoopVar9;
if (mParent[mPos] == NIL) {
return ;
}
- r = mPrev[mPos];
- s = mNext[mPos];
- mNext[r] = s;
- mPrev[s] = r;
- r = mParent[mPos];
+ LoopVar4 = mPrev[mPos];
+ LoopVar11 = mNext[mPos];
+ mNext[LoopVar4] = LoopVar11;
+ mPrev[LoopVar11] = LoopVar4;
+ LoopVar4 = mParent[mPos];
mParent[mPos] = NIL;
- if (r >= WNDSIZ) {
+ if (LoopVar4 >= WNDSIZ) {
return ;
}
- mChildCount[r]--;
- if (mChildCount[r] > 1) {
+ mChildCount[LoopVar4]--;
+ if (mChildCount[LoopVar4] > 1) {
return ;
}
- t = (NODE) (mPosition[r] & ~PERC_FLAG);
- if (t >= mPos) {
- t -= WNDSIZ;
+ LoopVar10 = (NODE) (mPosition[LoopVar4] & ~PERC_FLAG);
+ if (LoopVar10 >= mPos) {
+ LoopVar10 -= WNDSIZ;
}
- s = t;
- q = mParent[r];
- u = mPosition[q];
- while ((u & PERC_FLAG) != 0){
- u &= ~PERC_FLAG;
- if (u >= mPos) {
- u -= WNDSIZ;
+ LoopVar11 = LoopVar10;
+ LoopVar6 = mParent[LoopVar4];
+ LoopVar9 = mPosition[LoopVar6];
+ while ((LoopVar9 & PERC_FLAG) != 0){
+ LoopVar9 &= ~PERC_FLAG;
+ if (LoopVar9 >= mPos) {
+ LoopVar9 -= WNDSIZ;
}
- if (u > s) {
- s = u;
+ if (LoopVar9 > LoopVar11) {
+ LoopVar11 = LoopVar9;
}
- mPosition[q] = (NODE) (s | WNDSIZ);
- q = mParent[q];
- u = mPosition[q];
+ mPosition[LoopVar6] = (NODE) (LoopVar11 | WNDSIZ);
+ LoopVar6 = mParent[LoopVar6];
+ LoopVar9 = mPosition[LoopVar6];
}
- if (q < WNDSIZ) {
- if (u >= mPos) {
- u -= WNDSIZ;
+ if (LoopVar6 < WNDSIZ) {
+ if (LoopVar9 >= mPos) {
+ LoopVar9 -= WNDSIZ;
}
- if (u > s) {
- s = u;
+ if (LoopVar9 > LoopVar11) {
+ LoopVar11 = LoopVar9;
}
- mPosition[q] = (NODE) (s | WNDSIZ | PERC_FLAG);
+ mPosition[LoopVar6] = (NODE) (LoopVar11 | WNDSIZ | PERC_FLAG);
}
- s = Child (r, mText[t + mLevel[r]]);
- t = mPrev[s];
- u = mNext[s];
- mNext[t] = u;
- mPrev[u] = t;
- t = mPrev[r];
- mNext[t] = s;
- mPrev[s] = t;
- t = mNext[r];
- mPrev[t] = s;
- mNext[s] = t;
- mParent[s] = mParent[r];
- mParent[r] = NIL;
- mNext[r] = mAvail;
- mAvail = r;
+ LoopVar11 = Child (LoopVar4, mText[LoopVar10 + mLevel[LoopVar4]]);
+ LoopVar10 = mPrev[LoopVar11];
+ LoopVar9 = mNext[LoopVar11];
+ mNext[LoopVar10] = LoopVar9;
+ mPrev[LoopVar9] = LoopVar10;
+ LoopVar10 = mPrev[LoopVar4];
+ mNext[LoopVar10] = LoopVar11;
+ mPrev[LoopVar11] = LoopVar10;
+ LoopVar10 = mNext[LoopVar4];
+ mPrev[LoopVar10] = LoopVar11;
+ mNext[LoopVar11] = LoopVar10;
+ mParent[LoopVar11] = mParent[LoopVar4];
+ mParent[LoopVar4] = NIL;
+ mNext[LoopVar4] = mAvail;
+ mAvail = LoopVar4;
}
-VOID
+/**
+ Read in source data
+
+ @param[out] LoopVar7 The buffer to hold the data.
+ @param[in] LoopVar8 The number of bytes to read.
+
+ @return The number of bytes actually read.
+**/
+INT32
EFIAPI
-GetNextMatch (
- VOID
+FreadCrc (
+ OUT UINT8 *LoopVar7,
+ IN INT32 LoopVar8
)
-/*++
+{
+ INT32 LoopVar1;
-Routine Description:
+ for (LoopVar1 = 0; mSrc < mSrcUpperLimit && LoopVar1 < LoopVar8; LoopVar1++) {
+ *LoopVar7++ = *mSrc++;
+ }
- Advance the current position (read in new data if needed).
- Delete outdated string info. Find a match string for current position.
+ LoopVar8 = LoopVar1;
+
+ LoopVar7 -= LoopVar8;
+ mOrigSize += LoopVar8;
+ LoopVar1--;
+ while (LoopVar1 >= 0) {
+ UPDATE_CRC (*LoopVar7++);
+ LoopVar1--;
+ }
-Arguments: (VOID)
+ return LoopVar8;
+}
-Returns: (VOID)
+/**
+ Advance the current position (read in new data if needed).
+ Delete outdated string info. Find a match string for current position.
**/
+VOID
+EFIAPI
+GetNextMatch (
+ VOID
+ )
{
- INT32 n;
+ INT32 LoopVar8;
VOID *Temp;
mRemainder--;
mPos++;
if (mPos == WNDSIZ * 2) {
- Temp = AllocatePool (WNDSIZ + MAXMATCH);
+ Temp = AllocateZeroPool (WNDSIZ + MAXMATCH);
CopyMem (Temp, &mText[WNDSIZ], WNDSIZ + MAXMATCH);
CopyMem (&mText[0], Temp, WNDSIZ + MAXMATCH);
FreePool (Temp);
- n = FreadCrc (&mText[WNDSIZ + MAXMATCH], WNDSIZ);
- mRemainder += n;
+ LoopVar8 = FreadCrc (&mText[WNDSIZ + MAXMATCH], WNDSIZ);
+ mRemainder += LoopVar8;
mPos = WNDSIZ;
}
@@ -870,128 +635,350 @@ Returns: (VOID) InsertNode ();
}
-EFI_STATUS
+/**
+ Send entry LoopVar1 down the queue.
+
+ @param[in] LoopVar1 The index of the item to move.
+**/
+VOID
EFIAPI
-Encode (
- VOID
+DownHeap (
+ IN INT32 i
)
-/*++
+{
+ INT32 LoopVar1;
-Routine Description:
+ INT32 LoopVar2;
- The main controlling routine for compression process.
+ //
+ // priority queue: send i-th entry down heap
+ //
+ LoopVar2 = mHeap[i];
+ LoopVar1 = 2 * i;
+ while (LoopVar1 <= mHeapSize) {
+ if (LoopVar1 < mHeapSize && mFreq[mHeap[LoopVar1]] > mFreq[mHeap[LoopVar1 + 1]]) {
+ LoopVar1++;
+ }
-Arguments: (VOID)
+ if (mFreq[LoopVar2] <= mFreq[mHeap[LoopVar1]]) {
+ break;
+ }
-Returns:
+ mHeap[i] = mHeap[LoopVar1];
+ i = LoopVar1;
+ LoopVar1 = 2 * i;
+ }
- EFI_SUCCESS - The compression is successful
- EFI_OUT_0F_RESOURCES - Not enough memory for compression process
+ mHeap[i] = (INT16) LoopVar2;
+}
+/**
+ Count the number of each code length for a Huffman tree.
+
+ @param[in] LoopVar1 The top node.
**/
+VOID
+EFIAPI
+CountLen (
+ IN INT32 LoopVar1
+ )
{
- EFI_STATUS Status;
- INT32 LastMatchLen;
- NODE LastMatchPos;
+ if (LoopVar1 < mTempInt32) {
+ mLenCnt[(mHuffmanDepth < 16) ? mHuffmanDepth : 16]++;
+ } else {
+ mHuffmanDepth++;
+ CountLen (mLeft[LoopVar1]);
+ CountLen (mRight[LoopVar1]);
+ mHuffmanDepth--;
+ }
+}
- Status = AllocateMemory ();
- if (EFI_ERROR (Status)) {
- FreeMemory ();
- return Status;
+/**
+ Create code length array for a Huffman tree.
+
+ @param[in] Root The root of the tree.
+**/
+VOID
+EFIAPI
+MakeLen (
+ IN INT32 Root
+ )
+{
+ INT32 LoopVar1;
+
+ INT32 LoopVar2;
+ UINT32 Cum;
+
+ for (LoopVar1 = 0; LoopVar1 <= 16; LoopVar1++) {
+ mLenCnt[LoopVar1] = 0;
}
- InitSlide ();
+ CountLen (Root);
- HufEncodeStart ();
+ //
+ // Adjust the length count array so that
+ // no code will be generated longer than its designated length
+ //
+ Cum = 0;
+ for (LoopVar1 = 16; LoopVar1 > 0; LoopVar1--) {
+ Cum += mLenCnt[LoopVar1] << (16 - LoopVar1);
+ }
- mRemainder = FreadCrc (&mText[WNDSIZ], WNDSIZ + MAXMATCH);
+ while (Cum != (1U << 16)) {
+ mLenCnt[16]--;
+ for (LoopVar1 = 15; LoopVar1 > 0; LoopVar1--) {
+ if (mLenCnt[LoopVar1] != 0) {
+ mLenCnt[LoopVar1]--;
+ mLenCnt[LoopVar1 + 1] += 2;
+ break;
+ }
+ }
- mMatchLen = 0;
- mPos = WNDSIZ;
- InsertNode ();
- if (mMatchLen > mRemainder) {
- mMatchLen = mRemainder;
+ Cum--;
}
- while (mRemainder > 0) {
- LastMatchLen = mMatchLen;
- LastMatchPos = mMatchPos;
- GetNextMatch ();
- if (mMatchLen > mRemainder) {
- mMatchLen = mRemainder;
+ for (LoopVar1 = 16; LoopVar1 > 0; LoopVar1--) {
+ LoopVar2 = mLenCnt[LoopVar1];
+ LoopVar2--;
+ while (LoopVar2 >= 0) {
+ mLen[*mSortPtr++] = (UINT8) LoopVar1;
+ LoopVar2--;
}
+ }
+}
- if (mMatchLen > LastMatchLen || LastMatchLen < THRESHOLD) {
- //
- // Not enough benefits are gained by outputting a pointer,
- // so just output the original character
- //
- CompressOutput(mText[mPos - 1], 0);
+/**
+ Assign code to each symbol based on the code length array.
+
+ @param[in] LoopVar8 The number of symbols.
+ @param[in] Len The code length array.
+ @param[out] Code The stores codes for each symbol.
+**/
+VOID
+EFIAPI
+MakeCode (
+ IN INT32 LoopVar8,
+ IN UINT8 Len[ ],
+ OUT UINT16 Code[ ]
+ )
+{
+ INT32 LoopVar1;
+ UINT16 Start[18];
+
+ Start[1] = 0;
+ for (LoopVar1 = 1; LoopVar1 <= 16; LoopVar1++) {
+ Start[LoopVar1 + 1] = (UINT16) ((Start[LoopVar1] + mLenCnt[LoopVar1]) << 1);
+ }
+
+ for (LoopVar1 = 0; LoopVar1 < LoopVar8; LoopVar1++) {
+ Code[LoopVar1] = Start[Len[LoopVar1]]++;
+ }
+}
+
+/**
+ Generates Huffman codes given a frequency distribution of symbols.
+
+ @param[in] NParm The number of symbols.
+ @param[in] FreqParm The frequency of each symbol.
+ @param[out] LenParm The code length for each symbol.
+ @param[out] CodeParm The code for each symbol.
+
+ @return The root of the Huffman tree.
+**/
+INT32
+EFIAPI
+MakeTree (
+ IN INT32 NParm,
+ IN UINT16 FreqParm[ ],
+ OUT UINT8 LenParm[ ],
+ OUT UINT16 CodeParm[ ]
+ )
+{
+ INT32 LoopVar1;
+
+ INT32 LoopVar2;
+
+ INT32 LoopVar3;
+
+ INT32 Avail;
+
+ //
+ // make tree, calculate len[], return root
+ //
+ mTempInt32 = NParm;
+ mFreq = FreqParm;
+ mLen = LenParm;
+ Avail = mTempInt32;
+ mHeapSize = 0;
+ mHeap[1] = 0;
+ for (LoopVar1 = 0; LoopVar1 < mTempInt32; LoopVar1++) {
+ mLen[LoopVar1] = 0;
+ if ((mFreq[LoopVar1]) != 0) {
+ mHeapSize++;
+ mHeap[mHeapSize] = (INT16) LoopVar1;
+ }
+ }
+
+ if (mHeapSize < 2) {
+ CodeParm[mHeap[1]] = 0;
+ return mHeap[1];
+ }
+
+ for (LoopVar1 = mHeapSize / 2; LoopVar1 >= 1; LoopVar1--) {
+ //
+ // make priority queue
+ //
+ DownHeap (LoopVar1);
+ }
+
+ mSortPtr = CodeParm;
+ do {
+ LoopVar1 = mHeap[1];
+ if (LoopVar1 < mTempInt32) {
+ *mSortPtr++ = (UINT16) LoopVar1;
+ }
+
+ mHeap[1] = mHeap[mHeapSize--];
+ DownHeap (1);
+ LoopVar2 = mHeap[1];
+ if (LoopVar2 < mTempInt32) {
+ *mSortPtr++ = (UINT16) LoopVar2;
+ }
+
+ LoopVar3 = Avail++;
+ mFreq[LoopVar3] = (UINT16) (mFreq[LoopVar1] + mFreq[LoopVar2]);
+ mHeap[1] = (INT16) LoopVar3;
+ DownHeap (1);
+ mLeft[LoopVar3] = (UINT16) LoopVar1;
+ mRight[LoopVar3] = (UINT16) LoopVar2;
+ } while (mHeapSize > 1);
+
+ mSortPtr = CodeParm;
+ MakeLen (LoopVar3);
+ MakeCode (NParm, LenParm, CodeParm);
+
+ //
+ // return root
+ //
+ return LoopVar3;
+}
+
+/**
+ Outputs rightmost LoopVar8 bits of x
+
+ @param[in] LoopVar8 The rightmost LoopVar8 bits of the data is used.
+ @param[in] x The data.
+**/
+VOID
+EFIAPI
+PutBits (
+ IN INT32 LoopVar8,
+ IN UINT32 x
+ )
+{
+ UINT8 Temp;
+
+ if (LoopVar8 < mBitCount) {
+ mSubBitBuf |= x << (mBitCount -= LoopVar8);
+ } else {
+
+ Temp = (UINT8)(mSubBitBuf | (x >> (LoopVar8 -= mBitCount)));
+ if (mDst < mDstUpperLimit) {
+ *mDst++ = Temp;
+ }
+ mCompSize++;
+
+ if (LoopVar8 < UINT8_BIT) {
+ mSubBitBuf = x << (mBitCount = UINT8_BIT - LoopVar8);
} else {
- //
- // Outputting a pointer is beneficial enough, do it.
- //
- CompressOutput(LastMatchLen + (UINT8_MAX + 1 - THRESHOLD),
- (mPos - LastMatchPos - 2) & (WNDSIZ - 1));
- LastMatchLen--;
- while (LastMatchLen > 0) {
- GetNextMatch ();
- LastMatchLen--;
+ Temp = (UINT8)(x >> (LoopVar8 - UINT8_BIT));
+ if (mDst < mDstUpperLimit) {
+ *mDst++ = Temp;
}
+ mCompSize++;
- if (mMatchLen > mRemainder) {
- mMatchLen = mRemainder;
- }
+ mSubBitBuf = x << (mBitCount = 2 * UINT8_BIT - LoopVar8);
}
}
+}
- HufEncodeEnd ();
- FreeMemory ();
- return EFI_SUCCESS;
+/**
+ Encode a signed 32 bit number.
+
+ @param[in] LoopVar5 The number to encode.
+**/
+VOID
+EFIAPI
+EncodeC (
+ IN INT32 LoopVar5
+ )
+{
+ PutBits (mCLen[LoopVar5], mCCode[LoopVar5]);
}
+/**
+ Encode a unsigned 32 bit number.
+
+ @param[in] LoopVar7 The number to encode.
+**/
VOID
EFIAPI
-CountTFreq (
- VOID
+EncodeP (
+ IN UINT32 LoopVar7
)
-/*++
+{
+ UINT32 LoopVar5;
-Routine Description:
+ UINT32 LoopVar6;
- Count the frequencies for the Extra Set
+ LoopVar5 = 0;
+ LoopVar6 = LoopVar7;
+ while (LoopVar6 != 0) {
+ LoopVar6 >>= 1;
+ LoopVar5++;
+ }
-Arguments: (VOID)
+ PutBits (mPTLen[LoopVar5], mPTCode[LoopVar5]);
+ if (LoopVar5 > 1) {
+ PutBits(LoopVar5 - 1, LoopVar7 & (0xFFFFU >> (17 - LoopVar5)));
+ }
+}
-Returns: (VOID)
+/**
+ Count the frequencies for the Extra Set.
**/
+VOID
+EFIAPI
+CountTFreq (
+ VOID
+ )
{
- INT32 i;
+ INT32 LoopVar1;
- INT32 k;
+ INT32 LoopVar3;
- INT32 n;
+ INT32 LoopVar8;
INT32 Count;
- for (i = 0; i < NT; i++) {
- mTFreq[i] = 0;
+ for (LoopVar1 = 0; LoopVar1 < NT; LoopVar1++) {
+ mTFreq[LoopVar1] = 0;
}
- n = NC;
- while (n > 0 && mCLen[n - 1] == 0) {
- n--;
+ LoopVar8 = NC;
+ while (LoopVar8 > 0 && mCLen[LoopVar8 - 1] == 0) {
+ LoopVar8--;
}
- i = 0;
- while (i < n) {
- k = mCLen[i++];
- if (k == 0) {
+ LoopVar1 = 0;
+ while (LoopVar1 < LoopVar8) {
+ LoopVar3 = mCLen[LoopVar1++];
+ if (LoopVar3 == 0) {
Count = 1;
- while (i < n && mCLen[i] == 0) {
- i++;
+ while (LoopVar1 < LoopVar8 && mCLen[LoopVar1] == 0) {
+ LoopVar1++;
Count++;
}
@@ -1006,106 +993,92 @@ Returns: (VOID) mTFreq[2]++;
}
} else {
- ASSERT((k+2)<(2 * NT - 1));
- mTFreq[k + 2]++;
+ ASSERT((LoopVar3+2)<(2 * NT - 1));
+ mTFreq[LoopVar3 + 2]++;
}
}
}
+/**
+ Outputs the code length array for the Extra Set or the Position Set.
+
+ @param[in] LoopVar8 The number of symbols.
+ @param[in] nbit The number of bits needed to represent 'LoopVar8'.
+ @param[in] Special The special symbol that needs to be take care of.
+
+**/
VOID
EFIAPI
WritePTLen (
- IN INT32 n,
+ IN INT32 LoopVar8,
IN INT32 nbit,
IN INT32 Special
)
-/*++
-
-Routine Description:
-
- Outputs the code length array for the Extra Set or the Position Set.
-
-Arguments:
-
- n - the number of symbols
- nbit - the number of bits needed to represent 'n'
- Special - the special symbol that needs to be take care of
-
-Returns: (VOID)
-
-**/
{
- INT32 i;
+ INT32 LoopVar1;
- INT32 k;
+ INT32 LoopVar3;
- while (n > 0 && mPTLen[n - 1] == 0) {
- n--;
+ while (LoopVar8 > 0 && mPTLen[LoopVar8 - 1] == 0) {
+ LoopVar8--;
}
- PutBits (nbit, n);
- i = 0;
- while (i < n) {
- k = mPTLen[i++];
- if (k <= 6) {
- PutBits (3, k);
+ PutBits (nbit, LoopVar8);
+ LoopVar1 = 0;
+ while (LoopVar1 < LoopVar8) {
+ LoopVar3 = mPTLen[LoopVar1++];
+ if (LoopVar3 <= 6) {
+ PutBits (3, LoopVar3);
} else {
- PutBits (k - 3, (1U << (k - 3)) - 2);
+ PutBits (LoopVar3 - 3, (1U << (LoopVar3 - 3)) - 2);
}
- if (i == Special) {
- while (i < 6 && mPTLen[i] == 0) {
- i++;
+ if (LoopVar1 == Special) {
+ while (LoopVar1 < 6 && mPTLen[LoopVar1] == 0) {
+ LoopVar1++;
}
- PutBits (2, (i - 3) & 3);
+ PutBits (2, (LoopVar1 - 3) & 3);
}
}
}
+/**
+ Outputs the code length array for Char&Length Set
+
+**/
VOID
EFIAPI
WriteCLen (
VOID
)
-/*++
-
-Routine Description:
-
- Outputs the code length array for Char&Length Set
-
-Arguments: (VOID)
-
-Returns: (VOID)
-
-**/
{
- INT32 i;
+ INT32 LoopVar1;
- INT32 k;
+ INT32 LoopVar3;
- INT32 n;
+ INT32 LoopVar8;
INT32 Count;
- n = NC;
- while (n > 0 && mCLen[n - 1] == 0) {
- n--;
+ LoopVar8 = NC;
+ while (LoopVar8 > 0 && mCLen[LoopVar8 - 1] == 0) {
+ LoopVar8--;
}
- PutBits (CBIT, n);
- i = 0;
- while (i < n) {
- k = mCLen[i++];
- if (k == 0) {
+ PutBits (CBIT, LoopVar8);
+ LoopVar1 = 0;
+ while (LoopVar1 < LoopVar8) {
+ LoopVar3 = mCLen[LoopVar1++];
+ if (LoopVar3 == 0) {
Count = 1;
- while (i < n && mCLen[i] == 0) {
- i++;
+ while (LoopVar1 < LoopVar8 && mCLen[LoopVar1] == 0) {
+ LoopVar1++;
Count++;
}
if (Count <= 2) {
- for (k = 0; k < Count; k++) {
+ for (LoopVar3 = 0; LoopVar3 < Count; LoopVar3++) {
PutBits (mPTLen[0], mPTCode[0]);
}
} else if (Count <= 18) {
@@ -1120,68 +1093,25 @@ Returns: (VOID) PutBits (CBIT, Count - 20);
}
} else {
- ASSERT((k+2)<NPT);
- PutBits (mPTLen[k + 2], mPTCode[k + 2]);
+ ASSERT((LoopVar3+2)<NPT);
+ PutBits (mPTLen[LoopVar3 + 2], mPTCode[LoopVar3 + 2]);
}
}
}
-VOID
-EFIAPI
-EncodeC (
- IN INT32 c
- )
-{
- PutBits (mCLen[c], mCCode[c]);
-}
-
-VOID
-EFIAPI
-EncodeP (
- IN UINT32 p
- )
-{
- UINT32 c;
-
- UINT32 q;
-
- c = 0;
- q = p;
- while (q != 0) {
- q >>= 1;
- c++;
- }
-
- PutBits (mPTLen[c], mPTCode[c]);
- if (c > 1) {
- PutBits(c - 1, p & (0xFFFFU >> (17 - c)));
- }
-}
+/**
+ Huffman code the block and output it.
+**/
VOID
EFIAPI
SendBlock (
VOID
)
-/*++
-
-Routine Description:
-
- Huffman code the block and output it.
-
-Arguments:
-
- None
-
-Returns:
-
- None
-
-**/
{
- UINT32 i;
+ UINT32 LoopVar1;
- UINT32 k;
+ UINT32 LoopVar3;
UINT32 Flags;
@@ -1222,18 +1152,18 @@ Returns: }
Pos = 0;
- for (i = 0; i < Size; i++) {
- if (i % UINT8_BIT == 0) {
+ for (LoopVar1 = 0; LoopVar1 < Size; LoopVar1++) {
+ if (LoopVar1 % UINT8_BIT == 0) {
Flags = mBuf[Pos++];
} else {
Flags <<= 1;
}
if ((Flags & (1U << (UINT8_BIT - 1))) != 0){
EncodeC(mBuf[Pos++] + (1U << UINT8_BIT));
- k = mBuf[Pos++] << UINT8_BIT;
- k += mBuf[Pos++];
+ LoopVar3 = mBuf[Pos++] << UINT8_BIT;
+ LoopVar3 += mBuf[Pos++];
- EncodeP (k);
+ EncodeP (LoopVar3);
} else {
EncodeC (mBuf[Pos++]);
}
@@ -1243,26 +1173,38 @@ Returns: SetMem (mPFreq, NP * sizeof (UINT16), 0);
}
+/**
+ Start the huffman encoding.
+
+**/
VOID
EFIAPI
-CompressOutput (
- IN UINT32 c,
- IN UINT32 p
+HufEncodeStart (
+ VOID
)
-/*++
-
-Routine Description:
-
- Outputs an Original Character or a Pointer
+{
+ SetMem (mCFreq, NC * sizeof (UINT16), 0);
+ SetMem (mPFreq, NP * sizeof (UINT16), 0);
-Arguments:
+ mOutputPos = mOutputMask = 0;
- c - The original character or the 'String Length' element of a Pointer
- p - The 'Position' field of a Pointer
+ mBitCount = UINT8_BIT;
+ mSubBitBuf = 0;
+}
-Returns: (VOID)
+/**
+ Outputs an Original Character or a Pointer.
+ @param[in] LoopVar5 The original character or the 'String Length' element of
+ a Pointer.
+ @param[in] LoopVar7 The 'Position' field of a Pointer.
**/
+VOID
+EFIAPI
+CompressOutput (
+ IN UINT32 LoopVar5,
+ IN UINT32 LoopVar7
+ )
{
STATIC UINT32 CPos;
@@ -1276,35 +1218,25 @@ Returns: (VOID) CPos = mOutputPos++;
mBuf[CPos] = 0;
}
- mBuf[mOutputPos++] = (UINT8) c;
- mCFreq[c]++;
- if (c >= (1U << UINT8_BIT)) {
+ mBuf[mOutputPos++] = (UINT8) LoopVar5;
+ mCFreq[LoopVar5]++;
+ if (LoopVar5 >= (1U << UINT8_BIT)) {
mBuf[CPos] = (UINT8)(mBuf[CPos]|mOutputMask);
- mBuf[mOutputPos++] = (UINT8)(p >> UINT8_BIT);
- mBuf[mOutputPos++] = (UINT8) p;
- c = 0;
- while (p!=0) {
- p >>= 1;
- c++;
+ mBuf[mOutputPos++] = (UINT8)(LoopVar7 >> UINT8_BIT);
+ mBuf[mOutputPos++] = (UINT8) LoopVar7;
+ LoopVar5 = 0;
+ while (LoopVar7!=0) {
+ LoopVar7 >>= 1;
+ LoopVar5++;
}
- mPFreq[c]++;
+ mPFreq[LoopVar5]++;
}
}
-VOID
-EFIAPI
-HufEncodeStart (
- VOID
- )
-{
- SetMem (mCFreq, NC * sizeof (UINT16), 0);
- SetMem (mPFreq, NP * sizeof (UINT16), 0);
-
- mOutputPos = mOutputMask = 0;
- InitPutBits ();
- return ;
-}
+/**
+ End the huffman encoding.
+**/
VOID
EFIAPI
HufEncodeEnd (
@@ -1317,395 +1249,160 @@ HufEncodeEnd ( // Flush remaining bits
//
PutBits (UINT8_BIT - 1, 0);
-
- return ;
}
-VOID
+/**
+ The main controlling routine for compression process.
+
+ @retval EFI_SUCCESS The compression is successful.
+ @retval EFI_OUT_0F_RESOURCES Not enough memory for compression process.
+**/
+EFI_STATUS
EFIAPI
-MakeCrcTable (
+Encode (
VOID
)
{
- UINT32 i;
-
- UINT32 j;
-
- UINT32 r;
-
- for (i = 0; i <= UINT8_MAX; i++) {
- r = i;
- for (j = 0; j < UINT8_BIT; j++) {
- if ((r & 1) != 0) {
- r = (r >> 1) ^ CRCPOLY;
- } else {
- r >>= 1;
- }
- }
+ EFI_STATUS Status;
+ INT32 LastMatchLen;
+ NODE LastMatchPos;
- mCrcTable[i] = (UINT16) r;
+ Status = AllocateMemory ();
+ if (EFI_ERROR (Status)) {
+ FreeMemory ();
+ return Status;
}
-}
-VOID
-EFIAPI
-PutBits (
- IN INT32 n,
- IN UINT32 x
- )
-/*++
-
-Routine Description:
-
- Outputs rightmost n bits of x
-
-Arguments:
-
- n - the rightmost n bits of the data is used
- x - the data
-
-Returns:
+ InitSlide ();
- None
+ HufEncodeStart ();
-**/
-{
- UINT8 Temp;
+ mRemainder = FreadCrc (&mText[WNDSIZ], WNDSIZ + MAXMATCH);
- if (n < mBitCount) {
- mSubBitBuf |= x << (mBitCount -= n);
- } else {
+ mMatchLen = 0;
+ mPos = WNDSIZ;
+ InsertNode ();
+ if (mMatchLen > mRemainder) {
+ mMatchLen = mRemainder;
+ }
- Temp = (UINT8)(mSubBitBuf | (x >> (n -= mBitCount)));
- if (mDst < mDstUpperLimit) {
- *mDst++ = Temp;
+ while (mRemainder > 0) {
+ LastMatchLen = mMatchLen;
+ LastMatchPos = mMatchPos;
+ GetNextMatch ();
+ if (mMatchLen > mRemainder) {
+ mMatchLen = mRemainder;
}
- mCompSize++;
- if (n < UINT8_BIT) {
- mSubBitBuf = x << (mBitCount = UINT8_BIT - n);
+ if (mMatchLen > LastMatchLen || LastMatchLen < THRESHOLD) {
+ //
+ // Not enough benefits are gained by outputting a pointer,
+ // so just output the original character
+ //
+ CompressOutput(mText[mPos - 1], 0);
} else {
+ //
+ // Outputting a pointer is beneficial enough, do it.
+ //
- Temp = (UINT8)(x >> (n - UINT8_BIT));
- if (mDst < mDstUpperLimit) {
- *mDst++ = Temp;
+ CompressOutput(LastMatchLen + (UINT8_MAX + 1 - THRESHOLD),
+ (mPos - LastMatchPos - 2) & (WNDSIZ - 1));
+ LastMatchLen--;
+ while (LastMatchLen > 0) {
+ GetNextMatch ();
+ LastMatchLen--;
}
- mCompSize++;
- mSubBitBuf = x << (mBitCount = 2 * UINT8_BIT - n);
+ if (mMatchLen > mRemainder) {
+ mMatchLen = mRemainder;
+ }
}
}
-}
-
-INT32
-EFIAPI
-FreadCrc (
- OUT UINT8 *p,
- IN INT32 n
- )
-/*++
-
-Routine Description:
-
- Read in source data
-
-Arguments:
-
- p - the buffer to hold the data
- n - number of bytes to read
-
-Returns:
-
- number of bytes actually read
-
-**/
-{
- INT32 i;
-
- for (i = 0; mSrc < mSrcUpperLimit && i < n; i++) {
- *p++ = *mSrc++;
- }
-
- n = i;
- p -= n;
- mOrigSize += n;
- i--;
- while (i >= 0) {
- UPDATE_CRC (*p++);
- i--;
- }
-
- return n;
-}
-
-VOID
-EFIAPI
-InitPutBits (
- VOID
- )
-{
- mBitCount = UINT8_BIT;
- mSubBitBuf = 0;
+ HufEncodeEnd ();
+ FreeMemory ();
+ return EFI_SUCCESS;
}
-VOID
-EFIAPI
-CountLen (
- IN INT32 i
- )
-/*++
-
-Routine Description:
-
- Count the number of each code length for a Huffman tree.
-
-Arguments:
-
- i - the top node
+/**
+ The compression routine.
-Returns: (VOID)
+ @param[in] SrcBuffer The buffer containing the source data.
+ @param[in] SrcSize The number of bytes in SrcBuffer.
+ @param[in] DstBuffer The buffer to put the compressed image in.
+ @param[in,out] DstSize On input the size (in bytes) of DstBuffer, on
+ return the number of bytes placed in DstBuffer.
+ @retval EFI_SUCCESS The compression was sucessful.
+ @retval EFI_BUFFER_TOO_SMALL The buffer was too small. DstSize is required.
**/
-{
- STATIC INT32 Depth = 0;
-
- if (i < mN) {
- mLenCnt[(Depth < 16) ? Depth : 16]++;
- } else {
- Depth++;
- CountLen (mLeft[i]);
- CountLen (mRight[i]);
- Depth--;
- }
-}
-
-VOID
+EFI_STATUS
EFIAPI
-MakeLen (
- IN INT32 Root
+Compress (
+ IN VOID *SrcBuffer,
+ IN UINT64 SrcSize,
+ IN VOID *DstBuffer,
+ IN OUT UINT64 *DstSize
)
-/*++
-
-Routine Description:
-
- Create code length array for a Huffman tree
-
-Arguments:
-
- Root - the root of the tree
-
-Returns:
-
- None
-
-**/
{
- INT32 i;
-
- INT32 k;
- UINT32 Cum;
-
- for (i = 0; i <= 16; i++) {
- mLenCnt[i] = 0;
- }
-
- CountLen (Root);
+ EFI_STATUS Status;
//
- // Adjust the length count array so that
- // no code will be generated longer than its designated length
+ // Initializations
//
- Cum = 0;
- for (i = 16; i > 0; i--) {
- Cum += mLenCnt[i] << (16 - i);
- }
-
- while (Cum != (1U << 16)) {
- mLenCnt[16]--;
- for (i = 15; i > 0; i--) {
- if (mLenCnt[i] != 0) {
- mLenCnt[i]--;
- mLenCnt[i + 1] += 2;
- break;
- }
- }
+ mBufSiz = 0;
+ mBuf = NULL;
+ mText = NULL;
+ mLevel = NULL;
+ mChildCount = NULL;
+ mPosition = NULL;
+ mParent = NULL;
+ mPrev = NULL;
+ mNext = NULL;
- Cum--;
- }
+ mSrc = SrcBuffer;
+ mSrcUpperLimit = mSrc + SrcSize;
+ mDst = DstBuffer;
+ mDstUpperLimit = mDst +*DstSize;
- for (i = 16; i > 0; i--) {
- k = mLenCnt[i];
- k--;
- while (k >= 0) {
- mLen[*mSortPtr++] = (UINT8) i;
- k--;
- }
- }
-}
+ PutDword (0L);
+ PutDword (0L);
-VOID
-EFIAPI
-DownHeap (
- IN INT32 i
- )
-{
- INT32 j;
+ MakeCrcTable ();
- INT32 k;
+ mOrigSize = mCompSize = 0;
+ mCrc = INIT_CRC;
//
- // priority queue: send i-th entry down heap
+ // Compress it
//
- k = mHeap[i];
- j = 2 * i;
- while (j <= mHeapSize) {
- if (j < mHeapSize && mFreq[mHeap[j]] > mFreq[mHeap[j + 1]]) {
- j++;
- }
-
- if (mFreq[k] <= mFreq[mHeap[j]]) {
- break;
- }
-
- mHeap[i] = mHeap[j];
- i = j;
- j = 2 * i;
- }
-
- mHeap[i] = (INT16) k;
-}
-
-VOID
-EFIAPI
-MakeCode (
- IN INT32 n,
- IN UINT8 Len[ ],
- OUT UINT16 Code[ ]
- )
-/*++
-
-Routine Description:
-
- Assign code to each symbol based on the code length array
-
-Arguments:
-
- n - number of symbols
- Len - the code length array
- Code - stores codes for each symbol
-
-Returns:
-
- None
-
-**/
-{
- INT32 i;
- UINT16 Start[18];
-
- Start[1] = 0;
- for (i = 1; i <= 16; i++) {
- Start[i + 1] = (UINT16) ((Start[i] + mLenCnt[i]) << 1);
- }
-
- for (i = 0; i < n; i++) {
- Code[i] = Start[Len[i]]++;
+ Status = Encode ();
+ if (EFI_ERROR (Status)) {
+ return EFI_OUT_OF_RESOURCES;
}
-}
-
-INT32
-EFIAPI
-MakeTree (
- IN INT32 NParm,
- IN UINT16 FreqParm[ ],
- OUT UINT8 LenParm[ ],
- OUT UINT16 CodeParm[ ]
- )
-/*++
-
-Routine Description:
-
- Generates Huffman codes given a frequency distribution of symbols
-
-Arguments:
-
- NParm - number of symbols
- FreqParm - frequency of each symbol
- LenParm - code length for each symbol
- CodeParm - code for each symbol
-
-Returns:
-
- Root of the Huffman tree.
-
-**/
-{
- INT32 i;
-
- INT32 j;
-
- INT32 k;
-
- INT32 Avail;
-
//
- // make tree, calculate len[], return root
+ // Null terminate the compressed data
//
- mN = NParm;
- mFreq = FreqParm;
- mLen = LenParm;
- Avail = mN;
- mHeapSize = 0;
- mHeap[1] = 0;
- for (i = 0; i < mN; i++) {
- mLen[i] = 0;
- if ((mFreq[i]) != 0) {
- mHeapSize++;
- mHeap[mHeapSize] = (INT16) i;
- }
- }
-
- if (mHeapSize < 2) {
- CodeParm[mHeap[1]] = 0;
- return mHeap[1];
- }
-
- for (i = mHeapSize / 2; i >= 1; i--) {
- //
- // make priority queue
- //
- DownHeap (i);
+ if (mDst < mDstUpperLimit) {
+ *mDst++ = 0;
}
-
- mSortPtr = CodeParm;
- do {
- i = mHeap[1];
- if (i < mN) {
- *mSortPtr++ = (UINT16) i;
- }
-
- mHeap[1] = mHeap[mHeapSize--];
- DownHeap (1);
- j = mHeap[1];
- if (j < mN) {
- *mSortPtr++ = (UINT16) j;
- }
-
- k = Avail++;
- mFreq[k] = (UINT16) (mFreq[i] + mFreq[j]);
- mHeap[1] = (INT16) k;
- DownHeap (1);
- mLeft[k] = (UINT16) i;
- mRight[k] = (UINT16) j;
- } while (mHeapSize > 1);
-
- mSortPtr = CodeParm;
- MakeLen (k);
- MakeCode (NParm, LenParm, CodeParm);
+ //
+ // Fill in compressed size and original size
+ //
+ mDst = DstBuffer;
+ PutDword (mCompSize + 1);
+ PutDword (mOrigSize);
//
- // return root
+ // Return
//
- return k;
+ if (mCompSize + 1 + 8 > *DstSize) {
+ *DstSize = mCompSize + 1 + 8;
+ return EFI_BUFFER_TOO_SMALL;
+ } else {
+ *DstSize = mCompSize + 1 + 8;
+ return EFI_SUCCESS;
+ }
+
}
+
|