/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2012 University of California, Los Angeles
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program 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 a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
 *         卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
 *	   Alexander Afanasyev <alexander.afanasyev@ucla.edu>
 */

#include "sync-digest.h"
#include <string.h>

#include "ns3/assert.h"
#include <boost/exception/errinfo_at_line.hpp>

using namespace boost;

namespace Sync {

Digest::Digest ()
  : m_buffer (0)
  , m_hashLength (0)
{
  m_context = EVP_MD_CTX_create ();

  reset ();
}

Digest::~Digest ()
{
  if (m_buffer != 0)
    delete [] m_buffer;

  EVP_MD_CTX_destroy (m_context);
}

void
Digest::reset ()
{
  if (m_buffer != 0)
    {
      delete [] m_buffer;
      m_buffer = 0;
    }

  int ok = EVP_DigestInit_ex (m_context, EVP_sha1 (), 0);
  if (!ok)
    throw DigestCalculationError () << errinfo_at_line (__LINE__);
}


void
Digest::finalize ()
{
  if (m_buffer != 0) return;

  m_buffer = new uint8_t [HASH_SIZE];

  int ok = EVP_DigestFinal_ex (m_context,
			       m_buffer, &m_hashLength);
  if (!ok)
    throw DigestCalculationError () << errinfo_at_line (__LINE__);
}
  
std::size_t
Digest::getHash ()
{
  if (m_buffer == 0)
    finalize ();

  NS_ASSERT (sizeof (std::size_t) <= m_hashLength);
  
  // just getting first sizeof(std::size_t) bytes
  // not ideal, but should work pretty well
  return reinterpret_cast<std::size_t> (m_buffer);
}

bool
Digest::operator == (Digest &digest)
{
  if (m_buffer == 0)
    finalize ();

  if (digest.m_buffer == 0)
    digest.finalize ();
  
  NS_ASSERT (m_hashLength == digest.m_hashLength);

  return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
}


void
Digest::update (const uint8_t *buffer, size_t size)
{
  // cannot update Digest when it has been finalized
  if (m_buffer != 0)
    throw DigestCalculationError () << errinfo_at_line (__LINE__);

  bool ok = EVP_DigestUpdate (m_context, buffer, size);
  if (!ok)
    throw DigestCalculationError () << errinfo_at_line (__LINE__);
}


Digest &
Digest::operator << (const Digest &src)
{
  if (src.m_buffer == 0) 
    throw DigestCalculationError () << errinfo_at_line (__LINE__);

  update (src.m_buffer, src.m_hashLength);

  return *this;
}


} // Sync

