File: windll.c

package info (click to toggle)
unzip 6.0-16%2Bdeb8u3
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 8,768 kB
  • ctags: 10,194
  • sloc: ansic: 55,133; cpp: 4,084; makefile: 2,517; asm: 1,789; cs: 1,012; sh: 119
file content (1075 lines) | stat: -rw-r--r-- 32,101 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
/*
  Copyright (c) 1990-2009 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 2009-Jan-02 or later
  (the contents of which are also included in unzip.h) for terms of use.
  If, for some reason, all these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/* Windows Info-ZIP Unzip DLL module
 *
 * Author: Mike White
 *
 * Original: 1996
 *
 * This module has the entry points for "unzipping" a zip file.
 */

/*---------------------------------------------------------------------------

  This file is the WINDLL replacement for the generic ``main program source
  file'' unzip.c.

  See the general comments in the header part of unzip.c.

  Copyrights:  see accompanying file "COPYING" in UnZip source distribution.
               (This software is free but NOT IN THE PUBLIC DOMAIN.  There
               are some restrictions on commercial use.)

  ---------------------------------------------------------------------------*/

#define __WINDLL_C      /* identifies this source module */

#define WIN32_LEAN_AND_MEAN
#define UNZIP_INTERNAL
#include "../unzip.h"
#include "../crypt.h"
#include "../unzvers.h"
#include "../windll/windll.h"
#include "../windll/structs.h"
#include "../consts.h"
#include <malloc.h>

/* Added type casts to prevent potential "type mismatch" error messages. */
#ifdef REENTRANT
#  undef __G
#  undef __G__
#  define __G                 (Uz_Globs *)pG
#  define __G__               (Uz_Globs *)pG,
#endif

HANDLE hwildZipFN;
HANDLE hInst;               /* current instance */
HANDLE hDCL;
int fNoPrinting = 0;
extern jmp_buf dll_error_return;

/* Helper function to release memory allocated by Wiz_SetOpts() */
static void FreeDllMem(__GPRO);

/* For displaying status messages and error messages */
static int UZ_EXP DllMessagePrint(zvoid *pG, uch *buf, ulg size, int flag);

#if 0 /* currently unused */
/* For displaying files extracted to the display window */
int DllDisplayPrint(zvoid *pG, uch *buf, ulg size, int flag);
#endif /* never */

/* Callback function for status report and/or user interception */
static int UZ_EXP Wiz_StatReportCB(zvoid *pG, int fnflag, ZCONST char *zfn,
                                   ZCONST char *efn, ZCONST zvoid *details);

/* Dummy sound function for those applications that don't use sound */
static void WINAPI DummySound(void);

static int UnzipAllocMemory(unsigned, char *, char *, char ***, unsigned *);
static int UnzipParseString(LPCSTR, unsigned *, char ***);
static void UnzipFreeArguments(unsigned, char ***);

#ifndef UNZIPLIB
/*  DLL Entry Point */

#ifdef __BORLANDC__
#pragma argsused
/* Borland seems to want DllEntryPoint instead of DllMain like MSVC */
#define DllMain DllEntryPoint
#endif
#ifdef WIN32
BOOL WINAPI DllMain( HINSTANCE hInstance,
                     DWORD dwReason,
                     LPVOID plvReserved)
#else
int FAR PASCAL LibMain( HINSTANCE hInstance,
                        WORD wDataSegment,
                        WORD wHeapSize,
                        LPSTR lpszCmdLine )
#endif
{
#ifndef WIN32
   /* The startup code for the DLL initializes the local heap(if there is one)
    * with a call to LocalInit which locks the data segment.
    */

   if ( wHeapSize != 0 )
      {
      UnlockData( 0 );
      }
   hInst = hInstance;
   return 1;   /* Indicate that the DLL was initialized successfully. */
#else
   BOOL rc = TRUE;
   switch( dwReason )
      {
      case DLL_PROCESS_ATTACH:
         // DLL is loaded. Do your initialization here.
         // If cannot init, set rc to FALSE.
         hInst = hInstance;
         break;

      case DLL_PROCESS_DETACH:
         // DLL is unloaded. Do your cleanup here.
         break;
      default:
         break;
      }
   return rc;
#endif
}

#ifdef __BORLANDC__
#pragma argsused
#endif
int FAR PASCAL WEP ( int bSystemExit )
{
   return 1;
}
#endif /* !UNZIPLIB */

