home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Comunicatii / htttrack / httrack-3.32-2.exe / {app} / src / htsmodules.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-08  |  11.6 KB  |  402 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsmodules.c subroutines:                              */
  34. /*       external modules (parsers)                             */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htsglobal.h"
  42. #include "htsmodules.h"
  43. #include "htsopt.h"
  44. extern int fspc(FILE* fp,char* type);
  45.  
  46. #ifndef _WIN32
  47. #if HTS_DLOPEN
  48. #include <dlfcn.h>
  49. #endif
  50. #endif
  51.  
  52. /* >>> Put all modules definitions here */
  53. #include "htszlib.h"
  54. #include "htsbase.h"
  55.  
  56. typedef int (*t_hts_detect_swf)(htsmoduleStruct* str);
  57. typedef int (*t_hts_parse_swf)(htsmoduleStruct* str);
  58. /* <<< */
  59.  
  60. /* >>> Put all modules includes here */
  61. #include "htsjava.h"
  62. #if HTS_USESWF
  63. #endif
  64. /* <<< */
  65.  
  66. /* >>> Put all modules variables here */
  67.  
  68. int swf_is_available = 0;
  69. t_hts_detect_swf hts_detect_swf = NULL;
  70. t_hts_parse_swf hts_parse_swf = NULL;
  71.  
  72. int gz_is_available = 0;
  73. #if 0
  74. t_gzopen gzopen = NULL;
  75. t_gzread gzread = NULL;
  76. t_gzclose gzclose = NULL;
  77. #endif
  78.  
  79. int SSL_is_available = 0;
  80. t_SSL_shutdown SSL_shutdown = NULL;
  81. t_SSL_free SSL_free = NULL;
  82. t_SSL_CTX_ctrl SSL_CTX_ctrl = NULL;
  83. t_SSL_new SSL_new = NULL;
  84. t_SSL_clear SSL_clear = NULL;
  85. t_SSL_set_fd SSL_set_fd = NULL;
  86. t_SSL_set_connect_state SSL_set_connect_state = NULL;
  87. t_SSL_connect SSL_connect = NULL;
  88. t_SSL_get_error SSL_get_error = NULL;
  89. t_SSL_write SSL_write = NULL;
  90. t_SSL_read SSL_read = NULL;
  91. t_SSL_library_init SSL_library_init = NULL;
  92. t_ERR_load_crypto_strings ERR_load_crypto_strings = NULL;
  93. t_ERR_load_SSL_strings ERR_load_SSL_strings = NULL;
  94. t_SSLv23_client_method SSLv23_client_method = NULL;
  95. t_SSL_CTX_new SSL_CTX_new = NULL;
  96. t_ERR_error_string ERR_error_string = NULL;
  97. t_SSL_load_error_strings SSL_load_error_strings = NULL;
  98.  
  99. int V6_is_available = HTS_INET6;
  100.  
  101. char WHAT_is_available[64]="";
  102. /* <<< */
  103.  
  104. /* memory checks */
  105. HTSEXT_API htsErrorCallback htsCallbackErr = NULL;
  106. HTSEXT_API int htsMemoryFastXfr = 1;    /* fast xfr by default */
  107. void abortLog__fnc(char* msg, char* file, int line);
  108. void abortLog__fnc(char* msg, char* file, int line) {
  109.   FILE* fp = fopen("CRASH.TXT", "wb");
  110.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb");
  111.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb");
  112.   if (!fp) fp = fopen("CRASH.TXT", "wb");
  113.   if (fp) {
  114.     fprintf(fp, "HTTrack " HTTRACK_VERSIONID " closed at '%s', line %d\r\n", file, line);
  115.     fprintf(fp, "Reason:\r\n%s\r\n", msg);
  116.     fflush(fp);
  117.     fclose(fp);
  118.   }
  119. }
  120. HTSEXT_API t_abortLog abortLog__ = abortLog__fnc;    /* avoid VC++ inlining */
  121.  
  122. static void htspe_log(htsmoduleStruct* str, char* msg);
  123.  
  124. int hts_parse_externals(htsmoduleStruct* str) {
  125.   /* >>> Put all module calls here */
  126.  
  127.   /* JAVA */
  128.   if (hts_detect_java(str)) {
  129.     htspe_log(str, "java-lib");
  130.     return hts_parse_java(str);
  131.   }
  132.  
  133. #if HTS_USESWF
  134.   /* FLASH 
  135.    (external module derivated from Macromedia(tm)'s classes) 
  136.   */
  137.   else if (swf_is_available && hts_detect_swf(str)) {
  138.     htspe_log(str, "swf-lib");
  139.     return hts_parse_swf(str);
  140.   }
  141. #endif
  142.  
  143.   /* <<< */
  144.   
  145.   /* Not detected */
  146.   return -1;
  147. }
  148.  
  149. static void addCallback(htscallbacks* chain, void* moduleHandle, htscallbacksfncptr exitFnc) {
  150.   while(chain->next != NULL) {
  151.     chain = chain->next;
  152.   }
  153.   chain->next = calloct(1, sizeof(htscallbacks));
  154.   assertf(chain->next != NULL);
  155.   chain = chain->next;
  156.   memset(chain, 0, sizeof(*chain));
  157.   chain->exitFnc = exitFnc;
  158.   chain->moduleHandle = moduleHandle;
  159. }
  160.  
  161. void clearCallbacks(htscallbacks* chain_) {
  162.   htscallbacks* chain;
  163.   chain = chain_;
  164.   while(chain != NULL) {
  165.     if (chain->exitFnc != NULL) {
  166.       (void) chain->exitFnc();      /* result ignored */
  167.       chain->exitFnc = NULL;
  168.     }
  169.     chain = chain->next;
  170.   }
  171.   chain = chain_;
  172.   while(chain != NULL) {
  173.     if (chain->moduleHandle != NULL) {
  174. #ifdef _WIN32
  175.       FreeLibrary(chain->moduleHandle);
  176. #else
  177.       dlclose(chain->moduleHandle);
  178. #endif
  179.     }
  180.     chain = chain->next;
  181.   }
  182.   chain = chain_->next;     // Don't free the block #0
  183.   while(chain != NULL) {
  184.     htscallbacks* nextchain = chain->next;
  185.     freet(chain);
  186.     chain = nextchain;
  187.   }
  188.   chain_->next = NULL;    // Empty
  189. }
  190.  
  191. /* NOTE: handled NOT closed */
  192. void* getFunctionPtr(httrackp* opt, char* file_, char* fncname) {
  193.   char file[1024];
  194.   void* handle;
  195.   void* userfunction = NULL;
  196.   strcpybuff(file, file_);
  197. #ifdef _WIN32
  198.   handle = LoadLibraryA((char*)file);
  199.   if (handle == NULL) {
  200.     strcatbuff(file, ".dll");
  201.     handle = LoadLibraryA((char*)file);
  202.   }
  203. #else
  204.   handle = dlopen(file, RTLD_LAZY);
  205.   if (handle == NULL) {
  206.     strcatbuff(file, ".so");
  207.     handle = dlopen(file, RTLD_LAZY);
  208.   }
  209. #endif
  210.   if (handle) {
  211.     /* Thanks to Lars Clausen for the "wrapper-init" patch */
  212.     /* If given arguments, call "<wrappername>_init" */
  213.     char tmpName[1024];
  214.     char *comma;
  215.     if ((comma = strchr(fncname, ',')) != NULL) {   /* empty arg */
  216.       *comma++ = '\0';
  217.     }
  218.     
  219.     /* speficic plug init */
  220.     {
  221.       t_htsWrapperPlugInit initfunction;
  222.       sprintf(tmpName, "%s_init", fncname);
  223.       initfunction = (t_htsWrapperPlugInit)DynamicGet(handle, (char*)tmpName);
  224.       if (initfunction  != NULL) {
  225.         int result = (int) initfunction(comma);
  226.         if (!result) {
  227.           if (userfunction == NULL) {
  228. #ifdef _WIN32
  229.             FreeLibrary(handle);
  230. #else
  231.             dlclose(handle);
  232. #endif
  233.           }
  234.           return NULL;
  235.         }       
  236.       }
  237.     }
  238.     /* wrapper_init() */
  239.     {
  240.       t_htsWrapperInit initfunction = (t_htsWrapperInit)DynamicGet(handle, (char*)"wrapper_init");
  241.       if (initfunction != NULL) {
  242.         if (! initfunction(fncname, comma)) {
  243.           if (userfunction == NULL) {
  244. #ifdef _WIN32
  245.             FreeLibrary(handle);
  246. #else
  247.             dlclose(handle);
  248. #endif
  249.           }
  250.           return NULL;
  251.         }
  252.       }
  253.     }
  254.     /* the function itself */
  255.     userfunction = (void*) DynamicGet(handle, (char*)fncname);
  256.     if (userfunction == NULL) {
  257. #ifdef _WIN32
  258.       FreeLibrary(handle);
  259. #else
  260.       dlclose(handle);
  261. #endif
  262.     } else {
  263.       /* optional exit wrapper */
  264.       t_htsWrapperExit exitFnc = (t_htsWrapperExit) DynamicGet(handle, (char*)"wrapper_exit");
  265.       addCallback(&opt->state.callbacks, handle, exitFnc);      // exitFnc can be null
  266.     }
  267.   }
  268.   return userfunction;
  269. }
  270.  
  271. void htspe_init() {
  272.   static int initOk = 0;
  273.   if (!initOk) {
  274.     initOk = 1;
  275.     
  276.     /* >>> Put all module initializations here */
  277.     
  278.  
  279.     /* Zlib */
  280.     gz_is_available = 1;
  281.     /*
  282. #if HTS_DLOPEN
  283.     {
  284.       void* handle;
  285. #ifdef _WIN32
  286.       handle = LoadLibrary("zlib");
  287. #else
  288.       handle = dlopen("libz.so.1", RTLD_LAZY);
  289. #endif
  290.       if (handle) {
  291.         gzopen = (t_gzopen) DynamicGet(handle, "gzopen");
  292.         gzread = (t_gzread) DynamicGet(handle, "gzread");
  293.         gzclose = (t_gzclose) DynamicGet(handle, "gzclose");
  294.         if (gzopen && gzread && gzclose) {
  295.           gz_is_available = 1;
  296.         }
  297.       }
  298.     }
  299. #endif
  300.     */
  301.  
  302.     /* OpenSSL */
  303. #if HTS_DLOPEN
  304.     {
  305.       void* handle;
  306. #ifdef _WIN32
  307.       handle = LoadLibraryA((char*)"ssleay32");
  308. #else
  309.       /* We are compatible with 0.9.6/7 and potentially above */
  310.       handle = dlopen("libssl.so.0.9.7", RTLD_LAZY);
  311.       if (handle == NULL) {
  312.         handle = dlopen("libssl.so.0.9.6", RTLD_LAZY);
  313.       }
  314.       if (handle == NULL) {
  315.         /* Try harder */
  316.         handle = dlopen("libssl.so.0", RTLD_LAZY);
  317.       }
  318. #endif
  319.       if (handle) {
  320.         SSL_shutdown = (t_SSL_shutdown) DynamicGet(handle, (char*)"SSL_shutdown");
  321.         SSL_free = (t_SSL_free) DynamicGet(handle, (char*)"SSL_free");
  322.         SSL_new = (t_SSL_new) DynamicGet(handle, (char*)"SSL_new");
  323.         SSL_clear = (t_SSL_clear) DynamicGet(handle, (char*)"SSL_clear");
  324.         SSL_set_fd = (t_SSL_set_fd) DynamicGet(handle, (char*)"SSL_set_fd");
  325.         SSL_set_connect_state = (t_SSL_set_connect_state) DynamicGet(handle, (char*)"SSL_set_connect_state");
  326.         SSL_connect = (t_SSL_connect) DynamicGet(handle, (char*)"SSL_connect");
  327.         SSL_get_error = (t_SSL_get_error) DynamicGet(handle, (char*)"SSL_get_error");
  328.         SSL_write = (t_SSL_write) DynamicGet(handle, (char*)"SSL_write");
  329.         SSL_read = (t_SSL_read) DynamicGet(handle, (char*)"SSL_read");
  330.         SSL_library_init = (t_SSL_library_init) DynamicGet(handle, (char*)"SSL_library_init");
  331.         ERR_load_SSL_strings = (t_ERR_load_SSL_strings) DynamicGet(handle, (char*)"ERR_load_SSL_strings");
  332.         SSLv23_client_method = (t_SSLv23_client_method) DynamicGet(handle, (char*)"SSLv23_client_method");
  333.         SSL_CTX_new  = (t_SSL_CTX_new) DynamicGet(handle, (char*)"SSL_CTX_new");
  334.         SSL_load_error_strings = (t_SSL_load_error_strings) DynamicGet(handle, (char*)"SSL_load_error_strings");
  335.         SSL_CTX_ctrl = (t_SSL_CTX_ctrl) DynamicGet(handle, (char*)"SSL_CTX_ctrl");
  336. #ifdef _WIN32
  337.         handle = LoadLibraryA((char*)"libeay32");
  338. #endif
  339.         ERR_load_crypto_strings = (t_ERR_load_crypto_strings) DynamicGet(handle, (char*)"ERR_load_crypto_strings");
  340.         ERR_error_string = (t_ERR_error_string) DynamicGet(handle, (char*)"ERR_error_string");
  341.  
  342.         if (SSL_shutdown && SSL_free && SSL_CTX_ctrl && SSL_new && SSL_clear && 
  343.           SSL_set_fd && SSL_set_connect_state && SSL_connect && SSL_get_error && SSL_write 
  344.           && SSL_read && SSL_library_init && SSLv23_client_method && SSL_CTX_new
  345.           && SSL_load_error_strings && ERR_error_string) {
  346.           SSL_is_available = 1;
  347.         }
  348.       }
  349.     }
  350. #endif
  351.     /* */
  352.     
  353.     /* 
  354.     FLASH
  355.     Load the library on-the-fly, if available
  356.     If not, that's not a problem
  357.     */
  358. #if HTS_DLOPEN
  359.     {
  360. #ifdef _WIN32
  361.       void* handle = LoadLibraryA((char*)"htsswf");
  362. #else
  363.       void* handle = dlopen("libhtsswf.so.1", RTLD_LAZY);
  364. #endif
  365.       if (handle) {
  366.         hts_detect_swf = (t_hts_detect_swf) DynamicGet(handle, "hts_detect_swf");
  367.         hts_parse_swf = (t_hts_parse_swf) DynamicGet(handle, "hts_parse_swf");
  368.         if (hts_detect_swf && hts_parse_swf) {
  369.           swf_is_available = 1;
  370.         }
  371.       }
  372.       // FreeLibrary(handle);
  373.       // dlclose(handle);
  374.     }
  375. #endif
  376.  
  377.     /* <<< */
  378.  
  379.     /* Options availability */
  380.     sprintf(WHAT_is_available, "%s%s%s%s",
  381.       V6_is_available ? "" : "-noV6",
  382.       gz_is_available ? "" : "-nozip",
  383.       SSL_is_available ? "" : "-nossl",
  384.       swf_is_available ? "+swf" : "");
  385.  
  386.     
  387.   }
  388. }
  389.  
  390. static void htspe_log(htsmoduleStruct* str, char* msg) {
  391.   char* savename = str->filename;
  392.   httrackp* opt = (httrackp*) str->opt;
  393.   if ((opt->debug>1) && (opt->log!=NULL)) {
  394.     fspc(opt->log,"debug"); fprintf(opt->log,"(External module): parsing %s using module %s"LF, 
  395.       savename, msg);
  396.   }
  397. }
  398.  
  399. HTSEXT_API const char* hts_is_available(void) {
  400.   return WHAT_is_available;
  401. }
  402.