<commit_before>// Copyright (C) 2010-2013 <NAME>.
// See the file COPYING for copying permission.

#include "hadesmem/pelib/import_dir_list.hpp"

#include <utility>

#include "hadesmem/detail/warning_disable_prefix.hpp"
#include <boost/assert.hpp>
#include <boost/optional.hpp>
#include "hadesmem/detail/warning_disable_suffix.hpp"

#include <windows.h>

#include "hadesmem/read.hpp"
#include "hadesmem/error.hpp"
#include "hadesmem/config.hpp"
#include "hadesmem/process.hpp"
#include "hadesmem/pelib/pe_file.hpp"
#include "hadesmem/pelib/import_dir.hpp"

namespace hadesmem
{

struct ImportDirIterator::Impl
{
  explicit Impl(Process const& process, PeFile const& pe_file) 
    HADESMEM_NOEXCEPT
    : process_(&process), 
    pe_file_(&pe_file), 
    import_dir_()
  { }

  Process const* process_;
  PeFile const* pe_file_;
  boost::optional<ImportDir> import_dir_;
};

ImportDirIterator::ImportDirIterator() HADESMEM_NOEXCEPT
  : impl_()
{ }

ImportDirIterator::ImportDirIterator(Process const& process, 
  PeFile const& pe_file)
  : impl_(new Impl(process, pe_file))
{
  try
  {
    impl_->import_dir_ = ImportDir(process, pe_file, nullptr);
  }
  catch (std::exception const& /*e*/)
  {
    // TODO: Check whether this is the right thing to do. We should only 
    // flag as the 'end' once we've actually reached the end of the list. If 
    // the iteration fails we should throw an exception.
    impl_.reset();
  }
}

ImportDirIterator::ImportDirIterator(ImportDirIterator const& other) 
  HADESMEM_NOEXCEPT
  : impl_(other.impl_)
{ }

ImportDirIterator& ImportDirIterator::operator=(ImportDirIterator const& other) 
  HADESMEM_NOEXCEPT
{
  impl_ = other.impl_;

  return *this;
}

ImportDirIterator::ImportDirIterator(ImportDirIterator&& other) 
  HADESMEM_NOEXCEPT
  : impl_(std::move(other.impl_))
{ }

ImportDirIterator& ImportDirIterator::operator=(ImportDirIterator&& other) 
  HADESMEM_NOEXCEPT
{
  impl_ = std::move(other.impl_);

  return *this;
}

ImportDirIterator::~ImportDirIterator()
{ }

ImportDirIterator::reference ImportDirIterator::operator*() const 
  HADESMEM_NOEXCEPT
{
  BOOST_ASSERT(impl_.get());
  return *impl_->import_dir_;
}

ImportDirIterator::pointer ImportDirIterator::operator->() const 
  HADESMEM_NOEXCEPT
{
  BOOST_ASSERT(impl_.get());
  return &*impl_->import_dir_;
}

ImportDirIterator& ImportDirIterator::operator++()
{
  try
  {
    BOOST_ASSERT(impl_.get());

    auto const cur_base = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
      impl_->import_dir_->GetBase());
    impl_->import_dir_ = ImportDir(*impl_->process_, *impl_->pe_file_, 
      cur_base + 1);

    if (!impl_->import_dir_->GetCharacteristics())
    {
      impl_.reset();
      return *this;
    }
  }
  catch (std::exception const& /*e*/)
  {
    // TODO: Check whether this is the right thing to do. We should only 
    // flag as the 'end' once we've actually reached the end of the list. If 
    // the iteration fails we should throw an exception.
    impl_.reset();
  }
  
  return *this;
}

ImportDirIterator ImportDirIterator::operator++(int)
{
  ImportDirIterator iter(*this);
  ++*this;
  return iter;
}

bool ImportDirIterator::operator==(ImportDirIterator const& other) const 
  HADESMEM_NOEXCEPT<commit_msg>* Fix for previous commit.<commit_after>// Copyright (C) 2010-2013 <NAME>.