static int UnzipAllocMemory(unsigned int i, char *cmd, char *str,
                            char ***pargVee, unsigned int *pargCee)
{
    if (((*pargVee)[i] = (char *)malloc(sizeof(char) * strlen(cmd)+1 ))
        == NULL)
    {
        if (pargCee != NULL)
            (*pargCee)++;
        UnzipFreeArguments(*pargCee, pargVee);
        fprintf(stdout, "Unable to allocate memory in unzip library at %s\n",
                str);
        return PK_MEM;
    }
    strcpy((*pargVee)[i], cmd);
    (*pargCee)++;
    return PK_OK;
}

static void UnzipFreeArguments(unsigned int argCee, char ***pargVee)
{
    unsigned i;

    /* Free the arguments in the arrays */
    for (i = 0; i < argCee; i++)
    {
        free ((*pargVee)[i]);
        (*pargVee)[i] = NULL;
    }

    /* Then free the arrays themselves */
    free(*pargVee);
}


static int UnzipParseString(LPCSTR s, unsigned int *pargCee, char ***pargVee)
{
    unsigned int i = 0;
    char *str1, *str2, *str3;
    size_t size;

    str1 = (char *) malloc(lstrlen(s)+4);
    lstrcpy(str1, s);
    lstrcat(str1, " @");

    str2 = strchr(str1, '\"'); // get first occurance of double quote

    while ((str3 = strchr(str1, '\t')) != NULL)
    {
        str3[0] = ' '; // Change tabs into a single space
    }

    /* Note that if a quoted string contains multiple adjacent spaces, they
       will not be removed, because they could well point to a valid
       folder/file name.
     */
    while ((str2 = strchr(str1, '\"')) != NULL)
    {
        // Found an opening double quote; get the corresponding closing quote
        str3 = strchr(str2+1, '\"');
        if (str3 == NULL)
        {
            free(str1);
            return PK_PARAM; /* Something is screwy with the
                                string, bail out */
        }
        str3[0] = '\0';  // terminate str2 with a NULL

        size = _msize(*pargVee);
        if ((*pargVee = (char **)realloc(*pargVee, size + sizeof(char *)))
            == NULL)
        {
            fprintf(stdout, "Unable to allocate memory in unzip dll\n");
            return PK_MEM;
        }
        // argCee is incremented in UnzipAllocMemory
        if (UnzipAllocMemory(i, str2+1, "Creating file list from string",
                             pargVee, pargCee) != PK_OK)
        {
            free(str1);
            return PK_MEM;
        }
        i++;
        str3+=2;  // Point past the whitespace character
        str2[0] = '\0'; // Terminate str1
        lstrcat(str1, str3);
    }    // end while

    /* points to first occurance of a space */
    str2 = strchr(str1, ' ');

    /*  Go through the string character by character, looking for instances
        of two spaces together. Terminate when you find the trailing @
    */
    while ((str2[0] != '\0') && (str2[0] != '@'))
    {
        while ((str2[0] == ' ') && (str2[1] == ' '))
        {
            str3 = &str2[1];
            str2[0] = '\0';
            lstrcat(str1, str3);
        }
        str2++;
    }

    /* Do we still have a leading space? */
    if (str1[0] == ' ')
    {
        str3 = &str1[1];
        lstrcpy(str1, str3); // Dump the leading space
    }


    /* Okay, now we have gotten rid of any tabs and replaced them with
       spaces, and have replaced multiple spaces with a single space. We
       couldn't do this before because the folder names could have actually
       contained these characters.
    */

    str2 = str3 = str1;

    while ((str2[0] != '\0') && (str3[0] != '@'))
    {
        str3 = strchr(str2+1, ' ');
        str3[0] = '\0';
        size = _msize(pargVee);
        if ((*pargVee = (char **)realloc(*pargVee, size + sizeof(char *)))
            == NULL)
        {
            fprintf(stdout, "Unable to allocate memory in unzip dll\n");
            return PK_MEM;
        }
        if (UnzipAllocMemory(i, str2, "Creating file list from string",
                             pargVee, pargCee) != PK_OK)
        {
            free(str1);
            return PK_MEM;
        }
        i++;
        str3++;
        str2 = str3;
    }
    free(str1);
    return PK_OK;
}

/* DLL calls */

