compress.cpp
上传用户:nbcables
上传日期:2007-01-11
资源大小:1243k
文件大小:4k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <malloc.h>
  3. #include "util.h"
  4. #include "const.h"
  5. #include "compress.h"
  6. char compress_dll_file[128];
  7. CompressFilter::CompressFilter()
  8. {
  9. hModCompress =NULL;
  10. myCompressData =NULL;
  11. myDecompressData =NULL;
  12. m_pCompressInfo =NULL;
  13. m_compress_info_count =0;
  14. char init_file[128];
  15. char temp[100], temp1[128];
  16. if(g_szDllPath[0] ==0 || m_compress_info_count >0)
  17. return;
  18. //WriteLog("CompressInit...");
  19. wsprintf(init_file, "%s\compress.ini", g_szDllPath);
  20. if(GetPrivateProfileString("SETUP", "dll", "", temp1, sizeof(temp1), init_file) >0)
  21. wsprintf(compress_dll_file, "%s\%s", g_szDllPath, temp1);
  22. m_compress_info_count =0;
  23. while(1)
  24. {
  25. wsprintf(temp, "APP%d", m_compress_info_count+1);
  26. if(GetPrivateProfileString("SETUP", temp, "", temp1, sizeof(temp1), init_file) <1)
  27. break;
  28. if(m_pCompressInfo ==NULL) m_pCompressInfo =(COMPRESS_INFO *)malloc(sizeof(COMPRESS_INFO));
  29. else m_pCompressInfo =(COMPRESS_INFO *)realloc(m_pCompressInfo, sizeof(COMPRESS_INFO)*(m_compress_info_count+1));
  30. if(m_pCompressInfo ==NULL) return;
  31. memset(&m_pCompressInfo[m_compress_info_count], 0, sizeof(COMPRESS_INFO));
  32. strupr(temp1);
  33. strcpy(m_pCompressInfo[m_compress_info_count].app, temp1);
  34. wsprintf(temp, "IP%d", m_compress_info_count+1);
  35. GetPrivateProfileString("SETUP", temp, "", m_pCompressInfo[m_compress_info_count].ip, 16, init_file);
  36. wsprintf(temp, "PORT%d", m_compress_info_count+1);
  37. m_pCompressInfo[m_compress_info_count].server_port =GetPrivateProfileInt("SETUP", temp, 0, init_file);
  38. m_compress_info_count++;
  39. //WriteLog("compress:%d", m_compress_info_count);
  40. }
  41. if(compress_dll_file[0] ==0) return;
  42. if((hModCompress =LoadLibrary(compress_dll_file)) !=NULL)
  43. {
  44. myCompressData =(COMPRESSDATA)GetProcAddress((HMODULE)hModCompress, "CompressData");
  45. myDecompressData =(DECOMPRESSDATA)GetProcAddress((HMODULE)hModCompress, "DecompressData");
  46. }
  47. else WriteLog("LoadLibrary compress dll %s failed!", compress_dll_file);
  48. }
  49. CompressFilter::~CompressFilter()
  50. {
  51. if(hModCompress)
  52. {
  53. FreeLibrary((HINSTANCE)hModCompress);
  54. hModCompress =NULL;
  55. }
  56. if(m_pCompressInfo)
  57. {
  58. free(m_pCompressInfo);
  59. m_pCompressInfo =NULL;
  60. }
  61. m_compress_info_count =0;
  62. }
  63. // 检查是否需要压缩解压
  64. BOOL CompressFilter::CompressCheck(char *remote_ip, int local_port, int remote_port)
  65. {
  66. int i;
  67. int port;
  68. //WriteLog("Compress check...");
  69. for(i =0; i<m_compress_info_count; i++)
  70. {
  71. if(strcmp(m_pCompressInfo[i].app, g_szAppBaseName)==0 || strcmp(m_pCompressInfo[i].app, "ALL") ==0)
  72. {
  73. if(m_pCompressInfo[i].ip[0] ==0 || strcmp(m_pCompressInfo[i].ip, remote_ip) ==0)
  74. {
  75. if(m_pCompressInfo[i].server_port ==0) return true;
  76. if(m_pCompressInfo[i].local_port_type[0] =='S' || m_pCompressInfo[i].local_port_type[0] =='s')
  77. port =local_port;
  78. else port =remote_port;
  79. if(m_pCompressInfo[i].server_port ==port)
  80. return true;
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86. // CompressData
  87. // return: 0 - data not changed, 1 - data compressed, <0 - failed
  88. int CompressFilter::CompressData(char *data, int data_len, char *data_compressed, int *pdata_compressed_len)
  89. {
  90. int ret =0;
  91. if(myCompressData)
  92. ret = myCompressData(data, data_len, data_compressed, pdata_compressed_len);
  93. return ret;
  94. }
  95. // DecompressData
  96. // return: 0 - data not changed, 1 - data decompressed, <0 - failed
  97. int CompressFilter::DecompressData(char *data, int data_len, char *data_decompressed, int *pdata_decompressed_len)
  98. {
  99. int ret =0;
  100. if(myDecompressData)
  101. ret = myDecompressData(data, data_len, data_decompressed, pdata_decompressed_len);
  102. return ret;
  103. }