kusano fc6ab3
#include <stdio.h></stdio.h>
kusano fc6ab3
#include <stdlib.h></stdlib.h>
kusano fc6ab3
#include <windows.h></windows.h>
kusano fc6ab3
kusano fc6ab3
#include "zlib.h"
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
void MyDoMinus64(LARGE_INTEGER *R,LARGE_INTEGER A,LARGE_INTEGER B)
kusano fc6ab3
{
kusano fc6ab3
    R->HighPart = A.HighPart - B.HighPart;
kusano fc6ab3
    if (A.LowPart >= B.LowPart)
kusano fc6ab3
        R->LowPart = A.LowPart - B.LowPart;
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        R->LowPart = A.LowPart - B.LowPart;
kusano fc6ab3
        R->HighPart --;
kusano fc6ab3
    }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#ifdef _M_X64
kusano fc6ab3
// see http://msdn2.microsoft.com/library/twchhe95(en-us,vs.80).aspx for __rdtsc
kusano fc6ab3
unsigned __int64 __rdtsc(void);
kusano fc6ab3
void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
kusano fc6ab3
{
kusano fc6ab3
 //   printf("rdtsc = %I64x\n",__rdtsc());
kusano fc6ab3
   pbeginTime64->QuadPart=__rdtsc();
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
kusano fc6ab3
{
kusano fc6ab3
    LARGE_INTEGER LIres;
kusano fc6ab3
    unsigned _int64 res=__rdtsc()-((unsigned _int64)(beginTime64.QuadPart));
kusano fc6ab3
    LIres.QuadPart=res;
kusano fc6ab3
   // printf("rdtsc = %I64x\n",__rdtsc());
kusano fc6ab3
    return LIres;
kusano fc6ab3
}
kusano fc6ab3
#else
kusano fc6ab3
#ifdef _M_IX86
kusano fc6ab3
void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
kusano fc6ab3
{
kusano fc6ab3
    DWORD dwEdx,dwEax;
kusano fc6ab3
    _asm
kusano fc6ab3
    {
kusano fc6ab3
        rdtsc
kusano fc6ab3
        mov dwEax,eax
kusano fc6ab3
        mov dwEdx,edx
kusano fc6ab3
    }
kusano fc6ab3
    pbeginTime64->LowPart=dwEax;
kusano fc6ab3
    pbeginTime64->HighPart=dwEdx;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
kusano fc6ab3
{
kusano fc6ab3
    myGetRDTSC32(pbeginTime64);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
kusano fc6ab3
{
kusano fc6ab3
    LARGE_INTEGER LIres,endTime64;
kusano fc6ab3
    myGetRDTSC32(&endTime64);
kusano fc6ab3
kusano fc6ab3
    LIres.LowPart=LIres.HighPart=0;
kusano fc6ab3
    MyDoMinus64(&LIres,endTime64,beginTime64);
kusano fc6ab3
    return LIres;
kusano fc6ab3
}
kusano fc6ab3
#else
kusano fc6ab3
void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
kusano fc6ab3
{
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
kusano fc6ab3
{
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
kusano fc6ab3
{
kusano fc6ab3
    LARGE_INTEGER lr;
kusano fc6ab3
    lr.QuadPart=0;
kusano fc6ab3
    return lr;
kusano fc6ab3
}
kusano fc6ab3
#endif
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
void BeginCountPerfCounter(LARGE_INTEGER * pbeginTime64,BOOL fComputeTimeQueryPerf)
kusano fc6ab3
{
kusano fc6ab3
    if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(pbeginTime64)))
kusano fc6ab3
    {
kusano fc6ab3
        pbeginTime64->LowPart = GetTickCount();
kusano fc6ab3
        pbeginTime64->HighPart = 0;
kusano fc6ab3
    }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
DWORD GetMsecSincePerfCounter(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
kusano fc6ab3
{
kusano fc6ab3
    LARGE_INTEGER endTime64,ticksPerSecond,ticks;
kusano fc6ab3
    DWORDLONG ticksShifted,tickSecShifted;
kusano fc6ab3
    DWORD dwLog=16+0;
kusano fc6ab3
    DWORD dwRet;
kusano fc6ab3
    if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))
kusano fc6ab3
        dwRet = (GetTickCount() - beginTime64.LowPart)*1;
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        MyDoMinus64(&ticks,endTime64,beginTime64);
kusano fc6ab3
        QueryPerformanceFrequency(&ticksPerSecond);
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
        {
kusano fc6ab3
            ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);
kusano fc6ab3
            tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);
kusano fc6ab3
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));
kusano fc6ab3
        dwRet *=1;
kusano fc6ab3
    }
