util.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #if defined(os_windows)
00031 #include "common/h/ntHeaders.h"
00032 #endif
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string>
00036 #include <map>
00037 #include "dynutil/h/dyntypes.h"
00038
00039 using namespace std;
00040
00041 namespace Dyninst {
00042
00043 COMMON_EXPORT unsigned addrHashCommon(const Address &addr)
00044 {
00045
00046
00047 register unsigned result = 5381;
00048
00049 register Address accumulator = addr;
00050 while (accumulator > 0) {
00051
00052 result = (result << 4) + result + (accumulator & 0x07);
00053 accumulator >>= 3;
00054 }
00055
00056 return result;
00057 }
00058
00059 COMMON_EXPORT unsigned addrHash(const Address & iaddr)
00060 {
00061 return Dyninst::addrHashCommon(iaddr);
00062 }
00063
00064 COMMON_EXPORT unsigned ptrHash(const void * iaddr)
00065 {
00066 return Dyninst::addrHashCommon((Address)iaddr);
00067 }
00068
00069 COMMON_EXPORT unsigned ptrHash(void * iaddr)
00070 {
00071 return Dyninst::addrHashCommon((Address)iaddr);
00072 }
00073
00074 COMMON_EXPORT unsigned addrHash4(const Address &iaddr)
00075 {
00076
00077
00078 return Dyninst::addrHashCommon(iaddr >> 2);
00079 }
00080
00081 COMMON_EXPORT unsigned addrHash16(const Address &iaddr)
00082 {
00083
00084
00085 return Dyninst::addrHashCommon(iaddr >> 4);
00086 }
00087
00088
00089 unsigned stringhash(const std::string &s)
00090 {
00091 const char *str = s.c_str();
00092 if (!str)
00093 return 1;
00094
00095 unsigned h = 5381;
00096 while (*str) {
00097 h = (h << 5) + h + (unsigned) (*str);
00098 str++;
00099 }
00100 return h==0 ? 1 : h;
00101 }
00102
00103 std::string itos(int in)
00104 {
00105 char buf[16];
00106 snprintf(buf, 16, "%d", in);
00107 return std::string(buf);
00108 }
00109
00110 std::string utos(unsigned in)
00111 {
00112 char buf[16];
00113 snprintf(buf, 16, "%u", in);
00114 return std::string(buf);
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124 bool pattern_match( const char *p, const char *s, bool checkCase )
00125 {
00126
00127
00128
00129 while ( true ) {
00130
00131
00132
00133 if ( *p == '\0' )
00134 return ( *s == '\0' );
00135
00136
00137
00138 if ( *p == MULTIPLE_WILDCARD_CHAR ) {
00139 ++p;
00140
00141
00142 if ( *p == '\0' )
00143 return true;
00144
00145
00146 for (; *s != '\0'; ++s )
00147 if ( pattern_match( p, s, checkCase ) )
00148 return true;
00149
00150 return false;
00151 }
00152
00153
00154 if( *s == '\0' )
00155 return false;
00156
00157
00158 bool matchChar = false;
00159 if ( *p == WILDCARD_CHAR || *p == *s )
00160 matchChar = true;
00161 else if ( !checkCase ) {
00162 if ( *p >= 'A' && *p <= 'Z' && *s == ( *p + ( 'a' - 'A' ) ) )
00163 matchChar = true;
00164 else if ( *p >= 'a' && *p <= 'z' && *s == ( *p - ( 'a' - 'A' ) ) )
00165 matchChar = true;
00166 }
00167
00168 if ( matchChar ) {
00169 ++p;
00170 ++s;
00171 continue;
00172 }
00173
00174
00175 return false;
00176 }
00177 }
00178
00179 bool wildcardEquiv(const std::string &us, const std::string &them, bool checkCase )
00180 {
00181 if ( us == them )
00182 return true;
00183 else
00184 return pattern_match( us.c_str(), them.c_str(), checkCase );
00185 }
00186
00187
00188 const char *platform_string()
00189 {
00190 const char *plat_str = getenv("PLATFORM");
00191 if (plat_str)
00192 return plat_str;
00193
00194 #if defined (arch_x86)
00195 #if defined (os_linux)
00196 return "i386-unknown-linux2.4";
00197 #elif defined (os_windows)
00198 return "i386-unknown-nt4.0";
00199 #endif
00200 #elif defined (arch_x86_64)
00201 #if defined (os_linux)
00202 return "x86_64-unknown-linux2.4";
00203 #elif defined (os_windows)
00204 return "x86_64-unknown-nt4.0";
00205 #endif
00206 #elif defined (arch_power)
00207 #if defined (os_aix)
00208 return "rs6000-ibm-aix5.1";
00209 #elif defined (os_linux)
00210 #if defined (arch_64bit)
00211 return "ppc64_linux";
00212 #else
00213 return "ppc32_linux";
00214 #endif
00215 #endif
00216 #endif
00217 return "bad_platform";
00218 }
00219
00220
00221
00222
00223 class SymElf;
00224
00225 map<string, SymElf *> *getSymelfCache() {
00226 static map<string, SymElf *> elfmap;
00227 return &elfmap;
00228 }
00229
00230 }