-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBENode.h
145 lines (120 loc) · 3.69 KB
/
BENode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// BENode.h
//
// This file is part of PeerProject (peerproject.org) © 2008-2012
// Portions copyright Shareaza Development Team, 2002-2008.
//
// PeerProject is free software. You may redistribute and/or modify it
// under the terms of the GNU Affero General Public License
// as published by the Free Software Foundation (fsf.org);
// version 3 or later at your option. (AGPLv3)
//
// PeerProject is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License 3.0 for details:
// (http://www.gnu.org/licenses/agpl.html)
//
#pragma once
class CBuffer;
typedef const BYTE *LPCBYTE;
class CBENode
{
// Construction
public:
CBENode();
~CBENode();
// Attributes
public:
int m_nType;
LPVOID m_pValue;
__int64 m_nValue;
QWORD m_nSize;
QWORD m_nPosition;
static UINT m_nDefaultCP; // User codepage for string decoding
enum { beNull, beString, beInt, beList, beDict };
// Operations
public:
void Clear();
CBENode* Add(LPCBYTE pKey, size_t nKey);
CBENode* GetNode(LPCSTR pszKey) const;
CBENode* GetNode(const LPBYTE pKey, int nKey) const;
CString GetStringFromSubNode(LPCSTR pszKey, UINT nEncoding) const;
CString GetStringFromSubNode(int nItem, UINT nEncoding) const;
CSHA GetSHA1() const;
void Encode(CBuffer* pBuffer) const;
void Decode(LPCBYTE& pInput, DWORD& nInput, DWORD nSize);
static CBENode* Decode(const CBuffer* pBuffer, DWORD *pnReaden = NULL);
static CBENode* Decode(LPCBYTE pBuffer, DWORD nLength, DWORD *pnReaden = NULL);
private:
static int DecodeLen(LPCBYTE& pInput, DWORD& nInput);
// Inline
public:
inline bool IsType(int nType) const
{
if ( this == NULL ) return false;
return m_nType == nType;
}
inline __int64 GetInt() const
{
if ( m_nType != beInt ) return 0;
return m_nValue;
}
inline void SetInt(__int64 nValue)
{
Clear();
m_nType = beInt;
m_nValue = nValue;
}
// If a torrent is badly encoded, you can try forcing a code page.
// Trying codepages: nCodePage, m_nDefaultCP, OEM, ANSI, as-is
CString DecodeString(UINT nCodePage) const;
CString GetString() const;
// inline void SetString(LPCSTR psz)
// {
// SetString( psz, strlen(psz), TRUE );
// }
// inline void SetString(LPCWSTR psz)
// {
// CW2A pszASCII( psz );
// SetString( (LPCSTR)pszASCII, strlen( (LPCSTR)pszASCII ), TRUE );
// }
inline void SetString(const CString& strInput)
{
CStringA strUTF8 = UTF8Encode( strInput );
SetString( strUTF8, strUTF8.GetLength(), TRUE );
}
inline void SetString(LPCVOID pString, size_t nLength, BOOL bNull = FALSE)
{
Clear();
m_nType = beString;
m_nValue = (__int64)nLength;
m_pValue = new BYTE[ nLength + ( bNull ? 1 : 0 ) ];
CopyMemory( m_pValue, pString, nLength + ( bNull ? 1 : 0 ) );
}
//#ifdef HASHES_HPP_INCLUDED
bool GetString(Hashes::BtGuid& oGUID) const;
inline void SetString(const Hashes::BtGuid& oGUID)
{
SetString( &oGUID[0], oGUID.byteCount );
}
//#endif // HASHES_HPP_INCLUDED
inline CBENode* Add(LPCSTR pszKey = NULL)
{
return Add( (LPBYTE)pszKey, pszKey != NULL ? strlen( pszKey ) : 0 );
}
inline int GetCount() const
{
if ( m_nType != beList && m_nType != beDict ) return 0;
return (int)m_nValue;
}
inline CBENode* GetNode(int nItem) const
{
if ( m_nType != beList && m_nType != beDict ) return NULL;
if ( m_nType == beDict ) nItem *= 2;
if ( nItem < 0 || nItem >= m_nValue ) return NULL;
return *( (CBENode**)m_pValue + nItem );
}
// Encode node to human readable string
const CString Encode() const;
};