/* -*- 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 <boost/assert.hpp>
#include <boost/exception/errinfo_at_line.hpp>

// for printing, may be disabled in optimized build
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>

using namespace boost;
using namespace std;

using namespace boost::archive::iterators;
typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > base64_t;

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 ();

  BOOST_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 ();
  
  BOOST_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;
}


std::ostream&
Digest::print (std::ostream &os) const
{
  BOOST_ASSERT (m_hashLength != 0);
  
  ostreambuf_iterator<char> out_it (os); // ostream iterator
  // need to encode to base64
  copy (base64_t (reinterpret_cast<const char*> (m_buffer)),
        base64_t (reinterpret_cast<const char*> (m_buffer+m_hashLength)),
        out_it);

  return os;
}

std::ostream &
operator << (std::ostream &os, const Digest &digest)
{
  digest.print (os);
  return os;
}


} // Sync

