1 #if defined(os_windows)
2 #include "common/h/ntHeaders.h"
5 #include "dynutil/h/util.h"
9 COMMON_EXPORT unsigned addrHashCommon(const Address &addr)
11 // inspired by hashs of string class
13 register unsigned result = 5381;
15 register Address accumulator = addr;
16 while (accumulator > 0) {
17 // We use 3 bits at a time from the address
18 result = (result << 4) + result + (accumulator & 0x07);
25 COMMON_EXPORT unsigned addrHash(const Address & iaddr)
27 return Dyninst::addrHashCommon(iaddr);
30 COMMON_EXPORT unsigned ptrHash(const void * iaddr)
32 return Dyninst::addrHashCommon((Address)iaddr);
35 COMMON_EXPORT unsigned ptrHash(void * iaddr)
37 return Dyninst::addrHashCommon((Address)iaddr);
40 COMMON_EXPORT unsigned addrHash4(const Address &iaddr)
42 // call when you know that the low 2 bits are 0 (meaning they contribute
43 // nothing to an even hash distribution)
44 return Dyninst::addrHashCommon(iaddr >> 2);
47 COMMON_EXPORT unsigned addrHash16(const Address &iaddr)
49 // call when you know that the low 4 bits are 0 (meaning they contribute
50 // nothing to an even hash distribution)
51 return Dyninst::addrHashCommon(iaddr >> 4);
54 // string hash grabbed from pdstring
55 unsigned stringhash(const std::string &s)
57 const char *str = s.c_str();
59 return 1; // 0 is reserved for unhashed key
63 h = (h << 5) + h + (unsigned) (*str);
66 return h==0 ? 1 : h; // 0 is reserved for unhashed key
69 std::string itos(int in)
72 snprintf(buf, 16, "%d", in);
73 return std::string(buf);
76 std::string utos(unsigned in)
79 snprintf(buf, 16, "%u", in);
80 return std::string(buf);
84 // This function will match string s against pattern p.
85 // Asterisks match 0 or more wild characters, and a question
86 // mark matches exactly one wild character. In other words,
87 // the asterisk is the equivalent of the regex ".*" and the
88 // question mark is the equivalent of "."
90 bool pattern_match( const char *p, const char *s, bool checkCase )
92 //const char *p = ptrn;
97 // If at the end of the pattern, it matches if also at the end of the string
100 return ( *s == '\0' );
104 if ( *p == MULTIPLE_WILDCARD_CHAR ) {
107 // If at the end of the pattern, it matches
111 // Try to match the remaining pattern for each remaining substring of s
112 for (; *s != '\0'; ++s )
113 if ( pattern_match( p, s, checkCase ) )
119 // If at the end of the string (and at this point, not of the pattern), it fails
123 // Check if this character matches
124 bool matchChar = false;
125 if ( *p == WILDCARD_CHAR || *p == *s )
127 else if ( !checkCase ) {
128 if ( *p >= 'A' && *p <= 'Z' && *s == ( *p + ( 'a' - 'A' ) ) )
130 else if ( *p >= 'a' && *p <= 'z' && *s == ( *p - ( 'a' - 'A' ) ) )
145 bool wildcardEquiv(const std::string &us, const std::string &them, bool checkCase )
150 return pattern_match( us.c_str(), them.c_str(), checkCase );
154 } // namespace Dyninst