BOOL WINAPI Wiz_Init(pG, lpUserFunc)
zvoid *pG;
LPUSERFUNCTIONS lpUserFunc;
{
    G.message = DllMessagePrint;
    G.statreportcb = Wiz_StatReportCB;
    if (lpUserFunc->sound == NULL)
       lpUserFunc->sound = DummySound;
    G.lpUserFunctions = lpUserFunc;

    SETLOCALE(LC_CTYPE, "");

    if (!G.lpUserFunctions->print ||
        !G.lpUserFunctions->sound ||
        !G.lpUserFunctions->replace)
        return FALSE;

    return TRUE;
}

/*
    StructVersID      = version of this structure (= UZ_DCL_STRUCTVER)
    ExtractOnlyNewer  = TRUE for "update" without interaction
                        (extract only newer/new files, without queries)
    SpaceToUnderscore = TRUE if convert space to underscore
    PromptToOverwrite = TRUE if prompt to overwrite is wanted
    fQuiet            = quiet flag:
                         0 = all messages, 1 = few messages, 2 = no messages
    ncflag            = write to stdout if TRUE
    ntflag            = test zip file
    nvflag            = verbose listing
    nfflag            = "freshen" (replace existing files by newer versions)
    nzflag            = display zip file comment
    ndflag            = controls (sub)directory recreation during extraction
                        0 = junk paths from filenames
                        1 = "safe" usage of paths in filenames (skip "../")
                        2 = allow also unsafe path components (dir traversal)
    noflag            = always overwriting existing files if TRUE
    naflag            = do end-of-line translation
    nZIflag           = get ZipInfo if TRUE
    B_flag            = backup existing files if TRUE
    C_flag            = be case insensitive if TRUE
    D_flag            = controls restoration of timestamps
                        0 = restore all timestamps (default)
                        1 = skip restoration of timestamps for folders
                            created on behalf of directory entries in the
                            Zip archive
                        2 = do not restore any timestamps; extracted files
                            and directories get stamped with the current time
    U_flag            = controls UTF-8 filename coding support
                        0 = automatic UTF-8 translation enabled (default)
                        1 = recognize UTF-8 coded names, but all non-ASCII
                            characters are "escaped" into "#Uxxxx"
                        2 = UTF-8 support is disabled, filename handling
                            works exactly as in previous UnZip versions
    fPrivilege        = 1 => restore ACLs in user mode,
                        2 => try to use privileges for restoring ACLs
    lpszZipFN         = zip file name
    lpszExtractDir    = directory to extract to. This should be NULL if you
                        are extracting to the current directory.
 */

BOOL WINAPI Wiz_SetOpts(pG, lpDCL)
zvoid *pG;
LPDCL lpDCL;
{
    if (lpDCL->StructVersID != UZ_DCL_STRUCTVER)
        return FALSE;

    uO.qflag = lpDCL->fQuiet;  /* Quiet flag */
    G.pfnames = (char **)&fnames[0];    /* assign default file name vector */
    G.pxnames = (char **)&fnames[1];

    uO.jflag = (lpDCL->ndflag == 0);
    uO.ddotflag = (lpDCL->ndflag >= 2);
    uO.cflag = lpDCL->ncflag;
    uO.tflag = lpDCL->ntflag;
    uO.vflag = lpDCL->nvflag;
    uO.zflag = lpDCL->nzflag;
    uO.aflag = lpDCL->naflag;
#ifdef UNIXBACKUP
    uO.B_flag = lpDCL->B_flag;
#endif
    uO.C_flag = lpDCL->C_flag;
    uO.D_flag = lpDCL->D_flag;
    uO.U_flag = lpDCL->U_flag;
    uO.overwrite_all = lpDCL->noflag;
    uO.overwrite_none = !(lpDCL->noflag || lpDCL->PromptToOverwrite);
    uO.uflag = lpDCL->ExtractOnlyNewer || lpDCL->nfflag;
    uO.fflag = lpDCL->nfflag;
#ifdef WIN32
    uO.X_flag = lpDCL->fPrivilege;
#endif
    uO.sflag = lpDCL->SpaceToUnderscore; /* Translate spaces to underscores? */
    if (lpDCL->nZIflag)
      {
      uO.zipinfo_mode = TRUE;
      uO.hflag = TRUE;
      uO.lflag = 10;
      uO.qflag = 2;
      }
    else
      {
      uO.zipinfo_mode = FALSE;
      }

    G.extract_flag = (!uO.zipinfo_mode &&
                      !uO.cflag && !uO.tflag && !uO.vflag && !uO.zflag
#ifdef TIMESTAMP
                      && !uO.T_flag
#endif
                     );

    if (lpDCL->lpszExtractDir != NULL && G.extract_flag)
       {
#ifndef CRTL_CP_IS_ISO
       char *pExDirRoot = (char *)malloc(strlen(lpDCL->lpszExtractDir)+1);

       if (pExDirRoot == NULL)
           return FALSE;
       ISO_TO_INTERN(lpDCL->lpszExtractDir, pExDirRoot);
#else
#  define pExDirRoot lpDCL->lpszExtractDir
#endif
       if (strlen(pExDirRoot) >= FILNAMSIZ)
           {
           /* The supplied extract root path exceed the filename size limit. */
#ifndef CRTL_CP_IS_ISO
           free(pExDirRoot);
#endif
           return FALSE;
           }
       uO.exdir = pExDirRoot;
       }
    else
       {
       uO.exdir = (char *)NULL;
       }

/* G.wildzipfn needs to be initialized so that do_wild does not wind
   up clearing out the zip file name when it returns in process.c
*/
    if (strlen(lpDCL->lpszZipFN) >= FILNAMSIZ)
       /* length of supplied archive name exceed the system's filename limit */
       return FALSE;

    hwildZipFN = GlobalAlloc(GPTR, FILNAMSIZ);
    if (hwildZipFN == (HGLOBAL) NULL)
       return FALSE;

    G.wildzipfn = GlobalLock(hwildZipFN);
    lstrcpy(G.wildzipfn, lpDCL->lpszZipFN);
    _ISO_INTERN(G.wildzipfn);

    return TRUE;    /* set up was OK */
}

