home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / windll / windll.c < prev    next >
C/C++ Source or Header  |  1997-05-22  |  10KB  |  434 lines

  1. /*
  2.  Copyright (C) 1996 Mike White
  3.  Permission is granted to any individual or institution to use, copy, or
  4.  redistribute this software so long as all of the original files are included,
  5.  that it is not sold for profit, and that this copyright notice is retained.
  6.  
  7. */
  8. /* Windows Info-ZIP Unzip DLL module
  9.  *
  10.  * Author: Mike White
  11.  *
  12.  * Original: 1996
  13.  *
  14.  * This module has the entry points for "unzipping" a zip file.
  15.  */
  16.  
  17. /*---------------------------------------------------------------------------
  18.  
  19.   This file is the WINDLL replacement for the generic ``main program source
  20.   file'' unzip.c.
  21.  
  22.   See the general comments in the header part of unzip.c.
  23.  
  24.   Copyrights:  see accompanying file "COPYING" in UnZip source distribution.
  25.                (This software is free but NOT IN THE PUBLIC DOMAIN.  There
  26.                are some restrictions on commercial use.)
  27.  
  28.   ---------------------------------------------------------------------------*/
  29.  
  30. #include <windows.h>
  31. #ifdef __RSXNT__
  32. #  include "win32/rsxntwin.h"
  33. #endif
  34. #define UNZIP_INTERNAL
  35. #include "unzip.h"
  36. #include "crypt.h"
  37. #include "version.h"
  38. #include "windll.h"
  39. #include "structs.h"
  40.  
  41. /* Added type casts to prevent potential "type mismatch" error messages. */
  42. #ifdef REENTRANT
  43. #  undef G
  44. #  undef __G
  45. #  undef __G__
  46. #  define G                   (*(struct Globals *)pG)
  47. #  define __G                 (struct Globals *)pG
  48. #  define __G__               (struct Globals *)pG,
  49. #endif
  50.  
  51. HANDLE hwildZipFN;
  52.  
  53. HANDLE hInst;               /* current instance */
  54.  
  55. LPDCL lpDCL;
  56. HANDLE hDCL;
  57. LPUSERFUNCTIONS lpUserFunctions;
  58.  
  59. /* For displaying status messages and error messages */
  60. int UZ_EXP DllMessagePrint(zvoid *pG, uch *buf, ulg size, int flag);
  61.  
  62. /* For displaying files extracted to the display window */
  63. int DllDisplayPrint(zvoid *pG, uch *buf, ulg size, int flag);
  64. DLLPRNT *lpPrint;
  65.  
  66. /* Dummy sound function for those applications that don't use sound */
  67. void WINAPI DummySound(void);
  68.  
  69. /*  DLL Entry Point */
  70.  
  71. #ifdef __BORLANDC__
  72. #pragma argsused
  73. /* Borland seems to want DllEntryPoint instead of DllMain like MSVC */
  74. #define DllMain DllEntryPoint
  75. #endif
  76. #ifdef WIN32
  77. BOOL WINAPI DllMain( HINSTANCE hInstance,
  78.                      DWORD dwReason,
  79.                      LPVOID plvReserved)
  80. #else
  81. int FAR PASCAL LibMain( HINSTANCE hInstance,
  82.                         WORD wDataSegment,
  83.                         WORD wHeapSize,
  84.                         LPSTR lpszCmdLine )
  85. #endif
  86. {
  87. #ifndef WIN32
  88. /* The startup code for the DLL initializes the local heap(if there is one)
  89.  * with a call to LocalInit which locks the data segment.
  90.  */
  91.  
  92. if ( wHeapSize != 0 )
  93.    {
  94.    UnlockData( 0 );
  95.    }
  96. hInst = hInstance;
  97. return 1;   /* Indicate that the DLL was initialized successfully. */
  98. #else
  99. BOOL rc = TRUE;
  100. switch( dwReason )
  101.    {
  102.    case DLL_PROCESS_ATTACH:
  103.       // DLL is loaded. Do your initialization here.
  104.       // If cannot init, set rc to FALSE.
  105.       hInst = hInstance;
  106.       break;
  107.  
  108.    case DLL_PROCESS_DETACH:
  109.       // DLL is unloaded. Do your cleanup here.
  110.       break;
  111.    default:
  112.       break;
  113.    }
  114. return rc;
  115. #endif
  116. }
  117.  
  118. #ifdef __BORLANDC__
  119. #pragma argsused
  120. #endif
  121. int FAR PASCAL WEP ( int bSystemExit )
  122. {
  123. return 1;
  124. }
  125.  
  126. /* DLL calls */
  127.  
  128. jmp_buf dll_error_return;
  129.  
  130. int WINAPI windll_unzip(int argc, char **FNV, DCL far*C,
  131.    USERFUNCTIONS far *lpUserFunc)
  132. {
  133. int retcode;
  134. CONSTRUCTGLOBALS();
  135.  
  136. if (!Unz_Init((zvoid *)&G, lpUserFunc))
  137.    {
  138.    DESTROYGLOBALS();
  139.    return PK_BADERR;
  140.    }
  141.  
  142. if (C->lpszZipFN == NULL) /* Something has screwed up, we don't have a filename */
  143.    {
  144.    DESTROYGLOBALS();
  145.    return PK_NOZIP;
  146.    }
  147.  
  148. retcode = setjmp(dll_error_return);
  149. if (retcode)
  150.    {
  151.    DESTROYGLOBALS();
  152.    return PK_BADERR;
  153.    }
  154.  
  155.  
  156. lpDCL = C;
  157.  
  158. Unz_SetOpts((zvoid *)&G, C);
  159.  
  160. /* Here is the actual call to "unzip" the files (or whatever else you
  161.  * are doing.)
  162.  */
  163. retcode = Unz_Unzip((zvoid *)&G, argc, FNV);
  164.  
  165. DESTROYGLOBALS();
  166. return retcode;
  167. }
  168.  
  169.  
  170. BOOL WINAPI Unz_Init(pG, lpUserFunc)
  171. zvoid *pG;
  172. USERFUNCTIONS far * lpUserFunc;
  173. {
  174. lpUserFunctions = lpUserFunc;
  175. lpPrint = lpUserFunc->print;
  176. G.message = DllMessagePrint;
  177. G.sound = lpUserFunc->sound;
  178. if (G.sound == NULL)
  179.    G.sound = DummySound;
  180. G.replace = lpUserFunc->replace;
  181.  
  182. if (!lpPrint ||
  183.     !G.sound ||
  184.     !G.replace)
  185.     return FALSE;
  186.  
  187. return TRUE;
  188. }
  189.  
  190. int WINAPI Unz_Unzip(pG, argc, FNV)
  191. zvoid *pG;
  192. int argc;
  193. char **FNV;
  194. {
  195. int retcode;
  196.  
  197. G.filespecs = argc;
  198.  
  199. if (argc > 0) {
  200.     G.pfnames = FNV;
  201.     G.process_all_files = FALSE;
  202. #ifndef CRTL_CP_IS_ISO
  203.     for (; *FNV != NULL; FNV++)
  204.         {
  205.         _ISO_INTERN(*FNV);
  206.         }
  207. #endif
  208.     }
  209. else
  210.     G.process_all_files = TRUE;       /* for speed */
  211.  
  212. /*---------------------------------------------------------------------------
  213.     Okey dokey, we have everything we need to get started.  Let's roll.
  214.   ---------------------------------------------------------------------------*/
  215.  
  216. retcode = process_zipfiles(__G);
  217. FreeDllMem(__G);
  218. return retcode;
  219. }
  220.  
  221.  
  222. int win_fprintf(FILE *file, unsigned int size, char far *buffer)
  223. {
  224. if ((file != stderr) && (file != stdout))
  225.    {
  226.    return write(fileno(file),(char far *)(buffer),size);
  227.    }
  228. return lpPrint(buffer, size);
  229. }
  230.  
  231. /**********************************
  232.  * Function DllMessagePrint()     *
  233.  *                                *
  234.  * Send messages to status window *
  235.  **********************************/
  236. #ifdef __BORLANDC__
  237. #pragma argsused
  238. #endif
  239. int UZ_EXP DllMessagePrint(pG, buf, size, flag)
  240.     zvoid *pG;      /* globals struct:  always passed */
  241.     uch *buf;       /* preformatted string to be printed */
  242.     ulg size;       /* length of string (may include nulls) */
  243.     int flag;       /* flag bits */
  244. {
  245. return lpPrint((char far *)buf, size);
  246. }
  247.  
  248. /********************************
  249.  * Function DllDisplayPrint()   *
  250.  *                              *
  251.  * Send files to display window *
  252.  ********************************/
  253. #ifdef __BORLANDC__
  254. #pragma argsused
  255. #endif
  256. int DllDisplayPrint(pG, buf, size, flag)
  257.     zvoid *pG;      /* globals struct:  always passed */
  258.     uch *buf;       /* preformatted string to be printed */
  259.     ulg size;       /* length of string (may include nulls) */
  260.     int flag;       /* flag bits */
  261. {
  262. return lpPrint((char far *)buf, size);
  263. }
  264.  
  265.  
  266. /**********************************
  267.  * Function UzpPassword()         *
  268.  *                                *
  269.  * Prompt for decryption password *
  270.  **********************************/
  271. #ifdef __BORLANDC__
  272. #pragma argsused
  273. #endif
  274. int UZ_EXP UzpPassword(pG, rcnt, pwbuf, size, zfn, efn)
  275.     zvoid *pG;          /* globals struct: always passed */
  276.     int *rcnt;          /* retry counter */
  277.     char *pwbuf;        /* buffer for password */
  278.     int size;           /* size of password buffer */
  279.     ZCONST char *zfn;   /* name of zip archiv */
  280.     ZCONST char *efn;   /* name of archiv entry being processed */
  281. {
  282. #if CRYPT
  283.     char *m;
  284.  
  285.     if (*rcnt == 0) {
  286.         *rcnt = 2;
  287.         m = "Enter password for: ";
  288.     } else {
  289.         (*rcnt)--;
  290.         m = "Password incorrect--reenter: ";
  291.     }
  292.  
  293.     return (*lpUserFunctions->password)(pwbuf, size, m, efn);
  294. #else /* !CRYPT */
  295.     return IZ_PW_ERROR; /* internal error, function should never get called */
  296. #endif /* ?CRYPT */
  297. } /* end function UzpPassword() */
  298.  
  299.  
  300. int WINAPI unzipVB(int argc, char **FNV, DCL far *C,
  301.    VBUSERFUNCTIONS far *lpUF)
  302. {
  303. int retcode;
  304. HANDLE hUF;
  305. LPUSERFUNCTIONS lpUserFunc;
  306. void * lpSound;
  307. void * lpPassword;
  308. void * lpMessage;
  309. void * lpReplace;
  310. void * lpPrintVB;
  311. CONSTRUCTGLOBALS();
  312.  
  313. hUF = GlobalAlloc( GPTR, (DWORD)sizeof(VBUSERFUNCTIONS));
  314. if (!hUF)
  315.    {
  316.    DESTROYGLOBALS();
  317.    return PK_MEM;
  318.    }
  319. lpUserFunc = (LPUSERFUNCTIONS)GlobalLock(hUF);
  320. if (!lpUserFunc)
  321.    {
  322.    GlobalFree(hUF);
  323.    DESTROYGLOBALS();
  324.    return PK_MEM;
  325.    }
  326. lpPrintVB = GetProcAddress(lpUF->hInstance,
  327.    lpUF->print);
  328. lpUserFunc->print = lpPrintVB;
  329. if (!lpUserFunc->print)
  330.    {
  331.    GlobalUnlock(hUF);
  332.    GlobalFree(hUF);
  333.    DESTROYGLOBALS();
  334.    return PK_BADERR;
  335.    }
  336. lpSound = GetProcAddress(lpUF->hInstance,lpUF->sound);
  337. lpUserFunc->sound = lpSound;
  338. if (!lpUserFunc->sound)
  339.    {
  340.    GlobalUnlock(hUF);
  341.    GlobalFree(hUF);
  342.    DESTROYGLOBALS();
  343.    return PK_BADERR;
  344.    }
  345. #ifdef __GNUC__
  346. (int far WINAPI (*))lpReplace =
  347.         (int far WINAPI (*))GetProcAddress(lpUF->hInstance,lpUF->replace);
  348. #else
  349. (int far *WINAPI)lpReplace =
  350.         (int far *WINAPI)GetProcAddress(lpUF->hInstance,lpUF->replace);
  351. #endif
  352. lpUserFunc->replace = lpReplace;
  353. if (!lpUserFunc->replace)
  354.    {
  355.    GlobalUnlock(hUF);
  356.    GlobalFree(hUF);
  357.    DESTROYGLOBALS();
  358.    return PK_BADERR;
  359.    }
  360. #ifdef __GNUC__
  361. (int WINAPI (*)())lpPassword = (int WINAPI (*)())GetProcAddress(lpUF->hInstance,
  362.    lpUF->password);
  363. #else
  364. (int * WINAPI)lpPassword = (int * WINAPI)GetProcAddress(lpUF->hInstance,
  365.    lpUF->password);
  366. #endif
  367. lpUserFunc->password = lpPassword;
  368. if (!lpUserFunc->password)
  369.    {
  370.    GlobalUnlock(hUF);
  371.    GlobalFree(hUF);
  372.    DESTROYGLOBALS();
  373.    return PK_BADERR;
  374.    }
  375. lpMessage = GetProcAddress(lpUF->hInstance,
  376.    lpUF->SendApplicationMessage);
  377. lpUserFunc->SendApplicationMessage = lpMessage;
  378. if (!lpUserFunc->SendApplicationMessage)
  379.    {
  380.    GlobalUnlock(hUF);
  381.    GlobalFree(hUF);
  382.    DESTROYGLOBALS();
  383.    return PK_BADERR;
  384.    }
  385. lpUserFunc->cchComment = lpUF->cchComment;
  386. lpUserFunc->TotalSizeComp = lpUF->TotalSizeComp;
  387. lpUserFunc->TotalSize = lpUF->TotalSize;
  388. lpUserFunc->CompFactor = lpUF->CompFactor;
  389. lpUserFunc->NumMembers = lpUF->NumMembers;
  390.  
  391. if (!Unz_Init((zvoid *)pG, lpUserFunc))
  392.    {
  393.    GlobalUnlock(hUF);
  394.    GlobalFree(hUF);
  395.    DESTROYGLOBALS();
  396.    return PK_BADERR;
  397.    }
  398.  
  399. if (C->lpszZipFN == NULL)
  400.    {
  401.    GlobalUnlock(hUF);
  402.    GlobalFree(hUF);
  403.    DESTROYGLOBALS();
  404.    return PK_NOZIP;
  405.    }
  406.  
  407. retcode = setjmp(dll_error_return);
  408. if (retcode)
  409.    {
  410.    GlobalUnlock(hUF);
  411.    GlobalFree(hUF);
  412.    DESTROYGLOBALS();
  413.    return PK_BADERR;
  414.    }
  415.  
  416.  
  417. lpDCL = C;
  418.  
  419. Unz_SetOpts((zvoid *)pG, C);
  420.  
  421. retcode = Unz_Unzip((zvoid *)pG, argc, FNV);
  422.  
  423. GlobalUnlock(hUF);
  424. GlobalFree(hUF);
  425. DESTROYGLOBALS();
  426. return retcode;
  427. }
  428.  
  429. /* Dummy sound function for those applications that don't use sound */
  430. void WINAPI DummySound(void)
  431. {
  432. }
  433.  
  434.