~ubuntu-branches/ubuntu/precise/unzip/precise-proposed

« back to all changes in this revision

Viewing changes to windll/uzexampl.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2004-06-06 17:57:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040606175746-nl7p2dgp3aobyc2c
Tags: upstream-5.51
ImportĀ upstreamĀ versionĀ 5.51

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 1990-2002 Info-ZIP.  All rights reserved.
 
3
 
 
4
  See the accompanying file LICENSE, version 2000-Apr-09 or later
 
5
  (the contents of which are also included in unzip.h) for terms of use.
 
6
  If, for some reason, all these files are missing, the Info-ZIP license
 
7
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
 
8
*/
 
9
/*
 
10
   This is a very simplistic example of how to load and make a call into the
 
11
   dll. This has been compiled and tested for a 32-bit console version, but
 
12
   not under 16-bit windows. However, the #ifdef's have been left in for the
 
13
   16-bit code, simply as an example.
 
14
 
 
15
 */
 
16
 
 
17
#ifndef WIN32   /* this code is currently only tested for 32-bit console */
 
18
#  define WIN32
 
19
#endif
 
20
 
 
21
#if defined(__WIN32__) && !defined(WIN32)
 
22
#  define WIN32
 
23
#endif
 
24
 
 
25
#include <sys/types.h>
 
26
#include <sys/stat.h>
 
27
#include <time.h>
 
28
#include <string.h>
 
29
#include "uzexampl.h"
 
30
#include "../unzvers.h"
 
31
#ifdef WIN32
 
32
#  include <winver.h>
 
33
#else
 
34
#  include <ver.h>
 
35
#endif
 
36
 
 
37
#ifndef _MAX_PATH
 
38
#  define _MAX_PATH 260           /* max total file or directory name path */
 
39
#endif
 
40
 
 
41
#ifdef WIN32
 
42
#define UNZ_DLL_NAME "UNZIP32.DLL\0"
 
43
#else
 
44
#define UNZ_DLL_NAME "UNZIP16.DLL\0"
 
45
#endif
 
46
 
 
47
#define DLL_WARNING "Cannot find %s."\
 
48
            " The Dll must be in the application directory, the path, "\
 
49
            "the Windows directory or the Windows System directory."
 
50
#define DLL_VERSION_WARNING "%s has the wrong version number."\
 
51
            " Insure that you have the correct dll's installed, and that "\
 
52
            "an older dll is not in your path or Windows System directory."
 
53
 
 
54
int hFile;              /* file handle */
 
55
 
 
56
LPUSERFUNCTIONS lpUserFunctions;
 
57
HANDLE hUF = (HANDLE)NULL;
 
58
LPDCL lpDCL = NULL;
 
59
HANDLE hDCL = (HANDLE)NULL;
 
60
HINSTANCE hUnzipDll;
 
61
HANDLE hZCL = (HANDLE)NULL;
 
62
#ifdef WIN32
 
63
DWORD dwPlatformId = 0xFFFFFFFF;
 
64
#endif
 
65
 
 
66
 
 
67
/* Forward References */
 
68
int WINAPI DisplayBuf(LPSTR, unsigned long);
 
69
int WINAPI GetReplaceDlgRetVal(char *);
 
70
int WINAPI password(char *, int, const char *, const char *);
 
71
void WINAPI ReceiveDllMessage(unsigned long, unsigned long, unsigned,
 
72
    unsigned, unsigned, unsigned, unsigned, unsigned,
 
73
    char, LPSTR, LPSTR, unsigned long, char);
 
74
_DLL_UNZIP pWiz_SingleEntryUnzip;
 
75
void FreeUpMemory(void);
 
76
 
 
77
int main(int argc, char **argv)
 
