chasher.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CHASHER_H
00019 #define __CHASHER_H
00020
00021 #include <string.h>
00022 #include <stdlib.h>
00023 #include "simkerneldefs.h"
00024 #include "cobject.h"
00025 #include "cexception.h"
00026 #include "platdep/intxtypes.h"
00027
00028 NAMESPACE_BEGIN
00029
00030
00039 class SIM_API cHasher : noncopyable
00040 {
00041 private:
00042 uint32 value;
00043
00044 void merge(uint32 x) {
00045
00046 uint32 carry = (value & 0x80000000U) >> 31;
00047 value = ((value<<1)|carry) ^ x;
00048 }
00049
00050 void merge2(uint64 x) {
00051 merge((uint32)x);
00052 merge((uint32)(x>>32));
00053 }
00054
00055 public:
00059 cHasher() {ASSERT(sizeof(uint32)==4); ASSERT(sizeof(double)==8); value = 0;}
00060
00063 void reset() {value = 0;}
00064 void add(const char *p, size_t length);
00065 void add(char d) {merge((uint32)d);}
00066 void add(short d) {merge((uint32)d);}
00067 void add(int d) {merge((uint32)d);}
00068 void add(long d) {int64 tmp=d; merge2((uint64)tmp);}
00069 void add(opp_long_long d) {merge2((uint64)d);}
00070 void add(unsigned char d) {merge((uint32)d);}
00071 void add(unsigned short d) {merge((uint32)d);}
00072 void add(unsigned int d) {merge((uint32)d);}
00073 void add(unsigned long d) {uint64 tmp=d; merge2(tmp);}
00074 void add(opp_unsigned_long_long d) {merge2(d);}
00075
00076 void add(double d) {union _ {double d; uint64 i;}; merge2(((union _ *)&d)->i);}
00077 void add(const char *s) {if (s) add(s, strlen(s)+1); else add(0);}
00079
00085 uint32 getHash() const {return value;}
00086
00092 uint32 parse(const char *fingerprint) const;
00093
00097 bool equals(const char *fingerprint) const;
00098
00102 std::string str() const;
00104 };
00105
00106 NAMESPACE_END
00107
00108 #endif
00109
00110