home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / filter.cpp < prev    next >
C/C++ Source or Header  |  1997-10-25  |  3KB  |  120 lines

  1. #include "CkyPch.h"
  2.  
  3. #include "filter.h"
  4. #include "keyword.h"
  5. #include "utils.h"
  6. #include "globals.h"
  7.  
  8.  
  9.  
  10. //
  11. // First pass through the data: count how many extra bytes we're going to
  12. // need to allocate to hold the modified data, if any.
  13. //
  14.  
  15. int
  16. CountExtraBytes(
  17.     LPCTSTR ptszStart,
  18.     UINT    cch)
  19. {
  20.     ASSERT(ptszStart != NULL);
  21.     
  22.     LPCTSTR ptszEnd = ptszStart + cch;
  23.     CStateStack ss;
  24.     int cb = 0;
  25.     
  26.     for (LPCTSTR ptsz = ptszStart;  ptsz < ptszEnd; )
  27.     {
  28.         int cLen;
  29.         const CToken* ptok = g_trie.Search(ptsz, &cLen, ptszEnd - ptsz);
  30.  
  31.         if (ptok == NULL)
  32.             ++ptsz;
  33.         else
  34.         {
  35.             cb += ptok->CountBytes(ss, ptsz, ptszEnd - ptsz);
  36.             ptsz += ptok->m_str.length();
  37.         }
  38.     }
  39.  
  40.     return cb;
  41. }
  42.  
  43.  
  44.  
  45. //
  46. // Second pass: munge the data
  47. //
  48.  
  49. void
  50. DoFilter(
  51.     LPCTSTR ptszStart,
  52.     UINT    cch,
  53.     LPCTSTR ptszSessionID,
  54.     LPTSTR  ptszOutBuf)
  55. {
  56.     ASSERT(ptszStart != NULL  &&  cch > 0);
  57.     ASSERT(ptszSessionID != NULL  &&  _tcslen(ptszSessionID) > 0);
  58.     ASSERT(ptszOutBuf != NULL);
  59.     
  60.     LPCTSTR ptszEnd = ptszStart + cch;
  61.     // const int cchUrl = _tcslen(ptszSessionID);
  62.     // const int cchUrlNameValue = _tcslen(s_szUrlNameValue);
  63.     CStateStack ss(ptszSessionID);
  64.     
  65.     for (LPCTSTR ptsz = ptszStart;  ptsz < ptszEnd; )
  66.     {
  67.         int cLen;
  68.         const CToken* ptok = g_trie.Search(ptsz, &cLen, ptszEnd - ptsz);
  69.  
  70.         if (ptok == NULL)
  71.         {
  72.             // TRACE("%c", *ptsz);
  73.             *ptszOutBuf++ = *ptsz++;
  74.         }
  75.         else
  76.         {
  77.             // DoFilter is supposed to copy itself, if appropriate,
  78.             // and adjust ptsz and ptszOutBuf
  79.             ptok->DoFilter(ss, ptsz, ptszEnd - ptsz, ptszOutBuf);
  80.         }
  81.     }
  82. }
  83.  
  84.  
  85.  
  86. int
  87. Filter(
  88.     PHTTP_FILTER_CONTEXT  pfc,
  89.     PHTTP_FILTER_RAW_DATA pRawData,
  90.     LPCSTR                pszStart,
  91.     UINT                  cch,
  92.     int                   iStart,
  93.     LPCSTR                pszSessionID)
  94. {
  95.     ASSERT(pszSessionID != NULL);
  96.  
  97.     // If empty SessionID (typically happens on plain HTML pages), nothing
  98.     // useful can be done
  99.     if (strlen(pszSessionID) == 0)
  100.         return 0;
  101.  
  102.     const int nExtra = CountExtraBytes(pszStart + iStart, cch - iStart);
  103.  
  104.     if (nExtra > 0)
  105.     {
  106.         TRACE("Filtering `%s', found %d extra bytes\n", pszSessionID, nExtra);
  107.         const int nNewSize = nExtra + cch;
  108.         TRACE("FilterNew: ");
  109.         LPSTR pchNew = (LPSTR) AllocMem(pfc, nNewSize);
  110.         
  111.         memcpy(pchNew, pszStart, iStart);
  112.         DoFilter(pszStart + iStart, cch - iStart,
  113.                  pszSessionID, pchNew + iStart);
  114.         pRawData->pvInData = pchNew;
  115.         pRawData->cbInData = pRawData->cbInBuffer = nNewSize;
  116.     }
  117.  
  118.     return nExtra;
  119. }
  120.