This repository was archived by the owner on Jul 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMetalink3Reader.cpp
More file actions
168 lines (160 loc) · 5.23 KB
/
Metalink3Reader.cpp
File metadata and controls
168 lines (160 loc) · 5.23 KB
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
#include "Metalink3Reader.hpp"
#include "XmlParser.hpp"
Metalink3Reader::Metalink3Reader()
: recognized_(false)
{
}
void Metalink3Reader::load(wxString filename)
{
metalink_.clear();
state_ = STATE_NONE;
recognized_ = false;
XmlParser parser(*this);
parser.parse(filename);
if(state_ != STATE_NONE) {
throw MetalinkLoadError("Internal error while loading metalink!");
}
}
const Metalink& Metalink3Reader::get_metalink() const
{
return metalink_;
}
bool Metalink3Reader::is_recognized() const
{
return recognized_;
}
void Metalink3Reader::start_element(wxString name,
std::map<std::string, wxString> attrs)
{
if(!remove_namespace(name)) return;
switch(state_) {
case STATE_NONE:
if(name == wxT("metalink") && attrs["version"] == wxT("3.0")) {
recognized_ = true;
state_ = STATE_METALINK;
}
break;
case STATE_METALINK:
if(name == wxT("file")) {
if(attrs.count("name") != 1) {
throw MetalinkLoadError("Missing 'name' attribute on file "
"element.");
}
file_ = MetalinkFile(attrs["name"]);
state_ = STATE_FILE;
}
break;
case STATE_FILE:
if(name == wxT("resources")) {
state_ = STATE_RESOURCES;
} else if(name == wxT("pieces")) {
if(attrs.count("length") != 1 || attrs.count("type") != 1) {
throw MetalinkLoadError("Missing attribute on pieces "
"element.");
}
file_.set_piece_hash_type(attrs["type"]);
file_.set_piece_length(attrs["length"]);
piece_hashes_.clear();
state_ = STATE_PIECES;
} else if(name == wxT("hash")) {
if(attrs.count("type") != 1) {
throw MetalinkLoadError("Missing 'type' attribute on "
"hash element");
}
hash_ = MetalinkHash(attrs["type"]);
}
break;
case STATE_RESOURCES:
if(name == wxT("url")) {
source_ = MetalinkSource();
if(attrs["type"] == wxT("bittorrent")) {
source_.set_torrent(true);
}
if(attrs.count("location") == 1) {
source_.set_location(attrs["location"]);
}
if(attrs.count("preference") == 1) {
source_.set_priority(attrs["preference"]);
}
state_ = STATE_URL;
}
break;
case STATE_PIECES:
if(name == wxT("hash")) {
if(attrs.count("piece") != 1) {
throw MetalinkLoadError("Missing 'piece' attribute on "
"hash element");
}
bool isnum = attrs["piece"].ToLong(&piece_);
if(!isnum || piece_ < 0 || piece_ > 5000) {
throw MetalinkLoadError("Invalid piece number");
}
}
break;
}
data_.clear();
}
void Metalink3Reader::end_element(wxString name)
{
if(!remove_namespace(name)) return;
switch(state_) {
case STATE_METALINK:
if(name == wxT("metalink")) {
state_ = STATE_NONE;
}
break;
case STATE_FILE:
if(name == wxT("file")) {
metalink_.add_file(file_);
state_ = STATE_METALINK;
} else if(name == wxT("identity")) {
file_.set_identity(data_);
} else if(name == wxT("description")) {
file_.set_description(data_);
} else if(name == wxT("version")) {
file_.set_version(data_);
} else if(name == wxT("size")) {
file_.set_size(data_);
} else if(name == wxT("hash")) {
hash_.value = data_;
file_.add_file_hash(hash_);
}
break;
case STATE_RESOURCES:
if(name == wxT("resources")) {
state_ = STATE_FILE;
}
break;
case STATE_URL:
if(name == wxT("url")) {
source_.set_uri(data_);
file_.add_source(source_);
state_ = STATE_RESOURCES;
}
break;
case STATE_PIECES:
if(name == wxT("pieces")) {
file_.set_piece_hash(piece_hashes_);
state_ = STATE_FILE;
} else if(name == wxT("hash")) {
if(piece_hashes_.size() < piece_ + 1) {
piece_hashes_.resize(piece_ + 1);
}
piece_hashes_.at(piece_) = data_;
}
break;
}
}
void Metalink3Reader::char_data(wxString data)
{
data_.append(data);
}
bool Metalink3Reader::remove_namespace(wxString& name)
{
wxString ns = wxT("http://www.metalinker.org/\t");
if(name.compare(0, ns.length(), ns) == 0) {
name.erase(0, ns.length());
return true;
}
return false;
}