<commit_before>/*
* Copyright (c) 2003-2010 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

/*
* This routine processes Filter XML using the STANDARD and UNMODIFIED
* Xerces library V3.0.0 released on September 29, 2008
*
* See http://xerces.apache.org/xerces-c/
*
* Note: This is a cross-platform library and can be linked in as a
* Static library or used as a dynamic library e.g. DLL in Windows.
* To use the static version, the following pre-processor statement
* must be defined: XERCES_STATIC_LIBRARY
*
*/

/*
* NOTE: Xerces characters are ALWAYS in UTF-16 (may or may not be wchar_t 
* depending on platform).
* Non-unicode builds will need convert any results from parsing the XML
* document from UTF-16 to ASCII.
*/

#include "../XMLDefs.h"    // Required if testing "USE_XML_LIBRARY"

#if USE_XML_LIBRARY == XERCES

// PWS includes
#include "XFilterSAX2Handlers.h"

#include "../../Util.h"
#include "../../UUIDGen.h"
#include "../../corelib.h"
#include "../../PWSFilters.h"
#include "../../VerifyFormat.h"
#include "../../Match.h"
#include "../../../os/pws_tchar.h"
#include "./XMLChConverter.h"

#include <map>
#include <algorithm>

// Xerces includes
#include <xercesc/util/XMLString.hpp>

#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/sax/SAXException.hpp>

using namespace std;

XFilterSAX2Handlers::XFilterSAX2Handlers()
{
  m_strElemContent.clear();
  m_iXMLVersion = -1;
  m_iSchemaVersion = -1;
  m_bErrors = false;
  m_pAsker = NULL;
}

XFilterSAX2Handlers::~XFilterSAX2Handlers()
{
}

void XFilterSAX2Handlers::startDocument()
{
  m_strXMLErrors.clear();
  m_bentrybeingprocessed = false;
}

void XFilterSAX2Handlers::startElement(const XMLCh* const /* uri */,
                                      const XMLCh* const /* localname */,
                                      const XMLCh* const qname,
                                      const Attributes& attrs)
{
  USES_XMLCH_STR
  
  if (m_bValidation && XMLString::equals(qname, _A2X("filters"))) {
    if (m_pSchema_Version == NULL) {
      LoadAString(m_strXMLErrors, IDSC_MISSING_SCHEMA_VER);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return;
    }

    if (m_iSchemaVersion <= 0) {
      LoadAString(m_strXMLErrors, IDSC_INVALID_SCHEMA_VER);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return;
    }

    const XMLCh * xmlchValue = attrs.getValue(_A2X("version"));
    if (xmlchValue != NULL) {
      m_iXMLVersion = XMLString::parseInt(xmlchValue);
    }
    return;
  }

  if (m_bValidation)
    return;

  bool bfilter = XMLString::equals(qname, _A2X("filter"));
  bool bfilter_entry = XMLString::equals(qname, _A2X("filter_entry"));
 
   if (bfilter) {
    cur_filter = new st_filters;
  }

  if (bfilter_entry) {
    cur_filterentry = new st_FilterRow;
    cur_filterentry->Empty();
    cur_filterentry->bFilterActive = true;
    m_bentrybeingprocessed = true;
  }

  if (bfilter || bfilter_entry) {
    // Process the attributes we need.
    if (bfilter) {
      XMLCh * xmlchValue = (XMLCh *)attrs.getValue(_A2X("filtername")); 
      if (xmlchValue != NULL) {
#ifdef UNICODE
        cur_filter->fname = stringT(_X2ST(xmlchValue));
#else
        char *szValue = XMLString::transcode(xmlchValue);
        cur_filter->fname = stringT(szValue);
        XMLString::release(&szValue);
#endif
      }
    }

    if (bfilter_entry) {
      const XMLCh * xmlchValue = attrs.getValue(_A2X("active")); 
      if (xmlchValue != NULL && XMLString::equals(xmlchValue, _A2X("no")))
        cur_filterentry->bFilterActive = false;
    }
  }

  m_strElemContent = _T("");
}

