windll.c 3.7 KB

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