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 | { |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 25 | class Hash |
| 26 | { |
| 27 | public: |
| 28 | static unsigned char _origin; |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 29 | static ptr_lib::shared_ptr<Hash> Origin; |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 30 | |
| 31 | Hash () |
| 32 | : m_buf(0) |
| 33 | , m_length(0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | Hash (const void *buf, unsigned int length) |
| 38 | : m_length (length) |
| 39 | { |
| 40 | if (m_length != 0) |
| 41 | { |
| 42 | m_buf = new unsigned char [length]; |
| 43 | memcpy (m_buf, buf, length); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | Hash (const Hash &otherHash) |
| 48 | : m_length (otherHash.m_length) |
| 49 | { |
| 50 | if (m_length != 0) |
| 51 | { |
| 52 | m_buf = new unsigned char [m_length]; |
| 53 | memcpy (m_buf, otherHash.m_buf, otherHash.m_length); |
| 54 | } |
| 55 | } |
| 56 | |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 57 | static ptr_lib::shared_ptr<Hash> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 58 | FromString (const std::string &hashInTextEncoding); |
| 59 | |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 60 | static ptr_lib::shared_ptr<Hash> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 61 | FromFileContent (const char *fileName); |
| 62 | |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 63 | static ptr_lib::shared_ptr<Hash> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 64 | FromBytes (const ndn::Bytes &bytes); |
| 65 | |
| 66 | ~Hash () |
| 67 | { |
| 68 | if (m_length != 0) |
| 69 | delete [] m_buf; |
| 70 | } |
| 71 | |
| 72 | Hash & |
| 73 | operator = (const Hash &otherHash) |
| 74 | { |
| 75 | if (m_length != 0) |
| 76 | delete [] m_buf; |
| 77 | |
| 78 | m_length = otherHash.m_length; |
| 79 | if (m_length != 0) |
| 80 | { |
| 81 | m_buf = new unsigned char [m_length]; |
| 82 | memcpy (m_buf, otherHash.m_buf, otherHash.m_length); |
| 83 | } |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | bool |
| 88 | IsZero () const |
| 89 | { |
| 90 | return m_length == 0 || |
| 91 | (m_length == 1 && m_buf[0] == 0); |
| 92 | } |
| 93 | |
| 94 | bool |
| 95 | operator == (const Hash &otherHash) const |
| 96 | { |
| 97 | if (m_length != otherHash.m_length) |
| 98 | return false; |
| 99 | |
| 100 | return memcmp (m_buf, otherHash.m_buf, m_length) == 0; |
| 101 | } |
| 102 | |
| 103 | bool operator < (const Hash &otherHash) const |
| 104 | { |
| 105 | if (m_length < otherHash.m_length) |
| 106 | return true; |
| 107 | |
| 108 | if (m_length > otherHash.m_length) |
| 109 | return false; |
| 110 | |
| 111 | for (unsigned int i = 0; i < m_length; i++) |
| 112 | { |
| 113 | if (m_buf [i] < otherHash.m_buf [i]) |
| 114 | return true; |
| 115 | |
| 116 | if (m_buf [i] > otherHash.m_buf [i]) |
| 117 | return false; |
| 118 | |
| 119 | // if equal, continue |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | const void * |
| 126 | GetHash () const |
| 127 | { |
| 128 | return m_buf; |
| 129 | } |
| 130 | |
| 131 | unsigned int |
| 132 | GetHashBytes () const |
| 133 | { |
| 134 | return m_length; |
| 135 | } |
| 136 | |
| 137 | std::string |
| 138 | shortHash () const; |
| 139 | |
| 140 | private: |
| 141 | unsigned char *m_buf; |
| 142 | unsigned int m_length; |
| 143 | |
| 144 | friend std::ostream & |
| 145 | operator << (std::ostream &os, const Hash &digest); |
| 146 | }; |
| 147 | |
| 148 | namespace Error { |
| 149 | struct HashConversion : virtual boost::exception, virtual std::exception { }; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | std::ostream & |
| 154 | operator << (std::ostream &os, const Hash &digest); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | #endif // NDN_HASH_H |