Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #ifndef NDN_HASH_H |
| 14 | #define NDN_HASH_H |
| 15 | |
| 16 | #include <string.h> |
| 17 | #include <iostream> |
| 18 | #include <boost/shared_ptr.hpp> |
| 19 | #include <boost/exception/all.hpp> |
| 20 | |
| 21 | #include "ndn-cpp/common.h" |
| 22 | |
| 23 | namespace ndn |
| 24 | { |
| 25 | |
| 26 | class Hash; |
| 27 | typedef boost::shared_ptr<Hash> HashPtr; |
Jeff Thompson | 4454bf7 | 2013-06-18 13:33:12 -0700 | [diff] [blame] | 28 | typedef boost::shared_ptr<const Hash> ConstHashPtr; |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 29 | |
| 30 | class Hash |
| 31 | { |
| 32 | public: |
| 33 | static unsigned char _origin; |
| 34 | static HashPtr Origin; |
| 35 | |
| 36 | Hash () |
| 37 | : m_buf(0) |
| 38 | , m_length(0) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | Hash (const void *buf, unsigned int length) |
| 43 | : m_length (length) |
| 44 | { |
| 45 | if (m_length != 0) |
| 46 | { |
| 47 | m_buf = new unsigned char [length]; |
| 48 | memcpy (m_buf, buf, length); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | Hash (const Hash &otherHash) |
| 53 | : m_length (otherHash.m_length) |
| 54 | { |
| 55 | if (m_length != 0) |
| 56 | { |
| 57 | m_buf = new unsigned char [m_length]; |
| 58 | memcpy (m_buf, otherHash.m_buf, otherHash.m_length); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static HashPtr |
| 63 | FromString (const std::string &hashInTextEncoding); |
| 64 | |
| 65 | static HashPtr |
| 66 | FromFileContent (const char *fileName); |
| 67 | |
| 68 | static HashPtr |
| 69 | FromBytes (const ndn::Bytes &bytes); |
| 70 | |
| 71 | ~Hash () |
| 72 | { |
| 73 | if (m_length != 0) |
| 74 | delete [] m_buf; |
| 75 | } |
| 76 | |
| 77 | Hash & |
| 78 | operator = (const Hash &otherHash) |
| 79 | { |
| 80 | if (m_length != 0) |
| 81 | delete [] m_buf; |
| 82 | |
| 83 | m_length = otherHash.m_length; |
| 84 | if (m_length != 0) |
| 85 | { |
| 86 | m_buf = new unsigned char [m_length]; |
| 87 | memcpy (m_buf, otherHash.m_buf, otherHash.m_length); |
| 88 | } |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | IsZero () const |
| 94 | { |
| 95 | return m_length == 0 || |
| 96 | (m_length == 1 && m_buf[0] == 0); |
| 97 | } |
| 98 | |
| 99 | bool |
| 100 | operator == (const Hash &otherHash) const |
| 101 | { |
| 102 | if (m_length != otherHash.m_length) |
| 103 | return false; |
| 104 | |
| 105 | return memcmp (m_buf, otherHash.m_buf, m_length) == 0; |
| 106 | } |
| 107 | |
| 108 | bool operator < (const Hash &otherHash) const |
| 109 | { |
| 110 | if (m_length < otherHash.m_length) |
| 111 | return true; |
| 112 | |
| 113 | if (m_length > otherHash.m_length) |
| 114 | return false; |
| 115 | |
| 116 | for (unsigned int i = 0; i < m_length; i++) |
| 117 | { |
| 118 | if (m_buf [i] < otherHash.m_buf [i]) |
| 119 | return true; |
| 120 | |
| 121 | if (m_buf [i] > otherHash.m_buf [i]) |
| 122 | return false; |
| 123 | |
| 124 | // if equal, continue |
| 125 | } |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | const void * |
| 131 | GetHash () const |
| 132 | { |
| 133 | return m_buf; |
| 134 | } |
| 135 | |
| 136 | unsigned int |
| 137 | GetHashBytes () const |
| 138 | { |
| 139 | return m_length; |
| 140 | } |
| 141 | |
| 142 | std::string |
| 143 | shortHash () const; |
| 144 | |
| 145 | private: |
| 146 | unsigned char *m_buf; |
| 147 | unsigned int m_length; |
| 148 | |
| 149 | friend std::ostream & |
| 150 | operator << (std::ostream &os, const Hash &digest); |
| 151 | }; |
| 152 | |
| 153 | namespace Error { |
| 154 | struct HashConversion : virtual boost::exception, virtual std::exception { }; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | std::ostream & |
| 159 | operator << (std::ostream &os, const Hash &digest); |
| 160 | |
| 161 | } |
| 162 | |
| 163 | #endif // NDN_HASH_H |