static void FreeDllMem(__GPRO)
{
    if (G.wildzipfn) {
        GlobalUnlock(hwildZipFN);
        G.wildzipfn = NULL;
    }
    if (hwildZipFN)
        hwildZipFN = GlobalFree(hwildZipFN);

#ifndef CRTL_CP_IS_ISO
    if (uO.exdir != NULL) {
        free(uO.exdir);
        uO.exdir = NULL;
    }
#endif

    uO.zipinfo_mode = FALSE;
}

int WINAPI Wiz_Unzip(pG, ifnc, ifnv, xfnc, xfnv)
zvoid *pG;
int ifnc;
char **ifnv;
int xfnc;
char **xfnv;
{
    int retcode, f_cnt;
#ifndef CRTL_CP_IS_ISO
    unsigned bufsize;
    char **intern_ifv = NULL, **intern_xfv = NULL;
#endif

    if (ifnv == (char **)NULL && ifnc != 0)
        ifnc = 0;
    else
        for (f_cnt = 0; f_cnt < ifnc; f_cnt++)
            if (ifnv[f_cnt] == (char *)NULL) {
                ifnc = f_cnt;
                break;
            }
    if (xfnv == (char **)NULL && xfnc != 0)
        xfnc = 0;
    else
        for (f_cnt = 0; f_cnt < xfnc; f_cnt++)
            if (xfnv[f_cnt] == (char *)NULL) {
                xfnc = f_cnt;
                break;
            }

    G.process_all_files = (ifnc == 0 && xfnc == 0);         /* for speed */
    G.filespecs = ifnc;
    G.xfilespecs = xfnc;

    if (ifnc > 0) {
        for (f_cnt = ifnc; --f_cnt >= 0;)
            if (strlen(ifnv[f_cnt]) > ((WSIZE>>2) - 160)) {
                /* include filename pattern is too long for internal buffers */
                FreeDllMem(__G);
                return PK_PARAM;
            }

#ifdef CRTL_CP_IS_ISO
        G.pfnames = ifnv;
#else /* !CRTL_CP_IS_ISO */
        intern_ifv = (char **)malloc((ifnc+1)*sizeof(char **));
        if (intern_ifv == (char **)NULL)
        {
            FreeDllMem(__G);
            return PK_BADERR;
        }

        bufsize = 0;
        for (f_cnt = ifnc; --f_cnt >= 0;)
            bufsize += strlen(ifnv[f_cnt]) + 1;
        intern_ifv[0] = (char *)malloc(bufsize);
        if (intern_ifv[0] == (char *)NULL)
        {
            free(intern_ifv);
            FreeDllMem(__G);
            return PK_BADERR;
        }

        for (f_cnt = 0; ; f_cnt++)
        {
            ISO_TO_INTERN(ifnv[f_cnt], intern_ifv[f_cnt]);
            if ((f_cnt+1) >= ifnc)
                break;
            intern_ifv[f_cnt+1] = intern_ifv[f_cnt] +
                                  (strlen(intern_ifv[f_cnt]) + 1);
        }
        intern_ifv[ifnc] = (char *)NULL;
        G.pfnames = intern_ifv;
#endif /* ?CRTL_CP_IS_ISO */
    }

    if (xfnc > 0) {
        for (f_cnt = xfnc; --f_cnt >= 0;)
            if (strlen(xfnv[f_cnt]) > ((WSIZE>>2) - 160))
            {
                /* exclude filename pattern is too long for internal buffers */
#ifndef CRTL_CP_IS_ISO
                if (ifnc > 0)
                {
                    free(intern_ifv[0]);
                    free(intern_ifv);
                }
#endif
                FreeDllMem(__G);
                return PK_PARAM;
            }

#ifdef CRTL_CP_IS_ISO
        G.pxnames = xfnv;
#else /* !CRTL_CP_IS_ISO */
        intern_xfv = (char **)malloc((xfnc+1)*sizeof(char **));
        if (intern_xfv == (char **)NULL)
        {
            if (ifnc > 0)
            {
                free(intern_ifv[0]);
                free(intern_ifv);
            }
            FreeDllMem(__G);
            return PK_BADERR;
        }

        bufsize = 0;
        for (f_cnt = xfnc; --f_cnt >= 0;)
            bufsize += strlen(xfnv[f_cnt]) + 1;
        intern_xfv[0] = (char *)malloc(bufsize);
        if (intern_xfv[0] == (char *)NULL)
        {
            free(intern_xfv);
            if (ifnc > 0)
            {
                free(intern_ifv[0]);
                free(intern_ifv);
            }
            FreeDllMem(__G);
            return PK_BADERR;
        }

        for (f_cnt = 0; ; f_cnt++)
        {
            ISO_TO_INTERN(xfnv[f_cnt], intern_xfv[f_cnt]);
            if ((f_cnt+1) >= xfnc)
                break;
            intern_xfv[f_cnt+1] = intern_xfv[f_cnt] +
                                  (strlen(intern_xfv[f_cnt]) + 1);
        }
        intern_xfv[xfnc] = (char *)NULL;
        G.pxnames = intern_xfv;
#endif /* ?CRTL_CP_IS_ISO */
    }

/*---------------------------------------------------------------------------
    Okey dokey, we have everything we need to get started.  Let's roll.
  ---------------------------------------------------------------------------*/

    retcode = setjmp(dll_error_return);
    if (retcode)
    {
#ifndef CRTL_CP_IS_ISO
        if (xfnc > 0)
        {
            free(intern_xfv[0]);
            free(intern_xfv);
        }
        if (ifnc > 0)
        {
            free(intern_ifv[0]);
            free(intern_ifv);
        }
#endif
        FreeDllMem(__G);
        return PK_BADERR;
    }

    retcode = process_zipfiles(__G);
#ifndef CRTL_CP_IS_ISO
    if (xfnc > 0)
    {
        free(intern_xfv[0]);
        free(intern_xfv);
    }
    if (ifnc > 0)
    {
        free(intern_ifv[0]);
        free(intern_ifv);
    }
#endif
    FreeDllMem(__G);
    return retcode;
}


