00001 // Filename: hashVal.I 00002 // Created by: drose (14Nov00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: HashVal::Constructor 00018 // Access: Published 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE HashVal:: 00022 HashVal() { 00023 _hv[0] = _hv[1] = _hv[2] = _hv[3] = 0; 00024 } 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: HashVal::Copy Constructor 00028 // Access: Published 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 INLINE HashVal:: 00032 HashVal(const HashVal ©) { 00033 _hv[0] = copy._hv[0]; 00034 _hv[1] = copy._hv[1]; 00035 _hv[2] = copy._hv[2]; 00036 _hv[3] = copy._hv[3]; 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: HashVal::Copy Assignment Operator 00041 // Access: Published 00042 // Description: 00043 //////////////////////////////////////////////////////////////////// 00044 INLINE void HashVal:: 00045 operator = (const HashVal ©) { 00046 _hv[0] = copy._hv[0]; 00047 _hv[1] = copy._hv[1]; 00048 _hv[2] = copy._hv[2]; 00049 _hv[3] = copy._hv[3]; 00050 } 00051 00052 //////////////////////////////////////////////////////////////////// 00053 // Function: HashVal::operator == 00054 // Access: Published 00055 // Description: 00056 //////////////////////////////////////////////////////////////////// 00057 INLINE bool HashVal:: 00058 operator == (const HashVal &other) const { 00059 return (_hv[0] == other._hv[0] && 00060 _hv[1] == other._hv[1] && 00061 _hv[2] == other._hv[2] && 00062 _hv[3] == other._hv[3]); 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: HashVal::operator != 00067 // Access: Published 00068 // Description: 00069 //////////////////////////////////////////////////////////////////// 00070 INLINE bool HashVal:: 00071 operator != (const HashVal &other) const { 00072 return !operator == (other); 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: HashVal::operator < 00077 // Access: Published 00078 // Description: 00079 //////////////////////////////////////////////////////////////////// 00080 INLINE bool HashVal:: 00081 operator < (const HashVal &other) const { 00082 return compare_to(other) < 0; 00083 } 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: HashVal::compare_to 00087 // Access: Published 00088 // Description: 00089 //////////////////////////////////////////////////////////////////// 00090 INLINE int HashVal:: 00091 compare_to(const HashVal &other) const { 00092 if (_hv[0] != other._hv[0]) { 00093 return (int)_hv[0] - (int)other._hv[0]; 00094 } 00095 if (_hv[1] != other._hv[1]) { 00096 return (int)_hv[1] - (int)other._hv[1]; 00097 } 00098 if (_hv[2] != other._hv[2]) { 00099 return (int)_hv[2] - (int)other._hv[2]; 00100 } 00101 return (int)_hv[3] - (int)other._hv[3]; 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: HashVal::merge_with 00106 // Access: Published 00107 // Description: Generates a new HashVal representing the xor of this 00108 // one and the other one. 00109 //////////////////////////////////////////////////////////////////// 00110 INLINE void HashVal:: 00111 merge_with(const HashVal &other) { 00112 _hv[0] ^= other._hv[0]; 00113 _hv[1] ^= other._hv[1]; 00114 _hv[2] ^= other._hv[2]; 00115 _hv[3] ^= other._hv[3]; 00116 } 00117 00118 //////////////////////////////////////////////////////////////////// 00119 // Function: HashVal::output_dec 00120 // Access: Published 00121 // Description: Outputs the HashVal as four unsigned decimal 00122 // integers. 00123 //////////////////////////////////////////////////////////////////// 00124 INLINE void HashVal:: 00125 output_dec(ostream &out) const { 00126 out << _hv[0] << " " << _hv[1] << " " << _hv[2] << " " << _hv[3]; 00127 } 00128 00129 //////////////////////////////////////////////////////////////////// 00130 // Function: HashVal::input 00131 // Access: Published 00132 // Description: Inputs the HashVal as four unsigned decimal integers. 00133 //////////////////////////////////////////////////////////////////// 00134 INLINE void HashVal:: 00135 input_dec(istream &in) { 00136 in >> _hv[0] >> _hv[1] >> _hv[2] >> _hv[3]; 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: HashVal::output 00141 // Access: Published 00142 // Description: 00143 //////////////////////////////////////////////////////////////////// 00144 INLINE void HashVal:: 00145 output(ostream &out) const { 00146 output_hex(out); 00147 } 00148 00149 //////////////////////////////////////////////////////////////////// 00150 // Function: HashVal::write_datagram 00151 // Access: Published 00152 // Description: 00153 //////////////////////////////////////////////////////////////////// 00154 INLINE void HashVal:: 00155 write_datagram(Datagram &destination) const { 00156 destination.add_uint32(_hv[0]); 00157 destination.add_uint32(_hv[1]); 00158 destination.add_uint32(_hv[2]); 00159 destination.add_uint32(_hv[3]); 00160 } 00161 00162 //////////////////////////////////////////////////////////////////// 00163 // Function: HashVal::read_datagram 00164 // Access: Published 00165 // Description: 00166 //////////////////////////////////////////////////////////////////// 00167 INLINE void HashVal:: 00168 read_datagram(DatagramIterator &source) { 00169 _hv[0] = source.get_uint32(); 00170 _hv[1] = source.get_uint32(); 00171 _hv[2] = source.get_uint32(); 00172 _hv[3] = source.get_uint32(); 00173 } 00174 00175 //////////////////////////////////////////////////////////////////// 00176 // Function: HashVal::write_stream 00177 // Access: Published 00178 // Description: 00179 //////////////////////////////////////////////////////////////////// 00180 INLINE void HashVal:: 00181 write_stream(StreamWriter &destination) const { 00182 destination.add_uint32(_hv[0]); 00183 destination.add_uint32(_hv[1]); 00184 destination.add_uint32(_hv[2]); 00185 destination.add_uint32(_hv[3]); 00186 } 00187 00188 //////////////////////////////////////////////////////////////////// 00189 // Function: HashVal::read_stream 00190 // Access: Published 00191 // Description: 00192 //////////////////////////////////////////////////////////////////// 00193 INLINE void HashVal:: 00194 read_stream(StreamReader &source) { 00195 _hv[0] = source.get_uint32(); 00196 _hv[1] = source.get_uint32(); 00197 _hv[2] = source.get_uint32(); 00198 _hv[3] = source.get_uint32(); 00199 } 00200 00201 #ifdef HAVE_OPENSSL 00202 //////////////////////////////////////////////////////////////////// 00203 // Function: HashVal::hash_ramfile 00204 // Access: Published 00205 // Description: Generates the hash value by hashing the indicated 00206 // data. This method is only defined if we have the 00207 // OpenSSL library (which provides md5 functionality) 00208 // available. 00209 //////////////////////////////////////////////////////////////////// 00210 INLINE void HashVal:: 00211 hash_ramfile(const Ramfile &ramfile) { 00212 hash_buffer(ramfile._data.data(), ramfile._data.length()); 00213 } 00214 00215 //////////////////////////////////////////////////////////////////// 00216 // Function: HashVal::hash_string 00217 // Access: Published 00218 // Description: Generates the hash value by hashing the indicated 00219 // data. This method is only defined if we have the 00220 // OpenSSL library (which provides md5 functionality) 00221 // available. 00222 //////////////////////////////////////////////////////////////////// 00223 INLINE void HashVal:: 00224 hash_string(const string &data) { 00225 hash_buffer(data.data(), data.length()); 00226 } 00227 #endif // HAVE_OPENSSL 00228 00229 //////////////////////////////////////////////////////////////////// 00230 // Function: HashVal::tohex 00231 // Access: Private, Static 00232 // Description: Converts a single nibble to a hex digit. 00233 //////////////////////////////////////////////////////////////////// 00234 INLINE char HashVal:: 00235 tohex(unsigned int nibble) { 00236 nibble &= 0xf; 00237 if (nibble < 10) { 00238 return nibble + '0'; 00239 } 00240 return nibble - 10 + 'a'; 00241 } 00242 00243 //////////////////////////////////////////////////////////////////// 00244 // Function: HashVal::fromhex 00245 // Access: Private, Static 00246 // Description: Converts a single hex digit to a numerical value. 00247 //////////////////////////////////////////////////////////////////// 00248 INLINE unsigned int HashVal:: 00249 fromhex(char digit) { 00250 if (isdigit(digit)) { 00251 return digit - '0'; 00252 } else { 00253 return tolower(digit) - 'a' + 10; 00254 } 00255 } 00256 00257 00258 INLINE ostream &operator << (ostream &out, const HashVal &hv) { 00259 hv.output(out); 00260 return out; 00261 }