Panda3D
hashVal.I
1 // Filename: hashVal.I
2 // Created by: drose (14Nov00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: HashVal::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE HashVal::
22 HashVal() {
23  _hv[0] = _hv[1] = _hv[2] = _hv[3] = 0;
24 }
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: HashVal::Copy Constructor
28 // Access: Published
29 // Description:
30 ////////////////////////////////////////////////////////////////////
31 INLINE HashVal::
32 HashVal(const HashVal &copy) {
33  _hv[0] = copy._hv[0];
34  _hv[1] = copy._hv[1];
35  _hv[2] = copy._hv[2];
36  _hv[3] = copy._hv[3];
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: HashVal::Copy Assignment Operator
41 // Access: Published
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 INLINE void HashVal::
45 operator = (const HashVal &copy) {
46  _hv[0] = copy._hv[0];
47  _hv[1] = copy._hv[1];
48  _hv[2] = copy._hv[2];
49  _hv[3] = copy._hv[3];
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: HashVal::operator ==
54 // Access: Published
55 // Description:
56 ////////////////////////////////////////////////////////////////////
57 INLINE bool HashVal::
58 operator == (const HashVal &other) const {
59  return (_hv[0] == other._hv[0] &&
60  _hv[1] == other._hv[1] &&
61  _hv[2] == other._hv[2] &&
62  _hv[3] == other._hv[3]);
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: HashVal::operator !=
67 // Access: Published
68 // Description:
69 ////////////////////////////////////////////////////////////////////
70 INLINE bool HashVal::
71 operator != (const HashVal &other) const {
72  return !operator == (other);
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: HashVal::operator <
77 // Access: Published
78 // Description:
79 ////////////////////////////////////////////////////////////////////
80 INLINE bool HashVal::
81 operator < (const HashVal &other) const {
82  return compare_to(other) < 0;
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: HashVal::compare_to
87 // Access: Published
88 // Description:
89 ////////////////////////////////////////////////////////////////////
90 INLINE int HashVal::
91 compare_to(const HashVal &other) const {
92  if (_hv[0] != other._hv[0]) {
93  return (int)_hv[0] - (int)other._hv[0];
94  }
95  if (_hv[1] != other._hv[1]) {
96  return (int)_hv[1] - (int)other._hv[1];
97  }
98  if (_hv[2] != other._hv[2]) {
99  return (int)_hv[2] - (int)other._hv[2];
100  }
101  return (int)_hv[3] - (int)other._hv[3];
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: HashVal::merge_with
106 // Access: Published
107 // Description: Generates a new HashVal representing the xor of this
108 // one and the other one.
109 ////////////////////////////////////////////////////////////////////
110 INLINE void HashVal::
111 merge_with(const HashVal &other) {
112  _hv[0] ^= other._hv[0];
113  _hv[1] ^= other._hv[1];
114  _hv[2] ^= other._hv[2];
115  _hv[3] ^= other._hv[3];
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: HashVal::output_dec
120 // Access: Published
121 // Description: Outputs the HashVal as four unsigned decimal
122 // integers.
123 ////////////////////////////////////////////////////////////////////
124 INLINE void HashVal::
125 output_dec(ostream &out) const {
126  out << _hv[0] << " " << _hv[1] << " " << _hv[2] << " " << _hv[3];
127 }
128 
129 ////////////////////////////////////////////////////////////////////
130 // Function: HashVal::input
131 // Access: Published
132 // Description: Inputs the HashVal as four unsigned decimal integers.
133 ////////////////////////////////////////////////////////////////////
134 INLINE void HashVal::
135 input_dec(istream &in) {
136  in >> _hv[0] >> _hv[1] >> _hv[2] >> _hv[3];
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function: HashVal::output
141 // Access: Published
142 // Description:
143 ////////////////////////////////////////////////////////////////////
144 INLINE void HashVal::
145 output(ostream &out) const {
146  output_hex(out);
147 }
148 
149 ////////////////////////////////////////////////////////////////////
150 // Function: HashVal::write_datagram
151 // Access: Published
152 // Description:
153 ////////////////////////////////////////////////////////////////////
154 INLINE void HashVal::
155 write_datagram(Datagram &destination) const {
156  destination.add_uint32(_hv[0]);
157  destination.add_uint32(_hv[1]);
158  destination.add_uint32(_hv[2]);
159  destination.add_uint32(_hv[3]);
160 }
161 
162 ////////////////////////////////////////////////////////////////////
163 // Function: HashVal::read_datagram
164 // Access: Published
165 // Description:
166 ////////////////////////////////////////////////////////////////////
167 INLINE void HashVal::
168 read_datagram(DatagramIterator &source) {
169  _hv[0] = source.get_uint32();
170  _hv[1] = source.get_uint32();
171  _hv[2] = source.get_uint32();
172  _hv[3] = source.get_uint32();
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: HashVal::write_stream
177 // Access: Published
178 // Description:
179 ////////////////////////////////////////////////////////////////////
180 INLINE void HashVal::
181 write_stream(StreamWriter &destination) const {
182  destination.add_uint32(_hv[0]);
183  destination.add_uint32(_hv[1]);
184  destination.add_uint32(_hv[2]);
185  destination.add_uint32(_hv[3]);
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: HashVal::read_stream
190 // Access: Published
191 // Description:
192 ////////////////////////////////////////////////////////////////////
193 INLINE void HashVal::
194 read_stream(StreamReader &source) {
195  _hv[0] = source.get_uint32();
196  _hv[1] = source.get_uint32();
197  _hv[2] = source.get_uint32();
198  _hv[3] = source.get_uint32();
199 }
200 
201 #ifdef HAVE_OPENSSL
202 ////////////////////////////////////////////////////////////////////
203 // Function: HashVal::hash_ramfile
204 // Access: Published
205 // Description: Generates the hash value by hashing the indicated
206 // data. This method is only defined if we have the
207 // OpenSSL library (which provides md5 functionality)
208 // available.
209 ////////////////////////////////////////////////////////////////////
210 INLINE void HashVal::
211 hash_ramfile(const Ramfile &ramfile) {
212  hash_buffer(ramfile._data.data(), ramfile._data.length());
213 }
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: HashVal::hash_string
217 // Access: Published
218 // Description: Generates the hash value by hashing the indicated
219 // data. This method is only defined if we have the
220 // OpenSSL library (which provides md5 functionality)
221 // available.
222 ////////////////////////////////////////////////////////////////////
223 INLINE void HashVal::
224 hash_string(const string &data) {
225  hash_buffer(data.data(), data.length());
226 }
227 #endif // HAVE_OPENSSL
228 
229 ////////////////////////////////////////////////////////////////////
230 // Function: HashVal::tohex
231 // Access: Private, Static
232 // Description: Converts a single nibble to a hex digit.
233 ////////////////////////////////////////////////////////////////////
234 INLINE char HashVal::
235 tohex(unsigned int nibble) {
236  nibble &= 0xf;
237  if (nibble < 10) {
238  return nibble + '0';
239  }
240  return nibble - 10 + 'a';
241 }
242 
243 ////////////////////////////////////////////////////////////////////
244 // Function: HashVal::fromhex
245 // Access: Private, Static
246 // Description: Converts a single hex digit to a numerical value.
247 ////////////////////////////////////////////////////////////////////
248 INLINE unsigned int HashVal::
249 fromhex(char digit) {
250  if (isdigit(digit)) {
251  return digit - '0';
252  } else {
253  return tolower(digit) - 'a' + 10;
254  }
255 }
256 
257 
258 INLINE ostream &operator << (ostream &out, const HashVal &hv) {
259  hv.output(out);
260  return out;
261 }
A StreamWriter object is used to write sequential binary data directly to an ostream.
Definition: streamWriter.h:33
void input_dec(istream &in)
Inputs the HashVal as four unsigned decimal integers.
Definition: hashVal.I:135
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
Definition: streamReader.I:183
Stores a 128-bit value that represents the hashed contents (typically MD5) of a file or buffer...
Definition: hashVal.h:32
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
void output_hex(ostream &out) const
Outputs the HashVal as a 32-digit hexadecimal number.
Definition: hashVal.cxx:31
void merge_with(const HashVal &other)
Generates a new HashVal representing the xor of this one and the other one.
Definition: hashVal.I:111
An in-memory buffer specifically designed for downloading files to memory.
Definition: ramfile.h:27
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the stream.
Definition: streamWriter.I:170
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the datagram.
Definition: datagram.I:192
void output_dec(ostream &out) const
Outputs the HashVal as four unsigned decimal integers.
Definition: hashVal.I:125
A class to retrieve the individual data elements previously stored in a Datagram. ...
A class to read sequential binary data directly from an istream.
Definition: streamReader.h:30
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43