/*
ifnc       = number of file names being passed. If all files are to be
             extracted, then this can be zero.
ifnv       = file names to be unarchived. Wildcard patterns are recognized
             and expanded. If all files are to be extracted, then this can
             be NULL.
xfnc       = number of "file names to be excluded from processing" being
             passed. If all files are to be extracted, set this to zero.
xfnv       = file names to be excluded from the unarchiving process. Wildcard
             characters are allowed and expanded. If all files are to be
             extracted, set this argument to NULL.
lpDCL      = pointer to a structure with the flags for setting the
             various options, as well as the zip file name.
lpUserFunc = pointer to a structure that contains pointers to functions
             in the calling application, as well as sizes passed back to
             the calling application etc.
*/

int WINAPI Wiz_SingleEntryUnzip(int ifnc, char **ifnv, int xfnc, char **xfnv,
   LPDCL lpDCL, LPUSERFUNCTIONS lpUserFunc)
{
   int retcode;
   CONSTRUCTGLOBALS();

   if (!Wiz_Init((zvoid *)&G, lpUserFunc))
   {
      DESTROYGLOBALS();
      return PK_BADERR;
   }

   if (lpDCL->lpszZipFN == NULL)
   {
      /* Something has screwed up, we don't have a filename */
      DESTROYGLOBALS();
      return PK_NOZIP;
   }

   if (!Wiz_SetOpts((zvoid *)&G, lpDCL))
   {
      DESTROYGLOBALS();
      return PK_MEM;
   }

#ifdef SFX
   G.zipfn = lpDCL->lpszZipFN;
   G.argv0 = lpDCL->lpszZipFN;
#endif

   /* Here is the actual call to "unzip" the files (or whatever else you
    * are doing.)
    */
   retcode = Wiz_Unzip((zvoid *)&G, ifnc, ifnv, xfnc, xfnv);

   DESTROYGLOBALS();
   return retcode;
}