kusano fc6ab3
    return dwRet;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
int ReadFileMemory(const char* filename,long* plFileSize,unsigned char** pFilePtr)
kusano fc6ab3
{
kusano fc6ab3
    FILE* stream;
kusano fc6ab3
    unsigned char* ptr;
kusano fc6ab3
    int retVal=1;
kusano fc6ab3
    stream=fopen(filename, "rb");
kusano fc6ab3
    if (stream==NULL)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    fseek(stream,0,SEEK_END);
kusano fc6ab3
kusano fc6ab3
    *plFileSize=ftell(stream);
kusano fc6ab3
    fseek(stream,0,SEEK_SET);
kusano fc6ab3
    ptr=malloc((*plFileSize)+1);
kusano fc6ab3
    if (ptr==NULL)
kusano fc6ab3
        retVal=0;
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
        if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
kusano fc6ab3
            retVal=0;
kusano fc6ab3
    }
kusano fc6ab3
    fclose(stream);
kusano fc6ab3
    *pFilePtr=ptr;
kusano fc6ab3
    return retVal;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
int main(int argc, char *argv[])
kusano fc6ab3
{
kusano fc6ab3
    int BlockSizeCompress=0x8000;
kusano fc6ab3
    int BlockSizeUncompress=0x8000;
kusano fc6ab3
    int cprLevel=Z_DEFAULT_COMPRESSION ;
kusano fc6ab3
    long lFileSize;
kusano fc6ab3
    unsigned char* FilePtr;
kusano fc6ab3
    long lBufferSizeCpr;
kusano fc6ab3
    long lBufferSizeUncpr;
kusano fc6ab3
    long lCompressedSize=0;
kusano fc6ab3
    unsigned char* CprPtr;
kusano fc6ab3
    unsigned char* UncprPtr;
kusano fc6ab3
    long lSizeCpr,lSizeUncpr;
kusano fc6ab3
    DWORD dwGetTick,dwMsecQP;
kusano fc6ab3
    LARGE_INTEGER li_qp,li_rdtsc,dwResRdtsc;
kusano fc6ab3
kusano fc6ab3
    if (argc<=1)
kusano fc6ab3
    {
kusano fc6ab3
        printf("run TestZlib <file> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");</file>
kusano fc6ab3
        return 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
kusano fc6ab3
    {
kusano fc6ab3
        printf("error reading %s\n",argv[1]);
kusano fc6ab3
        return 1;
kusano fc6ab3
    }
kusano fc6ab3
    else printf("file %s read, %u bytes\n",argv[1],lFileSize);
kusano fc6ab3
kusano fc6ab3
    if (argc>=3)
kusano fc6ab3
        BlockSizeCompress=atol(argv[2]);
kusano fc6ab3
kusano fc6ab3
    if (argc>=4)
kusano fc6ab3
        BlockSizeUncompress=atol(argv[3]);
kusano fc6ab3
kusano fc6ab3
    if (argc>=5)
kusano fc6ab3
        cprLevel=(int)atol(argv[4]);
kusano fc6ab3
kusano fc6ab3
    lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
kusano fc6ab3
    lBufferSizeUncpr = lBufferSizeCpr;
kusano fc6ab3
kusano fc6ab3
    CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
kusano fc6ab3
kusano fc6ab3
    BeginCountPerfCounter(&li_qp,TRUE);
kusano fc6ab3
    dwGetTick=GetTickCount();
kusano fc6ab3
    BeginCountRdtsc(&li_rdtsc);
kusano fc6ab3
    {
kusano fc6ab3
        z_stream zcpr;
kusano fc6ab3
        int ret=Z_OK;
kusano fc6ab3
        long lOrigToDo = lFileSize;
kusano fc6ab3
        long lOrigDone = 0;
kusano fc6ab3
        int step=0;
kusano fc6ab3
        memset(&zcpr,0,sizeof(z_stream));
kusano fc6ab3
        deflateInit(&zcpr,cprLevel);
kusano fc6ab3
kusano fc6ab3
        zcpr.next_in = FilePtr;
kusano fc6ab3
        zcpr.next_out = CprPtr;
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
        do
kusano fc6ab3
        {
kusano fc6ab3
            long all_read_before = zcpr.total_in;
kusano fc6ab3
            zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
kusano fc6ab3
            zcpr.avail_out = BlockSizeCompress;
kusano fc6ab3
            ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
kusano fc6ab3
            lOrigDone += (zcpr.total_in-all_read_before);
kusano fc6ab3
            lOrigToDo -= (zcpr.total_in-all_read_before);
kusano fc6ab3
            step++;
kusano fc6ab3
        } while (ret==Z_OK);
kusano fc6ab3
kusano fc6ab3
        lSizeCpr=zcpr.total_out;
kusano fc6ab3
        deflateEnd(&zcpr);
kusano fc6ab3
        dwGetTick=GetTickCount()-dwGetTick;
kusano fc6ab3
        dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
kusano fc6ab3
        dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
kusano fc6ab3
        printf("total compress size = %u, in %u step\n",lSizeCpr,step);
kusano fc6ab3
        printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
kusano fc6ab3
        printf("defcpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
kusano fc6ab3
        printf("defcpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);
kusano fc6ab3
    UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
kusano fc6ab3
kusano fc6ab3
    BeginCountPerfCounter(&li_qp,TRUE);
kusano fc6ab3
    dwGetTick=GetTickCount();
kusano fc6ab3
    BeginCountRdtsc(&li_rdtsc);
kusano fc6ab3
    {
kusano fc6ab3
        z_stream zcpr;
kusano fc6ab3
        int ret=Z_OK;
kusano fc6ab3
        long lOrigToDo = lSizeCpr;
kusano fc6ab3
        long lOrigDone = 0;
kusano fc6ab3
        int step=0;
kusano fc6ab3
        memset(&zcpr,0,sizeof(z_stream));
kusano fc6ab3
        inflateInit(&zcpr);
kusano fc6ab3
kusano fc6ab3
        zcpr.next_in = CprPtr;
kusano fc6ab3
        zcpr.next_out = UncprPtr;
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
        do
kusano fc6ab3
        {
kusano fc6ab3
            long all_read_before = zcpr.total_in;
kusano fc6ab3
            zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
kusano fc6ab3
            zcpr.avail_out = BlockSizeUncompress;
kusano fc6ab3
            ret=inflate(&zcpr,Z_SYNC_FLUSH);
kusano fc6ab3
            lOrigDone += (zcpr.total_in-all_read_before);
kusano fc6ab3
            lOrigToDo -= (zcpr.total_in-all_read_before);
kusano fc6ab3
            step++;
kusano fc6ab3
        } while (ret==Z_OK);
kusano fc6ab3
kusano fc6ab3
        lSizeUncpr=zcpr.total_out;
kusano fc6ab3
        inflateEnd(&zcpr);
kusano fc6ab3
        dwGetTick=GetTickCount()-dwGetTick;
kusano fc6ab3
        dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
kusano fc6ab3
        dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
kusano fc6ab3
        printf("total uncompress size = %u, in %u step\n",lSizeUncpr,step);
kusano fc6ab3
        printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
kusano fc6ab3
        printf("uncpr  time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
kusano fc6ab3
        printf("uncpr  result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    if (lSizeUncpr==lFileSize)
kusano fc6ab3
    {
kusano fc6ab3
        if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
kusano fc6ab3
            printf("compare ok\n");
kusano fc6ab3
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    return 0;
kusano fc6ab3
}