24
24
25
25
#include " zlib.h"
26
26
27
- #define STR2 (s ) #s
28
- #define STR (s ) STR2(s)
29
-
27
+ // Defines controlling size of compressed data
30
28
#define BUFFER_SIZE (256 <<20 )
31
29
#define MAX_ITERATIONS 1 // number of passes to fully fill buffer, i.e. total processed data size will be up to (BUFFER_SIZE * MAX_ITERATIONS)
32
- #define COMPRESS_LEVEL 9
33
30
34
31
#if _WIN32
32
+ #define USE_DLL 1
33
+ #endif
34
+
35
+ #define STR2 (s ) #s
36
+ #define STR (s ) STR2(s)
37
+
38
+ #if USE_DLL
35
39
36
- #include < windows.h>
40
+ #include < windows.h> // for DLL stuff
37
41
38
42
static HMODULE zlibDll = NULL ;
39
43
static bool bWinapiCalls = false ;
40
44
41
- static gzFile gzopen_imp (const char * filename, const char * params)
42
- {
43
- if (zlibDll)
44
- {
45
- typedef gzFile ( *gzopen_f )(const char * filename, const char * params);
46
- typedef gzFile (WINAPI *gzopen_fw)(const char * filename, const char * params);
47
- static gzopen_f gzopen_ptr = NULL ;
48
- if (gzopen_ptr == NULL )
49
- {
50
- gzopen_ptr = (gzopen_f)GetProcAddress (zlibDll, " gzopen" );
51
- // assert(gzopen_ptr);
52
- }
53
- return bWinapiCalls ? ((gzopen_fw)gzopen_ptr)(filename, params) : gzopen_ptr (filename, params);
54
- }
55
- else
56
- {
57
- return gzopen (filename, params);
58
- }
45
+ // Make a wrappers for zlib functions allowing to call statically-linked function, or cdecl or winapi dll function.
46
+ // This macro receives argument list twice - with and without type specifiers.
47
+ #define DECLARE_WRAPPER (type, name, prototype, args )\
48
+ static type name##_imp prototype \
49
+ { \
50
+ if (zlibDll) \
51
+ { \
52
+ typedef type (*name##_f) prototype; \
53
+ typedef type (WINAPI *name##_fw) prototype; \
54
+ static name##_f func_ptr = NULL ; \
55
+ if (func_ptr == NULL ) \
56
+ { \
57
+ func_ptr = (name##_f)GetProcAddress (zlibDll, STR (name)); \
58
+ } \
59
+ return bWinapiCalls ? ((name##_fw)func_ptr) args : func_ptr args; \
60
+ } \
61
+ else \
62
+ { \
63
+ return name args; \
64
+ } \
59
65
}
60
66
61
- static int gzwrite_imp (gzFile file, voidpc buf, unsigned len)
62
- {
63
- if (zlibDll)
64
- {
65
- typedef int ( *gzwrite_f )(gzFile file, voidpc buf, unsigned len);
66
- typedef int (WINAPI *gzwrite_fw)(gzFile file, voidpc buf, unsigned len);
67
- static gzwrite_f gzwrite_ptr = NULL ;
68
- if (gzwrite_ptr == NULL )
69
- {
70
- gzwrite_ptr = (gzwrite_f)GetProcAddress (zlibDll, " gzwrite" );
71
- // assert(gzwrite_ptr);
72
- }
73
- return bWinapiCalls ? ((gzwrite_fw)gzwrite_ptr)(file, buf, len) : gzwrite_ptr (file, buf, len);
74
- }
75
- else
76
- {
77
- return gzwrite (file, buf, len);
78
- }
79
- }
80
-
81
- static int gzread_imp (gzFile file, voidp buf, unsigned len)
82
- {
83
- if (zlibDll)
84
- {
85
- typedef int ( *gzread_f )(gzFile file, voidp buf, unsigned len);
86
- typedef int (WINAPI *gzread_fw)(gzFile file, voidp buf, unsigned len);
87
- static gzread_f gzread_ptr = NULL ;
88
- if (gzread_ptr == NULL )
89
- {
90
- gzread_ptr = (gzread_f)GetProcAddress (zlibDll, " gzread" );
91
- // assert(gzread_ptr);
92
- }
93
- return bWinapiCalls ? ((gzread_fw)gzread_ptr)(file, buf, len) : gzread_ptr (file, buf, len);
94
- }
95
- else
96
- {
97
- return gzread (file, buf, len);
98
- }
99
- }
100
-
101
- static int gzclose_imp (gzFile file)
102
- {
103
- if (zlibDll)
104
- {
105
- typedef int ( *gzclose_f )(gzFile file);
106
- typedef int (WINAPI *gzclose_fw)(gzFile file);
107
- static gzclose_f gzclose_ptr = NULL ;
108
- if (gzclose_ptr == NULL )
109
- {
110
- gzclose_ptr = (gzclose_f)GetProcAddress (zlibDll, " gzclose" );
111
- // assert(gzclose_ptr);
112
- }
113
- return bWinapiCalls ? ((gzclose_fw)gzclose_ptr)(file) : gzclose_ptr (file);
114
- }
115
- else
116
- {
117
- return gzclose (file);
118
- }
119
- }
67
+ DECLARE_WRAPPER (gzFile, gzopen, (const char * filename, const char * params), (filename, params))
68
+ DECLARE_WRAPPER(int , gzwrite, (gzFile file, voidpc buf, unsigned len), (file, buf, len))
69
+ DECLARE_WRAPPER(int , gzread, (gzFile file, voidp buf, unsigned len), (file, buf, len))
70
+ DECLARE_WRAPPER(int , gzclose, (gzFile file), (file))
120
71
121
72
// Hook gzip functions
122
73
#define gzopen gzopen_imp
123
74
#define gzwrite gzwrite_imp
124
75
#define gzread gzread_imp
125
76
#define gzclose gzclose_imp
126
77
127
- #endif // _WIN32
78
+ #endif // USE_DLL
128
79
129
80
std::vector<std::string> fileList;
130
81
std::vector<std::string> fileExclude;
@@ -147,7 +98,7 @@ static bool ScanDirectory(const char *dir, bool recurse = true, int baseDirLen =
147
98
if (baseDirLen < 0 )
148
99
baseDirLen = strlen (dir) + 1 ;
149
100
150
- #if _WIN32
101
+ #if USE_DLL
151
102
sprintf (Path, " %s/*.*" , dir);
152
103
_finddatai64_t found;
153
104
intptr_t hFind = _findfirsti64 (Path, &found);
@@ -240,7 +191,7 @@ int main(int argc, const char **argv)
240
191
" Options:\n "
241
192
" --level=[0-9] set compression level, default 9\n "
242
193
" --exclude=<dir> exclude specified directory from tests\n "
243
- #if _WIN32
194
+ #if USE_DLL
244
195
" --dll=<file> use external WINAPI zlib dll\n "
245
196
#endif
246
197
" --compact use compact output\n "
@@ -257,7 +208,7 @@ int main(int argc, const char **argv)
257
208
bool unpackFile = false ;
258
209
bool eraseCompressedFile = false ;
259
210
260
- #if _WIN32
211
+ #if USE_DLL
261
212
const char * dllName = NULL ;
262
213
#endif
263
214
@@ -289,7 +240,7 @@ int main(int argc, const char **argv)
289
240
{
290
241
eraseCompressedFile = true ;
291
242
}
292
- #if _WIN32
243
+ #if USE_DLL
293
244
else if (!strnicmp (arg, " dll=" , 4 ))
294
245
{
295
246
if (zlibDll != NULL ) goto usage;
@@ -301,7 +252,7 @@ int main(int argc, const char **argv)
301
252
exit (1 );
302
253
}
303
254
}
304
- #endif // _WIN32
255
+ #endif // USE_DLL
305
256
else
306
257
{
307
258
goto usage;
@@ -329,15 +280,15 @@ int main(int argc, const char **argv)
329
280
}
330
281
// printf("%d files\n", fileList.size());
331
282
332
- #if _WIN32
283
+ #if USE_DLL
333
284
if (zlibDll)
334
285
{
335
286
typedef unsigned (*zlibCompileFlags_f)();
336
287
zlibCompileFlags_f zlibCompileFlags_ptr = (zlibCompileFlags_f)GetProcAddress (zlibDll, " zlibCompileFlags" );
337
288
unsigned zlibFlags = zlibCompileFlags_ptr ();
338
289
bWinapiCalls = (zlibFlags & 0x400 ) != 0 ;
339
290
}
340
- #endif // _WIN32
291
+ #endif // USE_DLL
341
292
342
293
clock_t clocks = 0 ;
343
294
@@ -368,7 +319,7 @@ int main(int argc, const char **argv)
368
319
369
320
// print results
370
321
const char * method = STR (VERSION);
371
- #if _WIN32
322
+ #if USE_DLL
372
323
if (zlibDll) method = " DLL" ;
373
324
#endif
374
325
float time = clocks / (float )CLOCKS_PER_SEC;
@@ -410,11 +361,8 @@ int main(int argc, const char **argv)
410
361
printf (" Unpack: %5.2f Mb/s" , totalDataSize / double (1 <<20 ) / time );
411
362
}
412
363
413
- #if _WIN32
414
- if (zlibDll)
415
- {
416
- printf (" (%s)" , dllName);
417
- }
364
+ #if USE_DLL
365
+ if (zlibDll) printf (" (%s)" , dllName);
418
366
#endif
419
367
420
368
printf (" \n " );
0 commit comments