#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "lzw.h"
char *decode(unsigned char *buffer,unsigned int code)
{
int i=0;
while(code>257)
{
*buffer++=lzw->suffix[code];
code=lzw->prefix[code];
if (i++>=TABLE_SIZE)
{
printf("内存溢出!\n");
exit(1);
}
}
*buffer=code;
return(buffer);
}
unsigned int hashsearch(int prefix,unsigned int suffix)
{
int index;
int offset;
index=(suffix<<HASHING_SHIFT)^prefix;
if(index==0)
offset=1;
else
offset=TABLE_SIZE-index;
while(1)
{
if(lzw->code[index]==-1)
return(index);
if(lzw->prefix[index]==prefix&&lzw->suffix[index]==suffix)
return(index);
index-=offset;
if(index<0) index+=TABLE_SIZE;
}
}
void outcode(FILE *output,unsigned int code)
{
static int ob=0;
static unsigned long obb=0L;
obb |=(unsigned long) code<<(32-BITS-ob);
ob+=BITS;
while(ob>=8)
{
putc(obb>>24,output);
obb<<=8;
ob-=8;
}
return;
}
unsigned int incode(FILE *input)
{
unsigned int ret;
static unsigned long ibb=0L;
static int ib=0; //使用静态变量,保存输入的位数
while(ib<=24)
{
ibb|=(unsigned long)getc(input)<<(24-ib);
ib+=8;
}
ret=ibb>>(32-BITS);
ibb<<=BITS;
ib-=BITS;
return(ret);
}
void compress(FILE *input,FILE *output)
{
unsigned int curr_code; //当前使用的标号
unsigned int suffix; //后缀字符
unsigned int prefix; //前缀字符
unsigned int index;
int i;
lzw1.code=malloc(TABLE_SIZE*sizeof(unsigned int)); //标号值数组
lzw1.prefix=malloc(TABLE_SIZE*sizeof(unsigned int)); //前缀数据
lzw1.suffix=malloc(TABLE_SIZE*sizeof(unsigned char)); //后缀数据
if (lzw1.code==NULL||lzw1.prefix==NULL||lzw1.suffix==NULL)
{
printf("内存分配失败!\n");
getch();
exit(1);
}
lzw=&lzw1;
curr_code=258;
for(i=0;i<TABLE_SIZE;i++)
lzw->code[i]=-1;
i=0;
printf("\nCompressing.");
prefix=getc(input);
while((suffix=getc(input))!=(unsigned)EOF)
{
if(++i==100)
{
i=0;
printf(".");
}
index=hashsearch(prefix,suffix);
if(lzw->code[index]!=-1)
prefix=lzw->code[index];
else{
if (curr_code<=MAX_CODE){
lzw->code[index]=curr_code++;
lzw->prefix[index]=prefix;
lzw->suffix[index]=suffix;
}
outcode(output,prefix);
prefix=suffix;
}
}
outcode(output,prefix);
outcode(output,(MAX_VALUE));
outcode(output,0);
free(lzw->code);
free(lzw->prefix);
free(lzw->suffix);
}
void expand(FILE *input,FILE *output)
{
unsigned int curr_code;
unsigned int suffix;
unsigned int prefix;
int ch;
int i;
unsigned char *ps;
char *decode(unsigned char *buffer,unsigned int code);
lzw1.code=malloc(TABLE_SIZE*sizeof(unsigned int)); //代码值数组
lzw1.prefix=malloc(TABLE_SIZE*sizeof(unsigned int)); //压缩前数据
lzw1.suffix=malloc(TABLE_SIZE*sizeof(unsigned char)); //压缩后数据
lzw=&lzw1;
if(lzw1.code==NULL||lzw1.prefix==NULL||lzw1.suffix==NULL)
{
printf("Fatal error allocating table space!\n");
getch();
exit(1);
}
curr_code=258;
i=0;
printf("\nExpanding.");
prefix=incode(input);
ch=prefix;
putc(prefix,output);
while((suffix=incode(input))!=(MAX_VALUE))
{
if(++i==100)
{
i=0;
printf(".");
}
if(suffix>=curr_code)
{
*decode_stack=ch;
ps=decode(decode_stack+1,prefix);
}
else
ps=decode(decode_stack,suffix);
ch=*ps;
while(ps>=decode_stack)
putc(*ps--,output);
if(curr_code<=MAX_CODE)
{
lzw->prefix[curr_code]=prefix;
lzw->suffix[curr_code]=ch;
curr_code++;
}
prefix=suffix;
}
free(lzw->code);
free(lzw->prefix);
free(lzw->suffix);
}
int main(int argc,char *argv[])
{
FILE *fp1,*fp2;
int flag=0;
char *op;
if(argc!=4)
flag=1;
else{
op=argv[1];
if((strcmp(op,"-z")!=0)&&strcmp(op,"-e")!=0) flag=1;
}
if(flag)
{
printf("usage:lzw -z/-e source dest\n");
exit(1);
}
if((fp1=fopen(argv[2],"rb"))==NULL)
{
printf("不能打开源文件!\n");
exit(1);
}
if((fp2=fopen(argv[3],"wb"))==NULL)
{
printf("不能创建目标文件!\n");
exit(1);
}
if(strcmp(op,"-z")==0)
compress(fp1,fp2); //压缩
else
expand(fp1,fp2); //解压缩
fclose(fp1);
fclose(fp2);
getch();
return 0;
}
LZW.rar_lzw
版权申诉
140 浏览量
2022-09-24
02:48:31
上传
评论
收藏 2KB RAR 举报
alvarocfc
- 粉丝: 105
- 资源: 1万+
最新资源
- 3122080306 邹子轩 实验报告二.docx
- 基于STM32 NUCLEO板设计彩色LED照明灯(纯cubeMX开发)(大赛作品,文档完整,可直接运行)
- 发那科工业机器人保养大全
- Sphere.h
- REMD固有时间尺度分解信号分量可视化(Matlab完整源码和数据)
- 嵌入式系统双单片机STC89C52+STC15W104多功能学习板电路图可扩展 适用于单片机初学者和教学
- 基于STM32蓝牙控制小车系统设计(硬件+源代码+论文)大赛作品
- XILINXFPGA源码基于Spartan3火龙刀系列FPGA开发板VGA测试例程
- Java聊天室的设计与实现【尚学堂·百战程序员】
- python中matplotlib教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0