1
+ ![ Icon] ( Assets/icon_64.png )
2
+
1
3
BencodeNET
2
4
==========
3
5
[ ![ license] ( https://img.shields.io/badge/license-Unlicense-blue.svg )] ( https://github.com/Krusen/BencodeNET/blob/master/LICENSE.md )
@@ -7,143 +9,105 @@ BencodeNET
7
9
[ ![ NuGet] ( https://buildstats.info/nuget/bencodenet?includePreReleases=false )] ( https://www.nuget.org/packages/BencodeNET/ )
8
10
[ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=shield )] ( https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_shield )
9
11
10
- A .NET library for encoding and decoding bencode.
12
+ A lightweight and fast .NET library for encoding and decoding bencode (e.g. torrent files and BitTorrent client/tracker communication) .
11
13
12
14
- http://en.wikipedia.org/wiki/Bencode
13
15
- https://wiki.theory.org/BitTorrentSpecification#Bencoding
14
16
15
- Overview
17
+ The main focus of this library is on supporting the bencode format; torrent file reading/manipulation is secondary.
18
+
19
+ Contents
16
20
--------
17
21
22
+ - [ Project status] ( #project-status )
18
23
- [ Installation] ( #installation )
19
- - [ Usage] ( #usage )
24
+ - [ Getting started] ( #getting-started )
25
+ - [ Parsing] ( #parsing )
26
+ - [ Encoding] ( #encoding )
27
+ - [ String character encoding] ( #string-character-encoding )
20
28
- [ Torrents] ( #torrents )
21
29
- [ File modes] ( #file-modes )
22
30
- [ Non-standard fields] ( #non-standard-fields )
23
- - [ Parsing] ( #parsing )
24
- - [ Encoding] ( #encoding )
25
- - [ String Character Encoding] ( #string-character-encoding )
26
- - [ Upgrading from 1.x to 2.0] ( #upgrading-from-version-1x-to-20 )
31
+ - [ Roadmap] ( #roadmap )
32
+ - [ Building the project] ( #building-the-project )
33
+ - [ Contributing] ( #contributing )
34
+ - [ Support] ( #support )
35
+
36
+ Project status
37
+ --------------
38
+ The project is in maintenance mode and only getting updated if issues are being reported or if new features are requested.
39
+
40
+ So, while I can't promise anything, go ahead and report any issues or feature requests by creating a new issue.
27
41
28
42
29
43
Installation
30
44
------------
31
-
32
- Install the package ** BencodeNET** from [ NuGet] ( https://www.nuget.org/packages/BencodeNET/ )
33
- or install it from the [ Package Manager Console] ( https://docs.microsoft.com/da-dk/nuget/tools/package-manager-console ) :
45
+ Install the package ** BencodeNET** from [ NuGet] ( https://www.nuget.org/packages/BencodeNET/ ) , using ` <PackageReference> ` or from the command line:
34
46
35
47
```
36
- PM> Install-Package BencodeNET
37
- ```
38
-
39
- Usage
40
- -----
41
- ### Torrents
42
- Working with torrent files:
43
-
44
- ``` C#
45
- // Parse torrent by specifying the file path
46
- var parser = new BencodeParser (); // Default encoding is Encoding.UT8F, but you can specify another if you need to
47
- var torrent = parser .Parse <Torrent >(" C:\u buntu.torrent" );
48
-
49
- // Alternatively, handle the stream yourself
50
- using (var stream = File .OpenRead (" C:\u buntu.torrent" ))
51
- {
52
- torrent = parser .Parse <Torrent >(stream );
53
- }
54
-
55
- // Calculate the info hash
56
- string infoHash = torrent .GetInfoHash ();
57
- // "B415C913643E5FF49FE37D304BBB5E6E11AD5101"
58
-
59
- // or as bytes instead of a string
60
- byte [] infoHashBytes = torrent .GetInfoHashBytes ();
61
-
62
- // Get Magnet link
63
- string magnetLink = torrent .GetMagnetLink ();
64
- // magnet:?xt=urn:btih:1CA512A4822EDC7C1B1CE354D7B8D2F84EE11C32&dn=ubuntu-14.10-desktop-amd64.iso&tr=http://torrent.ubuntu.com:6969/announce&tr=http://ipv6.torrent.ubuntu.com:6969/announce
48
+ // .csproj using PackageReference
49
+ <PackageReference Include="BencodeNET" Version="2.3.0" />
65
50
66
- // Convert Torrent to its BDictionary representation
67
- BDictionary bencode = torrent . ToBDictionary ();
51
+ // .NET CLI
52
+ > dotnet add package BencodeNET
68
53
```
69
54
70
- #### File modes
71
- The property ` FileMode ` indicates if the torrent is single-file or multi-file.
72
-
73
- For single-file torrents the ` File ` property contains the relevant file info.
74
- The ` Files ` property is null.
75
-
76
- For multi-file torrents the ` Files ` property contains a list of file info and the directory name.
77
- The ` File ` property is null.
78
-
79
- #### Non-standard fields
80
- The ` ExtraFields ` property is for any non-standard fields which are not accessible through any other property.
81
- Data set on this property will overwrite any data from the ` Torrent ` itself when encoding it. This way you are able to add to or owerwrite fields.
82
55
56
+ Getting started
57
+ ---------------
83
58
### Parsing
84
- Simple parsing of a bencoded string:
59
+ Here are some simple examples for parsing bencode strings directly.
85
60
86
61
``` C#
62
+ using BencodeNET .Parsing ;
63
+ using BencodeNET .Objects ;
64
+
87
65
var parser = new BencodeParser ();
88
- BString bstring = parser .ParseString (" 12:Hellow World!" );
66
+
67
+ // Parse unknown type
68
+ IBObject bstring = parser .ParseString (" 12:Hellow World!" );
89
69
// "Hello World!" (BString)
90
70
91
71
// If you know the type of the bencode you are parsing, you can use the generic version of `ParseString()` instead.
92
- var bstring2 = parser .ParseString <BString >(" 12:Hello World!" );
72
+ BString bstring = parser .ParseString <BString >(" 12:Hello World!" );
93
73
// "Hello World!" (BString)
94
74
95
- var bnumber = parser .ParseString <BNumber >(" i42e" );
75
+ BNumber bnumber = parser .ParseString <BNumber >(" i42e" );
96
76
// 42 (BNumber)
97
77
98
- var blist = parser .ParseString <BList >(" l3:foo3:bari42ee" );
78
+ BList blist = parser .ParseString <BList >(" l3:foo3:bari42ee" );
99
79
// { "foo", "bar", 42 } (BList)
100
80
101
- var bdictionary = parser .ParseString <BDictionary >(" d3:fooi42e5:Hello6:World!e" );
81
+ BDictionary bdictionary = parser .ParseString <BDictionary >(" d3:fooi42e5:Hello6:World!e" );
102
82
// { { "foo", 42 }, { "Hello", "World" } } (BDictionary)
103
83
```
104
84
105
- If you are unsure of the type you can just use the non-generic version:
85
+ Usually you would probably either parse a ` Stream ` of some kind or a ` PipeReader ` if using .NET Core.
106
86
107
87
``` C#
108
- IBObject bobject = parser .ParseString (" 12:Hello World!" );
109
-
110
- if (bobject is BString )
111
- {
112
- // The parsed object is a string
113
- }
114
- ```
115
-
116
- It is also possible to decode directly from a stream instead, for example a ` FileStream ` or a ` MemoryStream ` :
117
-
118
- ``` C#
119
- using (var stream = File .OpenRead (" Ubuntu.torrent" ))
120
- {
121
- var bdictionary = parser .Parse <BDictionary >(stream );
122
- }
88
+ BDictionary bdictionary = parser .Parse <BDictionary >(stream );
89
+ BDictionary bdictionary = await parser .ParseAsync <BDictionary >(stream );
90
+ BDictionary bdictionary = await parser .ParseAsync <BDictionary >(pipeReader );
123
91
```
124
92
125
93
### Encoding
126
- You have the option to encode ` BObject ` s either as a ` string ` , a ` byte[] ` , to a ` Stream ` or directly to a file path.
94
+ Encoding an object is simple and can be done in the following ways:
127
95
128
96
``` C#
129
97
var bstring = new BString (" Hello World!" );
98
+
130
99
bstring .EncodeAsString (); // "12:Hello World!"
131
100
bstring .EncodeAsBytes (); // [ 49, 50, 58, 72, ... ]
132
- bstring .EncodeTo (" C:\\ data.bencode" ); // Writes "12:Hello World!" to the specified file
133
- bstring .EncodeTo (new MemoryStream ());
134
101
135
- var bnumber = new BNumber (42 );
136
- bnumber .EncodeAsString (); // "i42e"
137
-
138
- var blist = new BList { " foo" , 42 , " bar" };
139
- blist .EncodeAsString (); // "l3:fooi42e3:bare"
140
-
141
- var bdictionary = new BDictionary { { " foo" , 42 }, { " Hello" , " World!" } };
142
- bdictionary .EncodeAsString () // "d3:fooi42e5:Hello6:World!e"
102
+ bstring .EncodeTo (" C:\\ data.bencode" ); // Writes "12:Hello World!" to the specified file
103
+ bstring .EncodeTo (stream );
104
+ await bstring .EncodeToAsync (stream );
105
+ bstring .EncodeTo (pipeWriter );
106
+ await bstring .EncodeToAsync (pipeWriter );
143
107
```
144
108
145
- String Character Encoding
146
- -------------------------
109
+ ### String character encoding
110
+
147
111
By default ` Encoding.UTF8 ` is used when rendering strings.
148
112
149
113
When parsing a string directly the encoding is used to convert the string to an array of bytes.
@@ -152,12 +116,14 @@ If no encoding is passed to `ToString` it will use the encoding the `BString` wa
152
116
153
117
``` C#
154
118
// Using the default encoding from Bencode.DefaultEncoding (UTF8)
155
- var bstring = Bencode .DecodeString (" 21:æøå äö èéê ñ" );
119
+ var parser = new BencodeParser ();
120
+ var bstring = parser .ParseString (" 21:æøå äö èéê ñ" );
156
121
bstring .ToString () // "æøå äö èéê ñ"
157
122
bstring .ToString (Encoding .UTF8 ) // "æøå äö èéê ñ"
158
123
159
124
// Using ISO-8859-1
160
- bstring = Bencode .DecodeString (" 12:æøå äö èéê ñ" , Encoding .GetEncoding (" ISO-8859-1" ));
125
+ var parser = new BencodeParser (Encoding .GetEncoding (" ISO-8859-1" ));
126
+ bstring = parser .ParseString (" 12:æøå äö èéê ñ" );
161
127
bstring .ToString (); // "æøå äö èéê ñ"
162
128
bstring .ToString (Encoding .UTF8 ); // "??? ?? ??? ?"
163
129
```
@@ -182,7 +148,7 @@ bstring.ToString(Encoding.GetEncoding("ISO-8859-1"));
182
148
183
149
// You have to specify the used encoding when creating the parser
184
150
// BStrings will then use that as the default when encoding the string
185
- parser = new BencodeParser (Encoding .GetEncoding (" ISO-8859-1" ));
151
+ var parser = new BencodeParser (Encoding .GetEncoding (" ISO-8859-1" ));
186
152
bstring = parser .Parse <BString >(bytes );
187
153
bstring .ToString ();
188
154
// "æøå äö èéê ñ"
@@ -207,36 +173,79 @@ blist.EncodeAsString(Encoding.UTF8); // "l12:??? ?? ??? ?e
207
173
blist .EncodeAsString (Encoding .GetEncoding (" ISO-8859-1" )); // "l12:æøå äö èéê ñe""
208
174
```
209
175
210
- Upgrading from version 1.x to 2.0
211
- ---------------------------------
212
- The API has changed quite a bit in version 2.0, but mostly naming wise and the usage is more or less
213
- the same with some added functionality and ease of use.
214
-
215
- Probably the biggest difference is that in 1.x you would use the static class ` Bencode ` and the methods
216
- ` DecodeString(string) ` , ` DecodeNumber(string) ` etc. In 2.0 you have to create an instance of ` BencodeParser `
217
- and use the methods on that.
176
+ ### Torrents
218
177
219
- Use ` BencodeParser.ParseString(string) ` for parsing strings directly or ` BencodeParser.Parse(...) `
220
- for parsing ` Stream ` , ` byte[] ` or a file by file path (` string ` ) without opening af stream yourself.
178
+ Working with torrent files:
221
179
222
180
``` C#
223
- // 1.x - Parsing strings directly
224
- BString bstring = Bencode .DecodeString (" 12:Hello World!" );
225
- BNumber bnumber = Bencode .DecodeNumber (" i42e" );
226
- BList blist = Bencode .DecodeList (" l3:foo3:bari42ee" );
227
- BDictionary bdictionary = Bencode .DecodeDictionary (" d3:fooi42e5:Hello6:World!e" );
181
+ using BencodeNET .Objects ;
182
+ using BencodeNET .Parsing ;
183
+ using BencodeNET .Torrents ;
228
184
229
- // 2.0 - Parsing strings directly
230
- var parser = new BencodeParser ();
231
- BString bstring = parser .ParseString <BString >(" 12:Hello World!" );
232
- BNumber bnumber = parser .ParseString <BNumber >(" i42e" );
233
- BList blist = parser .ParseString <BList >(" l3:foo3:bari42ee" );
234
- BDictionary bdictionary = parser .ParseString <BDictionary >(" d3:fooi42e5:Hello6:World!e" );
185
+ // Parse torrent by specifying the file path
186
+ var parser = new BencodeParser (); // Default encoding is Encoding.UTF8, but you can specify another if you need to
187
+ Torrent torrent = parser .Parse <Torrent >(" C:\\ ubuntu.torrent" );
188
+
189
+ // Or parse a stream
190
+ Torrent torrent = parser .Parse <Torrent >(stream );
191
+
192
+ // Calculate the info hash
193
+ string infoHash = torrent .GetInfoHash ();
194
+ // "B415C913643E5FF49FE37D304BBB5E6E11AD5101"
195
+
196
+ // or as bytes instead of a string
197
+ byte [] infoHashBytes = torrent .GetInfoHashBytes ();
198
+
199
+ // Get Magnet link
200
+ string magnetLink = torrent .GetMagnetLink ();
201
+ // magnet:?xt=urn:btih:1CA512A4822EDC7C1B1CE354D7B8D2F84EE11C32&dn=ubuntu-14.10-desktop-amd64.iso&tr=http://torrent.ubuntu.com:6969/announce&tr=http://ipv6.torrent.ubuntu.com:6969/announce
235
202
236
- // If you don't know the type you are parsing, you can use the non-generic method
237
- IBObject bobject = parser . ParseString ( " 12:Hellow World! " );
203
+ // Convert Torrent to its BDictionary representation
204
+ BDictionary bdictinoary = torrent . ToBDictionary ( );
238
205
```
239
206
207
+ #### File modes
208
+ The property ` FileMode ` indicates if the torrent is single-file or multi-file.
209
+
210
+ For single-file torrents the ` File ` property contains the relevant file info.
211
+ The ` Files ` property is null.
212
+
213
+ For multi-file torrents the ` Files ` property contains a list of file info and the directory name.
214
+ The ` File ` property is null.
215
+
216
+ #### Non-standard fields
217
+ The ` ExtraFields ` property is for any non-standard fields which are not accessible through any other property.
218
+ Data set on this property will overwrite any data from the ` Torrent ` itself when encoding it. This way you are able to add to or owerwrite fields.
219
+
220
+
221
+ Roadmap
222
+ -------
223
+ The project is in maintenance mode and no new features are currently planned.
224
+ Feel free to request new features by creating a new issue.
225
+
226
+
227
+ Building the project
228
+ ---------------------------------
229
+ Requirements:
230
+ - .NET Core 3.0 SDK
231
+ - Visual Studio 2019 (or other IDE support .NET Core 3.0)
232
+
233
+ Simply checkout the project, restore nuget packages and build the project.
234
+
235
+
236
+ Contributing
237
+ ------------
238
+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
239
+
240
+ Please make sure to update or add tests as appropriate.
241
+
242
+
243
+ Support
244
+ -------
245
+ If you use this or one of my other libraries and want to say thank you or support the development feel free to buy me a Red Bull through [ buymeacoffee.com] ( https://www.buymeacoffee.com/UCkS2tw ) or through [ ko-fi.com] ( https://ko-fi.com/krusen ) .
246
+
247
+ <a href =" https://www.buymeacoffee.com/UCkS2tw " target =" _blank " ><img src =" https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png " alt =" Buy Me A Coffee " style =" height : auto !important ;width : auto !important ;" ></a >
248
+
240
249
241
250
## License
242
- [ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=large )] ( https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_large )
251
+ [ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=large )] ( https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_large )
0 commit comments