1/*
2  windll/windll.c - Zip 3
3
4  Copyright (c) 1990-2004 Info-ZIP.  All rights reserved.
5
6  See the accompanying file LICENSE, version 2003-May-08 or later
7  (the contents of which are also included in zip.h) for terms of use.
8  If, for some reason, all these files are missing, the Info-ZIP license
9  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
10*/
11/*
12 *  windll.c by Mike White loosly based on Mark Adler's zip.c
13 */
14#include <windows.h>
15#include <process.h>
16#include <signal.h>
17#include <stdarg.h>
18#include <ctype.h>
19#include "../zip.h"
20#include "windll.h"
21
22HINSTANCE hCurrentInst;
23#ifdef ZIPLIB
24/*  DLL Entry Point */
25#ifdef __BORLANDC__
26#pragma argsused
27/* Borland seems to want DllEntryPoint instead of DllMain like MSVC */
28#define DllMain DllEntryPoint
29#endif
30#ifdef WIN32
31BOOL WINAPI DllMain( HINSTANCE hInstance,
32                     DWORD dwReason,
33                     LPVOID plvReserved)
34#else
35int WINAPI LibMain( HINSTANCE hInstance,
36                        WORD wDataSegment,
37                        WORD wHeapSize,
38                        LPSTR lpszCmdLine )
39#endif
40{
41#ifndef WIN32
42/* The startup code for the DLL initializes the local heap(if there is one)
43 with a call to LocalInit which locks the data segment. */
44
45if ( wHeapSize != 0 )
46   {
47   UnlockData( 0 );
48   }
49hCurrentInst = hInstance;
50return 1;   /* Indicate that the DLL was initialized successfully. */
51#else
52BOOL rc = TRUE;
53switch( dwReason )
54   {
55   case DLL_PROCESS_ATTACH:
56      // DLL is loaded. Do your initialization here.
57      // If cannot init, set rc to FALSE.
58      hCurrentInst = hInstance;
59      break;
60
61   case DLL_PROCESS_DETACH:
62      // DLL is unloaded. Do your cleanup here.
63      break;
64   default:
65      break;
66   }
67return rc;
68#endif
69}
70
71#ifdef __BORLANDC__
72#pragma argsused
73#endif
74int FAR PASCAL WEP ( int bSystemExit )
75{
76return 1;
77}
78#endif /* ZIPLIB */
79
80LPSTR szCommentBuf;
81HANDLE hStr;
82
83void comment(unsigned int comlen)
84{
85unsigned int i;
86if (comlen > 65534L)
87   comlen = (unsigned int) 65534L;
88hStr = GlobalAlloc( GPTR, (DWORD)65535L);
89if ( !hStr )
90   {
91   hStr = GlobalAlloc( GPTR, (DWORD) 2);
92   szCommentBuf = GlobalLock(hStr);
93   szCommentBuf[0] = '\0';
94   return;
95   }
96
97szCommentBuf = GlobalLock(hStr);
98if (comlen)
99   {
100   for (i = 0; i < comlen; i++)
101       szCommentBuf[i] = zcomment[i];
102   szCommentBuf[comlen] = '\0';
103   }
104else
105   szCommentBuf[0] = '\0';
106free(zcomment);
107zcomment = malloc(1);
108*zcomment = 0;
109lpZipUserFunctions->comment(szCommentBuf);
110return;
111}
112
113#define STDIO_BUF_SIZE 16384
114
115int __far __cdecl printf(const char *format, ...)
116{
117va_list argptr;
118HANDLE hMemory;
119LPSTR pszBuffer;
120int len;
121
122va_start(argptr, format);
123hMemory = GlobalAlloc(GMEM_MOVEABLE, STDIO_BUF_SIZE);
124WinAssert(hMemory);
125if (!hMemory)
126   {
127   return 0;
128   }
129pszBuffer = (LPSTR)GlobalLock(hMemory);
130WinAssert(pszBuffer);
131len = wvsprintf(pszBuffer, format, argptr);
132va_end(argptr);
133WinAssert(strlen(pszBuffer) < STDIO_BUF_SIZE);
134len = lpZipUserFunctions->print(pszBuffer, len);
135GlobalUnlock(hMemory);
136GlobalFree(hMemory);
137return len;
138}
139
140/* fprintf clone for code in zip.c, etc. */
141int __far __cdecl fprintf(FILE *file, const char *format, ...)
142{
143va_list argptr;
144HANDLE hMemory;
145LPSTR pszBuffer;
146int len;
147
148va_start(argptr, format);
149hMemory = GlobalAlloc(GMEM_MOVEABLE, STDIO_BUF_SIZE);
150WinAssert(hMemory);
151if (!hMemory)
152   {
153   return 0;
154   }
155pszBuffer = GlobalLock(hMemory);
156WinAssert(pszBuffer);
157len = wvsprintf(pszBuffer, format, argptr);
158va_end(argptr);
159WinAssert(strlen(pszBuffer) < STDIO_BUF_SIZE);
160if ((file == stderr) || (file == stdout))
161   {
162   len = lpZipUserFunctions->print(pszBuffer, len);
163   }
164else
165   len = write(fileno(file),(char far *)(pszBuffer), len);
166GlobalUnlock(hMemory);
167GlobalFree(hMemory);
168return len;
169}
170
171void __far __cdecl perror(const char *parm1)
172{
173printf("%s", parm1);
174}
175
176
177