?? lzari.cpp
字號:
{
int i, j, k;
i = 1; j = N;
while (i < j)
{
k = (i + j) / 2;
if (position_cum[k] > x) i = k + 1; else j = k;
}
return i - 1;
}
void LZARI::StartDecode(void)
{
int i;
for (i = 0; i < M + 2; i++)
value = 2 * value + GetBit();
}
int LZARI::DecodeChar(void)
{
int sym, ch;
unsigned long int range;
range = high - low;
sym = BinarySearchSym((unsigned int)
(((value - low + 1) * sym_cum[0] - 1) / range));
high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
low += (range * sym_cum[sym ]) / sym_cum[0];
for ( ; ; ) {
if (low >= Q2) {
value -= Q2; low -= Q2; high -= Q2;
} else if (low >= Q1 && high <= Q3) {
value -= Q1; low -= Q1; high -= Q1;
} else if (high > Q2) break;
low += low; high += high;
value = 2 * value + GetBit();
}
ch = sym_to_char[sym];
UpdateModel(sym);
return ch;
}
int LZARI::DecodePosition(void)
{
int position;
unsigned long int range;
range = high - low;
position = BinarySearchPos((unsigned int)
(((value - low + 1) * position_cum[0] - 1) / range));
high = low + (range * position_cum[position ]) / position_cum[0];
low += (range * position_cum[position + 1]) / position_cum[0];
for ( ; ; ) {
if (low >= Q2) {
value -= Q2; low -= Q2; high -= Q2;
} else if (low >= Q1 && high <= Q3) {
value -= Q1; low -= Q1; high -= Q1;
} else if (high > Q2) break;
low += low; high += high;
value = 2 * value + GetBit();
}
return position;
}
/********** Encode and Decode **********/
void LZARI::Encode(void)
{
int i, c, len, r, s, last_match_length;
if(!m_bMem)
{
fseek(infile, 0L, SEEK_END);
textsize = ftell(infile);
if (fwrite(&textsize, sizeof textsize, 1, outfile) < 1)
Error("Write Error"); /* output size of text */
codesize += sizeof textsize;
if (textsize == 0) return;
rewind(infile);
textsize = 0;
}
else
{
textsize = m_nInLength;
m_OutBuffer.resize(sizeof textsize);
memcpy(&m_OutBuffer[0],&textsize,sizeof textsize);
//m_nOutCur += sizeof textsize;
codesize += sizeof textsize;
if(textsize == 0) return;
m_nInCur = 0;
textsize = 0;
}
StartModel();
InitTree();
s = 0; r = N - F;
for (i = s; i < r; i++) text_buf[i] = ' ';
if(!m_bMem)
for (len = 0; len < F && (c = getc(infile)) != EOF; len++) text_buf[r + len] = c;
else
for (len = 0; len < F && m_nInCur < m_nInLength ; len++)
{
c = m_pInBuffer[m_nInCur++];
text_buf[r + len] = c;
}
textsize = len;
for (i = 1; i <= F; i++) InsertNode(r - i);
InsertNode(r);
do {
if (match_length > len) match_length = len;
if (match_length <= THRESHOLD)
{
match_length = 1; EncodeChar(text_buf[r]);
}
else
{
EncodeChar(255 - THRESHOLD + match_length);
EncodePosition(match_position - 1);
}
last_match_length = match_length;
if(!m_bMem)
{
for (i = 0; i < last_match_length && (c = getc(infile)) != EOF; i++)
{
DeleteNode(s); text_buf[s] = c;
if (s < F - 1) text_buf[s + N] = c;
s = (s + 1) & (N - 1);
r = (r + 1) & (N - 1);
InsertNode(r);
}
}
else
{
for (i = 0; i < last_match_length && m_nInCur < m_nInLength ; i++)
{
c = m_pInBuffer[m_nInCur++];
DeleteNode(s);
text_buf[s] = c;
if (s < F - 1) text_buf[s + N] = c;
s = (s + 1) & (N - 1);
r = (r + 1) & (N - 1);
InsertNode(r);
}
}
if ((textsize += i) > printcount)
{
#ifdef _OUTPUT_STATUS
printf("%12ld\r", textsize);
#endif
printcount += 1024;
}
while (i++ < last_match_length)
{
DeleteNode(s);
s = (s + 1) & (N - 1);
r = (r + 1) & (N - 1);
if (--len) InsertNode(r);
}
} while (len > 0);
EncodeEnd();
#ifdef _OUTPUT_STATUS
printf("In : %lu bytes\n", textsize);
printf("Out: %lu bytes\n", codesize);
printf("Out/In: %.3f\n", (double)codesize / textsize);
#endif
}
void LZARI::Decode(void)
{
int i, j, k, r, c;
unsigned long int count;
if (!m_bMem)
{
if (fread(&textsize, sizeof textsize, 1, infile) < 1)
Error("Read Error"); /* read size of text */
}
else
{
if(m_nInLength < sizeof textsize)
Error("Read Error");
memcpy(&textsize,m_pInBuffer + m_nInCur,sizeof textsize);
//m_OutBuffer.reserve(textsize);
m_nOutLength = textsize;
//m_nOutCur = 0;
m_nInCur += sizeof textsize;
}
if (textsize == 0) return;
StartDecode();
StartModel();
for (i = 0; i < N - F; i++) text_buf[i] = ' ';
r = N - F;
for (count = 0; count < textsize; )
{
c = DecodeChar();
if (c < 256)
{
if(!m_bMem)
putc(c, outfile);
else
{
//m_OutBuffer[m_nOutCur++] = c;
m_OutBuffer.push_back(c);
//m_nOutCur++;
}
text_buf[r++] = c;
r &= (N - 1);
count++;
}
else
{
i = (r - DecodePosition() - 1) & (N - 1);
j = c - 255 + THRESHOLD;
for (k = 0; k < j; k++)
{
c = text_buf[(i + k) & (N - 1)];
if(!m_bMem)
putc(c, outfile);
else
{
// m_pOutBuffer[m_nOutCur++] = c;
m_OutBuffer.push_back(c);
//m_nOutCur ++;
}
text_buf[r++] = c;
r &= (N - 1);
count++;
}
}
if (count > printcount)
{
#ifdef _OUTPUT_STATUS
printf("%12lu\r", count);
#endif
printcount += 1024;
}
}
#ifdef _OUTPUT_STATUS
printf("%12lu\n", count);
#endif
}
void LZARI::Compress(const char *lpszInfile,const char *lpszOutfile)
{
m_bMem = FALSE;
infile = fopen(lpszInfile,"rb");
outfile = fopen(lpszOutfile,"wb");
if(infile && outfile)
{
Encode();
fclose(infile);
fclose(outfile);
infile = NULL;
outfile = NULL;
}
}
void LZARI::UnCompress(const char *lpszInfile,const char *lpszOutfile)
{
m_bMem = FALSE;
infile = fopen(lpszInfile,"rb");
outfile = fopen(lpszOutfile,"wb");
if(infile && outfile)
{
Decode();
fclose(infile);
fclose(outfile);
infile = NULL;
outfile = NULL;
}
}
void LZARI::Compress(const BYTE *pInBuffer,int nInLength,const BYTE *&pOutBuffer ,int &nOutLength)
{
m_pInBuffer = pInBuffer;
m_nInLength = nInLength;
m_nInCur = 0;
// m_nOutCur = 0;
m_bMem = TRUE;
Encode();
pOutBuffer = &m_OutBuffer[0];
nOutLength = m_OutBuffer.size();
}
void LZARI::UnCompress(const BYTE *pInBuffer,int nInLength,const BYTE *&pOutBuffer ,int &nOutLength)
{
m_pInBuffer = pInBuffer;
m_nInLength = nInLength;
m_nInCur = 0;
m_bMem = TRUE;
Decode();
pOutBuffer = &m_OutBuffer[0];
nOutLength = m_OutBuffer.size();
m_OutBuffer.push_back(0);
}
void LZARI::Release()
{
if(!m_OutBuffer.empty())
{
infile = NULL;
outfile = NULL;
textsize = 0;
codesize = 0;
printcount = 0;
low = 0;
high = Q4;
value = 0;
shifts = 0;
m_bMem = FALSE;
m_pInBuffer = NULL;
m_nInLength = 0;
m_nInCur = 0;
m_OutBuffer.clear();
m_nOutLength = 0;
buffer_putbit = 0;
mask_putbit = 128;
buffer_getbit = 0;
mask_getbit = 0;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -