home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / globals.h < prev    next >
C/C++ Source or Header  |  1997-10-05  |  16KB  |  412 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   globals.h
  4.  
  5.   There is usually no need to include this file since unzip.h includes it.
  6.  
  7.   This header file is used by all of the UnZip source files.  It contains
  8.   a struct definition that is used to "house" all of the global variables.
  9.   This is done to allow for multithreaded environments (OS/2, NT, Win95,
  10.   Unix) to call UnZip through an API without a semaphore.  REENTRANT should
  11.   be defined for all platforms that require this.
  12.  
  13.   GLOBAL CONSTRUCTOR AND DESTRUCTOR (API WRITERS READ THIS!!!)
  14.   ------------------------------------------------------------
  15.  
  16.   No, it's not C++, but it's as close as we can get with K&R.
  17.  
  18.   The main() of each process that uses these globals must include the
  19.   CONSTRUCTGLOBALS; statement.  This will malloc enough memory for the
  20.   structure and initialize any variables that require it.  This must
  21.   also be done by any API function that jumps into the middle of the
  22.   code.
  23.  
  24.   The DESTROYGLOBALS; statement should be inserted before EVERY "EXIT(n)".
  25.   Naturally, it also needs to be put before any API returns as well.
  26.   In fact, it's much more important in API functions since the process
  27.   will NOT end, and therefore the memory WON'T automatically be freed
  28.   by the operating system.
  29.  
  30.   USING VARIABLES FROM THE STRUCTURE
  31.   ----------------------------------
  32.  
  33.   All global variables must now be prefixed with `G.' which is either a
  34.   global struct (in which case it should be the only global variable) or
  35.   a macro for the value of a local pointer variable that is passed from
  36.   function to function.  Yes, this is a pain.  But it's the only way to
  37.   allow full reentrancy.
  38.  
  39.   ADDING VARIABLES TO THE STRUCTURE
  40.   ---------------------------------
  41.  
  42.   If you make the inclusion of any variables conditional, be sure to only
  43.   check macros that are GUARANTEED to be included in every module.  For
  44.   instance, newzip, P_flag and pwdarg are needed only if CRYPT is TRUE,
  45.   but this is defined after unzip.h has been read.  If you are not careful,
  46.   some modules will expect your variable to be part of this struct while
  47.   others won't.  This will cause BIG problems. (Inexplicable crashes at
  48.   strange times, car fires, etc.)  When in doubt, always include it!
  49.  
  50.   Note also that UnZipSFX needs a few variables that UnZip doesn't.  However,
  51.   it also includes some object files from UnZip.  If we were to conditionally
  52.   include the extra variables that UnZipSFX needs, the object files from
  53.   UnZip would not mesh with the UnZipSFX object files.  Result: we just
  54.   include the UnZipSFX variables every time.  (It's only an extra 4 bytes
  55.   so who cares!)
  56.  
  57.   ADDING FUNCTIONS
  58.   ----------------
  59.  
  60.   To support this new global struct, all functions must now conditionally
  61.   pass the globals pointer (pG) to each other.  This is supported by 5 macros:
  62.   __GPRO, __GPRO__, __G, __G__ and __GDEF.  A function that needs no other
  63.   parameters would look like this:
  64.  
  65.     int extract_or_test_files(__G)
  66.       __GDEF
  67.     {
  68.        ... stuff ...
  69.     }
  70.  
  71.   A function with other parameters would look like:
  72.  
  73.     int memextract(__G__ tgt, tgtsize, src, srcsize)
  74.         __GDEF
  75.         uch *tgt, *src;
  76.         ulg tgtsize, srcsize;
  77.     {
  78.       ... stuff ...
  79.     }
  80.  
  81.   In the Function Prototypes section of unzpriv.h, you should use __GPRO and
  82.   __GPRO__ instead:
  83.  
  84.     int  uz_opts                   OF((__GPRO__ int *pargc, char ***pargv));
  85.     int  process_zipfiles          OF((__GPRO));
  86.  
  87.   Note that there is NO comma after __G__ or __GPRO__ and no semi-colon after
  88.   __GDEF.  I wish there was another way but I don't think there is.
  89.  
  90.  
  91.   TESTING THE CODE
  92.   -----------------
  93.  
  94.   Whether your platform requires reentrancy or not, you should always try
  95.   building with REENTRANT defined if any functions have been added.  It is
  96.   pretty easy to forget a __G__ or a __GDEF and this mistake will only show
  97.   up if REENTRANT is defined.  All platforms should run with REENTRANT
  98.   defined.  Platforms that can't take advantage of it will just be paying
  99.   a performance penalty needlessly.
  100.  
  101.   SIGNAL MADNESS
  102.   --------------
  103.  
  104.   This whole pointer passing scheme falls apart when it comes to SIGNALs.
  105.   I handle this situation 2 ways right now.  If you define USETHREADID,
  106.   UnZip will include a 64-entry table.  Each entry can hold a global
  107.   pointer and thread ID for one thread.  This should allow up to 64
  108.   threads to access UnZip simultaneously.  Calling DESTROYGLOBALS()
  109.   will free the global struct and zero the table entry.  If somebody
  110.   forgets to call DESTROYGLOBALS(), this table will eventually fill up
  111.   and UnZip will exit with an error message.  A good way to test your
  112.   code to make sure you didn't forget a DESTROYGLOBALS() is to change
  113.   THREADID_ENTRIES to 3 or 4 in globals.c, making the table real small.
  114.   Then make a small test program that calls your API a dozen times.
  115.  
  116.   Those platforms that don't have threads still need to be able to compile
  117.   with REENTRANT defined to test and see if new code is correctly written
  118.   to work either way.  For these platforms, I simply keep a global pointer
  119.   called GG that points to the Globals structure.  Good enough for testing.
  120.  
  121.   I believe that NT has thread level storage.  This could probably be used
  122.   to store a global pointer for the sake of the signal handler more cleanly
  123.   than my table approach.
  124.  
  125.   ---------------------------------------------------------------------------*/
  126.  
  127. #ifndef __globals_h
  128. #define __globals_h
  129.  
  130. #ifdef USE_ZLIB
  131. #  include "zlib.h"
  132. #endif
  133.  
  134.  
  135. /*************/
  136. /*  Globals  */
  137. /*************/
  138.  
  139. struct Globals {
  140.     int zipinfo_mode;   /* behave like ZipInfo or like normal UnZip? */
  141.     int aflag;          /* -a: do ASCII-EBCDIC and/or end-of-line translation */
  142. #ifdef VMS
  143.     int bflag;          /* -b: force fixed record format for binary files */
  144. #endif
  145. #ifdef UNIXBACKUP
  146.     int B_flag;         /* -B: back up existing files by renaming to *~ first */
  147. #endif
  148.     int cflag;          /* -c: output to stdout */
  149.     int C_flag;         /* -C: match filenames case-insensitively */
  150.     int dflag;          /* -d: all args are files/dirs to be extracted */
  151.     int fflag;          /* -f: "freshen" (extract only newer files) */
  152.     int hflag;          /* -h: header line (zipinfo) */
  153. #ifdef RISCOS
  154.     int scanimage;      /* -I: scan image files */
  155. #endif
  156.     int jflag;          /* -j: junk pathnames (unzip) */
  157.     int lflag;          /* -12slmv: listing format (zipinfo) */
  158.     int L_flag;         /* -L: convert filenames from some OSes to lowercase */
  159. #ifdef MORE
  160.     int M_flag;         /* -M: built-in "more" function */
  161.     int height;         /* check for SIGWINCH, etc., eventually... */
  162. #endif                  /* (take line-wrapping into account?) */
  163.     int overwrite_none; /* -n: never overwrite files (no prompting) */
  164.     int overwrite_all;  /* -o: OK to overwrite files without prompting */
  165.     int P_flag;         /* -P: give password on command line (ARGH!) */
  166.     int qflag;          /* -q: produce a lot less output */
  167. #ifdef DOS_FLX_OS2_W32
  168.     int sflag;          /* -s: convert spaces in filenames to underscores */
  169. #endif
  170. #ifdef DOS_OS2_W32
  171.     int volflag;        /* -$: extract volume labels */
  172. #endif
  173.     int tflag;          /* -t: test (unzip) or totals line (zipinfo) */
  174.     int T_flag;         /* -T: timestamps (unzip) or dec. time fmt (zipinfo) */
  175.     int uflag;          /* -u: "update" (extract only newer/brand-new files) */
  176.     int vflag;          /* -v: (verbosely) list directory */
  177.     int V_flag;         /* -V: don't strip VMS version numbers */
  178. #if defined(VMS) || defined(UNIX) || defined(OS2_W32) || defined(__BEOS__)
  179.     int X_flag;         /* -X: restore owner/protection or UID/GID or ACLs */
  180. #endif
  181.     int zflag;          /* -z: display the zipfile comment (only, for unzip) */
  182. #ifdef MACOS
  183.     int HFSFlag;
  184. #endif
  185.  
  186.     int noargs;           /* did true command line have *any* arguments? */
  187.     int filespecs;        /* number of real file specifications to be matched */
  188.     int xfilespecs;       /* number of excluded filespecs to be matched */
  189.     int process_all_files;
  190.     int create_dirs;      /* used by main(), mapname(), checkdir() */
  191.     int extract_flag;
  192.     int newzip;           /* reset in extract.c; used in crypt.c */
  193.     LONGINT   real_ecrec_offset;
  194.     LONGINT   expect_ecrec_offset;
  195.     long csize;           /* used by decompr. (NEXTBYTE): must be signed */
  196.     long ucsize;          /* used by unReduce(), explode() */
  197.     long used_csize;      /* used by extract_or_test_member(), explode() */
  198.  
  199. #ifdef DLL
  200.      int fValidate;       /* true if only validating an archive */
  201.      int filenotfound;
  202.      int redirect_data;   /* redirect data to memory buffer */
  203.      int redirect_text;   /* redirect text output to buffer */
  204. # ifdef OS2DLL
  205.      cbList(processExternally);    /* call-back list */
  206. # endif
  207.      unsigned _wsize;
  208.      int stem_len;
  209.      int putchar_idx;
  210.      uch *redirect_pointer;
  211.      uch *redirect_buffer;
  212.      unsigned redirect_size;
  213. #endif /* DLL */
  214.  
  215.     char **pfnames;
  216.     char **pxnames;
  217.     char sig[5];
  218.     char answerbuf[10];
  219.     min_info info[DIR_BLKSIZ];
  220.     min_info *pInfo;
  221.     union work area;                /* see unzpriv.h for definition of work */
  222.  
  223. #ifndef FUNZIP
  224.     ulg near  *crc_32_tab;
  225. #endif
  226.     ulg       crc32val;             /* CRC shift reg. (was static in funzip) */
  227.  
  228.     uch       *inbuf;               /* input buffer (any size is OK) */
  229.     uch       *inptr;               /* pointer into input buffer */
  230.     int       incnt;
  231.     ulg       bitbuf;
  232.     int       bits_left;            /* unreduce and unshrink only */
  233.     int       zipeof;
  234.     char      *argv0;               /* used for NT and EXE_EXTENSION */
  235.     char      *wildzipfn;
  236.     char      *zipfn;    /* GRR:  WINDLL:  must nuke any malloc'd zipfn... */
  237. #ifdef USE_STRM_INPUT
  238.     FILE      *zipfd;               /* zipfile file descriptor */
  239. #else
  240.     int       zipfd;                /* zipfile file handle */
  241. #endif
  242.     LONGINT   ziplen;
  243.     LONGINT   cur_zipfile_bufstart; /* extract_or_test, readbuf, ReadByte */
  244.     LONGINT   extra_bytes;          /* used in unzip.c, misc.c */
  245.     uch       *extra_field;         /* Unix, VMS, Mac, OS/2, Acorn, ... */
  246.     uch       *hold;
  247.     char      local_hdr_sig[5];     /* initialize sigs at runtime so unzip */
  248.     char      central_hdr_sig[5];   /*  executable won't look like a zipfile */
  249.     char      end_central_sig[5];
  250. /* char extd_local_sig[5];  NOT USED YET */
  251.  
  252.     local_file_hdr  lrec;          /* used in unzip.c, extract.c */
  253.     cdir_file_hdr   crec;          /* used in unzip.c, extract.c, misc.c */
  254.     ecdir_rec       ecrec;         /* used in unzip.c, extract.c */
  255.     struct stat     statbuf;       /* used by main, mapname, check_for_newer */
  256.  
  257.     int      mem_mode;
  258.     uch      *outbufptr;           /* extract.c static */
  259.     ulg      outsize;              /* extract.c static */
  260.     int      reported_backslash;   /* extract.c static */
  261.     int      disk_full;
  262.     int      newfile;
  263.  
  264.     int      didCRlast;            /* fileio static */
  265.     ulg      numlines;             /* fileio static: number of lines printed */
  266.     int      sol;                  /* fileio static: at start of line */
  267.     int      no_ecrec;             /* process static */
  268. #ifdef SYMLINKS
  269.     int      symlnk;
  270. #endif
  271. #ifdef NOVELL_BUG_FAILSAFE
  272.     int      dne;                  /* true if stat() says file doesn't exist */
  273. #endif
  274.  
  275. #ifdef FUNZIP
  276.     FILE     *in;
  277. #endif
  278.     FILE     *outfile;
  279.     uch      *outbuf;
  280.     uch      *realbuf;
  281.  
  282. #ifndef VMS                        /* if SMALL_MEM, outbuf2 is initialized in */
  283.     uch      *outbuf2;             /*  process_zipfiles() (never changes); */
  284. #endif                             /*  else malloc'd ONLY if unshrink and -a */
  285.     uch      *outptr;
  286.     ulg      outcnt;               /* number of chars stored in outbuf */
  287.     char     filename[FILNAMSIZ];  /* also used by NT for temporary SFX path */
  288.  
  289. #ifdef CMS_MVS
  290.     char     *tempfn;              /* temp file used; erase on close */
  291. #endif
  292.  
  293. #ifdef MACOS
  294.     short    gnVRefNum;
  295.     long     glDirID;
  296.     OSType   gostCreator;
  297.     OSType   gostType;
  298.     int      fMacZipped;
  299.     int      macflag;
  300.     short    giCursor;
  301.     CursHandle rghCursor[4];       /* status cursors */
  302. #endif
  303.  
  304.     char *pwdarg;      /* pointer to command-line password (-P option) */
  305.  
  306.     int nopwd;         /* crypt static */
  307.     ulg keys[3];       /* crypt static: keys defining pseudo-random sequence */
  308.     char *key;         /* crypt static: decryption password or NULL */
  309.  
  310. #if (!defined(DOS_FLX_H68_OS2_W32) && !defined(AMIGA) && !defined(RISCOS))
  311. #if (!defined(MACOS) && !defined(ATARI) && !defined(VMS))
  312.     int echofd;        /* crypt static: file descriptor whose echo is off */
  313. #endif /* !(MACOS || ATARI || VMS) */
  314. #endif /* !(DOS_FLX_H68_OS2_W32 || AMIGA || RISCOS) */
  315.  
  316.     unsigned hufts;    /* track memory usage */
  317.  
  318. #ifdef USE_ZLIB
  319.     int inflInit;             /* inflate static: zlib inflate() initialized */
  320.     z_stream dstrm;           /* inflate global: decompression stream */
  321. #else
  322.     struct huft *fixed_tl;    /* inflate static */
  323.     struct huft *fixed_td;    /* inflate static */
  324.     int fixed_bl, fixed_bd;   /* inflate static */
  325.     unsigned wp;              /* inflate static: current position in slide */
  326.     ulg bb;                   /* inflate static: bit buffer */
  327.     unsigned bk;              /* inflate static: bits in bit buffer */
  328. #endif /* ?USE_ZLIB */
  329.  
  330. #ifdef SMALL_MEM
  331.     char rgchBigBuffer[512];
  332.     char rgchSmallBuffer[96];
  333.     char rgchSmallBuffer2[160];  /* boosted to 160 for local3[] in unzip.c */
  334. #endif
  335.  
  336.     MsgFn *message;
  337.     InputFn *input;
  338.     PauseFn *mpause;
  339.     PasswdFn *decr_passwd;
  340. #ifdef WINDLL
  341.     ReplaceFn *replace;
  342.     SoundFn *sound;
  343. #endif
  344.  
  345.     int incnt_leftover;       /* so improved NEXTBYTE does not waste input */
  346.     uch *inptr_leftover;
  347.  
  348. #ifdef VMS_TEXT_CONV
  349.     int VMS_line_state;       /* so native VMS variable-length text files are */
  350.     int VMS_line_length;      /*  readable on other platforms */
  351.     int VMS_line_pad;
  352. #endif
  353.  
  354. #ifdef SYSTEM_SPECIFIC_GLOBALS
  355.     SYSTEM_SPECIFIC_GLOBALS
  356. #endif
  357.  
  358. };  /* end of struct Globals */
  359.  
  360.  
  361. /***************************************************************************/
  362.  
  363.  
  364. #ifdef FUNZIP
  365. #  if !defined(USE_ZLIB) || defined(USE_OWN_CRCTAB)
  366.      extern ulg near  crc_32_tab[];
  367. #  else
  368.      extern ulg near *crc_32_tab;
  369. #  endif
  370. #  define CRC_32_TAB  crc_32_tab
  371. #else
  372. #  define CRC_32_TAB  G.crc_32_tab
  373. #endif
  374.  
  375.  
  376. struct Globals *globalsCtor   OF((void));
  377.  
  378.  
  379. #ifdef REENTRANT
  380. #  define G                   (*pG)
  381. #  define __G                 pG
  382. #  define __G__               pG,
  383. #  define __GPRO              struct Globals *pG
  384. #  define __GPRO__            struct Globals *pG,
  385. #  define __GDEF              struct Globals *pG;
  386. #  ifdef  USETHREADID
  387.      extern int               lastScan;
  388.      void deregisterGlobalPointer     OF((__GPRO));
  389.      struct Globals *getGlobalPointer OF((void));
  390. #    define GETGLOBALS()      struct Globals *pG = getGlobalPointer();
  391. #    define DESTROYGLOBALS()  {free_G_buffers(pG); deregisterGlobalPointer(pG);}
  392. #  else
  393.      extern struct Globals    *GG;
  394. #    define GETGLOBALS()      struct Globals *pG = GG;
  395. #    define DESTROYGLOBALS()  {free_G_buffers(pG); free(pG);}
  396. #  endif /* ?USETHREADID */
  397. #  define CONSTRUCTGLOBALS()  struct Globals *pG = globalsCtor()
  398. #else /* !REENTRANT */
  399.    extern struct Globals      G;
  400. #  define __G
  401. #  define __G__
  402. #  define __GPRO              void
  403. #  define __GPRO__
  404. #  define __GDEF
  405. #  define GETGLOBALS()
  406. #  define CONSTRUCTGLOBALS()  globalsCtor()
  407. #  define DESTROYGLOBALS()
  408. #endif /* ?REENTRANT */
  409.  
  410.  
  411. #endif /* __globals_h */
  412.