home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckozli.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  7KB  |  295 lines

  1. /*
  2.   C K O Z L I . C --  ZLIB Interface Source for Kermit 95
  3.  
  4.   Copyright (C) 2001,2004, Trustees of Columbia University in the City of New
  5.   York.
  6.  
  7.   Author:  Jeffrey E Altman (jaltman@secure-endpoints.com)
  8.              Secure Endpoints Inc., New York City
  9. */
  10.  
  11. #include "ckcdeb.h"
  12.  
  13. #ifdef NT
  14. #include <windows.h>
  15. #else /* NT */
  16. #define INCL_DOSMODULEMGR
  17. #define INCL_DOSSEMAPHORES
  18. #include <os2.h>
  19. #endif /* NT */
  20. #include <stdarg.h>
  21.  
  22. #ifdef OS2
  23. #ifdef ZLIB
  24.  
  25. #include "ckozli.h"
  26.  
  27. #ifdef NT
  28. HINSTANCE hZLIB = NULL;
  29. #else /* NT */
  30. HMODULE hZLIB = NULL;
  31. #endif /* NT */
  32.  
  33. #ifdef NOT_KERMIT
  34. static int deblog = 0;
  35. #ifdef debug
  36. #undef debug
  37. #endif
  38. #define debug(x,a,b,c)
  39. #ifdef printf
  40. #undef printf
  41. #endif
  42. static int ssl_finished_messages = 0;
  43. static unsigned long startflags = 0;
  44. #endif /* NOT_KERMIT */
  45.  
  46. int zlib_dll_loaded=0;
  47.  
  48. int
  49. ck_zlib_is_installed()
  50. {
  51.     debug(F111,"ck_zlib_is_installed","hZLIB",hZLIB);
  52.     return(zlib_dll_loaded);
  53. }
  54.  
  55.  
  56. int (WINAPI *p_deflateInit_)(z_streamp strm, int level, const char *version, int stream_size)=NULL;
  57. int (WINAPI *p_inflateInit_)(z_streamp strm, const char *version, int stream_size)=NULL;
  58. int (WINAPI *p_deflateEnd)(z_streamp strm)=NULL;
  59. int (WINAPI *p_inflateEnd)(z_streamp strm)=NULL;
  60. int (WINAPI *p_inflate)(z_streamp strm, int flush)=NULL;
  61. int (WINAPI *p_deflate)(z_streamp strm, int flush)=NULL;
  62.  
  63. int
  64. ck_deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
  65. {
  66.     if ( p_deflateInit_ )
  67.         return p_deflateInit_(strm,level,version,stream_size);
  68.     return 0;
  69. }
  70.  
  71. int
  72. ck_inflateInit_(z_streamp strm, const char *version, int stream_size)
  73. {
  74.     if ( p_inflateInit_ )
  75.         return p_inflateInit_(strm, version, stream_size);
  76.     return 0;
  77.  
  78. }
  79. int
  80. ck_deflateEnd(z_streamp strm)
  81. {
  82.     if ( p_deflateEnd )
  83.         return p_deflateEnd(strm);
  84.     return 0;
  85. }
  86.  
  87. int
  88. ck_inflateEnd(z_streamp strm)
  89. {
  90.     if ( p_inflateEnd )
  91.         return p_inflateEnd(strm);
  92.     return 0;
  93. }
  94.  
  95. int
  96. ck_inflate(z_streamp strm, int flush)
  97. {
  98.     if ( p_inflate )
  99.         return p_inflate(strm, flush);
  100.     return 0;
  101. }
  102.  
  103. int
  104. ck_deflate(z_streamp strm, int flush)
  105. {
  106.     if ( p_deflate )
  107.         return p_deflate(strm, flush);
  108.     return 0;
  109. }
  110.  
  111. int
  112. ck_zlib_loaddll_eh(void)
  113. {
  114.     if ( hZLIB ) {
  115. #ifdef NT
  116.         FreeLibrary(hZLIB);
  117.         hZLIB = NULL;
  118. #else /* NT */
  119.         DosFreeModule(hZLIB);
  120.         hZLIB = 0;
  121. #endif  /* NT */
  122.     }
  123.  
  124.     p_inflateInit_ = NULL;
  125.     p_deflateInit_ = NULL;
  126.     p_inflateEnd = NULL;
  127.     p_deflateEnd = NULL;
  128.     p_inflate = NULL;
  129.     p_deflate = NULL;
  130.  
  131.     return(1);
  132. }
  133.  
  134. int
  135. ck_zlib_loaddll( void )
  136. {
  137.     ULONG rc = 0 ;
  138.     extern unsigned long startflags;
  139.     int load_error = 0;
  140. #ifdef OS2ONLY
  141.     CHAR *exe_path;
  142.     CHAR path[256];
  143.     CHAR * dllname = "ZLIB";
  144.     CHAR errbuf[256];
  145. #endif /* OS2ONLY */
  146.  
  147.     if ( zlib_dll_loaded )
  148.         return(1);
  149.  
  150.     if ( startflags & 8 )       /* do not load if we are not supposed to */
  151.         return(0);
  152.  
  153. #ifdef NT
  154.     hZLIB = LoadLibrary("ZLIB");
  155.     if ( !hZLIB ) {
  156.         rc = GetLastError() ;
  157.         debug(F111, "ZLIB LoadLibrary failed","ZLIB",rc) ;
  158.         return(0);
  159.     }
  160.  
  161.     if (((FARPROC) p_inflateInit_ =
  162.           GetProcAddress( hZLIB, "inflateInit_" )) == NULL )
  163.     {
  164.         rc = GetLastError() ;
  165.         debug(F111,"ZLIB GetProcAddress failed","inflateInit_",rc);
  166.         load_error = 1;
  167.     }
  168.     if (((FARPROC) p_deflateInit_ =
  169.           GetProcAddress( hZLIB, "deflateInit_" )) == NULL )
  170.     {
  171.         rc = GetLastError() ;
  172.         debug(F111,"ZLIB GetProcAddress failed","deflateInit_",rc);
  173.         load_error = 1;
  174.     }
  175.     if (((FARPROC) p_inflateEnd =
  176.           GetProcAddress( hZLIB, "inflateEnd" )) == NULL )
  177.     {
  178.         rc = GetLastError() ;
  179.         debug(F111,"ZLIB GetProcAddress failed","inflateEnd",rc);
  180.         load_error = 1;
  181.     }
  182.     if (((FARPROC) p_deflateEnd =
  183.           GetProcAddress( hZLIB, "deflateEnd" )) == NULL )
  184.     {
  185.         rc = GetLastError() ;
  186.         debug(F111,"ZLIB GetProcAddress failed","deflateEnd",rc);
  187.         load_error = 1;
  188.     }
  189.     if (((FARPROC) p_inflate =
  190.           GetProcAddress( hZLIB, "inflate" )) == NULL )
  191.     {
  192.         rc = GetLastError() ;
  193.         debug(F111,"ZLIB GetProcAddress failed","inflate",rc);
  194.         load_error = 1;
  195.     }
  196.     if (((FARPROC) p_deflate =
  197.           GetProcAddress( hZLIB, "deflate" )) == NULL )
  198.     {
  199.         rc = GetLastError() ;
  200.         debug(F111,"ZLIB GetProcAddress failed","deflate",rc);
  201.         load_error = 1;
  202.     }
  203. #else /* NT */
  204.     exe_path = GetLoadPath();
  205.     sprintf(path, "%.*s%s.DLL", (int)get_dir_len(exe_path), exe_path,dllname);
  206.     rc = DosLoadModule(errbuf, 256, path, &hZLIB);
  207.     if (rc) {
  208.         debug(F111, "ZLIB LoadLibrary failed",path,rc) ;
  209.         rc = DosLoadModule(errbuf, 256, dllname, &hZLIB);
  210.     }
  211.     if ( rc ) {
  212.         debug(F111, "ZLIB LoadLibrary failed",errbuf,rc) ;
  213.         return(0);
  214.     }
  215.  
  216.     if (rc = DosQueryProcAddr( hZLIB, 0, "inflateInit_",
  217.                                (PFN*) &p_inflateInit_) )
  218.     {
  219.         debug(F111,"ZLIB GetProcAddress failed","inflateInit_",rc);
  220.         load_error = 1;
  221.     }
  222.     if (rc = DosQueryProcAddr( hZLIB, 0, "deflateInit_",
  223.                                (PFN*) &p_deflateInit_) )
  224.     {
  225.         debug(F111,"ZLIB GetProcAddress failed","deflateInit_",rc);
  226.         load_error = 1;
  227.     }
  228.     if (rc = DosQueryProcAddr( hZLIB, 0, "inflateEnd",
  229.                                (PFN*) &p_inflateEnd) )
  230.     {
  231.         debug(F111,"ZLIB GetProcAddress failed","inflateEnd",rc);
  232.         load_error = 1;
  233.     }
  234.     if (rc = DosQueryProcAddr( hZLIB, 0, "deflateEnd",
  235.                                (PFN*) &p_deflateEnd) )
  236.     {
  237.         debug(F111,"ZLIB GetProcAddress failed","deflateEnd",rc);
  238.         load_error = 1;
  239.     }
  240.     if (rc = DosQueryProcAddr( hZLIB, 0, "inflate",
  241.                                (PFN*) &p_inflate) )
  242.     {
  243.         debug(F111,"ZLIB GetProcAddress failed","inflate",rc);
  244.         load_error = 1;
  245.     }
  246.     if (rc = DosQueryProcAddr( hZLIB, 0, "deflate",
  247.                                (PFN*) &p_deflate) )
  248.     {
  249.         debug(F111,"ZLIB GetProcAddress failed","deflate",rc);
  250.         load_error = 1;
  251.     }
  252. #endif /* NT */
  253.  
  254.     if ( load_error ) {
  255.         ck_zlib_loaddll_eh();
  256.         return 0;
  257.     }
  258.  
  259.     zlib_dll_loaded = 1;
  260.     if ( deblog ) {
  261.         printf("ZLIB DLL Loaded\n");
  262.         debug(F100,"ZLIB DLL Loaded","",0);
  263.     }
  264.  
  265.     return(1);
  266. }
  267.  
  268. int
  269. ck_zlib_unloaddll( void )
  270. {
  271.     if ( !zlib_dll_loaded )
  272.         return(1);
  273.  
  274.     /* unload dlls */
  275.     ck_zlib_loaddll_eh();
  276.  
  277.     /* success */
  278.     zlib_dll_loaded = 0;
  279.     return(1);
  280. }
  281. #else /* ZLIB */
  282. int
  283. ck_zlib_loaddll( void )
  284. {
  285.     return(0);
  286. }
  287.  
  288. int
  289. ck_zlib_unloaddll( void )
  290. {
  291.     return(0);
  292. }
  293. #endif /* ZLIB */
  294. #endif /* OS2 */
  295.