-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.c
217 lines (191 loc) · 6.77 KB
/
output.c
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
/*
This file is part of pmktorrent
Copyright (C) 2007, 2009 Emil Renner Berthing
Edited 2019-2020 xxkfqz <xxkfqz@gmail.com>
pmktorrent 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.
pmktorrent 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
*/
#ifndef ALLINONE
#include <sys/types.h> /* off_t */
#include <stdio.h> /* printf() etc. */
#include <string.h> /* strlen() etc. */
#include <time.h> /* time() */
#include <stdlib.h> /* free() */
#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA_DIGEST_LENGTH */
#else
#include <inttypes.h>
#include "sha1.h"
#endif
#include "pmktorrent.h"
#define EXPORT
#endif /* ALLINONE */
/*
* I don’t know exactly how many files there should be, so that their
* number causes problems for users of other clients.
*/
#define FILES_NUMBER_WARNING 10000000
/*
* write announce list
*/
static void write_announce_list(FILE *f, llist_t *list)
{
/* the announce list is a list of lists of urls */
fprintf(f, "13:announce-listl");
/* go through them all.. */
for (; list; list = list->next)
{
slist_t *l;
/* .. and print the lists */
fprintf(f, "l");
for (l = list->l; l; l = l->next)
fprintf(f, "%lu:%s",
(unsigned long)strlen(l->s), l->s);
fprintf(f, "e");
}
fprintf(f, "e");
}
/*
* write file list
*/
static void write_file_list(FILE *f, flist_t *list)
{
char *a, *b;
fprintf(f, "5:filesl");
/* go through all the files */
for (; list; list = list->next)
{
/* the file list contains a dictionary for every file
with entries for the length and path
write the length first */
fprintf(f, "d6:lengthi%" PRIoff "e4:pathl", list->size);
/* the file path is written as a list of subdirectories
and the last entry is the filename
sorry this code is even uglier than the rest */
a = list->path;
/* while there are subdirectories before the filename.. */
while ((b = strchr(a, DIRSEP_CHAR)) != NULL)
{
/* set the next DIRSEP_CHAR to '\0' so fprintf
will only write the first subdirectory name */
*b = '\0';
/* print it bencoded */
fprintf(f, "%lu:%s", (unsigned long)strlen(a), a);
/* undo our alteration to the string */
*b = DIRSEP_CHAR;
/* and move a to the beginning of the next
subdir or filename */
a = b + 1;
}
/* now print the filename bencoded and end the
path name list and file dictionary */
fprintf(f, "%lu:%see", (unsigned long)strlen(a), a);
}
/* whew, now end the file list */
fprintf(f, "e");
}
/*
* write web seed list
*/
static void write_web_seed_list(FILE *f, slist_t *list)
{
/* print the entry and start the list */
fprintf(f, "8:url-listl");
/* go through the list and write each URL */
for (; list; list = list->next)
fprintf(f, "%lu:%s", (unsigned long)strlen(list->s), list->s);
/* end the list */
fprintf(f, "e");
}
/*
* write metainfo to the file stream using all the information
* we've gathered so far and the hash string calculated
*/
EXPORT void write_metainfo(FILE *f, metafile_t *m, unsigned char *hash_string)
{
if (!m->quiet)
{
fprintf(stderr, "Total number of files: %" PRIu64 "\n",
m->file_count);
if(m->file_count >= FILES_NUMBER_WARNING)
fprintf(stderr,
"Warning: the current number of files can cause"
"problems on torrent clients");
/* let the user know we've started writing the metainfo file */
fprintf(stderr, "Writing metainfo file... ");
}
fflush(stdout);
/* every metainfo file is one big dictonary */
fprintf(f, "d");
if (m->announce_list != NULL)
{
/* write the announce URL */
fprintf(f, "8:announce%lu:%s",
(unsigned long)strlen(m->announce_list->l->s),
m->announce_list->l->s);
/* write the announce-list entry if we have
more than one announce URL */
if (m->announce_list->next || m->announce_list->l->next)
write_announce_list(f, m->announce_list);
}
/* add the comment if one is specified */
if (m->comment != NULL)
fprintf(f, "7:comment%lu:%s",
(unsigned long)strlen(m->comment),
m->comment);
/* I made this! */
fprintf(f, "10:created by%lu:%s", (unsigned long)strlen(m->created_by),
m->created_by);
/* add the creation date if specified */
if (!m->no_creation_date)
fprintf(f, "13:creation datei%lde", m->creation_date);
/* now here comes the info section
it is yet another dictionary */
fprintf(f, "4:infod");
/* first entry is either 'length', which specifies the length of a
single file torrent, or a list of files and their respective sizes */
if (!m->target_is_directory)
fprintf(f, "6:lengthi%" PRIoff "e", m->file_list->size);
else
write_file_list(f, m->file_list);
/* the info section also contains the name of the torrent,
the piece length and the hash string */
fprintf(f, "4:name%lu:%s12:piece lengthi%ue6:pieces%u:",
(unsigned long)strlen(m->torrent_name), m->torrent_name,
m->piece_length, m->pieces * SHA_DIGEST_LENGTH);
fwrite(hash_string, 1, m->pieces * SHA_DIGEST_LENGTH, f);
/* set the private flag */
if (m->private)
fprintf(f, "7:privatei1e");
if (m->source)
fprintf(f, "6:source%lu:%s", (unsigned long)strlen(m->source),
m->source);
/* end the info section */
fprintf(f, "e");
/* add url-list if one is specified */
if (m->web_seed_list != NULL)
{
if (m->web_seed_list->next == NULL)
fprintf(f, "8:url-list%lu:%s",
(unsigned long)strlen(m->web_seed_list->s),
m->web_seed_list->s);
else
write_web_seed_list(f, m->web_seed_list);
}
/* end the root dictionary */
fprintf(f, "e");
/* let the user know we're done already */
if (!m->quiet)
fprintf(stderr, "done.\n");
fflush(stdout);
free(hash_string);
}