/////////////////////////////////////////////////////////////////////////////// // General Information: // // Author: Dean Lee // mailto:deanlee2@hotmail.com // http://www.deanlee.cn // // Purpose: // // Platforms: Windows // ============================================================================ // Revision History: // // 10/12/2006 12:51:22 PM Dean Lee Initial Version // // ============================================================================ // Acknowledgements: // <> Many thanks to all of you, who have encouraged me to update my articles // and code, and who sent in bug reports and fixes. // /////////////////////////////////////////////////////////////////////////////// #pragma once #define IP_RECORD_LENGTH 7 #define READINT3(X) (( X[0] & 0xFF )|(( X[1] & 0xFF)<< 8 )|(( X[2] & 0xFF )<< 16 )) #define READINT4(X) (( X[0] & 0xFF )|(( X[1] & 0xFF)<< 8 )|(( X[2] & 0xFF )<< 16 )|(( X[3] & 0xFF ) << 24 )) class CIPSeeker { public: CIPSeeker(); ~CIPSeeker(); bool Create(HINSTANCE hInstance); inline bool getIPLocation(char* pszIP, char szDest[], size_t nSize) { ATLASSERT(NULL != m_pBuffer); return getIPRecord(searchIP(getIntIP(pszIP)), szDest, nSize); } inline bool getIPLocation(ULONG dwIP, char szDest[], size_t nSize) { ATLASSERT(NULL != m_pBuffer); return getIPRecord(searchIP(dwIP), szDest, nSize); } private: bool GetIndexOffset(); unsigned int readIP(unsigned int offset) { unsigned int tmpIP; memcpy(byte4, m_pBuffer + offset, 4 * sizeof(BYTE)); tmpIP = READINT4(byte4); return tmpIP; } int compareIP(const unsigned int ip1, const unsigned int ip2) { if( ip1 > ip2 ) return 1; else if ( ip1 < ip2 ) return -1; else return 0; } unsigned int getMiddleOffset(const unsigned int begin, const unsigned int end) { int records = (end - begin) / IP_RECORD_LENGTH; records >>= 1; if(records == 0) records = 1; return begin + records * IP_RECORD_LENGTH; } unsigned int searchIP(const unsigned int ip); //read a string start from the offset until meet a char '\0' //param: unsigned int offset //return: string str void readString(const unsigned int offset, char szString[], size_t nSize) { strncpy_s(szString, nSize, (char*)(m_pBuffer+offset), _TRUNCATE); if (szString[nSize - 1] != '\0') szString[nSize - 1] = 0; m_pCurPos = m_pBuffer + offset + strlen(szString) + 1; } //read the Area data start from the offset. //param: unsigned int offset //return: string areaData void readArea(const unsigned int offset, char szArea[], size_t nSize); //get one IP record (country and area) of the offset //param: unsigned int offset //return: string location bool getIPRecord(const unsigned int offset, char szIPRecord[], size_t nSize); UINT getIntIP(const char *strIP); BYTE *m_pBuffer; BYTE *m_pCurPos; BYTE byte4[4]; //tmp char array BYTE byte3[3]; //tmp char array int firstIndexOffset; //the first index offset of the index area int lastIndexOffset; //the last index offset of the index area };