// See the file COPYING for copying permission.

#include "hadesmem/pelib/import_dir_list.hpp"

#include <utility>

#include "hadesmem/detail/warning_disable_prefix.hpp"
#include <boost/assert.hpp>
#include <boost/optional.hpp>
#include "hadesmem/detail/warning_disable_suffix.hpp"

#include <windows.h>

#include "hadesmem/read.hpp"
#include "hadesmem/error.hpp"
#include "hadesmem/config.hpp"
#include "hadesmem/process.hpp"
#include "hadesmem/pelib/pe_file.hpp"
#include "hadesmem/pelib/import_dir.hpp"

namespace hadesmem
{

struct ImportDirIterator::Impl
{
  explicit Impl(Process const& process, PeFile const& pe_file) 
    HADESMEM_NOEXCEPT
    : process_(&process), 
    pe_file_(&pe_file), 
    import_dir_()
  { }

  Process const* process_;
  PeFile const* pe_file_;
  boost::optional<ImportDir> import_dir_;
};

ImportDirIterator::ImportDirIterator() HADESMEM_NOEXCEPT
  : impl_()
{ }

ImportDirIterator::ImportDirIterator(Process const& process, 
  PeFile const& pe_file)
  : impl_(new Impl(process, pe_file))
{
  try
  {
    impl_->import_dir_ = ImportDir(process, pe_file, nullptr);
  }
  catch (std::exception const& /*e*/)
  {
    // TODO: Check whether this is the right thing to do. We should only 
    // flag as the 'end' once we've actually reached the end of the list. If 
    // the iteration fails we should throw an exception.
    impl_.reset();
  }
}

ImportDirIterator::ImportDirIterator(ImportDirIterator const& other) 
  HADESMEM_NOEXCEPT
  : impl_(other.impl_)
{ }

ImportDirIterator& ImportDirIterator::operator=(ImportDirIterator const& other) 
  HADESMEM_NOEXCEPT
{
  impl_ = other.impl_;

  return *this;
}

ImportDirIterator::ImportDirIterator(ImportDirIterator&& other) 
  HADESMEM_NOEXCEPT
  : impl_(std::move(other.impl_))
{ }

ImportDirIterator& ImportDirIterator::operator=(ImportDirIterator&& other) 
  HADESMEM_NOEXCEPT
{
  impl_ = std::move(other.impl_);

  return *this;
}

ImportDirIterator::~ImportDirIterator()
{ }

ImportDirIterator::reference ImportDirIterator::operator*() const 
  HADESMEM_NOEXCEPT
{
  BOOST_ASSERT(impl_.get());
  return *impl_->import_dir_;
}

ImportDirIterator::pointer ImportDirIterator::operator->() const 
  HADESMEM_NOEXCEPT
{
  BOOST_ASSERT(impl_.get());
  return &*impl_->import_dir_;
}

ImportDirIterator& ImportDirIterator::operator++()
{
  try
  {
    BOOST_ASSERT(impl_.get());

    auto const cur_base = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
      impl_->import_dir_->GetBase());
    impl_->import_dir_ = ImportDir(*impl_->process_, *impl_->pe_file_, 
      cur_base + 1);

    if (!impl_->import_dir_->GetOriginalFirstThunk())
    {
      impl_.reset();
      return *this;
    }
  }
  catch (std::exception const& /*e*/)
  {
    // TODO: Check whether this is the right thing to do. We should only 
    // flag as the 'end' once we've actually reached the end of the list. If 
    // the iteration fails we should throw an exception.
    impl_.reset();
  }
  
  return *this;
}

ImportDirIterator ImportDirIterator::operator++(int)
{
  ImportDirIterator iter(*this);
  ++*this;
  return iter;
}

bool ImportDirIterator::operator==(ImportDirIterator const& other) const 
  HADESMEM_NOEXCEPT
{'fraction_non_alphanumeric': 0.12288266091777025, 'fraction_numerical': 0.002771789344009855, 'mean_word_length': 4.476391231028668, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6261059', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2125, 'n_words': 469}