Rizin
unix-like reverse engineering framework and cli tools
testzlib.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "zlib.h"

Go to the source code of this file.

Functions

void MyDoMinus64 (LARGE_INTEGER *R, LARGE_INTEGER A, LARGE_INTEGER B)
 
void myGetRDTSC32 (LARGE_INTEGER *pbeginTime64)
 
void BeginCountRdtsc (LARGE_INTEGER *pbeginTime64)
 
LARGE_INTEGER GetResRdtsc (LARGE_INTEGER beginTime64, BOOL fComputeTimeQueryPerf)
 
void BeginCountPerfCounter (LARGE_INTEGER *pbeginTime64, BOOL fComputeTimeQueryPerf)
 
DWORD GetMsecSincePerfCounter (LARGE_INTEGER beginTime64, BOOL fComputeTimeQueryPerf)
 
int ReadFileMemory (const char *filename, long *plFileSize, unsigned char **pFilePtr)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ BeginCountPerfCounter()

void BeginCountPerfCounter ( LARGE_INTEGER *  pbeginTime64,
BOOL  fComputeTimeQueryPerf 
)

Definition at line 84 of file testzlib.c.

85 {
86  if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(pbeginTime64)))
87  {
88  pbeginTime64->LowPart = GetTickCount();
89  pbeginTime64->HighPart = 0;
90  }
91 }

Referenced by main().

◆ BeginCountRdtsc()

void BeginCountRdtsc ( LARGE_INTEGER *  pbeginTime64)

Definition at line 71 of file testzlib.c.

72 {
73 }

Referenced by main().

◆ GetMsecSincePerfCounter()

DWORD GetMsecSincePerfCounter ( LARGE_INTEGER  beginTime64,
BOOL  fComputeTimeQueryPerf 
)

Definition at line 93 of file testzlib.c.

94 {
95  LARGE_INTEGER endTime64,ticksPerSecond,ticks;
96  DWORDLONG ticksShifted,tickSecShifted;
97  DWORD dwLog=16+0;
98  DWORD dwRet;
99  if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))
100  dwRet = (GetTickCount() - beginTime64.LowPart)*1;
101  else
102  {
103  MyDoMinus64(&ticks,endTime64,beginTime64);
104  QueryPerformanceFrequency(&ticksPerSecond);
105 
106 
107  {
108  ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);
109  tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);
110 
111  }
112 
113  dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));
114  dwRet *=1;
115  }
116  return dwRet;
117 }
void MyDoMinus64(LARGE_INTEGER *R, LARGE_INTEGER A, LARGE_INTEGER B)
Definition: testzlib.c:8
DWORD

References DWORD, and MyDoMinus64().

Referenced by main().

◆ GetResRdtsc()

LARGE_INTEGER GetResRdtsc ( LARGE_INTEGER  beginTime64,
BOOL  fComputeTimeQueryPerf 
)

Definition at line 75 of file testzlib.c.

76 {
77  LARGE_INTEGER lr;
78  lr.QuadPart=0;
79  return lr;
80 }

References lr.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 145 of file testzlib.c.

