forked from Wiladams/LAPHLibs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbencode.lua
More file actions
172 lines (137 loc) · 3.19 KB
/
bencode.lua
File metadata and controls
172 lines (137 loc) · 3.19 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
169
170
171
172
--[[
References:
https://bitbucket.org/wilhelmy/lua-bencode/src
public domain lua-module for handling bittorrent-bencoded data.
This module includes both a recursive decoder and a recursive encoder.
--]]
local floor = math.floor
local sort, concat = table.sort, table.concat
local pairs, ipairs, type = pairs, ipairs, type
local tonumber = tonumber
local exports = {}
local function islist(t)
local n = #t
for k, v in pairs(t) do
if type(k) ~= "number" or floor(k) ~= k or k < 1 or k > n then
return false
end
end
for i = 1, n do
if t[i] == nil then
return false
end
end
return true
end
local function isdictionary(t)
return not islist(t)
end
-- recursively bencode x
local encode_funcs = {
["string"] = function(x)
return #x .. ":" .. x
end,
["number"] = function(x)
if x % 1 ~= 0 then
return nil, "number is not an integer: '" .. x .. "'"
end
return "i" .. x .. "e"
end,
["table"] = function(x)
local ret = {}
if islist(x) then
table.insert(ret, "l")
for k, v in ipairs(x) do
table.insert(ret, exports.encode(v))
end
table.insert(ret,"e")
else -- dictionary
table.insert(ret,"d")
-- bittorrent requires the keys to be sorted.
local sortedkeys = {}
for k, v in pairs(x) do
if type(k) ~= "string" then
return nil, "bencoding requires dictionary keys to be strings"
end
sortedkeys[#sortedkeys + 1] = k
end
sort(sortedkeys)
for k, v in ipairs(sortedkeys) do
table.insert(ret,exports.encode(v))
table.insert(ret,exports.encode(x[v]))
end
table.insert(ret,"e")
end
return table.concat(ret)
end,
}
function exports.encode(x)
local tx = type(x)
local func = encode_funcs[tx]
if not func then
return nil, tx .. " cannot be converted to an acceptable type for bencoding"
end
return func(x)
end
local function decode_integer(s, index)
local a, b, int = s:find("^([0-9]+)e", index)
if not int then
return nil, "not a number: nil"
end
int = tonumber(int)
if not int then
return nil, "not a number: "..int
end
return int, b + 1
end
local function decode_list(s, index)
local t = {}
while s:sub(index, index) ~= "e" do
local obj
obj, index = exports.decode(s, index)
t[#t + 1] = obj
end
index = index + 1
return t, index
end
local function decode_dictionary(s, index)
local t = {}
while s:sub(index, index) ~= "e" do
local obj1
obj1, index = exports.decode(s, index)
local obj2
obj2, index = exports.decode(s, index)
t[obj1] = obj2
end
index = index + 1
return t, index
end
local function decode_string(s, index)
local a, b, len = s:find("^([0-9]+):", index)
if not len then
return nil, "not a length"
end
index = b + 1
local v = s:sub(index, index + len - 1)
index = index + len
return v, index
end
function exports.decode(s, index)
index = index or 1
local t = s:sub(index, index)
if not t then
return nil, "invalid index"
end
if t == "i" then
return decode_integer(s, index + 1)
elseif t == "l" then
return decode_list(s, index + 1)
elseif t == "d" then
return decode_dictionary(s, index + 1)
elseif t >= '0' and t <= '9' then
return decode_string(s, index)
else
return nil, "invalid type"
end
end
return exports