-
Notifications
You must be signed in to change notification settings - Fork 0
/
bio.lua
174 lines (143 loc) · 3.9 KB
/
bio.lua
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
173
174
-- binary input/output
local ffi = require "ffi"
local bit = require "bit"
-- ZigZag encoding to map signed to unsigned integers
local function s2u(n)
return bit.bxor(bit.lshift(n, 1), bit.arshift(n, 31))
end
-- ZigZag decoding to map unsigned back to signed integers
local function u2s(n)
return bit.bxor(bit.rshift(n, 1), -bit.band(n, 1))
end
local function read_byte(fp)
return fp:read(1):byte(1)
end
local function read_leu16(fp)
return read_byte(fp) + bit.lshift(read_byte(fp), 8)
end
local function read_leu32(fp)
return read_leu16(fp) + bit.lshift(read_leu16(fp), 16)
end
local function read_beu16(fp)
return bit.lshift(read_byte(fp), 8) + read_byte(fp)
end
local function read_beu32(fp)
return bit.lshift(read_beu16(fp), 16) + read_beu16(fp)
end
local function signed(u, nbits)
local p = 2^(nbits-1)
if u >= p then u = u - p - p end
return u
end
local function read_lei16(fp)
return signed(read_leu16(fp), 16)
end
local function read_lei32(fp)
return signed(read_leu32(fp), 32)
end
local function read_bei16(fp)
return signed(read_beu16(fp), 16)
end
local function read_bei32(fp)
return signed(read_beu32(fp), 32)
end
local function read_led64(fp)
return ffi.cast("double *", ffi.new("char[8]", fp:read(8)))[0]
end
local function write_byte(fp, n)
fp:write(string.char(n))
end
local function write_beu16(fp, n)
write_byte(fp, bit.rshift(n, 8))
write_byte(fp, bit.band(n, 0xFF))
end
local function write_beu32(fp, n)
write_beu16(fp, bit.rshift(n, 16))
write_beu16(fp, bit.band(n, 0xFFFF))
end
local function write_lei16(fp, n)
write_byte(fp, bit.band(n, 0xFF))
write_byte(fp, bit.rshift(n, 8))
end
local function write_bei16(fp, n)
if n < 0 then
n = n + 0x10000
end
write_beu16(fp, n)
end
local RiceR = {}
RiceR.__index = RiceR
function RiceR:get_bit()
if self.n == 0 then
self.b = read_byte(self.fp)
self.n = 8
end
self.n = self.n - 1
return bit.band(bit.rshift(self.b, self.n), 1)
end
function RiceR:get_unsigned()
local q = 0
while self:get_bit() == 1 do
q = q + 1
end
local r = 0
for i = 1, self.k do
r = bit.bor(bit.lshift(r, 1), self:get_bit())
end
return bit.bor(bit.lshift(q, self.k), r)
end
function RiceR:get_signed()
return u2s(self:get_unsigned())
end
local function rice_r(fp, k)
local self = setmetatable({}, RiceR)
self.fp = fp -- already opened file, read mode
self.k = k or 0 -- rice parameter
self.b = 0 -- value of last byte read
self.n = 0 -- number of bits available in self.b
return self
end
local RiceW = {}
RiceW.__index = RiceW
function RiceW:put_bit(b)
self.n = self.n - 1
self.b = bit.bor(self.b, bit.lshift(b, self.n))
if self.n == 0 then
self:flush()
end
end
function RiceW:flush()
if self.n ~= 8 then
write_byte(self.fp, self.b)
self.b = 0
self.n = 8
end
end
function RiceW:put_unsigned(n)
for i = 1, bit.rshift(n, self.k) do
self:put_bit(1)
end
self:put_bit(0)
for i = self.k-1, 0, -1 do
self:put_bit(bit.band(bit.rshift(n, i), 1))
end
end
function RiceW:put_signed(n)
self:put_unsigned(s2u(n))
end
local function rice_w(fp, k)
local self = setmetatable({}, RiceW)
self.fp = fp -- already opened file, write mode
self.k = k or 0 -- rice parameter
self.b = 0 -- value of next byte to write
self.n = 8 -- number of bits available in self.b
return self
end
return {
read_byte=read_byte, read_leu16=read_leu16, read_leu32=read_leu32,
read_beu16=read_beu16, read_beu32=read_beu32, read_lei16=read_lei16,
read_lei32=read_lei32, read_bei16=read_bei16, read_bei32=read_bei32,
read_led64=read_led64, write_byte=write_byte, write_beu16=write_beu16,
write_beu32=write_beu32, write_lei16=write_lei16, write_bei16=write_bei16,
rice_r=rice_r, rice_w=rice_w
}