conversion to/from bytes and data buffer modules
This commit is contained in:
parent
72e3f49d42
commit
9c66d87699
@ -4,8 +4,6 @@ local MAX_UINT16 = 65535
|
|||||||
local MIN_UINT16 = 0
|
local MIN_UINT16 = 0
|
||||||
local MAX_UINT32 = 4294967295
|
local MAX_UINT32 = 4294967295
|
||||||
local MIN_UINT32 = 0
|
local MIN_UINT32 = 0
|
||||||
local MAX_UINT64 = 18446744073709551615
|
|
||||||
local MIN_UINT64 = 0
|
|
||||||
|
|
||||||
local MAX_INT16 = 32767
|
local MAX_INT16 = 32767
|
||||||
local MIN_INT16 = -32768
|
local MIN_INT16 = -32768
|
||||||
@ -30,7 +28,7 @@ function bit_converter.string_to_bytes(str)
|
|||||||
|
|
||||||
local len = string.len(str)
|
local len = string.len(str)
|
||||||
|
|
||||||
local lenBytes = bit_converter.int32_to_bytes(len)
|
local lenBytes = bit_converter.uint16_to_bytes(len)
|
||||||
|
|
||||||
for i = 1, #lenBytes do
|
for i = 1, #lenBytes do
|
||||||
bytes[i] = lenBytes[i]
|
bytes[i] = lenBytes[i]
|
||||||
@ -127,23 +125,6 @@ function bit_converter.double_to_bytes(double)
|
|||||||
return floatOrDoubleToBytes(double, 'd')
|
return floatOrDoubleToBytes(double, 'd')
|
||||||
end
|
end
|
||||||
|
|
||||||
function bit_converter.int64_to_bytes(int)
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
function bit_converter.uint32_to_bytes(int)
|
function bit_converter.uint32_to_bytes(int)
|
||||||
if int > MAX_UINT32 or int < MIN_UINT32 then
|
if int > MAX_UINT32 or int < MIN_UINT32 then
|
||||||
error("invalid uint32")
|
error("invalid uint32")
|
||||||
@ -168,6 +149,23 @@ function bit_converter.uint16_to_bytes(int)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function bit_converter.int64_to_bytes(int)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
function bit_converter.int32_to_bytes(int)
|
function bit_converter.int32_to_bytes(int)
|
||||||
if int > MAX_INT32 or int < MIN_INT32 then
|
if int > MAX_INT32 or int < MIN_INT32 then
|
||||||
error("invalid int32")
|
error("invalid int32")
|
||||||
@ -193,7 +191,7 @@ function bit_converter.bytes_to_double(bytes)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function bit_converter.bytes_to_string(bytes)
|
function bit_converter.bytes_to_string(bytes)
|
||||||
local len = bit_converter.bytes_to_int32({ bytes[1], bytes[2], bytes[3], bytes[4]})
|
local len = bit_converter.bytes_to_uint16({ bytes[1], bytes[2] })
|
||||||
|
|
||||||
local str = ""
|
local str = ""
|
||||||
|
|
||||||
@ -215,27 +213,6 @@ function bit_converter.bytes_to_float(bytes)
|
|||||||
error("unsupported operation")
|
error("unsupported operation")
|
||||||
end
|
end
|
||||||
|
|
||||||
function bit_converter.bytes_to_int64(bytes)
|
|
||||||
if #bytes < 8 then
|
|
||||||
error("eof")
|
|
||||||
end
|
|
||||||
return
|
|
||||||
bit.bor(
|
|
||||||
bit.bor(
|
|
||||||
bit.bor(
|
|
||||||
bit.bor(
|
|
||||||
bit.bor(
|
|
||||||
bit.bor(
|
|
||||||
bit.bor(
|
|
||||||
bit.lshift(bytes[1], 56),
|
|
||||||
bit.lshift(bytes[2], 48)),
|
|
||||||
bit.lshift(bytes[3], 40)),
|
|
||||||
bit.lshift(bytes[4], 32)),
|
|
||||||
bit.lshift(bytes[5], 24)),
|
|
||||||
bit.lshift(bit.band(bytes[6], 0xFF), 16)),
|
|
||||||
bit.lshift(bit.band(bytes[7], 0xFF), 8)),bit.band(bytes[8], 0xFF))
|
|
||||||
end
|
|
||||||
|
|
||||||
function bit_converter.bytes_to_uint32(bytes)
|
function bit_converter.bytes_to_uint32(bytes)
|
||||||
if #bytes < 4 then
|
if #bytes < 4 then
|
||||||
error("eof")
|
error("eof")
|
||||||
@ -259,6 +236,27 @@ function bit_converter.bytes_to_uint16(bytes)
|
|||||||
bytes[2], 0)
|
bytes[2], 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function bit_converter.bytes_to_int64(bytes)
|
||||||
|
if #bytes < 8 then
|
||||||
|
error("eof")
|
||||||
|
end
|
||||||
|
return
|
||||||
|
bit.bor(
|
||||||
|
bit.bor(
|
||||||
|
bit.bor(
|
||||||
|
bit.bor(
|
||||||
|
bit.bor(
|
||||||
|
bit.bor(
|
||||||
|
bit.bor(
|
||||||
|
bit.lshift(bytes[1], 56),
|
||||||
|
bit.lshift(bytes[2], 48)),
|
||||||
|
bit.lshift(bytes[3], 40)),
|
||||||
|
bit.lshift(bytes[4], 32)),
|
||||||
|
bit.lshift(bytes[5], 24)),
|
||||||
|
bit.lshift(bit.band(bytes[6], 0xFF), 16)),
|
||||||
|
bit.lshift(bit.band(bytes[7], 0xFF), 8)),bit.band(bytes[8], 0xFF))
|
||||||
|
end
|
||||||
|
|
||||||
function bit_converter.bytes_to_int32(bytes)
|
function bit_converter.bytes_to_int32(bytes)
|
||||||
return bit_converter.bytes_to_uint32(bytes) - MAX_INT32
|
return bit_converter.bytes_to_uint32(bytes) - MAX_INT32
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,5 +1,25 @@
|
|||||||
local bit_converter = require "core:bit_converter"
|
local bit_converter = require "core:bit_converter"
|
||||||
|
|
||||||
|
local MAX_UINT16 = 65535
|
||||||
|
local MIN_UINT16 = 0
|
||||||
|
local MAX_UINT32 = 4294967295
|
||||||
|
local MIN_UINT32 = 0
|
||||||
|
|
||||||
|
local MAX_INT16 = 32767
|
||||||
|
local MIN_INT16 = -32768
|
||||||
|
local MAX_INT32 = 2147483647
|
||||||
|
local MIN_INT32 = -2147483648
|
||||||
|
local MAX_INT64 = 9223372036854775807
|
||||||
|
local MIN_INT64 = -9223372036854775808
|
||||||
|
|
||||||
|
local TYPE_ZERO = 0
|
||||||
|
local TYPE_UINT16 = 1
|
||||||
|
local TYPE_UINT32 = 2
|
||||||
|
local TYPE_INT16 = 3
|
||||||
|
local TYPE_INT32 = 4
|
||||||
|
local TYPE_INT64 = 5
|
||||||
|
local TYPE_DOUBLE = 6
|
||||||
|
|
||||||
-- Data buffer
|
-- Data buffer
|
||||||
|
|
||||||
local data_buffer = { }
|
local data_buffer = { }
|
||||||
@ -62,6 +82,44 @@ function data_buffer:put_int64(int64)
|
|||||||
self:put_bytes(bit_converter.int64_to_bytes(int64))
|
self:put_bytes(bit_converter.int64_to_bytes(int64))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function data_buffer:put_number(num)
|
||||||
|
local bytes
|
||||||
|
local type
|
||||||
|
|
||||||
|
if math.floor(num) ~= num then
|
||||||
|
type = TYPE_DOUBLE
|
||||||
|
bytes = bit_converter.double_to_bytes(num)
|
||||||
|
elseif num == 0 then
|
||||||
|
type = TYPE_ZERO
|
||||||
|
bytes = { }
|
||||||
|
elseif num > 0 then
|
||||||
|
if num <= MAX_UINT16 then
|
||||||
|
type = TYPE_UINT16
|
||||||
|
bytes = bit_converter.uint16_to_bytes(num)
|
||||||
|
elseif num <= MAX_UINT32 then
|
||||||
|
type = TYPE_UINT32
|
||||||
|
bytes = bit_converter.uint32_to_bytes(num)
|
||||||
|
elseif num <= MAX_INT64 then
|
||||||
|
type = TYPE_INT64
|
||||||
|
bytes = bit_converter.int64_to_bytes(num)
|
||||||
|
end
|
||||||
|
elseif num < 0 then
|
||||||
|
if num >= MIN_INT16 then
|
||||||
|
type = TYPE_INT16
|
||||||
|
bytes = bit_converter.int16_to_bytes(num)
|
||||||
|
elseif num >= MIN_INT32 then
|
||||||
|
type = TYPE_INT32
|
||||||
|
bytes = bit_converter.int32_to_bytes(num)
|
||||||
|
elseif num >= MIN_INT64 then
|
||||||
|
type = TYPE_INT64
|
||||||
|
bytes = bit_converter.int64_to_bytes(num)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self:put_byte(type)
|
||||||
|
self:put_bytes(bytes)
|
||||||
|
end
|
||||||
|
|
||||||
-- Get functions
|
-- Get functions
|
||||||
|
|
||||||
function data_buffer:get_byte()
|
function data_buffer:get_byte()
|
||||||
@ -70,6 +128,28 @@ function data_buffer:get_byte()
|
|||||||
return byte
|
return byte
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function data_buffer:get_number()
|
||||||
|
local type = self:get_byte()
|
||||||
|
|
||||||
|
if type == TYPE_ZERO then
|
||||||
|
return 0
|
||||||
|
elseif type == TYPE_UINT16 then
|
||||||
|
return self:get_uint16()
|
||||||
|
elseif type == TYPE_UINT32 then
|
||||||
|
return self:get_uint32()
|
||||||
|
elseif type == TYPE_INT16 then
|
||||||
|
return self:get_int16()
|
||||||
|
elseif type == TYPE_INT32 then
|
||||||
|
return self:get_int32()
|
||||||
|
elseif type == TYPE_INT64 then
|
||||||
|
return self:get_int64()
|
||||||
|
elseif type == TYPE_DOUBLE then
|
||||||
|
return self:get_double()
|
||||||
|
else
|
||||||
|
error("unknown lua number type: "..type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function data_buffer:get_single()
|
function data_buffer:get_single()
|
||||||
return bit_converter.bytes_to_single(self:get_bytes(4))
|
return bit_converter.bytes_to_single(self:get_bytes(4))
|
||||||
end
|
end
|
||||||
@ -79,8 +159,8 @@ function data_buffer:get_double()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function data_buffer:get_string()
|
function data_buffer:get_string()
|
||||||
local len = self:get_bytes(4)
|
local len = self:get_bytes(2)
|
||||||
local str = self:get_bytes(bit_converter.bytes_to_int32(len))
|
local str = self:get_bytes(bit_converter.bytes_to_uint16(len))
|
||||||
local bytes = { }
|
local bytes = { }
|
||||||
|
|
||||||
for i = 1, #len do
|
for i = 1, #len do
|
||||||
@ -98,6 +178,14 @@ function data_buffer:get_bool()
|
|||||||
return bit_converter.byte_to_bool(self:get_byte())
|
return bit_converter.byte_to_bool(self:get_byte())
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function data_buffer:get_uint16()
|
||||||
|
return bit_converter.bytes_to_uint16(self:get_bytes(2))
|
||||||
|
end
|
||||||
|
|
||||||
|
function data_buffer:get_uint32()
|
||||||
|
return bit_converter.bytes_to_uint32(self:get_bytes(4))
|
||||||
|
end
|
||||||
|
|
||||||
function data_buffer:get_int16()
|
function data_buffer:get_int16()
|
||||||
return bit_converter.bytes_to_int16(self:get_bytes(2))
|
return bit_converter.bytes_to_int16(self:get_bytes(2))
|
||||||
end
|
end
|
||||||
@ -114,8 +202,8 @@ function data_buffer:size()
|
|||||||
return #self.bytes
|
return #self.bytes
|
||||||
end
|
end
|
||||||
|
|
||||||
function data_buffer:get_bytes(len)
|
function data_buffer:get_bytes(n)
|
||||||
if len == nil then
|
if n == nil then
|
||||||
return self.bytes
|
return self.bytes
|
||||||
else
|
else
|
||||||
local bytes = { }
|
local bytes = { }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user