From 49fccb11b2adfc764637f0e5e38ee216cb37ba7c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 6 Oct 2015 16:06:22 +0200 Subject: net.websocket.frames: Simplify import of bitlib --- net/websocket/frames.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'net') diff --git a/net/websocket/frames.lua b/net/websocket/frames.lua index fa0e130d..23f18437 100644 --- a/net/websocket/frames.lua +++ b/net/websocket/frames.lua @@ -10,9 +10,7 @@ local softreq = require "util.dependencies".softreq; local log = require "util.logger".init "websocket.frames"; local random_bytes = require "util.random".bytes; -local bit; -pcall(function() bit = require"bit"; end); -bit = bit or softreq"bit32" +local bit = softreq"bit" or softreq"bit32"; if not bit then log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end local band = bit.band; local bor = bit.bor; -- cgit v1.2.3 From fc34f3fdbf8d1a0abe8f6ddc8e0f3bcb83710b8d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 6 Oct 2015 16:26:23 +0200 Subject: net.websocket.frames: Throw an error if no bit lib is found --- net/websocket/frames.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'net') diff --git a/net/websocket/frames.lua b/net/websocket/frames.lua index 23f18437..2ff30736 100644 --- a/net/websocket/frames.lua +++ b/net/websocket/frames.lua @@ -10,8 +10,8 @@ local softreq = require "util.dependencies".softreq; local log = require "util.logger".init "websocket.frames"; local random_bytes = require "util.random".bytes; -local bit = softreq"bit" or softreq"bit32"; -if not bit then log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end +local bit = assert(softreq"bit" or softreq"bit32", + "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); local band = bit.band; local bor = bit.bor; local bxor = bit.bxor; -- cgit v1.2.3 From 5d941b553acfce45e531ac56fa15cda472e728fe Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 6 Oct 2015 16:49:01 +0200 Subject: net.websocket.frames: Link to documentation when bitop is missing --- net/websocket/frames.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'net') diff --git a/net/websocket/frames.lua b/net/websocket/frames.lua index 2ff30736..e8faab2b 100644 --- a/net/websocket/frames.lua +++ b/net/websocket/frames.lua @@ -11,7 +11,7 @@ local log = require "util.logger".init "websocket.frames"; local random_bytes = require "util.random".bytes; local bit = assert(softreq"bit" or softreq"bit32", - "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); + "No bit module found. See https://prosody.im/doc/depends#bitop"); local band = bit.band; local bor = bit.bor; local bxor = bit.bxor; -- cgit v1.2.3 From 2efcb9f82f22b826e3903bb1e014e64570bbb3e5 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 6 Oct 2015 18:03:04 +0200 Subject: net.websocket.frames: Pack and unpack 64bit ints without overflows (lua-bitop/bit32 are 32bit) --- net/websocket/frames.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'net') diff --git a/net/websocket/frames.lua b/net/websocket/frames.lua index e8faab2b..8528ab99 100644 --- a/net/websocket/frames.lua +++ b/net/websocket/frames.lua @@ -30,8 +30,9 @@ end -- FIXME: this may lose precision local function read_uint64be(str, pos) local l1, l2, l3, l4, l5, l6, l7, l8 = s_byte(str, pos, pos+7); - return lshift(l1, 56) + lshift(l2, 48) + lshift(l3, 40) + lshift(l4, 32) - + lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8; + local h = lshift(l1, 24) + lshift(l2, 16) + lshift(l3, 8) + l4; + local l = lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8; + return h * 2^32 + l; end local function pack_uint16be(x) return s_char(rshift(x, 8), band(x, 0xFF)); @@ -40,8 +41,9 @@ local function get_byte(x, n) return band(rshift(x, n), 0xFF); end local function pack_uint64be(x) - return s_char(rshift(x, 56), get_byte(x, 48), get_byte(x, 40), get_byte(x, 32), - get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF)); + local h = band(x / 2^32, 2^32-1); + return s_char(get_byte(h, 24), get_byte(h, 16), get_byte(h, 8), band(h, 0xFF)); + get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF); end local function parse_frame_header(frame) -- cgit v1.2.3 From 0ce785a39ce22e4150c354a4ff4aec26ed6711a4 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 6 Oct 2015 18:05:27 +0200 Subject: net.websocket.frames: Use struct packing in Lua 5.3 or struct lib if available --- net/websocket/frames.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'net') diff --git a/net/websocket/frames.lua b/net/websocket/frames.lua index 8528ab99..95e41479 100644 --- a/net/websocket/frames.lua +++ b/net/websocket/frames.lua @@ -22,6 +22,13 @@ local t_concat = table.concat; local s_byte = string.byte; local s_char= string.char; local s_sub = string.sub; +local s_pack = string.pack; +local s_unpack = string.unpack; + +if not s_pack and softreq"struct" then + s_pack = softreq"struct".pack; + s_unpack = softreq"struct".unpack; +end local function read_uint16be(str, pos) local l1, l2 = s_byte(str, pos, pos+1); @@ -46,6 +53,24 @@ local function pack_uint64be(x) get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF); end +if s_pack then + function pack_uint16be(x) + return s_pack(">I2", x); + end + function pack_uint64be(x) + return s_pack(">I8", x); + end +end + +if s_unpack then + function read_uint16be(str, pos) + return s_unpack(">I2", str, pos); + end + function read_uint64be(str, pos) + return s_unpack(">I8", str, pos); + end +end + local function parse_frame_header(frame) if #frame < 2 then return; end -- cgit v1.2.3 From b75d87f63d7b0ccbdbdc1e20bac5b4759caaf494 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 6 Oct 2015 18:08:58 +0200 Subject: net.websocket.frames: Fix syntax error due to code copy pasting --- net/websocket/frames.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'net') diff --git a/net/websocket/frames.lua b/net/websocket/frames.lua index 95e41479..737f46bb 100644 --- a/net/websocket/frames.lua +++ b/net/websocket/frames.lua @@ -49,8 +49,8 @@ local function get_byte(x, n) end local function pack_uint64be(x) local h = band(x / 2^32, 2^32-1); - return s_char(get_byte(h, 24), get_byte(h, 16), get_byte(h, 8), band(h, 0xFF)); - get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF); + return s_char(get_byte(h, 24), get_byte(h, 16), get_byte(h, 8), band(h, 0xFF), + get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF)); end if s_pack then -- cgit v1.2.3