/*
This calling wrapper provided a method to pass file lists as plain strings
instead of the usual string arrays. For VB Users only...
ifnc       = number of file names being passed. If all files are to be
             extracted, then this can be zero.
ExtList    = Pointer to a list of file names to be extracted, separated by
             white space. If all files are to be extracted, then this should
             be NULL. Parameter ifnc should have an accurate count of the
             number of filenames being passed in.
xfnc       = number of "file names to be excluded from processing" being
             passed. If all files are to be extracted, set this to zero.
ExcList    = Pointer to a list of file names to be excluded from processing.
             Parameter xfnc should have an accurate count of the number of
             the number of filenames being passed in.
lpDCL      = pointer to a structure with the flags for setting the
             various options, as well as the zip file name.
lpUserFunc = pointer to a structure that contains pointers to functions
             in the calling application, as well as sizes passed back to
             the calling application etc.
*/

int WINAPI Wiz_SingleEntryUnzpList(unsigned int ifnc, LPCSTR ExtList,
   unsigned int xfnc, LPCSTR ExcList,
   LPDCL lpDCL, LPUSERFUNCTIONS lpUserFunc)
{
    int retcode;
    char **argVExt, **argVExc;
    unsigned int argCExt, argCExc;

    argCExt = argCExc = 0;

    if (ExtList != NULL)
    {
        if ((argVExt = (char **)malloc((ifnc)*sizeof(char *))) == NULL)
        {
            fprintf(stdout, "Unable to allocate memory in unzip dll\n");
            return PK_MEM;
        }
        if ((retcode = UnzipParseString(ExtList, &argCExt, &argVExt)) != PK_OK)
            return retcode;
    }

    if (ExcList != NULL)
    {
        if ((argVExc = (char **)malloc((ifnc)*sizeof(char *))) == NULL)
        {
            fprintf(stdout, "Unable to allocate memory in unzip dll\n");
            UnzipFreeArguments(argCExt, &argVExt);
            return PK_MEM;
        }
        if ((retcode = UnzipParseString(ExcList, &argCExc, &argVExc)) != PK_OK)
        {
            UnzipFreeArguments(argCExt, &argVExt);
            return retcode;
        }
    }

    retcode = Wiz_SingleEntryUnzip(argCExt, argVExt, argCExc, argVExc,
                                   lpDCL, lpUserFunc);
    UnzipFreeArguments(argCExc, &argVExc);
    UnzipFreeArguments(argCExt, &argVExt);
    return retcode;
}


int win_fprintf(zvoid *pG, FILE *file, unsigned int size, char far *buffer)
{
   if ((file != stderr) && (file != stdout))
   {
      return write(fileno(file),(char far *)(buffer),size);
   }
   if (!fNoPrinting)
      return G.lpUserFunctions->print((LPSTR)buffer, size);
   return (int)size;
}

/**********************************
 * Function DllMessagePrint()     *
 *                                *
 * Send messages to status window *
 **********************************/
#ifdef __BORLANDC__
#pragma argsused
#endif
static int UZ_EXP DllMessagePrint(pG, buf, size, flag)
    zvoid *pG;      /* globals struct:  always passed */
    uch *buf;       /* preformatted string to be printed */
    ulg size;       /* length of string (may include nulls) */
    int flag;       /* flag bits */
{
    if (!fNoPrinting)
        return G.lpUserFunctions->print((LPSTR)buf, size);
    else
        return (int)size;
}

#if 0 /* currently unused */
/********************************
 * Function DllDisplayPrint()   *
 *                              *
 * Send files to display window *
 ********************************/