78
{
 
79
int exfc, infc;
 
80
char **exfv, **infv;
 
81
char *x_opt;
 
82
DWORD dwVerInfoSize;
 
83
DWORD dwVerHnd;
 
84
char szFullPath[_MAX_PATH];
 
85
int retcode;
 
86
#ifdef WIN32
 
87
char *ptr;
 
88
#else
 
89
HFILE hfile;
 
90
OFSTRUCT ofs;
 
91
#endif
 
92
HANDLE  hMem;         /* handle to mem alloc'ed */
 
93
 
 
94
if (argc < 2)   /* We must have an archive to unzip */
 
95
   {
 
96
   printf("usage: %s <zipfile> [entry1 [entry2 [...]]] [-x xentry1 [...]]",
 
97
          "example");
 
98
   return 0;
 
99
   }
 
100
 
 
101
hDCL = GlobalAlloc( GPTR, (DWORD)sizeof(DCL));
 
102
if (!hDCL)
 
103
   {
 
104
   return 0;
 
105
   }
 
106
lpDCL = (LPDCL)GlobalLock(hDCL);
 
107
if (!lpDCL)
 
108
   {
 
109
   GlobalFree(hDCL);
 
110
   return 0;
 
111
   }
 
112
 
 
113
hUF = GlobalAlloc( GPTR, (DWORD)sizeof(USERFUNCTIONS));
 
114
if (!hUF)
 
115
   {
 
116
   GlobalUnlock(hDCL);
 
117
   GlobalFree(hDCL);
 
118
   return 0;
 
119
   }
 
120
lpUserFunctions = (LPUSERFUNCTIONS)GlobalLock(hUF);
 
121
 
 
122
if (!lpUserFunctions)
 
123
   {
 
124
   GlobalUnlock(hDCL);
 
125
   GlobalFree(hDCL);
 
126
   GlobalFree(hUF);
 
127
   return 0;
 
128
   }
 
129
 
 
130
lpUserFunctions->password = password;
 
131
lpUserFunctions->print = DisplayBuf;
 
132
lpUserFunctions->sound = NULL;
 
133
lpUserFunctions->replace = GetReplaceDlgRetVal;
 
134
lpUserFunctions->SendApplicationMessage = ReceiveDllMessage;
 
135
 
 
136
/* First we go look for the unzip dll */
 
137
#ifdef WIN32
 
138
if (SearchPath(
 
139
    NULL,               /* address of search path               */
 
140
    UNZ_DLL_NAME,       /* address of filename                  */
 
141
    NULL,               /* address of extension                 */
 
142
    _MAX_PATH,           /* size, in characters, of buffer       */
 
143
    szFullPath,         /* address of buffer for found filename */
 
144
    &ptr                /* address of pointer to file component */
 
145
   ) == 0)
 
146
#else
 
147
hfile = OpenFile(UNZ_DLL_NAME,  &ofs, OF_SEARCH);
 
148
if (hfile == HFILE_ERROR)
 
149
#endif
 
150
   {
 
151
   char str[256];
 
152
   wsprintf (str, DLL_WARNING, UNZ_DLL_NAME);
 
153
   printf("%s\n", str);
 
154
   FreeUpMemory();
 
155
   return 0;
 
156
   }
 
157
#ifndef WIN32
 
158
else
 
159
   lstrcpy(szFullPath, ofs.szPathName);
 
160
_lclose(hfile);
 
161
#endif
 
162
 
 
163
/* Now we'll check the unzip dll version information. Note that this is
 
164
   not the same information as is returned from a call to UzpVersion()
 
165
 */
 
166
dwVerInfoSize =
 
167
    GetFileVersionInfoSize(szFullPath, &dwVerHnd);
 
168
 
 
169
if (dwVerInfoSize)
 
170
   {
 
171
   BOOL  fRet, fRetName;
 
172
   char str[256];
 
173
   LPSTR   lpstrVffInfo; /* Pointer to block to hold info */
 
174
   LPSTR lszVer = NULL;
 
175
   LPSTR lszVerName = NULL;
 
176
   UINT  cchVer = 0;
 
177
 
 
178
   /* Get a block big enough to hold the version information */
 
179
   hMem          = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
 
180
   lpstrVffInfo  = GlobalLock(hMem);
 
181
 
 
182
   /* Get the version information */
 
183
   if (GetFileVersionInfo(szFullPath, 0L, dwVerInfoSize, lpstrVffInfo))
 
184
      {
 
185
      fRet = VerQueryValue(lpstrVffInfo,
 
186
               TEXT("\\StringFileInfo\\040904E4\\FileVersion"),
 
187
              (LPVOID)&lszVer,
 
188
              &cchVer);
 
189
      fRetName = VerQueryValue(lpstrVffInfo,
 
190
               TEXT("\\StringFileInfo\\040904E4\\CompanyName"),
 
191
               (LPVOID)&lszVerName,
 
192
               &cchVer);
 
193
      if (!fRet || !fRetName ||
 
194
         (lstrcmpi(lszVer, UNZ_DLL_VERSION) != 0) ||
 
195
         (lstrcmpi(lszVerName, IZ_COMPANY_NAME) != 0))
 
196
         {
 
197
         wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
 
198
         printf("%s\n", str);
 
199
         FreeUpMemory();
 
200
         GlobalUnlock(hMem);
 
201
         GlobalFree(hMem);
 
202
         return 0;
 
203
         }
 
204
      }
 
205
      /* free memory */
 
206
   GlobalUnlock(hMem);
 
207
   GlobalFree(hMem);
 
208
   }
 
209
else
 
210
   {
 
211
   char str[256];
 
212
   wsprintf (str, DLL_VERSION_WARNING, UNZ_DLL_NAME);
 
213
   printf("%s\n", str);
 
214
   FreeUpMemory();
 
215
   return 0;
 
216
   }
 
217
/* Okay, now we know that the dll exists, and has the proper version
 
218
 * information in it. We can go ahead and load it.
 
219
 */
 
220
hUnzipDll = LoadLibrary(UNZ_DLL_NAME);
 
221
#ifndef WIN32
 
222
if (hUnzipDll > HINSTANCE_ERROR)
 
223
#else
 
224
if (hUnzipDll != NULL)
 
225
#endif
 
226
   {
 
227
   pWiz_SingleEntryUnzip =
 
228
     (_DLL_UNZIP)GetProcAddress(hUnzipDll, "Wiz_SingleEntryUnzip");
 
229
   }
 
230
else
 
231
   {
 
232
   char str[256];
 
233
   wsprintf (str, "Could not load %s", UNZ_DLL_NAME);
 
234
   printf("%s\n", str);
 
235
   FreeUpMemory();
 
236
   return 0;
 
237
   }
 
238
 
 
239
/*
 
240
   Here is where the actual extraction process begins. First we set up the
 
241
   flags to be passed into the dll.
 
242
 */
 
243
lpDCL->ncflag = 0; /* Write to stdout if true */
 
244
lpDCL->fQuiet = 0; /* We want all messages.
 
245
                      1 = fewer messages,
 
246
                      2 = no messages */
 
247
lpDCL->ntflag = 0; /* test zip file if true */
 
248
lpDCL->nvflag = 0; /* give a verbose listing if true */
 
249
lpDCL->nzflag = 0; /* display a zip file comment if true */
 
250
lpDCL->ndflag = 1; /* Recreate directories != 0, skip "../" if < 2 */
 
251
lpDCL->naflag = 0; /* Do not convert CR to CRLF */
 
252
lpDCL->nfflag = 0; /* Do not freshen existing files only */
 
253
lpDCL->noflag = 1; /* Over-write all files if true */
 
254
lpDCL->ExtractOnlyNewer = 0; /* Do not extract only newer */
 
255
lpDCL->PromptToOverwrite = 0; /* "Overwrite all" selected -> no query mode */
 
256
lpDCL->lpszZipFN = argv[1]; /* The archive name */
 
257
lpDCL->lpszExtractDir = NULL; /* The directory to extract to. This is set
 
258
                                 to NULL if you are extracting to the
 
259
                                 current directory.
 
260
                               */
 
261
/*
 
262
   As this is a quite short example, intended primarily to show how to
 
263
   load and call in to the dll, the command-line parameters are only
 
264
   parsed in a very simplistic way:
 
265
   We assume that the command-line parameters after the zip archive
 
266
   make up a list of file patterns:
 
267
   " [file_i1] [file_i2] ... [file_iN] [-x file_x1 [file_x2] ...]".
 
268
   We scan for an argument "-x"; all arguments in front are
 
269
   "include file patterns", all arguments after are "exclude file patterns".
 
270
   If no more arguments are given, we extract ALL files.
 
271
 
 
272
   In summary, the example program should be run like:
 
273
   example <archive.name> [files to include] [-x files to exclude]
 
274
   ("<...> denotes mandatory arguments, "[...]" optional arguments)
 
275
 */
 
276
x_opt = NULL;
 
277
if (argc > 2) {
 
278
  infv = &argv[2];
 
279
  for (infc = 0; infc < argc-2; infc++)
 
280
    if (!strcmp("-x", infv[infc])) {
 
281
        x_opt = infv[infc];
 
282
        infv[infc] = NULL;
 
283
        break;
 
284
    }
 
285
  exfc = argc - infc - 3;
 
286
  if (exfc > 0)
 
287
    exfv = &argv[infc+3];
 
288
  else {
 
289
    exfc = 0;
 
290
    exfv = NULL;
 
291
  }
 
292
} else {
 
293
  infc = exfc = 0;
 
294
  infv = exfv = NULL;
 
295
}
 
296
retcode = (*pWiz_SingleEntryUnzip)(infc, infv, exfc, exfv, lpDCL,
 
297
                                   lpUserFunctions);
 
298
if (x_opt) {
 
299
  infv[infc] = x_opt;
 
300
  x_opt = NULL;
 
301
}
 
302
 
 
303
if (retcode != 0)
 
304
   printf("Error unzipping...\n");
 
305
 
 
306
FreeUpMemory();
 
307
FreeLibrary(hUnzipDll);
 
308
return 1;
 
309
}
 
