/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (c) 2013-2016, Regents of the University of California.
 *
 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
 *
 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received copies of the GNU General Public License along with
 * ChronoShare, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 *
 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
 */

#ifndef HASH_HELPER_H
#define HASH_HELPER_H

#include "ccnx-common.hpp"
#include <boost/exception/all.hpp>
#include <boost/filesystem.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <string.h>

// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
#define HASH_FUNCTION EVP_sha256

class Hash;
typedef boost::shared_ptr<Hash> HashPtr;

class Hash
{
public:
  static unsigned char _origin;
  static HashPtr Origin;

  Hash()
    : m_buf(0)
    , m_length(0)
  {
  }

  Hash(const void* buf, unsigned int length)
    : m_length(length)
  {
    if (m_length != 0) {
      m_buf = new unsigned char[length];
      memcpy(m_buf, buf, length);
    }
  }

  Hash(const Hash& otherHash)
    : m_length(otherHash.m_length)
  {
    if (m_length != 0) {
      m_buf = new unsigned char[m_length];
      memcpy(m_buf, otherHash.m_buf, otherHash.m_length);
    }
  }

  static HashPtr
  FromString(const std::string& hashInTextEncoding);

  static HashPtr
  FromFileContent(const boost::filesystem::path& fileName);

  static HashPtr
  FromBytes(const Ccnx::Bytes& bytes);

  ~Hash()
  {
    if (m_length != 0)
      delete[] m_buf;
  }

  Hash&
  operator=(const Hash& otherHash)
  {
    if (m_length != 0)
      delete[] m_buf;

    m_length = otherHash.m_length;
    if (m_length != 0) {
      m_buf = new unsigned char[m_length];
      memcpy(m_buf, otherHash.m_buf, otherHash.m_length);
    }
    return *this;
  }

  bool
  IsZero() const
  {
    return m_length == 0 || (m_length == 1 && m_buf[0] == 0);
  }

  bool
  operator==(const Hash& otherHash) const
  {
    if (m_length != otherHash.m_length)
      return false;

    return memcmp(m_buf, otherHash.m_buf, m_length) == 0;
  }

  bool
  operator<(const Hash& otherHash) const
  {
    if (m_length < otherHash.m_length)
      return true;

    if (m_length > otherHash.m_length)
      return false;

    for (unsigned int i = 0; i < m_length; i++) {
      if (m_buf[i] < otherHash.m_buf[i])
        return true;

      if (m_buf[i] > otherHash.m_buf[i])
        return false;

      // if equal, continue
    }

    return false;
  }

  const void*
  GetHash() const
  {
    return m_buf;
  }

  unsigned int
  GetHashBytes() const
  {
    return m_length;
  }

  std::string
  shortHash() const;

private:
  unsigned char* m_buf;
  unsigned int m_length;

  friend std::ostream&
  operator<<(std::ostream& os, const Hash& digest);
};

namespace Error {
struct HashConversion : virtual boost::exception, virtual std::exception
{
};
}


std::ostream&
operator<<(std::ostream& os, const Hash& digest);

#endif // HASH_STRING_CONVERTER_H
