📄 compress.cpp
字号:
// Compress.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#define TABSIZE 4096
#define NO_PRED 0xFFFF
#define NOT_FND 0xFFFF
#define EMPTY 0xFFFF
#define FEOF 0xFFFF
#define newline "\r\n"
unsigned int h(unsigned int pred,unsigned char foll) ;
unsigned int eolist(unsigned int index) ;
unsigned int hash(unsigned int pred,unsigned char foll,bool update) ;
unsigned int unhash(unsigned int pred,unsigned char foll) ;
void init_tab() ;
void upd_tab(unsigned int pred,unsigned char foll) ;
void putcode(FILE * fd,unsigned int code) ;
void flushout(FILE * fd) ;
bool Create_Compress_File(const unsigned char *pBuffer , int count , char* filename);
struct entry {
bool used;
unsigned int next; /* hi bit is 'used' flag */
unsigned int predecessor; /* 12 bit code */
unsigned char follower;
} string_tab[TABSIZE];
unsigned int outbuf = EMPTY;
int count_of_char = 0 , total_of_char = 0 ;
unsigned int h(unsigned int pred,unsigned char foll)
{
long temp, local; /* 32 bit receiving field for local^2 */
local = (pred + foll) | 0x0800;
temp = local * local;
local = (temp >> 6) & 0x0FFF;
return local; /* middle 12 bits of result */
}
/* return last element in a collision list */
unsigned int eolist(unsigned int index)
{
int temp;
while ( 0 != (temp = string_tab[index].next) )
index = temp;
return index;
}
unsigned int hash(unsigned int pred,unsigned char foll,bool update)
{
unsigned int local, tempnext;
struct entry *ep;
local = h(pred,foll);
if ( !string_tab[local].used )
return local;
else
{
local = eolist(local);
/* search for free entry from local + 101 */
tempnext = (local + 101) & 0x0FFF;
ep = &string_tab[tempnext]; /* initialize pointer */
while ( ep->used )
{
++tempnext;
if ( tempnext == TABSIZE )
{
tempnext = 0; /* handle wrap to beginning of table */
ep = string_tab; /* address of first element of table */
}
else
++ep; /* point to next element in table */
}
if ( update ) /* if update requested */
string_tab[local].next = tempnext;
return tempnext;
}
}
unsigned int unhash(unsigned int pred,unsigned char foll)
{
unsigned int local;
struct entry *ep; /* pointer to current entry */
local = h(pred,foll);
while(true)
{
ep = &string_tab[local];
if ( (ep->predecessor == pred) && (ep->follower == foll) )
return local;
if ( ep->next == 0 )
return NOT_FND;
local = ep->next;
}
}
void init_tab()
{
unsigned int i;
memset( (char *)string_tab, 0 , sizeof(string_tab) );
for (i = 0; i <= 255; i++)
{
upd_tab(NO_PRED,i);
}
return ;
}
void upd_tab(unsigned int pred,unsigned char foll)
{
struct entry *ep; /* pointer to current entry */
ep = &string_tab[ hash(pred,foll,true) ];
ep->used = true;
ep->next = 0;
ep->predecessor = pred;
ep->follower = foll;
return ;
}
void putcode(FILE * fd,unsigned int code)
{
unsigned char ch ;
char AfxBuffer[10] ;
if (EMPTY == outbuf)
{
ch = (code >> 4) & 0xFF ;
sprintf( AfxBuffer ,"0x%02x,", ch );
fwrite( AfxBuffer, 1, strlen(AfxBuffer), fd );
total_of_char ++ ;
count_of_char ++ ;
if(count_of_char>=16)
{
fwrite( newline, 1, strlen(newline), fd );
count_of_char = 0 ;
}
outbuf = code & 0x000F;
}
else
{
ch = ((outbuf << 4) & 0xFF0) + ((code >> 8) & 0x00F) ;
sprintf( AfxBuffer ,"0x%02x,", ch );
fwrite( AfxBuffer, 1, strlen(AfxBuffer), fd );
count_of_char ++ ;
total_of_char ++ ;
if(count_of_char>=16)
{
fwrite( newline, 1, strlen(newline), fd );
count_of_char = 0 ;
}
ch = code & 0x00FF ;
sprintf( AfxBuffer ,"0x%02x,", ch );
fwrite( AfxBuffer, 1, strlen(AfxBuffer), fd );
count_of_char ++ ;
total_of_char ++ ;
if(count_of_char>=16)
{
fwrite( newline, 1, strlen(newline), fd );
count_of_char = 0 ;
}
outbuf = EMPTY;
}
return ;
}
void flushout(FILE * fd)
{
unsigned char ch;
char AfxBuffer[100] ;
if (EMPTY != outbuf)
{
ch = (outbuf << 4) & 0x00F0 ;
sprintf( AfxBuffer ,"0x%02x,", ch );
fwrite( AfxBuffer, 1, strlen(AfxBuffer), fd );
total_of_char ++ ;
}
outbuf = EMPTY ;
fwrite( newline, 1, strlen(newline), fd );
sprintf( AfxBuffer ,"Total = 0x%x ", total_of_char );
fwrite( AfxBuffer, 1, strlen(AfxBuffer), fd );
}
bool Create_Compress_File(const unsigned char *pBuffer ,int count , char filename[80] )
{
unsigned char c ;
unsigned int code, localcode;
int i , code_count ;
FILE *filehandle;
if((filehandle=fopen(filename,"wb"))==NULL)
{
printf("cannot create file\n");
}
outbuf = EMPTY;
code_count = TABSIZE - 256;
total_of_char = 0;
count_of_char = 0;
init_tab(); /* initialize code table */
i =0 ;
c = pBuffer[i++];
code = unhash(NO_PRED,c);
while ( i < count )
{
c = pBuffer[i++] ;
if ( NOT_FND != (localcode = unhash(code,c)) )
{
code = localcode;
continue;
}
putcode(filehandle,code); /* only update table if table isn't full */
if ( code_count )
{
upd_tab(code,c);
--code_count;
}
code = unhash(NO_PRED,c);
}
putcode(filehandle,code);
flushout(filehandle);
fclose(filehandle);
return true;
}
const unsigned char MP3_DECODER[4014] =
{
58, 1,
42,4,
40,0,
41,0,
32,0,
33,0,
34,0,
35,0,
36,0,
37,0,
38,0,
39,0,
40,1,
40,2,
33,143,
40,3,
33,0,
40,4,
40,5,
40,6,
40,7,
40,8,
40,9,
40,10,
40,11,
40,12,
32,128,
33,144,
40,13,
32,0,
33,0,
40,14,
32,129,
33,145,
40,15,
32,0,
33,146,
40,16,
33,0,
40,17,
33,147,
40,18,
33,0,
40,19,
40,20,
40,21,
32,130,
40,22,
32,0,
40,23,
40,24,
40,25,
33,148,
40,26,
33,149,
40,27,
33,150,
40,28,
33,0,
40,29,
32,131,
40,30,
32,0,
40,31,
33,151,
40,32,
33,0,
40,33,
40,34,
40,35,
40,36,
40,37,
40,38,
40,39,
40,40,
40,41,
40,42,
32,132,
40,43,
32,0,
40,44,
40,45,
40,46,
40,47,
40,48,
40,49,
40,50,
32,133,
40,51,
32,0,
40,52,
40,53,
40,54,
40,55,
33,152,
40,56,
33,0,
40,57,
40,58,
40,59,
40,60,
40,61,
40,62,
40,63,
40,64,
40,65,
40,66,
40,67,
40,68,
40,69,
40,70,
40,71,
40,72,
40,73,
40,74,
40,75,
40,76,
40,77,
40,78,
40,79,
40,80,
40,81,
40,82,
40,83,
40,84,
40,85,
40,86,
40,87,
40,88,
40,89,
40,90,
40,91,
40,92,
40,93,
40,94,
40,95,
40,96,
40,97,
40,98,
33,153,
40,99,
33,0,
40,100,
40,101,
40,102,
40,103,
40,104,
40,105,
40,106,
40,107,
40,108,
40,109,
40,110,
40,111,
40,112,
40,113,
40,114,
40,115,
40,116,
40,117,
40,118,
40,119,
40,120,
40,121,
40,122,
40,123,
40,124,
40,125,
40,126,
40,127,
40,128,
40,129,
40,130,
40,131,
40,132,
40,133,
40,134,
40,135,
40,136,
40,137,
40,138,
40,139,
40,140,
40,141,
40,142,
40,143,
40,144,
40,145,
32,134,
40,146,
32,135,
40,147,
32,0,
40,148,
40,149,
40,150,
40,151,
40,152,
40,153,
40,154,
40,155,
40,156,
40,157,
40,158,
40,159,
33,154,
40,160,
33,0,
40,161,
40,162,
40,163,
40,164,
40,165,
40,166,
40,167,
40,168,
40,169,
40,170,
40,171,
40,172,
40,173,
40,174,
40,175,
40,176,
40,177,
40,178,
40,179,
40,180,
40,181,
40,182,
40,183,
40,184,
40,185,
32,136,
40,186,
32,0,
40,187,
32,137,
40,188,
32,0,
40,189,
40,190,
40,191,
40,192,
40,193,
40,194,
40,195,
33,155,
40,196,
33,0,
40,197,
40,198,
40,199,
40,200,
40,201,
40,202,
40,203,
40,204,
40,205,
40,206,
40,207,
40,208,
40,209,
40,210,
40,211,
40,212,
40,213,
40,214,
40,215,
40,216,
40,217,
40,218,
40,219,
40,220,
40,221,
40,222,
40,223,
40,224,
40,225,
32,138,
40,226,
32,139,
40,227,
32,0,
40,228,
40,229,
40,230,
40,231,
40,232,
40,233,
40,234,
40,235,
40,236,
40,237,
40,238,
40,239,
40,240,
40,241,
40,242,
40,243,
40,244,
40,245,
40,246,
40,247,
40,248,
32,140,
40,249,
32,0,
33,156,
40,250,
33,157,
40,251,
32,141,
33,158,
40,252,
32,142,
33,159,
40,253,
32,0,
33,0,
40,254,
40,255,
42,1,
40,0,
34,1,
35,149,
36,137,
37,7,
38,164,
39,7,
40,1,
33,199,
34,0,
35,128,
38,196,
39,12,
40,2,
32,9,
33,28,
34,4,
38,170,
39,10,
40,3,
32,0,
33,0,
34,0,
35,166,
38,160,
39,7,
40,4,
40,5,
32,5,
35,132,
38,180,
39,9,
40,6,
32,0,
34,3,
35,0,
36,129,
37,192,
38,171,
39,10,
40,7,
33,174,
34,0,
35,149,
36,137,
37,7,
38,164,
39,0,
40,8,
33,72,
34,1,
35,128,
38,196,
39,12,
40,9,
32,9,
33,4,
34,4,
38,170,
39,10,
40,10,
32,0,
33,0,
34,0,
35,168,
38,164,
39,7,
40,11,
40,12,
33,64,
34,32,
35,128,
38,196,
39,12,
40,13,
34,36,
38,198,
40,14,
33,158,
34,0,
38,200,
40,15,
32,9,
33,2,
34,20,
38,170,
39,10,
40,16,
32,5,
33,0,
34,0,
35,132,
38,180,
39,9,
40,17,
32,0,
33,1,
35,0,
36,1,
37,192,
38,171,
39,10,
40,18,
33,195,
35,149,
36,137,
37,7,
38,32,
39,0,
40,19,
32,3,
33,194,
35,131,
38,38,
39,13,
40,20,
33,179,
34,8,
40,21,
32,0,
33,198,
34,0,
35,149,
38,0,
39,8,
40,22,
33,0,
35,0,
36,150,
37,3,
38,170,
39,10,
40,23,
32,66,
33,182,
35,128,
36,137,
37,7,
40,24,
32,0,
33,0,
35,148,
38,14,
40,25,
32,15,
35,132,
38,180,
39,9,
40,26,
32,0,
35,147,
38,168,
39,3,
40,27,
38,40,
39,0,
40,28,
33,1,
35,0,
36,1,
37,192,
38,171,
39,10,
40,29,
33,197,
35,149,
36,137,
37,7,
38,32,
39,0,
40,30,
33,0,
35,148,
38,0,
39,10,
40,31,
38,14,
40,32,
32,3,
33,121,
34,1,
35,131,
38,38,
39,13,
40,33,
33,50,
40,34,
32,0,
33,4,
34,0,
35,128,
36,0,
37,252,
38,170,
39,10,
40,35,
33,0,
35,0,
37,0,
40,36,
33,163,
34,13,
35,128,
36,158,
37,59,
40,37,
32,66,
33,87,
34,1,
36,137,
37,7,
40,38,
32,0,
33,67,
34,13,
36,0,
37,56,
40,39,
33,8,
34,152,
35,149,
36,137,
37,7,
38,36,
39,0,
40,40,
32,66,
33,147,
34,1,
35,128,
38,170,
39,10,
40,41,
32,3,
33,126,
34,4,
35,131,
38,38,
39,13,
40,42,
32,0,
33,176,
34,0,
35,149,
38,160,
39,7,
40,43,
33,0,
35,0,
36,12,
37,4,
38,170,
39,10,
40,44,
33,2,
35,128,
36,134,
37,195,
38,171,
40,45,
32,66,
33,151,
34,1,
36,137,
37,7,
38,170,
40,46,
32,0,
33,0,
34,0,
36,31,
37,4,
40,47,
33,178,
35,149,
36,137,
37,7,
38,32,
39,4,
40,48,
32,66,
33,139,
34,4,
35,128,
38,170,
39,10,
40,49,
32,0,
33,177,
34,0,
35,149,
38,160,
39,7,
40,50,
32,3,
33,117,
34,4,
35,131,
38,38,
39,13,
40,51,
32,5,
33,0,
34,0,
35,132,
38,180,
39,9,
40,52,
32,0,
33,96,
35,0,
36,1,
37,192,
38,171,
39,10,
40,53,
33,8,
34,152,
35,149,
36,137,
37,7,
38,36,
39,0,
40,54,
33,188,
34,0,
40,55,
33,163,
34,13,
35,0,
36,1,
37,192,
38,171,
39,10,
40,56,
33,96,
34,0,
35,128,
36,0,
37,56,
38,170,
40,57,
33,6,
34,152,
35,149,
36,137,
37,7,
38,36,
39,0,
40,58,
33,190,
34,0,
40,59,
32,5,
33,0,
35,132,
38,180,
39,9,
40,60,
32,0,
33,96,
35,0,
36,1,
37,192,
38,171,
39,10,
40,61,
33,66,
34,1,
35,149,
36,137,
37,7,
38,36,
39,0,
40,62,
33,12,
34,152,
38,164,
39,7,
40,63,
33,8,
40,64,
33,96,
34,0,
35,0,
36,1,
37,192,
38,171,
39,10,
40,65,
33,12,
34,152,
35,149,
36,137,
37,7,
38,36,
39,0,
40,66,
32,5,
33,0,
34,0,
35,132,
38,180,
39,9,
40,67,
32,0,
35,128,
36,0,
37,16,
38,171,
39,10,
40,68,
33,34,
34,1,
35,149,
36,137,
37,7,
38,36,
39,0,
40,69,
33,0,
34,0,
35,0,
36,4,
37,18,
38,170,
39,10,
40,70,
32,3,
33,212,
34,5,
35,131,
36,137,
37,7,
38,38,
39,13,
40,71,
32,0,
33,96,
34,0,
35,0,
36,1,
37,192,
38,171,
39,10,
40,72,
33,179,
35,149,
36,137,
37,7,
38,0,
39,8,
40,73,
33,0,
35,0,
36,9,
37,0,
38,170,
39,10,
40,74,
33,2,
35,128,
36,134,
37,59,
40,75,
32,66,
33,221,
34,5,
36,137,
37,7,
40,76,
32,0,
33,0,
34,0,
36,31,
37,0,
40,77,
33,179,
35,149,
36,137,
37,7,
38,32,
39,0,
40,78,
33,0,
35,0,
36,151,
37,3,
38,170,
39,10,
40,79,
32,66,
33,226,
34,5,
35,128,
36,137,
37,7,
40,80,
32,0,
33,16,
34,0,
35,0,
36,128,
37,204,
38,171,
40,81,
33,0,
34,64,
35,180,
36,137,
37,7,
38,170,
39,4,
40,82,
34,0,
35,176,
38,146,
39,9,
40,83,
33,20,
34,1,
35,149,
38,0,
39,8,
40,84,
33,72,
35,164,
38,42,
39,0,
40,85,
33,0,
34,0,
35,0,
36,9,
37,0,
38,170,
39,10,
40,86,
33,8,
35,128,
36,134,
37,59,
40,87,
32,66,
33,17,
34,7,
36,137,
37,7,
40,88,
32,0,
33,0,
34,0,
36,31,
37,0,
40,89,
33,20,
34,1,
35,149,
36,137,
37,7,
38,32,
39,0,
40,90,
33,22,
38,2,
39,9,
40,91,
33,0,
34,0,
35,0,
36,128,
37,72,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -