?? lhadec.cpp
字號:
//'AwardMod BIOS Manipulation Tool
//'Copyright (C) 2001 Josef Hill
//'
//'This program is free software; you can redistribute it and/or
//'modify it under the terms of the GNU General Public License
//'as published by the Free Software Foundation; either version 2
//'of the License, or any later version.
//'
//'This program is distributed in the hope that it will be useful,
//'but WITHOUT ANY WARRANTY; without even the implied warranty of
//'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//'GNU General Public License for more details.
//'
//'You should have received a copy of the GNU General Public License
//'along with this program; if not, write to the Free Software
//'Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stdafx.h"
#include "lhadec.h"
#define CHAR_BIT 8
#define UCHAR_MAX 255
#define DICBIT 13
#define DICSIZ (1U << DICBIT)
#define MAXMATCH 256
#define THRESHOLD 3
#define PERC_FLAG 32768
#define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
#define CBIT 9
#define CODE_BIT 16
#define NP (DICBIT + 1)
#define NT (CODE_BIT + 3)
#define PBIT 4
#define TBIT 5
#define BITBUFSIZ (CHAR_BIT * 4)
dword me_BS;
dword me_Left[2 * NC - 1];
dword me_aRight[2*NC-1];
dword mac_len[NC];
dword mapt_len[NT];
dword mac_table[4096];
dword mapt_table[256];
byte* me_aIn;
dword me_pIn;
dword me_lIn;
byte me_aOut[8192];
dword me_pOut;
dword me_lOut;
//'io
dword mBB;
dword me_SBB;
dword me_BC;
dword mDecode_lI;
dword mL;
void decode();
dword decode_c();
void read_pt_len(dword iNN, dword iNBit, dword iSpecial);
void read_c_len();
dword decode_p();
void make_table(dword i_nchar,dword i_tablebits, dword* i_bitlen,dword* i_table);
dword getbits(dword iN);
void fillbuf(dword iN);
void e_clean();
void __stdcall lhadec_setinput(byte* iStr, dword* iLen)
{
dword lC;
e_clean();
me_lIn = iLen[0];
me_aIn = new byte[me_lIn];
for (lC=0;lC<me_lIn;lC++)
{
me_aIn[lC] = iStr[lC];
}
me_pIn = 0;
fillbuf (BITBUFSIZ);
}
void __stdcall lhadec_getoutput(byte* iStr, dword* iLen)
{
dword lC;
for (lC=0;lC<me_lOut;lC++)
{
iStr[lC]=me_aOut[lC];
}
iLen[0] = me_lOut;
if (me_lOut!=8192)
{
delete [] me_aIn;
}
}
void __stdcall lhadec_work(void)
{
decode();
}
void e_clean()
{
dword lC;
mBB = 0;
me_SBB = 0;
me_BC = 0;
mDecode_lI = 0;
mL = 0;
me_BS = 0;
mL = 0;
for (lC=0;lC < (2 * NC) ;lC++)
{
me_Left[lC] = 0;
me_aRight[lC] = 0;
}
for (lC=0;lC < NC + 1;lC++)
{
mac_len[lC] = 0;
}
for (lC=0;lC < NT + 1;lC++)
{
mapt_len[lC] = 0;
}
for (lC=0;lC < 4097;lC++)
{
mac_table[lC] = 0;
}
for (lC=0;lC < 257;lC++)
{
mapt_table[lC] = 0;
}
for (lC=0;lC < 8192;lC++)
{
me_aOut[lC] = 0;
}
}
void decode()
{
dword lR;
dword lC;
dword lL;
me_lOut = 8192;
lR =0;
lL = mL;
mL = 0;
goto lClean;
while (lR < DICSIZ)
{
lC = decode_c();
if (lC == 0xFFFFFFFF)
{
me_lOut = lR;
return;
}
if ((lC <= (dword)(UCHAR_MAX)))
{
me_aOut[me_pOut + lR++] = (byte)lC;
}
else
{
lL = lC - (UCHAR_MAX + 1 - THRESHOLD);
mDecode_lI = (lR - decode_p() - 1) & (DICSIZ - 1);
lClean:
if ((lR+lL) >= DICSIZ)
{
mL = lL;
lL = DICSIZ - lR;
mL -= lL;
}
while (--lL != 0xFFFFFFFF)
{
me_aOut[me_pOut + lR++] = me_aOut[me_pOut + mDecode_lI++];
mDecode_lI &= DICSIZ -1;
};
}
};
}
dword decode_c()
{
dword lJ;
dword lM;
if (me_BS ==0)
{
me_BS = getbits(16);
read_pt_len(NT, TBIT, 3);
read_c_len();
read_pt_len ((dword)NP, (dword)PBIT, (dword)0xFFFFFFFF);
}
me_BS--;
if (me_BS == 0xFFFFFFFF)
{
return me_BS;
}
lJ = mac_table[(mBB>>(BITBUFSIZ -12))];
if (lJ >= (dword) (NC))
{
lM = 1<<(BITBUFSIZ -13);
while (lJ >= (dword) (NC))
{
if ((mBB & lM) != 0)
{
lJ = me_aRight[ lJ];
}
else
{
lJ = me_Left[ lJ];
}
lM >>= 1;
} ;
}
fillbuf( (dword) mac_len[ lJ]);
return lJ;
}
void read_pt_len(dword iNN, dword iNBit, dword iSpecial)
{
dword lI;
dword lC;
dword lN;
dword lM;
lN = getbits(iNBit);
if (lN ==0)
{
lC = getbits(iNBit);
for (lI = 0; lI < (dword) (iNN + 1); lI++)
{
mapt_len[ lI] = 0;
}
for (lI = 0; lI < 256 + 1; lI++)
{
mapt_table[ lI] = lC;
}
}
else
{
lI = 0;
while (lI < lN)
{
lC = mBB >> (BITBUFSIZ -3);
if (lC == 7)
{
lM = 1 << (BITBUFSIZ - 1 -3);
while ((lM & mBB) != 0)
{
lM >>= 1;
lC++;
};
}
fillbuf((dword)((lC < 7) ? 3 : lC - 3));
mapt_len[ lI] = lC;
lI++;
if (lI == iSpecial)
{
lC = getbits(2);
while (--lC != 0xFFFFFFFF)
{
mapt_len[ lI] = 0;
lI++;
};
}
};
while (lI < iNN)
{
mapt_len[ lI] = 0;
lI++;
};
make_table (iNN, 8, mapt_len, mapt_table);
}
}
void read_c_len()
{
dword lI;
dword lC;
dword lN;
dword lM;
lN = getbits(CBIT);
if (lN ==0)
{
lC = getbits(CBIT);
for (lI = 0; lI <(dword)( NC + 1); lI++)
{
mac_len[ lI] = 0;
}
for (lI = 0; lI < 4097; lI++)
{
mac_table[lI] = lC;
}
}
else
{
lI = 0;
while (lI < lN)
{
lC = mapt_table[ ( mBB >> (BITBUFSIZ -8))];
if (lC >= NT)
{
lM = 1 << (BITBUFSIZ - 1-8);
while (lC >= NT)
{
if ((mBB & lM) != 0)
{
lC = me_aRight[ lC];
}
else
{
lC = me_Left[ lC];
}
lM >>= 1;
} ;
}
fillbuf((mapt_len[ lC]));
if (lC <=2)
{
if (lC ==0)
{
lC = 1;
}
else if (lC == 1)
{
lC = getbits(4) + 3;
}
else
{
lC = getbits(CBIT) + 20;
}
while (--lC !=0xFFFFFFFF)
{
mac_len[ lI] = 0;
lI++;
};
}
else
{
mac_len[ lI] = lC -2;
lI++;
}
};
while (lI <(dword) (NC))
{
mac_len[ lI] = 0;
lI++;
};
make_table(NC,12, mac_len, mac_table);
}
}
dword decode_p()
{
dword lJ;
dword lM;
lJ = mapt_table[(mBB >> (BITBUFSIZ -8))];
if (lJ >= NP)
{
lM = 1 << (BITBUFSIZ -1-8);
while (lJ >= NP)
{
if ((mBB & lM) != 0)
{
lJ = me_aRight[ lJ];
}
else
{
lJ = me_Left[ lJ];
}
lM >>= 1;
} ;
}
fillbuf( (dword)(mapt_len[ lJ]));
if (lJ != 0)
{
//lJ = 1;
lJ = (1<<(lJ -1)) + getbits((lJ) -1);
}
return lJ;
}
void make_table(dword i_nchar,dword i_tablebits, dword* iBitLen,dword* iTable)
{
dword lCC[17];
dword lWW[17];
dword lSS[18];
dword* lP;
dword lPp;
dword lI;
dword lK;
dword lL;
dword lCH;
dword lJ;
dword lA;
dword lNC;
dword lM;
//clear count
for (lI = 1;lI<17;lI++){
lCC[lI] = 0;
}
//build counts based on values in bitlen
for (lI = 0;lI < i_nchar; lI++){
lCC[iBitLen[lI]]++;
}
//load start with count values
lSS[1] = 0;
for (lI = 1; lI < 17; lI++){
lSS[lI + 1] = (lSS[lI] + (lCC[lI] << (16-lI))) & 0xFFFF;
}
//test if start[17] = 0 ; this apparantly means the archive is bad..
if (lSS[17] != 0) {
i_nchar = 0xFFFFFFFF;
return;
}
//not sure what jutbits is for but it seems to be some form of cleanup
lJ = 16 - i_tablebits;
for (lI = 1; lI < (i_tablebits + 1); lI++){
lSS[lI] = (lSS[lI] >> lJ) & 0xFFFF;
lWW[lI] = (1 << (i_tablebits - lI)) & 0xFFFF;
}
//clean up the reme_aIning weights
while (lI < 17){
lWW[lI] = (1<<(16 - lI)) & 0xFFFF;
lI++;
}
//if there ?? then flush some portion of the table
lI = lSS[i_tablebits + 1] >> lJ;
if (lI != 0){
lK = 1 << i_tablebits;
while (lI != lK){
iTable[lI] = 0;
lI++;
}
}
lA = i_nchar;
lM = 1 << (15 - i_tablebits);
for ( lCH = 0; lCH < i_nchar; lCH++){
lL = iBitLen[lCH];
if (lL == 0) continue;
lNC = (lSS[lL] + lWW[lL]) ;
if (lL <= i_tablebits){
for (lI = lSS[lL]; lI < lNC; lI++){
iTable[lI] = lCH;
}
}
else{
lK = lSS[lL];
lP = iTable;
lPp = lK >> lJ;
lI = lL - i_tablebits;
while (lI !=0){
if (lP[lPp] == 0){
me_aRight[lA] = 0;
me_Left[lA] = 0;
lP[lPp] = lA;
lA++;
}
if ((lK & lM) != 0) {
lPp = lP[lPp];
lP = me_aRight;
}
else {
lPp = lP[lPp];
lP = me_Left;
}
lK <<= 1;
lI--;
}
lP[lPp] = lCH;
}
lSS[lL] = lNC;
}
}
dword getbits(dword iN)
{
dword lO;
if (iN == 0){
return iN;
}
else
{
lO = mBB >> (BITBUFSIZ -iN);
fillbuf(iN);
return lO;
}
}
void fillbuf(dword iN)
{
mBB <<= iN;
while (iN > me_BC)
{
iN -= me_BC;
mBB |= (me_SBB << iN);
if (me_lIn != 0)
{
me_lIn--;
me_SBB = me_aIn[me_pIn];
me_pIn++;
}
else
{
me_SBB = 0;
}
me_BC = CHAR_BIT;
} ;
me_BC -= iN;
mBB |= (me_SBB >> me_BC);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -