home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / api.c next >
C/C++ Source or Header  |  1997-10-06  |  10KB  |  430 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   api.c
  4.  
  5.   This module supplies a Zip dll engine for use directly from C/C++
  6.   programs.
  7.  
  8.   The entry points are:
  9.  
  10.     ZpVer *ZpVersion(void);
  11.     int EXPENTRY ZpInit(ZIPUSERFUNCTIONS far * lpZipUserFunc);
  12.     BOOL EXPENTRY ZpSetOptions(ZPOPT Opts);
  13.     ZPOPT EXPENTRY ZpGetOptions(void);
  14.     int EXPENTRY ZpArchive(ZCL C);
  15.  
  16.   This module is currently only used by the Windows dll, and is not used at
  17.   all by any of the other platforms, although it should be easy enough to
  18.   implement this on most platforms.
  19.  
  20.   ---------------------------------------------------------------------------*/
  21. #ifdef WINDLL
  22. #  include <windows.h>
  23. #include "windll.h"
  24. #endif
  25.  
  26. #ifdef OS2
  27. #  define  INCL_DOSMEMMGR
  28. #  include <os2.h>
  29. #endif
  30.  
  31. #ifdef __BORLANDC__
  32. #include <dir.h>
  33. #endif
  34. #include <direct.h>
  35. #include <ctype.h>
  36. #include "zip.h"
  37. #include "crypt.h"
  38. #include "revision.h"
  39. #ifdef USE_ZLIB
  40. #  include "zlib.h"
  41. #endif
  42.  
  43. #ifndef PATH_MAX
  44. #define PATH_MAX 256
  45. #endif
  46.  
  47. DLLPRNT *lpZipPrint;
  48. DLLPASSWORD *lpZipPassword;
  49. extern DLLCOMMENT *lpComment;
  50.  
  51. int ZipRet;
  52.  
  53. /* Local forward declarations */
  54. extern int  zipmain OF((int, char **));
  55. int AllocMemory(int, char *, char *);
  56.  
  57. ZPOPT Options;
  58. HANDLE hInst;
  59. char **argVee;
  60. int argCee;
  61.  
  62. /*---------------------------------------------------------------------------
  63.     Local functions
  64.   ---------------------------------------------------------------------------*/
  65.  
  66. int AllocMemory(int i, char *cmd, char *str)
  67. {
  68. int j;
  69. if ((argVee[i] = (char *) malloc( sizeof(char) * strlen(cmd)+1 )) == NULL)
  70.    {
  71.    for (j = 0; j < i; j++)
  72.        {
  73.        free (argVee[j]);
  74.        argVee[j] = NULL;
  75.        }
  76.    free(argVee);
  77.    fprintf(stdout, "Unable to allocate memory in zip dll at %s\n", str);
  78.    return ZE_MEM;
  79.    }
  80. strcpy( argVee[i], cmd );
  81. return ZE_OK;
  82. }
  83.  
  84. /*---------------------------------------------------------------------------
  85.     Documented API entry points
  86.   ---------------------------------------------------------------------------*/
  87.  
  88. int EXPENTRY ZpInit(ZIPUSERFUNCTIONS far * lpZipUserFunc)
  89. {
  90. lpZipPassword = lpZipUserFunc->password;
  91. lpZipPrint = lpZipUserFunc->print;
  92. lpComment = lpZipUserFunc->comment;
  93.  
  94. if (!lpZipPrint ||
  95.     !lpComment)
  96.     return FALSE;
  97.  
  98. return TRUE;
  99. }
  100.  
  101. BOOL EXPENTRY ZpSetOptions(ZPOPT Opts)
  102. {
  103. Options = Opts;
  104. return TRUE;
  105. }
  106.  
  107. ZPOPT EXPENTRY ZpGetOptions(void)
  108. {
  109. #if CRYPT
  110. Options.fEncryption = TRUE;
  111. #else
  112. Options.fEncryption = FALSE;
  113. #endif
  114. return Options;
  115. }
  116.  
  117. int EXPENTRY ZpArchive(ZCL C)
  118. /* Add, update, freshen, or delete zip entries in a zip file.  See the
  119.    command help in help() zip.c */
  120. {
  121. int i, k, j, m;
  122. char szOrigDir[PATH_MAX];
  123.  
  124. argCee = 0;
  125. /* malloc additional 26 to allow for additional command line arguments */
  126. if ((argVee = (char **)malloc((C.argc+26)*sizeof(char *))) == NULL)
  127.    {
  128.    fprintf(stdout, "Unable to allocate memory in zip dll\n");
  129.    return ZE_MEM;
  130.    }
  131. if ((argVee[argCee] = (char *) malloc( sizeof(char) * strlen("wiz.exe")+1 )) == NULL)
  132.    {
  133.    free(argVee);
  134.    fprintf(stdout, "Unable to allocate memory in zip dll\n");
  135.    return ZE_MEM;
  136.    }
  137. strcpy( argVee[argCee], "wiz.exe" );
  138. argCee++;
  139.  
  140. /* Set compression level efficacy -0...-9 */
  141. if (AllocMemory(argCee, "-0", "Compression") != ZE_OK)
  142.    return ZE_MEM;
  143. argVee[argCee][1] = Options.fLevel;
  144. argCee++;
  145.  
  146. if (Options.fOffsets)    /* Update offsets for SFX prefix */
  147.    {
  148.    if (AllocMemory(argCee, "-A", "Offsets") != ZE_OK)
  149.       return ZE_MEM;
  150.    argCee++;
  151.    }
  152. if (Options.fDeleteEntries)    /* Delete files from zip file -d */
  153.    {
  154.    if (AllocMemory(argCee, "-d", "Delete") != ZE_OK)
  155.       return ZE_MEM;
  156.    argCee++;
  157.    }
  158. if (Options.fNoDirEntries) /* Do not add directory entries -D */
  159.    {
  160.    if (AllocMemory(argCee, "-D", "No Dir Entries") != ZE_OK)
  161.       return ZE_MEM;
  162.    argCee++;
  163.    }
  164. if (Options.fFreshen) /* Freshen zip file--overwrite only -f */
  165.    {
  166.    if (AllocMemory(argCee, "-f", "Freshen") != ZE_OK)
  167.       return ZE_MEM;
  168.    argCee++;
  169.    }
  170. if (Options.fRepair)  /* Fix archive -F or -FF */
  171.    {
  172.    if (Options.fRepair == 1)
  173.       {
  174.       if (AllocMemory(argCee, "-F", "Repair") != ZE_OK)
  175.          return ZE_MEM;
  176.       }
  177.    else
  178.       {
  179.       if (AllocMemory(argCee, "-FF", "Repair") != ZE_OK)
  180.          return ZE_MEM;
  181.       }
  182.    argCee++;
  183.    }
  184. if (Options.fGrow) /* Allow appending to a zip file -g */
  185.    {
  186.    if (AllocMemory(argCee, "-g", "Appending") != ZE_OK)
  187.       return ZE_MEM;
  188.    argCee++;
  189.    }
  190. if (Options.fJunkDir) /* Junk directory names -j */
  191.    {
  192.    if (AllocMemory(argCee, "-j", "Junk Dir Names") != ZE_OK)
  193.       return ZE_MEM;
  194.    argCee++;
  195.    }
  196. if (Options.fEncrypt) /* encrypt -e */
  197.    {
  198.    if (AllocMemory(argCee, "-e", "Encrypt") != ZE_OK)
  199.       return ZE_MEM;
  200.    argCee++;
  201.    }
  202. if (Options.fJunkSFX) /* Junk sfx prefix */
  203.    {
  204.    if (AllocMemory(argCee, "-J", "Junk SFX") != ZE_OK)
  205.       return ZE_MEM;
  206.    argCee++;
  207.    }
  208.  
  209. if (Options.fForce) /* Make entries using DOS names (k for Katz) -k */
  210.    {
  211.    if (AllocMemory(argCee, "-k", "Force DOS") != ZE_OK)
  212.       return ZE_MEM;
  213.    argCee++;
  214.    }
  215.  
  216. if (Options.fLF_CRLF) /* Translate LF_CRLF -l */
  217.    {
  218.    if (AllocMemory(argCee, "-l", "LF-CRLF") != ZE_OK)
  219.       return ZE_MEM;
  220.    argCee++;
  221.    }
  222. if (Options.fCRLF_LF) /* Translate CR/LF to LF -ll */
  223.    {
  224.    if (AllocMemory(argCee, "-ll", "CRLF-LF") != ZE_OK)
  225.       return ZE_MEM;
  226.    argCee++;
  227.    }
  228. if (Options.fMove) /* Delete files added or updated in zip file -m */
  229.    {
  230.    if (AllocMemory(argCee, "-m", "Move") != ZE_OK)
  231.       return ZE_MEM;
  232.    argCee++;
  233.    }
  234.  
  235. if (Options.fLatestTime) /* Set zip file time to time of latest file in it -o */
  236.    {
  237.    if (AllocMemory(argCee, "-o", "Time") != ZE_OK)
  238.       return ZE_MEM;
  239.    argCee++;
  240.    }
  241.  
  242. if (Options.fComment) /* Add archive comment "-z" */
  243.    {
  244.    if (AllocMemory(argCee, "-z", "Comment") != ZE_OK)
  245.       return ZE_MEM;
  246.    argCee++;
  247.    }
  248.  
  249. if (Options.fQuiet) /* quiet operation -q */
  250.    {
  251.    if (AllocMemory(argCee, "-q", "Quiet") != ZE_OK)
  252.       return ZE_MEM;
  253.    argCee++;
  254.    }
  255. if (Options.fRecurse) /* recurse into subdirectories -R */
  256.    {
  257.    if (AllocMemory(argCee, "-R", "Recurse") != ZE_OK)
  258.       return ZE_MEM;
  259.    argCee++;
  260.    }
  261. if (Options.fSystem)  /* include system and hidden files -S */
  262.    {
  263.    if (AllocMemory(argCee, "-S", "System") != ZE_OK)
  264.       return ZE_MEM;
  265.    argCee++;
  266.    }
  267. if (Options.fExcludeDate)    /* Exclude files earlier than specified date -tt */
  268.    {
  269.    if (Options.Date[0] != '\0')
  270.       {
  271.       if (AllocMemory(argCee, "-tt", "Date") != ZE_OK)
  272.          return ZE_MEM;
  273.       argCee++;
  274.       if (AllocMemory(argCee, Options.Date, "Date") != ZE_OK)
  275.          return ZE_MEM;
  276.       argCee++;
  277.       }
  278.    }
  279.  
  280. if (Options.fIncludeDate)    /* include files earlier than specified date -t */
  281.    {
  282.    if (Options.Date[0] != '\0')
  283.       {
  284.       if (AllocMemory(argCee, "-t", "Date") != ZE_OK)
  285.          return ZE_MEM;
  286.       argCee++;
  287.       if (AllocMemory(argCee, Options.Date, "Date") != ZE_OK)
  288.          return ZE_MEM;
  289.       argCee++;
  290.       }
  291.    }
  292.  
  293. if (Options.fUpdate) /* Update zip file--overwrite only if newer -u */
  294.    {
  295.    if (AllocMemory(argCee, "-u", "Update") != ZE_OK)
  296.       return ZE_MEM;
  297.    argCee++;
  298.    }
  299. if (Options.fVerbose)  /* Mention oddities in zip file structure -v */
  300.    {
  301.    if (AllocMemory(argCee, "-v", "Verbose") != ZE_OK)
  302.       return ZE_MEM;
  303.    argCee++;
  304.    }
  305. if (Options.fVolume)  /* Include volume label -$ */
  306.    {
  307.    if (AllocMemory(argCee, "-$", "Volume") != ZE_OK)
  308.       return ZE_MEM;
  309.    argCee++;
  310.    }
  311. #ifdef WIN32
  312. if (Options.fPrivilege)  /* Use privileges -! */
  313.    {
  314.    if (AllocMemory(argCee, "-!", "Privileges") != ZE_OK)
  315.       return ZE_MEM;
  316.    argCee++;
  317.    }
  318. #endif
  319. if (Options.fExtra)  /* Exclude extra attributes -X */
  320.    {
  321.    if (AllocMemory(argCee, "-X", "Extra") != ZE_OK)
  322.       return ZE_MEM;
  323.    argCee++;
  324.    }
  325.  
  326. if (AllocMemory(argCee, C.lpszZipFN, "Zip file name") != ZE_OK)
  327.    return ZE_MEM;
  328. argCee++;
  329.  
  330. getcwd(szOrigDir, PATH_MAX); /* Save current drive and directory */
  331.  
  332. if (Options.szRootDir[0] != '\0')
  333.    {
  334.    chdir(Options.szRootDir);
  335. #ifdef __BORLANDC__
  336.    setdisk(toupper(Options.szRootDir[0]) - 'A');
  337. #endif
  338.    lstrcat(Options.szRootDir, "\\"); /* append trailing \\ */
  339.    if (C.FNV != NULL)
  340.       {
  341.       for (k = 0; k < C.argc; k++)
  342.          {
  343.          if (AllocMemory(argCee, C.FNV[k], "Making argv") != ZE_OK)
  344.             return ZE_MEM;
  345.          if ((strncmp(Options.szRootDir, C.FNV[k], lstrlen(Options.szRootDir))) == 0)
  346.             {
  347.             m = 0;
  348.             for (j = lstrlen(Options.szRootDir); j < lstrlen(C.FNV[k]); j++)
  349.                argVee[argCee][m++] = C.FNV[k][j];
  350.             argVee[argCee][m] = '\0';
  351.             }
  352.          argCee++;
  353.          }
  354.       }
  355.    }
  356. else
  357.    if (C.FNV != NULL)
  358.       for (k = 0; k < C.argc; k++)
  359.          {
  360.          if (AllocMemory(argCee, C.FNV[k], "Making argv") != ZE_OK)
  361.             return ZE_MEM;
  362.          argCee++;
  363.          }
  364.  
  365. argVee[argCee] = NULL;
  366.  
  367. ZipRet = zipmain(argCee, argVee);
  368.  
  369. chdir(szOrigDir);
  370. #ifdef __BORLANDC__
  371. setdisk(toupper(szOrigDir[0]) - 'A');
  372. #endif
  373.  
  374. /* Free the arguments in the array */
  375. for (i = 0; i < argCee; i++)
  376.    {
  377.       free (argVee[i]);
  378.       argVee[i] = NULL;
  379.    }
  380. /* Then free the array itself */
  381. free(argVee);
  382.  
  383. return ZipRet;
  384. }
  385.  
  386. #if CRYPT
  387. int encr_passwd(int modeflag, char *pwbuf, int size, const char *zfn)
  388. {
  389. return (*lpZipPassword)(pwbuf, size, ((modeflag == ZP_PW_VERIFY) ?
  390.                   "Verify password: " : "Enter password: "),
  391.                   (char *)zfn);
  392. }
  393. #endif /* CRYPT */
  394.  
  395. void EXPENTRY ZpVersion(ZpVer far * p)   /* should be pointer to const struct */
  396. {
  397.     p->structlen = ZPVER_LEN;
  398.  
  399. #ifdef BETA
  400.     p->flag = 1;
  401. #else
  402.     p->flag = 0;
  403. #endif
  404.     lstrcpy(p->betalevel, Z_BETALEVEL);
  405.     lstrcpy(p->date, REVDATE);
  406.  
  407. #ifdef ZLIB_VERSION
  408.     lstrcpy(p->zlib_version, ZLIB_VERSION);
  409.     p->flag |= 2;
  410. #else
  411.     p->zlib_version[0] = '\0';
  412. #endif
  413.  
  414.     p->zip.major = Z_MAJORVER;
  415.     p->zip.minor = Z_MINORVER;
  416.     p->zip.patchlevel = Z_PATCHLEVEL;
  417.  
  418.  
  419.     p->os2dll.major = D2_MAJORVER;
  420.     p->os2dll.minor = D2_MINORVER;
  421.     p->os2dll.patchlevel = D2_PATCHLEVEL;
  422.  
  423.  
  424.     p->windll.major = DW_MAJORVER;
  425.     p->windll.minor = DW_MINORVER;
  426.     p->windll.patchlevel = DW_PATCHLEVEL;
  427. }
  428.  
  429.  
  430.