146 {
147  int BlockSizeCompress=0x8000;
148  int BlockSizeUncompress=0x8000;
149  int cprLevel=Z_DEFAULT_COMPRESSION ;
150  long lFileSize;
151  unsigned char* FilePtr;
152  long lBufferSizeCpr;
153  long lBufferSizeUncpr;
154  long lCompressedSize=0;
155  unsigned char* CprPtr;
156  unsigned char* UncprPtr;
157  long lSizeCpr,lSizeUncpr;
158  DWORD dwGetTick,dwMsecQP;
159  LARGE_INTEGER li_qp,li_rdtsc,dwResRdtsc;
160 
161  if (argc<=1)
162  {
163  printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");
164  return 0;
165  }
166 
167  if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
168  {
169  printf("error reading %s\n",argv[1]);
170  return 1;
171  }
172  else printf("file %s read, %u bytes\n",argv[1],lFileSize);
173 
174  if (argc>=3)
175  BlockSizeCompress=atol(argv[2]);
176 
177  if (argc>=4)
178  BlockSizeUncompress=atol(argv[3]);
179 
180  if (argc>=5)
181  cprLevel=(int)atol(argv[4]);
182 
183  lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
184  lBufferSizeUncpr = lBufferSizeCpr;
185 
186  CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
187 
188  BeginCountPerfCounter(&li_qp,TRUE);
189  dwGetTick=GetTickCount();
190  BeginCountRdtsc(&li_rdtsc);
191  {
192  z_stream zcpr;
193  int ret=Z_OK;
194  long lOrigToDo = lFileSize;
195  long lOrigDone = 0;
196  int step=0;
197  memset(&zcpr,0,sizeof(z_stream));
198  deflateInit(&zcpr,cprLevel);
199 
200  zcpr.next_in = FilePtr;
201  zcpr.next_out = CprPtr;
202 
203 
204  do
205  {
206  long all_read_before = zcpr.total_in;
207  zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
208  zcpr.avail_out = BlockSizeCompress;
209  ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
210  lOrigDone += (zcpr.total_in-all_read_before);
211  lOrigToDo -= (zcpr.total_in-all_read_before);
212  step++;
213  } while (ret==Z_OK);
214 
215  lSizeCpr=zcpr.total_out;
216  deflateEnd(&zcpr);
217  dwGetTick=GetTickCount()-dwGetTick;
218  dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
219  dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
220  printf("total compress size = %u, in %u step\n",lSizeCpr,step);
221  printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
222  printf("defcpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
223  printf("defcpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
224  }
225 
226  CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);
227  UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
228 
229  BeginCountPerfCounter(&li_qp,TRUE);
230  dwGetTick=GetTickCount();
231  BeginCountRdtsc(&li_rdtsc);
232  {
233  z_stream zcpr;
234  int ret=Z_OK;
235  long lOrigToDo = lSizeCpr;
236  long lOrigDone = 0;
237  int step=0;
238  memset(&zcpr,0,sizeof(z_stream));
239  inflateInit(&zcpr);
240 
241  zcpr.next_in = CprPtr;
242  zcpr.next_out = UncprPtr;
243 
244 
245  do
246  {
247  long all_read_before = zcpr.total_in;
248  zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
249  zcpr.avail_out = BlockSizeUncompress;
250  ret=inflate(&zcpr,Z_SYNC_FLUSH);
251  lOrigDone += (zcpr.total_in-all_read_before);
252  lOrigToDo -= (zcpr.total_in-all_read_before);
253  step++;
254  } while (ret==Z_OK);
255 
256  lSizeUncpr=zcpr.total_out;
257  inflateEnd(&zcpr);
258  dwGetTick=GetTickCount()-dwGetTick;
259  dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
260  dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
261  printf("total uncompress size = %u, in %u step\n",lSizeUncpr,step);
262  printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
263  printf("uncpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
264  printf("uncpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
265  }
266 
267  if (lSizeUncpr==lFileSize)
268  {
269  if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
270  printf("compare ok\n");
271 
272  }
273 
274  return 0;
275 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1119
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:804
static states step(struct re_guts *, sopno, sopno, states, int, states)
Definition: engine.c:888
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:623
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.c:1301
return memset(p, 0, total)
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define TRUE
Definition: mybfd.h:103
#define min(a, b)
Definition: qsort.h:83
static int
Definition: sfsocketcall.h:114
uInt avail_in
Definition: zlib.h:88
uInt avail_out
Definition: zlib.h:92
z_const Bytef * next_in
Definition: zlib.h:87
uLong total_in
Definition: zlib.h:89
uLong total_out
Definition: zlib.h:93
Bytef * next_out
Definition: zlib.h:91
void BeginCountRdtsc(LARGE_INTEGER *pbeginTime64)
Definition: testzlib.c:71
DWORD GetMsecSincePerfCounter(LARGE_INTEGER beginTime64, BOOL fComputeTimeQueryPerf)
Definition: testzlib.c:93
void BeginCountPerfCounter(LARGE_INTEGER *pbeginTime64, BOOL fComputeTimeQueryPerf)
Definition: testzlib.c:84
int ReadFileMemory(const char *filename, long *plFileSize, unsigned char **pFilePtr)
Definition: testzlib.c:119
LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64, BOOL fComputeTimeQueryPerf)
Definition: testzlib.c:75
#define Z_FINISH
Definition: zlib.h:172
#define Z_OK
Definition: zlib.h:177
#define Z_SYNC_FLUSH
Definition: zlib.h:170
#define inflateInit(strm)
Definition: zlib.h:1812
#define deflateInit(strm, level)
Definition: zlib.h:1810
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:193

References argv, z_stream_s::avail_in, z_stream_s::avail_out, BeginCountPerfCounter(), BeginCountRdtsc(), deflate(), deflateEnd(), deflateInit, DWORD, GetMsecSincePerfCounter(), GetResRdtsc(), inflate(), inflateEnd(), inflateInit, int, malloc(), memset(), min, z_stream_s::next_in, z_stream_s::next_out, printf(), ReadFileMemory(), realloc(), step(), z_stream_s::total_in, z_stream_s::total_out, TRUE, Z_DEFAULT_COMPRESSION, Z_FINISH, Z_OK, and Z_SYNC_FLUSH.

◆ MyDoMinus64()

void MyDoMinus64 ( LARGE_INTEGER *  R,
LARGE_INTEGER  A,
LARGE_INTEGER  B 
)

Definition at line 8 of file testzlib.c.

9 {
10  R->HighPart = A.HighPart - B.HighPart;
11  if (A.LowPart >= B.LowPart)
12  R->LowPart = A.LowPart - B.LowPart;
13  else
14  {
15  R->LowPart = A.LowPart - B.LowPart;
16  R->HighPart --;
17  }
18 }
#define A(x)
Definition: arc.h:165
#define R(x, b, m)
Definition: arc.h:168
#define B(x)
Definition: arc.h:166

References A, B, and R.

Referenced by GetMsecSincePerfCounter().

◆ myGetRDTSC32()

void myGetRDTSC32 ( LARGE_INTEGER *  pbeginTime64)

Definition at line 67 of file testzlib.c.

68 {
69 }

◆ ReadFileMemory()

int ReadFileMemory ( const char *  filename,
long plFileSize,
unsigned char **  pFilePtr 
)

Definition at line 119 of file testzlib.c.

120 {
121  FILE* stream;
122  unsigned char* ptr;
123  int retVal=1;
124  stream=fopen(filename, "rb");
125  if (stream==NULL)
126  return 0;
127 
128  fseek(stream,0,SEEK_END);
129 
130  *plFileSize=ftell(stream);
131  fseek(stream,0,SEEK_SET);
132  ptr=malloc((*plFileSize)+1);
133  if (ptr==NULL)
134  retVal=0;
135  else
136  {
137  if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
138  retVal=0;
139  }
140  fclose(stream);
141  *pFilePtr=ptr;
142  return retVal;
143 }
#define NULL
Definition: cris-opc.c:27
const char * filename
Definition: ioapi.h:137
voidpf stream
Definition: ioapi.h:138
string FILE
Definition: benchmark.py:21
#define SEEK_SET
Definition: zip.c:88
#define SEEK_END
Definition: zip.c:84

References benchmark::FILE, malloc(), NULL, SEEK_END, and SEEK_SET.

Referenced by main().