#ifdef __BORLANDC__
#pragma argsused
#endif
int DllDisplayPrint(pG, buf, size, flag)
    zvoid *pG;      /* globals struct:  always passed */
    uch *buf;       /* preformatted string to be printed */
    ulg size;       /* length of string (may include nulls) */
    int flag;       /* flag bits */
{
    return (!fNoPrinting
            ? G.lpUserFunctions->print((LPSTR)buf, size)
            : (int)size);
}
#endif /* never */


/**********************************
 * Function UzpPassword()         *
 *                                *
 * Prompt for decryption password *
 **********************************/
#ifdef __BORLANDC__
#pragma argsused
#endif
int UZ_EXP UzpPassword(pG, rcnt, pwbuf, size, zfn, efn)
    zvoid *pG;          /* globals struct: always passed */
    int *rcnt;          /* retry counter */
    char *pwbuf;        /* buffer for password */
    int size;           /* size of password buffer */
    ZCONST char *zfn;   /* name of zip archiv */
    ZCONST char *efn;   /* name of archiv entry being processed */
{
#if CRYPT
    LPCSTR m;

    if (*rcnt == 0) {
        *rcnt = 2;
        m = "Enter password for: ";
    } else {
        (*rcnt)--;
        m = "Password incorrect--reenter: ";
    }

    return (*G.lpUserFunctions->password)((LPSTR)pwbuf, size, m, (LPCSTR)efn);
#else /* !CRYPT */
    return IZ_PW_ERROR; /* internal error, function should never get called */
#endif /* ?CRYPT */
} /* end function UzpPassword() */

/* Turn off all messages to the calling application */
void WINAPI Wiz_NoPrinting(int f)
{
    fNoPrinting = f;
}

/* Dummy sound function for those applications that don't use sound */
static void WINAPI DummySound(void)
{
}

/* Interface between WINDLL specific service callback functions and the
   generic DLL's "status report & user interception" callback */
#ifdef __BORLANDC__
#pragma argsused
#endif
static int WINAPI Wiz_StatReportCB(zvoid *pG, int fnflag, ZCONST char *zfn,
                    ZCONST char *efn, ZCONST zvoid *details)
{
    int rval = UZ_ST_CONTINUE;

    switch (fnflag) {
      case UZ_ST_START_EXTRACT:
        if (G.lpUserFunctions->sound != NULL)
            (*G.lpUserFunctions->sound)();
        break;
      case UZ_ST_FINISH_MEMBER:
        if ((G.lpUserFunctions->ServCallBk != NULL) &&
            (*G.lpUserFunctions->ServCallBk)(efn,
                                             (details == NULL ? 0L :
#ifdef Z_UINT8_DEFINED
                                              (z_uint8)(*((zusz_t *)details))
#else
                                              *((zusz_t *)details)
#endif
                                             )))
            rval = UZ_ST_BREAK;
        else if (G.lpUserFunctions->ServCallBk_i32 != NULL) {
             unsigned long siz_u4L = 0L;
             unsigned long siz_u4H = 0L;
             if (details != NULL) {
                 siz_u4L = (unsigned long)(*(zusz_t *)details);
#ifdef ZIP64_SUPPORT
                 siz_u4H = (unsigned long)((*(zusz_t *)details) >> 32);
#endif
             }
             if ((*G.lpUserFunctions->ServCallBk_i32)(efn, siz_u4L, siz_u4H))
                 rval = UZ_ST_BREAK;
        }
        break;
      case UZ_ST_IN_PROGRESS:
        break;
      default:
        break;
    }
    return rval;
}


#ifndef SFX
#ifndef __16BIT__

int WINAPI Wiz_UnzipToMemory(LPSTR zip, LPSTR file,
    LPUSERFUNCTIONS lpUserFunctions, UzpBuffer *retstr)
{
    int r;
#ifndef CRTL_CP_IS_ISO
    char *intern_zip, *intern_file;
#endif

    CONSTRUCTGLOBALS();
#ifndef CRTL_CP_IS_ISO
    intern_zip = (char *)malloc(strlen(zip)+1);
    if (intern_zip == NULL) {
       DESTROYGLOBALS();
       return PK_MEM;
    }
    intern_file = (char *)malloc(strlen(file)+1);
    if (intern_file == NULL) {
       DESTROYGLOBALS();
       free(intern_zip);
       return PK_MEM;
    }
    ISO_TO_INTERN(zip, intern_zip);
    ISO_TO_INTERN(file, intern_file);
#   define zip intern_zip
#   define file intern_file
#endif
    if (!Wiz_Init((zvoid *)&G, lpUserFunctions)) {
       DESTROYGLOBALS();
       return PK_BADERR;
    }
    G.redirect_data = 1;

    r = (unzipToMemory(__G__ zip, file, retstr) == PK_COOL);

    DESTROYGLOBALS();
#ifndef CRTL_CP_IS_ISO
#  undef file
#  undef zip
    free(intern_file);
    free(intern_zip);
#endif
    if (!r && retstr->strlength) {
       free(retstr->strptr);
       retstr->strptr = NULL;
    }
    return r;
}




