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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
local bit = require("bit")
local ffi = require("ffi")
local band = bit.band
local bnot = bit.bnot
local m = {}
local Uint8Bound = 256 -- bound is the max uintN + 1
local Uint16Bound = 65536
local Uint32Bound = 4294967296
if not table.unpack then
table.unpack = unpack
end
if not table.pack then
table.pack = pack
end
m.GetAlignSize = function(k, size)
return band((bnot(k) + 1), (size - 1))
end
local function pack_I1(n)
return string.char(n)
end
local function pack_i1(n)
if n < 0 then
n = Uint8Bound + n
end
return pack_I1(n)
end
local function unpack_I1(n, pos)
return string.byte(n, pos)
end
local function unpack_i1(n, pos)
local res = unpack_I1(n, pos)
if res >= Uint8Bound / 2 then
return res - Uint8Bound
end
return res
end
local b2 = ffi.new("unsigned char[2]")
local function pack_I2(n)
for i = 0, 1 do
b2[i] = bit.band(n, 255)
n = bit.rshift(n, 8)
end
return ffi.string(b2, 2)
end
local function pack_i2(n)
if n < 0 then
n = Uint16Bound + n
end
return pack_I2(n)
end
local function unpack_I2(n, pos)
local a, b = string.byte(n, pos, pos + 1)
return b * Uint8Bound + a
end
local function unpack_i2(n, pos)
local res = unpack_I2(n, pos)
if res >= Uint16Bound / 2 then
return res - Uint16Bound
end
return res
end
local b4 = ffi.new("unsigned char[4]")
local function pack_I4(n)
for i = 0, 3 do
b4[i] = bit.band(n, 255)
n = bit.rshift(n, 8)
end
return ffi.string(b4, 4)
end
local function pack_i4(n)
if n < 0 then
n = Uint32Bound + n
end
return pack_I4(n)
end
local function unpack_I4(n, pos)
local a, b, c, d = string.byte(n, pos, pos + 3)
return Uint8Bound * (Uint8Bound * ((Uint8Bound * d) + c) + b) + a
end
local function unpack_i4(n, pos)
local res = unpack_I4(n, pos)
if res >= Uint32Bound / 2 then
return res - Uint32Bound
end
return res
end
local b8 = ffi.new("unsigned char[8]")
local function pack_I8(n)
n = ffi.cast("unsigned long long", n)
local hi = math.floor(tonumber(n / Uint32Bound))
local li = n % Uint32Bound
for i = 0, 3 do
b8[i] = bit.band(li, 255)
li = bit.rshift(li, 8)
end
for i = 4, 7 do
b8[i] = bit.band(hi, 255)
hi = bit.rshift(hi, 8)
end
return ffi.string(b8, 8)
end
local function pack_i8(n)
n = ffi.cast("signed long long", n)
return pack_I8(n)
end
local function unpack_I8(n, pos)
local a, b, c, d = string.byte(n, pos, pos + 3)
local li = Uint8Bound * (Uint8Bound * ((Uint8Bound * d) + c) + b) + a
local a, b, c, d = string.byte(n, pos + 4, pos + 7)
local hi = Uint8Bound * (Uint8Bound * ((Uint8Bound * d) + c) + b) + a
return ffi.cast("unsigned long long", hi) * Uint32Bound + li
end
local function unpack_i8(n, pos)
local res = unpack_I8(n, pos)
return ffi.cast("signed long long", res)
end
local bf = ffi.new("float[1]")
local function pack_f(n)
bf[0] = n
return ffi.string(bf, 4)
end
local function unpack_f(n, pos)
ffi.copy(bf, ffi.cast("char *", n) + pos - 1, 4)
return tonumber(bf[0])
end
local bd = ffi.new("double[1]")
local function pack_d(n)
bd[0] = n
return ffi.string(bd, 8)
end
local function unpack_d(n, pos)
ffi.copy(bd, ffi.cast("char *", n) + pos - 1, 8)
return tonumber(bd[0])
end
m.string_pack = function(fmt, i, ...)
if fmt == "<I1" then
return pack_I1(i)
elseif fmt == "<I2" then
return pack_I2(i)
elseif fmt == "<I4" then
return pack_I4(i)
elseif fmt == "<I8" then
return pack_I8(i)
elseif fmt == "<i1" then
return pack_i1(i)
elseif fmt == "<i2" then
return pack_i2(i)
elseif fmt == "<i4" then
return pack_i4(i)
elseif fmt == "<i8" then
return pack_i8(i)
elseif fmt == "<f" then
return pack_f(i)
elseif fmt == "<d" then
return pack_d(i)
else
error(string.format("FIXME: support fmt %s", fmt))
end
end
m.string_unpack = function(fmt, s, pos)
if not pos then
pos = 1
end
if fmt == "<I1" then
return unpack_I1(s, pos)
elseif fmt == "<I2" then
return unpack_I2(s, pos)
elseif fmt == "<I4" then
return unpack_I4(s, pos)
elseif fmt == "<I8" then
return unpack_I8(s, pos)
elseif fmt == "<i1" then
return unpack_i1(s, pos)
elseif fmt == "<i2" then
return unpack_i2(s, pos)
elseif fmt == "<i4" then
return unpack_i4(s, pos)
elseif fmt == "<i8" then
return unpack_i8(s, pos)
elseif fmt == "<f" then
return unpack_f(s, pos)
elseif fmt == "<d" then
return unpack_d(s, pos)
else
error(string.format("FIXME: support fmt %s", fmt))
end
end
return m
|