00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(AFX_BENODE_H__8E447816_68F5_461A_A032_09A0CB97F3CB__INCLUDED_)
00023 #define AFX_BENODE_H__8E447816_68F5_461A_A032_09A0CB97F3CB__INCLUDED_
00024
00025 #pragma once
00026
00027 class CBuffer;
00028
00029
00030 class CBENode
00031 {
00032
00033 public:
00034 CBENode();
00035 ~CBENode();
00036
00037
00038 public:
00039 int m_nType;
00040 LPVOID m_pValue;
00041 QWORD m_nValue;
00042
00043 enum { beNull, beString, beInt, beList, beDict };
00044
00045
00046 public:
00047 void Clear();
00048 CBENode* Add(const LPBYTE pKey, int nKey);
00049 CBENode* GetNode(LPCSTR pszKey) const;
00050 CBENode* GetNode(const LPBYTE pKey, int nKey) const;
00051 void GetSHA1(SHA1* pSHA1) const;
00052 CString GetStringFromSubNode(LPCSTR pszKey, UINT nEncoding, BOOL* pEncodingError);
00053 CString GetStringFromSubNode(int nItem, UINT nEncoding, BOOL* pEncodingError);
00054 void Encode(CBuffer* pBuffer) const;
00055 public:
00056 static CBENode* Decode(CBuffer* pBuffer);
00057 private:
00058 void Decode(LPBYTE& pInput, DWORD& nInput);
00059 static int DecodeLen(LPBYTE& pInput, DWORD& nInput);
00060
00061
00062
00063 public:
00064 inline bool IsType(int nType) const
00065 {
00066 if ( this == NULL ) return false;
00067 return m_nType == nType;
00068 }
00069
00070 inline QWORD GetInt() const
00071 {
00072 if ( m_nType != beInt ) return 0;
00073 return m_nValue;
00074 }
00075
00076 inline void SetInt(QWORD nValue)
00077 {
00078 Clear();
00079 m_nType = beInt;
00080 m_nValue = nValue;
00081 }
00082
00083 inline CString GetString() const
00084 {
00085 CString str;
00086 if ( m_nType != beString ) return str;
00087 str = (LPCSTR)m_pValue;
00088
00089 const DWORD nFlags = ( theApp.m_dwWindowsVersion == 4 ) ? 0 : MB_ERR_INVALID_CHARS;
00090
00091 int nLength = MultiByteToWideChar( CP_UTF8, nFlags, (LPCSTR)m_pValue, -1, NULL, 0 );
00092 if ( nLength > 0 )
00093 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)m_pValue, -1, str.GetBuffer( nLength ), nLength );
00094 else
00095 {
00096
00097 str.ReleaseBuffer();
00098 str = _T("#ERROR#");
00099 return str;
00100 }
00101 str.ReleaseBuffer();
00102
00103 return str;
00104 }
00105
00106
00107 inline CString DecodeString(UINT nCodePage) const
00108 {
00109 CString str;
00110 if ( m_nType != beString ) return str;
00111 str = (LPCSTR)m_pValue;
00112 int nSource = str.GetLength();
00113 if ( nSource == 0 ) return str;
00114 const DWORD nFlags = ( theApp.m_dwWindowsVersion == 4 ) ? 0 : MB_ERR_INVALID_CHARS;
00115 int nLength = 0;
00116
00117
00118 if ( nCodePage != 0 )
00119 nLength = MultiByteToWideChar( nCodePage, nFlags, (LPCSTR)m_pValue, -1 , NULL, 0 );
00120
00121 if ( nLength > 0 )
00122 MultiByteToWideChar( nCodePage, 0, (LPCSTR)m_pValue, -1 , str.GetBuffer( nLength ), nLength );
00123 else
00124 {
00125
00126 if ( Settings.BitTorrent.TorrentCodePage != 0 )
00127 nCodePage = Settings.BitTorrent.TorrentCodePage;
00128 else
00129 nCodePage = GetOEMCP();
00130
00131 nLength = MultiByteToWideChar( nCodePage, nFlags, (LPCSTR)m_pValue, nSource, NULL, 0 );
00132
00133 if ( nLength > 0 )
00134 MultiByteToWideChar( nCodePage, 0, (LPCSTR)m_pValue, nSource, str.GetBuffer( nLength ), nLength );
00135 else
00136 {
00137
00138 nCodePage = CP_ACP;
00139 nLength = MultiByteToWideChar( nCodePage, nFlags, (LPCSTR)m_pValue, nSource, NULL, 0 );
00140 if ( nLength > 0 )
00141 MultiByteToWideChar( nCodePage, 0, (LPCSTR)m_pValue, nSource, str.GetBuffer( nLength ), nLength );
00142 else
00143 {
00144
00145 str = _T("#ERROR#");
00146 return str;
00147 }
00148 }
00149 }
00150 str.ReleaseBuffer();
00151
00152 return str;
00153 }
00154
00155
00156 inline BOOL IsValid(LPCTSTR psz) const
00157 {
00158 if ( _tcsclen( psz ) == 0 ) return FALSE;
00159 if ( _tcschr( psz, '?' ) != NULL ) return FALSE;
00160 if ( _tcsicmp( psz , _T("#ERROR#") ) == 0 ) return FALSE;
00161
00162 return TRUE;
00163 }
00164
00165 inline void SetString(LPCSTR psz)
00166 {
00167 SetString( psz, strlen(psz), TRUE );
00168 }
00169
00170 void SetString(LPCWSTR psz)
00171 {
00172 USES_CONVERSION;
00173 LPCSTR pszASCII = W2CA(psz);
00174 SetString( pszASCII, strlen(pszASCII), TRUE );
00175 }
00176
00177 inline void SetString(LPCVOID pString, DWORD nLength, BOOL bNull = FALSE)
00178 {
00179 Clear();
00180 m_nType = beString;
00181 m_nValue = (QWORD)nLength;
00182 m_pValue = new BYTE[ nLength + ( bNull ? 1 : 0 ) ];
00183 CopyMemory( m_pValue, pString, nLength + ( bNull ? 1 : 0 ) );
00184 }
00185
00186 inline CBENode* Add(LPCSTR pszKey = NULL)
00187 {
00188 return Add( (LPBYTE)pszKey, pszKey != NULL ? strlen( pszKey ) : 0 );
00189 }
00190
00191 inline int GetCount() const
00192 {
00193 if ( m_nType != beList && m_nType != beDict ) return 0;
00194 return (int)m_nValue;
00195 }
00196
00197 inline CBENode* GetNode(int nItem) const
00198 {
00199 if ( m_nType != beList && m_nType != beDict ) return NULL;
00200 if ( m_nType == beDict ) nItem *= 2;
00201 if ( nItem < 0 || nItem >= (int)m_nValue ) return NULL;
00202 return *( (CBENode**)m_pValue + nItem );
00203 }
00204 };
00205
00206 #endif // !defined(AFX_BENODE_H__8E447816_68F5_461A_A032_09A0CB97F3CB__INCLUDED_)