/* Purpose: Determine if file in archive contains the string szSearch

   Parameters: archive  = archive name
               file     = file contained in the archive. This cannot be
                          a wildcard to be meaningful
               pattern  = string to search for
               cmd      = 0 - case-insensitive search
                          1 - case-sensitve search
                          2 - case-insensitive, whole words only
                          3 - case-sensitive, whole words only
               SkipBin  = if true, skip any files that have control
                          characters other than CR, LF, or tab in the first
                          100 characters.

   Returns:    TRUE if a match is found
               FALSE if no match is found
               -1 on error

   Comments: This does not pretend to be as useful as the standard
             Unix grep, which returns the strings associated with a
             particular pattern, nor does it search past the first
             matching occurrence of the pattern.
 */

int WINAPI Wiz_Grep(LPSTR archive, LPSTR file, LPSTR pattern, int cmd,
                   int SkipBin, LPUSERFUNCTIONS lpUserFunctions)
{
    int retcode = FALSE, compare;
    ulg i, j, patternLen, buflen;
    char * sz, *p;
    UzpBuffer retstr;

    /* Turn off any windows printing functions, as they may not have been
     * identified yet. There is no requirement that we initialize the
     * dll with printing stuff for this. */
    Wiz_NoPrinting(TRUE);

    if (!Wiz_UnzipToMemory(archive, file, lpUserFunctions, &retstr)) {
       Wiz_NoPrinting(FALSE);
       return -1;   /* not enough memory, file not found, or other error */
    }

    if (SkipBin) {
        if (retstr.strlength < 100)
            buflen = retstr.strlength;
        else
            buflen = 100;
        for (i = 0; i < buflen; i++) {
            if (iscntrl(retstr.strptr[i])) {
                if ((retstr.strptr[i] != 0x0A) &&
                    (retstr.strptr[i] != 0x0D) &&
                    (retstr.strptr[i] != 0x09))
                {
                    /* OK, we now think we have a binary file of some sort */
                    free(retstr.strptr);
                    Wiz_NoPrinting(FALSE);
                    return FALSE;
                }
            }
        }
    }

    patternLen = strlen(pattern);

    if (retstr.strlength < patternLen) {
        free(retstr.strptr);
        Wiz_NoPrinting(FALSE);
        return FALSE;
    }

    sz = malloc(patternLen + 3); /* add two in case doing whole words only */
    if (cmd > 1) {
        strcpy(sz, " ");
        strcat(sz, pattern);
        strcat(sz, " ");
    } else
        strcpy(sz, pattern);

    if ((cmd == 0) || (cmd == 2)) {
        for (i = 0; i < strlen(sz); i++)
            sz[i] = toupper(sz[i]);
        for (i = 0; i < retstr.strlength; i++)
            retstr.strptr[i] = toupper(retstr.strptr[i]);
    }

    for (i = 0; i < (retstr.strlength - patternLen); i++) {
        p = &retstr.strptr[i];
        compare = TRUE;
        for (j = 0; j < patternLen; j++) {
            /* We cannot do strncmp here, as we may be dealing with a
             * "binary" file, such as a word processing file, or perhaps
             * even a true executable of some sort. */
            if (p[j] != sz[j]) {
                compare = FALSE;
                break;
            }
        }
        if (compare == TRUE) {
            retcode = TRUE;
            break;
        }
    }

    free(sz);
    free(retstr.strptr);
    Wiz_NoPrinting(FALSE); /* Turn printing back on */
    return retcode;
}
#endif /* !__16BIT__ */

int WINAPI Wiz_Validate(LPSTR archive, int AllCodes)
{
    return UzpValidate((char *)archive, AllCodes);
}

#endif /* !SFX */