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 pathMetalink3Writer.cpp
More file actions
153 lines (142 loc) · 4.7 KB
/
Metalink3Writer.cpp
File metadata and controls
153 lines (142 loc) · 4.7 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
#include "Metalink3Writer.hpp"
Metalink3Writer::Metalink3Writer(const Metalink& metalink)
: metalink_(metalink)
{
}
void Metalink3Writer::write_metalink()
{
write_text(wxString(wxT("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")), true);
start(wxT("metalink"));
add_attr(wxT("version"), wxT("3.0"));
add_attr(wxT("generator"), get_generator());
add_attr(wxT("xmlns"), wxT("http://www.metalinker.org/"));
close_start();
start(wxT("files"));
close_start();
const std::vector<MetalinkFile>& files = metalink_.get_files();
for(std::vector<MetalinkFile>::const_iterator i = files.begin(),
eoi = files.end(); i != eoi; ++i) {
write_file(*i);
}
end(wxT("files"));
end(wxT("metalink"));
}
void Metalink3Writer::write_file(const MetalinkFile& file)
{
start(wxT("file"));
add_attr(wxT("name"), file.get_filename());
close_start();
add_element(wxT("identity"), file.get_identity());
add_element(wxT("description"), file.get_description());
add_element(wxT("version"), file.get_version());
add_element(wxT("size"), file.get_size());
write_verification(file);
start(wxT("resources"));
close_start();
const std::vector<MetalinkSource>& sources = file.get_sources();
for(std::vector<MetalinkSource>::const_iterator i = sources.begin(),
eoi = sources.end(); i != eoi; ++i) {
write_source(*i);
}
end(wxT("resources"));
end(wxT("file"));
}
void Metalink3Writer::write_verification(const MetalinkFile& file)
{
MetalinkFile file2 = convert_hash_types(file);
if(has_hashes(file2) || has_piece_hashes(file2)) {
start(wxT("verification"));
close_start();
write_hashes(file2);
write_piece_hashes(file2);
end(wxT("verification"));
}
}
MetalinkFile Metalink3Writer::convert_hash_types(const MetalinkFile& file)
{
std::vector<MetalinkHash> hashes = file.get_file_hashes();
for(std::vector<MetalinkHash>::iterator i = hashes.begin(),
eoi = hashes.end(); i != eoi; ++i) {
(*i).type = convert_hash_type((*i).type);
}
wxString piece_type = convert_hash_type(file.get_piece_hash_type());
MetalinkFile file2 = file;
file2.set_file_hashes(hashes);
file2.set_piece_hash_type(piece_type);
return file2;
}
const wxString Metalink3Writer::convert_hash_type(const wxString& type)
{
if(type == wxT("sha-1")){
return wxT("sha1");
} else if(type == wxT("sha-224")){
return wxT("sha224");
} else if(type == wxT("sha-256")){
return wxT("sha256");
} else if(type == wxT("sha-384")){
return wxT("sha384");
} else if(type == wxT("sha-256")){
return wxT("sha256");
}
return type;
}
bool Metalink3Writer::has_hashes(const MetalinkFile& file)
{
return !file.get_file_hashes().empty();
}
bool Metalink3Writer::has_piece_hashes(const MetalinkFile& file)
{
if(file.get_piece_hashes().empty()) return false;
if(file.get_piece_hash_type().empty()) return false;
return true;
}
void Metalink3Writer::write_hashes(const MetalinkFile& file)
{
const std::vector<MetalinkHash>& hashes =
file.get_file_hashes();
for(std::vector<MetalinkHash>::const_iterator i = hashes.begin(),
eoi = hashes.end(); i != eoi; ++i) {
start(wxT("hash"));
add_attr(wxT("type"), (*i).type);
end(wxT("hash"), (*i).value);
}
}
void Metalink3Writer::write_piece_hashes(const MetalinkFile& file)
{
if(!has_piece_hashes(file)) return;
start(wxT("pieces"));
add_attr(wxT("length"),
wxString::Format(wxT("%lu"), file.get_piece_length()));
add_attr(wxT("type"), file.get_piece_hash_type());
close_start();
const std::vector<wxString>& hashes = file.get_piece_hashes();
long index = 0;
for(std::vector<wxString>::const_iterator i = hashes.begin(),
eoi = hashes.end(); i != eoi; ++i) {
start(wxT("hash"));
add_attr(wxT("piece"), wxString::Format(wxT("%ld"), index));
end(wxT("hash"), *i);
index++;
}
end(wxT("pieces"));
}
void Metalink3Writer::write_source(const MetalinkSource& source)
{
if(source.is_torrent()) {
start(wxT("url"));
add_attr(wxT("type"), wxT("bittorrent"));
if(!source.get_prioritystr().empty()) {
add_attr(wxT("preference"), source.get_prioritystr());
}
end(wxT("url"), source.get_uri());
} else {
start(wxT("url"));
if(!source.get_prioritystr().empty()) {
add_attr(wxT("preference"), source.get_prioritystr());
}
if(!source.get_location().empty()) {
add_attr(wxT("location"), source.get_location());
}
end(wxT("url"), source.get_uri());
}
}