Add files via upload
This commit is contained in:
parent
0cd04e6570
commit
1058da074c
@ -12,17 +12,57 @@ local MIN_INT32 = -2147483648
|
||||
local MAX_INT64 = 9223372036854775807
|
||||
local MIN_INT64 = -9223372036854775808
|
||||
|
||||
local function intToByte(num)
|
||||
local function maskHighBytes(num)
|
||||
return bit.band(num, 0xFF)
|
||||
end
|
||||
|
||||
local function reverse(tab)
|
||||
for i = 1, math.floor(#tab, 2), 1 do
|
||||
tab[i], tab[#tab-i+1] = tab[#tab-i+1], tab[i]
|
||||
end
|
||||
return tab
|
||||
local function reverse(tbl)
|
||||
for i=1, math.floor(#tbl / 2) do
|
||||
local tmp = tbl[i]
|
||||
tbl[i] = tbl[#tbl - i + 1]
|
||||
tbl[#tbl - i + 1] = tmp
|
||||
end
|
||||
return tbl
|
||||
end
|
||||
|
||||
local orders = { "LE", "BE" }
|
||||
|
||||
local fromLEConvertors =
|
||||
{
|
||||
LE = function(bytes) return bytes end,
|
||||
BE = function(bytes) return reverse(bytes) end
|
||||
}
|
||||
|
||||
local toLEConvertors =
|
||||
{
|
||||
LE = function(bytes) return bytes end,
|
||||
BE = function(bytes) return reverse(bytes) end
|
||||
}
|
||||
|
||||
bit_converter.default_order = "LE"
|
||||
|
||||
local function fromLE(bytes, orderTo)
|
||||
if orderTo then
|
||||
bit_converter.validate_order(orderTo)
|
||||
return fromLEConvertors[orderTo](bytes)
|
||||
else return bytes end
|
||||
end
|
||||
|
||||
local function toLE(bytes, orderFrom)
|
||||
if orderFrom then
|
||||
bit_converter.validate_order(orderFrom)
|
||||
return toLEConvertors[orderFrom](bytes)
|
||||
else return bytes end
|
||||
end
|
||||
|
||||
function bit_converter.validate_order(order)
|
||||
if not bit_converter.is_valid_order(order) then
|
||||
error("invalid order: "..order)
|
||||
end
|
||||
end
|
||||
|
||||
function bit_converter.is_valid_order(order) return table.has(orders, order) end
|
||||
|
||||
function bit_converter.string_to_bytes(str)
|
||||
local bytes = { }
|
||||
|
||||
@ -83,19 +123,12 @@ local function floatOrDoubleToBytes(val, opt)
|
||||
bytes[#bytes + 1] = math.floor(sign * 128 + val) % (2 ^ 8)
|
||||
val = math.floor((sign * 128 + val) / (2 ^ 8))
|
||||
|
||||
if not endianness then
|
||||
reverse(bytes)
|
||||
end
|
||||
return bytes
|
||||
end
|
||||
|
||||
local function bytesToFloatOrDouble(bytes, opt)
|
||||
local n = (opt == 'd') and 8 or 4
|
||||
|
||||
if not endianness then
|
||||
reverse(bytes)
|
||||
end
|
||||
|
||||
local sign = 1
|
||||
local mantissa = bytes[n - 1] % ((opt == 'd') and 16 or 128)
|
||||
for i = n - 2, 1, -1 do
|
||||
@ -117,80 +150,128 @@ end
|
||||
|
||||
--
|
||||
|
||||
function bit_converter.single_to_bytes(float)
|
||||
return floatOrDoubleToBytes(float, 'f')
|
||||
function bit_converter.float32_to_bytes(float, order)
|
||||
return fromLE(floatOrDoubleToBytes(float, 'f'), order)
|
||||
end
|
||||
|
||||
function bit_converter.double_to_bytes(double)
|
||||
return floatOrDoubleToBytes(double, 'd')
|
||||
function bit_converter.float64_to_bytes(float, order)
|
||||
return fromLE(floatOrDoubleToBytes(float, 'd'), order)
|
||||
end
|
||||
|
||||
function bit_converter.uint32_to_bytes(int)
|
||||
function bit_converter.single_to_bytes(float, order)
|
||||
on_deprecated_call("bit_converter.float_to_bytes", "bit_converter.float32_to_bytes")
|
||||
return bit_converter.float32_to_bytes(bytes, order)
|
||||
end
|
||||
|
||||
function bit_converter.double_to_bytes(double, order)
|
||||
on_deprecated_call("bit_converter.double_to_bytes", "bit_converter.float64_to_bytes")
|
||||
return bit_converter.float64_to_bytes(bytes, order)
|
||||
end
|
||||
|
||||
local function uint32ToBytes(int, order)
|
||||
return fromLE({
|
||||
maskHighBytes(bit.rshift(int, 24)),
|
||||
maskHighBytes(bit.rshift(int, 16)),
|
||||
maskHighBytes(bit.rshift(int, 8)),
|
||||
maskHighBytes(int)
|
||||
}, order)
|
||||
end
|
||||
|
||||
local function uint16ToBytes(int, order)
|
||||
return fromLE({
|
||||
maskHighBytes(bit.rshift(int, 8)),
|
||||
maskHighBytes(int)
|
||||
}, order)
|
||||
end
|
||||
|
||||
function bit_converter.uint32_to_bytes(int, order)
|
||||
if int > MAX_UINT32 or int < MIN_UINT32 then
|
||||
error("invalid uint32")
|
||||
end
|
||||
|
||||
return {
|
||||
intToByte(bit.rshift(int, 24)),
|
||||
intToByte(bit.rshift(int, 16)),
|
||||
intToByte(bit.rshift(int, 8)),
|
||||
intToByte(int)
|
||||
}
|
||||
return uint32ToBytes(int, order)
|
||||
end
|
||||
|
||||
function bit_converter.uint16_to_bytes(int)
|
||||
function bit_converter.uint16_to_bytes(int, order)
|
||||
if int > MAX_UINT16 or int < MIN_UINT16 then
|
||||
error("invalid uint16")
|
||||
end
|
||||
|
||||
return {
|
||||
intToByte(bit.rshift(int, 8)),
|
||||
intToByte(int)
|
||||
}
|
||||
return uint16ToBytes(int, order)
|
||||
end
|
||||
|
||||
function bit_converter.int64_to_bytes(int)
|
||||
function bit_converter.int64_to_bytes(int, order)
|
||||
if int > MAX_INT64 or int < MIN_INT64 then
|
||||
error("invalid int64")
|
||||
end
|
||||
|
||||
return {
|
||||
intToByte(bit.rshift(int, 56)),
|
||||
intToByte(bit.rshift(int, 48)),
|
||||
intToByte(bit.rshift(int, 40)),
|
||||
intToByte(bit.rshift(int, 32)),
|
||||
intToByte(bit.rshift(int, 24)),
|
||||
intToByte(bit.rshift(int, 16)),
|
||||
intToByte(bit.rshift(int, 8)),
|
||||
intToByte(int)
|
||||
}
|
||||
return fromLE({
|
||||
maskHighBytes(bit.rshift(int, 56)),
|
||||
maskHighBytes(bit.rshift(int, 48)),
|
||||
maskHighBytes(bit.rshift(int, 40)),
|
||||
maskHighBytes(bit.rshift(int, 32)),
|
||||
maskHighBytes(bit.rshift(int, 24)),
|
||||
maskHighBytes(bit.rshift(int, 16)),
|
||||
maskHighBytes(bit.rshift(int, 8)),
|
||||
maskHighBytes(int)
|
||||
}, order)
|
||||
end
|
||||
|
||||
function bit_converter.int32_to_bytes(int)
|
||||
function bit_converter.int32_to_bytes(int, order)
|
||||
on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint32_to_bytes")
|
||||
|
||||
if int > MAX_INT32 or int < MIN_INT32 then
|
||||
error("invalid int32")
|
||||
end
|
||||
|
||||
return bit_converter.uint32_to_bytes(int + MAX_INT32)
|
||||
return uint32ToBytes(int + MAX_INT32, order)
|
||||
end
|
||||
|
||||
function bit_converter.int16_to_bytes(int)
|
||||
function bit_converter.int16_to_bytes(int, order)
|
||||
on_deprecated_call("bit_converter.int32_to_bytes", "bit_converter.sint16_to_bytes")
|
||||
|
||||
if int > MAX_INT16 or int < MIN_INT16 then
|
||||
error("invalid int16")
|
||||
end
|
||||
|
||||
return bit_converter.uint16_to_bytes(int + MAX_INT16)
|
||||
return uint16ToBytes(int + MAX_INT16, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_single(bytes)
|
||||
return bytesToFloatOrDouble(bytes, 'f')
|
||||
function bit_converter.sint32_to_bytes(int, order)
|
||||
if int > MAX_INT32 or int < MIN_INT32 then
|
||||
error("invalid sint32")
|
||||
end
|
||||
|
||||
return uint32ToBytes(int + MAX_UINT32 + 1, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_double(bytes)
|
||||
return bytesToFloatOrDouble(bytes, 'd')
|
||||
function bit_converter.sint16_to_bytes(int, order)
|
||||
if int > MAX_INT16 or int < MIN_INT16 then
|
||||
error("invalid sint16")
|
||||
end
|
||||
|
||||
return uint16ToBytes(int + MAX_UINT16 + 1, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_string(bytes)
|
||||
function bit_converter.bytes_to_float32(bytes, order)
|
||||
return bytesToFloatOrDouble(toLE(bytes, order), 'f')
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_float64(bytes, order)
|
||||
return bytesToFloatOrDouble(toLE(bytes, order), 'd')
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_single(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_single", "bit_converter.bytes_to_float32")
|
||||
return bit_converter.bytes_to_float32(bytes, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_double(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_double", "bit_converter.bytes_to_float64")
|
||||
return bit_converter.bytes_to_float64(bytes, order)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_string(bytes, order)
|
||||
local len = bit_converter.bytes_to_uint16({ bytes[1], bytes[2] })
|
||||
|
||||
local str = ""
|
||||
@ -206,17 +287,13 @@ function bit_converter.byte_to_bool(byte)
|
||||
return byte ~= 0
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_float(bytes)
|
||||
if #bytes < 8 then
|
||||
error("eof")
|
||||
end
|
||||
error("unsupported operation")
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_uint32(bytes)
|
||||
function bit_converter.bytes_to_uint32(bytes, order)
|
||||
if #bytes < 4 then
|
||||
error("eof")
|
||||
end
|
||||
|
||||
bytes = toLE(bytes, order)
|
||||
|
||||
return
|
||||
bit.bor(
|
||||
bit.bor(
|
||||
@ -226,20 +303,26 @@ function bit_converter.bytes_to_uint32(bytes)
|
||||
bit.lshift(bytes[3], 8)),bytes[4])
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_uint16(bytes)
|
||||
function bit_converter.bytes_to_uint16(bytes, order)
|
||||
if #bytes < 2 then
|
||||
error("eof")
|
||||
end
|
||||
|
||||
bytes = toLE(bytes, order)
|
||||
|
||||
return
|
||||
bit.bor(
|
||||
bit.lshift(bytes[1], 8),
|
||||
bytes[2], 0)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_int64(bytes)
|
||||
function bit_converter.bytes_to_int64(bytes, order)
|
||||
if #bytes < 8 then
|
||||
error("eof")
|
||||
end
|
||||
|
||||
bytes = toLE(bytes, order)
|
||||
|
||||
return
|
||||
bit.bor(
|
||||
bit.bor(
|
||||
@ -257,12 +340,26 @@ function bit_converter.bytes_to_int64(bytes)
|
||||
bit.lshift(bit.band(bytes[7], 0xFF), 8)),bit.band(bytes[8], 0xFF))
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_int32(bytes)
|
||||
return bit_converter.bytes_to_uint32(bytes) - MAX_INT32
|
||||
function bit_converter.bytes_to_int32(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_int32", "bit_converter.bytes_to_sint32")
|
||||
return bit_converter.bytes_to_uint32(bytes, order) - MAX_INT32
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_int16(bytes)
|
||||
return bit_converter.bytes_to_uint16(bytes) - MAX_INT16
|
||||
function bit_converter.bytes_to_int16(bytes, order)
|
||||
on_deprecated_call("bit_converter.bytes_to_int16", "bit_converter.bytes_to_sint16")
|
||||
return bit_converter.bytes_to_uint16(bytes, order) - MAX_INT16
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_sint32(bytes, order)
|
||||
local num = bit_converter.bytes_to_uint32(bytes, order)
|
||||
|
||||
return MIN_INT32 * (bit.band(MAX_INT32 + 1, num) ~= 0 and 1 or 0) + bit.band(MAX_INT32, num)
|
||||
end
|
||||
|
||||
function bit_converter.bytes_to_sint16(bytes, order)
|
||||
local num = bit_converter.bytes_to_uint16(bytes, order)
|
||||
|
||||
return MIN_INT16 * (bit.band(MAX_INT16 + 1, num) ~= 0 and 1 or 0) + bit.band(MAX_INT16, num)
|
||||
end
|
||||
|
||||
return bit_converter
|
||||
|
||||
@ -18,22 +18,31 @@ local TYPE_UINT32 = 2
|
||||
local TYPE_INT16 = 3
|
||||
local TYPE_INT32 = 4
|
||||
local TYPE_INT64 = 5
|
||||
local TYPE_DOUBLE = 6
|
||||
local TYPE_FLOAT64 = 6
|
||||
local TYPE_SINT16 = 7
|
||||
local TYPE_SINT32 = 8
|
||||
|
||||
-- Data buffer
|
||||
|
||||
local data_buffer =
|
||||
{
|
||||
__call =
|
||||
function(data_buffer, bytes)
|
||||
return data_buffer:new(bytes)
|
||||
function(data_buffer, ...)
|
||||
return data_buffer:new(...)
|
||||
end
|
||||
}
|
||||
|
||||
function data_buffer:new(bytes)
|
||||
function data_buffer:new(bytes, order, useBytearray)
|
||||
bytes = bytes or { }
|
||||
|
||||
if order then bit_converter.validate_order(order)
|
||||
else order = bit_converter.default_order end
|
||||
|
||||
local obj = {
|
||||
pos = 1,
|
||||
bytes = Bytearray(bytes or { })
|
||||
order = order,
|
||||
useBytearray = useBytearray or false,
|
||||
bytes = useBytearray and Bytearray(bytes) or bytes
|
||||
}
|
||||
|
||||
self.__index = self
|
||||
@ -42,6 +51,13 @@ function data_buffer:new(bytes)
|
||||
return obj
|
||||
end
|
||||
|
||||
function data_buffer:set_order(order)
|
||||
bit_converter.validate_order(order)
|
||||
|
||||
self.order = order
|
||||
self.floatsOrder = order
|
||||
end
|
||||
|
||||
-- Push functions
|
||||
|
||||
function data_buffer:put_byte(byte)
|
||||
@ -49,7 +65,8 @@ function data_buffer:put_byte(byte)
|
||||
error("invalid byte")
|
||||
end
|
||||
|
||||
self.bytes:insert(self.pos, byte)
|
||||
if self.useBytearray then self.bytes:insert(self.pos, byte)
|
||||
else table.insert(self.bytes, self.pos, byte) end
|
||||
|
||||
self.pos = self.pos + 1
|
||||
end
|
||||
@ -61,11 +78,21 @@ function data_buffer:put_bytes(bytes)
|
||||
end
|
||||
|
||||
function data_buffer:put_single(single)
|
||||
self:put_bytes(bit_converter.single_to_bytes(single))
|
||||
on_deprecated_call("data_buffer:put_single", "data_buffer:put_float32")
|
||||
self:put_bytes(bit_converter.single_to_bytes(single, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_double(double)
|
||||
self:put_bytes(bit_converter.double_to_bytes(double))
|
||||
on_deprecated_call("data_buffer:put_single", "data_buffer:put_float64")
|
||||
self:put_bytes(bit_converter.double_to_bytes(double, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_float32(single)
|
||||
self:put_bytes(bit_converter.float32_to_bytes(single, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_float64(float)
|
||||
self:put_bytes(bit_converter.float64_to_bytes(float, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_string(str)
|
||||
@ -77,23 +104,33 @@ function data_buffer:put_bool(bool)
|
||||
end
|
||||
|
||||
function data_buffer:put_uint16(uint16)
|
||||
self:put_bytes(bit_converter.uint16_to_bytes(uint16))
|
||||
self:put_bytes(bit_converter.uint16_to_bytes(uint16, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_uint32(uint32)
|
||||
self:put_bytes(bit_converter.uint32_to_bytes(uint32))
|
||||
self:put_bytes(bit_converter.uint32_to_bytes(uint32, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_int16(int16)
|
||||
self:put_bytes(bit_converter.int16_to_bytes(int16))
|
||||
on_deprecated_call("data_buffer:put_int16", "data_buffer:put_sint16")
|
||||
self:put_bytes(bit_converter.int16_to_bytes(int16, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_int32(int32)
|
||||
self:put_bytes(bit_converter.int32_to_bytes(int32))
|
||||
on_deprecated_call("data_buffer:put_int32", "data_buffer:put_sint32")
|
||||
self:put_bytes(bit_converter.int32_to_bytes(int32, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_sint16(int16)
|
||||
self:put_bytes(bit_converter.sint16_to_bytes(int16, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_sint32(int32)
|
||||
self:put_bytes(bit_converter.sint32_to_bytes(int32, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_int64(int64)
|
||||
self:put_bytes(bit_converter.int64_to_bytes(int64))
|
||||
self:put_bytes(bit_converter.int64_to_bytes(int64, self.order))
|
||||
end
|
||||
|
||||
function data_buffer:put_number(num)
|
||||
@ -101,8 +138,8 @@ function data_buffer:put_number(num)
|
||||
local type
|
||||
|
||||
if math.floor(num) ~= num then
|
||||
type = TYPE_DOUBLE
|
||||
bytes = bit_converter.double_to_bytes(num)
|
||||
type = TYPE_FLOAT64
|
||||
bytes = bit_converter.float64_to_bytes(num)
|
||||
elseif num == 0 then
|
||||
type = TYPE_ZERO
|
||||
bytes = { }
|
||||
@ -119,11 +156,11 @@ function data_buffer:put_number(num)
|
||||
end
|
||||
elseif num < 0 then
|
||||
if num >= MIN_INT16 then
|
||||
type = TYPE_INT16
|
||||
bytes = bit_converter.int16_to_bytes(num)
|
||||
type = TYPE_SINT16
|
||||
bytes = bit_converter.sint16_to_bytes(num)
|
||||
elseif num >= MIN_INT32 then
|
||||
type = TYPE_INT32
|
||||
bytes = bit_converter.int32_to_bytes(num)
|
||||
type = TYPE_SINT32
|
||||
bytes = bit_converter.sint32_to_bytes(num)
|
||||
elseif num >= MIN_INT64 then
|
||||
type = TYPE_INT64
|
||||
bytes = bit_converter.int64_to_bytes(num)
|
||||
@ -155,9 +192,13 @@ function data_buffer:get_number()
|
||||
return self:get_int16()
|
||||
elseif type == TYPE_INT32 then
|
||||
return self:get_int32()
|
||||
elseif type == TYPE_SINT16 then
|
||||
return self:get_sint16()
|
||||
elseif type == TYPE_SINT32 then
|
||||
return self:get_sint32()
|
||||
elseif type == TYPE_INT64 then
|
||||
return self:get_int64()
|
||||
elseif type == TYPE_DOUBLE then
|
||||
elseif type == TYPE_FLOAT64 then
|
||||
return self:get_double()
|
||||
else
|
||||
error("unknown lua number type: "..type)
|
||||
@ -165,11 +206,21 @@ function data_buffer:get_number()
|
||||
end
|
||||
|
||||
function data_buffer:get_single()
|
||||
return bit_converter.bytes_to_single(self:get_bytes(4))
|
||||
on_deprecated_call("data_buffer:get_single", "data_buffer:get_float32")
|
||||
return bit_converter.bytes_to_single(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_double()
|
||||
return bit_converter.bytes_to_double(self:get_bytes(8))
|
||||
on_deprecated_call("data_buffer:get_double", "data_buffer:get_float64")
|
||||
return bit_converter.bytes_to_double(self:get_bytes(8), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_float32()
|
||||
return bit_converter.bytes_to_float32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_float64()
|
||||
return bit_converter.bytes_to_float64(self:get_bytes(8), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_string()
|
||||
@ -193,23 +244,33 @@ function data_buffer:get_bool()
|
||||
end
|
||||
|
||||
function data_buffer:get_uint16()
|
||||
return bit_converter.bytes_to_uint16(self:get_bytes(2))
|
||||
return bit_converter.bytes_to_uint16(self:get_bytes(2), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_uint32()
|
||||
return bit_converter.bytes_to_uint32(self:get_bytes(4))
|
||||
return bit_converter.bytes_to_uint32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_int16()
|
||||
return bit_converter.bytes_to_int16(self:get_bytes(2))
|
||||
on_deprecated_call("data_buffer:get_int16", "data_buffer:get_sint16")
|
||||
return bit_converter.bytes_to_int16(self:get_bytes(2), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_int32()
|
||||
return bit_converter.bytes_to_int32(self:get_bytes(4))
|
||||
on_deprecated_call("data_buffer:get_int32", "data_buffer:get_sint32")
|
||||
return bit_converter.bytes_to_int32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_sint16()
|
||||
return bit_converter.bytes_to_sint16(self:get_bytes(2), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_sint32()
|
||||
return bit_converter.bytes_to_sint32(self:get_bytes(4), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:get_int64()
|
||||
return bit_converter.bytes_to_int64(self:get_bytes(8))
|
||||
return bit_converter.bytes_to_int64(self:get_bytes(8), self.order)
|
||||
end
|
||||
|
||||
function data_buffer:size()
|
||||
@ -247,4 +308,4 @@ end
|
||||
|
||||
setmetatable(data_buffer, data_buffer)
|
||||
|
||||
return data_buffer
|
||||
return data_buffer
|
||||
Loading…
x
Reference in New Issue
Block a user