1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "bencodeparser.h"
52
53#include <QList>
54#include <QMetaType>
55
56BencodeParser::BencodeParser()
57{
58}
59
60bool BencodeParser::parse(const QByteArray &content)
61{
62 if (content.isEmpty()) {
63 errString = QString("No content");
64 return false;
65 }
66
67 this->content = content;
68 index = 0;
69 infoStart = 0;
70 infoLength = 0;
71 return getDictionary(dictionary: &dictionaryValue);
72}
73
74QString BencodeParser::errorString() const
75{
76 return errString;
77}
78
79QMap<QByteArray, QVariant> BencodeParser::dictionary() const
80{
81 return dictionaryValue;
82}
83
84QByteArray BencodeParser::infoSection() const
85{
86 return content.mid(index: infoStart, len: infoLength);
87}
88
89bool BencodeParser::getByteString(QByteArray *byteString)
90{
91 const int contentSize = content.size();
92 int size = -1;
93 do {
94 char c = content.at(i: index);
95 if (c < '0' || c > '9') {
96 if (size == -1)
97 return false;
98 if (c != ':') {
99 errString = QString("Unexpected character at pos %1: %2")
100 .arg(a: index).arg(a: c);
101 return false;
102 }
103 ++index;
104 break;
105 }
106 if (size == -1)
107 size = 0;
108 size *= 10;
109 size += c - '0';
110 } while (++index < contentSize);
111
112 if (byteString)
113 *byteString = content.mid(index, len: size);
114 index += size;
115 return true;
116}
117
118bool BencodeParser::getInteger(qint64 *integer)
119{
120 const int contentSize = content.size();
121 if (content.at(i: index) != 'i')
122 return false;
123
124 ++index;
125 qint64 num = -1;
126 bool negative = false;
127
128 do {
129 char c = content.at(i: index);
130 if (c < '0' || c > '9') {
131 if (num == -1) {
132 if (c != '-' || negative)
133 return false;
134 negative = true;
135 continue;
136 } else {
137 if (c != 'e') {
138 errString = QString("Unexpected character at pos %1: %2")
139 .arg(a: index).arg(a: c);
140 return false;
141 }
142 ++index;
143 break;
144 }
145 }
146 if (num == -1)
147 num = 0;
148 num *= 10;
149 num += c - '0';
150 } while (++index < contentSize);
151
152 if (integer)
153 *integer = negative ? -num : num;
154 return true;
155}
156
157bool BencodeParser::getList(QList<QVariant> *list)
158{
159 const int contentSize = content.size();
160 if (content.at(i: index) != 'l')
161 return false;
162
163 QList<QVariant> tmp;
164 ++index;
165
166 do {
167 if (content.at(i: index) == 'e') {
168 ++index;
169 break;
170 }
171
172 qint64 number;
173 QByteArray byteString;
174 QList<QVariant> tmpList;
175 QMap<QByteArray, QVariant> dictionary;
176
177 if (getInteger(integer: &number))
178 tmp << number;
179 else if (getByteString(byteString: &byteString))
180 tmp << byteString;
181 else if (getList(list: &tmpList))
182 tmp << tmpList;
183 else if (getDictionary(dictionary: &dictionary))
184 tmp << QVariant::fromValue<QMap<QByteArray, QVariant> >(value: dictionary);
185 else {
186 errString = QString("error at index %1").arg(a: index);
187 return false;
188 }
189 } while (index < contentSize);
190
191 if (list)
192 *list = tmp;
193 return true;
194}
195
196bool BencodeParser::getDictionary(QMap<QByteArray, QVariant> *dictionary)
197{
198 const int contentSize = content.size();
199 if (content.at(i: index) != 'd')
200 return false;
201
202 QMap<QByteArray, QVariant> tmp;
203 ++index;
204
205 do {
206 if (content.at(i: index) == 'e') {
207 ++index;
208 break;
209 }
210
211 QByteArray key;
212 if (!getByteString(byteString: &key))
213 break;
214
215 if (key == "info")
216 infoStart = index;
217
218 qint64 number;
219 QByteArray byteString;
220 QList<QVariant> tmpList;
221 QMap<QByteArray, QVariant> dictionary;
222
223 if (getInteger(integer: &number))
224 tmp.insert(akey: key, avalue: number);
225 else if (getByteString(byteString: &byteString))
226 tmp.insert(akey: key, avalue: byteString);
227 else if (getList(list: &tmpList))
228 tmp.insert(akey: key, avalue: tmpList);
229 else if (getDictionary(dictionary: &dictionary))
230 tmp.insert(akey: key, avalue: QVariant::fromValue<QMap<QByteArray, QVariant> >(value: dictionary));
231 else {
232 errString = QString("error at index %1").arg(a: index);
233 return false;
234 }
235
236 if (key == "info")
237 infoLength = index - infoStart;
238
239 } while (index < contentSize);
240
241 if (dictionary)
242 *dictionary = tmp;
243 return true;
244}
245

source code of qtbase/examples/network/torrent/bencodeparser.cpp