310
 
 
311
int WINAPI GetReplaceDlgRetVal(char *filename)
 
312
{
 
313
/* This is where you will decide if you want to replace, rename etc existing
 
314
   files.
 
315
 */
 
316
return 1;
 
317
}
 
318
 
 
319
void FreeUpMemory(void)
 
320
{
 
321
if (hDCL)
 
322
   {
 
323
   GlobalUnlock(hDCL);
 
324
   GlobalFree(hDCL);
 
325
   }
 
326
if (hUF)
 
327
   {
 
328
   GlobalUnlock(hUF);
 
329
   GlobalFree(hUF);
 
330
   }
 
331
}
 
332
 
 
333
/* This is a very stripped down version of what is done in Wiz. Essentially
 
334
   what this function is for is to do a listing of an archive contents. It
 
335
   is actually never called in this example, but a dummy procedure had to
 
336
   be put in, so this was used.
 
337
 */
 
338
void WINAPI ReceiveDllMessage(unsigned long ucsize, unsigned long csiz,
 
339
    unsigned cfactor,
 
340
    unsigned mo, unsigned dy, unsigned yr, unsigned hh, unsigned mm,
 
341
    char c, LPSTR filename, LPSTR methbuf, unsigned long crc, char fCrypt)
 
342
{
 
343
char psLBEntry[_MAX_PATH];
 
344
char LongHdrStats[] =
 
345
          "%7lu  %7lu %4s  %02u-%02u-%02u  %02u:%02u  %c%s";
 
346
char CompFactorStr[] = "%c%d%%";
 
347
char CompFactor100[] = "100%%";
 
348
char szCompFactor[10];
 
349
char sgn;
 
350
 
 
351
if (csiz > ucsize)
 
352
   sgn = '-';
 
353
else
 
354
   sgn = ' ';
 
355
if (cfactor == 100)
 
356
   lstrcpy(szCompFactor, CompFactor100);
 
357
else
 
358
   sprintf(szCompFactor, CompFactorStr, sgn, cfactor);
 
359
   wsprintf(psLBEntry, LongHdrStats,
 
360
      ucsize, csiz, szCompFactor, mo, dy, yr, hh, mm, c, filename);
 
361
 
 
362
printf("%s\n", psLBEntry);
 
363
}
 
364
 
 
365
/* Password entry routine - see password.c in the wiz directory for how
 
366
   this is actually implemented in WiZ. If you have an encrypted file,
 
367
   this will probably give you great pain.
 
368
 */
 
369
int WINAPI password(char *p, int n, const char *m, const char *name)
 
370
{
 
371
return 1;
 
372
}
 
373
 
 
374
/* Dummy "print" routine that simply outputs what is sent from the dll */
 
375
int WINAPI DisplayBuf(LPSTR buf, unsigned long size)
 
376
{
 
377
printf("%s", (char *)buf);
 
378
return (unsigned int) size;
 
379
}