summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/bdecoder.cpp
blob: 6c5a1798b07bf370ce8416e6cbedd999abcd566c (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include <util/log.h>
#include <util/error.h>
#include <klocale.h>
#include "bdecoder.h"
#include "bnode.h"
#include "globals.h"

namespace bt
{

	BDecoder::BDecoder(const QByteArray & data,bool verbose,Uint32 off)
	: data(data),pos(off),verbose(verbose)
	{
	}


	BDecoder::~BDecoder()
	{}

	BNode* BDecoder::decode()
	{
		if (pos >= data.size())
			return 0;

		if (data[pos] == 'd')
		{
			return parseDict();
		}
		else if (data[pos] == 'l')
		{
			return parseList();
		}
		else if (data[pos] == 'i')
		{
			return parseInt();
		}
		else if (data[pos] >= '0' && data[pos] <= '9')
		{
			return parseString();
		}
		else
		{
			throw Error(i18n("Illegal token: %1").arg(data[pos]));
		}
	}

	BDictNode* BDecoder::parseDict()
	{
		Uint32 off = pos;
		// we're now entering a dictionary
		BDictNode* curr = new BDictNode(off);
		pos++;
		if (verbose) Out() << "DICT" << endl;
		try
		{
			while (pos < data.size() && data[pos] != 'e')
			{
				if (verbose) Out() << "Key : " << endl;
				BNode* kn = decode(); 
				BValueNode* k = dynamic_cast<BValueNode*>(kn);
				if (!k || k->data().getType() != Value::STRING)
				{
					delete kn;
					throw Error(i18n("Decode error"));
				}

				QByteArray key = k->data().toByteArray();
				delete kn;
				
				BNode* data = decode();
				curr->insert(key,data);
			}
			pos++;
		}
		catch (...)
		{
			delete curr;
			throw;
		}
		if (verbose) Out() << "END" << endl;
		curr->setLength(pos - off);
		return curr;
	}

	BListNode* BDecoder::parseList()
	{
		Uint32 off = pos;
		if (verbose) Out() << "LIST" << endl;
		BListNode* curr = new BListNode(off);
		pos++;
		try
		{
			while (pos < data.size() && data[pos] != 'e')
			{
				BNode* n = decode();
				curr->append(n);
			}
			pos++;
		}
		catch (...)
		{
			delete curr;
			throw;
		}
		if (verbose) Out() << "END" << endl;
		curr->setLength(pos - off);
		return curr;
	}

	BValueNode* BDecoder::parseInt()
	{
		Uint32 off = pos;
		pos++;
		QString n;
		// look for e and add everything between i and e to n
		while (pos < data.size() && data[pos] != 'e')
		{
			n += data[pos];
			pos++;
		}

		// check if we aren't at the end of the data
		if (pos >= data.size())
		{
			throw Error(i18n("Unexpected end of input"));
		}

		// try to decode the int
		bool ok = true;
		int val = 0;
		val = n.toInt(&ok);
		if (ok)
		{
			pos++;
			if (verbose) Out() << "INT = " << val << endl;
			BValueNode* vn = new BValueNode(Value(val),off);
			vn->setLength(pos - off);
			return vn;
		}
		else
		{
			Int64 bi = 0LL;
			bi = n.toLongLong(&ok);
			if (!ok)
				throw Error(i18n("Cannot convert %1 to an int").arg(n));

			pos++;
			if (verbose) Out() << "INT64 = " << n << endl;
			BValueNode* vn = new BValueNode(Value(bi),off);
			vn->setLength(pos - off);
			return vn;
		}
	}

	BValueNode* BDecoder::parseString()
	{
		Uint32 off = pos;
		// string are encoded 4:spam (length:string)

		// first get length by looking for the :
		QString n;
		while (pos < data.size() && data[pos] != ':')
		{
			n += data[pos];
			pos++;
		}
		// check if we aren't at the end of the data
		if (pos >= data.size())
		{
			throw Error(i18n("Unexpected end of input"));
		}

		// try to decode length
		bool ok = true;
		int len = 0;
		len = n.toInt(&ok);
		if (!ok)
		{
			throw Error(i18n("Cannot convert %1 to an int").arg(n));
		}
		// move pos to the first part of the string
		pos++;
		if (pos + len > data.size())
			throw Error(i18n("Torrent is incomplete!"));
			
		QByteArray arr(len);
		for (unsigned int i = pos;i < pos + len;i++)
			arr.at(i-pos) = data[i];
		pos += len;
		// read the string into n

		// pos should be positioned right after the string
		BValueNode* vn = new BValueNode(Value(arr),off);
		vn->setLength(pos - off);
		if (verbose)
		{
			if (arr.size() < 200)
				Out() << "STRING " << QString(arr) << endl;
			else
				Out() << "STRING " << "really long string" << endl;
		}
		return vn;
	}
}