实用的数据压缩解压缩方法

/*
*
*time:2014-10-27
*/
#define DATA_NUM 4
#define DATA_LENGTH 256
typedef unsigned char Byte;

//reach_data, contains several data blocks
typedef struct st_ReachData{
    int sth;//identify for reach_data
    size_t length[DATA_NUM];//data length
    Byte data[DATA_NUM][DATA_LENGTH];//several data blocks
}ReachData;

//thin_data,contains only one data block
typedef struct st_ThinData{
    int sth;//identify for original reach_data
    int index;//current data block 's index in original reach_data
    size_t length;//data length
    Byte data[DATA_LENGTH];//data
}ThinData;

void*CopyMem(const void*src, void*dst, size_t length){
    const Byte*bsrc;
    Byte *bdst;
    assert(src!=NULL&&dst!=NULL);
    bsrc=(Byte*)src;
    bdst=(Byte*)dst;
    while(length-->0)
        *bdst++=*bsrc++;
    return dst;
}

//zip,ReachData->ThinData;
//reachData
//pNum; when return , is the num of reach_data contains effective data blocks
//thinData
//return: num of thin_data
int ZipData(const ReachData*reachData, int*pNum, ThinData*thinData){
    int num=*pNum, realNum=*pNum;
    int thinNum=0;
    const ReachData*tmp;
    ThinData*tmpThin,*thinDst=thinData;
    assert(reachData!=NULL&&pNum!=NULL&&thinData!=NULL);
    for(tmp=reachData; tmplength[j]>0){
                isEmpty=false;
                ++thinNum;
            }
        }
        if(isEmpty)
            --realNum;
    }
    if(0==thinNum){
        *pNum=0;
        return 0;
    }
    tmpThin=new ThinData[thinNum];
    for(tmp=reachData, thinData=tmpThin; tmplength[j]>0){
                thinData->sth=tmp->sth;
                thinData->length=tmp->length[j];
                thinData->index=j;
                CopyMem(tmp->data[j], thinData->data, thinData->length);
                ++thinData;
            }
        }
    }
    for(int k=0; kReachData;assume thin data comes from zip
//return:num of reach_data
int UnZipData(const ThinData*thinData, int num, ReachData*reachData){
    int reachNum=0;
    ReachData*tmpReach=NULL, *tmp;
    assert(num>=0);
    if(num==0)
        return 0;
    //tempt buffer
    tmpReach=new ReachData[num];
    tmp=tmpReach;
    for(int j=0; jsth=thinData[j].sth;
        for(int k=0; klength[k]=0;
        while(jsth){
            int index=thinData[j].index;//index of current data block
            assert(0<=index&&DATA_NUM>index);
            tmp->length[index]=thinData[j].length;//length
            //copy data
            CopyMem(thinData[j].data, tmp->data[index], thinData[j].length);
            ++j;
        }
    }
    //富数据的个数
    reachNum=tmp-tmpReach;
    //复制到目的地
    for(tmp=tmpReach; tmp0)
                printf("index=%d, length=%d, data=%s\n",
                       j, reachData[k].length[j], reachData[k].data[j]);
    }
}


你可能感兴趣的:(编程语言)