home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / windll / windll.c < prev    next >
C/C++ Source or Header  |  1997-10-29  |  13KB  |  532 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. #ifdef __BORLANDC__
  35. #include <dir.h>
  36. #endif
  37. #define UNZIP_INTERNAL
  38. #include "unzip.h"
  39. #include "crypt.h"
  40. #include "version.h"
  41. #include "windll.h"
  42. #include "structs.h"
  43. #include "consts.h"
  44.  
  45. /* Added type casts to prevent potential "type mismatch" error messages. */
  46. #ifdef REENTRANT
  47. #  undef G
  48. #  undef __G
  49. #  undef __G__
  50. #  define G                   (*(struct Globals *)pG)
  51. #  define __G                 (struct Globals *)pG
  52. #  define __G__               (struct Globals *)pG,
  53. #endif
  54.  
  55. HANDLE hwildZipFN;
  56. HANDLE hInst;               /* current instance */
  57. LPDCL lpDCL;
  58. HANDLE hDCL;
  59. LPUSERFUNCTIONS lpUserFunctions;
  60. int fNoPrinting = 0;
  61. extern jmp_buf dll_error_return;
  62.  
  63. /* For displaying status messages and error messages */
  64. int UZ_EXP DllMessagePrint(zvoid *pG, uch *buf, ulg size, int flag);
  65.  
  66. /* For displaying files extracted to the display window */
  67. int DllDisplayPrint(zvoid *pG, uch *buf, ulg size, int flag);
  68. DLLPRNT *lpPrint;
  69.  
  70. /* Dummy sound function for those applications that don't use sound */
  71. void WINAPI DummySound(void);
  72.  
  73. #ifndef UNZIPLIB
  74. /*  DLL Entry Point */
  75.  
  76. #ifdef __BORLANDC__
  77. #pragma argsused
  78. /* Borland seems to want DllEntryPoint instead of DllMain like MSVC */
  79. #define DllMain DllEntryPoint
  80. #endif
  81. #ifdef WIN32
  82. BOOL WINAPI DllMain( HINSTANCE hInstance,
  83.                      DWORD dwReason,
  84.                      LPVOID plvReserved)
  85. #else
  86. int FAR PASCAL LibMain( HINSTANCE hInstance,
  87.                         WORD wDataSegment,
  88.                         WORD wHeapSize,
  89.                         LPSTR lpszCmdLine )
  90. #endif
  91. {
  92. #ifndef WIN32
  93. /* The startup code for the DLL initializes the local heap(if there is one)
  94.  * with a call to LocalInit which locks the data segment.
  95.  */
  96.  
  97. if ( wHeapSize != 0 )
  98.    {
  99.    UnlockData( 0 );
  100.    }
  101. hInst = hInstance;
  102. return 1;   /* Indicate that the DLL was initialized successfully. */
  103. #else
  104. BOOL rc = TRUE;
  105. switch( dwReason )
  106.    {
  107.    case DLL_PROCESS_ATTACH:
  108.       // DLL is loaded. Do your initialization here.
  109.       // If cannot init, set rc to FALSE.
  110.       hInst = hInstance;
  111.       break;
  112.  
  113.    case DLL_PROCESS_DETACH:
  114.       // DLL is unloaded. Do your cleanup here.
  115.       break;
  116.    default:
  117.       break;
  118.    }
  119. return rc;
  120. #endif
  121. }
  122.  
  123. #ifdef __BORLANDC__
  124. #pragma argsused
  125. #endif
  126. int FAR PASCAL WEP ( int bSystemExit )
  127. {
  128. return 1;
  129. }
  130. #endif /* !UNZIPLIB
  131.  
  132. /* DLL calls */
  133.  
  134. /*
  135.     ExtractOnlyNewer  = true if you are to extract only newer
  136.     SpaceToUnderscore = true if convert space to underscore
  137.     PromptToOverwrite = true if prompt to overwrite is wanted
  138.     fQuiet    = quiet flag. 1 = few messages, 2 = no messages, 0 = all messages
  139.     ncflag    = write to stdout if true
  140.     ntflag    = test zip file
  141.     nvflag    = verbose listing
  142.     nUflag    = "update" (extract only newer/new files)
  143.     nzflag    = display zip file comment
  144.     ndflag    = all args are files/dir to be extracted
  145.     noflag    = overwrite all files
  146.     naflag    = do end-of-line translation
  147.     nZIflag   = get Zip Info if TRUE
  148.     C_flag    = be case insensitive if TRUE
  149.     fPrivilege = restore ACL's if 1, use privileges if 2
  150.     lpszZipFN = zip file name
  151.     lpszExtractDir = directory to extract to; NULL means: current directory
  152. */
  153.  
  154. BOOL WINAPI Unz_SetOpts(pG, C)
  155. zvoid * pG;
  156. LPDCL C;
  157. {
  158.     G.qflag=C->fQuiet;  /* Quiet flag */
  159.     G.pfnames = &fnames[0];       /* assign default file name vector */
  160.     G.pxnames = &fnames[1];
  161.  
  162.     G.jflag = !C->ndflag;
  163.     G.cflag = C->ncflag;
  164.     G.overwrite_all = C->noflag;
  165.     G.tflag = C->ntflag ;
  166.     G.vflag = C->nvflag;
  167.     G.zflag = C->nzflag;
  168.     G.uflag = C->nUflag;
  169.     G.aflag = C->naflag;
  170.     G.C_flag = C->C_flag;
  171.     G.uflag = C->ExtractOnlyNewer;
  172. #ifdef WIN32
  173.     G.X_flag = C->fPrivilege;
  174. #endif
  175.     G.overwrite_none = !G.overwrite_all;
  176.     G.sflag = C->SpaceToUnderscore; /* Translate spaces to underscores? */
  177.     if (C->nZIflag)
  178.       {
  179.       G.zipinfo_mode = TRUE;
  180.       G.hflag = TRUE;
  181.       G.lflag = 10;
  182.       G.qflag = 2;
  183.       }
  184.     else
  185.       {
  186.       G.zipinfo_mode = FALSE;
  187.       }
  188.  
  189.     G.extract_flag = (!G.zipinfo_mode &&
  190.                       !G.cflag && !G.tflag && !G.vflag && !G.zflag
  191. #ifdef TIMESTAMP
  192.                       && !G.T_flag
  193. #endif
  194.                      );
  195.  
  196.     if (C->lpszExtractDir != NULL)
  197.        {
  198.        G.dflag = TRUE;
  199.        if (G.extract_flag)
  200.           {
  201. #ifndef CRTL_CP_IS_ISO
  202.           char *pExDirRoot = (char *)malloc(strlen(C->lpszExtractDir)+1);
  203.  
  204.           if (pExDirRoot == NULL)
  205.               return FALSE;
  206.           ISO_TO_INTERN(C->lpszExtractDir, pExDirRoot);
  207. #else
  208. #  define pExDirRoot C->lpszExtractDir
  209. #endif
  210.           G.create_dirs = !G.fflag;
  211.           if (checkdir(__G__ pExDirRoot, ROOT) > 2)
  212.              {
  213.              return FALSE;
  214.              }
  215.           }
  216.        }
  217.     else
  218.        {
  219.        G.dflag = FALSE;
  220.        }
  221.  
  222. /* G.wildzipfn needs to be initialized so that do_wild does not wind
  223.    up clearing out the zip file name when it returns in process.c
  224. */
  225.     if ((hwildZipFN = GlobalAlloc(GMEM_MOVEABLE, FILNAMSIZ))== (HGLOBAL)NULL)
  226.         return FALSE;
  227.  
  228.     G.wildzipfn = GlobalLock(hwildZipFN);
  229.     lstrcpy(G.wildzipfn, C->lpszZipFN);
  230.     _ISO_INTERN(G.wildzipfn);
  231.  
  232.     return TRUE;    /* set up was OK */
  233. }
  234.  
  235. void FreeDllMem(__GPRO)
  236. {
  237.     if (G.wildzipfn) {
  238.         GlobalUnlock(hwildZipFN);
  239.         G.wildzipfn = NULL;
  240.     }
  241.     if (hwildZipFN)
  242.         hwildZipFN = GlobalFree(hwildZipFN);
  243.  
  244.     G.zipinfo_mode = FALSE;
  245. }
  246.  
  247. int WINAPI windll_unzip(int ifnc, char **ifnv, int xfnc, char **xfnv,
  248.    DCL far*C, USERFUNCTIONS far *lpUserFunc)
  249. {
  250. int retcode;
  251. CONSTRUCTGLOBALS();
  252.  
  253. if (!Unz_Init((zvoid *)&G, lpUserFunc))
  254.    {
  255.    DESTROYGLOBALS();
  256.    return PK_BADERR;
  257.    }
  258.  
  259. if (C->lpszZipFN == NULL) /* Something has screwed up, we don't have a filename */
  260.    {
  261.    DESTROYGLOBALS();
  262.    return PK_NOZIP;
  263.    }
  264.  
  265. lpDCL = C;
  266.  
  267. Unz_SetOpts((zvoid *)&G, C);
  268.  
  269. /* Here is the actual call to "unzip" the files (or whatever else you
  270.  * are doing.)
  271.  */
  272. retcode = Unz_Unzip((zvoid *)&G, ifnc, ifnv, xfnc, xfnv);
  273.  
  274. DESTROYGLOBALS();
  275. return retcode;
  276. }
  277.  
  278.  
  279. BOOL WINAPI Unz_Init(pG, lpUserFunc)
  280. zvoid *pG;
  281. USERFUNCTIONS far * lpUserFunc;
  282. {
  283. lpUserFunctions = lpUserFunc;
  284. lpPrint = lpUserFunc->print;
  285. G.message = DllMessagePrint;
  286. G.sound = lpUserFunc->sound;
  287. if (G.sound == NULL)
  288.    G.sound = DummySound;
  289. G.replace = lpUserFunc->replace;
  290.  
  291. if (!lpPrint ||
  292.     !G.sound ||
  293.     !G.replace)
  294.     return FALSE;
  295.  
  296. return TRUE;
  297. }
  298.  
  299. int WINAPI Unz_Unzip(pG, ifnc, ifnv, xfnc, xfnv)
  300. zvoid *pG;
  301. int ifnc;
  302. char **ifnv;
  303. int xfnc;
  304. char **xfnv;
  305. {
  306. int retcode;
  307. #ifndef CRTL_CP_IS_ISO
  308. char **intern_ifv, **intern_xfv;
  309. #endif
  310.  
  311. G.process_all_files = (ifnc == 0 && xfnc == 0);         /* for speed */
  312. G.filespecs = ifnc;
  313. G.xfilespecs = xfnc;
  314.  
  315. if (ifnc > 0) {
  316. #ifdef CRTL_CP_IS_ISO
  317.     G.pfnames = ifnv;
  318. #else /* !CRTL_CP_IS_ISO */
  319.       {
  320.         int f_cnt;
  321.         unsigned bufsize = 0;
  322.  
  323.         intern_ifv = (char **)malloc((ifnc+1)*sizeof(char **));
  324.         if (intern_ifv == (char **)NULL)
  325.             {
  326.             FreeDllMem(__G);
  327.             return PK_BADERR;
  328.             }
  329.  
  330.         for (f_cnt = ifnc; --f_cnt >= 0;)
  331.             bufsize += strlen(ifnv[f_cnt]) + 1;
  332.         intern_ifv[0] = (char *)malloc(bufsize);
  333.         if (intern_ifv[0] == (char *)NULL)
  334.             {
  335.             free(intern_ifv);
  336.             FreeDllMem(__G);
  337.             return PK_BADERR;
  338.             }
  339.  
  340.         for (f_cnt = 0; f_cnt < ifnc; f_cnt++)
  341.             {
  342.             ISO_TO_INTERN(ifnv[f_cnt], intern_ifv[f_cnt]);
  343.             }
  344.         intern_ifv[ifnc] = (char *)NULL;
  345.         G.pfnames = intern_ifv;
  346.       }
  347. #endif /* ?CRTL_CP_IS_ISO */
  348.     }
  349.  
  350. if (xfnc > 0) {
  351. #ifdef CRTL_CP_IS_ISO
  352.     G.pxnames = xfnv;
  353. #else /* !CRTL_CP_IS_ISO */
  354.       {
  355.         int f_cnt;
  356.         unsigned bufsize = 0;
  357.  
  358.         intern_xfv = (char **)malloc((xfnc+1)*sizeof(char **));
  359.         if (intern_xfv == (char **)NULL)
  360.             {
  361.             if (ifnc > 0)
  362.                 {
  363.                 free(intern_ifv[0]);
  364.                 free(intern_ifv);
  365.                 }
  366.             FreeDllMem(__G);
  367.             return PK_BADERR;
  368.             }
  369.  
  370.         for (f_cnt = xfnc; --f_cnt >= 0;)
  371.             bufsize += strlen(xfnv[f_cnt]) + 1;
  372.         intern_xfv[0] = (char *)malloc(bufsize);
  373.         if (intern_xfv[0] == (char *)NULL)
  374.             {
  375.             free(intern_xfv);
  376.             if (ifnc > 0)
  377.                 {
  378.                 free(intern_ifv[0]);
  379.                 free(intern_ifv);
  380.                 }
  381.             FreeDllMem(__G);
  382.             return PK_BADERR;
  383.             }
  384.  
  385.         for (f_cnt = 0; f_cnt < xfnc; f_cnt++)
  386.             {
  387.             ISO_TO_INTERN(xfnv[f_cnt], intern_xfv[f_cnt]);
  388.             }
  389.         intern_xfv[xfnc] = (char *)NULL;
  390.         G.pxnames = intern_xfv;
  391.       }
  392. #endif /* ?CRTL_CP_IS_ISO */
  393.     }
  394.  
  395. /*---------------------------------------------------------------------------
  396.     Okey dokey, we have everything we need to get started.  Let's roll.
  397.   ---------------------------------------------------------------------------*/
  398.  
  399. retcode = setjmp(dll_error_return);
  400. if (retcode)
  401.    {
  402. #ifndef CRTL_CP_IS_ISO
  403.    if (xfnc > 0)
  404.       {
  405.       free(intern_xfv[0]);
  406.       free(intern_xfv);
  407.       }
  408.    if (ifnc > 0)
  409.       {
  410.       free(intern_ifv[0]);
  411.       free(intern_ifv);
  412.       }
  413. #endif
  414.    FreeDllMem(__G);
  415.    return PK_BADERR;
  416.    }
  417.  
  418. retcode = process_zipfiles(__G);
  419. #ifndef CRTL_CP_IS_ISO
  420. if (xfnc > 0)
  421.    {
  422.    free(intern_xfv[0]);
  423.    free(intern_xfv);
  424.    }
  425. if (ifnc > 0)
  426.    {
  427.    free(intern_ifv[0]);
  428.    free(intern_ifv);
  429.    }
  430. #endif
  431. FreeDllMem(__G);
  432. return retcode;
  433. }
  434.  
  435.  
  436. int win_fprintf(FILE *file, unsigned int size, char far *buffer)
  437. {
  438. if ((file != stderr) && (file != stdout))
  439.    {
  440.    return write(fileno(file),(char far *)(buffer),size);
  441.    }
  442. if (lpPrint != NULL)
  443.    return lpPrint((LPSTR)buffer, size);
  444. else
  445.    return (int)size;
  446. }
  447.  
  448. /**********************************
  449.  * Function DllMessagePrint()     *
  450.  *                                *
  451.  * Send messages to status window *
  452.  **********************************/
  453. #ifdef __BORLANDC__
  454. #pragma argsused
  455. #endif
  456. int UZ_EXP DllMessagePrint(pG, buf, size, flag)
  457.     zvoid *pG;      /* globals struct:  always passed */
  458.     uch *buf;       /* preformatted string to be printed */
  459.     ulg size;       /* length of string (may include nulls) */
  460.     int flag;       /* flag bits */
  461. {
  462. if (fNoPrinting)
  463.    return (int)size;
  464. if (lpPrint != NULL)
  465.    return lpPrint((LPSTR)buf, size);
  466. else
  467.    return (int)size;
  468. }
  469.  
  470. /********************************
  471.  * Function DllDisplayPrint()   *
  472.  *                              *
  473.  * Send files to display window *
  474.  ********************************/
  475. #ifdef __BORLANDC__
  476. #pragma argsused
  477. #endif
  478. int DllDisplayPrint(pG, buf, size, flag)
  479.     zvoid *pG;      /* globals struct:  always passed */
  480.     uch *buf;       /* preformatted string to be printed */
  481.     ulg size;       /* length of string (may include nulls) */
  482.     int flag;       /* flag bits */
  483. {
  484. return lpPrint((LPSTR)buf, size);
  485. }
  486.  
  487.  
  488. /**********************************
  489.  * Function UzpPassword()         *
  490.  *                                *
  491.  * Prompt for decryption password *
  492.  **********************************/
  493. #ifdef __BORLANDC__
  494. #pragma argsused
  495. #endif
  496. int UZ_EXP UzpPassword(pG, rcnt, pwbuf, size, zfn, efn)
  497.     zvoid *pG;          /* globals struct: always passed */
  498.     int *rcnt;          /* retry counter */
  499.     char *pwbuf;        /* buffer for password */
  500.     int size;           /* size of password buffer */
  501.     ZCONST char *zfn;   /* name of zip archiv */
  502.     ZCONST char *efn;   /* name of archiv entry being processed */
  503. {
  504. #if CRYPT
  505.     LPSTR m;
  506.  
  507.     if (*rcnt == 0) {
  508.         *rcnt = 2;
  509.         m = "Enter password for: ";
  510.     } else {
  511.         (*rcnt)--;
  512.         m = "Password incorrect--reenter: ";
  513.     }
  514.  
  515.     return (*lpUserFunctions->password)((LPSTR)pwbuf, size, m, (LPSTR)efn);
  516. #else /* !CRYPT */
  517.     return IZ_PW_ERROR; /* internal error, function should never get called */
  518. #endif /* ?CRYPT */
  519. } /* end function UzpPassword() */
  520.  
  521. /* Turn off all messages to the calling application */
  522. void WINAPI UzpNoPrinting(int f)
  523. {
  524. fNoPrinting = f;
  525. }
  526.  
  527. /* Dummy sound function for those applications that don't use sound */
  528. void WINAPI DummySound(void)
  529. {
  530. }
  531.  
  532.