blob: b5d96aaab77aa82e45ef985b72598996e53e26e1 [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- 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
23namespace ndn
24{
Jeff Thompsonfa306642013-06-17 15:06:57 -070025class Hash
26{
27public:
28 static unsigned char _origin;
Jeff Thompson29310402013-06-19 13:32:26 -070029 static ptr_lib::shared_ptr<Hash> Origin;
Jeff Thompsonfa306642013-06-17 15:06:57 -070030
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 Thompson29310402013-06-19 13:32:26 -070057 static ptr_lib::shared_ptr<Hash>
Jeff Thompsonfa306642013-06-17 15:06:57 -070058 FromString (const std::string &hashInTextEncoding);
59
Jeff Thompson29310402013-06-19 13:32:26 -070060 static ptr_lib::shared_ptr<Hash>
Jeff Thompsonfa306642013-06-17 15:06:57 -070061 FromFileContent (const char *fileName);
62
Jeff Thompson29310402013-06-19 13:32:26 -070063 static ptr_lib::shared_ptr<Hash>
Jeff Thompsonfa306642013-06-17 15:06:57 -070064 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
140private:
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
148namespace Error {
149struct HashConversion : virtual boost::exception, virtual std::exception { };
150}
151
152
153std::ostream &
154operator << (std::ostream &os, const Hash &digest);
155
156}
157
158#endif // NDN_HASH_H