this is a little more elegant
[QuestHelper.git] / collect_bitstream.lua
blob35f2efa741eb1c0e95c73786e6d13504592a54e4
1 QuestHelper_File["collect_bitstream.lua"] = "Development Version"
2 QuestHelper_Loadtime["collect_bitstream.lua"] = GetTime()
4 local Merger
6 local function Output(outbits)
7 return {
8 r = {},
9 cbits = 0,
10 cval = 0,
12 append = function (self, value, bits)
13 self.cbits = self.cbits + bits
14 self.cval = bit.lshift(self.cval, bits)
15 self.cval = self.cval + value
16 while self.cbits >= outbits do
17 Merger.Add(self.r, strchar(bit.rshift(self.cval, self.cbits - outbits)))
18 self.cbits = self.cbits - outbits
19 self.cval = bit.band(self.cval, bit.lshift(1, self.cbits) - 1)
20 end
21 end,
22 finish = function (self)
23 if self.cbits > 0 then self:append(0, outbits - self.cbits) end
24 return Merger.Finish(self.r)
25 end,
27 end
29 local function Input(indata, outbits)
30 return {
31 cbits = 0,
32 cval = 0,
33 coffset = 1,
35 depend = function (self, bits)
36 while self.cbits < bits do
37 self.cbits = self.cbits + outbits
38 self.cval = bit.lshift(self.cval, outbits) + strbyte(indata, self.coffset)
39 self.coffset = self.coffset + 1
40 end
41 local rv = bit.rshift(self.cval, self.cbits - bits)
42 self.cbits = self.cbits - bits;
43 self.cval = bit.band(self.cval, bit.lshift(1, self.cbits) - 1)
44 return rv
45 end,
47 end
49 function QH_Collect_Bitstream_Init(_, API)
50 Merger = API.Utility_Merger
51 QuestHelper: Assert(Merger)
53 API.Utility_Bitstream = {Input = Input, Output = Output}
54 end