Lzw解压缩算法

Lzw解压缩算法完整代码
/*============================================================================
 Name        : Exercise.cbp
 Author      : Haier
 Version     : 1.01
 Copyright   : Copyright (c) 2014
 Description : LZE DeCompress in C, Ansi-style, Compile by Code::Block
 ============================================================================*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BITS      (12)
#define HASHSHIFT (BITS-8)
#define MAXVALUE  ((1<<BITS)-1)
#define MAXCODE   (MAXVALUE-1)
#define TABLESIZE (4099)

struct Lzw
{
    int *code;      //已被编码
    int *prefix;    //编码索引
    char *suffix;   //对应字符
}Lzw,*lp;

char dectable[TABLESIZE];
/****************************************************************************
* Function     : incode
* Description  : acquire for the prefix
* Input        : The File of Input
* Return       : prefix
*****************************************************************************/

int incode(FILE *InputFile)
{
    static int ib=0;
    static unsigned long ibb=0L;
    int prefix;

    while(ib<=24)
    {
        ibb |=(unsigned)getc(InputFile)<<(24-ib);
        ib  +=8;
    }

    prefix=ibb>>(32-BITS);
    ibb <<=BITS;
    ib   -=BITS;

    return prefix;
}

/****************************************************************************
* Function     : decode
* Description  : decode for the index
* Input        : the index,the characters buffer
* Return       : the characters buffer
*****************************************************************************/

char *decode(char *buffer,int index)
{
    int BoundaryFlag=0;

    while(index>257)
    {
        *buffer++=lp->suffix[index];
        index=lp->prefix[index];

        if(BoundaryFlag++>TABLESIZE)
        {
            printf("The EMS memory spill over !");
            exit(1);
        }
    }

    *buffer=index;
    return buffer;
}

/****************************************************************************
* Function     : Common
* Description  : handle after failing to open file
* Input        : Filename
* Return       : void
*****************************************************************************/

void Common(char *FileName)
{
    char TempFileName[25];

    strcpy(TempFileName,"Can't open ");
    strcat(TempFileName,FileName);
    puts(TempFileName);
    exit(1);

}

/****************************************************************************
* Function     : DeCompress
* Description  : Lzw DeCompress Algorithm
* Input        : FileName of input ,FileName of Output
* Return       : void
*****************************************************************************/

void DeCompress(FILE *InputFile,FILE *OutputFile)
{
    int prefix,suffix,ccode=258;
    char TempChar,*CodeWord;

    Lzw.code  =malloc(TABLESIZE*sizeof(int));
    Lzw.prefix=malloc(TABLESIZE*sizeof(int));
    Lzw.suffix=malloc(TABLESIZE*sizeof(char));

    if(Lzw.code==NULL || Lzw.prefix==NULL || Lzw.suffix==NULL)
    {
        printf("The EMS memory spill over !");
        exit(1);
    }

    lp=&Lzw;


    prefix=incode(InputFile);
    TempChar=prefix;
    putc(prefix,OutputFile);
    while((suffix=incode(InputFile))!=MAXVALUE)
    {
        if(suffix>=ccode)
        {
            *dectable=TempChar;
            CodeWord =decode(dectable+1,prefix);
        }
        else
        {
            CodeWord =decode(dectable,suffix);
            TempChar =*CodeWord;
        }

        while(CodeWord>=dectable)
        {
            putc(*CodeWord--,OutputFile);
        }

        if(ccode<=MAXCODE)
        {
            lp->prefix[ccode]=prefix;
            lp->suffix[ccode]=TempChar;
            ccode++;
        }

        prefix=suffix;
    }

    free(lp->code);
    free(lp->prefix);
    free(lp->suffix);
}

/****************************************************************************
* Function     : main
*****************************************************************************/

int main(int argc,char *argv[])
{
    FILE *FileOfInput,*FileOfOutput;

    if(argc!=3)
    {
        printf("Usage: DeCompress FileInput FileOutput !");
        exit(1);
    }

    if((FileOfInput=fopen(argv[1],"rb"))==NULL)
    {
        Common(argv[1]);
    }

    if((FileOfOutput=fopen(argv[2],"wb"))==NULL)
    {
        Common(argv[2]);
    }

    DeCompress(FileOfInput,FileOfOutput);

    fclose(FileOfInput);
    fclose(FileOfOutput);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爻渡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值