📄 war.cpp
字号:
while (heap_length != 1)
{
heap_value = heap[1];
heap[1] = heap[heap_length--];
reheap (1);
findex = heap_length + 255;
frequency_count[findex] = frequency_count[heap[1]] +
frequency_count[heap_value];
father[heap_value] = findex;
father[heap[1]] = -findex;
heap[1] = findex;
reheap (1);
}
father[256] = 0;
}
/**************************************************************************
REHEAP ()
This function creates a "legal" heap from the current heap tree structure.
**************************************************************************/
void reheap (unsigned short heap_entry)
{
register unsigned short index;
register unsigned short flag = 1;
unsigned long heap_value;
heap_value = heap[heap_entry];
while ((heap_entry <= (heap_length >> 1)) && (flag))
{
index = heap_entry << 1;
if (index < heap_length)
if (frequency_count[heap[index]] >= frequency_count[heap[index+1]])
index++;
if (frequency_count[heap_value] < frequency_count[heap[index]])
flag--;
else
{
heap[heap_entry] = heap[index];
heap_entry = index;
}
}
heap[heap_entry] = heap_value;
}
/**************************************************************************
BUILD_INITIAL_HEAP ()
This function builds a heap from the initial frequency count data.
**************************************************************************/
void build_initial_heap ()
{
void reheap (unsigned short heap_entry);
register unsigned short loop;
heap_length = 0;
for (loop = 0; loop < 256; loop++)
if (frequency_count[loop])
heap[++heap_length] = (unsigned long) loop;
for (loop = heap_length; loop > 0; loop--)
reheap (loop);
}
/**************************************************************************
GET_FREQUENCY_COUNT ()
This function counts the number of occurrences of each byte in the data
that are to be compressed.
**************************************************************************/
void get_frequency_count ()
{
register unsigned long loop;
for (loop = 0; loop < file_size; loop++)
frequency_count[getc (ifile)]++;
}
void uncompress()
{
printf ("Decompression begins...\n");
if ((ifile = fopen (com, "rb")) != NULL)
{
fread (&ufile_size, sizeof (ufile_size), 1, ifile);
fread (ucode, 2, 256, ifile);
fread (ucode_length, 1, 256, ifile);
build_decomp_tree ();
if ((ofile = fopen (uncom, "wb")) != NULL)
{
decompress_image();
fclose (ofile);
}
fclose (ifile);
}
}
/**************************************************************************
BUILD_DECOMP_TREE ()
This function builds the decompression tree.
**************************************************************************/
void build_decomp_tree ()
{
register unsigned short loop1;
register unsigned short current_index;
unsigned short loop;
unsigned short current_node = 1;
decomp_tree[1] = 1;
for (loop = 0; loop < 256; loop++)
{
if (ucode_length[loop])
{
current_index = 1;
for (loop1 = ucode_length[loop] - 1; loop1 > 0; loop1--)
{
current_index = (decomp_tree[current_index] << 1) +
((ucode[loop] >> loop1) & 1);
if (!(decomp_tree[current_index]))
decomp_tree[current_index] = ++current_node;
}
decomp_tree[(decomp_tree[current_index] << 1) +(ucode[loop] & 1)] = -loop;
}
}
}
/**************************************************************************
DECOMPRESS_IMAGE ()
This function decompresses the compressed image.
**************************************************************************/
void decompress_image ()
{
register unsigned short cindex = 1;
register char curchar;
register short bitshift;
unsigned long charcount = 0L;
while (charcount < ufile_size)
{
curchar = (char) getc (ifile);
for (bitshift = 7; bitshift >= 0; --bitshift)
{
cindex = (cindex << 1) + ((curchar >> bitshift) & 1);
if (decomp_tree[cindex] <= 0)
{
putc ((int) (-decomp_tree[cindex]), ofile);
if ((++charcount) == ufile_size)
bitshift = 0;
else
cindex = 1;
}
else
cindex = decomp_tree[cindex];
}
}
}
void extract(char* war, char* unwar)
{
int j = 1, i;
FILE *in, *out;
in = fopen(war,"rb");//打开解包的.war的路径
CreateDirectory(unwar,NULL);
for (i=0; i<Dnum; i++) /*这个循环将按照源文件夹的顺序创建所有的文件夹*/
{
char temp[100]; /*主要通过遍历文件夹数组,得到所有文件夹的路径*/
ZeroMemory(temp,100);
strcpy(temp,unwar); /*然后调用CreateDirectory函数创建文件夹*/
strcat(temp,Dnode[i].path);
CreateDirectory(temp,NULL);
}
for (i=0; i<num; i++) /*这个循环从.war文件中读取文件,依靠fcount来作为文件的边界*/
{
char tep[100]; /*根据文件结构体的mothername得到文件相对于unwar的路径*/
ZeroMemory(tep,100);
strcpy(tep,unwar); /*对应拷贝到上个循环得到的所有文件夹中*/
strcat(tep,Fnode[i].mothername);
strcat(tep,Fnode[i].name);
out = fopen(tep,"wb");
while ((j <= Fnode[i].fcount) && !feof(out))
{
fputc(fgetc(in),out);
j += 1;
}
fclose(out);
}
fclose(in);
}
//递归遍历
void FindFileInDir(char* pname, char* rootDir)
{
WIN32_FIND_DATA fd;//文件或文件夹的属性结构体
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
HANDLE hSearch;//文件或文件夹句柄
char filePathName[256];
char temp[50];
char tmpPath[256];//临时保存目录
ZeroMemory(filePathName, 256);
ZeroMemory(tmpPath, 256);
ZeroMemory(temp, MAC_FILENAMELENOPATH);
strcpy(filePathName, rootDir);
BOOL bSearchFinished = FALSE;
if( filePathName[strlen(filePathName) -1] != '\\' )//向文件夹后面加上'\'
{ strcat(rootDir,"\\");
strcat(pname,"\\");
strcat(filePathName, "\\");
}
strcat(filePathName, "*");
hSearch = FindFirstFile(filePathName, &fd);
//Is directory
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
strcpy(temp,pname);
strcat(temp,fd.cFileName);
strcpy(tmpPath, rootDir);
strcat(tmpPath, fd.cFileName);
strcpy(Dnode[Dnum++].path,temp);//保存得到的文件夹属性
FindFileInDir(temp, tmpPath);//递归
}
else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
strcpy(Fnode[num].path,rootDir); //保存得到的文件属性
strcat(Fnode[num].path,fd.cFileName);
strcpy(Fnode[num].mothername,pname);
strcpy(Fnode[num++].name,fd.cFileName);
}
while( !bSearchFinished )
{
if( FindNextFile(hSearch, &fd) )
{
if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
strcpy(temp,pname);
strcat(temp,fd.cFileName);
strcpy(tmpPath, rootDir);
strcat(tmpPath, fd.cFileName);
strcpy(Dnode[Dnum++].path,temp);
FindFileInDir(temp, tmpPath);//递归
}
else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{ strcpy(Fnode[num].path,rootDir);
strcat(Fnode[num].path,fd.cFileName);
strcpy(Fnode[num].mothername,pname);
strcpy(Fnode[num++].name,fd.cFileName);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished
{
bSearchFinished = TRUE;
}
else
bSearchFinished = TRUE; //Terminate Search
}
}
FindClose(hSearch);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -