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