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