forked from cawka/ndn.cxx
diff --git a/ndn-cpp/helpers/hash.cc b/ndn-cpp/helpers/hash.cc
new file mode 100644
index 0000000..0ecf927
--- /dev/null
+++ b/ndn-cpp/helpers/hash.cc
@@ -0,0 +1,130 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Alexander Afanasyev
+ *                     Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include "hash.h"
+#include "ndn-cpp/helpers/uri.h"
+
+#include <boost/lexical_cast.hpp>
+#include <openssl/evp.h>
+#include <fstream>
+
+using namespace boost;
+using namespace std;
+
+// 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
+
+namespace ndn
+{
+
+std::ostream &
+operator << (std::ostream &os, const Hash &hash)
+{
+  if (hash.m_length == 0)
+    return os;
+
+  ostreambuf_iterator<char> out_it (os); // ostream iterator
+  // need to encode to base64
+  copy (detail::string_from_binary (reinterpret_cast<const char*> (hash.m_buf)),
+        detail::string_from_binary (reinterpret_cast<const char*> (hash.m_buf+hash.m_length)),
+        out_it);
+
+  return os;
+}
+
+std::string
+Hash::shortHash () const
+{
+  return lexical_cast<string> (*this).substr (0, 10);
+}
+
+
+unsigned char Hash::_origin = 0;
+HashPtr Hash::Origin(new Hash(&Hash::_origin, sizeof(unsigned char)));
+
+HashPtr
+Hash::FromString (const std::string &hashInTextEncoding)
+{
+  HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+
+  if (hashInTextEncoding.size () == 0)
+    {
+      return retval;
+    }
+
+  if (hashInTextEncoding.size () > EVP_MAX_MD_SIZE * 2)
+    {
+      cerr << "Input hash is too long. Returning an empty hash" << endl;
+      return retval;
+    }
+
+  retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+  unsigned char *end = copy (detail::string_to_binary (hashInTextEncoding.begin ()),
+                             detail::string_to_binary (hashInTextEncoding.end ()),
+                             retval->m_buf);
+
+  retval->m_length = end - retval->m_buf;
+
+  return retval;
+}
+
+HashPtr
+Hash::FromFileContent (const char *filename)
+{
+  HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+  retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+  EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
+  EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
+
+  ifstream iff (filename, std::ios::in | std::ios::binary);
+  while (iff.good ())
+    {
+      char buf[1024];
+      iff.read (buf, 1024);
+      EVP_DigestUpdate (hash_context, buf, iff.gcount ());
+    }
+
+  retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+  EVP_DigestFinal_ex (hash_context,
+                      retval->m_buf, &retval->m_length);
+
+  EVP_MD_CTX_destroy (hash_context);
+
+  return retval;
+}
+
+HashPtr
+Hash::FromBytes (const ndn::Bytes &bytes)
+{
+  HashPtr retval = make_shared<Hash> (reinterpret_cast<void*> (0), 0);
+  retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+  EVP_MD_CTX *hash_context = EVP_MD_CTX_create ();
+  EVP_DigestInit_ex (hash_context, HASH_FUNCTION (), 0);
+
+  // not sure whether it's bad to do so if bytes.size is huge
+  EVP_DigestUpdate(hash_context, ndn::head(bytes), bytes.size());
+
+  retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
+
+  EVP_DigestFinal_ex (hash_context,
+                      retval->m_buf, &retval->m_length);
+
+  EVP_MD_CTX_destroy (hash_context);
+
+  return retval;
+}
+
+} // ndn
diff --git a/ndn-cpp/helpers/hash.h b/ndn-cpp/helpers/hash.h
new file mode 100644
index 0000000..1652f2b
--- /dev/null
+++ b/ndn-cpp/helpers/hash.h
@@ -0,0 +1,162 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Alexander Afanasyev
+ *                     Zhenkai Zhu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#ifndef NDN_HASH_H
+#define NDN_HASH_H
+
+#include <string.h>
+#include <iostream>
+#include <boost/shared_ptr.hpp>
+#include <boost/exception/all.hpp>
+
+#include "ndn-cpp/common.h"
+
+namespace ndn
+{
+
+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 char *fileName);
+
+  static HashPtr
+  FromBytes (const ndn::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 // NDN_HASH_H
diff --git a/ndn-cpp/helpers/uri.h b/ndn-cpp/helpers/uri.h
new file mode 100644
index 0000000..3fbda7c
--- /dev/null
+++ b/ndn-cpp/helpers/uri.h
@@ -0,0 +1,155 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ *                     Alexander Afanasyev
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#ifndef NDN_DETAIL_URI_H
+#define NDN_DETAIL_URI_H
+
+#include "ndn-cpp/error.h"
+
+#include <boost/archive/iterators/transform_width.hpp>
+#include <boost/iterator/transform_iterator.hpp>
+
+namespace ndn
+{
+
+namespace detail
+{
+
+static const bool ESCAPE_CHARACTER [256] = {
+  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 26
+  1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 53
+  0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 107
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, // 134
+  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 161
+  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 188
+  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 215
+  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 242
+  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 255
+};
+
+/// @cond include_hidden
+template<class CharType>
+struct hex_from_4_bit
+{
+  typedef CharType result_type;
+  CharType operator () (CharType ch) const
+  {
+    const char lookup_table [16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+    // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
+    BOOST_ASSERT (ch < 16);
+    return lookup_table[static_cast<size_t>(ch)];
+  }
+};
+
+typedef boost::transform_iterator<hex_from_4_bit<std::string::const_iterator::value_type>,
+                                  boost::archive::iterators::transform_width<std::string::const_iterator, 4, 8, std::string::const_iterator::value_type> > string_from_binary;
+
+
+
+template<class CharType>
+struct hex_to_4_bit
+{
+  typedef CharType result_type;
+  CharType operator () (CharType ch) const
+  {
+    const signed char lookup_table [] = {
+      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+      0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
+      -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+      -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+      -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
+    };
+
+    signed char value = -1;
+    if ((unsigned)ch < 128)
+      value = lookup_table [(unsigned)ch];
+    if (value == -1)
+      boost::throw_exception (error::StringTransform ());
+
+    return value;
+  }
+};
+
+typedef boost::archive::iterators::transform_width<boost::transform_iterator<hex_to_4_bit<std::string::const_iterator::value_type>, std::string::const_iterator>, 8, 4> string_to_binary;
+/// @endcond
+
+} // detail
+
+/**
+ * @brief A helper class to convert to/from URI
+ */
+class Uri
+{
+public:
+  template<class Iterator1, class Iterator2>
+  inline static void
+  fromEscaped (Iterator1 begin, Iterator1 end, Iterator2 inserter)
+  {
+    Iterator1 i = begin;
+    while (i != end)
+      {
+        if (*i == '%')
+          {
+            try
+              {
+                ++i;
+                Iterator1 j = i;
+                advance (i, 2);
+              
+                std::copy (detail::string_to_binary (j), detail::string_to_binary (i), inserter);
+              }
+            catch (ndn::error::StringTransform &e)
+              {
+                BOOST_THROW_EXCEPTION (error::Uri ()
+                                       << error::pos (std::distance (i, begin)));
+              }
+          }
+        else if (!detail::ESCAPE_CHARACTER[static_cast<unsigned char> (*i)])
+          {
+            *inserter = *i;
+            ++inserter; ++i;
+          }
+        else
+          {
+            BOOST_THROW_EXCEPTION (error::Uri ()
+                                   << error::pos (std::distance (i, begin)));
+          }
+      }
+  }
+
+  template<class Iterator1, class Iterator2>
+  inline static void
+  toEscaped (Iterator1 begin, Iterator1 end, Iterator2 inserter)
+  {
+    const char lookup_table [16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+    
+    for (Iterator1 i = begin; i != end; i++)
+      {
+        if (detail::ESCAPE_CHARACTER[static_cast<unsigned char> (*i)])
+          {
+            *inserter = '%';         ++inserter;
+            *inserter = lookup_table [(*i >> 4) & 0x0F]; ++inserter;
+            *inserter = lookup_table [(*i & 0x0F)];      ++inserter;
+          }
+        else
+          {
+            *inserter = *i; ++inserter;
+          }
+      }
+  }
+};
+
+} // ndn
+
+#endif // NDN_DETAIL_URI_H