forked from flippant/parse
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitreader.lua
More file actions
110 lines (95 loc) · 2.81 KB
/
bitreader.lua
File metadata and controls
110 lines (95 loc) · 2.81 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
--[[
* Addons - Copyright (c) 2023 Ashita Development Team
* Contact: https://www.ashitaxi.com/
* Contact: https://discord.gg/Ashita
*
* This file is part of Ashita.
*
* Ashita 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 3 of the License, or
* (at your option) any later version.
*
* Ashita 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 Ashita. If not, see <https://www.gnu.org/licenses/>.
--]]
require('common');
local bitreader = T{
data = nil,
bit = 0,
pos = 0,
};
--[[
* Creates and returns a new bit reader instance.
*
* @param {table} o - The default object table, if provided.
* @return {table} The bit reader instance.
--]]
function bitreader:new(o)
o = o or T{};
setmetatable(o, self);
self.__index = self;
return o;
end
--[[
* Sets the current reader data.
*
* @param {string|table} data - The data to use with the reader. (Strings are converted to a byte table automatically.)
--]]
function bitreader:set_data(data)
self.bit = 0;
self.data = T{};
self.pos = 0;
switch(type(data), T{
['string'] = function ()
data:tohex():replace(' ', ''):gsub('(%x%x)', function (x)
return table.insert(self.data, tonumber(x, 16));
end);
end,
['table'] = function ()
self.data = data;
end,
[switch.default] = function ()
error('[BitReader] Invalid data type: ' .. type(data));
end,
});
end
--[[
* Sets the current reader position.
*
* @param {number} pos - The byte position to set the reader to. (Resets the bit position.)
--]]
function bitreader:set_pos(pos)
self.bit = 0;
self.pos = pos;
end
--[[
* Reads a packed value from the current data.
*
* @param {number} bits - The number of bits to read.
* @return {number} The read value.
--]]
function bitreader:read(bits)
local ret = 0;
if (self.data == nil) then
return ret;
end
for x = 0, bits - 1 do
local val = bit.lshift(bit.band(self.data[self.pos + 1], 1), x);
self.data[self.pos + 1] = bit.rshift(self.data[self.pos + 1], 1);
ret = bit.bor(ret, val);
self.bit = self.bit + 1;
if (self.bit == 8) then
self.bit = 0;
self.pos = self.pos + 1;
end
end
return ret;
end
-- Return the BitReader table..
return bitreader;