1 |
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <windows.h>
|
---|
5 | #include "zlib.h"
|
---|
6 |
|
---|
7 | int ReadFileMemory(const char* filename,long* plFileSize,void** pFilePtr)
|
---|
8 | {
|
---|
9 | FILE* stream;
|
---|
10 | void* ptr;
|
---|
11 | int retVal=1;
|
---|
12 | stream=fopen(filename, "rb");
|
---|
13 | if (stream==NULL)
|
---|
14 | return 0;
|
---|
15 |
|
---|
16 | fseek(stream,0,SEEK_END);
|
---|
17 |
|
---|
18 | *plFileSize=ftell(stream);
|
---|
19 | fseek(stream,0,SEEK_SET);
|
---|
20 | ptr=malloc((*plFileSize)+1);
|
---|
21 | if (ptr==NULL)
|
---|
22 | retVal=0;
|
---|
23 | else
|
---|
24 | {
|
---|
25 | if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
|
---|
26 | retVal=0;
|
---|
27 | }
|
---|
28 | fclose(stream);
|
---|
29 | *pFilePtr=ptr;
|
---|
30 | return retVal;
|
---|
31 | }
|
---|
32 |
|
---|
33 | int main(int argc, char *argv[])
|
---|
34 | {
|
---|
35 | int BlockSizeCompress=0x8000;
|
---|
36 | int BlockSizeUncompress=0x8000;
|
---|
37 | int cprLevel=Z_DEFAULT_COMPRESSION ;
|
---|
38 | long lFileSize;
|
---|
39 | unsigned char* FilePtr;
|
---|
40 | long lBufferSizeCpr;
|
---|
41 | long lBufferSizeUncpr;
|
---|
42 | long lCompressedSize=0;
|
---|
43 | unsigned char* CprPtr;
|
---|
44 | unsigned char* UncprPtr;
|
---|
45 | long lSizeCpr,lSizeUncpr;
|
---|
46 | DWORD dwGetTick;
|
---|
47 |
|
---|
48 | if (argc<=1)
|
---|
49 | {
|
---|
50 | printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");
|
---|
51 | return 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
|
---|
55 | {
|
---|
56 | printf("error reading %s\n",argv[1]);
|
---|
57 | return 1;
|
---|
58 | }
|
---|
59 | else printf("file %s read, %u bytes\n",argv[1],lFileSize);
|
---|
60 |
|
---|
61 | if (argc>=3)
|
---|
62 | BlockSizeCompress=atol(argv[2]);
|
---|
63 |
|
---|
64 | if (argc>=4)
|
---|
65 | BlockSizeUncompress=atol(argv[3]);
|
---|
66 |
|
---|
67 | if (argc>=5)
|
---|
68 | cprLevel=(int)atol(argv[4]);
|
---|
69 |
|
---|
70 | lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
|
---|
71 | lBufferSizeUncpr = lBufferSizeCpr;
|
---|
72 |
|
---|
73 | CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
|
---|
74 | UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
|
---|
75 |
|
---|
76 | dwGetTick=GetTickCount();
|
---|
77 | {
|
---|
78 | z_stream zcpr;
|
---|
79 | int ret=Z_OK;
|
---|
80 | long lOrigToDo = lFileSize;
|
---|
81 | long lOrigDone = 0;
|
---|
82 | int step=0;
|
---|
83 | memset(&zcpr,0,sizeof(z_stream));
|
---|
84 | deflateInit(&zcpr,cprLevel);
|
---|
85 |
|
---|
86 | zcpr.next_in = FilePtr;
|
---|
87 | zcpr.next_out = CprPtr;
|
---|
88 |
|
---|
89 |
|
---|
90 | do
|
---|
91 | {
|
---|
92 | long all_read_before = zcpr.total_in;
|
---|
93 | zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
|
---|
94 | zcpr.avail_out = BlockSizeCompress;
|
---|
95 | ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
|
---|
96 | lOrigDone += (zcpr.total_in-all_read_before);
|
---|
97 | lOrigToDo -= (zcpr.total_in-all_read_before);
|
---|
98 | step++;
|
---|
99 | } while (ret==Z_OK);
|
---|
100 |
|
---|
101 | lSizeCpr=zcpr.total_out;
|
---|
102 | deflateEnd(&zcpr);
|
---|
103 | dwGetTick=GetTickCount()-dwGetTick;
|
---|
104 | printf("total compress size = %u, in %u step\n",lSizeCpr,step);
|
---|
105 | printf("time = %u msec = %f sec\n\n",dwGetTick,dwGetTick/(double)1000.);
|
---|
106 | }
|
---|
107 |
|
---|
108 | dwGetTick=GetTickCount();
|
---|
109 | {
|
---|
110 | z_stream zcpr;
|
---|
111 | int ret=Z_OK;
|
---|
112 | long lOrigToDo = lSizeCpr;
|
---|
113 | long lOrigDone = 0;
|
---|
114 | int step=0;
|
---|
115 | memset(&zcpr,0,sizeof(z_stream));
|
---|
116 | inflateInit(&zcpr);
|
---|
117 |
|
---|
118 | zcpr.next_in = CprPtr;
|
---|
119 | zcpr.next_out = UncprPtr;
|
---|
120 |
|
---|
121 |
|
---|
122 | do
|
---|
123 | {
|
---|
124 | long all_read_before = zcpr.total_in;
|
---|
125 | zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
|
---|
126 | zcpr.avail_out = BlockSizeUncompress;
|
---|
127 | ret=inflate(&zcpr,Z_SYNC_FLUSH);
|
---|
128 | lOrigDone += (zcpr.total_in-all_read_before);
|
---|
129 | lOrigToDo -= (zcpr.total_in-all_read_before);
|
---|
130 | step++;
|
---|
131 | } while (ret==Z_OK);
|
---|
132 |
|
---|
133 | lSizeUncpr=zcpr.total_out;
|
---|
134 | inflateEnd(&zcpr);
|
---|
135 | dwGetTick=GetTickCount()-dwGetTick;
|
---|
136 | printf("total uncompress size = %u, in %u step\n",lSizeUncpr,step);
|
---|
137 | printf("time = %u msec = %f sec\n\n",dwGetTick,dwGetTick/(double)1000.);
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (lSizeUncpr==lFileSize)
|
---|
141 | {
|
---|
142 | if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
|
---|
143 | printf("compare ok\n");
|
---|
144 |
|
---|
145 | }
|
---|
146 |
|
---|
147 | return 0;
|
---|
148 |
|
---|
149 | }
|
---|