From e0db9dc96086f51c68bf8c7ed55fb166dc5a5e83 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/websocket') 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 0cd290671aee99100d0ca323b87ca4e62bb310cb 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/websocket') 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 9f7a18228f9442e63cd1183d951bf26f68031a2e 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/websocket') 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 98b2e87b9d013b64a9cc095d737b4431e9d4dea7 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/websocket') 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 7bff4f91287f17c1e6e6c4ba7275e325aae45f2d 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/websocket') 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 68c49f314ec7bcac01bd38c456b93914d298e2d4 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/websocket') 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