bcoder 1.1__py3-none-any.whl → 2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bcoder might be problematic. Click here for more details.
- bcoder/decoder.py +31 -43
- bcoder/encoder.py +7 -21
- {bcoder-1.1.dist-info → bcoder-2.0.dist-info}/METADATA +9 -8
- bcoder-2.0.dist-info/RECORD +9 -0
- {bcoder-1.1.dist-info → bcoder-2.0.dist-info}/WHEEL +1 -1
- bcoder-1.1.dist-info/RECORD +0 -9
- {bcoder-1.1.dist-info → bcoder-2.0.dist-info/licenses}/LICENSE +0 -0
- {bcoder-1.1.dist-info → bcoder-2.0.dist-info}/top_level.txt +0 -0
bcoder/decoder.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
|
-
from typing import
|
|
2
|
+
from typing import Union
|
|
3
3
|
|
|
4
4
|
from .errors import DecodeError
|
|
5
5
|
|
|
@@ -24,9 +24,6 @@ class DecodeType(Enum):
|
|
|
24
24
|
|
|
25
25
|
class BDecoder:
|
|
26
26
|
def __init__(self):
|
|
27
|
-
self._data = None
|
|
28
|
-
self._i = None
|
|
29
|
-
|
|
30
27
|
self._decoders = {
|
|
31
28
|
DecodeType.DICTIONARY: self._decode_dict,
|
|
32
29
|
DecodeType.STRING: self._decode_str,
|
|
@@ -35,64 +32,55 @@ class BDecoder:
|
|
|
35
32
|
}
|
|
36
33
|
|
|
37
34
|
def decode(self, data: bytes) -> BDecodeType:
|
|
38
|
-
|
|
39
|
-
self._i = 0
|
|
35
|
+
value, _ = self._decode(data, 0)
|
|
40
36
|
|
|
41
|
-
return
|
|
37
|
+
return value
|
|
42
38
|
|
|
43
|
-
def
|
|
44
|
-
return self._decoders[DecodeType.from_code(
|
|
39
|
+
def _decode(self, data: bytes, i: int) -> tuple[BDecodeType, int]:
|
|
40
|
+
return self._decoders[DecodeType.from_code(data[i])](data, i)
|
|
45
41
|
|
|
46
|
-
def _decode_dict(self) -> dict[bytes, BDecodeType]:
|
|
42
|
+
def _decode_dict(self, data: bytes, i: int) -> tuple[dict[bytes, BDecodeType], int]:
|
|
47
43
|
result = {}
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
while self._data[self._i] != ord("e"):
|
|
52
|
-
key = self._decode_str()
|
|
53
|
-
result[key] = self._get_decoder()()
|
|
45
|
+
i += 1
|
|
54
46
|
|
|
55
|
-
|
|
47
|
+
while data[i] != ord("e"):
|
|
48
|
+
key, i = self._decode_str(data, i)
|
|
49
|
+
result[key], i = self._decode(data, i)
|
|
56
50
|
|
|
57
|
-
return result
|
|
51
|
+
return result, i + 1
|
|
58
52
|
|
|
59
|
-
def _decode_str(self) -> bytes:
|
|
60
|
-
if
|
|
61
|
-
raise DecodeError(f"Invalid leading '0' value for str in {
|
|
53
|
+
def _decode_str(self, data: bytes, i: int) -> tuple[bytes, int]:
|
|
54
|
+
if data[i] == ord("0") and data[i + 1] != ord(":"):
|
|
55
|
+
raise DecodeError(f"Invalid leading '0' value for str in {i} position")
|
|
62
56
|
|
|
63
|
-
colon =
|
|
64
|
-
length = int(
|
|
57
|
+
colon = data.index(b":", i)
|
|
58
|
+
length = int(data[i:colon])
|
|
65
59
|
|
|
66
60
|
start = colon + 1
|
|
67
61
|
end = start + length
|
|
68
62
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return self._data[start:end]
|
|
63
|
+
return data[start:end], end
|
|
72
64
|
|
|
73
|
-
def _decode_list(self) -> list[BDecodeType]:
|
|
65
|
+
def _decode_list(self, data: bytes, i: int) -> tuple[list[BDecodeType], int]:
|
|
74
66
|
result = []
|
|
75
67
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
while self._data[self._i] != ord("e"):
|
|
79
|
-
result.append(self._get_decoder()())
|
|
80
|
-
|
|
81
|
-
self._i += 1
|
|
68
|
+
i += 1
|
|
82
69
|
|
|
83
|
-
|
|
70
|
+
while data[i] != ord("e"):
|
|
71
|
+
value, i = self._decode(data, i)
|
|
72
|
+
result.append(value)
|
|
84
73
|
|
|
85
|
-
|
|
86
|
-
self._i += 1
|
|
74
|
+
return result, i + 1
|
|
87
75
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if self._data[self._i] == ord("0") and self._data[self._i + 1] != ord("e"):
|
|
91
|
-
raise DecodeError(f"Invalid leading '0' value for int in {self._i} position")
|
|
76
|
+
def _decode_int(self, data: bytes, i: int) -> tuple[int, int]:
|
|
77
|
+
i += 1
|
|
92
78
|
|
|
93
|
-
|
|
94
|
-
|
|
79
|
+
if data[i] == ord("-") and data[i + 1] == ord("0"):
|
|
80
|
+
raise DecodeError(f"Invalid '-0' value for int in {i} position")
|
|
81
|
+
if data[i] == ord("0") and data[i + 1] != ord("e"):
|
|
82
|
+
raise DecodeError(f"Invalid leading '0' value for int in {i} position")
|
|
95
83
|
|
|
96
|
-
|
|
84
|
+
end = data.index(b"e", i)
|
|
97
85
|
|
|
98
|
-
return int(
|
|
86
|
+
return int(data[i:end]), end + 1
|
bcoder/encoder.py
CHANGED
|
@@ -2,8 +2,7 @@ from enum import Enum
|
|
|
2
2
|
from typing import Callable, Union
|
|
3
3
|
|
|
4
4
|
BEncodeType = Union[
|
|
5
|
-
dict[
|
|
6
|
-
str,
|
|
5
|
+
dict[bytes, "BEncodeType"],
|
|
7
6
|
list["BEncodeType"],
|
|
8
7
|
int,
|
|
9
8
|
bytes,
|
|
@@ -12,7 +11,6 @@ BEncodeType = Union[
|
|
|
12
11
|
|
|
13
12
|
class EncodeType(Enum):
|
|
14
13
|
DICTIONARY = dict
|
|
15
|
-
STRING = str
|
|
16
14
|
LIST = list
|
|
17
15
|
INTEGER = int
|
|
18
16
|
BYTES = bytes
|
|
@@ -23,12 +21,9 @@ class EncodeType(Enum):
|
|
|
23
21
|
|
|
24
22
|
|
|
25
23
|
class BEncoder:
|
|
26
|
-
def __init__(self
|
|
27
|
-
self._encoding = encoding
|
|
28
|
-
|
|
24
|
+
def __init__(self):
|
|
29
25
|
self._encoders = {
|
|
30
26
|
EncodeType.DICTIONARY: self._encode_dict,
|
|
31
|
-
EncodeType.STRING: self._encode_str,
|
|
32
27
|
EncodeType.LIST: self._encode_list,
|
|
33
28
|
EncodeType.INTEGER: self._encode_int,
|
|
34
29
|
EncodeType.BYTES: self._encode_bytes,
|
|
@@ -40,32 +35,23 @@ class BEncoder:
|
|
|
40
35
|
def _get_encoder(self, value: BEncodeType) -> Callable[["BEncoder", BEncodeType], bytes]:
|
|
41
36
|
return self._encoders[EncodeType.from_value(value)]
|
|
42
37
|
|
|
43
|
-
def _encode_dict(self, d: dict[
|
|
38
|
+
def _encode_dict(self, d: dict[bytes, BEncodeType]) -> bytes:
|
|
44
39
|
encoded_dict = b"".join(
|
|
45
|
-
self.
|
|
46
|
-
for k, v in d.items()
|
|
40
|
+
self._encode_bytes(k) + self._get_encoder(v)(v)
|
|
41
|
+
for k, v in sorted(d.items())
|
|
47
42
|
)
|
|
48
43
|
|
|
49
44
|
return b"d" + encoded_dict + b"e"
|
|
50
45
|
|
|
51
|
-
def _encode_str(self, s: str) -> bytes:
|
|
52
|
-
length = str(len(s)).encode(self._encoding)
|
|
53
|
-
encoded_str = s.encode(self._encoding)
|
|
54
|
-
|
|
55
|
-
return length + b":" + encoded_str
|
|
56
|
-
|
|
57
46
|
def _encode_list(self, l: list) -> bytes:
|
|
58
47
|
encoded_values = b"".join(
|
|
59
48
|
self._get_encoder(v)(v)
|
|
60
49
|
for v in l
|
|
61
50
|
)
|
|
62
|
-
|
|
63
51
|
return b"l" + encoded_values + b"e"
|
|
64
52
|
|
|
65
53
|
def _encode_int(self, i: int) -> bytes:
|
|
66
|
-
return b"i" + str(i).encode(
|
|
54
|
+
return b"i" + str(i).encode() + b"e"
|
|
67
55
|
|
|
68
56
|
def _encode_bytes(self, b: bytes) -> bytes:
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return length + b":" + b
|
|
57
|
+
return str(len(b)).encode() + b":" + b
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: bcoder
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0
|
|
4
4
|
Summary: Tool to decode and encode .torrent files
|
|
5
5
|
Author-email: Mykola Bezrukyi <kolya.bezrukiy.03@gmail.com>
|
|
6
6
|
License: Copyright (c) 2025 Mykola Bezrukyi
|
|
@@ -15,6 +15,7 @@ Project-URL: Homepage, https://github.com/mbezrukyi/bcoder
|
|
|
15
15
|
Requires-Python: >=3.13
|
|
16
16
|
Description-Content-Type: text/markdown
|
|
17
17
|
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
18
19
|
|
|
19
20
|
# bcoder
|
|
20
21
|
|
|
@@ -53,16 +54,16 @@ encoder = BEncoder()
|
|
|
53
54
|
|
|
54
55
|
data = {
|
|
55
56
|
b"announce-list": [
|
|
56
|
-
["udp:..."],
|
|
57
|
-
["udp:..."],
|
|
57
|
+
[b"udp:..."],
|
|
58
|
+
[b"udp:..."],
|
|
58
59
|
],
|
|
59
|
-
"info": {
|
|
60
|
-
"piece": b"xfsasf",
|
|
61
|
-
"piece length": 123,
|
|
60
|
+
b"info": {
|
|
61
|
+
b"piece": b"xfsasf",
|
|
62
|
+
b"piece length": 123,
|
|
62
63
|
},
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
encoded = encoder.encode(data)
|
|
66
67
|
```
|
|
67
68
|
|
|
68
|
-
__NOTE__: `BEncoder`
|
|
69
|
+
__NOTE__: `BEncoder` is spec-strict (BEP 3). Only `bytes`, `int`, `list`, and `dict` with `bytes` keys are accepted. Pass `str` through `.encode("utf-8")` yourself if you need to encode text.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
bcoder/__init__.py,sha256=YhoSA1fEI8h_bS5RaaSTzBs-k0NyX74CapqS3Tx9KSY,108
|
|
2
|
+
bcoder/decoder.py,sha256=6UQr7BLr3idzhCGW3IpS5Wsus7QxY9LsOxlguWTnrmQ,2331
|
|
3
|
+
bcoder/encoder.py,sha256=9jS8TwT-IGO42ojLhrat68Ypuc856HSrfU06BZE43WY,1576
|
|
4
|
+
bcoder/errors.py,sha256=f_TLCf8wYOHxzKEa85-N1k9NHMk9pGhFut8FzpICr1c,40
|
|
5
|
+
bcoder-2.0.dist-info/licenses/LICENSE,sha256=ZrFAe1-xWQ3Sg7hEdYw9wVMIv7LnAcUL55OyqZxATB8,1059
|
|
6
|
+
bcoder-2.0.dist-info/METADATA,sha256=-i-8cL5j3SqNUb5ucoiSzhEjNNSypBsIVuaSTiOZ1RM,2487
|
|
7
|
+
bcoder-2.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
8
|
+
bcoder-2.0.dist-info/top_level.txt,sha256=u-ORiwqxmd9H_2ZxoVU2WY6AIDJ2adI5KxA734GIceY,7
|
|
9
|
+
bcoder-2.0.dist-info/RECORD,,
|
bcoder-1.1.dist-info/RECORD
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
bcoder/__init__.py,sha256=YhoSA1fEI8h_bS5RaaSTzBs-k0NyX74CapqS3Tx9KSY,108
|
|
2
|
-
bcoder/decoder.py,sha256=1HLnbIBNmJkLo2ucz03NJOEZEf0hogPzatxlXlwLoSo,2512
|
|
3
|
-
bcoder/encoder.py,sha256=_wSgHp0Gdp7Ge652qOSGnbxS5pq693HpGvjZG_7k-oA,1957
|
|
4
|
-
bcoder/errors.py,sha256=f_TLCf8wYOHxzKEa85-N1k9NHMk9pGhFut8FzpICr1c,40
|
|
5
|
-
bcoder-1.1.dist-info/LICENSE,sha256=ZrFAe1-xWQ3Sg7hEdYw9wVMIv7LnAcUL55OyqZxATB8,1059
|
|
6
|
-
bcoder-1.1.dist-info/METADATA,sha256=ZbJ7fg1ESt8XlmRDRrH1YFL_jaYleJiQOjhfEYMNZUI,2351
|
|
7
|
-
bcoder-1.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
|
8
|
-
bcoder-1.1.dist-info/top_level.txt,sha256=u-ORiwqxmd9H_2ZxoVU2WY6AIDJ2adI5KxA734GIceY,7
|
|
9
|
-
bcoder-1.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|