void XFilterSAX2Handlers::characters(const XMLCh* const chars,
                                    const XMLSize_t length)
{
  USES_XMLCH_STR
  
  if (m_bValidation)
    return;

  XMLCh *xmlchData = new XMLCh[length + 1];
  XMLString::copyNString(xmlchData, chars, length);
  xmlchData[length] = L'\0';
#ifdef UNICODE
  m_strElemContent += StringX(_X2SX(xmlchData));
#else
  char *szData = XMLString::transcode(xmlchData);
  m_strElemContent += StringX(szData);
  XMLString::release(&szData);
#endif
  delete [] xmlchData;
}

void XFilterSAX2Handlers::ignorableWhitespace(const XMLCh* const chars,
                                             const XMLSize_t length)
{
  USES_XMLCH_STR
  
  if (m_bValidation)
    return;

  XMLCh *xmlchData = new XMLCh[length + 1];
  XMLString::copyNString(xmlchData, chars, length);
  xmlchData[length] = L'\0';
#ifdef UNICODE
  m_strElemContent += StringX(_X2SX(xmlchData));
#else
  char *szData = XMLString::transcode(xmlchData);
  m_strElemContent += StringX(szData);
  XMLString::release(&szData);
#endif
  delete [] xmlchData;
}

void XFilterSAX2Handlers::endElement(const XMLCh* const /* uri */,
                                    const XMLCh* const /* localname */,
                                    const XMLCh* const qname)
{
  USES_XMLCH_STR
  
  if (m_bValidation && XMLString::equals(qname, _A2X("filters"))) {
    // Check that the XML file version is present and that
    // a. it is less than or equal to the Filter schema version
    // b. it is less than or equal to the version supported by this PWS
    if (m_iXMLVersion < 0) {
      LoadAString(m_strXMLErrors, IDSC_MISSING_XML_VER);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return ;
    }
    if (m_iXMLVersion > m_iSchemaVersion) {
      Format(m_strXMLErrors,
             IDSC_INVALID_XML_VER1, m_iXMLVersion, m_iSchemaVersion);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return ;
    }
    if (m_iXMLVersion > PWS_XML_FILTER_VERSION) {
      Format(m_strXMLErrors, 
             IDSC_INVALID_XML_VER2, m_iXMLVersion, PWS_XML_FILTER_VERSION);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return;
    }
  }

  if (m_bValidation) {
    return;
  }

  if (XMLString::equals(qname, _A2X("filter"))) {
#ifdef _WIN32
    INT_PTR rc = IDYES;
#else
#pragma message("TODO - David - define IDYES for non-Windows platforms")
    enum {IDYES = 1};
    int rc = IDYES;
#endif
    st_Filterkey fk;
    fk.fpool = m_FPool;
    fk.cs_filtername = cur_filter->fname;
    if (m_MapFilters->find(fk) != m_MapFilters->end()) {
      stringT question;
      Format(question, IDSC_FILTEREXISTS, cur_filter->fname.c_str());
      if (m_pAsker == NULL || !(*m_pAsker)(question)) {
        m_MapFilters->erase(fk);
      }
    }
    if (rc == IDYES) {
      m_MapFilters->insert(PWSFilters::Pair(fk, *cur_filter));
    }
    delete cur_filter;
    return;
  }

  else if (XMLString::equals(qname, _A2X("filter_entry"))) {
    if (cur_filterentry->mtype  == PWSMatch::MT_DATE &&
        cur_filterentry->rule   != PWSMatch::MR_PRESENT &&
        cur_filterentry->rule   != PWSMatch::MR_NOTPRESENT &&
        cur_filterentry->fdate1 == (time_t)0 &&
        cur_filterentry->fdate2 == (time_t)0)
      cur_filterentry->fdatetype = 1; // Relative Date
    if (m_type == DFTYPE_MAIN) {
      cur_filter->num_Mactive++;
      cur_filter->vMfldata.push_back(*cur_filterentry);
    } else if (m_type == DFTYPE_PWHISTORY) {
      cur_filter->num_Hactive++;
      cur_filter->vHfldata.push_back(*cur_filterentry);
    } else if (m_type == DFTYPE_PWPOLICY) {
      cur_filter->num_Pactive++;
      cur_filter->vPfldata.push_back(*cur_filterentry);
    }
    delete cur_filterentry;
  }<commit_msg>Removed INT_PTR and IDYES usage<commit_after>/*
* Copyright (c) 2003-2010 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

/*
* This routine processes Filter XML using the STANDARD and UNMODIFIED
* Xerces library V3.0.0 released on September 29, 2008
*
* See http://xerces.apache.org/xerces-c/
*
* Note: This is a cross-platform library and can be linked in as a
* Static library or used as a dynamic library e.g. DLL in Windows.
* To use the static version, the following pre-processor statement
* must be defined: XERCES_STATIC_LIBRARY
*
*/

/*
* NOTE: Xerces characters are ALWAYS in UTF-16 (may or may not be wchar_t 
* depending on platform).
* Non-unicode builds will need convert any results from parsing the XML
* document from UTF-16 to ASCII.
*/

#include "../XMLDefs.h"    // Required if testing "USE_XML_LIBRARY"

#if USE_XML_LIBRARY == XERCES

// PWS includes
#include "XFilterSAX2Handlers.h"

#include "../../Util.h"
#include "../../UUIDGen.h"
#include "../../corelib.h"
#include "../../PWSFilters.h"
#include "../../VerifyFormat.h"
#include "../../Match.h"
#include "../../../os/pws_tchar.h"
#include "./XMLChConverter.h"

#include <map>
#include <algorithm>

// Xerces includes
#include <xercesc/util/XMLString.hpp>

#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/sax/SAXException.hpp>

using namespace std;

XFilterSAX2Handlers::XFilterSAX2Handlers()
{
  m_strElemContent.clear();
  m_iXMLVersion = -1;
  m_iSchemaVersion = -1;
  m_bErrors = false;
  m_pAsker = NULL;
}

XFilterSAX2Handlers::~XFilterSAX2Handlers()
{
}

void XFilterSAX2Handlers::startDocument()
{
  m_strXMLErrors.clear();
  m_bentrybeingprocessed = false;
}

void XFilterSAX2Handlers::startElement(const XMLCh* const /* uri */,
                                      const XMLCh* const /* localname */,
                                      const XMLCh* const qname,
                                      const Attributes& attrs)
{
  USES_XMLCH_STR
  
  if (m_bValidation && XMLString::equals(qname, _A2X("filters"))) {
    if (m_pSchema_Version == NULL) {
      LoadAString(m_strXMLErrors, IDSC_MISSING_SCHEMA_VER);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return;
    }

    if (m_iSchemaVersion <= 0) {
      LoadAString(m_strXMLErrors, IDSC_INVALID_SCHEMA_VER);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return;
    }

    const XMLCh * xmlchValue = attrs.getValue(_A2X("version"));
    if (xmlchValue != NULL) {
      m_iXMLVersion = XMLString::parseInt(xmlchValue);
    }
    return;
  }

  if (m_bValidation)
    return;

  bool bfilter = XMLString::equals(qname, _A2X("filter"));
  bool bfilter_entry = XMLString::equals(qname, _A2X("filter_entry"));
 
   if (bfilter) {
    cur_filter = new st_filters;
  }

  if (bfilter_entry) {
    cur_filterentry = new st_FilterRow;
    cur_filterentry->Empty();
    cur_filterentry->bFilterActive = true;
    m_bentrybeingprocessed = true;
  }

  if (bfilter || bfilter_entry) {
    // Process the attributes we need.
    if (bfilter) {
      XMLCh * xmlchValue = (XMLCh *)attrs.getValue(_A2X("filtername")); 
      if (xmlchValue != NULL) {
#ifdef UNICODE
        cur_filter->fname = stringT(_X2ST(xmlchValue));
#else
        char *szValue = XMLString::transcode(xmlchValue);
        cur_filter->fname = stringT(szValue);
        XMLString::release(&szValue);
#endif
      }
    }

    if (bfilter_entry) {
      const XMLCh * xmlchValue = attrs.getValue(_A2X("active")); 
      if (xmlchValue != NULL && XMLString::equals(xmlchValue, _A2X("no")))
        cur_filterentry->bFilterActive = false;
    }
  }

  m_strElemContent = _T("");
}

void XFilterSAX2Handlers::characters(const XMLCh* const chars,
                                    const XMLSize_t length)
{
  USES_XMLCH_STR
  
  if (m_bValidation)
    return;

  XMLCh *xmlchData = new XMLCh[length + 1];
  XMLString::copyNString(xmlchData, chars, length);
  xmlchData[length] = L'\0';
#ifdef UNICODE
  m_strElemContent += StringX(_X2SX(xmlchData));
#else
  char *szData = XMLString::transcode(xmlchData);
  m_strElemContent += StringX(szData);
  XMLString::release(&szData);
#endif
  delete [] xmlchData;
}

void XFilterSAX2Handlers::ignorableWhitespace(const XMLCh* const chars,
                                             const XMLSize_t length)
{
  USES_XMLCH_STR
  
  if (m_bValidation)
    return;

  XMLCh *xmlchData = new XMLCh[length + 1];
  XMLString::copyNString(xmlchData, chars, length);
  xmlchData[length] = L'\0';
#ifdef UNICODE
  m_strElemContent += StringX(_X2SX(xmlchData));
#else
  char *szData = XMLString::transcode(xmlchData);
  m_strElemContent += StringX(szData);
  XMLString::release(&szData);
#endif
  delete [] xmlchData;
}

void XFilterSAX2Handlers::endElement(const XMLCh* const /* uri */,
                                    const XMLCh* const /* localname */,
                                    const XMLCh* const qname)
{
  USES_XMLCH_STR
  
  if (m_bValidation && XMLString::equals(qname, _A2X("filters"))) {
    // Check that the XML file version is present and that
    // a. it is less than or equal to the Filter schema version
    // b. it is less than or equal to the version supported by this PWS
    if (m_iXMLVersion < 0) {
      LoadAString(m_strXMLErrors, IDSC_MISSING_XML_VER);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return ;
    }
    if (m_iXMLVersion > m_iSchemaVersion) {
      Format(m_strXMLErrors,
             IDSC_INVALID_XML_VER1, m_iXMLVersion, m_iSchemaVersion);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return ;
    }
    if (m_iXMLVersion > PWS_XML_FILTER_VERSION) {
      Format(m_strXMLErrors, 
             IDSC_INVALID_XML_VER2, m_iXMLVersion, PWS_XML_FILTER_VERSION);
#ifdef UNICODE
      const XMLCh *message = _W2X(m_strXMLErrors.c_str());
#else
      const XMLCh *message = XMLString::transcode(m_strXMLErrors.c_str());
#endif
      SAXParseException(message, *m_pLocator);
#ifndef UNICODE
      XMLString::release((XMLCh **)&message);
#endif
      return;
    }
  }

  if (m_bValidation) {
    return;
  }

  if (XMLString::equals(qname, _A2X("filter"))) {
    bool bAddFilter(true);
    st_Filterkey fk;
    fk.fpool = m_FPool;
    fk.cs_filtername = cur_filter->fname;
    if (m_MapFilters->find(fk) != m_MapFilters->end()) {
      stringT question;
      Format(question, IDSC_FILTEREXISTS, cur_filter->fname.c_str());
      if (m_pAsker == NULL || (bAddFilter = (*m_pAsker)(question)) == true) {
        m_MapFilters->erase(fk);
      }
    }
    if (bAddFilter) {
      m_MapFilters->insert(PWSFilters::Pair(fk, *cur_filter));
    }
    delete cur_filter;
    return;
  }

  else if (XMLString::equals(qname, _A2X("filter_entry"))) {
    if (cur_filterentry->mtype  == PWSMatch::MT_DATE &&
        cur_filterentry->rule   != PWSMatch::MR_PRESENT &&
        cur_filterentry->rule   != PWSMatch::MR_NOTPRESENT &&
        cur_filterentry->fdate1 == (time_t)0 &&
        cur_filterentry->fdate2 == (time_t)0)
      cur_filterentry->fdatetype = 1; // Relative Date
    if (m_type == DFTYPE_MAIN) {
      cur_filter->num_Mactive++;
      cur_filter->vMfldata.push_back(*cur_filterentry);
    } else if (m_type == DFTYPE_PWHISTORY) {
      cur_filter->num_Hactive++;
      cur_filter->vHfldata.push_back(*cur_filterentry);
    } else if (m_type == DFTYPE_PWPOLICY) {
      cur_filter->num_Pactive++;
      cur_filter->vPfldata.push_back(*cur_filterentry);
    }
    delete cur_filterentry;
  }
{'fraction_non_alphanumeric': 0.12656322371468273, 'fraction_numerical': 0.00816350162112089, 'mean_word_length': 3.3031888390632784, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 44}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2767088', 'n_tokens_mistral': 6232, 'n_tokens_neox': 6140, 'n_words': 1256}